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 bool AppliedToPointee = false; 74 DSAVarData() = default; 75 DSAVarData(OpenMPDirectiveKind DKind, OpenMPClauseKind CKind, 76 const Expr *RefExpr, DeclRefExpr *PrivateCopy, 77 SourceLocation ImplicitDSALoc, unsigned Modifier, 78 bool AppliedToPointee) 79 : DKind(DKind), CKind(CKind), Modifier(Modifier), RefExpr(RefExpr), 80 PrivateCopy(PrivateCopy), ImplicitDSALoc(ImplicitDSALoc), 81 AppliedToPointee(AppliedToPointee) {} 82 }; 83 using OperatorOffsetTy = 84 llvm::SmallVector<std::pair<Expr *, OverloadedOperatorKind>, 4>; 85 using DoacrossDependMapTy = 86 llvm::DenseMap<OMPDependClause *, OperatorOffsetTy>; 87 /// Kind of the declaration used in the uses_allocators clauses. 88 enum class UsesAllocatorsDeclKind { 89 /// Predefined allocator 90 PredefinedAllocator, 91 /// User-defined allocator 92 UserDefinedAllocator, 93 /// The declaration that represent allocator trait 94 AllocatorTrait, 95 }; 96 97 private: 98 struct DSAInfo { 99 OpenMPClauseKind Attributes = OMPC_unknown; 100 unsigned Modifier = 0; 101 /// Pointer to a reference expression and a flag which shows that the 102 /// variable is marked as lastprivate(true) or not (false). 103 llvm::PointerIntPair<const Expr *, 1, bool> RefExpr; 104 DeclRefExpr *PrivateCopy = nullptr; 105 /// true if the attribute is applied to the pointee, not the variable 106 /// itself. 107 bool AppliedToPointee = false; 108 }; 109 using DeclSAMapTy = llvm::SmallDenseMap<const ValueDecl *, DSAInfo, 8>; 110 using UsedRefMapTy = llvm::SmallDenseMap<const ValueDecl *, const Expr *, 8>; 111 using LCDeclInfo = std::pair<unsigned, VarDecl *>; 112 using LoopControlVariablesMapTy = 113 llvm::SmallDenseMap<const ValueDecl *, LCDeclInfo, 8>; 114 /// Struct that associates a component with the clause kind where they are 115 /// found. 116 struct MappedExprComponentTy { 117 OMPClauseMappableExprCommon::MappableExprComponentLists Components; 118 OpenMPClauseKind Kind = OMPC_unknown; 119 }; 120 using MappedExprComponentsTy = 121 llvm::DenseMap<const ValueDecl *, MappedExprComponentTy>; 122 using CriticalsWithHintsTy = 123 llvm::StringMap<std::pair<const OMPCriticalDirective *, llvm::APSInt>>; 124 struct ReductionData { 125 using BOKPtrType = llvm::PointerEmbeddedInt<BinaryOperatorKind, 16>; 126 SourceRange ReductionRange; 127 llvm::PointerUnion<const Expr *, BOKPtrType> ReductionOp; 128 ReductionData() = default; 129 void set(BinaryOperatorKind BO, SourceRange RR) { 130 ReductionRange = RR; 131 ReductionOp = BO; 132 } 133 void set(const Expr *RefExpr, SourceRange RR) { 134 ReductionRange = RR; 135 ReductionOp = RefExpr; 136 } 137 }; 138 using DeclReductionMapTy = 139 llvm::SmallDenseMap<const ValueDecl *, ReductionData, 4>; 140 struct DefaultmapInfo { 141 OpenMPDefaultmapClauseModifier ImplicitBehavior = 142 OMPC_DEFAULTMAP_MODIFIER_unknown; 143 SourceLocation SLoc; 144 DefaultmapInfo() = default; 145 DefaultmapInfo(OpenMPDefaultmapClauseModifier M, SourceLocation Loc) 146 : ImplicitBehavior(M), SLoc(Loc) {} 147 }; 148 149 struct SharingMapTy { 150 DeclSAMapTy SharingMap; 151 DeclReductionMapTy ReductionMap; 152 UsedRefMapTy AlignedMap; 153 UsedRefMapTy NontemporalMap; 154 MappedExprComponentsTy MappedExprComponents; 155 LoopControlVariablesMapTy LCVMap; 156 DefaultDataSharingAttributes DefaultAttr = DSA_unspecified; 157 SourceLocation DefaultAttrLoc; 158 DefaultmapInfo DefaultmapMap[OMPC_DEFAULTMAP_unknown]; 159 OpenMPDirectiveKind Directive = OMPD_unknown; 160 DeclarationNameInfo DirectiveName; 161 Scope *CurScope = nullptr; 162 SourceLocation ConstructLoc; 163 /// Set of 'depend' clauses with 'sink|source' dependence kind. Required to 164 /// get the data (loop counters etc.) about enclosing loop-based construct. 165 /// This data is required during codegen. 166 DoacrossDependMapTy DoacrossDepends; 167 /// First argument (Expr *) contains optional argument of the 168 /// 'ordered' clause, the second one is true if the regions has 'ordered' 169 /// clause, false otherwise. 170 llvm::Optional<std::pair<const Expr *, OMPOrderedClause *>> OrderedRegion; 171 unsigned AssociatedLoops = 1; 172 bool HasMutipleLoops = false; 173 const Decl *PossiblyLoopCounter = nullptr; 174 bool NowaitRegion = false; 175 bool CancelRegion = false; 176 bool LoopStart = false; 177 bool BodyComplete = false; 178 SourceLocation PrevScanLocation; 179 SourceLocation PrevOrderedLocation; 180 SourceLocation InnerTeamsRegionLoc; 181 /// Reference to the taskgroup task_reduction reference expression. 182 Expr *TaskgroupReductionRef = nullptr; 183 llvm::DenseSet<QualType> MappedClassesQualTypes; 184 SmallVector<Expr *, 4> InnerUsedAllocators; 185 llvm::DenseSet<CanonicalDeclPtr<Decl>> ImplicitTaskFirstprivates; 186 /// List of globals marked as declare target link in this target region 187 /// (isOpenMPTargetExecutionDirective(Directive) == true). 188 llvm::SmallVector<DeclRefExpr *, 4> DeclareTargetLinkVarDecls; 189 /// List of decls used in inclusive/exclusive clauses of the scan directive. 190 llvm::DenseSet<CanonicalDeclPtr<Decl>> UsedInScanDirective; 191 llvm::DenseMap<CanonicalDeclPtr<const Decl>, UsesAllocatorsDeclKind> 192 UsesAllocatorsDecls; 193 Expr *DeclareMapperVar = nullptr; 194 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name, 195 Scope *CurScope, SourceLocation Loc) 196 : Directive(DKind), DirectiveName(Name), CurScope(CurScope), 197 ConstructLoc(Loc) {} 198 SharingMapTy() = default; 199 }; 200 201 using StackTy = SmallVector<SharingMapTy, 4>; 202 203 /// Stack of used declaration and their data-sharing attributes. 204 DeclSAMapTy Threadprivates; 205 const FunctionScopeInfo *CurrentNonCapturingFunctionScope = nullptr; 206 SmallVector<std::pair<StackTy, const FunctionScopeInfo *>, 4> Stack; 207 /// true, if check for DSA must be from parent directive, false, if 208 /// from current directive. 209 OpenMPClauseKind ClauseKindMode = OMPC_unknown; 210 Sema &SemaRef; 211 bool ForceCapturing = false; 212 /// true if all the variables in the target executable directives must be 213 /// captured by reference. 214 bool ForceCaptureByReferenceInTargetExecutable = false; 215 CriticalsWithHintsTy Criticals; 216 unsigned IgnoredStackElements = 0; 217 218 /// Iterators over the stack iterate in order from innermost to outermost 219 /// directive. 220 using const_iterator = StackTy::const_reverse_iterator; 221 const_iterator begin() const { 222 return Stack.empty() ? const_iterator() 223 : Stack.back().first.rbegin() + IgnoredStackElements; 224 } 225 const_iterator end() const { 226 return Stack.empty() ? const_iterator() : Stack.back().first.rend(); 227 } 228 using iterator = StackTy::reverse_iterator; 229 iterator begin() { 230 return Stack.empty() ? iterator() 231 : Stack.back().first.rbegin() + IgnoredStackElements; 232 } 233 iterator end() { 234 return Stack.empty() ? iterator() : Stack.back().first.rend(); 235 } 236 237 // Convenience operations to get at the elements of the stack. 238 239 bool isStackEmpty() const { 240 return Stack.empty() || 241 Stack.back().second != CurrentNonCapturingFunctionScope || 242 Stack.back().first.size() <= IgnoredStackElements; 243 } 244 size_t getStackSize() const { 245 return isStackEmpty() ? 0 246 : Stack.back().first.size() - IgnoredStackElements; 247 } 248 249 SharingMapTy *getTopOfStackOrNull() { 250 size_t Size = getStackSize(); 251 if (Size == 0) 252 return nullptr; 253 return &Stack.back().first[Size - 1]; 254 } 255 const SharingMapTy *getTopOfStackOrNull() const { 256 return const_cast<DSAStackTy&>(*this).getTopOfStackOrNull(); 257 } 258 SharingMapTy &getTopOfStack() { 259 assert(!isStackEmpty() && "no current directive"); 260 return *getTopOfStackOrNull(); 261 } 262 const SharingMapTy &getTopOfStack() const { 263 return const_cast<DSAStackTy&>(*this).getTopOfStack(); 264 } 265 266 SharingMapTy *getSecondOnStackOrNull() { 267 size_t Size = getStackSize(); 268 if (Size <= 1) 269 return nullptr; 270 return &Stack.back().first[Size - 2]; 271 } 272 const SharingMapTy *getSecondOnStackOrNull() const { 273 return const_cast<DSAStackTy&>(*this).getSecondOnStackOrNull(); 274 } 275 276 /// Get the stack element at a certain level (previously returned by 277 /// \c getNestingLevel). 278 /// 279 /// Note that nesting levels count from outermost to innermost, and this is 280 /// the reverse of our iteration order where new inner levels are pushed at 281 /// the front of the stack. 282 SharingMapTy &getStackElemAtLevel(unsigned Level) { 283 assert(Level < getStackSize() && "no such stack element"); 284 return Stack.back().first[Level]; 285 } 286 const SharingMapTy &getStackElemAtLevel(unsigned Level) const { 287 return const_cast<DSAStackTy&>(*this).getStackElemAtLevel(Level); 288 } 289 290 DSAVarData getDSA(const_iterator &Iter, ValueDecl *D) const; 291 292 /// Checks if the variable is a local for OpenMP region. 293 bool isOpenMPLocal(VarDecl *D, const_iterator Iter) const; 294 295 /// Vector of previously declared requires directives 296 SmallVector<const OMPRequiresDecl *, 2> RequiresDecls; 297 /// omp_allocator_handle_t type. 298 QualType OMPAllocatorHandleT; 299 /// omp_depend_t type. 300 QualType OMPDependT; 301 /// omp_event_handle_t type. 302 QualType OMPEventHandleT; 303 /// omp_alloctrait_t type. 304 QualType OMPAlloctraitT; 305 /// Expression for the predefined allocators. 306 Expr *OMPPredefinedAllocators[OMPAllocateDeclAttr::OMPUserDefinedMemAlloc] = { 307 nullptr}; 308 /// Vector of previously encountered target directives 309 SmallVector<SourceLocation, 2> TargetLocations; 310 SourceLocation AtomicLocation; 311 312 public: 313 explicit DSAStackTy(Sema &S) : SemaRef(S) {} 314 315 /// Sets omp_allocator_handle_t type. 316 void setOMPAllocatorHandleT(QualType Ty) { OMPAllocatorHandleT = Ty; } 317 /// Gets omp_allocator_handle_t type. 318 QualType getOMPAllocatorHandleT() const { return OMPAllocatorHandleT; } 319 /// Sets omp_alloctrait_t type. 320 void setOMPAlloctraitT(QualType Ty) { OMPAlloctraitT = Ty; } 321 /// Gets omp_alloctrait_t type. 322 QualType getOMPAlloctraitT() const { return OMPAlloctraitT; } 323 /// Sets the given default allocator. 324 void setAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, 325 Expr *Allocator) { 326 OMPPredefinedAllocators[AllocatorKind] = Allocator; 327 } 328 /// Returns the specified default allocator. 329 Expr *getAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind) const { 330 return OMPPredefinedAllocators[AllocatorKind]; 331 } 332 /// Sets omp_depend_t type. 333 void setOMPDependT(QualType Ty) { OMPDependT = Ty; } 334 /// Gets omp_depend_t type. 335 QualType getOMPDependT() const { return OMPDependT; } 336 337 /// Sets omp_event_handle_t type. 338 void setOMPEventHandleT(QualType Ty) { OMPEventHandleT = Ty; } 339 /// Gets omp_event_handle_t type. 340 QualType getOMPEventHandleT() const { return OMPEventHandleT; } 341 342 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; } 343 OpenMPClauseKind getClauseParsingMode() const { 344 assert(isClauseParsingMode() && "Must be in clause parsing mode."); 345 return ClauseKindMode; 346 } 347 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; } 348 349 bool isBodyComplete() const { 350 const SharingMapTy *Top = getTopOfStackOrNull(); 351 return Top && Top->BodyComplete; 352 } 353 void setBodyComplete() { 354 getTopOfStack().BodyComplete = true; 355 } 356 357 bool isForceVarCapturing() const { return ForceCapturing; } 358 void setForceVarCapturing(bool V) { ForceCapturing = V; } 359 360 void setForceCaptureByReferenceInTargetExecutable(bool V) { 361 ForceCaptureByReferenceInTargetExecutable = V; 362 } 363 bool isForceCaptureByReferenceInTargetExecutable() const { 364 return ForceCaptureByReferenceInTargetExecutable; 365 } 366 367 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName, 368 Scope *CurScope, SourceLocation Loc) { 369 assert(!IgnoredStackElements && 370 "cannot change stack while ignoring elements"); 371 if (Stack.empty() || 372 Stack.back().second != CurrentNonCapturingFunctionScope) 373 Stack.emplace_back(StackTy(), CurrentNonCapturingFunctionScope); 374 Stack.back().first.emplace_back(DKind, DirName, CurScope, Loc); 375 Stack.back().first.back().DefaultAttrLoc = Loc; 376 } 377 378 void pop() { 379 assert(!IgnoredStackElements && 380 "cannot change stack while ignoring elements"); 381 assert(!Stack.back().first.empty() && 382 "Data-sharing attributes stack is empty!"); 383 Stack.back().first.pop_back(); 384 } 385 386 /// RAII object to temporarily leave the scope of a directive when we want to 387 /// logically operate in its parent. 388 class ParentDirectiveScope { 389 DSAStackTy &Self; 390 bool Active; 391 public: 392 ParentDirectiveScope(DSAStackTy &Self, bool Activate) 393 : Self(Self), Active(false) { 394 if (Activate) 395 enable(); 396 } 397 ~ParentDirectiveScope() { disable(); } 398 void disable() { 399 if (Active) { 400 --Self.IgnoredStackElements; 401 Active = false; 402 } 403 } 404 void enable() { 405 if (!Active) { 406 ++Self.IgnoredStackElements; 407 Active = true; 408 } 409 } 410 }; 411 412 /// Marks that we're started loop parsing. 413 void loopInit() { 414 assert(isOpenMPLoopDirective(getCurrentDirective()) && 415 "Expected loop-based directive."); 416 getTopOfStack().LoopStart = true; 417 } 418 /// Start capturing of the variables in the loop context. 419 void loopStart() { 420 assert(isOpenMPLoopDirective(getCurrentDirective()) && 421 "Expected loop-based directive."); 422 getTopOfStack().LoopStart = false; 423 } 424 /// true, if variables are captured, false otherwise. 425 bool isLoopStarted() const { 426 assert(isOpenMPLoopDirective(getCurrentDirective()) && 427 "Expected loop-based directive."); 428 return !getTopOfStack().LoopStart; 429 } 430 /// Marks (or clears) declaration as possibly loop counter. 431 void resetPossibleLoopCounter(const Decl *D = nullptr) { 432 getTopOfStack().PossiblyLoopCounter = 433 D ? D->getCanonicalDecl() : D; 434 } 435 /// Gets the possible loop counter decl. 436 const Decl *getPossiblyLoopCunter() const { 437 return getTopOfStack().PossiblyLoopCounter; 438 } 439 /// Start new OpenMP region stack in new non-capturing function. 440 void pushFunction() { 441 assert(!IgnoredStackElements && 442 "cannot change stack while ignoring elements"); 443 const FunctionScopeInfo *CurFnScope = SemaRef.getCurFunction(); 444 assert(!isa<CapturingScopeInfo>(CurFnScope)); 445 CurrentNonCapturingFunctionScope = CurFnScope; 446 } 447 /// Pop region stack for non-capturing function. 448 void popFunction(const FunctionScopeInfo *OldFSI) { 449 assert(!IgnoredStackElements && 450 "cannot change stack while ignoring elements"); 451 if (!Stack.empty() && Stack.back().second == OldFSI) { 452 assert(Stack.back().first.empty()); 453 Stack.pop_back(); 454 } 455 CurrentNonCapturingFunctionScope = nullptr; 456 for (const FunctionScopeInfo *FSI : llvm::reverse(SemaRef.FunctionScopes)) { 457 if (!isa<CapturingScopeInfo>(FSI)) { 458 CurrentNonCapturingFunctionScope = FSI; 459 break; 460 } 461 } 462 } 463 464 void addCriticalWithHint(const OMPCriticalDirective *D, llvm::APSInt Hint) { 465 Criticals.try_emplace(D->getDirectiveName().getAsString(), D, Hint); 466 } 467 const std::pair<const OMPCriticalDirective *, llvm::APSInt> 468 getCriticalWithHint(const DeclarationNameInfo &Name) const { 469 auto I = Criticals.find(Name.getAsString()); 470 if (I != Criticals.end()) 471 return I->second; 472 return std::make_pair(nullptr, llvm::APSInt()); 473 } 474 /// If 'aligned' declaration for given variable \a D was not seen yet, 475 /// add it and return NULL; otherwise return previous occurrence's expression 476 /// for diagnostics. 477 const Expr *addUniqueAligned(const ValueDecl *D, const Expr *NewDE); 478 /// If 'nontemporal' declaration for given variable \a D was not seen yet, 479 /// add it and return NULL; otherwise return previous occurrence's expression 480 /// for diagnostics. 481 const Expr *addUniqueNontemporal(const ValueDecl *D, const Expr *NewDE); 482 483 /// Register specified variable as loop control variable. 484 void addLoopControlVariable(const ValueDecl *D, VarDecl *Capture); 485 /// Check if the specified variable is a loop control variable for 486 /// current region. 487 /// \return The index of the loop control variable in the list of associated 488 /// for-loops (from outer to inner). 489 const LCDeclInfo isLoopControlVariable(const ValueDecl *D) const; 490 /// Check if the specified variable is a loop control variable for 491 /// parent region. 492 /// \return The index of the loop control variable in the list of associated 493 /// for-loops (from outer to inner). 494 const LCDeclInfo isParentLoopControlVariable(const ValueDecl *D) const; 495 /// Check if the specified variable is a loop control variable for 496 /// current region. 497 /// \return The index of the loop control variable in the list of associated 498 /// for-loops (from outer to inner). 499 const LCDeclInfo isLoopControlVariable(const ValueDecl *D, 500 unsigned Level) const; 501 /// Get the loop control variable for the I-th loop (or nullptr) in 502 /// parent directive. 503 const ValueDecl *getParentLoopControlVariable(unsigned I) const; 504 505 /// Marks the specified decl \p D as used in scan directive. 506 void markDeclAsUsedInScanDirective(ValueDecl *D) { 507 if (SharingMapTy *Stack = getSecondOnStackOrNull()) 508 Stack->UsedInScanDirective.insert(D); 509 } 510 511 /// Checks if the specified declaration was used in the inner scan directive. 512 bool isUsedInScanDirective(ValueDecl *D) const { 513 if (const SharingMapTy *Stack = getTopOfStackOrNull()) 514 return Stack->UsedInScanDirective.count(D) > 0; 515 return false; 516 } 517 518 /// Adds explicit data sharing attribute to the specified declaration. 519 void addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A, 520 DeclRefExpr *PrivateCopy = nullptr, unsigned Modifier = 0, 521 bool AppliedToPointee = false); 522 523 /// Adds additional information for the reduction items with the reduction id 524 /// represented as an operator. 525 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 526 BinaryOperatorKind BOK); 527 /// Adds additional information for the reduction items with the reduction id 528 /// represented as reduction identifier. 529 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 530 const Expr *ReductionRef); 531 /// Returns the location and reduction operation from the innermost parent 532 /// region for the given \p D. 533 const DSAVarData 534 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR, 535 BinaryOperatorKind &BOK, 536 Expr *&TaskgroupDescriptor) const; 537 /// Returns the location and reduction operation from the innermost parent 538 /// region for the given \p D. 539 const DSAVarData 540 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR, 541 const Expr *&ReductionRef, 542 Expr *&TaskgroupDescriptor) const; 543 /// Return reduction reference expression for the current taskgroup or 544 /// parallel/worksharing directives with task reductions. 545 Expr *getTaskgroupReductionRef() const { 546 assert((getTopOfStack().Directive == OMPD_taskgroup || 547 ((isOpenMPParallelDirective(getTopOfStack().Directive) || 548 isOpenMPWorksharingDirective(getTopOfStack().Directive)) && 549 !isOpenMPSimdDirective(getTopOfStack().Directive))) && 550 "taskgroup reference expression requested for non taskgroup or " 551 "parallel/worksharing directive."); 552 return getTopOfStack().TaskgroupReductionRef; 553 } 554 /// Checks if the given \p VD declaration is actually a taskgroup reduction 555 /// descriptor variable at the \p Level of OpenMP regions. 556 bool isTaskgroupReductionRef(const ValueDecl *VD, unsigned Level) const { 557 return getStackElemAtLevel(Level).TaskgroupReductionRef && 558 cast<DeclRefExpr>(getStackElemAtLevel(Level).TaskgroupReductionRef) 559 ->getDecl() == VD; 560 } 561 562 /// Returns data sharing attributes from top of the stack for the 563 /// specified declaration. 564 const DSAVarData getTopDSA(ValueDecl *D, bool FromParent); 565 /// Returns data-sharing attributes for the specified declaration. 566 const DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent) const; 567 /// Returns data-sharing attributes for the specified declaration. 568 const DSAVarData getImplicitDSA(ValueDecl *D, unsigned Level) const; 569 /// Checks if the specified variables has data-sharing attributes which 570 /// match specified \a CPred predicate in any directive which matches \a DPred 571 /// predicate. 572 const DSAVarData 573 hasDSA(ValueDecl *D, 574 const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred, 575 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 576 bool FromParent) const; 577 /// Checks if the specified variables has data-sharing attributes which 578 /// match specified \a CPred predicate in any innermost directive which 579 /// matches \a DPred predicate. 580 const DSAVarData 581 hasInnermostDSA(ValueDecl *D, 582 const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred, 583 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 584 bool FromParent) const; 585 /// Checks if the specified variables has explicit data-sharing 586 /// attributes which match specified \a CPred predicate at the specified 587 /// OpenMP region. 588 bool 589 hasExplicitDSA(const ValueDecl *D, 590 const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred, 591 unsigned Level, bool NotLastprivate = false) const; 592 593 /// Returns true if the directive at level \Level matches in the 594 /// specified \a DPred predicate. 595 bool hasExplicitDirective( 596 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 597 unsigned Level) const; 598 599 /// Finds a directive which matches specified \a DPred predicate. 600 bool hasDirective( 601 const llvm::function_ref<bool( 602 OpenMPDirectiveKind, const DeclarationNameInfo &, SourceLocation)> 603 DPred, 604 bool FromParent) const; 605 606 /// Returns currently analyzed directive. 607 OpenMPDirectiveKind getCurrentDirective() const { 608 const SharingMapTy *Top = getTopOfStackOrNull(); 609 return Top ? Top->Directive : OMPD_unknown; 610 } 611 /// Returns directive kind at specified level. 612 OpenMPDirectiveKind getDirective(unsigned Level) const { 613 assert(!isStackEmpty() && "No directive at specified level."); 614 return getStackElemAtLevel(Level).Directive; 615 } 616 /// Returns the capture region at the specified level. 617 OpenMPDirectiveKind getCaptureRegion(unsigned Level, 618 unsigned OpenMPCaptureLevel) const { 619 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 620 getOpenMPCaptureRegions(CaptureRegions, getDirective(Level)); 621 return CaptureRegions[OpenMPCaptureLevel]; 622 } 623 /// Returns parent directive. 624 OpenMPDirectiveKind getParentDirective() const { 625 const SharingMapTy *Parent = getSecondOnStackOrNull(); 626 return Parent ? Parent->Directive : OMPD_unknown; 627 } 628 629 /// Add requires decl to internal vector 630 void addRequiresDecl(OMPRequiresDecl *RD) { 631 RequiresDecls.push_back(RD); 632 } 633 634 /// Checks if the defined 'requires' directive has specified type of clause. 635 template <typename ClauseType> 636 bool hasRequiresDeclWithClause() const { 637 return llvm::any_of(RequiresDecls, [](const OMPRequiresDecl *D) { 638 return llvm::any_of(D->clauselists(), [](const OMPClause *C) { 639 return isa<ClauseType>(C); 640 }); 641 }); 642 } 643 644 /// Checks for a duplicate clause amongst previously declared requires 645 /// directives 646 bool hasDuplicateRequiresClause(ArrayRef<OMPClause *> ClauseList) const { 647 bool IsDuplicate = false; 648 for (OMPClause *CNew : ClauseList) { 649 for (const OMPRequiresDecl *D : RequiresDecls) { 650 for (const OMPClause *CPrev : D->clauselists()) { 651 if (CNew->getClauseKind() == CPrev->getClauseKind()) { 652 SemaRef.Diag(CNew->getBeginLoc(), 653 diag::err_omp_requires_clause_redeclaration) 654 << getOpenMPClauseName(CNew->getClauseKind()); 655 SemaRef.Diag(CPrev->getBeginLoc(), 656 diag::note_omp_requires_previous_clause) 657 << getOpenMPClauseName(CPrev->getClauseKind()); 658 IsDuplicate = true; 659 } 660 } 661 } 662 } 663 return IsDuplicate; 664 } 665 666 /// Add location of previously encountered target to internal vector 667 void addTargetDirLocation(SourceLocation LocStart) { 668 TargetLocations.push_back(LocStart); 669 } 670 671 /// Add location for the first encountered atomicc directive. 672 void addAtomicDirectiveLoc(SourceLocation Loc) { 673 if (AtomicLocation.isInvalid()) 674 AtomicLocation = Loc; 675 } 676 677 /// Returns the location of the first encountered atomic directive in the 678 /// module. 679 SourceLocation getAtomicDirectiveLoc() const { 680 return AtomicLocation; 681 } 682 683 // Return previously encountered target region locations. 684 ArrayRef<SourceLocation> getEncounteredTargetLocs() const { 685 return TargetLocations; 686 } 687 688 /// Set default data sharing attribute to none. 689 void setDefaultDSANone(SourceLocation Loc) { 690 getTopOfStack().DefaultAttr = DSA_none; 691 getTopOfStack().DefaultAttrLoc = Loc; 692 } 693 /// Set default data sharing attribute to shared. 694 void setDefaultDSAShared(SourceLocation Loc) { 695 getTopOfStack().DefaultAttr = DSA_shared; 696 getTopOfStack().DefaultAttrLoc = Loc; 697 } 698 /// Set default data sharing attribute to firstprivate. 699 void setDefaultDSAFirstPrivate(SourceLocation Loc) { 700 getTopOfStack().DefaultAttr = DSA_firstprivate; 701 getTopOfStack().DefaultAttrLoc = Loc; 702 } 703 /// Set default data mapping attribute to Modifier:Kind 704 void setDefaultDMAAttr(OpenMPDefaultmapClauseModifier M, 705 OpenMPDefaultmapClauseKind Kind, 706 SourceLocation Loc) { 707 DefaultmapInfo &DMI = getTopOfStack().DefaultmapMap[Kind]; 708 DMI.ImplicitBehavior = M; 709 DMI.SLoc = Loc; 710 } 711 /// Check whether the implicit-behavior has been set in defaultmap 712 bool checkDefaultmapCategory(OpenMPDefaultmapClauseKind VariableCategory) { 713 if (VariableCategory == OMPC_DEFAULTMAP_unknown) 714 return getTopOfStack() 715 .DefaultmapMap[OMPC_DEFAULTMAP_aggregate] 716 .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown || 717 getTopOfStack() 718 .DefaultmapMap[OMPC_DEFAULTMAP_scalar] 719 .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown || 720 getTopOfStack() 721 .DefaultmapMap[OMPC_DEFAULTMAP_pointer] 722 .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown; 723 return getTopOfStack().DefaultmapMap[VariableCategory].ImplicitBehavior != 724 OMPC_DEFAULTMAP_MODIFIER_unknown; 725 } 726 727 DefaultDataSharingAttributes getDefaultDSA(unsigned Level) const { 728 return getStackSize() <= Level ? DSA_unspecified 729 : getStackElemAtLevel(Level).DefaultAttr; 730 } 731 DefaultDataSharingAttributes getDefaultDSA() const { 732 return isStackEmpty() ? DSA_unspecified 733 : getTopOfStack().DefaultAttr; 734 } 735 SourceLocation getDefaultDSALocation() const { 736 return isStackEmpty() ? SourceLocation() 737 : getTopOfStack().DefaultAttrLoc; 738 } 739 OpenMPDefaultmapClauseModifier 740 getDefaultmapModifier(OpenMPDefaultmapClauseKind Kind) const { 741 return isStackEmpty() 742 ? OMPC_DEFAULTMAP_MODIFIER_unknown 743 : getTopOfStack().DefaultmapMap[Kind].ImplicitBehavior; 744 } 745 OpenMPDefaultmapClauseModifier 746 getDefaultmapModifierAtLevel(unsigned Level, 747 OpenMPDefaultmapClauseKind Kind) const { 748 return getStackElemAtLevel(Level).DefaultmapMap[Kind].ImplicitBehavior; 749 } 750 bool isDefaultmapCapturedByRef(unsigned Level, 751 OpenMPDefaultmapClauseKind Kind) const { 752 OpenMPDefaultmapClauseModifier M = 753 getDefaultmapModifierAtLevel(Level, Kind); 754 if (Kind == OMPC_DEFAULTMAP_scalar || Kind == OMPC_DEFAULTMAP_pointer) { 755 return (M == OMPC_DEFAULTMAP_MODIFIER_alloc) || 756 (M == OMPC_DEFAULTMAP_MODIFIER_to) || 757 (M == OMPC_DEFAULTMAP_MODIFIER_from) || 758 (M == OMPC_DEFAULTMAP_MODIFIER_tofrom); 759 } 760 return true; 761 } 762 static bool mustBeFirstprivateBase(OpenMPDefaultmapClauseModifier M, 763 OpenMPDefaultmapClauseKind Kind) { 764 switch (Kind) { 765 case OMPC_DEFAULTMAP_scalar: 766 case OMPC_DEFAULTMAP_pointer: 767 return (M == OMPC_DEFAULTMAP_MODIFIER_unknown) || 768 (M == OMPC_DEFAULTMAP_MODIFIER_firstprivate) || 769 (M == OMPC_DEFAULTMAP_MODIFIER_default); 770 case OMPC_DEFAULTMAP_aggregate: 771 return M == OMPC_DEFAULTMAP_MODIFIER_firstprivate; 772 default: 773 break; 774 } 775 llvm_unreachable("Unexpected OpenMPDefaultmapClauseKind enum"); 776 } 777 bool mustBeFirstprivateAtLevel(unsigned Level, 778 OpenMPDefaultmapClauseKind Kind) const { 779 OpenMPDefaultmapClauseModifier M = 780 getDefaultmapModifierAtLevel(Level, Kind); 781 return mustBeFirstprivateBase(M, Kind); 782 } 783 bool mustBeFirstprivate(OpenMPDefaultmapClauseKind Kind) const { 784 OpenMPDefaultmapClauseModifier M = getDefaultmapModifier(Kind); 785 return mustBeFirstprivateBase(M, Kind); 786 } 787 788 /// Checks if the specified variable is a threadprivate. 789 bool isThreadPrivate(VarDecl *D) { 790 const DSAVarData DVar = getTopDSA(D, false); 791 return isOpenMPThreadPrivate(DVar.CKind); 792 } 793 794 /// Marks current region as ordered (it has an 'ordered' clause). 795 void setOrderedRegion(bool IsOrdered, const Expr *Param, 796 OMPOrderedClause *Clause) { 797 if (IsOrdered) 798 getTopOfStack().OrderedRegion.emplace(Param, Clause); 799 else 800 getTopOfStack().OrderedRegion.reset(); 801 } 802 /// Returns true, if region is ordered (has associated 'ordered' clause), 803 /// false - otherwise. 804 bool isOrderedRegion() const { 805 if (const SharingMapTy *Top = getTopOfStackOrNull()) 806 return Top->OrderedRegion.hasValue(); 807 return false; 808 } 809 /// Returns optional parameter for the ordered region. 810 std::pair<const Expr *, OMPOrderedClause *> getOrderedRegionParam() const { 811 if (const SharingMapTy *Top = getTopOfStackOrNull()) 812 if (Top->OrderedRegion.hasValue()) 813 return Top->OrderedRegion.getValue(); 814 return std::make_pair(nullptr, nullptr); 815 } 816 /// Returns true, if parent region is ordered (has associated 817 /// 'ordered' clause), false - otherwise. 818 bool isParentOrderedRegion() const { 819 if (const SharingMapTy *Parent = getSecondOnStackOrNull()) 820 return Parent->OrderedRegion.hasValue(); 821 return false; 822 } 823 /// Returns optional parameter for the ordered region. 824 std::pair<const Expr *, OMPOrderedClause *> 825 getParentOrderedRegionParam() const { 826 if (const SharingMapTy *Parent = getSecondOnStackOrNull()) 827 if (Parent->OrderedRegion.hasValue()) 828 return Parent->OrderedRegion.getValue(); 829 return std::make_pair(nullptr, nullptr); 830 } 831 /// Marks current region as nowait (it has a 'nowait' clause). 832 void setNowaitRegion(bool IsNowait = true) { 833 getTopOfStack().NowaitRegion = IsNowait; 834 } 835 /// Returns true, if parent region is nowait (has associated 836 /// 'nowait' clause), false - otherwise. 837 bool isParentNowaitRegion() const { 838 if (const SharingMapTy *Parent = getSecondOnStackOrNull()) 839 return Parent->NowaitRegion; 840 return false; 841 } 842 /// Marks parent region as cancel region. 843 void setParentCancelRegion(bool Cancel = true) { 844 if (SharingMapTy *Parent = getSecondOnStackOrNull()) 845 Parent->CancelRegion |= Cancel; 846 } 847 /// Return true if current region has inner cancel construct. 848 bool isCancelRegion() const { 849 const SharingMapTy *Top = getTopOfStackOrNull(); 850 return Top ? Top->CancelRegion : false; 851 } 852 853 /// Mark that parent region already has scan directive. 854 void setParentHasScanDirective(SourceLocation Loc) { 855 if (SharingMapTy *Parent = getSecondOnStackOrNull()) 856 Parent->PrevScanLocation = Loc; 857 } 858 /// Return true if current region has inner cancel construct. 859 bool doesParentHasScanDirective() const { 860 const SharingMapTy *Top = getSecondOnStackOrNull(); 861 return Top ? Top->PrevScanLocation.isValid() : false; 862 } 863 /// Return true if current region has inner cancel construct. 864 SourceLocation getParentScanDirectiveLoc() const { 865 const SharingMapTy *Top = getSecondOnStackOrNull(); 866 return Top ? Top->PrevScanLocation : SourceLocation(); 867 } 868 /// Mark that parent region already has ordered directive. 869 void setParentHasOrderedDirective(SourceLocation Loc) { 870 if (SharingMapTy *Parent = getSecondOnStackOrNull()) 871 Parent->PrevOrderedLocation = Loc; 872 } 873 /// Return true if current region has inner ordered construct. 874 bool doesParentHasOrderedDirective() const { 875 const SharingMapTy *Top = getSecondOnStackOrNull(); 876 return Top ? Top->PrevOrderedLocation.isValid() : false; 877 } 878 /// Returns the location of the previously specified ordered directive. 879 SourceLocation getParentOrderedDirectiveLoc() const { 880 const SharingMapTy *Top = getSecondOnStackOrNull(); 881 return Top ? Top->PrevOrderedLocation : SourceLocation(); 882 } 883 884 /// Set collapse value for the region. 885 void setAssociatedLoops(unsigned Val) { 886 getTopOfStack().AssociatedLoops = Val; 887 if (Val > 1) 888 getTopOfStack().HasMutipleLoops = true; 889 } 890 /// Return collapse value for region. 891 unsigned getAssociatedLoops() const { 892 const SharingMapTy *Top = getTopOfStackOrNull(); 893 return Top ? Top->AssociatedLoops : 0; 894 } 895 /// Returns true if the construct is associated with multiple loops. 896 bool hasMutipleLoops() const { 897 const SharingMapTy *Top = getTopOfStackOrNull(); 898 return Top ? Top->HasMutipleLoops : false; 899 } 900 901 /// Marks current target region as one with closely nested teams 902 /// region. 903 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) { 904 if (SharingMapTy *Parent = getSecondOnStackOrNull()) 905 Parent->InnerTeamsRegionLoc = TeamsRegionLoc; 906 } 907 /// Returns true, if current region has closely nested teams region. 908 bool hasInnerTeamsRegion() const { 909 return getInnerTeamsRegionLoc().isValid(); 910 } 911 /// Returns location of the nested teams region (if any). 912 SourceLocation getInnerTeamsRegionLoc() const { 913 const SharingMapTy *Top = getTopOfStackOrNull(); 914 return Top ? Top->InnerTeamsRegionLoc : SourceLocation(); 915 } 916 917 Scope *getCurScope() const { 918 const SharingMapTy *Top = getTopOfStackOrNull(); 919 return Top ? Top->CurScope : nullptr; 920 } 921 SourceLocation getConstructLoc() const { 922 const SharingMapTy *Top = getTopOfStackOrNull(); 923 return Top ? Top->ConstructLoc : SourceLocation(); 924 } 925 926 /// Do the check specified in \a Check to all component lists and return true 927 /// if any issue is found. 928 bool checkMappableExprComponentListsForDecl( 929 const ValueDecl *VD, bool CurrentRegionOnly, 930 const llvm::function_ref< 931 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef, 932 OpenMPClauseKind)> 933 Check) const { 934 if (isStackEmpty()) 935 return false; 936 auto SI = begin(); 937 auto SE = end(); 938 939 if (SI == SE) 940 return false; 941 942 if (CurrentRegionOnly) 943 SE = std::next(SI); 944 else 945 std::advance(SI, 1); 946 947 for (; SI != SE; ++SI) { 948 auto MI = SI->MappedExprComponents.find(VD); 949 if (MI != SI->MappedExprComponents.end()) 950 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L : 951 MI->second.Components) 952 if (Check(L, MI->second.Kind)) 953 return true; 954 } 955 return false; 956 } 957 958 /// Do the check specified in \a Check to all component lists at a given level 959 /// and return true if any issue is found. 960 bool checkMappableExprComponentListsForDeclAtLevel( 961 const ValueDecl *VD, unsigned Level, 962 const llvm::function_ref< 963 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef, 964 OpenMPClauseKind)> 965 Check) const { 966 if (getStackSize() <= Level) 967 return false; 968 969 const SharingMapTy &StackElem = getStackElemAtLevel(Level); 970 auto MI = StackElem.MappedExprComponents.find(VD); 971 if (MI != StackElem.MappedExprComponents.end()) 972 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L : 973 MI->second.Components) 974 if (Check(L, MI->second.Kind)) 975 return true; 976 return false; 977 } 978 979 /// Create a new mappable expression component list associated with a given 980 /// declaration and initialize it with the provided list of components. 981 void addMappableExpressionComponents( 982 const ValueDecl *VD, 983 OMPClauseMappableExprCommon::MappableExprComponentListRef Components, 984 OpenMPClauseKind WhereFoundClauseKind) { 985 MappedExprComponentTy &MEC = getTopOfStack().MappedExprComponents[VD]; 986 // Create new entry and append the new components there. 987 MEC.Components.resize(MEC.Components.size() + 1); 988 MEC.Components.back().append(Components.begin(), Components.end()); 989 MEC.Kind = WhereFoundClauseKind; 990 } 991 992 unsigned getNestingLevel() const { 993 assert(!isStackEmpty()); 994 return getStackSize() - 1; 995 } 996 void addDoacrossDependClause(OMPDependClause *C, 997 const OperatorOffsetTy &OpsOffs) { 998 SharingMapTy *Parent = getSecondOnStackOrNull(); 999 assert(Parent && isOpenMPWorksharingDirective(Parent->Directive)); 1000 Parent->DoacrossDepends.try_emplace(C, OpsOffs); 1001 } 1002 llvm::iterator_range<DoacrossDependMapTy::const_iterator> 1003 getDoacrossDependClauses() const { 1004 const SharingMapTy &StackElem = getTopOfStack(); 1005 if (isOpenMPWorksharingDirective(StackElem.Directive)) { 1006 const DoacrossDependMapTy &Ref = StackElem.DoacrossDepends; 1007 return llvm::make_range(Ref.begin(), Ref.end()); 1008 } 1009 return llvm::make_range(StackElem.DoacrossDepends.end(), 1010 StackElem.DoacrossDepends.end()); 1011 } 1012 1013 // Store types of classes which have been explicitly mapped 1014 void addMappedClassesQualTypes(QualType QT) { 1015 SharingMapTy &StackElem = getTopOfStack(); 1016 StackElem.MappedClassesQualTypes.insert(QT); 1017 } 1018 1019 // Return set of mapped classes types 1020 bool isClassPreviouslyMapped(QualType QT) const { 1021 const SharingMapTy &StackElem = getTopOfStack(); 1022 return StackElem.MappedClassesQualTypes.count(QT) != 0; 1023 } 1024 1025 /// Adds global declare target to the parent target region. 1026 void addToParentTargetRegionLinkGlobals(DeclRefExpr *E) { 1027 assert(*OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration( 1028 E->getDecl()) == OMPDeclareTargetDeclAttr::MT_Link && 1029 "Expected declare target link global."); 1030 for (auto &Elem : *this) { 1031 if (isOpenMPTargetExecutionDirective(Elem.Directive)) { 1032 Elem.DeclareTargetLinkVarDecls.push_back(E); 1033 return; 1034 } 1035 } 1036 } 1037 1038 /// Returns the list of globals with declare target link if current directive 1039 /// is target. 1040 ArrayRef<DeclRefExpr *> getLinkGlobals() const { 1041 assert(isOpenMPTargetExecutionDirective(getCurrentDirective()) && 1042 "Expected target executable directive."); 1043 return getTopOfStack().DeclareTargetLinkVarDecls; 1044 } 1045 1046 /// Adds list of allocators expressions. 1047 void addInnerAllocatorExpr(Expr *E) { 1048 getTopOfStack().InnerUsedAllocators.push_back(E); 1049 } 1050 /// Return list of used allocators. 1051 ArrayRef<Expr *> getInnerAllocators() const { 1052 return getTopOfStack().InnerUsedAllocators; 1053 } 1054 /// Marks the declaration as implicitly firstprivate nin the task-based 1055 /// regions. 1056 void addImplicitTaskFirstprivate(unsigned Level, Decl *D) { 1057 getStackElemAtLevel(Level).ImplicitTaskFirstprivates.insert(D); 1058 } 1059 /// Checks if the decl is implicitly firstprivate in the task-based region. 1060 bool isImplicitTaskFirstprivate(Decl *D) const { 1061 return getTopOfStack().ImplicitTaskFirstprivates.count(D) > 0; 1062 } 1063 1064 /// Marks decl as used in uses_allocators clause as the allocator. 1065 void addUsesAllocatorsDecl(const Decl *D, UsesAllocatorsDeclKind Kind) { 1066 getTopOfStack().UsesAllocatorsDecls.try_emplace(D, Kind); 1067 } 1068 /// Checks if specified decl is used in uses allocator clause as the 1069 /// allocator. 1070 Optional<UsesAllocatorsDeclKind> isUsesAllocatorsDecl(unsigned Level, 1071 const Decl *D) const { 1072 const SharingMapTy &StackElem = getTopOfStack(); 1073 auto I = StackElem.UsesAllocatorsDecls.find(D); 1074 if (I == StackElem.UsesAllocatorsDecls.end()) 1075 return None; 1076 return I->getSecond(); 1077 } 1078 Optional<UsesAllocatorsDeclKind> isUsesAllocatorsDecl(const Decl *D) const { 1079 const SharingMapTy &StackElem = getTopOfStack(); 1080 auto I = StackElem.UsesAllocatorsDecls.find(D); 1081 if (I == StackElem.UsesAllocatorsDecls.end()) 1082 return None; 1083 return I->getSecond(); 1084 } 1085 1086 void addDeclareMapperVarRef(Expr *Ref) { 1087 SharingMapTy &StackElem = getTopOfStack(); 1088 StackElem.DeclareMapperVar = Ref; 1089 } 1090 const Expr *getDeclareMapperVarRef() const { 1091 const SharingMapTy *Top = getTopOfStackOrNull(); 1092 return Top ? Top->DeclareMapperVar : nullptr; 1093 } 1094 }; 1095 1096 bool isImplicitTaskingRegion(OpenMPDirectiveKind DKind) { 1097 return isOpenMPParallelDirective(DKind) || isOpenMPTeamsDirective(DKind); 1098 } 1099 1100 bool isImplicitOrExplicitTaskingRegion(OpenMPDirectiveKind DKind) { 1101 return isImplicitTaskingRegion(DKind) || isOpenMPTaskingDirective(DKind) || 1102 DKind == OMPD_unknown; 1103 } 1104 1105 } // namespace 1106 1107 static const Expr *getExprAsWritten(const Expr *E) { 1108 if (const auto *FE = dyn_cast<FullExpr>(E)) 1109 E = FE->getSubExpr(); 1110 1111 if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E)) 1112 E = MTE->getSubExpr(); 1113 1114 while (const auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E)) 1115 E = Binder->getSubExpr(); 1116 1117 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E)) 1118 E = ICE->getSubExprAsWritten(); 1119 return E->IgnoreParens(); 1120 } 1121 1122 static Expr *getExprAsWritten(Expr *E) { 1123 return const_cast<Expr *>(getExprAsWritten(const_cast<const Expr *>(E))); 1124 } 1125 1126 static const ValueDecl *getCanonicalDecl(const ValueDecl *D) { 1127 if (const auto *CED = dyn_cast<OMPCapturedExprDecl>(D)) 1128 if (const auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 1129 D = ME->getMemberDecl(); 1130 const auto *VD = dyn_cast<VarDecl>(D); 1131 const auto *FD = dyn_cast<FieldDecl>(D); 1132 if (VD != nullptr) { 1133 VD = VD->getCanonicalDecl(); 1134 D = VD; 1135 } else { 1136 assert(FD); 1137 FD = FD->getCanonicalDecl(); 1138 D = FD; 1139 } 1140 return D; 1141 } 1142 1143 static ValueDecl *getCanonicalDecl(ValueDecl *D) { 1144 return const_cast<ValueDecl *>( 1145 getCanonicalDecl(const_cast<const ValueDecl *>(D))); 1146 } 1147 1148 DSAStackTy::DSAVarData DSAStackTy::getDSA(const_iterator &Iter, 1149 ValueDecl *D) const { 1150 D = getCanonicalDecl(D); 1151 auto *VD = dyn_cast<VarDecl>(D); 1152 const auto *FD = dyn_cast<FieldDecl>(D); 1153 DSAVarData DVar; 1154 if (Iter == end()) { 1155 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1156 // in a region but not in construct] 1157 // File-scope or namespace-scope variables referenced in called routines 1158 // in the region are shared unless they appear in a threadprivate 1159 // directive. 1160 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(VD)) 1161 DVar.CKind = OMPC_shared; 1162 1163 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced 1164 // in a region but not in construct] 1165 // Variables with static storage duration that are declared in called 1166 // routines in the region are shared. 1167 if (VD && VD->hasGlobalStorage()) 1168 DVar.CKind = OMPC_shared; 1169 1170 // Non-static data members are shared by default. 1171 if (FD) 1172 DVar.CKind = OMPC_shared; 1173 1174 return DVar; 1175 } 1176 1177 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1178 // in a Construct, C/C++, predetermined, p.1] 1179 // Variables with automatic storage duration that are declared in a scope 1180 // inside the construct are private. 1181 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() && 1182 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) { 1183 DVar.CKind = OMPC_private; 1184 return DVar; 1185 } 1186 1187 DVar.DKind = Iter->Directive; 1188 // Explicitly specified attributes and local variables with predetermined 1189 // attributes. 1190 if (Iter->SharingMap.count(D)) { 1191 const DSAInfo &Data = Iter->SharingMap.lookup(D); 1192 DVar.RefExpr = Data.RefExpr.getPointer(); 1193 DVar.PrivateCopy = Data.PrivateCopy; 1194 DVar.CKind = Data.Attributes; 1195 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 1196 DVar.Modifier = Data.Modifier; 1197 DVar.AppliedToPointee = Data.AppliedToPointee; 1198 return DVar; 1199 } 1200 1201 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1202 // in a Construct, C/C++, implicitly determined, p.1] 1203 // In a parallel or task construct, the data-sharing attributes of these 1204 // variables are determined by the default clause, if present. 1205 switch (Iter->DefaultAttr) { 1206 case DSA_shared: 1207 DVar.CKind = OMPC_shared; 1208 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 1209 return DVar; 1210 case DSA_none: 1211 return DVar; 1212 case DSA_firstprivate: 1213 if (VD->getStorageDuration() == SD_Static && 1214 VD->getDeclContext()->isFileContext()) { 1215 DVar.CKind = OMPC_unknown; 1216 } else { 1217 DVar.CKind = OMPC_firstprivate; 1218 } 1219 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 1220 return DVar; 1221 case DSA_unspecified: 1222 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1223 // in a Construct, implicitly determined, p.2] 1224 // In a parallel construct, if no default clause is present, these 1225 // variables are shared. 1226 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 1227 if ((isOpenMPParallelDirective(DVar.DKind) && 1228 !isOpenMPTaskLoopDirective(DVar.DKind)) || 1229 isOpenMPTeamsDirective(DVar.DKind)) { 1230 DVar.CKind = OMPC_shared; 1231 return DVar; 1232 } 1233 1234 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1235 // in a Construct, implicitly determined, p.4] 1236 // In a task construct, if no default clause is present, a variable that in 1237 // the enclosing context is determined to be shared by all implicit tasks 1238 // bound to the current team is shared. 1239 if (isOpenMPTaskingDirective(DVar.DKind)) { 1240 DSAVarData DVarTemp; 1241 const_iterator I = Iter, E = end(); 1242 do { 1243 ++I; 1244 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables 1245 // Referenced in a Construct, implicitly determined, p.6] 1246 // In a task construct, if no default clause is present, a variable 1247 // whose data-sharing attribute is not determined by the rules above is 1248 // firstprivate. 1249 DVarTemp = getDSA(I, D); 1250 if (DVarTemp.CKind != OMPC_shared) { 1251 DVar.RefExpr = nullptr; 1252 DVar.CKind = OMPC_firstprivate; 1253 return DVar; 1254 } 1255 } while (I != E && !isImplicitTaskingRegion(I->Directive)); 1256 DVar.CKind = 1257 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared; 1258 return DVar; 1259 } 1260 } 1261 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1262 // in a Construct, implicitly determined, p.3] 1263 // For constructs other than task, if no default clause is present, these 1264 // variables inherit their data-sharing attributes from the enclosing 1265 // context. 1266 return getDSA(++Iter, D); 1267 } 1268 1269 const Expr *DSAStackTy::addUniqueAligned(const ValueDecl *D, 1270 const Expr *NewDE) { 1271 assert(!isStackEmpty() && "Data sharing attributes stack is empty"); 1272 D = getCanonicalDecl(D); 1273 SharingMapTy &StackElem = getTopOfStack(); 1274 auto It = StackElem.AlignedMap.find(D); 1275 if (It == StackElem.AlignedMap.end()) { 1276 assert(NewDE && "Unexpected nullptr expr to be added into aligned map"); 1277 StackElem.AlignedMap[D] = NewDE; 1278 return nullptr; 1279 } 1280 assert(It->second && "Unexpected nullptr expr in the aligned map"); 1281 return It->second; 1282 } 1283 1284 const Expr *DSAStackTy::addUniqueNontemporal(const ValueDecl *D, 1285 const Expr *NewDE) { 1286 assert(!isStackEmpty() && "Data sharing attributes stack is empty"); 1287 D = getCanonicalDecl(D); 1288 SharingMapTy &StackElem = getTopOfStack(); 1289 auto It = StackElem.NontemporalMap.find(D); 1290 if (It == StackElem.NontemporalMap.end()) { 1291 assert(NewDE && "Unexpected nullptr expr to be added into aligned map"); 1292 StackElem.NontemporalMap[D] = NewDE; 1293 return nullptr; 1294 } 1295 assert(It->second && "Unexpected nullptr expr in the aligned map"); 1296 return It->second; 1297 } 1298 1299 void DSAStackTy::addLoopControlVariable(const ValueDecl *D, VarDecl *Capture) { 1300 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1301 D = getCanonicalDecl(D); 1302 SharingMapTy &StackElem = getTopOfStack(); 1303 StackElem.LCVMap.try_emplace( 1304 D, LCDeclInfo(StackElem.LCVMap.size() + 1, Capture)); 1305 } 1306 1307 const DSAStackTy::LCDeclInfo 1308 DSAStackTy::isLoopControlVariable(const ValueDecl *D) const { 1309 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1310 D = getCanonicalDecl(D); 1311 const SharingMapTy &StackElem = getTopOfStack(); 1312 auto It = StackElem.LCVMap.find(D); 1313 if (It != StackElem.LCVMap.end()) 1314 return It->second; 1315 return {0, nullptr}; 1316 } 1317 1318 const DSAStackTy::LCDeclInfo 1319 DSAStackTy::isLoopControlVariable(const ValueDecl *D, unsigned Level) const { 1320 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1321 D = getCanonicalDecl(D); 1322 for (unsigned I = Level + 1; I > 0; --I) { 1323 const SharingMapTy &StackElem = getStackElemAtLevel(I - 1); 1324 auto It = StackElem.LCVMap.find(D); 1325 if (It != StackElem.LCVMap.end()) 1326 return It->second; 1327 } 1328 return {0, nullptr}; 1329 } 1330 1331 const DSAStackTy::LCDeclInfo 1332 DSAStackTy::isParentLoopControlVariable(const ValueDecl *D) const { 1333 const SharingMapTy *Parent = getSecondOnStackOrNull(); 1334 assert(Parent && "Data-sharing attributes stack is empty"); 1335 D = getCanonicalDecl(D); 1336 auto It = Parent->LCVMap.find(D); 1337 if (It != Parent->LCVMap.end()) 1338 return It->second; 1339 return {0, nullptr}; 1340 } 1341 1342 const ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) const { 1343 const SharingMapTy *Parent = getSecondOnStackOrNull(); 1344 assert(Parent && "Data-sharing attributes stack is empty"); 1345 if (Parent->LCVMap.size() < I) 1346 return nullptr; 1347 for (const auto &Pair : Parent->LCVMap) 1348 if (Pair.second.first == I) 1349 return Pair.first; 1350 return nullptr; 1351 } 1352 1353 void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A, 1354 DeclRefExpr *PrivateCopy, unsigned Modifier, 1355 bool AppliedToPointee) { 1356 D = getCanonicalDecl(D); 1357 if (A == OMPC_threadprivate) { 1358 DSAInfo &Data = Threadprivates[D]; 1359 Data.Attributes = A; 1360 Data.RefExpr.setPointer(E); 1361 Data.PrivateCopy = nullptr; 1362 Data.Modifier = Modifier; 1363 } else { 1364 DSAInfo &Data = getTopOfStack().SharingMap[D]; 1365 assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) || 1366 (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) || 1367 (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) || 1368 (isLoopControlVariable(D).first && A == OMPC_private)); 1369 Data.Modifier = Modifier; 1370 if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) { 1371 Data.RefExpr.setInt(/*IntVal=*/true); 1372 return; 1373 } 1374 const bool IsLastprivate = 1375 A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate; 1376 Data.Attributes = A; 1377 Data.RefExpr.setPointerAndInt(E, IsLastprivate); 1378 Data.PrivateCopy = PrivateCopy; 1379 Data.AppliedToPointee = AppliedToPointee; 1380 if (PrivateCopy) { 1381 DSAInfo &Data = getTopOfStack().SharingMap[PrivateCopy->getDecl()]; 1382 Data.Modifier = Modifier; 1383 Data.Attributes = A; 1384 Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate); 1385 Data.PrivateCopy = nullptr; 1386 Data.AppliedToPointee = AppliedToPointee; 1387 } 1388 } 1389 } 1390 1391 /// Build a variable declaration for OpenMP loop iteration variable. 1392 static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type, 1393 StringRef Name, const AttrVec *Attrs = nullptr, 1394 DeclRefExpr *OrigRef = nullptr) { 1395 DeclContext *DC = SemaRef.CurContext; 1396 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name); 1397 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc); 1398 auto *Decl = 1399 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None); 1400 if (Attrs) { 1401 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end()); 1402 I != E; ++I) 1403 Decl->addAttr(*I); 1404 } 1405 Decl->setImplicit(); 1406 if (OrigRef) { 1407 Decl->addAttr( 1408 OMPReferencedVarAttr::CreateImplicit(SemaRef.Context, OrigRef)); 1409 } 1410 return Decl; 1411 } 1412 1413 static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty, 1414 SourceLocation Loc, 1415 bool RefersToCapture = false) { 1416 D->setReferenced(); 1417 D->markUsed(S.Context); 1418 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(), 1419 SourceLocation(), D, RefersToCapture, Loc, Ty, 1420 VK_LValue); 1421 } 1422 1423 void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 1424 BinaryOperatorKind BOK) { 1425 D = getCanonicalDecl(D); 1426 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1427 assert( 1428 getTopOfStack().SharingMap[D].Attributes == OMPC_reduction && 1429 "Additional reduction info may be specified only for reduction items."); 1430 ReductionData &ReductionData = getTopOfStack().ReductionMap[D]; 1431 assert(ReductionData.ReductionRange.isInvalid() && 1432 (getTopOfStack().Directive == OMPD_taskgroup || 1433 ((isOpenMPParallelDirective(getTopOfStack().Directive) || 1434 isOpenMPWorksharingDirective(getTopOfStack().Directive)) && 1435 !isOpenMPSimdDirective(getTopOfStack().Directive))) && 1436 "Additional reduction info may be specified only once for reduction " 1437 "items."); 1438 ReductionData.set(BOK, SR); 1439 Expr *&TaskgroupReductionRef = 1440 getTopOfStack().TaskgroupReductionRef; 1441 if (!TaskgroupReductionRef) { 1442 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(), 1443 SemaRef.Context.VoidPtrTy, ".task_red."); 1444 TaskgroupReductionRef = 1445 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin()); 1446 } 1447 } 1448 1449 void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 1450 const Expr *ReductionRef) { 1451 D = getCanonicalDecl(D); 1452 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1453 assert( 1454 getTopOfStack().SharingMap[D].Attributes == OMPC_reduction && 1455 "Additional reduction info may be specified only for reduction items."); 1456 ReductionData &ReductionData = getTopOfStack().ReductionMap[D]; 1457 assert(ReductionData.ReductionRange.isInvalid() && 1458 (getTopOfStack().Directive == OMPD_taskgroup || 1459 ((isOpenMPParallelDirective(getTopOfStack().Directive) || 1460 isOpenMPWorksharingDirective(getTopOfStack().Directive)) && 1461 !isOpenMPSimdDirective(getTopOfStack().Directive))) && 1462 "Additional reduction info may be specified only once for reduction " 1463 "items."); 1464 ReductionData.set(ReductionRef, SR); 1465 Expr *&TaskgroupReductionRef = 1466 getTopOfStack().TaskgroupReductionRef; 1467 if (!TaskgroupReductionRef) { 1468 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(), 1469 SemaRef.Context.VoidPtrTy, ".task_red."); 1470 TaskgroupReductionRef = 1471 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin()); 1472 } 1473 } 1474 1475 const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData( 1476 const ValueDecl *D, SourceRange &SR, BinaryOperatorKind &BOK, 1477 Expr *&TaskgroupDescriptor) const { 1478 D = getCanonicalDecl(D); 1479 assert(!isStackEmpty() && "Data-sharing attributes stack is empty."); 1480 for (const_iterator I = begin() + 1, E = end(); I != E; ++I) { 1481 const DSAInfo &Data = I->SharingMap.lookup(D); 1482 if (Data.Attributes != OMPC_reduction || 1483 Data.Modifier != OMPC_REDUCTION_task) 1484 continue; 1485 const ReductionData &ReductionData = I->ReductionMap.lookup(D); 1486 if (!ReductionData.ReductionOp || 1487 ReductionData.ReductionOp.is<const Expr *>()) 1488 return DSAVarData(); 1489 SR = ReductionData.ReductionRange; 1490 BOK = ReductionData.ReductionOp.get<ReductionData::BOKPtrType>(); 1491 assert(I->TaskgroupReductionRef && "taskgroup reduction reference " 1492 "expression for the descriptor is not " 1493 "set."); 1494 TaskgroupDescriptor = I->TaskgroupReductionRef; 1495 return DSAVarData(I->Directive, OMPC_reduction, Data.RefExpr.getPointer(), 1496 Data.PrivateCopy, I->DefaultAttrLoc, OMPC_REDUCTION_task, 1497 /*AppliedToPointee=*/false); 1498 } 1499 return DSAVarData(); 1500 } 1501 1502 const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData( 1503 const ValueDecl *D, SourceRange &SR, const Expr *&ReductionRef, 1504 Expr *&TaskgroupDescriptor) const { 1505 D = getCanonicalDecl(D); 1506 assert(!isStackEmpty() && "Data-sharing attributes stack is empty."); 1507 for (const_iterator I = begin() + 1, E = end(); I != E; ++I) { 1508 const DSAInfo &Data = I->SharingMap.lookup(D); 1509 if (Data.Attributes != OMPC_reduction || 1510 Data.Modifier != OMPC_REDUCTION_task) 1511 continue; 1512 const ReductionData &ReductionData = I->ReductionMap.lookup(D); 1513 if (!ReductionData.ReductionOp || 1514 !ReductionData.ReductionOp.is<const Expr *>()) 1515 return DSAVarData(); 1516 SR = ReductionData.ReductionRange; 1517 ReductionRef = ReductionData.ReductionOp.get<const Expr *>(); 1518 assert(I->TaskgroupReductionRef && "taskgroup reduction reference " 1519 "expression for the descriptor is not " 1520 "set."); 1521 TaskgroupDescriptor = I->TaskgroupReductionRef; 1522 return DSAVarData(I->Directive, OMPC_reduction, Data.RefExpr.getPointer(), 1523 Data.PrivateCopy, I->DefaultAttrLoc, OMPC_REDUCTION_task, 1524 /*AppliedToPointee=*/false); 1525 } 1526 return DSAVarData(); 1527 } 1528 1529 bool DSAStackTy::isOpenMPLocal(VarDecl *D, const_iterator I) const { 1530 D = D->getCanonicalDecl(); 1531 for (const_iterator E = end(); I != E; ++I) { 1532 if (isImplicitOrExplicitTaskingRegion(I->Directive) || 1533 isOpenMPTargetExecutionDirective(I->Directive)) { 1534 Scope *TopScope = I->CurScope ? I->CurScope->getParent() : nullptr; 1535 Scope *CurScope = getCurScope(); 1536 while (CurScope && CurScope != TopScope && !CurScope->isDeclScope(D)) 1537 CurScope = CurScope->getParent(); 1538 return CurScope != TopScope; 1539 } 1540 } 1541 return false; 1542 } 1543 1544 static bool isConstNotMutableType(Sema &SemaRef, QualType Type, 1545 bool AcceptIfMutable = true, 1546 bool *IsClassType = nullptr) { 1547 ASTContext &Context = SemaRef.getASTContext(); 1548 Type = Type.getNonReferenceType().getCanonicalType(); 1549 bool IsConstant = Type.isConstant(Context); 1550 Type = Context.getBaseElementType(Type); 1551 const CXXRecordDecl *RD = AcceptIfMutable && SemaRef.getLangOpts().CPlusPlus 1552 ? Type->getAsCXXRecordDecl() 1553 : nullptr; 1554 if (const auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD)) 1555 if (const ClassTemplateDecl *CTD = CTSD->getSpecializedTemplate()) 1556 RD = CTD->getTemplatedDecl(); 1557 if (IsClassType) 1558 *IsClassType = RD; 1559 return IsConstant && !(SemaRef.getLangOpts().CPlusPlus && RD && 1560 RD->hasDefinition() && RD->hasMutableFields()); 1561 } 1562 1563 static bool rejectConstNotMutableType(Sema &SemaRef, const ValueDecl *D, 1564 QualType Type, OpenMPClauseKind CKind, 1565 SourceLocation ELoc, 1566 bool AcceptIfMutable = true, 1567 bool ListItemNotVar = false) { 1568 ASTContext &Context = SemaRef.getASTContext(); 1569 bool IsClassType; 1570 if (isConstNotMutableType(SemaRef, Type, AcceptIfMutable, &IsClassType)) { 1571 unsigned Diag = ListItemNotVar 1572 ? diag::err_omp_const_list_item 1573 : IsClassType ? diag::err_omp_const_not_mutable_variable 1574 : diag::err_omp_const_variable; 1575 SemaRef.Diag(ELoc, Diag) << getOpenMPClauseName(CKind); 1576 if (!ListItemNotVar && D) { 1577 const VarDecl *VD = dyn_cast<VarDecl>(D); 1578 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 1579 VarDecl::DeclarationOnly; 1580 SemaRef.Diag(D->getLocation(), 1581 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1582 << D; 1583 } 1584 return true; 1585 } 1586 return false; 1587 } 1588 1589 const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, 1590 bool FromParent) { 1591 D = getCanonicalDecl(D); 1592 DSAVarData DVar; 1593 1594 auto *VD = dyn_cast<VarDecl>(D); 1595 auto TI = Threadprivates.find(D); 1596 if (TI != Threadprivates.end()) { 1597 DVar.RefExpr = TI->getSecond().RefExpr.getPointer(); 1598 DVar.CKind = OMPC_threadprivate; 1599 DVar.Modifier = TI->getSecond().Modifier; 1600 return DVar; 1601 } 1602 if (VD && VD->hasAttr<OMPThreadPrivateDeclAttr>()) { 1603 DVar.RefExpr = buildDeclRefExpr( 1604 SemaRef, VD, D->getType().getNonReferenceType(), 1605 VD->getAttr<OMPThreadPrivateDeclAttr>()->getLocation()); 1606 DVar.CKind = OMPC_threadprivate; 1607 addDSA(D, DVar.RefExpr, OMPC_threadprivate); 1608 return DVar; 1609 } 1610 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1611 // in a Construct, C/C++, predetermined, p.1] 1612 // Variables appearing in threadprivate directives are threadprivate. 1613 if ((VD && VD->getTLSKind() != VarDecl::TLS_None && 1614 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() && 1615 SemaRef.getLangOpts().OpenMPUseTLS && 1616 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) || 1617 (VD && VD->getStorageClass() == SC_Register && 1618 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) { 1619 DVar.RefExpr = buildDeclRefExpr( 1620 SemaRef, VD, D->getType().getNonReferenceType(), D->getLocation()); 1621 DVar.CKind = OMPC_threadprivate; 1622 addDSA(D, DVar.RefExpr, OMPC_threadprivate); 1623 return DVar; 1624 } 1625 if (SemaRef.getLangOpts().OpenMPCUDAMode && VD && 1626 VD->isLocalVarDeclOrParm() && !isStackEmpty() && 1627 !isLoopControlVariable(D).first) { 1628 const_iterator IterTarget = 1629 std::find_if(begin(), end(), [](const SharingMapTy &Data) { 1630 return isOpenMPTargetExecutionDirective(Data.Directive); 1631 }); 1632 if (IterTarget != end()) { 1633 const_iterator ParentIterTarget = IterTarget + 1; 1634 for (const_iterator Iter = begin(); 1635 Iter != ParentIterTarget; ++Iter) { 1636 if (isOpenMPLocal(VD, Iter)) { 1637 DVar.RefExpr = 1638 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(), 1639 D->getLocation()); 1640 DVar.CKind = OMPC_threadprivate; 1641 return DVar; 1642 } 1643 } 1644 if (!isClauseParsingMode() || IterTarget != begin()) { 1645 auto DSAIter = IterTarget->SharingMap.find(D); 1646 if (DSAIter != IterTarget->SharingMap.end() && 1647 isOpenMPPrivate(DSAIter->getSecond().Attributes)) { 1648 DVar.RefExpr = DSAIter->getSecond().RefExpr.getPointer(); 1649 DVar.CKind = OMPC_threadprivate; 1650 return DVar; 1651 } 1652 const_iterator End = end(); 1653 if (!SemaRef.isOpenMPCapturedByRef( 1654 D, std::distance(ParentIterTarget, End), 1655 /*OpenMPCaptureLevel=*/0)) { 1656 DVar.RefExpr = 1657 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(), 1658 IterTarget->ConstructLoc); 1659 DVar.CKind = OMPC_threadprivate; 1660 return DVar; 1661 } 1662 } 1663 } 1664 } 1665 1666 if (isStackEmpty()) 1667 // Not in OpenMP execution region and top scope was already checked. 1668 return DVar; 1669 1670 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1671 // in a Construct, C/C++, predetermined, p.4] 1672 // Static data members are shared. 1673 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1674 // in a Construct, C/C++, predetermined, p.7] 1675 // Variables with static storage duration that are declared in a scope 1676 // inside the construct are shared. 1677 if (VD && VD->isStaticDataMember()) { 1678 // Check for explicitly specified attributes. 1679 const_iterator I = begin(); 1680 const_iterator EndI = end(); 1681 if (FromParent && I != EndI) 1682 ++I; 1683 if (I != EndI) { 1684 auto It = I->SharingMap.find(D); 1685 if (It != I->SharingMap.end()) { 1686 const DSAInfo &Data = It->getSecond(); 1687 DVar.RefExpr = Data.RefExpr.getPointer(); 1688 DVar.PrivateCopy = Data.PrivateCopy; 1689 DVar.CKind = Data.Attributes; 1690 DVar.ImplicitDSALoc = I->DefaultAttrLoc; 1691 DVar.DKind = I->Directive; 1692 DVar.Modifier = Data.Modifier; 1693 DVar.AppliedToPointee = Data.AppliedToPointee; 1694 return DVar; 1695 } 1696 } 1697 1698 DVar.CKind = OMPC_shared; 1699 return DVar; 1700 } 1701 1702 auto &&MatchesAlways = [](OpenMPDirectiveKind) { return true; }; 1703 // The predetermined shared attribute for const-qualified types having no 1704 // mutable members was removed after OpenMP 3.1. 1705 if (SemaRef.LangOpts.OpenMP <= 31) { 1706 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1707 // in a Construct, C/C++, predetermined, p.6] 1708 // Variables with const qualified type having no mutable member are 1709 // shared. 1710 if (isConstNotMutableType(SemaRef, D->getType())) { 1711 // Variables with const-qualified type having no mutable member may be 1712 // listed in a firstprivate clause, even if they are static data members. 1713 DSAVarData DVarTemp = hasInnermostDSA( 1714 D, 1715 [](OpenMPClauseKind C, bool) { 1716 return C == OMPC_firstprivate || C == OMPC_shared; 1717 }, 1718 MatchesAlways, FromParent); 1719 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr) 1720 return DVarTemp; 1721 1722 DVar.CKind = OMPC_shared; 1723 return DVar; 1724 } 1725 } 1726 1727 // Explicitly specified attributes and local variables with predetermined 1728 // attributes. 1729 const_iterator I = begin(); 1730 const_iterator EndI = end(); 1731 if (FromParent && I != EndI) 1732 ++I; 1733 if (I == EndI) 1734 return DVar; 1735 auto It = I->SharingMap.find(D); 1736 if (It != I->SharingMap.end()) { 1737 const DSAInfo &Data = It->getSecond(); 1738 DVar.RefExpr = Data.RefExpr.getPointer(); 1739 DVar.PrivateCopy = Data.PrivateCopy; 1740 DVar.CKind = Data.Attributes; 1741 DVar.ImplicitDSALoc = I->DefaultAttrLoc; 1742 DVar.DKind = I->Directive; 1743 DVar.Modifier = Data.Modifier; 1744 DVar.AppliedToPointee = Data.AppliedToPointee; 1745 } 1746 1747 return DVar; 1748 } 1749 1750 const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D, 1751 bool FromParent) const { 1752 if (isStackEmpty()) { 1753 const_iterator I; 1754 return getDSA(I, D); 1755 } 1756 D = getCanonicalDecl(D); 1757 const_iterator StartI = begin(); 1758 const_iterator EndI = end(); 1759 if (FromParent && StartI != EndI) 1760 ++StartI; 1761 return getDSA(StartI, D); 1762 } 1763 1764 const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D, 1765 unsigned Level) const { 1766 if (getStackSize() <= Level) 1767 return DSAVarData(); 1768 D = getCanonicalDecl(D); 1769 const_iterator StartI = std::next(begin(), getStackSize() - 1 - Level); 1770 return getDSA(StartI, D); 1771 } 1772 1773 const DSAStackTy::DSAVarData 1774 DSAStackTy::hasDSA(ValueDecl *D, 1775 const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred, 1776 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1777 bool FromParent) const { 1778 if (isStackEmpty()) 1779 return {}; 1780 D = getCanonicalDecl(D); 1781 const_iterator I = begin(); 1782 const_iterator EndI = end(); 1783 if (FromParent && I != EndI) 1784 ++I; 1785 for (; I != EndI; ++I) { 1786 if (!DPred(I->Directive) && 1787 !isImplicitOrExplicitTaskingRegion(I->Directive)) 1788 continue; 1789 const_iterator NewI = I; 1790 DSAVarData DVar = getDSA(NewI, D); 1791 if (I == NewI && CPred(DVar.CKind, DVar.AppliedToPointee)) 1792 return DVar; 1793 } 1794 return {}; 1795 } 1796 1797 const DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA( 1798 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred, 1799 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1800 bool FromParent) const { 1801 if (isStackEmpty()) 1802 return {}; 1803 D = getCanonicalDecl(D); 1804 const_iterator StartI = begin(); 1805 const_iterator EndI = end(); 1806 if (FromParent && StartI != EndI) 1807 ++StartI; 1808 if (StartI == EndI || !DPred(StartI->Directive)) 1809 return {}; 1810 const_iterator NewI = StartI; 1811 DSAVarData DVar = getDSA(NewI, D); 1812 return (NewI == StartI && CPred(DVar.CKind, DVar.AppliedToPointee)) 1813 ? DVar 1814 : DSAVarData(); 1815 } 1816 1817 bool DSAStackTy::hasExplicitDSA( 1818 const ValueDecl *D, 1819 const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred, 1820 unsigned Level, bool NotLastprivate) const { 1821 if (getStackSize() <= Level) 1822 return false; 1823 D = getCanonicalDecl(D); 1824 const SharingMapTy &StackElem = getStackElemAtLevel(Level); 1825 auto I = StackElem.SharingMap.find(D); 1826 if (I != StackElem.SharingMap.end() && I->getSecond().RefExpr.getPointer() && 1827 CPred(I->getSecond().Attributes, I->getSecond().AppliedToPointee) && 1828 (!NotLastprivate || !I->getSecond().RefExpr.getInt())) 1829 return true; 1830 // Check predetermined rules for the loop control variables. 1831 auto LI = StackElem.LCVMap.find(D); 1832 if (LI != StackElem.LCVMap.end()) 1833 return CPred(OMPC_private, /*AppliedToPointee=*/false); 1834 return false; 1835 } 1836 1837 bool DSAStackTy::hasExplicitDirective( 1838 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1839 unsigned Level) const { 1840 if (getStackSize() <= Level) 1841 return false; 1842 const SharingMapTy &StackElem = getStackElemAtLevel(Level); 1843 return DPred(StackElem.Directive); 1844 } 1845 1846 bool DSAStackTy::hasDirective( 1847 const llvm::function_ref<bool(OpenMPDirectiveKind, 1848 const DeclarationNameInfo &, SourceLocation)> 1849 DPred, 1850 bool FromParent) const { 1851 // We look only in the enclosing region. 1852 size_t Skip = FromParent ? 2 : 1; 1853 for (const_iterator I = begin() + std::min(Skip, getStackSize()), E = end(); 1854 I != E; ++I) { 1855 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc)) 1856 return true; 1857 } 1858 return false; 1859 } 1860 1861 void Sema::InitDataSharingAttributesStack() { 1862 VarDataSharingAttributesStack = new DSAStackTy(*this); 1863 } 1864 1865 #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack) 1866 1867 void Sema::pushOpenMPFunctionRegion() { 1868 DSAStack->pushFunction(); 1869 } 1870 1871 void Sema::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) { 1872 DSAStack->popFunction(OldFSI); 1873 } 1874 1875 static bool isOpenMPDeviceDelayedContext(Sema &S) { 1876 assert(S.LangOpts.OpenMP && S.LangOpts.OpenMPIsDevice && 1877 "Expected OpenMP device compilation."); 1878 return !S.isInOpenMPTargetExecutionDirective() && 1879 !S.isInOpenMPDeclareTargetContext(); 1880 } 1881 1882 namespace { 1883 /// Status of the function emission on the host/device. 1884 enum class FunctionEmissionStatus { 1885 Emitted, 1886 Discarded, 1887 Unknown, 1888 }; 1889 } // anonymous namespace 1890 1891 Sema::DeviceDiagBuilder Sema::diagIfOpenMPDeviceCode(SourceLocation Loc, 1892 unsigned DiagID) { 1893 assert(LangOpts.OpenMP && LangOpts.OpenMPIsDevice && 1894 "Expected OpenMP device compilation."); 1895 1896 FunctionDecl *FD = getCurFunctionDecl(); 1897 DeviceDiagBuilder::Kind Kind = DeviceDiagBuilder::K_Nop; 1898 if (FD) { 1899 FunctionEmissionStatus FES = getEmissionStatus(FD); 1900 switch (FES) { 1901 case FunctionEmissionStatus::Emitted: 1902 Kind = DeviceDiagBuilder::K_Immediate; 1903 break; 1904 case FunctionEmissionStatus::Unknown: 1905 Kind = isOpenMPDeviceDelayedContext(*this) 1906 ? DeviceDiagBuilder::K_Deferred 1907 : DeviceDiagBuilder::K_Immediate; 1908 break; 1909 case FunctionEmissionStatus::TemplateDiscarded: 1910 case FunctionEmissionStatus::OMPDiscarded: 1911 Kind = DeviceDiagBuilder::K_Nop; 1912 break; 1913 case FunctionEmissionStatus::CUDADiscarded: 1914 llvm_unreachable("CUDADiscarded unexpected in OpenMP device compilation"); 1915 break; 1916 } 1917 } 1918 1919 return DeviceDiagBuilder(Kind, Loc, DiagID, getCurFunctionDecl(), *this); 1920 } 1921 1922 Sema::DeviceDiagBuilder Sema::diagIfOpenMPHostCode(SourceLocation Loc, 1923 unsigned DiagID) { 1924 assert(LangOpts.OpenMP && !LangOpts.OpenMPIsDevice && 1925 "Expected OpenMP host compilation."); 1926 FunctionEmissionStatus FES = getEmissionStatus(getCurFunctionDecl()); 1927 DeviceDiagBuilder::Kind Kind = DeviceDiagBuilder::K_Nop; 1928 switch (FES) { 1929 case FunctionEmissionStatus::Emitted: 1930 Kind = DeviceDiagBuilder::K_Immediate; 1931 break; 1932 case FunctionEmissionStatus::Unknown: 1933 Kind = DeviceDiagBuilder::K_Deferred; 1934 break; 1935 case FunctionEmissionStatus::TemplateDiscarded: 1936 case FunctionEmissionStatus::OMPDiscarded: 1937 case FunctionEmissionStatus::CUDADiscarded: 1938 Kind = DeviceDiagBuilder::K_Nop; 1939 break; 1940 } 1941 1942 return DeviceDiagBuilder(Kind, Loc, DiagID, getCurFunctionDecl(), *this); 1943 } 1944 1945 static OpenMPDefaultmapClauseKind 1946 getVariableCategoryFromDecl(const LangOptions &LO, const ValueDecl *VD) { 1947 if (LO.OpenMP <= 45) { 1948 if (VD->getType().getNonReferenceType()->isScalarType()) 1949 return OMPC_DEFAULTMAP_scalar; 1950 return OMPC_DEFAULTMAP_aggregate; 1951 } 1952 if (VD->getType().getNonReferenceType()->isAnyPointerType()) 1953 return OMPC_DEFAULTMAP_pointer; 1954 if (VD->getType().getNonReferenceType()->isScalarType()) 1955 return OMPC_DEFAULTMAP_scalar; 1956 return OMPC_DEFAULTMAP_aggregate; 1957 } 1958 1959 bool Sema::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level, 1960 unsigned OpenMPCaptureLevel) const { 1961 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1962 1963 ASTContext &Ctx = getASTContext(); 1964 bool IsByRef = true; 1965 1966 // Find the directive that is associated with the provided scope. 1967 D = cast<ValueDecl>(D->getCanonicalDecl()); 1968 QualType Ty = D->getType(); 1969 1970 bool IsVariableUsedInMapClause = false; 1971 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) { 1972 // This table summarizes how a given variable should be passed to the device 1973 // given its type and the clauses where it appears. This table is based on 1974 // the description in OpenMP 4.5 [2.10.4, target Construct] and 1975 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses]. 1976 // 1977 // ========================================================================= 1978 // | type | defaultmap | pvt | first | is_device_ptr | map | res. | 1979 // | |(tofrom:scalar)| | pvt | | | | 1980 // ========================================================================= 1981 // | scl | | | | - | | bycopy| 1982 // | scl | | - | x | - | - | bycopy| 1983 // | scl | | x | - | - | - | null | 1984 // | scl | x | | | - | | byref | 1985 // | scl | x | - | x | - | - | bycopy| 1986 // | scl | x | x | - | - | - | null | 1987 // | scl | | - | - | - | x | byref | 1988 // | scl | x | - | - | - | x | byref | 1989 // 1990 // | agg | n.a. | | | - | | byref | 1991 // | agg | n.a. | - | x | - | - | byref | 1992 // | agg | n.a. | x | - | - | - | null | 1993 // | agg | n.a. | - | - | - | x | byref | 1994 // | agg | n.a. | - | - | - | x[] | byref | 1995 // 1996 // | ptr | n.a. | | | - | | bycopy| 1997 // | ptr | n.a. | - | x | - | - | bycopy| 1998 // | ptr | n.a. | x | - | - | - | null | 1999 // | ptr | n.a. | - | - | - | x | byref | 2000 // | ptr | n.a. | - | - | - | x[] | bycopy| 2001 // | ptr | n.a. | - | - | x | | bycopy| 2002 // | ptr | n.a. | - | - | x | x | bycopy| 2003 // | ptr | n.a. | - | - | x | x[] | bycopy| 2004 // ========================================================================= 2005 // Legend: 2006 // scl - scalar 2007 // ptr - pointer 2008 // agg - aggregate 2009 // x - applies 2010 // - - invalid in this combination 2011 // [] - mapped with an array section 2012 // byref - should be mapped by reference 2013 // byval - should be mapped by value 2014 // null - initialize a local variable to null on the device 2015 // 2016 // Observations: 2017 // - All scalar declarations that show up in a map clause have to be passed 2018 // by reference, because they may have been mapped in the enclosing data 2019 // environment. 2020 // - If the scalar value does not fit the size of uintptr, it has to be 2021 // passed by reference, regardless the result in the table above. 2022 // - For pointers mapped by value that have either an implicit map or an 2023 // array section, the runtime library may pass the NULL value to the 2024 // device instead of the value passed to it by the compiler. 2025 2026 if (Ty->isReferenceType()) 2027 Ty = Ty->castAs<ReferenceType>()->getPointeeType(); 2028 2029 // Locate map clauses and see if the variable being captured is referred to 2030 // in any of those clauses. Here we only care about variables, not fields, 2031 // because fields are part of aggregates. 2032 bool IsVariableAssociatedWithSection = false; 2033 2034 DSAStack->checkMappableExprComponentListsForDeclAtLevel( 2035 D, Level, 2036 [&IsVariableUsedInMapClause, &IsVariableAssociatedWithSection, D]( 2037 OMPClauseMappableExprCommon::MappableExprComponentListRef 2038 MapExprComponents, 2039 OpenMPClauseKind WhereFoundClauseKind) { 2040 // Only the map clause information influences how a variable is 2041 // captured. E.g. is_device_ptr does not require changing the default 2042 // behavior. 2043 if (WhereFoundClauseKind != OMPC_map) 2044 return false; 2045 2046 auto EI = MapExprComponents.rbegin(); 2047 auto EE = MapExprComponents.rend(); 2048 2049 assert(EI != EE && "Invalid map expression!"); 2050 2051 if (isa<DeclRefExpr>(EI->getAssociatedExpression())) 2052 IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D; 2053 2054 ++EI; 2055 if (EI == EE) 2056 return false; 2057 2058 if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) || 2059 isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) || 2060 isa<MemberExpr>(EI->getAssociatedExpression()) || 2061 isa<OMPArrayShapingExpr>(EI->getAssociatedExpression())) { 2062 IsVariableAssociatedWithSection = true; 2063 // There is nothing more we need to know about this variable. 2064 return true; 2065 } 2066 2067 // Keep looking for more map info. 2068 return false; 2069 }); 2070 2071 if (IsVariableUsedInMapClause) { 2072 // If variable is identified in a map clause it is always captured by 2073 // reference except if it is a pointer that is dereferenced somehow. 2074 IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection); 2075 } else { 2076 // By default, all the data that has a scalar type is mapped by copy 2077 // (except for reduction variables). 2078 // Defaultmap scalar is mutual exclusive to defaultmap pointer 2079 IsByRef = (DSAStack->isForceCaptureByReferenceInTargetExecutable() && 2080 !Ty->isAnyPointerType()) || 2081 !Ty->isScalarType() || 2082 DSAStack->isDefaultmapCapturedByRef( 2083 Level, getVariableCategoryFromDecl(LangOpts, D)) || 2084 DSAStack->hasExplicitDSA( 2085 D, 2086 [](OpenMPClauseKind K, bool AppliedToPointee) { 2087 return K == OMPC_reduction && !AppliedToPointee; 2088 }, 2089 Level); 2090 } 2091 } 2092 2093 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) { 2094 IsByRef = 2095 ((IsVariableUsedInMapClause && 2096 DSAStack->getCaptureRegion(Level, OpenMPCaptureLevel) == 2097 OMPD_target) || 2098 !(DSAStack->hasExplicitDSA( 2099 D, 2100 [](OpenMPClauseKind K, bool AppliedToPointee) -> bool { 2101 return K == OMPC_firstprivate || 2102 (K == OMPC_reduction && AppliedToPointee); 2103 }, 2104 Level, /*NotLastprivate=*/true) || 2105 DSAStack->isUsesAllocatorsDecl(Level, D))) && 2106 // If the variable is artificial and must be captured by value - try to 2107 // capture by value. 2108 !(isa<OMPCapturedExprDecl>(D) && !D->hasAttr<OMPCaptureNoInitAttr>() && 2109 !cast<OMPCapturedExprDecl>(D)->getInit()->isGLValue()) && 2110 // If the variable is implicitly firstprivate and scalar - capture by 2111 // copy 2112 !(DSAStack->getDefaultDSA() == DSA_firstprivate && 2113 !DSAStack->hasExplicitDSA( 2114 D, [](OpenMPClauseKind K, bool) { return K != OMPC_unknown; }, 2115 Level) && 2116 !DSAStack->isLoopControlVariable(D, Level).first); 2117 } 2118 2119 // When passing data by copy, we need to make sure it fits the uintptr size 2120 // and alignment, because the runtime library only deals with uintptr types. 2121 // If it does not fit the uintptr size, we need to pass the data by reference 2122 // instead. 2123 if (!IsByRef && 2124 (Ctx.getTypeSizeInChars(Ty) > 2125 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) || 2126 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) { 2127 IsByRef = true; 2128 } 2129 2130 return IsByRef; 2131 } 2132 2133 unsigned Sema::getOpenMPNestingLevel() const { 2134 assert(getLangOpts().OpenMP); 2135 return DSAStack->getNestingLevel(); 2136 } 2137 2138 bool Sema::isInOpenMPTargetExecutionDirective() const { 2139 return (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) && 2140 !DSAStack->isClauseParsingMode()) || 2141 DSAStack->hasDirective( 2142 [](OpenMPDirectiveKind K, const DeclarationNameInfo &, 2143 SourceLocation) -> bool { 2144 return isOpenMPTargetExecutionDirective(K); 2145 }, 2146 false); 2147 } 2148 2149 VarDecl *Sema::isOpenMPCapturedDecl(ValueDecl *D, bool CheckScopeInfo, 2150 unsigned StopAt) { 2151 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 2152 D = getCanonicalDecl(D); 2153 2154 auto *VD = dyn_cast<VarDecl>(D); 2155 // Do not capture constexpr variables. 2156 if (VD && VD->isConstexpr()) 2157 return nullptr; 2158 2159 // If we want to determine whether the variable should be captured from the 2160 // perspective of the current capturing scope, and we've already left all the 2161 // capturing scopes of the top directive on the stack, check from the 2162 // perspective of its parent directive (if any) instead. 2163 DSAStackTy::ParentDirectiveScope InParentDirectiveRAII( 2164 *DSAStack, CheckScopeInfo && DSAStack->isBodyComplete()); 2165 2166 // If we are attempting to capture a global variable in a directive with 2167 // 'target' we return true so that this global is also mapped to the device. 2168 // 2169 if (VD && !VD->hasLocalStorage() && 2170 (getCurCapturedRegion() || getCurBlock() || getCurLambda())) { 2171 if (isInOpenMPDeclareTargetContext()) { 2172 // Try to mark variable as declare target if it is used in capturing 2173 // regions. 2174 if (LangOpts.OpenMP <= 45 && 2175 !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) 2176 checkDeclIsAllowedInOpenMPTarget(nullptr, VD); 2177 return nullptr; 2178 } 2179 if (isInOpenMPTargetExecutionDirective()) { 2180 // If the declaration is enclosed in a 'declare target' directive, 2181 // then it should not be captured. 2182 // 2183 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) 2184 return nullptr; 2185 CapturedRegionScopeInfo *CSI = nullptr; 2186 for (FunctionScopeInfo *FSI : llvm::drop_begin( 2187 llvm::reverse(FunctionScopes), 2188 CheckScopeInfo ? (FunctionScopes.size() - (StopAt + 1)) : 0)) { 2189 if (!isa<CapturingScopeInfo>(FSI)) 2190 return nullptr; 2191 if (auto *RSI = dyn_cast<CapturedRegionScopeInfo>(FSI)) 2192 if (RSI->CapRegionKind == CR_OpenMP) { 2193 CSI = RSI; 2194 break; 2195 } 2196 } 2197 SmallVector<OpenMPDirectiveKind, 4> Regions; 2198 getOpenMPCaptureRegions(Regions, 2199 DSAStack->getDirective(CSI->OpenMPLevel)); 2200 if (Regions[CSI->OpenMPCaptureLevel] != OMPD_task) 2201 return VD; 2202 } 2203 } 2204 2205 if (CheckScopeInfo) { 2206 bool OpenMPFound = false; 2207 for (unsigned I = StopAt + 1; I > 0; --I) { 2208 FunctionScopeInfo *FSI = FunctionScopes[I - 1]; 2209 if(!isa<CapturingScopeInfo>(FSI)) 2210 return nullptr; 2211 if (auto *RSI = dyn_cast<CapturedRegionScopeInfo>(FSI)) 2212 if (RSI->CapRegionKind == CR_OpenMP) { 2213 OpenMPFound = true; 2214 break; 2215 } 2216 } 2217 if (!OpenMPFound) 2218 return nullptr; 2219 } 2220 2221 if (DSAStack->getCurrentDirective() != OMPD_unknown && 2222 (!DSAStack->isClauseParsingMode() || 2223 DSAStack->getParentDirective() != OMPD_unknown)) { 2224 auto &&Info = DSAStack->isLoopControlVariable(D); 2225 if (Info.first || 2226 (VD && VD->hasLocalStorage() && 2227 isImplicitOrExplicitTaskingRegion(DSAStack->getCurrentDirective())) || 2228 (VD && DSAStack->isForceVarCapturing())) 2229 return VD ? VD : Info.second; 2230 DSAStackTy::DSAVarData DVarTop = 2231 DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode()); 2232 if (DVarTop.CKind != OMPC_unknown && isOpenMPPrivate(DVarTop.CKind) && 2233 (!VD || VD->hasLocalStorage() || !DVarTop.AppliedToPointee)) 2234 return VD ? VD : cast<VarDecl>(DVarTop.PrivateCopy->getDecl()); 2235 // Threadprivate variables must not be captured. 2236 if (isOpenMPThreadPrivate(DVarTop.CKind)) 2237 return nullptr; 2238 // The variable is not private or it is the variable in the directive with 2239 // default(none) clause and not used in any clause. 2240 DSAStackTy::DSAVarData DVarPrivate = DSAStack->hasDSA( 2241 D, 2242 [](OpenMPClauseKind C, bool AppliedToPointee) { 2243 return isOpenMPPrivate(C) && !AppliedToPointee; 2244 }, 2245 [](OpenMPDirectiveKind) { return true; }, 2246 DSAStack->isClauseParsingMode()); 2247 // Global shared must not be captured. 2248 if (VD && !VD->hasLocalStorage() && DVarPrivate.CKind == OMPC_unknown && 2249 ((DSAStack->getDefaultDSA() != DSA_none && 2250 DSAStack->getDefaultDSA() != DSA_firstprivate) || 2251 DVarTop.CKind == OMPC_shared)) 2252 return nullptr; 2253 if (DVarPrivate.CKind != OMPC_unknown || 2254 (VD && (DSAStack->getDefaultDSA() == DSA_none || 2255 DSAStack->getDefaultDSA() == DSA_firstprivate))) 2256 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl()); 2257 } 2258 return nullptr; 2259 } 2260 2261 void Sema::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex, 2262 unsigned Level) const { 2263 FunctionScopesIndex -= getOpenMPCaptureLevels(DSAStack->getDirective(Level)); 2264 } 2265 2266 void Sema::startOpenMPLoop() { 2267 assert(LangOpts.OpenMP && "OpenMP must be enabled."); 2268 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) 2269 DSAStack->loopInit(); 2270 } 2271 2272 void Sema::startOpenMPCXXRangeFor() { 2273 assert(LangOpts.OpenMP && "OpenMP must be enabled."); 2274 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { 2275 DSAStack->resetPossibleLoopCounter(); 2276 DSAStack->loopStart(); 2277 } 2278 } 2279 2280 OpenMPClauseKind Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level, 2281 unsigned CapLevel) const { 2282 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 2283 if (DSAStack->hasExplicitDirective( 2284 [](OpenMPDirectiveKind K) { return isOpenMPTaskingDirective(K); }, 2285 Level)) { 2286 bool IsTriviallyCopyable = 2287 D->getType().getNonReferenceType().isTriviallyCopyableType(Context) && 2288 !D->getType() 2289 .getNonReferenceType() 2290 .getCanonicalType() 2291 ->getAsCXXRecordDecl(); 2292 OpenMPDirectiveKind DKind = DSAStack->getDirective(Level); 2293 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 2294 getOpenMPCaptureRegions(CaptureRegions, DKind); 2295 if (isOpenMPTaskingDirective(CaptureRegions[CapLevel]) && 2296 (IsTriviallyCopyable || 2297 !isOpenMPTaskLoopDirective(CaptureRegions[CapLevel]))) { 2298 if (DSAStack->hasExplicitDSA( 2299 D, 2300 [](OpenMPClauseKind K, bool) { return K == OMPC_firstprivate; }, 2301 Level, /*NotLastprivate=*/true)) 2302 return OMPC_firstprivate; 2303 DSAStackTy::DSAVarData DVar = DSAStack->getImplicitDSA(D, Level); 2304 if (DVar.CKind != OMPC_shared && 2305 !DSAStack->isLoopControlVariable(D, Level).first && !DVar.RefExpr) { 2306 DSAStack->addImplicitTaskFirstprivate(Level, D); 2307 return OMPC_firstprivate; 2308 } 2309 } 2310 } 2311 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { 2312 if (DSAStack->getAssociatedLoops() > 0 && 2313 !DSAStack->isLoopStarted()) { 2314 DSAStack->resetPossibleLoopCounter(D); 2315 DSAStack->loopStart(); 2316 return OMPC_private; 2317 } 2318 if ((DSAStack->getPossiblyLoopCunter() == D->getCanonicalDecl() || 2319 DSAStack->isLoopControlVariable(D).first) && 2320 !DSAStack->hasExplicitDSA( 2321 D, [](OpenMPClauseKind K, bool) { return K != OMPC_private; }, 2322 Level) && 2323 !isOpenMPSimdDirective(DSAStack->getCurrentDirective())) 2324 return OMPC_private; 2325 } 2326 if (const auto *VD = dyn_cast<VarDecl>(D)) { 2327 if (DSAStack->isThreadPrivate(const_cast<VarDecl *>(VD)) && 2328 DSAStack->isForceVarCapturing() && 2329 !DSAStack->hasExplicitDSA( 2330 D, [](OpenMPClauseKind K, bool) { return K == OMPC_copyin; }, 2331 Level)) 2332 return OMPC_private; 2333 } 2334 // User-defined allocators are private since they must be defined in the 2335 // context of target region. 2336 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level) && 2337 DSAStack->isUsesAllocatorsDecl(Level, D).getValueOr( 2338 DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait) == 2339 DSAStackTy::UsesAllocatorsDeclKind::UserDefinedAllocator) 2340 return OMPC_private; 2341 return (DSAStack->hasExplicitDSA( 2342 D, [](OpenMPClauseKind K, bool) { return K == OMPC_private; }, 2343 Level) || 2344 (DSAStack->isClauseParsingMode() && 2345 DSAStack->getClauseParsingMode() == OMPC_private) || 2346 // Consider taskgroup reduction descriptor variable a private 2347 // to avoid possible capture in the region. 2348 (DSAStack->hasExplicitDirective( 2349 [](OpenMPDirectiveKind K) { 2350 return K == OMPD_taskgroup || 2351 ((isOpenMPParallelDirective(K) || 2352 isOpenMPWorksharingDirective(K)) && 2353 !isOpenMPSimdDirective(K)); 2354 }, 2355 Level) && 2356 DSAStack->isTaskgroupReductionRef(D, Level))) 2357 ? OMPC_private 2358 : OMPC_unknown; 2359 } 2360 2361 void Sema::setOpenMPCaptureKind(FieldDecl *FD, const ValueDecl *D, 2362 unsigned Level) { 2363 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 2364 D = getCanonicalDecl(D); 2365 OpenMPClauseKind OMPC = OMPC_unknown; 2366 for (unsigned I = DSAStack->getNestingLevel() + 1; I > Level; --I) { 2367 const unsigned NewLevel = I - 1; 2368 if (DSAStack->hasExplicitDSA( 2369 D, 2370 [&OMPC](const OpenMPClauseKind K, bool AppliedToPointee) { 2371 if (isOpenMPPrivate(K) && !AppliedToPointee) { 2372 OMPC = K; 2373 return true; 2374 } 2375 return false; 2376 }, 2377 NewLevel)) 2378 break; 2379 if (DSAStack->checkMappableExprComponentListsForDeclAtLevel( 2380 D, NewLevel, 2381 [](OMPClauseMappableExprCommon::MappableExprComponentListRef, 2382 OpenMPClauseKind) { return true; })) { 2383 OMPC = OMPC_map; 2384 break; 2385 } 2386 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, 2387 NewLevel)) { 2388 OMPC = OMPC_map; 2389 if (DSAStack->mustBeFirstprivateAtLevel( 2390 NewLevel, getVariableCategoryFromDecl(LangOpts, D))) 2391 OMPC = OMPC_firstprivate; 2392 break; 2393 } 2394 } 2395 if (OMPC != OMPC_unknown) 2396 FD->addAttr(OMPCaptureKindAttr::CreateImplicit(Context, unsigned(OMPC))); 2397 } 2398 2399 bool Sema::isOpenMPTargetCapturedDecl(const ValueDecl *D, unsigned Level, 2400 unsigned CaptureLevel) const { 2401 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 2402 // Return true if the current level is no longer enclosed in a target region. 2403 2404 SmallVector<OpenMPDirectiveKind, 4> Regions; 2405 getOpenMPCaptureRegions(Regions, DSAStack->getDirective(Level)); 2406 const auto *VD = dyn_cast<VarDecl>(D); 2407 return VD && !VD->hasLocalStorage() && 2408 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, 2409 Level) && 2410 Regions[CaptureLevel] != OMPD_task; 2411 } 2412 2413 bool Sema::isOpenMPGlobalCapturedDecl(ValueDecl *D, unsigned Level, 2414 unsigned CaptureLevel) const { 2415 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 2416 // Return true if the current level is no longer enclosed in a target region. 2417 2418 if (const auto *VD = dyn_cast<VarDecl>(D)) { 2419 if (!VD->hasLocalStorage()) { 2420 DSAStackTy::DSAVarData TopDVar = 2421 DSAStack->getTopDSA(D, /*FromParent=*/false); 2422 unsigned NumLevels = 2423 getOpenMPCaptureLevels(DSAStack->getDirective(Level)); 2424 if (Level == 0) 2425 return (NumLevels == CaptureLevel + 1) && TopDVar.CKind != OMPC_shared; 2426 DSAStackTy::DSAVarData DVar = DSAStack->getImplicitDSA(D, Level - 1); 2427 return DVar.CKind != OMPC_shared || 2428 isOpenMPGlobalCapturedDecl( 2429 D, Level - 1, 2430 getOpenMPCaptureLevels(DSAStack->getDirective(Level - 1)) - 1); 2431 } 2432 } 2433 return true; 2434 } 2435 2436 void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; } 2437 2438 void Sema::ActOnOpenMPBeginDeclareVariant(SourceLocation Loc, 2439 OMPTraitInfo &TI) { 2440 if (!OMPDeclareVariantScopes.empty()) { 2441 Diag(Loc, diag::warn_nested_declare_variant); 2442 return; 2443 } 2444 OMPDeclareVariantScopes.push_back(OMPDeclareVariantScope(TI)); 2445 } 2446 2447 void Sema::ActOnOpenMPEndDeclareVariant() { 2448 assert(isInOpenMPDeclareVariantScope() && 2449 "Not in OpenMP declare variant scope!"); 2450 2451 OMPDeclareVariantScopes.pop_back(); 2452 } 2453 2454 void Sema::finalizeOpenMPDelayedAnalysis(const FunctionDecl *Caller, 2455 const FunctionDecl *Callee, 2456 SourceLocation Loc) { 2457 assert(LangOpts.OpenMP && "Expected OpenMP compilation mode."); 2458 Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy = 2459 OMPDeclareTargetDeclAttr::getDeviceType(Caller->getMostRecentDecl()); 2460 // Ignore host functions during device analyzis. 2461 if (LangOpts.OpenMPIsDevice && DevTy && 2462 *DevTy == OMPDeclareTargetDeclAttr::DT_Host) 2463 return; 2464 // Ignore nohost functions during host analyzis. 2465 if (!LangOpts.OpenMPIsDevice && DevTy && 2466 *DevTy == OMPDeclareTargetDeclAttr::DT_NoHost) 2467 return; 2468 const FunctionDecl *FD = Callee->getMostRecentDecl(); 2469 DevTy = OMPDeclareTargetDeclAttr::getDeviceType(FD); 2470 if (LangOpts.OpenMPIsDevice && DevTy && 2471 *DevTy == OMPDeclareTargetDeclAttr::DT_Host) { 2472 // Diagnose host function called during device codegen. 2473 StringRef HostDevTy = 2474 getOpenMPSimpleClauseTypeName(OMPC_device_type, OMPC_DEVICE_TYPE_host); 2475 Diag(Loc, diag::err_omp_wrong_device_function_call) << HostDevTy << 0; 2476 Diag(FD->getAttr<OMPDeclareTargetDeclAttr>()->getLocation(), 2477 diag::note_omp_marked_device_type_here) 2478 << HostDevTy; 2479 return; 2480 } 2481 if (!LangOpts.OpenMPIsDevice && DevTy && 2482 *DevTy == OMPDeclareTargetDeclAttr::DT_NoHost) { 2483 // Diagnose nohost function called during host codegen. 2484 StringRef NoHostDevTy = getOpenMPSimpleClauseTypeName( 2485 OMPC_device_type, OMPC_DEVICE_TYPE_nohost); 2486 Diag(Loc, diag::err_omp_wrong_device_function_call) << NoHostDevTy << 1; 2487 Diag(FD->getAttr<OMPDeclareTargetDeclAttr>()->getLocation(), 2488 diag::note_omp_marked_device_type_here) 2489 << NoHostDevTy; 2490 } 2491 } 2492 2493 void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind, 2494 const DeclarationNameInfo &DirName, 2495 Scope *CurScope, SourceLocation Loc) { 2496 DSAStack->push(DKind, DirName, CurScope, Loc); 2497 PushExpressionEvaluationContext( 2498 ExpressionEvaluationContext::PotentiallyEvaluated); 2499 } 2500 2501 void Sema::StartOpenMPClause(OpenMPClauseKind K) { 2502 DSAStack->setClauseParsingMode(K); 2503 } 2504 2505 void Sema::EndOpenMPClause() { 2506 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown); 2507 } 2508 2509 static std::pair<ValueDecl *, bool> 2510 getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc, 2511 SourceRange &ERange, bool AllowArraySection = false); 2512 2513 /// Check consistency of the reduction clauses. 2514 static void checkReductionClauses(Sema &S, DSAStackTy *Stack, 2515 ArrayRef<OMPClause *> Clauses) { 2516 bool InscanFound = false; 2517 SourceLocation InscanLoc; 2518 // OpenMP 5.0, 2.19.5.4 reduction Clause, Restrictions. 2519 // A reduction clause without the inscan reduction-modifier may not appear on 2520 // a construct on which a reduction clause with the inscan reduction-modifier 2521 // appears. 2522 for (OMPClause *C : Clauses) { 2523 if (C->getClauseKind() != OMPC_reduction) 2524 continue; 2525 auto *RC = cast<OMPReductionClause>(C); 2526 if (RC->getModifier() == OMPC_REDUCTION_inscan) { 2527 InscanFound = true; 2528 InscanLoc = RC->getModifierLoc(); 2529 continue; 2530 } 2531 if (RC->getModifier() == OMPC_REDUCTION_task) { 2532 // OpenMP 5.0, 2.19.5.4 reduction Clause. 2533 // A reduction clause with the task reduction-modifier may only appear on 2534 // a parallel construct, a worksharing construct or a combined or 2535 // composite construct for which any of the aforementioned constructs is a 2536 // constituent construct and simd or loop are not constituent constructs. 2537 OpenMPDirectiveKind CurDir = Stack->getCurrentDirective(); 2538 if (!(isOpenMPParallelDirective(CurDir) || 2539 isOpenMPWorksharingDirective(CurDir)) || 2540 isOpenMPSimdDirective(CurDir)) 2541 S.Diag(RC->getModifierLoc(), 2542 diag::err_omp_reduction_task_not_parallel_or_worksharing); 2543 continue; 2544 } 2545 } 2546 if (InscanFound) { 2547 for (OMPClause *C : Clauses) { 2548 if (C->getClauseKind() != OMPC_reduction) 2549 continue; 2550 auto *RC = cast<OMPReductionClause>(C); 2551 if (RC->getModifier() != OMPC_REDUCTION_inscan) { 2552 S.Diag(RC->getModifier() == OMPC_REDUCTION_unknown 2553 ? RC->getBeginLoc() 2554 : RC->getModifierLoc(), 2555 diag::err_omp_inscan_reduction_expected); 2556 S.Diag(InscanLoc, diag::note_omp_previous_inscan_reduction); 2557 continue; 2558 } 2559 for (Expr *Ref : RC->varlists()) { 2560 assert(Ref && "NULL expr in OpenMP nontemporal clause."); 2561 SourceLocation ELoc; 2562 SourceRange ERange; 2563 Expr *SimpleRefExpr = Ref; 2564 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange, 2565 /*AllowArraySection=*/true); 2566 ValueDecl *D = Res.first; 2567 if (!D) 2568 continue; 2569 if (!Stack->isUsedInScanDirective(getCanonicalDecl(D))) { 2570 S.Diag(Ref->getExprLoc(), 2571 diag::err_omp_reduction_not_inclusive_exclusive) 2572 << Ref->getSourceRange(); 2573 } 2574 } 2575 } 2576 } 2577 } 2578 2579 static void checkAllocateClauses(Sema &S, DSAStackTy *Stack, 2580 ArrayRef<OMPClause *> Clauses); 2581 static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr, 2582 bool WithInit); 2583 2584 static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack, 2585 const ValueDecl *D, 2586 const DSAStackTy::DSAVarData &DVar, 2587 bool IsLoopIterVar = false); 2588 2589 void Sema::EndOpenMPDSABlock(Stmt *CurDirective) { 2590 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1] 2591 // A variable of class type (or array thereof) that appears in a lastprivate 2592 // clause requires an accessible, unambiguous default constructor for the 2593 // class type, unless the list item is also specified in a firstprivate 2594 // clause. 2595 if (const auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) { 2596 for (OMPClause *C : D->clauses()) { 2597 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) { 2598 SmallVector<Expr *, 8> PrivateCopies; 2599 for (Expr *DE : Clause->varlists()) { 2600 if (DE->isValueDependent() || DE->isTypeDependent()) { 2601 PrivateCopies.push_back(nullptr); 2602 continue; 2603 } 2604 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens()); 2605 auto *VD = cast<VarDecl>(DRE->getDecl()); 2606 QualType Type = VD->getType().getNonReferenceType(); 2607 const DSAStackTy::DSAVarData DVar = 2608 DSAStack->getTopDSA(VD, /*FromParent=*/false); 2609 if (DVar.CKind == OMPC_lastprivate) { 2610 // Generate helper private variable and initialize it with the 2611 // default value. The address of the original variable is replaced 2612 // by the address of the new private variable in CodeGen. This new 2613 // variable is not added to IdResolver, so the code in the OpenMP 2614 // region uses original variable for proper diagnostics. 2615 VarDecl *VDPrivate = buildVarDecl( 2616 *this, DE->getExprLoc(), Type.getUnqualifiedType(), 2617 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr, DRE); 2618 ActOnUninitializedDecl(VDPrivate); 2619 if (VDPrivate->isInvalidDecl()) { 2620 PrivateCopies.push_back(nullptr); 2621 continue; 2622 } 2623 PrivateCopies.push_back(buildDeclRefExpr( 2624 *this, VDPrivate, DE->getType(), DE->getExprLoc())); 2625 } else { 2626 // The variable is also a firstprivate, so initialization sequence 2627 // for private copy is generated already. 2628 PrivateCopies.push_back(nullptr); 2629 } 2630 } 2631 Clause->setPrivateCopies(PrivateCopies); 2632 continue; 2633 } 2634 // Finalize nontemporal clause by handling private copies, if any. 2635 if (auto *Clause = dyn_cast<OMPNontemporalClause>(C)) { 2636 SmallVector<Expr *, 8> PrivateRefs; 2637 for (Expr *RefExpr : Clause->varlists()) { 2638 assert(RefExpr && "NULL expr in OpenMP nontemporal clause."); 2639 SourceLocation ELoc; 2640 SourceRange ERange; 2641 Expr *SimpleRefExpr = RefExpr; 2642 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 2643 if (Res.second) 2644 // It will be analyzed later. 2645 PrivateRefs.push_back(RefExpr); 2646 ValueDecl *D = Res.first; 2647 if (!D) 2648 continue; 2649 2650 const DSAStackTy::DSAVarData DVar = 2651 DSAStack->getTopDSA(D, /*FromParent=*/false); 2652 PrivateRefs.push_back(DVar.PrivateCopy ? DVar.PrivateCopy 2653 : SimpleRefExpr); 2654 } 2655 Clause->setPrivateRefs(PrivateRefs); 2656 continue; 2657 } 2658 if (auto *Clause = dyn_cast<OMPUsesAllocatorsClause>(C)) { 2659 for (unsigned I = 0, E = Clause->getNumberOfAllocators(); I < E; ++I) { 2660 OMPUsesAllocatorsClause::Data D = Clause->getAllocatorData(I); 2661 auto *DRE = dyn_cast<DeclRefExpr>(D.Allocator->IgnoreParenImpCasts()); 2662 if (!DRE) 2663 continue; 2664 ValueDecl *VD = DRE->getDecl(); 2665 if (!VD || !isa<VarDecl>(VD)) 2666 continue; 2667 DSAStackTy::DSAVarData DVar = 2668 DSAStack->getTopDSA(VD, /*FromParent=*/false); 2669 // OpenMP [2.12.5, target Construct] 2670 // Memory allocators that appear in a uses_allocators clause cannot 2671 // appear in other data-sharing attribute clauses or data-mapping 2672 // attribute clauses in the same construct. 2673 Expr *MapExpr = nullptr; 2674 if (DVar.RefExpr || 2675 DSAStack->checkMappableExprComponentListsForDecl( 2676 VD, /*CurrentRegionOnly=*/true, 2677 [VD, &MapExpr]( 2678 OMPClauseMappableExprCommon::MappableExprComponentListRef 2679 MapExprComponents, 2680 OpenMPClauseKind C) { 2681 auto MI = MapExprComponents.rbegin(); 2682 auto ME = MapExprComponents.rend(); 2683 if (MI != ME && 2684 MI->getAssociatedDeclaration()->getCanonicalDecl() == 2685 VD->getCanonicalDecl()) { 2686 MapExpr = MI->getAssociatedExpression(); 2687 return true; 2688 } 2689 return false; 2690 })) { 2691 Diag(D.Allocator->getExprLoc(), 2692 diag::err_omp_allocator_used_in_clauses) 2693 << D.Allocator->getSourceRange(); 2694 if (DVar.RefExpr) 2695 reportOriginalDsa(*this, DSAStack, VD, DVar); 2696 else 2697 Diag(MapExpr->getExprLoc(), diag::note_used_here) 2698 << MapExpr->getSourceRange(); 2699 } 2700 } 2701 continue; 2702 } 2703 } 2704 // Check allocate clauses. 2705 if (!CurContext->isDependentContext()) 2706 checkAllocateClauses(*this, DSAStack, D->clauses()); 2707 checkReductionClauses(*this, DSAStack, D->clauses()); 2708 } 2709 2710 DSAStack->pop(); 2711 DiscardCleanupsInEvaluationContext(); 2712 PopExpressionEvaluationContext(); 2713 } 2714 2715 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, 2716 Expr *NumIterations, Sema &SemaRef, 2717 Scope *S, DSAStackTy *Stack); 2718 2719 namespace { 2720 2721 class VarDeclFilterCCC final : public CorrectionCandidateCallback { 2722 private: 2723 Sema &SemaRef; 2724 2725 public: 2726 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {} 2727 bool ValidateCandidate(const TypoCorrection &Candidate) override { 2728 NamedDecl *ND = Candidate.getCorrectionDecl(); 2729 if (const auto *VD = dyn_cast_or_null<VarDecl>(ND)) { 2730 return VD->hasGlobalStorage() && 2731 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), 2732 SemaRef.getCurScope()); 2733 } 2734 return false; 2735 } 2736 2737 std::unique_ptr<CorrectionCandidateCallback> clone() override { 2738 return std::make_unique<VarDeclFilterCCC>(*this); 2739 } 2740 2741 }; 2742 2743 class VarOrFuncDeclFilterCCC final : public CorrectionCandidateCallback { 2744 private: 2745 Sema &SemaRef; 2746 2747 public: 2748 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {} 2749 bool ValidateCandidate(const TypoCorrection &Candidate) override { 2750 NamedDecl *ND = Candidate.getCorrectionDecl(); 2751 if (ND && ((isa<VarDecl>(ND) && ND->getKind() == Decl::Var) || 2752 isa<FunctionDecl>(ND))) { 2753 return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), 2754 SemaRef.getCurScope()); 2755 } 2756 return false; 2757 } 2758 2759 std::unique_ptr<CorrectionCandidateCallback> clone() override { 2760 return std::make_unique<VarOrFuncDeclFilterCCC>(*this); 2761 } 2762 }; 2763 2764 } // namespace 2765 2766 ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope, 2767 CXXScopeSpec &ScopeSpec, 2768 const DeclarationNameInfo &Id, 2769 OpenMPDirectiveKind Kind) { 2770 LookupResult Lookup(*this, Id, LookupOrdinaryName); 2771 LookupParsedName(Lookup, CurScope, &ScopeSpec, true); 2772 2773 if (Lookup.isAmbiguous()) 2774 return ExprError(); 2775 2776 VarDecl *VD; 2777 if (!Lookup.isSingleResult()) { 2778 VarDeclFilterCCC CCC(*this); 2779 if (TypoCorrection Corrected = 2780 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, CCC, 2781 CTK_ErrorRecovery)) { 2782 diagnoseTypo(Corrected, 2783 PDiag(Lookup.empty() 2784 ? diag::err_undeclared_var_use_suggest 2785 : diag::err_omp_expected_var_arg_suggest) 2786 << Id.getName()); 2787 VD = Corrected.getCorrectionDeclAs<VarDecl>(); 2788 } else { 2789 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use 2790 : diag::err_omp_expected_var_arg) 2791 << Id.getName(); 2792 return ExprError(); 2793 } 2794 } else if (!(VD = Lookup.getAsSingle<VarDecl>())) { 2795 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName(); 2796 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at); 2797 return ExprError(); 2798 } 2799 Lookup.suppressDiagnostics(); 2800 2801 // OpenMP [2.9.2, Syntax, C/C++] 2802 // Variables must be file-scope, namespace-scope, or static block-scope. 2803 if (Kind == OMPD_threadprivate && !VD->hasGlobalStorage()) { 2804 Diag(Id.getLoc(), diag::err_omp_global_var_arg) 2805 << getOpenMPDirectiveName(Kind) << !VD->isStaticLocal(); 2806 bool IsDecl = 2807 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2808 Diag(VD->getLocation(), 2809 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2810 << VD; 2811 return ExprError(); 2812 } 2813 2814 VarDecl *CanonicalVD = VD->getCanonicalDecl(); 2815 NamedDecl *ND = CanonicalVD; 2816 // OpenMP [2.9.2, Restrictions, C/C++, p.2] 2817 // A threadprivate directive for file-scope variables must appear outside 2818 // any definition or declaration. 2819 if (CanonicalVD->getDeclContext()->isTranslationUnit() && 2820 !getCurLexicalContext()->isTranslationUnit()) { 2821 Diag(Id.getLoc(), diag::err_omp_var_scope) 2822 << getOpenMPDirectiveName(Kind) << VD; 2823 bool IsDecl = 2824 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2825 Diag(VD->getLocation(), 2826 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2827 << VD; 2828 return ExprError(); 2829 } 2830 // OpenMP [2.9.2, Restrictions, C/C++, p.3] 2831 // A threadprivate directive for static class member variables must appear 2832 // in the class definition, in the same scope in which the member 2833 // variables are declared. 2834 if (CanonicalVD->isStaticDataMember() && 2835 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) { 2836 Diag(Id.getLoc(), diag::err_omp_var_scope) 2837 << getOpenMPDirectiveName(Kind) << VD; 2838 bool IsDecl = 2839 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2840 Diag(VD->getLocation(), 2841 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2842 << VD; 2843 return ExprError(); 2844 } 2845 // OpenMP [2.9.2, Restrictions, C/C++, p.4] 2846 // A threadprivate directive for namespace-scope variables must appear 2847 // outside any definition or declaration other than the namespace 2848 // definition itself. 2849 if (CanonicalVD->getDeclContext()->isNamespace() && 2850 (!getCurLexicalContext()->isFileContext() || 2851 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) { 2852 Diag(Id.getLoc(), diag::err_omp_var_scope) 2853 << getOpenMPDirectiveName(Kind) << VD; 2854 bool IsDecl = 2855 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2856 Diag(VD->getLocation(), 2857 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2858 << VD; 2859 return ExprError(); 2860 } 2861 // OpenMP [2.9.2, Restrictions, C/C++, p.6] 2862 // A threadprivate directive for static block-scope variables must appear 2863 // in the scope of the variable and not in a nested scope. 2864 if (CanonicalVD->isLocalVarDecl() && CurScope && 2865 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) { 2866 Diag(Id.getLoc(), diag::err_omp_var_scope) 2867 << getOpenMPDirectiveName(Kind) << VD; 2868 bool IsDecl = 2869 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2870 Diag(VD->getLocation(), 2871 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2872 << VD; 2873 return ExprError(); 2874 } 2875 2876 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6] 2877 // A threadprivate directive must lexically precede all references to any 2878 // of the variables in its list. 2879 if (Kind == OMPD_threadprivate && VD->isUsed() && 2880 !DSAStack->isThreadPrivate(VD)) { 2881 Diag(Id.getLoc(), diag::err_omp_var_used) 2882 << getOpenMPDirectiveName(Kind) << VD; 2883 return ExprError(); 2884 } 2885 2886 QualType ExprType = VD->getType().getNonReferenceType(); 2887 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(), 2888 SourceLocation(), VD, 2889 /*RefersToEnclosingVariableOrCapture=*/false, 2890 Id.getLoc(), ExprType, VK_LValue); 2891 } 2892 2893 Sema::DeclGroupPtrTy 2894 Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc, 2895 ArrayRef<Expr *> VarList) { 2896 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) { 2897 CurContext->addDecl(D); 2898 return DeclGroupPtrTy::make(DeclGroupRef(D)); 2899 } 2900 return nullptr; 2901 } 2902 2903 namespace { 2904 class LocalVarRefChecker final 2905 : public ConstStmtVisitor<LocalVarRefChecker, bool> { 2906 Sema &SemaRef; 2907 2908 public: 2909 bool VisitDeclRefExpr(const DeclRefExpr *E) { 2910 if (const auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 2911 if (VD->hasLocalStorage()) { 2912 SemaRef.Diag(E->getBeginLoc(), 2913 diag::err_omp_local_var_in_threadprivate_init) 2914 << E->getSourceRange(); 2915 SemaRef.Diag(VD->getLocation(), diag::note_defined_here) 2916 << VD << VD->getSourceRange(); 2917 return true; 2918 } 2919 } 2920 return false; 2921 } 2922 bool VisitStmt(const Stmt *S) { 2923 for (const Stmt *Child : S->children()) { 2924 if (Child && Visit(Child)) 2925 return true; 2926 } 2927 return false; 2928 } 2929 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {} 2930 }; 2931 } // namespace 2932 2933 OMPThreadPrivateDecl * 2934 Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) { 2935 SmallVector<Expr *, 8> Vars; 2936 for (Expr *RefExpr : VarList) { 2937 auto *DE = cast<DeclRefExpr>(RefExpr); 2938 auto *VD = cast<VarDecl>(DE->getDecl()); 2939 SourceLocation ILoc = DE->getExprLoc(); 2940 2941 // Mark variable as used. 2942 VD->setReferenced(); 2943 VD->markUsed(Context); 2944 2945 QualType QType = VD->getType(); 2946 if (QType->isDependentType() || QType->isInstantiationDependentType()) { 2947 // It will be analyzed later. 2948 Vars.push_back(DE); 2949 continue; 2950 } 2951 2952 // OpenMP [2.9.2, Restrictions, C/C++, p.10] 2953 // A threadprivate variable must not have an incomplete type. 2954 if (RequireCompleteType(ILoc, VD->getType(), 2955 diag::err_omp_threadprivate_incomplete_type)) { 2956 continue; 2957 } 2958 2959 // OpenMP [2.9.2, Restrictions, C/C++, p.10] 2960 // A threadprivate variable must not have a reference type. 2961 if (VD->getType()->isReferenceType()) { 2962 Diag(ILoc, diag::err_omp_ref_type_arg) 2963 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType(); 2964 bool IsDecl = 2965 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2966 Diag(VD->getLocation(), 2967 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2968 << VD; 2969 continue; 2970 } 2971 2972 // Check if this is a TLS variable. If TLS is not being supported, produce 2973 // the corresponding diagnostic. 2974 if ((VD->getTLSKind() != VarDecl::TLS_None && 2975 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() && 2976 getLangOpts().OpenMPUseTLS && 2977 getASTContext().getTargetInfo().isTLSSupported())) || 2978 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() && 2979 !VD->isLocalVarDecl())) { 2980 Diag(ILoc, diag::err_omp_var_thread_local) 2981 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1); 2982 bool IsDecl = 2983 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2984 Diag(VD->getLocation(), 2985 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2986 << VD; 2987 continue; 2988 } 2989 2990 // Check if initial value of threadprivate variable reference variable with 2991 // local storage (it is not supported by runtime). 2992 if (const Expr *Init = VD->getAnyInitializer()) { 2993 LocalVarRefChecker Checker(*this); 2994 if (Checker.Visit(Init)) 2995 continue; 2996 } 2997 2998 Vars.push_back(RefExpr); 2999 DSAStack->addDSA(VD, DE, OMPC_threadprivate); 3000 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit( 3001 Context, SourceRange(Loc, Loc))); 3002 if (ASTMutationListener *ML = Context.getASTMutationListener()) 3003 ML->DeclarationMarkedOpenMPThreadPrivate(VD); 3004 } 3005 OMPThreadPrivateDecl *D = nullptr; 3006 if (!Vars.empty()) { 3007 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc, 3008 Vars); 3009 D->setAccess(AS_public); 3010 } 3011 return D; 3012 } 3013 3014 static OMPAllocateDeclAttr::AllocatorTypeTy 3015 getAllocatorKind(Sema &S, DSAStackTy *Stack, Expr *Allocator) { 3016 if (!Allocator) 3017 return OMPAllocateDeclAttr::OMPNullMemAlloc; 3018 if (Allocator->isTypeDependent() || Allocator->isValueDependent() || 3019 Allocator->isInstantiationDependent() || 3020 Allocator->containsUnexpandedParameterPack()) 3021 return OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; 3022 auto AllocatorKindRes = OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; 3023 const Expr *AE = Allocator->IgnoreParenImpCasts(); 3024 for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) { 3025 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I); 3026 const Expr *DefAllocator = Stack->getAllocator(AllocatorKind); 3027 llvm::FoldingSetNodeID AEId, DAEId; 3028 AE->Profile(AEId, S.getASTContext(), /*Canonical=*/true); 3029 DefAllocator->Profile(DAEId, S.getASTContext(), /*Canonical=*/true); 3030 if (AEId == DAEId) { 3031 AllocatorKindRes = AllocatorKind; 3032 break; 3033 } 3034 } 3035 return AllocatorKindRes; 3036 } 3037 3038 static bool checkPreviousOMPAllocateAttribute( 3039 Sema &S, DSAStackTy *Stack, Expr *RefExpr, VarDecl *VD, 3040 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, Expr *Allocator) { 3041 if (!VD->hasAttr<OMPAllocateDeclAttr>()) 3042 return false; 3043 const auto *A = VD->getAttr<OMPAllocateDeclAttr>(); 3044 Expr *PrevAllocator = A->getAllocator(); 3045 OMPAllocateDeclAttr::AllocatorTypeTy PrevAllocatorKind = 3046 getAllocatorKind(S, Stack, PrevAllocator); 3047 bool AllocatorsMatch = AllocatorKind == PrevAllocatorKind; 3048 if (AllocatorsMatch && 3049 AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc && 3050 Allocator && PrevAllocator) { 3051 const Expr *AE = Allocator->IgnoreParenImpCasts(); 3052 const Expr *PAE = PrevAllocator->IgnoreParenImpCasts(); 3053 llvm::FoldingSetNodeID AEId, PAEId; 3054 AE->Profile(AEId, S.Context, /*Canonical=*/true); 3055 PAE->Profile(PAEId, S.Context, /*Canonical=*/true); 3056 AllocatorsMatch = AEId == PAEId; 3057 } 3058 if (!AllocatorsMatch) { 3059 SmallString<256> AllocatorBuffer; 3060 llvm::raw_svector_ostream AllocatorStream(AllocatorBuffer); 3061 if (Allocator) 3062 Allocator->printPretty(AllocatorStream, nullptr, S.getPrintingPolicy()); 3063 SmallString<256> PrevAllocatorBuffer; 3064 llvm::raw_svector_ostream PrevAllocatorStream(PrevAllocatorBuffer); 3065 if (PrevAllocator) 3066 PrevAllocator->printPretty(PrevAllocatorStream, nullptr, 3067 S.getPrintingPolicy()); 3068 3069 SourceLocation AllocatorLoc = 3070 Allocator ? Allocator->getExprLoc() : RefExpr->getExprLoc(); 3071 SourceRange AllocatorRange = 3072 Allocator ? Allocator->getSourceRange() : RefExpr->getSourceRange(); 3073 SourceLocation PrevAllocatorLoc = 3074 PrevAllocator ? PrevAllocator->getExprLoc() : A->getLocation(); 3075 SourceRange PrevAllocatorRange = 3076 PrevAllocator ? PrevAllocator->getSourceRange() : A->getRange(); 3077 S.Diag(AllocatorLoc, diag::warn_omp_used_different_allocator) 3078 << (Allocator ? 1 : 0) << AllocatorStream.str() 3079 << (PrevAllocator ? 1 : 0) << PrevAllocatorStream.str() 3080 << AllocatorRange; 3081 S.Diag(PrevAllocatorLoc, diag::note_omp_previous_allocator) 3082 << PrevAllocatorRange; 3083 return true; 3084 } 3085 return false; 3086 } 3087 3088 static void 3089 applyOMPAllocateAttribute(Sema &S, VarDecl *VD, 3090 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, 3091 Expr *Allocator, SourceRange SR) { 3092 if (VD->hasAttr<OMPAllocateDeclAttr>()) 3093 return; 3094 if (Allocator && 3095 (Allocator->isTypeDependent() || Allocator->isValueDependent() || 3096 Allocator->isInstantiationDependent() || 3097 Allocator->containsUnexpandedParameterPack())) 3098 return; 3099 auto *A = OMPAllocateDeclAttr::CreateImplicit(S.Context, AllocatorKind, 3100 Allocator, SR); 3101 VD->addAttr(A); 3102 if (ASTMutationListener *ML = S.Context.getASTMutationListener()) 3103 ML->DeclarationMarkedOpenMPAllocate(VD, A); 3104 } 3105 3106 Sema::DeclGroupPtrTy Sema::ActOnOpenMPAllocateDirective( 3107 SourceLocation Loc, ArrayRef<Expr *> VarList, 3108 ArrayRef<OMPClause *> Clauses, DeclContext *Owner) { 3109 assert(Clauses.size() <= 1 && "Expected at most one clause."); 3110 Expr *Allocator = nullptr; 3111 if (Clauses.empty()) { 3112 // OpenMP 5.0, 2.11.3 allocate Directive, Restrictions. 3113 // allocate directives that appear in a target region must specify an 3114 // allocator clause unless a requires directive with the dynamic_allocators 3115 // clause is present in the same compilation unit. 3116 if (LangOpts.OpenMPIsDevice && 3117 !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>()) 3118 targetDiag(Loc, diag::err_expected_allocator_clause); 3119 } else { 3120 Allocator = cast<OMPAllocatorClause>(Clauses.back())->getAllocator(); 3121 } 3122 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind = 3123 getAllocatorKind(*this, DSAStack, Allocator); 3124 SmallVector<Expr *, 8> Vars; 3125 for (Expr *RefExpr : VarList) { 3126 auto *DE = cast<DeclRefExpr>(RefExpr); 3127 auto *VD = cast<VarDecl>(DE->getDecl()); 3128 3129 // Check if this is a TLS variable or global register. 3130 if (VD->getTLSKind() != VarDecl::TLS_None || 3131 VD->hasAttr<OMPThreadPrivateDeclAttr>() || 3132 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() && 3133 !VD->isLocalVarDecl())) 3134 continue; 3135 3136 // If the used several times in the allocate directive, the same allocator 3137 // must be used. 3138 if (checkPreviousOMPAllocateAttribute(*this, DSAStack, RefExpr, VD, 3139 AllocatorKind, Allocator)) 3140 continue; 3141 3142 // OpenMP, 2.11.3 allocate Directive, Restrictions, C / C++ 3143 // If a list item has a static storage type, the allocator expression in the 3144 // allocator clause must be a constant expression that evaluates to one of 3145 // the predefined memory allocator values. 3146 if (Allocator && VD->hasGlobalStorage()) { 3147 if (AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc) { 3148 Diag(Allocator->getExprLoc(), 3149 diag::err_omp_expected_predefined_allocator) 3150 << Allocator->getSourceRange(); 3151 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 3152 VarDecl::DeclarationOnly; 3153 Diag(VD->getLocation(), 3154 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 3155 << VD; 3156 continue; 3157 } 3158 } 3159 3160 Vars.push_back(RefExpr); 3161 applyOMPAllocateAttribute(*this, VD, AllocatorKind, Allocator, 3162 DE->getSourceRange()); 3163 } 3164 if (Vars.empty()) 3165 return nullptr; 3166 if (!Owner) 3167 Owner = getCurLexicalContext(); 3168 auto *D = OMPAllocateDecl::Create(Context, Owner, Loc, Vars, Clauses); 3169 D->setAccess(AS_public); 3170 Owner->addDecl(D); 3171 return DeclGroupPtrTy::make(DeclGroupRef(D)); 3172 } 3173 3174 Sema::DeclGroupPtrTy 3175 Sema::ActOnOpenMPRequiresDirective(SourceLocation Loc, 3176 ArrayRef<OMPClause *> ClauseList) { 3177 OMPRequiresDecl *D = nullptr; 3178 if (!CurContext->isFileContext()) { 3179 Diag(Loc, diag::err_omp_invalid_scope) << "requires"; 3180 } else { 3181 D = CheckOMPRequiresDecl(Loc, ClauseList); 3182 if (D) { 3183 CurContext->addDecl(D); 3184 DSAStack->addRequiresDecl(D); 3185 } 3186 } 3187 return DeclGroupPtrTy::make(DeclGroupRef(D)); 3188 } 3189 3190 OMPRequiresDecl *Sema::CheckOMPRequiresDecl(SourceLocation Loc, 3191 ArrayRef<OMPClause *> ClauseList) { 3192 /// For target specific clauses, the requires directive cannot be 3193 /// specified after the handling of any of the target regions in the 3194 /// current compilation unit. 3195 ArrayRef<SourceLocation> TargetLocations = 3196 DSAStack->getEncounteredTargetLocs(); 3197 SourceLocation AtomicLoc = DSAStack->getAtomicDirectiveLoc(); 3198 if (!TargetLocations.empty() || !AtomicLoc.isInvalid()) { 3199 for (const OMPClause *CNew : ClauseList) { 3200 // Check if any of the requires clauses affect target regions. 3201 if (isa<OMPUnifiedSharedMemoryClause>(CNew) || 3202 isa<OMPUnifiedAddressClause>(CNew) || 3203 isa<OMPReverseOffloadClause>(CNew) || 3204 isa<OMPDynamicAllocatorsClause>(CNew)) { 3205 Diag(Loc, diag::err_omp_directive_before_requires) 3206 << "target" << getOpenMPClauseName(CNew->getClauseKind()); 3207 for (SourceLocation TargetLoc : TargetLocations) { 3208 Diag(TargetLoc, diag::note_omp_requires_encountered_directive) 3209 << "target"; 3210 } 3211 } else if (!AtomicLoc.isInvalid() && 3212 isa<OMPAtomicDefaultMemOrderClause>(CNew)) { 3213 Diag(Loc, diag::err_omp_directive_before_requires) 3214 << "atomic" << getOpenMPClauseName(CNew->getClauseKind()); 3215 Diag(AtomicLoc, diag::note_omp_requires_encountered_directive) 3216 << "atomic"; 3217 } 3218 } 3219 } 3220 3221 if (!DSAStack->hasDuplicateRequiresClause(ClauseList)) 3222 return OMPRequiresDecl::Create(Context, getCurLexicalContext(), Loc, 3223 ClauseList); 3224 return nullptr; 3225 } 3226 3227 static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack, 3228 const ValueDecl *D, 3229 const DSAStackTy::DSAVarData &DVar, 3230 bool IsLoopIterVar) { 3231 if (DVar.RefExpr) { 3232 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa) 3233 << getOpenMPClauseName(DVar.CKind); 3234 return; 3235 } 3236 enum { 3237 PDSA_StaticMemberShared, 3238 PDSA_StaticLocalVarShared, 3239 PDSA_LoopIterVarPrivate, 3240 PDSA_LoopIterVarLinear, 3241 PDSA_LoopIterVarLastprivate, 3242 PDSA_ConstVarShared, 3243 PDSA_GlobalVarShared, 3244 PDSA_TaskVarFirstprivate, 3245 PDSA_LocalVarPrivate, 3246 PDSA_Implicit 3247 } Reason = PDSA_Implicit; 3248 bool ReportHint = false; 3249 auto ReportLoc = D->getLocation(); 3250 auto *VD = dyn_cast<VarDecl>(D); 3251 if (IsLoopIterVar) { 3252 if (DVar.CKind == OMPC_private) 3253 Reason = PDSA_LoopIterVarPrivate; 3254 else if (DVar.CKind == OMPC_lastprivate) 3255 Reason = PDSA_LoopIterVarLastprivate; 3256 else 3257 Reason = PDSA_LoopIterVarLinear; 3258 } else if (isOpenMPTaskingDirective(DVar.DKind) && 3259 DVar.CKind == OMPC_firstprivate) { 3260 Reason = PDSA_TaskVarFirstprivate; 3261 ReportLoc = DVar.ImplicitDSALoc; 3262 } else if (VD && VD->isStaticLocal()) 3263 Reason = PDSA_StaticLocalVarShared; 3264 else if (VD && VD->isStaticDataMember()) 3265 Reason = PDSA_StaticMemberShared; 3266 else if (VD && VD->isFileVarDecl()) 3267 Reason = PDSA_GlobalVarShared; 3268 else if (D->getType().isConstant(SemaRef.getASTContext())) 3269 Reason = PDSA_ConstVarShared; 3270 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) { 3271 ReportHint = true; 3272 Reason = PDSA_LocalVarPrivate; 3273 } 3274 if (Reason != PDSA_Implicit) { 3275 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa) 3276 << Reason << ReportHint 3277 << getOpenMPDirectiveName(Stack->getCurrentDirective()); 3278 } else if (DVar.ImplicitDSALoc.isValid()) { 3279 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa) 3280 << getOpenMPClauseName(DVar.CKind); 3281 } 3282 } 3283 3284 static OpenMPMapClauseKind 3285 getMapClauseKindFromModifier(OpenMPDefaultmapClauseModifier M, 3286 bool IsAggregateOrDeclareTarget) { 3287 OpenMPMapClauseKind Kind = OMPC_MAP_unknown; 3288 switch (M) { 3289 case OMPC_DEFAULTMAP_MODIFIER_alloc: 3290 Kind = OMPC_MAP_alloc; 3291 break; 3292 case OMPC_DEFAULTMAP_MODIFIER_to: 3293 Kind = OMPC_MAP_to; 3294 break; 3295 case OMPC_DEFAULTMAP_MODIFIER_from: 3296 Kind = OMPC_MAP_from; 3297 break; 3298 case OMPC_DEFAULTMAP_MODIFIER_tofrom: 3299 Kind = OMPC_MAP_tofrom; 3300 break; 3301 case OMPC_DEFAULTMAP_MODIFIER_firstprivate: 3302 case OMPC_DEFAULTMAP_MODIFIER_last: 3303 llvm_unreachable("Unexpected defaultmap implicit behavior"); 3304 case OMPC_DEFAULTMAP_MODIFIER_none: 3305 case OMPC_DEFAULTMAP_MODIFIER_default: 3306 case OMPC_DEFAULTMAP_MODIFIER_unknown: 3307 // IsAggregateOrDeclareTarget could be true if: 3308 // 1. the implicit behavior for aggregate is tofrom 3309 // 2. it's a declare target link 3310 if (IsAggregateOrDeclareTarget) { 3311 Kind = OMPC_MAP_tofrom; 3312 break; 3313 } 3314 llvm_unreachable("Unexpected defaultmap implicit behavior"); 3315 } 3316 assert(Kind != OMPC_MAP_unknown && "Expect map kind to be known"); 3317 return Kind; 3318 } 3319 3320 namespace { 3321 class DSAAttrChecker final : public StmtVisitor<DSAAttrChecker, void> { 3322 DSAStackTy *Stack; 3323 Sema &SemaRef; 3324 bool ErrorFound = false; 3325 bool TryCaptureCXXThisMembers = false; 3326 CapturedStmt *CS = nullptr; 3327 llvm::SmallVector<Expr *, 4> ImplicitFirstprivate; 3328 llvm::SmallVector<Expr *, 4> ImplicitMap[OMPC_MAP_delete]; 3329 Sema::VarsWithInheritedDSAType VarsWithInheritedDSA; 3330 llvm::SmallDenseSet<const ValueDecl *, 4> ImplicitDeclarations; 3331 3332 void VisitSubCaptures(OMPExecutableDirective *S) { 3333 // Check implicitly captured variables. 3334 if (!S->hasAssociatedStmt() || !S->getAssociatedStmt() || 3335 S->getDirectiveKind() == OMPD_atomic || 3336 S->getDirectiveKind() == OMPD_critical || 3337 S->getDirectiveKind() == OMPD_section || 3338 S->getDirectiveKind() == OMPD_master) 3339 return; 3340 visitSubCaptures(S->getInnermostCapturedStmt()); 3341 // Try to capture inner this->member references to generate correct mappings 3342 // and diagnostics. 3343 if (TryCaptureCXXThisMembers || 3344 (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) && 3345 llvm::any_of(S->getInnermostCapturedStmt()->captures(), 3346 [](const CapturedStmt::Capture &C) { 3347 return C.capturesThis(); 3348 }))) { 3349 bool SavedTryCaptureCXXThisMembers = TryCaptureCXXThisMembers; 3350 TryCaptureCXXThisMembers = true; 3351 Visit(S->getInnermostCapturedStmt()->getCapturedStmt()); 3352 TryCaptureCXXThisMembers = SavedTryCaptureCXXThisMembers; 3353 } 3354 // In tasks firstprivates are not captured anymore, need to analyze them 3355 // explicitly. 3356 if (isOpenMPTaskingDirective(S->getDirectiveKind()) && 3357 !isOpenMPTaskLoopDirective(S->getDirectiveKind())) { 3358 for (OMPClause *C : S->clauses()) 3359 if (auto *FC = dyn_cast<OMPFirstprivateClause>(C)) { 3360 for (Expr *Ref : FC->varlists()) 3361 Visit(Ref); 3362 } 3363 } 3364 } 3365 3366 public: 3367 void VisitDeclRefExpr(DeclRefExpr *E) { 3368 if (TryCaptureCXXThisMembers || E->isTypeDependent() || 3369 E->isValueDependent() || E->containsUnexpandedParameterPack() || 3370 E->isInstantiationDependent()) 3371 return; 3372 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 3373 // Check the datasharing rules for the expressions in the clauses. 3374 if (!CS) { 3375 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD)) 3376 if (!CED->hasAttr<OMPCaptureNoInitAttr>()) { 3377 Visit(CED->getInit()); 3378 return; 3379 } 3380 } else if (VD->isImplicit() || isa<OMPCapturedExprDecl>(VD)) 3381 // Do not analyze internal variables and do not enclose them into 3382 // implicit clauses. 3383 return; 3384 VD = VD->getCanonicalDecl(); 3385 // Skip internally declared variables. 3386 if (VD->hasLocalStorage() && CS && !CS->capturesVariable(VD) && 3387 !Stack->isImplicitTaskFirstprivate(VD)) 3388 return; 3389 // Skip allocators in uses_allocators clauses. 3390 if (Stack->isUsesAllocatorsDecl(VD).hasValue()) 3391 return; 3392 3393 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false); 3394 // Check if the variable has explicit DSA set and stop analysis if it so. 3395 if (DVar.RefExpr || !ImplicitDeclarations.insert(VD).second) 3396 return; 3397 3398 // Skip internally declared static variables. 3399 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 3400 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); 3401 if (VD->hasGlobalStorage() && CS && !CS->capturesVariable(VD) && 3402 (Stack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() || 3403 !Res || *Res != OMPDeclareTargetDeclAttr::MT_Link) && 3404 !Stack->isImplicitTaskFirstprivate(VD)) 3405 return; 3406 3407 SourceLocation ELoc = E->getExprLoc(); 3408 OpenMPDirectiveKind DKind = Stack->getCurrentDirective(); 3409 // The default(none) clause requires that each variable that is referenced 3410 // in the construct, and does not have a predetermined data-sharing 3411 // attribute, must have its data-sharing attribute explicitly determined 3412 // by being listed in a data-sharing attribute clause. 3413 if (DVar.CKind == OMPC_unknown && 3414 (Stack->getDefaultDSA() == DSA_none || 3415 Stack->getDefaultDSA() == DSA_firstprivate) && 3416 isImplicitOrExplicitTaskingRegion(DKind) && 3417 VarsWithInheritedDSA.count(VD) == 0) { 3418 bool InheritedDSA = Stack->getDefaultDSA() == DSA_none; 3419 if (!InheritedDSA && Stack->getDefaultDSA() == DSA_firstprivate) { 3420 DSAStackTy::DSAVarData DVar = 3421 Stack->getImplicitDSA(VD, /*FromParent=*/false); 3422 InheritedDSA = DVar.CKind == OMPC_unknown; 3423 } 3424 if (InheritedDSA) 3425 VarsWithInheritedDSA[VD] = E; 3426 return; 3427 } 3428 3429 // OpenMP 5.0 [2.19.7.2, defaultmap clause, Description] 3430 // If implicit-behavior is none, each variable referenced in the 3431 // construct that does not have a predetermined data-sharing attribute 3432 // and does not appear in a to or link clause on a declare target 3433 // directive must be listed in a data-mapping attribute clause, a 3434 // data-haring attribute clause (including a data-sharing attribute 3435 // clause on a combined construct where target. is one of the 3436 // constituent constructs), or an is_device_ptr clause. 3437 OpenMPDefaultmapClauseKind ClauseKind = 3438 getVariableCategoryFromDecl(SemaRef.getLangOpts(), VD); 3439 if (SemaRef.getLangOpts().OpenMP >= 50) { 3440 bool IsModifierNone = Stack->getDefaultmapModifier(ClauseKind) == 3441 OMPC_DEFAULTMAP_MODIFIER_none; 3442 if (DVar.CKind == OMPC_unknown && IsModifierNone && 3443 VarsWithInheritedDSA.count(VD) == 0 && !Res) { 3444 // Only check for data-mapping attribute and is_device_ptr here 3445 // since we have already make sure that the declaration does not 3446 // have a data-sharing attribute above 3447 if (!Stack->checkMappableExprComponentListsForDecl( 3448 VD, /*CurrentRegionOnly=*/true, 3449 [VD](OMPClauseMappableExprCommon::MappableExprComponentListRef 3450 MapExprComponents, 3451 OpenMPClauseKind) { 3452 auto MI = MapExprComponents.rbegin(); 3453 auto ME = MapExprComponents.rend(); 3454 return MI != ME && MI->getAssociatedDeclaration() == VD; 3455 })) { 3456 VarsWithInheritedDSA[VD] = E; 3457 return; 3458 } 3459 } 3460 } 3461 3462 if (isOpenMPTargetExecutionDirective(DKind) && 3463 !Stack->isLoopControlVariable(VD).first) { 3464 if (!Stack->checkMappableExprComponentListsForDecl( 3465 VD, /*CurrentRegionOnly=*/true, 3466 [](OMPClauseMappableExprCommon::MappableExprComponentListRef 3467 StackComponents, 3468 OpenMPClauseKind) { 3469 // Variable is used if it has been marked as an array, array 3470 // section, array shaping or the variable iself. 3471 return StackComponents.size() == 1 || 3472 std::all_of( 3473 std::next(StackComponents.rbegin()), 3474 StackComponents.rend(), 3475 [](const OMPClauseMappableExprCommon:: 3476 MappableComponent &MC) { 3477 return MC.getAssociatedDeclaration() == 3478 nullptr && 3479 (isa<OMPArraySectionExpr>( 3480 MC.getAssociatedExpression()) || 3481 isa<OMPArrayShapingExpr>( 3482 MC.getAssociatedExpression()) || 3483 isa<ArraySubscriptExpr>( 3484 MC.getAssociatedExpression())); 3485 }); 3486 })) { 3487 bool IsFirstprivate = false; 3488 // By default lambdas are captured as firstprivates. 3489 if (const auto *RD = 3490 VD->getType().getNonReferenceType()->getAsCXXRecordDecl()) 3491 IsFirstprivate = RD->isLambda(); 3492 IsFirstprivate = 3493 IsFirstprivate || (Stack->mustBeFirstprivate(ClauseKind) && !Res); 3494 if (IsFirstprivate) { 3495 ImplicitFirstprivate.emplace_back(E); 3496 } else { 3497 OpenMPDefaultmapClauseModifier M = 3498 Stack->getDefaultmapModifier(ClauseKind); 3499 OpenMPMapClauseKind Kind = getMapClauseKindFromModifier( 3500 M, ClauseKind == OMPC_DEFAULTMAP_aggregate || Res); 3501 ImplicitMap[Kind].emplace_back(E); 3502 } 3503 return; 3504 } 3505 } 3506 3507 // OpenMP [2.9.3.6, Restrictions, p.2] 3508 // A list item that appears in a reduction clause of the innermost 3509 // enclosing worksharing or parallel construct may not be accessed in an 3510 // explicit task. 3511 DVar = Stack->hasInnermostDSA( 3512 VD, 3513 [](OpenMPClauseKind C, bool AppliedToPointee) { 3514 return C == OMPC_reduction && !AppliedToPointee; 3515 }, 3516 [](OpenMPDirectiveKind K) { 3517 return isOpenMPParallelDirective(K) || 3518 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K); 3519 }, 3520 /*FromParent=*/true); 3521 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) { 3522 ErrorFound = true; 3523 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); 3524 reportOriginalDsa(SemaRef, Stack, VD, DVar); 3525 return; 3526 } 3527 3528 // Define implicit data-sharing attributes for task. 3529 DVar = Stack->getImplicitDSA(VD, /*FromParent=*/false); 3530 if (((isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared) || 3531 (Stack->getDefaultDSA() == DSA_firstprivate && 3532 DVar.CKind == OMPC_firstprivate && !DVar.RefExpr)) && 3533 !Stack->isLoopControlVariable(VD).first) { 3534 ImplicitFirstprivate.push_back(E); 3535 return; 3536 } 3537 3538 // Store implicitly used globals with declare target link for parent 3539 // target. 3540 if (!isOpenMPTargetExecutionDirective(DKind) && Res && 3541 *Res == OMPDeclareTargetDeclAttr::MT_Link) { 3542 Stack->addToParentTargetRegionLinkGlobals(E); 3543 return; 3544 } 3545 } 3546 } 3547 void VisitMemberExpr(MemberExpr *E) { 3548 if (E->isTypeDependent() || E->isValueDependent() || 3549 E->containsUnexpandedParameterPack() || E->isInstantiationDependent()) 3550 return; 3551 auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); 3552 OpenMPDirectiveKind DKind = Stack->getCurrentDirective(); 3553 if (auto *TE = dyn_cast<CXXThisExpr>(E->getBase()->IgnoreParenCasts())) { 3554 if (!FD) 3555 return; 3556 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(FD, /*FromParent=*/false); 3557 // Check if the variable has explicit DSA set and stop analysis if it 3558 // so. 3559 if (DVar.RefExpr || !ImplicitDeclarations.insert(FD).second) 3560 return; 3561 3562 if (isOpenMPTargetExecutionDirective(DKind) && 3563 !Stack->isLoopControlVariable(FD).first && 3564 !Stack->checkMappableExprComponentListsForDecl( 3565 FD, /*CurrentRegionOnly=*/true, 3566 [](OMPClauseMappableExprCommon::MappableExprComponentListRef 3567 StackComponents, 3568 OpenMPClauseKind) { 3569 return isa<CXXThisExpr>( 3570 cast<MemberExpr>( 3571 StackComponents.back().getAssociatedExpression()) 3572 ->getBase() 3573 ->IgnoreParens()); 3574 })) { 3575 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3] 3576 // A bit-field cannot appear in a map clause. 3577 // 3578 if (FD->isBitField()) 3579 return; 3580 3581 // Check to see if the member expression is referencing a class that 3582 // has already been explicitly mapped 3583 if (Stack->isClassPreviouslyMapped(TE->getType())) 3584 return; 3585 3586 OpenMPDefaultmapClauseModifier Modifier = 3587 Stack->getDefaultmapModifier(OMPC_DEFAULTMAP_aggregate); 3588 OpenMPMapClauseKind Kind = getMapClauseKindFromModifier( 3589 Modifier, /*IsAggregateOrDeclareTarget*/ true); 3590 ImplicitMap[Kind].emplace_back(E); 3591 return; 3592 } 3593 3594 SourceLocation ELoc = E->getExprLoc(); 3595 // OpenMP [2.9.3.6, Restrictions, p.2] 3596 // A list item that appears in a reduction clause of the innermost 3597 // enclosing worksharing or parallel construct may not be accessed in 3598 // an explicit task. 3599 DVar = Stack->hasInnermostDSA( 3600 FD, 3601 [](OpenMPClauseKind C, bool AppliedToPointee) { 3602 return C == OMPC_reduction && !AppliedToPointee; 3603 }, 3604 [](OpenMPDirectiveKind K) { 3605 return isOpenMPParallelDirective(K) || 3606 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K); 3607 }, 3608 /*FromParent=*/true); 3609 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) { 3610 ErrorFound = true; 3611 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); 3612 reportOriginalDsa(SemaRef, Stack, FD, DVar); 3613 return; 3614 } 3615 3616 // Define implicit data-sharing attributes for task. 3617 DVar = Stack->getImplicitDSA(FD, /*FromParent=*/false); 3618 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared && 3619 !Stack->isLoopControlVariable(FD).first) { 3620 // Check if there is a captured expression for the current field in the 3621 // region. Do not mark it as firstprivate unless there is no captured 3622 // expression. 3623 // TODO: try to make it firstprivate. 3624 if (DVar.CKind != OMPC_unknown) 3625 ImplicitFirstprivate.push_back(E); 3626 } 3627 return; 3628 } 3629 if (isOpenMPTargetExecutionDirective(DKind)) { 3630 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents; 3631 if (!checkMapClauseExpressionBase(SemaRef, E, CurComponents, OMPC_map, 3632 /*NoDiagnose=*/true)) 3633 return; 3634 const auto *VD = cast<ValueDecl>( 3635 CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl()); 3636 if (!Stack->checkMappableExprComponentListsForDecl( 3637 VD, /*CurrentRegionOnly=*/true, 3638 [&CurComponents]( 3639 OMPClauseMappableExprCommon::MappableExprComponentListRef 3640 StackComponents, 3641 OpenMPClauseKind) { 3642 auto CCI = CurComponents.rbegin(); 3643 auto CCE = CurComponents.rend(); 3644 for (const auto &SC : llvm::reverse(StackComponents)) { 3645 // Do both expressions have the same kind? 3646 if (CCI->getAssociatedExpression()->getStmtClass() != 3647 SC.getAssociatedExpression()->getStmtClass()) 3648 if (!((isa<OMPArraySectionExpr>( 3649 SC.getAssociatedExpression()) || 3650 isa<OMPArrayShapingExpr>( 3651 SC.getAssociatedExpression())) && 3652 isa<ArraySubscriptExpr>( 3653 CCI->getAssociatedExpression()))) 3654 return false; 3655 3656 const Decl *CCD = CCI->getAssociatedDeclaration(); 3657 const Decl *SCD = SC.getAssociatedDeclaration(); 3658 CCD = CCD ? CCD->getCanonicalDecl() : nullptr; 3659 SCD = SCD ? SCD->getCanonicalDecl() : nullptr; 3660 if (SCD != CCD) 3661 return false; 3662 std::advance(CCI, 1); 3663 if (CCI == CCE) 3664 break; 3665 } 3666 return true; 3667 })) { 3668 Visit(E->getBase()); 3669 } 3670 } else if (!TryCaptureCXXThisMembers) { 3671 Visit(E->getBase()); 3672 } 3673 } 3674 void VisitOMPExecutableDirective(OMPExecutableDirective *S) { 3675 for (OMPClause *C : S->clauses()) { 3676 // Skip analysis of arguments of implicitly defined firstprivate clause 3677 // for task|target directives. 3678 // Skip analysis of arguments of implicitly defined map clause for target 3679 // directives. 3680 if (C && !((isa<OMPFirstprivateClause>(C) || isa<OMPMapClause>(C)) && 3681 C->isImplicit())) { 3682 for (Stmt *CC : C->children()) { 3683 if (CC) 3684 Visit(CC); 3685 } 3686 } 3687 } 3688 // Check implicitly captured variables. 3689 VisitSubCaptures(S); 3690 } 3691 void VisitStmt(Stmt *S) { 3692 for (Stmt *C : S->children()) { 3693 if (C) { 3694 // Check implicitly captured variables in the task-based directives to 3695 // check if they must be firstprivatized. 3696 Visit(C); 3697 } 3698 } 3699 } 3700 3701 void visitSubCaptures(CapturedStmt *S) { 3702 for (const CapturedStmt::Capture &Cap : S->captures()) { 3703 if (!Cap.capturesVariable() && !Cap.capturesVariableByCopy()) 3704 continue; 3705 VarDecl *VD = Cap.getCapturedVar(); 3706 // Do not try to map the variable if it or its sub-component was mapped 3707 // already. 3708 if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) && 3709 Stack->checkMappableExprComponentListsForDecl( 3710 VD, /*CurrentRegionOnly=*/true, 3711 [](OMPClauseMappableExprCommon::MappableExprComponentListRef, 3712 OpenMPClauseKind) { return true; })) 3713 continue; 3714 DeclRefExpr *DRE = buildDeclRefExpr( 3715 SemaRef, VD, VD->getType().getNonLValueExprType(SemaRef.Context), 3716 Cap.getLocation(), /*RefersToCapture=*/true); 3717 Visit(DRE); 3718 } 3719 } 3720 bool isErrorFound() const { return ErrorFound; } 3721 ArrayRef<Expr *> getImplicitFirstprivate() const { 3722 return ImplicitFirstprivate; 3723 } 3724 ArrayRef<Expr *> getImplicitMap(OpenMPDefaultmapClauseKind Kind) const { 3725 return ImplicitMap[Kind]; 3726 } 3727 const Sema::VarsWithInheritedDSAType &getVarsWithInheritedDSA() const { 3728 return VarsWithInheritedDSA; 3729 } 3730 3731 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS) 3732 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) { 3733 // Process declare target link variables for the target directives. 3734 if (isOpenMPTargetExecutionDirective(S->getCurrentDirective())) { 3735 for (DeclRefExpr *E : Stack->getLinkGlobals()) 3736 Visit(E); 3737 } 3738 } 3739 }; 3740 } // namespace 3741 3742 void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) { 3743 switch (DKind) { 3744 case OMPD_parallel: 3745 case OMPD_parallel_for: 3746 case OMPD_parallel_for_simd: 3747 case OMPD_parallel_sections: 3748 case OMPD_parallel_master: 3749 case OMPD_teams: 3750 case OMPD_teams_distribute: 3751 case OMPD_teams_distribute_simd: { 3752 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3753 QualType KmpInt32PtrTy = 3754 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3755 Sema::CapturedParamNameType Params[] = { 3756 std::make_pair(".global_tid.", KmpInt32PtrTy), 3757 std::make_pair(".bound_tid.", KmpInt32PtrTy), 3758 std::make_pair(StringRef(), QualType()) // __context with shared vars 3759 }; 3760 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3761 Params); 3762 break; 3763 } 3764 case OMPD_target_teams: 3765 case OMPD_target_parallel: 3766 case OMPD_target_parallel_for: 3767 case OMPD_target_parallel_for_simd: 3768 case OMPD_target_teams_distribute: 3769 case OMPD_target_teams_distribute_simd: { 3770 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3771 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 3772 QualType KmpInt32PtrTy = 3773 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3774 QualType Args[] = {VoidPtrTy}; 3775 FunctionProtoType::ExtProtoInfo EPI; 3776 EPI.Variadic = true; 3777 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 3778 Sema::CapturedParamNameType Params[] = { 3779 std::make_pair(".global_tid.", KmpInt32Ty), 3780 std::make_pair(".part_id.", KmpInt32PtrTy), 3781 std::make_pair(".privates.", VoidPtrTy), 3782 std::make_pair( 3783 ".copy_fn.", 3784 Context.getPointerType(CopyFnType).withConst().withRestrict()), 3785 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 3786 std::make_pair(StringRef(), QualType()) // __context with shared vars 3787 }; 3788 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3789 Params, /*OpenMPCaptureLevel=*/0); 3790 // Mark this captured region as inlined, because we don't use outlined 3791 // function directly. 3792 getCurCapturedRegion()->TheCapturedDecl->addAttr( 3793 AlwaysInlineAttr::CreateImplicit( 3794 Context, {}, AttributeCommonInfo::AS_Keyword, 3795 AlwaysInlineAttr::Keyword_forceinline)); 3796 Sema::CapturedParamNameType ParamsTarget[] = { 3797 std::make_pair(StringRef(), QualType()) // __context with shared vars 3798 }; 3799 // Start a captured region for 'target' with no implicit parameters. 3800 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3801 ParamsTarget, /*OpenMPCaptureLevel=*/1); 3802 Sema::CapturedParamNameType ParamsTeamsOrParallel[] = { 3803 std::make_pair(".global_tid.", KmpInt32PtrTy), 3804 std::make_pair(".bound_tid.", KmpInt32PtrTy), 3805 std::make_pair(StringRef(), QualType()) // __context with shared vars 3806 }; 3807 // Start a captured region for 'teams' or 'parallel'. Both regions have 3808 // the same implicit parameters. 3809 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3810 ParamsTeamsOrParallel, /*OpenMPCaptureLevel=*/2); 3811 break; 3812 } 3813 case OMPD_target: 3814 case OMPD_target_simd: { 3815 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3816 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 3817 QualType KmpInt32PtrTy = 3818 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3819 QualType Args[] = {VoidPtrTy}; 3820 FunctionProtoType::ExtProtoInfo EPI; 3821 EPI.Variadic = true; 3822 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 3823 Sema::CapturedParamNameType Params[] = { 3824 std::make_pair(".global_tid.", KmpInt32Ty), 3825 std::make_pair(".part_id.", KmpInt32PtrTy), 3826 std::make_pair(".privates.", VoidPtrTy), 3827 std::make_pair( 3828 ".copy_fn.", 3829 Context.getPointerType(CopyFnType).withConst().withRestrict()), 3830 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 3831 std::make_pair(StringRef(), QualType()) // __context with shared vars 3832 }; 3833 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3834 Params, /*OpenMPCaptureLevel=*/0); 3835 // Mark this captured region as inlined, because we don't use outlined 3836 // function directly. 3837 getCurCapturedRegion()->TheCapturedDecl->addAttr( 3838 AlwaysInlineAttr::CreateImplicit( 3839 Context, {}, AttributeCommonInfo::AS_Keyword, 3840 AlwaysInlineAttr::Keyword_forceinline)); 3841 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3842 std::make_pair(StringRef(), QualType()), 3843 /*OpenMPCaptureLevel=*/1); 3844 break; 3845 } 3846 case OMPD_atomic: 3847 case OMPD_critical: 3848 case OMPD_section: 3849 case OMPD_master: 3850 break; 3851 case OMPD_simd: 3852 case OMPD_for: 3853 case OMPD_for_simd: 3854 case OMPD_sections: 3855 case OMPD_single: 3856 case OMPD_taskgroup: 3857 case OMPD_distribute: 3858 case OMPD_distribute_simd: 3859 case OMPD_ordered: 3860 case OMPD_target_data: { 3861 Sema::CapturedParamNameType Params[] = { 3862 std::make_pair(StringRef(), QualType()) // __context with shared vars 3863 }; 3864 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3865 Params); 3866 break; 3867 } 3868 case OMPD_task: { 3869 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3870 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 3871 QualType KmpInt32PtrTy = 3872 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3873 QualType Args[] = {VoidPtrTy}; 3874 FunctionProtoType::ExtProtoInfo EPI; 3875 EPI.Variadic = true; 3876 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 3877 Sema::CapturedParamNameType Params[] = { 3878 std::make_pair(".global_tid.", KmpInt32Ty), 3879 std::make_pair(".part_id.", KmpInt32PtrTy), 3880 std::make_pair(".privates.", VoidPtrTy), 3881 std::make_pair( 3882 ".copy_fn.", 3883 Context.getPointerType(CopyFnType).withConst().withRestrict()), 3884 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 3885 std::make_pair(StringRef(), QualType()) // __context with shared vars 3886 }; 3887 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3888 Params); 3889 // Mark this captured region as inlined, because we don't use outlined 3890 // function directly. 3891 getCurCapturedRegion()->TheCapturedDecl->addAttr( 3892 AlwaysInlineAttr::CreateImplicit( 3893 Context, {}, AttributeCommonInfo::AS_Keyword, 3894 AlwaysInlineAttr::Keyword_forceinline)); 3895 break; 3896 } 3897 case OMPD_taskloop: 3898 case OMPD_taskloop_simd: 3899 case OMPD_master_taskloop: 3900 case OMPD_master_taskloop_simd: { 3901 QualType KmpInt32Ty = 3902 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1) 3903 .withConst(); 3904 QualType KmpUInt64Ty = 3905 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0) 3906 .withConst(); 3907 QualType KmpInt64Ty = 3908 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1) 3909 .withConst(); 3910 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 3911 QualType KmpInt32PtrTy = 3912 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3913 QualType Args[] = {VoidPtrTy}; 3914 FunctionProtoType::ExtProtoInfo EPI; 3915 EPI.Variadic = true; 3916 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 3917 Sema::CapturedParamNameType Params[] = { 3918 std::make_pair(".global_tid.", KmpInt32Ty), 3919 std::make_pair(".part_id.", KmpInt32PtrTy), 3920 std::make_pair(".privates.", VoidPtrTy), 3921 std::make_pair( 3922 ".copy_fn.", 3923 Context.getPointerType(CopyFnType).withConst().withRestrict()), 3924 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 3925 std::make_pair(".lb.", KmpUInt64Ty), 3926 std::make_pair(".ub.", KmpUInt64Ty), 3927 std::make_pair(".st.", KmpInt64Ty), 3928 std::make_pair(".liter.", KmpInt32Ty), 3929 std::make_pair(".reductions.", VoidPtrTy), 3930 std::make_pair(StringRef(), QualType()) // __context with shared vars 3931 }; 3932 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3933 Params); 3934 // Mark this captured region as inlined, because we don't use outlined 3935 // function directly. 3936 getCurCapturedRegion()->TheCapturedDecl->addAttr( 3937 AlwaysInlineAttr::CreateImplicit( 3938 Context, {}, AttributeCommonInfo::AS_Keyword, 3939 AlwaysInlineAttr::Keyword_forceinline)); 3940 break; 3941 } 3942 case OMPD_parallel_master_taskloop: 3943 case OMPD_parallel_master_taskloop_simd: { 3944 QualType KmpInt32Ty = 3945 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1) 3946 .withConst(); 3947 QualType KmpUInt64Ty = 3948 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0) 3949 .withConst(); 3950 QualType KmpInt64Ty = 3951 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1) 3952 .withConst(); 3953 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 3954 QualType KmpInt32PtrTy = 3955 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3956 Sema::CapturedParamNameType ParamsParallel[] = { 3957 std::make_pair(".global_tid.", KmpInt32PtrTy), 3958 std::make_pair(".bound_tid.", KmpInt32PtrTy), 3959 std::make_pair(StringRef(), QualType()) // __context with shared vars 3960 }; 3961 // Start a captured region for 'parallel'. 3962 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3963 ParamsParallel, /*OpenMPCaptureLevel=*/0); 3964 QualType Args[] = {VoidPtrTy}; 3965 FunctionProtoType::ExtProtoInfo EPI; 3966 EPI.Variadic = true; 3967 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 3968 Sema::CapturedParamNameType Params[] = { 3969 std::make_pair(".global_tid.", KmpInt32Ty), 3970 std::make_pair(".part_id.", KmpInt32PtrTy), 3971 std::make_pair(".privates.", VoidPtrTy), 3972 std::make_pair( 3973 ".copy_fn.", 3974 Context.getPointerType(CopyFnType).withConst().withRestrict()), 3975 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 3976 std::make_pair(".lb.", KmpUInt64Ty), 3977 std::make_pair(".ub.", KmpUInt64Ty), 3978 std::make_pair(".st.", KmpInt64Ty), 3979 std::make_pair(".liter.", KmpInt32Ty), 3980 std::make_pair(".reductions.", VoidPtrTy), 3981 std::make_pair(StringRef(), QualType()) // __context with shared vars 3982 }; 3983 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3984 Params, /*OpenMPCaptureLevel=*/1); 3985 // Mark this captured region as inlined, because we don't use outlined 3986 // function directly. 3987 getCurCapturedRegion()->TheCapturedDecl->addAttr( 3988 AlwaysInlineAttr::CreateImplicit( 3989 Context, {}, AttributeCommonInfo::AS_Keyword, 3990 AlwaysInlineAttr::Keyword_forceinline)); 3991 break; 3992 } 3993 case OMPD_distribute_parallel_for_simd: 3994 case OMPD_distribute_parallel_for: { 3995 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3996 QualType KmpInt32PtrTy = 3997 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3998 Sema::CapturedParamNameType Params[] = { 3999 std::make_pair(".global_tid.", KmpInt32PtrTy), 4000 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4001 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 4002 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 4003 std::make_pair(StringRef(), QualType()) // __context with shared vars 4004 }; 4005 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4006 Params); 4007 break; 4008 } 4009 case OMPD_target_teams_distribute_parallel_for: 4010 case OMPD_target_teams_distribute_parallel_for_simd: { 4011 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 4012 QualType KmpInt32PtrTy = 4013 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4014 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 4015 4016 QualType Args[] = {VoidPtrTy}; 4017 FunctionProtoType::ExtProtoInfo EPI; 4018 EPI.Variadic = true; 4019 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 4020 Sema::CapturedParamNameType Params[] = { 4021 std::make_pair(".global_tid.", KmpInt32Ty), 4022 std::make_pair(".part_id.", KmpInt32PtrTy), 4023 std::make_pair(".privates.", VoidPtrTy), 4024 std::make_pair( 4025 ".copy_fn.", 4026 Context.getPointerType(CopyFnType).withConst().withRestrict()), 4027 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 4028 std::make_pair(StringRef(), QualType()) // __context with shared vars 4029 }; 4030 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4031 Params, /*OpenMPCaptureLevel=*/0); 4032 // Mark this captured region as inlined, because we don't use outlined 4033 // function directly. 4034 getCurCapturedRegion()->TheCapturedDecl->addAttr( 4035 AlwaysInlineAttr::CreateImplicit( 4036 Context, {}, AttributeCommonInfo::AS_Keyword, 4037 AlwaysInlineAttr::Keyword_forceinline)); 4038 Sema::CapturedParamNameType ParamsTarget[] = { 4039 std::make_pair(StringRef(), QualType()) // __context with shared vars 4040 }; 4041 // Start a captured region for 'target' with no implicit parameters. 4042 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4043 ParamsTarget, /*OpenMPCaptureLevel=*/1); 4044 4045 Sema::CapturedParamNameType ParamsTeams[] = { 4046 std::make_pair(".global_tid.", KmpInt32PtrTy), 4047 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4048 std::make_pair(StringRef(), QualType()) // __context with shared vars 4049 }; 4050 // Start a captured region for 'target' with no implicit parameters. 4051 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4052 ParamsTeams, /*OpenMPCaptureLevel=*/2); 4053 4054 Sema::CapturedParamNameType ParamsParallel[] = { 4055 std::make_pair(".global_tid.", KmpInt32PtrTy), 4056 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4057 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 4058 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 4059 std::make_pair(StringRef(), QualType()) // __context with shared vars 4060 }; 4061 // Start a captured region for 'teams' or 'parallel'. Both regions have 4062 // the same implicit parameters. 4063 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4064 ParamsParallel, /*OpenMPCaptureLevel=*/3); 4065 break; 4066 } 4067 4068 case OMPD_teams_distribute_parallel_for: 4069 case OMPD_teams_distribute_parallel_for_simd: { 4070 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 4071 QualType KmpInt32PtrTy = 4072 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4073 4074 Sema::CapturedParamNameType ParamsTeams[] = { 4075 std::make_pair(".global_tid.", KmpInt32PtrTy), 4076 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4077 std::make_pair(StringRef(), QualType()) // __context with shared vars 4078 }; 4079 // Start a captured region for 'target' with no implicit parameters. 4080 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4081 ParamsTeams, /*OpenMPCaptureLevel=*/0); 4082 4083 Sema::CapturedParamNameType ParamsParallel[] = { 4084 std::make_pair(".global_tid.", KmpInt32PtrTy), 4085 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4086 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 4087 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 4088 std::make_pair(StringRef(), QualType()) // __context with shared vars 4089 }; 4090 // Start a captured region for 'teams' or 'parallel'. Both regions have 4091 // the same implicit parameters. 4092 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4093 ParamsParallel, /*OpenMPCaptureLevel=*/1); 4094 break; 4095 } 4096 case OMPD_target_update: 4097 case OMPD_target_enter_data: 4098 case OMPD_target_exit_data: { 4099 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 4100 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 4101 QualType KmpInt32PtrTy = 4102 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4103 QualType Args[] = {VoidPtrTy}; 4104 FunctionProtoType::ExtProtoInfo EPI; 4105 EPI.Variadic = true; 4106 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 4107 Sema::CapturedParamNameType Params[] = { 4108 std::make_pair(".global_tid.", KmpInt32Ty), 4109 std::make_pair(".part_id.", KmpInt32PtrTy), 4110 std::make_pair(".privates.", VoidPtrTy), 4111 std::make_pair( 4112 ".copy_fn.", 4113 Context.getPointerType(CopyFnType).withConst().withRestrict()), 4114 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 4115 std::make_pair(StringRef(), QualType()) // __context with shared vars 4116 }; 4117 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4118 Params); 4119 // Mark this captured region as inlined, because we don't use outlined 4120 // function directly. 4121 getCurCapturedRegion()->TheCapturedDecl->addAttr( 4122 AlwaysInlineAttr::CreateImplicit( 4123 Context, {}, AttributeCommonInfo::AS_Keyword, 4124 AlwaysInlineAttr::Keyword_forceinline)); 4125 break; 4126 } 4127 case OMPD_threadprivate: 4128 case OMPD_allocate: 4129 case OMPD_taskyield: 4130 case OMPD_barrier: 4131 case OMPD_taskwait: 4132 case OMPD_cancellation_point: 4133 case OMPD_cancel: 4134 case OMPD_flush: 4135 case OMPD_depobj: 4136 case OMPD_scan: 4137 case OMPD_declare_reduction: 4138 case OMPD_declare_mapper: 4139 case OMPD_declare_simd: 4140 case OMPD_declare_target: 4141 case OMPD_end_declare_target: 4142 case OMPD_requires: 4143 case OMPD_declare_variant: 4144 case OMPD_begin_declare_variant: 4145 case OMPD_end_declare_variant: 4146 llvm_unreachable("OpenMP Directive is not allowed"); 4147 case OMPD_unknown: 4148 default: 4149 llvm_unreachable("Unknown OpenMP directive"); 4150 } 4151 } 4152 4153 int Sema::getNumberOfConstructScopes(unsigned Level) const { 4154 return getOpenMPCaptureLevels(DSAStack->getDirective(Level)); 4155 } 4156 4157 int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) { 4158 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 4159 getOpenMPCaptureRegions(CaptureRegions, DKind); 4160 return CaptureRegions.size(); 4161 } 4162 4163 static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id, 4164 Expr *CaptureExpr, bool WithInit, 4165 bool AsExpression) { 4166 assert(CaptureExpr); 4167 ASTContext &C = S.getASTContext(); 4168 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts(); 4169 QualType Ty = Init->getType(); 4170 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) { 4171 if (S.getLangOpts().CPlusPlus) { 4172 Ty = C.getLValueReferenceType(Ty); 4173 } else { 4174 Ty = C.getPointerType(Ty); 4175 ExprResult Res = 4176 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init); 4177 if (!Res.isUsable()) 4178 return nullptr; 4179 Init = Res.get(); 4180 } 4181 WithInit = true; 4182 } 4183 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty, 4184 CaptureExpr->getBeginLoc()); 4185 if (!WithInit) 4186 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C)); 4187 S.CurContext->addHiddenDecl(CED); 4188 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false); 4189 return CED; 4190 } 4191 4192 static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr, 4193 bool WithInit) { 4194 OMPCapturedExprDecl *CD; 4195 if (VarDecl *VD = S.isOpenMPCapturedDecl(D)) 4196 CD = cast<OMPCapturedExprDecl>(VD); 4197 else 4198 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit, 4199 /*AsExpression=*/false); 4200 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(), 4201 CaptureExpr->getExprLoc()); 4202 } 4203 4204 static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) { 4205 CaptureExpr = S.DefaultLvalueConversion(CaptureExpr).get(); 4206 if (!Ref) { 4207 OMPCapturedExprDecl *CD = buildCaptureDecl( 4208 S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr, 4209 /*WithInit=*/true, /*AsExpression=*/true); 4210 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(), 4211 CaptureExpr->getExprLoc()); 4212 } 4213 ExprResult Res = Ref; 4214 if (!S.getLangOpts().CPlusPlus && 4215 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() && 4216 Ref->getType()->isPointerType()) { 4217 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref); 4218 if (!Res.isUsable()) 4219 return ExprError(); 4220 } 4221 return S.DefaultLvalueConversion(Res.get()); 4222 } 4223 4224 namespace { 4225 // OpenMP directives parsed in this section are represented as a 4226 // CapturedStatement with an associated statement. If a syntax error 4227 // is detected during the parsing of the associated statement, the 4228 // compiler must abort processing and close the CapturedStatement. 4229 // 4230 // Combined directives such as 'target parallel' have more than one 4231 // nested CapturedStatements. This RAII ensures that we unwind out 4232 // of all the nested CapturedStatements when an error is found. 4233 class CaptureRegionUnwinderRAII { 4234 private: 4235 Sema &S; 4236 bool &ErrorFound; 4237 OpenMPDirectiveKind DKind = OMPD_unknown; 4238 4239 public: 4240 CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound, 4241 OpenMPDirectiveKind DKind) 4242 : S(S), ErrorFound(ErrorFound), DKind(DKind) {} 4243 ~CaptureRegionUnwinderRAII() { 4244 if (ErrorFound) { 4245 int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind); 4246 while (--ThisCaptureLevel >= 0) 4247 S.ActOnCapturedRegionError(); 4248 } 4249 } 4250 }; 4251 } // namespace 4252 4253 void Sema::tryCaptureOpenMPLambdas(ValueDecl *V) { 4254 // Capture variables captured by reference in lambdas for target-based 4255 // directives. 4256 if (!CurContext->isDependentContext() && 4257 (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) || 4258 isOpenMPTargetDataManagementDirective( 4259 DSAStack->getCurrentDirective()))) { 4260 QualType Type = V->getType(); 4261 if (const auto *RD = Type.getCanonicalType() 4262 .getNonReferenceType() 4263 ->getAsCXXRecordDecl()) { 4264 bool SavedForceCaptureByReferenceInTargetExecutable = 4265 DSAStack->isForceCaptureByReferenceInTargetExecutable(); 4266 DSAStack->setForceCaptureByReferenceInTargetExecutable( 4267 /*V=*/true); 4268 if (RD->isLambda()) { 4269 llvm::DenseMap<const VarDecl *, FieldDecl *> Captures; 4270 FieldDecl *ThisCapture; 4271 RD->getCaptureFields(Captures, ThisCapture); 4272 for (const LambdaCapture &LC : RD->captures()) { 4273 if (LC.getCaptureKind() == LCK_ByRef) { 4274 VarDecl *VD = LC.getCapturedVar(); 4275 DeclContext *VDC = VD->getDeclContext(); 4276 if (!VDC->Encloses(CurContext)) 4277 continue; 4278 MarkVariableReferenced(LC.getLocation(), VD); 4279 } else if (LC.getCaptureKind() == LCK_This) { 4280 QualType ThisTy = getCurrentThisType(); 4281 if (!ThisTy.isNull() && 4282 Context.typesAreCompatible(ThisTy, ThisCapture->getType())) 4283 CheckCXXThisCapture(LC.getLocation()); 4284 } 4285 } 4286 } 4287 DSAStack->setForceCaptureByReferenceInTargetExecutable( 4288 SavedForceCaptureByReferenceInTargetExecutable); 4289 } 4290 } 4291 } 4292 4293 static bool checkOrderedOrderSpecified(Sema &S, 4294 const ArrayRef<OMPClause *> Clauses) { 4295 const OMPOrderedClause *Ordered = nullptr; 4296 const OMPOrderClause *Order = nullptr; 4297 4298 for (const OMPClause *Clause : Clauses) { 4299 if (Clause->getClauseKind() == OMPC_ordered) 4300 Ordered = cast<OMPOrderedClause>(Clause); 4301 else if (Clause->getClauseKind() == OMPC_order) { 4302 Order = cast<OMPOrderClause>(Clause); 4303 if (Order->getKind() != OMPC_ORDER_concurrent) 4304 Order = nullptr; 4305 } 4306 if (Ordered && Order) 4307 break; 4308 } 4309 4310 if (Ordered && Order) { 4311 S.Diag(Order->getKindKwLoc(), 4312 diag::err_omp_simple_clause_incompatible_with_ordered) 4313 << getOpenMPClauseName(OMPC_order) 4314 << getOpenMPSimpleClauseTypeName(OMPC_order, OMPC_ORDER_concurrent) 4315 << SourceRange(Order->getBeginLoc(), Order->getEndLoc()); 4316 S.Diag(Ordered->getBeginLoc(), diag::note_omp_ordered_param) 4317 << 0 << SourceRange(Ordered->getBeginLoc(), Ordered->getEndLoc()); 4318 return true; 4319 } 4320 return false; 4321 } 4322 4323 StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S, 4324 ArrayRef<OMPClause *> Clauses) { 4325 if (DSAStack->getCurrentDirective() == OMPD_atomic || 4326 DSAStack->getCurrentDirective() == OMPD_critical || 4327 DSAStack->getCurrentDirective() == OMPD_section || 4328 DSAStack->getCurrentDirective() == OMPD_master) 4329 return S; 4330 4331 bool ErrorFound = false; 4332 CaptureRegionUnwinderRAII CaptureRegionUnwinder( 4333 *this, ErrorFound, DSAStack->getCurrentDirective()); 4334 if (!S.isUsable()) { 4335 ErrorFound = true; 4336 return StmtError(); 4337 } 4338 4339 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 4340 getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective()); 4341 OMPOrderedClause *OC = nullptr; 4342 OMPScheduleClause *SC = nullptr; 4343 SmallVector<const OMPLinearClause *, 4> LCs; 4344 SmallVector<const OMPClauseWithPreInit *, 4> PICs; 4345 // This is required for proper codegen. 4346 for (OMPClause *Clause : Clauses) { 4347 if (!LangOpts.OpenMPSimd && 4348 isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) && 4349 Clause->getClauseKind() == OMPC_in_reduction) { 4350 // Capture taskgroup task_reduction descriptors inside the tasking regions 4351 // with the corresponding in_reduction items. 4352 auto *IRC = cast<OMPInReductionClause>(Clause); 4353 for (Expr *E : IRC->taskgroup_descriptors()) 4354 if (E) 4355 MarkDeclarationsReferencedInExpr(E); 4356 } 4357 if (isOpenMPPrivate(Clause->getClauseKind()) || 4358 Clause->getClauseKind() == OMPC_copyprivate || 4359 (getLangOpts().OpenMPUseTLS && 4360 getASTContext().getTargetInfo().isTLSSupported() && 4361 Clause->getClauseKind() == OMPC_copyin)) { 4362 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin); 4363 // Mark all variables in private list clauses as used in inner region. 4364 for (Stmt *VarRef : Clause->children()) { 4365 if (auto *E = cast_or_null<Expr>(VarRef)) { 4366 MarkDeclarationsReferencedInExpr(E); 4367 } 4368 } 4369 DSAStack->setForceVarCapturing(/*V=*/false); 4370 } else if (CaptureRegions.size() > 1 || 4371 CaptureRegions.back() != OMPD_unknown) { 4372 if (auto *C = OMPClauseWithPreInit::get(Clause)) 4373 PICs.push_back(C); 4374 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) { 4375 if (Expr *E = C->getPostUpdateExpr()) 4376 MarkDeclarationsReferencedInExpr(E); 4377 } 4378 } 4379 if (Clause->getClauseKind() == OMPC_schedule) 4380 SC = cast<OMPScheduleClause>(Clause); 4381 else if (Clause->getClauseKind() == OMPC_ordered) 4382 OC = cast<OMPOrderedClause>(Clause); 4383 else if (Clause->getClauseKind() == OMPC_linear) 4384 LCs.push_back(cast<OMPLinearClause>(Clause)); 4385 } 4386 // Capture allocator expressions if used. 4387 for (Expr *E : DSAStack->getInnerAllocators()) 4388 MarkDeclarationsReferencedInExpr(E); 4389 // OpenMP, 2.7.1 Loop Construct, Restrictions 4390 // The nonmonotonic modifier cannot be specified if an ordered clause is 4391 // specified. 4392 if (SC && 4393 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic || 4394 SC->getSecondScheduleModifier() == 4395 OMPC_SCHEDULE_MODIFIER_nonmonotonic) && 4396 OC) { 4397 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic 4398 ? SC->getFirstScheduleModifierLoc() 4399 : SC->getSecondScheduleModifierLoc(), 4400 diag::err_omp_simple_clause_incompatible_with_ordered) 4401 << getOpenMPClauseName(OMPC_schedule) 4402 << getOpenMPSimpleClauseTypeName(OMPC_schedule, 4403 OMPC_SCHEDULE_MODIFIER_nonmonotonic) 4404 << SourceRange(OC->getBeginLoc(), OC->getEndLoc()); 4405 ErrorFound = true; 4406 } 4407 // OpenMP 5.0, 2.9.2 Worksharing-Loop Construct, Restrictions. 4408 // If an order(concurrent) clause is present, an ordered clause may not appear 4409 // on the same directive. 4410 if (checkOrderedOrderSpecified(*this, Clauses)) 4411 ErrorFound = true; 4412 if (!LCs.empty() && OC && OC->getNumForLoops()) { 4413 for (const OMPLinearClause *C : LCs) { 4414 Diag(C->getBeginLoc(), diag::err_omp_linear_ordered) 4415 << SourceRange(OC->getBeginLoc(), OC->getEndLoc()); 4416 } 4417 ErrorFound = true; 4418 } 4419 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) && 4420 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC && 4421 OC->getNumForLoops()) { 4422 Diag(OC->getBeginLoc(), diag::err_omp_ordered_simd) 4423 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 4424 ErrorFound = true; 4425 } 4426 if (ErrorFound) { 4427 return StmtError(); 4428 } 4429 StmtResult SR = S; 4430 unsigned CompletedRegions = 0; 4431 for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(CaptureRegions)) { 4432 // Mark all variables in private list clauses as used in inner region. 4433 // Required for proper codegen of combined directives. 4434 // TODO: add processing for other clauses. 4435 if (ThisCaptureRegion != OMPD_unknown) { 4436 for (const clang::OMPClauseWithPreInit *C : PICs) { 4437 OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion(); 4438 // Find the particular capture region for the clause if the 4439 // directive is a combined one with multiple capture regions. 4440 // If the directive is not a combined one, the capture region 4441 // associated with the clause is OMPD_unknown and is generated 4442 // only once. 4443 if (CaptureRegion == ThisCaptureRegion || 4444 CaptureRegion == OMPD_unknown) { 4445 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) { 4446 for (Decl *D : DS->decls()) 4447 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D)); 4448 } 4449 } 4450 } 4451 } 4452 if (ThisCaptureRegion == OMPD_target) { 4453 // Capture allocator traits in the target region. They are used implicitly 4454 // and, thus, are not captured by default. 4455 for (OMPClause *C : Clauses) { 4456 if (const auto *UAC = dyn_cast<OMPUsesAllocatorsClause>(C)) { 4457 for (unsigned I = 0, End = UAC->getNumberOfAllocators(); I < End; 4458 ++I) { 4459 OMPUsesAllocatorsClause::Data D = UAC->getAllocatorData(I); 4460 if (Expr *E = D.AllocatorTraits) 4461 MarkDeclarationsReferencedInExpr(E); 4462 } 4463 continue; 4464 } 4465 } 4466 } 4467 if (++CompletedRegions == CaptureRegions.size()) 4468 DSAStack->setBodyComplete(); 4469 SR = ActOnCapturedRegionEnd(SR.get()); 4470 } 4471 return SR; 4472 } 4473 4474 static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion, 4475 OpenMPDirectiveKind CancelRegion, 4476 SourceLocation StartLoc) { 4477 // CancelRegion is only needed for cancel and cancellation_point. 4478 if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point) 4479 return false; 4480 4481 if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for || 4482 CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup) 4483 return false; 4484 4485 SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region) 4486 << getOpenMPDirectiveName(CancelRegion); 4487 return true; 4488 } 4489 4490 static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack, 4491 OpenMPDirectiveKind CurrentRegion, 4492 const DeclarationNameInfo &CurrentName, 4493 OpenMPDirectiveKind CancelRegion, 4494 SourceLocation StartLoc) { 4495 if (Stack->getCurScope()) { 4496 OpenMPDirectiveKind ParentRegion = Stack->getParentDirective(); 4497 OpenMPDirectiveKind OffendingRegion = ParentRegion; 4498 bool NestingProhibited = false; 4499 bool CloseNesting = true; 4500 bool OrphanSeen = false; 4501 enum { 4502 NoRecommend, 4503 ShouldBeInParallelRegion, 4504 ShouldBeInOrderedRegion, 4505 ShouldBeInTargetRegion, 4506 ShouldBeInTeamsRegion, 4507 ShouldBeInLoopSimdRegion, 4508 } Recommend = NoRecommend; 4509 if (isOpenMPSimdDirective(ParentRegion) && 4510 ((SemaRef.LangOpts.OpenMP <= 45 && CurrentRegion != OMPD_ordered) || 4511 (SemaRef.LangOpts.OpenMP >= 50 && CurrentRegion != OMPD_ordered && 4512 CurrentRegion != OMPD_simd && CurrentRegion != OMPD_atomic && 4513 CurrentRegion != OMPD_scan))) { 4514 // OpenMP [2.16, Nesting of Regions] 4515 // OpenMP constructs may not be nested inside a simd region. 4516 // OpenMP [2.8.1,simd Construct, Restrictions] 4517 // An ordered construct with the simd clause is the only OpenMP 4518 // construct that can appear in the simd region. 4519 // Allowing a SIMD construct nested in another SIMD construct is an 4520 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning 4521 // message. 4522 // OpenMP 5.0 [2.9.3.1, simd Construct, Restrictions] 4523 // The only OpenMP constructs that can be encountered during execution of 4524 // a simd region are the atomic construct, the loop construct, the simd 4525 // construct and the ordered construct with the simd clause. 4526 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd) 4527 ? diag::err_omp_prohibited_region_simd 4528 : diag::warn_omp_nesting_simd) 4529 << (SemaRef.LangOpts.OpenMP >= 50 ? 1 : 0); 4530 return CurrentRegion != OMPD_simd; 4531 } 4532 if (ParentRegion == OMPD_atomic) { 4533 // OpenMP [2.16, Nesting of Regions] 4534 // OpenMP constructs may not be nested inside an atomic region. 4535 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic); 4536 return true; 4537 } 4538 if (CurrentRegion == OMPD_section) { 4539 // OpenMP [2.7.2, sections Construct, Restrictions] 4540 // Orphaned section directives are prohibited. That is, the section 4541 // directives must appear within the sections construct and must not be 4542 // encountered elsewhere in the sections region. 4543 if (ParentRegion != OMPD_sections && 4544 ParentRegion != OMPD_parallel_sections) { 4545 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive) 4546 << (ParentRegion != OMPD_unknown) 4547 << getOpenMPDirectiveName(ParentRegion); 4548 return true; 4549 } 4550 return false; 4551 } 4552 // Allow some constructs (except teams and cancellation constructs) to be 4553 // orphaned (they could be used in functions, called from OpenMP regions 4554 // with the required preconditions). 4555 if (ParentRegion == OMPD_unknown && 4556 !isOpenMPNestingTeamsDirective(CurrentRegion) && 4557 CurrentRegion != OMPD_cancellation_point && 4558 CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_scan) 4559 return false; 4560 if (CurrentRegion == OMPD_cancellation_point || 4561 CurrentRegion == OMPD_cancel) { 4562 // OpenMP [2.16, Nesting of Regions] 4563 // A cancellation point construct for which construct-type-clause is 4564 // taskgroup must be nested inside a task construct. A cancellation 4565 // point construct for which construct-type-clause is not taskgroup must 4566 // be closely nested inside an OpenMP construct that matches the type 4567 // specified in construct-type-clause. 4568 // A cancel construct for which construct-type-clause is taskgroup must be 4569 // nested inside a task construct. A cancel construct for which 4570 // construct-type-clause is not taskgroup must be closely nested inside an 4571 // OpenMP construct that matches the type specified in 4572 // construct-type-clause. 4573 NestingProhibited = 4574 !((CancelRegion == OMPD_parallel && 4575 (ParentRegion == OMPD_parallel || 4576 ParentRegion == OMPD_target_parallel)) || 4577 (CancelRegion == OMPD_for && 4578 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for || 4579 ParentRegion == OMPD_target_parallel_for || 4580 ParentRegion == OMPD_distribute_parallel_for || 4581 ParentRegion == OMPD_teams_distribute_parallel_for || 4582 ParentRegion == OMPD_target_teams_distribute_parallel_for)) || 4583 (CancelRegion == OMPD_taskgroup && 4584 (ParentRegion == OMPD_task || 4585 (SemaRef.getLangOpts().OpenMP >= 50 && 4586 (ParentRegion == OMPD_taskloop || 4587 ParentRegion == OMPD_master_taskloop || 4588 ParentRegion == OMPD_parallel_master_taskloop)))) || 4589 (CancelRegion == OMPD_sections && 4590 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections || 4591 ParentRegion == OMPD_parallel_sections))); 4592 OrphanSeen = ParentRegion == OMPD_unknown; 4593 } else if (CurrentRegion == OMPD_master) { 4594 // OpenMP [2.16, Nesting of Regions] 4595 // A master region may not be closely nested inside a worksharing, 4596 // atomic, or explicit task region. 4597 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 4598 isOpenMPTaskingDirective(ParentRegion); 4599 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) { 4600 // OpenMP [2.16, Nesting of Regions] 4601 // A critical region may not be nested (closely or otherwise) inside a 4602 // critical region with the same name. Note that this restriction is not 4603 // sufficient to prevent deadlock. 4604 SourceLocation PreviousCriticalLoc; 4605 bool DeadLock = Stack->hasDirective( 4606 [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K, 4607 const DeclarationNameInfo &DNI, 4608 SourceLocation Loc) { 4609 if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) { 4610 PreviousCriticalLoc = Loc; 4611 return true; 4612 } 4613 return false; 4614 }, 4615 false /* skip top directive */); 4616 if (DeadLock) { 4617 SemaRef.Diag(StartLoc, 4618 diag::err_omp_prohibited_region_critical_same_name) 4619 << CurrentName.getName(); 4620 if (PreviousCriticalLoc.isValid()) 4621 SemaRef.Diag(PreviousCriticalLoc, 4622 diag::note_omp_previous_critical_region); 4623 return true; 4624 } 4625 } else if (CurrentRegion == OMPD_barrier) { 4626 // OpenMP [2.16, Nesting of Regions] 4627 // A barrier region may not be closely nested inside a worksharing, 4628 // explicit task, critical, ordered, atomic, or master region. 4629 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 4630 isOpenMPTaskingDirective(ParentRegion) || 4631 ParentRegion == OMPD_master || 4632 ParentRegion == OMPD_parallel_master || 4633 ParentRegion == OMPD_critical || 4634 ParentRegion == OMPD_ordered; 4635 } else if (isOpenMPWorksharingDirective(CurrentRegion) && 4636 !isOpenMPParallelDirective(CurrentRegion) && 4637 !isOpenMPTeamsDirective(CurrentRegion)) { 4638 // OpenMP [2.16, Nesting of Regions] 4639 // A worksharing region may not be closely nested inside a worksharing, 4640 // explicit task, critical, ordered, atomic, or master region. 4641 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 4642 isOpenMPTaskingDirective(ParentRegion) || 4643 ParentRegion == OMPD_master || 4644 ParentRegion == OMPD_parallel_master || 4645 ParentRegion == OMPD_critical || 4646 ParentRegion == OMPD_ordered; 4647 Recommend = ShouldBeInParallelRegion; 4648 } else if (CurrentRegion == OMPD_ordered) { 4649 // OpenMP [2.16, Nesting of Regions] 4650 // An ordered region may not be closely nested inside a critical, 4651 // atomic, or explicit task region. 4652 // An ordered region must be closely nested inside a loop region (or 4653 // parallel loop region) with an ordered clause. 4654 // OpenMP [2.8.1,simd Construct, Restrictions] 4655 // An ordered construct with the simd clause is the only OpenMP construct 4656 // that can appear in the simd region. 4657 NestingProhibited = ParentRegion == OMPD_critical || 4658 isOpenMPTaskingDirective(ParentRegion) || 4659 !(isOpenMPSimdDirective(ParentRegion) || 4660 Stack->isParentOrderedRegion()); 4661 Recommend = ShouldBeInOrderedRegion; 4662 } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) { 4663 // OpenMP [2.16, Nesting of Regions] 4664 // If specified, a teams construct must be contained within a target 4665 // construct. 4666 NestingProhibited = 4667 (SemaRef.LangOpts.OpenMP <= 45 && ParentRegion != OMPD_target) || 4668 (SemaRef.LangOpts.OpenMP >= 50 && ParentRegion != OMPD_unknown && 4669 ParentRegion != OMPD_target); 4670 OrphanSeen = ParentRegion == OMPD_unknown; 4671 Recommend = ShouldBeInTargetRegion; 4672 } else if (CurrentRegion == OMPD_scan) { 4673 // OpenMP [2.16, Nesting of Regions] 4674 // If specified, a teams construct must be contained within a target 4675 // construct. 4676 NestingProhibited = 4677 SemaRef.LangOpts.OpenMP < 50 || 4678 (ParentRegion != OMPD_simd && ParentRegion != OMPD_for && 4679 ParentRegion != OMPD_for_simd && ParentRegion != OMPD_parallel_for && 4680 ParentRegion != OMPD_parallel_for_simd); 4681 OrphanSeen = ParentRegion == OMPD_unknown; 4682 Recommend = ShouldBeInLoopSimdRegion; 4683 } 4684 if (!NestingProhibited && 4685 !isOpenMPTargetExecutionDirective(CurrentRegion) && 4686 !isOpenMPTargetDataManagementDirective(CurrentRegion) && 4687 (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) { 4688 // OpenMP [2.16, Nesting of Regions] 4689 // distribute, parallel, parallel sections, parallel workshare, and the 4690 // parallel loop and parallel loop SIMD constructs are the only OpenMP 4691 // constructs that can be closely nested in the teams region. 4692 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) && 4693 !isOpenMPDistributeDirective(CurrentRegion); 4694 Recommend = ShouldBeInParallelRegion; 4695 } 4696 if (!NestingProhibited && 4697 isOpenMPNestingDistributeDirective(CurrentRegion)) { 4698 // OpenMP 4.5 [2.17 Nesting of Regions] 4699 // The region associated with the distribute construct must be strictly 4700 // nested inside a teams region 4701 NestingProhibited = 4702 (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams); 4703 Recommend = ShouldBeInTeamsRegion; 4704 } 4705 if (!NestingProhibited && 4706 (isOpenMPTargetExecutionDirective(CurrentRegion) || 4707 isOpenMPTargetDataManagementDirective(CurrentRegion))) { 4708 // OpenMP 4.5 [2.17 Nesting of Regions] 4709 // If a target, target update, target data, target enter data, or 4710 // target exit data construct is encountered during execution of a 4711 // target region, the behavior is unspecified. 4712 NestingProhibited = Stack->hasDirective( 4713 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &, 4714 SourceLocation) { 4715 if (isOpenMPTargetExecutionDirective(K)) { 4716 OffendingRegion = K; 4717 return true; 4718 } 4719 return false; 4720 }, 4721 false /* don't skip top directive */); 4722 CloseNesting = false; 4723 } 4724 if (NestingProhibited) { 4725 if (OrphanSeen) { 4726 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive) 4727 << getOpenMPDirectiveName(CurrentRegion) << Recommend; 4728 } else { 4729 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region) 4730 << CloseNesting << getOpenMPDirectiveName(OffendingRegion) 4731 << Recommend << getOpenMPDirectiveName(CurrentRegion); 4732 } 4733 return true; 4734 } 4735 } 4736 return false; 4737 } 4738 4739 struct Kind2Unsigned { 4740 using argument_type = OpenMPDirectiveKind; 4741 unsigned operator()(argument_type DK) { return unsigned(DK); } 4742 }; 4743 static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind, 4744 ArrayRef<OMPClause *> Clauses, 4745 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) { 4746 bool ErrorFound = false; 4747 unsigned NamedModifiersNumber = 0; 4748 llvm::IndexedMap<const OMPIfClause *, Kind2Unsigned> FoundNameModifiers; 4749 FoundNameModifiers.resize(llvm::omp::Directive_enumSize + 1); 4750 SmallVector<SourceLocation, 4> NameModifierLoc; 4751 for (const OMPClause *C : Clauses) { 4752 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) { 4753 // At most one if clause without a directive-name-modifier can appear on 4754 // the directive. 4755 OpenMPDirectiveKind CurNM = IC->getNameModifier(); 4756 if (FoundNameModifiers[CurNM]) { 4757 S.Diag(C->getBeginLoc(), diag::err_omp_more_one_clause) 4758 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if) 4759 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM); 4760 ErrorFound = true; 4761 } else if (CurNM != OMPD_unknown) { 4762 NameModifierLoc.push_back(IC->getNameModifierLoc()); 4763 ++NamedModifiersNumber; 4764 } 4765 FoundNameModifiers[CurNM] = IC; 4766 if (CurNM == OMPD_unknown) 4767 continue; 4768 // Check if the specified name modifier is allowed for the current 4769 // directive. 4770 // At most one if clause with the particular directive-name-modifier can 4771 // appear on the directive. 4772 bool MatchFound = false; 4773 for (auto NM : AllowedNameModifiers) { 4774 if (CurNM == NM) { 4775 MatchFound = true; 4776 break; 4777 } 4778 } 4779 if (!MatchFound) { 4780 S.Diag(IC->getNameModifierLoc(), 4781 diag::err_omp_wrong_if_directive_name_modifier) 4782 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind); 4783 ErrorFound = true; 4784 } 4785 } 4786 } 4787 // If any if clause on the directive includes a directive-name-modifier then 4788 // all if clauses on the directive must include a directive-name-modifier. 4789 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) { 4790 if (NamedModifiersNumber == AllowedNameModifiers.size()) { 4791 S.Diag(FoundNameModifiers[OMPD_unknown]->getBeginLoc(), 4792 diag::err_omp_no_more_if_clause); 4793 } else { 4794 std::string Values; 4795 std::string Sep(", "); 4796 unsigned AllowedCnt = 0; 4797 unsigned TotalAllowedNum = 4798 AllowedNameModifiers.size() - NamedModifiersNumber; 4799 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End; 4800 ++Cnt) { 4801 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt]; 4802 if (!FoundNameModifiers[NM]) { 4803 Values += "'"; 4804 Values += getOpenMPDirectiveName(NM); 4805 Values += "'"; 4806 if (AllowedCnt + 2 == TotalAllowedNum) 4807 Values += " or "; 4808 else if (AllowedCnt + 1 != TotalAllowedNum) 4809 Values += Sep; 4810 ++AllowedCnt; 4811 } 4812 } 4813 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getBeginLoc(), 4814 diag::err_omp_unnamed_if_clause) 4815 << (TotalAllowedNum > 1) << Values; 4816 } 4817 for (SourceLocation Loc : NameModifierLoc) { 4818 S.Diag(Loc, diag::note_omp_previous_named_if_clause); 4819 } 4820 ErrorFound = true; 4821 } 4822 return ErrorFound; 4823 } 4824 4825 static std::pair<ValueDecl *, bool> getPrivateItem(Sema &S, Expr *&RefExpr, 4826 SourceLocation &ELoc, 4827 SourceRange &ERange, 4828 bool AllowArraySection) { 4829 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() || 4830 RefExpr->containsUnexpandedParameterPack()) 4831 return std::make_pair(nullptr, true); 4832 4833 // OpenMP [3.1, C/C++] 4834 // A list item is a variable name. 4835 // OpenMP [2.9.3.3, Restrictions, p.1] 4836 // A variable that is part of another variable (as an array or 4837 // structure element) cannot appear in a private clause. 4838 RefExpr = RefExpr->IgnoreParens(); 4839 enum { 4840 NoArrayExpr = -1, 4841 ArraySubscript = 0, 4842 OMPArraySection = 1 4843 } IsArrayExpr = NoArrayExpr; 4844 if (AllowArraySection) { 4845 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) { 4846 Expr *Base = ASE->getBase()->IgnoreParenImpCasts(); 4847 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 4848 Base = TempASE->getBase()->IgnoreParenImpCasts(); 4849 RefExpr = Base; 4850 IsArrayExpr = ArraySubscript; 4851 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) { 4852 Expr *Base = OASE->getBase()->IgnoreParenImpCasts(); 4853 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) 4854 Base = TempOASE->getBase()->IgnoreParenImpCasts(); 4855 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 4856 Base = TempASE->getBase()->IgnoreParenImpCasts(); 4857 RefExpr = Base; 4858 IsArrayExpr = OMPArraySection; 4859 } 4860 } 4861 ELoc = RefExpr->getExprLoc(); 4862 ERange = RefExpr->getSourceRange(); 4863 RefExpr = RefExpr->IgnoreParenImpCasts(); 4864 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr); 4865 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr); 4866 if ((!DE || !isa<VarDecl>(DE->getDecl())) && 4867 (S.getCurrentThisType().isNull() || !ME || 4868 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) || 4869 !isa<FieldDecl>(ME->getMemberDecl()))) { 4870 if (IsArrayExpr != NoArrayExpr) { 4871 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr 4872 << ERange; 4873 } else { 4874 S.Diag(ELoc, 4875 AllowArraySection 4876 ? diag::err_omp_expected_var_name_member_expr_or_array_item 4877 : diag::err_omp_expected_var_name_member_expr) 4878 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange; 4879 } 4880 return std::make_pair(nullptr, false); 4881 } 4882 return std::make_pair( 4883 getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false); 4884 } 4885 4886 namespace { 4887 /// Checks if the allocator is used in uses_allocators clause to be allowed in 4888 /// target regions. 4889 class AllocatorChecker final : public ConstStmtVisitor<AllocatorChecker, bool> { 4890 DSAStackTy *S = nullptr; 4891 4892 public: 4893 bool VisitDeclRefExpr(const DeclRefExpr *E) { 4894 return S->isUsesAllocatorsDecl(E->getDecl()) 4895 .getValueOr( 4896 DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait) == 4897 DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait; 4898 } 4899 bool VisitStmt(const Stmt *S) { 4900 for (const Stmt *Child : S->children()) { 4901 if (Child && Visit(Child)) 4902 return true; 4903 } 4904 return false; 4905 } 4906 explicit AllocatorChecker(DSAStackTy *S) : S(S) {} 4907 }; 4908 } // namespace 4909 4910 static void checkAllocateClauses(Sema &S, DSAStackTy *Stack, 4911 ArrayRef<OMPClause *> Clauses) { 4912 assert(!S.CurContext->isDependentContext() && 4913 "Expected non-dependent context."); 4914 auto AllocateRange = 4915 llvm::make_filter_range(Clauses, OMPAllocateClause::classof); 4916 llvm::DenseMap<CanonicalDeclPtr<Decl>, CanonicalDeclPtr<VarDecl>> 4917 DeclToCopy; 4918 auto PrivateRange = llvm::make_filter_range(Clauses, [](const OMPClause *C) { 4919 return isOpenMPPrivate(C->getClauseKind()); 4920 }); 4921 for (OMPClause *Cl : PrivateRange) { 4922 MutableArrayRef<Expr *>::iterator I, It, Et; 4923 if (Cl->getClauseKind() == OMPC_private) { 4924 auto *PC = cast<OMPPrivateClause>(Cl); 4925 I = PC->private_copies().begin(); 4926 It = PC->varlist_begin(); 4927 Et = PC->varlist_end(); 4928 } else if (Cl->getClauseKind() == OMPC_firstprivate) { 4929 auto *PC = cast<OMPFirstprivateClause>(Cl); 4930 I = PC->private_copies().begin(); 4931 It = PC->varlist_begin(); 4932 Et = PC->varlist_end(); 4933 } else if (Cl->getClauseKind() == OMPC_lastprivate) { 4934 auto *PC = cast<OMPLastprivateClause>(Cl); 4935 I = PC->private_copies().begin(); 4936 It = PC->varlist_begin(); 4937 Et = PC->varlist_end(); 4938 } else if (Cl->getClauseKind() == OMPC_linear) { 4939 auto *PC = cast<OMPLinearClause>(Cl); 4940 I = PC->privates().begin(); 4941 It = PC->varlist_begin(); 4942 Et = PC->varlist_end(); 4943 } else if (Cl->getClauseKind() == OMPC_reduction) { 4944 auto *PC = cast<OMPReductionClause>(Cl); 4945 I = PC->privates().begin(); 4946 It = PC->varlist_begin(); 4947 Et = PC->varlist_end(); 4948 } else if (Cl->getClauseKind() == OMPC_task_reduction) { 4949 auto *PC = cast<OMPTaskReductionClause>(Cl); 4950 I = PC->privates().begin(); 4951 It = PC->varlist_begin(); 4952 Et = PC->varlist_end(); 4953 } else if (Cl->getClauseKind() == OMPC_in_reduction) { 4954 auto *PC = cast<OMPInReductionClause>(Cl); 4955 I = PC->privates().begin(); 4956 It = PC->varlist_begin(); 4957 Et = PC->varlist_end(); 4958 } else { 4959 llvm_unreachable("Expected private clause."); 4960 } 4961 for (Expr *E : llvm::make_range(It, Et)) { 4962 if (!*I) { 4963 ++I; 4964 continue; 4965 } 4966 SourceLocation ELoc; 4967 SourceRange ERange; 4968 Expr *SimpleRefExpr = E; 4969 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange, 4970 /*AllowArraySection=*/true); 4971 DeclToCopy.try_emplace(Res.first, 4972 cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl())); 4973 ++I; 4974 } 4975 } 4976 for (OMPClause *C : AllocateRange) { 4977 auto *AC = cast<OMPAllocateClause>(C); 4978 if (S.getLangOpts().OpenMP >= 50 && 4979 !Stack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>() && 4980 isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) && 4981 AC->getAllocator()) { 4982 Expr *Allocator = AC->getAllocator(); 4983 // OpenMP, 2.12.5 target Construct 4984 // Memory allocators that do not appear in a uses_allocators clause cannot 4985 // appear as an allocator in an allocate clause or be used in the target 4986 // region unless a requires directive with the dynamic_allocators clause 4987 // is present in the same compilation unit. 4988 AllocatorChecker Checker(Stack); 4989 if (Checker.Visit(Allocator)) 4990 S.Diag(Allocator->getExprLoc(), 4991 diag::err_omp_allocator_not_in_uses_allocators) 4992 << Allocator->getSourceRange(); 4993 } 4994 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind = 4995 getAllocatorKind(S, Stack, AC->getAllocator()); 4996 // OpenMP, 2.11.4 allocate Clause, Restrictions. 4997 // For task, taskloop or target directives, allocation requests to memory 4998 // allocators with the trait access set to thread result in unspecified 4999 // behavior. 5000 if (AllocatorKind == OMPAllocateDeclAttr::OMPThreadMemAlloc && 5001 (isOpenMPTaskingDirective(Stack->getCurrentDirective()) || 5002 isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()))) { 5003 S.Diag(AC->getAllocator()->getExprLoc(), 5004 diag::warn_omp_allocate_thread_on_task_target_directive) 5005 << getOpenMPDirectiveName(Stack->getCurrentDirective()); 5006 } 5007 for (Expr *E : AC->varlists()) { 5008 SourceLocation ELoc; 5009 SourceRange ERange; 5010 Expr *SimpleRefExpr = E; 5011 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange); 5012 ValueDecl *VD = Res.first; 5013 DSAStackTy::DSAVarData Data = Stack->getTopDSA(VD, /*FromParent=*/false); 5014 if (!isOpenMPPrivate(Data.CKind)) { 5015 S.Diag(E->getExprLoc(), 5016 diag::err_omp_expected_private_copy_for_allocate); 5017 continue; 5018 } 5019 VarDecl *PrivateVD = DeclToCopy[VD]; 5020 if (checkPreviousOMPAllocateAttribute(S, Stack, E, PrivateVD, 5021 AllocatorKind, AC->getAllocator())) 5022 continue; 5023 applyOMPAllocateAttribute(S, PrivateVD, AllocatorKind, AC->getAllocator(), 5024 E->getSourceRange()); 5025 } 5026 } 5027 } 5028 5029 StmtResult Sema::ActOnOpenMPExecutableDirective( 5030 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName, 5031 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses, 5032 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { 5033 StmtResult Res = StmtError(); 5034 // First check CancelRegion which is then used in checkNestingOfRegions. 5035 if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) || 5036 checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion, 5037 StartLoc)) 5038 return StmtError(); 5039 5040 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit; 5041 VarsWithInheritedDSAType VarsWithInheritedDSA; 5042 bool ErrorFound = false; 5043 ClausesWithImplicit.append(Clauses.begin(), Clauses.end()); 5044 if (AStmt && !CurContext->isDependentContext() && Kind != OMPD_atomic && 5045 Kind != OMPD_critical && Kind != OMPD_section && Kind != OMPD_master) { 5046 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5047 5048 // Check default data sharing attributes for referenced variables. 5049 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt)); 5050 int ThisCaptureLevel = getOpenMPCaptureLevels(Kind); 5051 Stmt *S = AStmt; 5052 while (--ThisCaptureLevel >= 0) 5053 S = cast<CapturedStmt>(S)->getCapturedStmt(); 5054 DSAChecker.Visit(S); 5055 if (!isOpenMPTargetDataManagementDirective(Kind) && 5056 !isOpenMPTaskingDirective(Kind)) { 5057 // Visit subcaptures to generate implicit clauses for captured vars. 5058 auto *CS = cast<CapturedStmt>(AStmt); 5059 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 5060 getOpenMPCaptureRegions(CaptureRegions, Kind); 5061 // Ignore outer tasking regions for target directives. 5062 if (CaptureRegions.size() > 1 && CaptureRegions.front() == OMPD_task) 5063 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 5064 DSAChecker.visitSubCaptures(CS); 5065 } 5066 if (DSAChecker.isErrorFound()) 5067 return StmtError(); 5068 // Generate list of implicitly defined firstprivate variables. 5069 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA(); 5070 5071 SmallVector<Expr *, 4> ImplicitFirstprivates( 5072 DSAChecker.getImplicitFirstprivate().begin(), 5073 DSAChecker.getImplicitFirstprivate().end()); 5074 SmallVector<Expr *, 4> ImplicitMaps[OMPC_MAP_delete]; 5075 for (unsigned I = 0; I < OMPC_MAP_delete; ++I) { 5076 ArrayRef<Expr *> ImplicitMap = 5077 DSAChecker.getImplicitMap(static_cast<OpenMPDefaultmapClauseKind>(I)); 5078 ImplicitMaps[I].append(ImplicitMap.begin(), ImplicitMap.end()); 5079 } 5080 // Mark taskgroup task_reduction descriptors as implicitly firstprivate. 5081 for (OMPClause *C : Clauses) { 5082 if (auto *IRC = dyn_cast<OMPInReductionClause>(C)) { 5083 for (Expr *E : IRC->taskgroup_descriptors()) 5084 if (E) 5085 ImplicitFirstprivates.emplace_back(E); 5086 } 5087 // OpenMP 5.0, 2.10.1 task Construct 5088 // [detach clause]... The event-handle will be considered as if it was 5089 // specified on a firstprivate clause. 5090 if (auto *DC = dyn_cast<OMPDetachClause>(C)) 5091 ImplicitFirstprivates.push_back(DC->getEventHandler()); 5092 } 5093 if (!ImplicitFirstprivates.empty()) { 5094 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause( 5095 ImplicitFirstprivates, SourceLocation(), SourceLocation(), 5096 SourceLocation())) { 5097 ClausesWithImplicit.push_back(Implicit); 5098 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() != 5099 ImplicitFirstprivates.size(); 5100 } else { 5101 ErrorFound = true; 5102 } 5103 } 5104 int ClauseKindCnt = -1; 5105 for (ArrayRef<Expr *> ImplicitMap : ImplicitMaps) { 5106 ++ClauseKindCnt; 5107 if (ImplicitMap.empty()) 5108 continue; 5109 CXXScopeSpec MapperIdScopeSpec; 5110 DeclarationNameInfo MapperId; 5111 auto Kind = static_cast<OpenMPMapClauseKind>(ClauseKindCnt); 5112 if (OMPClause *Implicit = ActOnOpenMPMapClause( 5113 llvm::None, llvm::None, MapperIdScopeSpec, MapperId, Kind, 5114 /*IsMapTypeImplicit=*/true, SourceLocation(), SourceLocation(), 5115 ImplicitMap, OMPVarListLocTy())) { 5116 ClausesWithImplicit.emplace_back(Implicit); 5117 ErrorFound |= 5118 cast<OMPMapClause>(Implicit)->varlist_size() != ImplicitMap.size(); 5119 } else { 5120 ErrorFound = true; 5121 } 5122 } 5123 } 5124 5125 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers; 5126 switch (Kind) { 5127 case OMPD_parallel: 5128 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc, 5129 EndLoc); 5130 AllowedNameModifiers.push_back(OMPD_parallel); 5131 break; 5132 case OMPD_simd: 5133 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, 5134 VarsWithInheritedDSA); 5135 if (LangOpts.OpenMP >= 50) 5136 AllowedNameModifiers.push_back(OMPD_simd); 5137 break; 5138 case OMPD_for: 5139 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, 5140 VarsWithInheritedDSA); 5141 break; 5142 case OMPD_for_simd: 5143 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 5144 EndLoc, VarsWithInheritedDSA); 5145 if (LangOpts.OpenMP >= 50) 5146 AllowedNameModifiers.push_back(OMPD_simd); 5147 break; 5148 case OMPD_sections: 5149 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc, 5150 EndLoc); 5151 break; 5152 case OMPD_section: 5153 assert(ClausesWithImplicit.empty() && 5154 "No clauses are allowed for 'omp section' directive"); 5155 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc); 5156 break; 5157 case OMPD_single: 5158 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc, 5159 EndLoc); 5160 break; 5161 case OMPD_master: 5162 assert(ClausesWithImplicit.empty() && 5163 "No clauses are allowed for 'omp master' directive"); 5164 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc); 5165 break; 5166 case OMPD_critical: 5167 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt, 5168 StartLoc, EndLoc); 5169 break; 5170 case OMPD_parallel_for: 5171 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc, 5172 EndLoc, VarsWithInheritedDSA); 5173 AllowedNameModifiers.push_back(OMPD_parallel); 5174 break; 5175 case OMPD_parallel_for_simd: 5176 Res = ActOnOpenMPParallelForSimdDirective( 5177 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5178 AllowedNameModifiers.push_back(OMPD_parallel); 5179 if (LangOpts.OpenMP >= 50) 5180 AllowedNameModifiers.push_back(OMPD_simd); 5181 break; 5182 case OMPD_parallel_master: 5183 Res = ActOnOpenMPParallelMasterDirective(ClausesWithImplicit, AStmt, 5184 StartLoc, EndLoc); 5185 AllowedNameModifiers.push_back(OMPD_parallel); 5186 break; 5187 case OMPD_parallel_sections: 5188 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt, 5189 StartLoc, EndLoc); 5190 AllowedNameModifiers.push_back(OMPD_parallel); 5191 break; 5192 case OMPD_task: 5193 Res = 5194 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 5195 AllowedNameModifiers.push_back(OMPD_task); 5196 break; 5197 case OMPD_taskyield: 5198 assert(ClausesWithImplicit.empty() && 5199 "No clauses are allowed for 'omp taskyield' directive"); 5200 assert(AStmt == nullptr && 5201 "No associated statement allowed for 'omp taskyield' directive"); 5202 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc); 5203 break; 5204 case OMPD_barrier: 5205 assert(ClausesWithImplicit.empty() && 5206 "No clauses are allowed for 'omp barrier' directive"); 5207 assert(AStmt == nullptr && 5208 "No associated statement allowed for 'omp barrier' directive"); 5209 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc); 5210 break; 5211 case OMPD_taskwait: 5212 assert(ClausesWithImplicit.empty() && 5213 "No clauses are allowed for 'omp taskwait' directive"); 5214 assert(AStmt == nullptr && 5215 "No associated statement allowed for 'omp taskwait' directive"); 5216 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc); 5217 break; 5218 case OMPD_taskgroup: 5219 Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc, 5220 EndLoc); 5221 break; 5222 case OMPD_flush: 5223 assert(AStmt == nullptr && 5224 "No associated statement allowed for 'omp flush' directive"); 5225 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc); 5226 break; 5227 case OMPD_depobj: 5228 assert(AStmt == nullptr && 5229 "No associated statement allowed for 'omp depobj' directive"); 5230 Res = ActOnOpenMPDepobjDirective(ClausesWithImplicit, StartLoc, EndLoc); 5231 break; 5232 case OMPD_scan: 5233 assert(AStmt == nullptr && 5234 "No associated statement allowed for 'omp scan' directive"); 5235 Res = ActOnOpenMPScanDirective(ClausesWithImplicit, StartLoc, EndLoc); 5236 break; 5237 case OMPD_ordered: 5238 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc, 5239 EndLoc); 5240 break; 5241 case OMPD_atomic: 5242 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc, 5243 EndLoc); 5244 break; 5245 case OMPD_teams: 5246 Res = 5247 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 5248 break; 5249 case OMPD_target: 5250 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc, 5251 EndLoc); 5252 AllowedNameModifiers.push_back(OMPD_target); 5253 break; 5254 case OMPD_target_parallel: 5255 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt, 5256 StartLoc, EndLoc); 5257 AllowedNameModifiers.push_back(OMPD_target); 5258 AllowedNameModifiers.push_back(OMPD_parallel); 5259 break; 5260 case OMPD_target_parallel_for: 5261 Res = ActOnOpenMPTargetParallelForDirective( 5262 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5263 AllowedNameModifiers.push_back(OMPD_target); 5264 AllowedNameModifiers.push_back(OMPD_parallel); 5265 break; 5266 case OMPD_cancellation_point: 5267 assert(ClausesWithImplicit.empty() && 5268 "No clauses are allowed for 'omp cancellation point' directive"); 5269 assert(AStmt == nullptr && "No associated statement allowed for 'omp " 5270 "cancellation point' directive"); 5271 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion); 5272 break; 5273 case OMPD_cancel: 5274 assert(AStmt == nullptr && 5275 "No associated statement allowed for 'omp cancel' directive"); 5276 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc, 5277 CancelRegion); 5278 AllowedNameModifiers.push_back(OMPD_cancel); 5279 break; 5280 case OMPD_target_data: 5281 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc, 5282 EndLoc); 5283 AllowedNameModifiers.push_back(OMPD_target_data); 5284 break; 5285 case OMPD_target_enter_data: 5286 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc, 5287 EndLoc, AStmt); 5288 AllowedNameModifiers.push_back(OMPD_target_enter_data); 5289 break; 5290 case OMPD_target_exit_data: 5291 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc, 5292 EndLoc, AStmt); 5293 AllowedNameModifiers.push_back(OMPD_target_exit_data); 5294 break; 5295 case OMPD_taskloop: 5296 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc, 5297 EndLoc, VarsWithInheritedDSA); 5298 AllowedNameModifiers.push_back(OMPD_taskloop); 5299 break; 5300 case OMPD_taskloop_simd: 5301 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 5302 EndLoc, VarsWithInheritedDSA); 5303 AllowedNameModifiers.push_back(OMPD_taskloop); 5304 if (LangOpts.OpenMP >= 50) 5305 AllowedNameModifiers.push_back(OMPD_simd); 5306 break; 5307 case OMPD_master_taskloop: 5308 Res = ActOnOpenMPMasterTaskLoopDirective( 5309 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5310 AllowedNameModifiers.push_back(OMPD_taskloop); 5311 break; 5312 case OMPD_master_taskloop_simd: 5313 Res = ActOnOpenMPMasterTaskLoopSimdDirective( 5314 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5315 AllowedNameModifiers.push_back(OMPD_taskloop); 5316 if (LangOpts.OpenMP >= 50) 5317 AllowedNameModifiers.push_back(OMPD_simd); 5318 break; 5319 case OMPD_parallel_master_taskloop: 5320 Res = ActOnOpenMPParallelMasterTaskLoopDirective( 5321 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5322 AllowedNameModifiers.push_back(OMPD_taskloop); 5323 AllowedNameModifiers.push_back(OMPD_parallel); 5324 break; 5325 case OMPD_parallel_master_taskloop_simd: 5326 Res = ActOnOpenMPParallelMasterTaskLoopSimdDirective( 5327 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5328 AllowedNameModifiers.push_back(OMPD_taskloop); 5329 AllowedNameModifiers.push_back(OMPD_parallel); 5330 if (LangOpts.OpenMP >= 50) 5331 AllowedNameModifiers.push_back(OMPD_simd); 5332 break; 5333 case OMPD_distribute: 5334 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc, 5335 EndLoc, VarsWithInheritedDSA); 5336 break; 5337 case OMPD_target_update: 5338 Res = ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc, 5339 EndLoc, AStmt); 5340 AllowedNameModifiers.push_back(OMPD_target_update); 5341 break; 5342 case OMPD_distribute_parallel_for: 5343 Res = ActOnOpenMPDistributeParallelForDirective( 5344 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5345 AllowedNameModifiers.push_back(OMPD_parallel); 5346 break; 5347 case OMPD_distribute_parallel_for_simd: 5348 Res = ActOnOpenMPDistributeParallelForSimdDirective( 5349 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5350 AllowedNameModifiers.push_back(OMPD_parallel); 5351 if (LangOpts.OpenMP >= 50) 5352 AllowedNameModifiers.push_back(OMPD_simd); 5353 break; 5354 case OMPD_distribute_simd: 5355 Res = ActOnOpenMPDistributeSimdDirective( 5356 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5357 if (LangOpts.OpenMP >= 50) 5358 AllowedNameModifiers.push_back(OMPD_simd); 5359 break; 5360 case OMPD_target_parallel_for_simd: 5361 Res = ActOnOpenMPTargetParallelForSimdDirective( 5362 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5363 AllowedNameModifiers.push_back(OMPD_target); 5364 AllowedNameModifiers.push_back(OMPD_parallel); 5365 if (LangOpts.OpenMP >= 50) 5366 AllowedNameModifiers.push_back(OMPD_simd); 5367 break; 5368 case OMPD_target_simd: 5369 Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 5370 EndLoc, VarsWithInheritedDSA); 5371 AllowedNameModifiers.push_back(OMPD_target); 5372 if (LangOpts.OpenMP >= 50) 5373 AllowedNameModifiers.push_back(OMPD_simd); 5374 break; 5375 case OMPD_teams_distribute: 5376 Res = ActOnOpenMPTeamsDistributeDirective( 5377 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5378 break; 5379 case OMPD_teams_distribute_simd: 5380 Res = ActOnOpenMPTeamsDistributeSimdDirective( 5381 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5382 if (LangOpts.OpenMP >= 50) 5383 AllowedNameModifiers.push_back(OMPD_simd); 5384 break; 5385 case OMPD_teams_distribute_parallel_for_simd: 5386 Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective( 5387 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5388 AllowedNameModifiers.push_back(OMPD_parallel); 5389 if (LangOpts.OpenMP >= 50) 5390 AllowedNameModifiers.push_back(OMPD_simd); 5391 break; 5392 case OMPD_teams_distribute_parallel_for: 5393 Res = ActOnOpenMPTeamsDistributeParallelForDirective( 5394 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5395 AllowedNameModifiers.push_back(OMPD_parallel); 5396 break; 5397 case OMPD_target_teams: 5398 Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, 5399 EndLoc); 5400 AllowedNameModifiers.push_back(OMPD_target); 5401 break; 5402 case OMPD_target_teams_distribute: 5403 Res = ActOnOpenMPTargetTeamsDistributeDirective( 5404 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5405 AllowedNameModifiers.push_back(OMPD_target); 5406 break; 5407 case OMPD_target_teams_distribute_parallel_for: 5408 Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective( 5409 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5410 AllowedNameModifiers.push_back(OMPD_target); 5411 AllowedNameModifiers.push_back(OMPD_parallel); 5412 break; 5413 case OMPD_target_teams_distribute_parallel_for_simd: 5414 Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective( 5415 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5416 AllowedNameModifiers.push_back(OMPD_target); 5417 AllowedNameModifiers.push_back(OMPD_parallel); 5418 if (LangOpts.OpenMP >= 50) 5419 AllowedNameModifiers.push_back(OMPD_simd); 5420 break; 5421 case OMPD_target_teams_distribute_simd: 5422 Res = ActOnOpenMPTargetTeamsDistributeSimdDirective( 5423 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5424 AllowedNameModifiers.push_back(OMPD_target); 5425 if (LangOpts.OpenMP >= 50) 5426 AllowedNameModifiers.push_back(OMPD_simd); 5427 break; 5428 case OMPD_declare_target: 5429 case OMPD_end_declare_target: 5430 case OMPD_threadprivate: 5431 case OMPD_allocate: 5432 case OMPD_declare_reduction: 5433 case OMPD_declare_mapper: 5434 case OMPD_declare_simd: 5435 case OMPD_requires: 5436 case OMPD_declare_variant: 5437 case OMPD_begin_declare_variant: 5438 case OMPD_end_declare_variant: 5439 llvm_unreachable("OpenMP Directive is not allowed"); 5440 case OMPD_unknown: 5441 default: 5442 llvm_unreachable("Unknown OpenMP directive"); 5443 } 5444 5445 ErrorFound = Res.isInvalid() || ErrorFound; 5446 5447 // Check variables in the clauses if default(none) or 5448 // default(firstprivate) was specified. 5449 if (DSAStack->getDefaultDSA() == DSA_none || 5450 DSAStack->getDefaultDSA() == DSA_firstprivate) { 5451 DSAAttrChecker DSAChecker(DSAStack, *this, nullptr); 5452 for (OMPClause *C : Clauses) { 5453 switch (C->getClauseKind()) { 5454 case OMPC_num_threads: 5455 case OMPC_dist_schedule: 5456 // Do not analyse if no parent teams directive. 5457 if (isOpenMPTeamsDirective(Kind)) 5458 break; 5459 continue; 5460 case OMPC_if: 5461 if (isOpenMPTeamsDirective(Kind) && 5462 cast<OMPIfClause>(C)->getNameModifier() != OMPD_target) 5463 break; 5464 if (isOpenMPParallelDirective(Kind) && 5465 isOpenMPTaskLoopDirective(Kind) && 5466 cast<OMPIfClause>(C)->getNameModifier() != OMPD_parallel) 5467 break; 5468 continue; 5469 case OMPC_schedule: 5470 case OMPC_detach: 5471 break; 5472 case OMPC_grainsize: 5473 case OMPC_num_tasks: 5474 case OMPC_final: 5475 case OMPC_priority: 5476 // Do not analyze if no parent parallel directive. 5477 if (isOpenMPParallelDirective(Kind)) 5478 break; 5479 continue; 5480 case OMPC_ordered: 5481 case OMPC_device: 5482 case OMPC_num_teams: 5483 case OMPC_thread_limit: 5484 case OMPC_hint: 5485 case OMPC_collapse: 5486 case OMPC_safelen: 5487 case OMPC_simdlen: 5488 case OMPC_default: 5489 case OMPC_proc_bind: 5490 case OMPC_private: 5491 case OMPC_firstprivate: 5492 case OMPC_lastprivate: 5493 case OMPC_shared: 5494 case OMPC_reduction: 5495 case OMPC_task_reduction: 5496 case OMPC_in_reduction: 5497 case OMPC_linear: 5498 case OMPC_aligned: 5499 case OMPC_copyin: 5500 case OMPC_copyprivate: 5501 case OMPC_nowait: 5502 case OMPC_untied: 5503 case OMPC_mergeable: 5504 case OMPC_allocate: 5505 case OMPC_read: 5506 case OMPC_write: 5507 case OMPC_update: 5508 case OMPC_capture: 5509 case OMPC_seq_cst: 5510 case OMPC_acq_rel: 5511 case OMPC_acquire: 5512 case OMPC_release: 5513 case OMPC_relaxed: 5514 case OMPC_depend: 5515 case OMPC_threads: 5516 case OMPC_simd: 5517 case OMPC_map: 5518 case OMPC_nogroup: 5519 case OMPC_defaultmap: 5520 case OMPC_to: 5521 case OMPC_from: 5522 case OMPC_use_device_ptr: 5523 case OMPC_use_device_addr: 5524 case OMPC_is_device_ptr: 5525 case OMPC_nontemporal: 5526 case OMPC_order: 5527 case OMPC_destroy: 5528 case OMPC_inclusive: 5529 case OMPC_exclusive: 5530 case OMPC_uses_allocators: 5531 case OMPC_affinity: 5532 continue; 5533 case OMPC_allocator: 5534 case OMPC_flush: 5535 case OMPC_depobj: 5536 case OMPC_threadprivate: 5537 case OMPC_uniform: 5538 case OMPC_unknown: 5539 case OMPC_unified_address: 5540 case OMPC_unified_shared_memory: 5541 case OMPC_reverse_offload: 5542 case OMPC_dynamic_allocators: 5543 case OMPC_atomic_default_mem_order: 5544 case OMPC_device_type: 5545 case OMPC_match: 5546 default: 5547 llvm_unreachable("Unexpected clause"); 5548 } 5549 for (Stmt *CC : C->children()) { 5550 if (CC) 5551 DSAChecker.Visit(CC); 5552 } 5553 } 5554 for (const auto &P : DSAChecker.getVarsWithInheritedDSA()) 5555 VarsWithInheritedDSA[P.getFirst()] = P.getSecond(); 5556 } 5557 for (const auto &P : VarsWithInheritedDSA) { 5558 if (P.getFirst()->isImplicit() || isa<OMPCapturedExprDecl>(P.getFirst())) 5559 continue; 5560 ErrorFound = true; 5561 if (DSAStack->getDefaultDSA() == DSA_none || 5562 DSAStack->getDefaultDSA() == DSA_firstprivate) { 5563 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable) 5564 << P.first << P.second->getSourceRange(); 5565 Diag(DSAStack->getDefaultDSALocation(), diag::note_omp_default_dsa_none); 5566 } else if (getLangOpts().OpenMP >= 50) { 5567 Diag(P.second->getExprLoc(), 5568 diag::err_omp_defaultmap_no_attr_for_variable) 5569 << P.first << P.second->getSourceRange(); 5570 Diag(DSAStack->getDefaultDSALocation(), 5571 diag::note_omp_defaultmap_attr_none); 5572 } 5573 } 5574 5575 if (!AllowedNameModifiers.empty()) 5576 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) || 5577 ErrorFound; 5578 5579 if (ErrorFound) 5580 return StmtError(); 5581 5582 if (!CurContext->isDependentContext() && 5583 isOpenMPTargetExecutionDirective(Kind) && 5584 !(DSAStack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() || 5585 DSAStack->hasRequiresDeclWithClause<OMPUnifiedAddressClause>() || 5586 DSAStack->hasRequiresDeclWithClause<OMPReverseOffloadClause>() || 5587 DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())) { 5588 // Register target to DSA Stack. 5589 DSAStack->addTargetDirLocation(StartLoc); 5590 } 5591 5592 return Res; 5593 } 5594 5595 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective( 5596 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen, 5597 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds, 5598 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears, 5599 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) { 5600 assert(Aligneds.size() == Alignments.size()); 5601 assert(Linears.size() == LinModifiers.size()); 5602 assert(Linears.size() == Steps.size()); 5603 if (!DG || DG.get().isNull()) 5604 return DeclGroupPtrTy(); 5605 5606 const int SimdId = 0; 5607 if (!DG.get().isSingleDecl()) { 5608 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd_variant) 5609 << SimdId; 5610 return DG; 5611 } 5612 Decl *ADecl = DG.get().getSingleDecl(); 5613 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl)) 5614 ADecl = FTD->getTemplatedDecl(); 5615 5616 auto *FD = dyn_cast<FunctionDecl>(ADecl); 5617 if (!FD) { 5618 Diag(ADecl->getLocation(), diag::err_omp_function_expected) << SimdId; 5619 return DeclGroupPtrTy(); 5620 } 5621 5622 // OpenMP [2.8.2, declare simd construct, Description] 5623 // The parameter of the simdlen clause must be a constant positive integer 5624 // expression. 5625 ExprResult SL; 5626 if (Simdlen) 5627 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen); 5628 // OpenMP [2.8.2, declare simd construct, Description] 5629 // The special this pointer can be used as if was one of the arguments to the 5630 // function in any of the linear, aligned, or uniform clauses. 5631 // The uniform clause declares one or more arguments to have an invariant 5632 // value for all concurrent invocations of the function in the execution of a 5633 // single SIMD loop. 5634 llvm::DenseMap<const Decl *, const Expr *> UniformedArgs; 5635 const Expr *UniformedLinearThis = nullptr; 5636 for (const Expr *E : Uniforms) { 5637 E = E->IgnoreParenImpCasts(); 5638 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 5639 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) 5640 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 5641 FD->getParamDecl(PVD->getFunctionScopeIndex()) 5642 ->getCanonicalDecl() == PVD->getCanonicalDecl()) { 5643 UniformedArgs.try_emplace(PVD->getCanonicalDecl(), E); 5644 continue; 5645 } 5646 if (isa<CXXThisExpr>(E)) { 5647 UniformedLinearThis = E; 5648 continue; 5649 } 5650 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 5651 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 5652 } 5653 // OpenMP [2.8.2, declare simd construct, Description] 5654 // The aligned clause declares that the object to which each list item points 5655 // is aligned to the number of bytes expressed in the optional parameter of 5656 // the aligned clause. 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 // The type of list items appearing in the aligned clause must be array, 5660 // pointer, reference to array, or reference to pointer. 5661 llvm::DenseMap<const Decl *, const Expr *> AlignedArgs; 5662 const Expr *AlignedThis = nullptr; 5663 for (const Expr *E : Aligneds) { 5664 E = E->IgnoreParenImpCasts(); 5665 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 5666 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 5667 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 5668 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 5669 FD->getParamDecl(PVD->getFunctionScopeIndex()) 5670 ->getCanonicalDecl() == CanonPVD) { 5671 // OpenMP [2.8.1, simd construct, Restrictions] 5672 // A list-item cannot appear in more than one aligned clause. 5673 if (AlignedArgs.count(CanonPVD) > 0) { 5674 Diag(E->getExprLoc(), diag::err_omp_used_in_clause_twice) 5675 << 1 << getOpenMPClauseName(OMPC_aligned) 5676 << E->getSourceRange(); 5677 Diag(AlignedArgs[CanonPVD]->getExprLoc(), 5678 diag::note_omp_explicit_dsa) 5679 << getOpenMPClauseName(OMPC_aligned); 5680 continue; 5681 } 5682 AlignedArgs[CanonPVD] = E; 5683 QualType QTy = PVD->getType() 5684 .getNonReferenceType() 5685 .getUnqualifiedType() 5686 .getCanonicalType(); 5687 const Type *Ty = QTy.getTypePtrOrNull(); 5688 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) { 5689 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr) 5690 << QTy << getLangOpts().CPlusPlus << E->getSourceRange(); 5691 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD; 5692 } 5693 continue; 5694 } 5695 } 5696 if (isa<CXXThisExpr>(E)) { 5697 if (AlignedThis) { 5698 Diag(E->getExprLoc(), diag::err_omp_used_in_clause_twice) 5699 << 2 << getOpenMPClauseName(OMPC_aligned) << E->getSourceRange(); 5700 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa) 5701 << getOpenMPClauseName(OMPC_aligned); 5702 } 5703 AlignedThis = E; 5704 continue; 5705 } 5706 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 5707 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 5708 } 5709 // The optional parameter of the aligned clause, alignment, must be a constant 5710 // positive integer expression. If no optional parameter is specified, 5711 // implementation-defined default alignments for SIMD instructions on the 5712 // target platforms are assumed. 5713 SmallVector<const Expr *, 4> NewAligns; 5714 for (Expr *E : Alignments) { 5715 ExprResult Align; 5716 if (E) 5717 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned); 5718 NewAligns.push_back(Align.get()); 5719 } 5720 // OpenMP [2.8.2, declare simd construct, Description] 5721 // The linear clause declares one or more list items to be private to a SIMD 5722 // lane and to have a linear relationship with respect to the iteration space 5723 // of a loop. 5724 // The special this pointer can be used as if was one of the arguments to the 5725 // function in any of the linear, aligned, or uniform clauses. 5726 // When a linear-step expression is specified in a linear clause it must be 5727 // either a constant integer expression or an integer-typed parameter that is 5728 // specified in a uniform clause on the directive. 5729 llvm::DenseMap<const Decl *, const Expr *> LinearArgs; 5730 const bool IsUniformedThis = UniformedLinearThis != nullptr; 5731 auto MI = LinModifiers.begin(); 5732 for (const Expr *E : Linears) { 5733 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI); 5734 ++MI; 5735 E = E->IgnoreParenImpCasts(); 5736 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 5737 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 5738 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 5739 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 5740 FD->getParamDecl(PVD->getFunctionScopeIndex()) 5741 ->getCanonicalDecl() == CanonPVD) { 5742 // OpenMP [2.15.3.7, linear Clause, Restrictions] 5743 // A list-item cannot appear in more than one linear clause. 5744 if (LinearArgs.count(CanonPVD) > 0) { 5745 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 5746 << getOpenMPClauseName(OMPC_linear) 5747 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange(); 5748 Diag(LinearArgs[CanonPVD]->getExprLoc(), 5749 diag::note_omp_explicit_dsa) 5750 << getOpenMPClauseName(OMPC_linear); 5751 continue; 5752 } 5753 // Each argument can appear in at most one uniform or linear clause. 5754 if (UniformedArgs.count(CanonPVD) > 0) { 5755 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 5756 << getOpenMPClauseName(OMPC_linear) 5757 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange(); 5758 Diag(UniformedArgs[CanonPVD]->getExprLoc(), 5759 diag::note_omp_explicit_dsa) 5760 << getOpenMPClauseName(OMPC_uniform); 5761 continue; 5762 } 5763 LinearArgs[CanonPVD] = E; 5764 if (E->isValueDependent() || E->isTypeDependent() || 5765 E->isInstantiationDependent() || 5766 E->containsUnexpandedParameterPack()) 5767 continue; 5768 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind, 5769 PVD->getOriginalType(), 5770 /*IsDeclareSimd=*/true); 5771 continue; 5772 } 5773 } 5774 if (isa<CXXThisExpr>(E)) { 5775 if (UniformedLinearThis) { 5776 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 5777 << getOpenMPClauseName(OMPC_linear) 5778 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear) 5779 << E->getSourceRange(); 5780 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa) 5781 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform 5782 : OMPC_linear); 5783 continue; 5784 } 5785 UniformedLinearThis = E; 5786 if (E->isValueDependent() || E->isTypeDependent() || 5787 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 5788 continue; 5789 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind, 5790 E->getType(), /*IsDeclareSimd=*/true); 5791 continue; 5792 } 5793 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 5794 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 5795 } 5796 Expr *Step = nullptr; 5797 Expr *NewStep = nullptr; 5798 SmallVector<Expr *, 4> NewSteps; 5799 for (Expr *E : Steps) { 5800 // Skip the same step expression, it was checked already. 5801 if (Step == E || !E) { 5802 NewSteps.push_back(E ? NewStep : nullptr); 5803 continue; 5804 } 5805 Step = E; 5806 if (const auto *DRE = dyn_cast<DeclRefExpr>(Step)) 5807 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 5808 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 5809 if (UniformedArgs.count(CanonPVD) == 0) { 5810 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param) 5811 << Step->getSourceRange(); 5812 } else if (E->isValueDependent() || E->isTypeDependent() || 5813 E->isInstantiationDependent() || 5814 E->containsUnexpandedParameterPack() || 5815 CanonPVD->getType()->hasIntegerRepresentation()) { 5816 NewSteps.push_back(Step); 5817 } else { 5818 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param) 5819 << Step->getSourceRange(); 5820 } 5821 continue; 5822 } 5823 NewStep = Step; 5824 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && 5825 !Step->isInstantiationDependent() && 5826 !Step->containsUnexpandedParameterPack()) { 5827 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step) 5828 .get(); 5829 if (NewStep) 5830 NewStep = VerifyIntegerConstantExpression(NewStep).get(); 5831 } 5832 NewSteps.push_back(NewStep); 5833 } 5834 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit( 5835 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()), 5836 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(), 5837 const_cast<Expr **>(NewAligns.data()), NewAligns.size(), 5838 const_cast<Expr **>(Linears.data()), Linears.size(), 5839 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(), 5840 NewSteps.data(), NewSteps.size(), SR); 5841 ADecl->addAttr(NewAttr); 5842 return DG; 5843 } 5844 5845 static void setPrototype(Sema &S, FunctionDecl *FD, FunctionDecl *FDWithProto, 5846 QualType NewType) { 5847 assert(NewType->isFunctionProtoType() && 5848 "Expected function type with prototype."); 5849 assert(FD->getType()->isFunctionNoProtoType() && 5850 "Expected function with type with no prototype."); 5851 assert(FDWithProto->getType()->isFunctionProtoType() && 5852 "Expected function with prototype."); 5853 // Synthesize parameters with the same types. 5854 FD->setType(NewType); 5855 SmallVector<ParmVarDecl *, 16> Params; 5856 for (const ParmVarDecl *P : FDWithProto->parameters()) { 5857 auto *Param = ParmVarDecl::Create(S.getASTContext(), FD, SourceLocation(), 5858 SourceLocation(), nullptr, P->getType(), 5859 /*TInfo=*/nullptr, SC_None, nullptr); 5860 Param->setScopeInfo(0, Params.size()); 5861 Param->setImplicit(); 5862 Params.push_back(Param); 5863 } 5864 5865 FD->setParams(Params); 5866 } 5867 5868 Sema::OMPDeclareVariantScope::OMPDeclareVariantScope(OMPTraitInfo &TI) 5869 : TI(&TI), NameSuffix(TI.getMangledName()) {} 5870 5871 FunctionDecl * 5872 Sema::ActOnStartOfFunctionDefinitionInOpenMPDeclareVariantScope(Scope *S, 5873 Declarator &D) { 5874 IdentifierInfo *BaseII = D.getIdentifier(); 5875 LookupResult Lookup(*this, DeclarationName(BaseII), D.getIdentifierLoc(), 5876 LookupOrdinaryName); 5877 LookupParsedName(Lookup, S, &D.getCXXScopeSpec()); 5878 5879 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); 5880 QualType FType = TInfo->getType(); 5881 5882 bool IsConstexpr = D.getDeclSpec().getConstexprSpecifier() == CSK_constexpr; 5883 bool IsConsteval = D.getDeclSpec().getConstexprSpecifier() == CSK_consteval; 5884 5885 FunctionDecl *BaseFD = nullptr; 5886 for (auto *Candidate : Lookup) { 5887 auto *UDecl = dyn_cast<FunctionDecl>(Candidate->getUnderlyingDecl()); 5888 if (!UDecl) 5889 continue; 5890 5891 // Don't specialize constexpr/consteval functions with 5892 // non-constexpr/consteval functions. 5893 if (UDecl->isConstexpr() && !IsConstexpr) 5894 continue; 5895 if (UDecl->isConsteval() && !IsConsteval) 5896 continue; 5897 5898 QualType NewType = Context.mergeFunctionTypes( 5899 FType, UDecl->getType(), /* OfBlockPointer */ false, 5900 /* Unqualified */ false, /* AllowCXX */ true); 5901 if (NewType.isNull()) 5902 continue; 5903 5904 // Found a base! 5905 BaseFD = UDecl; 5906 break; 5907 } 5908 if (!BaseFD) { 5909 BaseFD = cast<FunctionDecl>(ActOnDeclarator(S, D)); 5910 BaseFD->setImplicit(true); 5911 } 5912 5913 OMPDeclareVariantScope &DVScope = OMPDeclareVariantScopes.back(); 5914 std::string MangledName; 5915 MangledName += D.getIdentifier()->getName(); 5916 MangledName += getOpenMPVariantManglingSeparatorStr(); 5917 MangledName += DVScope.NameSuffix; 5918 IdentifierInfo &VariantII = Context.Idents.get(MangledName); 5919 5920 VariantII.setMangledOpenMPVariantName(true); 5921 D.SetIdentifier(&VariantII, D.getBeginLoc()); 5922 return BaseFD; 5923 } 5924 5925 void Sema::ActOnFinishedFunctionDefinitionInOpenMPDeclareVariantScope( 5926 FunctionDecl *FD, FunctionDecl *BaseFD) { 5927 // Do not mark function as is used to prevent its emission if this is the 5928 // only place where it is used. 5929 EnterExpressionEvaluationContext Unevaluated( 5930 *this, Sema::ExpressionEvaluationContext::Unevaluated); 5931 5932 Expr *VariantFuncRef = DeclRefExpr::Create( 5933 Context, NestedNameSpecifierLoc(), SourceLocation(), FD, 5934 /* RefersToEnclosingVariableOrCapture */ false, 5935 /* NameLoc */ FD->getLocation(), FD->getType(), ExprValueKind::VK_RValue); 5936 5937 OMPDeclareVariantScope &DVScope = OMPDeclareVariantScopes.back(); 5938 auto *OMPDeclareVariantA = OMPDeclareVariantAttr::CreateImplicit( 5939 Context, VariantFuncRef, DVScope.TI); 5940 BaseFD->addAttr(OMPDeclareVariantA); 5941 } 5942 5943 ExprResult Sema::ActOnOpenMPCall(ExprResult Call, Scope *Scope, 5944 SourceLocation LParenLoc, 5945 MultiExprArg ArgExprs, 5946 SourceLocation RParenLoc, Expr *ExecConfig) { 5947 // The common case is a regular call we do not want to specialize at all. Try 5948 // to make that case fast by bailing early. 5949 CallExpr *CE = dyn_cast<CallExpr>(Call.get()); 5950 if (!CE) 5951 return Call; 5952 5953 FunctionDecl *CalleeFnDecl = CE->getDirectCallee(); 5954 if (!CalleeFnDecl) 5955 return Call; 5956 5957 if (!CalleeFnDecl->hasAttr<OMPDeclareVariantAttr>()) 5958 return Call; 5959 5960 ASTContext &Context = getASTContext(); 5961 std::function<void(StringRef)> DiagUnknownTrait = [this, 5962 CE](StringRef ISATrait) { 5963 // TODO Track the selector locations in a way that is accessible here to 5964 // improve the diagnostic location. 5965 Diag(CE->getBeginLoc(), diag::warn_unknown_declare_variant_isa_trait) 5966 << ISATrait; 5967 }; 5968 TargetOMPContext OMPCtx(Context, std::move(DiagUnknownTrait), 5969 getCurFunctionDecl()); 5970 5971 SmallVector<Expr *, 4> Exprs; 5972 SmallVector<VariantMatchInfo, 4> VMIs; 5973 while (CalleeFnDecl) { 5974 for (OMPDeclareVariantAttr *A : 5975 CalleeFnDecl->specific_attrs<OMPDeclareVariantAttr>()) { 5976 Expr *VariantRef = A->getVariantFuncRef(); 5977 5978 VariantMatchInfo VMI; 5979 OMPTraitInfo &TI = A->getTraitInfo(); 5980 TI.getAsVariantMatchInfo(Context, VMI); 5981 if (!isVariantApplicableInContext(VMI, OMPCtx, 5982 /* DeviceSetOnly */ false)) 5983 continue; 5984 5985 VMIs.push_back(VMI); 5986 Exprs.push_back(VariantRef); 5987 } 5988 5989 CalleeFnDecl = CalleeFnDecl->getPreviousDecl(); 5990 } 5991 5992 ExprResult NewCall; 5993 do { 5994 int BestIdx = getBestVariantMatchForContext(VMIs, OMPCtx); 5995 if (BestIdx < 0) 5996 return Call; 5997 Expr *BestExpr = cast<DeclRefExpr>(Exprs[BestIdx]); 5998 Decl *BestDecl = cast<DeclRefExpr>(BestExpr)->getDecl(); 5999 6000 { 6001 // Try to build a (member) call expression for the current best applicable 6002 // variant expression. We allow this to fail in which case we continue 6003 // with the next best variant expression. The fail case is part of the 6004 // implementation defined behavior in the OpenMP standard when it talks 6005 // about what differences in the function prototypes: "Any differences 6006 // that the specific OpenMP context requires in the prototype of the 6007 // variant from the base function prototype are implementation defined." 6008 // This wording is there to allow the specialized variant to have a 6009 // different type than the base function. This is intended and OK but if 6010 // we cannot create a call the difference is not in the "implementation 6011 // defined range" we allow. 6012 Sema::TentativeAnalysisScope Trap(*this); 6013 6014 if (auto *SpecializedMethod = dyn_cast<CXXMethodDecl>(BestDecl)) { 6015 auto *MemberCall = dyn_cast<CXXMemberCallExpr>(CE); 6016 BestExpr = MemberExpr::CreateImplicit( 6017 Context, MemberCall->getImplicitObjectArgument(), 6018 /* IsArrow */ false, SpecializedMethod, Context.BoundMemberTy, 6019 MemberCall->getValueKind(), MemberCall->getObjectKind()); 6020 } 6021 NewCall = BuildCallExpr(Scope, BestExpr, LParenLoc, ArgExprs, RParenLoc, 6022 ExecConfig); 6023 if (NewCall.isUsable()) 6024 break; 6025 } 6026 6027 VMIs.erase(VMIs.begin() + BestIdx); 6028 Exprs.erase(Exprs.begin() + BestIdx); 6029 } while (!VMIs.empty()); 6030 6031 if (!NewCall.isUsable()) 6032 return Call; 6033 return PseudoObjectExpr::Create(Context, CE, {NewCall.get()}, 0); 6034 } 6035 6036 Optional<std::pair<FunctionDecl *, Expr *>> 6037 Sema::checkOpenMPDeclareVariantFunction(Sema::DeclGroupPtrTy DG, 6038 Expr *VariantRef, OMPTraitInfo &TI, 6039 SourceRange SR) { 6040 if (!DG || DG.get().isNull()) 6041 return None; 6042 6043 const int VariantId = 1; 6044 // Must be applied only to single decl. 6045 if (!DG.get().isSingleDecl()) { 6046 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd_variant) 6047 << VariantId << SR; 6048 return None; 6049 } 6050 Decl *ADecl = DG.get().getSingleDecl(); 6051 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl)) 6052 ADecl = FTD->getTemplatedDecl(); 6053 6054 // Decl must be a function. 6055 auto *FD = dyn_cast<FunctionDecl>(ADecl); 6056 if (!FD) { 6057 Diag(ADecl->getLocation(), diag::err_omp_function_expected) 6058 << VariantId << SR; 6059 return None; 6060 } 6061 6062 auto &&HasMultiVersionAttributes = [](const FunctionDecl *FD) { 6063 return FD->hasAttrs() && 6064 (FD->hasAttr<CPUDispatchAttr>() || FD->hasAttr<CPUSpecificAttr>() || 6065 FD->hasAttr<TargetAttr>()); 6066 }; 6067 // OpenMP is not compatible with CPU-specific attributes. 6068 if (HasMultiVersionAttributes(FD)) { 6069 Diag(FD->getLocation(), diag::err_omp_declare_variant_incompat_attributes) 6070 << SR; 6071 return None; 6072 } 6073 6074 // Allow #pragma omp declare variant only if the function is not used. 6075 if (FD->isUsed(false)) 6076 Diag(SR.getBegin(), diag::warn_omp_declare_variant_after_used) 6077 << FD->getLocation(); 6078 6079 // Check if the function was emitted already. 6080 const FunctionDecl *Definition; 6081 if (!FD->isThisDeclarationADefinition() && FD->isDefined(Definition) && 6082 (LangOpts.EmitAllDecls || Context.DeclMustBeEmitted(Definition))) 6083 Diag(SR.getBegin(), diag::warn_omp_declare_variant_after_emitted) 6084 << FD->getLocation(); 6085 6086 // The VariantRef must point to function. 6087 if (!VariantRef) { 6088 Diag(SR.getBegin(), diag::err_omp_function_expected) << VariantId; 6089 return None; 6090 } 6091 6092 auto ShouldDelayChecks = [](Expr *&E, bool) { 6093 return E && (E->isTypeDependent() || E->isValueDependent() || 6094 E->containsUnexpandedParameterPack() || 6095 E->isInstantiationDependent()); 6096 }; 6097 // Do not check templates, wait until instantiation. 6098 if (FD->isDependentContext() || ShouldDelayChecks(VariantRef, false) || 6099 TI.anyScoreOrCondition(ShouldDelayChecks)) 6100 return std::make_pair(FD, VariantRef); 6101 6102 // Deal with non-constant score and user condition expressions. 6103 auto HandleNonConstantScoresAndConditions = [this](Expr *&E, 6104 bool IsScore) -> bool { 6105 if (!E || E->isIntegerConstantExpr(Context)) 6106 return false; 6107 6108 if (IsScore) { 6109 // We warn on non-constant scores and pretend they were not present. 6110 Diag(E->getExprLoc(), diag::warn_omp_declare_variant_score_not_constant) 6111 << E; 6112 E = nullptr; 6113 } else { 6114 // We could replace a non-constant user condition with "false" but we 6115 // will soon need to handle these anyway for the dynamic version of 6116 // OpenMP context selectors. 6117 Diag(E->getExprLoc(), 6118 diag::err_omp_declare_variant_user_condition_not_constant) 6119 << E; 6120 } 6121 return true; 6122 }; 6123 if (TI.anyScoreOrCondition(HandleNonConstantScoresAndConditions)) 6124 return None; 6125 6126 // Convert VariantRef expression to the type of the original function to 6127 // resolve possible conflicts. 6128 ExprResult VariantRefCast; 6129 if (LangOpts.CPlusPlus) { 6130 QualType FnPtrType; 6131 auto *Method = dyn_cast<CXXMethodDecl>(FD); 6132 if (Method && !Method->isStatic()) { 6133 const Type *ClassType = 6134 Context.getTypeDeclType(Method->getParent()).getTypePtr(); 6135 FnPtrType = Context.getMemberPointerType(FD->getType(), ClassType); 6136 ExprResult ER; 6137 { 6138 // Build adrr_of unary op to correctly handle type checks for member 6139 // functions. 6140 Sema::TentativeAnalysisScope Trap(*this); 6141 ER = CreateBuiltinUnaryOp(VariantRef->getBeginLoc(), UO_AddrOf, 6142 VariantRef); 6143 } 6144 if (!ER.isUsable()) { 6145 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected) 6146 << VariantId << VariantRef->getSourceRange(); 6147 return None; 6148 } 6149 VariantRef = ER.get(); 6150 } else { 6151 FnPtrType = Context.getPointerType(FD->getType()); 6152 } 6153 ImplicitConversionSequence ICS = 6154 TryImplicitConversion(VariantRef, FnPtrType.getUnqualifiedType(), 6155 /*SuppressUserConversions=*/false, 6156 AllowedExplicit::None, 6157 /*InOverloadResolution=*/false, 6158 /*CStyle=*/false, 6159 /*AllowObjCWritebackConversion=*/false); 6160 if (ICS.isFailure()) { 6161 Diag(VariantRef->getExprLoc(), 6162 diag::err_omp_declare_variant_incompat_types) 6163 << VariantRef->getType() 6164 << ((Method && !Method->isStatic()) ? FnPtrType : FD->getType()) 6165 << VariantRef->getSourceRange(); 6166 return None; 6167 } 6168 VariantRefCast = PerformImplicitConversion( 6169 VariantRef, FnPtrType.getUnqualifiedType(), AA_Converting); 6170 if (!VariantRefCast.isUsable()) 6171 return None; 6172 // Drop previously built artificial addr_of unary op for member functions. 6173 if (Method && !Method->isStatic()) { 6174 Expr *PossibleAddrOfVariantRef = VariantRefCast.get(); 6175 if (auto *UO = dyn_cast<UnaryOperator>( 6176 PossibleAddrOfVariantRef->IgnoreImplicit())) 6177 VariantRefCast = UO->getSubExpr(); 6178 } 6179 } else { 6180 VariantRefCast = VariantRef; 6181 } 6182 6183 ExprResult ER = CheckPlaceholderExpr(VariantRefCast.get()); 6184 if (!ER.isUsable() || 6185 !ER.get()->IgnoreParenImpCasts()->getType()->isFunctionType()) { 6186 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected) 6187 << VariantId << VariantRef->getSourceRange(); 6188 return None; 6189 } 6190 6191 // The VariantRef must point to function. 6192 auto *DRE = dyn_cast<DeclRefExpr>(ER.get()->IgnoreParenImpCasts()); 6193 if (!DRE) { 6194 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected) 6195 << VariantId << VariantRef->getSourceRange(); 6196 return None; 6197 } 6198 auto *NewFD = dyn_cast_or_null<FunctionDecl>(DRE->getDecl()); 6199 if (!NewFD) { 6200 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected) 6201 << VariantId << VariantRef->getSourceRange(); 6202 return None; 6203 } 6204 6205 // Check if function types are compatible in C. 6206 if (!LangOpts.CPlusPlus) { 6207 QualType NewType = 6208 Context.mergeFunctionTypes(FD->getType(), NewFD->getType()); 6209 if (NewType.isNull()) { 6210 Diag(VariantRef->getExprLoc(), 6211 diag::err_omp_declare_variant_incompat_types) 6212 << NewFD->getType() << FD->getType() << VariantRef->getSourceRange(); 6213 return None; 6214 } 6215 if (NewType->isFunctionProtoType()) { 6216 if (FD->getType()->isFunctionNoProtoType()) 6217 setPrototype(*this, FD, NewFD, NewType); 6218 else if (NewFD->getType()->isFunctionNoProtoType()) 6219 setPrototype(*this, NewFD, FD, NewType); 6220 } 6221 } 6222 6223 // Check if variant function is not marked with declare variant directive. 6224 if (NewFD->hasAttrs() && NewFD->hasAttr<OMPDeclareVariantAttr>()) { 6225 Diag(VariantRef->getExprLoc(), 6226 diag::warn_omp_declare_variant_marked_as_declare_variant) 6227 << VariantRef->getSourceRange(); 6228 SourceRange SR = 6229 NewFD->specific_attr_begin<OMPDeclareVariantAttr>()->getRange(); 6230 Diag(SR.getBegin(), diag::note_omp_marked_declare_variant_here) << SR; 6231 return None; 6232 } 6233 6234 enum DoesntSupport { 6235 VirtFuncs = 1, 6236 Constructors = 3, 6237 Destructors = 4, 6238 DeletedFuncs = 5, 6239 DefaultedFuncs = 6, 6240 ConstexprFuncs = 7, 6241 ConstevalFuncs = 8, 6242 }; 6243 if (const auto *CXXFD = dyn_cast<CXXMethodDecl>(FD)) { 6244 if (CXXFD->isVirtual()) { 6245 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 6246 << VirtFuncs; 6247 return None; 6248 } 6249 6250 if (isa<CXXConstructorDecl>(FD)) { 6251 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 6252 << Constructors; 6253 return None; 6254 } 6255 6256 if (isa<CXXDestructorDecl>(FD)) { 6257 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 6258 << Destructors; 6259 return None; 6260 } 6261 } 6262 6263 if (FD->isDeleted()) { 6264 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 6265 << DeletedFuncs; 6266 return None; 6267 } 6268 6269 if (FD->isDefaulted()) { 6270 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 6271 << DefaultedFuncs; 6272 return None; 6273 } 6274 6275 if (FD->isConstexpr()) { 6276 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 6277 << (NewFD->isConsteval() ? ConstevalFuncs : ConstexprFuncs); 6278 return None; 6279 } 6280 6281 // Check general compatibility. 6282 if (areMultiversionVariantFunctionsCompatible( 6283 FD, NewFD, PartialDiagnostic::NullDiagnostic(), 6284 PartialDiagnosticAt(SourceLocation(), 6285 PartialDiagnostic::NullDiagnostic()), 6286 PartialDiagnosticAt( 6287 VariantRef->getExprLoc(), 6288 PDiag(diag::err_omp_declare_variant_doesnt_support)), 6289 PartialDiagnosticAt(VariantRef->getExprLoc(), 6290 PDiag(diag::err_omp_declare_variant_diff) 6291 << FD->getLocation()), 6292 /*TemplatesSupported=*/true, /*ConstexprSupported=*/false, 6293 /*CLinkageMayDiffer=*/true)) 6294 return None; 6295 return std::make_pair(FD, cast<Expr>(DRE)); 6296 } 6297 6298 void Sema::ActOnOpenMPDeclareVariantDirective(FunctionDecl *FD, 6299 Expr *VariantRef, 6300 OMPTraitInfo &TI, 6301 SourceRange SR) { 6302 auto *NewAttr = 6303 OMPDeclareVariantAttr::CreateImplicit(Context, VariantRef, &TI, SR); 6304 FD->addAttr(NewAttr); 6305 } 6306 6307 StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses, 6308 Stmt *AStmt, 6309 SourceLocation StartLoc, 6310 SourceLocation EndLoc) { 6311 if (!AStmt) 6312 return StmtError(); 6313 6314 auto *CS = cast<CapturedStmt>(AStmt); 6315 // 1.2.2 OpenMP Language Terminology 6316 // Structured block - An executable statement with a single entry at the 6317 // top and a single exit at the bottom. 6318 // The point of exit cannot be a branch out of the structured block. 6319 // longjmp() and throw() must not violate the entry/exit criteria. 6320 CS->getCapturedDecl()->setNothrow(); 6321 6322 setFunctionHasBranchProtectedScope(); 6323 6324 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 6325 DSAStack->getTaskgroupReductionRef(), 6326 DSAStack->isCancelRegion()); 6327 } 6328 6329 namespace { 6330 /// Iteration space of a single for loop. 6331 struct LoopIterationSpace final { 6332 /// True if the condition operator is the strict compare operator (<, > or 6333 /// !=). 6334 bool IsStrictCompare = false; 6335 /// Condition of the loop. 6336 Expr *PreCond = nullptr; 6337 /// This expression calculates the number of iterations in the loop. 6338 /// It is always possible to calculate it before starting the loop. 6339 Expr *NumIterations = nullptr; 6340 /// The loop counter variable. 6341 Expr *CounterVar = nullptr; 6342 /// Private loop counter variable. 6343 Expr *PrivateCounterVar = nullptr; 6344 /// This is initializer for the initial value of #CounterVar. 6345 Expr *CounterInit = nullptr; 6346 /// This is step for the #CounterVar used to generate its update: 6347 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration. 6348 Expr *CounterStep = nullptr; 6349 /// Should step be subtracted? 6350 bool Subtract = false; 6351 /// Source range of the loop init. 6352 SourceRange InitSrcRange; 6353 /// Source range of the loop condition. 6354 SourceRange CondSrcRange; 6355 /// Source range of the loop increment. 6356 SourceRange IncSrcRange; 6357 /// Minimum value that can have the loop control variable. Used to support 6358 /// non-rectangular loops. Applied only for LCV with the non-iterator types, 6359 /// since only such variables can be used in non-loop invariant expressions. 6360 Expr *MinValue = nullptr; 6361 /// Maximum value that can have the loop control variable. Used to support 6362 /// non-rectangular loops. Applied only for LCV with the non-iterator type, 6363 /// since only such variables can be used in non-loop invariant expressions. 6364 Expr *MaxValue = nullptr; 6365 /// true, if the lower bound depends on the outer loop control var. 6366 bool IsNonRectangularLB = false; 6367 /// true, if the upper bound depends on the outer loop control var. 6368 bool IsNonRectangularUB = false; 6369 /// Index of the loop this loop depends on and forms non-rectangular loop 6370 /// nest. 6371 unsigned LoopDependentIdx = 0; 6372 /// Final condition for the non-rectangular loop nest support. It is used to 6373 /// check that the number of iterations for this particular counter must be 6374 /// finished. 6375 Expr *FinalCondition = nullptr; 6376 }; 6377 6378 /// Helper class for checking canonical form of the OpenMP loops and 6379 /// extracting iteration space of each loop in the loop nest, that will be used 6380 /// for IR generation. 6381 class OpenMPIterationSpaceChecker { 6382 /// Reference to Sema. 6383 Sema &SemaRef; 6384 /// Data-sharing stack. 6385 DSAStackTy &Stack; 6386 /// A location for diagnostics (when there is no some better location). 6387 SourceLocation DefaultLoc; 6388 /// A location for diagnostics (when increment is not compatible). 6389 SourceLocation ConditionLoc; 6390 /// A source location for referring to loop init later. 6391 SourceRange InitSrcRange; 6392 /// A source location for referring to condition later. 6393 SourceRange ConditionSrcRange; 6394 /// A source location for referring to increment later. 6395 SourceRange IncrementSrcRange; 6396 /// Loop variable. 6397 ValueDecl *LCDecl = nullptr; 6398 /// Reference to loop variable. 6399 Expr *LCRef = nullptr; 6400 /// Lower bound (initializer for the var). 6401 Expr *LB = nullptr; 6402 /// Upper bound. 6403 Expr *UB = nullptr; 6404 /// Loop step (increment). 6405 Expr *Step = nullptr; 6406 /// This flag is true when condition is one of: 6407 /// Var < UB 6408 /// Var <= UB 6409 /// UB > Var 6410 /// UB >= Var 6411 /// This will have no value when the condition is != 6412 llvm::Optional<bool> TestIsLessOp; 6413 /// This flag is true when condition is strict ( < or > ). 6414 bool TestIsStrictOp = false; 6415 /// This flag is true when step is subtracted on each iteration. 6416 bool SubtractStep = false; 6417 /// The outer loop counter this loop depends on (if any). 6418 const ValueDecl *DepDecl = nullptr; 6419 /// Contains number of loop (starts from 1) on which loop counter init 6420 /// expression of this loop depends on. 6421 Optional<unsigned> InitDependOnLC; 6422 /// Contains number of loop (starts from 1) on which loop counter condition 6423 /// expression of this loop depends on. 6424 Optional<unsigned> CondDependOnLC; 6425 /// Checks if the provide statement depends on the loop counter. 6426 Optional<unsigned> doesDependOnLoopCounter(const Stmt *S, bool IsInitializer); 6427 /// Original condition required for checking of the exit condition for 6428 /// non-rectangular loop. 6429 Expr *Condition = nullptr; 6430 6431 public: 6432 OpenMPIterationSpaceChecker(Sema &SemaRef, DSAStackTy &Stack, 6433 SourceLocation DefaultLoc) 6434 : SemaRef(SemaRef), Stack(Stack), DefaultLoc(DefaultLoc), 6435 ConditionLoc(DefaultLoc) {} 6436 /// Check init-expr for canonical loop form and save loop counter 6437 /// variable - #Var and its initialization value - #LB. 6438 bool checkAndSetInit(Stmt *S, bool EmitDiags = true); 6439 /// Check test-expr for canonical form, save upper-bound (#UB), flags 6440 /// for less/greater and for strict/non-strict comparison. 6441 bool checkAndSetCond(Expr *S); 6442 /// Check incr-expr for canonical loop form and return true if it 6443 /// does not conform, otherwise save loop step (#Step). 6444 bool checkAndSetInc(Expr *S); 6445 /// Return the loop counter variable. 6446 ValueDecl *getLoopDecl() const { return LCDecl; } 6447 /// Return the reference expression to loop counter variable. 6448 Expr *getLoopDeclRefExpr() const { return LCRef; } 6449 /// Source range of the loop init. 6450 SourceRange getInitSrcRange() const { return InitSrcRange; } 6451 /// Source range of the loop condition. 6452 SourceRange getConditionSrcRange() const { return ConditionSrcRange; } 6453 /// Source range of the loop increment. 6454 SourceRange getIncrementSrcRange() const { return IncrementSrcRange; } 6455 /// True if the step should be subtracted. 6456 bool shouldSubtractStep() const { return SubtractStep; } 6457 /// True, if the compare operator is strict (<, > or !=). 6458 bool isStrictTestOp() const { return TestIsStrictOp; } 6459 /// Build the expression to calculate the number of iterations. 6460 Expr *buildNumIterations( 6461 Scope *S, ArrayRef<LoopIterationSpace> ResultIterSpaces, bool LimitedType, 6462 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 6463 /// Build the precondition expression for the loops. 6464 Expr * 6465 buildPreCond(Scope *S, Expr *Cond, 6466 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 6467 /// Build reference expression to the counter be used for codegen. 6468 DeclRefExpr * 6469 buildCounterVar(llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 6470 DSAStackTy &DSA) const; 6471 /// Build reference expression to the private counter be used for 6472 /// codegen. 6473 Expr *buildPrivateCounterVar() const; 6474 /// Build initialization of the counter be used for codegen. 6475 Expr *buildCounterInit() const; 6476 /// Build step of the counter be used for codegen. 6477 Expr *buildCounterStep() const; 6478 /// Build loop data with counter value for depend clauses in ordered 6479 /// directives. 6480 Expr * 6481 buildOrderedLoopData(Scope *S, Expr *Counter, 6482 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 6483 SourceLocation Loc, Expr *Inc = nullptr, 6484 OverloadedOperatorKind OOK = OO_Amp); 6485 /// Builds the minimum value for the loop counter. 6486 std::pair<Expr *, Expr *> buildMinMaxValues( 6487 Scope *S, llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 6488 /// Builds final condition for the non-rectangular loops. 6489 Expr *buildFinalCondition(Scope *S) const; 6490 /// Return true if any expression is dependent. 6491 bool dependent() const; 6492 /// Returns true if the initializer forms non-rectangular loop. 6493 bool doesInitDependOnLC() const { return InitDependOnLC.hasValue(); } 6494 /// Returns true if the condition forms non-rectangular loop. 6495 bool doesCondDependOnLC() const { return CondDependOnLC.hasValue(); } 6496 /// Returns index of the loop we depend on (starting from 1), or 0 otherwise. 6497 unsigned getLoopDependentIdx() const { 6498 return InitDependOnLC.getValueOr(CondDependOnLC.getValueOr(0)); 6499 } 6500 6501 private: 6502 /// Check the right-hand side of an assignment in the increment 6503 /// expression. 6504 bool checkAndSetIncRHS(Expr *RHS); 6505 /// Helper to set loop counter variable and its initializer. 6506 bool setLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB, 6507 bool EmitDiags); 6508 /// Helper to set upper bound. 6509 bool setUB(Expr *NewUB, llvm::Optional<bool> LessOp, bool StrictOp, 6510 SourceRange SR, SourceLocation SL); 6511 /// Helper to set loop increment. 6512 bool setStep(Expr *NewStep, bool Subtract); 6513 }; 6514 6515 bool OpenMPIterationSpaceChecker::dependent() const { 6516 if (!LCDecl) { 6517 assert(!LB && !UB && !Step); 6518 return false; 6519 } 6520 return LCDecl->getType()->isDependentType() || 6521 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) || 6522 (Step && Step->isValueDependent()); 6523 } 6524 6525 bool OpenMPIterationSpaceChecker::setLCDeclAndLB(ValueDecl *NewLCDecl, 6526 Expr *NewLCRefExpr, 6527 Expr *NewLB, bool EmitDiags) { 6528 // State consistency checking to ensure correct usage. 6529 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr && 6530 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp); 6531 if (!NewLCDecl || !NewLB) 6532 return true; 6533 LCDecl = getCanonicalDecl(NewLCDecl); 6534 LCRef = NewLCRefExpr; 6535 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB)) 6536 if (const CXXConstructorDecl *Ctor = CE->getConstructor()) 6537 if ((Ctor->isCopyOrMoveConstructor() || 6538 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && 6539 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) 6540 NewLB = CE->getArg(0)->IgnoreParenImpCasts(); 6541 LB = NewLB; 6542 if (EmitDiags) 6543 InitDependOnLC = doesDependOnLoopCounter(LB, /*IsInitializer=*/true); 6544 return false; 6545 } 6546 6547 bool OpenMPIterationSpaceChecker::setUB(Expr *NewUB, 6548 llvm::Optional<bool> LessOp, 6549 bool StrictOp, SourceRange SR, 6550 SourceLocation SL) { 6551 // State consistency checking to ensure correct usage. 6552 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr && 6553 Step == nullptr && !TestIsLessOp && !TestIsStrictOp); 6554 if (!NewUB) 6555 return true; 6556 UB = NewUB; 6557 if (LessOp) 6558 TestIsLessOp = LessOp; 6559 TestIsStrictOp = StrictOp; 6560 ConditionSrcRange = SR; 6561 ConditionLoc = SL; 6562 CondDependOnLC = doesDependOnLoopCounter(UB, /*IsInitializer=*/false); 6563 return false; 6564 } 6565 6566 bool OpenMPIterationSpaceChecker::setStep(Expr *NewStep, bool Subtract) { 6567 // State consistency checking to ensure correct usage. 6568 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr); 6569 if (!NewStep) 6570 return true; 6571 if (!NewStep->isValueDependent()) { 6572 // Check that the step is integer expression. 6573 SourceLocation StepLoc = NewStep->getBeginLoc(); 6574 ExprResult Val = SemaRef.PerformOpenMPImplicitIntegerConversion( 6575 StepLoc, getExprAsWritten(NewStep)); 6576 if (Val.isInvalid()) 6577 return true; 6578 NewStep = Val.get(); 6579 6580 // OpenMP [2.6, Canonical Loop Form, Restrictions] 6581 // If test-expr is of form var relational-op b and relational-op is < or 6582 // <= then incr-expr must cause var to increase on each iteration of the 6583 // loop. If test-expr is of form var relational-op b and relational-op is 6584 // > or >= then incr-expr must cause var to decrease on each iteration of 6585 // the loop. 6586 // If test-expr is of form b relational-op var and relational-op is < or 6587 // <= then incr-expr must cause var to decrease on each iteration of the 6588 // loop. If test-expr is of form b relational-op var and relational-op is 6589 // > or >= then incr-expr must cause var to increase on each iteration of 6590 // the loop. 6591 Optional<llvm::APSInt> Result = 6592 NewStep->getIntegerConstantExpr(SemaRef.Context); 6593 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation(); 6594 bool IsConstNeg = 6595 Result && Result->isSigned() && (Subtract != Result->isNegative()); 6596 bool IsConstPos = 6597 Result && Result->isSigned() && (Subtract == Result->isNegative()); 6598 bool IsConstZero = Result && !Result->getBoolValue(); 6599 6600 // != with increment is treated as <; != with decrement is treated as > 6601 if (!TestIsLessOp.hasValue()) 6602 TestIsLessOp = IsConstPos || (IsUnsigned && !Subtract); 6603 if (UB && (IsConstZero || 6604 (TestIsLessOp.getValue() ? 6605 (IsConstNeg || (IsUnsigned && Subtract)) : 6606 (IsConstPos || (IsUnsigned && !Subtract))))) { 6607 SemaRef.Diag(NewStep->getExprLoc(), 6608 diag::err_omp_loop_incr_not_compatible) 6609 << LCDecl << TestIsLessOp.getValue() << NewStep->getSourceRange(); 6610 SemaRef.Diag(ConditionLoc, 6611 diag::note_omp_loop_cond_requres_compatible_incr) 6612 << TestIsLessOp.getValue() << ConditionSrcRange; 6613 return true; 6614 } 6615 if (TestIsLessOp.getValue() == Subtract) { 6616 NewStep = 6617 SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep) 6618 .get(); 6619 Subtract = !Subtract; 6620 } 6621 } 6622 6623 Step = NewStep; 6624 SubtractStep = Subtract; 6625 return false; 6626 } 6627 6628 namespace { 6629 /// Checker for the non-rectangular loops. Checks if the initializer or 6630 /// condition expression references loop counter variable. 6631 class LoopCounterRefChecker final 6632 : public ConstStmtVisitor<LoopCounterRefChecker, bool> { 6633 Sema &SemaRef; 6634 DSAStackTy &Stack; 6635 const ValueDecl *CurLCDecl = nullptr; 6636 const ValueDecl *DepDecl = nullptr; 6637 const ValueDecl *PrevDepDecl = nullptr; 6638 bool IsInitializer = true; 6639 unsigned BaseLoopId = 0; 6640 bool checkDecl(const Expr *E, const ValueDecl *VD) { 6641 if (getCanonicalDecl(VD) == getCanonicalDecl(CurLCDecl)) { 6642 SemaRef.Diag(E->getExprLoc(), diag::err_omp_stmt_depends_on_loop_counter) 6643 << (IsInitializer ? 0 : 1); 6644 return false; 6645 } 6646 const auto &&Data = Stack.isLoopControlVariable(VD); 6647 // OpenMP, 2.9.1 Canonical Loop Form, Restrictions. 6648 // The type of the loop iterator on which we depend may not have a random 6649 // access iterator type. 6650 if (Data.first && VD->getType()->isRecordType()) { 6651 SmallString<128> Name; 6652 llvm::raw_svector_ostream OS(Name); 6653 VD->getNameForDiagnostic(OS, SemaRef.getPrintingPolicy(), 6654 /*Qualified=*/true); 6655 SemaRef.Diag(E->getExprLoc(), 6656 diag::err_omp_wrong_dependency_iterator_type) 6657 << OS.str(); 6658 SemaRef.Diag(VD->getLocation(), diag::note_previous_decl) << VD; 6659 return false; 6660 } 6661 if (Data.first && 6662 (DepDecl || (PrevDepDecl && 6663 getCanonicalDecl(VD) != getCanonicalDecl(PrevDepDecl)))) { 6664 if (!DepDecl && PrevDepDecl) 6665 DepDecl = PrevDepDecl; 6666 SmallString<128> Name; 6667 llvm::raw_svector_ostream OS(Name); 6668 DepDecl->getNameForDiagnostic(OS, SemaRef.getPrintingPolicy(), 6669 /*Qualified=*/true); 6670 SemaRef.Diag(E->getExprLoc(), 6671 diag::err_omp_invariant_or_linear_dependency) 6672 << OS.str(); 6673 return false; 6674 } 6675 if (Data.first) { 6676 DepDecl = VD; 6677 BaseLoopId = Data.first; 6678 } 6679 return Data.first; 6680 } 6681 6682 public: 6683 bool VisitDeclRefExpr(const DeclRefExpr *E) { 6684 const ValueDecl *VD = E->getDecl(); 6685 if (isa<VarDecl>(VD)) 6686 return checkDecl(E, VD); 6687 return false; 6688 } 6689 bool VisitMemberExpr(const MemberExpr *E) { 6690 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) { 6691 const ValueDecl *VD = E->getMemberDecl(); 6692 if (isa<VarDecl>(VD) || isa<FieldDecl>(VD)) 6693 return checkDecl(E, VD); 6694 } 6695 return false; 6696 } 6697 bool VisitStmt(const Stmt *S) { 6698 bool Res = false; 6699 for (const Stmt *Child : S->children()) 6700 Res = (Child && Visit(Child)) || Res; 6701 return Res; 6702 } 6703 explicit LoopCounterRefChecker(Sema &SemaRef, DSAStackTy &Stack, 6704 const ValueDecl *CurLCDecl, bool IsInitializer, 6705 const ValueDecl *PrevDepDecl = nullptr) 6706 : SemaRef(SemaRef), Stack(Stack), CurLCDecl(CurLCDecl), 6707 PrevDepDecl(PrevDepDecl), IsInitializer(IsInitializer) {} 6708 unsigned getBaseLoopId() const { 6709 assert(CurLCDecl && "Expected loop dependency."); 6710 return BaseLoopId; 6711 } 6712 const ValueDecl *getDepDecl() const { 6713 assert(CurLCDecl && "Expected loop dependency."); 6714 return DepDecl; 6715 } 6716 }; 6717 } // namespace 6718 6719 Optional<unsigned> 6720 OpenMPIterationSpaceChecker::doesDependOnLoopCounter(const Stmt *S, 6721 bool IsInitializer) { 6722 // Check for the non-rectangular loops. 6723 LoopCounterRefChecker LoopStmtChecker(SemaRef, Stack, LCDecl, IsInitializer, 6724 DepDecl); 6725 if (LoopStmtChecker.Visit(S)) { 6726 DepDecl = LoopStmtChecker.getDepDecl(); 6727 return LoopStmtChecker.getBaseLoopId(); 6728 } 6729 return llvm::None; 6730 } 6731 6732 bool OpenMPIterationSpaceChecker::checkAndSetInit(Stmt *S, bool EmitDiags) { 6733 // Check init-expr for canonical loop form and save loop counter 6734 // variable - #Var and its initialization value - #LB. 6735 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following: 6736 // var = lb 6737 // integer-type var = lb 6738 // random-access-iterator-type var = lb 6739 // pointer-type var = lb 6740 // 6741 if (!S) { 6742 if (EmitDiags) { 6743 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init); 6744 } 6745 return true; 6746 } 6747 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S)) 6748 if (!ExprTemp->cleanupsHaveSideEffects()) 6749 S = ExprTemp->getSubExpr(); 6750 6751 InitSrcRange = S->getSourceRange(); 6752 if (Expr *E = dyn_cast<Expr>(S)) 6753 S = E->IgnoreParens(); 6754 if (auto *BO = dyn_cast<BinaryOperator>(S)) { 6755 if (BO->getOpcode() == BO_Assign) { 6756 Expr *LHS = BO->getLHS()->IgnoreParens(); 6757 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) { 6758 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) 6759 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 6760 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 6761 EmitDiags); 6762 return setLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS(), EmitDiags); 6763 } 6764 if (auto *ME = dyn_cast<MemberExpr>(LHS)) { 6765 if (ME->isArrow() && 6766 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 6767 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 6768 EmitDiags); 6769 } 6770 } 6771 } else if (auto *DS = dyn_cast<DeclStmt>(S)) { 6772 if (DS->isSingleDecl()) { 6773 if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) { 6774 if (Var->hasInit() && !Var->getType()->isReferenceType()) { 6775 // Accept non-canonical init form here but emit ext. warning. 6776 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags) 6777 SemaRef.Diag(S->getBeginLoc(), 6778 diag::ext_omp_loop_not_canonical_init) 6779 << S->getSourceRange(); 6780 return setLCDeclAndLB( 6781 Var, 6782 buildDeclRefExpr(SemaRef, Var, 6783 Var->getType().getNonReferenceType(), 6784 DS->getBeginLoc()), 6785 Var->getInit(), EmitDiags); 6786 } 6787 } 6788 } 6789 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 6790 if (CE->getOperator() == OO_Equal) { 6791 Expr *LHS = CE->getArg(0); 6792 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) { 6793 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) 6794 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 6795 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 6796 EmitDiags); 6797 return setLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1), EmitDiags); 6798 } 6799 if (auto *ME = dyn_cast<MemberExpr>(LHS)) { 6800 if (ME->isArrow() && 6801 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 6802 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 6803 EmitDiags); 6804 } 6805 } 6806 } 6807 6808 if (dependent() || SemaRef.CurContext->isDependentContext()) 6809 return false; 6810 if (EmitDiags) { 6811 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_init) 6812 << S->getSourceRange(); 6813 } 6814 return true; 6815 } 6816 6817 /// Ignore parenthesizes, implicit casts, copy constructor and return the 6818 /// variable (which may be the loop variable) if possible. 6819 static const ValueDecl *getInitLCDecl(const Expr *E) { 6820 if (!E) 6821 return nullptr; 6822 E = getExprAsWritten(E); 6823 if (const auto *CE = dyn_cast_or_null<CXXConstructExpr>(E)) 6824 if (const CXXConstructorDecl *Ctor = CE->getConstructor()) 6825 if ((Ctor->isCopyOrMoveConstructor() || 6826 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && 6827 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) 6828 E = CE->getArg(0)->IgnoreParenImpCasts(); 6829 if (const auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) { 6830 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) 6831 return getCanonicalDecl(VD); 6832 } 6833 if (const auto *ME = dyn_cast_or_null<MemberExpr>(E)) 6834 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 6835 return getCanonicalDecl(ME->getMemberDecl()); 6836 return nullptr; 6837 } 6838 6839 bool OpenMPIterationSpaceChecker::checkAndSetCond(Expr *S) { 6840 // Check test-expr for canonical form, save upper-bound UB, flags for 6841 // less/greater and for strict/non-strict comparison. 6842 // OpenMP [2.9] Canonical loop form. Test-expr may be one of the following: 6843 // var relational-op b 6844 // b relational-op var 6845 // 6846 bool IneqCondIsCanonical = SemaRef.getLangOpts().OpenMP >= 50; 6847 if (!S) { 6848 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) 6849 << (IneqCondIsCanonical ? 1 : 0) << LCDecl; 6850 return true; 6851 } 6852 Condition = S; 6853 S = getExprAsWritten(S); 6854 SourceLocation CondLoc = S->getBeginLoc(); 6855 if (auto *BO = dyn_cast<BinaryOperator>(S)) { 6856 if (BO->isRelationalOp()) { 6857 if (getInitLCDecl(BO->getLHS()) == LCDecl) 6858 return setUB(BO->getRHS(), 6859 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE), 6860 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT), 6861 BO->getSourceRange(), BO->getOperatorLoc()); 6862 if (getInitLCDecl(BO->getRHS()) == LCDecl) 6863 return setUB(BO->getLHS(), 6864 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE), 6865 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT), 6866 BO->getSourceRange(), BO->getOperatorLoc()); 6867 } else if (IneqCondIsCanonical && BO->getOpcode() == BO_NE) 6868 return setUB( 6869 getInitLCDecl(BO->getLHS()) == LCDecl ? BO->getRHS() : BO->getLHS(), 6870 /*LessOp=*/llvm::None, 6871 /*StrictOp=*/true, BO->getSourceRange(), BO->getOperatorLoc()); 6872 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 6873 if (CE->getNumArgs() == 2) { 6874 auto Op = CE->getOperator(); 6875 switch (Op) { 6876 case OO_Greater: 6877 case OO_GreaterEqual: 6878 case OO_Less: 6879 case OO_LessEqual: 6880 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 6881 return setUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual, 6882 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(), 6883 CE->getOperatorLoc()); 6884 if (getInitLCDecl(CE->getArg(1)) == LCDecl) 6885 return setUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual, 6886 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(), 6887 CE->getOperatorLoc()); 6888 break; 6889 case OO_ExclaimEqual: 6890 if (IneqCondIsCanonical) 6891 return setUB(getInitLCDecl(CE->getArg(0)) == LCDecl ? CE->getArg(1) 6892 : CE->getArg(0), 6893 /*LessOp=*/llvm::None, 6894 /*StrictOp=*/true, CE->getSourceRange(), 6895 CE->getOperatorLoc()); 6896 break; 6897 default: 6898 break; 6899 } 6900 } 6901 } 6902 if (dependent() || SemaRef.CurContext->isDependentContext()) 6903 return false; 6904 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond) 6905 << (IneqCondIsCanonical ? 1 : 0) << S->getSourceRange() << LCDecl; 6906 return true; 6907 } 6908 6909 bool OpenMPIterationSpaceChecker::checkAndSetIncRHS(Expr *RHS) { 6910 // RHS of canonical loop form increment can be: 6911 // var + incr 6912 // incr + var 6913 // var - incr 6914 // 6915 RHS = RHS->IgnoreParenImpCasts(); 6916 if (auto *BO = dyn_cast<BinaryOperator>(RHS)) { 6917 if (BO->isAdditiveOp()) { 6918 bool IsAdd = BO->getOpcode() == BO_Add; 6919 if (getInitLCDecl(BO->getLHS()) == LCDecl) 6920 return setStep(BO->getRHS(), !IsAdd); 6921 if (IsAdd && getInitLCDecl(BO->getRHS()) == LCDecl) 6922 return setStep(BO->getLHS(), /*Subtract=*/false); 6923 } 6924 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) { 6925 bool IsAdd = CE->getOperator() == OO_Plus; 6926 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) { 6927 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 6928 return setStep(CE->getArg(1), !IsAdd); 6929 if (IsAdd && getInitLCDecl(CE->getArg(1)) == LCDecl) 6930 return setStep(CE->getArg(0), /*Subtract=*/false); 6931 } 6932 } 6933 if (dependent() || SemaRef.CurContext->isDependentContext()) 6934 return false; 6935 SemaRef.Diag(RHS->getBeginLoc(), diag::err_omp_loop_not_canonical_incr) 6936 << RHS->getSourceRange() << LCDecl; 6937 return true; 6938 } 6939 6940 bool OpenMPIterationSpaceChecker::checkAndSetInc(Expr *S) { 6941 // Check incr-expr for canonical loop form and return true if it 6942 // does not conform. 6943 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following: 6944 // ++var 6945 // var++ 6946 // --var 6947 // var-- 6948 // var += incr 6949 // var -= incr 6950 // var = var + incr 6951 // var = incr + var 6952 // var = var - incr 6953 // 6954 if (!S) { 6955 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl; 6956 return true; 6957 } 6958 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S)) 6959 if (!ExprTemp->cleanupsHaveSideEffects()) 6960 S = ExprTemp->getSubExpr(); 6961 6962 IncrementSrcRange = S->getSourceRange(); 6963 S = S->IgnoreParens(); 6964 if (auto *UO = dyn_cast<UnaryOperator>(S)) { 6965 if (UO->isIncrementDecrementOp() && 6966 getInitLCDecl(UO->getSubExpr()) == LCDecl) 6967 return setStep(SemaRef 6968 .ActOnIntegerConstant(UO->getBeginLoc(), 6969 (UO->isDecrementOp() ? -1 : 1)) 6970 .get(), 6971 /*Subtract=*/false); 6972 } else if (auto *BO = dyn_cast<BinaryOperator>(S)) { 6973 switch (BO->getOpcode()) { 6974 case BO_AddAssign: 6975 case BO_SubAssign: 6976 if (getInitLCDecl(BO->getLHS()) == LCDecl) 6977 return setStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign); 6978 break; 6979 case BO_Assign: 6980 if (getInitLCDecl(BO->getLHS()) == LCDecl) 6981 return checkAndSetIncRHS(BO->getRHS()); 6982 break; 6983 default: 6984 break; 6985 } 6986 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 6987 switch (CE->getOperator()) { 6988 case OO_PlusPlus: 6989 case OO_MinusMinus: 6990 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 6991 return setStep(SemaRef 6992 .ActOnIntegerConstant( 6993 CE->getBeginLoc(), 6994 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)) 6995 .get(), 6996 /*Subtract=*/false); 6997 break; 6998 case OO_PlusEqual: 6999 case OO_MinusEqual: 7000 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 7001 return setStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual); 7002 break; 7003 case OO_Equal: 7004 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 7005 return checkAndSetIncRHS(CE->getArg(1)); 7006 break; 7007 default: 7008 break; 7009 } 7010 } 7011 if (dependent() || SemaRef.CurContext->isDependentContext()) 7012 return false; 7013 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_incr) 7014 << S->getSourceRange() << LCDecl; 7015 return true; 7016 } 7017 7018 static ExprResult 7019 tryBuildCapture(Sema &SemaRef, Expr *Capture, 7020 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 7021 if (SemaRef.CurContext->isDependentContext() || Capture->containsErrors()) 7022 return Capture; 7023 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects)) 7024 return SemaRef.PerformImplicitConversion( 7025 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting, 7026 /*AllowExplicit=*/true); 7027 auto I = Captures.find(Capture); 7028 if (I != Captures.end()) 7029 return buildCapture(SemaRef, Capture, I->second); 7030 DeclRefExpr *Ref = nullptr; 7031 ExprResult Res = buildCapture(SemaRef, Capture, Ref); 7032 Captures[Capture] = Ref; 7033 return Res; 7034 } 7035 7036 /// Calculate number of iterations, transforming to unsigned, if number of 7037 /// iterations may be larger than the original type. 7038 static Expr * 7039 calculateNumIters(Sema &SemaRef, Scope *S, SourceLocation DefaultLoc, 7040 Expr *Lower, Expr *Upper, Expr *Step, QualType LCTy, 7041 bool TestIsStrictOp, bool RoundToStep, 7042 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 7043 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures); 7044 if (!NewStep.isUsable()) 7045 return nullptr; 7046 llvm::APSInt LRes, SRes; 7047 bool IsLowerConst = false, IsStepConst = false; 7048 if (Optional<llvm::APSInt> Res = Lower->getIntegerConstantExpr(SemaRef.Context)) { 7049 LRes = *Res; 7050 IsLowerConst = true; 7051 } 7052 if (Optional<llvm::APSInt> Res = Step->getIntegerConstantExpr(SemaRef.Context)) { 7053 SRes = *Res; 7054 IsStepConst = true; 7055 } 7056 bool NoNeedToConvert = IsLowerConst && !RoundToStep && 7057 ((!TestIsStrictOp && LRes.isNonNegative()) || 7058 (TestIsStrictOp && LRes.isStrictlyPositive())); 7059 bool NeedToReorganize = false; 7060 // Check if any subexpressions in Lower -Step [+ 1] lead to overflow. 7061 if (!NoNeedToConvert && IsLowerConst && 7062 (TestIsStrictOp || (RoundToStep && IsStepConst))) { 7063 NoNeedToConvert = true; 7064 if (RoundToStep) { 7065 unsigned BW = LRes.getBitWidth() > SRes.getBitWidth() 7066 ? LRes.getBitWidth() 7067 : SRes.getBitWidth(); 7068 LRes = LRes.extend(BW + 1); 7069 LRes.setIsSigned(true); 7070 SRes = SRes.extend(BW + 1); 7071 SRes.setIsSigned(true); 7072 LRes -= SRes; 7073 NoNeedToConvert = LRes.trunc(BW).extend(BW + 1) == LRes; 7074 LRes = LRes.trunc(BW); 7075 } 7076 if (TestIsStrictOp) { 7077 unsigned BW = LRes.getBitWidth(); 7078 LRes = LRes.extend(BW + 1); 7079 LRes.setIsSigned(true); 7080 ++LRes; 7081 NoNeedToConvert = 7082 NoNeedToConvert && LRes.trunc(BW).extend(BW + 1) == LRes; 7083 // truncate to the original bitwidth. 7084 LRes = LRes.trunc(BW); 7085 } 7086 NeedToReorganize = NoNeedToConvert; 7087 } 7088 llvm::APSInt URes; 7089 bool IsUpperConst = false; 7090 if (Optional<llvm::APSInt> Res = Upper->getIntegerConstantExpr(SemaRef.Context)) { 7091 URes = *Res; 7092 IsUpperConst = true; 7093 } 7094 if (NoNeedToConvert && IsLowerConst && IsUpperConst && 7095 (!RoundToStep || IsStepConst)) { 7096 unsigned BW = LRes.getBitWidth() > URes.getBitWidth() ? LRes.getBitWidth() 7097 : URes.getBitWidth(); 7098 LRes = LRes.extend(BW + 1); 7099 LRes.setIsSigned(true); 7100 URes = URes.extend(BW + 1); 7101 URes.setIsSigned(true); 7102 URes -= LRes; 7103 NoNeedToConvert = URes.trunc(BW).extend(BW + 1) == URes; 7104 NeedToReorganize = NoNeedToConvert; 7105 } 7106 // If the boundaries are not constant or (Lower - Step [+ 1]) is not constant 7107 // or less than zero (Upper - (Lower - Step [+ 1]) may overflow) - promote to 7108 // unsigned. 7109 if ((!NoNeedToConvert || (LRes.isNegative() && !IsUpperConst)) && 7110 !LCTy->isDependentType() && LCTy->isIntegerType()) { 7111 QualType LowerTy = Lower->getType(); 7112 QualType UpperTy = Upper->getType(); 7113 uint64_t LowerSize = SemaRef.Context.getTypeSize(LowerTy); 7114 uint64_t UpperSize = SemaRef.Context.getTypeSize(UpperTy); 7115 if ((LowerSize <= UpperSize && UpperTy->hasSignedIntegerRepresentation()) || 7116 (LowerSize > UpperSize && LowerTy->hasSignedIntegerRepresentation())) { 7117 QualType CastType = SemaRef.Context.getIntTypeForBitwidth( 7118 LowerSize > UpperSize ? LowerSize : UpperSize, /*Signed=*/0); 7119 Upper = 7120 SemaRef 7121 .PerformImplicitConversion( 7122 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Upper).get(), 7123 CastType, Sema::AA_Converting) 7124 .get(); 7125 Lower = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Lower).get(); 7126 NewStep = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, NewStep.get()); 7127 } 7128 } 7129 if (!Lower || !Upper || NewStep.isInvalid()) 7130 return nullptr; 7131 7132 ExprResult Diff; 7133 // If need to reorganize, then calculate the form as Upper - (Lower - Step [+ 7134 // 1]). 7135 if (NeedToReorganize) { 7136 Diff = Lower; 7137 7138 if (RoundToStep) { 7139 // Lower - Step 7140 Diff = 7141 SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Diff.get(), NewStep.get()); 7142 if (!Diff.isUsable()) 7143 return nullptr; 7144 } 7145 7146 // Lower - Step [+ 1] 7147 if (TestIsStrictOp) 7148 Diff = SemaRef.BuildBinOp( 7149 S, DefaultLoc, BO_Add, Diff.get(), 7150 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 7151 if (!Diff.isUsable()) 7152 return nullptr; 7153 7154 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 7155 if (!Diff.isUsable()) 7156 return nullptr; 7157 7158 // Upper - (Lower - Step [+ 1]). 7159 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Diff.get()); 7160 if (!Diff.isUsable()) 7161 return nullptr; 7162 } else { 7163 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower); 7164 7165 if (!Diff.isUsable() && LCTy->getAsCXXRecordDecl()) { 7166 // BuildBinOp already emitted error, this one is to point user to upper 7167 // and lower bound, and to tell what is passed to 'operator-'. 7168 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx) 7169 << Upper->getSourceRange() << Lower->getSourceRange(); 7170 return nullptr; 7171 } 7172 7173 if (!Diff.isUsable()) 7174 return nullptr; 7175 7176 // Upper - Lower [- 1] 7177 if (TestIsStrictOp) 7178 Diff = SemaRef.BuildBinOp( 7179 S, DefaultLoc, BO_Sub, Diff.get(), 7180 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 7181 if (!Diff.isUsable()) 7182 return nullptr; 7183 7184 if (RoundToStep) { 7185 // Upper - Lower [- 1] + Step 7186 Diff = 7187 SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get()); 7188 if (!Diff.isUsable()) 7189 return nullptr; 7190 } 7191 } 7192 7193 // Parentheses (for dumping/debugging purposes only). 7194 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 7195 if (!Diff.isUsable()) 7196 return nullptr; 7197 7198 // (Upper - Lower [- 1] + Step) / Step or (Upper - Lower) / Step 7199 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get()); 7200 if (!Diff.isUsable()) 7201 return nullptr; 7202 7203 return Diff.get(); 7204 } 7205 7206 /// Build the expression to calculate the number of iterations. 7207 Expr *OpenMPIterationSpaceChecker::buildNumIterations( 7208 Scope *S, ArrayRef<LoopIterationSpace> ResultIterSpaces, bool LimitedType, 7209 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 7210 QualType VarType = LCDecl->getType().getNonReferenceType(); 7211 if (!VarType->isIntegerType() && !VarType->isPointerType() && 7212 !SemaRef.getLangOpts().CPlusPlus) 7213 return nullptr; 7214 Expr *LBVal = LB; 7215 Expr *UBVal = UB; 7216 // LB = TestIsLessOp.getValue() ? min(LB(MinVal), LB(MaxVal)) : 7217 // max(LB(MinVal), LB(MaxVal)) 7218 if (InitDependOnLC) { 7219 const LoopIterationSpace &IS = 7220 ResultIterSpaces[ResultIterSpaces.size() - 1 - 7221 InitDependOnLC.getValueOr( 7222 CondDependOnLC.getValueOr(0))]; 7223 if (!IS.MinValue || !IS.MaxValue) 7224 return nullptr; 7225 // OuterVar = Min 7226 ExprResult MinValue = 7227 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MinValue); 7228 if (!MinValue.isUsable()) 7229 return nullptr; 7230 7231 ExprResult LBMinVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign, 7232 IS.CounterVar, MinValue.get()); 7233 if (!LBMinVal.isUsable()) 7234 return nullptr; 7235 // OuterVar = Min, LBVal 7236 LBMinVal = 7237 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, LBMinVal.get(), LBVal); 7238 if (!LBMinVal.isUsable()) 7239 return nullptr; 7240 // (OuterVar = Min, LBVal) 7241 LBMinVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, LBMinVal.get()); 7242 if (!LBMinVal.isUsable()) 7243 return nullptr; 7244 7245 // OuterVar = Max 7246 ExprResult MaxValue = 7247 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MaxValue); 7248 if (!MaxValue.isUsable()) 7249 return nullptr; 7250 7251 ExprResult LBMaxVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign, 7252 IS.CounterVar, MaxValue.get()); 7253 if (!LBMaxVal.isUsable()) 7254 return nullptr; 7255 // OuterVar = Max, LBVal 7256 LBMaxVal = 7257 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, LBMaxVal.get(), LBVal); 7258 if (!LBMaxVal.isUsable()) 7259 return nullptr; 7260 // (OuterVar = Max, LBVal) 7261 LBMaxVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, LBMaxVal.get()); 7262 if (!LBMaxVal.isUsable()) 7263 return nullptr; 7264 7265 Expr *LBMin = tryBuildCapture(SemaRef, LBMinVal.get(), Captures).get(); 7266 Expr *LBMax = tryBuildCapture(SemaRef, LBMaxVal.get(), Captures).get(); 7267 if (!LBMin || !LBMax) 7268 return nullptr; 7269 // LB(MinVal) < LB(MaxVal) 7270 ExprResult MinLessMaxRes = 7271 SemaRef.BuildBinOp(S, DefaultLoc, BO_LT, LBMin, LBMax); 7272 if (!MinLessMaxRes.isUsable()) 7273 return nullptr; 7274 Expr *MinLessMax = 7275 tryBuildCapture(SemaRef, MinLessMaxRes.get(), Captures).get(); 7276 if (!MinLessMax) 7277 return nullptr; 7278 if (TestIsLessOp.getValue()) { 7279 // LB(MinVal) < LB(MaxVal) ? LB(MinVal) : LB(MaxVal) - min(LB(MinVal), 7280 // LB(MaxVal)) 7281 ExprResult MinLB = SemaRef.ActOnConditionalOp(DefaultLoc, DefaultLoc, 7282 MinLessMax, LBMin, LBMax); 7283 if (!MinLB.isUsable()) 7284 return nullptr; 7285 LBVal = MinLB.get(); 7286 } else { 7287 // LB(MinVal) < LB(MaxVal) ? LB(MaxVal) : LB(MinVal) - max(LB(MinVal), 7288 // LB(MaxVal)) 7289 ExprResult MaxLB = SemaRef.ActOnConditionalOp(DefaultLoc, DefaultLoc, 7290 MinLessMax, LBMax, LBMin); 7291 if (!MaxLB.isUsable()) 7292 return nullptr; 7293 LBVal = MaxLB.get(); 7294 } 7295 } 7296 // UB = TestIsLessOp.getValue() ? max(UB(MinVal), UB(MaxVal)) : 7297 // min(UB(MinVal), UB(MaxVal)) 7298 if (CondDependOnLC) { 7299 const LoopIterationSpace &IS = 7300 ResultIterSpaces[ResultIterSpaces.size() - 1 - 7301 InitDependOnLC.getValueOr( 7302 CondDependOnLC.getValueOr(0))]; 7303 if (!IS.MinValue || !IS.MaxValue) 7304 return nullptr; 7305 // OuterVar = Min 7306 ExprResult MinValue = 7307 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MinValue); 7308 if (!MinValue.isUsable()) 7309 return nullptr; 7310 7311 ExprResult UBMinVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign, 7312 IS.CounterVar, MinValue.get()); 7313 if (!UBMinVal.isUsable()) 7314 return nullptr; 7315 // OuterVar = Min, UBVal 7316 UBMinVal = 7317 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, UBMinVal.get(), UBVal); 7318 if (!UBMinVal.isUsable()) 7319 return nullptr; 7320 // (OuterVar = Min, UBVal) 7321 UBMinVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, UBMinVal.get()); 7322 if (!UBMinVal.isUsable()) 7323 return nullptr; 7324 7325 // OuterVar = Max 7326 ExprResult MaxValue = 7327 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MaxValue); 7328 if (!MaxValue.isUsable()) 7329 return nullptr; 7330 7331 ExprResult UBMaxVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign, 7332 IS.CounterVar, MaxValue.get()); 7333 if (!UBMaxVal.isUsable()) 7334 return nullptr; 7335 // OuterVar = Max, UBVal 7336 UBMaxVal = 7337 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, UBMaxVal.get(), UBVal); 7338 if (!UBMaxVal.isUsable()) 7339 return nullptr; 7340 // (OuterVar = Max, UBVal) 7341 UBMaxVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, UBMaxVal.get()); 7342 if (!UBMaxVal.isUsable()) 7343 return nullptr; 7344 7345 Expr *UBMin = tryBuildCapture(SemaRef, UBMinVal.get(), Captures).get(); 7346 Expr *UBMax = tryBuildCapture(SemaRef, UBMaxVal.get(), Captures).get(); 7347 if (!UBMin || !UBMax) 7348 return nullptr; 7349 // UB(MinVal) > UB(MaxVal) 7350 ExprResult MinGreaterMaxRes = 7351 SemaRef.BuildBinOp(S, DefaultLoc, BO_GT, UBMin, UBMax); 7352 if (!MinGreaterMaxRes.isUsable()) 7353 return nullptr; 7354 Expr *MinGreaterMax = 7355 tryBuildCapture(SemaRef, MinGreaterMaxRes.get(), Captures).get(); 7356 if (!MinGreaterMax) 7357 return nullptr; 7358 if (TestIsLessOp.getValue()) { 7359 // UB(MinVal) > UB(MaxVal) ? UB(MinVal) : UB(MaxVal) - max(UB(MinVal), 7360 // UB(MaxVal)) 7361 ExprResult MaxUB = SemaRef.ActOnConditionalOp( 7362 DefaultLoc, DefaultLoc, MinGreaterMax, UBMin, UBMax); 7363 if (!MaxUB.isUsable()) 7364 return nullptr; 7365 UBVal = MaxUB.get(); 7366 } else { 7367 // UB(MinVal) > UB(MaxVal) ? UB(MaxVal) : UB(MinVal) - min(UB(MinVal), 7368 // UB(MaxVal)) 7369 ExprResult MinUB = SemaRef.ActOnConditionalOp( 7370 DefaultLoc, DefaultLoc, MinGreaterMax, UBMax, UBMin); 7371 if (!MinUB.isUsable()) 7372 return nullptr; 7373 UBVal = MinUB.get(); 7374 } 7375 } 7376 Expr *UBExpr = TestIsLessOp.getValue() ? UBVal : LBVal; 7377 Expr *LBExpr = TestIsLessOp.getValue() ? LBVal : UBVal; 7378 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get(); 7379 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get(); 7380 if (!Upper || !Lower) 7381 return nullptr; 7382 7383 ExprResult Diff = 7384 calculateNumIters(SemaRef, S, DefaultLoc, Lower, Upper, Step, VarType, 7385 TestIsStrictOp, /*RoundToStep=*/true, Captures); 7386 if (!Diff.isUsable()) 7387 return nullptr; 7388 7389 // OpenMP runtime requires 32-bit or 64-bit loop variables. 7390 QualType Type = Diff.get()->getType(); 7391 ASTContext &C = SemaRef.Context; 7392 bool UseVarType = VarType->hasIntegerRepresentation() && 7393 C.getTypeSize(Type) > C.getTypeSize(VarType); 7394 if (!Type->isIntegerType() || UseVarType) { 7395 unsigned NewSize = 7396 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type); 7397 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation() 7398 : Type->hasSignedIntegerRepresentation(); 7399 Type = C.getIntTypeForBitwidth(NewSize, IsSigned); 7400 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) { 7401 Diff = SemaRef.PerformImplicitConversion( 7402 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true); 7403 if (!Diff.isUsable()) 7404 return nullptr; 7405 } 7406 } 7407 if (LimitedType) { 7408 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32; 7409 if (NewSize != C.getTypeSize(Type)) { 7410 if (NewSize < C.getTypeSize(Type)) { 7411 assert(NewSize == 64 && "incorrect loop var size"); 7412 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var) 7413 << InitSrcRange << ConditionSrcRange; 7414 } 7415 QualType NewType = C.getIntTypeForBitwidth( 7416 NewSize, Type->hasSignedIntegerRepresentation() || 7417 C.getTypeSize(Type) < NewSize); 7418 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) { 7419 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType, 7420 Sema::AA_Converting, true); 7421 if (!Diff.isUsable()) 7422 return nullptr; 7423 } 7424 } 7425 } 7426 7427 return Diff.get(); 7428 } 7429 7430 std::pair<Expr *, Expr *> OpenMPIterationSpaceChecker::buildMinMaxValues( 7431 Scope *S, llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 7432 // Do not build for iterators, they cannot be used in non-rectangular loop 7433 // nests. 7434 if (LCDecl->getType()->isRecordType()) 7435 return std::make_pair(nullptr, nullptr); 7436 // If we subtract, the min is in the condition, otherwise the min is in the 7437 // init value. 7438 Expr *MinExpr = nullptr; 7439 Expr *MaxExpr = nullptr; 7440 Expr *LBExpr = TestIsLessOp.getValue() ? LB : UB; 7441 Expr *UBExpr = TestIsLessOp.getValue() ? UB : LB; 7442 bool LBNonRect = TestIsLessOp.getValue() ? InitDependOnLC.hasValue() 7443 : CondDependOnLC.hasValue(); 7444 bool UBNonRect = TestIsLessOp.getValue() ? CondDependOnLC.hasValue() 7445 : InitDependOnLC.hasValue(); 7446 Expr *Lower = 7447 LBNonRect ? LBExpr : tryBuildCapture(SemaRef, LBExpr, Captures).get(); 7448 Expr *Upper = 7449 UBNonRect ? UBExpr : tryBuildCapture(SemaRef, UBExpr, Captures).get(); 7450 if (!Upper || !Lower) 7451 return std::make_pair(nullptr, nullptr); 7452 7453 if (TestIsLessOp.getValue()) 7454 MinExpr = Lower; 7455 else 7456 MaxExpr = Upper; 7457 7458 // Build minimum/maximum value based on number of iterations. 7459 QualType VarType = LCDecl->getType().getNonReferenceType(); 7460 7461 ExprResult Diff = 7462 calculateNumIters(SemaRef, S, DefaultLoc, Lower, Upper, Step, VarType, 7463 TestIsStrictOp, /*RoundToStep=*/false, Captures); 7464 if (!Diff.isUsable()) 7465 return std::make_pair(nullptr, nullptr); 7466 7467 // ((Upper - Lower [- 1]) / Step) * Step 7468 // Parentheses (for dumping/debugging purposes only). 7469 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 7470 if (!Diff.isUsable()) 7471 return std::make_pair(nullptr, nullptr); 7472 7473 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures); 7474 if (!NewStep.isUsable()) 7475 return std::make_pair(nullptr, nullptr); 7476 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Mul, Diff.get(), NewStep.get()); 7477 if (!Diff.isUsable()) 7478 return std::make_pair(nullptr, nullptr); 7479 7480 // Parentheses (for dumping/debugging purposes only). 7481 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 7482 if (!Diff.isUsable()) 7483 return std::make_pair(nullptr, nullptr); 7484 7485 // Convert to the ptrdiff_t, if original type is pointer. 7486 if (VarType->isAnyPointerType() && 7487 !SemaRef.Context.hasSameType( 7488 Diff.get()->getType(), 7489 SemaRef.Context.getUnsignedPointerDiffType())) { 7490 Diff = SemaRef.PerformImplicitConversion( 7491 Diff.get(), SemaRef.Context.getUnsignedPointerDiffType(), 7492 Sema::AA_Converting, /*AllowExplicit=*/true); 7493 } 7494 if (!Diff.isUsable()) 7495 return std::make_pair(nullptr, nullptr); 7496 7497 if (TestIsLessOp.getValue()) { 7498 // MinExpr = Lower; 7499 // MaxExpr = Lower + (((Upper - Lower [- 1]) / Step) * Step) 7500 Diff = SemaRef.BuildBinOp( 7501 S, DefaultLoc, BO_Add, 7502 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Lower).get(), 7503 Diff.get()); 7504 if (!Diff.isUsable()) 7505 return std::make_pair(nullptr, nullptr); 7506 } else { 7507 // MaxExpr = Upper; 7508 // MinExpr = Upper - (((Upper - Lower [- 1]) / Step) * Step) 7509 Diff = SemaRef.BuildBinOp( 7510 S, DefaultLoc, BO_Sub, 7511 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Upper).get(), 7512 Diff.get()); 7513 if (!Diff.isUsable()) 7514 return std::make_pair(nullptr, nullptr); 7515 } 7516 7517 // Convert to the original type. 7518 if (SemaRef.Context.hasSameType(Diff.get()->getType(), VarType)) 7519 Diff = SemaRef.PerformImplicitConversion(Diff.get(), VarType, 7520 Sema::AA_Converting, 7521 /*AllowExplicit=*/true); 7522 if (!Diff.isUsable()) 7523 return std::make_pair(nullptr, nullptr); 7524 7525 Diff = SemaRef.ActOnFinishFullExpr(Diff.get(), /*DiscardedValue=*/false); 7526 if (!Diff.isUsable()) 7527 return std::make_pair(nullptr, nullptr); 7528 7529 if (TestIsLessOp.getValue()) 7530 MaxExpr = Diff.get(); 7531 else 7532 MinExpr = Diff.get(); 7533 7534 return std::make_pair(MinExpr, MaxExpr); 7535 } 7536 7537 Expr *OpenMPIterationSpaceChecker::buildFinalCondition(Scope *S) const { 7538 if (InitDependOnLC || CondDependOnLC) 7539 return Condition; 7540 return nullptr; 7541 } 7542 7543 Expr *OpenMPIterationSpaceChecker::buildPreCond( 7544 Scope *S, Expr *Cond, 7545 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 7546 // Do not build a precondition when the condition/initialization is dependent 7547 // to prevent pessimistic early loop exit. 7548 // TODO: this can be improved by calculating min/max values but not sure that 7549 // it will be very effective. 7550 if (CondDependOnLC || InitDependOnLC) 7551 return SemaRef.PerformImplicitConversion( 7552 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(), 7553 SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting, 7554 /*AllowExplicit=*/true).get(); 7555 7556 // Try to build LB <op> UB, where <op> is <, >, <=, or >=. 7557 Sema::TentativeAnalysisScope Trap(SemaRef); 7558 7559 ExprResult NewLB = tryBuildCapture(SemaRef, LB, Captures); 7560 ExprResult NewUB = tryBuildCapture(SemaRef, UB, Captures); 7561 if (!NewLB.isUsable() || !NewUB.isUsable()) 7562 return nullptr; 7563 7564 ExprResult CondExpr = 7565 SemaRef.BuildBinOp(S, DefaultLoc, 7566 TestIsLessOp.getValue() ? 7567 (TestIsStrictOp ? BO_LT : BO_LE) : 7568 (TestIsStrictOp ? BO_GT : BO_GE), 7569 NewLB.get(), NewUB.get()); 7570 if (CondExpr.isUsable()) { 7571 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(), 7572 SemaRef.Context.BoolTy)) 7573 CondExpr = SemaRef.PerformImplicitConversion( 7574 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting, 7575 /*AllowExplicit=*/true); 7576 } 7577 7578 // Otherwise use original loop condition and evaluate it in runtime. 7579 return CondExpr.isUsable() ? CondExpr.get() : Cond; 7580 } 7581 7582 /// Build reference expression to the counter be used for codegen. 7583 DeclRefExpr *OpenMPIterationSpaceChecker::buildCounterVar( 7584 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 7585 DSAStackTy &DSA) const { 7586 auto *VD = dyn_cast<VarDecl>(LCDecl); 7587 if (!VD) { 7588 VD = SemaRef.isOpenMPCapturedDecl(LCDecl); 7589 DeclRefExpr *Ref = buildDeclRefExpr( 7590 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc); 7591 const DSAStackTy::DSAVarData Data = 7592 DSA.getTopDSA(LCDecl, /*FromParent=*/false); 7593 // If the loop control decl is explicitly marked as private, do not mark it 7594 // as captured again. 7595 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr) 7596 Captures.insert(std::make_pair(LCRef, Ref)); 7597 return Ref; 7598 } 7599 return cast<DeclRefExpr>(LCRef); 7600 } 7601 7602 Expr *OpenMPIterationSpaceChecker::buildPrivateCounterVar() const { 7603 if (LCDecl && !LCDecl->isInvalidDecl()) { 7604 QualType Type = LCDecl->getType().getNonReferenceType(); 7605 VarDecl *PrivateVar = buildVarDecl( 7606 SemaRef, DefaultLoc, Type, LCDecl->getName(), 7607 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr, 7608 isa<VarDecl>(LCDecl) 7609 ? buildDeclRefExpr(SemaRef, cast<VarDecl>(LCDecl), Type, DefaultLoc) 7610 : nullptr); 7611 if (PrivateVar->isInvalidDecl()) 7612 return nullptr; 7613 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc); 7614 } 7615 return nullptr; 7616 } 7617 7618 /// Build initialization of the counter to be used for codegen. 7619 Expr *OpenMPIterationSpaceChecker::buildCounterInit() const { return LB; } 7620 7621 /// Build step of the counter be used for codegen. 7622 Expr *OpenMPIterationSpaceChecker::buildCounterStep() const { return Step; } 7623 7624 Expr *OpenMPIterationSpaceChecker::buildOrderedLoopData( 7625 Scope *S, Expr *Counter, 7626 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, SourceLocation Loc, 7627 Expr *Inc, OverloadedOperatorKind OOK) { 7628 Expr *Cnt = SemaRef.DefaultLvalueConversion(Counter).get(); 7629 if (!Cnt) 7630 return nullptr; 7631 if (Inc) { 7632 assert((OOK == OO_Plus || OOK == OO_Minus) && 7633 "Expected only + or - operations for depend clauses."); 7634 BinaryOperatorKind BOK = (OOK == OO_Plus) ? BO_Add : BO_Sub; 7635 Cnt = SemaRef.BuildBinOp(S, Loc, BOK, Cnt, Inc).get(); 7636 if (!Cnt) 7637 return nullptr; 7638 } 7639 QualType VarType = LCDecl->getType().getNonReferenceType(); 7640 if (!VarType->isIntegerType() && !VarType->isPointerType() && 7641 !SemaRef.getLangOpts().CPlusPlus) 7642 return nullptr; 7643 // Upper - Lower 7644 Expr *Upper = TestIsLessOp.getValue() 7645 ? Cnt 7646 : tryBuildCapture(SemaRef, LB, Captures).get(); 7647 Expr *Lower = TestIsLessOp.getValue() 7648 ? tryBuildCapture(SemaRef, LB, Captures).get() 7649 : Cnt; 7650 if (!Upper || !Lower) 7651 return nullptr; 7652 7653 ExprResult Diff = calculateNumIters(SemaRef, S, DefaultLoc, Lower, Upper, 7654 Step, VarType, /*TestIsStrictOp=*/false, 7655 /*RoundToStep=*/false, Captures); 7656 if (!Diff.isUsable()) 7657 return nullptr; 7658 7659 return Diff.get(); 7660 } 7661 } // namespace 7662 7663 void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) { 7664 assert(getLangOpts().OpenMP && "OpenMP is not active."); 7665 assert(Init && "Expected loop in canonical form."); 7666 unsigned AssociatedLoops = DSAStack->getAssociatedLoops(); 7667 if (AssociatedLoops > 0 && 7668 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { 7669 DSAStack->loopStart(); 7670 OpenMPIterationSpaceChecker ISC(*this, *DSAStack, ForLoc); 7671 if (!ISC.checkAndSetInit(Init, /*EmitDiags=*/false)) { 7672 if (ValueDecl *D = ISC.getLoopDecl()) { 7673 auto *VD = dyn_cast<VarDecl>(D); 7674 DeclRefExpr *PrivateRef = nullptr; 7675 if (!VD) { 7676 if (VarDecl *Private = isOpenMPCapturedDecl(D)) { 7677 VD = Private; 7678 } else { 7679 PrivateRef = buildCapture(*this, D, ISC.getLoopDeclRefExpr(), 7680 /*WithInit=*/false); 7681 VD = cast<VarDecl>(PrivateRef->getDecl()); 7682 } 7683 } 7684 DSAStack->addLoopControlVariable(D, VD); 7685 const Decl *LD = DSAStack->getPossiblyLoopCunter(); 7686 if (LD != D->getCanonicalDecl()) { 7687 DSAStack->resetPossibleLoopCounter(); 7688 if (auto *Var = dyn_cast_or_null<VarDecl>(LD)) 7689 MarkDeclarationsReferencedInExpr( 7690 buildDeclRefExpr(*this, const_cast<VarDecl *>(Var), 7691 Var->getType().getNonLValueExprType(Context), 7692 ForLoc, /*RefersToCapture=*/true)); 7693 } 7694 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 7695 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables 7696 // Referenced in a Construct, C/C++]. The loop iteration variable in the 7697 // associated for-loop of a simd construct with just one associated 7698 // for-loop may be listed in a linear clause with a constant-linear-step 7699 // that is the increment of the associated for-loop. The loop iteration 7700 // variable(s) in the associated for-loop(s) of a for or parallel for 7701 // construct may be listed in a private or lastprivate clause. 7702 DSAStackTy::DSAVarData DVar = 7703 DSAStack->getTopDSA(D, /*FromParent=*/false); 7704 // If LoopVarRefExpr is nullptr it means the corresponding loop variable 7705 // is declared in the loop and it is predetermined as a private. 7706 Expr *LoopDeclRefExpr = ISC.getLoopDeclRefExpr(); 7707 OpenMPClauseKind PredeterminedCKind = 7708 isOpenMPSimdDirective(DKind) 7709 ? (DSAStack->hasMutipleLoops() ? OMPC_lastprivate : OMPC_linear) 7710 : OMPC_private; 7711 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && 7712 DVar.CKind != PredeterminedCKind && DVar.RefExpr && 7713 (LangOpts.OpenMP <= 45 || (DVar.CKind != OMPC_lastprivate && 7714 DVar.CKind != OMPC_private))) || 7715 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop || 7716 DKind == OMPD_master_taskloop || 7717 DKind == OMPD_parallel_master_taskloop || 7718 isOpenMPDistributeDirective(DKind)) && 7719 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && 7720 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) && 7721 (DVar.CKind != OMPC_private || DVar.RefExpr)) { 7722 Diag(Init->getBeginLoc(), diag::err_omp_loop_var_dsa) 7723 << getOpenMPClauseName(DVar.CKind) 7724 << getOpenMPDirectiveName(DKind) 7725 << getOpenMPClauseName(PredeterminedCKind); 7726 if (DVar.RefExpr == nullptr) 7727 DVar.CKind = PredeterminedCKind; 7728 reportOriginalDsa(*this, DSAStack, D, DVar, 7729 /*IsLoopIterVar=*/true); 7730 } else if (LoopDeclRefExpr) { 7731 // Make the loop iteration variable private (for worksharing 7732 // constructs), linear (for simd directives with the only one 7733 // associated loop) or lastprivate (for simd directives with several 7734 // collapsed or ordered loops). 7735 if (DVar.CKind == OMPC_unknown) 7736 DSAStack->addDSA(D, LoopDeclRefExpr, PredeterminedCKind, 7737 PrivateRef); 7738 } 7739 } 7740 } 7741 DSAStack->setAssociatedLoops(AssociatedLoops - 1); 7742 } 7743 } 7744 7745 /// Called on a for stmt to check and extract its iteration space 7746 /// for further processing (such as collapsing). 7747 static bool checkOpenMPIterationSpace( 7748 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA, 7749 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount, 7750 unsigned TotalNestedLoopCount, Expr *CollapseLoopCountExpr, 7751 Expr *OrderedLoopCountExpr, 7752 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA, 7753 llvm::MutableArrayRef<LoopIterationSpace> ResultIterSpaces, 7754 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 7755 // OpenMP [2.9.1, Canonical Loop Form] 7756 // for (init-expr; test-expr; incr-expr) structured-block 7757 // for (range-decl: range-expr) structured-block 7758 auto *For = dyn_cast_or_null<ForStmt>(S); 7759 auto *CXXFor = dyn_cast_or_null<CXXForRangeStmt>(S); 7760 // Ranged for is supported only in OpenMP 5.0. 7761 if (!For && (SemaRef.LangOpts.OpenMP <= 45 || !CXXFor)) { 7762 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_not_for) 7763 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr) 7764 << getOpenMPDirectiveName(DKind) << TotalNestedLoopCount 7765 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount; 7766 if (TotalNestedLoopCount > 1) { 7767 if (CollapseLoopCountExpr && OrderedLoopCountExpr) 7768 SemaRef.Diag(DSA.getConstructLoc(), 7769 diag::note_omp_collapse_ordered_expr) 7770 << 2 << CollapseLoopCountExpr->getSourceRange() 7771 << OrderedLoopCountExpr->getSourceRange(); 7772 else if (CollapseLoopCountExpr) 7773 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), 7774 diag::note_omp_collapse_ordered_expr) 7775 << 0 << CollapseLoopCountExpr->getSourceRange(); 7776 else 7777 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), 7778 diag::note_omp_collapse_ordered_expr) 7779 << 1 << OrderedLoopCountExpr->getSourceRange(); 7780 } 7781 return true; 7782 } 7783 assert(((For && For->getBody()) || (CXXFor && CXXFor->getBody())) && 7784 "No loop body."); 7785 7786 OpenMPIterationSpaceChecker ISC(SemaRef, DSA, 7787 For ? For->getForLoc() : CXXFor->getForLoc()); 7788 7789 // Check init. 7790 Stmt *Init = For ? For->getInit() : CXXFor->getBeginStmt(); 7791 if (ISC.checkAndSetInit(Init)) 7792 return true; 7793 7794 bool HasErrors = false; 7795 7796 // Check loop variable's type. 7797 if (ValueDecl *LCDecl = ISC.getLoopDecl()) { 7798 // OpenMP [2.6, Canonical Loop Form] 7799 // Var is one of the following: 7800 // A variable of signed or unsigned integer type. 7801 // For C++, a variable of a random access iterator type. 7802 // For C, a variable of a pointer type. 7803 QualType VarType = LCDecl->getType().getNonReferenceType(); 7804 if (!VarType->isDependentType() && !VarType->isIntegerType() && 7805 !VarType->isPointerType() && 7806 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) { 7807 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_variable_type) 7808 << SemaRef.getLangOpts().CPlusPlus; 7809 HasErrors = true; 7810 } 7811 7812 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in 7813 // a Construct 7814 // The loop iteration variable(s) in the associated for-loop(s) of a for or 7815 // parallel for construct is (are) private. 7816 // The loop iteration variable in the associated for-loop of a simd 7817 // construct with just one associated for-loop is linear with a 7818 // constant-linear-step that is the increment of the associated for-loop. 7819 // Exclude loop var from the list of variables with implicitly defined data 7820 // sharing attributes. 7821 VarsWithImplicitDSA.erase(LCDecl); 7822 7823 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars"); 7824 7825 // Check test-expr. 7826 HasErrors |= ISC.checkAndSetCond(For ? For->getCond() : CXXFor->getCond()); 7827 7828 // Check incr-expr. 7829 HasErrors |= ISC.checkAndSetInc(For ? For->getInc() : CXXFor->getInc()); 7830 } 7831 7832 if (ISC.dependent() || SemaRef.CurContext->isDependentContext() || HasErrors) 7833 return HasErrors; 7834 7835 // Build the loop's iteration space representation. 7836 ResultIterSpaces[CurrentNestedLoopCount].PreCond = ISC.buildPreCond( 7837 DSA.getCurScope(), For ? For->getCond() : CXXFor->getCond(), Captures); 7838 ResultIterSpaces[CurrentNestedLoopCount].NumIterations = 7839 ISC.buildNumIterations(DSA.getCurScope(), ResultIterSpaces, 7840 (isOpenMPWorksharingDirective(DKind) || 7841 isOpenMPTaskLoopDirective(DKind) || 7842 isOpenMPDistributeDirective(DKind)), 7843 Captures); 7844 ResultIterSpaces[CurrentNestedLoopCount].CounterVar = 7845 ISC.buildCounterVar(Captures, DSA); 7846 ResultIterSpaces[CurrentNestedLoopCount].PrivateCounterVar = 7847 ISC.buildPrivateCounterVar(); 7848 ResultIterSpaces[CurrentNestedLoopCount].CounterInit = ISC.buildCounterInit(); 7849 ResultIterSpaces[CurrentNestedLoopCount].CounterStep = ISC.buildCounterStep(); 7850 ResultIterSpaces[CurrentNestedLoopCount].InitSrcRange = ISC.getInitSrcRange(); 7851 ResultIterSpaces[CurrentNestedLoopCount].CondSrcRange = 7852 ISC.getConditionSrcRange(); 7853 ResultIterSpaces[CurrentNestedLoopCount].IncSrcRange = 7854 ISC.getIncrementSrcRange(); 7855 ResultIterSpaces[CurrentNestedLoopCount].Subtract = ISC.shouldSubtractStep(); 7856 ResultIterSpaces[CurrentNestedLoopCount].IsStrictCompare = 7857 ISC.isStrictTestOp(); 7858 std::tie(ResultIterSpaces[CurrentNestedLoopCount].MinValue, 7859 ResultIterSpaces[CurrentNestedLoopCount].MaxValue) = 7860 ISC.buildMinMaxValues(DSA.getCurScope(), Captures); 7861 ResultIterSpaces[CurrentNestedLoopCount].FinalCondition = 7862 ISC.buildFinalCondition(DSA.getCurScope()); 7863 ResultIterSpaces[CurrentNestedLoopCount].IsNonRectangularLB = 7864 ISC.doesInitDependOnLC(); 7865 ResultIterSpaces[CurrentNestedLoopCount].IsNonRectangularUB = 7866 ISC.doesCondDependOnLC(); 7867 ResultIterSpaces[CurrentNestedLoopCount].LoopDependentIdx = 7868 ISC.getLoopDependentIdx(); 7869 7870 HasErrors |= 7871 (ResultIterSpaces[CurrentNestedLoopCount].PreCond == nullptr || 7872 ResultIterSpaces[CurrentNestedLoopCount].NumIterations == nullptr || 7873 ResultIterSpaces[CurrentNestedLoopCount].CounterVar == nullptr || 7874 ResultIterSpaces[CurrentNestedLoopCount].PrivateCounterVar == nullptr || 7875 ResultIterSpaces[CurrentNestedLoopCount].CounterInit == nullptr || 7876 ResultIterSpaces[CurrentNestedLoopCount].CounterStep == nullptr); 7877 if (!HasErrors && DSA.isOrderedRegion()) { 7878 if (DSA.getOrderedRegionParam().second->getNumForLoops()) { 7879 if (CurrentNestedLoopCount < 7880 DSA.getOrderedRegionParam().second->getLoopNumIterations().size()) { 7881 DSA.getOrderedRegionParam().second->setLoopNumIterations( 7882 CurrentNestedLoopCount, 7883 ResultIterSpaces[CurrentNestedLoopCount].NumIterations); 7884 DSA.getOrderedRegionParam().second->setLoopCounter( 7885 CurrentNestedLoopCount, 7886 ResultIterSpaces[CurrentNestedLoopCount].CounterVar); 7887 } 7888 } 7889 for (auto &Pair : DSA.getDoacrossDependClauses()) { 7890 if (CurrentNestedLoopCount >= Pair.first->getNumLoops()) { 7891 // Erroneous case - clause has some problems. 7892 continue; 7893 } 7894 if (Pair.first->getDependencyKind() == OMPC_DEPEND_sink && 7895 Pair.second.size() <= CurrentNestedLoopCount) { 7896 // Erroneous case - clause has some problems. 7897 Pair.first->setLoopData(CurrentNestedLoopCount, nullptr); 7898 continue; 7899 } 7900 Expr *CntValue; 7901 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source) 7902 CntValue = ISC.buildOrderedLoopData( 7903 DSA.getCurScope(), 7904 ResultIterSpaces[CurrentNestedLoopCount].CounterVar, Captures, 7905 Pair.first->getDependencyLoc()); 7906 else 7907 CntValue = ISC.buildOrderedLoopData( 7908 DSA.getCurScope(), 7909 ResultIterSpaces[CurrentNestedLoopCount].CounterVar, Captures, 7910 Pair.first->getDependencyLoc(), 7911 Pair.second[CurrentNestedLoopCount].first, 7912 Pair.second[CurrentNestedLoopCount].second); 7913 Pair.first->setLoopData(CurrentNestedLoopCount, CntValue); 7914 } 7915 } 7916 7917 return HasErrors; 7918 } 7919 7920 /// Build 'VarRef = Start. 7921 static ExprResult 7922 buildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef, 7923 ExprResult Start, bool IsNonRectangularLB, 7924 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 7925 // Build 'VarRef = Start. 7926 ExprResult NewStart = IsNonRectangularLB 7927 ? Start.get() 7928 : tryBuildCapture(SemaRef, Start.get(), Captures); 7929 if (!NewStart.isUsable()) 7930 return ExprError(); 7931 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(), 7932 VarRef.get()->getType())) { 7933 NewStart = SemaRef.PerformImplicitConversion( 7934 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting, 7935 /*AllowExplicit=*/true); 7936 if (!NewStart.isUsable()) 7937 return ExprError(); 7938 } 7939 7940 ExprResult Init = 7941 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); 7942 return Init; 7943 } 7944 7945 /// Build 'VarRef = Start + Iter * Step'. 7946 static ExprResult buildCounterUpdate( 7947 Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef, 7948 ExprResult Start, ExprResult Iter, ExprResult Step, bool Subtract, 7949 bool IsNonRectangularLB, 7950 llvm::MapVector<const Expr *, DeclRefExpr *> *Captures = nullptr) { 7951 // Add parentheses (for debugging purposes only). 7952 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get()); 7953 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() || 7954 !Step.isUsable()) 7955 return ExprError(); 7956 7957 ExprResult NewStep = Step; 7958 if (Captures) 7959 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures); 7960 if (NewStep.isInvalid()) 7961 return ExprError(); 7962 ExprResult Update = 7963 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get()); 7964 if (!Update.isUsable()) 7965 return ExprError(); 7966 7967 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or 7968 // 'VarRef = Start (+|-) Iter * Step'. 7969 if (!Start.isUsable()) 7970 return ExprError(); 7971 ExprResult NewStart = SemaRef.ActOnParenExpr(Loc, Loc, Start.get()); 7972 if (!NewStart.isUsable()) 7973 return ExprError(); 7974 if (Captures && !IsNonRectangularLB) 7975 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures); 7976 if (NewStart.isInvalid()) 7977 return ExprError(); 7978 7979 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'. 7980 ExprResult SavedUpdate = Update; 7981 ExprResult UpdateVal; 7982 if (VarRef.get()->getType()->isOverloadableType() || 7983 NewStart.get()->getType()->isOverloadableType() || 7984 Update.get()->getType()->isOverloadableType()) { 7985 Sema::TentativeAnalysisScope Trap(SemaRef); 7986 7987 Update = 7988 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); 7989 if (Update.isUsable()) { 7990 UpdateVal = 7991 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign, 7992 VarRef.get(), SavedUpdate.get()); 7993 if (UpdateVal.isUsable()) { 7994 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(), 7995 UpdateVal.get()); 7996 } 7997 } 7998 } 7999 8000 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'. 8001 if (!Update.isUsable() || !UpdateVal.isUsable()) { 8002 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add, 8003 NewStart.get(), SavedUpdate.get()); 8004 if (!Update.isUsable()) 8005 return ExprError(); 8006 8007 if (!SemaRef.Context.hasSameType(Update.get()->getType(), 8008 VarRef.get()->getType())) { 8009 Update = SemaRef.PerformImplicitConversion( 8010 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true); 8011 if (!Update.isUsable()) 8012 return ExprError(); 8013 } 8014 8015 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get()); 8016 } 8017 return Update; 8018 } 8019 8020 /// Convert integer expression \a E to make it have at least \a Bits 8021 /// bits. 8022 static ExprResult widenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) { 8023 if (E == nullptr) 8024 return ExprError(); 8025 ASTContext &C = SemaRef.Context; 8026 QualType OldType = E->getType(); 8027 unsigned HasBits = C.getTypeSize(OldType); 8028 if (HasBits >= Bits) 8029 return ExprResult(E); 8030 // OK to convert to signed, because new type has more bits than old. 8031 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true); 8032 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting, 8033 true); 8034 } 8035 8036 /// Check if the given expression \a E is a constant integer that fits 8037 /// into \a Bits bits. 8038 static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) { 8039 if (E == nullptr) 8040 return false; 8041 if (Optional<llvm::APSInt> Result = 8042 E->getIntegerConstantExpr(SemaRef.Context)) 8043 return Signed ? Result->isSignedIntN(Bits) : Result->isIntN(Bits); 8044 return false; 8045 } 8046 8047 /// Build preinits statement for the given declarations. 8048 static Stmt *buildPreInits(ASTContext &Context, 8049 MutableArrayRef<Decl *> PreInits) { 8050 if (!PreInits.empty()) { 8051 return new (Context) DeclStmt( 8052 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()), 8053 SourceLocation(), SourceLocation()); 8054 } 8055 return nullptr; 8056 } 8057 8058 /// Build preinits statement for the given declarations. 8059 static Stmt * 8060 buildPreInits(ASTContext &Context, 8061 const llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 8062 if (!Captures.empty()) { 8063 SmallVector<Decl *, 16> PreInits; 8064 for (const auto &Pair : Captures) 8065 PreInits.push_back(Pair.second->getDecl()); 8066 return buildPreInits(Context, PreInits); 8067 } 8068 return nullptr; 8069 } 8070 8071 /// Build postupdate expression for the given list of postupdates expressions. 8072 static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) { 8073 Expr *PostUpdate = nullptr; 8074 if (!PostUpdates.empty()) { 8075 for (Expr *E : PostUpdates) { 8076 Expr *ConvE = S.BuildCStyleCastExpr( 8077 E->getExprLoc(), 8078 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy), 8079 E->getExprLoc(), E) 8080 .get(); 8081 PostUpdate = PostUpdate 8082 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma, 8083 PostUpdate, ConvE) 8084 .get() 8085 : ConvE; 8086 } 8087 } 8088 return PostUpdate; 8089 } 8090 8091 /// Called on a for stmt to check itself and nested loops (if any). 8092 /// \return Returns 0 if one of the collapsed stmts is not canonical for loop, 8093 /// number of collapsed loops otherwise. 8094 static unsigned 8095 checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr, 8096 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef, 8097 DSAStackTy &DSA, 8098 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA, 8099 OMPLoopDirective::HelperExprs &Built) { 8100 unsigned NestedLoopCount = 1; 8101 if (CollapseLoopCountExpr) { 8102 // Found 'collapse' clause - calculate collapse number. 8103 Expr::EvalResult Result; 8104 if (!CollapseLoopCountExpr->isValueDependent() && 8105 CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) { 8106 NestedLoopCount = Result.Val.getInt().getLimitedValue(); 8107 } else { 8108 Built.clear(/*Size=*/1); 8109 return 1; 8110 } 8111 } 8112 unsigned OrderedLoopCount = 1; 8113 if (OrderedLoopCountExpr) { 8114 // Found 'ordered' clause - calculate collapse number. 8115 Expr::EvalResult EVResult; 8116 if (!OrderedLoopCountExpr->isValueDependent() && 8117 OrderedLoopCountExpr->EvaluateAsInt(EVResult, 8118 SemaRef.getASTContext())) { 8119 llvm::APSInt Result = EVResult.Val.getInt(); 8120 if (Result.getLimitedValue() < NestedLoopCount) { 8121 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), 8122 diag::err_omp_wrong_ordered_loop_count) 8123 << OrderedLoopCountExpr->getSourceRange(); 8124 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), 8125 diag::note_collapse_loop_count) 8126 << CollapseLoopCountExpr->getSourceRange(); 8127 } 8128 OrderedLoopCount = Result.getLimitedValue(); 8129 } else { 8130 Built.clear(/*Size=*/1); 8131 return 1; 8132 } 8133 } 8134 // This is helper routine for loop directives (e.g., 'for', 'simd', 8135 // 'for simd', etc.). 8136 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 8137 SmallVector<LoopIterationSpace, 4> IterSpaces( 8138 std::max(OrderedLoopCount, NestedLoopCount)); 8139 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true); 8140 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) { 8141 if (checkOpenMPIterationSpace( 8142 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount, 8143 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr, 8144 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces, Captures)) 8145 return 0; 8146 // Move on to the next nested for loop, or to the loop body. 8147 // OpenMP [2.8.1, simd construct, Restrictions] 8148 // All loops associated with the construct must be perfectly nested; that 8149 // is, there must be no intervening code nor any OpenMP directive between 8150 // any two loops. 8151 if (auto *For = dyn_cast<ForStmt>(CurStmt)) { 8152 CurStmt = For->getBody(); 8153 } else { 8154 assert(isa<CXXForRangeStmt>(CurStmt) && 8155 "Expected canonical for or range-based for loops."); 8156 CurStmt = cast<CXXForRangeStmt>(CurStmt)->getBody(); 8157 } 8158 CurStmt = OMPLoopDirective::tryToFindNextInnerLoop( 8159 CurStmt, SemaRef.LangOpts.OpenMP >= 50); 8160 } 8161 for (unsigned Cnt = NestedLoopCount; Cnt < OrderedLoopCount; ++Cnt) { 8162 if (checkOpenMPIterationSpace( 8163 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount, 8164 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr, 8165 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces, Captures)) 8166 return 0; 8167 if (Cnt > 0 && IterSpaces[Cnt].CounterVar) { 8168 // Handle initialization of captured loop iterator variables. 8169 auto *DRE = cast<DeclRefExpr>(IterSpaces[Cnt].CounterVar); 8170 if (isa<OMPCapturedExprDecl>(DRE->getDecl())) { 8171 Captures[DRE] = DRE; 8172 } 8173 } 8174 // Move on to the next nested for loop, or to the loop body. 8175 // OpenMP [2.8.1, simd construct, Restrictions] 8176 // All loops associated with the construct must be perfectly nested; that 8177 // is, there must be no intervening code nor any OpenMP directive between 8178 // any two loops. 8179 if (auto *For = dyn_cast<ForStmt>(CurStmt)) { 8180 CurStmt = For->getBody(); 8181 } else { 8182 assert(isa<CXXForRangeStmt>(CurStmt) && 8183 "Expected canonical for or range-based for loops."); 8184 CurStmt = cast<CXXForRangeStmt>(CurStmt)->getBody(); 8185 } 8186 CurStmt = OMPLoopDirective::tryToFindNextInnerLoop( 8187 CurStmt, SemaRef.LangOpts.OpenMP >= 50); 8188 } 8189 8190 Built.clear(/* size */ NestedLoopCount); 8191 8192 if (SemaRef.CurContext->isDependentContext()) 8193 return NestedLoopCount; 8194 8195 // An example of what is generated for the following code: 8196 // 8197 // #pragma omp simd collapse(2) ordered(2) 8198 // for (i = 0; i < NI; ++i) 8199 // for (k = 0; k < NK; ++k) 8200 // for (j = J0; j < NJ; j+=2) { 8201 // <loop body> 8202 // } 8203 // 8204 // We generate the code below. 8205 // Note: the loop body may be outlined in CodeGen. 8206 // Note: some counters may be C++ classes, operator- is used to find number of 8207 // iterations and operator+= to calculate counter value. 8208 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32 8209 // or i64 is currently supported). 8210 // 8211 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2)) 8212 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) { 8213 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2); 8214 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2; 8215 // // similar updates for vars in clauses (e.g. 'linear') 8216 // <loop body (using local i and j)> 8217 // } 8218 // i = NI; // assign final values of counters 8219 // j = NJ; 8220 // 8221 8222 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are 8223 // the iteration counts of the collapsed for loops. 8224 // Precondition tests if there is at least one iteration (all conditions are 8225 // true). 8226 auto PreCond = ExprResult(IterSpaces[0].PreCond); 8227 Expr *N0 = IterSpaces[0].NumIterations; 8228 ExprResult LastIteration32 = 8229 widenIterationCount(/*Bits=*/32, 8230 SemaRef 8231 .PerformImplicitConversion( 8232 N0->IgnoreImpCasts(), N0->getType(), 8233 Sema::AA_Converting, /*AllowExplicit=*/true) 8234 .get(), 8235 SemaRef); 8236 ExprResult LastIteration64 = widenIterationCount( 8237 /*Bits=*/64, 8238 SemaRef 8239 .PerformImplicitConversion(N0->IgnoreImpCasts(), N0->getType(), 8240 Sema::AA_Converting, 8241 /*AllowExplicit=*/true) 8242 .get(), 8243 SemaRef); 8244 8245 if (!LastIteration32.isUsable() || !LastIteration64.isUsable()) 8246 return NestedLoopCount; 8247 8248 ASTContext &C = SemaRef.Context; 8249 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32; 8250 8251 Scope *CurScope = DSA.getCurScope(); 8252 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) { 8253 if (PreCond.isUsable()) { 8254 PreCond = 8255 SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd, 8256 PreCond.get(), IterSpaces[Cnt].PreCond); 8257 } 8258 Expr *N = IterSpaces[Cnt].NumIterations; 8259 SourceLocation Loc = N->getExprLoc(); 8260 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32; 8261 if (LastIteration32.isUsable()) 8262 LastIteration32 = SemaRef.BuildBinOp( 8263 CurScope, Loc, BO_Mul, LastIteration32.get(), 8264 SemaRef 8265 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), 8266 Sema::AA_Converting, 8267 /*AllowExplicit=*/true) 8268 .get()); 8269 if (LastIteration64.isUsable()) 8270 LastIteration64 = SemaRef.BuildBinOp( 8271 CurScope, Loc, BO_Mul, LastIteration64.get(), 8272 SemaRef 8273 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), 8274 Sema::AA_Converting, 8275 /*AllowExplicit=*/true) 8276 .get()); 8277 } 8278 8279 // Choose either the 32-bit or 64-bit version. 8280 ExprResult LastIteration = LastIteration64; 8281 if (SemaRef.getLangOpts().OpenMPOptimisticCollapse || 8282 (LastIteration32.isUsable() && 8283 C.getTypeSize(LastIteration32.get()->getType()) == 32 && 8284 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 || 8285 fitsInto( 8286 /*Bits=*/32, 8287 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(), 8288 LastIteration64.get(), SemaRef)))) 8289 LastIteration = LastIteration32; 8290 QualType VType = LastIteration.get()->getType(); 8291 QualType RealVType = VType; 8292 QualType StrideVType = VType; 8293 if (isOpenMPTaskLoopDirective(DKind)) { 8294 VType = 8295 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0); 8296 StrideVType = 8297 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1); 8298 } 8299 8300 if (!LastIteration.isUsable()) 8301 return 0; 8302 8303 // Save the number of iterations. 8304 ExprResult NumIterations = LastIteration; 8305 { 8306 LastIteration = SemaRef.BuildBinOp( 8307 CurScope, LastIteration.get()->getExprLoc(), BO_Sub, 8308 LastIteration.get(), 8309 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 8310 if (!LastIteration.isUsable()) 8311 return 0; 8312 } 8313 8314 // Calculate the last iteration number beforehand instead of doing this on 8315 // each iteration. Do not do this if the number of iterations may be kfold-ed. 8316 bool IsConstant = LastIteration.get()->isIntegerConstantExpr(SemaRef.Context); 8317 ExprResult CalcLastIteration; 8318 if (!IsConstant) { 8319 ExprResult SaveRef = 8320 tryBuildCapture(SemaRef, LastIteration.get(), Captures); 8321 LastIteration = SaveRef; 8322 8323 // Prepare SaveRef + 1. 8324 NumIterations = SemaRef.BuildBinOp( 8325 CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(), 8326 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 8327 if (!NumIterations.isUsable()) 8328 return 0; 8329 } 8330 8331 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin(); 8332 8333 // Build variables passed into runtime, necessary for worksharing directives. 8334 ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB; 8335 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || 8336 isOpenMPDistributeDirective(DKind)) { 8337 // Lower bound variable, initialized with zero. 8338 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb"); 8339 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc); 8340 SemaRef.AddInitializerToDecl(LBDecl, 8341 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 8342 /*DirectInit*/ false); 8343 8344 // Upper bound variable, initialized with last iteration number. 8345 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub"); 8346 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc); 8347 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(), 8348 /*DirectInit*/ false); 8349 8350 // A 32-bit variable-flag where runtime returns 1 for the last iteration. 8351 // This will be used to implement clause 'lastprivate'. 8352 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true); 8353 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last"); 8354 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc); 8355 SemaRef.AddInitializerToDecl(ILDecl, 8356 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 8357 /*DirectInit*/ false); 8358 8359 // Stride variable returned by runtime (we initialize it to 1 by default). 8360 VarDecl *STDecl = 8361 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride"); 8362 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc); 8363 SemaRef.AddInitializerToDecl(STDecl, 8364 SemaRef.ActOnIntegerConstant(InitLoc, 1).get(), 8365 /*DirectInit*/ false); 8366 8367 // Build expression: UB = min(UB, LastIteration) 8368 // It is necessary for CodeGen of directives with static scheduling. 8369 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT, 8370 UB.get(), LastIteration.get()); 8371 ExprResult CondOp = SemaRef.ActOnConditionalOp( 8372 LastIteration.get()->getExprLoc(), InitLoc, IsUBGreater.get(), 8373 LastIteration.get(), UB.get()); 8374 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(), 8375 CondOp.get()); 8376 EUB = SemaRef.ActOnFinishFullExpr(EUB.get(), /*DiscardedValue*/ false); 8377 8378 // If we have a combined directive that combines 'distribute', 'for' or 8379 // 'simd' we need to be able to access the bounds of the schedule of the 8380 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained 8381 // by scheduling 'distribute' have to be passed to the schedule of 'for'. 8382 if (isOpenMPLoopBoundSharingDirective(DKind)) { 8383 // Lower bound variable, initialized with zero. 8384 VarDecl *CombLBDecl = 8385 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb"); 8386 CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc); 8387 SemaRef.AddInitializerToDecl( 8388 CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 8389 /*DirectInit*/ false); 8390 8391 // Upper bound variable, initialized with last iteration number. 8392 VarDecl *CombUBDecl = 8393 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub"); 8394 CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc); 8395 SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(), 8396 /*DirectInit*/ false); 8397 8398 ExprResult CombIsUBGreater = SemaRef.BuildBinOp( 8399 CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get()); 8400 ExprResult CombCondOp = 8401 SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(), 8402 LastIteration.get(), CombUB.get()); 8403 CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(), 8404 CombCondOp.get()); 8405 CombEUB = 8406 SemaRef.ActOnFinishFullExpr(CombEUB.get(), /*DiscardedValue*/ false); 8407 8408 const CapturedDecl *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl(); 8409 // We expect to have at least 2 more parameters than the 'parallel' 8410 // directive does - the lower and upper bounds of the previous schedule. 8411 assert(CD->getNumParams() >= 4 && 8412 "Unexpected number of parameters in loop combined directive"); 8413 8414 // Set the proper type for the bounds given what we learned from the 8415 // enclosed loops. 8416 ImplicitParamDecl *PrevLBDecl = CD->getParam(/*PrevLB=*/2); 8417 ImplicitParamDecl *PrevUBDecl = CD->getParam(/*PrevUB=*/3); 8418 8419 // Previous lower and upper bounds are obtained from the region 8420 // parameters. 8421 PrevLB = 8422 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc); 8423 PrevUB = 8424 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc); 8425 } 8426 } 8427 8428 // Build the iteration variable and its initialization before loop. 8429 ExprResult IV; 8430 ExprResult Init, CombInit; 8431 { 8432 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv"); 8433 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc); 8434 Expr *RHS = 8435 (isOpenMPWorksharingDirective(DKind) || 8436 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)) 8437 ? LB.get() 8438 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get(); 8439 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS); 8440 Init = SemaRef.ActOnFinishFullExpr(Init.get(), /*DiscardedValue*/ false); 8441 8442 if (isOpenMPLoopBoundSharingDirective(DKind)) { 8443 Expr *CombRHS = 8444 (isOpenMPWorksharingDirective(DKind) || 8445 isOpenMPTaskLoopDirective(DKind) || 8446 isOpenMPDistributeDirective(DKind)) 8447 ? CombLB.get() 8448 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get(); 8449 CombInit = 8450 SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS); 8451 CombInit = 8452 SemaRef.ActOnFinishFullExpr(CombInit.get(), /*DiscardedValue*/ false); 8453 } 8454 } 8455 8456 bool UseStrictCompare = 8457 RealVType->hasUnsignedIntegerRepresentation() && 8458 llvm::all_of(IterSpaces, [](const LoopIterationSpace &LIS) { 8459 return LIS.IsStrictCompare; 8460 }); 8461 // Loop condition (IV < NumIterations) or (IV <= UB or IV < UB + 1 (for 8462 // unsigned IV)) for worksharing loops. 8463 SourceLocation CondLoc = AStmt->getBeginLoc(); 8464 Expr *BoundUB = UB.get(); 8465 if (UseStrictCompare) { 8466 BoundUB = 8467 SemaRef 8468 .BuildBinOp(CurScope, CondLoc, BO_Add, BoundUB, 8469 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()) 8470 .get(); 8471 BoundUB = 8472 SemaRef.ActOnFinishFullExpr(BoundUB, /*DiscardedValue*/ false).get(); 8473 } 8474 ExprResult Cond = 8475 (isOpenMPWorksharingDirective(DKind) || 8476 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)) 8477 ? SemaRef.BuildBinOp(CurScope, CondLoc, 8478 UseStrictCompare ? BO_LT : BO_LE, IV.get(), 8479 BoundUB) 8480 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(), 8481 NumIterations.get()); 8482 ExprResult CombDistCond; 8483 if (isOpenMPLoopBoundSharingDirective(DKind)) { 8484 CombDistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(), 8485 NumIterations.get()); 8486 } 8487 8488 ExprResult CombCond; 8489 if (isOpenMPLoopBoundSharingDirective(DKind)) { 8490 Expr *BoundCombUB = CombUB.get(); 8491 if (UseStrictCompare) { 8492 BoundCombUB = 8493 SemaRef 8494 .BuildBinOp( 8495 CurScope, CondLoc, BO_Add, BoundCombUB, 8496 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()) 8497 .get(); 8498 BoundCombUB = 8499 SemaRef.ActOnFinishFullExpr(BoundCombUB, /*DiscardedValue*/ false) 8500 .get(); 8501 } 8502 CombCond = 8503 SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, 8504 IV.get(), BoundCombUB); 8505 } 8506 // Loop increment (IV = IV + 1) 8507 SourceLocation IncLoc = AStmt->getBeginLoc(); 8508 ExprResult Inc = 8509 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(), 8510 SemaRef.ActOnIntegerConstant(IncLoc, 1).get()); 8511 if (!Inc.isUsable()) 8512 return 0; 8513 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get()); 8514 Inc = SemaRef.ActOnFinishFullExpr(Inc.get(), /*DiscardedValue*/ false); 8515 if (!Inc.isUsable()) 8516 return 0; 8517 8518 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST). 8519 // Used for directives with static scheduling. 8520 // In combined construct, add combined version that use CombLB and CombUB 8521 // base variables for the update 8522 ExprResult NextLB, NextUB, CombNextLB, CombNextUB; 8523 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || 8524 isOpenMPDistributeDirective(DKind)) { 8525 // LB + ST 8526 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get()); 8527 if (!NextLB.isUsable()) 8528 return 0; 8529 // LB = LB + ST 8530 NextLB = 8531 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get()); 8532 NextLB = 8533 SemaRef.ActOnFinishFullExpr(NextLB.get(), /*DiscardedValue*/ false); 8534 if (!NextLB.isUsable()) 8535 return 0; 8536 // UB + ST 8537 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get()); 8538 if (!NextUB.isUsable()) 8539 return 0; 8540 // UB = UB + ST 8541 NextUB = 8542 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get()); 8543 NextUB = 8544 SemaRef.ActOnFinishFullExpr(NextUB.get(), /*DiscardedValue*/ false); 8545 if (!NextUB.isUsable()) 8546 return 0; 8547 if (isOpenMPLoopBoundSharingDirective(DKind)) { 8548 CombNextLB = 8549 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get()); 8550 if (!NextLB.isUsable()) 8551 return 0; 8552 // LB = LB + ST 8553 CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(), 8554 CombNextLB.get()); 8555 CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get(), 8556 /*DiscardedValue*/ false); 8557 if (!CombNextLB.isUsable()) 8558 return 0; 8559 // UB + ST 8560 CombNextUB = 8561 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get()); 8562 if (!CombNextUB.isUsable()) 8563 return 0; 8564 // UB = UB + ST 8565 CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(), 8566 CombNextUB.get()); 8567 CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get(), 8568 /*DiscardedValue*/ false); 8569 if (!CombNextUB.isUsable()) 8570 return 0; 8571 } 8572 } 8573 8574 // Create increment expression for distribute loop when combined in a same 8575 // directive with for as IV = IV + ST; ensure upper bound expression based 8576 // on PrevUB instead of NumIterations - used to implement 'for' when found 8577 // in combination with 'distribute', like in 'distribute parallel for' 8578 SourceLocation DistIncLoc = AStmt->getBeginLoc(); 8579 ExprResult DistCond, DistInc, PrevEUB, ParForInDistCond; 8580 if (isOpenMPLoopBoundSharingDirective(DKind)) { 8581 DistCond = SemaRef.BuildBinOp( 8582 CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, IV.get(), BoundUB); 8583 assert(DistCond.isUsable() && "distribute cond expr was not built"); 8584 8585 DistInc = 8586 SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get()); 8587 assert(DistInc.isUsable() && "distribute inc expr was not built"); 8588 DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(), 8589 DistInc.get()); 8590 DistInc = 8591 SemaRef.ActOnFinishFullExpr(DistInc.get(), /*DiscardedValue*/ false); 8592 assert(DistInc.isUsable() && "distribute inc expr was not built"); 8593 8594 // Build expression: UB = min(UB, prevUB) for #for in composite or combined 8595 // construct 8596 SourceLocation DistEUBLoc = AStmt->getBeginLoc(); 8597 ExprResult IsUBGreater = 8598 SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, UB.get(), PrevUB.get()); 8599 ExprResult CondOp = SemaRef.ActOnConditionalOp( 8600 DistEUBLoc, DistEUBLoc, IsUBGreater.get(), PrevUB.get(), UB.get()); 8601 PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(), 8602 CondOp.get()); 8603 PrevEUB = 8604 SemaRef.ActOnFinishFullExpr(PrevEUB.get(), /*DiscardedValue*/ false); 8605 8606 // Build IV <= PrevUB or IV < PrevUB + 1 for unsigned IV to be used in 8607 // parallel for is in combination with a distribute directive with 8608 // schedule(static, 1) 8609 Expr *BoundPrevUB = PrevUB.get(); 8610 if (UseStrictCompare) { 8611 BoundPrevUB = 8612 SemaRef 8613 .BuildBinOp( 8614 CurScope, CondLoc, BO_Add, BoundPrevUB, 8615 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()) 8616 .get(); 8617 BoundPrevUB = 8618 SemaRef.ActOnFinishFullExpr(BoundPrevUB, /*DiscardedValue*/ false) 8619 .get(); 8620 } 8621 ParForInDistCond = 8622 SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, 8623 IV.get(), BoundPrevUB); 8624 } 8625 8626 // Build updates and final values of the loop counters. 8627 bool HasErrors = false; 8628 Built.Counters.resize(NestedLoopCount); 8629 Built.Inits.resize(NestedLoopCount); 8630 Built.Updates.resize(NestedLoopCount); 8631 Built.Finals.resize(NestedLoopCount); 8632 Built.DependentCounters.resize(NestedLoopCount); 8633 Built.DependentInits.resize(NestedLoopCount); 8634 Built.FinalsConditions.resize(NestedLoopCount); 8635 { 8636 // We implement the following algorithm for obtaining the 8637 // original loop iteration variable values based on the 8638 // value of the collapsed loop iteration variable IV. 8639 // 8640 // Let n+1 be the number of collapsed loops in the nest. 8641 // Iteration variables (I0, I1, .... In) 8642 // Iteration counts (N0, N1, ... Nn) 8643 // 8644 // Acc = IV; 8645 // 8646 // To compute Ik for loop k, 0 <= k <= n, generate: 8647 // Prod = N(k+1) * N(k+2) * ... * Nn; 8648 // Ik = Acc / Prod; 8649 // Acc -= Ik * Prod; 8650 // 8651 ExprResult Acc = IV; 8652 for (unsigned int Cnt = 0; Cnt < NestedLoopCount; ++Cnt) { 8653 LoopIterationSpace &IS = IterSpaces[Cnt]; 8654 SourceLocation UpdLoc = IS.IncSrcRange.getBegin(); 8655 ExprResult Iter; 8656 8657 // Compute prod 8658 ExprResult Prod = 8659 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(); 8660 for (unsigned int K = Cnt+1; K < NestedLoopCount; ++K) 8661 Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Prod.get(), 8662 IterSpaces[K].NumIterations); 8663 8664 // Iter = Acc / Prod 8665 // If there is at least one more inner loop to avoid 8666 // multiplication by 1. 8667 if (Cnt + 1 < NestedLoopCount) 8668 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, 8669 Acc.get(), Prod.get()); 8670 else 8671 Iter = Acc; 8672 if (!Iter.isUsable()) { 8673 HasErrors = true; 8674 break; 8675 } 8676 8677 // Update Acc: 8678 // Acc -= Iter * Prod 8679 // Check if there is at least one more inner loop to avoid 8680 // multiplication by 1. 8681 if (Cnt + 1 < NestedLoopCount) 8682 Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, 8683 Iter.get(), Prod.get()); 8684 else 8685 Prod = Iter; 8686 Acc = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Sub, 8687 Acc.get(), Prod.get()); 8688 8689 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step 8690 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()); 8691 DeclRefExpr *CounterVar = buildDeclRefExpr( 8692 SemaRef, VD, IS.CounterVar->getType(), IS.CounterVar->getExprLoc(), 8693 /*RefersToCapture=*/true); 8694 ExprResult Init = 8695 buildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar, 8696 IS.CounterInit, IS.IsNonRectangularLB, Captures); 8697 if (!Init.isUsable()) { 8698 HasErrors = true; 8699 break; 8700 } 8701 ExprResult Update = buildCounterUpdate( 8702 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter, 8703 IS.CounterStep, IS.Subtract, IS.IsNonRectangularLB, &Captures); 8704 if (!Update.isUsable()) { 8705 HasErrors = true; 8706 break; 8707 } 8708 8709 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step 8710 ExprResult Final = 8711 buildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar, 8712 IS.CounterInit, IS.NumIterations, IS.CounterStep, 8713 IS.Subtract, IS.IsNonRectangularLB, &Captures); 8714 if (!Final.isUsable()) { 8715 HasErrors = true; 8716 break; 8717 } 8718 8719 if (!Update.isUsable() || !Final.isUsable()) { 8720 HasErrors = true; 8721 break; 8722 } 8723 // Save results 8724 Built.Counters[Cnt] = IS.CounterVar; 8725 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar; 8726 Built.Inits[Cnt] = Init.get(); 8727 Built.Updates[Cnt] = Update.get(); 8728 Built.Finals[Cnt] = Final.get(); 8729 Built.DependentCounters[Cnt] = nullptr; 8730 Built.DependentInits[Cnt] = nullptr; 8731 Built.FinalsConditions[Cnt] = nullptr; 8732 if (IS.IsNonRectangularLB || IS.IsNonRectangularUB) { 8733 Built.DependentCounters[Cnt] = 8734 Built.Counters[NestedLoopCount - 1 - IS.LoopDependentIdx]; 8735 Built.DependentInits[Cnt] = 8736 Built.Inits[NestedLoopCount - 1 - IS.LoopDependentIdx]; 8737 Built.FinalsConditions[Cnt] = IS.FinalCondition; 8738 } 8739 } 8740 } 8741 8742 if (HasErrors) 8743 return 0; 8744 8745 // Save results 8746 Built.IterationVarRef = IV.get(); 8747 Built.LastIteration = LastIteration.get(); 8748 Built.NumIterations = NumIterations.get(); 8749 Built.CalcLastIteration = SemaRef 8750 .ActOnFinishFullExpr(CalcLastIteration.get(), 8751 /*DiscardedValue=*/false) 8752 .get(); 8753 Built.PreCond = PreCond.get(); 8754 Built.PreInits = buildPreInits(C, Captures); 8755 Built.Cond = Cond.get(); 8756 Built.Init = Init.get(); 8757 Built.Inc = Inc.get(); 8758 Built.LB = LB.get(); 8759 Built.UB = UB.get(); 8760 Built.IL = IL.get(); 8761 Built.ST = ST.get(); 8762 Built.EUB = EUB.get(); 8763 Built.NLB = NextLB.get(); 8764 Built.NUB = NextUB.get(); 8765 Built.PrevLB = PrevLB.get(); 8766 Built.PrevUB = PrevUB.get(); 8767 Built.DistInc = DistInc.get(); 8768 Built.PrevEUB = PrevEUB.get(); 8769 Built.DistCombinedFields.LB = CombLB.get(); 8770 Built.DistCombinedFields.UB = CombUB.get(); 8771 Built.DistCombinedFields.EUB = CombEUB.get(); 8772 Built.DistCombinedFields.Init = CombInit.get(); 8773 Built.DistCombinedFields.Cond = CombCond.get(); 8774 Built.DistCombinedFields.NLB = CombNextLB.get(); 8775 Built.DistCombinedFields.NUB = CombNextUB.get(); 8776 Built.DistCombinedFields.DistCond = CombDistCond.get(); 8777 Built.DistCombinedFields.ParForInDistCond = ParForInDistCond.get(); 8778 8779 return NestedLoopCount; 8780 } 8781 8782 static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) { 8783 auto CollapseClauses = 8784 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses); 8785 if (CollapseClauses.begin() != CollapseClauses.end()) 8786 return (*CollapseClauses.begin())->getNumForLoops(); 8787 return nullptr; 8788 } 8789 8790 static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) { 8791 auto OrderedClauses = 8792 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses); 8793 if (OrderedClauses.begin() != OrderedClauses.end()) 8794 return (*OrderedClauses.begin())->getNumForLoops(); 8795 return nullptr; 8796 } 8797 8798 static bool checkSimdlenSafelenSpecified(Sema &S, 8799 const ArrayRef<OMPClause *> Clauses) { 8800 const OMPSafelenClause *Safelen = nullptr; 8801 const OMPSimdlenClause *Simdlen = nullptr; 8802 8803 for (const OMPClause *Clause : Clauses) { 8804 if (Clause->getClauseKind() == OMPC_safelen) 8805 Safelen = cast<OMPSafelenClause>(Clause); 8806 else if (Clause->getClauseKind() == OMPC_simdlen) 8807 Simdlen = cast<OMPSimdlenClause>(Clause); 8808 if (Safelen && Simdlen) 8809 break; 8810 } 8811 8812 if (Simdlen && Safelen) { 8813 const Expr *SimdlenLength = Simdlen->getSimdlen(); 8814 const Expr *SafelenLength = Safelen->getSafelen(); 8815 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() || 8816 SimdlenLength->isInstantiationDependent() || 8817 SimdlenLength->containsUnexpandedParameterPack()) 8818 return false; 8819 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() || 8820 SafelenLength->isInstantiationDependent() || 8821 SafelenLength->containsUnexpandedParameterPack()) 8822 return false; 8823 Expr::EvalResult SimdlenResult, SafelenResult; 8824 SimdlenLength->EvaluateAsInt(SimdlenResult, S.Context); 8825 SafelenLength->EvaluateAsInt(SafelenResult, S.Context); 8826 llvm::APSInt SimdlenRes = SimdlenResult.Val.getInt(); 8827 llvm::APSInt SafelenRes = SafelenResult.Val.getInt(); 8828 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions] 8829 // If both simdlen and safelen clauses are specified, the value of the 8830 // simdlen parameter must be less than or equal to the value of the safelen 8831 // parameter. 8832 if (SimdlenRes > SafelenRes) { 8833 S.Diag(SimdlenLength->getExprLoc(), 8834 diag::err_omp_wrong_simdlen_safelen_values) 8835 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange(); 8836 return true; 8837 } 8838 } 8839 return false; 8840 } 8841 8842 StmtResult 8843 Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt, 8844 SourceLocation StartLoc, SourceLocation EndLoc, 8845 VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8846 if (!AStmt) 8847 return StmtError(); 8848 8849 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 8850 OMPLoopDirective::HelperExprs B; 8851 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 8852 // define the nested loops number. 8853 unsigned NestedLoopCount = checkOpenMPLoop( 8854 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), 8855 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 8856 if (NestedLoopCount == 0) 8857 return StmtError(); 8858 8859 assert((CurContext->isDependentContext() || B.builtAll()) && 8860 "omp simd loop exprs were not built"); 8861 8862 if (!CurContext->isDependentContext()) { 8863 // Finalize the clauses that need pre-built expressions for CodeGen. 8864 for (OMPClause *C : Clauses) { 8865 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 8866 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 8867 B.NumIterations, *this, CurScope, 8868 DSAStack)) 8869 return StmtError(); 8870 } 8871 } 8872 8873 if (checkSimdlenSafelenSpecified(*this, Clauses)) 8874 return StmtError(); 8875 8876 setFunctionHasBranchProtectedScope(); 8877 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 8878 Clauses, AStmt, B); 8879 } 8880 8881 StmtResult 8882 Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt, 8883 SourceLocation StartLoc, SourceLocation EndLoc, 8884 VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8885 if (!AStmt) 8886 return StmtError(); 8887 8888 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 8889 OMPLoopDirective::HelperExprs B; 8890 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 8891 // define the nested loops number. 8892 unsigned NestedLoopCount = checkOpenMPLoop( 8893 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), 8894 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 8895 if (NestedLoopCount == 0) 8896 return StmtError(); 8897 8898 assert((CurContext->isDependentContext() || B.builtAll()) && 8899 "omp for loop exprs were not built"); 8900 8901 if (!CurContext->isDependentContext()) { 8902 // Finalize the clauses that need pre-built expressions for CodeGen. 8903 for (OMPClause *C : Clauses) { 8904 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 8905 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 8906 B.NumIterations, *this, CurScope, 8907 DSAStack)) 8908 return StmtError(); 8909 } 8910 } 8911 8912 setFunctionHasBranchProtectedScope(); 8913 return OMPForDirective::Create( 8914 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 8915 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 8916 } 8917 8918 StmtResult Sema::ActOnOpenMPForSimdDirective( 8919 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8920 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8921 if (!AStmt) 8922 return StmtError(); 8923 8924 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 8925 OMPLoopDirective::HelperExprs B; 8926 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 8927 // define the nested loops number. 8928 unsigned NestedLoopCount = 8929 checkOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses), 8930 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 8931 VarsWithImplicitDSA, B); 8932 if (NestedLoopCount == 0) 8933 return StmtError(); 8934 8935 assert((CurContext->isDependentContext() || B.builtAll()) && 8936 "omp for simd loop exprs were not built"); 8937 8938 if (!CurContext->isDependentContext()) { 8939 // Finalize the clauses that need pre-built expressions for CodeGen. 8940 for (OMPClause *C : Clauses) { 8941 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 8942 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 8943 B.NumIterations, *this, CurScope, 8944 DSAStack)) 8945 return StmtError(); 8946 } 8947 } 8948 8949 if (checkSimdlenSafelenSpecified(*this, Clauses)) 8950 return StmtError(); 8951 8952 setFunctionHasBranchProtectedScope(); 8953 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 8954 Clauses, AStmt, B); 8955 } 8956 8957 StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses, 8958 Stmt *AStmt, 8959 SourceLocation StartLoc, 8960 SourceLocation EndLoc) { 8961 if (!AStmt) 8962 return StmtError(); 8963 8964 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 8965 auto BaseStmt = AStmt; 8966 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 8967 BaseStmt = CS->getCapturedStmt(); 8968 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 8969 auto S = C->children(); 8970 if (S.begin() == S.end()) 8971 return StmtError(); 8972 // All associated statements must be '#pragma omp section' except for 8973 // the first one. 8974 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) { 8975 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 8976 if (SectionStmt) 8977 Diag(SectionStmt->getBeginLoc(), 8978 diag::err_omp_sections_substmt_not_section); 8979 return StmtError(); 8980 } 8981 cast<OMPSectionDirective>(SectionStmt) 8982 ->setHasCancel(DSAStack->isCancelRegion()); 8983 } 8984 } else { 8985 Diag(AStmt->getBeginLoc(), diag::err_omp_sections_not_compound_stmt); 8986 return StmtError(); 8987 } 8988 8989 setFunctionHasBranchProtectedScope(); 8990 8991 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 8992 DSAStack->getTaskgroupReductionRef(), 8993 DSAStack->isCancelRegion()); 8994 } 8995 8996 StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt, 8997 SourceLocation StartLoc, 8998 SourceLocation EndLoc) { 8999 if (!AStmt) 9000 return StmtError(); 9001 9002 setFunctionHasBranchProtectedScope(); 9003 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion()); 9004 9005 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt, 9006 DSAStack->isCancelRegion()); 9007 } 9008 9009 StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses, 9010 Stmt *AStmt, 9011 SourceLocation StartLoc, 9012 SourceLocation EndLoc) { 9013 if (!AStmt) 9014 return StmtError(); 9015 9016 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9017 9018 setFunctionHasBranchProtectedScope(); 9019 9020 // OpenMP [2.7.3, single Construct, Restrictions] 9021 // The copyprivate clause must not be used with the nowait clause. 9022 const OMPClause *Nowait = nullptr; 9023 const OMPClause *Copyprivate = nullptr; 9024 for (const OMPClause *Clause : Clauses) { 9025 if (Clause->getClauseKind() == OMPC_nowait) 9026 Nowait = Clause; 9027 else if (Clause->getClauseKind() == OMPC_copyprivate) 9028 Copyprivate = Clause; 9029 if (Copyprivate && Nowait) { 9030 Diag(Copyprivate->getBeginLoc(), 9031 diag::err_omp_single_copyprivate_with_nowait); 9032 Diag(Nowait->getBeginLoc(), diag::note_omp_nowait_clause_here); 9033 return StmtError(); 9034 } 9035 } 9036 9037 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 9038 } 9039 9040 StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt, 9041 SourceLocation StartLoc, 9042 SourceLocation EndLoc) { 9043 if (!AStmt) 9044 return StmtError(); 9045 9046 setFunctionHasBranchProtectedScope(); 9047 9048 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt); 9049 } 9050 9051 StmtResult Sema::ActOnOpenMPCriticalDirective( 9052 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses, 9053 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { 9054 if (!AStmt) 9055 return StmtError(); 9056 9057 bool ErrorFound = false; 9058 llvm::APSInt Hint; 9059 SourceLocation HintLoc; 9060 bool DependentHint = false; 9061 for (const OMPClause *C : Clauses) { 9062 if (C->getClauseKind() == OMPC_hint) { 9063 if (!DirName.getName()) { 9064 Diag(C->getBeginLoc(), diag::err_omp_hint_clause_no_name); 9065 ErrorFound = true; 9066 } 9067 Expr *E = cast<OMPHintClause>(C)->getHint(); 9068 if (E->isTypeDependent() || E->isValueDependent() || 9069 E->isInstantiationDependent()) { 9070 DependentHint = true; 9071 } else { 9072 Hint = E->EvaluateKnownConstInt(Context); 9073 HintLoc = C->getBeginLoc(); 9074 } 9075 } 9076 } 9077 if (ErrorFound) 9078 return StmtError(); 9079 const auto Pair = DSAStack->getCriticalWithHint(DirName); 9080 if (Pair.first && DirName.getName() && !DependentHint) { 9081 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) { 9082 Diag(StartLoc, diag::err_omp_critical_with_hint); 9083 if (HintLoc.isValid()) 9084 Diag(HintLoc, diag::note_omp_critical_hint_here) 9085 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false); 9086 else 9087 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0; 9088 if (const auto *C = Pair.first->getSingleClause<OMPHintClause>()) { 9089 Diag(C->getBeginLoc(), diag::note_omp_critical_hint_here) 9090 << 1 9091 << C->getHint()->EvaluateKnownConstInt(Context).toString( 9092 /*Radix=*/10, /*Signed=*/false); 9093 } else { 9094 Diag(Pair.first->getBeginLoc(), diag::note_omp_critical_no_hint) << 1; 9095 } 9096 } 9097 } 9098 9099 setFunctionHasBranchProtectedScope(); 9100 9101 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc, 9102 Clauses, AStmt); 9103 if (!Pair.first && DirName.getName() && !DependentHint) 9104 DSAStack->addCriticalWithHint(Dir, Hint); 9105 return Dir; 9106 } 9107 9108 StmtResult Sema::ActOnOpenMPParallelForDirective( 9109 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 9110 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 9111 if (!AStmt) 9112 return StmtError(); 9113 9114 auto *CS = cast<CapturedStmt>(AStmt); 9115 // 1.2.2 OpenMP Language Terminology 9116 // Structured block - An executable statement with a single entry at the 9117 // top and a single exit at the bottom. 9118 // The point of exit cannot be a branch out of the structured block. 9119 // longjmp() and throw() must not violate the entry/exit criteria. 9120 CS->getCapturedDecl()->setNothrow(); 9121 9122 OMPLoopDirective::HelperExprs B; 9123 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 9124 // define the nested loops number. 9125 unsigned NestedLoopCount = 9126 checkOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses), 9127 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 9128 VarsWithImplicitDSA, B); 9129 if (NestedLoopCount == 0) 9130 return StmtError(); 9131 9132 assert((CurContext->isDependentContext() || B.builtAll()) && 9133 "omp parallel for loop exprs were not built"); 9134 9135 if (!CurContext->isDependentContext()) { 9136 // Finalize the clauses that need pre-built expressions for CodeGen. 9137 for (OMPClause *C : Clauses) { 9138 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 9139 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 9140 B.NumIterations, *this, CurScope, 9141 DSAStack)) 9142 return StmtError(); 9143 } 9144 } 9145 9146 setFunctionHasBranchProtectedScope(); 9147 return OMPParallelForDirective::Create( 9148 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 9149 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 9150 } 9151 9152 StmtResult Sema::ActOnOpenMPParallelForSimdDirective( 9153 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 9154 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 9155 if (!AStmt) 9156 return StmtError(); 9157 9158 auto *CS = cast<CapturedStmt>(AStmt); 9159 // 1.2.2 OpenMP Language Terminology 9160 // Structured block - An executable statement with a single entry at the 9161 // top and a single exit at the bottom. 9162 // The point of exit cannot be a branch out of the structured block. 9163 // longjmp() and throw() must not violate the entry/exit criteria. 9164 CS->getCapturedDecl()->setNothrow(); 9165 9166 OMPLoopDirective::HelperExprs B; 9167 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 9168 // define the nested loops number. 9169 unsigned NestedLoopCount = 9170 checkOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses), 9171 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 9172 VarsWithImplicitDSA, B); 9173 if (NestedLoopCount == 0) 9174 return StmtError(); 9175 9176 if (!CurContext->isDependentContext()) { 9177 // Finalize the clauses that need pre-built expressions for CodeGen. 9178 for (OMPClause *C : Clauses) { 9179 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 9180 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 9181 B.NumIterations, *this, CurScope, 9182 DSAStack)) 9183 return StmtError(); 9184 } 9185 } 9186 9187 if (checkSimdlenSafelenSpecified(*this, Clauses)) 9188 return StmtError(); 9189 9190 setFunctionHasBranchProtectedScope(); 9191 return OMPParallelForSimdDirective::Create( 9192 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 9193 } 9194 9195 StmtResult 9196 Sema::ActOnOpenMPParallelMasterDirective(ArrayRef<OMPClause *> Clauses, 9197 Stmt *AStmt, SourceLocation StartLoc, 9198 SourceLocation EndLoc) { 9199 if (!AStmt) 9200 return StmtError(); 9201 9202 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9203 auto *CS = cast<CapturedStmt>(AStmt); 9204 // 1.2.2 OpenMP Language Terminology 9205 // Structured block - An executable statement with a single entry at the 9206 // top and a single exit at the bottom. 9207 // The point of exit cannot be a branch out of the structured block. 9208 // longjmp() and throw() must not violate the entry/exit criteria. 9209 CS->getCapturedDecl()->setNothrow(); 9210 9211 setFunctionHasBranchProtectedScope(); 9212 9213 return OMPParallelMasterDirective::Create( 9214 Context, StartLoc, EndLoc, Clauses, AStmt, 9215 DSAStack->getTaskgroupReductionRef()); 9216 } 9217 9218 StmtResult 9219 Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses, 9220 Stmt *AStmt, SourceLocation StartLoc, 9221 SourceLocation EndLoc) { 9222 if (!AStmt) 9223 return StmtError(); 9224 9225 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9226 auto BaseStmt = AStmt; 9227 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 9228 BaseStmt = CS->getCapturedStmt(); 9229 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 9230 auto S = C->children(); 9231 if (S.begin() == S.end()) 9232 return StmtError(); 9233 // All associated statements must be '#pragma omp section' except for 9234 // the first one. 9235 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) { 9236 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 9237 if (SectionStmt) 9238 Diag(SectionStmt->getBeginLoc(), 9239 diag::err_omp_parallel_sections_substmt_not_section); 9240 return StmtError(); 9241 } 9242 cast<OMPSectionDirective>(SectionStmt) 9243 ->setHasCancel(DSAStack->isCancelRegion()); 9244 } 9245 } else { 9246 Diag(AStmt->getBeginLoc(), 9247 diag::err_omp_parallel_sections_not_compound_stmt); 9248 return StmtError(); 9249 } 9250 9251 setFunctionHasBranchProtectedScope(); 9252 9253 return OMPParallelSectionsDirective::Create( 9254 Context, StartLoc, EndLoc, Clauses, AStmt, 9255 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 9256 } 9257 9258 /// detach and mergeable clauses are mutially exclusive, check for it. 9259 static bool checkDetachMergeableClauses(Sema &S, 9260 ArrayRef<OMPClause *> Clauses) { 9261 const OMPClause *PrevClause = nullptr; 9262 bool ErrorFound = false; 9263 for (const OMPClause *C : Clauses) { 9264 if (C->getClauseKind() == OMPC_detach || 9265 C->getClauseKind() == OMPC_mergeable) { 9266 if (!PrevClause) { 9267 PrevClause = C; 9268 } else if (PrevClause->getClauseKind() != C->getClauseKind()) { 9269 S.Diag(C->getBeginLoc(), diag::err_omp_clauses_mutually_exclusive) 9270 << getOpenMPClauseName(C->getClauseKind()) 9271 << getOpenMPClauseName(PrevClause->getClauseKind()); 9272 S.Diag(PrevClause->getBeginLoc(), diag::note_omp_previous_clause) 9273 << getOpenMPClauseName(PrevClause->getClauseKind()); 9274 ErrorFound = true; 9275 } 9276 } 9277 } 9278 return ErrorFound; 9279 } 9280 9281 StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses, 9282 Stmt *AStmt, SourceLocation StartLoc, 9283 SourceLocation EndLoc) { 9284 if (!AStmt) 9285 return StmtError(); 9286 9287 // OpenMP 5.0, 2.10.1 task Construct 9288 // If a detach clause appears on the directive, then a mergeable clause cannot 9289 // appear on the same directive. 9290 if (checkDetachMergeableClauses(*this, Clauses)) 9291 return StmtError(); 9292 9293 auto *CS = cast<CapturedStmt>(AStmt); 9294 // 1.2.2 OpenMP Language Terminology 9295 // Structured block - An executable statement with a single entry at the 9296 // top and a single exit at the bottom. 9297 // The point of exit cannot be a branch out of the structured block. 9298 // longjmp() and throw() must not violate the entry/exit criteria. 9299 CS->getCapturedDecl()->setNothrow(); 9300 9301 setFunctionHasBranchProtectedScope(); 9302 9303 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 9304 DSAStack->isCancelRegion()); 9305 } 9306 9307 StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc, 9308 SourceLocation EndLoc) { 9309 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc); 9310 } 9311 9312 StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc, 9313 SourceLocation EndLoc) { 9314 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc); 9315 } 9316 9317 StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc, 9318 SourceLocation EndLoc) { 9319 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc); 9320 } 9321 9322 StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses, 9323 Stmt *AStmt, 9324 SourceLocation StartLoc, 9325 SourceLocation EndLoc) { 9326 if (!AStmt) 9327 return StmtError(); 9328 9329 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9330 9331 setFunctionHasBranchProtectedScope(); 9332 9333 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses, 9334 AStmt, 9335 DSAStack->getTaskgroupReductionRef()); 9336 } 9337 9338 StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses, 9339 SourceLocation StartLoc, 9340 SourceLocation EndLoc) { 9341 OMPFlushClause *FC = nullptr; 9342 OMPClause *OrderClause = nullptr; 9343 for (OMPClause *C : Clauses) { 9344 if (C->getClauseKind() == OMPC_flush) 9345 FC = cast<OMPFlushClause>(C); 9346 else 9347 OrderClause = C; 9348 } 9349 OpenMPClauseKind MemOrderKind = OMPC_unknown; 9350 SourceLocation MemOrderLoc; 9351 for (const OMPClause *C : Clauses) { 9352 if (C->getClauseKind() == OMPC_acq_rel || 9353 C->getClauseKind() == OMPC_acquire || 9354 C->getClauseKind() == OMPC_release) { 9355 if (MemOrderKind != OMPC_unknown) { 9356 Diag(C->getBeginLoc(), diag::err_omp_several_mem_order_clauses) 9357 << getOpenMPDirectiveName(OMPD_flush) << 1 9358 << SourceRange(C->getBeginLoc(), C->getEndLoc()); 9359 Diag(MemOrderLoc, diag::note_omp_previous_mem_order_clause) 9360 << getOpenMPClauseName(MemOrderKind); 9361 } else { 9362 MemOrderKind = C->getClauseKind(); 9363 MemOrderLoc = C->getBeginLoc(); 9364 } 9365 } 9366 } 9367 if (FC && OrderClause) { 9368 Diag(FC->getLParenLoc(), diag::err_omp_flush_order_clause_and_list) 9369 << getOpenMPClauseName(OrderClause->getClauseKind()); 9370 Diag(OrderClause->getBeginLoc(), diag::note_omp_flush_order_clause_here) 9371 << getOpenMPClauseName(OrderClause->getClauseKind()); 9372 return StmtError(); 9373 } 9374 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses); 9375 } 9376 9377 StmtResult Sema::ActOnOpenMPDepobjDirective(ArrayRef<OMPClause *> Clauses, 9378 SourceLocation StartLoc, 9379 SourceLocation EndLoc) { 9380 if (Clauses.empty()) { 9381 Diag(StartLoc, diag::err_omp_depobj_expected); 9382 return StmtError(); 9383 } else if (Clauses[0]->getClauseKind() != OMPC_depobj) { 9384 Diag(Clauses[0]->getBeginLoc(), diag::err_omp_depobj_expected); 9385 return StmtError(); 9386 } 9387 // Only depobj expression and another single clause is allowed. 9388 if (Clauses.size() > 2) { 9389 Diag(Clauses[2]->getBeginLoc(), 9390 diag::err_omp_depobj_single_clause_expected); 9391 return StmtError(); 9392 } else if (Clauses.size() < 1) { 9393 Diag(Clauses[0]->getEndLoc(), diag::err_omp_depobj_single_clause_expected); 9394 return StmtError(); 9395 } 9396 return OMPDepobjDirective::Create(Context, StartLoc, EndLoc, Clauses); 9397 } 9398 9399 StmtResult Sema::ActOnOpenMPScanDirective(ArrayRef<OMPClause *> Clauses, 9400 SourceLocation StartLoc, 9401 SourceLocation EndLoc) { 9402 // Check that exactly one clause is specified. 9403 if (Clauses.size() != 1) { 9404 Diag(Clauses.empty() ? EndLoc : Clauses[1]->getBeginLoc(), 9405 diag::err_omp_scan_single_clause_expected); 9406 return StmtError(); 9407 } 9408 // Check that scan directive is used in the scopeof the OpenMP loop body. 9409 if (Scope *S = DSAStack->getCurScope()) { 9410 Scope *ParentS = S->getParent(); 9411 if (!ParentS || ParentS->getParent() != ParentS->getBreakParent() || 9412 !ParentS->getBreakParent()->isOpenMPLoopScope()) 9413 return StmtError(Diag(StartLoc, diag::err_omp_orphaned_device_directive) 9414 << getOpenMPDirectiveName(OMPD_scan) << 5); 9415 } 9416 // Check that only one instance of scan directives is used in the same outer 9417 // region. 9418 if (DSAStack->doesParentHasScanDirective()) { 9419 Diag(StartLoc, diag::err_omp_several_directives_in_region) << "scan"; 9420 Diag(DSAStack->getParentScanDirectiveLoc(), 9421 diag::note_omp_previous_directive) 9422 << "scan"; 9423 return StmtError(); 9424 } 9425 DSAStack->setParentHasScanDirective(StartLoc); 9426 return OMPScanDirective::Create(Context, StartLoc, EndLoc, Clauses); 9427 } 9428 9429 StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses, 9430 Stmt *AStmt, 9431 SourceLocation StartLoc, 9432 SourceLocation EndLoc) { 9433 const OMPClause *DependFound = nullptr; 9434 const OMPClause *DependSourceClause = nullptr; 9435 const OMPClause *DependSinkClause = nullptr; 9436 bool ErrorFound = false; 9437 const OMPThreadsClause *TC = nullptr; 9438 const OMPSIMDClause *SC = nullptr; 9439 for (const OMPClause *C : Clauses) { 9440 if (auto *DC = dyn_cast<OMPDependClause>(C)) { 9441 DependFound = C; 9442 if (DC->getDependencyKind() == OMPC_DEPEND_source) { 9443 if (DependSourceClause) { 9444 Diag(C->getBeginLoc(), diag::err_omp_more_one_clause) 9445 << getOpenMPDirectiveName(OMPD_ordered) 9446 << getOpenMPClauseName(OMPC_depend) << 2; 9447 ErrorFound = true; 9448 } else { 9449 DependSourceClause = C; 9450 } 9451 if (DependSinkClause) { 9452 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed) 9453 << 0; 9454 ErrorFound = true; 9455 } 9456 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) { 9457 if (DependSourceClause) { 9458 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed) 9459 << 1; 9460 ErrorFound = true; 9461 } 9462 DependSinkClause = C; 9463 } 9464 } else if (C->getClauseKind() == OMPC_threads) { 9465 TC = cast<OMPThreadsClause>(C); 9466 } else if (C->getClauseKind() == OMPC_simd) { 9467 SC = cast<OMPSIMDClause>(C); 9468 } 9469 } 9470 if (!ErrorFound && !SC && 9471 isOpenMPSimdDirective(DSAStack->getParentDirective())) { 9472 // OpenMP [2.8.1,simd Construct, Restrictions] 9473 // An ordered construct with the simd clause is the only OpenMP construct 9474 // that can appear in the simd region. 9475 Diag(StartLoc, diag::err_omp_prohibited_region_simd) 9476 << (LangOpts.OpenMP >= 50 ? 1 : 0); 9477 ErrorFound = true; 9478 } else if (DependFound && (TC || SC)) { 9479 Diag(DependFound->getBeginLoc(), diag::err_omp_depend_clause_thread_simd) 9480 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind()); 9481 ErrorFound = true; 9482 } else if (DependFound && !DSAStack->getParentOrderedRegionParam().first) { 9483 Diag(DependFound->getBeginLoc(), 9484 diag::err_omp_ordered_directive_without_param); 9485 ErrorFound = true; 9486 } else if (TC || Clauses.empty()) { 9487 if (const Expr *Param = DSAStack->getParentOrderedRegionParam().first) { 9488 SourceLocation ErrLoc = TC ? TC->getBeginLoc() : StartLoc; 9489 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param) 9490 << (TC != nullptr); 9491 Diag(Param->getBeginLoc(), diag::note_omp_ordered_param) << 1; 9492 ErrorFound = true; 9493 } 9494 } 9495 if ((!AStmt && !DependFound) || ErrorFound) 9496 return StmtError(); 9497 9498 // OpenMP 5.0, 2.17.9, ordered Construct, Restrictions. 9499 // During execution of an iteration of a worksharing-loop or a loop nest 9500 // within a worksharing-loop, simd, or worksharing-loop SIMD region, a thread 9501 // must not execute more than one ordered region corresponding to an ordered 9502 // construct without a depend clause. 9503 if (!DependFound) { 9504 if (DSAStack->doesParentHasOrderedDirective()) { 9505 Diag(StartLoc, diag::err_omp_several_directives_in_region) << "ordered"; 9506 Diag(DSAStack->getParentOrderedDirectiveLoc(), 9507 diag::note_omp_previous_directive) 9508 << "ordered"; 9509 return StmtError(); 9510 } 9511 DSAStack->setParentHasOrderedDirective(StartLoc); 9512 } 9513 9514 if (AStmt) { 9515 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9516 9517 setFunctionHasBranchProtectedScope(); 9518 } 9519 9520 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 9521 } 9522 9523 namespace { 9524 /// Helper class for checking expression in 'omp atomic [update]' 9525 /// construct. 9526 class OpenMPAtomicUpdateChecker { 9527 /// Error results for atomic update expressions. 9528 enum ExprAnalysisErrorCode { 9529 /// A statement is not an expression statement. 9530 NotAnExpression, 9531 /// Expression is not builtin binary or unary operation. 9532 NotABinaryOrUnaryExpression, 9533 /// Unary operation is not post-/pre- increment/decrement operation. 9534 NotAnUnaryIncDecExpression, 9535 /// An expression is not of scalar type. 9536 NotAScalarType, 9537 /// A binary operation is not an assignment operation. 9538 NotAnAssignmentOp, 9539 /// RHS part of the binary operation is not a binary expression. 9540 NotABinaryExpression, 9541 /// RHS part is not additive/multiplicative/shift/biwise binary 9542 /// expression. 9543 NotABinaryOperator, 9544 /// RHS binary operation does not have reference to the updated LHS 9545 /// part. 9546 NotAnUpdateExpression, 9547 /// No errors is found. 9548 NoError 9549 }; 9550 /// Reference to Sema. 9551 Sema &SemaRef; 9552 /// A location for note diagnostics (when error is found). 9553 SourceLocation NoteLoc; 9554 /// 'x' lvalue part of the source atomic expression. 9555 Expr *X; 9556 /// 'expr' rvalue part of the source atomic expression. 9557 Expr *E; 9558 /// Helper expression of the form 9559 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or 9560 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. 9561 Expr *UpdateExpr; 9562 /// Is 'x' a LHS in a RHS part of full update expression. It is 9563 /// important for non-associative operations. 9564 bool IsXLHSInRHSPart; 9565 BinaryOperatorKind Op; 9566 SourceLocation OpLoc; 9567 /// true if the source expression is a postfix unary operation, false 9568 /// if it is a prefix unary operation. 9569 bool IsPostfixUpdate; 9570 9571 public: 9572 OpenMPAtomicUpdateChecker(Sema &SemaRef) 9573 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr), 9574 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {} 9575 /// Check specified statement that it is suitable for 'atomic update' 9576 /// constructs and extract 'x', 'expr' and Operation from the original 9577 /// expression. If DiagId and NoteId == 0, then only check is performed 9578 /// without error notification. 9579 /// \param DiagId Diagnostic which should be emitted if error is found. 9580 /// \param NoteId Diagnostic note for the main error message. 9581 /// \return true if statement is not an update expression, false otherwise. 9582 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0); 9583 /// Return the 'x' lvalue part of the source atomic expression. 9584 Expr *getX() const { return X; } 9585 /// Return the 'expr' rvalue part of the source atomic expression. 9586 Expr *getExpr() const { return E; } 9587 /// Return the update expression used in calculation of the updated 9588 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or 9589 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. 9590 Expr *getUpdateExpr() const { return UpdateExpr; } 9591 /// Return true if 'x' is LHS in RHS part of full update expression, 9592 /// false otherwise. 9593 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; } 9594 9595 /// true if the source expression is a postfix unary operation, false 9596 /// if it is a prefix unary operation. 9597 bool isPostfixUpdate() const { return IsPostfixUpdate; } 9598 9599 private: 9600 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0, 9601 unsigned NoteId = 0); 9602 }; 9603 } // namespace 9604 9605 bool OpenMPAtomicUpdateChecker::checkBinaryOperation( 9606 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) { 9607 ExprAnalysisErrorCode ErrorFound = NoError; 9608 SourceLocation ErrorLoc, NoteLoc; 9609 SourceRange ErrorRange, NoteRange; 9610 // Allowed constructs are: 9611 // x = x binop expr; 9612 // x = expr binop x; 9613 if (AtomicBinOp->getOpcode() == BO_Assign) { 9614 X = AtomicBinOp->getLHS(); 9615 if (const auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>( 9616 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) { 9617 if (AtomicInnerBinOp->isMultiplicativeOp() || 9618 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() || 9619 AtomicInnerBinOp->isBitwiseOp()) { 9620 Op = AtomicInnerBinOp->getOpcode(); 9621 OpLoc = AtomicInnerBinOp->getOperatorLoc(); 9622 Expr *LHS = AtomicInnerBinOp->getLHS(); 9623 Expr *RHS = AtomicInnerBinOp->getRHS(); 9624 llvm::FoldingSetNodeID XId, LHSId, RHSId; 9625 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(), 9626 /*Canonical=*/true); 9627 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(), 9628 /*Canonical=*/true); 9629 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(), 9630 /*Canonical=*/true); 9631 if (XId == LHSId) { 9632 E = RHS; 9633 IsXLHSInRHSPart = true; 9634 } else if (XId == RHSId) { 9635 E = LHS; 9636 IsXLHSInRHSPart = false; 9637 } else { 9638 ErrorLoc = AtomicInnerBinOp->getExprLoc(); 9639 ErrorRange = AtomicInnerBinOp->getSourceRange(); 9640 NoteLoc = X->getExprLoc(); 9641 NoteRange = X->getSourceRange(); 9642 ErrorFound = NotAnUpdateExpression; 9643 } 9644 } else { 9645 ErrorLoc = AtomicInnerBinOp->getExprLoc(); 9646 ErrorRange = AtomicInnerBinOp->getSourceRange(); 9647 NoteLoc = AtomicInnerBinOp->getOperatorLoc(); 9648 NoteRange = SourceRange(NoteLoc, NoteLoc); 9649 ErrorFound = NotABinaryOperator; 9650 } 9651 } else { 9652 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc(); 9653 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange(); 9654 ErrorFound = NotABinaryExpression; 9655 } 9656 } else { 9657 ErrorLoc = AtomicBinOp->getExprLoc(); 9658 ErrorRange = AtomicBinOp->getSourceRange(); 9659 NoteLoc = AtomicBinOp->getOperatorLoc(); 9660 NoteRange = SourceRange(NoteLoc, NoteLoc); 9661 ErrorFound = NotAnAssignmentOp; 9662 } 9663 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { 9664 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; 9665 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; 9666 return true; 9667 } 9668 if (SemaRef.CurContext->isDependentContext()) 9669 E = X = UpdateExpr = nullptr; 9670 return ErrorFound != NoError; 9671 } 9672 9673 bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId, 9674 unsigned NoteId) { 9675 ExprAnalysisErrorCode ErrorFound = NoError; 9676 SourceLocation ErrorLoc, NoteLoc; 9677 SourceRange ErrorRange, NoteRange; 9678 // Allowed constructs are: 9679 // x++; 9680 // x--; 9681 // ++x; 9682 // --x; 9683 // x binop= expr; 9684 // x = x binop expr; 9685 // x = expr binop x; 9686 if (auto *AtomicBody = dyn_cast<Expr>(S)) { 9687 AtomicBody = AtomicBody->IgnoreParenImpCasts(); 9688 if (AtomicBody->getType()->isScalarType() || 9689 AtomicBody->isInstantiationDependent()) { 9690 if (const auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>( 9691 AtomicBody->IgnoreParenImpCasts())) { 9692 // Check for Compound Assignment Operation 9693 Op = BinaryOperator::getOpForCompoundAssignment( 9694 AtomicCompAssignOp->getOpcode()); 9695 OpLoc = AtomicCompAssignOp->getOperatorLoc(); 9696 E = AtomicCompAssignOp->getRHS(); 9697 X = AtomicCompAssignOp->getLHS()->IgnoreParens(); 9698 IsXLHSInRHSPart = true; 9699 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>( 9700 AtomicBody->IgnoreParenImpCasts())) { 9701 // Check for Binary Operation 9702 if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId)) 9703 return true; 9704 } else if (const auto *AtomicUnaryOp = dyn_cast<UnaryOperator>( 9705 AtomicBody->IgnoreParenImpCasts())) { 9706 // Check for Unary Operation 9707 if (AtomicUnaryOp->isIncrementDecrementOp()) { 9708 IsPostfixUpdate = AtomicUnaryOp->isPostfix(); 9709 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub; 9710 OpLoc = AtomicUnaryOp->getOperatorLoc(); 9711 X = AtomicUnaryOp->getSubExpr()->IgnoreParens(); 9712 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get(); 9713 IsXLHSInRHSPart = true; 9714 } else { 9715 ErrorFound = NotAnUnaryIncDecExpression; 9716 ErrorLoc = AtomicUnaryOp->getExprLoc(); 9717 ErrorRange = AtomicUnaryOp->getSourceRange(); 9718 NoteLoc = AtomicUnaryOp->getOperatorLoc(); 9719 NoteRange = SourceRange(NoteLoc, NoteLoc); 9720 } 9721 } else if (!AtomicBody->isInstantiationDependent()) { 9722 ErrorFound = NotABinaryOrUnaryExpression; 9723 NoteLoc = ErrorLoc = AtomicBody->getExprLoc(); 9724 NoteRange = ErrorRange = AtomicBody->getSourceRange(); 9725 } 9726 } else { 9727 ErrorFound = NotAScalarType; 9728 NoteLoc = ErrorLoc = AtomicBody->getBeginLoc(); 9729 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 9730 } 9731 } else { 9732 ErrorFound = NotAnExpression; 9733 NoteLoc = ErrorLoc = S->getBeginLoc(); 9734 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 9735 } 9736 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { 9737 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; 9738 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; 9739 return true; 9740 } 9741 if (SemaRef.CurContext->isDependentContext()) 9742 E = X = UpdateExpr = nullptr; 9743 if (ErrorFound == NoError && E && X) { 9744 // Build an update expression of form 'OpaqueValueExpr(x) binop 9745 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop 9746 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression. 9747 auto *OVEX = new (SemaRef.getASTContext()) 9748 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue); 9749 auto *OVEExpr = new (SemaRef.getASTContext()) 9750 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue); 9751 ExprResult Update = 9752 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr, 9753 IsXLHSInRHSPart ? OVEExpr : OVEX); 9754 if (Update.isInvalid()) 9755 return true; 9756 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(), 9757 Sema::AA_Casting); 9758 if (Update.isInvalid()) 9759 return true; 9760 UpdateExpr = Update.get(); 9761 } 9762 return ErrorFound != NoError; 9763 } 9764 9765 StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses, 9766 Stmt *AStmt, 9767 SourceLocation StartLoc, 9768 SourceLocation EndLoc) { 9769 // Register location of the first atomic directive. 9770 DSAStack->addAtomicDirectiveLoc(StartLoc); 9771 if (!AStmt) 9772 return StmtError(); 9773 9774 // 1.2.2 OpenMP Language Terminology 9775 // Structured block - An executable statement with a single entry at the 9776 // top and a single exit at the bottom. 9777 // The point of exit cannot be a branch out of the structured block. 9778 // longjmp() and throw() must not violate the entry/exit criteria. 9779 OpenMPClauseKind AtomicKind = OMPC_unknown; 9780 SourceLocation AtomicKindLoc; 9781 OpenMPClauseKind MemOrderKind = OMPC_unknown; 9782 SourceLocation MemOrderLoc; 9783 for (const OMPClause *C : Clauses) { 9784 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write || 9785 C->getClauseKind() == OMPC_update || 9786 C->getClauseKind() == OMPC_capture) { 9787 if (AtomicKind != OMPC_unknown) { 9788 Diag(C->getBeginLoc(), diag::err_omp_atomic_several_clauses) 9789 << SourceRange(C->getBeginLoc(), C->getEndLoc()); 9790 Diag(AtomicKindLoc, diag::note_omp_previous_mem_order_clause) 9791 << getOpenMPClauseName(AtomicKind); 9792 } else { 9793 AtomicKind = C->getClauseKind(); 9794 AtomicKindLoc = C->getBeginLoc(); 9795 } 9796 } 9797 if (C->getClauseKind() == OMPC_seq_cst || 9798 C->getClauseKind() == OMPC_acq_rel || 9799 C->getClauseKind() == OMPC_acquire || 9800 C->getClauseKind() == OMPC_release || 9801 C->getClauseKind() == OMPC_relaxed) { 9802 if (MemOrderKind != OMPC_unknown) { 9803 Diag(C->getBeginLoc(), diag::err_omp_several_mem_order_clauses) 9804 << getOpenMPDirectiveName(OMPD_atomic) << 0 9805 << SourceRange(C->getBeginLoc(), C->getEndLoc()); 9806 Diag(MemOrderLoc, diag::note_omp_previous_mem_order_clause) 9807 << getOpenMPClauseName(MemOrderKind); 9808 } else { 9809 MemOrderKind = C->getClauseKind(); 9810 MemOrderLoc = C->getBeginLoc(); 9811 } 9812 } 9813 } 9814 // OpenMP 5.0, 2.17.7 atomic Construct, Restrictions 9815 // If atomic-clause is read then memory-order-clause must not be acq_rel or 9816 // release. 9817 // If atomic-clause is write then memory-order-clause must not be acq_rel or 9818 // acquire. 9819 // If atomic-clause is update or not present then memory-order-clause must not 9820 // be acq_rel or acquire. 9821 if ((AtomicKind == OMPC_read && 9822 (MemOrderKind == OMPC_acq_rel || MemOrderKind == OMPC_release)) || 9823 ((AtomicKind == OMPC_write || AtomicKind == OMPC_update || 9824 AtomicKind == OMPC_unknown) && 9825 (MemOrderKind == OMPC_acq_rel || MemOrderKind == OMPC_acquire))) { 9826 SourceLocation Loc = AtomicKindLoc; 9827 if (AtomicKind == OMPC_unknown) 9828 Loc = StartLoc; 9829 Diag(Loc, diag::err_omp_atomic_incompatible_mem_order_clause) 9830 << getOpenMPClauseName(AtomicKind) 9831 << (AtomicKind == OMPC_unknown ? 1 : 0) 9832 << getOpenMPClauseName(MemOrderKind); 9833 Diag(MemOrderLoc, diag::note_omp_previous_mem_order_clause) 9834 << getOpenMPClauseName(MemOrderKind); 9835 } 9836 9837 Stmt *Body = AStmt; 9838 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body)) 9839 Body = EWC->getSubExpr(); 9840 9841 Expr *X = nullptr; 9842 Expr *V = nullptr; 9843 Expr *E = nullptr; 9844 Expr *UE = nullptr; 9845 bool IsXLHSInRHSPart = false; 9846 bool IsPostfixUpdate = false; 9847 // OpenMP [2.12.6, atomic Construct] 9848 // In the next expressions: 9849 // * x and v (as applicable) are both l-value expressions with scalar type. 9850 // * During the execution of an atomic region, multiple syntactic 9851 // occurrences of x must designate the same storage location. 9852 // * Neither of v and expr (as applicable) may access the storage location 9853 // designated by x. 9854 // * Neither of x and expr (as applicable) may access the storage location 9855 // designated by v. 9856 // * expr is an expression with scalar type. 9857 // * binop is one of +, *, -, /, &, ^, |, <<, or >>. 9858 // * binop, binop=, ++, and -- are not overloaded operators. 9859 // * The expression x binop expr must be numerically equivalent to x binop 9860 // (expr). This requirement is satisfied if the operators in expr have 9861 // precedence greater than binop, or by using parentheses around expr or 9862 // subexpressions of expr. 9863 // * The expression expr binop x must be numerically equivalent to (expr) 9864 // binop x. This requirement is satisfied if the operators in expr have 9865 // precedence equal to or greater than binop, or by using parentheses around 9866 // expr or subexpressions of expr. 9867 // * For forms that allow multiple occurrences of x, the number of times 9868 // that x is evaluated is unspecified. 9869 if (AtomicKind == OMPC_read) { 9870 enum { 9871 NotAnExpression, 9872 NotAnAssignmentOp, 9873 NotAScalarType, 9874 NotAnLValue, 9875 NoError 9876 } ErrorFound = NoError; 9877 SourceLocation ErrorLoc, NoteLoc; 9878 SourceRange ErrorRange, NoteRange; 9879 // If clause is read: 9880 // v = x; 9881 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 9882 const auto *AtomicBinOp = 9883 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 9884 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 9885 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); 9886 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts(); 9887 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && 9888 (V->isInstantiationDependent() || V->getType()->isScalarType())) { 9889 if (!X->isLValue() || !V->isLValue()) { 9890 const Expr *NotLValueExpr = X->isLValue() ? V : X; 9891 ErrorFound = NotAnLValue; 9892 ErrorLoc = AtomicBinOp->getExprLoc(); 9893 ErrorRange = AtomicBinOp->getSourceRange(); 9894 NoteLoc = NotLValueExpr->getExprLoc(); 9895 NoteRange = NotLValueExpr->getSourceRange(); 9896 } 9897 } else if (!X->isInstantiationDependent() || 9898 !V->isInstantiationDependent()) { 9899 const Expr *NotScalarExpr = 9900 (X->isInstantiationDependent() || X->getType()->isScalarType()) 9901 ? V 9902 : X; 9903 ErrorFound = NotAScalarType; 9904 ErrorLoc = AtomicBinOp->getExprLoc(); 9905 ErrorRange = AtomicBinOp->getSourceRange(); 9906 NoteLoc = NotScalarExpr->getExprLoc(); 9907 NoteRange = NotScalarExpr->getSourceRange(); 9908 } 9909 } else if (!AtomicBody->isInstantiationDependent()) { 9910 ErrorFound = NotAnAssignmentOp; 9911 ErrorLoc = AtomicBody->getExprLoc(); 9912 ErrorRange = AtomicBody->getSourceRange(); 9913 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 9914 : AtomicBody->getExprLoc(); 9915 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 9916 : AtomicBody->getSourceRange(); 9917 } 9918 } else { 9919 ErrorFound = NotAnExpression; 9920 NoteLoc = ErrorLoc = Body->getBeginLoc(); 9921 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 9922 } 9923 if (ErrorFound != NoError) { 9924 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement) 9925 << ErrorRange; 9926 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound 9927 << NoteRange; 9928 return StmtError(); 9929 } 9930 if (CurContext->isDependentContext()) 9931 V = X = nullptr; 9932 } else if (AtomicKind == OMPC_write) { 9933 enum { 9934 NotAnExpression, 9935 NotAnAssignmentOp, 9936 NotAScalarType, 9937 NotAnLValue, 9938 NoError 9939 } ErrorFound = NoError; 9940 SourceLocation ErrorLoc, NoteLoc; 9941 SourceRange ErrorRange, NoteRange; 9942 // If clause is write: 9943 // x = expr; 9944 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 9945 const auto *AtomicBinOp = 9946 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 9947 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 9948 X = AtomicBinOp->getLHS(); 9949 E = AtomicBinOp->getRHS(); 9950 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && 9951 (E->isInstantiationDependent() || E->getType()->isScalarType())) { 9952 if (!X->isLValue()) { 9953 ErrorFound = NotAnLValue; 9954 ErrorLoc = AtomicBinOp->getExprLoc(); 9955 ErrorRange = AtomicBinOp->getSourceRange(); 9956 NoteLoc = X->getExprLoc(); 9957 NoteRange = X->getSourceRange(); 9958 } 9959 } else if (!X->isInstantiationDependent() || 9960 !E->isInstantiationDependent()) { 9961 const Expr *NotScalarExpr = 9962 (X->isInstantiationDependent() || X->getType()->isScalarType()) 9963 ? E 9964 : X; 9965 ErrorFound = NotAScalarType; 9966 ErrorLoc = AtomicBinOp->getExprLoc(); 9967 ErrorRange = AtomicBinOp->getSourceRange(); 9968 NoteLoc = NotScalarExpr->getExprLoc(); 9969 NoteRange = NotScalarExpr->getSourceRange(); 9970 } 9971 } else if (!AtomicBody->isInstantiationDependent()) { 9972 ErrorFound = NotAnAssignmentOp; 9973 ErrorLoc = AtomicBody->getExprLoc(); 9974 ErrorRange = AtomicBody->getSourceRange(); 9975 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 9976 : AtomicBody->getExprLoc(); 9977 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 9978 : AtomicBody->getSourceRange(); 9979 } 9980 } else { 9981 ErrorFound = NotAnExpression; 9982 NoteLoc = ErrorLoc = Body->getBeginLoc(); 9983 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 9984 } 9985 if (ErrorFound != NoError) { 9986 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement) 9987 << ErrorRange; 9988 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound 9989 << NoteRange; 9990 return StmtError(); 9991 } 9992 if (CurContext->isDependentContext()) 9993 E = X = nullptr; 9994 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) { 9995 // If clause is update: 9996 // x++; 9997 // x--; 9998 // ++x; 9999 // --x; 10000 // x binop= expr; 10001 // x = x binop expr; 10002 // x = expr binop x; 10003 OpenMPAtomicUpdateChecker Checker(*this); 10004 if (Checker.checkStatement( 10005 Body, (AtomicKind == OMPC_update) 10006 ? diag::err_omp_atomic_update_not_expression_statement 10007 : diag::err_omp_atomic_not_expression_statement, 10008 diag::note_omp_atomic_update)) 10009 return StmtError(); 10010 if (!CurContext->isDependentContext()) { 10011 E = Checker.getExpr(); 10012 X = Checker.getX(); 10013 UE = Checker.getUpdateExpr(); 10014 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 10015 } 10016 } else if (AtomicKind == OMPC_capture) { 10017 enum { 10018 NotAnAssignmentOp, 10019 NotACompoundStatement, 10020 NotTwoSubstatements, 10021 NotASpecificExpression, 10022 NoError 10023 } ErrorFound = NoError; 10024 SourceLocation ErrorLoc, NoteLoc; 10025 SourceRange ErrorRange, NoteRange; 10026 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 10027 // If clause is a capture: 10028 // v = x++; 10029 // v = x--; 10030 // v = ++x; 10031 // v = --x; 10032 // v = x binop= expr; 10033 // v = x = x binop expr; 10034 // v = x = expr binop x; 10035 const auto *AtomicBinOp = 10036 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 10037 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 10038 V = AtomicBinOp->getLHS(); 10039 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); 10040 OpenMPAtomicUpdateChecker Checker(*this); 10041 if (Checker.checkStatement( 10042 Body, diag::err_omp_atomic_capture_not_expression_statement, 10043 diag::note_omp_atomic_update)) 10044 return StmtError(); 10045 E = Checker.getExpr(); 10046 X = Checker.getX(); 10047 UE = Checker.getUpdateExpr(); 10048 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 10049 IsPostfixUpdate = Checker.isPostfixUpdate(); 10050 } else if (!AtomicBody->isInstantiationDependent()) { 10051 ErrorLoc = AtomicBody->getExprLoc(); 10052 ErrorRange = AtomicBody->getSourceRange(); 10053 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 10054 : AtomicBody->getExprLoc(); 10055 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 10056 : AtomicBody->getSourceRange(); 10057 ErrorFound = NotAnAssignmentOp; 10058 } 10059 if (ErrorFound != NoError) { 10060 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement) 10061 << ErrorRange; 10062 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; 10063 return StmtError(); 10064 } 10065 if (CurContext->isDependentContext()) 10066 UE = V = E = X = nullptr; 10067 } else { 10068 // If clause is a capture: 10069 // { v = x; x = expr; } 10070 // { v = x; x++; } 10071 // { v = x; x--; } 10072 // { v = x; ++x; } 10073 // { v = x; --x; } 10074 // { v = x; x binop= expr; } 10075 // { v = x; x = x binop expr; } 10076 // { v = x; x = expr binop x; } 10077 // { x++; v = x; } 10078 // { x--; v = x; } 10079 // { ++x; v = x; } 10080 // { --x; v = x; } 10081 // { x binop= expr; v = x; } 10082 // { x = x binop expr; v = x; } 10083 // { x = expr binop x; v = x; } 10084 if (auto *CS = dyn_cast<CompoundStmt>(Body)) { 10085 // Check that this is { expr1; expr2; } 10086 if (CS->size() == 2) { 10087 Stmt *First = CS->body_front(); 10088 Stmt *Second = CS->body_back(); 10089 if (auto *EWC = dyn_cast<ExprWithCleanups>(First)) 10090 First = EWC->getSubExpr()->IgnoreParenImpCasts(); 10091 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second)) 10092 Second = EWC->getSubExpr()->IgnoreParenImpCasts(); 10093 // Need to find what subexpression is 'v' and what is 'x'. 10094 OpenMPAtomicUpdateChecker Checker(*this); 10095 bool IsUpdateExprFound = !Checker.checkStatement(Second); 10096 BinaryOperator *BinOp = nullptr; 10097 if (IsUpdateExprFound) { 10098 BinOp = dyn_cast<BinaryOperator>(First); 10099 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; 10100 } 10101 if (IsUpdateExprFound && !CurContext->isDependentContext()) { 10102 // { v = x; x++; } 10103 // { v = x; x--; } 10104 // { v = x; ++x; } 10105 // { v = x; --x; } 10106 // { v = x; x binop= expr; } 10107 // { v = x; x = x binop expr; } 10108 // { v = x; x = expr binop x; } 10109 // Check that the first expression has form v = x. 10110 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); 10111 llvm::FoldingSetNodeID XId, PossibleXId; 10112 Checker.getX()->Profile(XId, Context, /*Canonical=*/true); 10113 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); 10114 IsUpdateExprFound = XId == PossibleXId; 10115 if (IsUpdateExprFound) { 10116 V = BinOp->getLHS(); 10117 X = Checker.getX(); 10118 E = Checker.getExpr(); 10119 UE = Checker.getUpdateExpr(); 10120 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 10121 IsPostfixUpdate = true; 10122 } 10123 } 10124 if (!IsUpdateExprFound) { 10125 IsUpdateExprFound = !Checker.checkStatement(First); 10126 BinOp = nullptr; 10127 if (IsUpdateExprFound) { 10128 BinOp = dyn_cast<BinaryOperator>(Second); 10129 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; 10130 } 10131 if (IsUpdateExprFound && !CurContext->isDependentContext()) { 10132 // { x++; v = x; } 10133 // { x--; v = x; } 10134 // { ++x; v = x; } 10135 // { --x; v = x; } 10136 // { x binop= expr; v = x; } 10137 // { x = x binop expr; v = x; } 10138 // { x = expr binop x; v = x; } 10139 // Check that the second expression has form v = x. 10140 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); 10141 llvm::FoldingSetNodeID XId, PossibleXId; 10142 Checker.getX()->Profile(XId, Context, /*Canonical=*/true); 10143 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); 10144 IsUpdateExprFound = XId == PossibleXId; 10145 if (IsUpdateExprFound) { 10146 V = BinOp->getLHS(); 10147 X = Checker.getX(); 10148 E = Checker.getExpr(); 10149 UE = Checker.getUpdateExpr(); 10150 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 10151 IsPostfixUpdate = false; 10152 } 10153 } 10154 } 10155 if (!IsUpdateExprFound) { 10156 // { v = x; x = expr; } 10157 auto *FirstExpr = dyn_cast<Expr>(First); 10158 auto *SecondExpr = dyn_cast<Expr>(Second); 10159 if (!FirstExpr || !SecondExpr || 10160 !(FirstExpr->isInstantiationDependent() || 10161 SecondExpr->isInstantiationDependent())) { 10162 auto *FirstBinOp = dyn_cast<BinaryOperator>(First); 10163 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) { 10164 ErrorFound = NotAnAssignmentOp; 10165 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc() 10166 : First->getBeginLoc(); 10167 NoteRange = ErrorRange = FirstBinOp 10168 ? FirstBinOp->getSourceRange() 10169 : SourceRange(ErrorLoc, ErrorLoc); 10170 } else { 10171 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second); 10172 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) { 10173 ErrorFound = NotAnAssignmentOp; 10174 NoteLoc = ErrorLoc = SecondBinOp 10175 ? SecondBinOp->getOperatorLoc() 10176 : Second->getBeginLoc(); 10177 NoteRange = ErrorRange = 10178 SecondBinOp ? SecondBinOp->getSourceRange() 10179 : SourceRange(ErrorLoc, ErrorLoc); 10180 } else { 10181 Expr *PossibleXRHSInFirst = 10182 FirstBinOp->getRHS()->IgnoreParenImpCasts(); 10183 Expr *PossibleXLHSInSecond = 10184 SecondBinOp->getLHS()->IgnoreParenImpCasts(); 10185 llvm::FoldingSetNodeID X1Id, X2Id; 10186 PossibleXRHSInFirst->Profile(X1Id, Context, 10187 /*Canonical=*/true); 10188 PossibleXLHSInSecond->Profile(X2Id, Context, 10189 /*Canonical=*/true); 10190 IsUpdateExprFound = X1Id == X2Id; 10191 if (IsUpdateExprFound) { 10192 V = FirstBinOp->getLHS(); 10193 X = SecondBinOp->getLHS(); 10194 E = SecondBinOp->getRHS(); 10195 UE = nullptr; 10196 IsXLHSInRHSPart = false; 10197 IsPostfixUpdate = true; 10198 } else { 10199 ErrorFound = NotASpecificExpression; 10200 ErrorLoc = FirstBinOp->getExprLoc(); 10201 ErrorRange = FirstBinOp->getSourceRange(); 10202 NoteLoc = SecondBinOp->getLHS()->getExprLoc(); 10203 NoteRange = SecondBinOp->getRHS()->getSourceRange(); 10204 } 10205 } 10206 } 10207 } 10208 } 10209 } else { 10210 NoteLoc = ErrorLoc = Body->getBeginLoc(); 10211 NoteRange = ErrorRange = 10212 SourceRange(Body->getBeginLoc(), Body->getBeginLoc()); 10213 ErrorFound = NotTwoSubstatements; 10214 } 10215 } else { 10216 NoteLoc = ErrorLoc = Body->getBeginLoc(); 10217 NoteRange = ErrorRange = 10218 SourceRange(Body->getBeginLoc(), Body->getBeginLoc()); 10219 ErrorFound = NotACompoundStatement; 10220 } 10221 if (ErrorFound != NoError) { 10222 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement) 10223 << ErrorRange; 10224 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; 10225 return StmtError(); 10226 } 10227 if (CurContext->isDependentContext()) 10228 UE = V = E = X = nullptr; 10229 } 10230 } 10231 10232 setFunctionHasBranchProtectedScope(); 10233 10234 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 10235 X, V, E, UE, IsXLHSInRHSPart, 10236 IsPostfixUpdate); 10237 } 10238 10239 StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses, 10240 Stmt *AStmt, 10241 SourceLocation StartLoc, 10242 SourceLocation EndLoc) { 10243 if (!AStmt) 10244 return StmtError(); 10245 10246 auto *CS = cast<CapturedStmt>(AStmt); 10247 // 1.2.2 OpenMP Language Terminology 10248 // Structured block - An executable statement with a single entry at the 10249 // top and a single exit at the bottom. 10250 // The point of exit cannot be a branch out of the structured block. 10251 // longjmp() and throw() must not violate the entry/exit criteria. 10252 CS->getCapturedDecl()->setNothrow(); 10253 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target); 10254 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10255 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10256 // 1.2.2 OpenMP Language Terminology 10257 // Structured block - An executable statement with a single entry at the 10258 // top and a single exit at the bottom. 10259 // The point of exit cannot be a branch out of the structured block. 10260 // longjmp() and throw() must not violate the entry/exit criteria. 10261 CS->getCapturedDecl()->setNothrow(); 10262 } 10263 10264 // OpenMP [2.16, Nesting of Regions] 10265 // If specified, a teams construct must be contained within a target 10266 // construct. That target construct must contain no statements or directives 10267 // outside of the teams construct. 10268 if (DSAStack->hasInnerTeamsRegion()) { 10269 const Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true); 10270 bool OMPTeamsFound = true; 10271 if (const auto *CS = dyn_cast<CompoundStmt>(S)) { 10272 auto I = CS->body_begin(); 10273 while (I != CS->body_end()) { 10274 const auto *OED = dyn_cast<OMPExecutableDirective>(*I); 10275 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind()) || 10276 OMPTeamsFound) { 10277 10278 OMPTeamsFound = false; 10279 break; 10280 } 10281 ++I; 10282 } 10283 assert(I != CS->body_end() && "Not found statement"); 10284 S = *I; 10285 } else { 10286 const auto *OED = dyn_cast<OMPExecutableDirective>(S); 10287 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind()); 10288 } 10289 if (!OMPTeamsFound) { 10290 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams); 10291 Diag(DSAStack->getInnerTeamsRegionLoc(), 10292 diag::note_omp_nested_teams_construct_here); 10293 Diag(S->getBeginLoc(), diag::note_omp_nested_statement_here) 10294 << isa<OMPExecutableDirective>(S); 10295 return StmtError(); 10296 } 10297 } 10298 10299 setFunctionHasBranchProtectedScope(); 10300 10301 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 10302 } 10303 10304 StmtResult 10305 Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses, 10306 Stmt *AStmt, SourceLocation StartLoc, 10307 SourceLocation EndLoc) { 10308 if (!AStmt) 10309 return StmtError(); 10310 10311 auto *CS = cast<CapturedStmt>(AStmt); 10312 // 1.2.2 OpenMP Language Terminology 10313 // Structured block - An executable statement with a single entry at the 10314 // top and a single exit at the bottom. 10315 // The point of exit cannot be a branch out of the structured block. 10316 // longjmp() and throw() must not violate the entry/exit criteria. 10317 CS->getCapturedDecl()->setNothrow(); 10318 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel); 10319 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10320 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10321 // 1.2.2 OpenMP Language Terminology 10322 // Structured block - An executable statement with a single entry at the 10323 // top and a single exit at the bottom. 10324 // The point of exit cannot be a branch out of the structured block. 10325 // longjmp() and throw() must not violate the entry/exit criteria. 10326 CS->getCapturedDecl()->setNothrow(); 10327 } 10328 10329 setFunctionHasBranchProtectedScope(); 10330 10331 return OMPTargetParallelDirective::Create( 10332 Context, StartLoc, EndLoc, Clauses, AStmt, 10333 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 10334 } 10335 10336 StmtResult Sema::ActOnOpenMPTargetParallelForDirective( 10337 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10338 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10339 if (!AStmt) 10340 return StmtError(); 10341 10342 auto *CS = cast<CapturedStmt>(AStmt); 10343 // 1.2.2 OpenMP Language Terminology 10344 // Structured block - An executable statement with a single entry at the 10345 // top and a single exit at the bottom. 10346 // The point of exit cannot be a branch out of the structured block. 10347 // longjmp() and throw() must not violate the entry/exit criteria. 10348 CS->getCapturedDecl()->setNothrow(); 10349 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for); 10350 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10351 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10352 // 1.2.2 OpenMP Language Terminology 10353 // Structured block - An executable statement with a single entry at the 10354 // top and a single exit at the bottom. 10355 // The point of exit cannot be a branch out of the structured block. 10356 // longjmp() and throw() must not violate the entry/exit criteria. 10357 CS->getCapturedDecl()->setNothrow(); 10358 } 10359 10360 OMPLoopDirective::HelperExprs B; 10361 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 10362 // define the nested loops number. 10363 unsigned NestedLoopCount = 10364 checkOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses), 10365 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 10366 VarsWithImplicitDSA, B); 10367 if (NestedLoopCount == 0) 10368 return StmtError(); 10369 10370 assert((CurContext->isDependentContext() || B.builtAll()) && 10371 "omp target parallel for loop exprs were not built"); 10372 10373 if (!CurContext->isDependentContext()) { 10374 // Finalize the clauses that need pre-built expressions for CodeGen. 10375 for (OMPClause *C : Clauses) { 10376 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 10377 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 10378 B.NumIterations, *this, CurScope, 10379 DSAStack)) 10380 return StmtError(); 10381 } 10382 } 10383 10384 setFunctionHasBranchProtectedScope(); 10385 return OMPTargetParallelForDirective::Create( 10386 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 10387 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 10388 } 10389 10390 /// Check for existence of a map clause in the list of clauses. 10391 static bool hasClauses(ArrayRef<OMPClause *> Clauses, 10392 const OpenMPClauseKind K) { 10393 return llvm::any_of( 10394 Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; }); 10395 } 10396 10397 template <typename... Params> 10398 static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K, 10399 const Params... ClauseTypes) { 10400 return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...); 10401 } 10402 10403 StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses, 10404 Stmt *AStmt, 10405 SourceLocation StartLoc, 10406 SourceLocation EndLoc) { 10407 if (!AStmt) 10408 return StmtError(); 10409 10410 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10411 10412 // OpenMP [2.12.2, target data Construct, Restrictions] 10413 // At least one map, use_device_addr or use_device_ptr clause must appear on 10414 // the directive. 10415 if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr) && 10416 (LangOpts.OpenMP < 50 || !hasClauses(Clauses, OMPC_use_device_addr))) { 10417 StringRef Expected; 10418 if (LangOpts.OpenMP < 50) 10419 Expected = "'map' or 'use_device_ptr'"; 10420 else 10421 Expected = "'map', 'use_device_ptr', or 'use_device_addr'"; 10422 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 10423 << Expected << getOpenMPDirectiveName(OMPD_target_data); 10424 return StmtError(); 10425 } 10426 10427 setFunctionHasBranchProtectedScope(); 10428 10429 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 10430 AStmt); 10431 } 10432 10433 StmtResult 10434 Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses, 10435 SourceLocation StartLoc, 10436 SourceLocation EndLoc, Stmt *AStmt) { 10437 if (!AStmt) 10438 return StmtError(); 10439 10440 auto *CS = cast<CapturedStmt>(AStmt); 10441 // 1.2.2 OpenMP Language Terminology 10442 // Structured block - An executable statement with a single entry at the 10443 // top and a single exit at the bottom. 10444 // The point of exit cannot be a branch out of the structured block. 10445 // longjmp() and throw() must not violate the entry/exit criteria. 10446 CS->getCapturedDecl()->setNothrow(); 10447 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_enter_data); 10448 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10449 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10450 // 1.2.2 OpenMP Language Terminology 10451 // Structured block - An executable statement with a single entry at the 10452 // top and a single exit at the bottom. 10453 // The point of exit cannot be a branch out of the structured block. 10454 // longjmp() and throw() must not violate the entry/exit criteria. 10455 CS->getCapturedDecl()->setNothrow(); 10456 } 10457 10458 // OpenMP [2.10.2, Restrictions, p. 99] 10459 // At least one map clause must appear on the directive. 10460 if (!hasClauses(Clauses, OMPC_map)) { 10461 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 10462 << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data); 10463 return StmtError(); 10464 } 10465 10466 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 10467 AStmt); 10468 } 10469 10470 StmtResult 10471 Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses, 10472 SourceLocation StartLoc, 10473 SourceLocation EndLoc, Stmt *AStmt) { 10474 if (!AStmt) 10475 return StmtError(); 10476 10477 auto *CS = cast<CapturedStmt>(AStmt); 10478 // 1.2.2 OpenMP Language Terminology 10479 // Structured block - An executable statement with a single entry at the 10480 // top and a single exit at the bottom. 10481 // The point of exit cannot be a branch out of the structured block. 10482 // longjmp() and throw() must not violate the entry/exit criteria. 10483 CS->getCapturedDecl()->setNothrow(); 10484 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_exit_data); 10485 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10486 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10487 // 1.2.2 OpenMP Language Terminology 10488 // Structured block - An executable statement with a single entry at the 10489 // top and a single exit at the bottom. 10490 // The point of exit cannot be a branch out of the structured block. 10491 // longjmp() and throw() must not violate the entry/exit criteria. 10492 CS->getCapturedDecl()->setNothrow(); 10493 } 10494 10495 // OpenMP [2.10.3, Restrictions, p. 102] 10496 // At least one map clause must appear on the directive. 10497 if (!hasClauses(Clauses, OMPC_map)) { 10498 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 10499 << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data); 10500 return StmtError(); 10501 } 10502 10503 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 10504 AStmt); 10505 } 10506 10507 StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses, 10508 SourceLocation StartLoc, 10509 SourceLocation EndLoc, 10510 Stmt *AStmt) { 10511 if (!AStmt) 10512 return StmtError(); 10513 10514 auto *CS = cast<CapturedStmt>(AStmt); 10515 // 1.2.2 OpenMP Language Terminology 10516 // Structured block - An executable statement with a single entry at the 10517 // top and a single exit at the bottom. 10518 // The point of exit cannot be a branch out of the structured block. 10519 // longjmp() and throw() must not violate the entry/exit criteria. 10520 CS->getCapturedDecl()->setNothrow(); 10521 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_update); 10522 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10523 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10524 // 1.2.2 OpenMP Language Terminology 10525 // Structured block - An executable statement with a single entry at the 10526 // top and a single exit at the bottom. 10527 // The point of exit cannot be a branch out of the structured block. 10528 // longjmp() and throw() must not violate the entry/exit criteria. 10529 CS->getCapturedDecl()->setNothrow(); 10530 } 10531 10532 if (!hasClauses(Clauses, OMPC_to, OMPC_from)) { 10533 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required); 10534 return StmtError(); 10535 } 10536 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses, 10537 AStmt); 10538 } 10539 10540 StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses, 10541 Stmt *AStmt, SourceLocation StartLoc, 10542 SourceLocation EndLoc) { 10543 if (!AStmt) 10544 return StmtError(); 10545 10546 auto *CS = cast<CapturedStmt>(AStmt); 10547 // 1.2.2 OpenMP Language Terminology 10548 // Structured block - An executable statement with a single entry at the 10549 // top and a single exit at the bottom. 10550 // The point of exit cannot be a branch out of the structured block. 10551 // longjmp() and throw() must not violate the entry/exit criteria. 10552 CS->getCapturedDecl()->setNothrow(); 10553 10554 setFunctionHasBranchProtectedScope(); 10555 10556 DSAStack->setParentTeamsRegionLoc(StartLoc); 10557 10558 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 10559 } 10560 10561 StmtResult 10562 Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc, 10563 SourceLocation EndLoc, 10564 OpenMPDirectiveKind CancelRegion) { 10565 if (DSAStack->isParentNowaitRegion()) { 10566 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0; 10567 return StmtError(); 10568 } 10569 if (DSAStack->isParentOrderedRegion()) { 10570 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0; 10571 return StmtError(); 10572 } 10573 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc, 10574 CancelRegion); 10575 } 10576 10577 StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses, 10578 SourceLocation StartLoc, 10579 SourceLocation EndLoc, 10580 OpenMPDirectiveKind CancelRegion) { 10581 if (DSAStack->isParentNowaitRegion()) { 10582 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1; 10583 return StmtError(); 10584 } 10585 if (DSAStack->isParentOrderedRegion()) { 10586 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1; 10587 return StmtError(); 10588 } 10589 DSAStack->setParentCancelRegion(/*Cancel=*/true); 10590 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses, 10591 CancelRegion); 10592 } 10593 10594 static bool checkGrainsizeNumTasksClauses(Sema &S, 10595 ArrayRef<OMPClause *> Clauses) { 10596 const OMPClause *PrevClause = nullptr; 10597 bool ErrorFound = false; 10598 for (const OMPClause *C : Clauses) { 10599 if (C->getClauseKind() == OMPC_grainsize || 10600 C->getClauseKind() == OMPC_num_tasks) { 10601 if (!PrevClause) 10602 PrevClause = C; 10603 else if (PrevClause->getClauseKind() != C->getClauseKind()) { 10604 S.Diag(C->getBeginLoc(), diag::err_omp_clauses_mutually_exclusive) 10605 << getOpenMPClauseName(C->getClauseKind()) 10606 << getOpenMPClauseName(PrevClause->getClauseKind()); 10607 S.Diag(PrevClause->getBeginLoc(), diag::note_omp_previous_clause) 10608 << getOpenMPClauseName(PrevClause->getClauseKind()); 10609 ErrorFound = true; 10610 } 10611 } 10612 } 10613 return ErrorFound; 10614 } 10615 10616 static bool checkReductionClauseWithNogroup(Sema &S, 10617 ArrayRef<OMPClause *> Clauses) { 10618 const OMPClause *ReductionClause = nullptr; 10619 const OMPClause *NogroupClause = nullptr; 10620 for (const OMPClause *C : Clauses) { 10621 if (C->getClauseKind() == OMPC_reduction) { 10622 ReductionClause = C; 10623 if (NogroupClause) 10624 break; 10625 continue; 10626 } 10627 if (C->getClauseKind() == OMPC_nogroup) { 10628 NogroupClause = C; 10629 if (ReductionClause) 10630 break; 10631 continue; 10632 } 10633 } 10634 if (ReductionClause && NogroupClause) { 10635 S.Diag(ReductionClause->getBeginLoc(), diag::err_omp_reduction_with_nogroup) 10636 << SourceRange(NogroupClause->getBeginLoc(), 10637 NogroupClause->getEndLoc()); 10638 return true; 10639 } 10640 return false; 10641 } 10642 10643 StmtResult Sema::ActOnOpenMPTaskLoopDirective( 10644 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10645 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10646 if (!AStmt) 10647 return StmtError(); 10648 10649 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10650 OMPLoopDirective::HelperExprs B; 10651 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 10652 // define the nested loops number. 10653 unsigned NestedLoopCount = 10654 checkOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses), 10655 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 10656 VarsWithImplicitDSA, B); 10657 if (NestedLoopCount == 0) 10658 return StmtError(); 10659 10660 assert((CurContext->isDependentContext() || B.builtAll()) && 10661 "omp for loop exprs were not built"); 10662 10663 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10664 // The grainsize clause and num_tasks clause are mutually exclusive and may 10665 // not appear on the same taskloop directive. 10666 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 10667 return StmtError(); 10668 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10669 // If a reduction clause is present on the taskloop directive, the nogroup 10670 // clause must not be specified. 10671 if (checkReductionClauseWithNogroup(*this, Clauses)) 10672 return StmtError(); 10673 10674 setFunctionHasBranchProtectedScope(); 10675 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc, 10676 NestedLoopCount, Clauses, AStmt, B, 10677 DSAStack->isCancelRegion()); 10678 } 10679 10680 StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective( 10681 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10682 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10683 if (!AStmt) 10684 return StmtError(); 10685 10686 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10687 OMPLoopDirective::HelperExprs B; 10688 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 10689 // define the nested loops number. 10690 unsigned NestedLoopCount = 10691 checkOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses), 10692 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 10693 VarsWithImplicitDSA, B); 10694 if (NestedLoopCount == 0) 10695 return StmtError(); 10696 10697 assert((CurContext->isDependentContext() || B.builtAll()) && 10698 "omp for loop exprs were not built"); 10699 10700 if (!CurContext->isDependentContext()) { 10701 // Finalize the clauses that need pre-built expressions for CodeGen. 10702 for (OMPClause *C : Clauses) { 10703 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 10704 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 10705 B.NumIterations, *this, CurScope, 10706 DSAStack)) 10707 return StmtError(); 10708 } 10709 } 10710 10711 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10712 // The grainsize clause and num_tasks clause are mutually exclusive and may 10713 // not appear on the same taskloop directive. 10714 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 10715 return StmtError(); 10716 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10717 // If a reduction clause is present on the taskloop directive, the nogroup 10718 // clause must not be specified. 10719 if (checkReductionClauseWithNogroup(*this, Clauses)) 10720 return StmtError(); 10721 if (checkSimdlenSafelenSpecified(*this, Clauses)) 10722 return StmtError(); 10723 10724 setFunctionHasBranchProtectedScope(); 10725 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc, 10726 NestedLoopCount, Clauses, AStmt, B); 10727 } 10728 10729 StmtResult Sema::ActOnOpenMPMasterTaskLoopDirective( 10730 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10731 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10732 if (!AStmt) 10733 return StmtError(); 10734 10735 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10736 OMPLoopDirective::HelperExprs B; 10737 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 10738 // define the nested loops number. 10739 unsigned NestedLoopCount = 10740 checkOpenMPLoop(OMPD_master_taskloop, getCollapseNumberExpr(Clauses), 10741 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 10742 VarsWithImplicitDSA, B); 10743 if (NestedLoopCount == 0) 10744 return StmtError(); 10745 10746 assert((CurContext->isDependentContext() || B.builtAll()) && 10747 "omp for loop exprs were not built"); 10748 10749 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10750 // The grainsize clause and num_tasks clause are mutually exclusive and may 10751 // not appear on the same taskloop directive. 10752 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 10753 return StmtError(); 10754 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10755 // If a reduction clause is present on the taskloop directive, the nogroup 10756 // clause must not be specified. 10757 if (checkReductionClauseWithNogroup(*this, Clauses)) 10758 return StmtError(); 10759 10760 setFunctionHasBranchProtectedScope(); 10761 return OMPMasterTaskLoopDirective::Create(Context, StartLoc, EndLoc, 10762 NestedLoopCount, Clauses, AStmt, B, 10763 DSAStack->isCancelRegion()); 10764 } 10765 10766 StmtResult Sema::ActOnOpenMPMasterTaskLoopSimdDirective( 10767 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10768 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10769 if (!AStmt) 10770 return StmtError(); 10771 10772 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10773 OMPLoopDirective::HelperExprs B; 10774 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 10775 // define the nested loops number. 10776 unsigned NestedLoopCount = 10777 checkOpenMPLoop(OMPD_master_taskloop_simd, getCollapseNumberExpr(Clauses), 10778 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 10779 VarsWithImplicitDSA, B); 10780 if (NestedLoopCount == 0) 10781 return StmtError(); 10782 10783 assert((CurContext->isDependentContext() || B.builtAll()) && 10784 "omp for loop exprs were not built"); 10785 10786 if (!CurContext->isDependentContext()) { 10787 // Finalize the clauses that need pre-built expressions for CodeGen. 10788 for (OMPClause *C : Clauses) { 10789 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 10790 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 10791 B.NumIterations, *this, CurScope, 10792 DSAStack)) 10793 return StmtError(); 10794 } 10795 } 10796 10797 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10798 // The grainsize clause and num_tasks clause are mutually exclusive and may 10799 // not appear on the same taskloop directive. 10800 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 10801 return StmtError(); 10802 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10803 // If a reduction clause is present on the taskloop directive, the nogroup 10804 // clause must not be specified. 10805 if (checkReductionClauseWithNogroup(*this, Clauses)) 10806 return StmtError(); 10807 if (checkSimdlenSafelenSpecified(*this, Clauses)) 10808 return StmtError(); 10809 10810 setFunctionHasBranchProtectedScope(); 10811 return OMPMasterTaskLoopSimdDirective::Create( 10812 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 10813 } 10814 10815 StmtResult Sema::ActOnOpenMPParallelMasterTaskLoopDirective( 10816 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10817 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10818 if (!AStmt) 10819 return StmtError(); 10820 10821 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10822 auto *CS = cast<CapturedStmt>(AStmt); 10823 // 1.2.2 OpenMP Language Terminology 10824 // Structured block - An executable statement with a single entry at the 10825 // top and a single exit at the bottom. 10826 // The point of exit cannot be a branch out of the structured block. 10827 // longjmp() and throw() must not violate the entry/exit criteria. 10828 CS->getCapturedDecl()->setNothrow(); 10829 for (int ThisCaptureLevel = 10830 getOpenMPCaptureLevels(OMPD_parallel_master_taskloop); 10831 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10832 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10833 // 1.2.2 OpenMP Language Terminology 10834 // Structured block - An executable statement with a single entry at the 10835 // top and a single exit at the bottom. 10836 // The point of exit cannot be a branch out of the structured block. 10837 // longjmp() and throw() must not violate the entry/exit criteria. 10838 CS->getCapturedDecl()->setNothrow(); 10839 } 10840 10841 OMPLoopDirective::HelperExprs B; 10842 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 10843 // define the nested loops number. 10844 unsigned NestedLoopCount = checkOpenMPLoop( 10845 OMPD_parallel_master_taskloop, getCollapseNumberExpr(Clauses), 10846 /*OrderedLoopCountExpr=*/nullptr, CS, *this, *DSAStack, 10847 VarsWithImplicitDSA, B); 10848 if (NestedLoopCount == 0) 10849 return StmtError(); 10850 10851 assert((CurContext->isDependentContext() || B.builtAll()) && 10852 "omp for loop exprs were not built"); 10853 10854 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10855 // The grainsize clause and num_tasks clause are mutually exclusive and may 10856 // not appear on the same taskloop directive. 10857 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 10858 return StmtError(); 10859 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10860 // If a reduction clause is present on the taskloop directive, the nogroup 10861 // clause must not be specified. 10862 if (checkReductionClauseWithNogroup(*this, Clauses)) 10863 return StmtError(); 10864 10865 setFunctionHasBranchProtectedScope(); 10866 return OMPParallelMasterTaskLoopDirective::Create( 10867 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 10868 DSAStack->isCancelRegion()); 10869 } 10870 10871 StmtResult Sema::ActOnOpenMPParallelMasterTaskLoopSimdDirective( 10872 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10873 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10874 if (!AStmt) 10875 return StmtError(); 10876 10877 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10878 auto *CS = cast<CapturedStmt>(AStmt); 10879 // 1.2.2 OpenMP Language Terminology 10880 // Structured block - An executable statement with a single entry at the 10881 // top and a single exit at the bottom. 10882 // The point of exit cannot be a branch out of the structured block. 10883 // longjmp() and throw() must not violate the entry/exit criteria. 10884 CS->getCapturedDecl()->setNothrow(); 10885 for (int ThisCaptureLevel = 10886 getOpenMPCaptureLevels(OMPD_parallel_master_taskloop_simd); 10887 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10888 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10889 // 1.2.2 OpenMP Language Terminology 10890 // Structured block - An executable statement with a single entry at the 10891 // top and a single exit at the bottom. 10892 // The point of exit cannot be a branch out of the structured block. 10893 // longjmp() and throw() must not violate the entry/exit criteria. 10894 CS->getCapturedDecl()->setNothrow(); 10895 } 10896 10897 OMPLoopDirective::HelperExprs B; 10898 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 10899 // define the nested loops number. 10900 unsigned NestedLoopCount = checkOpenMPLoop( 10901 OMPD_parallel_master_taskloop_simd, getCollapseNumberExpr(Clauses), 10902 /*OrderedLoopCountExpr=*/nullptr, CS, *this, *DSAStack, 10903 VarsWithImplicitDSA, B); 10904 if (NestedLoopCount == 0) 10905 return StmtError(); 10906 10907 assert((CurContext->isDependentContext() || B.builtAll()) && 10908 "omp for loop exprs were not built"); 10909 10910 if (!CurContext->isDependentContext()) { 10911 // Finalize the clauses that need pre-built expressions for CodeGen. 10912 for (OMPClause *C : Clauses) { 10913 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 10914 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 10915 B.NumIterations, *this, CurScope, 10916 DSAStack)) 10917 return StmtError(); 10918 } 10919 } 10920 10921 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10922 // The grainsize clause and num_tasks clause are mutually exclusive and may 10923 // not appear on the same taskloop directive. 10924 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 10925 return StmtError(); 10926 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10927 // If a reduction clause is present on the taskloop directive, the nogroup 10928 // clause must not be specified. 10929 if (checkReductionClauseWithNogroup(*this, Clauses)) 10930 return StmtError(); 10931 if (checkSimdlenSafelenSpecified(*this, Clauses)) 10932 return StmtError(); 10933 10934 setFunctionHasBranchProtectedScope(); 10935 return OMPParallelMasterTaskLoopSimdDirective::Create( 10936 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 10937 } 10938 10939 StmtResult Sema::ActOnOpenMPDistributeDirective( 10940 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10941 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10942 if (!AStmt) 10943 return StmtError(); 10944 10945 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10946 OMPLoopDirective::HelperExprs B; 10947 // In presence of clause 'collapse' with number of loops, it will 10948 // define the nested loops number. 10949 unsigned NestedLoopCount = 10950 checkOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses), 10951 nullptr /*ordered not a clause on distribute*/, AStmt, 10952 *this, *DSAStack, VarsWithImplicitDSA, B); 10953 if (NestedLoopCount == 0) 10954 return StmtError(); 10955 10956 assert((CurContext->isDependentContext() || B.builtAll()) && 10957 "omp for loop exprs were not built"); 10958 10959 setFunctionHasBranchProtectedScope(); 10960 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc, 10961 NestedLoopCount, Clauses, AStmt, B); 10962 } 10963 10964 StmtResult Sema::ActOnOpenMPDistributeParallelForDirective( 10965 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10966 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10967 if (!AStmt) 10968 return StmtError(); 10969 10970 auto *CS = cast<CapturedStmt>(AStmt); 10971 // 1.2.2 OpenMP Language Terminology 10972 // Structured block - An executable statement with a single entry at the 10973 // top and a single exit at the bottom. 10974 // The point of exit cannot be a branch out of the structured block. 10975 // longjmp() and throw() must not violate the entry/exit criteria. 10976 CS->getCapturedDecl()->setNothrow(); 10977 for (int ThisCaptureLevel = 10978 getOpenMPCaptureLevels(OMPD_distribute_parallel_for); 10979 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10980 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10981 // 1.2.2 OpenMP Language Terminology 10982 // Structured block - An executable statement with a single entry at the 10983 // top and a single exit at the bottom. 10984 // The point of exit cannot be a branch out of the structured block. 10985 // longjmp() and throw() must not violate the entry/exit criteria. 10986 CS->getCapturedDecl()->setNothrow(); 10987 } 10988 10989 OMPLoopDirective::HelperExprs B; 10990 // In presence of clause 'collapse' with number of loops, it will 10991 // define the nested loops number. 10992 unsigned NestedLoopCount = checkOpenMPLoop( 10993 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses), 10994 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 10995 VarsWithImplicitDSA, B); 10996 if (NestedLoopCount == 0) 10997 return StmtError(); 10998 10999 assert((CurContext->isDependentContext() || B.builtAll()) && 11000 "omp for loop exprs were not built"); 11001 11002 setFunctionHasBranchProtectedScope(); 11003 return OMPDistributeParallelForDirective::Create( 11004 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 11005 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 11006 } 11007 11008 StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective( 11009 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11010 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11011 if (!AStmt) 11012 return StmtError(); 11013 11014 auto *CS = cast<CapturedStmt>(AStmt); 11015 // 1.2.2 OpenMP Language Terminology 11016 // Structured block - An executable statement with a single entry at the 11017 // top and a single exit at the bottom. 11018 // The point of exit cannot be a branch out of the structured block. 11019 // longjmp() and throw() must not violate the entry/exit criteria. 11020 CS->getCapturedDecl()->setNothrow(); 11021 for (int ThisCaptureLevel = 11022 getOpenMPCaptureLevels(OMPD_distribute_parallel_for_simd); 11023 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11024 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11025 // 1.2.2 OpenMP Language Terminology 11026 // Structured block - An executable statement with a single entry at the 11027 // top and a single exit at the bottom. 11028 // The point of exit cannot be a branch out of the structured block. 11029 // longjmp() and throw() must not violate the entry/exit criteria. 11030 CS->getCapturedDecl()->setNothrow(); 11031 } 11032 11033 OMPLoopDirective::HelperExprs B; 11034 // In presence of clause 'collapse' with number of loops, it will 11035 // define the nested loops number. 11036 unsigned NestedLoopCount = checkOpenMPLoop( 11037 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses), 11038 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 11039 VarsWithImplicitDSA, B); 11040 if (NestedLoopCount == 0) 11041 return StmtError(); 11042 11043 assert((CurContext->isDependentContext() || B.builtAll()) && 11044 "omp for loop exprs were not built"); 11045 11046 if (!CurContext->isDependentContext()) { 11047 // Finalize the clauses that need pre-built expressions for CodeGen. 11048 for (OMPClause *C : Clauses) { 11049 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11050 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11051 B.NumIterations, *this, CurScope, 11052 DSAStack)) 11053 return StmtError(); 11054 } 11055 } 11056 11057 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11058 return StmtError(); 11059 11060 setFunctionHasBranchProtectedScope(); 11061 return OMPDistributeParallelForSimdDirective::Create( 11062 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 11063 } 11064 11065 StmtResult Sema::ActOnOpenMPDistributeSimdDirective( 11066 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11067 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11068 if (!AStmt) 11069 return StmtError(); 11070 11071 auto *CS = cast<CapturedStmt>(AStmt); 11072 // 1.2.2 OpenMP Language Terminology 11073 // Structured block - An executable statement with a single entry at the 11074 // top and a single exit at the bottom. 11075 // The point of exit cannot be a branch out of the structured block. 11076 // longjmp() and throw() must not violate the entry/exit criteria. 11077 CS->getCapturedDecl()->setNothrow(); 11078 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_distribute_simd); 11079 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11080 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11081 // 1.2.2 OpenMP Language Terminology 11082 // Structured block - An executable statement with a single entry at the 11083 // top and a single exit at the bottom. 11084 // The point of exit cannot be a branch out of the structured block. 11085 // longjmp() and throw() must not violate the entry/exit criteria. 11086 CS->getCapturedDecl()->setNothrow(); 11087 } 11088 11089 OMPLoopDirective::HelperExprs B; 11090 // In presence of clause 'collapse' with number of loops, it will 11091 // define the nested loops number. 11092 unsigned NestedLoopCount = 11093 checkOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses), 11094 nullptr /*ordered not a clause on distribute*/, CS, *this, 11095 *DSAStack, VarsWithImplicitDSA, B); 11096 if (NestedLoopCount == 0) 11097 return StmtError(); 11098 11099 assert((CurContext->isDependentContext() || B.builtAll()) && 11100 "omp for loop exprs were not built"); 11101 11102 if (!CurContext->isDependentContext()) { 11103 // Finalize the clauses that need pre-built expressions for CodeGen. 11104 for (OMPClause *C : Clauses) { 11105 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11106 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11107 B.NumIterations, *this, CurScope, 11108 DSAStack)) 11109 return StmtError(); 11110 } 11111 } 11112 11113 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11114 return StmtError(); 11115 11116 setFunctionHasBranchProtectedScope(); 11117 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc, 11118 NestedLoopCount, Clauses, AStmt, B); 11119 } 11120 11121 StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective( 11122 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11123 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11124 if (!AStmt) 11125 return StmtError(); 11126 11127 auto *CS = cast<CapturedStmt>(AStmt); 11128 // 1.2.2 OpenMP Language Terminology 11129 // Structured block - An executable statement with a single entry at the 11130 // top and a single exit at the bottom. 11131 // The point of exit cannot be a branch out of the structured block. 11132 // longjmp() and throw() must not violate the entry/exit criteria. 11133 CS->getCapturedDecl()->setNothrow(); 11134 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for); 11135 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11136 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11137 // 1.2.2 OpenMP Language Terminology 11138 // Structured block - An executable statement with a single entry at the 11139 // top and a single exit at the bottom. 11140 // The point of exit cannot be a branch out of the structured block. 11141 // longjmp() and throw() must not violate the entry/exit criteria. 11142 CS->getCapturedDecl()->setNothrow(); 11143 } 11144 11145 OMPLoopDirective::HelperExprs B; 11146 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11147 // define the nested loops number. 11148 unsigned NestedLoopCount = checkOpenMPLoop( 11149 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses), 11150 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 11151 VarsWithImplicitDSA, B); 11152 if (NestedLoopCount == 0) 11153 return StmtError(); 11154 11155 assert((CurContext->isDependentContext() || B.builtAll()) && 11156 "omp target parallel for simd loop exprs were not built"); 11157 11158 if (!CurContext->isDependentContext()) { 11159 // Finalize the clauses that need pre-built expressions for CodeGen. 11160 for (OMPClause *C : Clauses) { 11161 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11162 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11163 B.NumIterations, *this, CurScope, 11164 DSAStack)) 11165 return StmtError(); 11166 } 11167 } 11168 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11169 return StmtError(); 11170 11171 setFunctionHasBranchProtectedScope(); 11172 return OMPTargetParallelForSimdDirective::Create( 11173 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 11174 } 11175 11176 StmtResult Sema::ActOnOpenMPTargetSimdDirective( 11177 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11178 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11179 if (!AStmt) 11180 return StmtError(); 11181 11182 auto *CS = cast<CapturedStmt>(AStmt); 11183 // 1.2.2 OpenMP Language Terminology 11184 // Structured block - An executable statement with a single entry at the 11185 // top and a single exit at the bottom. 11186 // The point of exit cannot be a branch out of the structured block. 11187 // longjmp() and throw() must not violate the entry/exit criteria. 11188 CS->getCapturedDecl()->setNothrow(); 11189 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_simd); 11190 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11191 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11192 // 1.2.2 OpenMP Language Terminology 11193 // Structured block - An executable statement with a single entry at the 11194 // top and a single exit at the bottom. 11195 // The point of exit cannot be a branch out of the structured block. 11196 // longjmp() and throw() must not violate the entry/exit criteria. 11197 CS->getCapturedDecl()->setNothrow(); 11198 } 11199 11200 OMPLoopDirective::HelperExprs B; 11201 // In presence of clause 'collapse' with number of loops, it will define the 11202 // nested loops number. 11203 unsigned NestedLoopCount = 11204 checkOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses), 11205 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 11206 VarsWithImplicitDSA, B); 11207 if (NestedLoopCount == 0) 11208 return StmtError(); 11209 11210 assert((CurContext->isDependentContext() || B.builtAll()) && 11211 "omp target simd loop exprs were not built"); 11212 11213 if (!CurContext->isDependentContext()) { 11214 // Finalize the clauses that need pre-built expressions for CodeGen. 11215 for (OMPClause *C : Clauses) { 11216 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11217 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11218 B.NumIterations, *this, CurScope, 11219 DSAStack)) 11220 return StmtError(); 11221 } 11222 } 11223 11224 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11225 return StmtError(); 11226 11227 setFunctionHasBranchProtectedScope(); 11228 return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc, 11229 NestedLoopCount, Clauses, AStmt, B); 11230 } 11231 11232 StmtResult Sema::ActOnOpenMPTeamsDistributeDirective( 11233 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11234 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11235 if (!AStmt) 11236 return StmtError(); 11237 11238 auto *CS = cast<CapturedStmt>(AStmt); 11239 // 1.2.2 OpenMP Language Terminology 11240 // Structured block - An executable statement with a single entry at the 11241 // top and a single exit at the bottom. 11242 // The point of exit cannot be a branch out of the structured block. 11243 // longjmp() and throw() must not violate the entry/exit criteria. 11244 CS->getCapturedDecl()->setNothrow(); 11245 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_teams_distribute); 11246 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11247 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11248 // 1.2.2 OpenMP Language Terminology 11249 // Structured block - An executable statement with a single entry at the 11250 // top and a single exit at the bottom. 11251 // The point of exit cannot be a branch out of the structured block. 11252 // longjmp() and throw() must not violate the entry/exit criteria. 11253 CS->getCapturedDecl()->setNothrow(); 11254 } 11255 11256 OMPLoopDirective::HelperExprs B; 11257 // In presence of clause 'collapse' with number of loops, it will 11258 // define the nested loops number. 11259 unsigned NestedLoopCount = 11260 checkOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses), 11261 nullptr /*ordered not a clause on distribute*/, CS, *this, 11262 *DSAStack, VarsWithImplicitDSA, B); 11263 if (NestedLoopCount == 0) 11264 return StmtError(); 11265 11266 assert((CurContext->isDependentContext() || B.builtAll()) && 11267 "omp teams distribute loop exprs were not built"); 11268 11269 setFunctionHasBranchProtectedScope(); 11270 11271 DSAStack->setParentTeamsRegionLoc(StartLoc); 11272 11273 return OMPTeamsDistributeDirective::Create( 11274 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 11275 } 11276 11277 StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective( 11278 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11279 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11280 if (!AStmt) 11281 return StmtError(); 11282 11283 auto *CS = cast<CapturedStmt>(AStmt); 11284 // 1.2.2 OpenMP Language Terminology 11285 // Structured block - An executable statement with a single entry at the 11286 // top and a single exit at the bottom. 11287 // The point of exit cannot be a branch out of the structured block. 11288 // longjmp() and throw() must not violate the entry/exit criteria. 11289 CS->getCapturedDecl()->setNothrow(); 11290 for (int ThisCaptureLevel = 11291 getOpenMPCaptureLevels(OMPD_teams_distribute_simd); 11292 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11293 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11294 // 1.2.2 OpenMP Language Terminology 11295 // Structured block - An executable statement with a single entry at the 11296 // top and a single exit at the bottom. 11297 // The point of exit cannot be a branch out of the structured block. 11298 // longjmp() and throw() must not violate the entry/exit criteria. 11299 CS->getCapturedDecl()->setNothrow(); 11300 } 11301 11302 OMPLoopDirective::HelperExprs B; 11303 // In presence of clause 'collapse' with number of loops, it will 11304 // define the nested loops number. 11305 unsigned NestedLoopCount = checkOpenMPLoop( 11306 OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses), 11307 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 11308 VarsWithImplicitDSA, B); 11309 11310 if (NestedLoopCount == 0) 11311 return StmtError(); 11312 11313 assert((CurContext->isDependentContext() || B.builtAll()) && 11314 "omp teams distribute simd loop exprs were not built"); 11315 11316 if (!CurContext->isDependentContext()) { 11317 // Finalize the clauses that need pre-built expressions for CodeGen. 11318 for (OMPClause *C : Clauses) { 11319 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11320 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11321 B.NumIterations, *this, CurScope, 11322 DSAStack)) 11323 return StmtError(); 11324 } 11325 } 11326 11327 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11328 return StmtError(); 11329 11330 setFunctionHasBranchProtectedScope(); 11331 11332 DSAStack->setParentTeamsRegionLoc(StartLoc); 11333 11334 return OMPTeamsDistributeSimdDirective::Create( 11335 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 11336 } 11337 11338 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective( 11339 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11340 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11341 if (!AStmt) 11342 return StmtError(); 11343 11344 auto *CS = cast<CapturedStmt>(AStmt); 11345 // 1.2.2 OpenMP Language Terminology 11346 // Structured block - An executable statement with a single entry at the 11347 // top and a single exit at the bottom. 11348 // The point of exit cannot be a branch out of the structured block. 11349 // longjmp() and throw() must not violate the entry/exit criteria. 11350 CS->getCapturedDecl()->setNothrow(); 11351 11352 for (int ThisCaptureLevel = 11353 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for_simd); 11354 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11355 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11356 // 1.2.2 OpenMP Language Terminology 11357 // Structured block - An executable statement with a single entry at the 11358 // top and a single exit at the bottom. 11359 // The point of exit cannot be a branch out of the structured block. 11360 // longjmp() and throw() must not violate the entry/exit criteria. 11361 CS->getCapturedDecl()->setNothrow(); 11362 } 11363 11364 OMPLoopDirective::HelperExprs B; 11365 // In presence of clause 'collapse' with number of loops, it will 11366 // define the nested loops number. 11367 unsigned NestedLoopCount = checkOpenMPLoop( 11368 OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses), 11369 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 11370 VarsWithImplicitDSA, B); 11371 11372 if (NestedLoopCount == 0) 11373 return StmtError(); 11374 11375 assert((CurContext->isDependentContext() || B.builtAll()) && 11376 "omp for loop exprs were not built"); 11377 11378 if (!CurContext->isDependentContext()) { 11379 // Finalize the clauses that need pre-built expressions for CodeGen. 11380 for (OMPClause *C : Clauses) { 11381 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11382 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11383 B.NumIterations, *this, CurScope, 11384 DSAStack)) 11385 return StmtError(); 11386 } 11387 } 11388 11389 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11390 return StmtError(); 11391 11392 setFunctionHasBranchProtectedScope(); 11393 11394 DSAStack->setParentTeamsRegionLoc(StartLoc); 11395 11396 return OMPTeamsDistributeParallelForSimdDirective::Create( 11397 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 11398 } 11399 11400 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective( 11401 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11402 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11403 if (!AStmt) 11404 return StmtError(); 11405 11406 auto *CS = cast<CapturedStmt>(AStmt); 11407 // 1.2.2 OpenMP Language Terminology 11408 // Structured block - An executable statement with a single entry at the 11409 // top and a single exit at the bottom. 11410 // The point of exit cannot be a branch out of the structured block. 11411 // longjmp() and throw() must not violate the entry/exit criteria. 11412 CS->getCapturedDecl()->setNothrow(); 11413 11414 for (int ThisCaptureLevel = 11415 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for); 11416 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11417 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11418 // 1.2.2 OpenMP Language Terminology 11419 // Structured block - An executable statement with a single entry at the 11420 // top and a single exit at the bottom. 11421 // The point of exit cannot be a branch out of the structured block. 11422 // longjmp() and throw() must not violate the entry/exit criteria. 11423 CS->getCapturedDecl()->setNothrow(); 11424 } 11425 11426 OMPLoopDirective::HelperExprs B; 11427 // In presence of clause 'collapse' with number of loops, it will 11428 // define the nested loops number. 11429 unsigned NestedLoopCount = checkOpenMPLoop( 11430 OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses), 11431 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 11432 VarsWithImplicitDSA, B); 11433 11434 if (NestedLoopCount == 0) 11435 return StmtError(); 11436 11437 assert((CurContext->isDependentContext() || B.builtAll()) && 11438 "omp for loop exprs were not built"); 11439 11440 setFunctionHasBranchProtectedScope(); 11441 11442 DSAStack->setParentTeamsRegionLoc(StartLoc); 11443 11444 return OMPTeamsDistributeParallelForDirective::Create( 11445 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 11446 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 11447 } 11448 11449 StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses, 11450 Stmt *AStmt, 11451 SourceLocation StartLoc, 11452 SourceLocation EndLoc) { 11453 if (!AStmt) 11454 return StmtError(); 11455 11456 auto *CS = cast<CapturedStmt>(AStmt); 11457 // 1.2.2 OpenMP Language Terminology 11458 // Structured block - An executable statement with a single entry at the 11459 // top and a single exit at the bottom. 11460 // The point of exit cannot be a branch out of the structured block. 11461 // longjmp() and throw() must not violate the entry/exit criteria. 11462 CS->getCapturedDecl()->setNothrow(); 11463 11464 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_teams); 11465 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11466 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11467 // 1.2.2 OpenMP Language Terminology 11468 // Structured block - An executable statement with a single entry at the 11469 // top and a single exit at the bottom. 11470 // The point of exit cannot be a branch out of the structured block. 11471 // longjmp() and throw() must not violate the entry/exit criteria. 11472 CS->getCapturedDecl()->setNothrow(); 11473 } 11474 setFunctionHasBranchProtectedScope(); 11475 11476 return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, 11477 AStmt); 11478 } 11479 11480 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective( 11481 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11482 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11483 if (!AStmt) 11484 return StmtError(); 11485 11486 auto *CS = cast<CapturedStmt>(AStmt); 11487 // 1.2.2 OpenMP Language Terminology 11488 // Structured block - An executable statement with a single entry at the 11489 // top and a single exit at the bottom. 11490 // The point of exit cannot be a branch out of the structured block. 11491 // longjmp() and throw() must not violate the entry/exit criteria. 11492 CS->getCapturedDecl()->setNothrow(); 11493 for (int ThisCaptureLevel = 11494 getOpenMPCaptureLevels(OMPD_target_teams_distribute); 11495 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11496 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11497 // 1.2.2 OpenMP Language Terminology 11498 // Structured block - An executable statement with a single entry at the 11499 // top and a single exit at the bottom. 11500 // The point of exit cannot be a branch out of the structured block. 11501 // longjmp() and throw() must not violate the entry/exit criteria. 11502 CS->getCapturedDecl()->setNothrow(); 11503 } 11504 11505 OMPLoopDirective::HelperExprs B; 11506 // In presence of clause 'collapse' with number of loops, it will 11507 // define the nested loops number. 11508 unsigned NestedLoopCount = checkOpenMPLoop( 11509 OMPD_target_teams_distribute, getCollapseNumberExpr(Clauses), 11510 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 11511 VarsWithImplicitDSA, B); 11512 if (NestedLoopCount == 0) 11513 return StmtError(); 11514 11515 assert((CurContext->isDependentContext() || B.builtAll()) && 11516 "omp target teams distribute loop exprs were not built"); 11517 11518 setFunctionHasBranchProtectedScope(); 11519 return OMPTargetTeamsDistributeDirective::Create( 11520 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 11521 } 11522 11523 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective( 11524 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11525 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11526 if (!AStmt) 11527 return StmtError(); 11528 11529 auto *CS = cast<CapturedStmt>(AStmt); 11530 // 1.2.2 OpenMP Language Terminology 11531 // Structured block - An executable statement with a single entry at the 11532 // top and a single exit at the bottom. 11533 // The point of exit cannot be a branch out of the structured block. 11534 // longjmp() and throw() must not violate the entry/exit criteria. 11535 CS->getCapturedDecl()->setNothrow(); 11536 for (int ThisCaptureLevel = 11537 getOpenMPCaptureLevels(OMPD_target_teams_distribute_parallel_for); 11538 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11539 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11540 // 1.2.2 OpenMP Language Terminology 11541 // Structured block - An executable statement with a single entry at the 11542 // top and a single exit at the bottom. 11543 // The point of exit cannot be a branch out of the structured block. 11544 // longjmp() and throw() must not violate the entry/exit criteria. 11545 CS->getCapturedDecl()->setNothrow(); 11546 } 11547 11548 OMPLoopDirective::HelperExprs B; 11549 // In presence of clause 'collapse' with number of loops, it will 11550 // define the nested loops number. 11551 unsigned NestedLoopCount = checkOpenMPLoop( 11552 OMPD_target_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses), 11553 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 11554 VarsWithImplicitDSA, B); 11555 if (NestedLoopCount == 0) 11556 return StmtError(); 11557 11558 assert((CurContext->isDependentContext() || B.builtAll()) && 11559 "omp target teams distribute parallel for loop exprs were not built"); 11560 11561 if (!CurContext->isDependentContext()) { 11562 // Finalize the clauses that need pre-built expressions for CodeGen. 11563 for (OMPClause *C : Clauses) { 11564 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11565 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11566 B.NumIterations, *this, CurScope, 11567 DSAStack)) 11568 return StmtError(); 11569 } 11570 } 11571 11572 setFunctionHasBranchProtectedScope(); 11573 return OMPTargetTeamsDistributeParallelForDirective::Create( 11574 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 11575 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 11576 } 11577 11578 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective( 11579 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11580 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11581 if (!AStmt) 11582 return StmtError(); 11583 11584 auto *CS = cast<CapturedStmt>(AStmt); 11585 // 1.2.2 OpenMP Language Terminology 11586 // Structured block - An executable statement with a single entry at the 11587 // top and a single exit at the bottom. 11588 // The point of exit cannot be a branch out of the structured block. 11589 // longjmp() and throw() must not violate the entry/exit criteria. 11590 CS->getCapturedDecl()->setNothrow(); 11591 for (int ThisCaptureLevel = getOpenMPCaptureLevels( 11592 OMPD_target_teams_distribute_parallel_for_simd); 11593 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11594 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11595 // 1.2.2 OpenMP Language Terminology 11596 // Structured block - An executable statement with a single entry at the 11597 // top and a single exit at the bottom. 11598 // The point of exit cannot be a branch out of the structured block. 11599 // longjmp() and throw() must not violate the entry/exit criteria. 11600 CS->getCapturedDecl()->setNothrow(); 11601 } 11602 11603 OMPLoopDirective::HelperExprs B; 11604 // In presence of clause 'collapse' with number of loops, it will 11605 // define the nested loops number. 11606 unsigned NestedLoopCount = 11607 checkOpenMPLoop(OMPD_target_teams_distribute_parallel_for_simd, 11608 getCollapseNumberExpr(Clauses), 11609 nullptr /*ordered not a clause on distribute*/, CS, *this, 11610 *DSAStack, VarsWithImplicitDSA, B); 11611 if (NestedLoopCount == 0) 11612 return StmtError(); 11613 11614 assert((CurContext->isDependentContext() || B.builtAll()) && 11615 "omp target teams distribute parallel for simd loop exprs were not " 11616 "built"); 11617 11618 if (!CurContext->isDependentContext()) { 11619 // Finalize the clauses that need pre-built expressions for CodeGen. 11620 for (OMPClause *C : Clauses) { 11621 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11622 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11623 B.NumIterations, *this, CurScope, 11624 DSAStack)) 11625 return StmtError(); 11626 } 11627 } 11628 11629 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11630 return StmtError(); 11631 11632 setFunctionHasBranchProtectedScope(); 11633 return OMPTargetTeamsDistributeParallelForSimdDirective::Create( 11634 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 11635 } 11636 11637 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective( 11638 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11639 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11640 if (!AStmt) 11641 return StmtError(); 11642 11643 auto *CS = cast<CapturedStmt>(AStmt); 11644 // 1.2.2 OpenMP Language Terminology 11645 // Structured block - An executable statement with a single entry at the 11646 // top and a single exit at the bottom. 11647 // The point of exit cannot be a branch out of the structured block. 11648 // longjmp() and throw() must not violate the entry/exit criteria. 11649 CS->getCapturedDecl()->setNothrow(); 11650 for (int ThisCaptureLevel = 11651 getOpenMPCaptureLevels(OMPD_target_teams_distribute_simd); 11652 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11653 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11654 // 1.2.2 OpenMP Language Terminology 11655 // Structured block - An executable statement with a single entry at the 11656 // top and a single exit at the bottom. 11657 // The point of exit cannot be a branch out of the structured block. 11658 // longjmp() and throw() must not violate the entry/exit criteria. 11659 CS->getCapturedDecl()->setNothrow(); 11660 } 11661 11662 OMPLoopDirective::HelperExprs B; 11663 // In presence of clause 'collapse' with number of loops, it will 11664 // define the nested loops number. 11665 unsigned NestedLoopCount = checkOpenMPLoop( 11666 OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses), 11667 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 11668 VarsWithImplicitDSA, B); 11669 if (NestedLoopCount == 0) 11670 return StmtError(); 11671 11672 assert((CurContext->isDependentContext() || B.builtAll()) && 11673 "omp target teams distribute simd loop exprs were not built"); 11674 11675 if (!CurContext->isDependentContext()) { 11676 // Finalize the clauses that need pre-built expressions for CodeGen. 11677 for (OMPClause *C : Clauses) { 11678 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11679 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11680 B.NumIterations, *this, CurScope, 11681 DSAStack)) 11682 return StmtError(); 11683 } 11684 } 11685 11686 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11687 return StmtError(); 11688 11689 setFunctionHasBranchProtectedScope(); 11690 return OMPTargetTeamsDistributeSimdDirective::Create( 11691 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 11692 } 11693 11694 OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr, 11695 SourceLocation StartLoc, 11696 SourceLocation LParenLoc, 11697 SourceLocation EndLoc) { 11698 OMPClause *Res = nullptr; 11699 switch (Kind) { 11700 case OMPC_final: 11701 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc); 11702 break; 11703 case OMPC_num_threads: 11704 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc); 11705 break; 11706 case OMPC_safelen: 11707 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc); 11708 break; 11709 case OMPC_simdlen: 11710 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc); 11711 break; 11712 case OMPC_allocator: 11713 Res = ActOnOpenMPAllocatorClause(Expr, StartLoc, LParenLoc, EndLoc); 11714 break; 11715 case OMPC_collapse: 11716 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc); 11717 break; 11718 case OMPC_ordered: 11719 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr); 11720 break; 11721 case OMPC_num_teams: 11722 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc); 11723 break; 11724 case OMPC_thread_limit: 11725 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc); 11726 break; 11727 case OMPC_priority: 11728 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc); 11729 break; 11730 case OMPC_grainsize: 11731 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc); 11732 break; 11733 case OMPC_num_tasks: 11734 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc); 11735 break; 11736 case OMPC_hint: 11737 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc); 11738 break; 11739 case OMPC_depobj: 11740 Res = ActOnOpenMPDepobjClause(Expr, StartLoc, LParenLoc, EndLoc); 11741 break; 11742 case OMPC_detach: 11743 Res = ActOnOpenMPDetachClause(Expr, StartLoc, LParenLoc, EndLoc); 11744 break; 11745 case OMPC_device: 11746 case OMPC_if: 11747 case OMPC_default: 11748 case OMPC_proc_bind: 11749 case OMPC_schedule: 11750 case OMPC_private: 11751 case OMPC_firstprivate: 11752 case OMPC_lastprivate: 11753 case OMPC_shared: 11754 case OMPC_reduction: 11755 case OMPC_task_reduction: 11756 case OMPC_in_reduction: 11757 case OMPC_linear: 11758 case OMPC_aligned: 11759 case OMPC_copyin: 11760 case OMPC_copyprivate: 11761 case OMPC_nowait: 11762 case OMPC_untied: 11763 case OMPC_mergeable: 11764 case OMPC_threadprivate: 11765 case OMPC_allocate: 11766 case OMPC_flush: 11767 case OMPC_read: 11768 case OMPC_write: 11769 case OMPC_update: 11770 case OMPC_capture: 11771 case OMPC_seq_cst: 11772 case OMPC_acq_rel: 11773 case OMPC_acquire: 11774 case OMPC_release: 11775 case OMPC_relaxed: 11776 case OMPC_depend: 11777 case OMPC_threads: 11778 case OMPC_simd: 11779 case OMPC_map: 11780 case OMPC_nogroup: 11781 case OMPC_dist_schedule: 11782 case OMPC_defaultmap: 11783 case OMPC_unknown: 11784 case OMPC_uniform: 11785 case OMPC_to: 11786 case OMPC_from: 11787 case OMPC_use_device_ptr: 11788 case OMPC_use_device_addr: 11789 case OMPC_is_device_ptr: 11790 case OMPC_unified_address: 11791 case OMPC_unified_shared_memory: 11792 case OMPC_reverse_offload: 11793 case OMPC_dynamic_allocators: 11794 case OMPC_atomic_default_mem_order: 11795 case OMPC_device_type: 11796 case OMPC_match: 11797 case OMPC_nontemporal: 11798 case OMPC_order: 11799 case OMPC_destroy: 11800 case OMPC_inclusive: 11801 case OMPC_exclusive: 11802 case OMPC_uses_allocators: 11803 case OMPC_affinity: 11804 default: 11805 llvm_unreachable("Clause is not allowed."); 11806 } 11807 return Res; 11808 } 11809 11810 // An OpenMP directive such as 'target parallel' has two captured regions: 11811 // for the 'target' and 'parallel' respectively. This function returns 11812 // the region in which to capture expressions associated with a clause. 11813 // A return value of OMPD_unknown signifies that the expression should not 11814 // be captured. 11815 static OpenMPDirectiveKind getOpenMPCaptureRegionForClause( 11816 OpenMPDirectiveKind DKind, OpenMPClauseKind CKind, unsigned OpenMPVersion, 11817 OpenMPDirectiveKind NameModifier = OMPD_unknown) { 11818 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 11819 switch (CKind) { 11820 case OMPC_if: 11821 switch (DKind) { 11822 case OMPD_target_parallel_for_simd: 11823 if (OpenMPVersion >= 50 && 11824 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) { 11825 CaptureRegion = OMPD_parallel; 11826 break; 11827 } 11828 LLVM_FALLTHROUGH; 11829 case OMPD_target_parallel: 11830 case OMPD_target_parallel_for: 11831 // If this clause applies to the nested 'parallel' region, capture within 11832 // the 'target' region, otherwise do not capture. 11833 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel) 11834 CaptureRegion = OMPD_target; 11835 break; 11836 case OMPD_target_teams_distribute_parallel_for_simd: 11837 if (OpenMPVersion >= 50 && 11838 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) { 11839 CaptureRegion = OMPD_parallel; 11840 break; 11841 } 11842 LLVM_FALLTHROUGH; 11843 case OMPD_target_teams_distribute_parallel_for: 11844 // If this clause applies to the nested 'parallel' region, capture within 11845 // the 'teams' region, otherwise do not capture. 11846 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel) 11847 CaptureRegion = OMPD_teams; 11848 break; 11849 case OMPD_teams_distribute_parallel_for_simd: 11850 if (OpenMPVersion >= 50 && 11851 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) { 11852 CaptureRegion = OMPD_parallel; 11853 break; 11854 } 11855 LLVM_FALLTHROUGH; 11856 case OMPD_teams_distribute_parallel_for: 11857 CaptureRegion = OMPD_teams; 11858 break; 11859 case OMPD_target_update: 11860 case OMPD_target_enter_data: 11861 case OMPD_target_exit_data: 11862 CaptureRegion = OMPD_task; 11863 break; 11864 case OMPD_parallel_master_taskloop: 11865 if (NameModifier == OMPD_unknown || NameModifier == OMPD_taskloop) 11866 CaptureRegion = OMPD_parallel; 11867 break; 11868 case OMPD_parallel_master_taskloop_simd: 11869 if ((OpenMPVersion <= 45 && NameModifier == OMPD_unknown) || 11870 NameModifier == OMPD_taskloop) { 11871 CaptureRegion = OMPD_parallel; 11872 break; 11873 } 11874 if (OpenMPVersion <= 45) 11875 break; 11876 if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd) 11877 CaptureRegion = OMPD_taskloop; 11878 break; 11879 case OMPD_parallel_for_simd: 11880 if (OpenMPVersion <= 45) 11881 break; 11882 if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd) 11883 CaptureRegion = OMPD_parallel; 11884 break; 11885 case OMPD_taskloop_simd: 11886 case OMPD_master_taskloop_simd: 11887 if (OpenMPVersion <= 45) 11888 break; 11889 if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd) 11890 CaptureRegion = OMPD_taskloop; 11891 break; 11892 case OMPD_distribute_parallel_for_simd: 11893 if (OpenMPVersion <= 45) 11894 break; 11895 if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd) 11896 CaptureRegion = OMPD_parallel; 11897 break; 11898 case OMPD_target_simd: 11899 if (OpenMPVersion >= 50 && 11900 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) 11901 CaptureRegion = OMPD_target; 11902 break; 11903 case OMPD_teams_distribute_simd: 11904 case OMPD_target_teams_distribute_simd: 11905 if (OpenMPVersion >= 50 && 11906 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) 11907 CaptureRegion = OMPD_teams; 11908 break; 11909 case OMPD_cancel: 11910 case OMPD_parallel: 11911 case OMPD_parallel_master: 11912 case OMPD_parallel_sections: 11913 case OMPD_parallel_for: 11914 case OMPD_target: 11915 case OMPD_target_teams: 11916 case OMPD_target_teams_distribute: 11917 case OMPD_distribute_parallel_for: 11918 case OMPD_task: 11919 case OMPD_taskloop: 11920 case OMPD_master_taskloop: 11921 case OMPD_target_data: 11922 case OMPD_simd: 11923 case OMPD_for_simd: 11924 case OMPD_distribute_simd: 11925 // Do not capture if-clause expressions. 11926 break; 11927 case OMPD_threadprivate: 11928 case OMPD_allocate: 11929 case OMPD_taskyield: 11930 case OMPD_barrier: 11931 case OMPD_taskwait: 11932 case OMPD_cancellation_point: 11933 case OMPD_flush: 11934 case OMPD_depobj: 11935 case OMPD_scan: 11936 case OMPD_declare_reduction: 11937 case OMPD_declare_mapper: 11938 case OMPD_declare_simd: 11939 case OMPD_declare_variant: 11940 case OMPD_begin_declare_variant: 11941 case OMPD_end_declare_variant: 11942 case OMPD_declare_target: 11943 case OMPD_end_declare_target: 11944 case OMPD_teams: 11945 case OMPD_for: 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_teams_distribute: 11956 case OMPD_requires: 11957 llvm_unreachable("Unexpected OpenMP directive with if-clause"); 11958 case OMPD_unknown: 11959 default: 11960 llvm_unreachable("Unknown OpenMP directive"); 11961 } 11962 break; 11963 case OMPC_num_threads: 11964 switch (DKind) { 11965 case OMPD_target_parallel: 11966 case OMPD_target_parallel_for: 11967 case OMPD_target_parallel_for_simd: 11968 CaptureRegion = OMPD_target; 11969 break; 11970 case OMPD_teams_distribute_parallel_for: 11971 case OMPD_teams_distribute_parallel_for_simd: 11972 case OMPD_target_teams_distribute_parallel_for: 11973 case OMPD_target_teams_distribute_parallel_for_simd: 11974 CaptureRegion = OMPD_teams; 11975 break; 11976 case OMPD_parallel: 11977 case OMPD_parallel_master: 11978 case OMPD_parallel_sections: 11979 case OMPD_parallel_for: 11980 case OMPD_parallel_for_simd: 11981 case OMPD_distribute_parallel_for: 11982 case OMPD_distribute_parallel_for_simd: 11983 case OMPD_parallel_master_taskloop: 11984 case OMPD_parallel_master_taskloop_simd: 11985 // Do not capture num_threads-clause expressions. 11986 break; 11987 case OMPD_target_data: 11988 case OMPD_target_enter_data: 11989 case OMPD_target_exit_data: 11990 case OMPD_target_update: 11991 case OMPD_target: 11992 case OMPD_target_simd: 11993 case OMPD_target_teams: 11994 case OMPD_target_teams_distribute: 11995 case OMPD_target_teams_distribute_simd: 11996 case OMPD_cancel: 11997 case OMPD_task: 11998 case OMPD_taskloop: 11999 case OMPD_taskloop_simd: 12000 case OMPD_master_taskloop: 12001 case OMPD_master_taskloop_simd: 12002 case OMPD_threadprivate: 12003 case OMPD_allocate: 12004 case OMPD_taskyield: 12005 case OMPD_barrier: 12006 case OMPD_taskwait: 12007 case OMPD_cancellation_point: 12008 case OMPD_flush: 12009 case OMPD_depobj: 12010 case OMPD_scan: 12011 case OMPD_declare_reduction: 12012 case OMPD_declare_mapper: 12013 case OMPD_declare_simd: 12014 case OMPD_declare_variant: 12015 case OMPD_begin_declare_variant: 12016 case OMPD_end_declare_variant: 12017 case OMPD_declare_target: 12018 case OMPD_end_declare_target: 12019 case OMPD_teams: 12020 case OMPD_simd: 12021 case OMPD_for: 12022 case OMPD_for_simd: 12023 case OMPD_sections: 12024 case OMPD_section: 12025 case OMPD_single: 12026 case OMPD_master: 12027 case OMPD_critical: 12028 case OMPD_taskgroup: 12029 case OMPD_distribute: 12030 case OMPD_ordered: 12031 case OMPD_atomic: 12032 case OMPD_distribute_simd: 12033 case OMPD_teams_distribute: 12034 case OMPD_teams_distribute_simd: 12035 case OMPD_requires: 12036 llvm_unreachable("Unexpected OpenMP directive with num_threads-clause"); 12037 case OMPD_unknown: 12038 default: 12039 llvm_unreachable("Unknown OpenMP directive"); 12040 } 12041 break; 12042 case OMPC_num_teams: 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 num_teams-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 num_teams-clause"); 12114 case OMPD_unknown: 12115 default: 12116 llvm_unreachable("Unknown OpenMP directive"); 12117 } 12118 break; 12119 case OMPC_thread_limit: 12120 switch (DKind) { 12121 case OMPD_target_teams: 12122 case OMPD_target_teams_distribute: 12123 case OMPD_target_teams_distribute_simd: 12124 case OMPD_target_teams_distribute_parallel_for: 12125 case OMPD_target_teams_distribute_parallel_for_simd: 12126 CaptureRegion = OMPD_target; 12127 break; 12128 case OMPD_teams_distribute_parallel_for: 12129 case OMPD_teams_distribute_parallel_for_simd: 12130 case OMPD_teams: 12131 case OMPD_teams_distribute: 12132 case OMPD_teams_distribute_simd: 12133 // Do not capture thread_limit-clause expressions. 12134 break; 12135 case OMPD_distribute_parallel_for: 12136 case OMPD_distribute_parallel_for_simd: 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_cancel: 12149 case OMPD_parallel: 12150 case OMPD_parallel_master: 12151 case OMPD_parallel_sections: 12152 case OMPD_parallel_for: 12153 case OMPD_parallel_for_simd: 12154 case OMPD_target: 12155 case OMPD_target_simd: 12156 case OMPD_target_parallel: 12157 case OMPD_target_parallel_for: 12158 case OMPD_target_parallel_for_simd: 12159 case OMPD_threadprivate: 12160 case OMPD_allocate: 12161 case OMPD_taskyield: 12162 case OMPD_barrier: 12163 case OMPD_taskwait: 12164 case OMPD_cancellation_point: 12165 case OMPD_flush: 12166 case OMPD_depobj: 12167 case OMPD_scan: 12168 case OMPD_declare_reduction: 12169 case OMPD_declare_mapper: 12170 case OMPD_declare_simd: 12171 case OMPD_declare_variant: 12172 case OMPD_begin_declare_variant: 12173 case OMPD_end_declare_variant: 12174 case OMPD_declare_target: 12175 case OMPD_end_declare_target: 12176 case OMPD_simd: 12177 case OMPD_for: 12178 case OMPD_for_simd: 12179 case OMPD_sections: 12180 case OMPD_section: 12181 case OMPD_single: 12182 case OMPD_master: 12183 case OMPD_critical: 12184 case OMPD_taskgroup: 12185 case OMPD_distribute: 12186 case OMPD_ordered: 12187 case OMPD_atomic: 12188 case OMPD_distribute_simd: 12189 case OMPD_requires: 12190 llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause"); 12191 case OMPD_unknown: 12192 default: 12193 llvm_unreachable("Unknown OpenMP directive"); 12194 } 12195 break; 12196 case OMPC_schedule: 12197 switch (DKind) { 12198 case OMPD_parallel_for: 12199 case OMPD_parallel_for_simd: 12200 case OMPD_distribute_parallel_for: 12201 case OMPD_distribute_parallel_for_simd: 12202 case OMPD_teams_distribute_parallel_for: 12203 case OMPD_teams_distribute_parallel_for_simd: 12204 case OMPD_target_parallel_for: 12205 case OMPD_target_parallel_for_simd: 12206 case OMPD_target_teams_distribute_parallel_for: 12207 case OMPD_target_teams_distribute_parallel_for_simd: 12208 CaptureRegion = OMPD_parallel; 12209 break; 12210 case OMPD_for: 12211 case OMPD_for_simd: 12212 // Do not capture schedule-clause expressions. 12213 break; 12214 case OMPD_task: 12215 case OMPD_taskloop: 12216 case OMPD_taskloop_simd: 12217 case OMPD_master_taskloop: 12218 case OMPD_master_taskloop_simd: 12219 case OMPD_parallel_master_taskloop: 12220 case OMPD_parallel_master_taskloop_simd: 12221 case OMPD_target_data: 12222 case OMPD_target_enter_data: 12223 case OMPD_target_exit_data: 12224 case OMPD_target_update: 12225 case OMPD_teams: 12226 case OMPD_teams_distribute: 12227 case OMPD_teams_distribute_simd: 12228 case OMPD_target_teams_distribute: 12229 case OMPD_target_teams_distribute_simd: 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_sections: 12256 case OMPD_section: 12257 case OMPD_single: 12258 case OMPD_master: 12259 case OMPD_critical: 12260 case OMPD_taskgroup: 12261 case OMPD_distribute: 12262 case OMPD_ordered: 12263 case OMPD_atomic: 12264 case OMPD_distribute_simd: 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_dist_schedule: 12274 switch (DKind) { 12275 case OMPD_teams_distribute_parallel_for: 12276 case OMPD_teams_distribute_parallel_for_simd: 12277 case OMPD_teams_distribute: 12278 case OMPD_teams_distribute_simd: 12279 case OMPD_target_teams_distribute_parallel_for: 12280 case OMPD_target_teams_distribute_parallel_for_simd: 12281 case OMPD_target_teams_distribute: 12282 case OMPD_target_teams_distribute_simd: 12283 CaptureRegion = OMPD_teams; 12284 break; 12285 case OMPD_distribute_parallel_for: 12286 case OMPD_distribute_parallel_for_simd: 12287 case OMPD_distribute: 12288 case OMPD_distribute_simd: 12289 // Do not capture thread_limit-clause expressions. 12290 break; 12291 case OMPD_parallel_for: 12292 case OMPD_parallel_for_simd: 12293 case OMPD_target_parallel_for_simd: 12294 case OMPD_target_parallel_for: 12295 case OMPD_task: 12296 case OMPD_taskloop: 12297 case OMPD_taskloop_simd: 12298 case OMPD_master_taskloop: 12299 case OMPD_master_taskloop_simd: 12300 case OMPD_parallel_master_taskloop: 12301 case OMPD_parallel_master_taskloop_simd: 12302 case OMPD_target_data: 12303 case OMPD_target_enter_data: 12304 case OMPD_target_exit_data: 12305 case OMPD_target_update: 12306 case OMPD_teams: 12307 case OMPD_target: 12308 case OMPD_target_simd: 12309 case OMPD_target_parallel: 12310 case OMPD_cancel: 12311 case OMPD_parallel: 12312 case OMPD_parallel_master: 12313 case OMPD_parallel_sections: 12314 case OMPD_threadprivate: 12315 case OMPD_allocate: 12316 case OMPD_taskyield: 12317 case OMPD_barrier: 12318 case OMPD_taskwait: 12319 case OMPD_cancellation_point: 12320 case OMPD_flush: 12321 case OMPD_depobj: 12322 case OMPD_scan: 12323 case OMPD_declare_reduction: 12324 case OMPD_declare_mapper: 12325 case OMPD_declare_simd: 12326 case OMPD_declare_variant: 12327 case OMPD_begin_declare_variant: 12328 case OMPD_end_declare_variant: 12329 case OMPD_declare_target: 12330 case OMPD_end_declare_target: 12331 case OMPD_simd: 12332 case OMPD_for: 12333 case OMPD_for_simd: 12334 case OMPD_sections: 12335 case OMPD_section: 12336 case OMPD_single: 12337 case OMPD_master: 12338 case OMPD_critical: 12339 case OMPD_taskgroup: 12340 case OMPD_ordered: 12341 case OMPD_atomic: 12342 case OMPD_target_teams: 12343 case OMPD_requires: 12344 llvm_unreachable("Unexpected OpenMP directive with schedule clause"); 12345 case OMPD_unknown: 12346 default: 12347 llvm_unreachable("Unknown OpenMP directive"); 12348 } 12349 break; 12350 case OMPC_device: 12351 switch (DKind) { 12352 case OMPD_target_update: 12353 case OMPD_target_enter_data: 12354 case OMPD_target_exit_data: 12355 case OMPD_target: 12356 case OMPD_target_simd: 12357 case OMPD_target_teams: 12358 case OMPD_target_parallel: 12359 case OMPD_target_teams_distribute: 12360 case OMPD_target_teams_distribute_simd: 12361 case OMPD_target_parallel_for: 12362 case OMPD_target_parallel_for_simd: 12363 case OMPD_target_teams_distribute_parallel_for: 12364 case OMPD_target_teams_distribute_parallel_for_simd: 12365 CaptureRegion = OMPD_task; 12366 break; 12367 case OMPD_target_data: 12368 // Do not capture device-clause expressions. 12369 break; 12370 case OMPD_teams_distribute_parallel_for: 12371 case OMPD_teams_distribute_parallel_for_simd: 12372 case OMPD_teams: 12373 case OMPD_teams_distribute: 12374 case OMPD_teams_distribute_simd: 12375 case OMPD_distribute_parallel_for: 12376 case OMPD_distribute_parallel_for_simd: 12377 case OMPD_task: 12378 case OMPD_taskloop: 12379 case OMPD_taskloop_simd: 12380 case OMPD_master_taskloop: 12381 case OMPD_master_taskloop_simd: 12382 case OMPD_parallel_master_taskloop: 12383 case OMPD_parallel_master_taskloop_simd: 12384 case OMPD_cancel: 12385 case OMPD_parallel: 12386 case OMPD_parallel_master: 12387 case OMPD_parallel_sections: 12388 case OMPD_parallel_for: 12389 case OMPD_parallel_for_simd: 12390 case OMPD_threadprivate: 12391 case OMPD_allocate: 12392 case OMPD_taskyield: 12393 case OMPD_barrier: 12394 case OMPD_taskwait: 12395 case OMPD_cancellation_point: 12396 case OMPD_flush: 12397 case OMPD_depobj: 12398 case OMPD_scan: 12399 case OMPD_declare_reduction: 12400 case OMPD_declare_mapper: 12401 case OMPD_declare_simd: 12402 case OMPD_declare_variant: 12403 case OMPD_begin_declare_variant: 12404 case OMPD_end_declare_variant: 12405 case OMPD_declare_target: 12406 case OMPD_end_declare_target: 12407 case OMPD_simd: 12408 case OMPD_for: 12409 case OMPD_for_simd: 12410 case OMPD_sections: 12411 case OMPD_section: 12412 case OMPD_single: 12413 case OMPD_master: 12414 case OMPD_critical: 12415 case OMPD_taskgroup: 12416 case OMPD_distribute: 12417 case OMPD_ordered: 12418 case OMPD_atomic: 12419 case OMPD_distribute_simd: 12420 case OMPD_requires: 12421 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause"); 12422 case OMPD_unknown: 12423 default: 12424 llvm_unreachable("Unknown OpenMP directive"); 12425 } 12426 break; 12427 case OMPC_grainsize: 12428 case OMPC_num_tasks: 12429 case OMPC_final: 12430 case OMPC_priority: 12431 switch (DKind) { 12432 case OMPD_task: 12433 case OMPD_taskloop: 12434 case OMPD_taskloop_simd: 12435 case OMPD_master_taskloop: 12436 case OMPD_master_taskloop_simd: 12437 break; 12438 case OMPD_parallel_master_taskloop: 12439 case OMPD_parallel_master_taskloop_simd: 12440 CaptureRegion = OMPD_parallel; 12441 break; 12442 case OMPD_target_update: 12443 case OMPD_target_enter_data: 12444 case OMPD_target_exit_data: 12445 case OMPD_target: 12446 case OMPD_target_simd: 12447 case OMPD_target_teams: 12448 case OMPD_target_parallel: 12449 case OMPD_target_teams_distribute: 12450 case OMPD_target_teams_distribute_simd: 12451 case OMPD_target_parallel_for: 12452 case OMPD_target_parallel_for_simd: 12453 case OMPD_target_teams_distribute_parallel_for: 12454 case OMPD_target_teams_distribute_parallel_for_simd: 12455 case OMPD_target_data: 12456 case OMPD_teams_distribute_parallel_for: 12457 case OMPD_teams_distribute_parallel_for_simd: 12458 case OMPD_teams: 12459 case OMPD_teams_distribute: 12460 case OMPD_teams_distribute_simd: 12461 case OMPD_distribute_parallel_for: 12462 case OMPD_distribute_parallel_for_simd: 12463 case OMPD_cancel: 12464 case OMPD_parallel: 12465 case OMPD_parallel_master: 12466 case OMPD_parallel_sections: 12467 case OMPD_parallel_for: 12468 case OMPD_parallel_for_simd: 12469 case OMPD_threadprivate: 12470 case OMPD_allocate: 12471 case OMPD_taskyield: 12472 case OMPD_barrier: 12473 case OMPD_taskwait: 12474 case OMPD_cancellation_point: 12475 case OMPD_flush: 12476 case OMPD_depobj: 12477 case OMPD_scan: 12478 case OMPD_declare_reduction: 12479 case OMPD_declare_mapper: 12480 case OMPD_declare_simd: 12481 case OMPD_declare_variant: 12482 case OMPD_begin_declare_variant: 12483 case OMPD_end_declare_variant: 12484 case OMPD_declare_target: 12485 case OMPD_end_declare_target: 12486 case OMPD_simd: 12487 case OMPD_for: 12488 case OMPD_for_simd: 12489 case OMPD_sections: 12490 case OMPD_section: 12491 case OMPD_single: 12492 case OMPD_master: 12493 case OMPD_critical: 12494 case OMPD_taskgroup: 12495 case OMPD_distribute: 12496 case OMPD_ordered: 12497 case OMPD_atomic: 12498 case OMPD_distribute_simd: 12499 case OMPD_requires: 12500 llvm_unreachable("Unexpected OpenMP directive with grainsize-clause"); 12501 case OMPD_unknown: 12502 default: 12503 llvm_unreachable("Unknown OpenMP directive"); 12504 } 12505 break; 12506 case OMPC_firstprivate: 12507 case OMPC_lastprivate: 12508 case OMPC_reduction: 12509 case OMPC_task_reduction: 12510 case OMPC_in_reduction: 12511 case OMPC_linear: 12512 case OMPC_default: 12513 case OMPC_proc_bind: 12514 case OMPC_safelen: 12515 case OMPC_simdlen: 12516 case OMPC_allocator: 12517 case OMPC_collapse: 12518 case OMPC_private: 12519 case OMPC_shared: 12520 case OMPC_aligned: 12521 case OMPC_copyin: 12522 case OMPC_copyprivate: 12523 case OMPC_ordered: 12524 case OMPC_nowait: 12525 case OMPC_untied: 12526 case OMPC_mergeable: 12527 case OMPC_threadprivate: 12528 case OMPC_allocate: 12529 case OMPC_flush: 12530 case OMPC_depobj: 12531 case OMPC_read: 12532 case OMPC_write: 12533 case OMPC_update: 12534 case OMPC_capture: 12535 case OMPC_seq_cst: 12536 case OMPC_acq_rel: 12537 case OMPC_acquire: 12538 case OMPC_release: 12539 case OMPC_relaxed: 12540 case OMPC_depend: 12541 case OMPC_threads: 12542 case OMPC_simd: 12543 case OMPC_map: 12544 case OMPC_nogroup: 12545 case OMPC_hint: 12546 case OMPC_defaultmap: 12547 case OMPC_unknown: 12548 case OMPC_uniform: 12549 case OMPC_to: 12550 case OMPC_from: 12551 case OMPC_use_device_ptr: 12552 case OMPC_use_device_addr: 12553 case OMPC_is_device_ptr: 12554 case OMPC_unified_address: 12555 case OMPC_unified_shared_memory: 12556 case OMPC_reverse_offload: 12557 case OMPC_dynamic_allocators: 12558 case OMPC_atomic_default_mem_order: 12559 case OMPC_device_type: 12560 case OMPC_match: 12561 case OMPC_nontemporal: 12562 case OMPC_order: 12563 case OMPC_destroy: 12564 case OMPC_detach: 12565 case OMPC_inclusive: 12566 case OMPC_exclusive: 12567 case OMPC_uses_allocators: 12568 case OMPC_affinity: 12569 default: 12570 llvm_unreachable("Unexpected OpenMP clause."); 12571 } 12572 return CaptureRegion; 12573 } 12574 12575 OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier, 12576 Expr *Condition, SourceLocation StartLoc, 12577 SourceLocation LParenLoc, 12578 SourceLocation NameModifierLoc, 12579 SourceLocation ColonLoc, 12580 SourceLocation EndLoc) { 12581 Expr *ValExpr = Condition; 12582 Stmt *HelperValStmt = nullptr; 12583 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 12584 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 12585 !Condition->isInstantiationDependent() && 12586 !Condition->containsUnexpandedParameterPack()) { 12587 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 12588 if (Val.isInvalid()) 12589 return nullptr; 12590 12591 ValExpr = Val.get(); 12592 12593 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 12594 CaptureRegion = getOpenMPCaptureRegionForClause( 12595 DKind, OMPC_if, LangOpts.OpenMP, NameModifier); 12596 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 12597 ValExpr = MakeFullExpr(ValExpr).get(); 12598 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 12599 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 12600 HelperValStmt = buildPreInits(Context, Captures); 12601 } 12602 } 12603 12604 return new (Context) 12605 OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc, 12606 LParenLoc, NameModifierLoc, ColonLoc, EndLoc); 12607 } 12608 12609 OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition, 12610 SourceLocation StartLoc, 12611 SourceLocation LParenLoc, 12612 SourceLocation EndLoc) { 12613 Expr *ValExpr = Condition; 12614 Stmt *HelperValStmt = nullptr; 12615 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 12616 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 12617 !Condition->isInstantiationDependent() && 12618 !Condition->containsUnexpandedParameterPack()) { 12619 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 12620 if (Val.isInvalid()) 12621 return nullptr; 12622 12623 ValExpr = MakeFullExpr(Val.get()).get(); 12624 12625 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 12626 CaptureRegion = 12627 getOpenMPCaptureRegionForClause(DKind, OMPC_final, LangOpts.OpenMP); 12628 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 12629 ValExpr = MakeFullExpr(ValExpr).get(); 12630 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 12631 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 12632 HelperValStmt = buildPreInits(Context, Captures); 12633 } 12634 } 12635 12636 return new (Context) OMPFinalClause(ValExpr, HelperValStmt, CaptureRegion, 12637 StartLoc, LParenLoc, EndLoc); 12638 } 12639 12640 ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc, 12641 Expr *Op) { 12642 if (!Op) 12643 return ExprError(); 12644 12645 class IntConvertDiagnoser : public ICEConvertDiagnoser { 12646 public: 12647 IntConvertDiagnoser() 12648 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {} 12649 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, 12650 QualType T) override { 12651 return S.Diag(Loc, diag::err_omp_not_integral) << T; 12652 } 12653 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc, 12654 QualType T) override { 12655 return S.Diag(Loc, diag::err_omp_incomplete_type) << T; 12656 } 12657 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc, 12658 QualType T, 12659 QualType ConvTy) override { 12660 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy; 12661 } 12662 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv, 12663 QualType ConvTy) override { 12664 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 12665 << ConvTy->isEnumeralType() << ConvTy; 12666 } 12667 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc, 12668 QualType T) override { 12669 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T; 12670 } 12671 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv, 12672 QualType ConvTy) override { 12673 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 12674 << ConvTy->isEnumeralType() << ConvTy; 12675 } 12676 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType, 12677 QualType) override { 12678 llvm_unreachable("conversion functions are permitted"); 12679 } 12680 } ConvertDiagnoser; 12681 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser); 12682 } 12683 12684 static bool 12685 isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef, OpenMPClauseKind CKind, 12686 bool StrictlyPositive, bool BuildCapture = false, 12687 OpenMPDirectiveKind DKind = OMPD_unknown, 12688 OpenMPDirectiveKind *CaptureRegion = nullptr, 12689 Stmt **HelperValStmt = nullptr) { 12690 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() && 12691 !ValExpr->isInstantiationDependent()) { 12692 SourceLocation Loc = ValExpr->getExprLoc(); 12693 ExprResult Value = 12694 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr); 12695 if (Value.isInvalid()) 12696 return false; 12697 12698 ValExpr = Value.get(); 12699 // The expression must evaluate to a non-negative integer value. 12700 if (Optional<llvm::APSInt> Result = 12701 ValExpr->getIntegerConstantExpr(SemaRef.Context)) { 12702 if (Result->isSigned() && 12703 !((!StrictlyPositive && Result->isNonNegative()) || 12704 (StrictlyPositive && Result->isStrictlyPositive()))) { 12705 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause) 12706 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) 12707 << ValExpr->getSourceRange(); 12708 return false; 12709 } 12710 } 12711 if (!BuildCapture) 12712 return true; 12713 *CaptureRegion = 12714 getOpenMPCaptureRegionForClause(DKind, CKind, SemaRef.LangOpts.OpenMP); 12715 if (*CaptureRegion != OMPD_unknown && 12716 !SemaRef.CurContext->isDependentContext()) { 12717 ValExpr = SemaRef.MakeFullExpr(ValExpr).get(); 12718 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 12719 ValExpr = tryBuildCapture(SemaRef, ValExpr, Captures).get(); 12720 *HelperValStmt = buildPreInits(SemaRef.Context, Captures); 12721 } 12722 } 12723 return true; 12724 } 12725 12726 OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads, 12727 SourceLocation StartLoc, 12728 SourceLocation LParenLoc, 12729 SourceLocation EndLoc) { 12730 Expr *ValExpr = NumThreads; 12731 Stmt *HelperValStmt = nullptr; 12732 12733 // OpenMP [2.5, Restrictions] 12734 // The num_threads expression must evaluate to a positive integer value. 12735 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads, 12736 /*StrictlyPositive=*/true)) 12737 return nullptr; 12738 12739 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 12740 OpenMPDirectiveKind CaptureRegion = 12741 getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads, LangOpts.OpenMP); 12742 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 12743 ValExpr = MakeFullExpr(ValExpr).get(); 12744 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 12745 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 12746 HelperValStmt = buildPreInits(Context, Captures); 12747 } 12748 12749 return new (Context) OMPNumThreadsClause( 12750 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc); 12751 } 12752 12753 ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E, 12754 OpenMPClauseKind CKind, 12755 bool StrictlyPositive) { 12756 if (!E) 12757 return ExprError(); 12758 if (E->isValueDependent() || E->isTypeDependent() || 12759 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 12760 return E; 12761 llvm::APSInt Result; 12762 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result); 12763 if (ICE.isInvalid()) 12764 return ExprError(); 12765 if ((StrictlyPositive && !Result.isStrictlyPositive()) || 12766 (!StrictlyPositive && !Result.isNonNegative())) { 12767 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause) 12768 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) 12769 << E->getSourceRange(); 12770 return ExprError(); 12771 } 12772 if (CKind == OMPC_aligned && !Result.isPowerOf2()) { 12773 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two) 12774 << E->getSourceRange(); 12775 return ExprError(); 12776 } 12777 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1) 12778 DSAStack->setAssociatedLoops(Result.getExtValue()); 12779 else if (CKind == OMPC_ordered) 12780 DSAStack->setAssociatedLoops(Result.getExtValue()); 12781 return ICE; 12782 } 12783 12784 OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc, 12785 SourceLocation LParenLoc, 12786 SourceLocation EndLoc) { 12787 // OpenMP [2.8.1, simd construct, Description] 12788 // The parameter of the safelen clause must be a constant 12789 // positive integer expression. 12790 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen); 12791 if (Safelen.isInvalid()) 12792 return nullptr; 12793 return new (Context) 12794 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc); 12795 } 12796 12797 OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc, 12798 SourceLocation LParenLoc, 12799 SourceLocation EndLoc) { 12800 // OpenMP [2.8.1, simd construct, Description] 12801 // The parameter of the simdlen clause must be a constant 12802 // positive integer expression. 12803 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen); 12804 if (Simdlen.isInvalid()) 12805 return nullptr; 12806 return new (Context) 12807 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc); 12808 } 12809 12810 /// Tries to find omp_allocator_handle_t type. 12811 static bool findOMPAllocatorHandleT(Sema &S, SourceLocation Loc, 12812 DSAStackTy *Stack) { 12813 QualType OMPAllocatorHandleT = Stack->getOMPAllocatorHandleT(); 12814 if (!OMPAllocatorHandleT.isNull()) 12815 return true; 12816 // Build the predefined allocator expressions. 12817 bool ErrorFound = false; 12818 for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) { 12819 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I); 12820 StringRef Allocator = 12821 OMPAllocateDeclAttr::ConvertAllocatorTypeTyToStr(AllocatorKind); 12822 DeclarationName AllocatorName = &S.getASTContext().Idents.get(Allocator); 12823 auto *VD = dyn_cast_or_null<ValueDecl>( 12824 S.LookupSingleName(S.TUScope, AllocatorName, Loc, Sema::LookupAnyName)); 12825 if (!VD) { 12826 ErrorFound = true; 12827 break; 12828 } 12829 QualType AllocatorType = 12830 VD->getType().getNonLValueExprType(S.getASTContext()); 12831 ExprResult Res = S.BuildDeclRefExpr(VD, AllocatorType, VK_LValue, Loc); 12832 if (!Res.isUsable()) { 12833 ErrorFound = true; 12834 break; 12835 } 12836 if (OMPAllocatorHandleT.isNull()) 12837 OMPAllocatorHandleT = AllocatorType; 12838 if (!S.getASTContext().hasSameType(OMPAllocatorHandleT, AllocatorType)) { 12839 ErrorFound = true; 12840 break; 12841 } 12842 Stack->setAllocator(AllocatorKind, Res.get()); 12843 } 12844 if (ErrorFound) { 12845 S.Diag(Loc, diag::err_omp_implied_type_not_found) 12846 << "omp_allocator_handle_t"; 12847 return false; 12848 } 12849 OMPAllocatorHandleT.addConst(); 12850 Stack->setOMPAllocatorHandleT(OMPAllocatorHandleT); 12851 return true; 12852 } 12853 12854 OMPClause *Sema::ActOnOpenMPAllocatorClause(Expr *A, SourceLocation StartLoc, 12855 SourceLocation LParenLoc, 12856 SourceLocation EndLoc) { 12857 // OpenMP [2.11.3, allocate Directive, Description] 12858 // allocator is an expression of omp_allocator_handle_t type. 12859 if (!findOMPAllocatorHandleT(*this, A->getExprLoc(), DSAStack)) 12860 return nullptr; 12861 12862 ExprResult Allocator = DefaultLvalueConversion(A); 12863 if (Allocator.isInvalid()) 12864 return nullptr; 12865 Allocator = PerformImplicitConversion(Allocator.get(), 12866 DSAStack->getOMPAllocatorHandleT(), 12867 Sema::AA_Initializing, 12868 /*AllowExplicit=*/true); 12869 if (Allocator.isInvalid()) 12870 return nullptr; 12871 return new (Context) 12872 OMPAllocatorClause(Allocator.get(), StartLoc, LParenLoc, EndLoc); 12873 } 12874 12875 OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops, 12876 SourceLocation StartLoc, 12877 SourceLocation LParenLoc, 12878 SourceLocation EndLoc) { 12879 // OpenMP [2.7.1, loop construct, Description] 12880 // OpenMP [2.8.1, simd construct, Description] 12881 // OpenMP [2.9.6, distribute construct, Description] 12882 // The parameter of the collapse clause must be a constant 12883 // positive integer expression. 12884 ExprResult NumForLoopsResult = 12885 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse); 12886 if (NumForLoopsResult.isInvalid()) 12887 return nullptr; 12888 return new (Context) 12889 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc); 12890 } 12891 12892 OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc, 12893 SourceLocation EndLoc, 12894 SourceLocation LParenLoc, 12895 Expr *NumForLoops) { 12896 // OpenMP [2.7.1, loop construct, Description] 12897 // OpenMP [2.8.1, simd construct, Description] 12898 // OpenMP [2.9.6, distribute construct, Description] 12899 // The parameter of the ordered clause must be a constant 12900 // positive integer expression if any. 12901 if (NumForLoops && LParenLoc.isValid()) { 12902 ExprResult NumForLoopsResult = 12903 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered); 12904 if (NumForLoopsResult.isInvalid()) 12905 return nullptr; 12906 NumForLoops = NumForLoopsResult.get(); 12907 } else { 12908 NumForLoops = nullptr; 12909 } 12910 auto *Clause = OMPOrderedClause::Create( 12911 Context, NumForLoops, NumForLoops ? DSAStack->getAssociatedLoops() : 0, 12912 StartLoc, LParenLoc, EndLoc); 12913 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops, Clause); 12914 return Clause; 12915 } 12916 12917 OMPClause *Sema::ActOnOpenMPSimpleClause( 12918 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc, 12919 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 12920 OMPClause *Res = nullptr; 12921 switch (Kind) { 12922 case OMPC_default: 12923 Res = ActOnOpenMPDefaultClause(static_cast<DefaultKind>(Argument), 12924 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 12925 break; 12926 case OMPC_proc_bind: 12927 Res = ActOnOpenMPProcBindClause(static_cast<ProcBindKind>(Argument), 12928 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 12929 break; 12930 case OMPC_atomic_default_mem_order: 12931 Res = ActOnOpenMPAtomicDefaultMemOrderClause( 12932 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Argument), 12933 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 12934 break; 12935 case OMPC_order: 12936 Res = ActOnOpenMPOrderClause(static_cast<OpenMPOrderClauseKind>(Argument), 12937 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 12938 break; 12939 case OMPC_update: 12940 Res = ActOnOpenMPUpdateClause(static_cast<OpenMPDependClauseKind>(Argument), 12941 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 12942 break; 12943 case OMPC_if: 12944 case OMPC_final: 12945 case OMPC_num_threads: 12946 case OMPC_safelen: 12947 case OMPC_simdlen: 12948 case OMPC_allocator: 12949 case OMPC_collapse: 12950 case OMPC_schedule: 12951 case OMPC_private: 12952 case OMPC_firstprivate: 12953 case OMPC_lastprivate: 12954 case OMPC_shared: 12955 case OMPC_reduction: 12956 case OMPC_task_reduction: 12957 case OMPC_in_reduction: 12958 case OMPC_linear: 12959 case OMPC_aligned: 12960 case OMPC_copyin: 12961 case OMPC_copyprivate: 12962 case OMPC_ordered: 12963 case OMPC_nowait: 12964 case OMPC_untied: 12965 case OMPC_mergeable: 12966 case OMPC_threadprivate: 12967 case OMPC_allocate: 12968 case OMPC_flush: 12969 case OMPC_depobj: 12970 case OMPC_read: 12971 case OMPC_write: 12972 case OMPC_capture: 12973 case OMPC_seq_cst: 12974 case OMPC_acq_rel: 12975 case OMPC_acquire: 12976 case OMPC_release: 12977 case OMPC_relaxed: 12978 case OMPC_depend: 12979 case OMPC_device: 12980 case OMPC_threads: 12981 case OMPC_simd: 12982 case OMPC_map: 12983 case OMPC_num_teams: 12984 case OMPC_thread_limit: 12985 case OMPC_priority: 12986 case OMPC_grainsize: 12987 case OMPC_nogroup: 12988 case OMPC_num_tasks: 12989 case OMPC_hint: 12990 case OMPC_dist_schedule: 12991 case OMPC_defaultmap: 12992 case OMPC_unknown: 12993 case OMPC_uniform: 12994 case OMPC_to: 12995 case OMPC_from: 12996 case OMPC_use_device_ptr: 12997 case OMPC_use_device_addr: 12998 case OMPC_is_device_ptr: 12999 case OMPC_unified_address: 13000 case OMPC_unified_shared_memory: 13001 case OMPC_reverse_offload: 13002 case OMPC_dynamic_allocators: 13003 case OMPC_device_type: 13004 case OMPC_match: 13005 case OMPC_nontemporal: 13006 case OMPC_destroy: 13007 case OMPC_detach: 13008 case OMPC_inclusive: 13009 case OMPC_exclusive: 13010 case OMPC_uses_allocators: 13011 case OMPC_affinity: 13012 default: 13013 llvm_unreachable("Clause is not allowed."); 13014 } 13015 return Res; 13016 } 13017 13018 static std::string 13019 getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last, 13020 ArrayRef<unsigned> Exclude = llvm::None) { 13021 SmallString<256> Buffer; 13022 llvm::raw_svector_ostream Out(Buffer); 13023 unsigned Skipped = Exclude.size(); 13024 auto S = Exclude.begin(), E = Exclude.end(); 13025 for (unsigned I = First; I < Last; ++I) { 13026 if (std::find(S, E, I) != E) { 13027 --Skipped; 13028 continue; 13029 } 13030 Out << "'" << getOpenMPSimpleClauseTypeName(K, I) << "'"; 13031 if (I + Skipped + 2 == Last) 13032 Out << " or "; 13033 else if (I + Skipped + 1 != Last) 13034 Out << ", "; 13035 } 13036 return std::string(Out.str()); 13037 } 13038 13039 OMPClause *Sema::ActOnOpenMPDefaultClause(DefaultKind Kind, 13040 SourceLocation KindKwLoc, 13041 SourceLocation StartLoc, 13042 SourceLocation LParenLoc, 13043 SourceLocation EndLoc) { 13044 if (Kind == OMP_DEFAULT_unknown) { 13045 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 13046 << getListOfPossibleValues(OMPC_default, /*First=*/0, 13047 /*Last=*/unsigned(OMP_DEFAULT_unknown)) 13048 << getOpenMPClauseName(OMPC_default); 13049 return nullptr; 13050 } 13051 13052 switch (Kind) { 13053 case OMP_DEFAULT_none: 13054 DSAStack->setDefaultDSANone(KindKwLoc); 13055 break; 13056 case OMP_DEFAULT_shared: 13057 DSAStack->setDefaultDSAShared(KindKwLoc); 13058 break; 13059 case OMP_DEFAULT_firstprivate: 13060 DSAStack->setDefaultDSAFirstPrivate(KindKwLoc); 13061 break; 13062 default: 13063 llvm_unreachable("DSA unexpected in OpenMP default clause"); 13064 } 13065 13066 return new (Context) 13067 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 13068 } 13069 13070 OMPClause *Sema::ActOnOpenMPProcBindClause(ProcBindKind Kind, 13071 SourceLocation KindKwLoc, 13072 SourceLocation StartLoc, 13073 SourceLocation LParenLoc, 13074 SourceLocation EndLoc) { 13075 if (Kind == OMP_PROC_BIND_unknown) { 13076 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 13077 << getListOfPossibleValues(OMPC_proc_bind, 13078 /*First=*/unsigned(OMP_PROC_BIND_master), 13079 /*Last=*/5) 13080 << getOpenMPClauseName(OMPC_proc_bind); 13081 return nullptr; 13082 } 13083 return new (Context) 13084 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 13085 } 13086 13087 OMPClause *Sema::ActOnOpenMPAtomicDefaultMemOrderClause( 13088 OpenMPAtomicDefaultMemOrderClauseKind Kind, SourceLocation KindKwLoc, 13089 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 13090 if (Kind == OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) { 13091 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 13092 << getListOfPossibleValues( 13093 OMPC_atomic_default_mem_order, /*First=*/0, 13094 /*Last=*/OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) 13095 << getOpenMPClauseName(OMPC_atomic_default_mem_order); 13096 return nullptr; 13097 } 13098 return new (Context) OMPAtomicDefaultMemOrderClause(Kind, KindKwLoc, StartLoc, 13099 LParenLoc, EndLoc); 13100 } 13101 13102 OMPClause *Sema::ActOnOpenMPOrderClause(OpenMPOrderClauseKind Kind, 13103 SourceLocation KindKwLoc, 13104 SourceLocation StartLoc, 13105 SourceLocation LParenLoc, 13106 SourceLocation EndLoc) { 13107 if (Kind == OMPC_ORDER_unknown) { 13108 static_assert(OMPC_ORDER_unknown > 0, 13109 "OMPC_ORDER_unknown not greater than 0"); 13110 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 13111 << getListOfPossibleValues(OMPC_order, /*First=*/0, 13112 /*Last=*/OMPC_ORDER_unknown) 13113 << getOpenMPClauseName(OMPC_order); 13114 return nullptr; 13115 } 13116 return new (Context) 13117 OMPOrderClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 13118 } 13119 13120 OMPClause *Sema::ActOnOpenMPUpdateClause(OpenMPDependClauseKind Kind, 13121 SourceLocation KindKwLoc, 13122 SourceLocation StartLoc, 13123 SourceLocation LParenLoc, 13124 SourceLocation EndLoc) { 13125 if (Kind == OMPC_DEPEND_unknown || Kind == OMPC_DEPEND_source || 13126 Kind == OMPC_DEPEND_sink || Kind == OMPC_DEPEND_depobj) { 13127 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink, 13128 OMPC_DEPEND_depobj}; 13129 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 13130 << getListOfPossibleValues(OMPC_depend, /*First=*/0, 13131 /*Last=*/OMPC_DEPEND_unknown, Except) 13132 << getOpenMPClauseName(OMPC_update); 13133 return nullptr; 13134 } 13135 return OMPUpdateClause::Create(Context, StartLoc, LParenLoc, KindKwLoc, Kind, 13136 EndLoc); 13137 } 13138 13139 OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause( 13140 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr, 13141 SourceLocation StartLoc, SourceLocation LParenLoc, 13142 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc, 13143 SourceLocation EndLoc) { 13144 OMPClause *Res = nullptr; 13145 switch (Kind) { 13146 case OMPC_schedule: 13147 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements }; 13148 assert(Argument.size() == NumberOfElements && 13149 ArgumentLoc.size() == NumberOfElements); 13150 Res = ActOnOpenMPScheduleClause( 13151 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]), 13152 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]), 13153 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr, 13154 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2], 13155 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc); 13156 break; 13157 case OMPC_if: 13158 assert(Argument.size() == 1 && ArgumentLoc.size() == 1); 13159 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()), 13160 Expr, StartLoc, LParenLoc, ArgumentLoc.back(), 13161 DelimLoc, EndLoc); 13162 break; 13163 case OMPC_dist_schedule: 13164 Res = ActOnOpenMPDistScheduleClause( 13165 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr, 13166 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc); 13167 break; 13168 case OMPC_defaultmap: 13169 enum { Modifier, DefaultmapKind }; 13170 Res = ActOnOpenMPDefaultmapClause( 13171 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]), 13172 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]), 13173 StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind], 13174 EndLoc); 13175 break; 13176 case OMPC_device: 13177 assert(Argument.size() == 1 && ArgumentLoc.size() == 1); 13178 Res = ActOnOpenMPDeviceClause( 13179 static_cast<OpenMPDeviceClauseModifier>(Argument.back()), Expr, 13180 StartLoc, LParenLoc, ArgumentLoc.back(), EndLoc); 13181 break; 13182 case OMPC_final: 13183 case OMPC_num_threads: 13184 case OMPC_safelen: 13185 case OMPC_simdlen: 13186 case OMPC_allocator: 13187 case OMPC_collapse: 13188 case OMPC_default: 13189 case OMPC_proc_bind: 13190 case OMPC_private: 13191 case OMPC_firstprivate: 13192 case OMPC_lastprivate: 13193 case OMPC_shared: 13194 case OMPC_reduction: 13195 case OMPC_task_reduction: 13196 case OMPC_in_reduction: 13197 case OMPC_linear: 13198 case OMPC_aligned: 13199 case OMPC_copyin: 13200 case OMPC_copyprivate: 13201 case OMPC_ordered: 13202 case OMPC_nowait: 13203 case OMPC_untied: 13204 case OMPC_mergeable: 13205 case OMPC_threadprivate: 13206 case OMPC_allocate: 13207 case OMPC_flush: 13208 case OMPC_depobj: 13209 case OMPC_read: 13210 case OMPC_write: 13211 case OMPC_update: 13212 case OMPC_capture: 13213 case OMPC_seq_cst: 13214 case OMPC_acq_rel: 13215 case OMPC_acquire: 13216 case OMPC_release: 13217 case OMPC_relaxed: 13218 case OMPC_depend: 13219 case OMPC_threads: 13220 case OMPC_simd: 13221 case OMPC_map: 13222 case OMPC_num_teams: 13223 case OMPC_thread_limit: 13224 case OMPC_priority: 13225 case OMPC_grainsize: 13226 case OMPC_nogroup: 13227 case OMPC_num_tasks: 13228 case OMPC_hint: 13229 case OMPC_unknown: 13230 case OMPC_uniform: 13231 case OMPC_to: 13232 case OMPC_from: 13233 case OMPC_use_device_ptr: 13234 case OMPC_use_device_addr: 13235 case OMPC_is_device_ptr: 13236 case OMPC_unified_address: 13237 case OMPC_unified_shared_memory: 13238 case OMPC_reverse_offload: 13239 case OMPC_dynamic_allocators: 13240 case OMPC_atomic_default_mem_order: 13241 case OMPC_device_type: 13242 case OMPC_match: 13243 case OMPC_nontemporal: 13244 case OMPC_order: 13245 case OMPC_destroy: 13246 case OMPC_detach: 13247 case OMPC_inclusive: 13248 case OMPC_exclusive: 13249 case OMPC_uses_allocators: 13250 case OMPC_affinity: 13251 default: 13252 llvm_unreachable("Clause is not allowed."); 13253 } 13254 return Res; 13255 } 13256 13257 static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1, 13258 OpenMPScheduleClauseModifier M2, 13259 SourceLocation M1Loc, SourceLocation M2Loc) { 13260 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) { 13261 SmallVector<unsigned, 2> Excluded; 13262 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown) 13263 Excluded.push_back(M2); 13264 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) 13265 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic); 13266 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic) 13267 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic); 13268 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value) 13269 << getListOfPossibleValues(OMPC_schedule, 13270 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1, 13271 /*Last=*/OMPC_SCHEDULE_MODIFIER_last, 13272 Excluded) 13273 << getOpenMPClauseName(OMPC_schedule); 13274 return true; 13275 } 13276 return false; 13277 } 13278 13279 OMPClause *Sema::ActOnOpenMPScheduleClause( 13280 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2, 13281 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, 13282 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc, 13283 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) { 13284 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) || 13285 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc)) 13286 return nullptr; 13287 // OpenMP, 2.7.1, Loop Construct, Restrictions 13288 // Either the monotonic modifier or the nonmonotonic modifier can be specified 13289 // but not both. 13290 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) || 13291 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic && 13292 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) || 13293 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic && 13294 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) { 13295 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier) 13296 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2) 13297 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1); 13298 return nullptr; 13299 } 13300 if (Kind == OMPC_SCHEDULE_unknown) { 13301 std::string Values; 13302 if (M1Loc.isInvalid() && M2Loc.isInvalid()) { 13303 unsigned Exclude[] = {OMPC_SCHEDULE_unknown}; 13304 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0, 13305 /*Last=*/OMPC_SCHEDULE_MODIFIER_last, 13306 Exclude); 13307 } else { 13308 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0, 13309 /*Last=*/OMPC_SCHEDULE_unknown); 13310 } 13311 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 13312 << Values << getOpenMPClauseName(OMPC_schedule); 13313 return nullptr; 13314 } 13315 // OpenMP, 2.7.1, Loop Construct, Restrictions 13316 // The nonmonotonic modifier can only be specified with schedule(dynamic) or 13317 // schedule(guided). 13318 // OpenMP 5.0 does not have this restriction. 13319 if (LangOpts.OpenMP < 50 && 13320 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic || 13321 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) && 13322 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) { 13323 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc, 13324 diag::err_omp_schedule_nonmonotonic_static); 13325 return nullptr; 13326 } 13327 Expr *ValExpr = ChunkSize; 13328 Stmt *HelperValStmt = nullptr; 13329 if (ChunkSize) { 13330 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && 13331 !ChunkSize->isInstantiationDependent() && 13332 !ChunkSize->containsUnexpandedParameterPack()) { 13333 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc(); 13334 ExprResult Val = 13335 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); 13336 if (Val.isInvalid()) 13337 return nullptr; 13338 13339 ValExpr = Val.get(); 13340 13341 // OpenMP [2.7.1, Restrictions] 13342 // chunk_size must be a loop invariant integer expression with a positive 13343 // value. 13344 if (Optional<llvm::APSInt> Result = 13345 ValExpr->getIntegerConstantExpr(Context)) { 13346 if (Result->isSigned() && !Result->isStrictlyPositive()) { 13347 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) 13348 << "schedule" << 1 << ChunkSize->getSourceRange(); 13349 return nullptr; 13350 } 13351 } else if (getOpenMPCaptureRegionForClause( 13352 DSAStack->getCurrentDirective(), OMPC_schedule, 13353 LangOpts.OpenMP) != OMPD_unknown && 13354 !CurContext->isDependentContext()) { 13355 ValExpr = MakeFullExpr(ValExpr).get(); 13356 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 13357 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 13358 HelperValStmt = buildPreInits(Context, Captures); 13359 } 13360 } 13361 } 13362 13363 return new (Context) 13364 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind, 13365 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc); 13366 } 13367 13368 OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind, 13369 SourceLocation StartLoc, 13370 SourceLocation EndLoc) { 13371 OMPClause *Res = nullptr; 13372 switch (Kind) { 13373 case OMPC_ordered: 13374 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc); 13375 break; 13376 case OMPC_nowait: 13377 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc); 13378 break; 13379 case OMPC_untied: 13380 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc); 13381 break; 13382 case OMPC_mergeable: 13383 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc); 13384 break; 13385 case OMPC_read: 13386 Res = ActOnOpenMPReadClause(StartLoc, EndLoc); 13387 break; 13388 case OMPC_write: 13389 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc); 13390 break; 13391 case OMPC_update: 13392 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc); 13393 break; 13394 case OMPC_capture: 13395 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc); 13396 break; 13397 case OMPC_seq_cst: 13398 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc); 13399 break; 13400 case OMPC_acq_rel: 13401 Res = ActOnOpenMPAcqRelClause(StartLoc, EndLoc); 13402 break; 13403 case OMPC_acquire: 13404 Res = ActOnOpenMPAcquireClause(StartLoc, EndLoc); 13405 break; 13406 case OMPC_release: 13407 Res = ActOnOpenMPReleaseClause(StartLoc, EndLoc); 13408 break; 13409 case OMPC_relaxed: 13410 Res = ActOnOpenMPRelaxedClause(StartLoc, EndLoc); 13411 break; 13412 case OMPC_threads: 13413 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc); 13414 break; 13415 case OMPC_simd: 13416 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc); 13417 break; 13418 case OMPC_nogroup: 13419 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc); 13420 break; 13421 case OMPC_unified_address: 13422 Res = ActOnOpenMPUnifiedAddressClause(StartLoc, EndLoc); 13423 break; 13424 case OMPC_unified_shared_memory: 13425 Res = ActOnOpenMPUnifiedSharedMemoryClause(StartLoc, EndLoc); 13426 break; 13427 case OMPC_reverse_offload: 13428 Res = ActOnOpenMPReverseOffloadClause(StartLoc, EndLoc); 13429 break; 13430 case OMPC_dynamic_allocators: 13431 Res = ActOnOpenMPDynamicAllocatorsClause(StartLoc, EndLoc); 13432 break; 13433 case OMPC_destroy: 13434 Res = ActOnOpenMPDestroyClause(StartLoc, EndLoc); 13435 break; 13436 case OMPC_if: 13437 case OMPC_final: 13438 case OMPC_num_threads: 13439 case OMPC_safelen: 13440 case OMPC_simdlen: 13441 case OMPC_allocator: 13442 case OMPC_collapse: 13443 case OMPC_schedule: 13444 case OMPC_private: 13445 case OMPC_firstprivate: 13446 case OMPC_lastprivate: 13447 case OMPC_shared: 13448 case OMPC_reduction: 13449 case OMPC_task_reduction: 13450 case OMPC_in_reduction: 13451 case OMPC_linear: 13452 case OMPC_aligned: 13453 case OMPC_copyin: 13454 case OMPC_copyprivate: 13455 case OMPC_default: 13456 case OMPC_proc_bind: 13457 case OMPC_threadprivate: 13458 case OMPC_allocate: 13459 case OMPC_flush: 13460 case OMPC_depobj: 13461 case OMPC_depend: 13462 case OMPC_device: 13463 case OMPC_map: 13464 case OMPC_num_teams: 13465 case OMPC_thread_limit: 13466 case OMPC_priority: 13467 case OMPC_grainsize: 13468 case OMPC_num_tasks: 13469 case OMPC_hint: 13470 case OMPC_dist_schedule: 13471 case OMPC_defaultmap: 13472 case OMPC_unknown: 13473 case OMPC_uniform: 13474 case OMPC_to: 13475 case OMPC_from: 13476 case OMPC_use_device_ptr: 13477 case OMPC_use_device_addr: 13478 case OMPC_is_device_ptr: 13479 case OMPC_atomic_default_mem_order: 13480 case OMPC_device_type: 13481 case OMPC_match: 13482 case OMPC_nontemporal: 13483 case OMPC_order: 13484 case OMPC_detach: 13485 case OMPC_inclusive: 13486 case OMPC_exclusive: 13487 case OMPC_uses_allocators: 13488 case OMPC_affinity: 13489 default: 13490 llvm_unreachable("Clause is not allowed."); 13491 } 13492 return Res; 13493 } 13494 13495 OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc, 13496 SourceLocation EndLoc) { 13497 DSAStack->setNowaitRegion(); 13498 return new (Context) OMPNowaitClause(StartLoc, EndLoc); 13499 } 13500 13501 OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc, 13502 SourceLocation EndLoc) { 13503 return new (Context) OMPUntiedClause(StartLoc, EndLoc); 13504 } 13505 13506 OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc, 13507 SourceLocation EndLoc) { 13508 return new (Context) OMPMergeableClause(StartLoc, EndLoc); 13509 } 13510 13511 OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc, 13512 SourceLocation EndLoc) { 13513 return new (Context) OMPReadClause(StartLoc, EndLoc); 13514 } 13515 13516 OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc, 13517 SourceLocation EndLoc) { 13518 return new (Context) OMPWriteClause(StartLoc, EndLoc); 13519 } 13520 13521 OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc, 13522 SourceLocation EndLoc) { 13523 return OMPUpdateClause::Create(Context, StartLoc, EndLoc); 13524 } 13525 13526 OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc, 13527 SourceLocation EndLoc) { 13528 return new (Context) OMPCaptureClause(StartLoc, EndLoc); 13529 } 13530 13531 OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc, 13532 SourceLocation EndLoc) { 13533 return new (Context) OMPSeqCstClause(StartLoc, EndLoc); 13534 } 13535 13536 OMPClause *Sema::ActOnOpenMPAcqRelClause(SourceLocation StartLoc, 13537 SourceLocation EndLoc) { 13538 return new (Context) OMPAcqRelClause(StartLoc, EndLoc); 13539 } 13540 13541 OMPClause *Sema::ActOnOpenMPAcquireClause(SourceLocation StartLoc, 13542 SourceLocation EndLoc) { 13543 return new (Context) OMPAcquireClause(StartLoc, EndLoc); 13544 } 13545 13546 OMPClause *Sema::ActOnOpenMPReleaseClause(SourceLocation StartLoc, 13547 SourceLocation EndLoc) { 13548 return new (Context) OMPReleaseClause(StartLoc, EndLoc); 13549 } 13550 13551 OMPClause *Sema::ActOnOpenMPRelaxedClause(SourceLocation StartLoc, 13552 SourceLocation EndLoc) { 13553 return new (Context) OMPRelaxedClause(StartLoc, EndLoc); 13554 } 13555 13556 OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc, 13557 SourceLocation EndLoc) { 13558 return new (Context) OMPThreadsClause(StartLoc, EndLoc); 13559 } 13560 13561 OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc, 13562 SourceLocation EndLoc) { 13563 return new (Context) OMPSIMDClause(StartLoc, EndLoc); 13564 } 13565 13566 OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc, 13567 SourceLocation EndLoc) { 13568 return new (Context) OMPNogroupClause(StartLoc, EndLoc); 13569 } 13570 13571 OMPClause *Sema::ActOnOpenMPUnifiedAddressClause(SourceLocation StartLoc, 13572 SourceLocation EndLoc) { 13573 return new (Context) OMPUnifiedAddressClause(StartLoc, EndLoc); 13574 } 13575 13576 OMPClause *Sema::ActOnOpenMPUnifiedSharedMemoryClause(SourceLocation StartLoc, 13577 SourceLocation EndLoc) { 13578 return new (Context) OMPUnifiedSharedMemoryClause(StartLoc, EndLoc); 13579 } 13580 13581 OMPClause *Sema::ActOnOpenMPReverseOffloadClause(SourceLocation StartLoc, 13582 SourceLocation EndLoc) { 13583 return new (Context) OMPReverseOffloadClause(StartLoc, EndLoc); 13584 } 13585 13586 OMPClause *Sema::ActOnOpenMPDynamicAllocatorsClause(SourceLocation StartLoc, 13587 SourceLocation EndLoc) { 13588 return new (Context) OMPDynamicAllocatorsClause(StartLoc, EndLoc); 13589 } 13590 13591 OMPClause *Sema::ActOnOpenMPDestroyClause(SourceLocation StartLoc, 13592 SourceLocation EndLoc) { 13593 return new (Context) OMPDestroyClause(StartLoc, EndLoc); 13594 } 13595 13596 OMPClause *Sema::ActOnOpenMPVarListClause( 13597 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *DepModOrTailExpr, 13598 const OMPVarListLocTy &Locs, SourceLocation ColonLoc, 13599 CXXScopeSpec &ReductionOrMapperIdScopeSpec, 13600 DeclarationNameInfo &ReductionOrMapperId, int ExtraModifier, 13601 ArrayRef<OpenMPMapModifierKind> MapTypeModifiers, 13602 ArrayRef<SourceLocation> MapTypeModifiersLoc, bool IsMapTypeImplicit, 13603 SourceLocation ExtraModifierLoc, 13604 ArrayRef<OpenMPMotionModifierKind> MotionModifiers, 13605 ArrayRef<SourceLocation> MotionModifiersLoc) { 13606 SourceLocation StartLoc = Locs.StartLoc; 13607 SourceLocation LParenLoc = Locs.LParenLoc; 13608 SourceLocation EndLoc = Locs.EndLoc; 13609 OMPClause *Res = nullptr; 13610 switch (Kind) { 13611 case OMPC_private: 13612 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc); 13613 break; 13614 case OMPC_firstprivate: 13615 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 13616 break; 13617 case OMPC_lastprivate: 13618 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_LASTPRIVATE_unknown && 13619 "Unexpected lastprivate modifier."); 13620 Res = ActOnOpenMPLastprivateClause( 13621 VarList, static_cast<OpenMPLastprivateModifier>(ExtraModifier), 13622 ExtraModifierLoc, ColonLoc, StartLoc, LParenLoc, EndLoc); 13623 break; 13624 case OMPC_shared: 13625 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc); 13626 break; 13627 case OMPC_reduction: 13628 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_REDUCTION_unknown && 13629 "Unexpected lastprivate modifier."); 13630 Res = ActOnOpenMPReductionClause( 13631 VarList, static_cast<OpenMPReductionClauseModifier>(ExtraModifier), 13632 StartLoc, LParenLoc, ExtraModifierLoc, ColonLoc, EndLoc, 13633 ReductionOrMapperIdScopeSpec, ReductionOrMapperId); 13634 break; 13635 case OMPC_task_reduction: 13636 Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 13637 EndLoc, ReductionOrMapperIdScopeSpec, 13638 ReductionOrMapperId); 13639 break; 13640 case OMPC_in_reduction: 13641 Res = ActOnOpenMPInReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 13642 EndLoc, ReductionOrMapperIdScopeSpec, 13643 ReductionOrMapperId); 13644 break; 13645 case OMPC_linear: 13646 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_LINEAR_unknown && 13647 "Unexpected linear modifier."); 13648 Res = ActOnOpenMPLinearClause( 13649 VarList, DepModOrTailExpr, StartLoc, LParenLoc, 13650 static_cast<OpenMPLinearClauseKind>(ExtraModifier), ExtraModifierLoc, 13651 ColonLoc, EndLoc); 13652 break; 13653 case OMPC_aligned: 13654 Res = ActOnOpenMPAlignedClause(VarList, DepModOrTailExpr, StartLoc, 13655 LParenLoc, ColonLoc, EndLoc); 13656 break; 13657 case OMPC_copyin: 13658 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc); 13659 break; 13660 case OMPC_copyprivate: 13661 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 13662 break; 13663 case OMPC_flush: 13664 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc); 13665 break; 13666 case OMPC_depend: 13667 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_DEPEND_unknown && 13668 "Unexpected depend modifier."); 13669 Res = ActOnOpenMPDependClause( 13670 DepModOrTailExpr, static_cast<OpenMPDependClauseKind>(ExtraModifier), 13671 ExtraModifierLoc, ColonLoc, VarList, StartLoc, LParenLoc, EndLoc); 13672 break; 13673 case OMPC_map: 13674 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_MAP_unknown && 13675 "Unexpected map modifier."); 13676 Res = ActOnOpenMPMapClause( 13677 MapTypeModifiers, MapTypeModifiersLoc, ReductionOrMapperIdScopeSpec, 13678 ReductionOrMapperId, static_cast<OpenMPMapClauseKind>(ExtraModifier), 13679 IsMapTypeImplicit, ExtraModifierLoc, ColonLoc, VarList, Locs); 13680 break; 13681 case OMPC_to: 13682 Res = ActOnOpenMPToClause(MotionModifiers, MotionModifiersLoc, 13683 ReductionOrMapperIdScopeSpec, ReductionOrMapperId, 13684 ColonLoc, VarList, Locs); 13685 break; 13686 case OMPC_from: 13687 Res = ActOnOpenMPFromClause(MotionModifiers, MotionModifiersLoc, 13688 ReductionOrMapperIdScopeSpec, 13689 ReductionOrMapperId, ColonLoc, VarList, Locs); 13690 break; 13691 case OMPC_use_device_ptr: 13692 Res = ActOnOpenMPUseDevicePtrClause(VarList, Locs); 13693 break; 13694 case OMPC_use_device_addr: 13695 Res = ActOnOpenMPUseDeviceAddrClause(VarList, Locs); 13696 break; 13697 case OMPC_is_device_ptr: 13698 Res = ActOnOpenMPIsDevicePtrClause(VarList, Locs); 13699 break; 13700 case OMPC_allocate: 13701 Res = ActOnOpenMPAllocateClause(DepModOrTailExpr, VarList, StartLoc, 13702 LParenLoc, ColonLoc, EndLoc); 13703 break; 13704 case OMPC_nontemporal: 13705 Res = ActOnOpenMPNontemporalClause(VarList, StartLoc, LParenLoc, EndLoc); 13706 break; 13707 case OMPC_inclusive: 13708 Res = ActOnOpenMPInclusiveClause(VarList, StartLoc, LParenLoc, EndLoc); 13709 break; 13710 case OMPC_exclusive: 13711 Res = ActOnOpenMPExclusiveClause(VarList, StartLoc, LParenLoc, EndLoc); 13712 break; 13713 case OMPC_affinity: 13714 Res = ActOnOpenMPAffinityClause(StartLoc, LParenLoc, ColonLoc, EndLoc, 13715 DepModOrTailExpr, VarList); 13716 break; 13717 case OMPC_if: 13718 case OMPC_depobj: 13719 case OMPC_final: 13720 case OMPC_num_threads: 13721 case OMPC_safelen: 13722 case OMPC_simdlen: 13723 case OMPC_allocator: 13724 case OMPC_collapse: 13725 case OMPC_default: 13726 case OMPC_proc_bind: 13727 case OMPC_schedule: 13728 case OMPC_ordered: 13729 case OMPC_nowait: 13730 case OMPC_untied: 13731 case OMPC_mergeable: 13732 case OMPC_threadprivate: 13733 case OMPC_read: 13734 case OMPC_write: 13735 case OMPC_update: 13736 case OMPC_capture: 13737 case OMPC_seq_cst: 13738 case OMPC_acq_rel: 13739 case OMPC_acquire: 13740 case OMPC_release: 13741 case OMPC_relaxed: 13742 case OMPC_device: 13743 case OMPC_threads: 13744 case OMPC_simd: 13745 case OMPC_num_teams: 13746 case OMPC_thread_limit: 13747 case OMPC_priority: 13748 case OMPC_grainsize: 13749 case OMPC_nogroup: 13750 case OMPC_num_tasks: 13751 case OMPC_hint: 13752 case OMPC_dist_schedule: 13753 case OMPC_defaultmap: 13754 case OMPC_unknown: 13755 case OMPC_uniform: 13756 case OMPC_unified_address: 13757 case OMPC_unified_shared_memory: 13758 case OMPC_reverse_offload: 13759 case OMPC_dynamic_allocators: 13760 case OMPC_atomic_default_mem_order: 13761 case OMPC_device_type: 13762 case OMPC_match: 13763 case OMPC_order: 13764 case OMPC_destroy: 13765 case OMPC_detach: 13766 case OMPC_uses_allocators: 13767 default: 13768 llvm_unreachable("Clause is not allowed."); 13769 } 13770 return Res; 13771 } 13772 13773 ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK, 13774 ExprObjectKind OK, SourceLocation Loc) { 13775 ExprResult Res = BuildDeclRefExpr( 13776 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc); 13777 if (!Res.isUsable()) 13778 return ExprError(); 13779 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) { 13780 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get()); 13781 if (!Res.isUsable()) 13782 return ExprError(); 13783 } 13784 if (VK != VK_LValue && Res.get()->isGLValue()) { 13785 Res = DefaultLvalueConversion(Res.get()); 13786 if (!Res.isUsable()) 13787 return ExprError(); 13788 } 13789 return Res; 13790 } 13791 13792 OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList, 13793 SourceLocation StartLoc, 13794 SourceLocation LParenLoc, 13795 SourceLocation EndLoc) { 13796 SmallVector<Expr *, 8> Vars; 13797 SmallVector<Expr *, 8> PrivateCopies; 13798 for (Expr *RefExpr : VarList) { 13799 assert(RefExpr && "NULL expr in OpenMP private clause."); 13800 SourceLocation ELoc; 13801 SourceRange ERange; 13802 Expr *SimpleRefExpr = RefExpr; 13803 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 13804 if (Res.second) { 13805 // It will be analyzed later. 13806 Vars.push_back(RefExpr); 13807 PrivateCopies.push_back(nullptr); 13808 } 13809 ValueDecl *D = Res.first; 13810 if (!D) 13811 continue; 13812 13813 QualType Type = D->getType(); 13814 auto *VD = dyn_cast<VarDecl>(D); 13815 13816 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 13817 // A variable that appears in a private clause must not have an incomplete 13818 // type or a reference type. 13819 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type)) 13820 continue; 13821 Type = Type.getNonReferenceType(); 13822 13823 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions] 13824 // A variable that is privatized must not have a const-qualified type 13825 // unless it is of class type with a mutable member. This restriction does 13826 // not apply to the firstprivate clause. 13827 // 13828 // OpenMP 3.1 [2.9.3.3, private clause, Restrictions] 13829 // A variable that appears in a private clause must not have a 13830 // const-qualified type unless it is of class type with a mutable member. 13831 if (rejectConstNotMutableType(*this, D, Type, OMPC_private, ELoc)) 13832 continue; 13833 13834 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 13835 // in a Construct] 13836 // Variables with the predetermined data-sharing attributes may not be 13837 // listed in data-sharing attributes clauses, except for the cases 13838 // listed below. For these exceptions only, listing a predetermined 13839 // variable in a data-sharing attribute clause is allowed and overrides 13840 // the variable's predetermined data-sharing attributes. 13841 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 13842 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) { 13843 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 13844 << getOpenMPClauseName(OMPC_private); 13845 reportOriginalDsa(*this, DSAStack, D, DVar); 13846 continue; 13847 } 13848 13849 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 13850 // Variably modified types are not supported for tasks. 13851 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && 13852 isOpenMPTaskingDirective(CurrDir)) { 13853 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 13854 << getOpenMPClauseName(OMPC_private) << Type 13855 << getOpenMPDirectiveName(CurrDir); 13856 bool IsDecl = 13857 !VD || 13858 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 13859 Diag(D->getLocation(), 13860 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 13861 << D; 13862 continue; 13863 } 13864 13865 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 13866 // A list item cannot appear in both a map clause and a data-sharing 13867 // attribute clause on the same construct 13868 // 13869 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7] 13870 // A list item cannot appear in both a map clause and a data-sharing 13871 // attribute clause on the same construct unless the construct is a 13872 // combined construct. 13873 if ((LangOpts.OpenMP <= 45 && isOpenMPTargetExecutionDirective(CurrDir)) || 13874 CurrDir == OMPD_target) { 13875 OpenMPClauseKind ConflictKind; 13876 if (DSAStack->checkMappableExprComponentListsForDecl( 13877 VD, /*CurrentRegionOnly=*/true, 13878 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef, 13879 OpenMPClauseKind WhereFoundClauseKind) -> bool { 13880 ConflictKind = WhereFoundClauseKind; 13881 return true; 13882 })) { 13883 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 13884 << getOpenMPClauseName(OMPC_private) 13885 << getOpenMPClauseName(ConflictKind) 13886 << getOpenMPDirectiveName(CurrDir); 13887 reportOriginalDsa(*this, DSAStack, D, DVar); 13888 continue; 13889 } 13890 } 13891 13892 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1] 13893 // A variable of class type (or array thereof) that appears in a private 13894 // clause requires an accessible, unambiguous default constructor for the 13895 // class type. 13896 // Generate helper private variable and initialize it with the default 13897 // value. The address of the original variable is replaced by the address of 13898 // the new private variable in CodeGen. This new variable is not added to 13899 // IdResolver, so the code in the OpenMP region uses original variable for 13900 // proper diagnostics. 13901 Type = Type.getUnqualifiedType(); 13902 VarDecl *VDPrivate = 13903 buildVarDecl(*this, ELoc, Type, D->getName(), 13904 D->hasAttrs() ? &D->getAttrs() : nullptr, 13905 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 13906 ActOnUninitializedDecl(VDPrivate); 13907 if (VDPrivate->isInvalidDecl()) 13908 continue; 13909 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 13910 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc); 13911 13912 DeclRefExpr *Ref = nullptr; 13913 if (!VD && !CurContext->isDependentContext()) 13914 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 13915 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref); 13916 Vars.push_back((VD || CurContext->isDependentContext()) 13917 ? RefExpr->IgnoreParens() 13918 : Ref); 13919 PrivateCopies.push_back(VDPrivateRefExpr); 13920 } 13921 13922 if (Vars.empty()) 13923 return nullptr; 13924 13925 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 13926 PrivateCopies); 13927 } 13928 13929 namespace { 13930 class DiagsUninitializedSeveretyRAII { 13931 private: 13932 DiagnosticsEngine &Diags; 13933 SourceLocation SavedLoc; 13934 bool IsIgnored = false; 13935 13936 public: 13937 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc, 13938 bool IsIgnored) 13939 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) { 13940 if (!IsIgnored) { 13941 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init, 13942 /*Map*/ diag::Severity::Ignored, Loc); 13943 } 13944 } 13945 ~DiagsUninitializedSeveretyRAII() { 13946 if (!IsIgnored) 13947 Diags.popMappings(SavedLoc); 13948 } 13949 }; 13950 } 13951 13952 OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList, 13953 SourceLocation StartLoc, 13954 SourceLocation LParenLoc, 13955 SourceLocation EndLoc) { 13956 SmallVector<Expr *, 8> Vars; 13957 SmallVector<Expr *, 8> PrivateCopies; 13958 SmallVector<Expr *, 8> Inits; 13959 SmallVector<Decl *, 4> ExprCaptures; 13960 bool IsImplicitClause = 13961 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid(); 13962 SourceLocation ImplicitClauseLoc = DSAStack->getConstructLoc(); 13963 13964 for (Expr *RefExpr : VarList) { 13965 assert(RefExpr && "NULL expr in OpenMP firstprivate clause."); 13966 SourceLocation ELoc; 13967 SourceRange ERange; 13968 Expr *SimpleRefExpr = RefExpr; 13969 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 13970 if (Res.second) { 13971 // It will be analyzed later. 13972 Vars.push_back(RefExpr); 13973 PrivateCopies.push_back(nullptr); 13974 Inits.push_back(nullptr); 13975 } 13976 ValueDecl *D = Res.first; 13977 if (!D) 13978 continue; 13979 13980 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc; 13981 QualType Type = D->getType(); 13982 auto *VD = dyn_cast<VarDecl>(D); 13983 13984 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 13985 // A variable that appears in a private clause must not have an incomplete 13986 // type or a reference type. 13987 if (RequireCompleteType(ELoc, Type, 13988 diag::err_omp_firstprivate_incomplete_type)) 13989 continue; 13990 Type = Type.getNonReferenceType(); 13991 13992 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1] 13993 // A variable of class type (or array thereof) that appears in a private 13994 // clause requires an accessible, unambiguous copy constructor for the 13995 // class type. 13996 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType(); 13997 13998 // If an implicit firstprivate variable found it was checked already. 13999 DSAStackTy::DSAVarData TopDVar; 14000 if (!IsImplicitClause) { 14001 DSAStackTy::DSAVarData DVar = 14002 DSAStack->getTopDSA(D, /*FromParent=*/false); 14003 TopDVar = DVar; 14004 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 14005 bool IsConstant = ElemType.isConstant(Context); 14006 // OpenMP [2.4.13, Data-sharing Attribute Clauses] 14007 // A list item that specifies a given variable may not appear in more 14008 // than one clause on the same directive, except that a variable may be 14009 // specified in both firstprivate and lastprivate clauses. 14010 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3] 14011 // A list item may appear in a firstprivate or lastprivate clause but not 14012 // both. 14013 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate && 14014 (isOpenMPDistributeDirective(CurrDir) || 14015 DVar.CKind != OMPC_lastprivate) && 14016 DVar.RefExpr) { 14017 Diag(ELoc, diag::err_omp_wrong_dsa) 14018 << getOpenMPClauseName(DVar.CKind) 14019 << getOpenMPClauseName(OMPC_firstprivate); 14020 reportOriginalDsa(*this, DSAStack, D, DVar); 14021 continue; 14022 } 14023 14024 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 14025 // in a Construct] 14026 // Variables with the predetermined data-sharing attributes may not be 14027 // listed in data-sharing attributes clauses, except for the cases 14028 // listed below. For these exceptions only, listing a predetermined 14029 // variable in a data-sharing attribute clause is allowed and overrides 14030 // the variable's predetermined data-sharing attributes. 14031 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 14032 // in a Construct, C/C++, p.2] 14033 // Variables with const-qualified type having no mutable member may be 14034 // listed in a firstprivate clause, even if they are static data members. 14035 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr && 14036 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) { 14037 Diag(ELoc, diag::err_omp_wrong_dsa) 14038 << getOpenMPClauseName(DVar.CKind) 14039 << getOpenMPClauseName(OMPC_firstprivate); 14040 reportOriginalDsa(*this, DSAStack, D, DVar); 14041 continue; 14042 } 14043 14044 // OpenMP [2.9.3.4, Restrictions, p.2] 14045 // A list item that is private within a parallel region must not appear 14046 // in a firstprivate clause on a worksharing construct if any of the 14047 // worksharing regions arising from the worksharing construct ever bind 14048 // to any of the parallel regions arising from the parallel construct. 14049 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3] 14050 // A list item that is private within a teams region must not appear in a 14051 // firstprivate clause on a distribute construct if any of the distribute 14052 // regions arising from the distribute construct ever bind to any of the 14053 // teams regions arising from the teams construct. 14054 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3] 14055 // A list item that appears in a reduction clause of a teams construct 14056 // must not appear in a firstprivate clause on a distribute construct if 14057 // any of the distribute regions arising from the distribute construct 14058 // ever bind to any of the teams regions arising from the teams construct. 14059 if ((isOpenMPWorksharingDirective(CurrDir) || 14060 isOpenMPDistributeDirective(CurrDir)) && 14061 !isOpenMPParallelDirective(CurrDir) && 14062 !isOpenMPTeamsDirective(CurrDir)) { 14063 DVar = DSAStack->getImplicitDSA(D, true); 14064 if (DVar.CKind != OMPC_shared && 14065 (isOpenMPParallelDirective(DVar.DKind) || 14066 isOpenMPTeamsDirective(DVar.DKind) || 14067 DVar.DKind == OMPD_unknown)) { 14068 Diag(ELoc, diag::err_omp_required_access) 14069 << getOpenMPClauseName(OMPC_firstprivate) 14070 << getOpenMPClauseName(OMPC_shared); 14071 reportOriginalDsa(*this, DSAStack, D, DVar); 14072 continue; 14073 } 14074 } 14075 // OpenMP [2.9.3.4, Restrictions, p.3] 14076 // A list item that appears in a reduction clause of a parallel construct 14077 // must not appear in a firstprivate clause on a worksharing or task 14078 // construct if any of the worksharing or task regions arising from the 14079 // worksharing or task construct ever bind to any of the parallel regions 14080 // arising from the parallel construct. 14081 // OpenMP [2.9.3.4, Restrictions, p.4] 14082 // A list item that appears in a reduction clause in worksharing 14083 // construct must not appear in a firstprivate clause in a task construct 14084 // encountered during execution of any of the worksharing regions arising 14085 // from the worksharing construct. 14086 if (isOpenMPTaskingDirective(CurrDir)) { 14087 DVar = DSAStack->hasInnermostDSA( 14088 D, 14089 [](OpenMPClauseKind C, bool AppliedToPointee) { 14090 return C == OMPC_reduction && !AppliedToPointee; 14091 }, 14092 [](OpenMPDirectiveKind K) { 14093 return isOpenMPParallelDirective(K) || 14094 isOpenMPWorksharingDirective(K) || 14095 isOpenMPTeamsDirective(K); 14096 }, 14097 /*FromParent=*/true); 14098 if (DVar.CKind == OMPC_reduction && 14099 (isOpenMPParallelDirective(DVar.DKind) || 14100 isOpenMPWorksharingDirective(DVar.DKind) || 14101 isOpenMPTeamsDirective(DVar.DKind))) { 14102 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate) 14103 << getOpenMPDirectiveName(DVar.DKind); 14104 reportOriginalDsa(*this, DSAStack, D, DVar); 14105 continue; 14106 } 14107 } 14108 14109 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 14110 // A list item cannot appear in both a map clause and a data-sharing 14111 // attribute clause on the same construct 14112 // 14113 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7] 14114 // A list item cannot appear in both a map clause and a data-sharing 14115 // attribute clause on the same construct unless the construct is a 14116 // combined construct. 14117 if ((LangOpts.OpenMP <= 45 && 14118 isOpenMPTargetExecutionDirective(CurrDir)) || 14119 CurrDir == OMPD_target) { 14120 OpenMPClauseKind ConflictKind; 14121 if (DSAStack->checkMappableExprComponentListsForDecl( 14122 VD, /*CurrentRegionOnly=*/true, 14123 [&ConflictKind]( 14124 OMPClauseMappableExprCommon::MappableExprComponentListRef, 14125 OpenMPClauseKind WhereFoundClauseKind) { 14126 ConflictKind = WhereFoundClauseKind; 14127 return true; 14128 })) { 14129 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 14130 << getOpenMPClauseName(OMPC_firstprivate) 14131 << getOpenMPClauseName(ConflictKind) 14132 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 14133 reportOriginalDsa(*this, DSAStack, D, DVar); 14134 continue; 14135 } 14136 } 14137 } 14138 14139 // Variably modified types are not supported for tasks. 14140 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && 14141 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) { 14142 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 14143 << getOpenMPClauseName(OMPC_firstprivate) << Type 14144 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 14145 bool IsDecl = 14146 !VD || 14147 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 14148 Diag(D->getLocation(), 14149 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 14150 << D; 14151 continue; 14152 } 14153 14154 Type = Type.getUnqualifiedType(); 14155 VarDecl *VDPrivate = 14156 buildVarDecl(*this, ELoc, Type, D->getName(), 14157 D->hasAttrs() ? &D->getAttrs() : nullptr, 14158 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 14159 // Generate helper private variable and initialize it with the value of the 14160 // original variable. The address of the original variable is replaced by 14161 // the address of the new private variable in the CodeGen. This new variable 14162 // is not added to IdResolver, so the code in the OpenMP region uses 14163 // original variable for proper diagnostics and variable capturing. 14164 Expr *VDInitRefExpr = nullptr; 14165 // For arrays generate initializer for single element and replace it by the 14166 // original array element in CodeGen. 14167 if (Type->isArrayType()) { 14168 VarDecl *VDInit = 14169 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName()); 14170 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc); 14171 Expr *Init = DefaultLvalueConversion(VDInitRefExpr).get(); 14172 ElemType = ElemType.getUnqualifiedType(); 14173 VarDecl *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, 14174 ".firstprivate.temp"); 14175 InitializedEntity Entity = 14176 InitializedEntity::InitializeVariable(VDInitTemp); 14177 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc); 14178 14179 InitializationSequence InitSeq(*this, Entity, Kind, Init); 14180 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init); 14181 if (Result.isInvalid()) 14182 VDPrivate->setInvalidDecl(); 14183 else 14184 VDPrivate->setInit(Result.getAs<Expr>()); 14185 // Remove temp variable declaration. 14186 Context.Deallocate(VDInitTemp); 14187 } else { 14188 VarDecl *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type, 14189 ".firstprivate.temp"); 14190 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(), 14191 RefExpr->getExprLoc()); 14192 AddInitializerToDecl(VDPrivate, 14193 DefaultLvalueConversion(VDInitRefExpr).get(), 14194 /*DirectInit=*/false); 14195 } 14196 if (VDPrivate->isInvalidDecl()) { 14197 if (IsImplicitClause) { 14198 Diag(RefExpr->getExprLoc(), 14199 diag::note_omp_task_predetermined_firstprivate_here); 14200 } 14201 continue; 14202 } 14203 CurContext->addDecl(VDPrivate); 14204 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 14205 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), 14206 RefExpr->getExprLoc()); 14207 DeclRefExpr *Ref = nullptr; 14208 if (!VD && !CurContext->isDependentContext()) { 14209 if (TopDVar.CKind == OMPC_lastprivate) { 14210 Ref = TopDVar.PrivateCopy; 14211 } else { 14212 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 14213 if (!isOpenMPCapturedDecl(D)) 14214 ExprCaptures.push_back(Ref->getDecl()); 14215 } 14216 } 14217 if (!IsImplicitClause) 14218 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 14219 Vars.push_back((VD || CurContext->isDependentContext()) 14220 ? RefExpr->IgnoreParens() 14221 : Ref); 14222 PrivateCopies.push_back(VDPrivateRefExpr); 14223 Inits.push_back(VDInitRefExpr); 14224 } 14225 14226 if (Vars.empty()) 14227 return nullptr; 14228 14229 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 14230 Vars, PrivateCopies, Inits, 14231 buildPreInits(Context, ExprCaptures)); 14232 } 14233 14234 OMPClause *Sema::ActOnOpenMPLastprivateClause( 14235 ArrayRef<Expr *> VarList, OpenMPLastprivateModifier LPKind, 14236 SourceLocation LPKindLoc, SourceLocation ColonLoc, SourceLocation StartLoc, 14237 SourceLocation LParenLoc, SourceLocation EndLoc) { 14238 if (LPKind == OMPC_LASTPRIVATE_unknown && LPKindLoc.isValid()) { 14239 assert(ColonLoc.isValid() && "Colon location must be valid."); 14240 Diag(LPKindLoc, diag::err_omp_unexpected_clause_value) 14241 << getListOfPossibleValues(OMPC_lastprivate, /*First=*/0, 14242 /*Last=*/OMPC_LASTPRIVATE_unknown) 14243 << getOpenMPClauseName(OMPC_lastprivate); 14244 return nullptr; 14245 } 14246 14247 SmallVector<Expr *, 8> Vars; 14248 SmallVector<Expr *, 8> SrcExprs; 14249 SmallVector<Expr *, 8> DstExprs; 14250 SmallVector<Expr *, 8> AssignmentOps; 14251 SmallVector<Decl *, 4> ExprCaptures; 14252 SmallVector<Expr *, 4> ExprPostUpdates; 14253 for (Expr *RefExpr : VarList) { 14254 assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); 14255 SourceLocation ELoc; 14256 SourceRange ERange; 14257 Expr *SimpleRefExpr = RefExpr; 14258 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 14259 if (Res.second) { 14260 // It will be analyzed later. 14261 Vars.push_back(RefExpr); 14262 SrcExprs.push_back(nullptr); 14263 DstExprs.push_back(nullptr); 14264 AssignmentOps.push_back(nullptr); 14265 } 14266 ValueDecl *D = Res.first; 14267 if (!D) 14268 continue; 14269 14270 QualType Type = D->getType(); 14271 auto *VD = dyn_cast<VarDecl>(D); 14272 14273 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2] 14274 // A variable that appears in a lastprivate clause must not have an 14275 // incomplete type or a reference type. 14276 if (RequireCompleteType(ELoc, Type, 14277 diag::err_omp_lastprivate_incomplete_type)) 14278 continue; 14279 Type = Type.getNonReferenceType(); 14280 14281 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions] 14282 // A variable that is privatized must not have a const-qualified type 14283 // unless it is of class type with a mutable member. This restriction does 14284 // not apply to the firstprivate clause. 14285 // 14286 // OpenMP 3.1 [2.9.3.5, lastprivate clause, Restrictions] 14287 // A variable that appears in a lastprivate clause must not have a 14288 // const-qualified type unless it is of class type with a mutable member. 14289 if (rejectConstNotMutableType(*this, D, Type, OMPC_lastprivate, ELoc)) 14290 continue; 14291 14292 // OpenMP 5.0 [2.19.4.5 lastprivate Clause, Restrictions] 14293 // A list item that appears in a lastprivate clause with the conditional 14294 // modifier must be a scalar variable. 14295 if (LPKind == OMPC_LASTPRIVATE_conditional && !Type->isScalarType()) { 14296 Diag(ELoc, diag::err_omp_lastprivate_conditional_non_scalar); 14297 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 14298 VarDecl::DeclarationOnly; 14299 Diag(D->getLocation(), 14300 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 14301 << D; 14302 continue; 14303 } 14304 14305 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 14306 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 14307 // in a Construct] 14308 // Variables with the predetermined data-sharing attributes may not be 14309 // listed in data-sharing attributes clauses, except for the cases 14310 // listed below. 14311 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3] 14312 // A list item may appear in a firstprivate or lastprivate clause but not 14313 // both. 14314 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 14315 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate && 14316 (isOpenMPDistributeDirective(CurrDir) || 14317 DVar.CKind != OMPC_firstprivate) && 14318 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) { 14319 Diag(ELoc, diag::err_omp_wrong_dsa) 14320 << getOpenMPClauseName(DVar.CKind) 14321 << getOpenMPClauseName(OMPC_lastprivate); 14322 reportOriginalDsa(*this, DSAStack, D, DVar); 14323 continue; 14324 } 14325 14326 // OpenMP [2.14.3.5, Restrictions, p.2] 14327 // A list item that is private within a parallel region, or that appears in 14328 // the reduction clause of a parallel construct, must not appear in a 14329 // lastprivate clause on a worksharing construct if any of the corresponding 14330 // worksharing regions ever binds to any of the corresponding parallel 14331 // regions. 14332 DSAStackTy::DSAVarData TopDVar = DVar; 14333 if (isOpenMPWorksharingDirective(CurrDir) && 14334 !isOpenMPParallelDirective(CurrDir) && 14335 !isOpenMPTeamsDirective(CurrDir)) { 14336 DVar = DSAStack->getImplicitDSA(D, true); 14337 if (DVar.CKind != OMPC_shared) { 14338 Diag(ELoc, diag::err_omp_required_access) 14339 << getOpenMPClauseName(OMPC_lastprivate) 14340 << getOpenMPClauseName(OMPC_shared); 14341 reportOriginalDsa(*this, DSAStack, D, DVar); 14342 continue; 14343 } 14344 } 14345 14346 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2] 14347 // A variable of class type (or array thereof) that appears in a 14348 // lastprivate clause requires an accessible, unambiguous default 14349 // constructor for the class type, unless the list item is also specified 14350 // in a firstprivate clause. 14351 // A variable of class type (or array thereof) that appears in a 14352 // lastprivate clause requires an accessible, unambiguous copy assignment 14353 // operator for the class type. 14354 Type = Context.getBaseElementType(Type).getNonReferenceType(); 14355 VarDecl *SrcVD = buildVarDecl(*this, ERange.getBegin(), 14356 Type.getUnqualifiedType(), ".lastprivate.src", 14357 D->hasAttrs() ? &D->getAttrs() : nullptr); 14358 DeclRefExpr *PseudoSrcExpr = 14359 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc); 14360 VarDecl *DstVD = 14361 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst", 14362 D->hasAttrs() ? &D->getAttrs() : nullptr); 14363 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc); 14364 // For arrays generate assignment operation for single element and replace 14365 // it by the original array element in CodeGen. 14366 ExprResult AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign, 14367 PseudoDstExpr, PseudoSrcExpr); 14368 if (AssignmentOp.isInvalid()) 14369 continue; 14370 AssignmentOp = 14371 ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false); 14372 if (AssignmentOp.isInvalid()) 14373 continue; 14374 14375 DeclRefExpr *Ref = nullptr; 14376 if (!VD && !CurContext->isDependentContext()) { 14377 if (TopDVar.CKind == OMPC_firstprivate) { 14378 Ref = TopDVar.PrivateCopy; 14379 } else { 14380 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 14381 if (!isOpenMPCapturedDecl(D)) 14382 ExprCaptures.push_back(Ref->getDecl()); 14383 } 14384 if (TopDVar.CKind == OMPC_firstprivate || 14385 (!isOpenMPCapturedDecl(D) && 14386 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) { 14387 ExprResult RefRes = DefaultLvalueConversion(Ref); 14388 if (!RefRes.isUsable()) 14389 continue; 14390 ExprResult PostUpdateRes = 14391 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr, 14392 RefRes.get()); 14393 if (!PostUpdateRes.isUsable()) 14394 continue; 14395 ExprPostUpdates.push_back( 14396 IgnoredValueConversions(PostUpdateRes.get()).get()); 14397 } 14398 } 14399 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref); 14400 Vars.push_back((VD || CurContext->isDependentContext()) 14401 ? RefExpr->IgnoreParens() 14402 : Ref); 14403 SrcExprs.push_back(PseudoSrcExpr); 14404 DstExprs.push_back(PseudoDstExpr); 14405 AssignmentOps.push_back(AssignmentOp.get()); 14406 } 14407 14408 if (Vars.empty()) 14409 return nullptr; 14410 14411 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 14412 Vars, SrcExprs, DstExprs, AssignmentOps, 14413 LPKind, LPKindLoc, ColonLoc, 14414 buildPreInits(Context, ExprCaptures), 14415 buildPostUpdate(*this, ExprPostUpdates)); 14416 } 14417 14418 OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList, 14419 SourceLocation StartLoc, 14420 SourceLocation LParenLoc, 14421 SourceLocation EndLoc) { 14422 SmallVector<Expr *, 8> Vars; 14423 for (Expr *RefExpr : VarList) { 14424 assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); 14425 SourceLocation ELoc; 14426 SourceRange ERange; 14427 Expr *SimpleRefExpr = RefExpr; 14428 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 14429 if (Res.second) { 14430 // It will be analyzed later. 14431 Vars.push_back(RefExpr); 14432 } 14433 ValueDecl *D = Res.first; 14434 if (!D) 14435 continue; 14436 14437 auto *VD = dyn_cast<VarDecl>(D); 14438 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 14439 // in a Construct] 14440 // Variables with the predetermined data-sharing attributes may not be 14441 // listed in data-sharing attributes clauses, except for the cases 14442 // listed below. For these exceptions only, listing a predetermined 14443 // variable in a data-sharing attribute clause is allowed and overrides 14444 // the variable's predetermined data-sharing attributes. 14445 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 14446 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared && 14447 DVar.RefExpr) { 14448 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 14449 << getOpenMPClauseName(OMPC_shared); 14450 reportOriginalDsa(*this, DSAStack, D, DVar); 14451 continue; 14452 } 14453 14454 DeclRefExpr *Ref = nullptr; 14455 if (!VD && isOpenMPCapturedDecl(D) && !CurContext->isDependentContext()) 14456 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 14457 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref); 14458 Vars.push_back((VD || !Ref || CurContext->isDependentContext()) 14459 ? RefExpr->IgnoreParens() 14460 : Ref); 14461 } 14462 14463 if (Vars.empty()) 14464 return nullptr; 14465 14466 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 14467 } 14468 14469 namespace { 14470 class DSARefChecker : public StmtVisitor<DSARefChecker, bool> { 14471 DSAStackTy *Stack; 14472 14473 public: 14474 bool VisitDeclRefExpr(DeclRefExpr *E) { 14475 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 14476 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false); 14477 if (DVar.CKind == OMPC_shared && !DVar.RefExpr) 14478 return false; 14479 if (DVar.CKind != OMPC_unknown) 14480 return true; 14481 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA( 14482 VD, 14483 [](OpenMPClauseKind C, bool AppliedToPointee) { 14484 return isOpenMPPrivate(C) && !AppliedToPointee; 14485 }, 14486 [](OpenMPDirectiveKind) { return true; }, 14487 /*FromParent=*/true); 14488 return DVarPrivate.CKind != OMPC_unknown; 14489 } 14490 return false; 14491 } 14492 bool VisitStmt(Stmt *S) { 14493 for (Stmt *Child : S->children()) { 14494 if (Child && Visit(Child)) 14495 return true; 14496 } 14497 return false; 14498 } 14499 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {} 14500 }; 14501 } // namespace 14502 14503 namespace { 14504 // Transform MemberExpression for specified FieldDecl of current class to 14505 // DeclRefExpr to specified OMPCapturedExprDecl. 14506 class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> { 14507 typedef TreeTransform<TransformExprToCaptures> BaseTransform; 14508 ValueDecl *Field = nullptr; 14509 DeclRefExpr *CapturedExpr = nullptr; 14510 14511 public: 14512 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl) 14513 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {} 14514 14515 ExprResult TransformMemberExpr(MemberExpr *E) { 14516 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) && 14517 E->getMemberDecl() == Field) { 14518 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false); 14519 return CapturedExpr; 14520 } 14521 return BaseTransform::TransformMemberExpr(E); 14522 } 14523 DeclRefExpr *getCapturedExpr() { return CapturedExpr; } 14524 }; 14525 } // namespace 14526 14527 template <typename T, typename U> 14528 static T filterLookupForUDReductionAndMapper( 14529 SmallVectorImpl<U> &Lookups, const llvm::function_ref<T(ValueDecl *)> Gen) { 14530 for (U &Set : Lookups) { 14531 for (auto *D : Set) { 14532 if (T Res = Gen(cast<ValueDecl>(D))) 14533 return Res; 14534 } 14535 } 14536 return T(); 14537 } 14538 14539 static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D) { 14540 assert(!LookupResult::isVisible(SemaRef, D) && "not in slow case"); 14541 14542 for (auto RD : D->redecls()) { 14543 // Don't bother with extra checks if we already know this one isn't visible. 14544 if (RD == D) 14545 continue; 14546 14547 auto ND = cast<NamedDecl>(RD); 14548 if (LookupResult::isVisible(SemaRef, ND)) 14549 return ND; 14550 } 14551 14552 return nullptr; 14553 } 14554 14555 static void 14556 argumentDependentLookup(Sema &SemaRef, const DeclarationNameInfo &Id, 14557 SourceLocation Loc, QualType Ty, 14558 SmallVectorImpl<UnresolvedSet<8>> &Lookups) { 14559 // Find all of the associated namespaces and classes based on the 14560 // arguments we have. 14561 Sema::AssociatedNamespaceSet AssociatedNamespaces; 14562 Sema::AssociatedClassSet AssociatedClasses; 14563 OpaqueValueExpr OVE(Loc, Ty, VK_LValue); 14564 SemaRef.FindAssociatedClassesAndNamespaces(Loc, &OVE, AssociatedNamespaces, 14565 AssociatedClasses); 14566 14567 // C++ [basic.lookup.argdep]p3: 14568 // Let X be the lookup set produced by unqualified lookup (3.4.1) 14569 // and let Y be the lookup set produced by argument dependent 14570 // lookup (defined as follows). If X contains [...] then Y is 14571 // empty. Otherwise Y is the set of declarations found in the 14572 // namespaces associated with the argument types as described 14573 // below. The set of declarations found by the lookup of the name 14574 // is the union of X and Y. 14575 // 14576 // Here, we compute Y and add its members to the overloaded 14577 // candidate set. 14578 for (auto *NS : AssociatedNamespaces) { 14579 // When considering an associated namespace, the lookup is the 14580 // same as the lookup performed when the associated namespace is 14581 // used as a qualifier (3.4.3.2) except that: 14582 // 14583 // -- Any using-directives in the associated namespace are 14584 // ignored. 14585 // 14586 // -- Any namespace-scope friend functions declared in 14587 // associated classes are visible within their respective 14588 // namespaces even if they are not visible during an ordinary 14589 // lookup (11.4). 14590 DeclContext::lookup_result R = NS->lookup(Id.getName()); 14591 for (auto *D : R) { 14592 auto *Underlying = D; 14593 if (auto *USD = dyn_cast<UsingShadowDecl>(D)) 14594 Underlying = USD->getTargetDecl(); 14595 14596 if (!isa<OMPDeclareReductionDecl>(Underlying) && 14597 !isa<OMPDeclareMapperDecl>(Underlying)) 14598 continue; 14599 14600 if (!SemaRef.isVisible(D)) { 14601 D = findAcceptableDecl(SemaRef, D); 14602 if (!D) 14603 continue; 14604 if (auto *USD = dyn_cast<UsingShadowDecl>(D)) 14605 Underlying = USD->getTargetDecl(); 14606 } 14607 Lookups.emplace_back(); 14608 Lookups.back().addDecl(Underlying); 14609 } 14610 } 14611 } 14612 14613 static ExprResult 14614 buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range, 14615 Scope *S, CXXScopeSpec &ReductionIdScopeSpec, 14616 const DeclarationNameInfo &ReductionId, QualType Ty, 14617 CXXCastPath &BasePath, Expr *UnresolvedReduction) { 14618 if (ReductionIdScopeSpec.isInvalid()) 14619 return ExprError(); 14620 SmallVector<UnresolvedSet<8>, 4> Lookups; 14621 if (S) { 14622 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName); 14623 Lookup.suppressDiagnostics(); 14624 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) { 14625 NamedDecl *D = Lookup.getRepresentativeDecl(); 14626 do { 14627 S = S->getParent(); 14628 } while (S && !S->isDeclScope(D)); 14629 if (S) 14630 S = S->getParent(); 14631 Lookups.emplace_back(); 14632 Lookups.back().append(Lookup.begin(), Lookup.end()); 14633 Lookup.clear(); 14634 } 14635 } else if (auto *ULE = 14636 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) { 14637 Lookups.push_back(UnresolvedSet<8>()); 14638 Decl *PrevD = nullptr; 14639 for (NamedDecl *D : ULE->decls()) { 14640 if (D == PrevD) 14641 Lookups.push_back(UnresolvedSet<8>()); 14642 else if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(D)) 14643 Lookups.back().addDecl(DRD); 14644 PrevD = D; 14645 } 14646 } 14647 if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() || 14648 Ty->isInstantiationDependentType() || 14649 Ty->containsUnexpandedParameterPack() || 14650 filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) { 14651 return !D->isInvalidDecl() && 14652 (D->getType()->isDependentType() || 14653 D->getType()->isInstantiationDependentType() || 14654 D->getType()->containsUnexpandedParameterPack()); 14655 })) { 14656 UnresolvedSet<8> ResSet; 14657 for (const UnresolvedSet<8> &Set : Lookups) { 14658 if (Set.empty()) 14659 continue; 14660 ResSet.append(Set.begin(), Set.end()); 14661 // The last item marks the end of all declarations at the specified scope. 14662 ResSet.addDecl(Set[Set.size() - 1]); 14663 } 14664 return UnresolvedLookupExpr::Create( 14665 SemaRef.Context, /*NamingClass=*/nullptr, 14666 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId, 14667 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end()); 14668 } 14669 // Lookup inside the classes. 14670 // C++ [over.match.oper]p3: 14671 // For a unary operator @ with an operand of a type whose 14672 // cv-unqualified version is T1, and for a binary operator @ with 14673 // a left operand of a type whose cv-unqualified version is T1 and 14674 // a right operand of a type whose cv-unqualified version is T2, 14675 // three sets of candidate functions, designated member 14676 // candidates, non-member candidates and built-in candidates, are 14677 // constructed as follows: 14678 // -- If T1 is a complete class type or a class currently being 14679 // defined, the set of member candidates is the result of the 14680 // qualified lookup of T1::operator@ (13.3.1.1.1); otherwise, 14681 // the set of member candidates is empty. 14682 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName); 14683 Lookup.suppressDiagnostics(); 14684 if (const auto *TyRec = Ty->getAs<RecordType>()) { 14685 // Complete the type if it can be completed. 14686 // If the type is neither complete nor being defined, bail out now. 14687 if (SemaRef.isCompleteType(Loc, Ty) || TyRec->isBeingDefined() || 14688 TyRec->getDecl()->getDefinition()) { 14689 Lookup.clear(); 14690 SemaRef.LookupQualifiedName(Lookup, TyRec->getDecl()); 14691 if (Lookup.empty()) { 14692 Lookups.emplace_back(); 14693 Lookups.back().append(Lookup.begin(), Lookup.end()); 14694 } 14695 } 14696 } 14697 // Perform ADL. 14698 if (SemaRef.getLangOpts().CPlusPlus) 14699 argumentDependentLookup(SemaRef, ReductionId, Loc, Ty, Lookups); 14700 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 14701 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * { 14702 if (!D->isInvalidDecl() && 14703 SemaRef.Context.hasSameType(D->getType(), Ty)) 14704 return D; 14705 return nullptr; 14706 })) 14707 return SemaRef.BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(), 14708 VK_LValue, Loc); 14709 if (SemaRef.getLangOpts().CPlusPlus) { 14710 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 14711 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * { 14712 if (!D->isInvalidDecl() && 14713 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) && 14714 !Ty.isMoreQualifiedThan(D->getType())) 14715 return D; 14716 return nullptr; 14717 })) { 14718 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, 14719 /*DetectVirtual=*/false); 14720 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) { 14721 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType( 14722 VD->getType().getUnqualifiedType()))) { 14723 if (SemaRef.CheckBaseClassAccess( 14724 Loc, VD->getType(), Ty, Paths.front(), 14725 /*DiagID=*/0) != Sema::AR_inaccessible) { 14726 SemaRef.BuildBasePathArray(Paths, BasePath); 14727 return SemaRef.BuildDeclRefExpr( 14728 VD, VD->getType().getNonReferenceType(), VK_LValue, Loc); 14729 } 14730 } 14731 } 14732 } 14733 } 14734 if (ReductionIdScopeSpec.isSet()) { 14735 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) 14736 << Ty << Range; 14737 return ExprError(); 14738 } 14739 return ExprEmpty(); 14740 } 14741 14742 namespace { 14743 /// Data for the reduction-based clauses. 14744 struct ReductionData { 14745 /// List of original reduction items. 14746 SmallVector<Expr *, 8> Vars; 14747 /// List of private copies of the reduction items. 14748 SmallVector<Expr *, 8> Privates; 14749 /// LHS expressions for the reduction_op expressions. 14750 SmallVector<Expr *, 8> LHSs; 14751 /// RHS expressions for the reduction_op expressions. 14752 SmallVector<Expr *, 8> RHSs; 14753 /// Reduction operation expression. 14754 SmallVector<Expr *, 8> ReductionOps; 14755 /// inscan copy operation expressions. 14756 SmallVector<Expr *, 8> InscanCopyOps; 14757 /// inscan copy temp array expressions for prefix sums. 14758 SmallVector<Expr *, 8> InscanCopyArrayTemps; 14759 /// inscan copy temp array element expressions for prefix sums. 14760 SmallVector<Expr *, 8> InscanCopyArrayElems; 14761 /// Taskgroup descriptors for the corresponding reduction items in 14762 /// in_reduction clauses. 14763 SmallVector<Expr *, 8> TaskgroupDescriptors; 14764 /// List of captures for clause. 14765 SmallVector<Decl *, 4> ExprCaptures; 14766 /// List of postupdate expressions. 14767 SmallVector<Expr *, 4> ExprPostUpdates; 14768 /// Reduction modifier. 14769 unsigned RedModifier = 0; 14770 ReductionData() = delete; 14771 /// Reserves required memory for the reduction data. 14772 ReductionData(unsigned Size, unsigned Modifier = 0) : RedModifier(Modifier) { 14773 Vars.reserve(Size); 14774 Privates.reserve(Size); 14775 LHSs.reserve(Size); 14776 RHSs.reserve(Size); 14777 ReductionOps.reserve(Size); 14778 if (RedModifier == OMPC_REDUCTION_inscan) { 14779 InscanCopyOps.reserve(Size); 14780 InscanCopyArrayTemps.reserve(Size); 14781 InscanCopyArrayElems.reserve(Size); 14782 } 14783 TaskgroupDescriptors.reserve(Size); 14784 ExprCaptures.reserve(Size); 14785 ExprPostUpdates.reserve(Size); 14786 } 14787 /// Stores reduction item and reduction operation only (required for dependent 14788 /// reduction item). 14789 void push(Expr *Item, Expr *ReductionOp) { 14790 Vars.emplace_back(Item); 14791 Privates.emplace_back(nullptr); 14792 LHSs.emplace_back(nullptr); 14793 RHSs.emplace_back(nullptr); 14794 ReductionOps.emplace_back(ReductionOp); 14795 TaskgroupDescriptors.emplace_back(nullptr); 14796 if (RedModifier == OMPC_REDUCTION_inscan) { 14797 InscanCopyOps.push_back(nullptr); 14798 InscanCopyArrayTemps.push_back(nullptr); 14799 InscanCopyArrayElems.push_back(nullptr); 14800 } 14801 } 14802 /// Stores reduction data. 14803 void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp, 14804 Expr *TaskgroupDescriptor, Expr *CopyOp, Expr *CopyArrayTemp, 14805 Expr *CopyArrayElem) { 14806 Vars.emplace_back(Item); 14807 Privates.emplace_back(Private); 14808 LHSs.emplace_back(LHS); 14809 RHSs.emplace_back(RHS); 14810 ReductionOps.emplace_back(ReductionOp); 14811 TaskgroupDescriptors.emplace_back(TaskgroupDescriptor); 14812 if (RedModifier == OMPC_REDUCTION_inscan) { 14813 InscanCopyOps.push_back(CopyOp); 14814 InscanCopyArrayTemps.push_back(CopyArrayTemp); 14815 InscanCopyArrayElems.push_back(CopyArrayElem); 14816 } else { 14817 assert(CopyOp == nullptr && CopyArrayTemp == nullptr && 14818 CopyArrayElem == nullptr && 14819 "Copy operation must be used for inscan reductions only."); 14820 } 14821 } 14822 }; 14823 } // namespace 14824 14825 static bool checkOMPArraySectionConstantForReduction( 14826 ASTContext &Context, const OMPArraySectionExpr *OASE, bool &SingleElement, 14827 SmallVectorImpl<llvm::APSInt> &ArraySizes) { 14828 const Expr *Length = OASE->getLength(); 14829 if (Length == nullptr) { 14830 // For array sections of the form [1:] or [:], we would need to analyze 14831 // the lower bound... 14832 if (OASE->getColonLocFirst().isValid()) 14833 return false; 14834 14835 // This is an array subscript which has implicit length 1! 14836 SingleElement = true; 14837 ArraySizes.push_back(llvm::APSInt::get(1)); 14838 } else { 14839 Expr::EvalResult Result; 14840 if (!Length->EvaluateAsInt(Result, Context)) 14841 return false; 14842 14843 llvm::APSInt ConstantLengthValue = Result.Val.getInt(); 14844 SingleElement = (ConstantLengthValue.getSExtValue() == 1); 14845 ArraySizes.push_back(ConstantLengthValue); 14846 } 14847 14848 // Get the base of this array section and walk up from there. 14849 const Expr *Base = OASE->getBase()->IgnoreParenImpCasts(); 14850 14851 // We require length = 1 for all array sections except the right-most to 14852 // guarantee that the memory region is contiguous and has no holes in it. 14853 while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) { 14854 Length = TempOASE->getLength(); 14855 if (Length == nullptr) { 14856 // For array sections of the form [1:] or [:], we would need to analyze 14857 // the lower bound... 14858 if (OASE->getColonLocFirst().isValid()) 14859 return false; 14860 14861 // This is an array subscript which has implicit length 1! 14862 ArraySizes.push_back(llvm::APSInt::get(1)); 14863 } else { 14864 Expr::EvalResult Result; 14865 if (!Length->EvaluateAsInt(Result, Context)) 14866 return false; 14867 14868 llvm::APSInt ConstantLengthValue = Result.Val.getInt(); 14869 if (ConstantLengthValue.getSExtValue() != 1) 14870 return false; 14871 14872 ArraySizes.push_back(ConstantLengthValue); 14873 } 14874 Base = TempOASE->getBase()->IgnoreParenImpCasts(); 14875 } 14876 14877 // If we have a single element, we don't need to add the implicit lengths. 14878 if (!SingleElement) { 14879 while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) { 14880 // Has implicit length 1! 14881 ArraySizes.push_back(llvm::APSInt::get(1)); 14882 Base = TempASE->getBase()->IgnoreParenImpCasts(); 14883 } 14884 } 14885 14886 // This array section can be privatized as a single value or as a constant 14887 // sized array. 14888 return true; 14889 } 14890 14891 static bool actOnOMPReductionKindClause( 14892 Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind, 14893 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 14894 SourceLocation ColonLoc, SourceLocation EndLoc, 14895 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 14896 ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) { 14897 DeclarationName DN = ReductionId.getName(); 14898 OverloadedOperatorKind OOK = DN.getCXXOverloadedOperator(); 14899 BinaryOperatorKind BOK = BO_Comma; 14900 14901 ASTContext &Context = S.Context; 14902 // OpenMP [2.14.3.6, reduction clause] 14903 // C 14904 // reduction-identifier is either an identifier or one of the following 14905 // operators: +, -, *, &, |, ^, && and || 14906 // C++ 14907 // reduction-identifier is either an id-expression or one of the following 14908 // operators: +, -, *, &, |, ^, && and || 14909 switch (OOK) { 14910 case OO_Plus: 14911 case OO_Minus: 14912 BOK = BO_Add; 14913 break; 14914 case OO_Star: 14915 BOK = BO_Mul; 14916 break; 14917 case OO_Amp: 14918 BOK = BO_And; 14919 break; 14920 case OO_Pipe: 14921 BOK = BO_Or; 14922 break; 14923 case OO_Caret: 14924 BOK = BO_Xor; 14925 break; 14926 case OO_AmpAmp: 14927 BOK = BO_LAnd; 14928 break; 14929 case OO_PipePipe: 14930 BOK = BO_LOr; 14931 break; 14932 case OO_New: 14933 case OO_Delete: 14934 case OO_Array_New: 14935 case OO_Array_Delete: 14936 case OO_Slash: 14937 case OO_Percent: 14938 case OO_Tilde: 14939 case OO_Exclaim: 14940 case OO_Equal: 14941 case OO_Less: 14942 case OO_Greater: 14943 case OO_LessEqual: 14944 case OO_GreaterEqual: 14945 case OO_PlusEqual: 14946 case OO_MinusEqual: 14947 case OO_StarEqual: 14948 case OO_SlashEqual: 14949 case OO_PercentEqual: 14950 case OO_CaretEqual: 14951 case OO_AmpEqual: 14952 case OO_PipeEqual: 14953 case OO_LessLess: 14954 case OO_GreaterGreater: 14955 case OO_LessLessEqual: 14956 case OO_GreaterGreaterEqual: 14957 case OO_EqualEqual: 14958 case OO_ExclaimEqual: 14959 case OO_Spaceship: 14960 case OO_PlusPlus: 14961 case OO_MinusMinus: 14962 case OO_Comma: 14963 case OO_ArrowStar: 14964 case OO_Arrow: 14965 case OO_Call: 14966 case OO_Subscript: 14967 case OO_Conditional: 14968 case OO_Coawait: 14969 case NUM_OVERLOADED_OPERATORS: 14970 llvm_unreachable("Unexpected reduction identifier"); 14971 case OO_None: 14972 if (IdentifierInfo *II = DN.getAsIdentifierInfo()) { 14973 if (II->isStr("max")) 14974 BOK = BO_GT; 14975 else if (II->isStr("min")) 14976 BOK = BO_LT; 14977 } 14978 break; 14979 } 14980 SourceRange ReductionIdRange; 14981 if (ReductionIdScopeSpec.isValid()) 14982 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc()); 14983 else 14984 ReductionIdRange.setBegin(ReductionId.getBeginLoc()); 14985 ReductionIdRange.setEnd(ReductionId.getEndLoc()); 14986 14987 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end(); 14988 bool FirstIter = true; 14989 for (Expr *RefExpr : VarList) { 14990 assert(RefExpr && "nullptr expr in OpenMP reduction clause."); 14991 // OpenMP [2.1, C/C++] 14992 // A list item is a variable or array section, subject to the restrictions 14993 // specified in Section 2.4 on page 42 and in each of the sections 14994 // describing clauses and directives for which a list appears. 14995 // OpenMP [2.14.3.3, Restrictions, p.1] 14996 // A variable that is part of another variable (as an array or 14997 // structure element) cannot appear in a private clause. 14998 if (!FirstIter && IR != ER) 14999 ++IR; 15000 FirstIter = false; 15001 SourceLocation ELoc; 15002 SourceRange ERange; 15003 Expr *SimpleRefExpr = RefExpr; 15004 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange, 15005 /*AllowArraySection=*/true); 15006 if (Res.second) { 15007 // Try to find 'declare reduction' corresponding construct before using 15008 // builtin/overloaded operators. 15009 QualType Type = Context.DependentTy; 15010 CXXCastPath BasePath; 15011 ExprResult DeclareReductionRef = buildDeclareReductionRef( 15012 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec, 15013 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR); 15014 Expr *ReductionOp = nullptr; 15015 if (S.CurContext->isDependentContext() && 15016 (DeclareReductionRef.isUnset() || 15017 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) 15018 ReductionOp = DeclareReductionRef.get(); 15019 // It will be analyzed later. 15020 RD.push(RefExpr, ReductionOp); 15021 } 15022 ValueDecl *D = Res.first; 15023 if (!D) 15024 continue; 15025 15026 Expr *TaskgroupDescriptor = nullptr; 15027 QualType Type; 15028 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens()); 15029 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens()); 15030 if (ASE) { 15031 Type = ASE->getType().getNonReferenceType(); 15032 } else if (OASE) { 15033 QualType BaseType = 15034 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 15035 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 15036 Type = ATy->getElementType(); 15037 else 15038 Type = BaseType->getPointeeType(); 15039 Type = Type.getNonReferenceType(); 15040 } else { 15041 Type = Context.getBaseElementType(D->getType().getNonReferenceType()); 15042 } 15043 auto *VD = dyn_cast<VarDecl>(D); 15044 15045 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 15046 // A variable that appears in a private clause must not have an incomplete 15047 // type or a reference type. 15048 if (S.RequireCompleteType(ELoc, D->getType(), 15049 diag::err_omp_reduction_incomplete_type)) 15050 continue; 15051 // OpenMP [2.14.3.6, reduction clause, Restrictions] 15052 // A list item that appears in a reduction clause must not be 15053 // const-qualified. 15054 if (rejectConstNotMutableType(S, D, Type, ClauseKind, ELoc, 15055 /*AcceptIfMutable*/ false, ASE || OASE)) 15056 continue; 15057 15058 OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective(); 15059 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4] 15060 // If a list-item is a reference type then it must bind to the same object 15061 // for all threads of the team. 15062 if (!ASE && !OASE) { 15063 if (VD) { 15064 VarDecl *VDDef = VD->getDefinition(); 15065 if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) { 15066 DSARefChecker Check(Stack); 15067 if (Check.Visit(VDDef->getInit())) { 15068 S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg) 15069 << getOpenMPClauseName(ClauseKind) << ERange; 15070 S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef; 15071 continue; 15072 } 15073 } 15074 } 15075 15076 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 15077 // in a Construct] 15078 // Variables with the predetermined data-sharing attributes may not be 15079 // listed in data-sharing attributes clauses, except for the cases 15080 // listed below. For these exceptions only, listing a predetermined 15081 // variable in a data-sharing attribute clause is allowed and overrides 15082 // the variable's predetermined data-sharing attributes. 15083 // OpenMP [2.14.3.6, Restrictions, p.3] 15084 // Any number of reduction clauses can be specified on the directive, 15085 // but a list item can appear only once in the reduction clauses for that 15086 // directive. 15087 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false); 15088 if (DVar.CKind == OMPC_reduction) { 15089 S.Diag(ELoc, diag::err_omp_once_referenced) 15090 << getOpenMPClauseName(ClauseKind); 15091 if (DVar.RefExpr) 15092 S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced); 15093 continue; 15094 } 15095 if (DVar.CKind != OMPC_unknown) { 15096 S.Diag(ELoc, diag::err_omp_wrong_dsa) 15097 << getOpenMPClauseName(DVar.CKind) 15098 << getOpenMPClauseName(OMPC_reduction); 15099 reportOriginalDsa(S, Stack, D, DVar); 15100 continue; 15101 } 15102 15103 // OpenMP [2.14.3.6, Restrictions, p.1] 15104 // A list item that appears in a reduction clause of a worksharing 15105 // construct must be shared in the parallel regions to which any of the 15106 // worksharing regions arising from the worksharing construct bind. 15107 if (isOpenMPWorksharingDirective(CurrDir) && 15108 !isOpenMPParallelDirective(CurrDir) && 15109 !isOpenMPTeamsDirective(CurrDir)) { 15110 DVar = Stack->getImplicitDSA(D, true); 15111 if (DVar.CKind != OMPC_shared) { 15112 S.Diag(ELoc, diag::err_omp_required_access) 15113 << getOpenMPClauseName(OMPC_reduction) 15114 << getOpenMPClauseName(OMPC_shared); 15115 reportOriginalDsa(S, Stack, D, DVar); 15116 continue; 15117 } 15118 } 15119 } 15120 15121 // Try to find 'declare reduction' corresponding construct before using 15122 // builtin/overloaded operators. 15123 CXXCastPath BasePath; 15124 ExprResult DeclareReductionRef = buildDeclareReductionRef( 15125 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec, 15126 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR); 15127 if (DeclareReductionRef.isInvalid()) 15128 continue; 15129 if (S.CurContext->isDependentContext() && 15130 (DeclareReductionRef.isUnset() || 15131 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) { 15132 RD.push(RefExpr, DeclareReductionRef.get()); 15133 continue; 15134 } 15135 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) { 15136 // Not allowed reduction identifier is found. 15137 S.Diag(ReductionId.getBeginLoc(), 15138 diag::err_omp_unknown_reduction_identifier) 15139 << Type << ReductionIdRange; 15140 continue; 15141 } 15142 15143 // OpenMP [2.14.3.6, reduction clause, Restrictions] 15144 // The type of a list item that appears in a reduction clause must be valid 15145 // for the reduction-identifier. For a max or min reduction in C, the type 15146 // of the list item must be an allowed arithmetic data type: char, int, 15147 // float, double, or _Bool, possibly modified with long, short, signed, or 15148 // unsigned. For a max or min reduction in C++, the type of the list item 15149 // must be an allowed arithmetic data type: char, wchar_t, int, float, 15150 // double, or bool, possibly modified with long, short, signed, or unsigned. 15151 if (DeclareReductionRef.isUnset()) { 15152 if ((BOK == BO_GT || BOK == BO_LT) && 15153 !(Type->isScalarType() || 15154 (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) { 15155 S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg) 15156 << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus; 15157 if (!ASE && !OASE) { 15158 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 15159 VarDecl::DeclarationOnly; 15160 S.Diag(D->getLocation(), 15161 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 15162 << D; 15163 } 15164 continue; 15165 } 15166 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) && 15167 !S.getLangOpts().CPlusPlus && Type->isFloatingType()) { 15168 S.Diag(ELoc, diag::err_omp_clause_floating_type_arg) 15169 << getOpenMPClauseName(ClauseKind); 15170 if (!ASE && !OASE) { 15171 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 15172 VarDecl::DeclarationOnly; 15173 S.Diag(D->getLocation(), 15174 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 15175 << D; 15176 } 15177 continue; 15178 } 15179 } 15180 15181 Type = Type.getNonLValueExprType(Context).getUnqualifiedType(); 15182 VarDecl *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs", 15183 D->hasAttrs() ? &D->getAttrs() : nullptr); 15184 VarDecl *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(), 15185 D->hasAttrs() ? &D->getAttrs() : nullptr); 15186 QualType PrivateTy = Type; 15187 15188 // Try if we can determine constant lengths for all array sections and avoid 15189 // the VLA. 15190 bool ConstantLengthOASE = false; 15191 if (OASE) { 15192 bool SingleElement; 15193 llvm::SmallVector<llvm::APSInt, 4> ArraySizes; 15194 ConstantLengthOASE = checkOMPArraySectionConstantForReduction( 15195 Context, OASE, SingleElement, ArraySizes); 15196 15197 // If we don't have a single element, we must emit a constant array type. 15198 if (ConstantLengthOASE && !SingleElement) { 15199 for (llvm::APSInt &Size : ArraySizes) 15200 PrivateTy = Context.getConstantArrayType(PrivateTy, Size, nullptr, 15201 ArrayType::Normal, 15202 /*IndexTypeQuals=*/0); 15203 } 15204 } 15205 15206 if ((OASE && !ConstantLengthOASE) || 15207 (!OASE && !ASE && 15208 D->getType().getNonReferenceType()->isVariablyModifiedType())) { 15209 if (!Context.getTargetInfo().isVLASupported()) { 15210 if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective())) { 15211 S.Diag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE; 15212 S.Diag(ELoc, diag::note_vla_unsupported); 15213 continue; 15214 } else { 15215 S.targetDiag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE; 15216 S.targetDiag(ELoc, diag::note_vla_unsupported); 15217 } 15218 } 15219 // For arrays/array sections only: 15220 // Create pseudo array type for private copy. The size for this array will 15221 // be generated during codegen. 15222 // For array subscripts or single variables Private Ty is the same as Type 15223 // (type of the variable or single array element). 15224 PrivateTy = Context.getVariableArrayType( 15225 Type, 15226 new (Context) OpaqueValueExpr(ELoc, Context.getSizeType(), VK_RValue), 15227 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange()); 15228 } else if (!ASE && !OASE && 15229 Context.getAsArrayType(D->getType().getNonReferenceType())) { 15230 PrivateTy = D->getType().getNonReferenceType(); 15231 } 15232 // Private copy. 15233 VarDecl *PrivateVD = 15234 buildVarDecl(S, ELoc, PrivateTy, D->getName(), 15235 D->hasAttrs() ? &D->getAttrs() : nullptr, 15236 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 15237 // Add initializer for private variable. 15238 Expr *Init = nullptr; 15239 DeclRefExpr *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc); 15240 DeclRefExpr *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc); 15241 if (DeclareReductionRef.isUsable()) { 15242 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>(); 15243 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl()); 15244 if (DRD->getInitializer()) { 15245 S.ActOnUninitializedDecl(PrivateVD); 15246 Init = DRDRef; 15247 RHSVD->setInit(DRDRef); 15248 RHSVD->setInitStyle(VarDecl::CallInit); 15249 } 15250 } else { 15251 switch (BOK) { 15252 case BO_Add: 15253 case BO_Xor: 15254 case BO_Or: 15255 case BO_LOr: 15256 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'. 15257 if (Type->isScalarType() || Type->isAnyComplexType()) 15258 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get(); 15259 break; 15260 case BO_Mul: 15261 case BO_LAnd: 15262 if (Type->isScalarType() || Type->isAnyComplexType()) { 15263 // '*' and '&&' reduction ops - initializer is '1'. 15264 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get(); 15265 } 15266 break; 15267 case BO_And: { 15268 // '&' reduction op - initializer is '~0'. 15269 QualType OrigType = Type; 15270 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) 15271 Type = ComplexTy->getElementType(); 15272 if (Type->isRealFloatingType()) { 15273 llvm::APFloat InitValue = llvm::APFloat::getAllOnesValue( 15274 Context.getFloatTypeSemantics(Type), 15275 Context.getTypeSize(Type)); 15276 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, 15277 Type, ELoc); 15278 } else if (Type->isScalarType()) { 15279 uint64_t Size = Context.getTypeSize(Type); 15280 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0); 15281 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size); 15282 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); 15283 } 15284 if (Init && OrigType->isAnyComplexType()) { 15285 // Init = 0xFFFF + 0xFFFFi; 15286 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType); 15287 Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get(); 15288 } 15289 Type = OrigType; 15290 break; 15291 } 15292 case BO_LT: 15293 case BO_GT: { 15294 // 'min' reduction op - initializer is 'Largest representable number in 15295 // the reduction list item type'. 15296 // 'max' reduction op - initializer is 'Least representable number in 15297 // the reduction list item type'. 15298 if (Type->isIntegerType() || Type->isPointerType()) { 15299 bool IsSigned = Type->hasSignedIntegerRepresentation(); 15300 uint64_t Size = Context.getTypeSize(Type); 15301 QualType IntTy = 15302 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned); 15303 llvm::APInt InitValue = 15304 (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size) 15305 : llvm::APInt::getMinValue(Size) 15306 : IsSigned ? llvm::APInt::getSignedMaxValue(Size) 15307 : llvm::APInt::getMaxValue(Size); 15308 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); 15309 if (Type->isPointerType()) { 15310 // Cast to pointer type. 15311 ExprResult CastExpr = S.BuildCStyleCastExpr( 15312 ELoc, Context.getTrivialTypeSourceInfo(Type, ELoc), ELoc, Init); 15313 if (CastExpr.isInvalid()) 15314 continue; 15315 Init = CastExpr.get(); 15316 } 15317 } else if (Type->isRealFloatingType()) { 15318 llvm::APFloat InitValue = llvm::APFloat::getLargest( 15319 Context.getFloatTypeSemantics(Type), BOK != BO_LT); 15320 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, 15321 Type, ELoc); 15322 } 15323 break; 15324 } 15325 case BO_PtrMemD: 15326 case BO_PtrMemI: 15327 case BO_MulAssign: 15328 case BO_Div: 15329 case BO_Rem: 15330 case BO_Sub: 15331 case BO_Shl: 15332 case BO_Shr: 15333 case BO_LE: 15334 case BO_GE: 15335 case BO_EQ: 15336 case BO_NE: 15337 case BO_Cmp: 15338 case BO_AndAssign: 15339 case BO_XorAssign: 15340 case BO_OrAssign: 15341 case BO_Assign: 15342 case BO_AddAssign: 15343 case BO_SubAssign: 15344 case BO_DivAssign: 15345 case BO_RemAssign: 15346 case BO_ShlAssign: 15347 case BO_ShrAssign: 15348 case BO_Comma: 15349 llvm_unreachable("Unexpected reduction operation"); 15350 } 15351 } 15352 if (Init && DeclareReductionRef.isUnset()) { 15353 S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false); 15354 // Store initializer for single element in private copy. Will be used 15355 // during codegen. 15356 PrivateVD->setInit(RHSVD->getInit()); 15357 PrivateVD->setInitStyle(RHSVD->getInitStyle()); 15358 } else if (!Init) { 15359 S.ActOnUninitializedDecl(RHSVD); 15360 // Store initializer for single element in private copy. Will be used 15361 // during codegen. 15362 PrivateVD->setInit(RHSVD->getInit()); 15363 PrivateVD->setInitStyle(RHSVD->getInitStyle()); 15364 } 15365 if (RHSVD->isInvalidDecl()) 15366 continue; 15367 if (!RHSVD->hasInit() && 15368 (DeclareReductionRef.isUnset() || !S.LangOpts.CPlusPlus)) { 15369 S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible) 15370 << Type << ReductionIdRange; 15371 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 15372 VarDecl::DeclarationOnly; 15373 S.Diag(D->getLocation(), 15374 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 15375 << D; 15376 continue; 15377 } 15378 DeclRefExpr *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc); 15379 ExprResult ReductionOp; 15380 if (DeclareReductionRef.isUsable()) { 15381 QualType RedTy = DeclareReductionRef.get()->getType(); 15382 QualType PtrRedTy = Context.getPointerType(RedTy); 15383 ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE); 15384 ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE); 15385 if (!BasePath.empty()) { 15386 LHS = S.DefaultLvalueConversion(LHS.get()); 15387 RHS = S.DefaultLvalueConversion(RHS.get()); 15388 LHS = ImplicitCastExpr::Create(Context, PtrRedTy, 15389 CK_UncheckedDerivedToBase, LHS.get(), 15390 &BasePath, LHS.get()->getValueKind()); 15391 RHS = ImplicitCastExpr::Create(Context, PtrRedTy, 15392 CK_UncheckedDerivedToBase, RHS.get(), 15393 &BasePath, RHS.get()->getValueKind()); 15394 } 15395 FunctionProtoType::ExtProtoInfo EPI; 15396 QualType Params[] = {PtrRedTy, PtrRedTy}; 15397 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI); 15398 auto *OVE = new (Context) OpaqueValueExpr( 15399 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary, 15400 S.DefaultLvalueConversion(DeclareReductionRef.get()).get()); 15401 Expr *Args[] = {LHS.get(), RHS.get()}; 15402 ReductionOp = 15403 CallExpr::Create(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc, 15404 S.CurFPFeatureOverrides()); 15405 } else { 15406 ReductionOp = S.BuildBinOp( 15407 Stack->getCurScope(), ReductionId.getBeginLoc(), BOK, LHSDRE, RHSDRE); 15408 if (ReductionOp.isUsable()) { 15409 if (BOK != BO_LT && BOK != BO_GT) { 15410 ReductionOp = 15411 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(), 15412 BO_Assign, LHSDRE, ReductionOp.get()); 15413 } else { 15414 auto *ConditionalOp = new (Context) 15415 ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc, RHSDRE, 15416 Type, VK_LValue, OK_Ordinary); 15417 ReductionOp = 15418 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(), 15419 BO_Assign, LHSDRE, ConditionalOp); 15420 } 15421 if (ReductionOp.isUsable()) 15422 ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get(), 15423 /*DiscardedValue*/ false); 15424 } 15425 if (!ReductionOp.isUsable()) 15426 continue; 15427 } 15428 15429 // Add copy operations for inscan reductions. 15430 // LHS = RHS; 15431 ExprResult CopyOpRes, TempArrayRes, TempArrayElem; 15432 if (ClauseKind == OMPC_reduction && 15433 RD.RedModifier == OMPC_REDUCTION_inscan) { 15434 ExprResult RHS = S.DefaultLvalueConversion(RHSDRE); 15435 CopyOpRes = S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, LHSDRE, 15436 RHS.get()); 15437 if (!CopyOpRes.isUsable()) 15438 continue; 15439 CopyOpRes = 15440 S.ActOnFinishFullExpr(CopyOpRes.get(), /*DiscardedValue=*/true); 15441 if (!CopyOpRes.isUsable()) 15442 continue; 15443 // For simd directive and simd-based directives in simd mode no need to 15444 // construct temp array, need just a single temp element. 15445 if (Stack->getCurrentDirective() == OMPD_simd || 15446 (S.getLangOpts().OpenMPSimd && 15447 isOpenMPSimdDirective(Stack->getCurrentDirective()))) { 15448 VarDecl *TempArrayVD = 15449 buildVarDecl(S, ELoc, PrivateTy, D->getName(), 15450 D->hasAttrs() ? &D->getAttrs() : nullptr); 15451 // Add a constructor to the temp decl. 15452 S.ActOnUninitializedDecl(TempArrayVD); 15453 TempArrayRes = buildDeclRefExpr(S, TempArrayVD, PrivateTy, ELoc); 15454 } else { 15455 // Build temp array for prefix sum. 15456 auto *Dim = new (S.Context) 15457 OpaqueValueExpr(ELoc, S.Context.getSizeType(), VK_RValue); 15458 QualType ArrayTy = 15459 S.Context.getVariableArrayType(PrivateTy, Dim, ArrayType::Normal, 15460 /*IndexTypeQuals=*/0, {ELoc, ELoc}); 15461 VarDecl *TempArrayVD = 15462 buildVarDecl(S, ELoc, ArrayTy, D->getName(), 15463 D->hasAttrs() ? &D->getAttrs() : nullptr); 15464 // Add a constructor to the temp decl. 15465 S.ActOnUninitializedDecl(TempArrayVD); 15466 TempArrayRes = buildDeclRefExpr(S, TempArrayVD, ArrayTy, ELoc); 15467 TempArrayElem = 15468 S.DefaultFunctionArrayLvalueConversion(TempArrayRes.get()); 15469 auto *Idx = new (S.Context) 15470 OpaqueValueExpr(ELoc, S.Context.getSizeType(), VK_RValue); 15471 TempArrayElem = S.CreateBuiltinArraySubscriptExpr(TempArrayElem.get(), 15472 ELoc, Idx, ELoc); 15473 } 15474 } 15475 15476 // OpenMP [2.15.4.6, Restrictions, p.2] 15477 // A list item that appears in an in_reduction clause of a task construct 15478 // must appear in a task_reduction clause of a construct associated with a 15479 // taskgroup region that includes the participating task in its taskgroup 15480 // set. The construct associated with the innermost region that meets this 15481 // condition must specify the same reduction-identifier as the in_reduction 15482 // clause. 15483 if (ClauseKind == OMPC_in_reduction) { 15484 SourceRange ParentSR; 15485 BinaryOperatorKind ParentBOK; 15486 const Expr *ParentReductionOp = nullptr; 15487 Expr *ParentBOKTD = nullptr, *ParentReductionOpTD = nullptr; 15488 DSAStackTy::DSAVarData ParentBOKDSA = 15489 Stack->getTopMostTaskgroupReductionData(D, ParentSR, ParentBOK, 15490 ParentBOKTD); 15491 DSAStackTy::DSAVarData ParentReductionOpDSA = 15492 Stack->getTopMostTaskgroupReductionData( 15493 D, ParentSR, ParentReductionOp, ParentReductionOpTD); 15494 bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown; 15495 bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown; 15496 if ((DeclareReductionRef.isUnset() && IsParentReductionOp) || 15497 (DeclareReductionRef.isUsable() && IsParentBOK) || 15498 (IsParentBOK && BOK != ParentBOK) || IsParentReductionOp) { 15499 bool EmitError = true; 15500 if (IsParentReductionOp && DeclareReductionRef.isUsable()) { 15501 llvm::FoldingSetNodeID RedId, ParentRedId; 15502 ParentReductionOp->Profile(ParentRedId, Context, /*Canonical=*/true); 15503 DeclareReductionRef.get()->Profile(RedId, Context, 15504 /*Canonical=*/true); 15505 EmitError = RedId != ParentRedId; 15506 } 15507 if (EmitError) { 15508 S.Diag(ReductionId.getBeginLoc(), 15509 diag::err_omp_reduction_identifier_mismatch) 15510 << ReductionIdRange << RefExpr->getSourceRange(); 15511 S.Diag(ParentSR.getBegin(), 15512 diag::note_omp_previous_reduction_identifier) 15513 << ParentSR 15514 << (IsParentBOK ? ParentBOKDSA.RefExpr 15515 : ParentReductionOpDSA.RefExpr) 15516 ->getSourceRange(); 15517 continue; 15518 } 15519 } 15520 TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD; 15521 } 15522 15523 DeclRefExpr *Ref = nullptr; 15524 Expr *VarsExpr = RefExpr->IgnoreParens(); 15525 if (!VD && !S.CurContext->isDependentContext()) { 15526 if (ASE || OASE) { 15527 TransformExprToCaptures RebuildToCapture(S, D); 15528 VarsExpr = 15529 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get(); 15530 Ref = RebuildToCapture.getCapturedExpr(); 15531 } else { 15532 VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false); 15533 } 15534 if (!S.isOpenMPCapturedDecl(D)) { 15535 RD.ExprCaptures.emplace_back(Ref->getDecl()); 15536 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) { 15537 ExprResult RefRes = S.DefaultLvalueConversion(Ref); 15538 if (!RefRes.isUsable()) 15539 continue; 15540 ExprResult PostUpdateRes = 15541 S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr, 15542 RefRes.get()); 15543 if (!PostUpdateRes.isUsable()) 15544 continue; 15545 if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) || 15546 Stack->getCurrentDirective() == OMPD_taskgroup) { 15547 S.Diag(RefExpr->getExprLoc(), 15548 diag::err_omp_reduction_non_addressable_expression) 15549 << RefExpr->getSourceRange(); 15550 continue; 15551 } 15552 RD.ExprPostUpdates.emplace_back( 15553 S.IgnoredValueConversions(PostUpdateRes.get()).get()); 15554 } 15555 } 15556 } 15557 // All reduction items are still marked as reduction (to do not increase 15558 // code base size). 15559 unsigned Modifier = RD.RedModifier; 15560 // Consider task_reductions as reductions with task modifier. Required for 15561 // correct analysis of in_reduction clauses. 15562 if (CurrDir == OMPD_taskgroup && ClauseKind == OMPC_task_reduction) 15563 Modifier = OMPC_REDUCTION_task; 15564 Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref, Modifier, 15565 ASE || OASE); 15566 if (Modifier == OMPC_REDUCTION_task && 15567 (CurrDir == OMPD_taskgroup || 15568 ((isOpenMPParallelDirective(CurrDir) || 15569 isOpenMPWorksharingDirective(CurrDir)) && 15570 !isOpenMPSimdDirective(CurrDir)))) { 15571 if (DeclareReductionRef.isUsable()) 15572 Stack->addTaskgroupReductionData(D, ReductionIdRange, 15573 DeclareReductionRef.get()); 15574 else 15575 Stack->addTaskgroupReductionData(D, ReductionIdRange, BOK); 15576 } 15577 RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get(), 15578 TaskgroupDescriptor, CopyOpRes.get(), TempArrayRes.get(), 15579 TempArrayElem.get()); 15580 } 15581 return RD.Vars.empty(); 15582 } 15583 15584 OMPClause *Sema::ActOnOpenMPReductionClause( 15585 ArrayRef<Expr *> VarList, OpenMPReductionClauseModifier Modifier, 15586 SourceLocation StartLoc, SourceLocation LParenLoc, 15587 SourceLocation ModifierLoc, SourceLocation ColonLoc, SourceLocation EndLoc, 15588 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 15589 ArrayRef<Expr *> UnresolvedReductions) { 15590 if (ModifierLoc.isValid() && Modifier == OMPC_REDUCTION_unknown) { 15591 Diag(LParenLoc, diag::err_omp_unexpected_clause_value) 15592 << getListOfPossibleValues(OMPC_reduction, /*First=*/0, 15593 /*Last=*/OMPC_REDUCTION_unknown) 15594 << getOpenMPClauseName(OMPC_reduction); 15595 return nullptr; 15596 } 15597 // OpenMP 5.0, 2.19.5.4 reduction Clause, Restrictions 15598 // A reduction clause with the inscan reduction-modifier may only appear on a 15599 // worksharing-loop construct, a worksharing-loop SIMD construct, a simd 15600 // construct, a parallel worksharing-loop construct or a parallel 15601 // worksharing-loop SIMD construct. 15602 if (Modifier == OMPC_REDUCTION_inscan && 15603 (DSAStack->getCurrentDirective() != OMPD_for && 15604 DSAStack->getCurrentDirective() != OMPD_for_simd && 15605 DSAStack->getCurrentDirective() != OMPD_simd && 15606 DSAStack->getCurrentDirective() != OMPD_parallel_for && 15607 DSAStack->getCurrentDirective() != OMPD_parallel_for_simd)) { 15608 Diag(ModifierLoc, diag::err_omp_wrong_inscan_reduction); 15609 return nullptr; 15610 } 15611 15612 ReductionData RD(VarList.size(), Modifier); 15613 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList, 15614 StartLoc, LParenLoc, ColonLoc, EndLoc, 15615 ReductionIdScopeSpec, ReductionId, 15616 UnresolvedReductions, RD)) 15617 return nullptr; 15618 15619 return OMPReductionClause::Create( 15620 Context, StartLoc, LParenLoc, ModifierLoc, ColonLoc, EndLoc, Modifier, 15621 RD.Vars, ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 15622 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.InscanCopyOps, 15623 RD.InscanCopyArrayTemps, RD.InscanCopyArrayElems, 15624 buildPreInits(Context, RD.ExprCaptures), 15625 buildPostUpdate(*this, RD.ExprPostUpdates)); 15626 } 15627 15628 OMPClause *Sema::ActOnOpenMPTaskReductionClause( 15629 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 15630 SourceLocation ColonLoc, SourceLocation EndLoc, 15631 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 15632 ArrayRef<Expr *> UnresolvedReductions) { 15633 ReductionData RD(VarList.size()); 15634 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction, VarList, 15635 StartLoc, LParenLoc, ColonLoc, EndLoc, 15636 ReductionIdScopeSpec, ReductionId, 15637 UnresolvedReductions, RD)) 15638 return nullptr; 15639 15640 return OMPTaskReductionClause::Create( 15641 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 15642 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 15643 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, 15644 buildPreInits(Context, RD.ExprCaptures), 15645 buildPostUpdate(*this, RD.ExprPostUpdates)); 15646 } 15647 15648 OMPClause *Sema::ActOnOpenMPInReductionClause( 15649 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 15650 SourceLocation ColonLoc, SourceLocation EndLoc, 15651 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 15652 ArrayRef<Expr *> UnresolvedReductions) { 15653 ReductionData RD(VarList.size()); 15654 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_in_reduction, VarList, 15655 StartLoc, LParenLoc, ColonLoc, EndLoc, 15656 ReductionIdScopeSpec, ReductionId, 15657 UnresolvedReductions, RD)) 15658 return nullptr; 15659 15660 return OMPInReductionClause::Create( 15661 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 15662 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 15663 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.TaskgroupDescriptors, 15664 buildPreInits(Context, RD.ExprCaptures), 15665 buildPostUpdate(*this, RD.ExprPostUpdates)); 15666 } 15667 15668 bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind, 15669 SourceLocation LinLoc) { 15670 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) || 15671 LinKind == OMPC_LINEAR_unknown) { 15672 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus; 15673 return true; 15674 } 15675 return false; 15676 } 15677 15678 bool Sema::CheckOpenMPLinearDecl(const ValueDecl *D, SourceLocation ELoc, 15679 OpenMPLinearClauseKind LinKind, QualType Type, 15680 bool IsDeclareSimd) { 15681 const auto *VD = dyn_cast_or_null<VarDecl>(D); 15682 // A variable must not have an incomplete type or a reference type. 15683 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type)) 15684 return true; 15685 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) && 15686 !Type->isReferenceType()) { 15687 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference) 15688 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind); 15689 return true; 15690 } 15691 Type = Type.getNonReferenceType(); 15692 15693 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions] 15694 // A variable that is privatized must not have a const-qualified type 15695 // unless it is of class type with a mutable member. This restriction does 15696 // not apply to the firstprivate clause, nor to the linear clause on 15697 // declarative directives (like declare simd). 15698 if (!IsDeclareSimd && 15699 rejectConstNotMutableType(*this, D, Type, OMPC_linear, ELoc)) 15700 return true; 15701 15702 // A list item must be of integral or pointer type. 15703 Type = Type.getUnqualifiedType().getCanonicalType(); 15704 const auto *Ty = Type.getTypePtrOrNull(); 15705 if (!Ty || (LinKind != OMPC_LINEAR_ref && !Ty->isDependentType() && 15706 !Ty->isIntegralType(Context) && !Ty->isPointerType())) { 15707 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type; 15708 if (D) { 15709 bool IsDecl = 15710 !VD || 15711 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 15712 Diag(D->getLocation(), 15713 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 15714 << D; 15715 } 15716 return true; 15717 } 15718 return false; 15719 } 15720 15721 OMPClause *Sema::ActOnOpenMPLinearClause( 15722 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc, 15723 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind, 15724 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 15725 SmallVector<Expr *, 8> Vars; 15726 SmallVector<Expr *, 8> Privates; 15727 SmallVector<Expr *, 8> Inits; 15728 SmallVector<Decl *, 4> ExprCaptures; 15729 SmallVector<Expr *, 4> ExprPostUpdates; 15730 if (CheckOpenMPLinearModifier(LinKind, LinLoc)) 15731 LinKind = OMPC_LINEAR_val; 15732 for (Expr *RefExpr : VarList) { 15733 assert(RefExpr && "NULL expr in OpenMP linear clause."); 15734 SourceLocation ELoc; 15735 SourceRange ERange; 15736 Expr *SimpleRefExpr = RefExpr; 15737 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 15738 if (Res.second) { 15739 // It will be analyzed later. 15740 Vars.push_back(RefExpr); 15741 Privates.push_back(nullptr); 15742 Inits.push_back(nullptr); 15743 } 15744 ValueDecl *D = Res.first; 15745 if (!D) 15746 continue; 15747 15748 QualType Type = D->getType(); 15749 auto *VD = dyn_cast<VarDecl>(D); 15750 15751 // OpenMP [2.14.3.7, linear clause] 15752 // A list-item cannot appear in more than one linear clause. 15753 // A list-item that appears in a linear clause cannot appear in any 15754 // other data-sharing attribute clause. 15755 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 15756 if (DVar.RefExpr) { 15757 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 15758 << getOpenMPClauseName(OMPC_linear); 15759 reportOriginalDsa(*this, DSAStack, D, DVar); 15760 continue; 15761 } 15762 15763 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type)) 15764 continue; 15765 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType(); 15766 15767 // Build private copy of original var. 15768 VarDecl *Private = 15769 buildVarDecl(*this, ELoc, Type, D->getName(), 15770 D->hasAttrs() ? &D->getAttrs() : nullptr, 15771 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 15772 DeclRefExpr *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc); 15773 // Build var to save initial value. 15774 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start"); 15775 Expr *InitExpr; 15776 DeclRefExpr *Ref = nullptr; 15777 if (!VD && !CurContext->isDependentContext()) { 15778 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 15779 if (!isOpenMPCapturedDecl(D)) { 15780 ExprCaptures.push_back(Ref->getDecl()); 15781 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) { 15782 ExprResult RefRes = DefaultLvalueConversion(Ref); 15783 if (!RefRes.isUsable()) 15784 continue; 15785 ExprResult PostUpdateRes = 15786 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, 15787 SimpleRefExpr, RefRes.get()); 15788 if (!PostUpdateRes.isUsable()) 15789 continue; 15790 ExprPostUpdates.push_back( 15791 IgnoredValueConversions(PostUpdateRes.get()).get()); 15792 } 15793 } 15794 } 15795 if (LinKind == OMPC_LINEAR_uval) 15796 InitExpr = VD ? VD->getInit() : SimpleRefExpr; 15797 else 15798 InitExpr = VD ? SimpleRefExpr : Ref; 15799 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(), 15800 /*DirectInit=*/false); 15801 DeclRefExpr *InitRef = buildDeclRefExpr(*this, Init, Type, ELoc); 15802 15803 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref); 15804 Vars.push_back((VD || CurContext->isDependentContext()) 15805 ? RefExpr->IgnoreParens() 15806 : Ref); 15807 Privates.push_back(PrivateRef); 15808 Inits.push_back(InitRef); 15809 } 15810 15811 if (Vars.empty()) 15812 return nullptr; 15813 15814 Expr *StepExpr = Step; 15815 Expr *CalcStepExpr = nullptr; 15816 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && 15817 !Step->isInstantiationDependent() && 15818 !Step->containsUnexpandedParameterPack()) { 15819 SourceLocation StepLoc = Step->getBeginLoc(); 15820 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step); 15821 if (Val.isInvalid()) 15822 return nullptr; 15823 StepExpr = Val.get(); 15824 15825 // Build var to save the step value. 15826 VarDecl *SaveVar = 15827 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step"); 15828 ExprResult SaveRef = 15829 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc); 15830 ExprResult CalcStep = 15831 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr); 15832 CalcStep = ActOnFinishFullExpr(CalcStep.get(), /*DiscardedValue*/ false); 15833 15834 // Warn about zero linear step (it would be probably better specified as 15835 // making corresponding variables 'const'). 15836 if (Optional<llvm::APSInt> Result = 15837 StepExpr->getIntegerConstantExpr(Context)) { 15838 if (!Result->isNegative() && !Result->isStrictlyPositive()) 15839 Diag(StepLoc, diag::warn_omp_linear_step_zero) 15840 << Vars[0] << (Vars.size() > 1); 15841 } else if (CalcStep.isUsable()) { 15842 // Calculate the step beforehand instead of doing this on each iteration. 15843 // (This is not used if the number of iterations may be kfold-ed). 15844 CalcStepExpr = CalcStep.get(); 15845 } 15846 } 15847 15848 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc, 15849 ColonLoc, EndLoc, Vars, Privates, Inits, 15850 StepExpr, CalcStepExpr, 15851 buildPreInits(Context, ExprCaptures), 15852 buildPostUpdate(*this, ExprPostUpdates)); 15853 } 15854 15855 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, 15856 Expr *NumIterations, Sema &SemaRef, 15857 Scope *S, DSAStackTy *Stack) { 15858 // Walk the vars and build update/final expressions for the CodeGen. 15859 SmallVector<Expr *, 8> Updates; 15860 SmallVector<Expr *, 8> Finals; 15861 SmallVector<Expr *, 8> UsedExprs; 15862 Expr *Step = Clause.getStep(); 15863 Expr *CalcStep = Clause.getCalcStep(); 15864 // OpenMP [2.14.3.7, linear clause] 15865 // If linear-step is not specified it is assumed to be 1. 15866 if (!Step) 15867 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(); 15868 else if (CalcStep) 15869 Step = cast<BinaryOperator>(CalcStep)->getLHS(); 15870 bool HasErrors = false; 15871 auto CurInit = Clause.inits().begin(); 15872 auto CurPrivate = Clause.privates().begin(); 15873 OpenMPLinearClauseKind LinKind = Clause.getModifier(); 15874 for (Expr *RefExpr : Clause.varlists()) { 15875 SourceLocation ELoc; 15876 SourceRange ERange; 15877 Expr *SimpleRefExpr = RefExpr; 15878 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange); 15879 ValueDecl *D = Res.first; 15880 if (Res.second || !D) { 15881 Updates.push_back(nullptr); 15882 Finals.push_back(nullptr); 15883 HasErrors = true; 15884 continue; 15885 } 15886 auto &&Info = Stack->isLoopControlVariable(D); 15887 // OpenMP [2.15.11, distribute simd Construct] 15888 // A list item may not appear in a linear clause, unless it is the loop 15889 // iteration variable. 15890 if (isOpenMPDistributeDirective(Stack->getCurrentDirective()) && 15891 isOpenMPSimdDirective(Stack->getCurrentDirective()) && !Info.first) { 15892 SemaRef.Diag(ELoc, 15893 diag::err_omp_linear_distribute_var_non_loop_iteration); 15894 Updates.push_back(nullptr); 15895 Finals.push_back(nullptr); 15896 HasErrors = true; 15897 continue; 15898 } 15899 Expr *InitExpr = *CurInit; 15900 15901 // Build privatized reference to the current linear var. 15902 auto *DE = cast<DeclRefExpr>(SimpleRefExpr); 15903 Expr *CapturedRef; 15904 if (LinKind == OMPC_LINEAR_uval) 15905 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit(); 15906 else 15907 CapturedRef = 15908 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()), 15909 DE->getType().getUnqualifiedType(), DE->getExprLoc(), 15910 /*RefersToCapture=*/true); 15911 15912 // Build update: Var = InitExpr + IV * Step 15913 ExprResult Update; 15914 if (!Info.first) 15915 Update = buildCounterUpdate( 15916 SemaRef, S, RefExpr->getExprLoc(), *CurPrivate, InitExpr, IV, Step, 15917 /*Subtract=*/false, /*IsNonRectangularLB=*/false); 15918 else 15919 Update = *CurPrivate; 15920 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getBeginLoc(), 15921 /*DiscardedValue*/ false); 15922 15923 // Build final: Var = InitExpr + NumIterations * Step 15924 ExprResult Final; 15925 if (!Info.first) 15926 Final = 15927 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef, 15928 InitExpr, NumIterations, Step, /*Subtract=*/false, 15929 /*IsNonRectangularLB=*/false); 15930 else 15931 Final = *CurPrivate; 15932 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getBeginLoc(), 15933 /*DiscardedValue*/ false); 15934 15935 if (!Update.isUsable() || !Final.isUsable()) { 15936 Updates.push_back(nullptr); 15937 Finals.push_back(nullptr); 15938 UsedExprs.push_back(nullptr); 15939 HasErrors = true; 15940 } else { 15941 Updates.push_back(Update.get()); 15942 Finals.push_back(Final.get()); 15943 if (!Info.first) 15944 UsedExprs.push_back(SimpleRefExpr); 15945 } 15946 ++CurInit; 15947 ++CurPrivate; 15948 } 15949 if (Expr *S = Clause.getStep()) 15950 UsedExprs.push_back(S); 15951 // Fill the remaining part with the nullptr. 15952 UsedExprs.append(Clause.varlist_size() + 1 - UsedExprs.size(), nullptr); 15953 Clause.setUpdates(Updates); 15954 Clause.setFinals(Finals); 15955 Clause.setUsedExprs(UsedExprs); 15956 return HasErrors; 15957 } 15958 15959 OMPClause *Sema::ActOnOpenMPAlignedClause( 15960 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc, 15961 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 15962 SmallVector<Expr *, 8> Vars; 15963 for (Expr *RefExpr : VarList) { 15964 assert(RefExpr && "NULL expr in OpenMP linear clause."); 15965 SourceLocation ELoc; 15966 SourceRange ERange; 15967 Expr *SimpleRefExpr = RefExpr; 15968 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 15969 if (Res.second) { 15970 // It will be analyzed later. 15971 Vars.push_back(RefExpr); 15972 } 15973 ValueDecl *D = Res.first; 15974 if (!D) 15975 continue; 15976 15977 QualType QType = D->getType(); 15978 auto *VD = dyn_cast<VarDecl>(D); 15979 15980 // OpenMP [2.8.1, simd construct, Restrictions] 15981 // The type of list items appearing in the aligned clause must be 15982 // array, pointer, reference to array, or reference to pointer. 15983 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType(); 15984 const Type *Ty = QType.getTypePtrOrNull(); 15985 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) { 15986 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr) 15987 << QType << getLangOpts().CPlusPlus << ERange; 15988 bool IsDecl = 15989 !VD || 15990 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 15991 Diag(D->getLocation(), 15992 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 15993 << D; 15994 continue; 15995 } 15996 15997 // OpenMP [2.8.1, simd construct, Restrictions] 15998 // A list-item cannot appear in more than one aligned clause. 15999 if (const Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) { 16000 Diag(ELoc, diag::err_omp_used_in_clause_twice) 16001 << 0 << getOpenMPClauseName(OMPC_aligned) << ERange; 16002 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa) 16003 << getOpenMPClauseName(OMPC_aligned); 16004 continue; 16005 } 16006 16007 DeclRefExpr *Ref = nullptr; 16008 if (!VD && isOpenMPCapturedDecl(D)) 16009 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 16010 Vars.push_back(DefaultFunctionArrayConversion( 16011 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref) 16012 .get()); 16013 } 16014 16015 // OpenMP [2.8.1, simd construct, Description] 16016 // The parameter of the aligned clause, alignment, must be a constant 16017 // positive integer expression. 16018 // If no optional parameter is specified, implementation-defined default 16019 // alignments for SIMD instructions on the target platforms are assumed. 16020 if (Alignment != nullptr) { 16021 ExprResult AlignResult = 16022 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned); 16023 if (AlignResult.isInvalid()) 16024 return nullptr; 16025 Alignment = AlignResult.get(); 16026 } 16027 if (Vars.empty()) 16028 return nullptr; 16029 16030 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc, 16031 EndLoc, Vars, Alignment); 16032 } 16033 16034 OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList, 16035 SourceLocation StartLoc, 16036 SourceLocation LParenLoc, 16037 SourceLocation EndLoc) { 16038 SmallVector<Expr *, 8> Vars; 16039 SmallVector<Expr *, 8> SrcExprs; 16040 SmallVector<Expr *, 8> DstExprs; 16041 SmallVector<Expr *, 8> AssignmentOps; 16042 for (Expr *RefExpr : VarList) { 16043 assert(RefExpr && "NULL expr in OpenMP copyin clause."); 16044 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 16045 // It will be analyzed later. 16046 Vars.push_back(RefExpr); 16047 SrcExprs.push_back(nullptr); 16048 DstExprs.push_back(nullptr); 16049 AssignmentOps.push_back(nullptr); 16050 continue; 16051 } 16052 16053 SourceLocation ELoc = RefExpr->getExprLoc(); 16054 // OpenMP [2.1, C/C++] 16055 // A list item is a variable name. 16056 // OpenMP [2.14.4.1, Restrictions, p.1] 16057 // A list item that appears in a copyin clause must be threadprivate. 16058 auto *DE = dyn_cast<DeclRefExpr>(RefExpr); 16059 if (!DE || !isa<VarDecl>(DE->getDecl())) { 16060 Diag(ELoc, diag::err_omp_expected_var_name_member_expr) 16061 << 0 << RefExpr->getSourceRange(); 16062 continue; 16063 } 16064 16065 Decl *D = DE->getDecl(); 16066 auto *VD = cast<VarDecl>(D); 16067 16068 QualType Type = VD->getType(); 16069 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 16070 // It will be analyzed later. 16071 Vars.push_back(DE); 16072 SrcExprs.push_back(nullptr); 16073 DstExprs.push_back(nullptr); 16074 AssignmentOps.push_back(nullptr); 16075 continue; 16076 } 16077 16078 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1] 16079 // A list item that appears in a copyin clause must be threadprivate. 16080 if (!DSAStack->isThreadPrivate(VD)) { 16081 Diag(ELoc, diag::err_omp_required_access) 16082 << getOpenMPClauseName(OMPC_copyin) 16083 << getOpenMPDirectiveName(OMPD_threadprivate); 16084 continue; 16085 } 16086 16087 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 16088 // A variable of class type (or array thereof) that appears in a 16089 // copyin clause requires an accessible, unambiguous copy assignment 16090 // operator for the class type. 16091 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType(); 16092 VarDecl *SrcVD = 16093 buildVarDecl(*this, DE->getBeginLoc(), ElemType.getUnqualifiedType(), 16094 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr); 16095 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr( 16096 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc()); 16097 VarDecl *DstVD = 16098 buildVarDecl(*this, DE->getBeginLoc(), ElemType, ".copyin.dst", 16099 VD->hasAttrs() ? &VD->getAttrs() : nullptr); 16100 DeclRefExpr *PseudoDstExpr = 16101 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc()); 16102 // For arrays generate assignment operation for single element and replace 16103 // it by the original array element in CodeGen. 16104 ExprResult AssignmentOp = 16105 BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, PseudoDstExpr, 16106 PseudoSrcExpr); 16107 if (AssignmentOp.isInvalid()) 16108 continue; 16109 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(), 16110 /*DiscardedValue*/ false); 16111 if (AssignmentOp.isInvalid()) 16112 continue; 16113 16114 DSAStack->addDSA(VD, DE, OMPC_copyin); 16115 Vars.push_back(DE); 16116 SrcExprs.push_back(PseudoSrcExpr); 16117 DstExprs.push_back(PseudoDstExpr); 16118 AssignmentOps.push_back(AssignmentOp.get()); 16119 } 16120 16121 if (Vars.empty()) 16122 return nullptr; 16123 16124 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 16125 SrcExprs, DstExprs, AssignmentOps); 16126 } 16127 16128 OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList, 16129 SourceLocation StartLoc, 16130 SourceLocation LParenLoc, 16131 SourceLocation EndLoc) { 16132 SmallVector<Expr *, 8> Vars; 16133 SmallVector<Expr *, 8> SrcExprs; 16134 SmallVector<Expr *, 8> DstExprs; 16135 SmallVector<Expr *, 8> AssignmentOps; 16136 for (Expr *RefExpr : VarList) { 16137 assert(RefExpr && "NULL expr in OpenMP linear clause."); 16138 SourceLocation ELoc; 16139 SourceRange ERange; 16140 Expr *SimpleRefExpr = RefExpr; 16141 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 16142 if (Res.second) { 16143 // It will be analyzed later. 16144 Vars.push_back(RefExpr); 16145 SrcExprs.push_back(nullptr); 16146 DstExprs.push_back(nullptr); 16147 AssignmentOps.push_back(nullptr); 16148 } 16149 ValueDecl *D = Res.first; 16150 if (!D) 16151 continue; 16152 16153 QualType Type = D->getType(); 16154 auto *VD = dyn_cast<VarDecl>(D); 16155 16156 // OpenMP [2.14.4.2, Restrictions, p.2] 16157 // A list item that appears in a copyprivate clause may not appear in a 16158 // private or firstprivate clause on the single construct. 16159 if (!VD || !DSAStack->isThreadPrivate(VD)) { 16160 DSAStackTy::DSAVarData DVar = 16161 DSAStack->getTopDSA(D, /*FromParent=*/false); 16162 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate && 16163 DVar.RefExpr) { 16164 Diag(ELoc, diag::err_omp_wrong_dsa) 16165 << getOpenMPClauseName(DVar.CKind) 16166 << getOpenMPClauseName(OMPC_copyprivate); 16167 reportOriginalDsa(*this, DSAStack, D, DVar); 16168 continue; 16169 } 16170 16171 // OpenMP [2.11.4.2, Restrictions, p.1] 16172 // All list items that appear in a copyprivate clause must be either 16173 // threadprivate or private in the enclosing context. 16174 if (DVar.CKind == OMPC_unknown) { 16175 DVar = DSAStack->getImplicitDSA(D, false); 16176 if (DVar.CKind == OMPC_shared) { 16177 Diag(ELoc, diag::err_omp_required_access) 16178 << getOpenMPClauseName(OMPC_copyprivate) 16179 << "threadprivate or private in the enclosing context"; 16180 reportOriginalDsa(*this, DSAStack, D, DVar); 16181 continue; 16182 } 16183 } 16184 } 16185 16186 // Variably modified types are not supported. 16187 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) { 16188 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 16189 << getOpenMPClauseName(OMPC_copyprivate) << Type 16190 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 16191 bool IsDecl = 16192 !VD || 16193 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 16194 Diag(D->getLocation(), 16195 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 16196 << D; 16197 continue; 16198 } 16199 16200 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 16201 // A variable of class type (or array thereof) that appears in a 16202 // copyin clause requires an accessible, unambiguous copy assignment 16203 // operator for the class type. 16204 Type = Context.getBaseElementType(Type.getNonReferenceType()) 16205 .getUnqualifiedType(); 16206 VarDecl *SrcVD = 16207 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.src", 16208 D->hasAttrs() ? &D->getAttrs() : nullptr); 16209 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc); 16210 VarDecl *DstVD = 16211 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.dst", 16212 D->hasAttrs() ? &D->getAttrs() : nullptr); 16213 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc); 16214 ExprResult AssignmentOp = BuildBinOp( 16215 DSAStack->getCurScope(), ELoc, BO_Assign, PseudoDstExpr, PseudoSrcExpr); 16216 if (AssignmentOp.isInvalid()) 16217 continue; 16218 AssignmentOp = 16219 ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false); 16220 if (AssignmentOp.isInvalid()) 16221 continue; 16222 16223 // No need to mark vars as copyprivate, they are already threadprivate or 16224 // implicitly private. 16225 assert(VD || isOpenMPCapturedDecl(D)); 16226 Vars.push_back( 16227 VD ? RefExpr->IgnoreParens() 16228 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false)); 16229 SrcExprs.push_back(PseudoSrcExpr); 16230 DstExprs.push_back(PseudoDstExpr); 16231 AssignmentOps.push_back(AssignmentOp.get()); 16232 } 16233 16234 if (Vars.empty()) 16235 return nullptr; 16236 16237 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 16238 Vars, SrcExprs, DstExprs, AssignmentOps); 16239 } 16240 16241 OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList, 16242 SourceLocation StartLoc, 16243 SourceLocation LParenLoc, 16244 SourceLocation EndLoc) { 16245 if (VarList.empty()) 16246 return nullptr; 16247 16248 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList); 16249 } 16250 16251 /// Tries to find omp_depend_t. type. 16252 static bool findOMPDependT(Sema &S, SourceLocation Loc, DSAStackTy *Stack, 16253 bool Diagnose = true) { 16254 QualType OMPDependT = Stack->getOMPDependT(); 16255 if (!OMPDependT.isNull()) 16256 return true; 16257 IdentifierInfo *II = &S.PP.getIdentifierTable().get("omp_depend_t"); 16258 ParsedType PT = S.getTypeName(*II, Loc, S.getCurScope()); 16259 if (!PT.getAsOpaquePtr() || PT.get().isNull()) { 16260 if (Diagnose) 16261 S.Diag(Loc, diag::err_omp_implied_type_not_found) << "omp_depend_t"; 16262 return false; 16263 } 16264 Stack->setOMPDependT(PT.get()); 16265 return true; 16266 } 16267 16268 OMPClause *Sema::ActOnOpenMPDepobjClause(Expr *Depobj, SourceLocation StartLoc, 16269 SourceLocation LParenLoc, 16270 SourceLocation EndLoc) { 16271 if (!Depobj) 16272 return nullptr; 16273 16274 bool OMPDependTFound = findOMPDependT(*this, StartLoc, DSAStack); 16275 16276 // OpenMP 5.0, 2.17.10.1 depobj Construct 16277 // depobj is an lvalue expression of type omp_depend_t. 16278 if (!Depobj->isTypeDependent() && !Depobj->isValueDependent() && 16279 !Depobj->isInstantiationDependent() && 16280 !Depobj->containsUnexpandedParameterPack() && 16281 (OMPDependTFound && 16282 !Context.typesAreCompatible(DSAStack->getOMPDependT(), Depobj->getType(), 16283 /*CompareUnqualified=*/true))) { 16284 Diag(Depobj->getExprLoc(), diag::err_omp_expected_omp_depend_t_lvalue) 16285 << 0 << Depobj->getType() << Depobj->getSourceRange(); 16286 } 16287 16288 if (!Depobj->isLValue()) { 16289 Diag(Depobj->getExprLoc(), diag::err_omp_expected_omp_depend_t_lvalue) 16290 << 1 << Depobj->getSourceRange(); 16291 } 16292 16293 return OMPDepobjClause::Create(Context, StartLoc, LParenLoc, EndLoc, Depobj); 16294 } 16295 16296 OMPClause * 16297 Sema::ActOnOpenMPDependClause(Expr *DepModifier, OpenMPDependClauseKind DepKind, 16298 SourceLocation DepLoc, SourceLocation ColonLoc, 16299 ArrayRef<Expr *> VarList, SourceLocation StartLoc, 16300 SourceLocation LParenLoc, SourceLocation EndLoc) { 16301 if (DSAStack->getCurrentDirective() == OMPD_ordered && 16302 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) { 16303 Diag(DepLoc, diag::err_omp_unexpected_clause_value) 16304 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend); 16305 return nullptr; 16306 } 16307 if ((DSAStack->getCurrentDirective() != OMPD_ordered || 16308 DSAStack->getCurrentDirective() == OMPD_depobj) && 16309 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source || 16310 DepKind == OMPC_DEPEND_sink || 16311 ((LangOpts.OpenMP < 50 || 16312 DSAStack->getCurrentDirective() == OMPD_depobj) && 16313 DepKind == OMPC_DEPEND_depobj))) { 16314 SmallVector<unsigned, 3> Except; 16315 Except.push_back(OMPC_DEPEND_source); 16316 Except.push_back(OMPC_DEPEND_sink); 16317 if (LangOpts.OpenMP < 50 || DSAStack->getCurrentDirective() == OMPD_depobj) 16318 Except.push_back(OMPC_DEPEND_depobj); 16319 std::string Expected = (LangOpts.OpenMP >= 50 && !DepModifier) 16320 ? "depend modifier(iterator) or " 16321 : ""; 16322 Diag(DepLoc, diag::err_omp_unexpected_clause_value) 16323 << Expected + getListOfPossibleValues(OMPC_depend, /*First=*/0, 16324 /*Last=*/OMPC_DEPEND_unknown, 16325 Except) 16326 << getOpenMPClauseName(OMPC_depend); 16327 return nullptr; 16328 } 16329 if (DepModifier && 16330 (DepKind == OMPC_DEPEND_source || DepKind == OMPC_DEPEND_sink)) { 16331 Diag(DepModifier->getExprLoc(), 16332 diag::err_omp_depend_sink_source_with_modifier); 16333 return nullptr; 16334 } 16335 if (DepModifier && 16336 !DepModifier->getType()->isSpecificBuiltinType(BuiltinType::OMPIterator)) 16337 Diag(DepModifier->getExprLoc(), diag::err_omp_depend_modifier_not_iterator); 16338 16339 SmallVector<Expr *, 8> Vars; 16340 DSAStackTy::OperatorOffsetTy OpsOffs; 16341 llvm::APSInt DepCounter(/*BitWidth=*/32); 16342 llvm::APSInt TotalDepCount(/*BitWidth=*/32); 16343 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) { 16344 if (const Expr *OrderedCountExpr = 16345 DSAStack->getParentOrderedRegionParam().first) { 16346 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context); 16347 TotalDepCount.setIsUnsigned(/*Val=*/true); 16348 } 16349 } 16350 for (Expr *RefExpr : VarList) { 16351 assert(RefExpr && "NULL expr in OpenMP shared clause."); 16352 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 16353 // It will be analyzed later. 16354 Vars.push_back(RefExpr); 16355 continue; 16356 } 16357 16358 SourceLocation ELoc = RefExpr->getExprLoc(); 16359 Expr *SimpleExpr = RefExpr->IgnoreParenCasts(); 16360 if (DepKind == OMPC_DEPEND_sink) { 16361 if (DSAStack->getParentOrderedRegionParam().first && 16362 DepCounter >= TotalDepCount) { 16363 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr); 16364 continue; 16365 } 16366 ++DepCounter; 16367 // OpenMP [2.13.9, Summary] 16368 // depend(dependence-type : vec), where dependence-type is: 16369 // 'sink' and where vec is the iteration vector, which has the form: 16370 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn] 16371 // where n is the value specified by the ordered clause in the loop 16372 // directive, xi denotes the loop iteration variable of the i-th nested 16373 // loop associated with the loop directive, and di is a constant 16374 // non-negative integer. 16375 if (CurContext->isDependentContext()) { 16376 // It will be analyzed later. 16377 Vars.push_back(RefExpr); 16378 continue; 16379 } 16380 SimpleExpr = SimpleExpr->IgnoreImplicit(); 16381 OverloadedOperatorKind OOK = OO_None; 16382 SourceLocation OOLoc; 16383 Expr *LHS = SimpleExpr; 16384 Expr *RHS = nullptr; 16385 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) { 16386 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode()); 16387 OOLoc = BO->getOperatorLoc(); 16388 LHS = BO->getLHS()->IgnoreParenImpCasts(); 16389 RHS = BO->getRHS()->IgnoreParenImpCasts(); 16390 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) { 16391 OOK = OCE->getOperator(); 16392 OOLoc = OCE->getOperatorLoc(); 16393 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); 16394 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts(); 16395 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) { 16396 OOK = MCE->getMethodDecl() 16397 ->getNameInfo() 16398 .getName() 16399 .getCXXOverloadedOperator(); 16400 OOLoc = MCE->getCallee()->getExprLoc(); 16401 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts(); 16402 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); 16403 } 16404 SourceLocation ELoc; 16405 SourceRange ERange; 16406 auto Res = getPrivateItem(*this, LHS, ELoc, ERange); 16407 if (Res.second) { 16408 // It will be analyzed later. 16409 Vars.push_back(RefExpr); 16410 } 16411 ValueDecl *D = Res.first; 16412 if (!D) 16413 continue; 16414 16415 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) { 16416 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus); 16417 continue; 16418 } 16419 if (RHS) { 16420 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause( 16421 RHS, OMPC_depend, /*StrictlyPositive=*/false); 16422 if (RHSRes.isInvalid()) 16423 continue; 16424 } 16425 if (!CurContext->isDependentContext() && 16426 DSAStack->getParentOrderedRegionParam().first && 16427 DepCounter != DSAStack->isParentLoopControlVariable(D).first) { 16428 const ValueDecl *VD = 16429 DSAStack->getParentLoopControlVariable(DepCounter.getZExtValue()); 16430 if (VD) 16431 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) 16432 << 1 << VD; 16433 else 16434 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) << 0; 16435 continue; 16436 } 16437 OpsOffs.emplace_back(RHS, OOK); 16438 } else { 16439 bool OMPDependTFound = LangOpts.OpenMP >= 50; 16440 if (OMPDependTFound) 16441 OMPDependTFound = findOMPDependT(*this, StartLoc, DSAStack, 16442 DepKind == OMPC_DEPEND_depobj); 16443 if (DepKind == OMPC_DEPEND_depobj) { 16444 // OpenMP 5.0, 2.17.11 depend Clause, Restrictions, C/C++ 16445 // List items used in depend clauses with the depobj dependence type 16446 // must be expressions of the omp_depend_t type. 16447 if (!RefExpr->isValueDependent() && !RefExpr->isTypeDependent() && 16448 !RefExpr->isInstantiationDependent() && 16449 !RefExpr->containsUnexpandedParameterPack() && 16450 (OMPDependTFound && 16451 !Context.hasSameUnqualifiedType(DSAStack->getOMPDependT(), 16452 RefExpr->getType()))) { 16453 Diag(ELoc, diag::err_omp_expected_omp_depend_t_lvalue) 16454 << 0 << RefExpr->getType() << RefExpr->getSourceRange(); 16455 continue; 16456 } 16457 if (!RefExpr->isLValue()) { 16458 Diag(ELoc, diag::err_omp_expected_omp_depend_t_lvalue) 16459 << 1 << RefExpr->getType() << RefExpr->getSourceRange(); 16460 continue; 16461 } 16462 } else { 16463 // OpenMP 5.0 [2.17.11, Restrictions] 16464 // List items used in depend clauses cannot be zero-length array 16465 // sections. 16466 QualType ExprTy = RefExpr->getType().getNonReferenceType(); 16467 const auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr); 16468 if (OASE) { 16469 QualType BaseType = 16470 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 16471 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 16472 ExprTy = ATy->getElementType(); 16473 else 16474 ExprTy = BaseType->getPointeeType(); 16475 ExprTy = ExprTy.getNonReferenceType(); 16476 const Expr *Length = OASE->getLength(); 16477 Expr::EvalResult Result; 16478 if (Length && !Length->isValueDependent() && 16479 Length->EvaluateAsInt(Result, Context) && 16480 Result.Val.getInt().isNullValue()) { 16481 Diag(ELoc, 16482 diag::err_omp_depend_zero_length_array_section_not_allowed) 16483 << SimpleExpr->getSourceRange(); 16484 continue; 16485 } 16486 } 16487 16488 // OpenMP 5.0, 2.17.11 depend Clause, Restrictions, C/C++ 16489 // List items used in depend clauses with the in, out, inout or 16490 // mutexinoutset dependence types cannot be expressions of the 16491 // omp_depend_t type. 16492 if (!RefExpr->isValueDependent() && !RefExpr->isTypeDependent() && 16493 !RefExpr->isInstantiationDependent() && 16494 !RefExpr->containsUnexpandedParameterPack() && 16495 (OMPDependTFound && 16496 DSAStack->getOMPDependT().getTypePtr() == ExprTy.getTypePtr())) { 16497 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 16498 << (LangOpts.OpenMP >= 50 ? 1 : 0) << 1 16499 << RefExpr->getSourceRange(); 16500 continue; 16501 } 16502 16503 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr); 16504 if (!RefExpr->IgnoreParenImpCasts()->isLValue() || 16505 (ASE && !ASE->getBase()->isTypeDependent() && 16506 !ASE->getBase() 16507 ->getType() 16508 .getNonReferenceType() 16509 ->isPointerType() && 16510 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) { 16511 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 16512 << (LangOpts.OpenMP >= 50 ? 1 : 0) 16513 << (LangOpts.OpenMP >= 50 ? 1 : 0) << RefExpr->getSourceRange(); 16514 continue; 16515 } 16516 16517 ExprResult Res; 16518 { 16519 Sema::TentativeAnalysisScope Trap(*this); 16520 Res = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, 16521 RefExpr->IgnoreParenImpCasts()); 16522 } 16523 if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr) && 16524 !isa<OMPArrayShapingExpr>(SimpleExpr)) { 16525 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 16526 << (LangOpts.OpenMP >= 50 ? 1 : 0) 16527 << (LangOpts.OpenMP >= 50 ? 1 : 0) << RefExpr->getSourceRange(); 16528 continue; 16529 } 16530 } 16531 } 16532 Vars.push_back(RefExpr->IgnoreParenImpCasts()); 16533 } 16534 16535 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink && 16536 TotalDepCount > VarList.size() && 16537 DSAStack->getParentOrderedRegionParam().first && 16538 DSAStack->getParentLoopControlVariable(VarList.size() + 1)) { 16539 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration) 16540 << 1 << DSAStack->getParentLoopControlVariable(VarList.size() + 1); 16541 } 16542 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink && 16543 Vars.empty()) 16544 return nullptr; 16545 16546 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, 16547 DepModifier, DepKind, DepLoc, ColonLoc, 16548 Vars, TotalDepCount.getZExtValue()); 16549 if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) && 16550 DSAStack->isParentOrderedRegion()) 16551 DSAStack->addDoacrossDependClause(C, OpsOffs); 16552 return C; 16553 } 16554 16555 OMPClause *Sema::ActOnOpenMPDeviceClause(OpenMPDeviceClauseModifier Modifier, 16556 Expr *Device, SourceLocation StartLoc, 16557 SourceLocation LParenLoc, 16558 SourceLocation ModifierLoc, 16559 SourceLocation EndLoc) { 16560 assert((ModifierLoc.isInvalid() || LangOpts.OpenMP >= 50) && 16561 "Unexpected device modifier in OpenMP < 50."); 16562 16563 bool ErrorFound = false; 16564 if (ModifierLoc.isValid() && Modifier == OMPC_DEVICE_unknown) { 16565 std::string Values = 16566 getListOfPossibleValues(OMPC_device, /*First=*/0, OMPC_DEVICE_unknown); 16567 Diag(ModifierLoc, diag::err_omp_unexpected_clause_value) 16568 << Values << getOpenMPClauseName(OMPC_device); 16569 ErrorFound = true; 16570 } 16571 16572 Expr *ValExpr = Device; 16573 Stmt *HelperValStmt = nullptr; 16574 16575 // OpenMP [2.9.1, Restrictions] 16576 // The device expression must evaluate to a non-negative integer value. 16577 ErrorFound = !isNonNegativeIntegerValue(ValExpr, *this, OMPC_device, 16578 /*StrictlyPositive=*/false) || 16579 ErrorFound; 16580 if (ErrorFound) 16581 return nullptr; 16582 16583 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 16584 OpenMPDirectiveKind CaptureRegion = 16585 getOpenMPCaptureRegionForClause(DKind, OMPC_device, LangOpts.OpenMP); 16586 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 16587 ValExpr = MakeFullExpr(ValExpr).get(); 16588 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 16589 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 16590 HelperValStmt = buildPreInits(Context, Captures); 16591 } 16592 16593 return new (Context) 16594 OMPDeviceClause(Modifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc, 16595 LParenLoc, ModifierLoc, EndLoc); 16596 } 16597 16598 static bool checkTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef, 16599 DSAStackTy *Stack, QualType QTy, 16600 bool FullCheck = true) { 16601 NamedDecl *ND; 16602 if (QTy->isIncompleteType(&ND)) { 16603 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR; 16604 return false; 16605 } 16606 if (FullCheck && !SemaRef.CurContext->isDependentContext() && 16607 !QTy.isTriviallyCopyableType(SemaRef.Context)) 16608 SemaRef.Diag(SL, diag::warn_omp_non_trivial_type_mapped) << QTy << SR; 16609 return true; 16610 } 16611 16612 /// Return true if it can be proven that the provided array expression 16613 /// (array section or array subscript) does NOT specify the whole size of the 16614 /// array whose base type is \a BaseQTy. 16615 static bool checkArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef, 16616 const Expr *E, 16617 QualType BaseQTy) { 16618 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E); 16619 16620 // If this is an array subscript, it refers to the whole size if the size of 16621 // the dimension is constant and equals 1. Also, an array section assumes the 16622 // format of an array subscript if no colon is used. 16623 if (isa<ArraySubscriptExpr>(E) || 16624 (OASE && OASE->getColonLocFirst().isInvalid())) { 16625 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) 16626 return ATy->getSize().getSExtValue() != 1; 16627 // Size can't be evaluated statically. 16628 return false; 16629 } 16630 16631 assert(OASE && "Expecting array section if not an array subscript."); 16632 const Expr *LowerBound = OASE->getLowerBound(); 16633 const Expr *Length = OASE->getLength(); 16634 16635 // If there is a lower bound that does not evaluates to zero, we are not 16636 // covering the whole dimension. 16637 if (LowerBound) { 16638 Expr::EvalResult Result; 16639 if (!LowerBound->EvaluateAsInt(Result, SemaRef.getASTContext())) 16640 return false; // Can't get the integer value as a constant. 16641 16642 llvm::APSInt ConstLowerBound = Result.Val.getInt(); 16643 if (ConstLowerBound.getSExtValue()) 16644 return true; 16645 } 16646 16647 // If we don't have a length we covering the whole dimension. 16648 if (!Length) 16649 return false; 16650 16651 // If the base is a pointer, we don't have a way to get the size of the 16652 // pointee. 16653 if (BaseQTy->isPointerType()) 16654 return false; 16655 16656 // We can only check if the length is the same as the size of the dimension 16657 // if we have a constant array. 16658 const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()); 16659 if (!CATy) 16660 return false; 16661 16662 Expr::EvalResult Result; 16663 if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext())) 16664 return false; // Can't get the integer value as a constant. 16665 16666 llvm::APSInt ConstLength = Result.Val.getInt(); 16667 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue(); 16668 } 16669 16670 // Return true if it can be proven that the provided array expression (array 16671 // section or array subscript) does NOT specify a single element of the array 16672 // whose base type is \a BaseQTy. 16673 static bool checkArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef, 16674 const Expr *E, 16675 QualType BaseQTy) { 16676 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E); 16677 16678 // An array subscript always refer to a single element. Also, an array section 16679 // assumes the format of an array subscript if no colon is used. 16680 if (isa<ArraySubscriptExpr>(E) || 16681 (OASE && OASE->getColonLocFirst().isInvalid())) 16682 return false; 16683 16684 assert(OASE && "Expecting array section if not an array subscript."); 16685 const Expr *Length = OASE->getLength(); 16686 16687 // If we don't have a length we have to check if the array has unitary size 16688 // for this dimension. Also, we should always expect a length if the base type 16689 // is pointer. 16690 if (!Length) { 16691 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) 16692 return ATy->getSize().getSExtValue() != 1; 16693 // We cannot assume anything. 16694 return false; 16695 } 16696 16697 // Check if the length evaluates to 1. 16698 Expr::EvalResult Result; 16699 if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext())) 16700 return false; // Can't get the integer value as a constant. 16701 16702 llvm::APSInt ConstLength = Result.Val.getInt(); 16703 return ConstLength.getSExtValue() != 1; 16704 } 16705 16706 // The base of elements of list in a map clause have to be either: 16707 // - a reference to variable or field. 16708 // - a member expression. 16709 // - an array expression. 16710 // 16711 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the 16712 // reference to 'r'. 16713 // 16714 // If we have: 16715 // 16716 // struct SS { 16717 // Bla S; 16718 // foo() { 16719 // #pragma omp target map (S.Arr[:12]); 16720 // } 16721 // } 16722 // 16723 // We want to retrieve the member expression 'this->S'; 16724 16725 // OpenMP 5.0 [2.19.7.1, map Clause, Restrictions, p.2] 16726 // If a list item is an array section, it must specify contiguous storage. 16727 // 16728 // For this restriction it is sufficient that we make sure only references 16729 // to variables or fields and array expressions, and that no array sections 16730 // exist except in the rightmost expression (unless they cover the whole 16731 // dimension of the array). E.g. these would be invalid: 16732 // 16733 // r.ArrS[3:5].Arr[6:7] 16734 // 16735 // r.ArrS[3:5].x 16736 // 16737 // but these would be valid: 16738 // r.ArrS[3].Arr[6:7] 16739 // 16740 // r.ArrS[3].x 16741 namespace { 16742 class MapBaseChecker final : public StmtVisitor<MapBaseChecker, bool> { 16743 Sema &SemaRef; 16744 OpenMPClauseKind CKind = OMPC_unknown; 16745 OMPClauseMappableExprCommon::MappableExprComponentList &Components; 16746 bool NoDiagnose = false; 16747 const Expr *RelevantExpr = nullptr; 16748 bool AllowUnitySizeArraySection = true; 16749 bool AllowWholeSizeArraySection = true; 16750 SourceLocation ELoc; 16751 SourceRange ERange; 16752 16753 void emitErrorMsg() { 16754 // If nothing else worked, this is not a valid map clause expression. 16755 if (SemaRef.getLangOpts().OpenMP < 50) { 16756 SemaRef.Diag(ELoc, 16757 diag::err_omp_expected_named_var_member_or_array_expression) 16758 << ERange; 16759 } else { 16760 SemaRef.Diag(ELoc, diag::err_omp_non_lvalue_in_map_or_motion_clauses) 16761 << getOpenMPClauseName(CKind) << ERange; 16762 } 16763 } 16764 16765 public: 16766 bool VisitDeclRefExpr(DeclRefExpr *DRE) { 16767 if (!isa<VarDecl>(DRE->getDecl())) { 16768 emitErrorMsg(); 16769 return false; 16770 } 16771 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 16772 RelevantExpr = DRE; 16773 // Record the component. 16774 Components.emplace_back(DRE, DRE->getDecl()); 16775 return true; 16776 } 16777 16778 bool VisitMemberExpr(MemberExpr *ME) { 16779 Expr *E = ME; 16780 Expr *BaseE = ME->getBase()->IgnoreParenCasts(); 16781 16782 if (isa<CXXThisExpr>(BaseE)) { 16783 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 16784 // We found a base expression: this->Val. 16785 RelevantExpr = ME; 16786 } else { 16787 E = BaseE; 16788 } 16789 16790 if (!isa<FieldDecl>(ME->getMemberDecl())) { 16791 if (!NoDiagnose) { 16792 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field) 16793 << ME->getSourceRange(); 16794 return false; 16795 } 16796 if (RelevantExpr) 16797 return false; 16798 return Visit(E); 16799 } 16800 16801 auto *FD = cast<FieldDecl>(ME->getMemberDecl()); 16802 16803 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3] 16804 // A bit-field cannot appear in a map clause. 16805 // 16806 if (FD->isBitField()) { 16807 if (!NoDiagnose) { 16808 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause) 16809 << ME->getSourceRange() << getOpenMPClauseName(CKind); 16810 return false; 16811 } 16812 if (RelevantExpr) 16813 return false; 16814 return Visit(E); 16815 } 16816 16817 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 16818 // If the type of a list item is a reference to a type T then the type 16819 // will be considered to be T for all purposes of this clause. 16820 QualType CurType = BaseE->getType().getNonReferenceType(); 16821 16822 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2] 16823 // A list item cannot be a variable that is a member of a structure with 16824 // a union type. 16825 // 16826 if (CurType->isUnionType()) { 16827 if (!NoDiagnose) { 16828 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed) 16829 << ME->getSourceRange(); 16830 return false; 16831 } 16832 return RelevantExpr || Visit(E); 16833 } 16834 16835 // If we got a member expression, we should not expect any array section 16836 // before that: 16837 // 16838 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7] 16839 // If a list item is an element of a structure, only the rightmost symbol 16840 // of the variable reference can be an array section. 16841 // 16842 AllowUnitySizeArraySection = false; 16843 AllowWholeSizeArraySection = false; 16844 16845 // Record the component. 16846 Components.emplace_back(ME, FD); 16847 return RelevantExpr || Visit(E); 16848 } 16849 16850 bool VisitArraySubscriptExpr(ArraySubscriptExpr *AE) { 16851 Expr *E = AE->getBase()->IgnoreParenImpCasts(); 16852 16853 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) { 16854 if (!NoDiagnose) { 16855 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name) 16856 << 0 << AE->getSourceRange(); 16857 return false; 16858 } 16859 return RelevantExpr || Visit(E); 16860 } 16861 16862 // If we got an array subscript that express the whole dimension we 16863 // can have any array expressions before. If it only expressing part of 16864 // the dimension, we can only have unitary-size array expressions. 16865 if (checkArrayExpressionDoesNotReferToWholeSize(SemaRef, AE, 16866 E->getType())) 16867 AllowWholeSizeArraySection = false; 16868 16869 if (const auto *TE = dyn_cast<CXXThisExpr>(E->IgnoreParenCasts())) { 16870 Expr::EvalResult Result; 16871 if (!AE->getIdx()->isValueDependent() && 16872 AE->getIdx()->EvaluateAsInt(Result, SemaRef.getASTContext()) && 16873 !Result.Val.getInt().isNullValue()) { 16874 SemaRef.Diag(AE->getIdx()->getExprLoc(), 16875 diag::err_omp_invalid_map_this_expr); 16876 SemaRef.Diag(AE->getIdx()->getExprLoc(), 16877 diag::note_omp_invalid_subscript_on_this_ptr_map); 16878 } 16879 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 16880 RelevantExpr = TE; 16881 } 16882 16883 // Record the component - we don't have any declaration associated. 16884 Components.emplace_back(AE, nullptr); 16885 16886 return RelevantExpr || Visit(E); 16887 } 16888 16889 bool VisitOMPArraySectionExpr(OMPArraySectionExpr *OASE) { 16890 assert(!NoDiagnose && "Array sections cannot be implicitly mapped."); 16891 Expr *E = OASE->getBase()->IgnoreParenImpCasts(); 16892 QualType CurType = 16893 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType(); 16894 16895 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 16896 // If the type of a list item is a reference to a type T then the type 16897 // will be considered to be T for all purposes of this clause. 16898 if (CurType->isReferenceType()) 16899 CurType = CurType->getPointeeType(); 16900 16901 bool IsPointer = CurType->isAnyPointerType(); 16902 16903 if (!IsPointer && !CurType->isArrayType()) { 16904 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name) 16905 << 0 << OASE->getSourceRange(); 16906 return false; 16907 } 16908 16909 bool NotWhole = 16910 checkArrayExpressionDoesNotReferToWholeSize(SemaRef, OASE, CurType); 16911 bool NotUnity = 16912 checkArrayExpressionDoesNotReferToUnitySize(SemaRef, OASE, CurType); 16913 16914 if (AllowWholeSizeArraySection) { 16915 // Any array section is currently allowed. Allowing a whole size array 16916 // section implies allowing a unity array section as well. 16917 // 16918 // If this array section refers to the whole dimension we can still 16919 // accept other array sections before this one, except if the base is a 16920 // pointer. Otherwise, only unitary sections are accepted. 16921 if (NotWhole || IsPointer) 16922 AllowWholeSizeArraySection = false; 16923 } else if (AllowUnitySizeArraySection && NotUnity) { 16924 // A unity or whole array section is not allowed and that is not 16925 // compatible with the properties of the current array section. 16926 SemaRef.Diag( 16927 ELoc, diag::err_array_section_does_not_specify_contiguous_storage) 16928 << OASE->getSourceRange(); 16929 return false; 16930 } 16931 16932 if (const auto *TE = dyn_cast<CXXThisExpr>(E)) { 16933 Expr::EvalResult ResultR; 16934 Expr::EvalResult ResultL; 16935 if (!OASE->getLength()->isValueDependent() && 16936 OASE->getLength()->EvaluateAsInt(ResultR, SemaRef.getASTContext()) && 16937 !ResultR.Val.getInt().isOneValue()) { 16938 SemaRef.Diag(OASE->getLength()->getExprLoc(), 16939 diag::err_omp_invalid_map_this_expr); 16940 SemaRef.Diag(OASE->getLength()->getExprLoc(), 16941 diag::note_omp_invalid_length_on_this_ptr_mapping); 16942 } 16943 if (OASE->getLowerBound() && !OASE->getLowerBound()->isValueDependent() && 16944 OASE->getLowerBound()->EvaluateAsInt(ResultL, 16945 SemaRef.getASTContext()) && 16946 !ResultL.Val.getInt().isNullValue()) { 16947 SemaRef.Diag(OASE->getLowerBound()->getExprLoc(), 16948 diag::err_omp_invalid_map_this_expr); 16949 SemaRef.Diag(OASE->getLowerBound()->getExprLoc(), 16950 diag::note_omp_invalid_lower_bound_on_this_ptr_mapping); 16951 } 16952 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 16953 RelevantExpr = TE; 16954 } 16955 16956 // Record the component - we don't have any declaration associated. 16957 Components.emplace_back(OASE, nullptr); 16958 return RelevantExpr || Visit(E); 16959 } 16960 bool VisitOMPArrayShapingExpr(OMPArrayShapingExpr *E) { 16961 Expr *Base = E->getBase(); 16962 16963 // Record the component - we don't have any declaration associated. 16964 Components.emplace_back(E, nullptr); 16965 16966 return Visit(Base->IgnoreParenImpCasts()); 16967 } 16968 16969 bool VisitUnaryOperator(UnaryOperator *UO) { 16970 if (SemaRef.getLangOpts().OpenMP < 50 || !UO->isLValue() || 16971 UO->getOpcode() != UO_Deref) { 16972 emitErrorMsg(); 16973 return false; 16974 } 16975 if (!RelevantExpr) { 16976 // Record the component if haven't found base decl. 16977 Components.emplace_back(UO, nullptr); 16978 } 16979 return RelevantExpr || Visit(UO->getSubExpr()->IgnoreParenImpCasts()); 16980 } 16981 bool VisitBinaryOperator(BinaryOperator *BO) { 16982 if (SemaRef.getLangOpts().OpenMP < 50 || !BO->getType()->isPointerType()) { 16983 emitErrorMsg(); 16984 return false; 16985 } 16986 16987 // Pointer arithmetic is the only thing we expect to happen here so after we 16988 // make sure the binary operator is a pointer type, the we only thing need 16989 // to to is to visit the subtree that has the same type as root (so that we 16990 // know the other subtree is just an offset) 16991 Expr *LE = BO->getLHS()->IgnoreParenImpCasts(); 16992 Expr *RE = BO->getRHS()->IgnoreParenImpCasts(); 16993 Components.emplace_back(BO, nullptr); 16994 assert((LE->getType().getTypePtr() == BO->getType().getTypePtr() || 16995 RE->getType().getTypePtr() == BO->getType().getTypePtr()) && 16996 "Either LHS or RHS have base decl inside"); 16997 if (BO->getType().getTypePtr() == LE->getType().getTypePtr()) 16998 return RelevantExpr || Visit(LE); 16999 return RelevantExpr || Visit(RE); 17000 } 17001 bool VisitCXXThisExpr(CXXThisExpr *CTE) { 17002 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 17003 RelevantExpr = CTE; 17004 Components.emplace_back(CTE, nullptr); 17005 return true; 17006 } 17007 bool VisitCXXOperatorCallExpr(CXXOperatorCallExpr *COCE) { 17008 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 17009 Components.emplace_back(COCE, nullptr); 17010 return true; 17011 } 17012 bool VisitStmt(Stmt *) { 17013 emitErrorMsg(); 17014 return false; 17015 } 17016 const Expr *getFoundBase() const { 17017 return RelevantExpr; 17018 } 17019 explicit MapBaseChecker( 17020 Sema &SemaRef, OpenMPClauseKind CKind, 17021 OMPClauseMappableExprCommon::MappableExprComponentList &Components, 17022 bool NoDiagnose, SourceLocation &ELoc, SourceRange &ERange) 17023 : SemaRef(SemaRef), CKind(CKind), Components(Components), 17024 NoDiagnose(NoDiagnose), ELoc(ELoc), ERange(ERange) {} 17025 }; 17026 } // namespace 17027 17028 /// Return the expression of the base of the mappable expression or null if it 17029 /// cannot be determined and do all the necessary checks to see if the expression 17030 /// is valid as a standalone mappable expression. In the process, record all the 17031 /// components of the expression. 17032 static const Expr *checkMapClauseExpressionBase( 17033 Sema &SemaRef, Expr *E, 17034 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents, 17035 OpenMPClauseKind CKind, bool NoDiagnose) { 17036 SourceLocation ELoc = E->getExprLoc(); 17037 SourceRange ERange = E->getSourceRange(); 17038 MapBaseChecker Checker(SemaRef, CKind, CurComponents, NoDiagnose, ELoc, 17039 ERange); 17040 if (Checker.Visit(E->IgnoreParens())) 17041 return Checker.getFoundBase(); 17042 return nullptr; 17043 } 17044 17045 // Return true if expression E associated with value VD has conflicts with other 17046 // map information. 17047 static bool checkMapConflicts( 17048 Sema &SemaRef, DSAStackTy *DSAS, const ValueDecl *VD, const Expr *E, 17049 bool CurrentRegionOnly, 17050 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents, 17051 OpenMPClauseKind CKind) { 17052 assert(VD && E); 17053 SourceLocation ELoc = E->getExprLoc(); 17054 SourceRange ERange = E->getSourceRange(); 17055 17056 // In order to easily check the conflicts we need to match each component of 17057 // the expression under test with the components of the expressions that are 17058 // already in the stack. 17059 17060 assert(!CurComponents.empty() && "Map clause expression with no components!"); 17061 assert(CurComponents.back().getAssociatedDeclaration() == VD && 17062 "Map clause expression with unexpected base!"); 17063 17064 // Variables to help detecting enclosing problems in data environment nests. 17065 bool IsEnclosedByDataEnvironmentExpr = false; 17066 const Expr *EnclosingExpr = nullptr; 17067 17068 bool FoundError = DSAS->checkMappableExprComponentListsForDecl( 17069 VD, CurrentRegionOnly, 17070 [&IsEnclosedByDataEnvironmentExpr, &SemaRef, VD, CurrentRegionOnly, ELoc, 17071 ERange, CKind, &EnclosingExpr, 17072 CurComponents](OMPClauseMappableExprCommon::MappableExprComponentListRef 17073 StackComponents, 17074 OpenMPClauseKind) { 17075 assert(!StackComponents.empty() && 17076 "Map clause expression with no components!"); 17077 assert(StackComponents.back().getAssociatedDeclaration() == VD && 17078 "Map clause expression with unexpected base!"); 17079 (void)VD; 17080 17081 // The whole expression in the stack. 17082 const Expr *RE = StackComponents.front().getAssociatedExpression(); 17083 17084 // Expressions must start from the same base. Here we detect at which 17085 // point both expressions diverge from each other and see if we can 17086 // detect if the memory referred to both expressions is contiguous and 17087 // do not overlap. 17088 auto CI = CurComponents.rbegin(); 17089 auto CE = CurComponents.rend(); 17090 auto SI = StackComponents.rbegin(); 17091 auto SE = StackComponents.rend(); 17092 for (; CI != CE && SI != SE; ++CI, ++SI) { 17093 17094 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3] 17095 // At most one list item can be an array item derived from a given 17096 // variable in map clauses of the same construct. 17097 if (CurrentRegionOnly && 17098 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) || 17099 isa<OMPArraySectionExpr>(CI->getAssociatedExpression()) || 17100 isa<OMPArrayShapingExpr>(CI->getAssociatedExpression())) && 17101 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) || 17102 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()) || 17103 isa<OMPArrayShapingExpr>(SI->getAssociatedExpression()))) { 17104 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(), 17105 diag::err_omp_multiple_array_items_in_map_clause) 17106 << CI->getAssociatedExpression()->getSourceRange(); 17107 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(), 17108 diag::note_used_here) 17109 << SI->getAssociatedExpression()->getSourceRange(); 17110 return true; 17111 } 17112 17113 // Do both expressions have the same kind? 17114 if (CI->getAssociatedExpression()->getStmtClass() != 17115 SI->getAssociatedExpression()->getStmtClass()) 17116 break; 17117 17118 // Are we dealing with different variables/fields? 17119 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration()) 17120 break; 17121 } 17122 // Check if the extra components of the expressions in the enclosing 17123 // data environment are redundant for the current base declaration. 17124 // If they are, the maps completely overlap, which is legal. 17125 for (; SI != SE; ++SI) { 17126 QualType Type; 17127 if (const auto *ASE = 17128 dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) { 17129 Type = ASE->getBase()->IgnoreParenImpCasts()->getType(); 17130 } else if (const auto *OASE = dyn_cast<OMPArraySectionExpr>( 17131 SI->getAssociatedExpression())) { 17132 const Expr *E = OASE->getBase()->IgnoreParenImpCasts(); 17133 Type = 17134 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType(); 17135 } else if (const auto *OASE = dyn_cast<OMPArrayShapingExpr>( 17136 SI->getAssociatedExpression())) { 17137 Type = OASE->getBase()->getType()->getPointeeType(); 17138 } 17139 if (Type.isNull() || Type->isAnyPointerType() || 17140 checkArrayExpressionDoesNotReferToWholeSize( 17141 SemaRef, SI->getAssociatedExpression(), Type)) 17142 break; 17143 } 17144 17145 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4] 17146 // List items of map clauses in the same construct must not share 17147 // original storage. 17148 // 17149 // If the expressions are exactly the same or one is a subset of the 17150 // other, it means they are sharing storage. 17151 if (CI == CE && SI == SE) { 17152 if (CurrentRegionOnly) { 17153 if (CKind == OMPC_map) { 17154 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange; 17155 } else { 17156 assert(CKind == OMPC_to || CKind == OMPC_from); 17157 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update) 17158 << ERange; 17159 } 17160 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 17161 << RE->getSourceRange(); 17162 return true; 17163 } 17164 // If we find the same expression in the enclosing data environment, 17165 // that is legal. 17166 IsEnclosedByDataEnvironmentExpr = true; 17167 return false; 17168 } 17169 17170 QualType DerivedType = 17171 std::prev(CI)->getAssociatedDeclaration()->getType(); 17172 SourceLocation DerivedLoc = 17173 std::prev(CI)->getAssociatedExpression()->getExprLoc(); 17174 17175 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 17176 // If the type of a list item is a reference to a type T then the type 17177 // will be considered to be T for all purposes of this clause. 17178 DerivedType = DerivedType.getNonReferenceType(); 17179 17180 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1] 17181 // A variable for which the type is pointer and an array section 17182 // derived from that variable must not appear as list items of map 17183 // clauses of the same construct. 17184 // 17185 // Also, cover one of the cases in: 17186 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5] 17187 // If any part of the original storage of a list item has corresponding 17188 // storage in the device data environment, all of the original storage 17189 // must have corresponding storage in the device data environment. 17190 // 17191 if (DerivedType->isAnyPointerType()) { 17192 if (CI == CE || SI == SE) { 17193 SemaRef.Diag( 17194 DerivedLoc, 17195 diag::err_omp_pointer_mapped_along_with_derived_section) 17196 << DerivedLoc; 17197 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 17198 << RE->getSourceRange(); 17199 return true; 17200 } 17201 if (CI->getAssociatedExpression()->getStmtClass() != 17202 SI->getAssociatedExpression()->getStmtClass() || 17203 CI->getAssociatedDeclaration()->getCanonicalDecl() == 17204 SI->getAssociatedDeclaration()->getCanonicalDecl()) { 17205 assert(CI != CE && SI != SE); 17206 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_dereferenced) 17207 << DerivedLoc; 17208 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 17209 << RE->getSourceRange(); 17210 return true; 17211 } 17212 } 17213 17214 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4] 17215 // List items of map clauses in the same construct must not share 17216 // original storage. 17217 // 17218 // An expression is a subset of the other. 17219 if (CurrentRegionOnly && (CI == CE || SI == SE)) { 17220 if (CKind == OMPC_map) { 17221 if (CI != CE || SI != SE) { 17222 // Allow constructs like this: map(s, s.ptr[0:1]), where s.ptr is 17223 // a pointer. 17224 auto Begin = 17225 CI != CE ? CurComponents.begin() : StackComponents.begin(); 17226 auto End = CI != CE ? CurComponents.end() : StackComponents.end(); 17227 auto It = Begin; 17228 while (It != End && !It->getAssociatedDeclaration()) 17229 std::advance(It, 1); 17230 assert(It != End && 17231 "Expected at least one component with the declaration."); 17232 if (It != Begin && It->getAssociatedDeclaration() 17233 ->getType() 17234 .getCanonicalType() 17235 ->isAnyPointerType()) { 17236 IsEnclosedByDataEnvironmentExpr = false; 17237 EnclosingExpr = nullptr; 17238 return false; 17239 } 17240 } 17241 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange; 17242 } else { 17243 assert(CKind == OMPC_to || CKind == OMPC_from); 17244 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update) 17245 << ERange; 17246 } 17247 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 17248 << RE->getSourceRange(); 17249 return true; 17250 } 17251 17252 // The current expression uses the same base as other expression in the 17253 // data environment but does not contain it completely. 17254 if (!CurrentRegionOnly && SI != SE) 17255 EnclosingExpr = RE; 17256 17257 // The current expression is a subset of the expression in the data 17258 // environment. 17259 IsEnclosedByDataEnvironmentExpr |= 17260 (!CurrentRegionOnly && CI != CE && SI == SE); 17261 17262 return false; 17263 }); 17264 17265 if (CurrentRegionOnly) 17266 return FoundError; 17267 17268 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5] 17269 // If any part of the original storage of a list item has corresponding 17270 // storage in the device data environment, all of the original storage must 17271 // have corresponding storage in the device data environment. 17272 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6] 17273 // If a list item is an element of a structure, and a different element of 17274 // the structure has a corresponding list item in the device data environment 17275 // prior to a task encountering the construct associated with the map clause, 17276 // then the list item must also have a corresponding list item in the device 17277 // data environment prior to the task encountering the construct. 17278 // 17279 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) { 17280 SemaRef.Diag(ELoc, 17281 diag::err_omp_original_storage_is_shared_and_does_not_contain) 17282 << ERange; 17283 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here) 17284 << EnclosingExpr->getSourceRange(); 17285 return true; 17286 } 17287 17288 return FoundError; 17289 } 17290 17291 // Look up the user-defined mapper given the mapper name and mapped type, and 17292 // build a reference to it. 17293 static ExprResult buildUserDefinedMapperRef(Sema &SemaRef, Scope *S, 17294 CXXScopeSpec &MapperIdScopeSpec, 17295 const DeclarationNameInfo &MapperId, 17296 QualType Type, 17297 Expr *UnresolvedMapper) { 17298 if (MapperIdScopeSpec.isInvalid()) 17299 return ExprError(); 17300 // Get the actual type for the array type. 17301 if (Type->isArrayType()) { 17302 assert(Type->getAsArrayTypeUnsafe() && "Expect to get a valid array type"); 17303 Type = Type->getAsArrayTypeUnsafe()->getElementType().getCanonicalType(); 17304 } 17305 // Find all user-defined mappers with the given MapperId. 17306 SmallVector<UnresolvedSet<8>, 4> Lookups; 17307 LookupResult Lookup(SemaRef, MapperId, Sema::LookupOMPMapperName); 17308 Lookup.suppressDiagnostics(); 17309 if (S) { 17310 while (S && SemaRef.LookupParsedName(Lookup, S, &MapperIdScopeSpec)) { 17311 NamedDecl *D = Lookup.getRepresentativeDecl(); 17312 while (S && !S->isDeclScope(D)) 17313 S = S->getParent(); 17314 if (S) 17315 S = S->getParent(); 17316 Lookups.emplace_back(); 17317 Lookups.back().append(Lookup.begin(), Lookup.end()); 17318 Lookup.clear(); 17319 } 17320 } else if (auto *ULE = cast_or_null<UnresolvedLookupExpr>(UnresolvedMapper)) { 17321 // Extract the user-defined mappers with the given MapperId. 17322 Lookups.push_back(UnresolvedSet<8>()); 17323 for (NamedDecl *D : ULE->decls()) { 17324 auto *DMD = cast<OMPDeclareMapperDecl>(D); 17325 assert(DMD && "Expect valid OMPDeclareMapperDecl during instantiation."); 17326 Lookups.back().addDecl(DMD); 17327 } 17328 } 17329 // Defer the lookup for dependent types. The results will be passed through 17330 // UnresolvedMapper on instantiation. 17331 if (SemaRef.CurContext->isDependentContext() || Type->isDependentType() || 17332 Type->isInstantiationDependentType() || 17333 Type->containsUnexpandedParameterPack() || 17334 filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) { 17335 return !D->isInvalidDecl() && 17336 (D->getType()->isDependentType() || 17337 D->getType()->isInstantiationDependentType() || 17338 D->getType()->containsUnexpandedParameterPack()); 17339 })) { 17340 UnresolvedSet<8> URS; 17341 for (const UnresolvedSet<8> &Set : Lookups) { 17342 if (Set.empty()) 17343 continue; 17344 URS.append(Set.begin(), Set.end()); 17345 } 17346 return UnresolvedLookupExpr::Create( 17347 SemaRef.Context, /*NamingClass=*/nullptr, 17348 MapperIdScopeSpec.getWithLocInContext(SemaRef.Context), MapperId, 17349 /*ADL=*/false, /*Overloaded=*/true, URS.begin(), URS.end()); 17350 } 17351 SourceLocation Loc = MapperId.getLoc(); 17352 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions 17353 // The type must be of struct, union or class type in C and C++ 17354 if (!Type->isStructureOrClassType() && !Type->isUnionType() && 17355 (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default")) { 17356 SemaRef.Diag(Loc, diag::err_omp_mapper_wrong_type); 17357 return ExprError(); 17358 } 17359 // Perform argument dependent lookup. 17360 if (SemaRef.getLangOpts().CPlusPlus && !MapperIdScopeSpec.isSet()) 17361 argumentDependentLookup(SemaRef, MapperId, Loc, Type, Lookups); 17362 // Return the first user-defined mapper with the desired type. 17363 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 17364 Lookups, [&SemaRef, Type](ValueDecl *D) -> ValueDecl * { 17365 if (!D->isInvalidDecl() && 17366 SemaRef.Context.hasSameType(D->getType(), Type)) 17367 return D; 17368 return nullptr; 17369 })) 17370 return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc); 17371 // Find the first user-defined mapper with a type derived from the desired 17372 // type. 17373 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 17374 Lookups, [&SemaRef, Type, Loc](ValueDecl *D) -> ValueDecl * { 17375 if (!D->isInvalidDecl() && 17376 SemaRef.IsDerivedFrom(Loc, Type, D->getType()) && 17377 !Type.isMoreQualifiedThan(D->getType())) 17378 return D; 17379 return nullptr; 17380 })) { 17381 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, 17382 /*DetectVirtual=*/false); 17383 if (SemaRef.IsDerivedFrom(Loc, Type, VD->getType(), Paths)) { 17384 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType( 17385 VD->getType().getUnqualifiedType()))) { 17386 if (SemaRef.CheckBaseClassAccess( 17387 Loc, VD->getType(), Type, Paths.front(), 17388 /*DiagID=*/0) != Sema::AR_inaccessible) { 17389 return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc); 17390 } 17391 } 17392 } 17393 } 17394 // Report error if a mapper is specified, but cannot be found. 17395 if (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default") { 17396 SemaRef.Diag(Loc, diag::err_omp_invalid_mapper) 17397 << Type << MapperId.getName(); 17398 return ExprError(); 17399 } 17400 return ExprEmpty(); 17401 } 17402 17403 namespace { 17404 // Utility struct that gathers all the related lists associated with a mappable 17405 // expression. 17406 struct MappableVarListInfo { 17407 // The list of expressions. 17408 ArrayRef<Expr *> VarList; 17409 // The list of processed expressions. 17410 SmallVector<Expr *, 16> ProcessedVarList; 17411 // The mappble components for each expression. 17412 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents; 17413 // The base declaration of the variable. 17414 SmallVector<ValueDecl *, 16> VarBaseDeclarations; 17415 // The reference to the user-defined mapper associated with every expression. 17416 SmallVector<Expr *, 16> UDMapperList; 17417 17418 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) { 17419 // We have a list of components and base declarations for each entry in the 17420 // variable list. 17421 VarComponents.reserve(VarList.size()); 17422 VarBaseDeclarations.reserve(VarList.size()); 17423 } 17424 }; 17425 } 17426 17427 // Check the validity of the provided variable list for the provided clause kind 17428 // \a CKind. In the check process the valid expressions, mappable expression 17429 // components, variables, and user-defined mappers are extracted and used to 17430 // fill \a ProcessedVarList, \a VarComponents, \a VarBaseDeclarations, and \a 17431 // UDMapperList in MVLI. \a MapType, \a IsMapTypeImplicit, \a MapperIdScopeSpec, 17432 // and \a MapperId are expected to be valid if the clause kind is 'map'. 17433 static void checkMappableExpressionList( 17434 Sema &SemaRef, DSAStackTy *DSAS, OpenMPClauseKind CKind, 17435 MappableVarListInfo &MVLI, SourceLocation StartLoc, 17436 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo MapperId, 17437 ArrayRef<Expr *> UnresolvedMappers, 17438 OpenMPMapClauseKind MapType = OMPC_MAP_unknown, 17439 bool IsMapTypeImplicit = false) { 17440 // We only expect mappable expressions in 'to', 'from', and 'map' clauses. 17441 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) && 17442 "Unexpected clause kind with mappable expressions!"); 17443 17444 // If the identifier of user-defined mapper is not specified, it is "default". 17445 // We do not change the actual name in this clause to distinguish whether a 17446 // mapper is specified explicitly, i.e., it is not explicitly specified when 17447 // MapperId.getName() is empty. 17448 if (!MapperId.getName() || MapperId.getName().isEmpty()) { 17449 auto &DeclNames = SemaRef.getASTContext().DeclarationNames; 17450 MapperId.setName(DeclNames.getIdentifier( 17451 &SemaRef.getASTContext().Idents.get("default"))); 17452 } 17453 17454 // Iterators to find the current unresolved mapper expression. 17455 auto UMIt = UnresolvedMappers.begin(), UMEnd = UnresolvedMappers.end(); 17456 bool UpdateUMIt = false; 17457 Expr *UnresolvedMapper = nullptr; 17458 17459 // Keep track of the mappable components and base declarations in this clause. 17460 // Each entry in the list is going to have a list of components associated. We 17461 // record each set of the components so that we can build the clause later on. 17462 // In the end we should have the same amount of declarations and component 17463 // lists. 17464 17465 for (Expr *RE : MVLI.VarList) { 17466 assert(RE && "Null expr in omp to/from/map clause"); 17467 SourceLocation ELoc = RE->getExprLoc(); 17468 17469 // Find the current unresolved mapper expression. 17470 if (UpdateUMIt && UMIt != UMEnd) { 17471 UMIt++; 17472 assert( 17473 UMIt != UMEnd && 17474 "Expect the size of UnresolvedMappers to match with that of VarList"); 17475 } 17476 UpdateUMIt = true; 17477 if (UMIt != UMEnd) 17478 UnresolvedMapper = *UMIt; 17479 17480 const Expr *VE = RE->IgnoreParenLValueCasts(); 17481 17482 if (VE->isValueDependent() || VE->isTypeDependent() || 17483 VE->isInstantiationDependent() || 17484 VE->containsUnexpandedParameterPack()) { 17485 // Try to find the associated user-defined mapper. 17486 ExprResult ER = buildUserDefinedMapperRef( 17487 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId, 17488 VE->getType().getCanonicalType(), UnresolvedMapper); 17489 if (ER.isInvalid()) 17490 continue; 17491 MVLI.UDMapperList.push_back(ER.get()); 17492 // We can only analyze this information once the missing information is 17493 // resolved. 17494 MVLI.ProcessedVarList.push_back(RE); 17495 continue; 17496 } 17497 17498 Expr *SimpleExpr = RE->IgnoreParenCasts(); 17499 17500 if (!RE->isLValue()) { 17501 if (SemaRef.getLangOpts().OpenMP < 50) { 17502 SemaRef.Diag( 17503 ELoc, diag::err_omp_expected_named_var_member_or_array_expression) 17504 << RE->getSourceRange(); 17505 } else { 17506 SemaRef.Diag(ELoc, diag::err_omp_non_lvalue_in_map_or_motion_clauses) 17507 << getOpenMPClauseName(CKind) << RE->getSourceRange(); 17508 } 17509 continue; 17510 } 17511 17512 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents; 17513 ValueDecl *CurDeclaration = nullptr; 17514 17515 // Obtain the array or member expression bases if required. Also, fill the 17516 // components array with all the components identified in the process. 17517 const Expr *BE = checkMapClauseExpressionBase( 17518 SemaRef, SimpleExpr, CurComponents, CKind, /*NoDiagnose=*/false); 17519 if (!BE) 17520 continue; 17521 17522 assert(!CurComponents.empty() && 17523 "Invalid mappable expression information."); 17524 17525 if (const auto *TE = dyn_cast<CXXThisExpr>(BE)) { 17526 // Add store "this" pointer to class in DSAStackTy for future checking 17527 DSAS->addMappedClassesQualTypes(TE->getType()); 17528 // Try to find the associated user-defined mapper. 17529 ExprResult ER = buildUserDefinedMapperRef( 17530 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId, 17531 VE->getType().getCanonicalType(), UnresolvedMapper); 17532 if (ER.isInvalid()) 17533 continue; 17534 MVLI.UDMapperList.push_back(ER.get()); 17535 // Skip restriction checking for variable or field declarations 17536 MVLI.ProcessedVarList.push_back(RE); 17537 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 17538 MVLI.VarComponents.back().append(CurComponents.begin(), 17539 CurComponents.end()); 17540 MVLI.VarBaseDeclarations.push_back(nullptr); 17541 continue; 17542 } 17543 17544 // For the following checks, we rely on the base declaration which is 17545 // expected to be associated with the last component. The declaration is 17546 // expected to be a variable or a field (if 'this' is being mapped). 17547 CurDeclaration = CurComponents.back().getAssociatedDeclaration(); 17548 assert(CurDeclaration && "Null decl on map clause."); 17549 assert( 17550 CurDeclaration->isCanonicalDecl() && 17551 "Expecting components to have associated only canonical declarations."); 17552 17553 auto *VD = dyn_cast<VarDecl>(CurDeclaration); 17554 const auto *FD = dyn_cast<FieldDecl>(CurDeclaration); 17555 17556 assert((VD || FD) && "Only variables or fields are expected here!"); 17557 (void)FD; 17558 17559 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10] 17560 // threadprivate variables cannot appear in a map clause. 17561 // OpenMP 4.5 [2.10.5, target update Construct] 17562 // threadprivate variables cannot appear in a from clause. 17563 if (VD && DSAS->isThreadPrivate(VD)) { 17564 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false); 17565 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause) 17566 << getOpenMPClauseName(CKind); 17567 reportOriginalDsa(SemaRef, DSAS, VD, DVar); 17568 continue; 17569 } 17570 17571 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9] 17572 // A list item cannot appear in both a map clause and a data-sharing 17573 // attribute clause on the same construct. 17574 17575 // Check conflicts with other map clause expressions. We check the conflicts 17576 // with the current construct separately from the enclosing data 17577 // environment, because the restrictions are different. We only have to 17578 // check conflicts across regions for the map clauses. 17579 if (checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr, 17580 /*CurrentRegionOnly=*/true, CurComponents, CKind)) 17581 break; 17582 if (CKind == OMPC_map && 17583 (SemaRef.getLangOpts().OpenMP <= 45 || StartLoc.isValid()) && 17584 checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr, 17585 /*CurrentRegionOnly=*/false, CurComponents, CKind)) 17586 break; 17587 17588 // OpenMP 4.5 [2.10.5, target update Construct] 17589 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 17590 // If the type of a list item is a reference to a type T then the type will 17591 // be considered to be T for all purposes of this clause. 17592 auto I = llvm::find_if( 17593 CurComponents, 17594 [](const OMPClauseMappableExprCommon::MappableComponent &MC) { 17595 return MC.getAssociatedDeclaration(); 17596 }); 17597 assert(I != CurComponents.end() && "Null decl on map clause."); 17598 QualType Type; 17599 auto *ASE = dyn_cast<ArraySubscriptExpr>(VE->IgnoreParens()); 17600 auto *OASE = dyn_cast<OMPArraySectionExpr>(VE->IgnoreParens()); 17601 auto *OAShE = dyn_cast<OMPArrayShapingExpr>(VE->IgnoreParens()); 17602 if (ASE) { 17603 Type = ASE->getType().getNonReferenceType(); 17604 } else if (OASE) { 17605 QualType BaseType = 17606 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 17607 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 17608 Type = ATy->getElementType(); 17609 else 17610 Type = BaseType->getPointeeType(); 17611 Type = Type.getNonReferenceType(); 17612 } else if (OAShE) { 17613 Type = OAShE->getBase()->getType()->getPointeeType(); 17614 } else { 17615 Type = VE->getType(); 17616 } 17617 17618 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4] 17619 // A list item in a to or from clause must have a mappable type. 17620 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9] 17621 // A list item must have a mappable type. 17622 if (!checkTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef, 17623 DSAS, Type)) 17624 continue; 17625 17626 Type = I->getAssociatedDeclaration()->getType().getNonReferenceType(); 17627 17628 if (CKind == OMPC_map) { 17629 // target enter data 17630 // OpenMP [2.10.2, Restrictions, p. 99] 17631 // A map-type must be specified in all map clauses and must be either 17632 // to or alloc. 17633 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective(); 17634 if (DKind == OMPD_target_enter_data && 17635 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) { 17636 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 17637 << (IsMapTypeImplicit ? 1 : 0) 17638 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 17639 << getOpenMPDirectiveName(DKind); 17640 continue; 17641 } 17642 17643 // target exit_data 17644 // OpenMP [2.10.3, Restrictions, p. 102] 17645 // A map-type must be specified in all map clauses and must be either 17646 // from, release, or delete. 17647 if (DKind == OMPD_target_exit_data && 17648 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release || 17649 MapType == OMPC_MAP_delete)) { 17650 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 17651 << (IsMapTypeImplicit ? 1 : 0) 17652 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 17653 << getOpenMPDirectiveName(DKind); 17654 continue; 17655 } 17656 17657 // target, target data 17658 // OpenMP 5.0 [2.12.2, Restrictions, p. 163] 17659 // OpenMP 5.0 [2.12.5, Restrictions, p. 174] 17660 // A map-type in a map clause must be to, from, tofrom or alloc 17661 if ((DKind == OMPD_target_data || 17662 isOpenMPTargetExecutionDirective(DKind)) && 17663 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_from || 17664 MapType == OMPC_MAP_tofrom || MapType == OMPC_MAP_alloc)) { 17665 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 17666 << (IsMapTypeImplicit ? 1 : 0) 17667 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 17668 << getOpenMPDirectiveName(DKind); 17669 continue; 17670 } 17671 17672 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 17673 // A list item cannot appear in both a map clause and a data-sharing 17674 // attribute clause on the same construct 17675 // 17676 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7] 17677 // A list item cannot appear in both a map clause and a data-sharing 17678 // attribute clause on the same construct unless the construct is a 17679 // combined construct. 17680 if (VD && ((SemaRef.LangOpts.OpenMP <= 45 && 17681 isOpenMPTargetExecutionDirective(DKind)) || 17682 DKind == OMPD_target)) { 17683 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false); 17684 if (isOpenMPPrivate(DVar.CKind)) { 17685 SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 17686 << getOpenMPClauseName(DVar.CKind) 17687 << getOpenMPClauseName(OMPC_map) 17688 << getOpenMPDirectiveName(DSAS->getCurrentDirective()); 17689 reportOriginalDsa(SemaRef, DSAS, CurDeclaration, DVar); 17690 continue; 17691 } 17692 } 17693 } 17694 17695 // Try to find the associated user-defined mapper. 17696 ExprResult ER = buildUserDefinedMapperRef( 17697 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId, 17698 Type.getCanonicalType(), UnresolvedMapper); 17699 if (ER.isInvalid()) 17700 continue; 17701 MVLI.UDMapperList.push_back(ER.get()); 17702 17703 // Save the current expression. 17704 MVLI.ProcessedVarList.push_back(RE); 17705 17706 // Store the components in the stack so that they can be used to check 17707 // against other clauses later on. 17708 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents, 17709 /*WhereFoundClauseKind=*/OMPC_map); 17710 17711 // Save the components and declaration to create the clause. For purposes of 17712 // the clause creation, any component list that has has base 'this' uses 17713 // null as base declaration. 17714 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 17715 MVLI.VarComponents.back().append(CurComponents.begin(), 17716 CurComponents.end()); 17717 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr 17718 : CurDeclaration); 17719 } 17720 } 17721 17722 OMPClause *Sema::ActOnOpenMPMapClause( 17723 ArrayRef<OpenMPMapModifierKind> MapTypeModifiers, 17724 ArrayRef<SourceLocation> MapTypeModifiersLoc, 17725 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId, 17726 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, SourceLocation MapLoc, 17727 SourceLocation ColonLoc, ArrayRef<Expr *> VarList, 17728 const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) { 17729 OpenMPMapModifierKind Modifiers[] = { 17730 OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown, 17731 OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown}; 17732 SourceLocation ModifiersLoc[NumberOfOMPMapClauseModifiers]; 17733 17734 // Process map-type-modifiers, flag errors for duplicate modifiers. 17735 unsigned Count = 0; 17736 for (unsigned I = 0, E = MapTypeModifiers.size(); I < E; ++I) { 17737 if (MapTypeModifiers[I] != OMPC_MAP_MODIFIER_unknown && 17738 llvm::find(Modifiers, MapTypeModifiers[I]) != std::end(Modifiers)) { 17739 Diag(MapTypeModifiersLoc[I], diag::err_omp_duplicate_map_type_modifier); 17740 continue; 17741 } 17742 assert(Count < NumberOfOMPMapClauseModifiers && 17743 "Modifiers exceed the allowed number of map type modifiers"); 17744 Modifiers[Count] = MapTypeModifiers[I]; 17745 ModifiersLoc[Count] = MapTypeModifiersLoc[I]; 17746 ++Count; 17747 } 17748 17749 MappableVarListInfo MVLI(VarList); 17750 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, Locs.StartLoc, 17751 MapperIdScopeSpec, MapperId, UnresolvedMappers, 17752 MapType, IsMapTypeImplicit); 17753 17754 // We need to produce a map clause even if we don't have variables so that 17755 // other diagnostics related with non-existing map clauses are accurate. 17756 return OMPMapClause::Create(Context, Locs, MVLI.ProcessedVarList, 17757 MVLI.VarBaseDeclarations, MVLI.VarComponents, 17758 MVLI.UDMapperList, Modifiers, ModifiersLoc, 17759 MapperIdScopeSpec.getWithLocInContext(Context), 17760 MapperId, MapType, IsMapTypeImplicit, MapLoc); 17761 } 17762 17763 QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc, 17764 TypeResult ParsedType) { 17765 assert(ParsedType.isUsable()); 17766 17767 QualType ReductionType = GetTypeFromParser(ParsedType.get()); 17768 if (ReductionType.isNull()) 17769 return QualType(); 17770 17771 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++ 17772 // A type name in a declare reduction directive cannot be a function type, an 17773 // array type, a reference type, or a type qualified with const, volatile or 17774 // restrict. 17775 if (ReductionType.hasQualifiers()) { 17776 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0; 17777 return QualType(); 17778 } 17779 17780 if (ReductionType->isFunctionType()) { 17781 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1; 17782 return QualType(); 17783 } 17784 if (ReductionType->isReferenceType()) { 17785 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2; 17786 return QualType(); 17787 } 17788 if (ReductionType->isArrayType()) { 17789 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3; 17790 return QualType(); 17791 } 17792 return ReductionType; 17793 } 17794 17795 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart( 17796 Scope *S, DeclContext *DC, DeclarationName Name, 17797 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes, 17798 AccessSpecifier AS, Decl *PrevDeclInScope) { 17799 SmallVector<Decl *, 8> Decls; 17800 Decls.reserve(ReductionTypes.size()); 17801 17802 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName, 17803 forRedeclarationInCurContext()); 17804 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions 17805 // A reduction-identifier may not be re-declared in the current scope for the 17806 // same type or for a type that is compatible according to the base language 17807 // rules. 17808 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes; 17809 OMPDeclareReductionDecl *PrevDRD = nullptr; 17810 bool InCompoundScope = true; 17811 if (S != nullptr) { 17812 // Find previous declaration with the same name not referenced in other 17813 // declarations. 17814 FunctionScopeInfo *ParentFn = getEnclosingFunction(); 17815 InCompoundScope = 17816 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty(); 17817 LookupName(Lookup, S); 17818 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false, 17819 /*AllowInlineNamespace=*/false); 17820 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious; 17821 LookupResult::Filter Filter = Lookup.makeFilter(); 17822 while (Filter.hasNext()) { 17823 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next()); 17824 if (InCompoundScope) { 17825 auto I = UsedAsPrevious.find(PrevDecl); 17826 if (I == UsedAsPrevious.end()) 17827 UsedAsPrevious[PrevDecl] = false; 17828 if (OMPDeclareReductionDecl *D = PrevDecl->getPrevDeclInScope()) 17829 UsedAsPrevious[D] = true; 17830 } 17831 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] = 17832 PrevDecl->getLocation(); 17833 } 17834 Filter.done(); 17835 if (InCompoundScope) { 17836 for (const auto &PrevData : UsedAsPrevious) { 17837 if (!PrevData.second) { 17838 PrevDRD = PrevData.first; 17839 break; 17840 } 17841 } 17842 } 17843 } else if (PrevDeclInScope != nullptr) { 17844 auto *PrevDRDInScope = PrevDRD = 17845 cast<OMPDeclareReductionDecl>(PrevDeclInScope); 17846 do { 17847 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] = 17848 PrevDRDInScope->getLocation(); 17849 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope(); 17850 } while (PrevDRDInScope != nullptr); 17851 } 17852 for (const auto &TyData : ReductionTypes) { 17853 const auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType()); 17854 bool Invalid = false; 17855 if (I != PreviousRedeclTypes.end()) { 17856 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition) 17857 << TyData.first; 17858 Diag(I->second, diag::note_previous_definition); 17859 Invalid = true; 17860 } 17861 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second; 17862 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second, 17863 Name, TyData.first, PrevDRD); 17864 DC->addDecl(DRD); 17865 DRD->setAccess(AS); 17866 Decls.push_back(DRD); 17867 if (Invalid) 17868 DRD->setInvalidDecl(); 17869 else 17870 PrevDRD = DRD; 17871 } 17872 17873 return DeclGroupPtrTy::make( 17874 DeclGroupRef::Create(Context, Decls.begin(), Decls.size())); 17875 } 17876 17877 void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) { 17878 auto *DRD = cast<OMPDeclareReductionDecl>(D); 17879 17880 // Enter new function scope. 17881 PushFunctionScope(); 17882 setFunctionHasBranchProtectedScope(); 17883 getCurFunction()->setHasOMPDeclareReductionCombiner(); 17884 17885 if (S != nullptr) 17886 PushDeclContext(S, DRD); 17887 else 17888 CurContext = DRD; 17889 17890 PushExpressionEvaluationContext( 17891 ExpressionEvaluationContext::PotentiallyEvaluated); 17892 17893 QualType ReductionType = DRD->getType(); 17894 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will 17895 // be replaced by '*omp_parm' during codegen. This required because 'omp_in' 17896 // uses semantics of argument handles by value, but it should be passed by 17897 // reference. C lang does not support references, so pass all parameters as 17898 // pointers. 17899 // Create 'T omp_in;' variable. 17900 VarDecl *OmpInParm = 17901 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in"); 17902 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will 17903 // be replaced by '*omp_parm' during codegen. This required because 'omp_out' 17904 // uses semantics of argument handles by value, but it should be passed by 17905 // reference. C lang does not support references, so pass all parameters as 17906 // pointers. 17907 // Create 'T omp_out;' variable. 17908 VarDecl *OmpOutParm = 17909 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out"); 17910 if (S != nullptr) { 17911 PushOnScopeChains(OmpInParm, S); 17912 PushOnScopeChains(OmpOutParm, S); 17913 } else { 17914 DRD->addDecl(OmpInParm); 17915 DRD->addDecl(OmpOutParm); 17916 } 17917 Expr *InE = 17918 ::buildDeclRefExpr(*this, OmpInParm, ReductionType, D->getLocation()); 17919 Expr *OutE = 17920 ::buildDeclRefExpr(*this, OmpOutParm, ReductionType, D->getLocation()); 17921 DRD->setCombinerData(InE, OutE); 17922 } 17923 17924 void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) { 17925 auto *DRD = cast<OMPDeclareReductionDecl>(D); 17926 DiscardCleanupsInEvaluationContext(); 17927 PopExpressionEvaluationContext(); 17928 17929 PopDeclContext(); 17930 PopFunctionScopeInfo(); 17931 17932 if (Combiner != nullptr) 17933 DRD->setCombiner(Combiner); 17934 else 17935 DRD->setInvalidDecl(); 17936 } 17937 17938 VarDecl *Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) { 17939 auto *DRD = cast<OMPDeclareReductionDecl>(D); 17940 17941 // Enter new function scope. 17942 PushFunctionScope(); 17943 setFunctionHasBranchProtectedScope(); 17944 17945 if (S != nullptr) 17946 PushDeclContext(S, DRD); 17947 else 17948 CurContext = DRD; 17949 17950 PushExpressionEvaluationContext( 17951 ExpressionEvaluationContext::PotentiallyEvaluated); 17952 17953 QualType ReductionType = DRD->getType(); 17954 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will 17955 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv' 17956 // uses semantics of argument handles by value, but it should be passed by 17957 // reference. C lang does not support references, so pass all parameters as 17958 // pointers. 17959 // Create 'T omp_priv;' variable. 17960 VarDecl *OmpPrivParm = 17961 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv"); 17962 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will 17963 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig' 17964 // uses semantics of argument handles by value, but it should be passed by 17965 // reference. C lang does not support references, so pass all parameters as 17966 // pointers. 17967 // Create 'T omp_orig;' variable. 17968 VarDecl *OmpOrigParm = 17969 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig"); 17970 if (S != nullptr) { 17971 PushOnScopeChains(OmpPrivParm, S); 17972 PushOnScopeChains(OmpOrigParm, S); 17973 } else { 17974 DRD->addDecl(OmpPrivParm); 17975 DRD->addDecl(OmpOrigParm); 17976 } 17977 Expr *OrigE = 17978 ::buildDeclRefExpr(*this, OmpOrigParm, ReductionType, D->getLocation()); 17979 Expr *PrivE = 17980 ::buildDeclRefExpr(*this, OmpPrivParm, ReductionType, D->getLocation()); 17981 DRD->setInitializerData(OrigE, PrivE); 17982 return OmpPrivParm; 17983 } 17984 17985 void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, Expr *Initializer, 17986 VarDecl *OmpPrivParm) { 17987 auto *DRD = cast<OMPDeclareReductionDecl>(D); 17988 DiscardCleanupsInEvaluationContext(); 17989 PopExpressionEvaluationContext(); 17990 17991 PopDeclContext(); 17992 PopFunctionScopeInfo(); 17993 17994 if (Initializer != nullptr) { 17995 DRD->setInitializer(Initializer, OMPDeclareReductionDecl::CallInit); 17996 } else if (OmpPrivParm->hasInit()) { 17997 DRD->setInitializer(OmpPrivParm->getInit(), 17998 OmpPrivParm->isDirectInit() 17999 ? OMPDeclareReductionDecl::DirectInit 18000 : OMPDeclareReductionDecl::CopyInit); 18001 } else { 18002 DRD->setInvalidDecl(); 18003 } 18004 } 18005 18006 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd( 18007 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) { 18008 for (Decl *D : DeclReductions.get()) { 18009 if (IsValid) { 18010 if (S) 18011 PushOnScopeChains(cast<OMPDeclareReductionDecl>(D), S, 18012 /*AddToContext=*/false); 18013 } else { 18014 D->setInvalidDecl(); 18015 } 18016 } 18017 return DeclReductions; 18018 } 18019 18020 TypeResult Sema::ActOnOpenMPDeclareMapperVarDecl(Scope *S, Declarator &D) { 18021 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); 18022 QualType T = TInfo->getType(); 18023 if (D.isInvalidType()) 18024 return true; 18025 18026 if (getLangOpts().CPlusPlus) { 18027 // Check that there are no default arguments (C++ only). 18028 CheckExtraCXXDefaultArguments(D); 18029 } 18030 18031 return CreateParsedType(T, TInfo); 18032 } 18033 18034 QualType Sema::ActOnOpenMPDeclareMapperType(SourceLocation TyLoc, 18035 TypeResult ParsedType) { 18036 assert(ParsedType.isUsable() && "Expect usable parsed mapper type"); 18037 18038 QualType MapperType = GetTypeFromParser(ParsedType.get()); 18039 assert(!MapperType.isNull() && "Expect valid mapper type"); 18040 18041 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions 18042 // The type must be of struct, union or class type in C and C++ 18043 if (!MapperType->isStructureOrClassType() && !MapperType->isUnionType()) { 18044 Diag(TyLoc, diag::err_omp_mapper_wrong_type); 18045 return QualType(); 18046 } 18047 return MapperType; 18048 } 18049 18050 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareMapperDirective( 18051 Scope *S, DeclContext *DC, DeclarationName Name, QualType MapperType, 18052 SourceLocation StartLoc, DeclarationName VN, AccessSpecifier AS, 18053 Expr *MapperVarRef, ArrayRef<OMPClause *> Clauses, Decl *PrevDeclInScope) { 18054 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPMapperName, 18055 forRedeclarationInCurContext()); 18056 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions 18057 // A mapper-identifier may not be redeclared in the current scope for the 18058 // same type or for a type that is compatible according to the base language 18059 // rules. 18060 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes; 18061 OMPDeclareMapperDecl *PrevDMD = nullptr; 18062 bool InCompoundScope = true; 18063 if (S != nullptr) { 18064 // Find previous declaration with the same name not referenced in other 18065 // declarations. 18066 FunctionScopeInfo *ParentFn = getEnclosingFunction(); 18067 InCompoundScope = 18068 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty(); 18069 LookupName(Lookup, S); 18070 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false, 18071 /*AllowInlineNamespace=*/false); 18072 llvm::DenseMap<OMPDeclareMapperDecl *, bool> UsedAsPrevious; 18073 LookupResult::Filter Filter = Lookup.makeFilter(); 18074 while (Filter.hasNext()) { 18075 auto *PrevDecl = cast<OMPDeclareMapperDecl>(Filter.next()); 18076 if (InCompoundScope) { 18077 auto I = UsedAsPrevious.find(PrevDecl); 18078 if (I == UsedAsPrevious.end()) 18079 UsedAsPrevious[PrevDecl] = false; 18080 if (OMPDeclareMapperDecl *D = PrevDecl->getPrevDeclInScope()) 18081 UsedAsPrevious[D] = true; 18082 } 18083 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] = 18084 PrevDecl->getLocation(); 18085 } 18086 Filter.done(); 18087 if (InCompoundScope) { 18088 for (const auto &PrevData : UsedAsPrevious) { 18089 if (!PrevData.second) { 18090 PrevDMD = PrevData.first; 18091 break; 18092 } 18093 } 18094 } 18095 } else if (PrevDeclInScope) { 18096 auto *PrevDMDInScope = PrevDMD = 18097 cast<OMPDeclareMapperDecl>(PrevDeclInScope); 18098 do { 18099 PreviousRedeclTypes[PrevDMDInScope->getType().getCanonicalType()] = 18100 PrevDMDInScope->getLocation(); 18101 PrevDMDInScope = PrevDMDInScope->getPrevDeclInScope(); 18102 } while (PrevDMDInScope != nullptr); 18103 } 18104 const auto I = PreviousRedeclTypes.find(MapperType.getCanonicalType()); 18105 bool Invalid = false; 18106 if (I != PreviousRedeclTypes.end()) { 18107 Diag(StartLoc, diag::err_omp_declare_mapper_redefinition) 18108 << MapperType << Name; 18109 Diag(I->second, diag::note_previous_definition); 18110 Invalid = true; 18111 } 18112 auto *DMD = OMPDeclareMapperDecl::Create(Context, DC, StartLoc, Name, 18113 MapperType, VN, Clauses, PrevDMD); 18114 if (S) 18115 PushOnScopeChains(DMD, S); 18116 else 18117 DC->addDecl(DMD); 18118 DMD->setAccess(AS); 18119 if (Invalid) 18120 DMD->setInvalidDecl(); 18121 18122 auto *VD = cast<DeclRefExpr>(MapperVarRef)->getDecl(); 18123 VD->setDeclContext(DMD); 18124 VD->setLexicalDeclContext(DMD); 18125 DMD->addDecl(VD); 18126 DMD->setMapperVarRef(MapperVarRef); 18127 18128 return DeclGroupPtrTy::make(DeclGroupRef(DMD)); 18129 } 18130 18131 ExprResult 18132 Sema::ActOnOpenMPDeclareMapperDirectiveVarDecl(Scope *S, QualType MapperType, 18133 SourceLocation StartLoc, 18134 DeclarationName VN) { 18135 TypeSourceInfo *TInfo = 18136 Context.getTrivialTypeSourceInfo(MapperType, StartLoc); 18137 auto *VD = VarDecl::Create(Context, Context.getTranslationUnitDecl(), 18138 StartLoc, StartLoc, VN.getAsIdentifierInfo(), 18139 MapperType, TInfo, SC_None); 18140 if (S) 18141 PushOnScopeChains(VD, S, /*AddToContext=*/false); 18142 Expr *E = buildDeclRefExpr(*this, VD, MapperType, StartLoc); 18143 DSAStack->addDeclareMapperVarRef(E); 18144 return E; 18145 } 18146 18147 bool Sema::isOpenMPDeclareMapperVarDeclAllowed(const VarDecl *VD) const { 18148 assert(LangOpts.OpenMP && "Expected OpenMP mode."); 18149 const Expr *Ref = DSAStack->getDeclareMapperVarRef(); 18150 if (const auto *DRE = cast_or_null<DeclRefExpr>(Ref)) 18151 return VD->getCanonicalDecl() == DRE->getDecl()->getCanonicalDecl(); 18152 return true; 18153 } 18154 18155 const ValueDecl *Sema::getOpenMPDeclareMapperVarName() const { 18156 assert(LangOpts.OpenMP && "Expected OpenMP mode."); 18157 return cast<DeclRefExpr>(DSAStack->getDeclareMapperVarRef())->getDecl(); 18158 } 18159 18160 OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams, 18161 SourceLocation StartLoc, 18162 SourceLocation LParenLoc, 18163 SourceLocation EndLoc) { 18164 Expr *ValExpr = NumTeams; 18165 Stmt *HelperValStmt = nullptr; 18166 18167 // OpenMP [teams Constrcut, Restrictions] 18168 // The num_teams expression must evaluate to a positive integer value. 18169 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams, 18170 /*StrictlyPositive=*/true)) 18171 return nullptr; 18172 18173 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 18174 OpenMPDirectiveKind CaptureRegion = 18175 getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams, LangOpts.OpenMP); 18176 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 18177 ValExpr = MakeFullExpr(ValExpr).get(); 18178 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 18179 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 18180 HelperValStmt = buildPreInits(Context, Captures); 18181 } 18182 18183 return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion, 18184 StartLoc, LParenLoc, EndLoc); 18185 } 18186 18187 OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit, 18188 SourceLocation StartLoc, 18189 SourceLocation LParenLoc, 18190 SourceLocation EndLoc) { 18191 Expr *ValExpr = ThreadLimit; 18192 Stmt *HelperValStmt = nullptr; 18193 18194 // OpenMP [teams Constrcut, Restrictions] 18195 // The thread_limit expression must evaluate to a positive integer value. 18196 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit, 18197 /*StrictlyPositive=*/true)) 18198 return nullptr; 18199 18200 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 18201 OpenMPDirectiveKind CaptureRegion = getOpenMPCaptureRegionForClause( 18202 DKind, OMPC_thread_limit, LangOpts.OpenMP); 18203 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 18204 ValExpr = MakeFullExpr(ValExpr).get(); 18205 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 18206 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 18207 HelperValStmt = buildPreInits(Context, Captures); 18208 } 18209 18210 return new (Context) OMPThreadLimitClause( 18211 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc); 18212 } 18213 18214 OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority, 18215 SourceLocation StartLoc, 18216 SourceLocation LParenLoc, 18217 SourceLocation EndLoc) { 18218 Expr *ValExpr = Priority; 18219 Stmt *HelperValStmt = nullptr; 18220 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 18221 18222 // OpenMP [2.9.1, task Constrcut] 18223 // The priority-value is a non-negative numerical scalar expression. 18224 if (!isNonNegativeIntegerValue( 18225 ValExpr, *this, OMPC_priority, 18226 /*StrictlyPositive=*/false, /*BuildCapture=*/true, 18227 DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt)) 18228 return nullptr; 18229 18230 return new (Context) OMPPriorityClause(ValExpr, HelperValStmt, CaptureRegion, 18231 StartLoc, LParenLoc, EndLoc); 18232 } 18233 18234 OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize, 18235 SourceLocation StartLoc, 18236 SourceLocation LParenLoc, 18237 SourceLocation EndLoc) { 18238 Expr *ValExpr = Grainsize; 18239 Stmt *HelperValStmt = nullptr; 18240 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 18241 18242 // OpenMP [2.9.2, taskloop Constrcut] 18243 // The parameter of the grainsize clause must be a positive integer 18244 // expression. 18245 if (!isNonNegativeIntegerValue( 18246 ValExpr, *this, OMPC_grainsize, 18247 /*StrictlyPositive=*/true, /*BuildCapture=*/true, 18248 DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt)) 18249 return nullptr; 18250 18251 return new (Context) OMPGrainsizeClause(ValExpr, HelperValStmt, CaptureRegion, 18252 StartLoc, LParenLoc, EndLoc); 18253 } 18254 18255 OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks, 18256 SourceLocation StartLoc, 18257 SourceLocation LParenLoc, 18258 SourceLocation EndLoc) { 18259 Expr *ValExpr = NumTasks; 18260 Stmt *HelperValStmt = nullptr; 18261 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 18262 18263 // OpenMP [2.9.2, taskloop Constrcut] 18264 // The parameter of the num_tasks clause must be a positive integer 18265 // expression. 18266 if (!isNonNegativeIntegerValue( 18267 ValExpr, *this, OMPC_num_tasks, 18268 /*StrictlyPositive=*/true, /*BuildCapture=*/true, 18269 DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt)) 18270 return nullptr; 18271 18272 return new (Context) OMPNumTasksClause(ValExpr, HelperValStmt, CaptureRegion, 18273 StartLoc, LParenLoc, EndLoc); 18274 } 18275 18276 OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc, 18277 SourceLocation LParenLoc, 18278 SourceLocation EndLoc) { 18279 // OpenMP [2.13.2, critical construct, Description] 18280 // ... where hint-expression is an integer constant expression that evaluates 18281 // to a valid lock hint. 18282 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint); 18283 if (HintExpr.isInvalid()) 18284 return nullptr; 18285 return new (Context) 18286 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc); 18287 } 18288 18289 /// Tries to find omp_event_handle_t type. 18290 static bool findOMPEventHandleT(Sema &S, SourceLocation Loc, 18291 DSAStackTy *Stack) { 18292 QualType OMPEventHandleT = Stack->getOMPEventHandleT(); 18293 if (!OMPEventHandleT.isNull()) 18294 return true; 18295 IdentifierInfo *II = &S.PP.getIdentifierTable().get("omp_event_handle_t"); 18296 ParsedType PT = S.getTypeName(*II, Loc, S.getCurScope()); 18297 if (!PT.getAsOpaquePtr() || PT.get().isNull()) { 18298 S.Diag(Loc, diag::err_omp_implied_type_not_found) << "omp_event_handle_t"; 18299 return false; 18300 } 18301 Stack->setOMPEventHandleT(PT.get()); 18302 return true; 18303 } 18304 18305 OMPClause *Sema::ActOnOpenMPDetachClause(Expr *Evt, SourceLocation StartLoc, 18306 SourceLocation LParenLoc, 18307 SourceLocation EndLoc) { 18308 if (!Evt->isValueDependent() && !Evt->isTypeDependent() && 18309 !Evt->isInstantiationDependent() && 18310 !Evt->containsUnexpandedParameterPack()) { 18311 if (!findOMPEventHandleT(*this, Evt->getExprLoc(), DSAStack)) 18312 return nullptr; 18313 // OpenMP 5.0, 2.10.1 task Construct. 18314 // event-handle is a variable of the omp_event_handle_t type. 18315 auto *Ref = dyn_cast<DeclRefExpr>(Evt->IgnoreParenImpCasts()); 18316 if (!Ref) { 18317 Diag(Evt->getExprLoc(), diag::err_omp_var_expected) 18318 << "omp_event_handle_t" << 0 << Evt->getSourceRange(); 18319 return nullptr; 18320 } 18321 auto *VD = dyn_cast_or_null<VarDecl>(Ref->getDecl()); 18322 if (!VD) { 18323 Diag(Evt->getExprLoc(), diag::err_omp_var_expected) 18324 << "omp_event_handle_t" << 0 << Evt->getSourceRange(); 18325 return nullptr; 18326 } 18327 if (!Context.hasSameUnqualifiedType(DSAStack->getOMPEventHandleT(), 18328 VD->getType()) || 18329 VD->getType().isConstant(Context)) { 18330 Diag(Evt->getExprLoc(), diag::err_omp_var_expected) 18331 << "omp_event_handle_t" << 1 << VD->getType() 18332 << Evt->getSourceRange(); 18333 return nullptr; 18334 } 18335 // OpenMP 5.0, 2.10.1 task Construct 18336 // [detach clause]... The event-handle will be considered as if it was 18337 // specified on a firstprivate clause. 18338 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, /*FromParent=*/false); 18339 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate && 18340 DVar.RefExpr) { 18341 Diag(Evt->getExprLoc(), diag::err_omp_wrong_dsa) 18342 << getOpenMPClauseName(DVar.CKind) 18343 << getOpenMPClauseName(OMPC_firstprivate); 18344 reportOriginalDsa(*this, DSAStack, VD, DVar); 18345 return nullptr; 18346 } 18347 } 18348 18349 return new (Context) OMPDetachClause(Evt, StartLoc, LParenLoc, EndLoc); 18350 } 18351 18352 OMPClause *Sema::ActOnOpenMPDistScheduleClause( 18353 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, 18354 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc, 18355 SourceLocation EndLoc) { 18356 if (Kind == OMPC_DIST_SCHEDULE_unknown) { 18357 std::string Values; 18358 Values += "'"; 18359 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0); 18360 Values += "'"; 18361 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 18362 << Values << getOpenMPClauseName(OMPC_dist_schedule); 18363 return nullptr; 18364 } 18365 Expr *ValExpr = ChunkSize; 18366 Stmt *HelperValStmt = nullptr; 18367 if (ChunkSize) { 18368 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && 18369 !ChunkSize->isInstantiationDependent() && 18370 !ChunkSize->containsUnexpandedParameterPack()) { 18371 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc(); 18372 ExprResult Val = 18373 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); 18374 if (Val.isInvalid()) 18375 return nullptr; 18376 18377 ValExpr = Val.get(); 18378 18379 // OpenMP [2.7.1, Restrictions] 18380 // chunk_size must be a loop invariant integer expression with a positive 18381 // value. 18382 if (Optional<llvm::APSInt> Result = 18383 ValExpr->getIntegerConstantExpr(Context)) { 18384 if (Result->isSigned() && !Result->isStrictlyPositive()) { 18385 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) 18386 << "dist_schedule" << ChunkSize->getSourceRange(); 18387 return nullptr; 18388 } 18389 } else if (getOpenMPCaptureRegionForClause( 18390 DSAStack->getCurrentDirective(), OMPC_dist_schedule, 18391 LangOpts.OpenMP) != OMPD_unknown && 18392 !CurContext->isDependentContext()) { 18393 ValExpr = MakeFullExpr(ValExpr).get(); 18394 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 18395 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 18396 HelperValStmt = buildPreInits(Context, Captures); 18397 } 18398 } 18399 } 18400 18401 return new (Context) 18402 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, 18403 Kind, ValExpr, HelperValStmt); 18404 } 18405 18406 OMPClause *Sema::ActOnOpenMPDefaultmapClause( 18407 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind, 18408 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc, 18409 SourceLocation KindLoc, SourceLocation EndLoc) { 18410 if (getLangOpts().OpenMP < 50) { 18411 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || 18412 Kind != OMPC_DEFAULTMAP_scalar) { 18413 std::string Value; 18414 SourceLocation Loc; 18415 Value += "'"; 18416 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) { 18417 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap, 18418 OMPC_DEFAULTMAP_MODIFIER_tofrom); 18419 Loc = MLoc; 18420 } else { 18421 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap, 18422 OMPC_DEFAULTMAP_scalar); 18423 Loc = KindLoc; 18424 } 18425 Value += "'"; 18426 Diag(Loc, diag::err_omp_unexpected_clause_value) 18427 << Value << getOpenMPClauseName(OMPC_defaultmap); 18428 return nullptr; 18429 } 18430 } else { 18431 bool isDefaultmapModifier = (M != OMPC_DEFAULTMAP_MODIFIER_unknown); 18432 bool isDefaultmapKind = (Kind != OMPC_DEFAULTMAP_unknown) || 18433 (LangOpts.OpenMP >= 50 && KindLoc.isInvalid()); 18434 if (!isDefaultmapKind || !isDefaultmapModifier) { 18435 std::string ModifierValue = "'alloc', 'from', 'to', 'tofrom', " 18436 "'firstprivate', 'none', 'default'"; 18437 std::string KindValue = "'scalar', 'aggregate', 'pointer'"; 18438 if (!isDefaultmapKind && isDefaultmapModifier) { 18439 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 18440 << KindValue << getOpenMPClauseName(OMPC_defaultmap); 18441 } else if (isDefaultmapKind && !isDefaultmapModifier) { 18442 Diag(MLoc, diag::err_omp_unexpected_clause_value) 18443 << ModifierValue << getOpenMPClauseName(OMPC_defaultmap); 18444 } else { 18445 Diag(MLoc, diag::err_omp_unexpected_clause_value) 18446 << ModifierValue << getOpenMPClauseName(OMPC_defaultmap); 18447 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 18448 << KindValue << getOpenMPClauseName(OMPC_defaultmap); 18449 } 18450 return nullptr; 18451 } 18452 18453 // OpenMP [5.0, 2.12.5, Restrictions, p. 174] 18454 // At most one defaultmap clause for each category can appear on the 18455 // directive. 18456 if (DSAStack->checkDefaultmapCategory(Kind)) { 18457 Diag(StartLoc, diag::err_omp_one_defaultmap_each_category); 18458 return nullptr; 18459 } 18460 } 18461 if (Kind == OMPC_DEFAULTMAP_unknown) { 18462 // Variable category is not specified - mark all categories. 18463 DSAStack->setDefaultDMAAttr(M, OMPC_DEFAULTMAP_aggregate, StartLoc); 18464 DSAStack->setDefaultDMAAttr(M, OMPC_DEFAULTMAP_scalar, StartLoc); 18465 DSAStack->setDefaultDMAAttr(M, OMPC_DEFAULTMAP_pointer, StartLoc); 18466 } else { 18467 DSAStack->setDefaultDMAAttr(M, Kind, StartLoc); 18468 } 18469 18470 return new (Context) 18471 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M); 18472 } 18473 18474 bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) { 18475 DeclContext *CurLexicalContext = getCurLexicalContext(); 18476 if (!CurLexicalContext->isFileContext() && 18477 !CurLexicalContext->isExternCContext() && 18478 !CurLexicalContext->isExternCXXContext() && 18479 !isa<CXXRecordDecl>(CurLexicalContext) && 18480 !isa<ClassTemplateDecl>(CurLexicalContext) && 18481 !isa<ClassTemplatePartialSpecializationDecl>(CurLexicalContext) && 18482 !isa<ClassTemplateSpecializationDecl>(CurLexicalContext)) { 18483 Diag(Loc, diag::err_omp_region_not_file_context); 18484 return false; 18485 } 18486 ++DeclareTargetNestingLevel; 18487 return true; 18488 } 18489 18490 void Sema::ActOnFinishOpenMPDeclareTargetDirective() { 18491 assert(DeclareTargetNestingLevel > 0 && 18492 "Unexpected ActOnFinishOpenMPDeclareTargetDirective"); 18493 --DeclareTargetNestingLevel; 18494 } 18495 18496 NamedDecl * 18497 Sema::lookupOpenMPDeclareTargetName(Scope *CurScope, CXXScopeSpec &ScopeSpec, 18498 const DeclarationNameInfo &Id, 18499 NamedDeclSetType &SameDirectiveDecls) { 18500 LookupResult Lookup(*this, Id, LookupOrdinaryName); 18501 LookupParsedName(Lookup, CurScope, &ScopeSpec, true); 18502 18503 if (Lookup.isAmbiguous()) 18504 return nullptr; 18505 Lookup.suppressDiagnostics(); 18506 18507 if (!Lookup.isSingleResult()) { 18508 VarOrFuncDeclFilterCCC CCC(*this); 18509 if (TypoCorrection Corrected = 18510 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, CCC, 18511 CTK_ErrorRecovery)) { 18512 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest) 18513 << Id.getName()); 18514 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl()); 18515 return nullptr; 18516 } 18517 18518 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName(); 18519 return nullptr; 18520 } 18521 18522 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>(); 18523 if (!isa<VarDecl>(ND) && !isa<FunctionDecl>(ND) && 18524 !isa<FunctionTemplateDecl>(ND)) { 18525 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName(); 18526 return nullptr; 18527 } 18528 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl()))) 18529 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName(); 18530 return ND; 18531 } 18532 18533 void Sema::ActOnOpenMPDeclareTargetName( 18534 NamedDecl *ND, SourceLocation Loc, OMPDeclareTargetDeclAttr::MapTypeTy MT, 18535 OMPDeclareTargetDeclAttr::DevTypeTy DT) { 18536 assert((isa<VarDecl>(ND) || isa<FunctionDecl>(ND) || 18537 isa<FunctionTemplateDecl>(ND)) && 18538 "Expected variable, function or function template."); 18539 18540 // Diagnose marking after use as it may lead to incorrect diagnosis and 18541 // codegen. 18542 if (LangOpts.OpenMP >= 50 && 18543 (ND->isUsed(/*CheckUsedAttr=*/false) || ND->isReferenced())) 18544 Diag(Loc, diag::warn_omp_declare_target_after_first_use); 18545 18546 Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy = 18547 OMPDeclareTargetDeclAttr::getDeviceType(cast<ValueDecl>(ND)); 18548 if (DevTy.hasValue() && *DevTy != DT) { 18549 Diag(Loc, diag::err_omp_device_type_mismatch) 18550 << OMPDeclareTargetDeclAttr::ConvertDevTypeTyToStr(DT) 18551 << OMPDeclareTargetDeclAttr::ConvertDevTypeTyToStr(*DevTy); 18552 return; 18553 } 18554 Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 18555 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(cast<ValueDecl>(ND)); 18556 if (!Res) { 18557 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT, DT, 18558 SourceRange(Loc, Loc)); 18559 ND->addAttr(A); 18560 if (ASTMutationListener *ML = Context.getASTMutationListener()) 18561 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A); 18562 checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Loc); 18563 } else if (*Res != MT) { 18564 Diag(Loc, diag::err_omp_declare_target_to_and_link) << ND; 18565 } 18566 } 18567 18568 static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR, 18569 Sema &SemaRef, Decl *D) { 18570 if (!D || !isa<VarDecl>(D)) 18571 return; 18572 auto *VD = cast<VarDecl>(D); 18573 Optional<OMPDeclareTargetDeclAttr::MapTypeTy> MapTy = 18574 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); 18575 if (SemaRef.LangOpts.OpenMP >= 50 && 18576 (SemaRef.getCurLambda(/*IgnoreNonLambdaCapturingScope=*/true) || 18577 SemaRef.getCurBlock() || SemaRef.getCurCapturedRegion()) && 18578 VD->hasGlobalStorage()) { 18579 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> MapTy = 18580 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); 18581 if (!MapTy || *MapTy != OMPDeclareTargetDeclAttr::MT_To) { 18582 // OpenMP 5.0, 2.12.7 declare target Directive, Restrictions 18583 // If a lambda declaration and definition appears between a 18584 // declare target directive and the matching end declare target 18585 // directive, all variables that are captured by the lambda 18586 // expression must also appear in a to clause. 18587 SemaRef.Diag(VD->getLocation(), 18588 diag::err_omp_lambda_capture_in_declare_target_not_to); 18589 SemaRef.Diag(SL, diag::note_var_explicitly_captured_here) 18590 << VD << 0 << SR; 18591 return; 18592 } 18593 } 18594 if (MapTy.hasValue()) 18595 return; 18596 SemaRef.Diag(VD->getLocation(), diag::warn_omp_not_in_target_context); 18597 SemaRef.Diag(SL, diag::note_used_here) << SR; 18598 } 18599 18600 static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR, 18601 Sema &SemaRef, DSAStackTy *Stack, 18602 ValueDecl *VD) { 18603 return OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD) || 18604 checkTypeMappable(SL, SR, SemaRef, Stack, VD->getType(), 18605 /*FullCheck=*/false); 18606 } 18607 18608 void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D, 18609 SourceLocation IdLoc) { 18610 if (!D || D->isInvalidDecl()) 18611 return; 18612 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange(); 18613 SourceLocation SL = E ? E->getBeginLoc() : D->getLocation(); 18614 if (auto *VD = dyn_cast<VarDecl>(D)) { 18615 // Only global variables can be marked as declare target. 18616 if (!VD->isFileVarDecl() && !VD->isStaticLocal() && 18617 !VD->isStaticDataMember()) 18618 return; 18619 // 2.10.6: threadprivate variable cannot appear in a declare target 18620 // directive. 18621 if (DSAStack->isThreadPrivate(VD)) { 18622 Diag(SL, diag::err_omp_threadprivate_in_target); 18623 reportOriginalDsa(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false)); 18624 return; 18625 } 18626 } 18627 if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(D)) 18628 D = FTD->getTemplatedDecl(); 18629 if (auto *FD = dyn_cast<FunctionDecl>(D)) { 18630 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 18631 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(FD); 18632 if (IdLoc.isValid() && Res && *Res == OMPDeclareTargetDeclAttr::MT_Link) { 18633 Diag(IdLoc, diag::err_omp_function_in_link_clause); 18634 Diag(FD->getLocation(), diag::note_defined_here) << FD; 18635 return; 18636 } 18637 } 18638 if (auto *VD = dyn_cast<ValueDecl>(D)) { 18639 // Problem if any with var declared with incomplete type will be reported 18640 // as normal, so no need to check it here. 18641 if ((E || !VD->getType()->isIncompleteType()) && 18642 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) 18643 return; 18644 if (!E && !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) { 18645 // Checking declaration inside declare target region. 18646 if (isa<VarDecl>(D) || isa<FunctionDecl>(D) || 18647 isa<FunctionTemplateDecl>(D)) { 18648 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit( 18649 Context, OMPDeclareTargetDeclAttr::MT_To, 18650 OMPDeclareTargetDeclAttr::DT_Any, SourceRange(IdLoc, IdLoc)); 18651 D->addAttr(A); 18652 if (ASTMutationListener *ML = Context.getASTMutationListener()) 18653 ML->DeclarationMarkedOpenMPDeclareTarget(D, A); 18654 } 18655 return; 18656 } 18657 } 18658 if (!E) 18659 return; 18660 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D); 18661 } 18662 18663 OMPClause *Sema::ActOnOpenMPToClause( 18664 ArrayRef<OpenMPMotionModifierKind> MotionModifiers, 18665 ArrayRef<SourceLocation> MotionModifiersLoc, 18666 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId, 18667 SourceLocation ColonLoc, ArrayRef<Expr *> VarList, 18668 const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) { 18669 OpenMPMotionModifierKind Modifiers[] = {OMPC_MOTION_MODIFIER_unknown, 18670 OMPC_MOTION_MODIFIER_unknown}; 18671 SourceLocation ModifiersLoc[NumberOfOMPMotionModifiers]; 18672 18673 // Process motion-modifiers, flag errors for duplicate modifiers. 18674 unsigned Count = 0; 18675 for (unsigned I = 0, E = MotionModifiers.size(); I < E; ++I) { 18676 if (MotionModifiers[I] != OMPC_MOTION_MODIFIER_unknown && 18677 llvm::find(Modifiers, MotionModifiers[I]) != std::end(Modifiers)) { 18678 Diag(MotionModifiersLoc[I], diag::err_omp_duplicate_motion_modifier); 18679 continue; 18680 } 18681 assert(Count < NumberOfOMPMotionModifiers && 18682 "Modifiers exceed the allowed number of motion modifiers"); 18683 Modifiers[Count] = MotionModifiers[I]; 18684 ModifiersLoc[Count] = MotionModifiersLoc[I]; 18685 ++Count; 18686 } 18687 18688 MappableVarListInfo MVLI(VarList); 18689 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, Locs.StartLoc, 18690 MapperIdScopeSpec, MapperId, UnresolvedMappers); 18691 if (MVLI.ProcessedVarList.empty()) 18692 return nullptr; 18693 18694 return OMPToClause::Create( 18695 Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, 18696 MVLI.VarComponents, MVLI.UDMapperList, Modifiers, ModifiersLoc, 18697 MapperIdScopeSpec.getWithLocInContext(Context), MapperId); 18698 } 18699 18700 OMPClause *Sema::ActOnOpenMPFromClause( 18701 ArrayRef<OpenMPMotionModifierKind> MotionModifiers, 18702 ArrayRef<SourceLocation> MotionModifiersLoc, 18703 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId, 18704 SourceLocation ColonLoc, ArrayRef<Expr *> VarList, 18705 const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) { 18706 OpenMPMotionModifierKind Modifiers[] = {OMPC_MOTION_MODIFIER_unknown, 18707 OMPC_MOTION_MODIFIER_unknown}; 18708 SourceLocation ModifiersLoc[NumberOfOMPMotionModifiers]; 18709 18710 // Process motion-modifiers, flag errors for duplicate modifiers. 18711 unsigned Count = 0; 18712 for (unsigned I = 0, E = MotionModifiers.size(); I < E; ++I) { 18713 if (MotionModifiers[I] != OMPC_MOTION_MODIFIER_unknown && 18714 llvm::find(Modifiers, MotionModifiers[I]) != std::end(Modifiers)) { 18715 Diag(MotionModifiersLoc[I], diag::err_omp_duplicate_motion_modifier); 18716 continue; 18717 } 18718 assert(Count < NumberOfOMPMotionModifiers && 18719 "Modifiers exceed the allowed number of motion modifiers"); 18720 Modifiers[Count] = MotionModifiers[I]; 18721 ModifiersLoc[Count] = MotionModifiersLoc[I]; 18722 ++Count; 18723 } 18724 18725 MappableVarListInfo MVLI(VarList); 18726 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, Locs.StartLoc, 18727 MapperIdScopeSpec, MapperId, UnresolvedMappers); 18728 if (MVLI.ProcessedVarList.empty()) 18729 return nullptr; 18730 18731 return OMPFromClause::Create( 18732 Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, 18733 MVLI.VarComponents, MVLI.UDMapperList, Modifiers, ModifiersLoc, 18734 MapperIdScopeSpec.getWithLocInContext(Context), MapperId); 18735 } 18736 18737 OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList, 18738 const OMPVarListLocTy &Locs) { 18739 MappableVarListInfo MVLI(VarList); 18740 SmallVector<Expr *, 8> PrivateCopies; 18741 SmallVector<Expr *, 8> Inits; 18742 18743 for (Expr *RefExpr : VarList) { 18744 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause."); 18745 SourceLocation ELoc; 18746 SourceRange ERange; 18747 Expr *SimpleRefExpr = RefExpr; 18748 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 18749 if (Res.second) { 18750 // It will be analyzed later. 18751 MVLI.ProcessedVarList.push_back(RefExpr); 18752 PrivateCopies.push_back(nullptr); 18753 Inits.push_back(nullptr); 18754 } 18755 ValueDecl *D = Res.first; 18756 if (!D) 18757 continue; 18758 18759 QualType Type = D->getType(); 18760 Type = Type.getNonReferenceType().getUnqualifiedType(); 18761 18762 auto *VD = dyn_cast<VarDecl>(D); 18763 18764 // Item should be a pointer or reference to pointer. 18765 if (!Type->isPointerType()) { 18766 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer) 18767 << 0 << RefExpr->getSourceRange(); 18768 continue; 18769 } 18770 18771 // Build the private variable and the expression that refers to it. 18772 auto VDPrivate = 18773 buildVarDecl(*this, ELoc, Type, D->getName(), 18774 D->hasAttrs() ? &D->getAttrs() : nullptr, 18775 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 18776 if (VDPrivate->isInvalidDecl()) 18777 continue; 18778 18779 CurContext->addDecl(VDPrivate); 18780 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 18781 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc); 18782 18783 // Add temporary variable to initialize the private copy of the pointer. 18784 VarDecl *VDInit = 18785 buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp"); 18786 DeclRefExpr *VDInitRefExpr = buildDeclRefExpr( 18787 *this, VDInit, RefExpr->getType(), RefExpr->getExprLoc()); 18788 AddInitializerToDecl(VDPrivate, 18789 DefaultLvalueConversion(VDInitRefExpr).get(), 18790 /*DirectInit=*/false); 18791 18792 // If required, build a capture to implement the privatization initialized 18793 // with the current list item value. 18794 DeclRefExpr *Ref = nullptr; 18795 if (!VD) 18796 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 18797 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref); 18798 PrivateCopies.push_back(VDPrivateRefExpr); 18799 Inits.push_back(VDInitRefExpr); 18800 18801 // We need to add a data sharing attribute for this variable to make sure it 18802 // is correctly captured. A variable that shows up in a use_device_ptr has 18803 // similar properties of a first private variable. 18804 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 18805 18806 // Create a mappable component for the list item. List items in this clause 18807 // only need a component. 18808 MVLI.VarBaseDeclarations.push_back(D); 18809 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 18810 MVLI.VarComponents.back().push_back( 18811 OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D)); 18812 } 18813 18814 if (MVLI.ProcessedVarList.empty()) 18815 return nullptr; 18816 18817 return OMPUseDevicePtrClause::Create( 18818 Context, Locs, MVLI.ProcessedVarList, PrivateCopies, Inits, 18819 MVLI.VarBaseDeclarations, MVLI.VarComponents); 18820 } 18821 18822 OMPClause *Sema::ActOnOpenMPUseDeviceAddrClause(ArrayRef<Expr *> VarList, 18823 const OMPVarListLocTy &Locs) { 18824 MappableVarListInfo MVLI(VarList); 18825 18826 for (Expr *RefExpr : VarList) { 18827 assert(RefExpr && "NULL expr in OpenMP use_device_addr clause."); 18828 SourceLocation ELoc; 18829 SourceRange ERange; 18830 Expr *SimpleRefExpr = RefExpr; 18831 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, 18832 /*AllowArraySection=*/true); 18833 if (Res.second) { 18834 // It will be analyzed later. 18835 MVLI.ProcessedVarList.push_back(RefExpr); 18836 } 18837 ValueDecl *D = Res.first; 18838 if (!D) 18839 continue; 18840 auto *VD = dyn_cast<VarDecl>(D); 18841 18842 // If required, build a capture to implement the privatization initialized 18843 // with the current list item value. 18844 DeclRefExpr *Ref = nullptr; 18845 if (!VD) 18846 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 18847 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref); 18848 18849 // We need to add a data sharing attribute for this variable to make sure it 18850 // is correctly captured. A variable that shows up in a use_device_addr has 18851 // similar properties of a first private variable. 18852 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 18853 18854 // Create a mappable component for the list item. List items in this clause 18855 // only need a component. 18856 MVLI.VarBaseDeclarations.push_back(D); 18857 MVLI.VarComponents.emplace_back(); 18858 Expr *Component = SimpleRefExpr; 18859 if (VD && (isa<OMPArraySectionExpr>(RefExpr->IgnoreParenImpCasts()) || 18860 isa<ArraySubscriptExpr>(RefExpr->IgnoreParenImpCasts()))) 18861 Component = DefaultFunctionArrayLvalueConversion(SimpleRefExpr).get(); 18862 MVLI.VarComponents.back().push_back( 18863 OMPClauseMappableExprCommon::MappableComponent(Component, D)); 18864 } 18865 18866 if (MVLI.ProcessedVarList.empty()) 18867 return nullptr; 18868 18869 return OMPUseDeviceAddrClause::Create(Context, Locs, MVLI.ProcessedVarList, 18870 MVLI.VarBaseDeclarations, 18871 MVLI.VarComponents); 18872 } 18873 18874 OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList, 18875 const OMPVarListLocTy &Locs) { 18876 MappableVarListInfo MVLI(VarList); 18877 for (Expr *RefExpr : VarList) { 18878 assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause."); 18879 SourceLocation ELoc; 18880 SourceRange ERange; 18881 Expr *SimpleRefExpr = RefExpr; 18882 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 18883 if (Res.second) { 18884 // It will be analyzed later. 18885 MVLI.ProcessedVarList.push_back(RefExpr); 18886 } 18887 ValueDecl *D = Res.first; 18888 if (!D) 18889 continue; 18890 18891 QualType Type = D->getType(); 18892 // item should be a pointer or array or reference to pointer or array 18893 if (!Type.getNonReferenceType()->isPointerType() && 18894 !Type.getNonReferenceType()->isArrayType()) { 18895 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr) 18896 << 0 << RefExpr->getSourceRange(); 18897 continue; 18898 } 18899 18900 // Check if the declaration in the clause does not show up in any data 18901 // sharing attribute. 18902 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 18903 if (isOpenMPPrivate(DVar.CKind)) { 18904 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 18905 << getOpenMPClauseName(DVar.CKind) 18906 << getOpenMPClauseName(OMPC_is_device_ptr) 18907 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 18908 reportOriginalDsa(*this, DSAStack, D, DVar); 18909 continue; 18910 } 18911 18912 const Expr *ConflictExpr; 18913 if (DSAStack->checkMappableExprComponentListsForDecl( 18914 D, /*CurrentRegionOnly=*/true, 18915 [&ConflictExpr]( 18916 OMPClauseMappableExprCommon::MappableExprComponentListRef R, 18917 OpenMPClauseKind) -> bool { 18918 ConflictExpr = R.front().getAssociatedExpression(); 18919 return true; 18920 })) { 18921 Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange(); 18922 Diag(ConflictExpr->getExprLoc(), diag::note_used_here) 18923 << ConflictExpr->getSourceRange(); 18924 continue; 18925 } 18926 18927 // Store the components in the stack so that they can be used to check 18928 // against other clauses later on. 18929 OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D); 18930 DSAStack->addMappableExpressionComponents( 18931 D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr); 18932 18933 // Record the expression we've just processed. 18934 MVLI.ProcessedVarList.push_back(SimpleRefExpr); 18935 18936 // Create a mappable component for the list item. List items in this clause 18937 // only need a component. We use a null declaration to signal fields in 18938 // 'this'. 18939 assert((isa<DeclRefExpr>(SimpleRefExpr) || 18940 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) && 18941 "Unexpected device pointer expression!"); 18942 MVLI.VarBaseDeclarations.push_back( 18943 isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr); 18944 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 18945 MVLI.VarComponents.back().push_back(MC); 18946 } 18947 18948 if (MVLI.ProcessedVarList.empty()) 18949 return nullptr; 18950 18951 return OMPIsDevicePtrClause::Create(Context, Locs, MVLI.ProcessedVarList, 18952 MVLI.VarBaseDeclarations, 18953 MVLI.VarComponents); 18954 } 18955 18956 OMPClause *Sema::ActOnOpenMPAllocateClause( 18957 Expr *Allocator, ArrayRef<Expr *> VarList, SourceLocation StartLoc, 18958 SourceLocation ColonLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 18959 if (Allocator) { 18960 // OpenMP [2.11.4 allocate Clause, Description] 18961 // allocator is an expression of omp_allocator_handle_t type. 18962 if (!findOMPAllocatorHandleT(*this, Allocator->getExprLoc(), DSAStack)) 18963 return nullptr; 18964 18965 ExprResult AllocatorRes = DefaultLvalueConversion(Allocator); 18966 if (AllocatorRes.isInvalid()) 18967 return nullptr; 18968 AllocatorRes = PerformImplicitConversion(AllocatorRes.get(), 18969 DSAStack->getOMPAllocatorHandleT(), 18970 Sema::AA_Initializing, 18971 /*AllowExplicit=*/true); 18972 if (AllocatorRes.isInvalid()) 18973 return nullptr; 18974 Allocator = AllocatorRes.get(); 18975 } else { 18976 // OpenMP 5.0, 2.11.4 allocate Clause, Restrictions. 18977 // allocate clauses that appear on a target construct or on constructs in a 18978 // target region must specify an allocator expression unless a requires 18979 // directive with the dynamic_allocators clause is present in the same 18980 // compilation unit. 18981 if (LangOpts.OpenMPIsDevice && 18982 !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>()) 18983 targetDiag(StartLoc, diag::err_expected_allocator_expression); 18984 } 18985 // Analyze and build list of variables. 18986 SmallVector<Expr *, 8> Vars; 18987 for (Expr *RefExpr : VarList) { 18988 assert(RefExpr && "NULL expr in OpenMP private clause."); 18989 SourceLocation ELoc; 18990 SourceRange ERange; 18991 Expr *SimpleRefExpr = RefExpr; 18992 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 18993 if (Res.second) { 18994 // It will be analyzed later. 18995 Vars.push_back(RefExpr); 18996 } 18997 ValueDecl *D = Res.first; 18998 if (!D) 18999 continue; 19000 19001 auto *VD = dyn_cast<VarDecl>(D); 19002 DeclRefExpr *Ref = nullptr; 19003 if (!VD && !CurContext->isDependentContext()) 19004 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 19005 Vars.push_back((VD || CurContext->isDependentContext()) 19006 ? RefExpr->IgnoreParens() 19007 : Ref); 19008 } 19009 19010 if (Vars.empty()) 19011 return nullptr; 19012 19013 if (Allocator) 19014 DSAStack->addInnerAllocatorExpr(Allocator); 19015 return OMPAllocateClause::Create(Context, StartLoc, LParenLoc, Allocator, 19016 ColonLoc, EndLoc, Vars); 19017 } 19018 19019 OMPClause *Sema::ActOnOpenMPNontemporalClause(ArrayRef<Expr *> VarList, 19020 SourceLocation StartLoc, 19021 SourceLocation LParenLoc, 19022 SourceLocation EndLoc) { 19023 SmallVector<Expr *, 8> Vars; 19024 for (Expr *RefExpr : VarList) { 19025 assert(RefExpr && "NULL expr in OpenMP nontemporal clause."); 19026 SourceLocation ELoc; 19027 SourceRange ERange; 19028 Expr *SimpleRefExpr = RefExpr; 19029 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 19030 if (Res.second) 19031 // It will be analyzed later. 19032 Vars.push_back(RefExpr); 19033 ValueDecl *D = Res.first; 19034 if (!D) 19035 continue; 19036 19037 // OpenMP 5.0, 2.9.3.1 simd Construct, Restrictions. 19038 // A list-item cannot appear in more than one nontemporal clause. 19039 if (const Expr *PrevRef = 19040 DSAStack->addUniqueNontemporal(D, SimpleRefExpr)) { 19041 Diag(ELoc, diag::err_omp_used_in_clause_twice) 19042 << 0 << getOpenMPClauseName(OMPC_nontemporal) << ERange; 19043 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa) 19044 << getOpenMPClauseName(OMPC_nontemporal); 19045 continue; 19046 } 19047 19048 Vars.push_back(RefExpr); 19049 } 19050 19051 if (Vars.empty()) 19052 return nullptr; 19053 19054 return OMPNontemporalClause::Create(Context, StartLoc, LParenLoc, EndLoc, 19055 Vars); 19056 } 19057 19058 OMPClause *Sema::ActOnOpenMPInclusiveClause(ArrayRef<Expr *> VarList, 19059 SourceLocation StartLoc, 19060 SourceLocation LParenLoc, 19061 SourceLocation EndLoc) { 19062 SmallVector<Expr *, 8> Vars; 19063 for (Expr *RefExpr : VarList) { 19064 assert(RefExpr && "NULL expr in OpenMP nontemporal clause."); 19065 SourceLocation ELoc; 19066 SourceRange ERange; 19067 Expr *SimpleRefExpr = RefExpr; 19068 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, 19069 /*AllowArraySection=*/true); 19070 if (Res.second) 19071 // It will be analyzed later. 19072 Vars.push_back(RefExpr); 19073 ValueDecl *D = Res.first; 19074 if (!D) 19075 continue; 19076 19077 const DSAStackTy::DSAVarData DVar = 19078 DSAStack->getTopDSA(D, /*FromParent=*/true); 19079 // OpenMP 5.0, 2.9.6, scan Directive, Restrictions. 19080 // A list item that appears in the inclusive or exclusive clause must appear 19081 // in a reduction clause with the inscan modifier on the enclosing 19082 // worksharing-loop, worksharing-loop SIMD, or simd construct. 19083 if (DVar.CKind != OMPC_reduction || 19084 DVar.Modifier != OMPC_REDUCTION_inscan) 19085 Diag(ELoc, diag::err_omp_inclusive_exclusive_not_reduction) 19086 << RefExpr->getSourceRange(); 19087 19088 if (DSAStack->getParentDirective() != OMPD_unknown) 19089 DSAStack->markDeclAsUsedInScanDirective(D); 19090 Vars.push_back(RefExpr); 19091 } 19092 19093 if (Vars.empty()) 19094 return nullptr; 19095 19096 return OMPInclusiveClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 19097 } 19098 19099 OMPClause *Sema::ActOnOpenMPExclusiveClause(ArrayRef<Expr *> VarList, 19100 SourceLocation StartLoc, 19101 SourceLocation LParenLoc, 19102 SourceLocation EndLoc) { 19103 SmallVector<Expr *, 8> Vars; 19104 for (Expr *RefExpr : VarList) { 19105 assert(RefExpr && "NULL expr in OpenMP nontemporal clause."); 19106 SourceLocation ELoc; 19107 SourceRange ERange; 19108 Expr *SimpleRefExpr = RefExpr; 19109 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, 19110 /*AllowArraySection=*/true); 19111 if (Res.second) 19112 // It will be analyzed later. 19113 Vars.push_back(RefExpr); 19114 ValueDecl *D = Res.first; 19115 if (!D) 19116 continue; 19117 19118 OpenMPDirectiveKind ParentDirective = DSAStack->getParentDirective(); 19119 DSAStackTy::DSAVarData DVar; 19120 if (ParentDirective != OMPD_unknown) 19121 DVar = DSAStack->getTopDSA(D, /*FromParent=*/true); 19122 // OpenMP 5.0, 2.9.6, scan Directive, Restrictions. 19123 // A list item that appears in the inclusive or exclusive clause must appear 19124 // in a reduction clause with the inscan modifier on the enclosing 19125 // worksharing-loop, worksharing-loop SIMD, or simd construct. 19126 if (ParentDirective == OMPD_unknown || DVar.CKind != OMPC_reduction || 19127 DVar.Modifier != OMPC_REDUCTION_inscan) { 19128 Diag(ELoc, diag::err_omp_inclusive_exclusive_not_reduction) 19129 << RefExpr->getSourceRange(); 19130 } else { 19131 DSAStack->markDeclAsUsedInScanDirective(D); 19132 } 19133 Vars.push_back(RefExpr); 19134 } 19135 19136 if (Vars.empty()) 19137 return nullptr; 19138 19139 return OMPExclusiveClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 19140 } 19141 19142 /// Tries to find omp_alloctrait_t type. 19143 static bool findOMPAlloctraitT(Sema &S, SourceLocation Loc, DSAStackTy *Stack) { 19144 QualType OMPAlloctraitT = Stack->getOMPAlloctraitT(); 19145 if (!OMPAlloctraitT.isNull()) 19146 return true; 19147 IdentifierInfo &II = S.PP.getIdentifierTable().get("omp_alloctrait_t"); 19148 ParsedType PT = S.getTypeName(II, Loc, S.getCurScope()); 19149 if (!PT.getAsOpaquePtr() || PT.get().isNull()) { 19150 S.Diag(Loc, diag::err_omp_implied_type_not_found) << "omp_alloctrait_t"; 19151 return false; 19152 } 19153 Stack->setOMPAlloctraitT(PT.get()); 19154 return true; 19155 } 19156 19157 OMPClause *Sema::ActOnOpenMPUsesAllocatorClause( 19158 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc, 19159 ArrayRef<UsesAllocatorsData> Data) { 19160 // OpenMP [2.12.5, target Construct] 19161 // allocator is an identifier of omp_allocator_handle_t type. 19162 if (!findOMPAllocatorHandleT(*this, StartLoc, DSAStack)) 19163 return nullptr; 19164 // OpenMP [2.12.5, target Construct] 19165 // allocator-traits-array is an identifier of const omp_alloctrait_t * type. 19166 if (llvm::any_of( 19167 Data, 19168 [](const UsesAllocatorsData &D) { return D.AllocatorTraits; }) && 19169 !findOMPAlloctraitT(*this, StartLoc, DSAStack)) 19170 return nullptr; 19171 llvm::SmallPtrSet<CanonicalDeclPtr<Decl>, 4> PredefinedAllocators; 19172 for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) { 19173 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I); 19174 StringRef Allocator = 19175 OMPAllocateDeclAttr::ConvertAllocatorTypeTyToStr(AllocatorKind); 19176 DeclarationName AllocatorName = &Context.Idents.get(Allocator); 19177 PredefinedAllocators.insert(LookupSingleName( 19178 TUScope, AllocatorName, StartLoc, Sema::LookupAnyName)); 19179 } 19180 19181 SmallVector<OMPUsesAllocatorsClause::Data, 4> NewData; 19182 for (const UsesAllocatorsData &D : Data) { 19183 Expr *AllocatorExpr = nullptr; 19184 // Check allocator expression. 19185 if (D.Allocator->isTypeDependent()) { 19186 AllocatorExpr = D.Allocator; 19187 } else { 19188 // Traits were specified - need to assign new allocator to the specified 19189 // allocator, so it must be an lvalue. 19190 AllocatorExpr = D.Allocator->IgnoreParenImpCasts(); 19191 auto *DRE = dyn_cast<DeclRefExpr>(AllocatorExpr); 19192 bool IsPredefinedAllocator = false; 19193 if (DRE) 19194 IsPredefinedAllocator = PredefinedAllocators.count(DRE->getDecl()); 19195 if (!DRE || 19196 !(Context.hasSameUnqualifiedType( 19197 AllocatorExpr->getType(), DSAStack->getOMPAllocatorHandleT()) || 19198 Context.typesAreCompatible(AllocatorExpr->getType(), 19199 DSAStack->getOMPAllocatorHandleT(), 19200 /*CompareUnqualified=*/true)) || 19201 (!IsPredefinedAllocator && 19202 (AllocatorExpr->getType().isConstant(Context) || 19203 !AllocatorExpr->isLValue()))) { 19204 Diag(D.Allocator->getExprLoc(), diag::err_omp_var_expected) 19205 << "omp_allocator_handle_t" << (DRE ? 1 : 0) 19206 << AllocatorExpr->getType() << D.Allocator->getSourceRange(); 19207 continue; 19208 } 19209 // OpenMP [2.12.5, target Construct] 19210 // Predefined allocators appearing in a uses_allocators clause cannot have 19211 // traits specified. 19212 if (IsPredefinedAllocator && D.AllocatorTraits) { 19213 Diag(D.AllocatorTraits->getExprLoc(), 19214 diag::err_omp_predefined_allocator_with_traits) 19215 << D.AllocatorTraits->getSourceRange(); 19216 Diag(D.Allocator->getExprLoc(), diag::note_omp_predefined_allocator) 19217 << cast<NamedDecl>(DRE->getDecl())->getName() 19218 << D.Allocator->getSourceRange(); 19219 continue; 19220 } 19221 // OpenMP [2.12.5, target Construct] 19222 // Non-predefined allocators appearing in a uses_allocators clause must 19223 // have traits specified. 19224 if (!IsPredefinedAllocator && !D.AllocatorTraits) { 19225 Diag(D.Allocator->getExprLoc(), 19226 diag::err_omp_nonpredefined_allocator_without_traits); 19227 continue; 19228 } 19229 // No allocator traits - just convert it to rvalue. 19230 if (!D.AllocatorTraits) 19231 AllocatorExpr = DefaultLvalueConversion(AllocatorExpr).get(); 19232 DSAStack->addUsesAllocatorsDecl( 19233 DRE->getDecl(), 19234 IsPredefinedAllocator 19235 ? DSAStackTy::UsesAllocatorsDeclKind::PredefinedAllocator 19236 : DSAStackTy::UsesAllocatorsDeclKind::UserDefinedAllocator); 19237 } 19238 Expr *AllocatorTraitsExpr = nullptr; 19239 if (D.AllocatorTraits) { 19240 if (D.AllocatorTraits->isTypeDependent()) { 19241 AllocatorTraitsExpr = D.AllocatorTraits; 19242 } else { 19243 // OpenMP [2.12.5, target Construct] 19244 // Arrays that contain allocator traits that appear in a uses_allocators 19245 // clause must be constant arrays, have constant values and be defined 19246 // in the same scope as the construct in which the clause appears. 19247 AllocatorTraitsExpr = D.AllocatorTraits->IgnoreParenImpCasts(); 19248 // Check that traits expr is a constant array. 19249 QualType TraitTy; 19250 if (const ArrayType *Ty = 19251 AllocatorTraitsExpr->getType()->getAsArrayTypeUnsafe()) 19252 if (const auto *ConstArrayTy = dyn_cast<ConstantArrayType>(Ty)) 19253 TraitTy = ConstArrayTy->getElementType(); 19254 if (TraitTy.isNull() || 19255 !(Context.hasSameUnqualifiedType(TraitTy, 19256 DSAStack->getOMPAlloctraitT()) || 19257 Context.typesAreCompatible(TraitTy, DSAStack->getOMPAlloctraitT(), 19258 /*CompareUnqualified=*/true))) { 19259 Diag(D.AllocatorTraits->getExprLoc(), 19260 diag::err_omp_expected_array_alloctraits) 19261 << AllocatorTraitsExpr->getType(); 19262 continue; 19263 } 19264 // Do not map by default allocator traits if it is a standalone 19265 // variable. 19266 if (auto *DRE = dyn_cast<DeclRefExpr>(AllocatorTraitsExpr)) 19267 DSAStack->addUsesAllocatorsDecl( 19268 DRE->getDecl(), 19269 DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait); 19270 } 19271 } 19272 OMPUsesAllocatorsClause::Data &NewD = NewData.emplace_back(); 19273 NewD.Allocator = AllocatorExpr; 19274 NewD.AllocatorTraits = AllocatorTraitsExpr; 19275 NewD.LParenLoc = D.LParenLoc; 19276 NewD.RParenLoc = D.RParenLoc; 19277 } 19278 return OMPUsesAllocatorsClause::Create(Context, StartLoc, LParenLoc, EndLoc, 19279 NewData); 19280 } 19281 19282 OMPClause *Sema::ActOnOpenMPAffinityClause( 19283 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc, 19284 SourceLocation EndLoc, Expr *Modifier, ArrayRef<Expr *> Locators) { 19285 SmallVector<Expr *, 8> Vars; 19286 for (Expr *RefExpr : Locators) { 19287 assert(RefExpr && "NULL expr in OpenMP shared clause."); 19288 if (isa<DependentScopeDeclRefExpr>(RefExpr) || RefExpr->isTypeDependent()) { 19289 // It will be analyzed later. 19290 Vars.push_back(RefExpr); 19291 continue; 19292 } 19293 19294 SourceLocation ELoc = RefExpr->getExprLoc(); 19295 Expr *SimpleExpr = RefExpr->IgnoreParenImpCasts(); 19296 19297 if (!SimpleExpr->isLValue()) { 19298 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 19299 << 1 << 0 << RefExpr->getSourceRange(); 19300 continue; 19301 } 19302 19303 ExprResult Res; 19304 { 19305 Sema::TentativeAnalysisScope Trap(*this); 19306 Res = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, SimpleExpr); 19307 } 19308 if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr) && 19309 !isa<OMPArrayShapingExpr>(SimpleExpr)) { 19310 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 19311 << 1 << 0 << RefExpr->getSourceRange(); 19312 continue; 19313 } 19314 Vars.push_back(SimpleExpr); 19315 } 19316 19317 return OMPAffinityClause::Create(Context, StartLoc, LParenLoc, ColonLoc, 19318 EndLoc, Modifier, Vars); 19319 } 19320