1 //===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 /// \file 9 /// This file implements semantic analysis for OpenMP directives and 10 /// clauses. 11 /// 12 //===----------------------------------------------------------------------===// 13 14 #include "TreeTransform.h" 15 #include "clang/AST/ASTContext.h" 16 #include "clang/AST/ASTMutationListener.h" 17 #include "clang/AST/CXXInheritance.h" 18 #include "clang/AST/Decl.h" 19 #include "clang/AST/DeclCXX.h" 20 #include "clang/AST/DeclOpenMP.h" 21 #include "clang/AST/OpenMPClause.h" 22 #include "clang/AST/StmtCXX.h" 23 #include "clang/AST/StmtOpenMP.h" 24 #include "clang/AST/StmtVisitor.h" 25 #include "clang/AST/TypeOrdering.h" 26 #include "clang/Basic/DiagnosticSema.h" 27 #include "clang/Basic/OpenMPKinds.h" 28 #include "clang/Basic/PartialDiagnostic.h" 29 #include "clang/Basic/TargetInfo.h" 30 #include "clang/Sema/Initialization.h" 31 #include "clang/Sema/Lookup.h" 32 #include "clang/Sema/Scope.h" 33 #include "clang/Sema/ScopeInfo.h" 34 #include "clang/Sema/SemaInternal.h" 35 #include "llvm/ADT/IndexedMap.h" 36 #include "llvm/ADT/PointerEmbeddedInt.h" 37 #include "llvm/ADT/STLExtras.h" 38 #include "llvm/Frontend/OpenMP/OMPConstants.h" 39 #include <set> 40 41 using namespace clang; 42 using namespace llvm::omp; 43 44 //===----------------------------------------------------------------------===// 45 // Stack of data-sharing attributes for variables 46 //===----------------------------------------------------------------------===// 47 48 static const Expr *checkMapClauseExpressionBase( 49 Sema &SemaRef, Expr *E, 50 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents, 51 OpenMPClauseKind CKind, bool NoDiagnose); 52 53 namespace { 54 /// Default data sharing attributes, which can be applied to directive. 55 enum DefaultDataSharingAttributes { 56 DSA_unspecified = 0, /// Data sharing attribute not specified. 57 DSA_none = 1 << 0, /// Default data sharing attribute 'none'. 58 DSA_shared = 1 << 1, /// Default data sharing attribute 'shared'. 59 DSA_firstprivate = 1 << 2, /// Default data sharing attribute 'firstprivate'. 60 }; 61 62 /// Stack for tracking declarations used in OpenMP directives and 63 /// clauses and their data-sharing attributes. 64 class DSAStackTy { 65 public: 66 struct DSAVarData { 67 OpenMPDirectiveKind DKind = OMPD_unknown; 68 OpenMPClauseKind CKind = OMPC_unknown; 69 unsigned Modifier = 0; 70 const Expr *RefExpr = nullptr; 71 DeclRefExpr *PrivateCopy = nullptr; 72 SourceLocation ImplicitDSALoc; 73 DSAVarData() = default; 74 DSAVarData(OpenMPDirectiveKind DKind, OpenMPClauseKind CKind, 75 const Expr *RefExpr, DeclRefExpr *PrivateCopy, 76 SourceLocation ImplicitDSALoc, unsigned Modifier) 77 : DKind(DKind), CKind(CKind), Modifier(Modifier), RefExpr(RefExpr), 78 PrivateCopy(PrivateCopy), ImplicitDSALoc(ImplicitDSALoc) {} 79 }; 80 using OperatorOffsetTy = 81 llvm::SmallVector<std::pair<Expr *, OverloadedOperatorKind>, 4>; 82 using DoacrossDependMapTy = 83 llvm::DenseMap<OMPDependClause *, OperatorOffsetTy>; 84 /// Kind of the declaration used in the uses_allocators clauses. 85 enum class UsesAllocatorsDeclKind { 86 /// Predefined allocator 87 PredefinedAllocator, 88 /// User-defined allocator 89 UserDefinedAllocator, 90 /// The declaration that represent allocator trait 91 AllocatorTrait, 92 }; 93 94 private: 95 struct DSAInfo { 96 OpenMPClauseKind Attributes = OMPC_unknown; 97 unsigned Modifier = 0; 98 /// Pointer to a reference expression and a flag which shows that the 99 /// variable is marked as lastprivate(true) or not (false). 100 llvm::PointerIntPair<const Expr *, 1, bool> RefExpr; 101 DeclRefExpr *PrivateCopy = nullptr; 102 }; 103 using DeclSAMapTy = llvm::SmallDenseMap<const ValueDecl *, DSAInfo, 8>; 104 using UsedRefMapTy = llvm::SmallDenseMap<const ValueDecl *, const Expr *, 8>; 105 using LCDeclInfo = std::pair<unsigned, VarDecl *>; 106 using LoopControlVariablesMapTy = 107 llvm::SmallDenseMap<const ValueDecl *, LCDeclInfo, 8>; 108 /// Struct that associates a component with the clause kind where they are 109 /// found. 110 struct MappedExprComponentTy { 111 OMPClauseMappableExprCommon::MappableExprComponentLists Components; 112 OpenMPClauseKind Kind = OMPC_unknown; 113 }; 114 using MappedExprComponentsTy = 115 llvm::DenseMap<const ValueDecl *, MappedExprComponentTy>; 116 using CriticalsWithHintsTy = 117 llvm::StringMap<std::pair<const OMPCriticalDirective *, llvm::APSInt>>; 118 struct ReductionData { 119 using BOKPtrType = llvm::PointerEmbeddedInt<BinaryOperatorKind, 16>; 120 SourceRange ReductionRange; 121 llvm::PointerUnion<const Expr *, BOKPtrType> ReductionOp; 122 ReductionData() = default; 123 void set(BinaryOperatorKind BO, SourceRange RR) { 124 ReductionRange = RR; 125 ReductionOp = BO; 126 } 127 void set(const Expr *RefExpr, SourceRange RR) { 128 ReductionRange = RR; 129 ReductionOp = RefExpr; 130 } 131 }; 132 using DeclReductionMapTy = 133 llvm::SmallDenseMap<const ValueDecl *, ReductionData, 4>; 134 struct DefaultmapInfo { 135 OpenMPDefaultmapClauseModifier ImplicitBehavior = 136 OMPC_DEFAULTMAP_MODIFIER_unknown; 137 SourceLocation SLoc; 138 DefaultmapInfo() = default; 139 DefaultmapInfo(OpenMPDefaultmapClauseModifier M, SourceLocation Loc) 140 : ImplicitBehavior(M), SLoc(Loc) {} 141 }; 142 143 struct SharingMapTy { 144 DeclSAMapTy SharingMap; 145 DeclReductionMapTy ReductionMap; 146 UsedRefMapTy AlignedMap; 147 UsedRefMapTy NontemporalMap; 148 MappedExprComponentsTy MappedExprComponents; 149 LoopControlVariablesMapTy LCVMap; 150 DefaultDataSharingAttributes DefaultAttr = DSA_unspecified; 151 SourceLocation DefaultAttrLoc; 152 DefaultmapInfo DefaultmapMap[OMPC_DEFAULTMAP_unknown]; 153 OpenMPDirectiveKind Directive = OMPD_unknown; 154 DeclarationNameInfo DirectiveName; 155 Scope *CurScope = nullptr; 156 SourceLocation ConstructLoc; 157 /// Set of 'depend' clauses with 'sink|source' dependence kind. Required to 158 /// get the data (loop counters etc.) about enclosing loop-based construct. 159 /// This data is required during codegen. 160 DoacrossDependMapTy DoacrossDepends; 161 /// First argument (Expr *) contains optional argument of the 162 /// 'ordered' clause, the second one is true if the regions has 'ordered' 163 /// clause, false otherwise. 164 llvm::Optional<std::pair<const Expr *, OMPOrderedClause *>> OrderedRegion; 165 unsigned AssociatedLoops = 1; 166 bool HasMutipleLoops = false; 167 const Decl *PossiblyLoopCounter = nullptr; 168 bool NowaitRegion = false; 169 bool CancelRegion = false; 170 bool LoopStart = false; 171 bool BodyComplete = false; 172 SourceLocation PrevScanLocation; 173 SourceLocation PrevOrderedLocation; 174 SourceLocation InnerTeamsRegionLoc; 175 /// Reference to the taskgroup task_reduction reference expression. 176 Expr *TaskgroupReductionRef = nullptr; 177 llvm::DenseSet<QualType> MappedClassesQualTypes; 178 SmallVector<Expr *, 4> InnerUsedAllocators; 179 llvm::DenseSet<CanonicalDeclPtr<Decl>> ImplicitTaskFirstprivates; 180 /// List of globals marked as declare target link in this target region 181 /// (isOpenMPTargetExecutionDirective(Directive) == true). 182 llvm::SmallVector<DeclRefExpr *, 4> DeclareTargetLinkVarDecls; 183 /// List of decls used in inclusive/exclusive clauses of the scan directive. 184 llvm::DenseSet<CanonicalDeclPtr<Decl>> UsedInScanDirective; 185 llvm::DenseMap<CanonicalDeclPtr<const Decl>, UsesAllocatorsDeclKind> 186 UsesAllocatorsDecls; 187 Expr *DeclareMapperVar = nullptr; 188 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name, 189 Scope *CurScope, SourceLocation Loc) 190 : Directive(DKind), DirectiveName(Name), CurScope(CurScope), 191 ConstructLoc(Loc) {} 192 SharingMapTy() = default; 193 }; 194 195 using StackTy = SmallVector<SharingMapTy, 4>; 196 197 /// Stack of used declaration and their data-sharing attributes. 198 DeclSAMapTy Threadprivates; 199 const FunctionScopeInfo *CurrentNonCapturingFunctionScope = nullptr; 200 SmallVector<std::pair<StackTy, const FunctionScopeInfo *>, 4> Stack; 201 /// true, if check for DSA must be from parent directive, false, if 202 /// from current directive. 203 OpenMPClauseKind ClauseKindMode = OMPC_unknown; 204 Sema &SemaRef; 205 bool ForceCapturing = false; 206 /// true if all the variables in the target executable directives must be 207 /// captured by reference. 208 bool ForceCaptureByReferenceInTargetExecutable = false; 209 CriticalsWithHintsTy Criticals; 210 unsigned IgnoredStackElements = 0; 211 212 /// Iterators over the stack iterate in order from innermost to outermost 213 /// directive. 214 using const_iterator = StackTy::const_reverse_iterator; 215 const_iterator begin() const { 216 return Stack.empty() ? const_iterator() 217 : Stack.back().first.rbegin() + IgnoredStackElements; 218 } 219 const_iterator end() const { 220 return Stack.empty() ? const_iterator() : Stack.back().first.rend(); 221 } 222 using iterator = StackTy::reverse_iterator; 223 iterator begin() { 224 return Stack.empty() ? iterator() 225 : Stack.back().first.rbegin() + IgnoredStackElements; 226 } 227 iterator end() { 228 return Stack.empty() ? iterator() : Stack.back().first.rend(); 229 } 230 231 // Convenience operations to get at the elements of the stack. 232 233 bool isStackEmpty() const { 234 return Stack.empty() || 235 Stack.back().second != CurrentNonCapturingFunctionScope || 236 Stack.back().first.size() <= IgnoredStackElements; 237 } 238 size_t getStackSize() const { 239 return isStackEmpty() ? 0 240 : Stack.back().first.size() - IgnoredStackElements; 241 } 242 243 SharingMapTy *getTopOfStackOrNull() { 244 size_t Size = getStackSize(); 245 if (Size == 0) 246 return nullptr; 247 return &Stack.back().first[Size - 1]; 248 } 249 const SharingMapTy *getTopOfStackOrNull() const { 250 return const_cast<DSAStackTy&>(*this).getTopOfStackOrNull(); 251 } 252 SharingMapTy &getTopOfStack() { 253 assert(!isStackEmpty() && "no current directive"); 254 return *getTopOfStackOrNull(); 255 } 256 const SharingMapTy &getTopOfStack() const { 257 return const_cast<DSAStackTy&>(*this).getTopOfStack(); 258 } 259 260 SharingMapTy *getSecondOnStackOrNull() { 261 size_t Size = getStackSize(); 262 if (Size <= 1) 263 return nullptr; 264 return &Stack.back().first[Size - 2]; 265 } 266 const SharingMapTy *getSecondOnStackOrNull() const { 267 return const_cast<DSAStackTy&>(*this).getSecondOnStackOrNull(); 268 } 269 270 /// Get the stack element at a certain level (previously returned by 271 /// \c getNestingLevel). 272 /// 273 /// Note that nesting levels count from outermost to innermost, and this is 274 /// the reverse of our iteration order where new inner levels are pushed at 275 /// the front of the stack. 276 SharingMapTy &getStackElemAtLevel(unsigned Level) { 277 assert(Level < getStackSize() && "no such stack element"); 278 return Stack.back().first[Level]; 279 } 280 const SharingMapTy &getStackElemAtLevel(unsigned Level) const { 281 return const_cast<DSAStackTy&>(*this).getStackElemAtLevel(Level); 282 } 283 284 DSAVarData getDSA(const_iterator &Iter, ValueDecl *D) const; 285 286 /// Checks if the variable is a local for OpenMP region. 287 bool isOpenMPLocal(VarDecl *D, const_iterator Iter) const; 288 289 /// Vector of previously declared requires directives 290 SmallVector<const OMPRequiresDecl *, 2> RequiresDecls; 291 /// omp_allocator_handle_t type. 292 QualType OMPAllocatorHandleT; 293 /// omp_depend_t type. 294 QualType OMPDependT; 295 /// omp_event_handle_t type. 296 QualType OMPEventHandleT; 297 /// omp_alloctrait_t type. 298 QualType OMPAlloctraitT; 299 /// Expression for the predefined allocators. 300 Expr *OMPPredefinedAllocators[OMPAllocateDeclAttr::OMPUserDefinedMemAlloc] = { 301 nullptr}; 302 /// Vector of previously encountered target directives 303 SmallVector<SourceLocation, 2> TargetLocations; 304 SourceLocation AtomicLocation; 305 306 public: 307 explicit DSAStackTy(Sema &S) : SemaRef(S) {} 308 309 /// Sets omp_allocator_handle_t type. 310 void setOMPAllocatorHandleT(QualType Ty) { OMPAllocatorHandleT = Ty; } 311 /// Gets omp_allocator_handle_t type. 312 QualType getOMPAllocatorHandleT() const { return OMPAllocatorHandleT; } 313 /// Sets omp_alloctrait_t type. 314 void setOMPAlloctraitT(QualType Ty) { OMPAlloctraitT = Ty; } 315 /// Gets omp_alloctrait_t type. 316 QualType getOMPAlloctraitT() const { return OMPAlloctraitT; } 317 /// Sets the given default allocator. 318 void setAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, 319 Expr *Allocator) { 320 OMPPredefinedAllocators[AllocatorKind] = Allocator; 321 } 322 /// Returns the specified default allocator. 323 Expr *getAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind) const { 324 return OMPPredefinedAllocators[AllocatorKind]; 325 } 326 /// Sets omp_depend_t type. 327 void setOMPDependT(QualType Ty) { OMPDependT = Ty; } 328 /// Gets omp_depend_t type. 329 QualType getOMPDependT() const { return OMPDependT; } 330 331 /// Sets omp_event_handle_t type. 332 void setOMPEventHandleT(QualType Ty) { OMPEventHandleT = Ty; } 333 /// Gets omp_event_handle_t type. 334 QualType getOMPEventHandleT() const { return OMPEventHandleT; } 335 336 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; } 337 OpenMPClauseKind getClauseParsingMode() const { 338 assert(isClauseParsingMode() && "Must be in clause parsing mode."); 339 return ClauseKindMode; 340 } 341 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; } 342 343 bool isBodyComplete() const { 344 const SharingMapTy *Top = getTopOfStackOrNull(); 345 return Top && Top->BodyComplete; 346 } 347 void setBodyComplete() { 348 getTopOfStack().BodyComplete = true; 349 } 350 351 bool isForceVarCapturing() const { return ForceCapturing; } 352 void setForceVarCapturing(bool V) { ForceCapturing = V; } 353 354 void setForceCaptureByReferenceInTargetExecutable(bool V) { 355 ForceCaptureByReferenceInTargetExecutable = V; 356 } 357 bool isForceCaptureByReferenceInTargetExecutable() const { 358 return ForceCaptureByReferenceInTargetExecutable; 359 } 360 361 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName, 362 Scope *CurScope, SourceLocation Loc) { 363 assert(!IgnoredStackElements && 364 "cannot change stack while ignoring elements"); 365 if (Stack.empty() || 366 Stack.back().second != CurrentNonCapturingFunctionScope) 367 Stack.emplace_back(StackTy(), CurrentNonCapturingFunctionScope); 368 Stack.back().first.emplace_back(DKind, DirName, CurScope, Loc); 369 Stack.back().first.back().DefaultAttrLoc = Loc; 370 } 371 372 void pop() { 373 assert(!IgnoredStackElements && 374 "cannot change stack while ignoring elements"); 375 assert(!Stack.back().first.empty() && 376 "Data-sharing attributes stack is empty!"); 377 Stack.back().first.pop_back(); 378 } 379 380 /// RAII object to temporarily leave the scope of a directive when we want to 381 /// logically operate in its parent. 382 class ParentDirectiveScope { 383 DSAStackTy &Self; 384 bool Active; 385 public: 386 ParentDirectiveScope(DSAStackTy &Self, bool Activate) 387 : Self(Self), Active(false) { 388 if (Activate) 389 enable(); 390 } 391 ~ParentDirectiveScope() { disable(); } 392 void disable() { 393 if (Active) { 394 --Self.IgnoredStackElements; 395 Active = false; 396 } 397 } 398 void enable() { 399 if (!Active) { 400 ++Self.IgnoredStackElements; 401 Active = true; 402 } 403 } 404 }; 405 406 /// Marks that we're started loop parsing. 407 void loopInit() { 408 assert(isOpenMPLoopDirective(getCurrentDirective()) && 409 "Expected loop-based directive."); 410 getTopOfStack().LoopStart = true; 411 } 412 /// Start capturing of the variables in the loop context. 413 void loopStart() { 414 assert(isOpenMPLoopDirective(getCurrentDirective()) && 415 "Expected loop-based directive."); 416 getTopOfStack().LoopStart = false; 417 } 418 /// true, if variables are captured, false otherwise. 419 bool isLoopStarted() const { 420 assert(isOpenMPLoopDirective(getCurrentDirective()) && 421 "Expected loop-based directive."); 422 return !getTopOfStack().LoopStart; 423 } 424 /// Marks (or clears) declaration as possibly loop counter. 425 void resetPossibleLoopCounter(const Decl *D = nullptr) { 426 getTopOfStack().PossiblyLoopCounter = 427 D ? D->getCanonicalDecl() : D; 428 } 429 /// Gets the possible loop counter decl. 430 const Decl *getPossiblyLoopCunter() const { 431 return getTopOfStack().PossiblyLoopCounter; 432 } 433 /// Start new OpenMP region stack in new non-capturing function. 434 void pushFunction() { 435 assert(!IgnoredStackElements && 436 "cannot change stack while ignoring elements"); 437 const FunctionScopeInfo *CurFnScope = SemaRef.getCurFunction(); 438 assert(!isa<CapturingScopeInfo>(CurFnScope)); 439 CurrentNonCapturingFunctionScope = CurFnScope; 440 } 441 /// Pop region stack for non-capturing function. 442 void popFunction(const FunctionScopeInfo *OldFSI) { 443 assert(!IgnoredStackElements && 444 "cannot change stack while ignoring elements"); 445 if (!Stack.empty() && Stack.back().second == OldFSI) { 446 assert(Stack.back().first.empty()); 447 Stack.pop_back(); 448 } 449 CurrentNonCapturingFunctionScope = nullptr; 450 for (const FunctionScopeInfo *FSI : llvm::reverse(SemaRef.FunctionScopes)) { 451 if (!isa<CapturingScopeInfo>(FSI)) { 452 CurrentNonCapturingFunctionScope = FSI; 453 break; 454 } 455 } 456 } 457 458 void addCriticalWithHint(const OMPCriticalDirective *D, llvm::APSInt Hint) { 459 Criticals.try_emplace(D->getDirectiveName().getAsString(), D, Hint); 460 } 461 const std::pair<const OMPCriticalDirective *, llvm::APSInt> 462 getCriticalWithHint(const DeclarationNameInfo &Name) const { 463 auto I = Criticals.find(Name.getAsString()); 464 if (I != Criticals.end()) 465 return I->second; 466 return std::make_pair(nullptr, llvm::APSInt()); 467 } 468 /// If 'aligned' declaration for given variable \a D was not seen yet, 469 /// add it and return NULL; otherwise return previous occurrence's expression 470 /// for diagnostics. 471 const Expr *addUniqueAligned(const ValueDecl *D, const Expr *NewDE); 472 /// If 'nontemporal' declaration for given variable \a D was not seen yet, 473 /// add it and return NULL; otherwise return previous occurrence's expression 474 /// for diagnostics. 475 const Expr *addUniqueNontemporal(const ValueDecl *D, const Expr *NewDE); 476 477 /// Register specified variable as loop control variable. 478 void addLoopControlVariable(const ValueDecl *D, VarDecl *Capture); 479 /// Check if the specified variable is a loop control variable for 480 /// current region. 481 /// \return The index of the loop control variable in the list of associated 482 /// for-loops (from outer to inner). 483 const LCDeclInfo isLoopControlVariable(const ValueDecl *D) const; 484 /// Check if the specified variable is a loop control variable for 485 /// parent region. 486 /// \return The index of the loop control variable in the list of associated 487 /// for-loops (from outer to inner). 488 const LCDeclInfo isParentLoopControlVariable(const ValueDecl *D) const; 489 /// Check if the specified variable is a loop control variable for 490 /// current region. 491 /// \return The index of the loop control variable in the list of associated 492 /// for-loops (from outer to inner). 493 const LCDeclInfo isLoopControlVariable(const ValueDecl *D, 494 unsigned Level) const; 495 /// Get the loop control variable for the I-th loop (or nullptr) in 496 /// parent directive. 497 const ValueDecl *getParentLoopControlVariable(unsigned I) const; 498 499 /// Marks the specified decl \p D as used in scan directive. 500 void markDeclAsUsedInScanDirective(ValueDecl *D) { 501 if (SharingMapTy *Stack = getSecondOnStackOrNull()) 502 Stack->UsedInScanDirective.insert(D); 503 } 504 505 /// Checks if the specified declaration was used in the inner scan directive. 506 bool isUsedInScanDirective(ValueDecl *D) const { 507 if (const SharingMapTy *Stack = getTopOfStackOrNull()) 508 return Stack->UsedInScanDirective.count(D) > 0; 509 return false; 510 } 511 512 /// Adds explicit data sharing attribute to the specified declaration. 513 void addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A, 514 DeclRefExpr *PrivateCopy = nullptr, unsigned Modifier = 0); 515 516 /// Adds additional information for the reduction items with the reduction id 517 /// represented as an operator. 518 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 519 BinaryOperatorKind BOK); 520 /// Adds additional information for the reduction items with the reduction id 521 /// represented as reduction identifier. 522 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 523 const Expr *ReductionRef); 524 /// Returns the location and reduction operation from the innermost parent 525 /// region for the given \p D. 526 const DSAVarData 527 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR, 528 BinaryOperatorKind &BOK, 529 Expr *&TaskgroupDescriptor) const; 530 /// Returns the location and reduction operation from the innermost parent 531 /// region for the given \p D. 532 const DSAVarData 533 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR, 534 const Expr *&ReductionRef, 535 Expr *&TaskgroupDescriptor) const; 536 /// Return reduction reference expression for the current taskgroup or 537 /// parallel/worksharing directives with task reductions. 538 Expr *getTaskgroupReductionRef() const { 539 assert((getTopOfStack().Directive == OMPD_taskgroup || 540 ((isOpenMPParallelDirective(getTopOfStack().Directive) || 541 isOpenMPWorksharingDirective(getTopOfStack().Directive)) && 542 !isOpenMPSimdDirective(getTopOfStack().Directive))) && 543 "taskgroup reference expression requested for non taskgroup or " 544 "parallel/worksharing directive."); 545 return getTopOfStack().TaskgroupReductionRef; 546 } 547 /// Checks if the given \p VD declaration is actually a taskgroup reduction 548 /// descriptor variable at the \p Level of OpenMP regions. 549 bool isTaskgroupReductionRef(const ValueDecl *VD, unsigned Level) const { 550 return getStackElemAtLevel(Level).TaskgroupReductionRef && 551 cast<DeclRefExpr>(getStackElemAtLevel(Level).TaskgroupReductionRef) 552 ->getDecl() == VD; 553 } 554 555 /// Returns data sharing attributes from top of the stack for the 556 /// specified declaration. 557 const DSAVarData getTopDSA(ValueDecl *D, bool FromParent); 558 /// Returns data-sharing attributes for the specified declaration. 559 const DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent) const; 560 /// Returns data-sharing attributes for the specified declaration. 561 const DSAVarData getImplicitDSA(ValueDecl *D, unsigned Level) const; 562 /// Checks if the specified variables has data-sharing attributes which 563 /// match specified \a CPred predicate in any directive which matches \a DPred 564 /// predicate. 565 const DSAVarData 566 hasDSA(ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 567 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 568 bool FromParent) const; 569 /// Checks if the specified variables has data-sharing attributes which 570 /// match specified \a CPred predicate in any innermost directive which 571 /// matches \a DPred predicate. 572 const DSAVarData 573 hasInnermostDSA(ValueDecl *D, 574 const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 575 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 576 bool FromParent) const; 577 /// Checks if the specified variables has explicit data-sharing 578 /// attributes which match specified \a CPred predicate at the specified 579 /// OpenMP region. 580 bool hasExplicitDSA(const ValueDecl *D, 581 const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 582 unsigned Level, bool NotLastprivate = false) const; 583 584 /// Returns true if the directive at level \Level matches in the 585 /// specified \a DPred predicate. 586 bool hasExplicitDirective( 587 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 588 unsigned Level) const; 589 590 /// Finds a directive which matches specified \a DPred predicate. 591 bool hasDirective( 592 const llvm::function_ref<bool( 593 OpenMPDirectiveKind, const DeclarationNameInfo &, SourceLocation)> 594 DPred, 595 bool FromParent) const; 596 597 /// Returns currently analyzed directive. 598 OpenMPDirectiveKind getCurrentDirective() const { 599 const SharingMapTy *Top = getTopOfStackOrNull(); 600 return Top ? Top->Directive : OMPD_unknown; 601 } 602 /// Returns directive kind at specified level. 603 OpenMPDirectiveKind getDirective(unsigned Level) const { 604 assert(!isStackEmpty() && "No directive at specified level."); 605 return getStackElemAtLevel(Level).Directive; 606 } 607 /// Returns the capture region at the specified level. 608 OpenMPDirectiveKind getCaptureRegion(unsigned Level, 609 unsigned OpenMPCaptureLevel) const { 610 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 611 getOpenMPCaptureRegions(CaptureRegions, getDirective(Level)); 612 return CaptureRegions[OpenMPCaptureLevel]; 613 } 614 /// Returns parent directive. 615 OpenMPDirectiveKind getParentDirective() const { 616 const SharingMapTy *Parent = getSecondOnStackOrNull(); 617 return Parent ? Parent->Directive : OMPD_unknown; 618 } 619 620 /// Add requires decl to internal vector 621 void addRequiresDecl(OMPRequiresDecl *RD) { 622 RequiresDecls.push_back(RD); 623 } 624 625 /// Checks if the defined 'requires' directive has specified type of clause. 626 template <typename ClauseType> 627 bool hasRequiresDeclWithClause() const { 628 return llvm::any_of(RequiresDecls, [](const OMPRequiresDecl *D) { 629 return llvm::any_of(D->clauselists(), [](const OMPClause *C) { 630 return isa<ClauseType>(C); 631 }); 632 }); 633 } 634 635 /// Checks for a duplicate clause amongst previously declared requires 636 /// directives 637 bool hasDuplicateRequiresClause(ArrayRef<OMPClause *> ClauseList) const { 638 bool IsDuplicate = false; 639 for (OMPClause *CNew : ClauseList) { 640 for (const OMPRequiresDecl *D : RequiresDecls) { 641 for (const OMPClause *CPrev : D->clauselists()) { 642 if (CNew->getClauseKind() == CPrev->getClauseKind()) { 643 SemaRef.Diag(CNew->getBeginLoc(), 644 diag::err_omp_requires_clause_redeclaration) 645 << getOpenMPClauseName(CNew->getClauseKind()); 646 SemaRef.Diag(CPrev->getBeginLoc(), 647 diag::note_omp_requires_previous_clause) 648 << getOpenMPClauseName(CPrev->getClauseKind()); 649 IsDuplicate = true; 650 } 651 } 652 } 653 } 654 return IsDuplicate; 655 } 656 657 /// Add location of previously encountered target to internal vector 658 void addTargetDirLocation(SourceLocation LocStart) { 659 TargetLocations.push_back(LocStart); 660 } 661 662 /// Add location for the first encountered atomicc directive. 663 void addAtomicDirectiveLoc(SourceLocation Loc) { 664 if (AtomicLocation.isInvalid()) 665 AtomicLocation = Loc; 666 } 667 668 /// Returns the location of the first encountered atomic directive in the 669 /// module. 670 SourceLocation getAtomicDirectiveLoc() const { 671 return AtomicLocation; 672 } 673 674 // Return previously encountered target region locations. 675 ArrayRef<SourceLocation> getEncounteredTargetLocs() const { 676 return TargetLocations; 677 } 678 679 /// Set default data sharing attribute to none. 680 void setDefaultDSANone(SourceLocation Loc) { 681 getTopOfStack().DefaultAttr = DSA_none; 682 getTopOfStack().DefaultAttrLoc = Loc; 683 } 684 /// Set default data sharing attribute to shared. 685 void setDefaultDSAShared(SourceLocation Loc) { 686 getTopOfStack().DefaultAttr = DSA_shared; 687 getTopOfStack().DefaultAttrLoc = Loc; 688 } 689 /// Set default data sharing attribute to firstprivate. 690 void setDefaultDSAFirstPrivate(SourceLocation Loc) { 691 getTopOfStack().DefaultAttr = DSA_firstprivate; 692 getTopOfStack().DefaultAttrLoc = Loc; 693 } 694 /// Set default data mapping attribute to Modifier:Kind 695 void setDefaultDMAAttr(OpenMPDefaultmapClauseModifier M, 696 OpenMPDefaultmapClauseKind Kind, 697 SourceLocation Loc) { 698 DefaultmapInfo &DMI = getTopOfStack().DefaultmapMap[Kind]; 699 DMI.ImplicitBehavior = M; 700 DMI.SLoc = Loc; 701 } 702 /// Check whether the implicit-behavior has been set in defaultmap 703 bool checkDefaultmapCategory(OpenMPDefaultmapClauseKind VariableCategory) { 704 if (VariableCategory == OMPC_DEFAULTMAP_unknown) 705 return getTopOfStack() 706 .DefaultmapMap[OMPC_DEFAULTMAP_aggregate] 707 .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown || 708 getTopOfStack() 709 .DefaultmapMap[OMPC_DEFAULTMAP_scalar] 710 .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown || 711 getTopOfStack() 712 .DefaultmapMap[OMPC_DEFAULTMAP_pointer] 713 .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown; 714 return getTopOfStack().DefaultmapMap[VariableCategory].ImplicitBehavior != 715 OMPC_DEFAULTMAP_MODIFIER_unknown; 716 } 717 718 DefaultDataSharingAttributes getDefaultDSA(unsigned Level) const { 719 return getStackSize() <= Level ? DSA_unspecified 720 : getStackElemAtLevel(Level).DefaultAttr; 721 } 722 DefaultDataSharingAttributes getDefaultDSA() const { 723 return isStackEmpty() ? DSA_unspecified 724 : getTopOfStack().DefaultAttr; 725 } 726 SourceLocation getDefaultDSALocation() const { 727 return isStackEmpty() ? SourceLocation() 728 : getTopOfStack().DefaultAttrLoc; 729 } 730 OpenMPDefaultmapClauseModifier 731 getDefaultmapModifier(OpenMPDefaultmapClauseKind Kind) const { 732 return isStackEmpty() 733 ? OMPC_DEFAULTMAP_MODIFIER_unknown 734 : getTopOfStack().DefaultmapMap[Kind].ImplicitBehavior; 735 } 736 OpenMPDefaultmapClauseModifier 737 getDefaultmapModifierAtLevel(unsigned Level, 738 OpenMPDefaultmapClauseKind Kind) const { 739 return getStackElemAtLevel(Level).DefaultmapMap[Kind].ImplicitBehavior; 740 } 741 bool isDefaultmapCapturedByRef(unsigned Level, 742 OpenMPDefaultmapClauseKind Kind) const { 743 OpenMPDefaultmapClauseModifier M = 744 getDefaultmapModifierAtLevel(Level, Kind); 745 if (Kind == OMPC_DEFAULTMAP_scalar || Kind == OMPC_DEFAULTMAP_pointer) { 746 return (M == OMPC_DEFAULTMAP_MODIFIER_alloc) || 747 (M == OMPC_DEFAULTMAP_MODIFIER_to) || 748 (M == OMPC_DEFAULTMAP_MODIFIER_from) || 749 (M == OMPC_DEFAULTMAP_MODIFIER_tofrom); 750 } 751 return true; 752 } 753 static bool mustBeFirstprivateBase(OpenMPDefaultmapClauseModifier M, 754 OpenMPDefaultmapClauseKind Kind) { 755 switch (Kind) { 756 case OMPC_DEFAULTMAP_scalar: 757 case OMPC_DEFAULTMAP_pointer: 758 return (M == OMPC_DEFAULTMAP_MODIFIER_unknown) || 759 (M == OMPC_DEFAULTMAP_MODIFIER_firstprivate) || 760 (M == OMPC_DEFAULTMAP_MODIFIER_default); 761 case OMPC_DEFAULTMAP_aggregate: 762 return M == OMPC_DEFAULTMAP_MODIFIER_firstprivate; 763 default: 764 break; 765 } 766 llvm_unreachable("Unexpected OpenMPDefaultmapClauseKind enum"); 767 } 768 bool mustBeFirstprivateAtLevel(unsigned Level, 769 OpenMPDefaultmapClauseKind Kind) const { 770 OpenMPDefaultmapClauseModifier M = 771 getDefaultmapModifierAtLevel(Level, Kind); 772 return mustBeFirstprivateBase(M, Kind); 773 } 774 bool mustBeFirstprivate(OpenMPDefaultmapClauseKind Kind) const { 775 OpenMPDefaultmapClauseModifier M = getDefaultmapModifier(Kind); 776 return mustBeFirstprivateBase(M, Kind); 777 } 778 779 /// Checks if the specified variable is a threadprivate. 780 bool isThreadPrivate(VarDecl *D) { 781 const DSAVarData DVar = getTopDSA(D, false); 782 return isOpenMPThreadPrivate(DVar.CKind); 783 } 784 785 /// Marks current region as ordered (it has an 'ordered' clause). 786 void setOrderedRegion(bool IsOrdered, const Expr *Param, 787 OMPOrderedClause *Clause) { 788 if (IsOrdered) 789 getTopOfStack().OrderedRegion.emplace(Param, Clause); 790 else 791 getTopOfStack().OrderedRegion.reset(); 792 } 793 /// Returns true, if region is ordered (has associated 'ordered' clause), 794 /// false - otherwise. 795 bool isOrderedRegion() const { 796 if (const SharingMapTy *Top = getTopOfStackOrNull()) 797 return Top->OrderedRegion.hasValue(); 798 return false; 799 } 800 /// Returns optional parameter for the ordered region. 801 std::pair<const Expr *, OMPOrderedClause *> getOrderedRegionParam() const { 802 if (const SharingMapTy *Top = getTopOfStackOrNull()) 803 if (Top->OrderedRegion.hasValue()) 804 return Top->OrderedRegion.getValue(); 805 return std::make_pair(nullptr, nullptr); 806 } 807 /// Returns true, if parent region is ordered (has associated 808 /// 'ordered' clause), false - otherwise. 809 bool isParentOrderedRegion() const { 810 if (const SharingMapTy *Parent = getSecondOnStackOrNull()) 811 return Parent->OrderedRegion.hasValue(); 812 return false; 813 } 814 /// Returns optional parameter for the ordered region. 815 std::pair<const Expr *, OMPOrderedClause *> 816 getParentOrderedRegionParam() const { 817 if (const SharingMapTy *Parent = getSecondOnStackOrNull()) 818 if (Parent->OrderedRegion.hasValue()) 819 return Parent->OrderedRegion.getValue(); 820 return std::make_pair(nullptr, nullptr); 821 } 822 /// Marks current region as nowait (it has a 'nowait' clause). 823 void setNowaitRegion(bool IsNowait = true) { 824 getTopOfStack().NowaitRegion = IsNowait; 825 } 826 /// Returns true, if parent region is nowait (has associated 827 /// 'nowait' clause), false - otherwise. 828 bool isParentNowaitRegion() const { 829 if (const SharingMapTy *Parent = getSecondOnStackOrNull()) 830 return Parent->NowaitRegion; 831 return false; 832 } 833 /// Marks parent region as cancel region. 834 void setParentCancelRegion(bool Cancel = true) { 835 if (SharingMapTy *Parent = getSecondOnStackOrNull()) 836 Parent->CancelRegion |= Cancel; 837 } 838 /// Return true if current region has inner cancel construct. 839 bool isCancelRegion() const { 840 const SharingMapTy *Top = getTopOfStackOrNull(); 841 return Top ? Top->CancelRegion : false; 842 } 843 844 /// Mark that parent region already has scan directive. 845 void setParentHasScanDirective(SourceLocation Loc) { 846 if (SharingMapTy *Parent = getSecondOnStackOrNull()) 847 Parent->PrevScanLocation = Loc; 848 } 849 /// Return true if current region has inner cancel construct. 850 bool doesParentHasScanDirective() const { 851 const SharingMapTy *Top = getSecondOnStackOrNull(); 852 return Top ? Top->PrevScanLocation.isValid() : false; 853 } 854 /// Return true if current region has inner cancel construct. 855 SourceLocation getParentScanDirectiveLoc() const { 856 const SharingMapTy *Top = getSecondOnStackOrNull(); 857 return Top ? Top->PrevScanLocation : SourceLocation(); 858 } 859 /// Mark that parent region already has ordered directive. 860 void setParentHasOrderedDirective(SourceLocation Loc) { 861 if (SharingMapTy *Parent = getSecondOnStackOrNull()) 862 Parent->PrevOrderedLocation = Loc; 863 } 864 /// Return true if current region has inner ordered construct. 865 bool doesParentHasOrderedDirective() const { 866 const SharingMapTy *Top = getSecondOnStackOrNull(); 867 return Top ? Top->PrevOrderedLocation.isValid() : false; 868 } 869 /// Returns the location of the previously specified ordered directive. 870 SourceLocation getParentOrderedDirectiveLoc() const { 871 const SharingMapTy *Top = getSecondOnStackOrNull(); 872 return Top ? Top->PrevOrderedLocation : SourceLocation(); 873 } 874 875 /// Set collapse value for the region. 876 void setAssociatedLoops(unsigned Val) { 877 getTopOfStack().AssociatedLoops = Val; 878 if (Val > 1) 879 getTopOfStack().HasMutipleLoops = true; 880 } 881 /// Return collapse value for region. 882 unsigned getAssociatedLoops() const { 883 const SharingMapTy *Top = getTopOfStackOrNull(); 884 return Top ? Top->AssociatedLoops : 0; 885 } 886 /// Returns true if the construct is associated with multiple loops. 887 bool hasMutipleLoops() const { 888 const SharingMapTy *Top = getTopOfStackOrNull(); 889 return Top ? Top->HasMutipleLoops : false; 890 } 891 892 /// Marks current target region as one with closely nested teams 893 /// region. 894 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) { 895 if (SharingMapTy *Parent = getSecondOnStackOrNull()) 896 Parent->InnerTeamsRegionLoc = TeamsRegionLoc; 897 } 898 /// Returns true, if current region has closely nested teams region. 899 bool hasInnerTeamsRegion() const { 900 return getInnerTeamsRegionLoc().isValid(); 901 } 902 /// Returns location of the nested teams region (if any). 903 SourceLocation getInnerTeamsRegionLoc() const { 904 const SharingMapTy *Top = getTopOfStackOrNull(); 905 return Top ? Top->InnerTeamsRegionLoc : SourceLocation(); 906 } 907 908 Scope *getCurScope() const { 909 const SharingMapTy *Top = getTopOfStackOrNull(); 910 return Top ? Top->CurScope : nullptr; 911 } 912 SourceLocation getConstructLoc() const { 913 const SharingMapTy *Top = getTopOfStackOrNull(); 914 return Top ? Top->ConstructLoc : SourceLocation(); 915 } 916 917 /// Do the check specified in \a Check to all component lists and return true 918 /// if any issue is found. 919 bool checkMappableExprComponentListsForDecl( 920 const ValueDecl *VD, bool CurrentRegionOnly, 921 const llvm::function_ref< 922 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef, 923 OpenMPClauseKind)> 924 Check) const { 925 if (isStackEmpty()) 926 return false; 927 auto SI = begin(); 928 auto SE = end(); 929 930 if (SI == SE) 931 return false; 932 933 if (CurrentRegionOnly) 934 SE = std::next(SI); 935 else 936 std::advance(SI, 1); 937 938 for (; SI != SE; ++SI) { 939 auto MI = SI->MappedExprComponents.find(VD); 940 if (MI != SI->MappedExprComponents.end()) 941 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L : 942 MI->second.Components) 943 if (Check(L, MI->second.Kind)) 944 return true; 945 } 946 return false; 947 } 948 949 /// Do the check specified in \a Check to all component lists at a given level 950 /// and return true if any issue is found. 951 bool checkMappableExprComponentListsForDeclAtLevel( 952 const ValueDecl *VD, unsigned Level, 953 const llvm::function_ref< 954 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef, 955 OpenMPClauseKind)> 956 Check) const { 957 if (getStackSize() <= Level) 958 return false; 959 960 const SharingMapTy &StackElem = getStackElemAtLevel(Level); 961 auto MI = StackElem.MappedExprComponents.find(VD); 962 if (MI != StackElem.MappedExprComponents.end()) 963 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L : 964 MI->second.Components) 965 if (Check(L, MI->second.Kind)) 966 return true; 967 return false; 968 } 969 970 /// Create a new mappable expression component list associated with a given 971 /// declaration and initialize it with the provided list of components. 972 void addMappableExpressionComponents( 973 const ValueDecl *VD, 974 OMPClauseMappableExprCommon::MappableExprComponentListRef Components, 975 OpenMPClauseKind WhereFoundClauseKind) { 976 MappedExprComponentTy &MEC = getTopOfStack().MappedExprComponents[VD]; 977 // Create new entry and append the new components there. 978 MEC.Components.resize(MEC.Components.size() + 1); 979 MEC.Components.back().append(Components.begin(), Components.end()); 980 MEC.Kind = WhereFoundClauseKind; 981 } 982 983 unsigned getNestingLevel() const { 984 assert(!isStackEmpty()); 985 return getStackSize() - 1; 986 } 987 void addDoacrossDependClause(OMPDependClause *C, 988 const OperatorOffsetTy &OpsOffs) { 989 SharingMapTy *Parent = getSecondOnStackOrNull(); 990 assert(Parent && isOpenMPWorksharingDirective(Parent->Directive)); 991 Parent->DoacrossDepends.try_emplace(C, OpsOffs); 992 } 993 llvm::iterator_range<DoacrossDependMapTy::const_iterator> 994 getDoacrossDependClauses() const { 995 const SharingMapTy &StackElem = getTopOfStack(); 996 if (isOpenMPWorksharingDirective(StackElem.Directive)) { 997 const DoacrossDependMapTy &Ref = StackElem.DoacrossDepends; 998 return llvm::make_range(Ref.begin(), Ref.end()); 999 } 1000 return llvm::make_range(StackElem.DoacrossDepends.end(), 1001 StackElem.DoacrossDepends.end()); 1002 } 1003 1004 // Store types of classes which have been explicitly mapped 1005 void addMappedClassesQualTypes(QualType QT) { 1006 SharingMapTy &StackElem = getTopOfStack(); 1007 StackElem.MappedClassesQualTypes.insert(QT); 1008 } 1009 1010 // Return set of mapped classes types 1011 bool isClassPreviouslyMapped(QualType QT) const { 1012 const SharingMapTy &StackElem = getTopOfStack(); 1013 return StackElem.MappedClassesQualTypes.count(QT) != 0; 1014 } 1015 1016 /// Adds global declare target to the parent target region. 1017 void addToParentTargetRegionLinkGlobals(DeclRefExpr *E) { 1018 assert(*OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration( 1019 E->getDecl()) == OMPDeclareTargetDeclAttr::MT_Link && 1020 "Expected declare target link global."); 1021 for (auto &Elem : *this) { 1022 if (isOpenMPTargetExecutionDirective(Elem.Directive)) { 1023 Elem.DeclareTargetLinkVarDecls.push_back(E); 1024 return; 1025 } 1026 } 1027 } 1028 1029 /// Returns the list of globals with declare target link if current directive 1030 /// is target. 1031 ArrayRef<DeclRefExpr *> getLinkGlobals() const { 1032 assert(isOpenMPTargetExecutionDirective(getCurrentDirective()) && 1033 "Expected target executable directive."); 1034 return getTopOfStack().DeclareTargetLinkVarDecls; 1035 } 1036 1037 /// Adds list of allocators expressions. 1038 void addInnerAllocatorExpr(Expr *E) { 1039 getTopOfStack().InnerUsedAllocators.push_back(E); 1040 } 1041 /// Return list of used allocators. 1042 ArrayRef<Expr *> getInnerAllocators() const { 1043 return getTopOfStack().InnerUsedAllocators; 1044 } 1045 /// Marks the declaration as implicitly firstprivate nin the task-based 1046 /// regions. 1047 void addImplicitTaskFirstprivate(unsigned Level, Decl *D) { 1048 getStackElemAtLevel(Level).ImplicitTaskFirstprivates.insert(D); 1049 } 1050 /// Checks if the decl is implicitly firstprivate in the task-based region. 1051 bool isImplicitTaskFirstprivate(Decl *D) const { 1052 return getTopOfStack().ImplicitTaskFirstprivates.count(D) > 0; 1053 } 1054 1055 /// Marks decl as used in uses_allocators clause as the allocator. 1056 void addUsesAllocatorsDecl(const Decl *D, UsesAllocatorsDeclKind Kind) { 1057 getTopOfStack().UsesAllocatorsDecls.try_emplace(D, Kind); 1058 } 1059 /// Checks if specified decl is used in uses allocator clause as the 1060 /// allocator. 1061 Optional<UsesAllocatorsDeclKind> isUsesAllocatorsDecl(unsigned Level, 1062 const Decl *D) const { 1063 const SharingMapTy &StackElem = getTopOfStack(); 1064 auto I = StackElem.UsesAllocatorsDecls.find(D); 1065 if (I == StackElem.UsesAllocatorsDecls.end()) 1066 return None; 1067 return I->getSecond(); 1068 } 1069 Optional<UsesAllocatorsDeclKind> isUsesAllocatorsDecl(const Decl *D) const { 1070 const SharingMapTy &StackElem = getTopOfStack(); 1071 auto I = StackElem.UsesAllocatorsDecls.find(D); 1072 if (I == StackElem.UsesAllocatorsDecls.end()) 1073 return None; 1074 return I->getSecond(); 1075 } 1076 1077 void addDeclareMapperVarRef(Expr *Ref) { 1078 SharingMapTy &StackElem = getTopOfStack(); 1079 StackElem.DeclareMapperVar = Ref; 1080 } 1081 const Expr *getDeclareMapperVarRef() const { 1082 const SharingMapTy *Top = getTopOfStackOrNull(); 1083 return Top ? Top->DeclareMapperVar : nullptr; 1084 } 1085 }; 1086 1087 bool isImplicitTaskingRegion(OpenMPDirectiveKind DKind) { 1088 return isOpenMPParallelDirective(DKind) || isOpenMPTeamsDirective(DKind); 1089 } 1090 1091 bool isImplicitOrExplicitTaskingRegion(OpenMPDirectiveKind DKind) { 1092 return isImplicitTaskingRegion(DKind) || isOpenMPTaskingDirective(DKind) || 1093 DKind == OMPD_unknown; 1094 } 1095 1096 } // namespace 1097 1098 static const Expr *getExprAsWritten(const Expr *E) { 1099 if (const auto *FE = dyn_cast<FullExpr>(E)) 1100 E = FE->getSubExpr(); 1101 1102 if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E)) 1103 E = MTE->getSubExpr(); 1104 1105 while (const auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E)) 1106 E = Binder->getSubExpr(); 1107 1108 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E)) 1109 E = ICE->getSubExprAsWritten(); 1110 return E->IgnoreParens(); 1111 } 1112 1113 static Expr *getExprAsWritten(Expr *E) { 1114 return const_cast<Expr *>(getExprAsWritten(const_cast<const Expr *>(E))); 1115 } 1116 1117 static const ValueDecl *getCanonicalDecl(const ValueDecl *D) { 1118 if (const auto *CED = dyn_cast<OMPCapturedExprDecl>(D)) 1119 if (const auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 1120 D = ME->getMemberDecl(); 1121 const auto *VD = dyn_cast<VarDecl>(D); 1122 const auto *FD = dyn_cast<FieldDecl>(D); 1123 if (VD != nullptr) { 1124 VD = VD->getCanonicalDecl(); 1125 D = VD; 1126 } else { 1127 assert(FD); 1128 FD = FD->getCanonicalDecl(); 1129 D = FD; 1130 } 1131 return D; 1132 } 1133 1134 static ValueDecl *getCanonicalDecl(ValueDecl *D) { 1135 return const_cast<ValueDecl *>( 1136 getCanonicalDecl(const_cast<const ValueDecl *>(D))); 1137 } 1138 1139 DSAStackTy::DSAVarData DSAStackTy::getDSA(const_iterator &Iter, 1140 ValueDecl *D) const { 1141 D = getCanonicalDecl(D); 1142 auto *VD = dyn_cast<VarDecl>(D); 1143 const auto *FD = dyn_cast<FieldDecl>(D); 1144 DSAVarData DVar; 1145 if (Iter == end()) { 1146 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1147 // in a region but not in construct] 1148 // File-scope or namespace-scope variables referenced in called routines 1149 // in the region are shared unless they appear in a threadprivate 1150 // directive. 1151 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(VD)) 1152 DVar.CKind = OMPC_shared; 1153 1154 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced 1155 // in a region but not in construct] 1156 // Variables with static storage duration that are declared in called 1157 // routines in the region are shared. 1158 if (VD && VD->hasGlobalStorage()) 1159 DVar.CKind = OMPC_shared; 1160 1161 // Non-static data members are shared by default. 1162 if (FD) 1163 DVar.CKind = OMPC_shared; 1164 1165 return DVar; 1166 } 1167 1168 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1169 // in a Construct, C/C++, predetermined, p.1] 1170 // Variables with automatic storage duration that are declared in a scope 1171 // inside the construct are private. 1172 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() && 1173 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) { 1174 DVar.CKind = OMPC_private; 1175 return DVar; 1176 } 1177 1178 DVar.DKind = Iter->Directive; 1179 // Explicitly specified attributes and local variables with predetermined 1180 // attributes. 1181 if (Iter->SharingMap.count(D)) { 1182 const DSAInfo &Data = Iter->SharingMap.lookup(D); 1183 DVar.RefExpr = Data.RefExpr.getPointer(); 1184 DVar.PrivateCopy = Data.PrivateCopy; 1185 DVar.CKind = Data.Attributes; 1186 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 1187 DVar.Modifier = Data.Modifier; 1188 return DVar; 1189 } 1190 1191 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1192 // in a Construct, C/C++, implicitly determined, p.1] 1193 // In a parallel or task construct, the data-sharing attributes of these 1194 // variables are determined by the default clause, if present. 1195 switch (Iter->DefaultAttr) { 1196 case DSA_shared: 1197 DVar.CKind = OMPC_shared; 1198 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 1199 return DVar; 1200 case DSA_none: 1201 return DVar; 1202 case DSA_firstprivate: 1203 if (VD->getStorageDuration() == SD_Static && 1204 VD->getDeclContext()->isFileContext()) { 1205 DVar.CKind = OMPC_unknown; 1206 } else { 1207 DVar.CKind = OMPC_firstprivate; 1208 } 1209 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 1210 return DVar; 1211 case DSA_unspecified: 1212 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1213 // in a Construct, implicitly determined, p.2] 1214 // In a parallel construct, if no default clause is present, these 1215 // variables are shared. 1216 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 1217 if ((isOpenMPParallelDirective(DVar.DKind) && 1218 !isOpenMPTaskLoopDirective(DVar.DKind)) || 1219 isOpenMPTeamsDirective(DVar.DKind)) { 1220 DVar.CKind = OMPC_shared; 1221 return DVar; 1222 } 1223 1224 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1225 // in a Construct, implicitly determined, p.4] 1226 // In a task construct, if no default clause is present, a variable that in 1227 // the enclosing context is determined to be shared by all implicit tasks 1228 // bound to the current team is shared. 1229 if (isOpenMPTaskingDirective(DVar.DKind)) { 1230 DSAVarData DVarTemp; 1231 const_iterator I = Iter, E = end(); 1232 do { 1233 ++I; 1234 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables 1235 // Referenced in a Construct, implicitly determined, p.6] 1236 // In a task construct, if no default clause is present, a variable 1237 // whose data-sharing attribute is not determined by the rules above is 1238 // firstprivate. 1239 DVarTemp = getDSA(I, D); 1240 if (DVarTemp.CKind != OMPC_shared) { 1241 DVar.RefExpr = nullptr; 1242 DVar.CKind = OMPC_firstprivate; 1243 return DVar; 1244 } 1245 } while (I != E && !isImplicitTaskingRegion(I->Directive)); 1246 DVar.CKind = 1247 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared; 1248 return DVar; 1249 } 1250 } 1251 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1252 // in a Construct, implicitly determined, p.3] 1253 // For constructs other than task, if no default clause is present, these 1254 // variables inherit their data-sharing attributes from the enclosing 1255 // context. 1256 return getDSA(++Iter, D); 1257 } 1258 1259 const Expr *DSAStackTy::addUniqueAligned(const ValueDecl *D, 1260 const Expr *NewDE) { 1261 assert(!isStackEmpty() && "Data sharing attributes stack is empty"); 1262 D = getCanonicalDecl(D); 1263 SharingMapTy &StackElem = getTopOfStack(); 1264 auto It = StackElem.AlignedMap.find(D); 1265 if (It == StackElem.AlignedMap.end()) { 1266 assert(NewDE && "Unexpected nullptr expr to be added into aligned map"); 1267 StackElem.AlignedMap[D] = NewDE; 1268 return nullptr; 1269 } 1270 assert(It->second && "Unexpected nullptr expr in the aligned map"); 1271 return It->second; 1272 } 1273 1274 const Expr *DSAStackTy::addUniqueNontemporal(const ValueDecl *D, 1275 const Expr *NewDE) { 1276 assert(!isStackEmpty() && "Data sharing attributes stack is empty"); 1277 D = getCanonicalDecl(D); 1278 SharingMapTy &StackElem = getTopOfStack(); 1279 auto It = StackElem.NontemporalMap.find(D); 1280 if (It == StackElem.NontemporalMap.end()) { 1281 assert(NewDE && "Unexpected nullptr expr to be added into aligned map"); 1282 StackElem.NontemporalMap[D] = NewDE; 1283 return nullptr; 1284 } 1285 assert(It->second && "Unexpected nullptr expr in the aligned map"); 1286 return It->second; 1287 } 1288 1289 void DSAStackTy::addLoopControlVariable(const ValueDecl *D, VarDecl *Capture) { 1290 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1291 D = getCanonicalDecl(D); 1292 SharingMapTy &StackElem = getTopOfStack(); 1293 StackElem.LCVMap.try_emplace( 1294 D, LCDeclInfo(StackElem.LCVMap.size() + 1, Capture)); 1295 } 1296 1297 const DSAStackTy::LCDeclInfo 1298 DSAStackTy::isLoopControlVariable(const ValueDecl *D) const { 1299 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1300 D = getCanonicalDecl(D); 1301 const SharingMapTy &StackElem = getTopOfStack(); 1302 auto It = StackElem.LCVMap.find(D); 1303 if (It != StackElem.LCVMap.end()) 1304 return It->second; 1305 return {0, nullptr}; 1306 } 1307 1308 const DSAStackTy::LCDeclInfo 1309 DSAStackTy::isLoopControlVariable(const ValueDecl *D, unsigned Level) const { 1310 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1311 D = getCanonicalDecl(D); 1312 for (unsigned I = Level + 1; I > 0; --I) { 1313 const SharingMapTy &StackElem = getStackElemAtLevel(I - 1); 1314 auto It = StackElem.LCVMap.find(D); 1315 if (It != StackElem.LCVMap.end()) 1316 return It->second; 1317 } 1318 return {0, nullptr}; 1319 } 1320 1321 const DSAStackTy::LCDeclInfo 1322 DSAStackTy::isParentLoopControlVariable(const ValueDecl *D) const { 1323 const SharingMapTy *Parent = getSecondOnStackOrNull(); 1324 assert(Parent && "Data-sharing attributes stack is empty"); 1325 D = getCanonicalDecl(D); 1326 auto It = Parent->LCVMap.find(D); 1327 if (It != Parent->LCVMap.end()) 1328 return It->second; 1329 return {0, nullptr}; 1330 } 1331 1332 const ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) const { 1333 const SharingMapTy *Parent = getSecondOnStackOrNull(); 1334 assert(Parent && "Data-sharing attributes stack is empty"); 1335 if (Parent->LCVMap.size() < I) 1336 return nullptr; 1337 for (const auto &Pair : Parent->LCVMap) 1338 if (Pair.second.first == I) 1339 return Pair.first; 1340 return nullptr; 1341 } 1342 1343 void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A, 1344 DeclRefExpr *PrivateCopy, unsigned Modifier) { 1345 D = getCanonicalDecl(D); 1346 if (A == OMPC_threadprivate) { 1347 DSAInfo &Data = Threadprivates[D]; 1348 Data.Attributes = A; 1349 Data.RefExpr.setPointer(E); 1350 Data.PrivateCopy = nullptr; 1351 Data.Modifier = Modifier; 1352 } else { 1353 DSAInfo &Data = getTopOfStack().SharingMap[D]; 1354 assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) || 1355 (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) || 1356 (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) || 1357 (isLoopControlVariable(D).first && A == OMPC_private)); 1358 Data.Modifier = Modifier; 1359 if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) { 1360 Data.RefExpr.setInt(/*IntVal=*/true); 1361 return; 1362 } 1363 const bool IsLastprivate = 1364 A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate; 1365 Data.Attributes = A; 1366 Data.RefExpr.setPointerAndInt(E, IsLastprivate); 1367 Data.PrivateCopy = PrivateCopy; 1368 if (PrivateCopy) { 1369 DSAInfo &Data = getTopOfStack().SharingMap[PrivateCopy->getDecl()]; 1370 Data.Modifier = Modifier; 1371 Data.Attributes = A; 1372 Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate); 1373 Data.PrivateCopy = nullptr; 1374 } 1375 } 1376 } 1377 1378 /// Build a variable declaration for OpenMP loop iteration variable. 1379 static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type, 1380 StringRef Name, const AttrVec *Attrs = nullptr, 1381 DeclRefExpr *OrigRef = nullptr) { 1382 DeclContext *DC = SemaRef.CurContext; 1383 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name); 1384 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc); 1385 auto *Decl = 1386 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None); 1387 if (Attrs) { 1388 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end()); 1389 I != E; ++I) 1390 Decl->addAttr(*I); 1391 } 1392 Decl->setImplicit(); 1393 if (OrigRef) { 1394 Decl->addAttr( 1395 OMPReferencedVarAttr::CreateImplicit(SemaRef.Context, OrigRef)); 1396 } 1397 return Decl; 1398 } 1399 1400 static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty, 1401 SourceLocation Loc, 1402 bool RefersToCapture = false) { 1403 D->setReferenced(); 1404 D->markUsed(S.Context); 1405 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(), 1406 SourceLocation(), D, RefersToCapture, Loc, Ty, 1407 VK_LValue); 1408 } 1409 1410 void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 1411 BinaryOperatorKind BOK) { 1412 D = getCanonicalDecl(D); 1413 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1414 assert( 1415 getTopOfStack().SharingMap[D].Attributes == OMPC_reduction && 1416 "Additional reduction info may be specified only for reduction items."); 1417 ReductionData &ReductionData = getTopOfStack().ReductionMap[D]; 1418 assert(ReductionData.ReductionRange.isInvalid() && 1419 (getTopOfStack().Directive == OMPD_taskgroup || 1420 ((isOpenMPParallelDirective(getTopOfStack().Directive) || 1421 isOpenMPWorksharingDirective(getTopOfStack().Directive)) && 1422 !isOpenMPSimdDirective(getTopOfStack().Directive))) && 1423 "Additional reduction info may be specified only once for reduction " 1424 "items."); 1425 ReductionData.set(BOK, SR); 1426 Expr *&TaskgroupReductionRef = 1427 getTopOfStack().TaskgroupReductionRef; 1428 if (!TaskgroupReductionRef) { 1429 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(), 1430 SemaRef.Context.VoidPtrTy, ".task_red."); 1431 TaskgroupReductionRef = 1432 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin()); 1433 } 1434 } 1435 1436 void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 1437 const Expr *ReductionRef) { 1438 D = getCanonicalDecl(D); 1439 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1440 assert( 1441 getTopOfStack().SharingMap[D].Attributes == OMPC_reduction && 1442 "Additional reduction info may be specified only for reduction items."); 1443 ReductionData &ReductionData = getTopOfStack().ReductionMap[D]; 1444 assert(ReductionData.ReductionRange.isInvalid() && 1445 (getTopOfStack().Directive == OMPD_taskgroup || 1446 ((isOpenMPParallelDirective(getTopOfStack().Directive) || 1447 isOpenMPWorksharingDirective(getTopOfStack().Directive)) && 1448 !isOpenMPSimdDirective(getTopOfStack().Directive))) && 1449 "Additional reduction info may be specified only once for reduction " 1450 "items."); 1451 ReductionData.set(ReductionRef, SR); 1452 Expr *&TaskgroupReductionRef = 1453 getTopOfStack().TaskgroupReductionRef; 1454 if (!TaskgroupReductionRef) { 1455 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(), 1456 SemaRef.Context.VoidPtrTy, ".task_red."); 1457 TaskgroupReductionRef = 1458 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin()); 1459 } 1460 } 1461 1462 const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData( 1463 const ValueDecl *D, SourceRange &SR, BinaryOperatorKind &BOK, 1464 Expr *&TaskgroupDescriptor) const { 1465 D = getCanonicalDecl(D); 1466 assert(!isStackEmpty() && "Data-sharing attributes stack is empty."); 1467 for (const_iterator I = begin() + 1, E = end(); I != E; ++I) { 1468 const DSAInfo &Data = I->SharingMap.lookup(D); 1469 if (Data.Attributes != OMPC_reduction || 1470 Data.Modifier != OMPC_REDUCTION_task) 1471 continue; 1472 const ReductionData &ReductionData = I->ReductionMap.lookup(D); 1473 if (!ReductionData.ReductionOp || 1474 ReductionData.ReductionOp.is<const Expr *>()) 1475 return DSAVarData(); 1476 SR = ReductionData.ReductionRange; 1477 BOK = ReductionData.ReductionOp.get<ReductionData::BOKPtrType>(); 1478 assert(I->TaskgroupReductionRef && "taskgroup reduction reference " 1479 "expression for the descriptor is not " 1480 "set."); 1481 TaskgroupDescriptor = I->TaskgroupReductionRef; 1482 return DSAVarData(I->Directive, OMPC_reduction, Data.RefExpr.getPointer(), 1483 Data.PrivateCopy, I->DefaultAttrLoc, OMPC_REDUCTION_task); 1484 } 1485 return DSAVarData(); 1486 } 1487 1488 const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData( 1489 const ValueDecl *D, SourceRange &SR, const Expr *&ReductionRef, 1490 Expr *&TaskgroupDescriptor) const { 1491 D = getCanonicalDecl(D); 1492 assert(!isStackEmpty() && "Data-sharing attributes stack is empty."); 1493 for (const_iterator I = begin() + 1, E = end(); I != E; ++I) { 1494 const DSAInfo &Data = I->SharingMap.lookup(D); 1495 if (Data.Attributes != OMPC_reduction || 1496 Data.Modifier != OMPC_REDUCTION_task) 1497 continue; 1498 const ReductionData &ReductionData = I->ReductionMap.lookup(D); 1499 if (!ReductionData.ReductionOp || 1500 !ReductionData.ReductionOp.is<const Expr *>()) 1501 return DSAVarData(); 1502 SR = ReductionData.ReductionRange; 1503 ReductionRef = ReductionData.ReductionOp.get<const Expr *>(); 1504 assert(I->TaskgroupReductionRef && "taskgroup reduction reference " 1505 "expression for the descriptor is not " 1506 "set."); 1507 TaskgroupDescriptor = I->TaskgroupReductionRef; 1508 return DSAVarData(I->Directive, OMPC_reduction, Data.RefExpr.getPointer(), 1509 Data.PrivateCopy, I->DefaultAttrLoc, OMPC_REDUCTION_task); 1510 } 1511 return DSAVarData(); 1512 } 1513 1514 bool DSAStackTy::isOpenMPLocal(VarDecl *D, const_iterator I) const { 1515 D = D->getCanonicalDecl(); 1516 for (const_iterator E = end(); I != E; ++I) { 1517 if (isImplicitOrExplicitTaskingRegion(I->Directive) || 1518 isOpenMPTargetExecutionDirective(I->Directive)) { 1519 Scope *TopScope = I->CurScope ? I->CurScope->getParent() : nullptr; 1520 Scope *CurScope = getCurScope(); 1521 while (CurScope && CurScope != TopScope && !CurScope->isDeclScope(D)) 1522 CurScope = CurScope->getParent(); 1523 return CurScope != TopScope; 1524 } 1525 } 1526 return false; 1527 } 1528 1529 static bool isConstNotMutableType(Sema &SemaRef, QualType Type, 1530 bool AcceptIfMutable = true, 1531 bool *IsClassType = nullptr) { 1532 ASTContext &Context = SemaRef.getASTContext(); 1533 Type = Type.getNonReferenceType().getCanonicalType(); 1534 bool IsConstant = Type.isConstant(Context); 1535 Type = Context.getBaseElementType(Type); 1536 const CXXRecordDecl *RD = AcceptIfMutable && SemaRef.getLangOpts().CPlusPlus 1537 ? Type->getAsCXXRecordDecl() 1538 : nullptr; 1539 if (const auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD)) 1540 if (const ClassTemplateDecl *CTD = CTSD->getSpecializedTemplate()) 1541 RD = CTD->getTemplatedDecl(); 1542 if (IsClassType) 1543 *IsClassType = RD; 1544 return IsConstant && !(SemaRef.getLangOpts().CPlusPlus && RD && 1545 RD->hasDefinition() && RD->hasMutableFields()); 1546 } 1547 1548 static bool rejectConstNotMutableType(Sema &SemaRef, const ValueDecl *D, 1549 QualType Type, OpenMPClauseKind CKind, 1550 SourceLocation ELoc, 1551 bool AcceptIfMutable = true, 1552 bool ListItemNotVar = false) { 1553 ASTContext &Context = SemaRef.getASTContext(); 1554 bool IsClassType; 1555 if (isConstNotMutableType(SemaRef, Type, AcceptIfMutable, &IsClassType)) { 1556 unsigned Diag = ListItemNotVar 1557 ? diag::err_omp_const_list_item 1558 : IsClassType ? diag::err_omp_const_not_mutable_variable 1559 : diag::err_omp_const_variable; 1560 SemaRef.Diag(ELoc, Diag) << getOpenMPClauseName(CKind); 1561 if (!ListItemNotVar && D) { 1562 const VarDecl *VD = dyn_cast<VarDecl>(D); 1563 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 1564 VarDecl::DeclarationOnly; 1565 SemaRef.Diag(D->getLocation(), 1566 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1567 << D; 1568 } 1569 return true; 1570 } 1571 return false; 1572 } 1573 1574 const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, 1575 bool FromParent) { 1576 D = getCanonicalDecl(D); 1577 DSAVarData DVar; 1578 1579 auto *VD = dyn_cast<VarDecl>(D); 1580 auto TI = Threadprivates.find(D); 1581 if (TI != Threadprivates.end()) { 1582 DVar.RefExpr = TI->getSecond().RefExpr.getPointer(); 1583 DVar.CKind = OMPC_threadprivate; 1584 DVar.Modifier = TI->getSecond().Modifier; 1585 return DVar; 1586 } 1587 if (VD && VD->hasAttr<OMPThreadPrivateDeclAttr>()) { 1588 DVar.RefExpr = buildDeclRefExpr( 1589 SemaRef, VD, D->getType().getNonReferenceType(), 1590 VD->getAttr<OMPThreadPrivateDeclAttr>()->getLocation()); 1591 DVar.CKind = OMPC_threadprivate; 1592 addDSA(D, DVar.RefExpr, OMPC_threadprivate); 1593 return DVar; 1594 } 1595 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1596 // in a Construct, C/C++, predetermined, p.1] 1597 // Variables appearing in threadprivate directives are threadprivate. 1598 if ((VD && VD->getTLSKind() != VarDecl::TLS_None && 1599 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() && 1600 SemaRef.getLangOpts().OpenMPUseTLS && 1601 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) || 1602 (VD && VD->getStorageClass() == SC_Register && 1603 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) { 1604 DVar.RefExpr = buildDeclRefExpr( 1605 SemaRef, VD, D->getType().getNonReferenceType(), D->getLocation()); 1606 DVar.CKind = OMPC_threadprivate; 1607 addDSA(D, DVar.RefExpr, OMPC_threadprivate); 1608 return DVar; 1609 } 1610 if (SemaRef.getLangOpts().OpenMPCUDAMode && VD && 1611 VD->isLocalVarDeclOrParm() && !isStackEmpty() && 1612 !isLoopControlVariable(D).first) { 1613 const_iterator IterTarget = 1614 std::find_if(begin(), end(), [](const SharingMapTy &Data) { 1615 return isOpenMPTargetExecutionDirective(Data.Directive); 1616 }); 1617 if (IterTarget != end()) { 1618 const_iterator ParentIterTarget = IterTarget + 1; 1619 for (const_iterator Iter = begin(); 1620 Iter != ParentIterTarget; ++Iter) { 1621 if (isOpenMPLocal(VD, Iter)) { 1622 DVar.RefExpr = 1623 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(), 1624 D->getLocation()); 1625 DVar.CKind = OMPC_threadprivate; 1626 return DVar; 1627 } 1628 } 1629 if (!isClauseParsingMode() || IterTarget != begin()) { 1630 auto DSAIter = IterTarget->SharingMap.find(D); 1631 if (DSAIter != IterTarget->SharingMap.end() && 1632 isOpenMPPrivate(DSAIter->getSecond().Attributes)) { 1633 DVar.RefExpr = DSAIter->getSecond().RefExpr.getPointer(); 1634 DVar.CKind = OMPC_threadprivate; 1635 return DVar; 1636 } 1637 const_iterator End = end(); 1638 if (!SemaRef.isOpenMPCapturedByRef( 1639 D, std::distance(ParentIterTarget, End), 1640 /*OpenMPCaptureLevel=*/0)) { 1641 DVar.RefExpr = 1642 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(), 1643 IterTarget->ConstructLoc); 1644 DVar.CKind = OMPC_threadprivate; 1645 return DVar; 1646 } 1647 } 1648 } 1649 } 1650 1651 if (isStackEmpty()) 1652 // Not in OpenMP execution region and top scope was already checked. 1653 return DVar; 1654 1655 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1656 // in a Construct, C/C++, predetermined, p.4] 1657 // Static data members are shared. 1658 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1659 // in a Construct, C/C++, predetermined, p.7] 1660 // Variables with static storage duration that are declared in a scope 1661 // inside the construct are shared. 1662 if (VD && VD->isStaticDataMember()) { 1663 // Check for explicitly specified attributes. 1664 const_iterator I = begin(); 1665 const_iterator EndI = end(); 1666 if (FromParent && I != EndI) 1667 ++I; 1668 if (I != EndI) { 1669 auto It = I->SharingMap.find(D); 1670 if (It != I->SharingMap.end()) { 1671 const DSAInfo &Data = It->getSecond(); 1672 DVar.RefExpr = Data.RefExpr.getPointer(); 1673 DVar.PrivateCopy = Data.PrivateCopy; 1674 DVar.CKind = Data.Attributes; 1675 DVar.ImplicitDSALoc = I->DefaultAttrLoc; 1676 DVar.DKind = I->Directive; 1677 DVar.Modifier = Data.Modifier; 1678 return DVar; 1679 } 1680 } 1681 1682 DVar.CKind = OMPC_shared; 1683 return DVar; 1684 } 1685 1686 auto &&MatchesAlways = [](OpenMPDirectiveKind) { return true; }; 1687 // The predetermined shared attribute for const-qualified types having no 1688 // mutable members was removed after OpenMP 3.1. 1689 if (SemaRef.LangOpts.OpenMP <= 31) { 1690 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1691 // in a Construct, C/C++, predetermined, p.6] 1692 // Variables with const qualified type having no mutable member are 1693 // shared. 1694 if (isConstNotMutableType(SemaRef, D->getType())) { 1695 // Variables with const-qualified type having no mutable member may be 1696 // listed in a firstprivate clause, even if they are static data members. 1697 DSAVarData DVarTemp = hasInnermostDSA( 1698 D, 1699 [](OpenMPClauseKind C) { 1700 return C == OMPC_firstprivate || C == OMPC_shared; 1701 }, 1702 MatchesAlways, FromParent); 1703 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr) 1704 return DVarTemp; 1705 1706 DVar.CKind = OMPC_shared; 1707 return DVar; 1708 } 1709 } 1710 1711 // Explicitly specified attributes and local variables with predetermined 1712 // attributes. 1713 const_iterator I = begin(); 1714 const_iterator EndI = end(); 1715 if (FromParent && I != EndI) 1716 ++I; 1717 if (I == EndI) 1718 return DVar; 1719 auto It = I->SharingMap.find(D); 1720 if (It != I->SharingMap.end()) { 1721 const DSAInfo &Data = It->getSecond(); 1722 DVar.RefExpr = Data.RefExpr.getPointer(); 1723 DVar.PrivateCopy = Data.PrivateCopy; 1724 DVar.CKind = Data.Attributes; 1725 DVar.ImplicitDSALoc = I->DefaultAttrLoc; 1726 DVar.DKind = I->Directive; 1727 DVar.Modifier = Data.Modifier; 1728 } 1729 1730 return DVar; 1731 } 1732 1733 const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D, 1734 bool FromParent) const { 1735 if (isStackEmpty()) { 1736 const_iterator I; 1737 return getDSA(I, D); 1738 } 1739 D = getCanonicalDecl(D); 1740 const_iterator StartI = begin(); 1741 const_iterator EndI = end(); 1742 if (FromParent && StartI != EndI) 1743 ++StartI; 1744 return getDSA(StartI, D); 1745 } 1746 1747 const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D, 1748 unsigned Level) const { 1749 if (getStackSize() <= Level) 1750 return DSAVarData(); 1751 D = getCanonicalDecl(D); 1752 const_iterator StartI = std::next(begin(), getStackSize() - 1 - Level); 1753 return getDSA(StartI, D); 1754 } 1755 1756 const DSAStackTy::DSAVarData 1757 DSAStackTy::hasDSA(ValueDecl *D, 1758 const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 1759 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1760 bool FromParent) const { 1761 if (isStackEmpty()) 1762 return {}; 1763 D = getCanonicalDecl(D); 1764 const_iterator I = begin(); 1765 const_iterator EndI = end(); 1766 if (FromParent && I != EndI) 1767 ++I; 1768 for (; I != EndI; ++I) { 1769 if (!DPred(I->Directive) && 1770 !isImplicitOrExplicitTaskingRegion(I->Directive)) 1771 continue; 1772 const_iterator NewI = I; 1773 DSAVarData DVar = getDSA(NewI, D); 1774 if (I == NewI && CPred(DVar.CKind)) 1775 return DVar; 1776 } 1777 return {}; 1778 } 1779 1780 const DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA( 1781 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 1782 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1783 bool FromParent) const { 1784 if (isStackEmpty()) 1785 return {}; 1786 D = getCanonicalDecl(D); 1787 const_iterator StartI = begin(); 1788 const_iterator EndI = end(); 1789 if (FromParent && StartI != EndI) 1790 ++StartI; 1791 if (StartI == EndI || !DPred(StartI->Directive)) 1792 return {}; 1793 const_iterator NewI = StartI; 1794 DSAVarData DVar = getDSA(NewI, D); 1795 return (NewI == StartI && CPred(DVar.CKind)) ? DVar : DSAVarData(); 1796 } 1797 1798 bool DSAStackTy::hasExplicitDSA( 1799 const ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 1800 unsigned Level, bool NotLastprivate) const { 1801 if (getStackSize() <= Level) 1802 return false; 1803 D = getCanonicalDecl(D); 1804 const SharingMapTy &StackElem = getStackElemAtLevel(Level); 1805 auto I = StackElem.SharingMap.find(D); 1806 if (I != StackElem.SharingMap.end() && 1807 I->getSecond().RefExpr.getPointer() && 1808 CPred(I->getSecond().Attributes) && 1809 (!NotLastprivate || !I->getSecond().RefExpr.getInt())) 1810 return true; 1811 // Check predetermined rules for the loop control variables. 1812 auto LI = StackElem.LCVMap.find(D); 1813 if (LI != StackElem.LCVMap.end()) 1814 return CPred(OMPC_private); 1815 return false; 1816 } 1817 1818 bool DSAStackTy::hasExplicitDirective( 1819 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1820 unsigned Level) const { 1821 if (getStackSize() <= Level) 1822 return false; 1823 const SharingMapTy &StackElem = getStackElemAtLevel(Level); 1824 return DPred(StackElem.Directive); 1825 } 1826 1827 bool DSAStackTy::hasDirective( 1828 const llvm::function_ref<bool(OpenMPDirectiveKind, 1829 const DeclarationNameInfo &, SourceLocation)> 1830 DPred, 1831 bool FromParent) const { 1832 // We look only in the enclosing region. 1833 size_t Skip = FromParent ? 2 : 1; 1834 for (const_iterator I = begin() + std::min(Skip, getStackSize()), E = end(); 1835 I != E; ++I) { 1836 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc)) 1837 return true; 1838 } 1839 return false; 1840 } 1841 1842 void Sema::InitDataSharingAttributesStack() { 1843 VarDataSharingAttributesStack = new DSAStackTy(*this); 1844 } 1845 1846 #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack) 1847 1848 void Sema::pushOpenMPFunctionRegion() { 1849 DSAStack->pushFunction(); 1850 } 1851 1852 void Sema::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) { 1853 DSAStack->popFunction(OldFSI); 1854 } 1855 1856 static bool isOpenMPDeviceDelayedContext(Sema &S) { 1857 assert(S.LangOpts.OpenMP && S.LangOpts.OpenMPIsDevice && 1858 "Expected OpenMP device compilation."); 1859 return !S.isInOpenMPTargetExecutionDirective() && 1860 !S.isInOpenMPDeclareTargetContext(); 1861 } 1862 1863 namespace { 1864 /// Status of the function emission on the host/device. 1865 enum class FunctionEmissionStatus { 1866 Emitted, 1867 Discarded, 1868 Unknown, 1869 }; 1870 } // anonymous namespace 1871 1872 Sema::DeviceDiagBuilder Sema::diagIfOpenMPDeviceCode(SourceLocation Loc, 1873 unsigned DiagID) { 1874 assert(LangOpts.OpenMP && LangOpts.OpenMPIsDevice && 1875 "Expected OpenMP device compilation."); 1876 1877 FunctionDecl *FD = getCurFunctionDecl(); 1878 DeviceDiagBuilder::Kind Kind = DeviceDiagBuilder::K_Nop; 1879 if (FD) { 1880 FunctionEmissionStatus FES = getEmissionStatus(FD); 1881 switch (FES) { 1882 case FunctionEmissionStatus::Emitted: 1883 Kind = DeviceDiagBuilder::K_Immediate; 1884 break; 1885 case FunctionEmissionStatus::Unknown: 1886 Kind = isOpenMPDeviceDelayedContext(*this) 1887 ? DeviceDiagBuilder::K_Deferred 1888 : DeviceDiagBuilder::K_Immediate; 1889 break; 1890 case FunctionEmissionStatus::TemplateDiscarded: 1891 case FunctionEmissionStatus::OMPDiscarded: 1892 Kind = DeviceDiagBuilder::K_Nop; 1893 break; 1894 case FunctionEmissionStatus::CUDADiscarded: 1895 llvm_unreachable("CUDADiscarded unexpected in OpenMP device compilation"); 1896 break; 1897 } 1898 } 1899 1900 return DeviceDiagBuilder(Kind, Loc, DiagID, getCurFunctionDecl(), *this); 1901 } 1902 1903 Sema::DeviceDiagBuilder Sema::diagIfOpenMPHostCode(SourceLocation Loc, 1904 unsigned DiagID) { 1905 assert(LangOpts.OpenMP && !LangOpts.OpenMPIsDevice && 1906 "Expected OpenMP host compilation."); 1907 FunctionEmissionStatus FES = getEmissionStatus(getCurFunctionDecl()); 1908 DeviceDiagBuilder::Kind Kind = DeviceDiagBuilder::K_Nop; 1909 switch (FES) { 1910 case FunctionEmissionStatus::Emitted: 1911 Kind = DeviceDiagBuilder::K_Immediate; 1912 break; 1913 case FunctionEmissionStatus::Unknown: 1914 Kind = DeviceDiagBuilder::K_Deferred; 1915 break; 1916 case FunctionEmissionStatus::TemplateDiscarded: 1917 case FunctionEmissionStatus::OMPDiscarded: 1918 case FunctionEmissionStatus::CUDADiscarded: 1919 Kind = DeviceDiagBuilder::K_Nop; 1920 break; 1921 } 1922 1923 return DeviceDiagBuilder(Kind, Loc, DiagID, getCurFunctionDecl(), *this); 1924 } 1925 1926 static OpenMPDefaultmapClauseKind 1927 getVariableCategoryFromDecl(const LangOptions &LO, const ValueDecl *VD) { 1928 if (LO.OpenMP <= 45) { 1929 if (VD->getType().getNonReferenceType()->isScalarType()) 1930 return OMPC_DEFAULTMAP_scalar; 1931 return OMPC_DEFAULTMAP_aggregate; 1932 } 1933 if (VD->getType().getNonReferenceType()->isAnyPointerType()) 1934 return OMPC_DEFAULTMAP_pointer; 1935 if (VD->getType().getNonReferenceType()->isScalarType()) 1936 return OMPC_DEFAULTMAP_scalar; 1937 return OMPC_DEFAULTMAP_aggregate; 1938 } 1939 1940 bool Sema::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level, 1941 unsigned OpenMPCaptureLevel) const { 1942 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1943 1944 ASTContext &Ctx = getASTContext(); 1945 bool IsByRef = true; 1946 1947 // Find the directive that is associated with the provided scope. 1948 D = cast<ValueDecl>(D->getCanonicalDecl()); 1949 QualType Ty = D->getType(); 1950 1951 bool IsVariableUsedInMapClause = false; 1952 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) { 1953 // This table summarizes how a given variable should be passed to the device 1954 // given its type and the clauses where it appears. This table is based on 1955 // the description in OpenMP 4.5 [2.10.4, target Construct] and 1956 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses]. 1957 // 1958 // ========================================================================= 1959 // | type | defaultmap | pvt | first | is_device_ptr | map | res. | 1960 // | |(tofrom:scalar)| | pvt | | | | 1961 // ========================================================================= 1962 // | scl | | | | - | | bycopy| 1963 // | scl | | - | x | - | - | bycopy| 1964 // | scl | | x | - | - | - | null | 1965 // | scl | x | | | - | | byref | 1966 // | scl | x | - | x | - | - | bycopy| 1967 // | scl | x | x | - | - | - | null | 1968 // | scl | | - | - | - | x | byref | 1969 // | scl | x | - | - | - | x | byref | 1970 // 1971 // | agg | n.a. | | | - | | byref | 1972 // | agg | n.a. | - | x | - | - | byref | 1973 // | agg | n.a. | x | - | - | - | null | 1974 // | agg | n.a. | - | - | - | x | byref | 1975 // | agg | n.a. | - | - | - | x[] | byref | 1976 // 1977 // | ptr | n.a. | | | - | | bycopy| 1978 // | ptr | n.a. | - | x | - | - | bycopy| 1979 // | ptr | n.a. | x | - | - | - | null | 1980 // | ptr | n.a. | - | - | - | x | byref | 1981 // | ptr | n.a. | - | - | - | x[] | bycopy| 1982 // | ptr | n.a. | - | - | x | | bycopy| 1983 // | ptr | n.a. | - | - | x | x | bycopy| 1984 // | ptr | n.a. | - | - | x | x[] | bycopy| 1985 // ========================================================================= 1986 // Legend: 1987 // scl - scalar 1988 // ptr - pointer 1989 // agg - aggregate 1990 // x - applies 1991 // - - invalid in this combination 1992 // [] - mapped with an array section 1993 // byref - should be mapped by reference 1994 // byval - should be mapped by value 1995 // null - initialize a local variable to null on the device 1996 // 1997 // Observations: 1998 // - All scalar declarations that show up in a map clause have to be passed 1999 // by reference, because they may have been mapped in the enclosing data 2000 // environment. 2001 // - If the scalar value does not fit the size of uintptr, it has to be 2002 // passed by reference, regardless the result in the table above. 2003 // - For pointers mapped by value that have either an implicit map or an 2004 // array section, the runtime library may pass the NULL value to the 2005 // device instead of the value passed to it by the compiler. 2006 2007 if (Ty->isReferenceType()) 2008 Ty = Ty->castAs<ReferenceType>()->getPointeeType(); 2009 2010 // Locate map clauses and see if the variable being captured is referred to 2011 // in any of those clauses. Here we only care about variables, not fields, 2012 // because fields are part of aggregates. 2013 bool IsVariableAssociatedWithSection = false; 2014 2015 DSAStack->checkMappableExprComponentListsForDeclAtLevel( 2016 D, Level, 2017 [&IsVariableUsedInMapClause, &IsVariableAssociatedWithSection, D]( 2018 OMPClauseMappableExprCommon::MappableExprComponentListRef 2019 MapExprComponents, 2020 OpenMPClauseKind WhereFoundClauseKind) { 2021 // Only the map clause information influences how a variable is 2022 // captured. E.g. is_device_ptr does not require changing the default 2023 // behavior. 2024 if (WhereFoundClauseKind != OMPC_map) 2025 return false; 2026 2027 auto EI = MapExprComponents.rbegin(); 2028 auto EE = MapExprComponents.rend(); 2029 2030 assert(EI != EE && "Invalid map expression!"); 2031 2032 if (isa<DeclRefExpr>(EI->getAssociatedExpression())) 2033 IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D; 2034 2035 ++EI; 2036 if (EI == EE) 2037 return false; 2038 2039 if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) || 2040 isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) || 2041 isa<MemberExpr>(EI->getAssociatedExpression()) || 2042 isa<OMPArrayShapingExpr>(EI->getAssociatedExpression())) { 2043 IsVariableAssociatedWithSection = true; 2044 // There is nothing more we need to know about this variable. 2045 return true; 2046 } 2047 2048 // Keep looking for more map info. 2049 return false; 2050 }); 2051 2052 if (IsVariableUsedInMapClause) { 2053 // If variable is identified in a map clause it is always captured by 2054 // reference except if it is a pointer that is dereferenced somehow. 2055 IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection); 2056 } else { 2057 // By default, all the data that has a scalar type is mapped by copy 2058 // (except for reduction variables). 2059 // Defaultmap scalar is mutual exclusive to defaultmap pointer 2060 IsByRef = 2061 (DSAStack->isForceCaptureByReferenceInTargetExecutable() && 2062 !Ty->isAnyPointerType()) || 2063 !Ty->isScalarType() || 2064 DSAStack->isDefaultmapCapturedByRef( 2065 Level, getVariableCategoryFromDecl(LangOpts, D)) || 2066 DSAStack->hasExplicitDSA( 2067 D, [](OpenMPClauseKind K) { return K == OMPC_reduction; }, Level); 2068 } 2069 } 2070 2071 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) { 2072 IsByRef = 2073 ((IsVariableUsedInMapClause && 2074 DSAStack->getCaptureRegion(Level, OpenMPCaptureLevel) == 2075 OMPD_target) || 2076 !(DSAStack->hasExplicitDSA( 2077 D, 2078 [](OpenMPClauseKind K) -> bool { 2079 return K == OMPC_firstprivate; 2080 }, 2081 Level, /*NotLastprivate=*/true) || 2082 DSAStack->isUsesAllocatorsDecl(Level, D))) && 2083 // If the variable is artificial and must be captured by value - try to 2084 // capture by value. 2085 !(isa<OMPCapturedExprDecl>(D) && !D->hasAttr<OMPCaptureNoInitAttr>() && 2086 !cast<OMPCapturedExprDecl>(D)->getInit()->isGLValue()) && 2087 // If the variable is implicitly firstprivate and scalar - capture by 2088 // copy 2089 !(DSAStack->getDefaultDSA() == DSA_firstprivate && 2090 !DSAStack->hasExplicitDSA( 2091 D, [](OpenMPClauseKind K) { return K != OMPC_unknown; }, Level) && 2092 !DSAStack->isLoopControlVariable(D, Level).first); 2093 } 2094 2095 // When passing data by copy, we need to make sure it fits the uintptr size 2096 // and alignment, because the runtime library only deals with uintptr types. 2097 // If it does not fit the uintptr size, we need to pass the data by reference 2098 // instead. 2099 if (!IsByRef && 2100 (Ctx.getTypeSizeInChars(Ty) > 2101 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) || 2102 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) { 2103 IsByRef = true; 2104 } 2105 2106 return IsByRef; 2107 } 2108 2109 unsigned Sema::getOpenMPNestingLevel() const { 2110 assert(getLangOpts().OpenMP); 2111 return DSAStack->getNestingLevel(); 2112 } 2113 2114 bool Sema::isInOpenMPTargetExecutionDirective() const { 2115 return (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) && 2116 !DSAStack->isClauseParsingMode()) || 2117 DSAStack->hasDirective( 2118 [](OpenMPDirectiveKind K, const DeclarationNameInfo &, 2119 SourceLocation) -> bool { 2120 return isOpenMPTargetExecutionDirective(K); 2121 }, 2122 false); 2123 } 2124 2125 VarDecl *Sema::isOpenMPCapturedDecl(ValueDecl *D, bool CheckScopeInfo, 2126 unsigned StopAt) { 2127 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 2128 D = getCanonicalDecl(D); 2129 2130 auto *VD = dyn_cast<VarDecl>(D); 2131 // Do not capture constexpr variables. 2132 if (VD && VD->isConstexpr()) 2133 return nullptr; 2134 2135 // If we want to determine whether the variable should be captured from the 2136 // perspective of the current capturing scope, and we've already left all the 2137 // capturing scopes of the top directive on the stack, check from the 2138 // perspective of its parent directive (if any) instead. 2139 DSAStackTy::ParentDirectiveScope InParentDirectiveRAII( 2140 *DSAStack, CheckScopeInfo && DSAStack->isBodyComplete()); 2141 2142 // If we are attempting to capture a global variable in a directive with 2143 // 'target' we return true so that this global is also mapped to the device. 2144 // 2145 if (VD && !VD->hasLocalStorage() && 2146 (getCurCapturedRegion() || getCurBlock() || getCurLambda())) { 2147 if (isInOpenMPDeclareTargetContext()) { 2148 // Try to mark variable as declare target if it is used in capturing 2149 // regions. 2150 if (LangOpts.OpenMP <= 45 && 2151 !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) 2152 checkDeclIsAllowedInOpenMPTarget(nullptr, VD); 2153 return nullptr; 2154 } else if (isInOpenMPTargetExecutionDirective()) { 2155 // If the declaration is enclosed in a 'declare target' directive, 2156 // then it should not be captured. 2157 // 2158 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) 2159 return nullptr; 2160 CapturedRegionScopeInfo *CSI = nullptr; 2161 for (FunctionScopeInfo *FSI : llvm::drop_begin( 2162 llvm::reverse(FunctionScopes), 2163 CheckScopeInfo ? (FunctionScopes.size() - (StopAt + 1)) : 0)) { 2164 if (!isa<CapturingScopeInfo>(FSI)) 2165 return nullptr; 2166 if (auto *RSI = dyn_cast<CapturedRegionScopeInfo>(FSI)) 2167 if (RSI->CapRegionKind == CR_OpenMP) { 2168 CSI = RSI; 2169 break; 2170 } 2171 } 2172 SmallVector<OpenMPDirectiveKind, 4> Regions; 2173 getOpenMPCaptureRegions(Regions, 2174 DSAStack->getDirective(CSI->OpenMPLevel)); 2175 if (Regions[CSI->OpenMPCaptureLevel] != OMPD_task) 2176 return VD; 2177 } 2178 } 2179 2180 if (CheckScopeInfo) { 2181 bool OpenMPFound = false; 2182 for (unsigned I = StopAt + 1; I > 0; --I) { 2183 FunctionScopeInfo *FSI = FunctionScopes[I - 1]; 2184 if(!isa<CapturingScopeInfo>(FSI)) 2185 return nullptr; 2186 if (auto *RSI = dyn_cast<CapturedRegionScopeInfo>(FSI)) 2187 if (RSI->CapRegionKind == CR_OpenMP) { 2188 OpenMPFound = true; 2189 break; 2190 } 2191 } 2192 if (!OpenMPFound) 2193 return nullptr; 2194 } 2195 2196 if (DSAStack->getCurrentDirective() != OMPD_unknown && 2197 (!DSAStack->isClauseParsingMode() || 2198 DSAStack->getParentDirective() != OMPD_unknown)) { 2199 auto &&Info = DSAStack->isLoopControlVariable(D); 2200 if (Info.first || 2201 (VD && VD->hasLocalStorage() && 2202 isImplicitOrExplicitTaskingRegion(DSAStack->getCurrentDirective())) || 2203 (VD && DSAStack->isForceVarCapturing())) 2204 return VD ? VD : Info.second; 2205 DSAStackTy::DSAVarData DVarTop = 2206 DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode()); 2207 if (DVarTop.CKind != OMPC_unknown && isOpenMPPrivate(DVarTop.CKind)) 2208 return VD ? VD : cast<VarDecl>(DVarTop.PrivateCopy->getDecl()); 2209 // Threadprivate variables must not be captured. 2210 if (isOpenMPThreadPrivate(DVarTop.CKind)) 2211 return nullptr; 2212 // The variable is not private or it is the variable in the directive with 2213 // default(none) clause and not used in any clause. 2214 DSAStackTy::DSAVarData DVarPrivate = DSAStack->hasDSA( 2215 D, isOpenMPPrivate, [](OpenMPDirectiveKind) { return true; }, 2216 DSAStack->isClauseParsingMode()); 2217 // Global shared must not be captured. 2218 if (VD && !VD->hasLocalStorage() && DVarPrivate.CKind == OMPC_unknown && 2219 ((DSAStack->getDefaultDSA() != DSA_none && 2220 DSAStack->getDefaultDSA() != DSA_firstprivate) || 2221 DVarTop.CKind == OMPC_shared)) 2222 return nullptr; 2223 if (DVarPrivate.CKind != OMPC_unknown || 2224 (VD && (DSAStack->getDefaultDSA() == DSA_none || 2225 DSAStack->getDefaultDSA() == DSA_firstprivate))) 2226 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl()); 2227 } 2228 return nullptr; 2229 } 2230 2231 void Sema::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex, 2232 unsigned Level) const { 2233 FunctionScopesIndex -= getOpenMPCaptureLevels(DSAStack->getDirective(Level)); 2234 } 2235 2236 void Sema::startOpenMPLoop() { 2237 assert(LangOpts.OpenMP && "OpenMP must be enabled."); 2238 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) 2239 DSAStack->loopInit(); 2240 } 2241 2242 void Sema::startOpenMPCXXRangeFor() { 2243 assert(LangOpts.OpenMP && "OpenMP must be enabled."); 2244 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { 2245 DSAStack->resetPossibleLoopCounter(); 2246 DSAStack->loopStart(); 2247 } 2248 } 2249 2250 OpenMPClauseKind Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level, 2251 unsigned CapLevel) const { 2252 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 2253 if (DSAStack->hasExplicitDirective( 2254 [](OpenMPDirectiveKind K) { return isOpenMPTaskingDirective(K); }, 2255 Level)) { 2256 bool IsTriviallyCopyable = 2257 D->getType().getNonReferenceType().isTriviallyCopyableType(Context) && 2258 !D->getType() 2259 .getNonReferenceType() 2260 .getCanonicalType() 2261 ->getAsCXXRecordDecl(); 2262 OpenMPDirectiveKind DKind = DSAStack->getDirective(Level); 2263 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 2264 getOpenMPCaptureRegions(CaptureRegions, DKind); 2265 if (isOpenMPTaskingDirective(CaptureRegions[CapLevel]) && 2266 (IsTriviallyCopyable || 2267 !isOpenMPTaskLoopDirective(CaptureRegions[CapLevel]))) { 2268 if (DSAStack->hasExplicitDSA( 2269 D, [](OpenMPClauseKind K) { return K == OMPC_firstprivate; }, 2270 Level, /*NotLastprivate=*/true)) 2271 return OMPC_firstprivate; 2272 DSAStackTy::DSAVarData DVar = DSAStack->getImplicitDSA(D, Level); 2273 if (DVar.CKind != OMPC_shared && 2274 !DSAStack->isLoopControlVariable(D, Level).first && !DVar.RefExpr) { 2275 DSAStack->addImplicitTaskFirstprivate(Level, D); 2276 return OMPC_firstprivate; 2277 } 2278 } 2279 } 2280 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { 2281 if (DSAStack->getAssociatedLoops() > 0 && 2282 !DSAStack->isLoopStarted()) { 2283 DSAStack->resetPossibleLoopCounter(D); 2284 DSAStack->loopStart(); 2285 return OMPC_private; 2286 } 2287 if ((DSAStack->getPossiblyLoopCunter() == D->getCanonicalDecl() || 2288 DSAStack->isLoopControlVariable(D).first) && 2289 !DSAStack->hasExplicitDSA( 2290 D, [](OpenMPClauseKind K) { return K != OMPC_private; }, Level) && 2291 !isOpenMPSimdDirective(DSAStack->getCurrentDirective())) 2292 return OMPC_private; 2293 } 2294 if (const auto *VD = dyn_cast<VarDecl>(D)) { 2295 if (DSAStack->isThreadPrivate(const_cast<VarDecl *>(VD)) && 2296 DSAStack->isForceVarCapturing() && 2297 !DSAStack->hasExplicitDSA( 2298 D, [](OpenMPClauseKind K) { return K == OMPC_copyin; }, Level)) 2299 return OMPC_private; 2300 } 2301 // User-defined allocators are private since they must be defined in the 2302 // context of target region. 2303 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level) && 2304 DSAStack->isUsesAllocatorsDecl(Level, D).getValueOr( 2305 DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait) == 2306 DSAStackTy::UsesAllocatorsDeclKind::UserDefinedAllocator) 2307 return OMPC_private; 2308 return (DSAStack->hasExplicitDSA( 2309 D, [](OpenMPClauseKind K) { return K == OMPC_private; }, Level) || 2310 (DSAStack->isClauseParsingMode() && 2311 DSAStack->getClauseParsingMode() == OMPC_private) || 2312 // Consider taskgroup reduction descriptor variable a private 2313 // to avoid possible capture in the region. 2314 (DSAStack->hasExplicitDirective( 2315 [](OpenMPDirectiveKind K) { 2316 return K == OMPD_taskgroup || 2317 ((isOpenMPParallelDirective(K) || 2318 isOpenMPWorksharingDirective(K)) && 2319 !isOpenMPSimdDirective(K)); 2320 }, 2321 Level) && 2322 DSAStack->isTaskgroupReductionRef(D, Level))) 2323 ? OMPC_private 2324 : OMPC_unknown; 2325 } 2326 2327 void Sema::setOpenMPCaptureKind(FieldDecl *FD, const ValueDecl *D, 2328 unsigned Level) { 2329 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 2330 D = getCanonicalDecl(D); 2331 OpenMPClauseKind OMPC = OMPC_unknown; 2332 for (unsigned I = DSAStack->getNestingLevel() + 1; I > Level; --I) { 2333 const unsigned NewLevel = I - 1; 2334 if (DSAStack->hasExplicitDSA(D, 2335 [&OMPC](const OpenMPClauseKind K) { 2336 if (isOpenMPPrivate(K)) { 2337 OMPC = K; 2338 return true; 2339 } 2340 return false; 2341 }, 2342 NewLevel)) 2343 break; 2344 if (DSAStack->checkMappableExprComponentListsForDeclAtLevel( 2345 D, NewLevel, 2346 [](OMPClauseMappableExprCommon::MappableExprComponentListRef, 2347 OpenMPClauseKind) { return true; })) { 2348 OMPC = OMPC_map; 2349 break; 2350 } 2351 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, 2352 NewLevel)) { 2353 OMPC = OMPC_map; 2354 if (DSAStack->mustBeFirstprivateAtLevel( 2355 NewLevel, getVariableCategoryFromDecl(LangOpts, D))) 2356 OMPC = OMPC_firstprivate; 2357 break; 2358 } 2359 } 2360 if (OMPC != OMPC_unknown) 2361 FD->addAttr(OMPCaptureKindAttr::CreateImplicit(Context, unsigned(OMPC))); 2362 } 2363 2364 bool Sema::isOpenMPTargetCapturedDecl(const ValueDecl *D, unsigned Level, 2365 unsigned CaptureLevel) const { 2366 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 2367 // Return true if the current level is no longer enclosed in a target region. 2368 2369 SmallVector<OpenMPDirectiveKind, 4> Regions; 2370 getOpenMPCaptureRegions(Regions, DSAStack->getDirective(Level)); 2371 const auto *VD = dyn_cast<VarDecl>(D); 2372 return VD && !VD->hasLocalStorage() && 2373 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, 2374 Level) && 2375 Regions[CaptureLevel] != OMPD_task; 2376 } 2377 2378 bool Sema::isOpenMPGlobalCapturedDecl(ValueDecl *D, unsigned Level, 2379 unsigned CaptureLevel) const { 2380 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 2381 // Return true if the current level is no longer enclosed in a target region. 2382 2383 if (const auto *VD = dyn_cast<VarDecl>(D)) { 2384 if (!VD->hasLocalStorage()) { 2385 DSAStackTy::DSAVarData TopDVar = 2386 DSAStack->getTopDSA(D, /*FromParent=*/false); 2387 unsigned NumLevels = 2388 getOpenMPCaptureLevels(DSAStack->getDirective(Level)); 2389 if (Level == 0) 2390 return (NumLevels == CaptureLevel + 1) && TopDVar.CKind != OMPC_shared; 2391 DSAStackTy::DSAVarData DVar = DSAStack->getImplicitDSA(D, Level - 1); 2392 return DVar.CKind != OMPC_shared || 2393 isOpenMPGlobalCapturedDecl( 2394 D, Level - 1, 2395 getOpenMPCaptureLevels(DSAStack->getDirective(Level - 1)) - 1); 2396 } 2397 } 2398 return true; 2399 } 2400 2401 void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; } 2402 2403 void Sema::ActOnOpenMPBeginDeclareVariant(SourceLocation Loc, 2404 OMPTraitInfo &TI) { 2405 if (!OMPDeclareVariantScopes.empty()) { 2406 Diag(Loc, diag::warn_nested_declare_variant); 2407 return; 2408 } 2409 OMPDeclareVariantScopes.push_back(OMPDeclareVariantScope(TI)); 2410 } 2411 2412 void Sema::ActOnOpenMPEndDeclareVariant() { 2413 assert(isInOpenMPDeclareVariantScope() && 2414 "Not in OpenMP declare variant scope!"); 2415 2416 OMPDeclareVariantScopes.pop_back(); 2417 } 2418 2419 void Sema::finalizeOpenMPDelayedAnalysis(const FunctionDecl *Caller, 2420 const FunctionDecl *Callee, 2421 SourceLocation Loc) { 2422 assert(LangOpts.OpenMP && "Expected OpenMP compilation mode."); 2423 Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy = 2424 OMPDeclareTargetDeclAttr::getDeviceType(Caller->getMostRecentDecl()); 2425 // Ignore host functions during device analyzis. 2426 if (LangOpts.OpenMPIsDevice && DevTy && 2427 *DevTy == OMPDeclareTargetDeclAttr::DT_Host) 2428 return; 2429 // Ignore nohost functions during host analyzis. 2430 if (!LangOpts.OpenMPIsDevice && DevTy && 2431 *DevTy == OMPDeclareTargetDeclAttr::DT_NoHost) 2432 return; 2433 const FunctionDecl *FD = Callee->getMostRecentDecl(); 2434 DevTy = OMPDeclareTargetDeclAttr::getDeviceType(FD); 2435 if (LangOpts.OpenMPIsDevice && DevTy && 2436 *DevTy == OMPDeclareTargetDeclAttr::DT_Host) { 2437 // Diagnose host function called during device codegen. 2438 StringRef HostDevTy = 2439 getOpenMPSimpleClauseTypeName(OMPC_device_type, OMPC_DEVICE_TYPE_host); 2440 Diag(Loc, diag::err_omp_wrong_device_function_call) << HostDevTy << 0; 2441 Diag(FD->getAttr<OMPDeclareTargetDeclAttr>()->getLocation(), 2442 diag::note_omp_marked_device_type_here) 2443 << HostDevTy; 2444 return; 2445 } 2446 if (!LangOpts.OpenMPIsDevice && DevTy && 2447 *DevTy == OMPDeclareTargetDeclAttr::DT_NoHost) { 2448 // Diagnose nohost function called during host codegen. 2449 StringRef NoHostDevTy = getOpenMPSimpleClauseTypeName( 2450 OMPC_device_type, OMPC_DEVICE_TYPE_nohost); 2451 Diag(Loc, diag::err_omp_wrong_device_function_call) << NoHostDevTy << 1; 2452 Diag(FD->getAttr<OMPDeclareTargetDeclAttr>()->getLocation(), 2453 diag::note_omp_marked_device_type_here) 2454 << NoHostDevTy; 2455 } 2456 } 2457 2458 void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind, 2459 const DeclarationNameInfo &DirName, 2460 Scope *CurScope, SourceLocation Loc) { 2461 DSAStack->push(DKind, DirName, CurScope, Loc); 2462 PushExpressionEvaluationContext( 2463 ExpressionEvaluationContext::PotentiallyEvaluated); 2464 } 2465 2466 void Sema::StartOpenMPClause(OpenMPClauseKind K) { 2467 DSAStack->setClauseParsingMode(K); 2468 } 2469 2470 void Sema::EndOpenMPClause() { 2471 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown); 2472 } 2473 2474 static std::pair<ValueDecl *, bool> 2475 getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc, 2476 SourceRange &ERange, bool AllowArraySection = false); 2477 2478 /// Check consistency of the reduction clauses. 2479 static void checkReductionClauses(Sema &S, DSAStackTy *Stack, 2480 ArrayRef<OMPClause *> Clauses) { 2481 bool InscanFound = false; 2482 SourceLocation InscanLoc; 2483 // OpenMP 5.0, 2.19.5.4 reduction Clause, Restrictions. 2484 // A reduction clause without the inscan reduction-modifier may not appear on 2485 // a construct on which a reduction clause with the inscan reduction-modifier 2486 // appears. 2487 for (OMPClause *C : Clauses) { 2488 if (C->getClauseKind() != OMPC_reduction) 2489 continue; 2490 auto *RC = cast<OMPReductionClause>(C); 2491 if (RC->getModifier() == OMPC_REDUCTION_inscan) { 2492 InscanFound = true; 2493 InscanLoc = RC->getModifierLoc(); 2494 continue; 2495 } 2496 if (RC->getModifier() == OMPC_REDUCTION_task) { 2497 // OpenMP 5.0, 2.19.5.4 reduction Clause. 2498 // A reduction clause with the task reduction-modifier may only appear on 2499 // a parallel construct, a worksharing construct or a combined or 2500 // composite construct for which any of the aforementioned constructs is a 2501 // constituent construct and simd or loop are not constituent constructs. 2502 OpenMPDirectiveKind CurDir = Stack->getCurrentDirective(); 2503 if (!(isOpenMPParallelDirective(CurDir) || 2504 isOpenMPWorksharingDirective(CurDir)) || 2505 isOpenMPSimdDirective(CurDir)) 2506 S.Diag(RC->getModifierLoc(), 2507 diag::err_omp_reduction_task_not_parallel_or_worksharing); 2508 continue; 2509 } 2510 } 2511 if (InscanFound) { 2512 for (OMPClause *C : Clauses) { 2513 if (C->getClauseKind() != OMPC_reduction) 2514 continue; 2515 auto *RC = cast<OMPReductionClause>(C); 2516 if (RC->getModifier() != OMPC_REDUCTION_inscan) { 2517 S.Diag(RC->getModifier() == OMPC_REDUCTION_unknown 2518 ? RC->getBeginLoc() 2519 : RC->getModifierLoc(), 2520 diag::err_omp_inscan_reduction_expected); 2521 S.Diag(InscanLoc, diag::note_omp_previous_inscan_reduction); 2522 continue; 2523 } 2524 for (Expr *Ref : RC->varlists()) { 2525 assert(Ref && "NULL expr in OpenMP nontemporal clause."); 2526 SourceLocation ELoc; 2527 SourceRange ERange; 2528 Expr *SimpleRefExpr = Ref; 2529 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange, 2530 /*AllowArraySection=*/true); 2531 ValueDecl *D = Res.first; 2532 if (!D) 2533 continue; 2534 if (!Stack->isUsedInScanDirective(getCanonicalDecl(D))) { 2535 S.Diag(Ref->getExprLoc(), 2536 diag::err_omp_reduction_not_inclusive_exclusive) 2537 << Ref->getSourceRange(); 2538 } 2539 } 2540 } 2541 } 2542 } 2543 2544 static void checkAllocateClauses(Sema &S, DSAStackTy *Stack, 2545 ArrayRef<OMPClause *> Clauses); 2546 static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr, 2547 bool WithInit); 2548 2549 static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack, 2550 const ValueDecl *D, 2551 const DSAStackTy::DSAVarData &DVar, 2552 bool IsLoopIterVar = false); 2553 2554 void Sema::EndOpenMPDSABlock(Stmt *CurDirective) { 2555 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1] 2556 // A variable of class type (or array thereof) that appears in a lastprivate 2557 // clause requires an accessible, unambiguous default constructor for the 2558 // class type, unless the list item is also specified in a firstprivate 2559 // clause. 2560 if (const auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) { 2561 for (OMPClause *C : D->clauses()) { 2562 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) { 2563 SmallVector<Expr *, 8> PrivateCopies; 2564 for (Expr *DE : Clause->varlists()) { 2565 if (DE->isValueDependent() || DE->isTypeDependent()) { 2566 PrivateCopies.push_back(nullptr); 2567 continue; 2568 } 2569 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens()); 2570 auto *VD = cast<VarDecl>(DRE->getDecl()); 2571 QualType Type = VD->getType().getNonReferenceType(); 2572 const DSAStackTy::DSAVarData DVar = 2573 DSAStack->getTopDSA(VD, /*FromParent=*/false); 2574 if (DVar.CKind == OMPC_lastprivate) { 2575 // Generate helper private variable and initialize it with the 2576 // default value. The address of the original variable is replaced 2577 // by the address of the new private variable in CodeGen. This new 2578 // variable is not added to IdResolver, so the code in the OpenMP 2579 // region uses original variable for proper diagnostics. 2580 VarDecl *VDPrivate = buildVarDecl( 2581 *this, DE->getExprLoc(), Type.getUnqualifiedType(), 2582 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr, DRE); 2583 ActOnUninitializedDecl(VDPrivate); 2584 if (VDPrivate->isInvalidDecl()) { 2585 PrivateCopies.push_back(nullptr); 2586 continue; 2587 } 2588 PrivateCopies.push_back(buildDeclRefExpr( 2589 *this, VDPrivate, DE->getType(), DE->getExprLoc())); 2590 } else { 2591 // The variable is also a firstprivate, so initialization sequence 2592 // for private copy is generated already. 2593 PrivateCopies.push_back(nullptr); 2594 } 2595 } 2596 Clause->setPrivateCopies(PrivateCopies); 2597 continue; 2598 } 2599 // Finalize nontemporal clause by handling private copies, if any. 2600 if (auto *Clause = dyn_cast<OMPNontemporalClause>(C)) { 2601 SmallVector<Expr *, 8> PrivateRefs; 2602 for (Expr *RefExpr : Clause->varlists()) { 2603 assert(RefExpr && "NULL expr in OpenMP nontemporal clause."); 2604 SourceLocation ELoc; 2605 SourceRange ERange; 2606 Expr *SimpleRefExpr = RefExpr; 2607 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 2608 if (Res.second) 2609 // It will be analyzed later. 2610 PrivateRefs.push_back(RefExpr); 2611 ValueDecl *D = Res.first; 2612 if (!D) 2613 continue; 2614 2615 const DSAStackTy::DSAVarData DVar = 2616 DSAStack->getTopDSA(D, /*FromParent=*/false); 2617 PrivateRefs.push_back(DVar.PrivateCopy ? DVar.PrivateCopy 2618 : SimpleRefExpr); 2619 } 2620 Clause->setPrivateRefs(PrivateRefs); 2621 continue; 2622 } 2623 if (auto *Clause = dyn_cast<OMPUsesAllocatorsClause>(C)) { 2624 for (unsigned I = 0, E = Clause->getNumberOfAllocators(); I < E; ++I) { 2625 OMPUsesAllocatorsClause::Data D = Clause->getAllocatorData(I); 2626 auto *DRE = dyn_cast<DeclRefExpr>(D.Allocator->IgnoreParenImpCasts()); 2627 if (!DRE) 2628 continue; 2629 ValueDecl *VD = DRE->getDecl(); 2630 if (!VD || !isa<VarDecl>(VD)) 2631 continue; 2632 DSAStackTy::DSAVarData DVar = 2633 DSAStack->getTopDSA(VD, /*FromParent=*/false); 2634 // OpenMP [2.12.5, target Construct] 2635 // Memory allocators that appear in a uses_allocators clause cannot 2636 // appear in other data-sharing attribute clauses or data-mapping 2637 // attribute clauses in the same construct. 2638 Expr *MapExpr = nullptr; 2639 if (DVar.RefExpr || 2640 DSAStack->checkMappableExprComponentListsForDecl( 2641 VD, /*CurrentRegionOnly=*/true, 2642 [VD, &MapExpr]( 2643 OMPClauseMappableExprCommon::MappableExprComponentListRef 2644 MapExprComponents, 2645 OpenMPClauseKind C) { 2646 auto MI = MapExprComponents.rbegin(); 2647 auto ME = MapExprComponents.rend(); 2648 if (MI != ME && 2649 MI->getAssociatedDeclaration()->getCanonicalDecl() == 2650 VD->getCanonicalDecl()) { 2651 MapExpr = MI->getAssociatedExpression(); 2652 return true; 2653 } 2654 return false; 2655 })) { 2656 Diag(D.Allocator->getExprLoc(), 2657 diag::err_omp_allocator_used_in_clauses) 2658 << D.Allocator->getSourceRange(); 2659 if (DVar.RefExpr) 2660 reportOriginalDsa(*this, DSAStack, VD, DVar); 2661 else 2662 Diag(MapExpr->getExprLoc(), diag::note_used_here) 2663 << MapExpr->getSourceRange(); 2664 } 2665 } 2666 continue; 2667 } 2668 } 2669 // Check allocate clauses. 2670 if (!CurContext->isDependentContext()) 2671 checkAllocateClauses(*this, DSAStack, D->clauses()); 2672 checkReductionClauses(*this, DSAStack, D->clauses()); 2673 } 2674 2675 DSAStack->pop(); 2676 DiscardCleanupsInEvaluationContext(); 2677 PopExpressionEvaluationContext(); 2678 } 2679 2680 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, 2681 Expr *NumIterations, Sema &SemaRef, 2682 Scope *S, DSAStackTy *Stack); 2683 2684 namespace { 2685 2686 class VarDeclFilterCCC final : public CorrectionCandidateCallback { 2687 private: 2688 Sema &SemaRef; 2689 2690 public: 2691 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {} 2692 bool ValidateCandidate(const TypoCorrection &Candidate) override { 2693 NamedDecl *ND = Candidate.getCorrectionDecl(); 2694 if (const auto *VD = dyn_cast_or_null<VarDecl>(ND)) { 2695 return VD->hasGlobalStorage() && 2696 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), 2697 SemaRef.getCurScope()); 2698 } 2699 return false; 2700 } 2701 2702 std::unique_ptr<CorrectionCandidateCallback> clone() override { 2703 return std::make_unique<VarDeclFilterCCC>(*this); 2704 } 2705 2706 }; 2707 2708 class VarOrFuncDeclFilterCCC final : public CorrectionCandidateCallback { 2709 private: 2710 Sema &SemaRef; 2711 2712 public: 2713 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {} 2714 bool ValidateCandidate(const TypoCorrection &Candidate) override { 2715 NamedDecl *ND = Candidate.getCorrectionDecl(); 2716 if (ND && ((isa<VarDecl>(ND) && ND->getKind() == Decl::Var) || 2717 isa<FunctionDecl>(ND))) { 2718 return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), 2719 SemaRef.getCurScope()); 2720 } 2721 return false; 2722 } 2723 2724 std::unique_ptr<CorrectionCandidateCallback> clone() override { 2725 return std::make_unique<VarOrFuncDeclFilterCCC>(*this); 2726 } 2727 }; 2728 2729 } // namespace 2730 2731 ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope, 2732 CXXScopeSpec &ScopeSpec, 2733 const DeclarationNameInfo &Id, 2734 OpenMPDirectiveKind Kind) { 2735 LookupResult Lookup(*this, Id, LookupOrdinaryName); 2736 LookupParsedName(Lookup, CurScope, &ScopeSpec, true); 2737 2738 if (Lookup.isAmbiguous()) 2739 return ExprError(); 2740 2741 VarDecl *VD; 2742 if (!Lookup.isSingleResult()) { 2743 VarDeclFilterCCC CCC(*this); 2744 if (TypoCorrection Corrected = 2745 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, CCC, 2746 CTK_ErrorRecovery)) { 2747 diagnoseTypo(Corrected, 2748 PDiag(Lookup.empty() 2749 ? diag::err_undeclared_var_use_suggest 2750 : diag::err_omp_expected_var_arg_suggest) 2751 << Id.getName()); 2752 VD = Corrected.getCorrectionDeclAs<VarDecl>(); 2753 } else { 2754 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use 2755 : diag::err_omp_expected_var_arg) 2756 << Id.getName(); 2757 return ExprError(); 2758 } 2759 } else if (!(VD = Lookup.getAsSingle<VarDecl>())) { 2760 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName(); 2761 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at); 2762 return ExprError(); 2763 } 2764 Lookup.suppressDiagnostics(); 2765 2766 // OpenMP [2.9.2, Syntax, C/C++] 2767 // Variables must be file-scope, namespace-scope, or static block-scope. 2768 if (Kind == OMPD_threadprivate && !VD->hasGlobalStorage()) { 2769 Diag(Id.getLoc(), diag::err_omp_global_var_arg) 2770 << getOpenMPDirectiveName(Kind) << !VD->isStaticLocal(); 2771 bool IsDecl = 2772 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2773 Diag(VD->getLocation(), 2774 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2775 << VD; 2776 return ExprError(); 2777 } 2778 2779 VarDecl *CanonicalVD = VD->getCanonicalDecl(); 2780 NamedDecl *ND = CanonicalVD; 2781 // OpenMP [2.9.2, Restrictions, C/C++, p.2] 2782 // A threadprivate directive for file-scope variables must appear outside 2783 // any definition or declaration. 2784 if (CanonicalVD->getDeclContext()->isTranslationUnit() && 2785 !getCurLexicalContext()->isTranslationUnit()) { 2786 Diag(Id.getLoc(), diag::err_omp_var_scope) 2787 << getOpenMPDirectiveName(Kind) << VD; 2788 bool IsDecl = 2789 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2790 Diag(VD->getLocation(), 2791 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2792 << VD; 2793 return ExprError(); 2794 } 2795 // OpenMP [2.9.2, Restrictions, C/C++, p.3] 2796 // A threadprivate directive for static class member variables must appear 2797 // in the class definition, in the same scope in which the member 2798 // variables are declared. 2799 if (CanonicalVD->isStaticDataMember() && 2800 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) { 2801 Diag(Id.getLoc(), diag::err_omp_var_scope) 2802 << getOpenMPDirectiveName(Kind) << VD; 2803 bool IsDecl = 2804 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2805 Diag(VD->getLocation(), 2806 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2807 << VD; 2808 return ExprError(); 2809 } 2810 // OpenMP [2.9.2, Restrictions, C/C++, p.4] 2811 // A threadprivate directive for namespace-scope variables must appear 2812 // outside any definition or declaration other than the namespace 2813 // definition itself. 2814 if (CanonicalVD->getDeclContext()->isNamespace() && 2815 (!getCurLexicalContext()->isFileContext() || 2816 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) { 2817 Diag(Id.getLoc(), diag::err_omp_var_scope) 2818 << getOpenMPDirectiveName(Kind) << VD; 2819 bool IsDecl = 2820 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2821 Diag(VD->getLocation(), 2822 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2823 << VD; 2824 return ExprError(); 2825 } 2826 // OpenMP [2.9.2, Restrictions, C/C++, p.6] 2827 // A threadprivate directive for static block-scope variables must appear 2828 // in the scope of the variable and not in a nested scope. 2829 if (CanonicalVD->isLocalVarDecl() && CurScope && 2830 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) { 2831 Diag(Id.getLoc(), diag::err_omp_var_scope) 2832 << getOpenMPDirectiveName(Kind) << VD; 2833 bool IsDecl = 2834 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2835 Diag(VD->getLocation(), 2836 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2837 << VD; 2838 return ExprError(); 2839 } 2840 2841 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6] 2842 // A threadprivate directive must lexically precede all references to any 2843 // of the variables in its list. 2844 if (Kind == OMPD_threadprivate && VD->isUsed() && 2845 !DSAStack->isThreadPrivate(VD)) { 2846 Diag(Id.getLoc(), diag::err_omp_var_used) 2847 << getOpenMPDirectiveName(Kind) << VD; 2848 return ExprError(); 2849 } 2850 2851 QualType ExprType = VD->getType().getNonReferenceType(); 2852 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(), 2853 SourceLocation(), VD, 2854 /*RefersToEnclosingVariableOrCapture=*/false, 2855 Id.getLoc(), ExprType, VK_LValue); 2856 } 2857 2858 Sema::DeclGroupPtrTy 2859 Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc, 2860 ArrayRef<Expr *> VarList) { 2861 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) { 2862 CurContext->addDecl(D); 2863 return DeclGroupPtrTy::make(DeclGroupRef(D)); 2864 } 2865 return nullptr; 2866 } 2867 2868 namespace { 2869 class LocalVarRefChecker final 2870 : public ConstStmtVisitor<LocalVarRefChecker, bool> { 2871 Sema &SemaRef; 2872 2873 public: 2874 bool VisitDeclRefExpr(const DeclRefExpr *E) { 2875 if (const auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 2876 if (VD->hasLocalStorage()) { 2877 SemaRef.Diag(E->getBeginLoc(), 2878 diag::err_omp_local_var_in_threadprivate_init) 2879 << E->getSourceRange(); 2880 SemaRef.Diag(VD->getLocation(), diag::note_defined_here) 2881 << VD << VD->getSourceRange(); 2882 return true; 2883 } 2884 } 2885 return false; 2886 } 2887 bool VisitStmt(const Stmt *S) { 2888 for (const Stmt *Child : S->children()) { 2889 if (Child && Visit(Child)) 2890 return true; 2891 } 2892 return false; 2893 } 2894 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {} 2895 }; 2896 } // namespace 2897 2898 OMPThreadPrivateDecl * 2899 Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) { 2900 SmallVector<Expr *, 8> Vars; 2901 for (Expr *RefExpr : VarList) { 2902 auto *DE = cast<DeclRefExpr>(RefExpr); 2903 auto *VD = cast<VarDecl>(DE->getDecl()); 2904 SourceLocation ILoc = DE->getExprLoc(); 2905 2906 // Mark variable as used. 2907 VD->setReferenced(); 2908 VD->markUsed(Context); 2909 2910 QualType QType = VD->getType(); 2911 if (QType->isDependentType() || QType->isInstantiationDependentType()) { 2912 // It will be analyzed later. 2913 Vars.push_back(DE); 2914 continue; 2915 } 2916 2917 // OpenMP [2.9.2, Restrictions, C/C++, p.10] 2918 // A threadprivate variable must not have an incomplete type. 2919 if (RequireCompleteType(ILoc, VD->getType(), 2920 diag::err_omp_threadprivate_incomplete_type)) { 2921 continue; 2922 } 2923 2924 // OpenMP [2.9.2, Restrictions, C/C++, p.10] 2925 // A threadprivate variable must not have a reference type. 2926 if (VD->getType()->isReferenceType()) { 2927 Diag(ILoc, diag::err_omp_ref_type_arg) 2928 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType(); 2929 bool IsDecl = 2930 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2931 Diag(VD->getLocation(), 2932 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2933 << VD; 2934 continue; 2935 } 2936 2937 // Check if this is a TLS variable. If TLS is not being supported, produce 2938 // the corresponding diagnostic. 2939 if ((VD->getTLSKind() != VarDecl::TLS_None && 2940 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() && 2941 getLangOpts().OpenMPUseTLS && 2942 getASTContext().getTargetInfo().isTLSSupported())) || 2943 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() && 2944 !VD->isLocalVarDecl())) { 2945 Diag(ILoc, diag::err_omp_var_thread_local) 2946 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1); 2947 bool IsDecl = 2948 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2949 Diag(VD->getLocation(), 2950 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2951 << VD; 2952 continue; 2953 } 2954 2955 // Check if initial value of threadprivate variable reference variable with 2956 // local storage (it is not supported by runtime). 2957 if (const Expr *Init = VD->getAnyInitializer()) { 2958 LocalVarRefChecker Checker(*this); 2959 if (Checker.Visit(Init)) 2960 continue; 2961 } 2962 2963 Vars.push_back(RefExpr); 2964 DSAStack->addDSA(VD, DE, OMPC_threadprivate); 2965 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit( 2966 Context, SourceRange(Loc, Loc))); 2967 if (ASTMutationListener *ML = Context.getASTMutationListener()) 2968 ML->DeclarationMarkedOpenMPThreadPrivate(VD); 2969 } 2970 OMPThreadPrivateDecl *D = nullptr; 2971 if (!Vars.empty()) { 2972 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc, 2973 Vars); 2974 D->setAccess(AS_public); 2975 } 2976 return D; 2977 } 2978 2979 static OMPAllocateDeclAttr::AllocatorTypeTy 2980 getAllocatorKind(Sema &S, DSAStackTy *Stack, Expr *Allocator) { 2981 if (!Allocator) 2982 return OMPAllocateDeclAttr::OMPNullMemAlloc; 2983 if (Allocator->isTypeDependent() || Allocator->isValueDependent() || 2984 Allocator->isInstantiationDependent() || 2985 Allocator->containsUnexpandedParameterPack()) 2986 return OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; 2987 auto AllocatorKindRes = OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; 2988 const Expr *AE = Allocator->IgnoreParenImpCasts(); 2989 for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) { 2990 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I); 2991 const Expr *DefAllocator = Stack->getAllocator(AllocatorKind); 2992 llvm::FoldingSetNodeID AEId, DAEId; 2993 AE->Profile(AEId, S.getASTContext(), /*Canonical=*/true); 2994 DefAllocator->Profile(DAEId, S.getASTContext(), /*Canonical=*/true); 2995 if (AEId == DAEId) { 2996 AllocatorKindRes = AllocatorKind; 2997 break; 2998 } 2999 } 3000 return AllocatorKindRes; 3001 } 3002 3003 static bool checkPreviousOMPAllocateAttribute( 3004 Sema &S, DSAStackTy *Stack, Expr *RefExpr, VarDecl *VD, 3005 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, Expr *Allocator) { 3006 if (!VD->hasAttr<OMPAllocateDeclAttr>()) 3007 return false; 3008 const auto *A = VD->getAttr<OMPAllocateDeclAttr>(); 3009 Expr *PrevAllocator = A->getAllocator(); 3010 OMPAllocateDeclAttr::AllocatorTypeTy PrevAllocatorKind = 3011 getAllocatorKind(S, Stack, PrevAllocator); 3012 bool AllocatorsMatch = AllocatorKind == PrevAllocatorKind; 3013 if (AllocatorsMatch && 3014 AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc && 3015 Allocator && PrevAllocator) { 3016 const Expr *AE = Allocator->IgnoreParenImpCasts(); 3017 const Expr *PAE = PrevAllocator->IgnoreParenImpCasts(); 3018 llvm::FoldingSetNodeID AEId, PAEId; 3019 AE->Profile(AEId, S.Context, /*Canonical=*/true); 3020 PAE->Profile(PAEId, S.Context, /*Canonical=*/true); 3021 AllocatorsMatch = AEId == PAEId; 3022 } 3023 if (!AllocatorsMatch) { 3024 SmallString<256> AllocatorBuffer; 3025 llvm::raw_svector_ostream AllocatorStream(AllocatorBuffer); 3026 if (Allocator) 3027 Allocator->printPretty(AllocatorStream, nullptr, S.getPrintingPolicy()); 3028 SmallString<256> PrevAllocatorBuffer; 3029 llvm::raw_svector_ostream PrevAllocatorStream(PrevAllocatorBuffer); 3030 if (PrevAllocator) 3031 PrevAllocator->printPretty(PrevAllocatorStream, nullptr, 3032 S.getPrintingPolicy()); 3033 3034 SourceLocation AllocatorLoc = 3035 Allocator ? Allocator->getExprLoc() : RefExpr->getExprLoc(); 3036 SourceRange AllocatorRange = 3037 Allocator ? Allocator->getSourceRange() : RefExpr->getSourceRange(); 3038 SourceLocation PrevAllocatorLoc = 3039 PrevAllocator ? PrevAllocator->getExprLoc() : A->getLocation(); 3040 SourceRange PrevAllocatorRange = 3041 PrevAllocator ? PrevAllocator->getSourceRange() : A->getRange(); 3042 S.Diag(AllocatorLoc, diag::warn_omp_used_different_allocator) 3043 << (Allocator ? 1 : 0) << AllocatorStream.str() 3044 << (PrevAllocator ? 1 : 0) << PrevAllocatorStream.str() 3045 << AllocatorRange; 3046 S.Diag(PrevAllocatorLoc, diag::note_omp_previous_allocator) 3047 << PrevAllocatorRange; 3048 return true; 3049 } 3050 return false; 3051 } 3052 3053 static void 3054 applyOMPAllocateAttribute(Sema &S, VarDecl *VD, 3055 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, 3056 Expr *Allocator, SourceRange SR) { 3057 if (VD->hasAttr<OMPAllocateDeclAttr>()) 3058 return; 3059 if (Allocator && 3060 (Allocator->isTypeDependent() || Allocator->isValueDependent() || 3061 Allocator->isInstantiationDependent() || 3062 Allocator->containsUnexpandedParameterPack())) 3063 return; 3064 auto *A = OMPAllocateDeclAttr::CreateImplicit(S.Context, AllocatorKind, 3065 Allocator, SR); 3066 VD->addAttr(A); 3067 if (ASTMutationListener *ML = S.Context.getASTMutationListener()) 3068 ML->DeclarationMarkedOpenMPAllocate(VD, A); 3069 } 3070 3071 Sema::DeclGroupPtrTy Sema::ActOnOpenMPAllocateDirective( 3072 SourceLocation Loc, ArrayRef<Expr *> VarList, 3073 ArrayRef<OMPClause *> Clauses, DeclContext *Owner) { 3074 assert(Clauses.size() <= 1 && "Expected at most one clause."); 3075 Expr *Allocator = nullptr; 3076 if (Clauses.empty()) { 3077 // OpenMP 5.0, 2.11.3 allocate Directive, Restrictions. 3078 // allocate directives that appear in a target region must specify an 3079 // allocator clause unless a requires directive with the dynamic_allocators 3080 // clause is present in the same compilation unit. 3081 if (LangOpts.OpenMPIsDevice && 3082 !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>()) 3083 targetDiag(Loc, diag::err_expected_allocator_clause); 3084 } else { 3085 Allocator = cast<OMPAllocatorClause>(Clauses.back())->getAllocator(); 3086 } 3087 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind = 3088 getAllocatorKind(*this, DSAStack, Allocator); 3089 SmallVector<Expr *, 8> Vars; 3090 for (Expr *RefExpr : VarList) { 3091 auto *DE = cast<DeclRefExpr>(RefExpr); 3092 auto *VD = cast<VarDecl>(DE->getDecl()); 3093 3094 // Check if this is a TLS variable or global register. 3095 if (VD->getTLSKind() != VarDecl::TLS_None || 3096 VD->hasAttr<OMPThreadPrivateDeclAttr>() || 3097 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() && 3098 !VD->isLocalVarDecl())) 3099 continue; 3100 3101 // If the used several times in the allocate directive, the same allocator 3102 // must be used. 3103 if (checkPreviousOMPAllocateAttribute(*this, DSAStack, RefExpr, VD, 3104 AllocatorKind, Allocator)) 3105 continue; 3106 3107 // OpenMP, 2.11.3 allocate Directive, Restrictions, C / C++ 3108 // If a list item has a static storage type, the allocator expression in the 3109 // allocator clause must be a constant expression that evaluates to one of 3110 // the predefined memory allocator values. 3111 if (Allocator && VD->hasGlobalStorage()) { 3112 if (AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc) { 3113 Diag(Allocator->getExprLoc(), 3114 diag::err_omp_expected_predefined_allocator) 3115 << Allocator->getSourceRange(); 3116 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 3117 VarDecl::DeclarationOnly; 3118 Diag(VD->getLocation(), 3119 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 3120 << VD; 3121 continue; 3122 } 3123 } 3124 3125 Vars.push_back(RefExpr); 3126 applyOMPAllocateAttribute(*this, VD, AllocatorKind, Allocator, 3127 DE->getSourceRange()); 3128 } 3129 if (Vars.empty()) 3130 return nullptr; 3131 if (!Owner) 3132 Owner = getCurLexicalContext(); 3133 auto *D = OMPAllocateDecl::Create(Context, Owner, Loc, Vars, Clauses); 3134 D->setAccess(AS_public); 3135 Owner->addDecl(D); 3136 return DeclGroupPtrTy::make(DeclGroupRef(D)); 3137 } 3138 3139 Sema::DeclGroupPtrTy 3140 Sema::ActOnOpenMPRequiresDirective(SourceLocation Loc, 3141 ArrayRef<OMPClause *> ClauseList) { 3142 OMPRequiresDecl *D = nullptr; 3143 if (!CurContext->isFileContext()) { 3144 Diag(Loc, diag::err_omp_invalid_scope) << "requires"; 3145 } else { 3146 D = CheckOMPRequiresDecl(Loc, ClauseList); 3147 if (D) { 3148 CurContext->addDecl(D); 3149 DSAStack->addRequiresDecl(D); 3150 } 3151 } 3152 return DeclGroupPtrTy::make(DeclGroupRef(D)); 3153 } 3154 3155 OMPRequiresDecl *Sema::CheckOMPRequiresDecl(SourceLocation Loc, 3156 ArrayRef<OMPClause *> ClauseList) { 3157 /// For target specific clauses, the requires directive cannot be 3158 /// specified after the handling of any of the target regions in the 3159 /// current compilation unit. 3160 ArrayRef<SourceLocation> TargetLocations = 3161 DSAStack->getEncounteredTargetLocs(); 3162 SourceLocation AtomicLoc = DSAStack->getAtomicDirectiveLoc(); 3163 if (!TargetLocations.empty() || !AtomicLoc.isInvalid()) { 3164 for (const OMPClause *CNew : ClauseList) { 3165 // Check if any of the requires clauses affect target regions. 3166 if (isa<OMPUnifiedSharedMemoryClause>(CNew) || 3167 isa<OMPUnifiedAddressClause>(CNew) || 3168 isa<OMPReverseOffloadClause>(CNew) || 3169 isa<OMPDynamicAllocatorsClause>(CNew)) { 3170 Diag(Loc, diag::err_omp_directive_before_requires) 3171 << "target" << getOpenMPClauseName(CNew->getClauseKind()); 3172 for (SourceLocation TargetLoc : TargetLocations) { 3173 Diag(TargetLoc, diag::note_omp_requires_encountered_directive) 3174 << "target"; 3175 } 3176 } else if (!AtomicLoc.isInvalid() && 3177 isa<OMPAtomicDefaultMemOrderClause>(CNew)) { 3178 Diag(Loc, diag::err_omp_directive_before_requires) 3179 << "atomic" << getOpenMPClauseName(CNew->getClauseKind()); 3180 Diag(AtomicLoc, diag::note_omp_requires_encountered_directive) 3181 << "atomic"; 3182 } 3183 } 3184 } 3185 3186 if (!DSAStack->hasDuplicateRequiresClause(ClauseList)) 3187 return OMPRequiresDecl::Create(Context, getCurLexicalContext(), Loc, 3188 ClauseList); 3189 return nullptr; 3190 } 3191 3192 static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack, 3193 const ValueDecl *D, 3194 const DSAStackTy::DSAVarData &DVar, 3195 bool IsLoopIterVar) { 3196 if (DVar.RefExpr) { 3197 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa) 3198 << getOpenMPClauseName(DVar.CKind); 3199 return; 3200 } 3201 enum { 3202 PDSA_StaticMemberShared, 3203 PDSA_StaticLocalVarShared, 3204 PDSA_LoopIterVarPrivate, 3205 PDSA_LoopIterVarLinear, 3206 PDSA_LoopIterVarLastprivate, 3207 PDSA_ConstVarShared, 3208 PDSA_GlobalVarShared, 3209 PDSA_TaskVarFirstprivate, 3210 PDSA_LocalVarPrivate, 3211 PDSA_Implicit 3212 } Reason = PDSA_Implicit; 3213 bool ReportHint = false; 3214 auto ReportLoc = D->getLocation(); 3215 auto *VD = dyn_cast<VarDecl>(D); 3216 if (IsLoopIterVar) { 3217 if (DVar.CKind == OMPC_private) 3218 Reason = PDSA_LoopIterVarPrivate; 3219 else if (DVar.CKind == OMPC_lastprivate) 3220 Reason = PDSA_LoopIterVarLastprivate; 3221 else 3222 Reason = PDSA_LoopIterVarLinear; 3223 } else if (isOpenMPTaskingDirective(DVar.DKind) && 3224 DVar.CKind == OMPC_firstprivate) { 3225 Reason = PDSA_TaskVarFirstprivate; 3226 ReportLoc = DVar.ImplicitDSALoc; 3227 } else if (VD && VD->isStaticLocal()) 3228 Reason = PDSA_StaticLocalVarShared; 3229 else if (VD && VD->isStaticDataMember()) 3230 Reason = PDSA_StaticMemberShared; 3231 else if (VD && VD->isFileVarDecl()) 3232 Reason = PDSA_GlobalVarShared; 3233 else if (D->getType().isConstant(SemaRef.getASTContext())) 3234 Reason = PDSA_ConstVarShared; 3235 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) { 3236 ReportHint = true; 3237 Reason = PDSA_LocalVarPrivate; 3238 } 3239 if (Reason != PDSA_Implicit) { 3240 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa) 3241 << Reason << ReportHint 3242 << getOpenMPDirectiveName(Stack->getCurrentDirective()); 3243 } else if (DVar.ImplicitDSALoc.isValid()) { 3244 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa) 3245 << getOpenMPClauseName(DVar.CKind); 3246 } 3247 } 3248 3249 static OpenMPMapClauseKind 3250 getMapClauseKindFromModifier(OpenMPDefaultmapClauseModifier M, 3251 bool IsAggregateOrDeclareTarget) { 3252 OpenMPMapClauseKind Kind = OMPC_MAP_unknown; 3253 switch (M) { 3254 case OMPC_DEFAULTMAP_MODIFIER_alloc: 3255 Kind = OMPC_MAP_alloc; 3256 break; 3257 case OMPC_DEFAULTMAP_MODIFIER_to: 3258 Kind = OMPC_MAP_to; 3259 break; 3260 case OMPC_DEFAULTMAP_MODIFIER_from: 3261 Kind = OMPC_MAP_from; 3262 break; 3263 case OMPC_DEFAULTMAP_MODIFIER_tofrom: 3264 Kind = OMPC_MAP_tofrom; 3265 break; 3266 case OMPC_DEFAULTMAP_MODIFIER_firstprivate: 3267 case OMPC_DEFAULTMAP_MODIFIER_last: 3268 llvm_unreachable("Unexpected defaultmap implicit behavior"); 3269 case OMPC_DEFAULTMAP_MODIFIER_none: 3270 case OMPC_DEFAULTMAP_MODIFIER_default: 3271 case OMPC_DEFAULTMAP_MODIFIER_unknown: 3272 // IsAggregateOrDeclareTarget could be true if: 3273 // 1. the implicit behavior for aggregate is tofrom 3274 // 2. it's a declare target link 3275 if (IsAggregateOrDeclareTarget) { 3276 Kind = OMPC_MAP_tofrom; 3277 break; 3278 } 3279 llvm_unreachable("Unexpected defaultmap implicit behavior"); 3280 } 3281 assert(Kind != OMPC_MAP_unknown && "Expect map kind to be known"); 3282 return Kind; 3283 } 3284 3285 namespace { 3286 class DSAAttrChecker final : public StmtVisitor<DSAAttrChecker, void> { 3287 DSAStackTy *Stack; 3288 Sema &SemaRef; 3289 bool ErrorFound = false; 3290 bool TryCaptureCXXThisMembers = false; 3291 CapturedStmt *CS = nullptr; 3292 llvm::SmallVector<Expr *, 4> ImplicitFirstprivate; 3293 llvm::SmallVector<Expr *, 4> ImplicitMap[OMPC_MAP_delete]; 3294 Sema::VarsWithInheritedDSAType VarsWithInheritedDSA; 3295 llvm::SmallDenseSet<const ValueDecl *, 4> ImplicitDeclarations; 3296 3297 void VisitSubCaptures(OMPExecutableDirective *S) { 3298 // Check implicitly captured variables. 3299 if (!S->hasAssociatedStmt() || !S->getAssociatedStmt() || 3300 S->getDirectiveKind() == OMPD_atomic || 3301 S->getDirectiveKind() == OMPD_critical || 3302 S->getDirectiveKind() == OMPD_section || 3303 S->getDirectiveKind() == OMPD_master) 3304 return; 3305 visitSubCaptures(S->getInnermostCapturedStmt()); 3306 // Try to capture inner this->member references to generate correct mappings 3307 // and diagnostics. 3308 if (TryCaptureCXXThisMembers || 3309 (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) && 3310 llvm::any_of(S->getInnermostCapturedStmt()->captures(), 3311 [](const CapturedStmt::Capture &C) { 3312 return C.capturesThis(); 3313 }))) { 3314 bool SavedTryCaptureCXXThisMembers = TryCaptureCXXThisMembers; 3315 TryCaptureCXXThisMembers = true; 3316 Visit(S->getInnermostCapturedStmt()->getCapturedStmt()); 3317 TryCaptureCXXThisMembers = SavedTryCaptureCXXThisMembers; 3318 } 3319 // In tasks firstprivates are not captured anymore, need to analyze them 3320 // explicitly. 3321 if (isOpenMPTaskingDirective(S->getDirectiveKind()) && 3322 !isOpenMPTaskLoopDirective(S->getDirectiveKind())) { 3323 for (OMPClause *C : S->clauses()) 3324 if (auto *FC = dyn_cast<OMPFirstprivateClause>(C)) { 3325 for (Expr *Ref : FC->varlists()) 3326 Visit(Ref); 3327 } 3328 } 3329 } 3330 3331 public: 3332 void VisitDeclRefExpr(DeclRefExpr *E) { 3333 if (TryCaptureCXXThisMembers || E->isTypeDependent() || 3334 E->isValueDependent() || E->containsUnexpandedParameterPack() || 3335 E->isInstantiationDependent()) 3336 return; 3337 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 3338 // Check the datasharing rules for the expressions in the clauses. 3339 if (!CS) { 3340 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD)) 3341 if (!CED->hasAttr<OMPCaptureNoInitAttr>()) { 3342 Visit(CED->getInit()); 3343 return; 3344 } 3345 } else if (VD->isImplicit() || isa<OMPCapturedExprDecl>(VD)) 3346 // Do not analyze internal variables and do not enclose them into 3347 // implicit clauses. 3348 return; 3349 VD = VD->getCanonicalDecl(); 3350 // Skip internally declared variables. 3351 if (VD->hasLocalStorage() && CS && !CS->capturesVariable(VD) && 3352 !Stack->isImplicitTaskFirstprivate(VD)) 3353 return; 3354 // Skip allocators in uses_allocators clauses. 3355 if (Stack->isUsesAllocatorsDecl(VD).hasValue()) 3356 return; 3357 3358 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false); 3359 // Check if the variable has explicit DSA set and stop analysis if it so. 3360 if (DVar.RefExpr || !ImplicitDeclarations.insert(VD).second) 3361 return; 3362 3363 // Skip internally declared static variables. 3364 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 3365 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); 3366 if (VD->hasGlobalStorage() && CS && !CS->capturesVariable(VD) && 3367 (Stack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() || 3368 !Res || *Res != OMPDeclareTargetDeclAttr::MT_Link) && 3369 !Stack->isImplicitTaskFirstprivate(VD)) 3370 return; 3371 3372 SourceLocation ELoc = E->getExprLoc(); 3373 OpenMPDirectiveKind DKind = Stack->getCurrentDirective(); 3374 // The default(none) clause requires that each variable that is referenced 3375 // in the construct, and does not have a predetermined data-sharing 3376 // attribute, must have its data-sharing attribute explicitly determined 3377 // by being listed in a data-sharing attribute clause. 3378 if (DVar.CKind == OMPC_unknown && 3379 (Stack->getDefaultDSA() == DSA_none || 3380 Stack->getDefaultDSA() == DSA_firstprivate) && 3381 isImplicitOrExplicitTaskingRegion(DKind) && 3382 VarsWithInheritedDSA.count(VD) == 0) { 3383 bool InheritedDSA = Stack->getDefaultDSA() == DSA_none; 3384 if (!InheritedDSA && Stack->getDefaultDSA() == DSA_firstprivate) { 3385 DSAStackTy::DSAVarData DVar = 3386 Stack->getImplicitDSA(VD, /*FromParent=*/false); 3387 InheritedDSA = DVar.CKind == OMPC_unknown; 3388 } 3389 if (InheritedDSA) 3390 VarsWithInheritedDSA[VD] = E; 3391 return; 3392 } 3393 3394 // OpenMP 5.0 [2.19.7.2, defaultmap clause, Description] 3395 // If implicit-behavior is none, each variable referenced in the 3396 // construct that does not have a predetermined data-sharing attribute 3397 // and does not appear in a to or link clause on a declare target 3398 // directive must be listed in a data-mapping attribute clause, a 3399 // data-haring attribute clause (including a data-sharing attribute 3400 // clause on a combined construct where target. is one of the 3401 // constituent constructs), or an is_device_ptr clause. 3402 OpenMPDefaultmapClauseKind ClauseKind = 3403 getVariableCategoryFromDecl(SemaRef.getLangOpts(), VD); 3404 if (SemaRef.getLangOpts().OpenMP >= 50) { 3405 bool IsModifierNone = Stack->getDefaultmapModifier(ClauseKind) == 3406 OMPC_DEFAULTMAP_MODIFIER_none; 3407 if (DVar.CKind == OMPC_unknown && IsModifierNone && 3408 VarsWithInheritedDSA.count(VD) == 0 && !Res) { 3409 // Only check for data-mapping attribute and is_device_ptr here 3410 // since we have already make sure that the declaration does not 3411 // have a data-sharing attribute above 3412 if (!Stack->checkMappableExprComponentListsForDecl( 3413 VD, /*CurrentRegionOnly=*/true, 3414 [VD](OMPClauseMappableExprCommon::MappableExprComponentListRef 3415 MapExprComponents, 3416 OpenMPClauseKind) { 3417 auto MI = MapExprComponents.rbegin(); 3418 auto ME = MapExprComponents.rend(); 3419 return MI != ME && MI->getAssociatedDeclaration() == VD; 3420 })) { 3421 VarsWithInheritedDSA[VD] = E; 3422 return; 3423 } 3424 } 3425 } 3426 3427 if (isOpenMPTargetExecutionDirective(DKind) && 3428 !Stack->isLoopControlVariable(VD).first) { 3429 if (!Stack->checkMappableExprComponentListsForDecl( 3430 VD, /*CurrentRegionOnly=*/true, 3431 [](OMPClauseMappableExprCommon::MappableExprComponentListRef 3432 StackComponents, 3433 OpenMPClauseKind) { 3434 // Variable is used if it has been marked as an array, array 3435 // section, array shaping or the variable iself. 3436 return StackComponents.size() == 1 || 3437 std::all_of( 3438 std::next(StackComponents.rbegin()), 3439 StackComponents.rend(), 3440 [](const OMPClauseMappableExprCommon:: 3441 MappableComponent &MC) { 3442 return MC.getAssociatedDeclaration() == 3443 nullptr && 3444 (isa<OMPArraySectionExpr>( 3445 MC.getAssociatedExpression()) || 3446 isa<OMPArrayShapingExpr>( 3447 MC.getAssociatedExpression()) || 3448 isa<ArraySubscriptExpr>( 3449 MC.getAssociatedExpression())); 3450 }); 3451 })) { 3452 bool IsFirstprivate = false; 3453 // By default lambdas are captured as firstprivates. 3454 if (const auto *RD = 3455 VD->getType().getNonReferenceType()->getAsCXXRecordDecl()) 3456 IsFirstprivate = RD->isLambda(); 3457 IsFirstprivate = 3458 IsFirstprivate || (Stack->mustBeFirstprivate(ClauseKind) && !Res); 3459 if (IsFirstprivate) { 3460 ImplicitFirstprivate.emplace_back(E); 3461 } else { 3462 OpenMPDefaultmapClauseModifier M = 3463 Stack->getDefaultmapModifier(ClauseKind); 3464 OpenMPMapClauseKind Kind = getMapClauseKindFromModifier( 3465 M, ClauseKind == OMPC_DEFAULTMAP_aggregate || Res); 3466 ImplicitMap[Kind].emplace_back(E); 3467 } 3468 return; 3469 } 3470 } 3471 3472 // OpenMP [2.9.3.6, Restrictions, p.2] 3473 // A list item that appears in a reduction clause of the innermost 3474 // enclosing worksharing or parallel construct may not be accessed in an 3475 // explicit task. 3476 DVar = Stack->hasInnermostDSA( 3477 VD, [](OpenMPClauseKind C) { return C == OMPC_reduction; }, 3478 [](OpenMPDirectiveKind K) { 3479 return isOpenMPParallelDirective(K) || 3480 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K); 3481 }, 3482 /*FromParent=*/true); 3483 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) { 3484 ErrorFound = true; 3485 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); 3486 reportOriginalDsa(SemaRef, Stack, VD, DVar); 3487 return; 3488 } 3489 3490 // Define implicit data-sharing attributes for task. 3491 DVar = Stack->getImplicitDSA(VD, /*FromParent=*/false); 3492 if (((isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared) || 3493 (Stack->getDefaultDSA() == DSA_firstprivate && 3494 DVar.CKind == OMPC_firstprivate && !DVar.RefExpr)) && 3495 !Stack->isLoopControlVariable(VD).first) { 3496 ImplicitFirstprivate.push_back(E); 3497 return; 3498 } 3499 3500 // Store implicitly used globals with declare target link for parent 3501 // target. 3502 if (!isOpenMPTargetExecutionDirective(DKind) && Res && 3503 *Res == OMPDeclareTargetDeclAttr::MT_Link) { 3504 Stack->addToParentTargetRegionLinkGlobals(E); 3505 return; 3506 } 3507 } 3508 } 3509 void VisitMemberExpr(MemberExpr *E) { 3510 if (E->isTypeDependent() || E->isValueDependent() || 3511 E->containsUnexpandedParameterPack() || E->isInstantiationDependent()) 3512 return; 3513 auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); 3514 OpenMPDirectiveKind DKind = Stack->getCurrentDirective(); 3515 if (auto *TE = dyn_cast<CXXThisExpr>(E->getBase()->IgnoreParenCasts())) { 3516 if (!FD) 3517 return; 3518 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(FD, /*FromParent=*/false); 3519 // Check if the variable has explicit DSA set and stop analysis if it 3520 // so. 3521 if (DVar.RefExpr || !ImplicitDeclarations.insert(FD).second) 3522 return; 3523 3524 if (isOpenMPTargetExecutionDirective(DKind) && 3525 !Stack->isLoopControlVariable(FD).first && 3526 !Stack->checkMappableExprComponentListsForDecl( 3527 FD, /*CurrentRegionOnly=*/true, 3528 [](OMPClauseMappableExprCommon::MappableExprComponentListRef 3529 StackComponents, 3530 OpenMPClauseKind) { 3531 return isa<CXXThisExpr>( 3532 cast<MemberExpr>( 3533 StackComponents.back().getAssociatedExpression()) 3534 ->getBase() 3535 ->IgnoreParens()); 3536 })) { 3537 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3] 3538 // A bit-field cannot appear in a map clause. 3539 // 3540 if (FD->isBitField()) 3541 return; 3542 3543 // Check to see if the member expression is referencing a class that 3544 // has already been explicitly mapped 3545 if (Stack->isClassPreviouslyMapped(TE->getType())) 3546 return; 3547 3548 OpenMPDefaultmapClauseModifier Modifier = 3549 Stack->getDefaultmapModifier(OMPC_DEFAULTMAP_aggregate); 3550 OpenMPMapClauseKind Kind = getMapClauseKindFromModifier( 3551 Modifier, /*IsAggregateOrDeclareTarget*/ true); 3552 ImplicitMap[Kind].emplace_back(E); 3553 return; 3554 } 3555 3556 SourceLocation ELoc = E->getExprLoc(); 3557 // OpenMP [2.9.3.6, Restrictions, p.2] 3558 // A list item that appears in a reduction clause of the innermost 3559 // enclosing worksharing or parallel construct may not be accessed in 3560 // an explicit task. 3561 DVar = Stack->hasInnermostDSA( 3562 FD, [](OpenMPClauseKind C) { return C == OMPC_reduction; }, 3563 [](OpenMPDirectiveKind K) { 3564 return isOpenMPParallelDirective(K) || 3565 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K); 3566 }, 3567 /*FromParent=*/true); 3568 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) { 3569 ErrorFound = true; 3570 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); 3571 reportOriginalDsa(SemaRef, Stack, FD, DVar); 3572 return; 3573 } 3574 3575 // Define implicit data-sharing attributes for task. 3576 DVar = Stack->getImplicitDSA(FD, /*FromParent=*/false); 3577 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared && 3578 !Stack->isLoopControlVariable(FD).first) { 3579 // Check if there is a captured expression for the current field in the 3580 // region. Do not mark it as firstprivate unless there is no captured 3581 // expression. 3582 // TODO: try to make it firstprivate. 3583 if (DVar.CKind != OMPC_unknown) 3584 ImplicitFirstprivate.push_back(E); 3585 } 3586 return; 3587 } 3588 if (isOpenMPTargetExecutionDirective(DKind)) { 3589 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents; 3590 if (!checkMapClauseExpressionBase(SemaRef, E, CurComponents, OMPC_map, 3591 /*NoDiagnose=*/true)) 3592 return; 3593 const auto *VD = cast<ValueDecl>( 3594 CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl()); 3595 if (!Stack->checkMappableExprComponentListsForDecl( 3596 VD, /*CurrentRegionOnly=*/true, 3597 [&CurComponents]( 3598 OMPClauseMappableExprCommon::MappableExprComponentListRef 3599 StackComponents, 3600 OpenMPClauseKind) { 3601 auto CCI = CurComponents.rbegin(); 3602 auto CCE = CurComponents.rend(); 3603 for (const auto &SC : llvm::reverse(StackComponents)) { 3604 // Do both expressions have the same kind? 3605 if (CCI->getAssociatedExpression()->getStmtClass() != 3606 SC.getAssociatedExpression()->getStmtClass()) 3607 if (!((isa<OMPArraySectionExpr>( 3608 SC.getAssociatedExpression()) || 3609 isa<OMPArrayShapingExpr>( 3610 SC.getAssociatedExpression())) && 3611 isa<ArraySubscriptExpr>( 3612 CCI->getAssociatedExpression()))) 3613 return false; 3614 3615 const Decl *CCD = CCI->getAssociatedDeclaration(); 3616 const Decl *SCD = SC.getAssociatedDeclaration(); 3617 CCD = CCD ? CCD->getCanonicalDecl() : nullptr; 3618 SCD = SCD ? SCD->getCanonicalDecl() : nullptr; 3619 if (SCD != CCD) 3620 return false; 3621 std::advance(CCI, 1); 3622 if (CCI == CCE) 3623 break; 3624 } 3625 return true; 3626 })) { 3627 Visit(E->getBase()); 3628 } 3629 } else if (!TryCaptureCXXThisMembers) { 3630 Visit(E->getBase()); 3631 } 3632 } 3633 void VisitOMPExecutableDirective(OMPExecutableDirective *S) { 3634 for (OMPClause *C : S->clauses()) { 3635 // Skip analysis of arguments of implicitly defined firstprivate clause 3636 // for task|target directives. 3637 // Skip analysis of arguments of implicitly defined map clause for target 3638 // directives. 3639 if (C && !((isa<OMPFirstprivateClause>(C) || isa<OMPMapClause>(C)) && 3640 C->isImplicit())) { 3641 for (Stmt *CC : C->children()) { 3642 if (CC) 3643 Visit(CC); 3644 } 3645 } 3646 } 3647 // Check implicitly captured variables. 3648 VisitSubCaptures(S); 3649 } 3650 void VisitStmt(Stmt *S) { 3651 for (Stmt *C : S->children()) { 3652 if (C) { 3653 // Check implicitly captured variables in the task-based directives to 3654 // check if they must be firstprivatized. 3655 Visit(C); 3656 } 3657 } 3658 } 3659 3660 void visitSubCaptures(CapturedStmt *S) { 3661 for (const CapturedStmt::Capture &Cap : S->captures()) { 3662 if (!Cap.capturesVariable() && !Cap.capturesVariableByCopy()) 3663 continue; 3664 VarDecl *VD = Cap.getCapturedVar(); 3665 // Do not try to map the variable if it or its sub-component was mapped 3666 // already. 3667 if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) && 3668 Stack->checkMappableExprComponentListsForDecl( 3669 VD, /*CurrentRegionOnly=*/true, 3670 [](OMPClauseMappableExprCommon::MappableExprComponentListRef, 3671 OpenMPClauseKind) { return true; })) 3672 continue; 3673 DeclRefExpr *DRE = buildDeclRefExpr( 3674 SemaRef, VD, VD->getType().getNonLValueExprType(SemaRef.Context), 3675 Cap.getLocation(), /*RefersToCapture=*/true); 3676 Visit(DRE); 3677 } 3678 } 3679 bool isErrorFound() const { return ErrorFound; } 3680 ArrayRef<Expr *> getImplicitFirstprivate() const { 3681 return ImplicitFirstprivate; 3682 } 3683 ArrayRef<Expr *> getImplicitMap(OpenMPDefaultmapClauseKind Kind) const { 3684 return ImplicitMap[Kind]; 3685 } 3686 const Sema::VarsWithInheritedDSAType &getVarsWithInheritedDSA() const { 3687 return VarsWithInheritedDSA; 3688 } 3689 3690 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS) 3691 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) { 3692 // Process declare target link variables for the target directives. 3693 if (isOpenMPTargetExecutionDirective(S->getCurrentDirective())) { 3694 for (DeclRefExpr *E : Stack->getLinkGlobals()) 3695 Visit(E); 3696 } 3697 } 3698 }; 3699 } // namespace 3700 3701 void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) { 3702 switch (DKind) { 3703 case OMPD_parallel: 3704 case OMPD_parallel_for: 3705 case OMPD_parallel_for_simd: 3706 case OMPD_parallel_sections: 3707 case OMPD_parallel_master: 3708 case OMPD_teams: 3709 case OMPD_teams_distribute: 3710 case OMPD_teams_distribute_simd: { 3711 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3712 QualType KmpInt32PtrTy = 3713 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3714 Sema::CapturedParamNameType Params[] = { 3715 std::make_pair(".global_tid.", KmpInt32PtrTy), 3716 std::make_pair(".bound_tid.", KmpInt32PtrTy), 3717 std::make_pair(StringRef(), QualType()) // __context with shared vars 3718 }; 3719 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3720 Params); 3721 break; 3722 } 3723 case OMPD_target_teams: 3724 case OMPD_target_parallel: 3725 case OMPD_target_parallel_for: 3726 case OMPD_target_parallel_for_simd: 3727 case OMPD_target_teams_distribute: 3728 case OMPD_target_teams_distribute_simd: { 3729 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3730 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 3731 QualType KmpInt32PtrTy = 3732 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3733 QualType Args[] = {VoidPtrTy}; 3734 FunctionProtoType::ExtProtoInfo EPI; 3735 EPI.Variadic = true; 3736 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 3737 Sema::CapturedParamNameType Params[] = { 3738 std::make_pair(".global_tid.", KmpInt32Ty), 3739 std::make_pair(".part_id.", KmpInt32PtrTy), 3740 std::make_pair(".privates.", VoidPtrTy), 3741 std::make_pair( 3742 ".copy_fn.", 3743 Context.getPointerType(CopyFnType).withConst().withRestrict()), 3744 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 3745 std::make_pair(StringRef(), QualType()) // __context with shared vars 3746 }; 3747 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3748 Params, /*OpenMPCaptureLevel=*/0); 3749 // Mark this captured region as inlined, because we don't use outlined 3750 // function directly. 3751 getCurCapturedRegion()->TheCapturedDecl->addAttr( 3752 AlwaysInlineAttr::CreateImplicit( 3753 Context, {}, AttributeCommonInfo::AS_Keyword, 3754 AlwaysInlineAttr::Keyword_forceinline)); 3755 Sema::CapturedParamNameType ParamsTarget[] = { 3756 std::make_pair(StringRef(), QualType()) // __context with shared vars 3757 }; 3758 // Start a captured region for 'target' with no implicit parameters. 3759 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3760 ParamsTarget, /*OpenMPCaptureLevel=*/1); 3761 Sema::CapturedParamNameType ParamsTeamsOrParallel[] = { 3762 std::make_pair(".global_tid.", KmpInt32PtrTy), 3763 std::make_pair(".bound_tid.", KmpInt32PtrTy), 3764 std::make_pair(StringRef(), QualType()) // __context with shared vars 3765 }; 3766 // Start a captured region for 'teams' or 'parallel'. Both regions have 3767 // the same implicit parameters. 3768 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3769 ParamsTeamsOrParallel, /*OpenMPCaptureLevel=*/2); 3770 break; 3771 } 3772 case OMPD_target: 3773 case OMPD_target_simd: { 3774 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3775 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 3776 QualType KmpInt32PtrTy = 3777 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3778 QualType Args[] = {VoidPtrTy}; 3779 FunctionProtoType::ExtProtoInfo EPI; 3780 EPI.Variadic = true; 3781 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 3782 Sema::CapturedParamNameType Params[] = { 3783 std::make_pair(".global_tid.", KmpInt32Ty), 3784 std::make_pair(".part_id.", KmpInt32PtrTy), 3785 std::make_pair(".privates.", VoidPtrTy), 3786 std::make_pair( 3787 ".copy_fn.", 3788 Context.getPointerType(CopyFnType).withConst().withRestrict()), 3789 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 3790 std::make_pair(StringRef(), QualType()) // __context with shared vars 3791 }; 3792 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3793 Params, /*OpenMPCaptureLevel=*/0); 3794 // Mark this captured region as inlined, because we don't use outlined 3795 // function directly. 3796 getCurCapturedRegion()->TheCapturedDecl->addAttr( 3797 AlwaysInlineAttr::CreateImplicit( 3798 Context, {}, AttributeCommonInfo::AS_Keyword, 3799 AlwaysInlineAttr::Keyword_forceinline)); 3800 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3801 std::make_pair(StringRef(), QualType()), 3802 /*OpenMPCaptureLevel=*/1); 3803 break; 3804 } 3805 case OMPD_atomic: 3806 case OMPD_critical: 3807 case OMPD_section: 3808 case OMPD_master: 3809 break; 3810 case OMPD_simd: 3811 case OMPD_for: 3812 case OMPD_for_simd: 3813 case OMPD_sections: 3814 case OMPD_single: 3815 case OMPD_taskgroup: 3816 case OMPD_distribute: 3817 case OMPD_distribute_simd: 3818 case OMPD_ordered: 3819 case OMPD_target_data: { 3820 Sema::CapturedParamNameType Params[] = { 3821 std::make_pair(StringRef(), QualType()) // __context with shared vars 3822 }; 3823 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3824 Params); 3825 break; 3826 } 3827 case OMPD_task: { 3828 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3829 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 3830 QualType KmpInt32PtrTy = 3831 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3832 QualType Args[] = {VoidPtrTy}; 3833 FunctionProtoType::ExtProtoInfo EPI; 3834 EPI.Variadic = true; 3835 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 3836 Sema::CapturedParamNameType Params[] = { 3837 std::make_pair(".global_tid.", KmpInt32Ty), 3838 std::make_pair(".part_id.", KmpInt32PtrTy), 3839 std::make_pair(".privates.", VoidPtrTy), 3840 std::make_pair( 3841 ".copy_fn.", 3842 Context.getPointerType(CopyFnType).withConst().withRestrict()), 3843 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 3844 std::make_pair(StringRef(), QualType()) // __context with shared vars 3845 }; 3846 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3847 Params); 3848 // Mark this captured region as inlined, because we don't use outlined 3849 // function directly. 3850 getCurCapturedRegion()->TheCapturedDecl->addAttr( 3851 AlwaysInlineAttr::CreateImplicit( 3852 Context, {}, AttributeCommonInfo::AS_Keyword, 3853 AlwaysInlineAttr::Keyword_forceinline)); 3854 break; 3855 } 3856 case OMPD_taskloop: 3857 case OMPD_taskloop_simd: 3858 case OMPD_master_taskloop: 3859 case OMPD_master_taskloop_simd: { 3860 QualType KmpInt32Ty = 3861 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1) 3862 .withConst(); 3863 QualType KmpUInt64Ty = 3864 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0) 3865 .withConst(); 3866 QualType KmpInt64Ty = 3867 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1) 3868 .withConst(); 3869 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 3870 QualType KmpInt32PtrTy = 3871 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3872 QualType Args[] = {VoidPtrTy}; 3873 FunctionProtoType::ExtProtoInfo EPI; 3874 EPI.Variadic = true; 3875 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 3876 Sema::CapturedParamNameType Params[] = { 3877 std::make_pair(".global_tid.", KmpInt32Ty), 3878 std::make_pair(".part_id.", KmpInt32PtrTy), 3879 std::make_pair(".privates.", VoidPtrTy), 3880 std::make_pair( 3881 ".copy_fn.", 3882 Context.getPointerType(CopyFnType).withConst().withRestrict()), 3883 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 3884 std::make_pair(".lb.", KmpUInt64Ty), 3885 std::make_pair(".ub.", KmpUInt64Ty), 3886 std::make_pair(".st.", KmpInt64Ty), 3887 std::make_pair(".liter.", KmpInt32Ty), 3888 std::make_pair(".reductions.", VoidPtrTy), 3889 std::make_pair(StringRef(), QualType()) // __context with shared vars 3890 }; 3891 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3892 Params); 3893 // Mark this captured region as inlined, because we don't use outlined 3894 // function directly. 3895 getCurCapturedRegion()->TheCapturedDecl->addAttr( 3896 AlwaysInlineAttr::CreateImplicit( 3897 Context, {}, AttributeCommonInfo::AS_Keyword, 3898 AlwaysInlineAttr::Keyword_forceinline)); 3899 break; 3900 } 3901 case OMPD_parallel_master_taskloop: 3902 case OMPD_parallel_master_taskloop_simd: { 3903 QualType KmpInt32Ty = 3904 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1) 3905 .withConst(); 3906 QualType KmpUInt64Ty = 3907 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0) 3908 .withConst(); 3909 QualType KmpInt64Ty = 3910 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1) 3911 .withConst(); 3912 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 3913 QualType KmpInt32PtrTy = 3914 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3915 Sema::CapturedParamNameType ParamsParallel[] = { 3916 std::make_pair(".global_tid.", KmpInt32PtrTy), 3917 std::make_pair(".bound_tid.", KmpInt32PtrTy), 3918 std::make_pair(StringRef(), QualType()) // __context with shared vars 3919 }; 3920 // Start a captured region for 'parallel'. 3921 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3922 ParamsParallel, /*OpenMPCaptureLevel=*/0); 3923 QualType Args[] = {VoidPtrTy}; 3924 FunctionProtoType::ExtProtoInfo EPI; 3925 EPI.Variadic = true; 3926 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 3927 Sema::CapturedParamNameType Params[] = { 3928 std::make_pair(".global_tid.", KmpInt32Ty), 3929 std::make_pair(".part_id.", KmpInt32PtrTy), 3930 std::make_pair(".privates.", VoidPtrTy), 3931 std::make_pair( 3932 ".copy_fn.", 3933 Context.getPointerType(CopyFnType).withConst().withRestrict()), 3934 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 3935 std::make_pair(".lb.", KmpUInt64Ty), 3936 std::make_pair(".ub.", KmpUInt64Ty), 3937 std::make_pair(".st.", KmpInt64Ty), 3938 std::make_pair(".liter.", KmpInt32Ty), 3939 std::make_pair(".reductions.", VoidPtrTy), 3940 std::make_pair(StringRef(), QualType()) // __context with shared vars 3941 }; 3942 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3943 Params, /*OpenMPCaptureLevel=*/1); 3944 // Mark this captured region as inlined, because we don't use outlined 3945 // function directly. 3946 getCurCapturedRegion()->TheCapturedDecl->addAttr( 3947 AlwaysInlineAttr::CreateImplicit( 3948 Context, {}, AttributeCommonInfo::AS_Keyword, 3949 AlwaysInlineAttr::Keyword_forceinline)); 3950 break; 3951 } 3952 case OMPD_distribute_parallel_for_simd: 3953 case OMPD_distribute_parallel_for: { 3954 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3955 QualType KmpInt32PtrTy = 3956 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3957 Sema::CapturedParamNameType Params[] = { 3958 std::make_pair(".global_tid.", KmpInt32PtrTy), 3959 std::make_pair(".bound_tid.", KmpInt32PtrTy), 3960 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 3961 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 3962 std::make_pair(StringRef(), QualType()) // __context with shared vars 3963 }; 3964 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3965 Params); 3966 break; 3967 } 3968 case OMPD_target_teams_distribute_parallel_for: 3969 case OMPD_target_teams_distribute_parallel_for_simd: { 3970 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3971 QualType KmpInt32PtrTy = 3972 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3973 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 3974 3975 QualType Args[] = {VoidPtrTy}; 3976 FunctionProtoType::ExtProtoInfo EPI; 3977 EPI.Variadic = true; 3978 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 3979 Sema::CapturedParamNameType Params[] = { 3980 std::make_pair(".global_tid.", KmpInt32Ty), 3981 std::make_pair(".part_id.", KmpInt32PtrTy), 3982 std::make_pair(".privates.", VoidPtrTy), 3983 std::make_pair( 3984 ".copy_fn.", 3985 Context.getPointerType(CopyFnType).withConst().withRestrict()), 3986 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 3987 std::make_pair(StringRef(), QualType()) // __context with shared vars 3988 }; 3989 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3990 Params, /*OpenMPCaptureLevel=*/0); 3991 // Mark this captured region as inlined, because we don't use outlined 3992 // function directly. 3993 getCurCapturedRegion()->TheCapturedDecl->addAttr( 3994 AlwaysInlineAttr::CreateImplicit( 3995 Context, {}, AttributeCommonInfo::AS_Keyword, 3996 AlwaysInlineAttr::Keyword_forceinline)); 3997 Sema::CapturedParamNameType ParamsTarget[] = { 3998 std::make_pair(StringRef(), QualType()) // __context with shared vars 3999 }; 4000 // Start a captured region for 'target' with no implicit parameters. 4001 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4002 ParamsTarget, /*OpenMPCaptureLevel=*/1); 4003 4004 Sema::CapturedParamNameType ParamsTeams[] = { 4005 std::make_pair(".global_tid.", KmpInt32PtrTy), 4006 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4007 std::make_pair(StringRef(), QualType()) // __context with shared vars 4008 }; 4009 // Start a captured region for 'target' with no implicit parameters. 4010 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4011 ParamsTeams, /*OpenMPCaptureLevel=*/2); 4012 4013 Sema::CapturedParamNameType ParamsParallel[] = { 4014 std::make_pair(".global_tid.", KmpInt32PtrTy), 4015 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4016 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 4017 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 4018 std::make_pair(StringRef(), QualType()) // __context with shared vars 4019 }; 4020 // Start a captured region for 'teams' or 'parallel'. Both regions have 4021 // the same implicit parameters. 4022 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4023 ParamsParallel, /*OpenMPCaptureLevel=*/3); 4024 break; 4025 } 4026 4027 case OMPD_teams_distribute_parallel_for: 4028 case OMPD_teams_distribute_parallel_for_simd: { 4029 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 4030 QualType KmpInt32PtrTy = 4031 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4032 4033 Sema::CapturedParamNameType ParamsTeams[] = { 4034 std::make_pair(".global_tid.", KmpInt32PtrTy), 4035 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4036 std::make_pair(StringRef(), QualType()) // __context with shared vars 4037 }; 4038 // Start a captured region for 'target' with no implicit parameters. 4039 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4040 ParamsTeams, /*OpenMPCaptureLevel=*/0); 4041 4042 Sema::CapturedParamNameType ParamsParallel[] = { 4043 std::make_pair(".global_tid.", KmpInt32PtrTy), 4044 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4045 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 4046 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 4047 std::make_pair(StringRef(), QualType()) // __context with shared vars 4048 }; 4049 // Start a captured region for 'teams' or 'parallel'. Both regions have 4050 // the same implicit parameters. 4051 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4052 ParamsParallel, /*OpenMPCaptureLevel=*/1); 4053 break; 4054 } 4055 case OMPD_target_update: 4056 case OMPD_target_enter_data: 4057 case OMPD_target_exit_data: { 4058 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 4059 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 4060 QualType KmpInt32PtrTy = 4061 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4062 QualType Args[] = {VoidPtrTy}; 4063 FunctionProtoType::ExtProtoInfo EPI; 4064 EPI.Variadic = true; 4065 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 4066 Sema::CapturedParamNameType Params[] = { 4067 std::make_pair(".global_tid.", KmpInt32Ty), 4068 std::make_pair(".part_id.", KmpInt32PtrTy), 4069 std::make_pair(".privates.", VoidPtrTy), 4070 std::make_pair( 4071 ".copy_fn.", 4072 Context.getPointerType(CopyFnType).withConst().withRestrict()), 4073 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 4074 std::make_pair(StringRef(), QualType()) // __context with shared vars 4075 }; 4076 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4077 Params); 4078 // Mark this captured region as inlined, because we don't use outlined 4079 // function directly. 4080 getCurCapturedRegion()->TheCapturedDecl->addAttr( 4081 AlwaysInlineAttr::CreateImplicit( 4082 Context, {}, AttributeCommonInfo::AS_Keyword, 4083 AlwaysInlineAttr::Keyword_forceinline)); 4084 break; 4085 } 4086 case OMPD_threadprivate: 4087 case OMPD_allocate: 4088 case OMPD_taskyield: 4089 case OMPD_barrier: 4090 case OMPD_taskwait: 4091 case OMPD_cancellation_point: 4092 case OMPD_cancel: 4093 case OMPD_flush: 4094 case OMPD_depobj: 4095 case OMPD_scan: 4096 case OMPD_declare_reduction: 4097 case OMPD_declare_mapper: 4098 case OMPD_declare_simd: 4099 case OMPD_declare_target: 4100 case OMPD_end_declare_target: 4101 case OMPD_requires: 4102 case OMPD_declare_variant: 4103 case OMPD_begin_declare_variant: 4104 case OMPD_end_declare_variant: 4105 llvm_unreachable("OpenMP Directive is not allowed"); 4106 case OMPD_unknown: 4107 default: 4108 llvm_unreachable("Unknown OpenMP directive"); 4109 } 4110 } 4111 4112 int Sema::getNumberOfConstructScopes(unsigned Level) const { 4113 return getOpenMPCaptureLevels(DSAStack->getDirective(Level)); 4114 } 4115 4116 int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) { 4117 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 4118 getOpenMPCaptureRegions(CaptureRegions, DKind); 4119 return CaptureRegions.size(); 4120 } 4121 4122 static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id, 4123 Expr *CaptureExpr, bool WithInit, 4124 bool AsExpression) { 4125 assert(CaptureExpr); 4126 ASTContext &C = S.getASTContext(); 4127 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts(); 4128 QualType Ty = Init->getType(); 4129 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) { 4130 if (S.getLangOpts().CPlusPlus) { 4131 Ty = C.getLValueReferenceType(Ty); 4132 } else { 4133 Ty = C.getPointerType(Ty); 4134 ExprResult Res = 4135 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init); 4136 if (!Res.isUsable()) 4137 return nullptr; 4138 Init = Res.get(); 4139 } 4140 WithInit = true; 4141 } 4142 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty, 4143 CaptureExpr->getBeginLoc()); 4144 if (!WithInit) 4145 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C)); 4146 S.CurContext->addHiddenDecl(CED); 4147 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false); 4148 return CED; 4149 } 4150 4151 static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr, 4152 bool WithInit) { 4153 OMPCapturedExprDecl *CD; 4154 if (VarDecl *VD = S.isOpenMPCapturedDecl(D)) 4155 CD = cast<OMPCapturedExprDecl>(VD); 4156 else 4157 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit, 4158 /*AsExpression=*/false); 4159 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(), 4160 CaptureExpr->getExprLoc()); 4161 } 4162 4163 static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) { 4164 CaptureExpr = S.DefaultLvalueConversion(CaptureExpr).get(); 4165 if (!Ref) { 4166 OMPCapturedExprDecl *CD = buildCaptureDecl( 4167 S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr, 4168 /*WithInit=*/true, /*AsExpression=*/true); 4169 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(), 4170 CaptureExpr->getExprLoc()); 4171 } 4172 ExprResult Res = Ref; 4173 if (!S.getLangOpts().CPlusPlus && 4174 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() && 4175 Ref->getType()->isPointerType()) { 4176 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref); 4177 if (!Res.isUsable()) 4178 return ExprError(); 4179 } 4180 return S.DefaultLvalueConversion(Res.get()); 4181 } 4182 4183 namespace { 4184 // OpenMP directives parsed in this section are represented as a 4185 // CapturedStatement with an associated statement. If a syntax error 4186 // is detected during the parsing of the associated statement, the 4187 // compiler must abort processing and close the CapturedStatement. 4188 // 4189 // Combined directives such as 'target parallel' have more than one 4190 // nested CapturedStatements. This RAII ensures that we unwind out 4191 // of all the nested CapturedStatements when an error is found. 4192 class CaptureRegionUnwinderRAII { 4193 private: 4194 Sema &S; 4195 bool &ErrorFound; 4196 OpenMPDirectiveKind DKind = OMPD_unknown; 4197 4198 public: 4199 CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound, 4200 OpenMPDirectiveKind DKind) 4201 : S(S), ErrorFound(ErrorFound), DKind(DKind) {} 4202 ~CaptureRegionUnwinderRAII() { 4203 if (ErrorFound) { 4204 int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind); 4205 while (--ThisCaptureLevel >= 0) 4206 S.ActOnCapturedRegionError(); 4207 } 4208 } 4209 }; 4210 } // namespace 4211 4212 void Sema::tryCaptureOpenMPLambdas(ValueDecl *V) { 4213 // Capture variables captured by reference in lambdas for target-based 4214 // directives. 4215 if (!CurContext->isDependentContext() && 4216 (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) || 4217 isOpenMPTargetDataManagementDirective( 4218 DSAStack->getCurrentDirective()))) { 4219 QualType Type = V->getType(); 4220 if (const auto *RD = Type.getCanonicalType() 4221 .getNonReferenceType() 4222 ->getAsCXXRecordDecl()) { 4223 bool SavedForceCaptureByReferenceInTargetExecutable = 4224 DSAStack->isForceCaptureByReferenceInTargetExecutable(); 4225 DSAStack->setForceCaptureByReferenceInTargetExecutable( 4226 /*V=*/true); 4227 if (RD->isLambda()) { 4228 llvm::DenseMap<const VarDecl *, FieldDecl *> Captures; 4229 FieldDecl *ThisCapture; 4230 RD->getCaptureFields(Captures, ThisCapture); 4231 for (const LambdaCapture &LC : RD->captures()) { 4232 if (LC.getCaptureKind() == LCK_ByRef) { 4233 VarDecl *VD = LC.getCapturedVar(); 4234 DeclContext *VDC = VD->getDeclContext(); 4235 if (!VDC->Encloses(CurContext)) 4236 continue; 4237 MarkVariableReferenced(LC.getLocation(), VD); 4238 } else if (LC.getCaptureKind() == LCK_This) { 4239 QualType ThisTy = getCurrentThisType(); 4240 if (!ThisTy.isNull() && 4241 Context.typesAreCompatible(ThisTy, ThisCapture->getType())) 4242 CheckCXXThisCapture(LC.getLocation()); 4243 } 4244 } 4245 } 4246 DSAStack->setForceCaptureByReferenceInTargetExecutable( 4247 SavedForceCaptureByReferenceInTargetExecutable); 4248 } 4249 } 4250 } 4251 4252 static bool checkOrderedOrderSpecified(Sema &S, 4253 const ArrayRef<OMPClause *> Clauses) { 4254 const OMPOrderedClause *Ordered = nullptr; 4255 const OMPOrderClause *Order = nullptr; 4256 4257 for (const OMPClause *Clause : Clauses) { 4258 if (Clause->getClauseKind() == OMPC_ordered) 4259 Ordered = cast<OMPOrderedClause>(Clause); 4260 else if (Clause->getClauseKind() == OMPC_order) { 4261 Order = cast<OMPOrderClause>(Clause); 4262 if (Order->getKind() != OMPC_ORDER_concurrent) 4263 Order = nullptr; 4264 } 4265 if (Ordered && Order) 4266 break; 4267 } 4268 4269 if (Ordered && Order) { 4270 S.Diag(Order->getKindKwLoc(), 4271 diag::err_omp_simple_clause_incompatible_with_ordered) 4272 << getOpenMPClauseName(OMPC_order) 4273 << getOpenMPSimpleClauseTypeName(OMPC_order, OMPC_ORDER_concurrent) 4274 << SourceRange(Order->getBeginLoc(), Order->getEndLoc()); 4275 S.Diag(Ordered->getBeginLoc(), diag::note_omp_ordered_param) 4276 << 0 << SourceRange(Ordered->getBeginLoc(), Ordered->getEndLoc()); 4277 return true; 4278 } 4279 return false; 4280 } 4281 4282 StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S, 4283 ArrayRef<OMPClause *> Clauses) { 4284 if (DSAStack->getCurrentDirective() == OMPD_atomic || 4285 DSAStack->getCurrentDirective() == OMPD_critical || 4286 DSAStack->getCurrentDirective() == OMPD_section || 4287 DSAStack->getCurrentDirective() == OMPD_master) 4288 return S; 4289 4290 bool ErrorFound = false; 4291 CaptureRegionUnwinderRAII CaptureRegionUnwinder( 4292 *this, ErrorFound, DSAStack->getCurrentDirective()); 4293 if (!S.isUsable()) { 4294 ErrorFound = true; 4295 return StmtError(); 4296 } 4297 4298 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 4299 getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective()); 4300 OMPOrderedClause *OC = nullptr; 4301 OMPScheduleClause *SC = nullptr; 4302 SmallVector<const OMPLinearClause *, 4> LCs; 4303 SmallVector<const OMPClauseWithPreInit *, 4> PICs; 4304 // This is required for proper codegen. 4305 for (OMPClause *Clause : Clauses) { 4306 if (!LangOpts.OpenMPSimd && 4307 isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) && 4308 Clause->getClauseKind() == OMPC_in_reduction) { 4309 // Capture taskgroup task_reduction descriptors inside the tasking regions 4310 // with the corresponding in_reduction items. 4311 auto *IRC = cast<OMPInReductionClause>(Clause); 4312 for (Expr *E : IRC->taskgroup_descriptors()) 4313 if (E) 4314 MarkDeclarationsReferencedInExpr(E); 4315 } 4316 if (isOpenMPPrivate(Clause->getClauseKind()) || 4317 Clause->getClauseKind() == OMPC_copyprivate || 4318 (getLangOpts().OpenMPUseTLS && 4319 getASTContext().getTargetInfo().isTLSSupported() && 4320 Clause->getClauseKind() == OMPC_copyin)) { 4321 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin); 4322 // Mark all variables in private list clauses as used in inner region. 4323 for (Stmt *VarRef : Clause->children()) { 4324 if (auto *E = cast_or_null<Expr>(VarRef)) { 4325 MarkDeclarationsReferencedInExpr(E); 4326 } 4327 } 4328 DSAStack->setForceVarCapturing(/*V=*/false); 4329 } else if (CaptureRegions.size() > 1 || 4330 CaptureRegions.back() != OMPD_unknown) { 4331 if (auto *C = OMPClauseWithPreInit::get(Clause)) 4332 PICs.push_back(C); 4333 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) { 4334 if (Expr *E = C->getPostUpdateExpr()) 4335 MarkDeclarationsReferencedInExpr(E); 4336 } 4337 } 4338 if (Clause->getClauseKind() == OMPC_schedule) 4339 SC = cast<OMPScheduleClause>(Clause); 4340 else if (Clause->getClauseKind() == OMPC_ordered) 4341 OC = cast<OMPOrderedClause>(Clause); 4342 else if (Clause->getClauseKind() == OMPC_linear) 4343 LCs.push_back(cast<OMPLinearClause>(Clause)); 4344 } 4345 // Capture allocator expressions if used. 4346 for (Expr *E : DSAStack->getInnerAllocators()) 4347 MarkDeclarationsReferencedInExpr(E); 4348 // OpenMP, 2.7.1 Loop Construct, Restrictions 4349 // The nonmonotonic modifier cannot be specified if an ordered clause is 4350 // specified. 4351 if (SC && 4352 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic || 4353 SC->getSecondScheduleModifier() == 4354 OMPC_SCHEDULE_MODIFIER_nonmonotonic) && 4355 OC) { 4356 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic 4357 ? SC->getFirstScheduleModifierLoc() 4358 : SC->getSecondScheduleModifierLoc(), 4359 diag::err_omp_simple_clause_incompatible_with_ordered) 4360 << getOpenMPClauseName(OMPC_schedule) 4361 << getOpenMPSimpleClauseTypeName(OMPC_schedule, 4362 OMPC_SCHEDULE_MODIFIER_nonmonotonic) 4363 << SourceRange(OC->getBeginLoc(), OC->getEndLoc()); 4364 ErrorFound = true; 4365 } 4366 // OpenMP 5.0, 2.9.2 Worksharing-Loop Construct, Restrictions. 4367 // If an order(concurrent) clause is present, an ordered clause may not appear 4368 // on the same directive. 4369 if (checkOrderedOrderSpecified(*this, Clauses)) 4370 ErrorFound = true; 4371 if (!LCs.empty() && OC && OC->getNumForLoops()) { 4372 for (const OMPLinearClause *C : LCs) { 4373 Diag(C->getBeginLoc(), diag::err_omp_linear_ordered) 4374 << SourceRange(OC->getBeginLoc(), OC->getEndLoc()); 4375 } 4376 ErrorFound = true; 4377 } 4378 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) && 4379 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC && 4380 OC->getNumForLoops()) { 4381 Diag(OC->getBeginLoc(), diag::err_omp_ordered_simd) 4382 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 4383 ErrorFound = true; 4384 } 4385 if (ErrorFound) { 4386 return StmtError(); 4387 } 4388 StmtResult SR = S; 4389 unsigned CompletedRegions = 0; 4390 for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(CaptureRegions)) { 4391 // Mark all variables in private list clauses as used in inner region. 4392 // Required for proper codegen of combined directives. 4393 // TODO: add processing for other clauses. 4394 if (ThisCaptureRegion != OMPD_unknown) { 4395 for (const clang::OMPClauseWithPreInit *C : PICs) { 4396 OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion(); 4397 // Find the particular capture region for the clause if the 4398 // directive is a combined one with multiple capture regions. 4399 // If the directive is not a combined one, the capture region 4400 // associated with the clause is OMPD_unknown and is generated 4401 // only once. 4402 if (CaptureRegion == ThisCaptureRegion || 4403 CaptureRegion == OMPD_unknown) { 4404 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) { 4405 for (Decl *D : DS->decls()) 4406 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D)); 4407 } 4408 } 4409 } 4410 } 4411 if (ThisCaptureRegion == OMPD_target) { 4412 // Capture allocator traits in the target region. They are used implicitly 4413 // and, thus, are not captured by default. 4414 for (OMPClause *C : Clauses) { 4415 if (const auto *UAC = dyn_cast<OMPUsesAllocatorsClause>(C)) { 4416 for (unsigned I = 0, End = UAC->getNumberOfAllocators(); I < End; 4417 ++I) { 4418 OMPUsesAllocatorsClause::Data D = UAC->getAllocatorData(I); 4419 if (Expr *E = D.AllocatorTraits) 4420 MarkDeclarationsReferencedInExpr(E); 4421 } 4422 continue; 4423 } 4424 } 4425 } 4426 if (++CompletedRegions == CaptureRegions.size()) 4427 DSAStack->setBodyComplete(); 4428 SR = ActOnCapturedRegionEnd(SR.get()); 4429 } 4430 return SR; 4431 } 4432 4433 static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion, 4434 OpenMPDirectiveKind CancelRegion, 4435 SourceLocation StartLoc) { 4436 // CancelRegion is only needed for cancel and cancellation_point. 4437 if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point) 4438 return false; 4439 4440 if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for || 4441 CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup) 4442 return false; 4443 4444 SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region) 4445 << getOpenMPDirectiveName(CancelRegion); 4446 return true; 4447 } 4448 4449 static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack, 4450 OpenMPDirectiveKind CurrentRegion, 4451 const DeclarationNameInfo &CurrentName, 4452 OpenMPDirectiveKind CancelRegion, 4453 SourceLocation StartLoc) { 4454 if (Stack->getCurScope()) { 4455 OpenMPDirectiveKind ParentRegion = Stack->getParentDirective(); 4456 OpenMPDirectiveKind OffendingRegion = ParentRegion; 4457 bool NestingProhibited = false; 4458 bool CloseNesting = true; 4459 bool OrphanSeen = false; 4460 enum { 4461 NoRecommend, 4462 ShouldBeInParallelRegion, 4463 ShouldBeInOrderedRegion, 4464 ShouldBeInTargetRegion, 4465 ShouldBeInTeamsRegion, 4466 ShouldBeInLoopSimdRegion, 4467 } Recommend = NoRecommend; 4468 if (isOpenMPSimdDirective(ParentRegion) && 4469 ((SemaRef.LangOpts.OpenMP <= 45 && CurrentRegion != OMPD_ordered) || 4470 (SemaRef.LangOpts.OpenMP >= 50 && CurrentRegion != OMPD_ordered && 4471 CurrentRegion != OMPD_simd && CurrentRegion != OMPD_atomic && 4472 CurrentRegion != OMPD_scan))) { 4473 // OpenMP [2.16, Nesting of Regions] 4474 // OpenMP constructs may not be nested inside a simd region. 4475 // OpenMP [2.8.1,simd Construct, Restrictions] 4476 // An ordered construct with the simd clause is the only OpenMP 4477 // construct that can appear in the simd region. 4478 // Allowing a SIMD construct nested in another SIMD construct is an 4479 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning 4480 // message. 4481 // OpenMP 5.0 [2.9.3.1, simd Construct, Restrictions] 4482 // The only OpenMP constructs that can be encountered during execution of 4483 // a simd region are the atomic construct, the loop construct, the simd 4484 // construct and the ordered construct with the simd clause. 4485 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd) 4486 ? diag::err_omp_prohibited_region_simd 4487 : diag::warn_omp_nesting_simd) 4488 << (SemaRef.LangOpts.OpenMP >= 50 ? 1 : 0); 4489 return CurrentRegion != OMPD_simd; 4490 } 4491 if (ParentRegion == OMPD_atomic) { 4492 // OpenMP [2.16, Nesting of Regions] 4493 // OpenMP constructs may not be nested inside an atomic region. 4494 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic); 4495 return true; 4496 } 4497 if (CurrentRegion == OMPD_section) { 4498 // OpenMP [2.7.2, sections Construct, Restrictions] 4499 // Orphaned section directives are prohibited. That is, the section 4500 // directives must appear within the sections construct and must not be 4501 // encountered elsewhere in the sections region. 4502 if (ParentRegion != OMPD_sections && 4503 ParentRegion != OMPD_parallel_sections) { 4504 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive) 4505 << (ParentRegion != OMPD_unknown) 4506 << getOpenMPDirectiveName(ParentRegion); 4507 return true; 4508 } 4509 return false; 4510 } 4511 // Allow some constructs (except teams and cancellation constructs) to be 4512 // orphaned (they could be used in functions, called from OpenMP regions 4513 // with the required preconditions). 4514 if (ParentRegion == OMPD_unknown && 4515 !isOpenMPNestingTeamsDirective(CurrentRegion) && 4516 CurrentRegion != OMPD_cancellation_point && 4517 CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_scan) 4518 return false; 4519 if (CurrentRegion == OMPD_cancellation_point || 4520 CurrentRegion == OMPD_cancel) { 4521 // OpenMP [2.16, Nesting of Regions] 4522 // A cancellation point construct for which construct-type-clause is 4523 // taskgroup must be nested inside a task construct. A cancellation 4524 // point construct for which construct-type-clause is not taskgroup must 4525 // be closely nested inside an OpenMP construct that matches the type 4526 // specified in construct-type-clause. 4527 // A cancel construct for which construct-type-clause is taskgroup must be 4528 // nested inside a task construct. A cancel construct for which 4529 // construct-type-clause is not taskgroup must be closely nested inside an 4530 // OpenMP construct that matches the type specified in 4531 // construct-type-clause. 4532 NestingProhibited = 4533 !((CancelRegion == OMPD_parallel && 4534 (ParentRegion == OMPD_parallel || 4535 ParentRegion == OMPD_target_parallel)) || 4536 (CancelRegion == OMPD_for && 4537 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for || 4538 ParentRegion == OMPD_target_parallel_for || 4539 ParentRegion == OMPD_distribute_parallel_for || 4540 ParentRegion == OMPD_teams_distribute_parallel_for || 4541 ParentRegion == OMPD_target_teams_distribute_parallel_for)) || 4542 (CancelRegion == OMPD_taskgroup && 4543 (ParentRegion == OMPD_task || 4544 (SemaRef.getLangOpts().OpenMP >= 50 && 4545 (ParentRegion == OMPD_taskloop || 4546 ParentRegion == OMPD_master_taskloop || 4547 ParentRegion == OMPD_parallel_master_taskloop)))) || 4548 (CancelRegion == OMPD_sections && 4549 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections || 4550 ParentRegion == OMPD_parallel_sections))); 4551 OrphanSeen = ParentRegion == OMPD_unknown; 4552 } else if (CurrentRegion == OMPD_master) { 4553 // OpenMP [2.16, Nesting of Regions] 4554 // A master region may not be closely nested inside a worksharing, 4555 // atomic, or explicit task region. 4556 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 4557 isOpenMPTaskingDirective(ParentRegion); 4558 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) { 4559 // OpenMP [2.16, Nesting of Regions] 4560 // A critical region may not be nested (closely or otherwise) inside a 4561 // critical region with the same name. Note that this restriction is not 4562 // sufficient to prevent deadlock. 4563 SourceLocation PreviousCriticalLoc; 4564 bool DeadLock = Stack->hasDirective( 4565 [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K, 4566 const DeclarationNameInfo &DNI, 4567 SourceLocation Loc) { 4568 if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) { 4569 PreviousCriticalLoc = Loc; 4570 return true; 4571 } 4572 return false; 4573 }, 4574 false /* skip top directive */); 4575 if (DeadLock) { 4576 SemaRef.Diag(StartLoc, 4577 diag::err_omp_prohibited_region_critical_same_name) 4578 << CurrentName.getName(); 4579 if (PreviousCriticalLoc.isValid()) 4580 SemaRef.Diag(PreviousCriticalLoc, 4581 diag::note_omp_previous_critical_region); 4582 return true; 4583 } 4584 } else if (CurrentRegion == OMPD_barrier) { 4585 // OpenMP [2.16, Nesting of Regions] 4586 // A barrier region may not be closely nested inside a worksharing, 4587 // explicit task, critical, ordered, atomic, or master region. 4588 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 4589 isOpenMPTaskingDirective(ParentRegion) || 4590 ParentRegion == OMPD_master || 4591 ParentRegion == OMPD_parallel_master || 4592 ParentRegion == OMPD_critical || 4593 ParentRegion == OMPD_ordered; 4594 } else if (isOpenMPWorksharingDirective(CurrentRegion) && 4595 !isOpenMPParallelDirective(CurrentRegion) && 4596 !isOpenMPTeamsDirective(CurrentRegion)) { 4597 // OpenMP [2.16, Nesting of Regions] 4598 // A worksharing region may not be closely nested inside a worksharing, 4599 // explicit task, critical, ordered, atomic, or master region. 4600 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 4601 isOpenMPTaskingDirective(ParentRegion) || 4602 ParentRegion == OMPD_master || 4603 ParentRegion == OMPD_parallel_master || 4604 ParentRegion == OMPD_critical || 4605 ParentRegion == OMPD_ordered; 4606 Recommend = ShouldBeInParallelRegion; 4607 } else if (CurrentRegion == OMPD_ordered) { 4608 // OpenMP [2.16, Nesting of Regions] 4609 // An ordered region may not be closely nested inside a critical, 4610 // atomic, or explicit task region. 4611 // An ordered region must be closely nested inside a loop region (or 4612 // parallel loop region) with an ordered clause. 4613 // OpenMP [2.8.1,simd Construct, Restrictions] 4614 // An ordered construct with the simd clause is the only OpenMP construct 4615 // that can appear in the simd region. 4616 NestingProhibited = ParentRegion == OMPD_critical || 4617 isOpenMPTaskingDirective(ParentRegion) || 4618 !(isOpenMPSimdDirective(ParentRegion) || 4619 Stack->isParentOrderedRegion()); 4620 Recommend = ShouldBeInOrderedRegion; 4621 } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) { 4622 // OpenMP [2.16, Nesting of Regions] 4623 // If specified, a teams construct must be contained within a target 4624 // construct. 4625 NestingProhibited = 4626 (SemaRef.LangOpts.OpenMP <= 45 && ParentRegion != OMPD_target) || 4627 (SemaRef.LangOpts.OpenMP >= 50 && ParentRegion != OMPD_unknown && 4628 ParentRegion != OMPD_target); 4629 OrphanSeen = ParentRegion == OMPD_unknown; 4630 Recommend = ShouldBeInTargetRegion; 4631 } else if (CurrentRegion == OMPD_scan) { 4632 // OpenMP [2.16, Nesting of Regions] 4633 // If specified, a teams construct must be contained within a target 4634 // construct. 4635 NestingProhibited = 4636 SemaRef.LangOpts.OpenMP < 50 || 4637 (ParentRegion != OMPD_simd && ParentRegion != OMPD_for && 4638 ParentRegion != OMPD_for_simd && ParentRegion != OMPD_parallel_for && 4639 ParentRegion != OMPD_parallel_for_simd); 4640 OrphanSeen = ParentRegion == OMPD_unknown; 4641 Recommend = ShouldBeInLoopSimdRegion; 4642 } 4643 if (!NestingProhibited && 4644 !isOpenMPTargetExecutionDirective(CurrentRegion) && 4645 !isOpenMPTargetDataManagementDirective(CurrentRegion) && 4646 (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) { 4647 // OpenMP [2.16, Nesting of Regions] 4648 // distribute, parallel, parallel sections, parallel workshare, and the 4649 // parallel loop and parallel loop SIMD constructs are the only OpenMP 4650 // constructs that can be closely nested in the teams region. 4651 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) && 4652 !isOpenMPDistributeDirective(CurrentRegion); 4653 Recommend = ShouldBeInParallelRegion; 4654 } 4655 if (!NestingProhibited && 4656 isOpenMPNestingDistributeDirective(CurrentRegion)) { 4657 // OpenMP 4.5 [2.17 Nesting of Regions] 4658 // The region associated with the distribute construct must be strictly 4659 // nested inside a teams region 4660 NestingProhibited = 4661 (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams); 4662 Recommend = ShouldBeInTeamsRegion; 4663 } 4664 if (!NestingProhibited && 4665 (isOpenMPTargetExecutionDirective(CurrentRegion) || 4666 isOpenMPTargetDataManagementDirective(CurrentRegion))) { 4667 // OpenMP 4.5 [2.17 Nesting of Regions] 4668 // If a target, target update, target data, target enter data, or 4669 // target exit data construct is encountered during execution of a 4670 // target region, the behavior is unspecified. 4671 NestingProhibited = Stack->hasDirective( 4672 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &, 4673 SourceLocation) { 4674 if (isOpenMPTargetExecutionDirective(K)) { 4675 OffendingRegion = K; 4676 return true; 4677 } 4678 return false; 4679 }, 4680 false /* don't skip top directive */); 4681 CloseNesting = false; 4682 } 4683 if (NestingProhibited) { 4684 if (OrphanSeen) { 4685 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive) 4686 << getOpenMPDirectiveName(CurrentRegion) << Recommend; 4687 } else { 4688 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region) 4689 << CloseNesting << getOpenMPDirectiveName(OffendingRegion) 4690 << Recommend << getOpenMPDirectiveName(CurrentRegion); 4691 } 4692 return true; 4693 } 4694 } 4695 return false; 4696 } 4697 4698 struct Kind2Unsigned { 4699 using argument_type = OpenMPDirectiveKind; 4700 unsigned operator()(argument_type DK) { return unsigned(DK); } 4701 }; 4702 static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind, 4703 ArrayRef<OMPClause *> Clauses, 4704 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) { 4705 bool ErrorFound = false; 4706 unsigned NamedModifiersNumber = 0; 4707 llvm::IndexedMap<const OMPIfClause *, Kind2Unsigned> FoundNameModifiers; 4708 FoundNameModifiers.resize(llvm::omp::Directive_enumSize + 1); 4709 SmallVector<SourceLocation, 4> NameModifierLoc; 4710 for (const OMPClause *C : Clauses) { 4711 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) { 4712 // At most one if clause without a directive-name-modifier can appear on 4713 // the directive. 4714 OpenMPDirectiveKind CurNM = IC->getNameModifier(); 4715 if (FoundNameModifiers[CurNM]) { 4716 S.Diag(C->getBeginLoc(), diag::err_omp_more_one_clause) 4717 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if) 4718 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM); 4719 ErrorFound = true; 4720 } else if (CurNM != OMPD_unknown) { 4721 NameModifierLoc.push_back(IC->getNameModifierLoc()); 4722 ++NamedModifiersNumber; 4723 } 4724 FoundNameModifiers[CurNM] = IC; 4725 if (CurNM == OMPD_unknown) 4726 continue; 4727 // Check if the specified name modifier is allowed for the current 4728 // directive. 4729 // At most one if clause with the particular directive-name-modifier can 4730 // appear on the directive. 4731 bool MatchFound = false; 4732 for (auto NM : AllowedNameModifiers) { 4733 if (CurNM == NM) { 4734 MatchFound = true; 4735 break; 4736 } 4737 } 4738 if (!MatchFound) { 4739 S.Diag(IC->getNameModifierLoc(), 4740 diag::err_omp_wrong_if_directive_name_modifier) 4741 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind); 4742 ErrorFound = true; 4743 } 4744 } 4745 } 4746 // If any if clause on the directive includes a directive-name-modifier then 4747 // all if clauses on the directive must include a directive-name-modifier. 4748 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) { 4749 if (NamedModifiersNumber == AllowedNameModifiers.size()) { 4750 S.Diag(FoundNameModifiers[OMPD_unknown]->getBeginLoc(), 4751 diag::err_omp_no_more_if_clause); 4752 } else { 4753 std::string Values; 4754 std::string Sep(", "); 4755 unsigned AllowedCnt = 0; 4756 unsigned TotalAllowedNum = 4757 AllowedNameModifiers.size() - NamedModifiersNumber; 4758 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End; 4759 ++Cnt) { 4760 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt]; 4761 if (!FoundNameModifiers[NM]) { 4762 Values += "'"; 4763 Values += getOpenMPDirectiveName(NM); 4764 Values += "'"; 4765 if (AllowedCnt + 2 == TotalAllowedNum) 4766 Values += " or "; 4767 else if (AllowedCnt + 1 != TotalAllowedNum) 4768 Values += Sep; 4769 ++AllowedCnt; 4770 } 4771 } 4772 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getBeginLoc(), 4773 diag::err_omp_unnamed_if_clause) 4774 << (TotalAllowedNum > 1) << Values; 4775 } 4776 for (SourceLocation Loc : NameModifierLoc) { 4777 S.Diag(Loc, diag::note_omp_previous_named_if_clause); 4778 } 4779 ErrorFound = true; 4780 } 4781 return ErrorFound; 4782 } 4783 4784 static std::pair<ValueDecl *, bool> getPrivateItem(Sema &S, Expr *&RefExpr, 4785 SourceLocation &ELoc, 4786 SourceRange &ERange, 4787 bool AllowArraySection) { 4788 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() || 4789 RefExpr->containsUnexpandedParameterPack()) 4790 return std::make_pair(nullptr, true); 4791 4792 // OpenMP [3.1, C/C++] 4793 // A list item is a variable name. 4794 // OpenMP [2.9.3.3, Restrictions, p.1] 4795 // A variable that is part of another variable (as an array or 4796 // structure element) cannot appear in a private clause. 4797 RefExpr = RefExpr->IgnoreParens(); 4798 enum { 4799 NoArrayExpr = -1, 4800 ArraySubscript = 0, 4801 OMPArraySection = 1 4802 } IsArrayExpr = NoArrayExpr; 4803 if (AllowArraySection) { 4804 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) { 4805 Expr *Base = ASE->getBase()->IgnoreParenImpCasts(); 4806 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 4807 Base = TempASE->getBase()->IgnoreParenImpCasts(); 4808 RefExpr = Base; 4809 IsArrayExpr = ArraySubscript; 4810 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) { 4811 Expr *Base = OASE->getBase()->IgnoreParenImpCasts(); 4812 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) 4813 Base = TempOASE->getBase()->IgnoreParenImpCasts(); 4814 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 4815 Base = TempASE->getBase()->IgnoreParenImpCasts(); 4816 RefExpr = Base; 4817 IsArrayExpr = OMPArraySection; 4818 } 4819 } 4820 ELoc = RefExpr->getExprLoc(); 4821 ERange = RefExpr->getSourceRange(); 4822 RefExpr = RefExpr->IgnoreParenImpCasts(); 4823 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr); 4824 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr); 4825 if ((!DE || !isa<VarDecl>(DE->getDecl())) && 4826 (S.getCurrentThisType().isNull() || !ME || 4827 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) || 4828 !isa<FieldDecl>(ME->getMemberDecl()))) { 4829 if (IsArrayExpr != NoArrayExpr) { 4830 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr 4831 << ERange; 4832 } else { 4833 S.Diag(ELoc, 4834 AllowArraySection 4835 ? diag::err_omp_expected_var_name_member_expr_or_array_item 4836 : diag::err_omp_expected_var_name_member_expr) 4837 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange; 4838 } 4839 return std::make_pair(nullptr, false); 4840 } 4841 return std::make_pair( 4842 getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false); 4843 } 4844 4845 namespace { 4846 /// Checks if the allocator is used in uses_allocators clause to be allowed in 4847 /// target regions. 4848 class AllocatorChecker final : public ConstStmtVisitor<AllocatorChecker, bool> { 4849 DSAStackTy *S = nullptr; 4850 4851 public: 4852 bool VisitDeclRefExpr(const DeclRefExpr *E) { 4853 return S->isUsesAllocatorsDecl(E->getDecl()) 4854 .getValueOr( 4855 DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait) == 4856 DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait; 4857 } 4858 bool VisitStmt(const Stmt *S) { 4859 for (const Stmt *Child : S->children()) { 4860 if (Child && Visit(Child)) 4861 return true; 4862 } 4863 return false; 4864 } 4865 explicit AllocatorChecker(DSAStackTy *S) : S(S) {} 4866 }; 4867 } // namespace 4868 4869 static void checkAllocateClauses(Sema &S, DSAStackTy *Stack, 4870 ArrayRef<OMPClause *> Clauses) { 4871 assert(!S.CurContext->isDependentContext() && 4872 "Expected non-dependent context."); 4873 auto AllocateRange = 4874 llvm::make_filter_range(Clauses, OMPAllocateClause::classof); 4875 llvm::DenseMap<CanonicalDeclPtr<Decl>, CanonicalDeclPtr<VarDecl>> 4876 DeclToCopy; 4877 auto PrivateRange = llvm::make_filter_range(Clauses, [](const OMPClause *C) { 4878 return isOpenMPPrivate(C->getClauseKind()); 4879 }); 4880 for (OMPClause *Cl : PrivateRange) { 4881 MutableArrayRef<Expr *>::iterator I, It, Et; 4882 if (Cl->getClauseKind() == OMPC_private) { 4883 auto *PC = cast<OMPPrivateClause>(Cl); 4884 I = PC->private_copies().begin(); 4885 It = PC->varlist_begin(); 4886 Et = PC->varlist_end(); 4887 } else if (Cl->getClauseKind() == OMPC_firstprivate) { 4888 auto *PC = cast<OMPFirstprivateClause>(Cl); 4889 I = PC->private_copies().begin(); 4890 It = PC->varlist_begin(); 4891 Et = PC->varlist_end(); 4892 } else if (Cl->getClauseKind() == OMPC_lastprivate) { 4893 auto *PC = cast<OMPLastprivateClause>(Cl); 4894 I = PC->private_copies().begin(); 4895 It = PC->varlist_begin(); 4896 Et = PC->varlist_end(); 4897 } else if (Cl->getClauseKind() == OMPC_linear) { 4898 auto *PC = cast<OMPLinearClause>(Cl); 4899 I = PC->privates().begin(); 4900 It = PC->varlist_begin(); 4901 Et = PC->varlist_end(); 4902 } else if (Cl->getClauseKind() == OMPC_reduction) { 4903 auto *PC = cast<OMPReductionClause>(Cl); 4904 I = PC->privates().begin(); 4905 It = PC->varlist_begin(); 4906 Et = PC->varlist_end(); 4907 } else if (Cl->getClauseKind() == OMPC_task_reduction) { 4908 auto *PC = cast<OMPTaskReductionClause>(Cl); 4909 I = PC->privates().begin(); 4910 It = PC->varlist_begin(); 4911 Et = PC->varlist_end(); 4912 } else if (Cl->getClauseKind() == OMPC_in_reduction) { 4913 auto *PC = cast<OMPInReductionClause>(Cl); 4914 I = PC->privates().begin(); 4915 It = PC->varlist_begin(); 4916 Et = PC->varlist_end(); 4917 } else { 4918 llvm_unreachable("Expected private clause."); 4919 } 4920 for (Expr *E : llvm::make_range(It, Et)) { 4921 if (!*I) { 4922 ++I; 4923 continue; 4924 } 4925 SourceLocation ELoc; 4926 SourceRange ERange; 4927 Expr *SimpleRefExpr = E; 4928 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange, 4929 /*AllowArraySection=*/true); 4930 DeclToCopy.try_emplace(Res.first, 4931 cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl())); 4932 ++I; 4933 } 4934 } 4935 for (OMPClause *C : AllocateRange) { 4936 auto *AC = cast<OMPAllocateClause>(C); 4937 if (S.getLangOpts().OpenMP >= 50 && 4938 !Stack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>() && 4939 isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) && 4940 AC->getAllocator()) { 4941 Expr *Allocator = AC->getAllocator(); 4942 // OpenMP, 2.12.5 target Construct 4943 // Memory allocators that do not appear in a uses_allocators clause cannot 4944 // appear as an allocator in an allocate clause or be used in the target 4945 // region unless a requires directive with the dynamic_allocators clause 4946 // is present in the same compilation unit. 4947 AllocatorChecker Checker(Stack); 4948 if (Checker.Visit(Allocator)) 4949 S.Diag(Allocator->getExprLoc(), 4950 diag::err_omp_allocator_not_in_uses_allocators) 4951 << Allocator->getSourceRange(); 4952 } 4953 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind = 4954 getAllocatorKind(S, Stack, AC->getAllocator()); 4955 // OpenMP, 2.11.4 allocate Clause, Restrictions. 4956 // For task, taskloop or target directives, allocation requests to memory 4957 // allocators with the trait access set to thread result in unspecified 4958 // behavior. 4959 if (AllocatorKind == OMPAllocateDeclAttr::OMPThreadMemAlloc && 4960 (isOpenMPTaskingDirective(Stack->getCurrentDirective()) || 4961 isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()))) { 4962 S.Diag(AC->getAllocator()->getExprLoc(), 4963 diag::warn_omp_allocate_thread_on_task_target_directive) 4964 << getOpenMPDirectiveName(Stack->getCurrentDirective()); 4965 } 4966 for (Expr *E : AC->varlists()) { 4967 SourceLocation ELoc; 4968 SourceRange ERange; 4969 Expr *SimpleRefExpr = E; 4970 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange); 4971 ValueDecl *VD = Res.first; 4972 DSAStackTy::DSAVarData Data = Stack->getTopDSA(VD, /*FromParent=*/false); 4973 if (!isOpenMPPrivate(Data.CKind)) { 4974 S.Diag(E->getExprLoc(), 4975 diag::err_omp_expected_private_copy_for_allocate); 4976 continue; 4977 } 4978 VarDecl *PrivateVD = DeclToCopy[VD]; 4979 if (checkPreviousOMPAllocateAttribute(S, Stack, E, PrivateVD, 4980 AllocatorKind, AC->getAllocator())) 4981 continue; 4982 applyOMPAllocateAttribute(S, PrivateVD, AllocatorKind, AC->getAllocator(), 4983 E->getSourceRange()); 4984 } 4985 } 4986 } 4987 4988 StmtResult Sema::ActOnOpenMPExecutableDirective( 4989 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName, 4990 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses, 4991 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { 4992 StmtResult Res = StmtError(); 4993 // First check CancelRegion which is then used in checkNestingOfRegions. 4994 if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) || 4995 checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion, 4996 StartLoc)) 4997 return StmtError(); 4998 4999 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit; 5000 VarsWithInheritedDSAType VarsWithInheritedDSA; 5001 bool ErrorFound = false; 5002 ClausesWithImplicit.append(Clauses.begin(), Clauses.end()); 5003 if (AStmt && !CurContext->isDependentContext() && Kind != OMPD_atomic && 5004 Kind != OMPD_critical && Kind != OMPD_section && Kind != OMPD_master) { 5005 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5006 5007 // Check default data sharing attributes for referenced variables. 5008 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt)); 5009 int ThisCaptureLevel = getOpenMPCaptureLevels(Kind); 5010 Stmt *S = AStmt; 5011 while (--ThisCaptureLevel >= 0) 5012 S = cast<CapturedStmt>(S)->getCapturedStmt(); 5013 DSAChecker.Visit(S); 5014 if (!isOpenMPTargetDataManagementDirective(Kind) && 5015 !isOpenMPTaskingDirective(Kind)) { 5016 // Visit subcaptures to generate implicit clauses for captured vars. 5017 auto *CS = cast<CapturedStmt>(AStmt); 5018 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 5019 getOpenMPCaptureRegions(CaptureRegions, Kind); 5020 // Ignore outer tasking regions for target directives. 5021 if (CaptureRegions.size() > 1 && CaptureRegions.front() == OMPD_task) 5022 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 5023 DSAChecker.visitSubCaptures(CS); 5024 } 5025 if (DSAChecker.isErrorFound()) 5026 return StmtError(); 5027 // Generate list of implicitly defined firstprivate variables. 5028 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA(); 5029 5030 SmallVector<Expr *, 4> ImplicitFirstprivates( 5031 DSAChecker.getImplicitFirstprivate().begin(), 5032 DSAChecker.getImplicitFirstprivate().end()); 5033 SmallVector<Expr *, 4> ImplicitMaps[OMPC_MAP_delete]; 5034 for (unsigned I = 0; I < OMPC_MAP_delete; ++I) { 5035 ArrayRef<Expr *> ImplicitMap = 5036 DSAChecker.getImplicitMap(static_cast<OpenMPDefaultmapClauseKind>(I)); 5037 ImplicitMaps[I].append(ImplicitMap.begin(), ImplicitMap.end()); 5038 } 5039 // Mark taskgroup task_reduction descriptors as implicitly firstprivate. 5040 for (OMPClause *C : Clauses) { 5041 if (auto *IRC = dyn_cast<OMPInReductionClause>(C)) { 5042 for (Expr *E : IRC->taskgroup_descriptors()) 5043 if (E) 5044 ImplicitFirstprivates.emplace_back(E); 5045 } 5046 // OpenMP 5.0, 2.10.1 task Construct 5047 // [detach clause]... The event-handle will be considered as if it was 5048 // specified on a firstprivate clause. 5049 if (auto *DC = dyn_cast<OMPDetachClause>(C)) 5050 ImplicitFirstprivates.push_back(DC->getEventHandler()); 5051 } 5052 if (!ImplicitFirstprivates.empty()) { 5053 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause( 5054 ImplicitFirstprivates, SourceLocation(), SourceLocation(), 5055 SourceLocation())) { 5056 ClausesWithImplicit.push_back(Implicit); 5057 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() != 5058 ImplicitFirstprivates.size(); 5059 } else { 5060 ErrorFound = true; 5061 } 5062 } 5063 int ClauseKindCnt = -1; 5064 for (ArrayRef<Expr *> ImplicitMap : ImplicitMaps) { 5065 ++ClauseKindCnt; 5066 if (ImplicitMap.empty()) 5067 continue; 5068 CXXScopeSpec MapperIdScopeSpec; 5069 DeclarationNameInfo MapperId; 5070 auto Kind = static_cast<OpenMPMapClauseKind>(ClauseKindCnt); 5071 if (OMPClause *Implicit = ActOnOpenMPMapClause( 5072 llvm::None, llvm::None, MapperIdScopeSpec, MapperId, Kind, 5073 /*IsMapTypeImplicit=*/true, SourceLocation(), SourceLocation(), 5074 ImplicitMap, OMPVarListLocTy())) { 5075 ClausesWithImplicit.emplace_back(Implicit); 5076 ErrorFound |= 5077 cast<OMPMapClause>(Implicit)->varlist_size() != ImplicitMap.size(); 5078 } else { 5079 ErrorFound = true; 5080 } 5081 } 5082 } 5083 5084 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers; 5085 switch (Kind) { 5086 case OMPD_parallel: 5087 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc, 5088 EndLoc); 5089 AllowedNameModifiers.push_back(OMPD_parallel); 5090 break; 5091 case OMPD_simd: 5092 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, 5093 VarsWithInheritedDSA); 5094 if (LangOpts.OpenMP >= 50) 5095 AllowedNameModifiers.push_back(OMPD_simd); 5096 break; 5097 case OMPD_for: 5098 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, 5099 VarsWithInheritedDSA); 5100 break; 5101 case OMPD_for_simd: 5102 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 5103 EndLoc, VarsWithInheritedDSA); 5104 if (LangOpts.OpenMP >= 50) 5105 AllowedNameModifiers.push_back(OMPD_simd); 5106 break; 5107 case OMPD_sections: 5108 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc, 5109 EndLoc); 5110 break; 5111 case OMPD_section: 5112 assert(ClausesWithImplicit.empty() && 5113 "No clauses are allowed for 'omp section' directive"); 5114 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc); 5115 break; 5116 case OMPD_single: 5117 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc, 5118 EndLoc); 5119 break; 5120 case OMPD_master: 5121 assert(ClausesWithImplicit.empty() && 5122 "No clauses are allowed for 'omp master' directive"); 5123 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc); 5124 break; 5125 case OMPD_critical: 5126 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt, 5127 StartLoc, EndLoc); 5128 break; 5129 case OMPD_parallel_for: 5130 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc, 5131 EndLoc, VarsWithInheritedDSA); 5132 AllowedNameModifiers.push_back(OMPD_parallel); 5133 break; 5134 case OMPD_parallel_for_simd: 5135 Res = ActOnOpenMPParallelForSimdDirective( 5136 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5137 AllowedNameModifiers.push_back(OMPD_parallel); 5138 if (LangOpts.OpenMP >= 50) 5139 AllowedNameModifiers.push_back(OMPD_simd); 5140 break; 5141 case OMPD_parallel_master: 5142 Res = ActOnOpenMPParallelMasterDirective(ClausesWithImplicit, AStmt, 5143 StartLoc, EndLoc); 5144 AllowedNameModifiers.push_back(OMPD_parallel); 5145 break; 5146 case OMPD_parallel_sections: 5147 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt, 5148 StartLoc, EndLoc); 5149 AllowedNameModifiers.push_back(OMPD_parallel); 5150 break; 5151 case OMPD_task: 5152 Res = 5153 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 5154 AllowedNameModifiers.push_back(OMPD_task); 5155 break; 5156 case OMPD_taskyield: 5157 assert(ClausesWithImplicit.empty() && 5158 "No clauses are allowed for 'omp taskyield' directive"); 5159 assert(AStmt == nullptr && 5160 "No associated statement allowed for 'omp taskyield' directive"); 5161 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc); 5162 break; 5163 case OMPD_barrier: 5164 assert(ClausesWithImplicit.empty() && 5165 "No clauses are allowed for 'omp barrier' directive"); 5166 assert(AStmt == nullptr && 5167 "No associated statement allowed for 'omp barrier' directive"); 5168 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc); 5169 break; 5170 case OMPD_taskwait: 5171 assert(ClausesWithImplicit.empty() && 5172 "No clauses are allowed for 'omp taskwait' directive"); 5173 assert(AStmt == nullptr && 5174 "No associated statement allowed for 'omp taskwait' directive"); 5175 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc); 5176 break; 5177 case OMPD_taskgroup: 5178 Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc, 5179 EndLoc); 5180 break; 5181 case OMPD_flush: 5182 assert(AStmt == nullptr && 5183 "No associated statement allowed for 'omp flush' directive"); 5184 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc); 5185 break; 5186 case OMPD_depobj: 5187 assert(AStmt == nullptr && 5188 "No associated statement allowed for 'omp depobj' directive"); 5189 Res = ActOnOpenMPDepobjDirective(ClausesWithImplicit, StartLoc, EndLoc); 5190 break; 5191 case OMPD_scan: 5192 assert(AStmt == nullptr && 5193 "No associated statement allowed for 'omp scan' directive"); 5194 Res = ActOnOpenMPScanDirective(ClausesWithImplicit, StartLoc, EndLoc); 5195 break; 5196 case OMPD_ordered: 5197 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc, 5198 EndLoc); 5199 break; 5200 case OMPD_atomic: 5201 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc, 5202 EndLoc); 5203 break; 5204 case OMPD_teams: 5205 Res = 5206 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 5207 break; 5208 case OMPD_target: 5209 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc, 5210 EndLoc); 5211 AllowedNameModifiers.push_back(OMPD_target); 5212 break; 5213 case OMPD_target_parallel: 5214 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt, 5215 StartLoc, EndLoc); 5216 AllowedNameModifiers.push_back(OMPD_target); 5217 AllowedNameModifiers.push_back(OMPD_parallel); 5218 break; 5219 case OMPD_target_parallel_for: 5220 Res = ActOnOpenMPTargetParallelForDirective( 5221 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5222 AllowedNameModifiers.push_back(OMPD_target); 5223 AllowedNameModifiers.push_back(OMPD_parallel); 5224 break; 5225 case OMPD_cancellation_point: 5226 assert(ClausesWithImplicit.empty() && 5227 "No clauses are allowed for 'omp cancellation point' directive"); 5228 assert(AStmt == nullptr && "No associated statement allowed for 'omp " 5229 "cancellation point' directive"); 5230 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion); 5231 break; 5232 case OMPD_cancel: 5233 assert(AStmt == nullptr && 5234 "No associated statement allowed for 'omp cancel' directive"); 5235 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc, 5236 CancelRegion); 5237 AllowedNameModifiers.push_back(OMPD_cancel); 5238 break; 5239 case OMPD_target_data: 5240 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc, 5241 EndLoc); 5242 AllowedNameModifiers.push_back(OMPD_target_data); 5243 break; 5244 case OMPD_target_enter_data: 5245 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc, 5246 EndLoc, AStmt); 5247 AllowedNameModifiers.push_back(OMPD_target_enter_data); 5248 break; 5249 case OMPD_target_exit_data: 5250 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc, 5251 EndLoc, AStmt); 5252 AllowedNameModifiers.push_back(OMPD_target_exit_data); 5253 break; 5254 case OMPD_taskloop: 5255 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc, 5256 EndLoc, VarsWithInheritedDSA); 5257 AllowedNameModifiers.push_back(OMPD_taskloop); 5258 break; 5259 case OMPD_taskloop_simd: 5260 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 5261 EndLoc, VarsWithInheritedDSA); 5262 AllowedNameModifiers.push_back(OMPD_taskloop); 5263 if (LangOpts.OpenMP >= 50) 5264 AllowedNameModifiers.push_back(OMPD_simd); 5265 break; 5266 case OMPD_master_taskloop: 5267 Res = ActOnOpenMPMasterTaskLoopDirective( 5268 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5269 AllowedNameModifiers.push_back(OMPD_taskloop); 5270 break; 5271 case OMPD_master_taskloop_simd: 5272 Res = ActOnOpenMPMasterTaskLoopSimdDirective( 5273 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5274 AllowedNameModifiers.push_back(OMPD_taskloop); 5275 if (LangOpts.OpenMP >= 50) 5276 AllowedNameModifiers.push_back(OMPD_simd); 5277 break; 5278 case OMPD_parallel_master_taskloop: 5279 Res = ActOnOpenMPParallelMasterTaskLoopDirective( 5280 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5281 AllowedNameModifiers.push_back(OMPD_taskloop); 5282 AllowedNameModifiers.push_back(OMPD_parallel); 5283 break; 5284 case OMPD_parallel_master_taskloop_simd: 5285 Res = ActOnOpenMPParallelMasterTaskLoopSimdDirective( 5286 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5287 AllowedNameModifiers.push_back(OMPD_taskloop); 5288 AllowedNameModifiers.push_back(OMPD_parallel); 5289 if (LangOpts.OpenMP >= 50) 5290 AllowedNameModifiers.push_back(OMPD_simd); 5291 break; 5292 case OMPD_distribute: 5293 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc, 5294 EndLoc, VarsWithInheritedDSA); 5295 break; 5296 case OMPD_target_update: 5297 Res = ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc, 5298 EndLoc, AStmt); 5299 AllowedNameModifiers.push_back(OMPD_target_update); 5300 break; 5301 case OMPD_distribute_parallel_for: 5302 Res = ActOnOpenMPDistributeParallelForDirective( 5303 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5304 AllowedNameModifiers.push_back(OMPD_parallel); 5305 break; 5306 case OMPD_distribute_parallel_for_simd: 5307 Res = ActOnOpenMPDistributeParallelForSimdDirective( 5308 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5309 AllowedNameModifiers.push_back(OMPD_parallel); 5310 if (LangOpts.OpenMP >= 50) 5311 AllowedNameModifiers.push_back(OMPD_simd); 5312 break; 5313 case OMPD_distribute_simd: 5314 Res = ActOnOpenMPDistributeSimdDirective( 5315 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5316 if (LangOpts.OpenMP >= 50) 5317 AllowedNameModifiers.push_back(OMPD_simd); 5318 break; 5319 case OMPD_target_parallel_for_simd: 5320 Res = ActOnOpenMPTargetParallelForSimdDirective( 5321 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5322 AllowedNameModifiers.push_back(OMPD_target); 5323 AllowedNameModifiers.push_back(OMPD_parallel); 5324 if (LangOpts.OpenMP >= 50) 5325 AllowedNameModifiers.push_back(OMPD_simd); 5326 break; 5327 case OMPD_target_simd: 5328 Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 5329 EndLoc, VarsWithInheritedDSA); 5330 AllowedNameModifiers.push_back(OMPD_target); 5331 if (LangOpts.OpenMP >= 50) 5332 AllowedNameModifiers.push_back(OMPD_simd); 5333 break; 5334 case OMPD_teams_distribute: 5335 Res = ActOnOpenMPTeamsDistributeDirective( 5336 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5337 break; 5338 case OMPD_teams_distribute_simd: 5339 Res = ActOnOpenMPTeamsDistributeSimdDirective( 5340 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5341 if (LangOpts.OpenMP >= 50) 5342 AllowedNameModifiers.push_back(OMPD_simd); 5343 break; 5344 case OMPD_teams_distribute_parallel_for_simd: 5345 Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective( 5346 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5347 AllowedNameModifiers.push_back(OMPD_parallel); 5348 if (LangOpts.OpenMP >= 50) 5349 AllowedNameModifiers.push_back(OMPD_simd); 5350 break; 5351 case OMPD_teams_distribute_parallel_for: 5352 Res = ActOnOpenMPTeamsDistributeParallelForDirective( 5353 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5354 AllowedNameModifiers.push_back(OMPD_parallel); 5355 break; 5356 case OMPD_target_teams: 5357 Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, 5358 EndLoc); 5359 AllowedNameModifiers.push_back(OMPD_target); 5360 break; 5361 case OMPD_target_teams_distribute: 5362 Res = ActOnOpenMPTargetTeamsDistributeDirective( 5363 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5364 AllowedNameModifiers.push_back(OMPD_target); 5365 break; 5366 case OMPD_target_teams_distribute_parallel_for: 5367 Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective( 5368 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5369 AllowedNameModifiers.push_back(OMPD_target); 5370 AllowedNameModifiers.push_back(OMPD_parallel); 5371 break; 5372 case OMPD_target_teams_distribute_parallel_for_simd: 5373 Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective( 5374 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5375 AllowedNameModifiers.push_back(OMPD_target); 5376 AllowedNameModifiers.push_back(OMPD_parallel); 5377 if (LangOpts.OpenMP >= 50) 5378 AllowedNameModifiers.push_back(OMPD_simd); 5379 break; 5380 case OMPD_target_teams_distribute_simd: 5381 Res = ActOnOpenMPTargetTeamsDistributeSimdDirective( 5382 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5383 AllowedNameModifiers.push_back(OMPD_target); 5384 if (LangOpts.OpenMP >= 50) 5385 AllowedNameModifiers.push_back(OMPD_simd); 5386 break; 5387 case OMPD_declare_target: 5388 case OMPD_end_declare_target: 5389 case OMPD_threadprivate: 5390 case OMPD_allocate: 5391 case OMPD_declare_reduction: 5392 case OMPD_declare_mapper: 5393 case OMPD_declare_simd: 5394 case OMPD_requires: 5395 case OMPD_declare_variant: 5396 case OMPD_begin_declare_variant: 5397 case OMPD_end_declare_variant: 5398 llvm_unreachable("OpenMP Directive is not allowed"); 5399 case OMPD_unknown: 5400 default: 5401 llvm_unreachable("Unknown OpenMP directive"); 5402 } 5403 5404 ErrorFound = Res.isInvalid() || ErrorFound; 5405 5406 // Check variables in the clauses if default(none) or 5407 // default(firstprivate) was specified. 5408 if (DSAStack->getDefaultDSA() == DSA_none || 5409 DSAStack->getDefaultDSA() == DSA_firstprivate) { 5410 DSAAttrChecker DSAChecker(DSAStack, *this, nullptr); 5411 for (OMPClause *C : Clauses) { 5412 switch (C->getClauseKind()) { 5413 case OMPC_num_threads: 5414 case OMPC_dist_schedule: 5415 // Do not analyse if no parent teams directive. 5416 if (isOpenMPTeamsDirective(Kind)) 5417 break; 5418 continue; 5419 case OMPC_if: 5420 if (isOpenMPTeamsDirective(Kind) && 5421 cast<OMPIfClause>(C)->getNameModifier() != OMPD_target) 5422 break; 5423 if (isOpenMPParallelDirective(Kind) && 5424 isOpenMPTaskLoopDirective(Kind) && 5425 cast<OMPIfClause>(C)->getNameModifier() != OMPD_parallel) 5426 break; 5427 continue; 5428 case OMPC_schedule: 5429 case OMPC_detach: 5430 break; 5431 case OMPC_grainsize: 5432 case OMPC_num_tasks: 5433 case OMPC_final: 5434 case OMPC_priority: 5435 // Do not analyze if no parent parallel directive. 5436 if (isOpenMPParallelDirective(Kind)) 5437 break; 5438 continue; 5439 case OMPC_ordered: 5440 case OMPC_device: 5441 case OMPC_num_teams: 5442 case OMPC_thread_limit: 5443 case OMPC_hint: 5444 case OMPC_collapse: 5445 case OMPC_safelen: 5446 case OMPC_simdlen: 5447 case OMPC_default: 5448 case OMPC_proc_bind: 5449 case OMPC_private: 5450 case OMPC_firstprivate: 5451 case OMPC_lastprivate: 5452 case OMPC_shared: 5453 case OMPC_reduction: 5454 case OMPC_task_reduction: 5455 case OMPC_in_reduction: 5456 case OMPC_linear: 5457 case OMPC_aligned: 5458 case OMPC_copyin: 5459 case OMPC_copyprivate: 5460 case OMPC_nowait: 5461 case OMPC_untied: 5462 case OMPC_mergeable: 5463 case OMPC_allocate: 5464 case OMPC_read: 5465 case OMPC_write: 5466 case OMPC_update: 5467 case OMPC_capture: 5468 case OMPC_seq_cst: 5469 case OMPC_acq_rel: 5470 case OMPC_acquire: 5471 case OMPC_release: 5472 case OMPC_relaxed: 5473 case OMPC_depend: 5474 case OMPC_threads: 5475 case OMPC_simd: 5476 case OMPC_map: 5477 case OMPC_nogroup: 5478 case OMPC_defaultmap: 5479 case OMPC_to: 5480 case OMPC_from: 5481 case OMPC_use_device_ptr: 5482 case OMPC_use_device_addr: 5483 case OMPC_is_device_ptr: 5484 case OMPC_nontemporal: 5485 case OMPC_order: 5486 case OMPC_destroy: 5487 case OMPC_inclusive: 5488 case OMPC_exclusive: 5489 case OMPC_uses_allocators: 5490 case OMPC_affinity: 5491 continue; 5492 case OMPC_allocator: 5493 case OMPC_flush: 5494 case OMPC_depobj: 5495 case OMPC_threadprivate: 5496 case OMPC_uniform: 5497 case OMPC_unknown: 5498 case OMPC_unified_address: 5499 case OMPC_unified_shared_memory: 5500 case OMPC_reverse_offload: 5501 case OMPC_dynamic_allocators: 5502 case OMPC_atomic_default_mem_order: 5503 case OMPC_device_type: 5504 case OMPC_match: 5505 default: 5506 llvm_unreachable("Unexpected clause"); 5507 } 5508 for (Stmt *CC : C->children()) { 5509 if (CC) 5510 DSAChecker.Visit(CC); 5511 } 5512 } 5513 for (const auto &P : DSAChecker.getVarsWithInheritedDSA()) 5514 VarsWithInheritedDSA[P.getFirst()] = P.getSecond(); 5515 } 5516 for (const auto &P : VarsWithInheritedDSA) { 5517 if (P.getFirst()->isImplicit() || isa<OMPCapturedExprDecl>(P.getFirst())) 5518 continue; 5519 ErrorFound = true; 5520 if (DSAStack->getDefaultDSA() == DSA_none || 5521 DSAStack->getDefaultDSA() == DSA_firstprivate) { 5522 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable) 5523 << P.first << P.second->getSourceRange(); 5524 Diag(DSAStack->getDefaultDSALocation(), diag::note_omp_default_dsa_none); 5525 } else if (getLangOpts().OpenMP >= 50) { 5526 Diag(P.second->getExprLoc(), 5527 diag::err_omp_defaultmap_no_attr_for_variable) 5528 << P.first << P.second->getSourceRange(); 5529 Diag(DSAStack->getDefaultDSALocation(), 5530 diag::note_omp_defaultmap_attr_none); 5531 } 5532 } 5533 5534 if (!AllowedNameModifiers.empty()) 5535 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) || 5536 ErrorFound; 5537 5538 if (ErrorFound) 5539 return StmtError(); 5540 5541 if (!CurContext->isDependentContext() && 5542 isOpenMPTargetExecutionDirective(Kind) && 5543 !(DSAStack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() || 5544 DSAStack->hasRequiresDeclWithClause<OMPUnifiedAddressClause>() || 5545 DSAStack->hasRequiresDeclWithClause<OMPReverseOffloadClause>() || 5546 DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())) { 5547 // Register target to DSA Stack. 5548 DSAStack->addTargetDirLocation(StartLoc); 5549 } 5550 5551 return Res; 5552 } 5553 5554 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective( 5555 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen, 5556 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds, 5557 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears, 5558 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) { 5559 assert(Aligneds.size() == Alignments.size()); 5560 assert(Linears.size() == LinModifiers.size()); 5561 assert(Linears.size() == Steps.size()); 5562 if (!DG || DG.get().isNull()) 5563 return DeclGroupPtrTy(); 5564 5565 const int SimdId = 0; 5566 if (!DG.get().isSingleDecl()) { 5567 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd_variant) 5568 << SimdId; 5569 return DG; 5570 } 5571 Decl *ADecl = DG.get().getSingleDecl(); 5572 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl)) 5573 ADecl = FTD->getTemplatedDecl(); 5574 5575 auto *FD = dyn_cast<FunctionDecl>(ADecl); 5576 if (!FD) { 5577 Diag(ADecl->getLocation(), diag::err_omp_function_expected) << SimdId; 5578 return DeclGroupPtrTy(); 5579 } 5580 5581 // OpenMP [2.8.2, declare simd construct, Description] 5582 // The parameter of the simdlen clause must be a constant positive integer 5583 // expression. 5584 ExprResult SL; 5585 if (Simdlen) 5586 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen); 5587 // OpenMP [2.8.2, declare simd construct, Description] 5588 // The special this pointer can be used as if was one of the arguments to the 5589 // function in any of the linear, aligned, or uniform clauses. 5590 // The uniform clause declares one or more arguments to have an invariant 5591 // value for all concurrent invocations of the function in the execution of a 5592 // single SIMD loop. 5593 llvm::DenseMap<const Decl *, const Expr *> UniformedArgs; 5594 const Expr *UniformedLinearThis = nullptr; 5595 for (const Expr *E : Uniforms) { 5596 E = E->IgnoreParenImpCasts(); 5597 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 5598 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) 5599 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 5600 FD->getParamDecl(PVD->getFunctionScopeIndex()) 5601 ->getCanonicalDecl() == PVD->getCanonicalDecl()) { 5602 UniformedArgs.try_emplace(PVD->getCanonicalDecl(), E); 5603 continue; 5604 } 5605 if (isa<CXXThisExpr>(E)) { 5606 UniformedLinearThis = E; 5607 continue; 5608 } 5609 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 5610 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 5611 } 5612 // OpenMP [2.8.2, declare simd construct, Description] 5613 // The aligned clause declares that the object to which each list item points 5614 // is aligned to the number of bytes expressed in the optional parameter of 5615 // the aligned clause. 5616 // The special this pointer can be used as if was one of the arguments to the 5617 // function in any of the linear, aligned, or uniform clauses. 5618 // The type of list items appearing in the aligned clause must be array, 5619 // pointer, reference to array, or reference to pointer. 5620 llvm::DenseMap<const Decl *, const Expr *> AlignedArgs; 5621 const Expr *AlignedThis = nullptr; 5622 for (const Expr *E : Aligneds) { 5623 E = E->IgnoreParenImpCasts(); 5624 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 5625 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 5626 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 5627 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 5628 FD->getParamDecl(PVD->getFunctionScopeIndex()) 5629 ->getCanonicalDecl() == CanonPVD) { 5630 // OpenMP [2.8.1, simd construct, Restrictions] 5631 // A list-item cannot appear in more than one aligned clause. 5632 if (AlignedArgs.count(CanonPVD) > 0) { 5633 Diag(E->getExprLoc(), diag::err_omp_used_in_clause_twice) 5634 << 1 << getOpenMPClauseName(OMPC_aligned) 5635 << E->getSourceRange(); 5636 Diag(AlignedArgs[CanonPVD]->getExprLoc(), 5637 diag::note_omp_explicit_dsa) 5638 << getOpenMPClauseName(OMPC_aligned); 5639 continue; 5640 } 5641 AlignedArgs[CanonPVD] = E; 5642 QualType QTy = PVD->getType() 5643 .getNonReferenceType() 5644 .getUnqualifiedType() 5645 .getCanonicalType(); 5646 const Type *Ty = QTy.getTypePtrOrNull(); 5647 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) { 5648 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr) 5649 << QTy << getLangOpts().CPlusPlus << E->getSourceRange(); 5650 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD; 5651 } 5652 continue; 5653 } 5654 } 5655 if (isa<CXXThisExpr>(E)) { 5656 if (AlignedThis) { 5657 Diag(E->getExprLoc(), diag::err_omp_used_in_clause_twice) 5658 << 2 << getOpenMPClauseName(OMPC_aligned) << E->getSourceRange(); 5659 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa) 5660 << getOpenMPClauseName(OMPC_aligned); 5661 } 5662 AlignedThis = E; 5663 continue; 5664 } 5665 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 5666 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 5667 } 5668 // The optional parameter of the aligned clause, alignment, must be a constant 5669 // positive integer expression. If no optional parameter is specified, 5670 // implementation-defined default alignments for SIMD instructions on the 5671 // target platforms are assumed. 5672 SmallVector<const Expr *, 4> NewAligns; 5673 for (Expr *E : Alignments) { 5674 ExprResult Align; 5675 if (E) 5676 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned); 5677 NewAligns.push_back(Align.get()); 5678 } 5679 // OpenMP [2.8.2, declare simd construct, Description] 5680 // The linear clause declares one or more list items to be private to a SIMD 5681 // lane and to have a linear relationship with respect to the iteration space 5682 // of a loop. 5683 // The special this pointer can be used as if was one of the arguments to the 5684 // function in any of the linear, aligned, or uniform clauses. 5685 // When a linear-step expression is specified in a linear clause it must be 5686 // either a constant integer expression or an integer-typed parameter that is 5687 // specified in a uniform clause on the directive. 5688 llvm::DenseMap<const Decl *, const Expr *> LinearArgs; 5689 const bool IsUniformedThis = UniformedLinearThis != nullptr; 5690 auto MI = LinModifiers.begin(); 5691 for (const Expr *E : Linears) { 5692 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI); 5693 ++MI; 5694 E = E->IgnoreParenImpCasts(); 5695 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 5696 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 5697 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 5698 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 5699 FD->getParamDecl(PVD->getFunctionScopeIndex()) 5700 ->getCanonicalDecl() == CanonPVD) { 5701 // OpenMP [2.15.3.7, linear Clause, Restrictions] 5702 // A list-item cannot appear in more than one linear clause. 5703 if (LinearArgs.count(CanonPVD) > 0) { 5704 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 5705 << getOpenMPClauseName(OMPC_linear) 5706 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange(); 5707 Diag(LinearArgs[CanonPVD]->getExprLoc(), 5708 diag::note_omp_explicit_dsa) 5709 << getOpenMPClauseName(OMPC_linear); 5710 continue; 5711 } 5712 // Each argument can appear in at most one uniform or linear clause. 5713 if (UniformedArgs.count(CanonPVD) > 0) { 5714 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 5715 << getOpenMPClauseName(OMPC_linear) 5716 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange(); 5717 Diag(UniformedArgs[CanonPVD]->getExprLoc(), 5718 diag::note_omp_explicit_dsa) 5719 << getOpenMPClauseName(OMPC_uniform); 5720 continue; 5721 } 5722 LinearArgs[CanonPVD] = E; 5723 if (E->isValueDependent() || E->isTypeDependent() || 5724 E->isInstantiationDependent() || 5725 E->containsUnexpandedParameterPack()) 5726 continue; 5727 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind, 5728 PVD->getOriginalType(), 5729 /*IsDeclareSimd=*/true); 5730 continue; 5731 } 5732 } 5733 if (isa<CXXThisExpr>(E)) { 5734 if (UniformedLinearThis) { 5735 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 5736 << getOpenMPClauseName(OMPC_linear) 5737 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear) 5738 << E->getSourceRange(); 5739 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa) 5740 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform 5741 : OMPC_linear); 5742 continue; 5743 } 5744 UniformedLinearThis = E; 5745 if (E->isValueDependent() || E->isTypeDependent() || 5746 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 5747 continue; 5748 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind, 5749 E->getType(), /*IsDeclareSimd=*/true); 5750 continue; 5751 } 5752 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 5753 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 5754 } 5755 Expr *Step = nullptr; 5756 Expr *NewStep = nullptr; 5757 SmallVector<Expr *, 4> NewSteps; 5758 for (Expr *E : Steps) { 5759 // Skip the same step expression, it was checked already. 5760 if (Step == E || !E) { 5761 NewSteps.push_back(E ? NewStep : nullptr); 5762 continue; 5763 } 5764 Step = E; 5765 if (const auto *DRE = dyn_cast<DeclRefExpr>(Step)) 5766 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 5767 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 5768 if (UniformedArgs.count(CanonPVD) == 0) { 5769 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param) 5770 << Step->getSourceRange(); 5771 } else if (E->isValueDependent() || E->isTypeDependent() || 5772 E->isInstantiationDependent() || 5773 E->containsUnexpandedParameterPack() || 5774 CanonPVD->getType()->hasIntegerRepresentation()) { 5775 NewSteps.push_back(Step); 5776 } else { 5777 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param) 5778 << Step->getSourceRange(); 5779 } 5780 continue; 5781 } 5782 NewStep = Step; 5783 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && 5784 !Step->isInstantiationDependent() && 5785 !Step->containsUnexpandedParameterPack()) { 5786 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step) 5787 .get(); 5788 if (NewStep) 5789 NewStep = VerifyIntegerConstantExpression(NewStep).get(); 5790 } 5791 NewSteps.push_back(NewStep); 5792 } 5793 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit( 5794 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()), 5795 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(), 5796 const_cast<Expr **>(NewAligns.data()), NewAligns.size(), 5797 const_cast<Expr **>(Linears.data()), Linears.size(), 5798 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(), 5799 NewSteps.data(), NewSteps.size(), SR); 5800 ADecl->addAttr(NewAttr); 5801 return DG; 5802 } 5803 5804 static void setPrototype(Sema &S, FunctionDecl *FD, FunctionDecl *FDWithProto, 5805 QualType NewType) { 5806 assert(NewType->isFunctionProtoType() && 5807 "Expected function type with prototype."); 5808 assert(FD->getType()->isFunctionNoProtoType() && 5809 "Expected function with type with no prototype."); 5810 assert(FDWithProto->getType()->isFunctionProtoType() && 5811 "Expected function with prototype."); 5812 // Synthesize parameters with the same types. 5813 FD->setType(NewType); 5814 SmallVector<ParmVarDecl *, 16> Params; 5815 for (const ParmVarDecl *P : FDWithProto->parameters()) { 5816 auto *Param = ParmVarDecl::Create(S.getASTContext(), FD, SourceLocation(), 5817 SourceLocation(), nullptr, P->getType(), 5818 /*TInfo=*/nullptr, SC_None, nullptr); 5819 Param->setScopeInfo(0, Params.size()); 5820 Param->setImplicit(); 5821 Params.push_back(Param); 5822 } 5823 5824 FD->setParams(Params); 5825 } 5826 5827 Sema::OMPDeclareVariantScope::OMPDeclareVariantScope(OMPTraitInfo &TI) 5828 : TI(&TI), NameSuffix(TI.getMangledName()) {} 5829 5830 FunctionDecl * 5831 Sema::ActOnStartOfFunctionDefinitionInOpenMPDeclareVariantScope(Scope *S, 5832 Declarator &D) { 5833 IdentifierInfo *BaseII = D.getIdentifier(); 5834 LookupResult Lookup(*this, DeclarationName(BaseII), D.getIdentifierLoc(), 5835 LookupOrdinaryName); 5836 LookupParsedName(Lookup, S, &D.getCXXScopeSpec()); 5837 5838 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); 5839 QualType FType = TInfo->getType(); 5840 5841 bool IsConstexpr = D.getDeclSpec().getConstexprSpecifier() == CSK_constexpr; 5842 bool IsConsteval = D.getDeclSpec().getConstexprSpecifier() == CSK_consteval; 5843 5844 FunctionDecl *BaseFD = nullptr; 5845 for (auto *Candidate : Lookup) { 5846 auto *UDecl = dyn_cast<FunctionDecl>(Candidate->getUnderlyingDecl()); 5847 if (!UDecl) 5848 continue; 5849 5850 // Don't specialize constexpr/consteval functions with 5851 // non-constexpr/consteval functions. 5852 if (UDecl->isConstexpr() && !IsConstexpr) 5853 continue; 5854 if (UDecl->isConsteval() && !IsConsteval) 5855 continue; 5856 5857 QualType NewType = Context.mergeFunctionTypes( 5858 FType, UDecl->getType(), /* OfBlockPointer */ false, 5859 /* Unqualified */ false, /* AllowCXX */ true); 5860 if (NewType.isNull()) 5861 continue; 5862 5863 // Found a base! 5864 BaseFD = UDecl; 5865 break; 5866 } 5867 if (!BaseFD) { 5868 BaseFD = cast<FunctionDecl>(ActOnDeclarator(S, D)); 5869 BaseFD->setImplicit(true); 5870 } 5871 5872 OMPDeclareVariantScope &DVScope = OMPDeclareVariantScopes.back(); 5873 std::string MangledName; 5874 MangledName += D.getIdentifier()->getName(); 5875 MangledName += getOpenMPVariantManglingSeparatorStr(); 5876 MangledName += DVScope.NameSuffix; 5877 IdentifierInfo &VariantII = Context.Idents.get(MangledName); 5878 5879 VariantII.setMangledOpenMPVariantName(true); 5880 D.SetIdentifier(&VariantII, D.getBeginLoc()); 5881 return BaseFD; 5882 } 5883 5884 void Sema::ActOnFinishedFunctionDefinitionInOpenMPDeclareVariantScope( 5885 FunctionDecl *FD, FunctionDecl *BaseFD) { 5886 // Do not mark function as is used to prevent its emission if this is the 5887 // only place where it is used. 5888 EnterExpressionEvaluationContext Unevaluated( 5889 *this, Sema::ExpressionEvaluationContext::Unevaluated); 5890 5891 Expr *VariantFuncRef = DeclRefExpr::Create( 5892 Context, NestedNameSpecifierLoc(), SourceLocation(), FD, 5893 /* RefersToEnclosingVariableOrCapture */ false, 5894 /* NameLoc */ FD->getLocation(), FD->getType(), ExprValueKind::VK_RValue); 5895 5896 OMPDeclareVariantScope &DVScope = OMPDeclareVariantScopes.back(); 5897 auto *OMPDeclareVariantA = OMPDeclareVariantAttr::CreateImplicit( 5898 Context, VariantFuncRef, DVScope.TI); 5899 BaseFD->addAttr(OMPDeclareVariantA); 5900 } 5901 5902 ExprResult Sema::ActOnOpenMPCall(ExprResult Call, Scope *Scope, 5903 SourceLocation LParenLoc, 5904 MultiExprArg ArgExprs, 5905 SourceLocation RParenLoc, Expr *ExecConfig) { 5906 // The common case is a regular call we do not want to specialize at all. Try 5907 // to make that case fast by bailing early. 5908 CallExpr *CE = dyn_cast<CallExpr>(Call.get()); 5909 if (!CE) 5910 return Call; 5911 5912 FunctionDecl *CalleeFnDecl = CE->getDirectCallee(); 5913 if (!CalleeFnDecl) 5914 return Call; 5915 5916 if (!CalleeFnDecl->hasAttr<OMPDeclareVariantAttr>()) 5917 return Call; 5918 5919 ASTContext &Context = getASTContext(); 5920 std::function<void(StringRef)> DiagUnknownTrait = [this, 5921 CE](StringRef ISATrait) { 5922 // TODO Track the selector locations in a way that is accessible here to 5923 // improve the diagnostic location. 5924 Diag(CE->getBeginLoc(), diag::warn_unknown_declare_variant_isa_trait) 5925 << ISATrait; 5926 }; 5927 TargetOMPContext OMPCtx(Context, std::move(DiagUnknownTrait), 5928 getCurFunctionDecl()); 5929 5930 SmallVector<Expr *, 4> Exprs; 5931 SmallVector<VariantMatchInfo, 4> VMIs; 5932 while (CalleeFnDecl) { 5933 for (OMPDeclareVariantAttr *A : 5934 CalleeFnDecl->specific_attrs<OMPDeclareVariantAttr>()) { 5935 Expr *VariantRef = A->getVariantFuncRef(); 5936 5937 VariantMatchInfo VMI; 5938 OMPTraitInfo &TI = A->getTraitInfo(); 5939 TI.getAsVariantMatchInfo(Context, VMI); 5940 if (!isVariantApplicableInContext(VMI, OMPCtx, 5941 /* DeviceSetOnly */ false)) 5942 continue; 5943 5944 VMIs.push_back(VMI); 5945 Exprs.push_back(VariantRef); 5946 } 5947 5948 CalleeFnDecl = CalleeFnDecl->getPreviousDecl(); 5949 } 5950 5951 ExprResult NewCall; 5952 do { 5953 int BestIdx = getBestVariantMatchForContext(VMIs, OMPCtx); 5954 if (BestIdx < 0) 5955 return Call; 5956 Expr *BestExpr = cast<DeclRefExpr>(Exprs[BestIdx]); 5957 Decl *BestDecl = cast<DeclRefExpr>(BestExpr)->getDecl(); 5958 5959 { 5960 // Try to build a (member) call expression for the current best applicable 5961 // variant expression. We allow this to fail in which case we continue 5962 // with the next best variant expression. The fail case is part of the 5963 // implementation defined behavior in the OpenMP standard when it talks 5964 // about what differences in the function prototypes: "Any differences 5965 // that the specific OpenMP context requires in the prototype of the 5966 // variant from the base function prototype are implementation defined." 5967 // This wording is there to allow the specialized variant to have a 5968 // different type than the base function. This is intended and OK but if 5969 // we cannot create a call the difference is not in the "implementation 5970 // defined range" we allow. 5971 Sema::TentativeAnalysisScope Trap(*this); 5972 5973 if (auto *SpecializedMethod = dyn_cast<CXXMethodDecl>(BestDecl)) { 5974 auto *MemberCall = dyn_cast<CXXMemberCallExpr>(CE); 5975 BestExpr = MemberExpr::CreateImplicit( 5976 Context, MemberCall->getImplicitObjectArgument(), 5977 /* IsArrow */ false, SpecializedMethod, Context.BoundMemberTy, 5978 MemberCall->getValueKind(), MemberCall->getObjectKind()); 5979 } 5980 NewCall = BuildCallExpr(Scope, BestExpr, LParenLoc, ArgExprs, RParenLoc, 5981 ExecConfig); 5982 if (NewCall.isUsable()) 5983 break; 5984 } 5985 5986 VMIs.erase(VMIs.begin() + BestIdx); 5987 Exprs.erase(Exprs.begin() + BestIdx); 5988 } while (!VMIs.empty()); 5989 5990 if (!NewCall.isUsable()) 5991 return Call; 5992 return PseudoObjectExpr::Create(Context, CE, {NewCall.get()}, 0); 5993 } 5994 5995 Optional<std::pair<FunctionDecl *, Expr *>> 5996 Sema::checkOpenMPDeclareVariantFunction(Sema::DeclGroupPtrTy DG, 5997 Expr *VariantRef, OMPTraitInfo &TI, 5998 SourceRange SR) { 5999 if (!DG || DG.get().isNull()) 6000 return None; 6001 6002 const int VariantId = 1; 6003 // Must be applied only to single decl. 6004 if (!DG.get().isSingleDecl()) { 6005 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd_variant) 6006 << VariantId << SR; 6007 return None; 6008 } 6009 Decl *ADecl = DG.get().getSingleDecl(); 6010 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl)) 6011 ADecl = FTD->getTemplatedDecl(); 6012 6013 // Decl must be a function. 6014 auto *FD = dyn_cast<FunctionDecl>(ADecl); 6015 if (!FD) { 6016 Diag(ADecl->getLocation(), diag::err_omp_function_expected) 6017 << VariantId << SR; 6018 return None; 6019 } 6020 6021 auto &&HasMultiVersionAttributes = [](const FunctionDecl *FD) { 6022 return FD->hasAttrs() && 6023 (FD->hasAttr<CPUDispatchAttr>() || FD->hasAttr<CPUSpecificAttr>() || 6024 FD->hasAttr<TargetAttr>()); 6025 }; 6026 // OpenMP is not compatible with CPU-specific attributes. 6027 if (HasMultiVersionAttributes(FD)) { 6028 Diag(FD->getLocation(), diag::err_omp_declare_variant_incompat_attributes) 6029 << SR; 6030 return None; 6031 } 6032 6033 // Allow #pragma omp declare variant only if the function is not used. 6034 if (FD->isUsed(false)) 6035 Diag(SR.getBegin(), diag::warn_omp_declare_variant_after_used) 6036 << FD->getLocation(); 6037 6038 // Check if the function was emitted already. 6039 const FunctionDecl *Definition; 6040 if (!FD->isThisDeclarationADefinition() && FD->isDefined(Definition) && 6041 (LangOpts.EmitAllDecls || Context.DeclMustBeEmitted(Definition))) 6042 Diag(SR.getBegin(), diag::warn_omp_declare_variant_after_emitted) 6043 << FD->getLocation(); 6044 6045 // The VariantRef must point to function. 6046 if (!VariantRef) { 6047 Diag(SR.getBegin(), diag::err_omp_function_expected) << VariantId; 6048 return None; 6049 } 6050 6051 auto ShouldDelayChecks = [](Expr *&E, bool) { 6052 return E && (E->isTypeDependent() || E->isValueDependent() || 6053 E->containsUnexpandedParameterPack() || 6054 E->isInstantiationDependent()); 6055 }; 6056 // Do not check templates, wait until instantiation. 6057 if (FD->isDependentContext() || ShouldDelayChecks(VariantRef, false) || 6058 TI.anyScoreOrCondition(ShouldDelayChecks)) 6059 return std::make_pair(FD, VariantRef); 6060 6061 // Deal with non-constant score and user condition expressions. 6062 auto HandleNonConstantScoresAndConditions = [this](Expr *&E, 6063 bool IsScore) -> bool { 6064 if (!E || E->isIntegerConstantExpr(Context)) 6065 return false; 6066 6067 if (IsScore) { 6068 // We warn on non-constant scores and pretend they were not present. 6069 Diag(E->getExprLoc(), diag::warn_omp_declare_variant_score_not_constant) 6070 << E; 6071 E = nullptr; 6072 } else { 6073 // We could replace a non-constant user condition with "false" but we 6074 // will soon need to handle these anyway for the dynamic version of 6075 // OpenMP context selectors. 6076 Diag(E->getExprLoc(), 6077 diag::err_omp_declare_variant_user_condition_not_constant) 6078 << E; 6079 } 6080 return true; 6081 }; 6082 if (TI.anyScoreOrCondition(HandleNonConstantScoresAndConditions)) 6083 return None; 6084 6085 // Convert VariantRef expression to the type of the original function to 6086 // resolve possible conflicts. 6087 ExprResult VariantRefCast; 6088 if (LangOpts.CPlusPlus) { 6089 QualType FnPtrType; 6090 auto *Method = dyn_cast<CXXMethodDecl>(FD); 6091 if (Method && !Method->isStatic()) { 6092 const Type *ClassType = 6093 Context.getTypeDeclType(Method->getParent()).getTypePtr(); 6094 FnPtrType = Context.getMemberPointerType(FD->getType(), ClassType); 6095 ExprResult ER; 6096 { 6097 // Build adrr_of unary op to correctly handle type checks for member 6098 // functions. 6099 Sema::TentativeAnalysisScope Trap(*this); 6100 ER = CreateBuiltinUnaryOp(VariantRef->getBeginLoc(), UO_AddrOf, 6101 VariantRef); 6102 } 6103 if (!ER.isUsable()) { 6104 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected) 6105 << VariantId << VariantRef->getSourceRange(); 6106 return None; 6107 } 6108 VariantRef = ER.get(); 6109 } else { 6110 FnPtrType = Context.getPointerType(FD->getType()); 6111 } 6112 ImplicitConversionSequence ICS = 6113 TryImplicitConversion(VariantRef, FnPtrType.getUnqualifiedType(), 6114 /*SuppressUserConversions=*/false, 6115 AllowedExplicit::None, 6116 /*InOverloadResolution=*/false, 6117 /*CStyle=*/false, 6118 /*AllowObjCWritebackConversion=*/false); 6119 if (ICS.isFailure()) { 6120 Diag(VariantRef->getExprLoc(), 6121 diag::err_omp_declare_variant_incompat_types) 6122 << VariantRef->getType() 6123 << ((Method && !Method->isStatic()) ? FnPtrType : FD->getType()) 6124 << VariantRef->getSourceRange(); 6125 return None; 6126 } 6127 VariantRefCast = PerformImplicitConversion( 6128 VariantRef, FnPtrType.getUnqualifiedType(), AA_Converting); 6129 if (!VariantRefCast.isUsable()) 6130 return None; 6131 // Drop previously built artificial addr_of unary op for member functions. 6132 if (Method && !Method->isStatic()) { 6133 Expr *PossibleAddrOfVariantRef = VariantRefCast.get(); 6134 if (auto *UO = dyn_cast<UnaryOperator>( 6135 PossibleAddrOfVariantRef->IgnoreImplicit())) 6136 VariantRefCast = UO->getSubExpr(); 6137 } 6138 } else { 6139 VariantRefCast = VariantRef; 6140 } 6141 6142 ExprResult ER = CheckPlaceholderExpr(VariantRefCast.get()); 6143 if (!ER.isUsable() || 6144 !ER.get()->IgnoreParenImpCasts()->getType()->isFunctionType()) { 6145 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected) 6146 << VariantId << VariantRef->getSourceRange(); 6147 return None; 6148 } 6149 6150 // The VariantRef must point to function. 6151 auto *DRE = dyn_cast<DeclRefExpr>(ER.get()->IgnoreParenImpCasts()); 6152 if (!DRE) { 6153 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected) 6154 << VariantId << VariantRef->getSourceRange(); 6155 return None; 6156 } 6157 auto *NewFD = dyn_cast_or_null<FunctionDecl>(DRE->getDecl()); 6158 if (!NewFD) { 6159 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected) 6160 << VariantId << VariantRef->getSourceRange(); 6161 return None; 6162 } 6163 6164 // Check if function types are compatible in C. 6165 if (!LangOpts.CPlusPlus) { 6166 QualType NewType = 6167 Context.mergeFunctionTypes(FD->getType(), NewFD->getType()); 6168 if (NewType.isNull()) { 6169 Diag(VariantRef->getExprLoc(), 6170 diag::err_omp_declare_variant_incompat_types) 6171 << NewFD->getType() << FD->getType() << VariantRef->getSourceRange(); 6172 return None; 6173 } 6174 if (NewType->isFunctionProtoType()) { 6175 if (FD->getType()->isFunctionNoProtoType()) 6176 setPrototype(*this, FD, NewFD, NewType); 6177 else if (NewFD->getType()->isFunctionNoProtoType()) 6178 setPrototype(*this, NewFD, FD, NewType); 6179 } 6180 } 6181 6182 // Check if variant function is not marked with declare variant directive. 6183 if (NewFD->hasAttrs() && NewFD->hasAttr<OMPDeclareVariantAttr>()) { 6184 Diag(VariantRef->getExprLoc(), 6185 diag::warn_omp_declare_variant_marked_as_declare_variant) 6186 << VariantRef->getSourceRange(); 6187 SourceRange SR = 6188 NewFD->specific_attr_begin<OMPDeclareVariantAttr>()->getRange(); 6189 Diag(SR.getBegin(), diag::note_omp_marked_declare_variant_here) << SR; 6190 return None; 6191 } 6192 6193 enum DoesntSupport { 6194 VirtFuncs = 1, 6195 Constructors = 3, 6196 Destructors = 4, 6197 DeletedFuncs = 5, 6198 DefaultedFuncs = 6, 6199 ConstexprFuncs = 7, 6200 ConstevalFuncs = 8, 6201 }; 6202 if (const auto *CXXFD = dyn_cast<CXXMethodDecl>(FD)) { 6203 if (CXXFD->isVirtual()) { 6204 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 6205 << VirtFuncs; 6206 return None; 6207 } 6208 6209 if (isa<CXXConstructorDecl>(FD)) { 6210 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 6211 << Constructors; 6212 return None; 6213 } 6214 6215 if (isa<CXXDestructorDecl>(FD)) { 6216 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 6217 << Destructors; 6218 return None; 6219 } 6220 } 6221 6222 if (FD->isDeleted()) { 6223 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 6224 << DeletedFuncs; 6225 return None; 6226 } 6227 6228 if (FD->isDefaulted()) { 6229 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 6230 << DefaultedFuncs; 6231 return None; 6232 } 6233 6234 if (FD->isConstexpr()) { 6235 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 6236 << (NewFD->isConsteval() ? ConstevalFuncs : ConstexprFuncs); 6237 return None; 6238 } 6239 6240 // Check general compatibility. 6241 if (areMultiversionVariantFunctionsCompatible( 6242 FD, NewFD, PartialDiagnostic::NullDiagnostic(), 6243 PartialDiagnosticAt(SourceLocation(), 6244 PartialDiagnostic::NullDiagnostic()), 6245 PartialDiagnosticAt( 6246 VariantRef->getExprLoc(), 6247 PDiag(diag::err_omp_declare_variant_doesnt_support)), 6248 PartialDiagnosticAt(VariantRef->getExprLoc(), 6249 PDiag(diag::err_omp_declare_variant_diff) 6250 << FD->getLocation()), 6251 /*TemplatesSupported=*/true, /*ConstexprSupported=*/false, 6252 /*CLinkageMayDiffer=*/true)) 6253 return None; 6254 return std::make_pair(FD, cast<Expr>(DRE)); 6255 } 6256 6257 void Sema::ActOnOpenMPDeclareVariantDirective(FunctionDecl *FD, 6258 Expr *VariantRef, 6259 OMPTraitInfo &TI, 6260 SourceRange SR) { 6261 auto *NewAttr = 6262 OMPDeclareVariantAttr::CreateImplicit(Context, VariantRef, &TI, SR); 6263 FD->addAttr(NewAttr); 6264 } 6265 6266 StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses, 6267 Stmt *AStmt, 6268 SourceLocation StartLoc, 6269 SourceLocation EndLoc) { 6270 if (!AStmt) 6271 return StmtError(); 6272 6273 auto *CS = cast<CapturedStmt>(AStmt); 6274 // 1.2.2 OpenMP Language Terminology 6275 // Structured block - An executable statement with a single entry at the 6276 // top and a single exit at the bottom. 6277 // The point of exit cannot be a branch out of the structured block. 6278 // longjmp() and throw() must not violate the entry/exit criteria. 6279 CS->getCapturedDecl()->setNothrow(); 6280 6281 setFunctionHasBranchProtectedScope(); 6282 6283 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 6284 DSAStack->getTaskgroupReductionRef(), 6285 DSAStack->isCancelRegion()); 6286 } 6287 6288 namespace { 6289 /// Iteration space of a single for loop. 6290 struct LoopIterationSpace final { 6291 /// True if the condition operator is the strict compare operator (<, > or 6292 /// !=). 6293 bool IsStrictCompare = false; 6294 /// Condition of the loop. 6295 Expr *PreCond = nullptr; 6296 /// This expression calculates the number of iterations in the loop. 6297 /// It is always possible to calculate it before starting the loop. 6298 Expr *NumIterations = nullptr; 6299 /// The loop counter variable. 6300 Expr *CounterVar = nullptr; 6301 /// Private loop counter variable. 6302 Expr *PrivateCounterVar = nullptr; 6303 /// This is initializer for the initial value of #CounterVar. 6304 Expr *CounterInit = nullptr; 6305 /// This is step for the #CounterVar used to generate its update: 6306 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration. 6307 Expr *CounterStep = nullptr; 6308 /// Should step be subtracted? 6309 bool Subtract = false; 6310 /// Source range of the loop init. 6311 SourceRange InitSrcRange; 6312 /// Source range of the loop condition. 6313 SourceRange CondSrcRange; 6314 /// Source range of the loop increment. 6315 SourceRange IncSrcRange; 6316 /// Minimum value that can have the loop control variable. Used to support 6317 /// non-rectangular loops. Applied only for LCV with the non-iterator types, 6318 /// since only such variables can be used in non-loop invariant expressions. 6319 Expr *MinValue = nullptr; 6320 /// Maximum value that can have the loop control variable. Used to support 6321 /// non-rectangular loops. Applied only for LCV with the non-iterator type, 6322 /// since only such variables can be used in non-loop invariant expressions. 6323 Expr *MaxValue = nullptr; 6324 /// true, if the lower bound depends on the outer loop control var. 6325 bool IsNonRectangularLB = false; 6326 /// true, if the upper bound depends on the outer loop control var. 6327 bool IsNonRectangularUB = false; 6328 /// Index of the loop this loop depends on and forms non-rectangular loop 6329 /// nest. 6330 unsigned LoopDependentIdx = 0; 6331 /// Final condition for the non-rectangular loop nest support. It is used to 6332 /// check that the number of iterations for this particular counter must be 6333 /// finished. 6334 Expr *FinalCondition = nullptr; 6335 }; 6336 6337 /// Helper class for checking canonical form of the OpenMP loops and 6338 /// extracting iteration space of each loop in the loop nest, that will be used 6339 /// for IR generation. 6340 class OpenMPIterationSpaceChecker { 6341 /// Reference to Sema. 6342 Sema &SemaRef; 6343 /// Data-sharing stack. 6344 DSAStackTy &Stack; 6345 /// A location for diagnostics (when there is no some better location). 6346 SourceLocation DefaultLoc; 6347 /// A location for diagnostics (when increment is not compatible). 6348 SourceLocation ConditionLoc; 6349 /// A source location for referring to loop init later. 6350 SourceRange InitSrcRange; 6351 /// A source location for referring to condition later. 6352 SourceRange ConditionSrcRange; 6353 /// A source location for referring to increment later. 6354 SourceRange IncrementSrcRange; 6355 /// Loop variable. 6356 ValueDecl *LCDecl = nullptr; 6357 /// Reference to loop variable. 6358 Expr *LCRef = nullptr; 6359 /// Lower bound (initializer for the var). 6360 Expr *LB = nullptr; 6361 /// Upper bound. 6362 Expr *UB = nullptr; 6363 /// Loop step (increment). 6364 Expr *Step = nullptr; 6365 /// This flag is true when condition is one of: 6366 /// Var < UB 6367 /// Var <= UB 6368 /// UB > Var 6369 /// UB >= Var 6370 /// This will have no value when the condition is != 6371 llvm::Optional<bool> TestIsLessOp; 6372 /// This flag is true when condition is strict ( < or > ). 6373 bool TestIsStrictOp = false; 6374 /// This flag is true when step is subtracted on each iteration. 6375 bool SubtractStep = false; 6376 /// The outer loop counter this loop depends on (if any). 6377 const ValueDecl *DepDecl = nullptr; 6378 /// Contains number of loop (starts from 1) on which loop counter init 6379 /// expression of this loop depends on. 6380 Optional<unsigned> InitDependOnLC; 6381 /// Contains number of loop (starts from 1) on which loop counter condition 6382 /// expression of this loop depends on. 6383 Optional<unsigned> CondDependOnLC; 6384 /// Checks if the provide statement depends on the loop counter. 6385 Optional<unsigned> doesDependOnLoopCounter(const Stmt *S, bool IsInitializer); 6386 /// Original condition required for checking of the exit condition for 6387 /// non-rectangular loop. 6388 Expr *Condition = nullptr; 6389 6390 public: 6391 OpenMPIterationSpaceChecker(Sema &SemaRef, DSAStackTy &Stack, 6392 SourceLocation DefaultLoc) 6393 : SemaRef(SemaRef), Stack(Stack), DefaultLoc(DefaultLoc), 6394 ConditionLoc(DefaultLoc) {} 6395 /// Check init-expr for canonical loop form and save loop counter 6396 /// variable - #Var and its initialization value - #LB. 6397 bool checkAndSetInit(Stmt *S, bool EmitDiags = true); 6398 /// Check test-expr for canonical form, save upper-bound (#UB), flags 6399 /// for less/greater and for strict/non-strict comparison. 6400 bool checkAndSetCond(Expr *S); 6401 /// Check incr-expr for canonical loop form and return true if it 6402 /// does not conform, otherwise save loop step (#Step). 6403 bool checkAndSetInc(Expr *S); 6404 /// Return the loop counter variable. 6405 ValueDecl *getLoopDecl() const { return LCDecl; } 6406 /// Return the reference expression to loop counter variable. 6407 Expr *getLoopDeclRefExpr() const { return LCRef; } 6408 /// Source range of the loop init. 6409 SourceRange getInitSrcRange() const { return InitSrcRange; } 6410 /// Source range of the loop condition. 6411 SourceRange getConditionSrcRange() const { return ConditionSrcRange; } 6412 /// Source range of the loop increment. 6413 SourceRange getIncrementSrcRange() const { return IncrementSrcRange; } 6414 /// True if the step should be subtracted. 6415 bool shouldSubtractStep() const { return SubtractStep; } 6416 /// True, if the compare operator is strict (<, > or !=). 6417 bool isStrictTestOp() const { return TestIsStrictOp; } 6418 /// Build the expression to calculate the number of iterations. 6419 Expr *buildNumIterations( 6420 Scope *S, ArrayRef<LoopIterationSpace> ResultIterSpaces, bool LimitedType, 6421 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 6422 /// Build the precondition expression for the loops. 6423 Expr * 6424 buildPreCond(Scope *S, Expr *Cond, 6425 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 6426 /// Build reference expression to the counter be used for codegen. 6427 DeclRefExpr * 6428 buildCounterVar(llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 6429 DSAStackTy &DSA) const; 6430 /// Build reference expression to the private counter be used for 6431 /// codegen. 6432 Expr *buildPrivateCounterVar() const; 6433 /// Build initialization of the counter be used for codegen. 6434 Expr *buildCounterInit() const; 6435 /// Build step of the counter be used for codegen. 6436 Expr *buildCounterStep() const; 6437 /// Build loop data with counter value for depend clauses in ordered 6438 /// directives. 6439 Expr * 6440 buildOrderedLoopData(Scope *S, Expr *Counter, 6441 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 6442 SourceLocation Loc, Expr *Inc = nullptr, 6443 OverloadedOperatorKind OOK = OO_Amp); 6444 /// Builds the minimum value for the loop counter. 6445 std::pair<Expr *, Expr *> buildMinMaxValues( 6446 Scope *S, llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 6447 /// Builds final condition for the non-rectangular loops. 6448 Expr *buildFinalCondition(Scope *S) const; 6449 /// Return true if any expression is dependent. 6450 bool dependent() const; 6451 /// Returns true if the initializer forms non-rectangular loop. 6452 bool doesInitDependOnLC() const { return InitDependOnLC.hasValue(); } 6453 /// Returns true if the condition forms non-rectangular loop. 6454 bool doesCondDependOnLC() const { return CondDependOnLC.hasValue(); } 6455 /// Returns index of the loop we depend on (starting from 1), or 0 otherwise. 6456 unsigned getLoopDependentIdx() const { 6457 return InitDependOnLC.getValueOr(CondDependOnLC.getValueOr(0)); 6458 } 6459 6460 private: 6461 /// Check the right-hand side of an assignment in the increment 6462 /// expression. 6463 bool checkAndSetIncRHS(Expr *RHS); 6464 /// Helper to set loop counter variable and its initializer. 6465 bool setLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB, 6466 bool EmitDiags); 6467 /// Helper to set upper bound. 6468 bool setUB(Expr *NewUB, llvm::Optional<bool> LessOp, bool StrictOp, 6469 SourceRange SR, SourceLocation SL); 6470 /// Helper to set loop increment. 6471 bool setStep(Expr *NewStep, bool Subtract); 6472 }; 6473 6474 bool OpenMPIterationSpaceChecker::dependent() const { 6475 if (!LCDecl) { 6476 assert(!LB && !UB && !Step); 6477 return false; 6478 } 6479 return LCDecl->getType()->isDependentType() || 6480 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) || 6481 (Step && Step->isValueDependent()); 6482 } 6483 6484 bool OpenMPIterationSpaceChecker::setLCDeclAndLB(ValueDecl *NewLCDecl, 6485 Expr *NewLCRefExpr, 6486 Expr *NewLB, bool EmitDiags) { 6487 // State consistency checking to ensure correct usage. 6488 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr && 6489 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp); 6490 if (!NewLCDecl || !NewLB) 6491 return true; 6492 LCDecl = getCanonicalDecl(NewLCDecl); 6493 LCRef = NewLCRefExpr; 6494 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB)) 6495 if (const CXXConstructorDecl *Ctor = CE->getConstructor()) 6496 if ((Ctor->isCopyOrMoveConstructor() || 6497 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && 6498 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) 6499 NewLB = CE->getArg(0)->IgnoreParenImpCasts(); 6500 LB = NewLB; 6501 if (EmitDiags) 6502 InitDependOnLC = doesDependOnLoopCounter(LB, /*IsInitializer=*/true); 6503 return false; 6504 } 6505 6506 bool OpenMPIterationSpaceChecker::setUB(Expr *NewUB, 6507 llvm::Optional<bool> LessOp, 6508 bool StrictOp, SourceRange SR, 6509 SourceLocation SL) { 6510 // State consistency checking to ensure correct usage. 6511 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr && 6512 Step == nullptr && !TestIsLessOp && !TestIsStrictOp); 6513 if (!NewUB) 6514 return true; 6515 UB = NewUB; 6516 if (LessOp) 6517 TestIsLessOp = LessOp; 6518 TestIsStrictOp = StrictOp; 6519 ConditionSrcRange = SR; 6520 ConditionLoc = SL; 6521 CondDependOnLC = doesDependOnLoopCounter(UB, /*IsInitializer=*/false); 6522 return false; 6523 } 6524 6525 bool OpenMPIterationSpaceChecker::setStep(Expr *NewStep, bool Subtract) { 6526 // State consistency checking to ensure correct usage. 6527 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr); 6528 if (!NewStep) 6529 return true; 6530 if (!NewStep->isValueDependent()) { 6531 // Check that the step is integer expression. 6532 SourceLocation StepLoc = NewStep->getBeginLoc(); 6533 ExprResult Val = SemaRef.PerformOpenMPImplicitIntegerConversion( 6534 StepLoc, getExprAsWritten(NewStep)); 6535 if (Val.isInvalid()) 6536 return true; 6537 NewStep = Val.get(); 6538 6539 // OpenMP [2.6, Canonical Loop Form, Restrictions] 6540 // If test-expr is of form var relational-op b and relational-op is < or 6541 // <= then incr-expr must cause var to increase on each iteration of the 6542 // loop. If test-expr is of form var relational-op b and relational-op is 6543 // > or >= then incr-expr must cause var to decrease on each iteration of 6544 // the loop. 6545 // If test-expr is of form b relational-op var and relational-op is < or 6546 // <= then incr-expr must cause var to decrease on each iteration of the 6547 // loop. If test-expr is of form b relational-op var and relational-op is 6548 // > or >= then incr-expr must cause var to increase on each iteration of 6549 // the loop. 6550 Optional<llvm::APSInt> Result = 6551 NewStep->getIntegerConstantExpr(SemaRef.Context); 6552 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation(); 6553 bool IsConstNeg = 6554 Result && Result->isSigned() && (Subtract != Result->isNegative()); 6555 bool IsConstPos = 6556 Result && Result->isSigned() && (Subtract == Result->isNegative()); 6557 bool IsConstZero = Result && !Result->getBoolValue(); 6558 6559 // != with increment is treated as <; != with decrement is treated as > 6560 if (!TestIsLessOp.hasValue()) 6561 TestIsLessOp = IsConstPos || (IsUnsigned && !Subtract); 6562 if (UB && (IsConstZero || 6563 (TestIsLessOp.getValue() ? 6564 (IsConstNeg || (IsUnsigned && Subtract)) : 6565 (IsConstPos || (IsUnsigned && !Subtract))))) { 6566 SemaRef.Diag(NewStep->getExprLoc(), 6567 diag::err_omp_loop_incr_not_compatible) 6568 << LCDecl << TestIsLessOp.getValue() << NewStep->getSourceRange(); 6569 SemaRef.Diag(ConditionLoc, 6570 diag::note_omp_loop_cond_requres_compatible_incr) 6571 << TestIsLessOp.getValue() << ConditionSrcRange; 6572 return true; 6573 } 6574 if (TestIsLessOp.getValue() == Subtract) { 6575 NewStep = 6576 SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep) 6577 .get(); 6578 Subtract = !Subtract; 6579 } 6580 } 6581 6582 Step = NewStep; 6583 SubtractStep = Subtract; 6584 return false; 6585 } 6586 6587 namespace { 6588 /// Checker for the non-rectangular loops. Checks if the initializer or 6589 /// condition expression references loop counter variable. 6590 class LoopCounterRefChecker final 6591 : public ConstStmtVisitor<LoopCounterRefChecker, bool> { 6592 Sema &SemaRef; 6593 DSAStackTy &Stack; 6594 const ValueDecl *CurLCDecl = nullptr; 6595 const ValueDecl *DepDecl = nullptr; 6596 const ValueDecl *PrevDepDecl = nullptr; 6597 bool IsInitializer = true; 6598 unsigned BaseLoopId = 0; 6599 bool checkDecl(const Expr *E, const ValueDecl *VD) { 6600 if (getCanonicalDecl(VD) == getCanonicalDecl(CurLCDecl)) { 6601 SemaRef.Diag(E->getExprLoc(), diag::err_omp_stmt_depends_on_loop_counter) 6602 << (IsInitializer ? 0 : 1); 6603 return false; 6604 } 6605 const auto &&Data = Stack.isLoopControlVariable(VD); 6606 // OpenMP, 2.9.1 Canonical Loop Form, Restrictions. 6607 // The type of the loop iterator on which we depend may not have a random 6608 // access iterator type. 6609 if (Data.first && VD->getType()->isRecordType()) { 6610 SmallString<128> Name; 6611 llvm::raw_svector_ostream OS(Name); 6612 VD->getNameForDiagnostic(OS, SemaRef.getPrintingPolicy(), 6613 /*Qualified=*/true); 6614 SemaRef.Diag(E->getExprLoc(), 6615 diag::err_omp_wrong_dependency_iterator_type) 6616 << OS.str(); 6617 SemaRef.Diag(VD->getLocation(), diag::note_previous_decl) << VD; 6618 return false; 6619 } 6620 if (Data.first && 6621 (DepDecl || (PrevDepDecl && 6622 getCanonicalDecl(VD) != getCanonicalDecl(PrevDepDecl)))) { 6623 if (!DepDecl && PrevDepDecl) 6624 DepDecl = PrevDepDecl; 6625 SmallString<128> Name; 6626 llvm::raw_svector_ostream OS(Name); 6627 DepDecl->getNameForDiagnostic(OS, SemaRef.getPrintingPolicy(), 6628 /*Qualified=*/true); 6629 SemaRef.Diag(E->getExprLoc(), 6630 diag::err_omp_invariant_or_linear_dependency) 6631 << OS.str(); 6632 return false; 6633 } 6634 if (Data.first) { 6635 DepDecl = VD; 6636 BaseLoopId = Data.first; 6637 } 6638 return Data.first; 6639 } 6640 6641 public: 6642 bool VisitDeclRefExpr(const DeclRefExpr *E) { 6643 const ValueDecl *VD = E->getDecl(); 6644 if (isa<VarDecl>(VD)) 6645 return checkDecl(E, VD); 6646 return false; 6647 } 6648 bool VisitMemberExpr(const MemberExpr *E) { 6649 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) { 6650 const ValueDecl *VD = E->getMemberDecl(); 6651 if (isa<VarDecl>(VD) || isa<FieldDecl>(VD)) 6652 return checkDecl(E, VD); 6653 } 6654 return false; 6655 } 6656 bool VisitStmt(const Stmt *S) { 6657 bool Res = false; 6658 for (const Stmt *Child : S->children()) 6659 Res = (Child && Visit(Child)) || Res; 6660 return Res; 6661 } 6662 explicit LoopCounterRefChecker(Sema &SemaRef, DSAStackTy &Stack, 6663 const ValueDecl *CurLCDecl, bool IsInitializer, 6664 const ValueDecl *PrevDepDecl = nullptr) 6665 : SemaRef(SemaRef), Stack(Stack), CurLCDecl(CurLCDecl), 6666 PrevDepDecl(PrevDepDecl), IsInitializer(IsInitializer) {} 6667 unsigned getBaseLoopId() const { 6668 assert(CurLCDecl && "Expected loop dependency."); 6669 return BaseLoopId; 6670 } 6671 const ValueDecl *getDepDecl() const { 6672 assert(CurLCDecl && "Expected loop dependency."); 6673 return DepDecl; 6674 } 6675 }; 6676 } // namespace 6677 6678 Optional<unsigned> 6679 OpenMPIterationSpaceChecker::doesDependOnLoopCounter(const Stmt *S, 6680 bool IsInitializer) { 6681 // Check for the non-rectangular loops. 6682 LoopCounterRefChecker LoopStmtChecker(SemaRef, Stack, LCDecl, IsInitializer, 6683 DepDecl); 6684 if (LoopStmtChecker.Visit(S)) { 6685 DepDecl = LoopStmtChecker.getDepDecl(); 6686 return LoopStmtChecker.getBaseLoopId(); 6687 } 6688 return llvm::None; 6689 } 6690 6691 bool OpenMPIterationSpaceChecker::checkAndSetInit(Stmt *S, bool EmitDiags) { 6692 // Check init-expr for canonical loop form and save loop counter 6693 // variable - #Var and its initialization value - #LB. 6694 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following: 6695 // var = lb 6696 // integer-type var = lb 6697 // random-access-iterator-type var = lb 6698 // pointer-type var = lb 6699 // 6700 if (!S) { 6701 if (EmitDiags) { 6702 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init); 6703 } 6704 return true; 6705 } 6706 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S)) 6707 if (!ExprTemp->cleanupsHaveSideEffects()) 6708 S = ExprTemp->getSubExpr(); 6709 6710 InitSrcRange = S->getSourceRange(); 6711 if (Expr *E = dyn_cast<Expr>(S)) 6712 S = E->IgnoreParens(); 6713 if (auto *BO = dyn_cast<BinaryOperator>(S)) { 6714 if (BO->getOpcode() == BO_Assign) { 6715 Expr *LHS = BO->getLHS()->IgnoreParens(); 6716 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) { 6717 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) 6718 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 6719 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 6720 EmitDiags); 6721 return setLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS(), EmitDiags); 6722 } 6723 if (auto *ME = dyn_cast<MemberExpr>(LHS)) { 6724 if (ME->isArrow() && 6725 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 6726 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 6727 EmitDiags); 6728 } 6729 } 6730 } else if (auto *DS = dyn_cast<DeclStmt>(S)) { 6731 if (DS->isSingleDecl()) { 6732 if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) { 6733 if (Var->hasInit() && !Var->getType()->isReferenceType()) { 6734 // Accept non-canonical init form here but emit ext. warning. 6735 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags) 6736 SemaRef.Diag(S->getBeginLoc(), 6737 diag::ext_omp_loop_not_canonical_init) 6738 << S->getSourceRange(); 6739 return setLCDeclAndLB( 6740 Var, 6741 buildDeclRefExpr(SemaRef, Var, 6742 Var->getType().getNonReferenceType(), 6743 DS->getBeginLoc()), 6744 Var->getInit(), EmitDiags); 6745 } 6746 } 6747 } 6748 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 6749 if (CE->getOperator() == OO_Equal) { 6750 Expr *LHS = CE->getArg(0); 6751 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) { 6752 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) 6753 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 6754 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 6755 EmitDiags); 6756 return setLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1), EmitDiags); 6757 } 6758 if (auto *ME = dyn_cast<MemberExpr>(LHS)) { 6759 if (ME->isArrow() && 6760 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 6761 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 6762 EmitDiags); 6763 } 6764 } 6765 } 6766 6767 if (dependent() || SemaRef.CurContext->isDependentContext()) 6768 return false; 6769 if (EmitDiags) { 6770 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_init) 6771 << S->getSourceRange(); 6772 } 6773 return true; 6774 } 6775 6776 /// Ignore parenthesizes, implicit casts, copy constructor and return the 6777 /// variable (which may be the loop variable) if possible. 6778 static const ValueDecl *getInitLCDecl(const Expr *E) { 6779 if (!E) 6780 return nullptr; 6781 E = getExprAsWritten(E); 6782 if (const auto *CE = dyn_cast_or_null<CXXConstructExpr>(E)) 6783 if (const CXXConstructorDecl *Ctor = CE->getConstructor()) 6784 if ((Ctor->isCopyOrMoveConstructor() || 6785 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && 6786 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) 6787 E = CE->getArg(0)->IgnoreParenImpCasts(); 6788 if (const auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) { 6789 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) 6790 return getCanonicalDecl(VD); 6791 } 6792 if (const auto *ME = dyn_cast_or_null<MemberExpr>(E)) 6793 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 6794 return getCanonicalDecl(ME->getMemberDecl()); 6795 return nullptr; 6796 } 6797 6798 bool OpenMPIterationSpaceChecker::checkAndSetCond(Expr *S) { 6799 // Check test-expr for canonical form, save upper-bound UB, flags for 6800 // less/greater and for strict/non-strict comparison. 6801 // OpenMP [2.9] Canonical loop form. Test-expr may be one of the following: 6802 // var relational-op b 6803 // b relational-op var 6804 // 6805 bool IneqCondIsCanonical = SemaRef.getLangOpts().OpenMP >= 50; 6806 if (!S) { 6807 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) 6808 << (IneqCondIsCanonical ? 1 : 0) << LCDecl; 6809 return true; 6810 } 6811 Condition = S; 6812 S = getExprAsWritten(S); 6813 SourceLocation CondLoc = S->getBeginLoc(); 6814 if (auto *BO = dyn_cast<BinaryOperator>(S)) { 6815 if (BO->isRelationalOp()) { 6816 if (getInitLCDecl(BO->getLHS()) == LCDecl) 6817 return setUB(BO->getRHS(), 6818 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE), 6819 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT), 6820 BO->getSourceRange(), BO->getOperatorLoc()); 6821 if (getInitLCDecl(BO->getRHS()) == LCDecl) 6822 return setUB(BO->getLHS(), 6823 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE), 6824 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT), 6825 BO->getSourceRange(), BO->getOperatorLoc()); 6826 } else if (IneqCondIsCanonical && BO->getOpcode() == BO_NE) 6827 return setUB( 6828 getInitLCDecl(BO->getLHS()) == LCDecl ? BO->getRHS() : BO->getLHS(), 6829 /*LessOp=*/llvm::None, 6830 /*StrictOp=*/true, BO->getSourceRange(), BO->getOperatorLoc()); 6831 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 6832 if (CE->getNumArgs() == 2) { 6833 auto Op = CE->getOperator(); 6834 switch (Op) { 6835 case OO_Greater: 6836 case OO_GreaterEqual: 6837 case OO_Less: 6838 case OO_LessEqual: 6839 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 6840 return setUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual, 6841 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(), 6842 CE->getOperatorLoc()); 6843 if (getInitLCDecl(CE->getArg(1)) == LCDecl) 6844 return setUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual, 6845 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(), 6846 CE->getOperatorLoc()); 6847 break; 6848 case OO_ExclaimEqual: 6849 if (IneqCondIsCanonical) 6850 return setUB(getInitLCDecl(CE->getArg(0)) == LCDecl ? CE->getArg(1) 6851 : CE->getArg(0), 6852 /*LessOp=*/llvm::None, 6853 /*StrictOp=*/true, CE->getSourceRange(), 6854 CE->getOperatorLoc()); 6855 break; 6856 default: 6857 break; 6858 } 6859 } 6860 } 6861 if (dependent() || SemaRef.CurContext->isDependentContext()) 6862 return false; 6863 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond) 6864 << (IneqCondIsCanonical ? 1 : 0) << S->getSourceRange() << LCDecl; 6865 return true; 6866 } 6867 6868 bool OpenMPIterationSpaceChecker::checkAndSetIncRHS(Expr *RHS) { 6869 // RHS of canonical loop form increment can be: 6870 // var + incr 6871 // incr + var 6872 // var - incr 6873 // 6874 RHS = RHS->IgnoreParenImpCasts(); 6875 if (auto *BO = dyn_cast<BinaryOperator>(RHS)) { 6876 if (BO->isAdditiveOp()) { 6877 bool IsAdd = BO->getOpcode() == BO_Add; 6878 if (getInitLCDecl(BO->getLHS()) == LCDecl) 6879 return setStep(BO->getRHS(), !IsAdd); 6880 if (IsAdd && getInitLCDecl(BO->getRHS()) == LCDecl) 6881 return setStep(BO->getLHS(), /*Subtract=*/false); 6882 } 6883 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) { 6884 bool IsAdd = CE->getOperator() == OO_Plus; 6885 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) { 6886 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 6887 return setStep(CE->getArg(1), !IsAdd); 6888 if (IsAdd && getInitLCDecl(CE->getArg(1)) == LCDecl) 6889 return setStep(CE->getArg(0), /*Subtract=*/false); 6890 } 6891 } 6892 if (dependent() || SemaRef.CurContext->isDependentContext()) 6893 return false; 6894 SemaRef.Diag(RHS->getBeginLoc(), diag::err_omp_loop_not_canonical_incr) 6895 << RHS->getSourceRange() << LCDecl; 6896 return true; 6897 } 6898 6899 bool OpenMPIterationSpaceChecker::checkAndSetInc(Expr *S) { 6900 // Check incr-expr for canonical loop form and return true if it 6901 // does not conform. 6902 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following: 6903 // ++var 6904 // var++ 6905 // --var 6906 // var-- 6907 // var += incr 6908 // var -= incr 6909 // var = var + incr 6910 // var = incr + var 6911 // var = var - incr 6912 // 6913 if (!S) { 6914 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl; 6915 return true; 6916 } 6917 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S)) 6918 if (!ExprTemp->cleanupsHaveSideEffects()) 6919 S = ExprTemp->getSubExpr(); 6920 6921 IncrementSrcRange = S->getSourceRange(); 6922 S = S->IgnoreParens(); 6923 if (auto *UO = dyn_cast<UnaryOperator>(S)) { 6924 if (UO->isIncrementDecrementOp() && 6925 getInitLCDecl(UO->getSubExpr()) == LCDecl) 6926 return setStep(SemaRef 6927 .ActOnIntegerConstant(UO->getBeginLoc(), 6928 (UO->isDecrementOp() ? -1 : 1)) 6929 .get(), 6930 /*Subtract=*/false); 6931 } else if (auto *BO = dyn_cast<BinaryOperator>(S)) { 6932 switch (BO->getOpcode()) { 6933 case BO_AddAssign: 6934 case BO_SubAssign: 6935 if (getInitLCDecl(BO->getLHS()) == LCDecl) 6936 return setStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign); 6937 break; 6938 case BO_Assign: 6939 if (getInitLCDecl(BO->getLHS()) == LCDecl) 6940 return checkAndSetIncRHS(BO->getRHS()); 6941 break; 6942 default: 6943 break; 6944 } 6945 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 6946 switch (CE->getOperator()) { 6947 case OO_PlusPlus: 6948 case OO_MinusMinus: 6949 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 6950 return setStep(SemaRef 6951 .ActOnIntegerConstant( 6952 CE->getBeginLoc(), 6953 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)) 6954 .get(), 6955 /*Subtract=*/false); 6956 break; 6957 case OO_PlusEqual: 6958 case OO_MinusEqual: 6959 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 6960 return setStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual); 6961 break; 6962 case OO_Equal: 6963 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 6964 return checkAndSetIncRHS(CE->getArg(1)); 6965 break; 6966 default: 6967 break; 6968 } 6969 } 6970 if (dependent() || SemaRef.CurContext->isDependentContext()) 6971 return false; 6972 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_incr) 6973 << S->getSourceRange() << LCDecl; 6974 return true; 6975 } 6976 6977 static ExprResult 6978 tryBuildCapture(Sema &SemaRef, Expr *Capture, 6979 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 6980 if (SemaRef.CurContext->isDependentContext() || Capture->containsErrors()) 6981 return Capture; 6982 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects)) 6983 return SemaRef.PerformImplicitConversion( 6984 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting, 6985 /*AllowExplicit=*/true); 6986 auto I = Captures.find(Capture); 6987 if (I != Captures.end()) 6988 return buildCapture(SemaRef, Capture, I->second); 6989 DeclRefExpr *Ref = nullptr; 6990 ExprResult Res = buildCapture(SemaRef, Capture, Ref); 6991 Captures[Capture] = Ref; 6992 return Res; 6993 } 6994 6995 /// Calculate number of iterations, transforming to unsigned, if number of 6996 /// iterations may be larger than the original type. 6997 static Expr * 6998 calculateNumIters(Sema &SemaRef, Scope *S, SourceLocation DefaultLoc, 6999 Expr *Lower, Expr *Upper, Expr *Step, QualType LCTy, 7000 bool TestIsStrictOp, bool RoundToStep, 7001 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 7002 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures); 7003 if (!NewStep.isUsable()) 7004 return nullptr; 7005 llvm::APSInt LRes, SRes; 7006 bool IsLowerConst = false, IsStepConst = false; 7007 if (Optional<llvm::APSInt> Res = Lower->getIntegerConstantExpr(SemaRef.Context)) { 7008 LRes = *Res; 7009 IsLowerConst = true; 7010 } 7011 if (Optional<llvm::APSInt> Res = Step->getIntegerConstantExpr(SemaRef.Context)) { 7012 SRes = *Res; 7013 IsStepConst = true; 7014 } 7015 bool NoNeedToConvert = IsLowerConst && !RoundToStep && 7016 ((!TestIsStrictOp && LRes.isNonNegative()) || 7017 (TestIsStrictOp && LRes.isStrictlyPositive())); 7018 bool NeedToReorganize = false; 7019 // Check if any subexpressions in Lower -Step [+ 1] lead to overflow. 7020 if (!NoNeedToConvert && IsLowerConst && 7021 (TestIsStrictOp || (RoundToStep && IsStepConst))) { 7022 NoNeedToConvert = true; 7023 if (RoundToStep) { 7024 unsigned BW = LRes.getBitWidth() > SRes.getBitWidth() 7025 ? LRes.getBitWidth() 7026 : SRes.getBitWidth(); 7027 LRes = LRes.extend(BW + 1); 7028 LRes.setIsSigned(true); 7029 SRes = SRes.extend(BW + 1); 7030 SRes.setIsSigned(true); 7031 LRes -= SRes; 7032 NoNeedToConvert = LRes.trunc(BW).extend(BW + 1) == LRes; 7033 LRes = LRes.trunc(BW); 7034 } 7035 if (TestIsStrictOp) { 7036 unsigned BW = LRes.getBitWidth(); 7037 LRes = LRes.extend(BW + 1); 7038 LRes.setIsSigned(true); 7039 ++LRes; 7040 NoNeedToConvert = 7041 NoNeedToConvert && LRes.trunc(BW).extend(BW + 1) == LRes; 7042 // truncate to the original bitwidth. 7043 LRes = LRes.trunc(BW); 7044 } 7045 NeedToReorganize = NoNeedToConvert; 7046 } 7047 llvm::APSInt URes; 7048 bool IsUpperConst = false; 7049 if (Optional<llvm::APSInt> Res = Upper->getIntegerConstantExpr(SemaRef.Context)) { 7050 URes = *Res; 7051 IsUpperConst = true; 7052 } 7053 if (NoNeedToConvert && IsLowerConst && IsUpperConst && 7054 (!RoundToStep || IsStepConst)) { 7055 unsigned BW = LRes.getBitWidth() > URes.getBitWidth() ? LRes.getBitWidth() 7056 : URes.getBitWidth(); 7057 LRes = LRes.extend(BW + 1); 7058 LRes.setIsSigned(true); 7059 URes = URes.extend(BW + 1); 7060 URes.setIsSigned(true); 7061 URes -= LRes; 7062 NoNeedToConvert = URes.trunc(BW).extend(BW + 1) == URes; 7063 NeedToReorganize = NoNeedToConvert; 7064 } 7065 // If the boundaries are not constant or (Lower - Step [+ 1]) is not constant 7066 // or less than zero (Upper - (Lower - Step [+ 1]) may overflow) - promote to 7067 // unsigned. 7068 if ((!NoNeedToConvert || (LRes.isNegative() && !IsUpperConst)) && 7069 !LCTy->isDependentType() && LCTy->isIntegerType()) { 7070 QualType LowerTy = Lower->getType(); 7071 QualType UpperTy = Upper->getType(); 7072 uint64_t LowerSize = SemaRef.Context.getTypeSize(LowerTy); 7073 uint64_t UpperSize = SemaRef.Context.getTypeSize(UpperTy); 7074 if ((LowerSize <= UpperSize && UpperTy->hasSignedIntegerRepresentation()) || 7075 (LowerSize > UpperSize && LowerTy->hasSignedIntegerRepresentation())) { 7076 QualType CastType = SemaRef.Context.getIntTypeForBitwidth( 7077 LowerSize > UpperSize ? LowerSize : UpperSize, /*Signed=*/0); 7078 Upper = 7079 SemaRef 7080 .PerformImplicitConversion( 7081 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Upper).get(), 7082 CastType, Sema::AA_Converting) 7083 .get(); 7084 Lower = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Lower).get(); 7085 NewStep = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, NewStep.get()); 7086 } 7087 } 7088 if (!Lower || !Upper || NewStep.isInvalid()) 7089 return nullptr; 7090 7091 ExprResult Diff; 7092 // If need to reorganize, then calculate the form as Upper - (Lower - Step [+ 7093 // 1]). 7094 if (NeedToReorganize) { 7095 Diff = Lower; 7096 7097 if (RoundToStep) { 7098 // Lower - Step 7099 Diff = 7100 SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Diff.get(), NewStep.get()); 7101 if (!Diff.isUsable()) 7102 return nullptr; 7103 } 7104 7105 // Lower - Step [+ 1] 7106 if (TestIsStrictOp) 7107 Diff = SemaRef.BuildBinOp( 7108 S, DefaultLoc, BO_Add, Diff.get(), 7109 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 7110 if (!Diff.isUsable()) 7111 return nullptr; 7112 7113 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 7114 if (!Diff.isUsable()) 7115 return nullptr; 7116 7117 // Upper - (Lower - Step [+ 1]). 7118 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Diff.get()); 7119 if (!Diff.isUsable()) 7120 return nullptr; 7121 } else { 7122 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower); 7123 7124 if (!Diff.isUsable() && LCTy->getAsCXXRecordDecl()) { 7125 // BuildBinOp already emitted error, this one is to point user to upper 7126 // and lower bound, and to tell what is passed to 'operator-'. 7127 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx) 7128 << Upper->getSourceRange() << Lower->getSourceRange(); 7129 return nullptr; 7130 } 7131 7132 if (!Diff.isUsable()) 7133 return nullptr; 7134 7135 // Upper - Lower [- 1] 7136 if (TestIsStrictOp) 7137 Diff = SemaRef.BuildBinOp( 7138 S, DefaultLoc, BO_Sub, Diff.get(), 7139 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 7140 if (!Diff.isUsable()) 7141 return nullptr; 7142 7143 if (RoundToStep) { 7144 // Upper - Lower [- 1] + Step 7145 Diff = 7146 SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get()); 7147 if (!Diff.isUsable()) 7148 return nullptr; 7149 } 7150 } 7151 7152 // Parentheses (for dumping/debugging purposes only). 7153 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 7154 if (!Diff.isUsable()) 7155 return nullptr; 7156 7157 // (Upper - Lower [- 1] + Step) / Step or (Upper - Lower) / Step 7158 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get()); 7159 if (!Diff.isUsable()) 7160 return nullptr; 7161 7162 return Diff.get(); 7163 } 7164 7165 /// Build the expression to calculate the number of iterations. 7166 Expr *OpenMPIterationSpaceChecker::buildNumIterations( 7167 Scope *S, ArrayRef<LoopIterationSpace> ResultIterSpaces, bool LimitedType, 7168 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 7169 QualType VarType = LCDecl->getType().getNonReferenceType(); 7170 if (!VarType->isIntegerType() && !VarType->isPointerType() && 7171 !SemaRef.getLangOpts().CPlusPlus) 7172 return nullptr; 7173 Expr *LBVal = LB; 7174 Expr *UBVal = UB; 7175 // LB = TestIsLessOp.getValue() ? min(LB(MinVal), LB(MaxVal)) : 7176 // max(LB(MinVal), LB(MaxVal)) 7177 if (InitDependOnLC) { 7178 const LoopIterationSpace &IS = 7179 ResultIterSpaces[ResultIterSpaces.size() - 1 - 7180 InitDependOnLC.getValueOr( 7181 CondDependOnLC.getValueOr(0))]; 7182 if (!IS.MinValue || !IS.MaxValue) 7183 return nullptr; 7184 // OuterVar = Min 7185 ExprResult MinValue = 7186 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MinValue); 7187 if (!MinValue.isUsable()) 7188 return nullptr; 7189 7190 ExprResult LBMinVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign, 7191 IS.CounterVar, MinValue.get()); 7192 if (!LBMinVal.isUsable()) 7193 return nullptr; 7194 // OuterVar = Min, LBVal 7195 LBMinVal = 7196 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, LBMinVal.get(), LBVal); 7197 if (!LBMinVal.isUsable()) 7198 return nullptr; 7199 // (OuterVar = Min, LBVal) 7200 LBMinVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, LBMinVal.get()); 7201 if (!LBMinVal.isUsable()) 7202 return nullptr; 7203 7204 // OuterVar = Max 7205 ExprResult MaxValue = 7206 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MaxValue); 7207 if (!MaxValue.isUsable()) 7208 return nullptr; 7209 7210 ExprResult LBMaxVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign, 7211 IS.CounterVar, MaxValue.get()); 7212 if (!LBMaxVal.isUsable()) 7213 return nullptr; 7214 // OuterVar = Max, LBVal 7215 LBMaxVal = 7216 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, LBMaxVal.get(), LBVal); 7217 if (!LBMaxVal.isUsable()) 7218 return nullptr; 7219 // (OuterVar = Max, LBVal) 7220 LBMaxVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, LBMaxVal.get()); 7221 if (!LBMaxVal.isUsable()) 7222 return nullptr; 7223 7224 Expr *LBMin = tryBuildCapture(SemaRef, LBMinVal.get(), Captures).get(); 7225 Expr *LBMax = tryBuildCapture(SemaRef, LBMaxVal.get(), Captures).get(); 7226 if (!LBMin || !LBMax) 7227 return nullptr; 7228 // LB(MinVal) < LB(MaxVal) 7229 ExprResult MinLessMaxRes = 7230 SemaRef.BuildBinOp(S, DefaultLoc, BO_LT, LBMin, LBMax); 7231 if (!MinLessMaxRes.isUsable()) 7232 return nullptr; 7233 Expr *MinLessMax = 7234 tryBuildCapture(SemaRef, MinLessMaxRes.get(), Captures).get(); 7235 if (!MinLessMax) 7236 return nullptr; 7237 if (TestIsLessOp.getValue()) { 7238 // LB(MinVal) < LB(MaxVal) ? LB(MinVal) : LB(MaxVal) - min(LB(MinVal), 7239 // LB(MaxVal)) 7240 ExprResult MinLB = SemaRef.ActOnConditionalOp(DefaultLoc, DefaultLoc, 7241 MinLessMax, LBMin, LBMax); 7242 if (!MinLB.isUsable()) 7243 return nullptr; 7244 LBVal = MinLB.get(); 7245 } else { 7246 // LB(MinVal) < LB(MaxVal) ? LB(MaxVal) : LB(MinVal) - max(LB(MinVal), 7247 // LB(MaxVal)) 7248 ExprResult MaxLB = SemaRef.ActOnConditionalOp(DefaultLoc, DefaultLoc, 7249 MinLessMax, LBMax, LBMin); 7250 if (!MaxLB.isUsable()) 7251 return nullptr; 7252 LBVal = MaxLB.get(); 7253 } 7254 } 7255 // UB = TestIsLessOp.getValue() ? max(UB(MinVal), UB(MaxVal)) : 7256 // min(UB(MinVal), UB(MaxVal)) 7257 if (CondDependOnLC) { 7258 const LoopIterationSpace &IS = 7259 ResultIterSpaces[ResultIterSpaces.size() - 1 - 7260 InitDependOnLC.getValueOr( 7261 CondDependOnLC.getValueOr(0))]; 7262 if (!IS.MinValue || !IS.MaxValue) 7263 return nullptr; 7264 // OuterVar = Min 7265 ExprResult MinValue = 7266 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MinValue); 7267 if (!MinValue.isUsable()) 7268 return nullptr; 7269 7270 ExprResult UBMinVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign, 7271 IS.CounterVar, MinValue.get()); 7272 if (!UBMinVal.isUsable()) 7273 return nullptr; 7274 // OuterVar = Min, UBVal 7275 UBMinVal = 7276 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, UBMinVal.get(), UBVal); 7277 if (!UBMinVal.isUsable()) 7278 return nullptr; 7279 // (OuterVar = Min, UBVal) 7280 UBMinVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, UBMinVal.get()); 7281 if (!UBMinVal.isUsable()) 7282 return nullptr; 7283 7284 // OuterVar = Max 7285 ExprResult MaxValue = 7286 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MaxValue); 7287 if (!MaxValue.isUsable()) 7288 return nullptr; 7289 7290 ExprResult UBMaxVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign, 7291 IS.CounterVar, MaxValue.get()); 7292 if (!UBMaxVal.isUsable()) 7293 return nullptr; 7294 // OuterVar = Max, UBVal 7295 UBMaxVal = 7296 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, UBMaxVal.get(), UBVal); 7297 if (!UBMaxVal.isUsable()) 7298 return nullptr; 7299 // (OuterVar = Max, UBVal) 7300 UBMaxVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, UBMaxVal.get()); 7301 if (!UBMaxVal.isUsable()) 7302 return nullptr; 7303 7304 Expr *UBMin = tryBuildCapture(SemaRef, UBMinVal.get(), Captures).get(); 7305 Expr *UBMax = tryBuildCapture(SemaRef, UBMaxVal.get(), Captures).get(); 7306 if (!UBMin || !UBMax) 7307 return nullptr; 7308 // UB(MinVal) > UB(MaxVal) 7309 ExprResult MinGreaterMaxRes = 7310 SemaRef.BuildBinOp(S, DefaultLoc, BO_GT, UBMin, UBMax); 7311 if (!MinGreaterMaxRes.isUsable()) 7312 return nullptr; 7313 Expr *MinGreaterMax = 7314 tryBuildCapture(SemaRef, MinGreaterMaxRes.get(), Captures).get(); 7315 if (!MinGreaterMax) 7316 return nullptr; 7317 if (TestIsLessOp.getValue()) { 7318 // UB(MinVal) > UB(MaxVal) ? UB(MinVal) : UB(MaxVal) - max(UB(MinVal), 7319 // UB(MaxVal)) 7320 ExprResult MaxUB = SemaRef.ActOnConditionalOp( 7321 DefaultLoc, DefaultLoc, MinGreaterMax, UBMin, UBMax); 7322 if (!MaxUB.isUsable()) 7323 return nullptr; 7324 UBVal = MaxUB.get(); 7325 } else { 7326 // UB(MinVal) > UB(MaxVal) ? UB(MaxVal) : UB(MinVal) - min(UB(MinVal), 7327 // UB(MaxVal)) 7328 ExprResult MinUB = SemaRef.ActOnConditionalOp( 7329 DefaultLoc, DefaultLoc, MinGreaterMax, UBMax, UBMin); 7330 if (!MinUB.isUsable()) 7331 return nullptr; 7332 UBVal = MinUB.get(); 7333 } 7334 } 7335 Expr *UBExpr = TestIsLessOp.getValue() ? UBVal : LBVal; 7336 Expr *LBExpr = TestIsLessOp.getValue() ? LBVal : UBVal; 7337 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get(); 7338 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get(); 7339 if (!Upper || !Lower) 7340 return nullptr; 7341 7342 ExprResult Diff = 7343 calculateNumIters(SemaRef, S, DefaultLoc, Lower, Upper, Step, VarType, 7344 TestIsStrictOp, /*RoundToStep=*/true, Captures); 7345 if (!Diff.isUsable()) 7346 return nullptr; 7347 7348 // OpenMP runtime requires 32-bit or 64-bit loop variables. 7349 QualType Type = Diff.get()->getType(); 7350 ASTContext &C = SemaRef.Context; 7351 bool UseVarType = VarType->hasIntegerRepresentation() && 7352 C.getTypeSize(Type) > C.getTypeSize(VarType); 7353 if (!Type->isIntegerType() || UseVarType) { 7354 unsigned NewSize = 7355 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type); 7356 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation() 7357 : Type->hasSignedIntegerRepresentation(); 7358 Type = C.getIntTypeForBitwidth(NewSize, IsSigned); 7359 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) { 7360 Diff = SemaRef.PerformImplicitConversion( 7361 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true); 7362 if (!Diff.isUsable()) 7363 return nullptr; 7364 } 7365 } 7366 if (LimitedType) { 7367 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32; 7368 if (NewSize != C.getTypeSize(Type)) { 7369 if (NewSize < C.getTypeSize(Type)) { 7370 assert(NewSize == 64 && "incorrect loop var size"); 7371 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var) 7372 << InitSrcRange << ConditionSrcRange; 7373 } 7374 QualType NewType = C.getIntTypeForBitwidth( 7375 NewSize, Type->hasSignedIntegerRepresentation() || 7376 C.getTypeSize(Type) < NewSize); 7377 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) { 7378 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType, 7379 Sema::AA_Converting, true); 7380 if (!Diff.isUsable()) 7381 return nullptr; 7382 } 7383 } 7384 } 7385 7386 return Diff.get(); 7387 } 7388 7389 std::pair<Expr *, Expr *> OpenMPIterationSpaceChecker::buildMinMaxValues( 7390 Scope *S, llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 7391 // Do not build for iterators, they cannot be used in non-rectangular loop 7392 // nests. 7393 if (LCDecl->getType()->isRecordType()) 7394 return std::make_pair(nullptr, nullptr); 7395 // If we subtract, the min is in the condition, otherwise the min is in the 7396 // init value. 7397 Expr *MinExpr = nullptr; 7398 Expr *MaxExpr = nullptr; 7399 Expr *LBExpr = TestIsLessOp.getValue() ? LB : UB; 7400 Expr *UBExpr = TestIsLessOp.getValue() ? UB : LB; 7401 bool LBNonRect = TestIsLessOp.getValue() ? InitDependOnLC.hasValue() 7402 : CondDependOnLC.hasValue(); 7403 bool UBNonRect = TestIsLessOp.getValue() ? CondDependOnLC.hasValue() 7404 : InitDependOnLC.hasValue(); 7405 Expr *Lower = 7406 LBNonRect ? LBExpr : tryBuildCapture(SemaRef, LBExpr, Captures).get(); 7407 Expr *Upper = 7408 UBNonRect ? UBExpr : tryBuildCapture(SemaRef, UBExpr, Captures).get(); 7409 if (!Upper || !Lower) 7410 return std::make_pair(nullptr, nullptr); 7411 7412 if (TestIsLessOp.getValue()) 7413 MinExpr = Lower; 7414 else 7415 MaxExpr = Upper; 7416 7417 // Build minimum/maximum value based on number of iterations. 7418 QualType VarType = LCDecl->getType().getNonReferenceType(); 7419 7420 ExprResult Diff = 7421 calculateNumIters(SemaRef, S, DefaultLoc, Lower, Upper, Step, VarType, 7422 TestIsStrictOp, /*RoundToStep=*/false, Captures); 7423 if (!Diff.isUsable()) 7424 return std::make_pair(nullptr, nullptr); 7425 7426 // ((Upper - Lower [- 1]) / Step) * Step 7427 // Parentheses (for dumping/debugging purposes only). 7428 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 7429 if (!Diff.isUsable()) 7430 return std::make_pair(nullptr, nullptr); 7431 7432 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures); 7433 if (!NewStep.isUsable()) 7434 return std::make_pair(nullptr, nullptr); 7435 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Mul, Diff.get(), NewStep.get()); 7436 if (!Diff.isUsable()) 7437 return std::make_pair(nullptr, nullptr); 7438 7439 // Parentheses (for dumping/debugging purposes only). 7440 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 7441 if (!Diff.isUsable()) 7442 return std::make_pair(nullptr, nullptr); 7443 7444 // Convert to the ptrdiff_t, if original type is pointer. 7445 if (VarType->isAnyPointerType() && 7446 !SemaRef.Context.hasSameType( 7447 Diff.get()->getType(), 7448 SemaRef.Context.getUnsignedPointerDiffType())) { 7449 Diff = SemaRef.PerformImplicitConversion( 7450 Diff.get(), SemaRef.Context.getUnsignedPointerDiffType(), 7451 Sema::AA_Converting, /*AllowExplicit=*/true); 7452 } 7453 if (!Diff.isUsable()) 7454 return std::make_pair(nullptr, nullptr); 7455 7456 if (TestIsLessOp.getValue()) { 7457 // MinExpr = Lower; 7458 // MaxExpr = Lower + (((Upper - Lower [- 1]) / Step) * Step) 7459 Diff = SemaRef.BuildBinOp( 7460 S, DefaultLoc, BO_Add, 7461 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Lower).get(), 7462 Diff.get()); 7463 if (!Diff.isUsable()) 7464 return std::make_pair(nullptr, nullptr); 7465 } else { 7466 // MaxExpr = Upper; 7467 // MinExpr = Upper - (((Upper - Lower [- 1]) / Step) * Step) 7468 Diff = SemaRef.BuildBinOp( 7469 S, DefaultLoc, BO_Sub, 7470 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Upper).get(), 7471 Diff.get()); 7472 if (!Diff.isUsable()) 7473 return std::make_pair(nullptr, nullptr); 7474 } 7475 7476 // Convert to the original type. 7477 if (SemaRef.Context.hasSameType(Diff.get()->getType(), VarType)) 7478 Diff = SemaRef.PerformImplicitConversion(Diff.get(), VarType, 7479 Sema::AA_Converting, 7480 /*AllowExplicit=*/true); 7481 if (!Diff.isUsable()) 7482 return std::make_pair(nullptr, nullptr); 7483 7484 Diff = SemaRef.ActOnFinishFullExpr(Diff.get(), /*DiscardedValue=*/false); 7485 if (!Diff.isUsable()) 7486 return std::make_pair(nullptr, nullptr); 7487 7488 if (TestIsLessOp.getValue()) 7489 MaxExpr = Diff.get(); 7490 else 7491 MinExpr = Diff.get(); 7492 7493 return std::make_pair(MinExpr, MaxExpr); 7494 } 7495 7496 Expr *OpenMPIterationSpaceChecker::buildFinalCondition(Scope *S) const { 7497 if (InitDependOnLC || CondDependOnLC) 7498 return Condition; 7499 return nullptr; 7500 } 7501 7502 Expr *OpenMPIterationSpaceChecker::buildPreCond( 7503 Scope *S, Expr *Cond, 7504 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 7505 // Do not build a precondition when the condition/initialization is dependent 7506 // to prevent pessimistic early loop exit. 7507 // TODO: this can be improved by calculating min/max values but not sure that 7508 // it will be very effective. 7509 if (CondDependOnLC || InitDependOnLC) 7510 return SemaRef.PerformImplicitConversion( 7511 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(), 7512 SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting, 7513 /*AllowExplicit=*/true).get(); 7514 7515 // Try to build LB <op> UB, where <op> is <, >, <=, or >=. 7516 Sema::TentativeAnalysisScope Trap(SemaRef); 7517 7518 ExprResult NewLB = tryBuildCapture(SemaRef, LB, Captures); 7519 ExprResult NewUB = tryBuildCapture(SemaRef, UB, Captures); 7520 if (!NewLB.isUsable() || !NewUB.isUsable()) 7521 return nullptr; 7522 7523 ExprResult CondExpr = 7524 SemaRef.BuildBinOp(S, DefaultLoc, 7525 TestIsLessOp.getValue() ? 7526 (TestIsStrictOp ? BO_LT : BO_LE) : 7527 (TestIsStrictOp ? BO_GT : BO_GE), 7528 NewLB.get(), NewUB.get()); 7529 if (CondExpr.isUsable()) { 7530 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(), 7531 SemaRef.Context.BoolTy)) 7532 CondExpr = SemaRef.PerformImplicitConversion( 7533 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting, 7534 /*AllowExplicit=*/true); 7535 } 7536 7537 // Otherwise use original loop condition and evaluate it in runtime. 7538 return CondExpr.isUsable() ? CondExpr.get() : Cond; 7539 } 7540 7541 /// Build reference expression to the counter be used for codegen. 7542 DeclRefExpr *OpenMPIterationSpaceChecker::buildCounterVar( 7543 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 7544 DSAStackTy &DSA) const { 7545 auto *VD = dyn_cast<VarDecl>(LCDecl); 7546 if (!VD) { 7547 VD = SemaRef.isOpenMPCapturedDecl(LCDecl); 7548 DeclRefExpr *Ref = buildDeclRefExpr( 7549 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc); 7550 const DSAStackTy::DSAVarData Data = 7551 DSA.getTopDSA(LCDecl, /*FromParent=*/false); 7552 // If the loop control decl is explicitly marked as private, do not mark it 7553 // as captured again. 7554 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr) 7555 Captures.insert(std::make_pair(LCRef, Ref)); 7556 return Ref; 7557 } 7558 return cast<DeclRefExpr>(LCRef); 7559 } 7560 7561 Expr *OpenMPIterationSpaceChecker::buildPrivateCounterVar() const { 7562 if (LCDecl && !LCDecl->isInvalidDecl()) { 7563 QualType Type = LCDecl->getType().getNonReferenceType(); 7564 VarDecl *PrivateVar = buildVarDecl( 7565 SemaRef, DefaultLoc, Type, LCDecl->getName(), 7566 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr, 7567 isa<VarDecl>(LCDecl) 7568 ? buildDeclRefExpr(SemaRef, cast<VarDecl>(LCDecl), Type, DefaultLoc) 7569 : nullptr); 7570 if (PrivateVar->isInvalidDecl()) 7571 return nullptr; 7572 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc); 7573 } 7574 return nullptr; 7575 } 7576 7577 /// Build initialization of the counter to be used for codegen. 7578 Expr *OpenMPIterationSpaceChecker::buildCounterInit() const { return LB; } 7579 7580 /// Build step of the counter be used for codegen. 7581 Expr *OpenMPIterationSpaceChecker::buildCounterStep() const { return Step; } 7582 7583 Expr *OpenMPIterationSpaceChecker::buildOrderedLoopData( 7584 Scope *S, Expr *Counter, 7585 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, SourceLocation Loc, 7586 Expr *Inc, OverloadedOperatorKind OOK) { 7587 Expr *Cnt = SemaRef.DefaultLvalueConversion(Counter).get(); 7588 if (!Cnt) 7589 return nullptr; 7590 if (Inc) { 7591 assert((OOK == OO_Plus || OOK == OO_Minus) && 7592 "Expected only + or - operations for depend clauses."); 7593 BinaryOperatorKind BOK = (OOK == OO_Plus) ? BO_Add : BO_Sub; 7594 Cnt = SemaRef.BuildBinOp(S, Loc, BOK, Cnt, Inc).get(); 7595 if (!Cnt) 7596 return nullptr; 7597 } 7598 QualType VarType = LCDecl->getType().getNonReferenceType(); 7599 if (!VarType->isIntegerType() && !VarType->isPointerType() && 7600 !SemaRef.getLangOpts().CPlusPlus) 7601 return nullptr; 7602 // Upper - Lower 7603 Expr *Upper = TestIsLessOp.getValue() 7604 ? Cnt 7605 : tryBuildCapture(SemaRef, LB, Captures).get(); 7606 Expr *Lower = TestIsLessOp.getValue() 7607 ? tryBuildCapture(SemaRef, LB, Captures).get() 7608 : Cnt; 7609 if (!Upper || !Lower) 7610 return nullptr; 7611 7612 ExprResult Diff = calculateNumIters(SemaRef, S, DefaultLoc, Lower, Upper, 7613 Step, VarType, /*TestIsStrictOp=*/false, 7614 /*RoundToStep=*/false, Captures); 7615 if (!Diff.isUsable()) 7616 return nullptr; 7617 7618 return Diff.get(); 7619 } 7620 } // namespace 7621 7622 void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) { 7623 assert(getLangOpts().OpenMP && "OpenMP is not active."); 7624 assert(Init && "Expected loop in canonical form."); 7625 unsigned AssociatedLoops = DSAStack->getAssociatedLoops(); 7626 if (AssociatedLoops > 0 && 7627 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { 7628 DSAStack->loopStart(); 7629 OpenMPIterationSpaceChecker ISC(*this, *DSAStack, ForLoc); 7630 if (!ISC.checkAndSetInit(Init, /*EmitDiags=*/false)) { 7631 if (ValueDecl *D = ISC.getLoopDecl()) { 7632 auto *VD = dyn_cast<VarDecl>(D); 7633 DeclRefExpr *PrivateRef = nullptr; 7634 if (!VD) { 7635 if (VarDecl *Private = isOpenMPCapturedDecl(D)) { 7636 VD = Private; 7637 } else { 7638 PrivateRef = buildCapture(*this, D, ISC.getLoopDeclRefExpr(), 7639 /*WithInit=*/false); 7640 VD = cast<VarDecl>(PrivateRef->getDecl()); 7641 } 7642 } 7643 DSAStack->addLoopControlVariable(D, VD); 7644 const Decl *LD = DSAStack->getPossiblyLoopCunter(); 7645 if (LD != D->getCanonicalDecl()) { 7646 DSAStack->resetPossibleLoopCounter(); 7647 if (auto *Var = dyn_cast_or_null<VarDecl>(LD)) 7648 MarkDeclarationsReferencedInExpr( 7649 buildDeclRefExpr(*this, const_cast<VarDecl *>(Var), 7650 Var->getType().getNonLValueExprType(Context), 7651 ForLoc, /*RefersToCapture=*/true)); 7652 } 7653 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 7654 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables 7655 // Referenced in a Construct, C/C++]. The loop iteration variable in the 7656 // associated for-loop of a simd construct with just one associated 7657 // for-loop may be listed in a linear clause with a constant-linear-step 7658 // that is the increment of the associated for-loop. The loop iteration 7659 // variable(s) in the associated for-loop(s) of a for or parallel for 7660 // construct may be listed in a private or lastprivate clause. 7661 DSAStackTy::DSAVarData DVar = 7662 DSAStack->getTopDSA(D, /*FromParent=*/false); 7663 // If LoopVarRefExpr is nullptr it means the corresponding loop variable 7664 // is declared in the loop and it is predetermined as a private. 7665 Expr *LoopDeclRefExpr = ISC.getLoopDeclRefExpr(); 7666 OpenMPClauseKind PredeterminedCKind = 7667 isOpenMPSimdDirective(DKind) 7668 ? (DSAStack->hasMutipleLoops() ? OMPC_lastprivate : OMPC_linear) 7669 : OMPC_private; 7670 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && 7671 DVar.CKind != PredeterminedCKind && DVar.RefExpr && 7672 (LangOpts.OpenMP <= 45 || (DVar.CKind != OMPC_lastprivate && 7673 DVar.CKind != OMPC_private))) || 7674 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop || 7675 DKind == OMPD_master_taskloop || 7676 DKind == OMPD_parallel_master_taskloop || 7677 isOpenMPDistributeDirective(DKind)) && 7678 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && 7679 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) && 7680 (DVar.CKind != OMPC_private || DVar.RefExpr)) { 7681 Diag(Init->getBeginLoc(), diag::err_omp_loop_var_dsa) 7682 << getOpenMPClauseName(DVar.CKind) 7683 << getOpenMPDirectiveName(DKind) 7684 << getOpenMPClauseName(PredeterminedCKind); 7685 if (DVar.RefExpr == nullptr) 7686 DVar.CKind = PredeterminedCKind; 7687 reportOriginalDsa(*this, DSAStack, D, DVar, 7688 /*IsLoopIterVar=*/true); 7689 } else if (LoopDeclRefExpr) { 7690 // Make the loop iteration variable private (for worksharing 7691 // constructs), linear (for simd directives with the only one 7692 // associated loop) or lastprivate (for simd directives with several 7693 // collapsed or ordered loops). 7694 if (DVar.CKind == OMPC_unknown) 7695 DSAStack->addDSA(D, LoopDeclRefExpr, PredeterminedCKind, 7696 PrivateRef); 7697 } 7698 } 7699 } 7700 DSAStack->setAssociatedLoops(AssociatedLoops - 1); 7701 } 7702 } 7703 7704 /// Called on a for stmt to check and extract its iteration space 7705 /// for further processing (such as collapsing). 7706 static bool checkOpenMPIterationSpace( 7707 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA, 7708 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount, 7709 unsigned TotalNestedLoopCount, Expr *CollapseLoopCountExpr, 7710 Expr *OrderedLoopCountExpr, 7711 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA, 7712 llvm::MutableArrayRef<LoopIterationSpace> ResultIterSpaces, 7713 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 7714 // OpenMP [2.9.1, Canonical Loop Form] 7715 // for (init-expr; test-expr; incr-expr) structured-block 7716 // for (range-decl: range-expr) structured-block 7717 auto *For = dyn_cast_or_null<ForStmt>(S); 7718 auto *CXXFor = dyn_cast_or_null<CXXForRangeStmt>(S); 7719 // Ranged for is supported only in OpenMP 5.0. 7720 if (!For && (SemaRef.LangOpts.OpenMP <= 45 || !CXXFor)) { 7721 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_not_for) 7722 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr) 7723 << getOpenMPDirectiveName(DKind) << TotalNestedLoopCount 7724 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount; 7725 if (TotalNestedLoopCount > 1) { 7726 if (CollapseLoopCountExpr && OrderedLoopCountExpr) 7727 SemaRef.Diag(DSA.getConstructLoc(), 7728 diag::note_omp_collapse_ordered_expr) 7729 << 2 << CollapseLoopCountExpr->getSourceRange() 7730 << OrderedLoopCountExpr->getSourceRange(); 7731 else if (CollapseLoopCountExpr) 7732 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), 7733 diag::note_omp_collapse_ordered_expr) 7734 << 0 << CollapseLoopCountExpr->getSourceRange(); 7735 else 7736 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), 7737 diag::note_omp_collapse_ordered_expr) 7738 << 1 << OrderedLoopCountExpr->getSourceRange(); 7739 } 7740 return true; 7741 } 7742 assert(((For && For->getBody()) || (CXXFor && CXXFor->getBody())) && 7743 "No loop body."); 7744 7745 OpenMPIterationSpaceChecker ISC(SemaRef, DSA, 7746 For ? For->getForLoc() : CXXFor->getForLoc()); 7747 7748 // Check init. 7749 Stmt *Init = For ? For->getInit() : CXXFor->getBeginStmt(); 7750 if (ISC.checkAndSetInit(Init)) 7751 return true; 7752 7753 bool HasErrors = false; 7754 7755 // Check loop variable's type. 7756 if (ValueDecl *LCDecl = ISC.getLoopDecl()) { 7757 // OpenMP [2.6, Canonical Loop Form] 7758 // Var is one of the following: 7759 // A variable of signed or unsigned integer type. 7760 // For C++, a variable of a random access iterator type. 7761 // For C, a variable of a pointer type. 7762 QualType VarType = LCDecl->getType().getNonReferenceType(); 7763 if (!VarType->isDependentType() && !VarType->isIntegerType() && 7764 !VarType->isPointerType() && 7765 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) { 7766 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_variable_type) 7767 << SemaRef.getLangOpts().CPlusPlus; 7768 HasErrors = true; 7769 } 7770 7771 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in 7772 // a Construct 7773 // The loop iteration variable(s) in the associated for-loop(s) of a for or 7774 // parallel for construct is (are) private. 7775 // The loop iteration variable in the associated for-loop of a simd 7776 // construct with just one associated for-loop is linear with a 7777 // constant-linear-step that is the increment of the associated for-loop. 7778 // Exclude loop var from the list of variables with implicitly defined data 7779 // sharing attributes. 7780 VarsWithImplicitDSA.erase(LCDecl); 7781 7782 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars"); 7783 7784 // Check test-expr. 7785 HasErrors |= ISC.checkAndSetCond(For ? For->getCond() : CXXFor->getCond()); 7786 7787 // Check incr-expr. 7788 HasErrors |= ISC.checkAndSetInc(For ? For->getInc() : CXXFor->getInc()); 7789 } 7790 7791 if (ISC.dependent() || SemaRef.CurContext->isDependentContext() || HasErrors) 7792 return HasErrors; 7793 7794 // Build the loop's iteration space representation. 7795 ResultIterSpaces[CurrentNestedLoopCount].PreCond = ISC.buildPreCond( 7796 DSA.getCurScope(), For ? For->getCond() : CXXFor->getCond(), Captures); 7797 ResultIterSpaces[CurrentNestedLoopCount].NumIterations = 7798 ISC.buildNumIterations(DSA.getCurScope(), ResultIterSpaces, 7799 (isOpenMPWorksharingDirective(DKind) || 7800 isOpenMPTaskLoopDirective(DKind) || 7801 isOpenMPDistributeDirective(DKind)), 7802 Captures); 7803 ResultIterSpaces[CurrentNestedLoopCount].CounterVar = 7804 ISC.buildCounterVar(Captures, DSA); 7805 ResultIterSpaces[CurrentNestedLoopCount].PrivateCounterVar = 7806 ISC.buildPrivateCounterVar(); 7807 ResultIterSpaces[CurrentNestedLoopCount].CounterInit = ISC.buildCounterInit(); 7808 ResultIterSpaces[CurrentNestedLoopCount].CounterStep = ISC.buildCounterStep(); 7809 ResultIterSpaces[CurrentNestedLoopCount].InitSrcRange = ISC.getInitSrcRange(); 7810 ResultIterSpaces[CurrentNestedLoopCount].CondSrcRange = 7811 ISC.getConditionSrcRange(); 7812 ResultIterSpaces[CurrentNestedLoopCount].IncSrcRange = 7813 ISC.getIncrementSrcRange(); 7814 ResultIterSpaces[CurrentNestedLoopCount].Subtract = ISC.shouldSubtractStep(); 7815 ResultIterSpaces[CurrentNestedLoopCount].IsStrictCompare = 7816 ISC.isStrictTestOp(); 7817 std::tie(ResultIterSpaces[CurrentNestedLoopCount].MinValue, 7818 ResultIterSpaces[CurrentNestedLoopCount].MaxValue) = 7819 ISC.buildMinMaxValues(DSA.getCurScope(), Captures); 7820 ResultIterSpaces[CurrentNestedLoopCount].FinalCondition = 7821 ISC.buildFinalCondition(DSA.getCurScope()); 7822 ResultIterSpaces[CurrentNestedLoopCount].IsNonRectangularLB = 7823 ISC.doesInitDependOnLC(); 7824 ResultIterSpaces[CurrentNestedLoopCount].IsNonRectangularUB = 7825 ISC.doesCondDependOnLC(); 7826 ResultIterSpaces[CurrentNestedLoopCount].LoopDependentIdx = 7827 ISC.getLoopDependentIdx(); 7828 7829 HasErrors |= 7830 (ResultIterSpaces[CurrentNestedLoopCount].PreCond == nullptr || 7831 ResultIterSpaces[CurrentNestedLoopCount].NumIterations == nullptr || 7832 ResultIterSpaces[CurrentNestedLoopCount].CounterVar == nullptr || 7833 ResultIterSpaces[CurrentNestedLoopCount].PrivateCounterVar == nullptr || 7834 ResultIterSpaces[CurrentNestedLoopCount].CounterInit == nullptr || 7835 ResultIterSpaces[CurrentNestedLoopCount].CounterStep == nullptr); 7836 if (!HasErrors && DSA.isOrderedRegion()) { 7837 if (DSA.getOrderedRegionParam().second->getNumForLoops()) { 7838 if (CurrentNestedLoopCount < 7839 DSA.getOrderedRegionParam().second->getLoopNumIterations().size()) { 7840 DSA.getOrderedRegionParam().second->setLoopNumIterations( 7841 CurrentNestedLoopCount, 7842 ResultIterSpaces[CurrentNestedLoopCount].NumIterations); 7843 DSA.getOrderedRegionParam().second->setLoopCounter( 7844 CurrentNestedLoopCount, 7845 ResultIterSpaces[CurrentNestedLoopCount].CounterVar); 7846 } 7847 } 7848 for (auto &Pair : DSA.getDoacrossDependClauses()) { 7849 if (CurrentNestedLoopCount >= Pair.first->getNumLoops()) { 7850 // Erroneous case - clause has some problems. 7851 continue; 7852 } 7853 if (Pair.first->getDependencyKind() == OMPC_DEPEND_sink && 7854 Pair.second.size() <= CurrentNestedLoopCount) { 7855 // Erroneous case - clause has some problems. 7856 Pair.first->setLoopData(CurrentNestedLoopCount, nullptr); 7857 continue; 7858 } 7859 Expr *CntValue; 7860 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source) 7861 CntValue = ISC.buildOrderedLoopData( 7862 DSA.getCurScope(), 7863 ResultIterSpaces[CurrentNestedLoopCount].CounterVar, Captures, 7864 Pair.first->getDependencyLoc()); 7865 else 7866 CntValue = ISC.buildOrderedLoopData( 7867 DSA.getCurScope(), 7868 ResultIterSpaces[CurrentNestedLoopCount].CounterVar, Captures, 7869 Pair.first->getDependencyLoc(), 7870 Pair.second[CurrentNestedLoopCount].first, 7871 Pair.second[CurrentNestedLoopCount].second); 7872 Pair.first->setLoopData(CurrentNestedLoopCount, CntValue); 7873 } 7874 } 7875 7876 return HasErrors; 7877 } 7878 7879 /// Build 'VarRef = Start. 7880 static ExprResult 7881 buildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef, 7882 ExprResult Start, bool IsNonRectangularLB, 7883 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 7884 // Build 'VarRef = Start. 7885 ExprResult NewStart = IsNonRectangularLB 7886 ? Start.get() 7887 : tryBuildCapture(SemaRef, Start.get(), Captures); 7888 if (!NewStart.isUsable()) 7889 return ExprError(); 7890 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(), 7891 VarRef.get()->getType())) { 7892 NewStart = SemaRef.PerformImplicitConversion( 7893 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting, 7894 /*AllowExplicit=*/true); 7895 if (!NewStart.isUsable()) 7896 return ExprError(); 7897 } 7898 7899 ExprResult Init = 7900 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); 7901 return Init; 7902 } 7903 7904 /// Build 'VarRef = Start + Iter * Step'. 7905 static ExprResult buildCounterUpdate( 7906 Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef, 7907 ExprResult Start, ExprResult Iter, ExprResult Step, bool Subtract, 7908 bool IsNonRectangularLB, 7909 llvm::MapVector<const Expr *, DeclRefExpr *> *Captures = nullptr) { 7910 // Add parentheses (for debugging purposes only). 7911 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get()); 7912 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() || 7913 !Step.isUsable()) 7914 return ExprError(); 7915 7916 ExprResult NewStep = Step; 7917 if (Captures) 7918 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures); 7919 if (NewStep.isInvalid()) 7920 return ExprError(); 7921 ExprResult Update = 7922 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get()); 7923 if (!Update.isUsable()) 7924 return ExprError(); 7925 7926 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or 7927 // 'VarRef = Start (+|-) Iter * Step'. 7928 if (!Start.isUsable()) 7929 return ExprError(); 7930 ExprResult NewStart = SemaRef.ActOnParenExpr(Loc, Loc, Start.get()); 7931 if (!NewStart.isUsable()) 7932 return ExprError(); 7933 if (Captures && !IsNonRectangularLB) 7934 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures); 7935 if (NewStart.isInvalid()) 7936 return ExprError(); 7937 7938 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'. 7939 ExprResult SavedUpdate = Update; 7940 ExprResult UpdateVal; 7941 if (VarRef.get()->getType()->isOverloadableType() || 7942 NewStart.get()->getType()->isOverloadableType() || 7943 Update.get()->getType()->isOverloadableType()) { 7944 Sema::TentativeAnalysisScope Trap(SemaRef); 7945 7946 Update = 7947 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); 7948 if (Update.isUsable()) { 7949 UpdateVal = 7950 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign, 7951 VarRef.get(), SavedUpdate.get()); 7952 if (UpdateVal.isUsable()) { 7953 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(), 7954 UpdateVal.get()); 7955 } 7956 } 7957 } 7958 7959 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'. 7960 if (!Update.isUsable() || !UpdateVal.isUsable()) { 7961 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add, 7962 NewStart.get(), SavedUpdate.get()); 7963 if (!Update.isUsable()) 7964 return ExprError(); 7965 7966 if (!SemaRef.Context.hasSameType(Update.get()->getType(), 7967 VarRef.get()->getType())) { 7968 Update = SemaRef.PerformImplicitConversion( 7969 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true); 7970 if (!Update.isUsable()) 7971 return ExprError(); 7972 } 7973 7974 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get()); 7975 } 7976 return Update; 7977 } 7978 7979 /// Convert integer expression \a E to make it have at least \a Bits 7980 /// bits. 7981 static ExprResult widenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) { 7982 if (E == nullptr) 7983 return ExprError(); 7984 ASTContext &C = SemaRef.Context; 7985 QualType OldType = E->getType(); 7986 unsigned HasBits = C.getTypeSize(OldType); 7987 if (HasBits >= Bits) 7988 return ExprResult(E); 7989 // OK to convert to signed, because new type has more bits than old. 7990 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true); 7991 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting, 7992 true); 7993 } 7994 7995 /// Check if the given expression \a E is a constant integer that fits 7996 /// into \a Bits bits. 7997 static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) { 7998 if (E == nullptr) 7999 return false; 8000 if (Optional<llvm::APSInt> Result = 8001 E->getIntegerConstantExpr(SemaRef.Context)) 8002 return Signed ? Result->isSignedIntN(Bits) : Result->isIntN(Bits); 8003 return false; 8004 } 8005 8006 /// Build preinits statement for the given declarations. 8007 static Stmt *buildPreInits(ASTContext &Context, 8008 MutableArrayRef<Decl *> PreInits) { 8009 if (!PreInits.empty()) { 8010 return new (Context) DeclStmt( 8011 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()), 8012 SourceLocation(), SourceLocation()); 8013 } 8014 return nullptr; 8015 } 8016 8017 /// Build preinits statement for the given declarations. 8018 static Stmt * 8019 buildPreInits(ASTContext &Context, 8020 const llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 8021 if (!Captures.empty()) { 8022 SmallVector<Decl *, 16> PreInits; 8023 for (const auto &Pair : Captures) 8024 PreInits.push_back(Pair.second->getDecl()); 8025 return buildPreInits(Context, PreInits); 8026 } 8027 return nullptr; 8028 } 8029 8030 /// Build postupdate expression for the given list of postupdates expressions. 8031 static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) { 8032 Expr *PostUpdate = nullptr; 8033 if (!PostUpdates.empty()) { 8034 for (Expr *E : PostUpdates) { 8035 Expr *ConvE = S.BuildCStyleCastExpr( 8036 E->getExprLoc(), 8037 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy), 8038 E->getExprLoc(), E) 8039 .get(); 8040 PostUpdate = PostUpdate 8041 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma, 8042 PostUpdate, ConvE) 8043 .get() 8044 : ConvE; 8045 } 8046 } 8047 return PostUpdate; 8048 } 8049 8050 /// Called on a for stmt to check itself and nested loops (if any). 8051 /// \return Returns 0 if one of the collapsed stmts is not canonical for loop, 8052 /// number of collapsed loops otherwise. 8053 static unsigned 8054 checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr, 8055 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef, 8056 DSAStackTy &DSA, 8057 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA, 8058 OMPLoopDirective::HelperExprs &Built) { 8059 unsigned NestedLoopCount = 1; 8060 if (CollapseLoopCountExpr) { 8061 // Found 'collapse' clause - calculate collapse number. 8062 Expr::EvalResult Result; 8063 if (!CollapseLoopCountExpr->isValueDependent() && 8064 CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) { 8065 NestedLoopCount = Result.Val.getInt().getLimitedValue(); 8066 } else { 8067 Built.clear(/*Size=*/1); 8068 return 1; 8069 } 8070 } 8071 unsigned OrderedLoopCount = 1; 8072 if (OrderedLoopCountExpr) { 8073 // Found 'ordered' clause - calculate collapse number. 8074 Expr::EvalResult EVResult; 8075 if (!OrderedLoopCountExpr->isValueDependent() && 8076 OrderedLoopCountExpr->EvaluateAsInt(EVResult, 8077 SemaRef.getASTContext())) { 8078 llvm::APSInt Result = EVResult.Val.getInt(); 8079 if (Result.getLimitedValue() < NestedLoopCount) { 8080 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), 8081 diag::err_omp_wrong_ordered_loop_count) 8082 << OrderedLoopCountExpr->getSourceRange(); 8083 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), 8084 diag::note_collapse_loop_count) 8085 << CollapseLoopCountExpr->getSourceRange(); 8086 } 8087 OrderedLoopCount = Result.getLimitedValue(); 8088 } else { 8089 Built.clear(/*Size=*/1); 8090 return 1; 8091 } 8092 } 8093 // This is helper routine for loop directives (e.g., 'for', 'simd', 8094 // 'for simd', etc.). 8095 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 8096 SmallVector<LoopIterationSpace, 4> IterSpaces( 8097 std::max(OrderedLoopCount, NestedLoopCount)); 8098 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true); 8099 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) { 8100 if (checkOpenMPIterationSpace( 8101 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount, 8102 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr, 8103 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces, Captures)) 8104 return 0; 8105 // Move on to the next nested for loop, or to the loop body. 8106 // OpenMP [2.8.1, simd construct, Restrictions] 8107 // All loops associated with the construct must be perfectly nested; that 8108 // is, there must be no intervening code nor any OpenMP directive between 8109 // any two loops. 8110 if (auto *For = dyn_cast<ForStmt>(CurStmt)) { 8111 CurStmt = For->getBody(); 8112 } else { 8113 assert(isa<CXXForRangeStmt>(CurStmt) && 8114 "Expected canonical for or range-based for loops."); 8115 CurStmt = cast<CXXForRangeStmt>(CurStmt)->getBody(); 8116 } 8117 CurStmt = OMPLoopDirective::tryToFindNextInnerLoop( 8118 CurStmt, SemaRef.LangOpts.OpenMP >= 50); 8119 } 8120 for (unsigned Cnt = NestedLoopCount; Cnt < OrderedLoopCount; ++Cnt) { 8121 if (checkOpenMPIterationSpace( 8122 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount, 8123 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr, 8124 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces, Captures)) 8125 return 0; 8126 if (Cnt > 0 && IterSpaces[Cnt].CounterVar) { 8127 // Handle initialization of captured loop iterator variables. 8128 auto *DRE = cast<DeclRefExpr>(IterSpaces[Cnt].CounterVar); 8129 if (isa<OMPCapturedExprDecl>(DRE->getDecl())) { 8130 Captures[DRE] = DRE; 8131 } 8132 } 8133 // Move on to the next nested for loop, or to the loop body. 8134 // OpenMP [2.8.1, simd construct, Restrictions] 8135 // All loops associated with the construct must be perfectly nested; that 8136 // is, there must be no intervening code nor any OpenMP directive between 8137 // any two loops. 8138 if (auto *For = dyn_cast<ForStmt>(CurStmt)) { 8139 CurStmt = For->getBody(); 8140 } else { 8141 assert(isa<CXXForRangeStmt>(CurStmt) && 8142 "Expected canonical for or range-based for loops."); 8143 CurStmt = cast<CXXForRangeStmt>(CurStmt)->getBody(); 8144 } 8145 CurStmt = OMPLoopDirective::tryToFindNextInnerLoop( 8146 CurStmt, SemaRef.LangOpts.OpenMP >= 50); 8147 } 8148 8149 Built.clear(/* size */ NestedLoopCount); 8150 8151 if (SemaRef.CurContext->isDependentContext()) 8152 return NestedLoopCount; 8153 8154 // An example of what is generated for the following code: 8155 // 8156 // #pragma omp simd collapse(2) ordered(2) 8157 // for (i = 0; i < NI; ++i) 8158 // for (k = 0; k < NK; ++k) 8159 // for (j = J0; j < NJ; j+=2) { 8160 // <loop body> 8161 // } 8162 // 8163 // We generate the code below. 8164 // Note: the loop body may be outlined in CodeGen. 8165 // Note: some counters may be C++ classes, operator- is used to find number of 8166 // iterations and operator+= to calculate counter value. 8167 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32 8168 // or i64 is currently supported). 8169 // 8170 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2)) 8171 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) { 8172 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2); 8173 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2; 8174 // // similar updates for vars in clauses (e.g. 'linear') 8175 // <loop body (using local i and j)> 8176 // } 8177 // i = NI; // assign final values of counters 8178 // j = NJ; 8179 // 8180 8181 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are 8182 // the iteration counts of the collapsed for loops. 8183 // Precondition tests if there is at least one iteration (all conditions are 8184 // true). 8185 auto PreCond = ExprResult(IterSpaces[0].PreCond); 8186 Expr *N0 = IterSpaces[0].NumIterations; 8187 ExprResult LastIteration32 = 8188 widenIterationCount(/*Bits=*/32, 8189 SemaRef 8190 .PerformImplicitConversion( 8191 N0->IgnoreImpCasts(), N0->getType(), 8192 Sema::AA_Converting, /*AllowExplicit=*/true) 8193 .get(), 8194 SemaRef); 8195 ExprResult LastIteration64 = widenIterationCount( 8196 /*Bits=*/64, 8197 SemaRef 8198 .PerformImplicitConversion(N0->IgnoreImpCasts(), N0->getType(), 8199 Sema::AA_Converting, 8200 /*AllowExplicit=*/true) 8201 .get(), 8202 SemaRef); 8203 8204 if (!LastIteration32.isUsable() || !LastIteration64.isUsable()) 8205 return NestedLoopCount; 8206 8207 ASTContext &C = SemaRef.Context; 8208 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32; 8209 8210 Scope *CurScope = DSA.getCurScope(); 8211 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) { 8212 if (PreCond.isUsable()) { 8213 PreCond = 8214 SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd, 8215 PreCond.get(), IterSpaces[Cnt].PreCond); 8216 } 8217 Expr *N = IterSpaces[Cnt].NumIterations; 8218 SourceLocation Loc = N->getExprLoc(); 8219 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32; 8220 if (LastIteration32.isUsable()) 8221 LastIteration32 = SemaRef.BuildBinOp( 8222 CurScope, Loc, BO_Mul, LastIteration32.get(), 8223 SemaRef 8224 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), 8225 Sema::AA_Converting, 8226 /*AllowExplicit=*/true) 8227 .get()); 8228 if (LastIteration64.isUsable()) 8229 LastIteration64 = SemaRef.BuildBinOp( 8230 CurScope, Loc, BO_Mul, LastIteration64.get(), 8231 SemaRef 8232 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), 8233 Sema::AA_Converting, 8234 /*AllowExplicit=*/true) 8235 .get()); 8236 } 8237 8238 // Choose either the 32-bit or 64-bit version. 8239 ExprResult LastIteration = LastIteration64; 8240 if (SemaRef.getLangOpts().OpenMPOptimisticCollapse || 8241 (LastIteration32.isUsable() && 8242 C.getTypeSize(LastIteration32.get()->getType()) == 32 && 8243 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 || 8244 fitsInto( 8245 /*Bits=*/32, 8246 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(), 8247 LastIteration64.get(), SemaRef)))) 8248 LastIteration = LastIteration32; 8249 QualType VType = LastIteration.get()->getType(); 8250 QualType RealVType = VType; 8251 QualType StrideVType = VType; 8252 if (isOpenMPTaskLoopDirective(DKind)) { 8253 VType = 8254 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0); 8255 StrideVType = 8256 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1); 8257 } 8258 8259 if (!LastIteration.isUsable()) 8260 return 0; 8261 8262 // Save the number of iterations. 8263 ExprResult NumIterations = LastIteration; 8264 { 8265 LastIteration = SemaRef.BuildBinOp( 8266 CurScope, LastIteration.get()->getExprLoc(), BO_Sub, 8267 LastIteration.get(), 8268 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 8269 if (!LastIteration.isUsable()) 8270 return 0; 8271 } 8272 8273 // Calculate the last iteration number beforehand instead of doing this on 8274 // each iteration. Do not do this if the number of iterations may be kfold-ed. 8275 bool IsConstant = LastIteration.get()->isIntegerConstantExpr(SemaRef.Context); 8276 ExprResult CalcLastIteration; 8277 if (!IsConstant) { 8278 ExprResult SaveRef = 8279 tryBuildCapture(SemaRef, LastIteration.get(), Captures); 8280 LastIteration = SaveRef; 8281 8282 // Prepare SaveRef + 1. 8283 NumIterations = SemaRef.BuildBinOp( 8284 CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(), 8285 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 8286 if (!NumIterations.isUsable()) 8287 return 0; 8288 } 8289 8290 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin(); 8291 8292 // Build variables passed into runtime, necessary for worksharing directives. 8293 ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB; 8294 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || 8295 isOpenMPDistributeDirective(DKind)) { 8296 // Lower bound variable, initialized with zero. 8297 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb"); 8298 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc); 8299 SemaRef.AddInitializerToDecl(LBDecl, 8300 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 8301 /*DirectInit*/ false); 8302 8303 // Upper bound variable, initialized with last iteration number. 8304 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub"); 8305 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc); 8306 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(), 8307 /*DirectInit*/ false); 8308 8309 // A 32-bit variable-flag where runtime returns 1 for the last iteration. 8310 // This will be used to implement clause 'lastprivate'. 8311 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true); 8312 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last"); 8313 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc); 8314 SemaRef.AddInitializerToDecl(ILDecl, 8315 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 8316 /*DirectInit*/ false); 8317 8318 // Stride variable returned by runtime (we initialize it to 1 by default). 8319 VarDecl *STDecl = 8320 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride"); 8321 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc); 8322 SemaRef.AddInitializerToDecl(STDecl, 8323 SemaRef.ActOnIntegerConstant(InitLoc, 1).get(), 8324 /*DirectInit*/ false); 8325 8326 // Build expression: UB = min(UB, LastIteration) 8327 // It is necessary for CodeGen of directives with static scheduling. 8328 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT, 8329 UB.get(), LastIteration.get()); 8330 ExprResult CondOp = SemaRef.ActOnConditionalOp( 8331 LastIteration.get()->getExprLoc(), InitLoc, IsUBGreater.get(), 8332 LastIteration.get(), UB.get()); 8333 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(), 8334 CondOp.get()); 8335 EUB = SemaRef.ActOnFinishFullExpr(EUB.get(), /*DiscardedValue*/ false); 8336 8337 // If we have a combined directive that combines 'distribute', 'for' or 8338 // 'simd' we need to be able to access the bounds of the schedule of the 8339 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained 8340 // by scheduling 'distribute' have to be passed to the schedule of 'for'. 8341 if (isOpenMPLoopBoundSharingDirective(DKind)) { 8342 // Lower bound variable, initialized with zero. 8343 VarDecl *CombLBDecl = 8344 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb"); 8345 CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc); 8346 SemaRef.AddInitializerToDecl( 8347 CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 8348 /*DirectInit*/ false); 8349 8350 // Upper bound variable, initialized with last iteration number. 8351 VarDecl *CombUBDecl = 8352 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub"); 8353 CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc); 8354 SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(), 8355 /*DirectInit*/ false); 8356 8357 ExprResult CombIsUBGreater = SemaRef.BuildBinOp( 8358 CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get()); 8359 ExprResult CombCondOp = 8360 SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(), 8361 LastIteration.get(), CombUB.get()); 8362 CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(), 8363 CombCondOp.get()); 8364 CombEUB = 8365 SemaRef.ActOnFinishFullExpr(CombEUB.get(), /*DiscardedValue*/ false); 8366 8367 const CapturedDecl *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl(); 8368 // We expect to have at least 2 more parameters than the 'parallel' 8369 // directive does - the lower and upper bounds of the previous schedule. 8370 assert(CD->getNumParams() >= 4 && 8371 "Unexpected number of parameters in loop combined directive"); 8372 8373 // Set the proper type for the bounds given what we learned from the 8374 // enclosed loops. 8375 ImplicitParamDecl *PrevLBDecl = CD->getParam(/*PrevLB=*/2); 8376 ImplicitParamDecl *PrevUBDecl = CD->getParam(/*PrevUB=*/3); 8377 8378 // Previous lower and upper bounds are obtained from the region 8379 // parameters. 8380 PrevLB = 8381 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc); 8382 PrevUB = 8383 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc); 8384 } 8385 } 8386 8387 // Build the iteration variable and its initialization before loop. 8388 ExprResult IV; 8389 ExprResult Init, CombInit; 8390 { 8391 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv"); 8392 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc); 8393 Expr *RHS = 8394 (isOpenMPWorksharingDirective(DKind) || 8395 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)) 8396 ? LB.get() 8397 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get(); 8398 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS); 8399 Init = SemaRef.ActOnFinishFullExpr(Init.get(), /*DiscardedValue*/ false); 8400 8401 if (isOpenMPLoopBoundSharingDirective(DKind)) { 8402 Expr *CombRHS = 8403 (isOpenMPWorksharingDirective(DKind) || 8404 isOpenMPTaskLoopDirective(DKind) || 8405 isOpenMPDistributeDirective(DKind)) 8406 ? CombLB.get() 8407 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get(); 8408 CombInit = 8409 SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS); 8410 CombInit = 8411 SemaRef.ActOnFinishFullExpr(CombInit.get(), /*DiscardedValue*/ false); 8412 } 8413 } 8414 8415 bool UseStrictCompare = 8416 RealVType->hasUnsignedIntegerRepresentation() && 8417 llvm::all_of(IterSpaces, [](const LoopIterationSpace &LIS) { 8418 return LIS.IsStrictCompare; 8419 }); 8420 // Loop condition (IV < NumIterations) or (IV <= UB or IV < UB + 1 (for 8421 // unsigned IV)) for worksharing loops. 8422 SourceLocation CondLoc = AStmt->getBeginLoc(); 8423 Expr *BoundUB = UB.get(); 8424 if (UseStrictCompare) { 8425 BoundUB = 8426 SemaRef 8427 .BuildBinOp(CurScope, CondLoc, BO_Add, BoundUB, 8428 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()) 8429 .get(); 8430 BoundUB = 8431 SemaRef.ActOnFinishFullExpr(BoundUB, /*DiscardedValue*/ false).get(); 8432 } 8433 ExprResult Cond = 8434 (isOpenMPWorksharingDirective(DKind) || 8435 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)) 8436 ? SemaRef.BuildBinOp(CurScope, CondLoc, 8437 UseStrictCompare ? BO_LT : BO_LE, IV.get(), 8438 BoundUB) 8439 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(), 8440 NumIterations.get()); 8441 ExprResult CombDistCond; 8442 if (isOpenMPLoopBoundSharingDirective(DKind)) { 8443 CombDistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(), 8444 NumIterations.get()); 8445 } 8446 8447 ExprResult CombCond; 8448 if (isOpenMPLoopBoundSharingDirective(DKind)) { 8449 Expr *BoundCombUB = CombUB.get(); 8450 if (UseStrictCompare) { 8451 BoundCombUB = 8452 SemaRef 8453 .BuildBinOp( 8454 CurScope, CondLoc, BO_Add, BoundCombUB, 8455 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()) 8456 .get(); 8457 BoundCombUB = 8458 SemaRef.ActOnFinishFullExpr(BoundCombUB, /*DiscardedValue*/ false) 8459 .get(); 8460 } 8461 CombCond = 8462 SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, 8463 IV.get(), BoundCombUB); 8464 } 8465 // Loop increment (IV = IV + 1) 8466 SourceLocation IncLoc = AStmt->getBeginLoc(); 8467 ExprResult Inc = 8468 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(), 8469 SemaRef.ActOnIntegerConstant(IncLoc, 1).get()); 8470 if (!Inc.isUsable()) 8471 return 0; 8472 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get()); 8473 Inc = SemaRef.ActOnFinishFullExpr(Inc.get(), /*DiscardedValue*/ false); 8474 if (!Inc.isUsable()) 8475 return 0; 8476 8477 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST). 8478 // Used for directives with static scheduling. 8479 // In combined construct, add combined version that use CombLB and CombUB 8480 // base variables for the update 8481 ExprResult NextLB, NextUB, CombNextLB, CombNextUB; 8482 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || 8483 isOpenMPDistributeDirective(DKind)) { 8484 // LB + ST 8485 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get()); 8486 if (!NextLB.isUsable()) 8487 return 0; 8488 // LB = LB + ST 8489 NextLB = 8490 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get()); 8491 NextLB = 8492 SemaRef.ActOnFinishFullExpr(NextLB.get(), /*DiscardedValue*/ false); 8493 if (!NextLB.isUsable()) 8494 return 0; 8495 // UB + ST 8496 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get()); 8497 if (!NextUB.isUsable()) 8498 return 0; 8499 // UB = UB + ST 8500 NextUB = 8501 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get()); 8502 NextUB = 8503 SemaRef.ActOnFinishFullExpr(NextUB.get(), /*DiscardedValue*/ false); 8504 if (!NextUB.isUsable()) 8505 return 0; 8506 if (isOpenMPLoopBoundSharingDirective(DKind)) { 8507 CombNextLB = 8508 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get()); 8509 if (!NextLB.isUsable()) 8510 return 0; 8511 // LB = LB + ST 8512 CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(), 8513 CombNextLB.get()); 8514 CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get(), 8515 /*DiscardedValue*/ false); 8516 if (!CombNextLB.isUsable()) 8517 return 0; 8518 // UB + ST 8519 CombNextUB = 8520 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get()); 8521 if (!CombNextUB.isUsable()) 8522 return 0; 8523 // UB = UB + ST 8524 CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(), 8525 CombNextUB.get()); 8526 CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get(), 8527 /*DiscardedValue*/ false); 8528 if (!CombNextUB.isUsable()) 8529 return 0; 8530 } 8531 } 8532 8533 // Create increment expression for distribute loop when combined in a same 8534 // directive with for as IV = IV + ST; ensure upper bound expression based 8535 // on PrevUB instead of NumIterations - used to implement 'for' when found 8536 // in combination with 'distribute', like in 'distribute parallel for' 8537 SourceLocation DistIncLoc = AStmt->getBeginLoc(); 8538 ExprResult DistCond, DistInc, PrevEUB, ParForInDistCond; 8539 if (isOpenMPLoopBoundSharingDirective(DKind)) { 8540 DistCond = SemaRef.BuildBinOp( 8541 CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, IV.get(), BoundUB); 8542 assert(DistCond.isUsable() && "distribute cond expr was not built"); 8543 8544 DistInc = 8545 SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get()); 8546 assert(DistInc.isUsable() && "distribute inc expr was not built"); 8547 DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(), 8548 DistInc.get()); 8549 DistInc = 8550 SemaRef.ActOnFinishFullExpr(DistInc.get(), /*DiscardedValue*/ false); 8551 assert(DistInc.isUsable() && "distribute inc expr was not built"); 8552 8553 // Build expression: UB = min(UB, prevUB) for #for in composite or combined 8554 // construct 8555 SourceLocation DistEUBLoc = AStmt->getBeginLoc(); 8556 ExprResult IsUBGreater = 8557 SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, UB.get(), PrevUB.get()); 8558 ExprResult CondOp = SemaRef.ActOnConditionalOp( 8559 DistEUBLoc, DistEUBLoc, IsUBGreater.get(), PrevUB.get(), UB.get()); 8560 PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(), 8561 CondOp.get()); 8562 PrevEUB = 8563 SemaRef.ActOnFinishFullExpr(PrevEUB.get(), /*DiscardedValue*/ false); 8564 8565 // Build IV <= PrevUB or IV < PrevUB + 1 for unsigned IV to be used in 8566 // parallel for is in combination with a distribute directive with 8567 // schedule(static, 1) 8568 Expr *BoundPrevUB = PrevUB.get(); 8569 if (UseStrictCompare) { 8570 BoundPrevUB = 8571 SemaRef 8572 .BuildBinOp( 8573 CurScope, CondLoc, BO_Add, BoundPrevUB, 8574 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()) 8575 .get(); 8576 BoundPrevUB = 8577 SemaRef.ActOnFinishFullExpr(BoundPrevUB, /*DiscardedValue*/ false) 8578 .get(); 8579 } 8580 ParForInDistCond = 8581 SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, 8582 IV.get(), BoundPrevUB); 8583 } 8584 8585 // Build updates and final values of the loop counters. 8586 bool HasErrors = false; 8587 Built.Counters.resize(NestedLoopCount); 8588 Built.Inits.resize(NestedLoopCount); 8589 Built.Updates.resize(NestedLoopCount); 8590 Built.Finals.resize(NestedLoopCount); 8591 Built.DependentCounters.resize(NestedLoopCount); 8592 Built.DependentInits.resize(NestedLoopCount); 8593 Built.FinalsConditions.resize(NestedLoopCount); 8594 { 8595 // We implement the following algorithm for obtaining the 8596 // original loop iteration variable values based on the 8597 // value of the collapsed loop iteration variable IV. 8598 // 8599 // Let n+1 be the number of collapsed loops in the nest. 8600 // Iteration variables (I0, I1, .... In) 8601 // Iteration counts (N0, N1, ... Nn) 8602 // 8603 // Acc = IV; 8604 // 8605 // To compute Ik for loop k, 0 <= k <= n, generate: 8606 // Prod = N(k+1) * N(k+2) * ... * Nn; 8607 // Ik = Acc / Prod; 8608 // Acc -= Ik * Prod; 8609 // 8610 ExprResult Acc = IV; 8611 for (unsigned int Cnt = 0; Cnt < NestedLoopCount; ++Cnt) { 8612 LoopIterationSpace &IS = IterSpaces[Cnt]; 8613 SourceLocation UpdLoc = IS.IncSrcRange.getBegin(); 8614 ExprResult Iter; 8615 8616 // Compute prod 8617 ExprResult Prod = 8618 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(); 8619 for (unsigned int K = Cnt+1; K < NestedLoopCount; ++K) 8620 Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Prod.get(), 8621 IterSpaces[K].NumIterations); 8622 8623 // Iter = Acc / Prod 8624 // If there is at least one more inner loop to avoid 8625 // multiplication by 1. 8626 if (Cnt + 1 < NestedLoopCount) 8627 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, 8628 Acc.get(), Prod.get()); 8629 else 8630 Iter = Acc; 8631 if (!Iter.isUsable()) { 8632 HasErrors = true; 8633 break; 8634 } 8635 8636 // Update Acc: 8637 // Acc -= Iter * Prod 8638 // Check if there is at least one more inner loop to avoid 8639 // multiplication by 1. 8640 if (Cnt + 1 < NestedLoopCount) 8641 Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, 8642 Iter.get(), Prod.get()); 8643 else 8644 Prod = Iter; 8645 Acc = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Sub, 8646 Acc.get(), Prod.get()); 8647 8648 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step 8649 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()); 8650 DeclRefExpr *CounterVar = buildDeclRefExpr( 8651 SemaRef, VD, IS.CounterVar->getType(), IS.CounterVar->getExprLoc(), 8652 /*RefersToCapture=*/true); 8653 ExprResult Init = 8654 buildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar, 8655 IS.CounterInit, IS.IsNonRectangularLB, Captures); 8656 if (!Init.isUsable()) { 8657 HasErrors = true; 8658 break; 8659 } 8660 ExprResult Update = buildCounterUpdate( 8661 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter, 8662 IS.CounterStep, IS.Subtract, IS.IsNonRectangularLB, &Captures); 8663 if (!Update.isUsable()) { 8664 HasErrors = true; 8665 break; 8666 } 8667 8668 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step 8669 ExprResult Final = 8670 buildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar, 8671 IS.CounterInit, IS.NumIterations, IS.CounterStep, 8672 IS.Subtract, IS.IsNonRectangularLB, &Captures); 8673 if (!Final.isUsable()) { 8674 HasErrors = true; 8675 break; 8676 } 8677 8678 if (!Update.isUsable() || !Final.isUsable()) { 8679 HasErrors = true; 8680 break; 8681 } 8682 // Save results 8683 Built.Counters[Cnt] = IS.CounterVar; 8684 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar; 8685 Built.Inits[Cnt] = Init.get(); 8686 Built.Updates[Cnt] = Update.get(); 8687 Built.Finals[Cnt] = Final.get(); 8688 Built.DependentCounters[Cnt] = nullptr; 8689 Built.DependentInits[Cnt] = nullptr; 8690 Built.FinalsConditions[Cnt] = nullptr; 8691 if (IS.IsNonRectangularLB || IS.IsNonRectangularUB) { 8692 Built.DependentCounters[Cnt] = 8693 Built.Counters[NestedLoopCount - 1 - IS.LoopDependentIdx]; 8694 Built.DependentInits[Cnt] = 8695 Built.Inits[NestedLoopCount - 1 - IS.LoopDependentIdx]; 8696 Built.FinalsConditions[Cnt] = IS.FinalCondition; 8697 } 8698 } 8699 } 8700 8701 if (HasErrors) 8702 return 0; 8703 8704 // Save results 8705 Built.IterationVarRef = IV.get(); 8706 Built.LastIteration = LastIteration.get(); 8707 Built.NumIterations = NumIterations.get(); 8708 Built.CalcLastIteration = SemaRef 8709 .ActOnFinishFullExpr(CalcLastIteration.get(), 8710 /*DiscardedValue=*/false) 8711 .get(); 8712 Built.PreCond = PreCond.get(); 8713 Built.PreInits = buildPreInits(C, Captures); 8714 Built.Cond = Cond.get(); 8715 Built.Init = Init.get(); 8716 Built.Inc = Inc.get(); 8717 Built.LB = LB.get(); 8718 Built.UB = UB.get(); 8719 Built.IL = IL.get(); 8720 Built.ST = ST.get(); 8721 Built.EUB = EUB.get(); 8722 Built.NLB = NextLB.get(); 8723 Built.NUB = NextUB.get(); 8724 Built.PrevLB = PrevLB.get(); 8725 Built.PrevUB = PrevUB.get(); 8726 Built.DistInc = DistInc.get(); 8727 Built.PrevEUB = PrevEUB.get(); 8728 Built.DistCombinedFields.LB = CombLB.get(); 8729 Built.DistCombinedFields.UB = CombUB.get(); 8730 Built.DistCombinedFields.EUB = CombEUB.get(); 8731 Built.DistCombinedFields.Init = CombInit.get(); 8732 Built.DistCombinedFields.Cond = CombCond.get(); 8733 Built.DistCombinedFields.NLB = CombNextLB.get(); 8734 Built.DistCombinedFields.NUB = CombNextUB.get(); 8735 Built.DistCombinedFields.DistCond = CombDistCond.get(); 8736 Built.DistCombinedFields.ParForInDistCond = ParForInDistCond.get(); 8737 8738 return NestedLoopCount; 8739 } 8740 8741 static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) { 8742 auto CollapseClauses = 8743 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses); 8744 if (CollapseClauses.begin() != CollapseClauses.end()) 8745 return (*CollapseClauses.begin())->getNumForLoops(); 8746 return nullptr; 8747 } 8748 8749 static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) { 8750 auto OrderedClauses = 8751 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses); 8752 if (OrderedClauses.begin() != OrderedClauses.end()) 8753 return (*OrderedClauses.begin())->getNumForLoops(); 8754 return nullptr; 8755 } 8756 8757 static bool checkSimdlenSafelenSpecified(Sema &S, 8758 const ArrayRef<OMPClause *> Clauses) { 8759 const OMPSafelenClause *Safelen = nullptr; 8760 const OMPSimdlenClause *Simdlen = nullptr; 8761 8762 for (const OMPClause *Clause : Clauses) { 8763 if (Clause->getClauseKind() == OMPC_safelen) 8764 Safelen = cast<OMPSafelenClause>(Clause); 8765 else if (Clause->getClauseKind() == OMPC_simdlen) 8766 Simdlen = cast<OMPSimdlenClause>(Clause); 8767 if (Safelen && Simdlen) 8768 break; 8769 } 8770 8771 if (Simdlen && Safelen) { 8772 const Expr *SimdlenLength = Simdlen->getSimdlen(); 8773 const Expr *SafelenLength = Safelen->getSafelen(); 8774 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() || 8775 SimdlenLength->isInstantiationDependent() || 8776 SimdlenLength->containsUnexpandedParameterPack()) 8777 return false; 8778 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() || 8779 SafelenLength->isInstantiationDependent() || 8780 SafelenLength->containsUnexpandedParameterPack()) 8781 return false; 8782 Expr::EvalResult SimdlenResult, SafelenResult; 8783 SimdlenLength->EvaluateAsInt(SimdlenResult, S.Context); 8784 SafelenLength->EvaluateAsInt(SafelenResult, S.Context); 8785 llvm::APSInt SimdlenRes = SimdlenResult.Val.getInt(); 8786 llvm::APSInt SafelenRes = SafelenResult.Val.getInt(); 8787 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions] 8788 // If both simdlen and safelen clauses are specified, the value of the 8789 // simdlen parameter must be less than or equal to the value of the safelen 8790 // parameter. 8791 if (SimdlenRes > SafelenRes) { 8792 S.Diag(SimdlenLength->getExprLoc(), 8793 diag::err_omp_wrong_simdlen_safelen_values) 8794 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange(); 8795 return true; 8796 } 8797 } 8798 return false; 8799 } 8800 8801 StmtResult 8802 Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt, 8803 SourceLocation StartLoc, SourceLocation EndLoc, 8804 VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8805 if (!AStmt) 8806 return StmtError(); 8807 8808 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 8809 OMPLoopDirective::HelperExprs B; 8810 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 8811 // define the nested loops number. 8812 unsigned NestedLoopCount = checkOpenMPLoop( 8813 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), 8814 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 8815 if (NestedLoopCount == 0) 8816 return StmtError(); 8817 8818 assert((CurContext->isDependentContext() || B.builtAll()) && 8819 "omp simd loop exprs were not built"); 8820 8821 if (!CurContext->isDependentContext()) { 8822 // Finalize the clauses that need pre-built expressions for CodeGen. 8823 for (OMPClause *C : Clauses) { 8824 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 8825 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 8826 B.NumIterations, *this, CurScope, 8827 DSAStack)) 8828 return StmtError(); 8829 } 8830 } 8831 8832 if (checkSimdlenSafelenSpecified(*this, Clauses)) 8833 return StmtError(); 8834 8835 setFunctionHasBranchProtectedScope(); 8836 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 8837 Clauses, AStmt, B); 8838 } 8839 8840 StmtResult 8841 Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt, 8842 SourceLocation StartLoc, SourceLocation EndLoc, 8843 VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8844 if (!AStmt) 8845 return StmtError(); 8846 8847 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 8848 OMPLoopDirective::HelperExprs B; 8849 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 8850 // define the nested loops number. 8851 unsigned NestedLoopCount = checkOpenMPLoop( 8852 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), 8853 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 8854 if (NestedLoopCount == 0) 8855 return StmtError(); 8856 8857 assert((CurContext->isDependentContext() || B.builtAll()) && 8858 "omp for loop exprs were not built"); 8859 8860 if (!CurContext->isDependentContext()) { 8861 // Finalize the clauses that need pre-built expressions for CodeGen. 8862 for (OMPClause *C : Clauses) { 8863 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 8864 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 8865 B.NumIterations, *this, CurScope, 8866 DSAStack)) 8867 return StmtError(); 8868 } 8869 } 8870 8871 setFunctionHasBranchProtectedScope(); 8872 return OMPForDirective::Create( 8873 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 8874 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 8875 } 8876 8877 StmtResult Sema::ActOnOpenMPForSimdDirective( 8878 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8879 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8880 if (!AStmt) 8881 return StmtError(); 8882 8883 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 8884 OMPLoopDirective::HelperExprs B; 8885 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 8886 // define the nested loops number. 8887 unsigned NestedLoopCount = 8888 checkOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses), 8889 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 8890 VarsWithImplicitDSA, B); 8891 if (NestedLoopCount == 0) 8892 return StmtError(); 8893 8894 assert((CurContext->isDependentContext() || B.builtAll()) && 8895 "omp for simd loop exprs were not built"); 8896 8897 if (!CurContext->isDependentContext()) { 8898 // Finalize the clauses that need pre-built expressions for CodeGen. 8899 for (OMPClause *C : Clauses) { 8900 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 8901 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 8902 B.NumIterations, *this, CurScope, 8903 DSAStack)) 8904 return StmtError(); 8905 } 8906 } 8907 8908 if (checkSimdlenSafelenSpecified(*this, Clauses)) 8909 return StmtError(); 8910 8911 setFunctionHasBranchProtectedScope(); 8912 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 8913 Clauses, AStmt, B); 8914 } 8915 8916 StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses, 8917 Stmt *AStmt, 8918 SourceLocation StartLoc, 8919 SourceLocation EndLoc) { 8920 if (!AStmt) 8921 return StmtError(); 8922 8923 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 8924 auto BaseStmt = AStmt; 8925 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 8926 BaseStmt = CS->getCapturedStmt(); 8927 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 8928 auto S = C->children(); 8929 if (S.begin() == S.end()) 8930 return StmtError(); 8931 // All associated statements must be '#pragma omp section' except for 8932 // the first one. 8933 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) { 8934 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 8935 if (SectionStmt) 8936 Diag(SectionStmt->getBeginLoc(), 8937 diag::err_omp_sections_substmt_not_section); 8938 return StmtError(); 8939 } 8940 cast<OMPSectionDirective>(SectionStmt) 8941 ->setHasCancel(DSAStack->isCancelRegion()); 8942 } 8943 } else { 8944 Diag(AStmt->getBeginLoc(), diag::err_omp_sections_not_compound_stmt); 8945 return StmtError(); 8946 } 8947 8948 setFunctionHasBranchProtectedScope(); 8949 8950 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 8951 DSAStack->getTaskgroupReductionRef(), 8952 DSAStack->isCancelRegion()); 8953 } 8954 8955 StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt, 8956 SourceLocation StartLoc, 8957 SourceLocation EndLoc) { 8958 if (!AStmt) 8959 return StmtError(); 8960 8961 setFunctionHasBranchProtectedScope(); 8962 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion()); 8963 8964 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt, 8965 DSAStack->isCancelRegion()); 8966 } 8967 8968 StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses, 8969 Stmt *AStmt, 8970 SourceLocation StartLoc, 8971 SourceLocation EndLoc) { 8972 if (!AStmt) 8973 return StmtError(); 8974 8975 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 8976 8977 setFunctionHasBranchProtectedScope(); 8978 8979 // OpenMP [2.7.3, single Construct, Restrictions] 8980 // The copyprivate clause must not be used with the nowait clause. 8981 const OMPClause *Nowait = nullptr; 8982 const OMPClause *Copyprivate = nullptr; 8983 for (const OMPClause *Clause : Clauses) { 8984 if (Clause->getClauseKind() == OMPC_nowait) 8985 Nowait = Clause; 8986 else if (Clause->getClauseKind() == OMPC_copyprivate) 8987 Copyprivate = Clause; 8988 if (Copyprivate && Nowait) { 8989 Diag(Copyprivate->getBeginLoc(), 8990 diag::err_omp_single_copyprivate_with_nowait); 8991 Diag(Nowait->getBeginLoc(), diag::note_omp_nowait_clause_here); 8992 return StmtError(); 8993 } 8994 } 8995 8996 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 8997 } 8998 8999 StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt, 9000 SourceLocation StartLoc, 9001 SourceLocation EndLoc) { 9002 if (!AStmt) 9003 return StmtError(); 9004 9005 setFunctionHasBranchProtectedScope(); 9006 9007 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt); 9008 } 9009 9010 StmtResult Sema::ActOnOpenMPCriticalDirective( 9011 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses, 9012 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { 9013 if (!AStmt) 9014 return StmtError(); 9015 9016 bool ErrorFound = false; 9017 llvm::APSInt Hint; 9018 SourceLocation HintLoc; 9019 bool DependentHint = false; 9020 for (const OMPClause *C : Clauses) { 9021 if (C->getClauseKind() == OMPC_hint) { 9022 if (!DirName.getName()) { 9023 Diag(C->getBeginLoc(), diag::err_omp_hint_clause_no_name); 9024 ErrorFound = true; 9025 } 9026 Expr *E = cast<OMPHintClause>(C)->getHint(); 9027 if (E->isTypeDependent() || E->isValueDependent() || 9028 E->isInstantiationDependent()) { 9029 DependentHint = true; 9030 } else { 9031 Hint = E->EvaluateKnownConstInt(Context); 9032 HintLoc = C->getBeginLoc(); 9033 } 9034 } 9035 } 9036 if (ErrorFound) 9037 return StmtError(); 9038 const auto Pair = DSAStack->getCriticalWithHint(DirName); 9039 if (Pair.first && DirName.getName() && !DependentHint) { 9040 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) { 9041 Diag(StartLoc, diag::err_omp_critical_with_hint); 9042 if (HintLoc.isValid()) 9043 Diag(HintLoc, diag::note_omp_critical_hint_here) 9044 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false); 9045 else 9046 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0; 9047 if (const auto *C = Pair.first->getSingleClause<OMPHintClause>()) { 9048 Diag(C->getBeginLoc(), diag::note_omp_critical_hint_here) 9049 << 1 9050 << C->getHint()->EvaluateKnownConstInt(Context).toString( 9051 /*Radix=*/10, /*Signed=*/false); 9052 } else { 9053 Diag(Pair.first->getBeginLoc(), diag::note_omp_critical_no_hint) << 1; 9054 } 9055 } 9056 } 9057 9058 setFunctionHasBranchProtectedScope(); 9059 9060 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc, 9061 Clauses, AStmt); 9062 if (!Pair.first && DirName.getName() && !DependentHint) 9063 DSAStack->addCriticalWithHint(Dir, Hint); 9064 return Dir; 9065 } 9066 9067 StmtResult Sema::ActOnOpenMPParallelForDirective( 9068 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 9069 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 9070 if (!AStmt) 9071 return StmtError(); 9072 9073 auto *CS = cast<CapturedStmt>(AStmt); 9074 // 1.2.2 OpenMP Language Terminology 9075 // Structured block - An executable statement with a single entry at the 9076 // top and a single exit at the bottom. 9077 // The point of exit cannot be a branch out of the structured block. 9078 // longjmp() and throw() must not violate the entry/exit criteria. 9079 CS->getCapturedDecl()->setNothrow(); 9080 9081 OMPLoopDirective::HelperExprs B; 9082 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 9083 // define the nested loops number. 9084 unsigned NestedLoopCount = 9085 checkOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses), 9086 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 9087 VarsWithImplicitDSA, B); 9088 if (NestedLoopCount == 0) 9089 return StmtError(); 9090 9091 assert((CurContext->isDependentContext() || B.builtAll()) && 9092 "omp parallel for loop exprs were not built"); 9093 9094 if (!CurContext->isDependentContext()) { 9095 // Finalize the clauses that need pre-built expressions for CodeGen. 9096 for (OMPClause *C : Clauses) { 9097 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 9098 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 9099 B.NumIterations, *this, CurScope, 9100 DSAStack)) 9101 return StmtError(); 9102 } 9103 } 9104 9105 setFunctionHasBranchProtectedScope(); 9106 return OMPParallelForDirective::Create( 9107 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 9108 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 9109 } 9110 9111 StmtResult Sema::ActOnOpenMPParallelForSimdDirective( 9112 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 9113 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 9114 if (!AStmt) 9115 return StmtError(); 9116 9117 auto *CS = cast<CapturedStmt>(AStmt); 9118 // 1.2.2 OpenMP Language Terminology 9119 // Structured block - An executable statement with a single entry at the 9120 // top and a single exit at the bottom. 9121 // The point of exit cannot be a branch out of the structured block. 9122 // longjmp() and throw() must not violate the entry/exit criteria. 9123 CS->getCapturedDecl()->setNothrow(); 9124 9125 OMPLoopDirective::HelperExprs B; 9126 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 9127 // define the nested loops number. 9128 unsigned NestedLoopCount = 9129 checkOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses), 9130 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 9131 VarsWithImplicitDSA, B); 9132 if (NestedLoopCount == 0) 9133 return StmtError(); 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 if (checkSimdlenSafelenSpecified(*this, Clauses)) 9147 return StmtError(); 9148 9149 setFunctionHasBranchProtectedScope(); 9150 return OMPParallelForSimdDirective::Create( 9151 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 9152 } 9153 9154 StmtResult 9155 Sema::ActOnOpenMPParallelMasterDirective(ArrayRef<OMPClause *> Clauses, 9156 Stmt *AStmt, SourceLocation StartLoc, 9157 SourceLocation EndLoc) { 9158 if (!AStmt) 9159 return StmtError(); 9160 9161 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9162 auto *CS = cast<CapturedStmt>(AStmt); 9163 // 1.2.2 OpenMP Language Terminology 9164 // Structured block - An executable statement with a single entry at the 9165 // top and a single exit at the bottom. 9166 // The point of exit cannot be a branch out of the structured block. 9167 // longjmp() and throw() must not violate the entry/exit criteria. 9168 CS->getCapturedDecl()->setNothrow(); 9169 9170 setFunctionHasBranchProtectedScope(); 9171 9172 return OMPParallelMasterDirective::Create( 9173 Context, StartLoc, EndLoc, Clauses, AStmt, 9174 DSAStack->getTaskgroupReductionRef()); 9175 } 9176 9177 StmtResult 9178 Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses, 9179 Stmt *AStmt, SourceLocation StartLoc, 9180 SourceLocation EndLoc) { 9181 if (!AStmt) 9182 return StmtError(); 9183 9184 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9185 auto BaseStmt = AStmt; 9186 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 9187 BaseStmt = CS->getCapturedStmt(); 9188 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 9189 auto S = C->children(); 9190 if (S.begin() == S.end()) 9191 return StmtError(); 9192 // All associated statements must be '#pragma omp section' except for 9193 // the first one. 9194 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) { 9195 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 9196 if (SectionStmt) 9197 Diag(SectionStmt->getBeginLoc(), 9198 diag::err_omp_parallel_sections_substmt_not_section); 9199 return StmtError(); 9200 } 9201 cast<OMPSectionDirective>(SectionStmt) 9202 ->setHasCancel(DSAStack->isCancelRegion()); 9203 } 9204 } else { 9205 Diag(AStmt->getBeginLoc(), 9206 diag::err_omp_parallel_sections_not_compound_stmt); 9207 return StmtError(); 9208 } 9209 9210 setFunctionHasBranchProtectedScope(); 9211 9212 return OMPParallelSectionsDirective::Create( 9213 Context, StartLoc, EndLoc, Clauses, AStmt, 9214 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 9215 } 9216 9217 /// detach and mergeable clauses are mutially exclusive, check for it. 9218 static bool checkDetachMergeableClauses(Sema &S, 9219 ArrayRef<OMPClause *> Clauses) { 9220 const OMPClause *PrevClause = nullptr; 9221 bool ErrorFound = false; 9222 for (const OMPClause *C : Clauses) { 9223 if (C->getClauseKind() == OMPC_detach || 9224 C->getClauseKind() == OMPC_mergeable) { 9225 if (!PrevClause) { 9226 PrevClause = C; 9227 } else if (PrevClause->getClauseKind() != C->getClauseKind()) { 9228 S.Diag(C->getBeginLoc(), diag::err_omp_clauses_mutually_exclusive) 9229 << getOpenMPClauseName(C->getClauseKind()) 9230 << getOpenMPClauseName(PrevClause->getClauseKind()); 9231 S.Diag(PrevClause->getBeginLoc(), diag::note_omp_previous_clause) 9232 << getOpenMPClauseName(PrevClause->getClauseKind()); 9233 ErrorFound = true; 9234 } 9235 } 9236 } 9237 return ErrorFound; 9238 } 9239 9240 StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses, 9241 Stmt *AStmt, SourceLocation StartLoc, 9242 SourceLocation EndLoc) { 9243 if (!AStmt) 9244 return StmtError(); 9245 9246 // OpenMP 5.0, 2.10.1 task Construct 9247 // If a detach clause appears on the directive, then a mergeable clause cannot 9248 // appear on the same directive. 9249 if (checkDetachMergeableClauses(*this, Clauses)) 9250 return StmtError(); 9251 9252 auto *CS = cast<CapturedStmt>(AStmt); 9253 // 1.2.2 OpenMP Language Terminology 9254 // Structured block - An executable statement with a single entry at the 9255 // top and a single exit at the bottom. 9256 // The point of exit cannot be a branch out of the structured block. 9257 // longjmp() and throw() must not violate the entry/exit criteria. 9258 CS->getCapturedDecl()->setNothrow(); 9259 9260 setFunctionHasBranchProtectedScope(); 9261 9262 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 9263 DSAStack->isCancelRegion()); 9264 } 9265 9266 StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc, 9267 SourceLocation EndLoc) { 9268 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc); 9269 } 9270 9271 StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc, 9272 SourceLocation EndLoc) { 9273 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc); 9274 } 9275 9276 StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc, 9277 SourceLocation EndLoc) { 9278 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc); 9279 } 9280 9281 StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses, 9282 Stmt *AStmt, 9283 SourceLocation StartLoc, 9284 SourceLocation EndLoc) { 9285 if (!AStmt) 9286 return StmtError(); 9287 9288 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9289 9290 setFunctionHasBranchProtectedScope(); 9291 9292 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses, 9293 AStmt, 9294 DSAStack->getTaskgroupReductionRef()); 9295 } 9296 9297 StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses, 9298 SourceLocation StartLoc, 9299 SourceLocation EndLoc) { 9300 OMPFlushClause *FC = nullptr; 9301 OMPClause *OrderClause = nullptr; 9302 for (OMPClause *C : Clauses) { 9303 if (C->getClauseKind() == OMPC_flush) 9304 FC = cast<OMPFlushClause>(C); 9305 else 9306 OrderClause = C; 9307 } 9308 OpenMPClauseKind MemOrderKind = OMPC_unknown; 9309 SourceLocation MemOrderLoc; 9310 for (const OMPClause *C : Clauses) { 9311 if (C->getClauseKind() == OMPC_acq_rel || 9312 C->getClauseKind() == OMPC_acquire || 9313 C->getClauseKind() == OMPC_release) { 9314 if (MemOrderKind != OMPC_unknown) { 9315 Diag(C->getBeginLoc(), diag::err_omp_several_mem_order_clauses) 9316 << getOpenMPDirectiveName(OMPD_flush) << 1 9317 << SourceRange(C->getBeginLoc(), C->getEndLoc()); 9318 Diag(MemOrderLoc, diag::note_omp_previous_mem_order_clause) 9319 << getOpenMPClauseName(MemOrderKind); 9320 } else { 9321 MemOrderKind = C->getClauseKind(); 9322 MemOrderLoc = C->getBeginLoc(); 9323 } 9324 } 9325 } 9326 if (FC && OrderClause) { 9327 Diag(FC->getLParenLoc(), diag::err_omp_flush_order_clause_and_list) 9328 << getOpenMPClauseName(OrderClause->getClauseKind()); 9329 Diag(OrderClause->getBeginLoc(), diag::note_omp_flush_order_clause_here) 9330 << getOpenMPClauseName(OrderClause->getClauseKind()); 9331 return StmtError(); 9332 } 9333 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses); 9334 } 9335 9336 StmtResult Sema::ActOnOpenMPDepobjDirective(ArrayRef<OMPClause *> Clauses, 9337 SourceLocation StartLoc, 9338 SourceLocation EndLoc) { 9339 if (Clauses.empty()) { 9340 Diag(StartLoc, diag::err_omp_depobj_expected); 9341 return StmtError(); 9342 } else if (Clauses[0]->getClauseKind() != OMPC_depobj) { 9343 Diag(Clauses[0]->getBeginLoc(), diag::err_omp_depobj_expected); 9344 return StmtError(); 9345 } 9346 // Only depobj expression and another single clause is allowed. 9347 if (Clauses.size() > 2) { 9348 Diag(Clauses[2]->getBeginLoc(), 9349 diag::err_omp_depobj_single_clause_expected); 9350 return StmtError(); 9351 } else if (Clauses.size() < 1) { 9352 Diag(Clauses[0]->getEndLoc(), diag::err_omp_depobj_single_clause_expected); 9353 return StmtError(); 9354 } 9355 return OMPDepobjDirective::Create(Context, StartLoc, EndLoc, Clauses); 9356 } 9357 9358 StmtResult Sema::ActOnOpenMPScanDirective(ArrayRef<OMPClause *> Clauses, 9359 SourceLocation StartLoc, 9360 SourceLocation EndLoc) { 9361 // Check that exactly one clause is specified. 9362 if (Clauses.size() != 1) { 9363 Diag(Clauses.empty() ? EndLoc : Clauses[1]->getBeginLoc(), 9364 diag::err_omp_scan_single_clause_expected); 9365 return StmtError(); 9366 } 9367 // Check that scan directive is used in the scopeof the OpenMP loop body. 9368 if (Scope *S = DSAStack->getCurScope()) { 9369 Scope *ParentS = S->getParent(); 9370 if (!ParentS || ParentS->getParent() != ParentS->getBreakParent() || 9371 !ParentS->getBreakParent()->isOpenMPLoopScope()) 9372 return StmtError(Diag(StartLoc, diag::err_omp_orphaned_device_directive) 9373 << getOpenMPDirectiveName(OMPD_scan) << 5); 9374 } 9375 // Check that only one instance of scan directives is used in the same outer 9376 // region. 9377 if (DSAStack->doesParentHasScanDirective()) { 9378 Diag(StartLoc, diag::err_omp_several_directives_in_region) << "scan"; 9379 Diag(DSAStack->getParentScanDirectiveLoc(), 9380 diag::note_omp_previous_directive) 9381 << "scan"; 9382 return StmtError(); 9383 } 9384 DSAStack->setParentHasScanDirective(StartLoc); 9385 return OMPScanDirective::Create(Context, StartLoc, EndLoc, Clauses); 9386 } 9387 9388 StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses, 9389 Stmt *AStmt, 9390 SourceLocation StartLoc, 9391 SourceLocation EndLoc) { 9392 const OMPClause *DependFound = nullptr; 9393 const OMPClause *DependSourceClause = nullptr; 9394 const OMPClause *DependSinkClause = nullptr; 9395 bool ErrorFound = false; 9396 const OMPThreadsClause *TC = nullptr; 9397 const OMPSIMDClause *SC = nullptr; 9398 for (const OMPClause *C : Clauses) { 9399 if (auto *DC = dyn_cast<OMPDependClause>(C)) { 9400 DependFound = C; 9401 if (DC->getDependencyKind() == OMPC_DEPEND_source) { 9402 if (DependSourceClause) { 9403 Diag(C->getBeginLoc(), diag::err_omp_more_one_clause) 9404 << getOpenMPDirectiveName(OMPD_ordered) 9405 << getOpenMPClauseName(OMPC_depend) << 2; 9406 ErrorFound = true; 9407 } else { 9408 DependSourceClause = C; 9409 } 9410 if (DependSinkClause) { 9411 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed) 9412 << 0; 9413 ErrorFound = true; 9414 } 9415 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) { 9416 if (DependSourceClause) { 9417 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed) 9418 << 1; 9419 ErrorFound = true; 9420 } 9421 DependSinkClause = C; 9422 } 9423 } else if (C->getClauseKind() == OMPC_threads) { 9424 TC = cast<OMPThreadsClause>(C); 9425 } else if (C->getClauseKind() == OMPC_simd) { 9426 SC = cast<OMPSIMDClause>(C); 9427 } 9428 } 9429 if (!ErrorFound && !SC && 9430 isOpenMPSimdDirective(DSAStack->getParentDirective())) { 9431 // OpenMP [2.8.1,simd Construct, Restrictions] 9432 // An ordered construct with the simd clause is the only OpenMP construct 9433 // that can appear in the simd region. 9434 Diag(StartLoc, diag::err_omp_prohibited_region_simd) 9435 << (LangOpts.OpenMP >= 50 ? 1 : 0); 9436 ErrorFound = true; 9437 } else if (DependFound && (TC || SC)) { 9438 Diag(DependFound->getBeginLoc(), diag::err_omp_depend_clause_thread_simd) 9439 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind()); 9440 ErrorFound = true; 9441 } else if (DependFound && !DSAStack->getParentOrderedRegionParam().first) { 9442 Diag(DependFound->getBeginLoc(), 9443 diag::err_omp_ordered_directive_without_param); 9444 ErrorFound = true; 9445 } else if (TC || Clauses.empty()) { 9446 if (const Expr *Param = DSAStack->getParentOrderedRegionParam().first) { 9447 SourceLocation ErrLoc = TC ? TC->getBeginLoc() : StartLoc; 9448 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param) 9449 << (TC != nullptr); 9450 Diag(Param->getBeginLoc(), diag::note_omp_ordered_param) << 1; 9451 ErrorFound = true; 9452 } 9453 } 9454 if ((!AStmt && !DependFound) || ErrorFound) 9455 return StmtError(); 9456 9457 // OpenMP 5.0, 2.17.9, ordered Construct, Restrictions. 9458 // During execution of an iteration of a worksharing-loop or a loop nest 9459 // within a worksharing-loop, simd, or worksharing-loop SIMD region, a thread 9460 // must not execute more than one ordered region corresponding to an ordered 9461 // construct without a depend clause. 9462 if (!DependFound) { 9463 if (DSAStack->doesParentHasOrderedDirective()) { 9464 Diag(StartLoc, diag::err_omp_several_directives_in_region) << "ordered"; 9465 Diag(DSAStack->getParentOrderedDirectiveLoc(), 9466 diag::note_omp_previous_directive) 9467 << "ordered"; 9468 return StmtError(); 9469 } 9470 DSAStack->setParentHasOrderedDirective(StartLoc); 9471 } 9472 9473 if (AStmt) { 9474 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9475 9476 setFunctionHasBranchProtectedScope(); 9477 } 9478 9479 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 9480 } 9481 9482 namespace { 9483 /// Helper class for checking expression in 'omp atomic [update]' 9484 /// construct. 9485 class OpenMPAtomicUpdateChecker { 9486 /// Error results for atomic update expressions. 9487 enum ExprAnalysisErrorCode { 9488 /// A statement is not an expression statement. 9489 NotAnExpression, 9490 /// Expression is not builtin binary or unary operation. 9491 NotABinaryOrUnaryExpression, 9492 /// Unary operation is not post-/pre- increment/decrement operation. 9493 NotAnUnaryIncDecExpression, 9494 /// An expression is not of scalar type. 9495 NotAScalarType, 9496 /// A binary operation is not an assignment operation. 9497 NotAnAssignmentOp, 9498 /// RHS part of the binary operation is not a binary expression. 9499 NotABinaryExpression, 9500 /// RHS part is not additive/multiplicative/shift/biwise binary 9501 /// expression. 9502 NotABinaryOperator, 9503 /// RHS binary operation does not have reference to the updated LHS 9504 /// part. 9505 NotAnUpdateExpression, 9506 /// No errors is found. 9507 NoError 9508 }; 9509 /// Reference to Sema. 9510 Sema &SemaRef; 9511 /// A location for note diagnostics (when error is found). 9512 SourceLocation NoteLoc; 9513 /// 'x' lvalue part of the source atomic expression. 9514 Expr *X; 9515 /// 'expr' rvalue part of the source atomic expression. 9516 Expr *E; 9517 /// Helper expression of the form 9518 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or 9519 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. 9520 Expr *UpdateExpr; 9521 /// Is 'x' a LHS in a RHS part of full update expression. It is 9522 /// important for non-associative operations. 9523 bool IsXLHSInRHSPart; 9524 BinaryOperatorKind Op; 9525 SourceLocation OpLoc; 9526 /// true if the source expression is a postfix unary operation, false 9527 /// if it is a prefix unary operation. 9528 bool IsPostfixUpdate; 9529 9530 public: 9531 OpenMPAtomicUpdateChecker(Sema &SemaRef) 9532 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr), 9533 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {} 9534 /// Check specified statement that it is suitable for 'atomic update' 9535 /// constructs and extract 'x', 'expr' and Operation from the original 9536 /// expression. If DiagId and NoteId == 0, then only check is performed 9537 /// without error notification. 9538 /// \param DiagId Diagnostic which should be emitted if error is found. 9539 /// \param NoteId Diagnostic note for the main error message. 9540 /// \return true if statement is not an update expression, false otherwise. 9541 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0); 9542 /// Return the 'x' lvalue part of the source atomic expression. 9543 Expr *getX() const { return X; } 9544 /// Return the 'expr' rvalue part of the source atomic expression. 9545 Expr *getExpr() const { return E; } 9546 /// Return the update expression used in calculation of the updated 9547 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or 9548 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. 9549 Expr *getUpdateExpr() const { return UpdateExpr; } 9550 /// Return true if 'x' is LHS in RHS part of full update expression, 9551 /// false otherwise. 9552 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; } 9553 9554 /// true if the source expression is a postfix unary operation, false 9555 /// if it is a prefix unary operation. 9556 bool isPostfixUpdate() const { return IsPostfixUpdate; } 9557 9558 private: 9559 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0, 9560 unsigned NoteId = 0); 9561 }; 9562 } // namespace 9563 9564 bool OpenMPAtomicUpdateChecker::checkBinaryOperation( 9565 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) { 9566 ExprAnalysisErrorCode ErrorFound = NoError; 9567 SourceLocation ErrorLoc, NoteLoc; 9568 SourceRange ErrorRange, NoteRange; 9569 // Allowed constructs are: 9570 // x = x binop expr; 9571 // x = expr binop x; 9572 if (AtomicBinOp->getOpcode() == BO_Assign) { 9573 X = AtomicBinOp->getLHS(); 9574 if (const auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>( 9575 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) { 9576 if (AtomicInnerBinOp->isMultiplicativeOp() || 9577 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() || 9578 AtomicInnerBinOp->isBitwiseOp()) { 9579 Op = AtomicInnerBinOp->getOpcode(); 9580 OpLoc = AtomicInnerBinOp->getOperatorLoc(); 9581 Expr *LHS = AtomicInnerBinOp->getLHS(); 9582 Expr *RHS = AtomicInnerBinOp->getRHS(); 9583 llvm::FoldingSetNodeID XId, LHSId, RHSId; 9584 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(), 9585 /*Canonical=*/true); 9586 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(), 9587 /*Canonical=*/true); 9588 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(), 9589 /*Canonical=*/true); 9590 if (XId == LHSId) { 9591 E = RHS; 9592 IsXLHSInRHSPart = true; 9593 } else if (XId == RHSId) { 9594 E = LHS; 9595 IsXLHSInRHSPart = false; 9596 } else { 9597 ErrorLoc = AtomicInnerBinOp->getExprLoc(); 9598 ErrorRange = AtomicInnerBinOp->getSourceRange(); 9599 NoteLoc = X->getExprLoc(); 9600 NoteRange = X->getSourceRange(); 9601 ErrorFound = NotAnUpdateExpression; 9602 } 9603 } else { 9604 ErrorLoc = AtomicInnerBinOp->getExprLoc(); 9605 ErrorRange = AtomicInnerBinOp->getSourceRange(); 9606 NoteLoc = AtomicInnerBinOp->getOperatorLoc(); 9607 NoteRange = SourceRange(NoteLoc, NoteLoc); 9608 ErrorFound = NotABinaryOperator; 9609 } 9610 } else { 9611 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc(); 9612 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange(); 9613 ErrorFound = NotABinaryExpression; 9614 } 9615 } else { 9616 ErrorLoc = AtomicBinOp->getExprLoc(); 9617 ErrorRange = AtomicBinOp->getSourceRange(); 9618 NoteLoc = AtomicBinOp->getOperatorLoc(); 9619 NoteRange = SourceRange(NoteLoc, NoteLoc); 9620 ErrorFound = NotAnAssignmentOp; 9621 } 9622 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { 9623 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; 9624 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; 9625 return true; 9626 } 9627 if (SemaRef.CurContext->isDependentContext()) 9628 E = X = UpdateExpr = nullptr; 9629 return ErrorFound != NoError; 9630 } 9631 9632 bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId, 9633 unsigned NoteId) { 9634 ExprAnalysisErrorCode ErrorFound = NoError; 9635 SourceLocation ErrorLoc, NoteLoc; 9636 SourceRange ErrorRange, NoteRange; 9637 // Allowed constructs are: 9638 // x++; 9639 // x--; 9640 // ++x; 9641 // --x; 9642 // x binop= expr; 9643 // x = x binop expr; 9644 // x = expr binop x; 9645 if (auto *AtomicBody = dyn_cast<Expr>(S)) { 9646 AtomicBody = AtomicBody->IgnoreParenImpCasts(); 9647 if (AtomicBody->getType()->isScalarType() || 9648 AtomicBody->isInstantiationDependent()) { 9649 if (const auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>( 9650 AtomicBody->IgnoreParenImpCasts())) { 9651 // Check for Compound Assignment Operation 9652 Op = BinaryOperator::getOpForCompoundAssignment( 9653 AtomicCompAssignOp->getOpcode()); 9654 OpLoc = AtomicCompAssignOp->getOperatorLoc(); 9655 E = AtomicCompAssignOp->getRHS(); 9656 X = AtomicCompAssignOp->getLHS()->IgnoreParens(); 9657 IsXLHSInRHSPart = true; 9658 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>( 9659 AtomicBody->IgnoreParenImpCasts())) { 9660 // Check for Binary Operation 9661 if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId)) 9662 return true; 9663 } else if (const auto *AtomicUnaryOp = dyn_cast<UnaryOperator>( 9664 AtomicBody->IgnoreParenImpCasts())) { 9665 // Check for Unary Operation 9666 if (AtomicUnaryOp->isIncrementDecrementOp()) { 9667 IsPostfixUpdate = AtomicUnaryOp->isPostfix(); 9668 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub; 9669 OpLoc = AtomicUnaryOp->getOperatorLoc(); 9670 X = AtomicUnaryOp->getSubExpr()->IgnoreParens(); 9671 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get(); 9672 IsXLHSInRHSPart = true; 9673 } else { 9674 ErrorFound = NotAnUnaryIncDecExpression; 9675 ErrorLoc = AtomicUnaryOp->getExprLoc(); 9676 ErrorRange = AtomicUnaryOp->getSourceRange(); 9677 NoteLoc = AtomicUnaryOp->getOperatorLoc(); 9678 NoteRange = SourceRange(NoteLoc, NoteLoc); 9679 } 9680 } else if (!AtomicBody->isInstantiationDependent()) { 9681 ErrorFound = NotABinaryOrUnaryExpression; 9682 NoteLoc = ErrorLoc = AtomicBody->getExprLoc(); 9683 NoteRange = ErrorRange = AtomicBody->getSourceRange(); 9684 } 9685 } else { 9686 ErrorFound = NotAScalarType; 9687 NoteLoc = ErrorLoc = AtomicBody->getBeginLoc(); 9688 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 9689 } 9690 } else { 9691 ErrorFound = NotAnExpression; 9692 NoteLoc = ErrorLoc = S->getBeginLoc(); 9693 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 9694 } 9695 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { 9696 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; 9697 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; 9698 return true; 9699 } 9700 if (SemaRef.CurContext->isDependentContext()) 9701 E = X = UpdateExpr = nullptr; 9702 if (ErrorFound == NoError && E && X) { 9703 // Build an update expression of form 'OpaqueValueExpr(x) binop 9704 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop 9705 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression. 9706 auto *OVEX = new (SemaRef.getASTContext()) 9707 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue); 9708 auto *OVEExpr = new (SemaRef.getASTContext()) 9709 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue); 9710 ExprResult Update = 9711 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr, 9712 IsXLHSInRHSPart ? OVEExpr : OVEX); 9713 if (Update.isInvalid()) 9714 return true; 9715 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(), 9716 Sema::AA_Casting); 9717 if (Update.isInvalid()) 9718 return true; 9719 UpdateExpr = Update.get(); 9720 } 9721 return ErrorFound != NoError; 9722 } 9723 9724 StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses, 9725 Stmt *AStmt, 9726 SourceLocation StartLoc, 9727 SourceLocation EndLoc) { 9728 // Register location of the first atomic directive. 9729 DSAStack->addAtomicDirectiveLoc(StartLoc); 9730 if (!AStmt) 9731 return StmtError(); 9732 9733 // 1.2.2 OpenMP Language Terminology 9734 // Structured block - An executable statement with a single entry at the 9735 // top and a single exit at the bottom. 9736 // The point of exit cannot be a branch out of the structured block. 9737 // longjmp() and throw() must not violate the entry/exit criteria. 9738 OpenMPClauseKind AtomicKind = OMPC_unknown; 9739 SourceLocation AtomicKindLoc; 9740 OpenMPClauseKind MemOrderKind = OMPC_unknown; 9741 SourceLocation MemOrderLoc; 9742 for (const OMPClause *C : Clauses) { 9743 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write || 9744 C->getClauseKind() == OMPC_update || 9745 C->getClauseKind() == OMPC_capture) { 9746 if (AtomicKind != OMPC_unknown) { 9747 Diag(C->getBeginLoc(), diag::err_omp_atomic_several_clauses) 9748 << SourceRange(C->getBeginLoc(), C->getEndLoc()); 9749 Diag(AtomicKindLoc, diag::note_omp_previous_mem_order_clause) 9750 << getOpenMPClauseName(AtomicKind); 9751 } else { 9752 AtomicKind = C->getClauseKind(); 9753 AtomicKindLoc = C->getBeginLoc(); 9754 } 9755 } 9756 if (C->getClauseKind() == OMPC_seq_cst || 9757 C->getClauseKind() == OMPC_acq_rel || 9758 C->getClauseKind() == OMPC_acquire || 9759 C->getClauseKind() == OMPC_release || 9760 C->getClauseKind() == OMPC_relaxed) { 9761 if (MemOrderKind != OMPC_unknown) { 9762 Diag(C->getBeginLoc(), diag::err_omp_several_mem_order_clauses) 9763 << getOpenMPDirectiveName(OMPD_atomic) << 0 9764 << SourceRange(C->getBeginLoc(), C->getEndLoc()); 9765 Diag(MemOrderLoc, diag::note_omp_previous_mem_order_clause) 9766 << getOpenMPClauseName(MemOrderKind); 9767 } else { 9768 MemOrderKind = C->getClauseKind(); 9769 MemOrderLoc = C->getBeginLoc(); 9770 } 9771 } 9772 } 9773 // OpenMP 5.0, 2.17.7 atomic Construct, Restrictions 9774 // If atomic-clause is read then memory-order-clause must not be acq_rel or 9775 // release. 9776 // If atomic-clause is write then memory-order-clause must not be acq_rel or 9777 // acquire. 9778 // If atomic-clause is update or not present then memory-order-clause must not 9779 // be acq_rel or acquire. 9780 if ((AtomicKind == OMPC_read && 9781 (MemOrderKind == OMPC_acq_rel || MemOrderKind == OMPC_release)) || 9782 ((AtomicKind == OMPC_write || AtomicKind == OMPC_update || 9783 AtomicKind == OMPC_unknown) && 9784 (MemOrderKind == OMPC_acq_rel || MemOrderKind == OMPC_acquire))) { 9785 SourceLocation Loc = AtomicKindLoc; 9786 if (AtomicKind == OMPC_unknown) 9787 Loc = StartLoc; 9788 Diag(Loc, diag::err_omp_atomic_incompatible_mem_order_clause) 9789 << getOpenMPClauseName(AtomicKind) 9790 << (AtomicKind == OMPC_unknown ? 1 : 0) 9791 << getOpenMPClauseName(MemOrderKind); 9792 Diag(MemOrderLoc, diag::note_omp_previous_mem_order_clause) 9793 << getOpenMPClauseName(MemOrderKind); 9794 } 9795 9796 Stmt *Body = AStmt; 9797 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body)) 9798 Body = EWC->getSubExpr(); 9799 9800 Expr *X = nullptr; 9801 Expr *V = nullptr; 9802 Expr *E = nullptr; 9803 Expr *UE = nullptr; 9804 bool IsXLHSInRHSPart = false; 9805 bool IsPostfixUpdate = false; 9806 // OpenMP [2.12.6, atomic Construct] 9807 // In the next expressions: 9808 // * x and v (as applicable) are both l-value expressions with scalar type. 9809 // * During the execution of an atomic region, multiple syntactic 9810 // occurrences of x must designate the same storage location. 9811 // * Neither of v and expr (as applicable) may access the storage location 9812 // designated by x. 9813 // * Neither of x and expr (as applicable) may access the storage location 9814 // designated by v. 9815 // * expr is an expression with scalar type. 9816 // * binop is one of +, *, -, /, &, ^, |, <<, or >>. 9817 // * binop, binop=, ++, and -- are not overloaded operators. 9818 // * The expression x binop expr must be numerically equivalent to x binop 9819 // (expr). This requirement is satisfied if the operators in expr have 9820 // precedence greater than binop, or by using parentheses around expr or 9821 // subexpressions of expr. 9822 // * The expression expr binop x must be numerically equivalent to (expr) 9823 // binop x. This requirement is satisfied if the operators in expr have 9824 // precedence equal to or greater than binop, or by using parentheses around 9825 // expr or subexpressions of expr. 9826 // * For forms that allow multiple occurrences of x, the number of times 9827 // that x is evaluated is unspecified. 9828 if (AtomicKind == OMPC_read) { 9829 enum { 9830 NotAnExpression, 9831 NotAnAssignmentOp, 9832 NotAScalarType, 9833 NotAnLValue, 9834 NoError 9835 } ErrorFound = NoError; 9836 SourceLocation ErrorLoc, NoteLoc; 9837 SourceRange ErrorRange, NoteRange; 9838 // If clause is read: 9839 // v = x; 9840 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 9841 const auto *AtomicBinOp = 9842 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 9843 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 9844 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); 9845 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts(); 9846 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && 9847 (V->isInstantiationDependent() || V->getType()->isScalarType())) { 9848 if (!X->isLValue() || !V->isLValue()) { 9849 const Expr *NotLValueExpr = X->isLValue() ? V : X; 9850 ErrorFound = NotAnLValue; 9851 ErrorLoc = AtomicBinOp->getExprLoc(); 9852 ErrorRange = AtomicBinOp->getSourceRange(); 9853 NoteLoc = NotLValueExpr->getExprLoc(); 9854 NoteRange = NotLValueExpr->getSourceRange(); 9855 } 9856 } else if (!X->isInstantiationDependent() || 9857 !V->isInstantiationDependent()) { 9858 const Expr *NotScalarExpr = 9859 (X->isInstantiationDependent() || X->getType()->isScalarType()) 9860 ? V 9861 : X; 9862 ErrorFound = NotAScalarType; 9863 ErrorLoc = AtomicBinOp->getExprLoc(); 9864 ErrorRange = AtomicBinOp->getSourceRange(); 9865 NoteLoc = NotScalarExpr->getExprLoc(); 9866 NoteRange = NotScalarExpr->getSourceRange(); 9867 } 9868 } else if (!AtomicBody->isInstantiationDependent()) { 9869 ErrorFound = NotAnAssignmentOp; 9870 ErrorLoc = AtomicBody->getExprLoc(); 9871 ErrorRange = AtomicBody->getSourceRange(); 9872 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 9873 : AtomicBody->getExprLoc(); 9874 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 9875 : AtomicBody->getSourceRange(); 9876 } 9877 } else { 9878 ErrorFound = NotAnExpression; 9879 NoteLoc = ErrorLoc = Body->getBeginLoc(); 9880 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 9881 } 9882 if (ErrorFound != NoError) { 9883 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement) 9884 << ErrorRange; 9885 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound 9886 << NoteRange; 9887 return StmtError(); 9888 } 9889 if (CurContext->isDependentContext()) 9890 V = X = nullptr; 9891 } else if (AtomicKind == OMPC_write) { 9892 enum { 9893 NotAnExpression, 9894 NotAnAssignmentOp, 9895 NotAScalarType, 9896 NotAnLValue, 9897 NoError 9898 } ErrorFound = NoError; 9899 SourceLocation ErrorLoc, NoteLoc; 9900 SourceRange ErrorRange, NoteRange; 9901 // If clause is write: 9902 // x = expr; 9903 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 9904 const auto *AtomicBinOp = 9905 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 9906 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 9907 X = AtomicBinOp->getLHS(); 9908 E = AtomicBinOp->getRHS(); 9909 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && 9910 (E->isInstantiationDependent() || E->getType()->isScalarType())) { 9911 if (!X->isLValue()) { 9912 ErrorFound = NotAnLValue; 9913 ErrorLoc = AtomicBinOp->getExprLoc(); 9914 ErrorRange = AtomicBinOp->getSourceRange(); 9915 NoteLoc = X->getExprLoc(); 9916 NoteRange = X->getSourceRange(); 9917 } 9918 } else if (!X->isInstantiationDependent() || 9919 !E->isInstantiationDependent()) { 9920 const Expr *NotScalarExpr = 9921 (X->isInstantiationDependent() || X->getType()->isScalarType()) 9922 ? E 9923 : X; 9924 ErrorFound = NotAScalarType; 9925 ErrorLoc = AtomicBinOp->getExprLoc(); 9926 ErrorRange = AtomicBinOp->getSourceRange(); 9927 NoteLoc = NotScalarExpr->getExprLoc(); 9928 NoteRange = NotScalarExpr->getSourceRange(); 9929 } 9930 } else if (!AtomicBody->isInstantiationDependent()) { 9931 ErrorFound = NotAnAssignmentOp; 9932 ErrorLoc = AtomicBody->getExprLoc(); 9933 ErrorRange = AtomicBody->getSourceRange(); 9934 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 9935 : AtomicBody->getExprLoc(); 9936 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 9937 : AtomicBody->getSourceRange(); 9938 } 9939 } else { 9940 ErrorFound = NotAnExpression; 9941 NoteLoc = ErrorLoc = Body->getBeginLoc(); 9942 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 9943 } 9944 if (ErrorFound != NoError) { 9945 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement) 9946 << ErrorRange; 9947 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound 9948 << NoteRange; 9949 return StmtError(); 9950 } 9951 if (CurContext->isDependentContext()) 9952 E = X = nullptr; 9953 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) { 9954 // If clause is update: 9955 // x++; 9956 // x--; 9957 // ++x; 9958 // --x; 9959 // x binop= expr; 9960 // x = x binop expr; 9961 // x = expr binop x; 9962 OpenMPAtomicUpdateChecker Checker(*this); 9963 if (Checker.checkStatement( 9964 Body, (AtomicKind == OMPC_update) 9965 ? diag::err_omp_atomic_update_not_expression_statement 9966 : diag::err_omp_atomic_not_expression_statement, 9967 diag::note_omp_atomic_update)) 9968 return StmtError(); 9969 if (!CurContext->isDependentContext()) { 9970 E = Checker.getExpr(); 9971 X = Checker.getX(); 9972 UE = Checker.getUpdateExpr(); 9973 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 9974 } 9975 } else if (AtomicKind == OMPC_capture) { 9976 enum { 9977 NotAnAssignmentOp, 9978 NotACompoundStatement, 9979 NotTwoSubstatements, 9980 NotASpecificExpression, 9981 NoError 9982 } ErrorFound = NoError; 9983 SourceLocation ErrorLoc, NoteLoc; 9984 SourceRange ErrorRange, NoteRange; 9985 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 9986 // If clause is a capture: 9987 // v = x++; 9988 // v = x--; 9989 // v = ++x; 9990 // v = --x; 9991 // v = x binop= expr; 9992 // v = x = x binop expr; 9993 // v = x = expr binop x; 9994 const auto *AtomicBinOp = 9995 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 9996 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 9997 V = AtomicBinOp->getLHS(); 9998 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); 9999 OpenMPAtomicUpdateChecker Checker(*this); 10000 if (Checker.checkStatement( 10001 Body, diag::err_omp_atomic_capture_not_expression_statement, 10002 diag::note_omp_atomic_update)) 10003 return StmtError(); 10004 E = Checker.getExpr(); 10005 X = Checker.getX(); 10006 UE = Checker.getUpdateExpr(); 10007 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 10008 IsPostfixUpdate = Checker.isPostfixUpdate(); 10009 } else if (!AtomicBody->isInstantiationDependent()) { 10010 ErrorLoc = AtomicBody->getExprLoc(); 10011 ErrorRange = AtomicBody->getSourceRange(); 10012 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 10013 : AtomicBody->getExprLoc(); 10014 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 10015 : AtomicBody->getSourceRange(); 10016 ErrorFound = NotAnAssignmentOp; 10017 } 10018 if (ErrorFound != NoError) { 10019 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement) 10020 << ErrorRange; 10021 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; 10022 return StmtError(); 10023 } 10024 if (CurContext->isDependentContext()) 10025 UE = V = E = X = nullptr; 10026 } else { 10027 // If clause is a capture: 10028 // { v = x; x = expr; } 10029 // { v = x; x++; } 10030 // { v = x; x--; } 10031 // { v = x; ++x; } 10032 // { v = x; --x; } 10033 // { v = x; x binop= expr; } 10034 // { v = x; x = x binop expr; } 10035 // { v = x; x = expr binop x; } 10036 // { x++; v = x; } 10037 // { x--; v = x; } 10038 // { ++x; v = x; } 10039 // { --x; v = x; } 10040 // { x binop= expr; v = x; } 10041 // { x = x binop expr; v = x; } 10042 // { x = expr binop x; v = x; } 10043 if (auto *CS = dyn_cast<CompoundStmt>(Body)) { 10044 // Check that this is { expr1; expr2; } 10045 if (CS->size() == 2) { 10046 Stmt *First = CS->body_front(); 10047 Stmt *Second = CS->body_back(); 10048 if (auto *EWC = dyn_cast<ExprWithCleanups>(First)) 10049 First = EWC->getSubExpr()->IgnoreParenImpCasts(); 10050 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second)) 10051 Second = EWC->getSubExpr()->IgnoreParenImpCasts(); 10052 // Need to find what subexpression is 'v' and what is 'x'. 10053 OpenMPAtomicUpdateChecker Checker(*this); 10054 bool IsUpdateExprFound = !Checker.checkStatement(Second); 10055 BinaryOperator *BinOp = nullptr; 10056 if (IsUpdateExprFound) { 10057 BinOp = dyn_cast<BinaryOperator>(First); 10058 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; 10059 } 10060 if (IsUpdateExprFound && !CurContext->isDependentContext()) { 10061 // { v = x; x++; } 10062 // { v = x; x--; } 10063 // { v = x; ++x; } 10064 // { v = x; --x; } 10065 // { v = x; x binop= expr; } 10066 // { v = x; x = x binop expr; } 10067 // { v = x; x = expr binop x; } 10068 // Check that the first expression has form v = x. 10069 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); 10070 llvm::FoldingSetNodeID XId, PossibleXId; 10071 Checker.getX()->Profile(XId, Context, /*Canonical=*/true); 10072 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); 10073 IsUpdateExprFound = XId == PossibleXId; 10074 if (IsUpdateExprFound) { 10075 V = BinOp->getLHS(); 10076 X = Checker.getX(); 10077 E = Checker.getExpr(); 10078 UE = Checker.getUpdateExpr(); 10079 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 10080 IsPostfixUpdate = true; 10081 } 10082 } 10083 if (!IsUpdateExprFound) { 10084 IsUpdateExprFound = !Checker.checkStatement(First); 10085 BinOp = nullptr; 10086 if (IsUpdateExprFound) { 10087 BinOp = dyn_cast<BinaryOperator>(Second); 10088 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; 10089 } 10090 if (IsUpdateExprFound && !CurContext->isDependentContext()) { 10091 // { x++; v = x; } 10092 // { x--; v = x; } 10093 // { ++x; v = x; } 10094 // { --x; v = x; } 10095 // { x binop= expr; v = x; } 10096 // { x = x binop expr; v = x; } 10097 // { x = expr binop x; v = x; } 10098 // Check that the second expression has form v = x. 10099 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); 10100 llvm::FoldingSetNodeID XId, PossibleXId; 10101 Checker.getX()->Profile(XId, Context, /*Canonical=*/true); 10102 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); 10103 IsUpdateExprFound = XId == PossibleXId; 10104 if (IsUpdateExprFound) { 10105 V = BinOp->getLHS(); 10106 X = Checker.getX(); 10107 E = Checker.getExpr(); 10108 UE = Checker.getUpdateExpr(); 10109 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 10110 IsPostfixUpdate = false; 10111 } 10112 } 10113 } 10114 if (!IsUpdateExprFound) { 10115 // { v = x; x = expr; } 10116 auto *FirstExpr = dyn_cast<Expr>(First); 10117 auto *SecondExpr = dyn_cast<Expr>(Second); 10118 if (!FirstExpr || !SecondExpr || 10119 !(FirstExpr->isInstantiationDependent() || 10120 SecondExpr->isInstantiationDependent())) { 10121 auto *FirstBinOp = dyn_cast<BinaryOperator>(First); 10122 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) { 10123 ErrorFound = NotAnAssignmentOp; 10124 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc() 10125 : First->getBeginLoc(); 10126 NoteRange = ErrorRange = FirstBinOp 10127 ? FirstBinOp->getSourceRange() 10128 : SourceRange(ErrorLoc, ErrorLoc); 10129 } else { 10130 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second); 10131 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) { 10132 ErrorFound = NotAnAssignmentOp; 10133 NoteLoc = ErrorLoc = SecondBinOp 10134 ? SecondBinOp->getOperatorLoc() 10135 : Second->getBeginLoc(); 10136 NoteRange = ErrorRange = 10137 SecondBinOp ? SecondBinOp->getSourceRange() 10138 : SourceRange(ErrorLoc, ErrorLoc); 10139 } else { 10140 Expr *PossibleXRHSInFirst = 10141 FirstBinOp->getRHS()->IgnoreParenImpCasts(); 10142 Expr *PossibleXLHSInSecond = 10143 SecondBinOp->getLHS()->IgnoreParenImpCasts(); 10144 llvm::FoldingSetNodeID X1Id, X2Id; 10145 PossibleXRHSInFirst->Profile(X1Id, Context, 10146 /*Canonical=*/true); 10147 PossibleXLHSInSecond->Profile(X2Id, Context, 10148 /*Canonical=*/true); 10149 IsUpdateExprFound = X1Id == X2Id; 10150 if (IsUpdateExprFound) { 10151 V = FirstBinOp->getLHS(); 10152 X = SecondBinOp->getLHS(); 10153 E = SecondBinOp->getRHS(); 10154 UE = nullptr; 10155 IsXLHSInRHSPart = false; 10156 IsPostfixUpdate = true; 10157 } else { 10158 ErrorFound = NotASpecificExpression; 10159 ErrorLoc = FirstBinOp->getExprLoc(); 10160 ErrorRange = FirstBinOp->getSourceRange(); 10161 NoteLoc = SecondBinOp->getLHS()->getExprLoc(); 10162 NoteRange = SecondBinOp->getRHS()->getSourceRange(); 10163 } 10164 } 10165 } 10166 } 10167 } 10168 } else { 10169 NoteLoc = ErrorLoc = Body->getBeginLoc(); 10170 NoteRange = ErrorRange = 10171 SourceRange(Body->getBeginLoc(), Body->getBeginLoc()); 10172 ErrorFound = NotTwoSubstatements; 10173 } 10174 } else { 10175 NoteLoc = ErrorLoc = Body->getBeginLoc(); 10176 NoteRange = ErrorRange = 10177 SourceRange(Body->getBeginLoc(), Body->getBeginLoc()); 10178 ErrorFound = NotACompoundStatement; 10179 } 10180 if (ErrorFound != NoError) { 10181 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement) 10182 << ErrorRange; 10183 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; 10184 return StmtError(); 10185 } 10186 if (CurContext->isDependentContext()) 10187 UE = V = E = X = nullptr; 10188 } 10189 } 10190 10191 setFunctionHasBranchProtectedScope(); 10192 10193 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 10194 X, V, E, UE, IsXLHSInRHSPart, 10195 IsPostfixUpdate); 10196 } 10197 10198 StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses, 10199 Stmt *AStmt, 10200 SourceLocation StartLoc, 10201 SourceLocation EndLoc) { 10202 if (!AStmt) 10203 return StmtError(); 10204 10205 auto *CS = cast<CapturedStmt>(AStmt); 10206 // 1.2.2 OpenMP Language Terminology 10207 // Structured block - An executable statement with a single entry at the 10208 // top and a single exit at the bottom. 10209 // The point of exit cannot be a branch out of the structured block. 10210 // longjmp() and throw() must not violate the entry/exit criteria. 10211 CS->getCapturedDecl()->setNothrow(); 10212 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target); 10213 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10214 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10215 // 1.2.2 OpenMP Language Terminology 10216 // Structured block - An executable statement with a single entry at the 10217 // top and a single exit at the bottom. 10218 // The point of exit cannot be a branch out of the structured block. 10219 // longjmp() and throw() must not violate the entry/exit criteria. 10220 CS->getCapturedDecl()->setNothrow(); 10221 } 10222 10223 // OpenMP [2.16, Nesting of Regions] 10224 // If specified, a teams construct must be contained within a target 10225 // construct. That target construct must contain no statements or directives 10226 // outside of the teams construct. 10227 if (DSAStack->hasInnerTeamsRegion()) { 10228 const Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true); 10229 bool OMPTeamsFound = true; 10230 if (const auto *CS = dyn_cast<CompoundStmt>(S)) { 10231 auto I = CS->body_begin(); 10232 while (I != CS->body_end()) { 10233 const auto *OED = dyn_cast<OMPExecutableDirective>(*I); 10234 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind()) || 10235 OMPTeamsFound) { 10236 10237 OMPTeamsFound = false; 10238 break; 10239 } 10240 ++I; 10241 } 10242 assert(I != CS->body_end() && "Not found statement"); 10243 S = *I; 10244 } else { 10245 const auto *OED = dyn_cast<OMPExecutableDirective>(S); 10246 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind()); 10247 } 10248 if (!OMPTeamsFound) { 10249 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams); 10250 Diag(DSAStack->getInnerTeamsRegionLoc(), 10251 diag::note_omp_nested_teams_construct_here); 10252 Diag(S->getBeginLoc(), diag::note_omp_nested_statement_here) 10253 << isa<OMPExecutableDirective>(S); 10254 return StmtError(); 10255 } 10256 } 10257 10258 setFunctionHasBranchProtectedScope(); 10259 10260 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 10261 } 10262 10263 StmtResult 10264 Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses, 10265 Stmt *AStmt, SourceLocation StartLoc, 10266 SourceLocation EndLoc) { 10267 if (!AStmt) 10268 return StmtError(); 10269 10270 auto *CS = cast<CapturedStmt>(AStmt); 10271 // 1.2.2 OpenMP Language Terminology 10272 // Structured block - An executable statement with a single entry at the 10273 // top and a single exit at the bottom. 10274 // The point of exit cannot be a branch out of the structured block. 10275 // longjmp() and throw() must not violate the entry/exit criteria. 10276 CS->getCapturedDecl()->setNothrow(); 10277 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel); 10278 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10279 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10280 // 1.2.2 OpenMP Language Terminology 10281 // Structured block - An executable statement with a single entry at the 10282 // top and a single exit at the bottom. 10283 // The point of exit cannot be a branch out of the structured block. 10284 // longjmp() and throw() must not violate the entry/exit criteria. 10285 CS->getCapturedDecl()->setNothrow(); 10286 } 10287 10288 setFunctionHasBranchProtectedScope(); 10289 10290 return OMPTargetParallelDirective::Create( 10291 Context, StartLoc, EndLoc, Clauses, AStmt, 10292 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 10293 } 10294 10295 StmtResult Sema::ActOnOpenMPTargetParallelForDirective( 10296 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10297 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10298 if (!AStmt) 10299 return StmtError(); 10300 10301 auto *CS = cast<CapturedStmt>(AStmt); 10302 // 1.2.2 OpenMP Language Terminology 10303 // Structured block - An executable statement with a single entry at the 10304 // top and a single exit at the bottom. 10305 // The point of exit cannot be a branch out of the structured block. 10306 // longjmp() and throw() must not violate the entry/exit criteria. 10307 CS->getCapturedDecl()->setNothrow(); 10308 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for); 10309 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10310 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10311 // 1.2.2 OpenMP Language Terminology 10312 // Structured block - An executable statement with a single entry at the 10313 // top and a single exit at the bottom. 10314 // The point of exit cannot be a branch out of the structured block. 10315 // longjmp() and throw() must not violate the entry/exit criteria. 10316 CS->getCapturedDecl()->setNothrow(); 10317 } 10318 10319 OMPLoopDirective::HelperExprs B; 10320 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 10321 // define the nested loops number. 10322 unsigned NestedLoopCount = 10323 checkOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses), 10324 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 10325 VarsWithImplicitDSA, B); 10326 if (NestedLoopCount == 0) 10327 return StmtError(); 10328 10329 assert((CurContext->isDependentContext() || B.builtAll()) && 10330 "omp target parallel for loop exprs were not built"); 10331 10332 if (!CurContext->isDependentContext()) { 10333 // Finalize the clauses that need pre-built expressions for CodeGen. 10334 for (OMPClause *C : Clauses) { 10335 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 10336 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 10337 B.NumIterations, *this, CurScope, 10338 DSAStack)) 10339 return StmtError(); 10340 } 10341 } 10342 10343 setFunctionHasBranchProtectedScope(); 10344 return OMPTargetParallelForDirective::Create( 10345 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 10346 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 10347 } 10348 10349 /// Check for existence of a map clause in the list of clauses. 10350 static bool hasClauses(ArrayRef<OMPClause *> Clauses, 10351 const OpenMPClauseKind K) { 10352 return llvm::any_of( 10353 Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; }); 10354 } 10355 10356 template <typename... Params> 10357 static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K, 10358 const Params... ClauseTypes) { 10359 return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...); 10360 } 10361 10362 StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses, 10363 Stmt *AStmt, 10364 SourceLocation StartLoc, 10365 SourceLocation EndLoc) { 10366 if (!AStmt) 10367 return StmtError(); 10368 10369 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10370 10371 // OpenMP [2.12.2, target data Construct, Restrictions] 10372 // At least one map, use_device_addr or use_device_ptr clause must appear on 10373 // the directive. 10374 if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr) && 10375 (LangOpts.OpenMP < 50 || !hasClauses(Clauses, OMPC_use_device_addr))) { 10376 StringRef Expected; 10377 if (LangOpts.OpenMP < 50) 10378 Expected = "'map' or 'use_device_ptr'"; 10379 else 10380 Expected = "'map', 'use_device_ptr', or 'use_device_addr'"; 10381 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 10382 << Expected << getOpenMPDirectiveName(OMPD_target_data); 10383 return StmtError(); 10384 } 10385 10386 setFunctionHasBranchProtectedScope(); 10387 10388 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 10389 AStmt); 10390 } 10391 10392 StmtResult 10393 Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses, 10394 SourceLocation StartLoc, 10395 SourceLocation EndLoc, Stmt *AStmt) { 10396 if (!AStmt) 10397 return StmtError(); 10398 10399 auto *CS = cast<CapturedStmt>(AStmt); 10400 // 1.2.2 OpenMP Language Terminology 10401 // Structured block - An executable statement with a single entry at the 10402 // top and a single exit at the bottom. 10403 // The point of exit cannot be a branch out of the structured block. 10404 // longjmp() and throw() must not violate the entry/exit criteria. 10405 CS->getCapturedDecl()->setNothrow(); 10406 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_enter_data); 10407 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10408 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10409 // 1.2.2 OpenMP Language Terminology 10410 // Structured block - An executable statement with a single entry at the 10411 // top and a single exit at the bottom. 10412 // The point of exit cannot be a branch out of the structured block. 10413 // longjmp() and throw() must not violate the entry/exit criteria. 10414 CS->getCapturedDecl()->setNothrow(); 10415 } 10416 10417 // OpenMP [2.10.2, Restrictions, p. 99] 10418 // At least one map clause must appear on the directive. 10419 if (!hasClauses(Clauses, OMPC_map)) { 10420 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 10421 << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data); 10422 return StmtError(); 10423 } 10424 10425 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 10426 AStmt); 10427 } 10428 10429 StmtResult 10430 Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses, 10431 SourceLocation StartLoc, 10432 SourceLocation EndLoc, Stmt *AStmt) { 10433 if (!AStmt) 10434 return StmtError(); 10435 10436 auto *CS = cast<CapturedStmt>(AStmt); 10437 // 1.2.2 OpenMP Language Terminology 10438 // Structured block - An executable statement with a single entry at the 10439 // top and a single exit at the bottom. 10440 // The point of exit cannot be a branch out of the structured block. 10441 // longjmp() and throw() must not violate the entry/exit criteria. 10442 CS->getCapturedDecl()->setNothrow(); 10443 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_exit_data); 10444 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10445 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10446 // 1.2.2 OpenMP Language Terminology 10447 // Structured block - An executable statement with a single entry at the 10448 // top and a single exit at the bottom. 10449 // The point of exit cannot be a branch out of the structured block. 10450 // longjmp() and throw() must not violate the entry/exit criteria. 10451 CS->getCapturedDecl()->setNothrow(); 10452 } 10453 10454 // OpenMP [2.10.3, Restrictions, p. 102] 10455 // At least one map clause must appear on the directive. 10456 if (!hasClauses(Clauses, OMPC_map)) { 10457 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 10458 << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data); 10459 return StmtError(); 10460 } 10461 10462 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 10463 AStmt); 10464 } 10465 10466 StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses, 10467 SourceLocation StartLoc, 10468 SourceLocation EndLoc, 10469 Stmt *AStmt) { 10470 if (!AStmt) 10471 return StmtError(); 10472 10473 auto *CS = cast<CapturedStmt>(AStmt); 10474 // 1.2.2 OpenMP Language Terminology 10475 // Structured block - An executable statement with a single entry at the 10476 // top and a single exit at the bottom. 10477 // The point of exit cannot be a branch out of the structured block. 10478 // longjmp() and throw() must not violate the entry/exit criteria. 10479 CS->getCapturedDecl()->setNothrow(); 10480 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_update); 10481 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10482 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10483 // 1.2.2 OpenMP Language Terminology 10484 // Structured block - An executable statement with a single entry at the 10485 // top and a single exit at the bottom. 10486 // The point of exit cannot be a branch out of the structured block. 10487 // longjmp() and throw() must not violate the entry/exit criteria. 10488 CS->getCapturedDecl()->setNothrow(); 10489 } 10490 10491 if (!hasClauses(Clauses, OMPC_to, OMPC_from)) { 10492 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required); 10493 return StmtError(); 10494 } 10495 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses, 10496 AStmt); 10497 } 10498 10499 StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses, 10500 Stmt *AStmt, SourceLocation StartLoc, 10501 SourceLocation EndLoc) { 10502 if (!AStmt) 10503 return StmtError(); 10504 10505 auto *CS = cast<CapturedStmt>(AStmt); 10506 // 1.2.2 OpenMP Language Terminology 10507 // Structured block - An executable statement with a single entry at the 10508 // top and a single exit at the bottom. 10509 // The point of exit cannot be a branch out of the structured block. 10510 // longjmp() and throw() must not violate the entry/exit criteria. 10511 CS->getCapturedDecl()->setNothrow(); 10512 10513 setFunctionHasBranchProtectedScope(); 10514 10515 DSAStack->setParentTeamsRegionLoc(StartLoc); 10516 10517 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 10518 } 10519 10520 StmtResult 10521 Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc, 10522 SourceLocation EndLoc, 10523 OpenMPDirectiveKind CancelRegion) { 10524 if (DSAStack->isParentNowaitRegion()) { 10525 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0; 10526 return StmtError(); 10527 } 10528 if (DSAStack->isParentOrderedRegion()) { 10529 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0; 10530 return StmtError(); 10531 } 10532 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc, 10533 CancelRegion); 10534 } 10535 10536 StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses, 10537 SourceLocation StartLoc, 10538 SourceLocation EndLoc, 10539 OpenMPDirectiveKind CancelRegion) { 10540 if (DSAStack->isParentNowaitRegion()) { 10541 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1; 10542 return StmtError(); 10543 } 10544 if (DSAStack->isParentOrderedRegion()) { 10545 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1; 10546 return StmtError(); 10547 } 10548 DSAStack->setParentCancelRegion(/*Cancel=*/true); 10549 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses, 10550 CancelRegion); 10551 } 10552 10553 static bool checkGrainsizeNumTasksClauses(Sema &S, 10554 ArrayRef<OMPClause *> Clauses) { 10555 const OMPClause *PrevClause = nullptr; 10556 bool ErrorFound = false; 10557 for (const OMPClause *C : Clauses) { 10558 if (C->getClauseKind() == OMPC_grainsize || 10559 C->getClauseKind() == OMPC_num_tasks) { 10560 if (!PrevClause) 10561 PrevClause = C; 10562 else if (PrevClause->getClauseKind() != C->getClauseKind()) { 10563 S.Diag(C->getBeginLoc(), diag::err_omp_clauses_mutually_exclusive) 10564 << getOpenMPClauseName(C->getClauseKind()) 10565 << getOpenMPClauseName(PrevClause->getClauseKind()); 10566 S.Diag(PrevClause->getBeginLoc(), diag::note_omp_previous_clause) 10567 << getOpenMPClauseName(PrevClause->getClauseKind()); 10568 ErrorFound = true; 10569 } 10570 } 10571 } 10572 return ErrorFound; 10573 } 10574 10575 static bool checkReductionClauseWithNogroup(Sema &S, 10576 ArrayRef<OMPClause *> Clauses) { 10577 const OMPClause *ReductionClause = nullptr; 10578 const OMPClause *NogroupClause = nullptr; 10579 for (const OMPClause *C : Clauses) { 10580 if (C->getClauseKind() == OMPC_reduction) { 10581 ReductionClause = C; 10582 if (NogroupClause) 10583 break; 10584 continue; 10585 } 10586 if (C->getClauseKind() == OMPC_nogroup) { 10587 NogroupClause = C; 10588 if (ReductionClause) 10589 break; 10590 continue; 10591 } 10592 } 10593 if (ReductionClause && NogroupClause) { 10594 S.Diag(ReductionClause->getBeginLoc(), diag::err_omp_reduction_with_nogroup) 10595 << SourceRange(NogroupClause->getBeginLoc(), 10596 NogroupClause->getEndLoc()); 10597 return true; 10598 } 10599 return false; 10600 } 10601 10602 StmtResult Sema::ActOnOpenMPTaskLoopDirective( 10603 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10604 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10605 if (!AStmt) 10606 return StmtError(); 10607 10608 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10609 OMPLoopDirective::HelperExprs B; 10610 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 10611 // define the nested loops number. 10612 unsigned NestedLoopCount = 10613 checkOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses), 10614 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 10615 VarsWithImplicitDSA, B); 10616 if (NestedLoopCount == 0) 10617 return StmtError(); 10618 10619 assert((CurContext->isDependentContext() || B.builtAll()) && 10620 "omp for loop exprs were not built"); 10621 10622 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10623 // The grainsize clause and num_tasks clause are mutually exclusive and may 10624 // not appear on the same taskloop directive. 10625 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 10626 return StmtError(); 10627 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10628 // If a reduction clause is present on the taskloop directive, the nogroup 10629 // clause must not be specified. 10630 if (checkReductionClauseWithNogroup(*this, Clauses)) 10631 return StmtError(); 10632 10633 setFunctionHasBranchProtectedScope(); 10634 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc, 10635 NestedLoopCount, Clauses, AStmt, B, 10636 DSAStack->isCancelRegion()); 10637 } 10638 10639 StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective( 10640 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10641 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10642 if (!AStmt) 10643 return StmtError(); 10644 10645 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10646 OMPLoopDirective::HelperExprs B; 10647 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 10648 // define the nested loops number. 10649 unsigned NestedLoopCount = 10650 checkOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses), 10651 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 10652 VarsWithImplicitDSA, B); 10653 if (NestedLoopCount == 0) 10654 return StmtError(); 10655 10656 assert((CurContext->isDependentContext() || B.builtAll()) && 10657 "omp for loop exprs were not built"); 10658 10659 if (!CurContext->isDependentContext()) { 10660 // Finalize the clauses that need pre-built expressions for CodeGen. 10661 for (OMPClause *C : Clauses) { 10662 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 10663 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 10664 B.NumIterations, *this, CurScope, 10665 DSAStack)) 10666 return StmtError(); 10667 } 10668 } 10669 10670 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10671 // The grainsize clause and num_tasks clause are mutually exclusive and may 10672 // not appear on the same taskloop directive. 10673 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 10674 return StmtError(); 10675 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10676 // If a reduction clause is present on the taskloop directive, the nogroup 10677 // clause must not be specified. 10678 if (checkReductionClauseWithNogroup(*this, Clauses)) 10679 return StmtError(); 10680 if (checkSimdlenSafelenSpecified(*this, Clauses)) 10681 return StmtError(); 10682 10683 setFunctionHasBranchProtectedScope(); 10684 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc, 10685 NestedLoopCount, Clauses, AStmt, B); 10686 } 10687 10688 StmtResult Sema::ActOnOpenMPMasterTaskLoopDirective( 10689 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10690 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10691 if (!AStmt) 10692 return StmtError(); 10693 10694 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10695 OMPLoopDirective::HelperExprs B; 10696 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 10697 // define the nested loops number. 10698 unsigned NestedLoopCount = 10699 checkOpenMPLoop(OMPD_master_taskloop, getCollapseNumberExpr(Clauses), 10700 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 10701 VarsWithImplicitDSA, B); 10702 if (NestedLoopCount == 0) 10703 return StmtError(); 10704 10705 assert((CurContext->isDependentContext() || B.builtAll()) && 10706 "omp for loop exprs were not built"); 10707 10708 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10709 // The grainsize clause and num_tasks clause are mutually exclusive and may 10710 // not appear on the same taskloop directive. 10711 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 10712 return StmtError(); 10713 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10714 // If a reduction clause is present on the taskloop directive, the nogroup 10715 // clause must not be specified. 10716 if (checkReductionClauseWithNogroup(*this, Clauses)) 10717 return StmtError(); 10718 10719 setFunctionHasBranchProtectedScope(); 10720 return OMPMasterTaskLoopDirective::Create(Context, StartLoc, EndLoc, 10721 NestedLoopCount, Clauses, AStmt, B, 10722 DSAStack->isCancelRegion()); 10723 } 10724 10725 StmtResult Sema::ActOnOpenMPMasterTaskLoopSimdDirective( 10726 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10727 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10728 if (!AStmt) 10729 return StmtError(); 10730 10731 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10732 OMPLoopDirective::HelperExprs B; 10733 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 10734 // define the nested loops number. 10735 unsigned NestedLoopCount = 10736 checkOpenMPLoop(OMPD_master_taskloop_simd, getCollapseNumberExpr(Clauses), 10737 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 10738 VarsWithImplicitDSA, B); 10739 if (NestedLoopCount == 0) 10740 return StmtError(); 10741 10742 assert((CurContext->isDependentContext() || B.builtAll()) && 10743 "omp for loop exprs were not built"); 10744 10745 if (!CurContext->isDependentContext()) { 10746 // Finalize the clauses that need pre-built expressions for CodeGen. 10747 for (OMPClause *C : Clauses) { 10748 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 10749 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 10750 B.NumIterations, *this, CurScope, 10751 DSAStack)) 10752 return StmtError(); 10753 } 10754 } 10755 10756 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10757 // The grainsize clause and num_tasks clause are mutually exclusive and may 10758 // not appear on the same taskloop directive. 10759 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 10760 return StmtError(); 10761 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10762 // If a reduction clause is present on the taskloop directive, the nogroup 10763 // clause must not be specified. 10764 if (checkReductionClauseWithNogroup(*this, Clauses)) 10765 return StmtError(); 10766 if (checkSimdlenSafelenSpecified(*this, Clauses)) 10767 return StmtError(); 10768 10769 setFunctionHasBranchProtectedScope(); 10770 return OMPMasterTaskLoopSimdDirective::Create( 10771 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 10772 } 10773 10774 StmtResult Sema::ActOnOpenMPParallelMasterTaskLoopDirective( 10775 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10776 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10777 if (!AStmt) 10778 return StmtError(); 10779 10780 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10781 auto *CS = cast<CapturedStmt>(AStmt); 10782 // 1.2.2 OpenMP Language Terminology 10783 // Structured block - An executable statement with a single entry at the 10784 // top and a single exit at the bottom. 10785 // The point of exit cannot be a branch out of the structured block. 10786 // longjmp() and throw() must not violate the entry/exit criteria. 10787 CS->getCapturedDecl()->setNothrow(); 10788 for (int ThisCaptureLevel = 10789 getOpenMPCaptureLevels(OMPD_parallel_master_taskloop); 10790 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10791 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10792 // 1.2.2 OpenMP Language Terminology 10793 // Structured block - An executable statement with a single entry at the 10794 // top and a single exit at the bottom. 10795 // The point of exit cannot be a branch out of the structured block. 10796 // longjmp() and throw() must not violate the entry/exit criteria. 10797 CS->getCapturedDecl()->setNothrow(); 10798 } 10799 10800 OMPLoopDirective::HelperExprs B; 10801 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 10802 // define the nested loops number. 10803 unsigned NestedLoopCount = checkOpenMPLoop( 10804 OMPD_parallel_master_taskloop, getCollapseNumberExpr(Clauses), 10805 /*OrderedLoopCountExpr=*/nullptr, CS, *this, *DSAStack, 10806 VarsWithImplicitDSA, B); 10807 if (NestedLoopCount == 0) 10808 return StmtError(); 10809 10810 assert((CurContext->isDependentContext() || B.builtAll()) && 10811 "omp for loop exprs were not built"); 10812 10813 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10814 // The grainsize clause and num_tasks clause are mutually exclusive and may 10815 // not appear on the same taskloop directive. 10816 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 10817 return StmtError(); 10818 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10819 // If a reduction clause is present on the taskloop directive, the nogroup 10820 // clause must not be specified. 10821 if (checkReductionClauseWithNogroup(*this, Clauses)) 10822 return StmtError(); 10823 10824 setFunctionHasBranchProtectedScope(); 10825 return OMPParallelMasterTaskLoopDirective::Create( 10826 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 10827 DSAStack->isCancelRegion()); 10828 } 10829 10830 StmtResult Sema::ActOnOpenMPParallelMasterTaskLoopSimdDirective( 10831 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10832 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10833 if (!AStmt) 10834 return StmtError(); 10835 10836 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10837 auto *CS = cast<CapturedStmt>(AStmt); 10838 // 1.2.2 OpenMP Language Terminology 10839 // Structured block - An executable statement with a single entry at the 10840 // top and a single exit at the bottom. 10841 // The point of exit cannot be a branch out of the structured block. 10842 // longjmp() and throw() must not violate the entry/exit criteria. 10843 CS->getCapturedDecl()->setNothrow(); 10844 for (int ThisCaptureLevel = 10845 getOpenMPCaptureLevels(OMPD_parallel_master_taskloop_simd); 10846 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10847 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10848 // 1.2.2 OpenMP Language Terminology 10849 // Structured block - An executable statement with a single entry at the 10850 // top and a single exit at the bottom. 10851 // The point of exit cannot be a branch out of the structured block. 10852 // longjmp() and throw() must not violate the entry/exit criteria. 10853 CS->getCapturedDecl()->setNothrow(); 10854 } 10855 10856 OMPLoopDirective::HelperExprs B; 10857 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 10858 // define the nested loops number. 10859 unsigned NestedLoopCount = checkOpenMPLoop( 10860 OMPD_parallel_master_taskloop_simd, getCollapseNumberExpr(Clauses), 10861 /*OrderedLoopCountExpr=*/nullptr, CS, *this, *DSAStack, 10862 VarsWithImplicitDSA, B); 10863 if (NestedLoopCount == 0) 10864 return StmtError(); 10865 10866 assert((CurContext->isDependentContext() || B.builtAll()) && 10867 "omp for loop exprs were not built"); 10868 10869 if (!CurContext->isDependentContext()) { 10870 // Finalize the clauses that need pre-built expressions for CodeGen. 10871 for (OMPClause *C : Clauses) { 10872 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 10873 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 10874 B.NumIterations, *this, CurScope, 10875 DSAStack)) 10876 return StmtError(); 10877 } 10878 } 10879 10880 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10881 // The grainsize clause and num_tasks clause are mutually exclusive and may 10882 // not appear on the same taskloop directive. 10883 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 10884 return StmtError(); 10885 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10886 // If a reduction clause is present on the taskloop directive, the nogroup 10887 // clause must not be specified. 10888 if (checkReductionClauseWithNogroup(*this, Clauses)) 10889 return StmtError(); 10890 if (checkSimdlenSafelenSpecified(*this, Clauses)) 10891 return StmtError(); 10892 10893 setFunctionHasBranchProtectedScope(); 10894 return OMPParallelMasterTaskLoopSimdDirective::Create( 10895 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 10896 } 10897 10898 StmtResult Sema::ActOnOpenMPDistributeDirective( 10899 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10900 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10901 if (!AStmt) 10902 return StmtError(); 10903 10904 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10905 OMPLoopDirective::HelperExprs B; 10906 // In presence of clause 'collapse' with number of loops, it will 10907 // define the nested loops number. 10908 unsigned NestedLoopCount = 10909 checkOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses), 10910 nullptr /*ordered not a clause on distribute*/, AStmt, 10911 *this, *DSAStack, VarsWithImplicitDSA, B); 10912 if (NestedLoopCount == 0) 10913 return StmtError(); 10914 10915 assert((CurContext->isDependentContext() || B.builtAll()) && 10916 "omp for loop exprs were not built"); 10917 10918 setFunctionHasBranchProtectedScope(); 10919 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc, 10920 NestedLoopCount, Clauses, AStmt, B); 10921 } 10922 10923 StmtResult Sema::ActOnOpenMPDistributeParallelForDirective( 10924 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10925 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10926 if (!AStmt) 10927 return StmtError(); 10928 10929 auto *CS = cast<CapturedStmt>(AStmt); 10930 // 1.2.2 OpenMP Language Terminology 10931 // Structured block - An executable statement with a single entry at the 10932 // top and a single exit at the bottom. 10933 // The point of exit cannot be a branch out of the structured block. 10934 // longjmp() and throw() must not violate the entry/exit criteria. 10935 CS->getCapturedDecl()->setNothrow(); 10936 for (int ThisCaptureLevel = 10937 getOpenMPCaptureLevels(OMPD_distribute_parallel_for); 10938 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10939 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10940 // 1.2.2 OpenMP Language Terminology 10941 // Structured block - An executable statement with a single entry at the 10942 // top and a single exit at the bottom. 10943 // The point of exit cannot be a branch out of the structured block. 10944 // longjmp() and throw() must not violate the entry/exit criteria. 10945 CS->getCapturedDecl()->setNothrow(); 10946 } 10947 10948 OMPLoopDirective::HelperExprs B; 10949 // In presence of clause 'collapse' with number of loops, it will 10950 // define the nested loops number. 10951 unsigned NestedLoopCount = checkOpenMPLoop( 10952 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses), 10953 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 10954 VarsWithImplicitDSA, B); 10955 if (NestedLoopCount == 0) 10956 return StmtError(); 10957 10958 assert((CurContext->isDependentContext() || B.builtAll()) && 10959 "omp for loop exprs were not built"); 10960 10961 setFunctionHasBranchProtectedScope(); 10962 return OMPDistributeParallelForDirective::Create( 10963 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 10964 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 10965 } 10966 10967 StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective( 10968 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10969 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10970 if (!AStmt) 10971 return StmtError(); 10972 10973 auto *CS = cast<CapturedStmt>(AStmt); 10974 // 1.2.2 OpenMP Language Terminology 10975 // Structured block - An executable statement with a single entry at the 10976 // top and a single exit at the bottom. 10977 // The point of exit cannot be a branch out of the structured block. 10978 // longjmp() and throw() must not violate the entry/exit criteria. 10979 CS->getCapturedDecl()->setNothrow(); 10980 for (int ThisCaptureLevel = 10981 getOpenMPCaptureLevels(OMPD_distribute_parallel_for_simd); 10982 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10983 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10984 // 1.2.2 OpenMP Language Terminology 10985 // Structured block - An executable statement with a single entry at the 10986 // top and a single exit at the bottom. 10987 // The point of exit cannot be a branch out of the structured block. 10988 // longjmp() and throw() must not violate the entry/exit criteria. 10989 CS->getCapturedDecl()->setNothrow(); 10990 } 10991 10992 OMPLoopDirective::HelperExprs B; 10993 // In presence of clause 'collapse' with number of loops, it will 10994 // define the nested loops number. 10995 unsigned NestedLoopCount = checkOpenMPLoop( 10996 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses), 10997 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 10998 VarsWithImplicitDSA, B); 10999 if (NestedLoopCount == 0) 11000 return StmtError(); 11001 11002 assert((CurContext->isDependentContext() || B.builtAll()) && 11003 "omp for loop exprs were not built"); 11004 11005 if (!CurContext->isDependentContext()) { 11006 // Finalize the clauses that need pre-built expressions for CodeGen. 11007 for (OMPClause *C : Clauses) { 11008 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11009 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11010 B.NumIterations, *this, CurScope, 11011 DSAStack)) 11012 return StmtError(); 11013 } 11014 } 11015 11016 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11017 return StmtError(); 11018 11019 setFunctionHasBranchProtectedScope(); 11020 return OMPDistributeParallelForSimdDirective::Create( 11021 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 11022 } 11023 11024 StmtResult Sema::ActOnOpenMPDistributeSimdDirective( 11025 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11026 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11027 if (!AStmt) 11028 return StmtError(); 11029 11030 auto *CS = cast<CapturedStmt>(AStmt); 11031 // 1.2.2 OpenMP Language Terminology 11032 // Structured block - An executable statement with a single entry at the 11033 // top and a single exit at the bottom. 11034 // The point of exit cannot be a branch out of the structured block. 11035 // longjmp() and throw() must not violate the entry/exit criteria. 11036 CS->getCapturedDecl()->setNothrow(); 11037 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_distribute_simd); 11038 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11039 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11040 // 1.2.2 OpenMP Language Terminology 11041 // Structured block - An executable statement with a single entry at the 11042 // top and a single exit at the bottom. 11043 // The point of exit cannot be a branch out of the structured block. 11044 // longjmp() and throw() must not violate the entry/exit criteria. 11045 CS->getCapturedDecl()->setNothrow(); 11046 } 11047 11048 OMPLoopDirective::HelperExprs B; 11049 // In presence of clause 'collapse' with number of loops, it will 11050 // define the nested loops number. 11051 unsigned NestedLoopCount = 11052 checkOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses), 11053 nullptr /*ordered not a clause on distribute*/, CS, *this, 11054 *DSAStack, VarsWithImplicitDSA, B); 11055 if (NestedLoopCount == 0) 11056 return StmtError(); 11057 11058 assert((CurContext->isDependentContext() || B.builtAll()) && 11059 "omp for loop exprs were not built"); 11060 11061 if (!CurContext->isDependentContext()) { 11062 // Finalize the clauses that need pre-built expressions for CodeGen. 11063 for (OMPClause *C : Clauses) { 11064 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11065 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11066 B.NumIterations, *this, CurScope, 11067 DSAStack)) 11068 return StmtError(); 11069 } 11070 } 11071 11072 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11073 return StmtError(); 11074 11075 setFunctionHasBranchProtectedScope(); 11076 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc, 11077 NestedLoopCount, Clauses, AStmt, B); 11078 } 11079 11080 StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective( 11081 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11082 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11083 if (!AStmt) 11084 return StmtError(); 11085 11086 auto *CS = cast<CapturedStmt>(AStmt); 11087 // 1.2.2 OpenMP Language Terminology 11088 // Structured block - An executable statement with a single entry at the 11089 // top and a single exit at the bottom. 11090 // The point of exit cannot be a branch out of the structured block. 11091 // longjmp() and throw() must not violate the entry/exit criteria. 11092 CS->getCapturedDecl()->setNothrow(); 11093 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for); 11094 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11095 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11096 // 1.2.2 OpenMP Language Terminology 11097 // Structured block - An executable statement with a single entry at the 11098 // top and a single exit at the bottom. 11099 // The point of exit cannot be a branch out of the structured block. 11100 // longjmp() and throw() must not violate the entry/exit criteria. 11101 CS->getCapturedDecl()->setNothrow(); 11102 } 11103 11104 OMPLoopDirective::HelperExprs B; 11105 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11106 // define the nested loops number. 11107 unsigned NestedLoopCount = checkOpenMPLoop( 11108 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses), 11109 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 11110 VarsWithImplicitDSA, B); 11111 if (NestedLoopCount == 0) 11112 return StmtError(); 11113 11114 assert((CurContext->isDependentContext() || B.builtAll()) && 11115 "omp target parallel for simd loop exprs were not built"); 11116 11117 if (!CurContext->isDependentContext()) { 11118 // Finalize the clauses that need pre-built expressions for CodeGen. 11119 for (OMPClause *C : Clauses) { 11120 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11121 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11122 B.NumIterations, *this, CurScope, 11123 DSAStack)) 11124 return StmtError(); 11125 } 11126 } 11127 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11128 return StmtError(); 11129 11130 setFunctionHasBranchProtectedScope(); 11131 return OMPTargetParallelForSimdDirective::Create( 11132 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 11133 } 11134 11135 StmtResult Sema::ActOnOpenMPTargetSimdDirective( 11136 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11137 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11138 if (!AStmt) 11139 return StmtError(); 11140 11141 auto *CS = cast<CapturedStmt>(AStmt); 11142 // 1.2.2 OpenMP Language Terminology 11143 // Structured block - An executable statement with a single entry at the 11144 // top and a single exit at the bottom. 11145 // The point of exit cannot be a branch out of the structured block. 11146 // longjmp() and throw() must not violate the entry/exit criteria. 11147 CS->getCapturedDecl()->setNothrow(); 11148 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_simd); 11149 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11150 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11151 // 1.2.2 OpenMP Language Terminology 11152 // Structured block - An executable statement with a single entry at the 11153 // top and a single exit at the bottom. 11154 // The point of exit cannot be a branch out of the structured block. 11155 // longjmp() and throw() must not violate the entry/exit criteria. 11156 CS->getCapturedDecl()->setNothrow(); 11157 } 11158 11159 OMPLoopDirective::HelperExprs B; 11160 // In presence of clause 'collapse' with number of loops, it will define the 11161 // nested loops number. 11162 unsigned NestedLoopCount = 11163 checkOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses), 11164 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 11165 VarsWithImplicitDSA, B); 11166 if (NestedLoopCount == 0) 11167 return StmtError(); 11168 11169 assert((CurContext->isDependentContext() || B.builtAll()) && 11170 "omp target simd loop exprs were not built"); 11171 11172 if (!CurContext->isDependentContext()) { 11173 // Finalize the clauses that need pre-built expressions for CodeGen. 11174 for (OMPClause *C : Clauses) { 11175 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11176 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11177 B.NumIterations, *this, CurScope, 11178 DSAStack)) 11179 return StmtError(); 11180 } 11181 } 11182 11183 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11184 return StmtError(); 11185 11186 setFunctionHasBranchProtectedScope(); 11187 return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc, 11188 NestedLoopCount, Clauses, AStmt, B); 11189 } 11190 11191 StmtResult Sema::ActOnOpenMPTeamsDistributeDirective( 11192 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11193 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11194 if (!AStmt) 11195 return StmtError(); 11196 11197 auto *CS = cast<CapturedStmt>(AStmt); 11198 // 1.2.2 OpenMP Language Terminology 11199 // Structured block - An executable statement with a single entry at the 11200 // top and a single exit at the bottom. 11201 // The point of exit cannot be a branch out of the structured block. 11202 // longjmp() and throw() must not violate the entry/exit criteria. 11203 CS->getCapturedDecl()->setNothrow(); 11204 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_teams_distribute); 11205 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11206 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11207 // 1.2.2 OpenMP Language Terminology 11208 // Structured block - An executable statement with a single entry at the 11209 // top and a single exit at the bottom. 11210 // The point of exit cannot be a branch out of the structured block. 11211 // longjmp() and throw() must not violate the entry/exit criteria. 11212 CS->getCapturedDecl()->setNothrow(); 11213 } 11214 11215 OMPLoopDirective::HelperExprs B; 11216 // In presence of clause 'collapse' with number of loops, it will 11217 // define the nested loops number. 11218 unsigned NestedLoopCount = 11219 checkOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses), 11220 nullptr /*ordered not a clause on distribute*/, CS, *this, 11221 *DSAStack, VarsWithImplicitDSA, B); 11222 if (NestedLoopCount == 0) 11223 return StmtError(); 11224 11225 assert((CurContext->isDependentContext() || B.builtAll()) && 11226 "omp teams distribute loop exprs were not built"); 11227 11228 setFunctionHasBranchProtectedScope(); 11229 11230 DSAStack->setParentTeamsRegionLoc(StartLoc); 11231 11232 return OMPTeamsDistributeDirective::Create( 11233 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 11234 } 11235 11236 StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective( 11237 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11238 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11239 if (!AStmt) 11240 return StmtError(); 11241 11242 auto *CS = cast<CapturedStmt>(AStmt); 11243 // 1.2.2 OpenMP Language Terminology 11244 // Structured block - An executable statement with a single entry at the 11245 // top and a single exit at the bottom. 11246 // The point of exit cannot be a branch out of the structured block. 11247 // longjmp() and throw() must not violate the entry/exit criteria. 11248 CS->getCapturedDecl()->setNothrow(); 11249 for (int ThisCaptureLevel = 11250 getOpenMPCaptureLevels(OMPD_teams_distribute_simd); 11251 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11252 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11253 // 1.2.2 OpenMP Language Terminology 11254 // Structured block - An executable statement with a single entry at the 11255 // top and a single exit at the bottom. 11256 // The point of exit cannot be a branch out of the structured block. 11257 // longjmp() and throw() must not violate the entry/exit criteria. 11258 CS->getCapturedDecl()->setNothrow(); 11259 } 11260 11261 OMPLoopDirective::HelperExprs B; 11262 // In presence of clause 'collapse' with number of loops, it will 11263 // define the nested loops number. 11264 unsigned NestedLoopCount = checkOpenMPLoop( 11265 OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses), 11266 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 11267 VarsWithImplicitDSA, B); 11268 11269 if (NestedLoopCount == 0) 11270 return StmtError(); 11271 11272 assert((CurContext->isDependentContext() || B.builtAll()) && 11273 "omp teams distribute simd loop exprs were not built"); 11274 11275 if (!CurContext->isDependentContext()) { 11276 // Finalize the clauses that need pre-built expressions for CodeGen. 11277 for (OMPClause *C : Clauses) { 11278 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11279 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11280 B.NumIterations, *this, CurScope, 11281 DSAStack)) 11282 return StmtError(); 11283 } 11284 } 11285 11286 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11287 return StmtError(); 11288 11289 setFunctionHasBranchProtectedScope(); 11290 11291 DSAStack->setParentTeamsRegionLoc(StartLoc); 11292 11293 return OMPTeamsDistributeSimdDirective::Create( 11294 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 11295 } 11296 11297 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective( 11298 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11299 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11300 if (!AStmt) 11301 return StmtError(); 11302 11303 auto *CS = cast<CapturedStmt>(AStmt); 11304 // 1.2.2 OpenMP Language Terminology 11305 // Structured block - An executable statement with a single entry at the 11306 // top and a single exit at the bottom. 11307 // The point of exit cannot be a branch out of the structured block. 11308 // longjmp() and throw() must not violate the entry/exit criteria. 11309 CS->getCapturedDecl()->setNothrow(); 11310 11311 for (int ThisCaptureLevel = 11312 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for_simd); 11313 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11314 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11315 // 1.2.2 OpenMP Language Terminology 11316 // Structured block - An executable statement with a single entry at the 11317 // top and a single exit at the bottom. 11318 // The point of exit cannot be a branch out of the structured block. 11319 // longjmp() and throw() must not violate the entry/exit criteria. 11320 CS->getCapturedDecl()->setNothrow(); 11321 } 11322 11323 OMPLoopDirective::HelperExprs B; 11324 // In presence of clause 'collapse' with number of loops, it will 11325 // define the nested loops number. 11326 unsigned NestedLoopCount = checkOpenMPLoop( 11327 OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses), 11328 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 11329 VarsWithImplicitDSA, B); 11330 11331 if (NestedLoopCount == 0) 11332 return StmtError(); 11333 11334 assert((CurContext->isDependentContext() || B.builtAll()) && 11335 "omp for loop exprs were not built"); 11336 11337 if (!CurContext->isDependentContext()) { 11338 // Finalize the clauses that need pre-built expressions for CodeGen. 11339 for (OMPClause *C : Clauses) { 11340 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11341 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11342 B.NumIterations, *this, CurScope, 11343 DSAStack)) 11344 return StmtError(); 11345 } 11346 } 11347 11348 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11349 return StmtError(); 11350 11351 setFunctionHasBranchProtectedScope(); 11352 11353 DSAStack->setParentTeamsRegionLoc(StartLoc); 11354 11355 return OMPTeamsDistributeParallelForSimdDirective::Create( 11356 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 11357 } 11358 11359 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective( 11360 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11361 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11362 if (!AStmt) 11363 return StmtError(); 11364 11365 auto *CS = cast<CapturedStmt>(AStmt); 11366 // 1.2.2 OpenMP Language Terminology 11367 // Structured block - An executable statement with a single entry at the 11368 // top and a single exit at the bottom. 11369 // The point of exit cannot be a branch out of the structured block. 11370 // longjmp() and throw() must not violate the entry/exit criteria. 11371 CS->getCapturedDecl()->setNothrow(); 11372 11373 for (int ThisCaptureLevel = 11374 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for); 11375 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11376 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11377 // 1.2.2 OpenMP Language Terminology 11378 // Structured block - An executable statement with a single entry at the 11379 // top and a single exit at the bottom. 11380 // The point of exit cannot be a branch out of the structured block. 11381 // longjmp() and throw() must not violate the entry/exit criteria. 11382 CS->getCapturedDecl()->setNothrow(); 11383 } 11384 11385 OMPLoopDirective::HelperExprs B; 11386 // In presence of clause 'collapse' with number of loops, it will 11387 // define the nested loops number. 11388 unsigned NestedLoopCount = checkOpenMPLoop( 11389 OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses), 11390 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 11391 VarsWithImplicitDSA, B); 11392 11393 if (NestedLoopCount == 0) 11394 return StmtError(); 11395 11396 assert((CurContext->isDependentContext() || B.builtAll()) && 11397 "omp for loop exprs were not built"); 11398 11399 setFunctionHasBranchProtectedScope(); 11400 11401 DSAStack->setParentTeamsRegionLoc(StartLoc); 11402 11403 return OMPTeamsDistributeParallelForDirective::Create( 11404 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 11405 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 11406 } 11407 11408 StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses, 11409 Stmt *AStmt, 11410 SourceLocation StartLoc, 11411 SourceLocation EndLoc) { 11412 if (!AStmt) 11413 return StmtError(); 11414 11415 auto *CS = cast<CapturedStmt>(AStmt); 11416 // 1.2.2 OpenMP Language Terminology 11417 // Structured block - An executable statement with a single entry at the 11418 // top and a single exit at the bottom. 11419 // The point of exit cannot be a branch out of the structured block. 11420 // longjmp() and throw() must not violate the entry/exit criteria. 11421 CS->getCapturedDecl()->setNothrow(); 11422 11423 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_teams); 11424 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11425 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11426 // 1.2.2 OpenMP Language Terminology 11427 // Structured block - An executable statement with a single entry at the 11428 // top and a single exit at the bottom. 11429 // The point of exit cannot be a branch out of the structured block. 11430 // longjmp() and throw() must not violate the entry/exit criteria. 11431 CS->getCapturedDecl()->setNothrow(); 11432 } 11433 setFunctionHasBranchProtectedScope(); 11434 11435 return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, 11436 AStmt); 11437 } 11438 11439 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective( 11440 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11441 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11442 if (!AStmt) 11443 return StmtError(); 11444 11445 auto *CS = cast<CapturedStmt>(AStmt); 11446 // 1.2.2 OpenMP Language Terminology 11447 // Structured block - An executable statement with a single entry at the 11448 // top and a single exit at the bottom. 11449 // The point of exit cannot be a branch out of the structured block. 11450 // longjmp() and throw() must not violate the entry/exit criteria. 11451 CS->getCapturedDecl()->setNothrow(); 11452 for (int ThisCaptureLevel = 11453 getOpenMPCaptureLevels(OMPD_target_teams_distribute); 11454 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11455 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11456 // 1.2.2 OpenMP Language Terminology 11457 // Structured block - An executable statement with a single entry at the 11458 // top and a single exit at the bottom. 11459 // The point of exit cannot be a branch out of the structured block. 11460 // longjmp() and throw() must not violate the entry/exit criteria. 11461 CS->getCapturedDecl()->setNothrow(); 11462 } 11463 11464 OMPLoopDirective::HelperExprs B; 11465 // In presence of clause 'collapse' with number of loops, it will 11466 // define the nested loops number. 11467 unsigned NestedLoopCount = checkOpenMPLoop( 11468 OMPD_target_teams_distribute, getCollapseNumberExpr(Clauses), 11469 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 11470 VarsWithImplicitDSA, B); 11471 if (NestedLoopCount == 0) 11472 return StmtError(); 11473 11474 assert((CurContext->isDependentContext() || B.builtAll()) && 11475 "omp target teams distribute loop exprs were not built"); 11476 11477 setFunctionHasBranchProtectedScope(); 11478 return OMPTargetTeamsDistributeDirective::Create( 11479 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 11480 } 11481 11482 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective( 11483 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11484 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11485 if (!AStmt) 11486 return StmtError(); 11487 11488 auto *CS = cast<CapturedStmt>(AStmt); 11489 // 1.2.2 OpenMP Language Terminology 11490 // Structured block - An executable statement with a single entry at the 11491 // top and a single exit at the bottom. 11492 // The point of exit cannot be a branch out of the structured block. 11493 // longjmp() and throw() must not violate the entry/exit criteria. 11494 CS->getCapturedDecl()->setNothrow(); 11495 for (int ThisCaptureLevel = 11496 getOpenMPCaptureLevels(OMPD_target_teams_distribute_parallel_for); 11497 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11498 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11499 // 1.2.2 OpenMP Language Terminology 11500 // Structured block - An executable statement with a single entry at the 11501 // top and a single exit at the bottom. 11502 // The point of exit cannot be a branch out of the structured block. 11503 // longjmp() and throw() must not violate the entry/exit criteria. 11504 CS->getCapturedDecl()->setNothrow(); 11505 } 11506 11507 OMPLoopDirective::HelperExprs B; 11508 // In presence of clause 'collapse' with number of loops, it will 11509 // define the nested loops number. 11510 unsigned NestedLoopCount = checkOpenMPLoop( 11511 OMPD_target_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses), 11512 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 11513 VarsWithImplicitDSA, B); 11514 if (NestedLoopCount == 0) 11515 return StmtError(); 11516 11517 assert((CurContext->isDependentContext() || B.builtAll()) && 11518 "omp target teams distribute parallel for loop exprs were not built"); 11519 11520 if (!CurContext->isDependentContext()) { 11521 // Finalize the clauses that need pre-built expressions for CodeGen. 11522 for (OMPClause *C : Clauses) { 11523 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11524 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11525 B.NumIterations, *this, CurScope, 11526 DSAStack)) 11527 return StmtError(); 11528 } 11529 } 11530 11531 setFunctionHasBranchProtectedScope(); 11532 return OMPTargetTeamsDistributeParallelForDirective::Create( 11533 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 11534 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 11535 } 11536 11537 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective( 11538 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11539 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11540 if (!AStmt) 11541 return StmtError(); 11542 11543 auto *CS = cast<CapturedStmt>(AStmt); 11544 // 1.2.2 OpenMP Language Terminology 11545 // Structured block - An executable statement with a single entry at the 11546 // top and a single exit at the bottom. 11547 // The point of exit cannot be a branch out of the structured block. 11548 // longjmp() and throw() must not violate the entry/exit criteria. 11549 CS->getCapturedDecl()->setNothrow(); 11550 for (int ThisCaptureLevel = getOpenMPCaptureLevels( 11551 OMPD_target_teams_distribute_parallel_for_simd); 11552 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11553 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11554 // 1.2.2 OpenMP Language Terminology 11555 // Structured block - An executable statement with a single entry at the 11556 // top and a single exit at the bottom. 11557 // The point of exit cannot be a branch out of the structured block. 11558 // longjmp() and throw() must not violate the entry/exit criteria. 11559 CS->getCapturedDecl()->setNothrow(); 11560 } 11561 11562 OMPLoopDirective::HelperExprs B; 11563 // In presence of clause 'collapse' with number of loops, it will 11564 // define the nested loops number. 11565 unsigned NestedLoopCount = 11566 checkOpenMPLoop(OMPD_target_teams_distribute_parallel_for_simd, 11567 getCollapseNumberExpr(Clauses), 11568 nullptr /*ordered not a clause on distribute*/, CS, *this, 11569 *DSAStack, VarsWithImplicitDSA, B); 11570 if (NestedLoopCount == 0) 11571 return StmtError(); 11572 11573 assert((CurContext->isDependentContext() || B.builtAll()) && 11574 "omp target teams distribute parallel for simd loop exprs were not " 11575 "built"); 11576 11577 if (!CurContext->isDependentContext()) { 11578 // Finalize the clauses that need pre-built expressions for CodeGen. 11579 for (OMPClause *C : Clauses) { 11580 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11581 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11582 B.NumIterations, *this, CurScope, 11583 DSAStack)) 11584 return StmtError(); 11585 } 11586 } 11587 11588 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11589 return StmtError(); 11590 11591 setFunctionHasBranchProtectedScope(); 11592 return OMPTargetTeamsDistributeParallelForSimdDirective::Create( 11593 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 11594 } 11595 11596 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective( 11597 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11598 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11599 if (!AStmt) 11600 return StmtError(); 11601 11602 auto *CS = cast<CapturedStmt>(AStmt); 11603 // 1.2.2 OpenMP Language Terminology 11604 // Structured block - An executable statement with a single entry at the 11605 // top and a single exit at the bottom. 11606 // The point of exit cannot be a branch out of the structured block. 11607 // longjmp() and throw() must not violate the entry/exit criteria. 11608 CS->getCapturedDecl()->setNothrow(); 11609 for (int ThisCaptureLevel = 11610 getOpenMPCaptureLevels(OMPD_target_teams_distribute_simd); 11611 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11612 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11613 // 1.2.2 OpenMP Language Terminology 11614 // Structured block - An executable statement with a single entry at the 11615 // top and a single exit at the bottom. 11616 // The point of exit cannot be a branch out of the structured block. 11617 // longjmp() and throw() must not violate the entry/exit criteria. 11618 CS->getCapturedDecl()->setNothrow(); 11619 } 11620 11621 OMPLoopDirective::HelperExprs B; 11622 // In presence of clause 'collapse' with number of loops, it will 11623 // define the nested loops number. 11624 unsigned NestedLoopCount = checkOpenMPLoop( 11625 OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses), 11626 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 11627 VarsWithImplicitDSA, B); 11628 if (NestedLoopCount == 0) 11629 return StmtError(); 11630 11631 assert((CurContext->isDependentContext() || B.builtAll()) && 11632 "omp target teams distribute simd loop exprs were not built"); 11633 11634 if (!CurContext->isDependentContext()) { 11635 // Finalize the clauses that need pre-built expressions for CodeGen. 11636 for (OMPClause *C : Clauses) { 11637 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11638 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11639 B.NumIterations, *this, CurScope, 11640 DSAStack)) 11641 return StmtError(); 11642 } 11643 } 11644 11645 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11646 return StmtError(); 11647 11648 setFunctionHasBranchProtectedScope(); 11649 return OMPTargetTeamsDistributeSimdDirective::Create( 11650 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 11651 } 11652 11653 OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr, 11654 SourceLocation StartLoc, 11655 SourceLocation LParenLoc, 11656 SourceLocation EndLoc) { 11657 OMPClause *Res = nullptr; 11658 switch (Kind) { 11659 case OMPC_final: 11660 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc); 11661 break; 11662 case OMPC_num_threads: 11663 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc); 11664 break; 11665 case OMPC_safelen: 11666 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc); 11667 break; 11668 case OMPC_simdlen: 11669 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc); 11670 break; 11671 case OMPC_allocator: 11672 Res = ActOnOpenMPAllocatorClause(Expr, StartLoc, LParenLoc, EndLoc); 11673 break; 11674 case OMPC_collapse: 11675 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc); 11676 break; 11677 case OMPC_ordered: 11678 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr); 11679 break; 11680 case OMPC_num_teams: 11681 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc); 11682 break; 11683 case OMPC_thread_limit: 11684 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc); 11685 break; 11686 case OMPC_priority: 11687 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc); 11688 break; 11689 case OMPC_grainsize: 11690 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc); 11691 break; 11692 case OMPC_num_tasks: 11693 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc); 11694 break; 11695 case OMPC_hint: 11696 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc); 11697 break; 11698 case OMPC_depobj: 11699 Res = ActOnOpenMPDepobjClause(Expr, StartLoc, LParenLoc, EndLoc); 11700 break; 11701 case OMPC_detach: 11702 Res = ActOnOpenMPDetachClause(Expr, StartLoc, LParenLoc, EndLoc); 11703 break; 11704 case OMPC_device: 11705 case OMPC_if: 11706 case OMPC_default: 11707 case OMPC_proc_bind: 11708 case OMPC_schedule: 11709 case OMPC_private: 11710 case OMPC_firstprivate: 11711 case OMPC_lastprivate: 11712 case OMPC_shared: 11713 case OMPC_reduction: 11714 case OMPC_task_reduction: 11715 case OMPC_in_reduction: 11716 case OMPC_linear: 11717 case OMPC_aligned: 11718 case OMPC_copyin: 11719 case OMPC_copyprivate: 11720 case OMPC_nowait: 11721 case OMPC_untied: 11722 case OMPC_mergeable: 11723 case OMPC_threadprivate: 11724 case OMPC_allocate: 11725 case OMPC_flush: 11726 case OMPC_read: 11727 case OMPC_write: 11728 case OMPC_update: 11729 case OMPC_capture: 11730 case OMPC_seq_cst: 11731 case OMPC_acq_rel: 11732 case OMPC_acquire: 11733 case OMPC_release: 11734 case OMPC_relaxed: 11735 case OMPC_depend: 11736 case OMPC_threads: 11737 case OMPC_simd: 11738 case OMPC_map: 11739 case OMPC_nogroup: 11740 case OMPC_dist_schedule: 11741 case OMPC_defaultmap: 11742 case OMPC_unknown: 11743 case OMPC_uniform: 11744 case OMPC_to: 11745 case OMPC_from: 11746 case OMPC_use_device_ptr: 11747 case OMPC_use_device_addr: 11748 case OMPC_is_device_ptr: 11749 case OMPC_unified_address: 11750 case OMPC_unified_shared_memory: 11751 case OMPC_reverse_offload: 11752 case OMPC_dynamic_allocators: 11753 case OMPC_atomic_default_mem_order: 11754 case OMPC_device_type: 11755 case OMPC_match: 11756 case OMPC_nontemporal: 11757 case OMPC_order: 11758 case OMPC_destroy: 11759 case OMPC_inclusive: 11760 case OMPC_exclusive: 11761 case OMPC_uses_allocators: 11762 case OMPC_affinity: 11763 default: 11764 llvm_unreachable("Clause is not allowed."); 11765 } 11766 return Res; 11767 } 11768 11769 // An OpenMP directive such as 'target parallel' has two captured regions: 11770 // for the 'target' and 'parallel' respectively. This function returns 11771 // the region in which to capture expressions associated with a clause. 11772 // A return value of OMPD_unknown signifies that the expression should not 11773 // be captured. 11774 static OpenMPDirectiveKind getOpenMPCaptureRegionForClause( 11775 OpenMPDirectiveKind DKind, OpenMPClauseKind CKind, unsigned OpenMPVersion, 11776 OpenMPDirectiveKind NameModifier = OMPD_unknown) { 11777 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 11778 switch (CKind) { 11779 case OMPC_if: 11780 switch (DKind) { 11781 case OMPD_target_parallel_for_simd: 11782 if (OpenMPVersion >= 50 && 11783 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) { 11784 CaptureRegion = OMPD_parallel; 11785 break; 11786 } 11787 LLVM_FALLTHROUGH; 11788 case OMPD_target_parallel: 11789 case OMPD_target_parallel_for: 11790 // If this clause applies to the nested 'parallel' region, capture within 11791 // the 'target' region, otherwise do not capture. 11792 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel) 11793 CaptureRegion = OMPD_target; 11794 break; 11795 case OMPD_target_teams_distribute_parallel_for_simd: 11796 if (OpenMPVersion >= 50 && 11797 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) { 11798 CaptureRegion = OMPD_parallel; 11799 break; 11800 } 11801 LLVM_FALLTHROUGH; 11802 case OMPD_target_teams_distribute_parallel_for: 11803 // If this clause applies to the nested 'parallel' region, capture within 11804 // the 'teams' region, otherwise do not capture. 11805 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel) 11806 CaptureRegion = OMPD_teams; 11807 break; 11808 case OMPD_teams_distribute_parallel_for_simd: 11809 if (OpenMPVersion >= 50 && 11810 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) { 11811 CaptureRegion = OMPD_parallel; 11812 break; 11813 } 11814 LLVM_FALLTHROUGH; 11815 case OMPD_teams_distribute_parallel_for: 11816 CaptureRegion = OMPD_teams; 11817 break; 11818 case OMPD_target_update: 11819 case OMPD_target_enter_data: 11820 case OMPD_target_exit_data: 11821 CaptureRegion = OMPD_task; 11822 break; 11823 case OMPD_parallel_master_taskloop: 11824 if (NameModifier == OMPD_unknown || NameModifier == OMPD_taskloop) 11825 CaptureRegion = OMPD_parallel; 11826 break; 11827 case OMPD_parallel_master_taskloop_simd: 11828 if ((OpenMPVersion <= 45 && NameModifier == OMPD_unknown) || 11829 NameModifier == OMPD_taskloop) { 11830 CaptureRegion = OMPD_parallel; 11831 break; 11832 } 11833 if (OpenMPVersion <= 45) 11834 break; 11835 if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd) 11836 CaptureRegion = OMPD_taskloop; 11837 break; 11838 case OMPD_parallel_for_simd: 11839 if (OpenMPVersion <= 45) 11840 break; 11841 if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd) 11842 CaptureRegion = OMPD_parallel; 11843 break; 11844 case OMPD_taskloop_simd: 11845 case OMPD_master_taskloop_simd: 11846 if (OpenMPVersion <= 45) 11847 break; 11848 if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd) 11849 CaptureRegion = OMPD_taskloop; 11850 break; 11851 case OMPD_distribute_parallel_for_simd: 11852 if (OpenMPVersion <= 45) 11853 break; 11854 if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd) 11855 CaptureRegion = OMPD_parallel; 11856 break; 11857 case OMPD_target_simd: 11858 if (OpenMPVersion >= 50 && 11859 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) 11860 CaptureRegion = OMPD_target; 11861 break; 11862 case OMPD_teams_distribute_simd: 11863 case OMPD_target_teams_distribute_simd: 11864 if (OpenMPVersion >= 50 && 11865 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) 11866 CaptureRegion = OMPD_teams; 11867 break; 11868 case OMPD_cancel: 11869 case OMPD_parallel: 11870 case OMPD_parallel_master: 11871 case OMPD_parallel_sections: 11872 case OMPD_parallel_for: 11873 case OMPD_target: 11874 case OMPD_target_teams: 11875 case OMPD_target_teams_distribute: 11876 case OMPD_distribute_parallel_for: 11877 case OMPD_task: 11878 case OMPD_taskloop: 11879 case OMPD_master_taskloop: 11880 case OMPD_target_data: 11881 case OMPD_simd: 11882 case OMPD_for_simd: 11883 case OMPD_distribute_simd: 11884 // Do not capture if-clause expressions. 11885 break; 11886 case OMPD_threadprivate: 11887 case OMPD_allocate: 11888 case OMPD_taskyield: 11889 case OMPD_barrier: 11890 case OMPD_taskwait: 11891 case OMPD_cancellation_point: 11892 case OMPD_flush: 11893 case OMPD_depobj: 11894 case OMPD_scan: 11895 case OMPD_declare_reduction: 11896 case OMPD_declare_mapper: 11897 case OMPD_declare_simd: 11898 case OMPD_declare_variant: 11899 case OMPD_begin_declare_variant: 11900 case OMPD_end_declare_variant: 11901 case OMPD_declare_target: 11902 case OMPD_end_declare_target: 11903 case OMPD_teams: 11904 case OMPD_for: 11905 case OMPD_sections: 11906 case OMPD_section: 11907 case OMPD_single: 11908 case OMPD_master: 11909 case OMPD_critical: 11910 case OMPD_taskgroup: 11911 case OMPD_distribute: 11912 case OMPD_ordered: 11913 case OMPD_atomic: 11914 case OMPD_teams_distribute: 11915 case OMPD_requires: 11916 llvm_unreachable("Unexpected OpenMP directive with if-clause"); 11917 case OMPD_unknown: 11918 default: 11919 llvm_unreachable("Unknown OpenMP directive"); 11920 } 11921 break; 11922 case OMPC_num_threads: 11923 switch (DKind) { 11924 case OMPD_target_parallel: 11925 case OMPD_target_parallel_for: 11926 case OMPD_target_parallel_for_simd: 11927 CaptureRegion = OMPD_target; 11928 break; 11929 case OMPD_teams_distribute_parallel_for: 11930 case OMPD_teams_distribute_parallel_for_simd: 11931 case OMPD_target_teams_distribute_parallel_for: 11932 case OMPD_target_teams_distribute_parallel_for_simd: 11933 CaptureRegion = OMPD_teams; 11934 break; 11935 case OMPD_parallel: 11936 case OMPD_parallel_master: 11937 case OMPD_parallel_sections: 11938 case OMPD_parallel_for: 11939 case OMPD_parallel_for_simd: 11940 case OMPD_distribute_parallel_for: 11941 case OMPD_distribute_parallel_for_simd: 11942 case OMPD_parallel_master_taskloop: 11943 case OMPD_parallel_master_taskloop_simd: 11944 // Do not capture num_threads-clause expressions. 11945 break; 11946 case OMPD_target_data: 11947 case OMPD_target_enter_data: 11948 case OMPD_target_exit_data: 11949 case OMPD_target_update: 11950 case OMPD_target: 11951 case OMPD_target_simd: 11952 case OMPD_target_teams: 11953 case OMPD_target_teams_distribute: 11954 case OMPD_target_teams_distribute_simd: 11955 case OMPD_cancel: 11956 case OMPD_task: 11957 case OMPD_taskloop: 11958 case OMPD_taskloop_simd: 11959 case OMPD_master_taskloop: 11960 case OMPD_master_taskloop_simd: 11961 case OMPD_threadprivate: 11962 case OMPD_allocate: 11963 case OMPD_taskyield: 11964 case OMPD_barrier: 11965 case OMPD_taskwait: 11966 case OMPD_cancellation_point: 11967 case OMPD_flush: 11968 case OMPD_depobj: 11969 case OMPD_scan: 11970 case OMPD_declare_reduction: 11971 case OMPD_declare_mapper: 11972 case OMPD_declare_simd: 11973 case OMPD_declare_variant: 11974 case OMPD_begin_declare_variant: 11975 case OMPD_end_declare_variant: 11976 case OMPD_declare_target: 11977 case OMPD_end_declare_target: 11978 case OMPD_teams: 11979 case OMPD_simd: 11980 case OMPD_for: 11981 case OMPD_for_simd: 11982 case OMPD_sections: 11983 case OMPD_section: 11984 case OMPD_single: 11985 case OMPD_master: 11986 case OMPD_critical: 11987 case OMPD_taskgroup: 11988 case OMPD_distribute: 11989 case OMPD_ordered: 11990 case OMPD_atomic: 11991 case OMPD_distribute_simd: 11992 case OMPD_teams_distribute: 11993 case OMPD_teams_distribute_simd: 11994 case OMPD_requires: 11995 llvm_unreachable("Unexpected OpenMP directive with num_threads-clause"); 11996 case OMPD_unknown: 11997 default: 11998 llvm_unreachable("Unknown OpenMP directive"); 11999 } 12000 break; 12001 case OMPC_num_teams: 12002 switch (DKind) { 12003 case OMPD_target_teams: 12004 case OMPD_target_teams_distribute: 12005 case OMPD_target_teams_distribute_simd: 12006 case OMPD_target_teams_distribute_parallel_for: 12007 case OMPD_target_teams_distribute_parallel_for_simd: 12008 CaptureRegion = OMPD_target; 12009 break; 12010 case OMPD_teams_distribute_parallel_for: 12011 case OMPD_teams_distribute_parallel_for_simd: 12012 case OMPD_teams: 12013 case OMPD_teams_distribute: 12014 case OMPD_teams_distribute_simd: 12015 // Do not capture num_teams-clause expressions. 12016 break; 12017 case OMPD_distribute_parallel_for: 12018 case OMPD_distribute_parallel_for_simd: 12019 case OMPD_task: 12020 case OMPD_taskloop: 12021 case OMPD_taskloop_simd: 12022 case OMPD_master_taskloop: 12023 case OMPD_master_taskloop_simd: 12024 case OMPD_parallel_master_taskloop: 12025 case OMPD_parallel_master_taskloop_simd: 12026 case OMPD_target_data: 12027 case OMPD_target_enter_data: 12028 case OMPD_target_exit_data: 12029 case OMPD_target_update: 12030 case OMPD_cancel: 12031 case OMPD_parallel: 12032 case OMPD_parallel_master: 12033 case OMPD_parallel_sections: 12034 case OMPD_parallel_for: 12035 case OMPD_parallel_for_simd: 12036 case OMPD_target: 12037 case OMPD_target_simd: 12038 case OMPD_target_parallel: 12039 case OMPD_target_parallel_for: 12040 case OMPD_target_parallel_for_simd: 12041 case OMPD_threadprivate: 12042 case OMPD_allocate: 12043 case OMPD_taskyield: 12044 case OMPD_barrier: 12045 case OMPD_taskwait: 12046 case OMPD_cancellation_point: 12047 case OMPD_flush: 12048 case OMPD_depobj: 12049 case OMPD_scan: 12050 case OMPD_declare_reduction: 12051 case OMPD_declare_mapper: 12052 case OMPD_declare_simd: 12053 case OMPD_declare_variant: 12054 case OMPD_begin_declare_variant: 12055 case OMPD_end_declare_variant: 12056 case OMPD_declare_target: 12057 case OMPD_end_declare_target: 12058 case OMPD_simd: 12059 case OMPD_for: 12060 case OMPD_for_simd: 12061 case OMPD_sections: 12062 case OMPD_section: 12063 case OMPD_single: 12064 case OMPD_master: 12065 case OMPD_critical: 12066 case OMPD_taskgroup: 12067 case OMPD_distribute: 12068 case OMPD_ordered: 12069 case OMPD_atomic: 12070 case OMPD_distribute_simd: 12071 case OMPD_requires: 12072 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause"); 12073 case OMPD_unknown: 12074 default: 12075 llvm_unreachable("Unknown OpenMP directive"); 12076 } 12077 break; 12078 case OMPC_thread_limit: 12079 switch (DKind) { 12080 case OMPD_target_teams: 12081 case OMPD_target_teams_distribute: 12082 case OMPD_target_teams_distribute_simd: 12083 case OMPD_target_teams_distribute_parallel_for: 12084 case OMPD_target_teams_distribute_parallel_for_simd: 12085 CaptureRegion = OMPD_target; 12086 break; 12087 case OMPD_teams_distribute_parallel_for: 12088 case OMPD_teams_distribute_parallel_for_simd: 12089 case OMPD_teams: 12090 case OMPD_teams_distribute: 12091 case OMPD_teams_distribute_simd: 12092 // Do not capture thread_limit-clause expressions. 12093 break; 12094 case OMPD_distribute_parallel_for: 12095 case OMPD_distribute_parallel_for_simd: 12096 case OMPD_task: 12097 case OMPD_taskloop: 12098 case OMPD_taskloop_simd: 12099 case OMPD_master_taskloop: 12100 case OMPD_master_taskloop_simd: 12101 case OMPD_parallel_master_taskloop: 12102 case OMPD_parallel_master_taskloop_simd: 12103 case OMPD_target_data: 12104 case OMPD_target_enter_data: 12105 case OMPD_target_exit_data: 12106 case OMPD_target_update: 12107 case OMPD_cancel: 12108 case OMPD_parallel: 12109 case OMPD_parallel_master: 12110 case OMPD_parallel_sections: 12111 case OMPD_parallel_for: 12112 case OMPD_parallel_for_simd: 12113 case OMPD_target: 12114 case OMPD_target_simd: 12115 case OMPD_target_parallel: 12116 case OMPD_target_parallel_for: 12117 case OMPD_target_parallel_for_simd: 12118 case OMPD_threadprivate: 12119 case OMPD_allocate: 12120 case OMPD_taskyield: 12121 case OMPD_barrier: 12122 case OMPD_taskwait: 12123 case OMPD_cancellation_point: 12124 case OMPD_flush: 12125 case OMPD_depobj: 12126 case OMPD_scan: 12127 case OMPD_declare_reduction: 12128 case OMPD_declare_mapper: 12129 case OMPD_declare_simd: 12130 case OMPD_declare_variant: 12131 case OMPD_begin_declare_variant: 12132 case OMPD_end_declare_variant: 12133 case OMPD_declare_target: 12134 case OMPD_end_declare_target: 12135 case OMPD_simd: 12136 case OMPD_for: 12137 case OMPD_for_simd: 12138 case OMPD_sections: 12139 case OMPD_section: 12140 case OMPD_single: 12141 case OMPD_master: 12142 case OMPD_critical: 12143 case OMPD_taskgroup: 12144 case OMPD_distribute: 12145 case OMPD_ordered: 12146 case OMPD_atomic: 12147 case OMPD_distribute_simd: 12148 case OMPD_requires: 12149 llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause"); 12150 case OMPD_unknown: 12151 default: 12152 llvm_unreachable("Unknown OpenMP directive"); 12153 } 12154 break; 12155 case OMPC_schedule: 12156 switch (DKind) { 12157 case OMPD_parallel_for: 12158 case OMPD_parallel_for_simd: 12159 case OMPD_distribute_parallel_for: 12160 case OMPD_distribute_parallel_for_simd: 12161 case OMPD_teams_distribute_parallel_for: 12162 case OMPD_teams_distribute_parallel_for_simd: 12163 case OMPD_target_parallel_for: 12164 case OMPD_target_parallel_for_simd: 12165 case OMPD_target_teams_distribute_parallel_for: 12166 case OMPD_target_teams_distribute_parallel_for_simd: 12167 CaptureRegion = OMPD_parallel; 12168 break; 12169 case OMPD_for: 12170 case OMPD_for_simd: 12171 // Do not capture schedule-clause expressions. 12172 break; 12173 case OMPD_task: 12174 case OMPD_taskloop: 12175 case OMPD_taskloop_simd: 12176 case OMPD_master_taskloop: 12177 case OMPD_master_taskloop_simd: 12178 case OMPD_parallel_master_taskloop: 12179 case OMPD_parallel_master_taskloop_simd: 12180 case OMPD_target_data: 12181 case OMPD_target_enter_data: 12182 case OMPD_target_exit_data: 12183 case OMPD_target_update: 12184 case OMPD_teams: 12185 case OMPD_teams_distribute: 12186 case OMPD_teams_distribute_simd: 12187 case OMPD_target_teams_distribute: 12188 case OMPD_target_teams_distribute_simd: 12189 case OMPD_target: 12190 case OMPD_target_simd: 12191 case OMPD_target_parallel: 12192 case OMPD_cancel: 12193 case OMPD_parallel: 12194 case OMPD_parallel_master: 12195 case OMPD_parallel_sections: 12196 case OMPD_threadprivate: 12197 case OMPD_allocate: 12198 case OMPD_taskyield: 12199 case OMPD_barrier: 12200 case OMPD_taskwait: 12201 case OMPD_cancellation_point: 12202 case OMPD_flush: 12203 case OMPD_depobj: 12204 case OMPD_scan: 12205 case OMPD_declare_reduction: 12206 case OMPD_declare_mapper: 12207 case OMPD_declare_simd: 12208 case OMPD_declare_variant: 12209 case OMPD_begin_declare_variant: 12210 case OMPD_end_declare_variant: 12211 case OMPD_declare_target: 12212 case OMPD_end_declare_target: 12213 case OMPD_simd: 12214 case OMPD_sections: 12215 case OMPD_section: 12216 case OMPD_single: 12217 case OMPD_master: 12218 case OMPD_critical: 12219 case OMPD_taskgroup: 12220 case OMPD_distribute: 12221 case OMPD_ordered: 12222 case OMPD_atomic: 12223 case OMPD_distribute_simd: 12224 case OMPD_target_teams: 12225 case OMPD_requires: 12226 llvm_unreachable("Unexpected OpenMP directive with schedule clause"); 12227 case OMPD_unknown: 12228 default: 12229 llvm_unreachable("Unknown OpenMP directive"); 12230 } 12231 break; 12232 case OMPC_dist_schedule: 12233 switch (DKind) { 12234 case OMPD_teams_distribute_parallel_for: 12235 case OMPD_teams_distribute_parallel_for_simd: 12236 case OMPD_teams_distribute: 12237 case OMPD_teams_distribute_simd: 12238 case OMPD_target_teams_distribute_parallel_for: 12239 case OMPD_target_teams_distribute_parallel_for_simd: 12240 case OMPD_target_teams_distribute: 12241 case OMPD_target_teams_distribute_simd: 12242 CaptureRegion = OMPD_teams; 12243 break; 12244 case OMPD_distribute_parallel_for: 12245 case OMPD_distribute_parallel_for_simd: 12246 case OMPD_distribute: 12247 case OMPD_distribute_simd: 12248 // Do not capture thread_limit-clause expressions. 12249 break; 12250 case OMPD_parallel_for: 12251 case OMPD_parallel_for_simd: 12252 case OMPD_target_parallel_for_simd: 12253 case OMPD_target_parallel_for: 12254 case OMPD_task: 12255 case OMPD_taskloop: 12256 case OMPD_taskloop_simd: 12257 case OMPD_master_taskloop: 12258 case OMPD_master_taskloop_simd: 12259 case OMPD_parallel_master_taskloop: 12260 case OMPD_parallel_master_taskloop_simd: 12261 case OMPD_target_data: 12262 case OMPD_target_enter_data: 12263 case OMPD_target_exit_data: 12264 case OMPD_target_update: 12265 case OMPD_teams: 12266 case OMPD_target: 12267 case OMPD_target_simd: 12268 case OMPD_target_parallel: 12269 case OMPD_cancel: 12270 case OMPD_parallel: 12271 case OMPD_parallel_master: 12272 case OMPD_parallel_sections: 12273 case OMPD_threadprivate: 12274 case OMPD_allocate: 12275 case OMPD_taskyield: 12276 case OMPD_barrier: 12277 case OMPD_taskwait: 12278 case OMPD_cancellation_point: 12279 case OMPD_flush: 12280 case OMPD_depobj: 12281 case OMPD_scan: 12282 case OMPD_declare_reduction: 12283 case OMPD_declare_mapper: 12284 case OMPD_declare_simd: 12285 case OMPD_declare_variant: 12286 case OMPD_begin_declare_variant: 12287 case OMPD_end_declare_variant: 12288 case OMPD_declare_target: 12289 case OMPD_end_declare_target: 12290 case OMPD_simd: 12291 case OMPD_for: 12292 case OMPD_for_simd: 12293 case OMPD_sections: 12294 case OMPD_section: 12295 case OMPD_single: 12296 case OMPD_master: 12297 case OMPD_critical: 12298 case OMPD_taskgroup: 12299 case OMPD_ordered: 12300 case OMPD_atomic: 12301 case OMPD_target_teams: 12302 case OMPD_requires: 12303 llvm_unreachable("Unexpected OpenMP directive with schedule clause"); 12304 case OMPD_unknown: 12305 default: 12306 llvm_unreachable("Unknown OpenMP directive"); 12307 } 12308 break; 12309 case OMPC_device: 12310 switch (DKind) { 12311 case OMPD_target_update: 12312 case OMPD_target_enter_data: 12313 case OMPD_target_exit_data: 12314 case OMPD_target: 12315 case OMPD_target_simd: 12316 case OMPD_target_teams: 12317 case OMPD_target_parallel: 12318 case OMPD_target_teams_distribute: 12319 case OMPD_target_teams_distribute_simd: 12320 case OMPD_target_parallel_for: 12321 case OMPD_target_parallel_for_simd: 12322 case OMPD_target_teams_distribute_parallel_for: 12323 case OMPD_target_teams_distribute_parallel_for_simd: 12324 CaptureRegion = OMPD_task; 12325 break; 12326 case OMPD_target_data: 12327 // Do not capture device-clause expressions. 12328 break; 12329 case OMPD_teams_distribute_parallel_for: 12330 case OMPD_teams_distribute_parallel_for_simd: 12331 case OMPD_teams: 12332 case OMPD_teams_distribute: 12333 case OMPD_teams_distribute_simd: 12334 case OMPD_distribute_parallel_for: 12335 case OMPD_distribute_parallel_for_simd: 12336 case OMPD_task: 12337 case OMPD_taskloop: 12338 case OMPD_taskloop_simd: 12339 case OMPD_master_taskloop: 12340 case OMPD_master_taskloop_simd: 12341 case OMPD_parallel_master_taskloop: 12342 case OMPD_parallel_master_taskloop_simd: 12343 case OMPD_cancel: 12344 case OMPD_parallel: 12345 case OMPD_parallel_master: 12346 case OMPD_parallel_sections: 12347 case OMPD_parallel_for: 12348 case OMPD_parallel_for_simd: 12349 case OMPD_threadprivate: 12350 case OMPD_allocate: 12351 case OMPD_taskyield: 12352 case OMPD_barrier: 12353 case OMPD_taskwait: 12354 case OMPD_cancellation_point: 12355 case OMPD_flush: 12356 case OMPD_depobj: 12357 case OMPD_scan: 12358 case OMPD_declare_reduction: 12359 case OMPD_declare_mapper: 12360 case OMPD_declare_simd: 12361 case OMPD_declare_variant: 12362 case OMPD_begin_declare_variant: 12363 case OMPD_end_declare_variant: 12364 case OMPD_declare_target: 12365 case OMPD_end_declare_target: 12366 case OMPD_simd: 12367 case OMPD_for: 12368 case OMPD_for_simd: 12369 case OMPD_sections: 12370 case OMPD_section: 12371 case OMPD_single: 12372 case OMPD_master: 12373 case OMPD_critical: 12374 case OMPD_taskgroup: 12375 case OMPD_distribute: 12376 case OMPD_ordered: 12377 case OMPD_atomic: 12378 case OMPD_distribute_simd: 12379 case OMPD_requires: 12380 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause"); 12381 case OMPD_unknown: 12382 default: 12383 llvm_unreachable("Unknown OpenMP directive"); 12384 } 12385 break; 12386 case OMPC_grainsize: 12387 case OMPC_num_tasks: 12388 case OMPC_final: 12389 case OMPC_priority: 12390 switch (DKind) { 12391 case OMPD_task: 12392 case OMPD_taskloop: 12393 case OMPD_taskloop_simd: 12394 case OMPD_master_taskloop: 12395 case OMPD_master_taskloop_simd: 12396 break; 12397 case OMPD_parallel_master_taskloop: 12398 case OMPD_parallel_master_taskloop_simd: 12399 CaptureRegion = OMPD_parallel; 12400 break; 12401 case OMPD_target_update: 12402 case OMPD_target_enter_data: 12403 case OMPD_target_exit_data: 12404 case OMPD_target: 12405 case OMPD_target_simd: 12406 case OMPD_target_teams: 12407 case OMPD_target_parallel: 12408 case OMPD_target_teams_distribute: 12409 case OMPD_target_teams_distribute_simd: 12410 case OMPD_target_parallel_for: 12411 case OMPD_target_parallel_for_simd: 12412 case OMPD_target_teams_distribute_parallel_for: 12413 case OMPD_target_teams_distribute_parallel_for_simd: 12414 case OMPD_target_data: 12415 case OMPD_teams_distribute_parallel_for: 12416 case OMPD_teams_distribute_parallel_for_simd: 12417 case OMPD_teams: 12418 case OMPD_teams_distribute: 12419 case OMPD_teams_distribute_simd: 12420 case OMPD_distribute_parallel_for: 12421 case OMPD_distribute_parallel_for_simd: 12422 case OMPD_cancel: 12423 case OMPD_parallel: 12424 case OMPD_parallel_master: 12425 case OMPD_parallel_sections: 12426 case OMPD_parallel_for: 12427 case OMPD_parallel_for_simd: 12428 case OMPD_threadprivate: 12429 case OMPD_allocate: 12430 case OMPD_taskyield: 12431 case OMPD_barrier: 12432 case OMPD_taskwait: 12433 case OMPD_cancellation_point: 12434 case OMPD_flush: 12435 case OMPD_depobj: 12436 case OMPD_scan: 12437 case OMPD_declare_reduction: 12438 case OMPD_declare_mapper: 12439 case OMPD_declare_simd: 12440 case OMPD_declare_variant: 12441 case OMPD_begin_declare_variant: 12442 case OMPD_end_declare_variant: 12443 case OMPD_declare_target: 12444 case OMPD_end_declare_target: 12445 case OMPD_simd: 12446 case OMPD_for: 12447 case OMPD_for_simd: 12448 case OMPD_sections: 12449 case OMPD_section: 12450 case OMPD_single: 12451 case OMPD_master: 12452 case OMPD_critical: 12453 case OMPD_taskgroup: 12454 case OMPD_distribute: 12455 case OMPD_ordered: 12456 case OMPD_atomic: 12457 case OMPD_distribute_simd: 12458 case OMPD_requires: 12459 llvm_unreachable("Unexpected OpenMP directive with grainsize-clause"); 12460 case OMPD_unknown: 12461 default: 12462 llvm_unreachable("Unknown OpenMP directive"); 12463 } 12464 break; 12465 case OMPC_firstprivate: 12466 case OMPC_lastprivate: 12467 case OMPC_reduction: 12468 case OMPC_task_reduction: 12469 case OMPC_in_reduction: 12470 case OMPC_linear: 12471 case OMPC_default: 12472 case OMPC_proc_bind: 12473 case OMPC_safelen: 12474 case OMPC_simdlen: 12475 case OMPC_allocator: 12476 case OMPC_collapse: 12477 case OMPC_private: 12478 case OMPC_shared: 12479 case OMPC_aligned: 12480 case OMPC_copyin: 12481 case OMPC_copyprivate: 12482 case OMPC_ordered: 12483 case OMPC_nowait: 12484 case OMPC_untied: 12485 case OMPC_mergeable: 12486 case OMPC_threadprivate: 12487 case OMPC_allocate: 12488 case OMPC_flush: 12489 case OMPC_depobj: 12490 case OMPC_read: 12491 case OMPC_write: 12492 case OMPC_update: 12493 case OMPC_capture: 12494 case OMPC_seq_cst: 12495 case OMPC_acq_rel: 12496 case OMPC_acquire: 12497 case OMPC_release: 12498 case OMPC_relaxed: 12499 case OMPC_depend: 12500 case OMPC_threads: 12501 case OMPC_simd: 12502 case OMPC_map: 12503 case OMPC_nogroup: 12504 case OMPC_hint: 12505 case OMPC_defaultmap: 12506 case OMPC_unknown: 12507 case OMPC_uniform: 12508 case OMPC_to: 12509 case OMPC_from: 12510 case OMPC_use_device_ptr: 12511 case OMPC_use_device_addr: 12512 case OMPC_is_device_ptr: 12513 case OMPC_unified_address: 12514 case OMPC_unified_shared_memory: 12515 case OMPC_reverse_offload: 12516 case OMPC_dynamic_allocators: 12517 case OMPC_atomic_default_mem_order: 12518 case OMPC_device_type: 12519 case OMPC_match: 12520 case OMPC_nontemporal: 12521 case OMPC_order: 12522 case OMPC_destroy: 12523 case OMPC_detach: 12524 case OMPC_inclusive: 12525 case OMPC_exclusive: 12526 case OMPC_uses_allocators: 12527 case OMPC_affinity: 12528 default: 12529 llvm_unreachable("Unexpected OpenMP clause."); 12530 } 12531 return CaptureRegion; 12532 } 12533 12534 OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier, 12535 Expr *Condition, SourceLocation StartLoc, 12536 SourceLocation LParenLoc, 12537 SourceLocation NameModifierLoc, 12538 SourceLocation ColonLoc, 12539 SourceLocation EndLoc) { 12540 Expr *ValExpr = Condition; 12541 Stmt *HelperValStmt = nullptr; 12542 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 12543 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 12544 !Condition->isInstantiationDependent() && 12545 !Condition->containsUnexpandedParameterPack()) { 12546 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 12547 if (Val.isInvalid()) 12548 return nullptr; 12549 12550 ValExpr = Val.get(); 12551 12552 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 12553 CaptureRegion = getOpenMPCaptureRegionForClause( 12554 DKind, OMPC_if, LangOpts.OpenMP, NameModifier); 12555 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 12556 ValExpr = MakeFullExpr(ValExpr).get(); 12557 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 12558 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 12559 HelperValStmt = buildPreInits(Context, Captures); 12560 } 12561 } 12562 12563 return new (Context) 12564 OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc, 12565 LParenLoc, NameModifierLoc, ColonLoc, EndLoc); 12566 } 12567 12568 OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition, 12569 SourceLocation StartLoc, 12570 SourceLocation LParenLoc, 12571 SourceLocation EndLoc) { 12572 Expr *ValExpr = Condition; 12573 Stmt *HelperValStmt = nullptr; 12574 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 12575 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 12576 !Condition->isInstantiationDependent() && 12577 !Condition->containsUnexpandedParameterPack()) { 12578 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 12579 if (Val.isInvalid()) 12580 return nullptr; 12581 12582 ValExpr = MakeFullExpr(Val.get()).get(); 12583 12584 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 12585 CaptureRegion = 12586 getOpenMPCaptureRegionForClause(DKind, OMPC_final, LangOpts.OpenMP); 12587 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 12588 ValExpr = MakeFullExpr(ValExpr).get(); 12589 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 12590 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 12591 HelperValStmt = buildPreInits(Context, Captures); 12592 } 12593 } 12594 12595 return new (Context) OMPFinalClause(ValExpr, HelperValStmt, CaptureRegion, 12596 StartLoc, LParenLoc, EndLoc); 12597 } 12598 12599 ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc, 12600 Expr *Op) { 12601 if (!Op) 12602 return ExprError(); 12603 12604 class IntConvertDiagnoser : public ICEConvertDiagnoser { 12605 public: 12606 IntConvertDiagnoser() 12607 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {} 12608 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, 12609 QualType T) override { 12610 return S.Diag(Loc, diag::err_omp_not_integral) << T; 12611 } 12612 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc, 12613 QualType T) override { 12614 return S.Diag(Loc, diag::err_omp_incomplete_type) << T; 12615 } 12616 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc, 12617 QualType T, 12618 QualType ConvTy) override { 12619 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy; 12620 } 12621 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv, 12622 QualType ConvTy) override { 12623 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 12624 << ConvTy->isEnumeralType() << ConvTy; 12625 } 12626 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc, 12627 QualType T) override { 12628 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T; 12629 } 12630 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv, 12631 QualType ConvTy) override { 12632 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 12633 << ConvTy->isEnumeralType() << ConvTy; 12634 } 12635 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType, 12636 QualType) override { 12637 llvm_unreachable("conversion functions are permitted"); 12638 } 12639 } ConvertDiagnoser; 12640 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser); 12641 } 12642 12643 static bool 12644 isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef, OpenMPClauseKind CKind, 12645 bool StrictlyPositive, bool BuildCapture = false, 12646 OpenMPDirectiveKind DKind = OMPD_unknown, 12647 OpenMPDirectiveKind *CaptureRegion = nullptr, 12648 Stmt **HelperValStmt = nullptr) { 12649 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() && 12650 !ValExpr->isInstantiationDependent()) { 12651 SourceLocation Loc = ValExpr->getExprLoc(); 12652 ExprResult Value = 12653 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr); 12654 if (Value.isInvalid()) 12655 return false; 12656 12657 ValExpr = Value.get(); 12658 // The expression must evaluate to a non-negative integer value. 12659 if (Optional<llvm::APSInt> Result = 12660 ValExpr->getIntegerConstantExpr(SemaRef.Context)) { 12661 if (Result->isSigned() && 12662 !((!StrictlyPositive && Result->isNonNegative()) || 12663 (StrictlyPositive && Result->isStrictlyPositive()))) { 12664 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause) 12665 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) 12666 << ValExpr->getSourceRange(); 12667 return false; 12668 } 12669 } 12670 if (!BuildCapture) 12671 return true; 12672 *CaptureRegion = 12673 getOpenMPCaptureRegionForClause(DKind, CKind, SemaRef.LangOpts.OpenMP); 12674 if (*CaptureRegion != OMPD_unknown && 12675 !SemaRef.CurContext->isDependentContext()) { 12676 ValExpr = SemaRef.MakeFullExpr(ValExpr).get(); 12677 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 12678 ValExpr = tryBuildCapture(SemaRef, ValExpr, Captures).get(); 12679 *HelperValStmt = buildPreInits(SemaRef.Context, Captures); 12680 } 12681 } 12682 return true; 12683 } 12684 12685 OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads, 12686 SourceLocation StartLoc, 12687 SourceLocation LParenLoc, 12688 SourceLocation EndLoc) { 12689 Expr *ValExpr = NumThreads; 12690 Stmt *HelperValStmt = nullptr; 12691 12692 // OpenMP [2.5, Restrictions] 12693 // The num_threads expression must evaluate to a positive integer value. 12694 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads, 12695 /*StrictlyPositive=*/true)) 12696 return nullptr; 12697 12698 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 12699 OpenMPDirectiveKind CaptureRegion = 12700 getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads, LangOpts.OpenMP); 12701 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 12702 ValExpr = MakeFullExpr(ValExpr).get(); 12703 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 12704 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 12705 HelperValStmt = buildPreInits(Context, Captures); 12706 } 12707 12708 return new (Context) OMPNumThreadsClause( 12709 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc); 12710 } 12711 12712 ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E, 12713 OpenMPClauseKind CKind, 12714 bool StrictlyPositive) { 12715 if (!E) 12716 return ExprError(); 12717 if (E->isValueDependent() || E->isTypeDependent() || 12718 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 12719 return E; 12720 llvm::APSInt Result; 12721 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result); 12722 if (ICE.isInvalid()) 12723 return ExprError(); 12724 if ((StrictlyPositive && !Result.isStrictlyPositive()) || 12725 (!StrictlyPositive && !Result.isNonNegative())) { 12726 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause) 12727 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) 12728 << E->getSourceRange(); 12729 return ExprError(); 12730 } 12731 if (CKind == OMPC_aligned && !Result.isPowerOf2()) { 12732 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two) 12733 << E->getSourceRange(); 12734 return ExprError(); 12735 } 12736 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1) 12737 DSAStack->setAssociatedLoops(Result.getExtValue()); 12738 else if (CKind == OMPC_ordered) 12739 DSAStack->setAssociatedLoops(Result.getExtValue()); 12740 return ICE; 12741 } 12742 12743 OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc, 12744 SourceLocation LParenLoc, 12745 SourceLocation EndLoc) { 12746 // OpenMP [2.8.1, simd construct, Description] 12747 // The parameter of the safelen clause must be a constant 12748 // positive integer expression. 12749 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen); 12750 if (Safelen.isInvalid()) 12751 return nullptr; 12752 return new (Context) 12753 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc); 12754 } 12755 12756 OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc, 12757 SourceLocation LParenLoc, 12758 SourceLocation EndLoc) { 12759 // OpenMP [2.8.1, simd construct, Description] 12760 // The parameter of the simdlen clause must be a constant 12761 // positive integer expression. 12762 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen); 12763 if (Simdlen.isInvalid()) 12764 return nullptr; 12765 return new (Context) 12766 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc); 12767 } 12768 12769 /// Tries to find omp_allocator_handle_t type. 12770 static bool findOMPAllocatorHandleT(Sema &S, SourceLocation Loc, 12771 DSAStackTy *Stack) { 12772 QualType OMPAllocatorHandleT = Stack->getOMPAllocatorHandleT(); 12773 if (!OMPAllocatorHandleT.isNull()) 12774 return true; 12775 // Build the predefined allocator expressions. 12776 bool ErrorFound = false; 12777 for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) { 12778 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I); 12779 StringRef Allocator = 12780 OMPAllocateDeclAttr::ConvertAllocatorTypeTyToStr(AllocatorKind); 12781 DeclarationName AllocatorName = &S.getASTContext().Idents.get(Allocator); 12782 auto *VD = dyn_cast_or_null<ValueDecl>( 12783 S.LookupSingleName(S.TUScope, AllocatorName, Loc, Sema::LookupAnyName)); 12784 if (!VD) { 12785 ErrorFound = true; 12786 break; 12787 } 12788 QualType AllocatorType = 12789 VD->getType().getNonLValueExprType(S.getASTContext()); 12790 ExprResult Res = S.BuildDeclRefExpr(VD, AllocatorType, VK_LValue, Loc); 12791 if (!Res.isUsable()) { 12792 ErrorFound = true; 12793 break; 12794 } 12795 if (OMPAllocatorHandleT.isNull()) 12796 OMPAllocatorHandleT = AllocatorType; 12797 if (!S.getASTContext().hasSameType(OMPAllocatorHandleT, AllocatorType)) { 12798 ErrorFound = true; 12799 break; 12800 } 12801 Stack->setAllocator(AllocatorKind, Res.get()); 12802 } 12803 if (ErrorFound) { 12804 S.Diag(Loc, diag::err_omp_implied_type_not_found) 12805 << "omp_allocator_handle_t"; 12806 return false; 12807 } 12808 OMPAllocatorHandleT.addConst(); 12809 Stack->setOMPAllocatorHandleT(OMPAllocatorHandleT); 12810 return true; 12811 } 12812 12813 OMPClause *Sema::ActOnOpenMPAllocatorClause(Expr *A, SourceLocation StartLoc, 12814 SourceLocation LParenLoc, 12815 SourceLocation EndLoc) { 12816 // OpenMP [2.11.3, allocate Directive, Description] 12817 // allocator is an expression of omp_allocator_handle_t type. 12818 if (!findOMPAllocatorHandleT(*this, A->getExprLoc(), DSAStack)) 12819 return nullptr; 12820 12821 ExprResult Allocator = DefaultLvalueConversion(A); 12822 if (Allocator.isInvalid()) 12823 return nullptr; 12824 Allocator = PerformImplicitConversion(Allocator.get(), 12825 DSAStack->getOMPAllocatorHandleT(), 12826 Sema::AA_Initializing, 12827 /*AllowExplicit=*/true); 12828 if (Allocator.isInvalid()) 12829 return nullptr; 12830 return new (Context) 12831 OMPAllocatorClause(Allocator.get(), StartLoc, LParenLoc, EndLoc); 12832 } 12833 12834 OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops, 12835 SourceLocation StartLoc, 12836 SourceLocation LParenLoc, 12837 SourceLocation EndLoc) { 12838 // OpenMP [2.7.1, loop construct, Description] 12839 // OpenMP [2.8.1, simd construct, Description] 12840 // OpenMP [2.9.6, distribute construct, Description] 12841 // The parameter of the collapse clause must be a constant 12842 // positive integer expression. 12843 ExprResult NumForLoopsResult = 12844 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse); 12845 if (NumForLoopsResult.isInvalid()) 12846 return nullptr; 12847 return new (Context) 12848 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc); 12849 } 12850 12851 OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc, 12852 SourceLocation EndLoc, 12853 SourceLocation LParenLoc, 12854 Expr *NumForLoops) { 12855 // OpenMP [2.7.1, loop construct, Description] 12856 // OpenMP [2.8.1, simd construct, Description] 12857 // OpenMP [2.9.6, distribute construct, Description] 12858 // The parameter of the ordered clause must be a constant 12859 // positive integer expression if any. 12860 if (NumForLoops && LParenLoc.isValid()) { 12861 ExprResult NumForLoopsResult = 12862 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered); 12863 if (NumForLoopsResult.isInvalid()) 12864 return nullptr; 12865 NumForLoops = NumForLoopsResult.get(); 12866 } else { 12867 NumForLoops = nullptr; 12868 } 12869 auto *Clause = OMPOrderedClause::Create( 12870 Context, NumForLoops, NumForLoops ? DSAStack->getAssociatedLoops() : 0, 12871 StartLoc, LParenLoc, EndLoc); 12872 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops, Clause); 12873 return Clause; 12874 } 12875 12876 OMPClause *Sema::ActOnOpenMPSimpleClause( 12877 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc, 12878 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 12879 OMPClause *Res = nullptr; 12880 switch (Kind) { 12881 case OMPC_default: 12882 Res = ActOnOpenMPDefaultClause(static_cast<DefaultKind>(Argument), 12883 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 12884 break; 12885 case OMPC_proc_bind: 12886 Res = ActOnOpenMPProcBindClause(static_cast<ProcBindKind>(Argument), 12887 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 12888 break; 12889 case OMPC_atomic_default_mem_order: 12890 Res = ActOnOpenMPAtomicDefaultMemOrderClause( 12891 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Argument), 12892 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 12893 break; 12894 case OMPC_order: 12895 Res = ActOnOpenMPOrderClause(static_cast<OpenMPOrderClauseKind>(Argument), 12896 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 12897 break; 12898 case OMPC_update: 12899 Res = ActOnOpenMPUpdateClause(static_cast<OpenMPDependClauseKind>(Argument), 12900 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 12901 break; 12902 case OMPC_if: 12903 case OMPC_final: 12904 case OMPC_num_threads: 12905 case OMPC_safelen: 12906 case OMPC_simdlen: 12907 case OMPC_allocator: 12908 case OMPC_collapse: 12909 case OMPC_schedule: 12910 case OMPC_private: 12911 case OMPC_firstprivate: 12912 case OMPC_lastprivate: 12913 case OMPC_shared: 12914 case OMPC_reduction: 12915 case OMPC_task_reduction: 12916 case OMPC_in_reduction: 12917 case OMPC_linear: 12918 case OMPC_aligned: 12919 case OMPC_copyin: 12920 case OMPC_copyprivate: 12921 case OMPC_ordered: 12922 case OMPC_nowait: 12923 case OMPC_untied: 12924 case OMPC_mergeable: 12925 case OMPC_threadprivate: 12926 case OMPC_allocate: 12927 case OMPC_flush: 12928 case OMPC_depobj: 12929 case OMPC_read: 12930 case OMPC_write: 12931 case OMPC_capture: 12932 case OMPC_seq_cst: 12933 case OMPC_acq_rel: 12934 case OMPC_acquire: 12935 case OMPC_release: 12936 case OMPC_relaxed: 12937 case OMPC_depend: 12938 case OMPC_device: 12939 case OMPC_threads: 12940 case OMPC_simd: 12941 case OMPC_map: 12942 case OMPC_num_teams: 12943 case OMPC_thread_limit: 12944 case OMPC_priority: 12945 case OMPC_grainsize: 12946 case OMPC_nogroup: 12947 case OMPC_num_tasks: 12948 case OMPC_hint: 12949 case OMPC_dist_schedule: 12950 case OMPC_defaultmap: 12951 case OMPC_unknown: 12952 case OMPC_uniform: 12953 case OMPC_to: 12954 case OMPC_from: 12955 case OMPC_use_device_ptr: 12956 case OMPC_use_device_addr: 12957 case OMPC_is_device_ptr: 12958 case OMPC_unified_address: 12959 case OMPC_unified_shared_memory: 12960 case OMPC_reverse_offload: 12961 case OMPC_dynamic_allocators: 12962 case OMPC_device_type: 12963 case OMPC_match: 12964 case OMPC_nontemporal: 12965 case OMPC_destroy: 12966 case OMPC_detach: 12967 case OMPC_inclusive: 12968 case OMPC_exclusive: 12969 case OMPC_uses_allocators: 12970 case OMPC_affinity: 12971 default: 12972 llvm_unreachable("Clause is not allowed."); 12973 } 12974 return Res; 12975 } 12976 12977 static std::string 12978 getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last, 12979 ArrayRef<unsigned> Exclude = llvm::None) { 12980 SmallString<256> Buffer; 12981 llvm::raw_svector_ostream Out(Buffer); 12982 unsigned Skipped = Exclude.size(); 12983 auto S = Exclude.begin(), E = Exclude.end(); 12984 for (unsigned I = First; I < Last; ++I) { 12985 if (std::find(S, E, I) != E) { 12986 --Skipped; 12987 continue; 12988 } 12989 Out << "'" << getOpenMPSimpleClauseTypeName(K, I) << "'"; 12990 if (I + Skipped + 2 == Last) 12991 Out << " or "; 12992 else if (I + Skipped + 1 != Last) 12993 Out << ", "; 12994 } 12995 return std::string(Out.str()); 12996 } 12997 12998 OMPClause *Sema::ActOnOpenMPDefaultClause(DefaultKind Kind, 12999 SourceLocation KindKwLoc, 13000 SourceLocation StartLoc, 13001 SourceLocation LParenLoc, 13002 SourceLocation EndLoc) { 13003 if (Kind == OMP_DEFAULT_unknown) { 13004 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 13005 << getListOfPossibleValues(OMPC_default, /*First=*/0, 13006 /*Last=*/unsigned(OMP_DEFAULT_unknown)) 13007 << getOpenMPClauseName(OMPC_default); 13008 return nullptr; 13009 } 13010 13011 switch (Kind) { 13012 case OMP_DEFAULT_none: 13013 DSAStack->setDefaultDSANone(KindKwLoc); 13014 break; 13015 case OMP_DEFAULT_shared: 13016 DSAStack->setDefaultDSAShared(KindKwLoc); 13017 break; 13018 case OMP_DEFAULT_firstprivate: 13019 DSAStack->setDefaultDSAFirstPrivate(KindKwLoc); 13020 break; 13021 default: 13022 llvm_unreachable("DSA unexpected in OpenMP default clause"); 13023 } 13024 13025 return new (Context) 13026 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 13027 } 13028 13029 OMPClause *Sema::ActOnOpenMPProcBindClause(ProcBindKind Kind, 13030 SourceLocation KindKwLoc, 13031 SourceLocation StartLoc, 13032 SourceLocation LParenLoc, 13033 SourceLocation EndLoc) { 13034 if (Kind == OMP_PROC_BIND_unknown) { 13035 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 13036 << getListOfPossibleValues(OMPC_proc_bind, 13037 /*First=*/unsigned(OMP_PROC_BIND_master), 13038 /*Last=*/5) 13039 << getOpenMPClauseName(OMPC_proc_bind); 13040 return nullptr; 13041 } 13042 return new (Context) 13043 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 13044 } 13045 13046 OMPClause *Sema::ActOnOpenMPAtomicDefaultMemOrderClause( 13047 OpenMPAtomicDefaultMemOrderClauseKind Kind, SourceLocation KindKwLoc, 13048 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 13049 if (Kind == OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) { 13050 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 13051 << getListOfPossibleValues( 13052 OMPC_atomic_default_mem_order, /*First=*/0, 13053 /*Last=*/OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) 13054 << getOpenMPClauseName(OMPC_atomic_default_mem_order); 13055 return nullptr; 13056 } 13057 return new (Context) OMPAtomicDefaultMemOrderClause(Kind, KindKwLoc, StartLoc, 13058 LParenLoc, EndLoc); 13059 } 13060 13061 OMPClause *Sema::ActOnOpenMPOrderClause(OpenMPOrderClauseKind Kind, 13062 SourceLocation KindKwLoc, 13063 SourceLocation StartLoc, 13064 SourceLocation LParenLoc, 13065 SourceLocation EndLoc) { 13066 if (Kind == OMPC_ORDER_unknown) { 13067 static_assert(OMPC_ORDER_unknown > 0, 13068 "OMPC_ORDER_unknown not greater than 0"); 13069 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 13070 << getListOfPossibleValues(OMPC_order, /*First=*/0, 13071 /*Last=*/OMPC_ORDER_unknown) 13072 << getOpenMPClauseName(OMPC_order); 13073 return nullptr; 13074 } 13075 return new (Context) 13076 OMPOrderClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 13077 } 13078 13079 OMPClause *Sema::ActOnOpenMPUpdateClause(OpenMPDependClauseKind Kind, 13080 SourceLocation KindKwLoc, 13081 SourceLocation StartLoc, 13082 SourceLocation LParenLoc, 13083 SourceLocation EndLoc) { 13084 if (Kind == OMPC_DEPEND_unknown || Kind == OMPC_DEPEND_source || 13085 Kind == OMPC_DEPEND_sink || Kind == OMPC_DEPEND_depobj) { 13086 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink, 13087 OMPC_DEPEND_depobj}; 13088 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 13089 << getListOfPossibleValues(OMPC_depend, /*First=*/0, 13090 /*Last=*/OMPC_DEPEND_unknown, Except) 13091 << getOpenMPClauseName(OMPC_update); 13092 return nullptr; 13093 } 13094 return OMPUpdateClause::Create(Context, StartLoc, LParenLoc, KindKwLoc, Kind, 13095 EndLoc); 13096 } 13097 13098 OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause( 13099 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr, 13100 SourceLocation StartLoc, SourceLocation LParenLoc, 13101 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc, 13102 SourceLocation EndLoc) { 13103 OMPClause *Res = nullptr; 13104 switch (Kind) { 13105 case OMPC_schedule: 13106 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements }; 13107 assert(Argument.size() == NumberOfElements && 13108 ArgumentLoc.size() == NumberOfElements); 13109 Res = ActOnOpenMPScheduleClause( 13110 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]), 13111 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]), 13112 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr, 13113 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2], 13114 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc); 13115 break; 13116 case OMPC_if: 13117 assert(Argument.size() == 1 && ArgumentLoc.size() == 1); 13118 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()), 13119 Expr, StartLoc, LParenLoc, ArgumentLoc.back(), 13120 DelimLoc, EndLoc); 13121 break; 13122 case OMPC_dist_schedule: 13123 Res = ActOnOpenMPDistScheduleClause( 13124 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr, 13125 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc); 13126 break; 13127 case OMPC_defaultmap: 13128 enum { Modifier, DefaultmapKind }; 13129 Res = ActOnOpenMPDefaultmapClause( 13130 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]), 13131 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]), 13132 StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind], 13133 EndLoc); 13134 break; 13135 case OMPC_device: 13136 assert(Argument.size() == 1 && ArgumentLoc.size() == 1); 13137 Res = ActOnOpenMPDeviceClause( 13138 static_cast<OpenMPDeviceClauseModifier>(Argument.back()), Expr, 13139 StartLoc, LParenLoc, ArgumentLoc.back(), EndLoc); 13140 break; 13141 case OMPC_final: 13142 case OMPC_num_threads: 13143 case OMPC_safelen: 13144 case OMPC_simdlen: 13145 case OMPC_allocator: 13146 case OMPC_collapse: 13147 case OMPC_default: 13148 case OMPC_proc_bind: 13149 case OMPC_private: 13150 case OMPC_firstprivate: 13151 case OMPC_lastprivate: 13152 case OMPC_shared: 13153 case OMPC_reduction: 13154 case OMPC_task_reduction: 13155 case OMPC_in_reduction: 13156 case OMPC_linear: 13157 case OMPC_aligned: 13158 case OMPC_copyin: 13159 case OMPC_copyprivate: 13160 case OMPC_ordered: 13161 case OMPC_nowait: 13162 case OMPC_untied: 13163 case OMPC_mergeable: 13164 case OMPC_threadprivate: 13165 case OMPC_allocate: 13166 case OMPC_flush: 13167 case OMPC_depobj: 13168 case OMPC_read: 13169 case OMPC_write: 13170 case OMPC_update: 13171 case OMPC_capture: 13172 case OMPC_seq_cst: 13173 case OMPC_acq_rel: 13174 case OMPC_acquire: 13175 case OMPC_release: 13176 case OMPC_relaxed: 13177 case OMPC_depend: 13178 case OMPC_threads: 13179 case OMPC_simd: 13180 case OMPC_map: 13181 case OMPC_num_teams: 13182 case OMPC_thread_limit: 13183 case OMPC_priority: 13184 case OMPC_grainsize: 13185 case OMPC_nogroup: 13186 case OMPC_num_tasks: 13187 case OMPC_hint: 13188 case OMPC_unknown: 13189 case OMPC_uniform: 13190 case OMPC_to: 13191 case OMPC_from: 13192 case OMPC_use_device_ptr: 13193 case OMPC_use_device_addr: 13194 case OMPC_is_device_ptr: 13195 case OMPC_unified_address: 13196 case OMPC_unified_shared_memory: 13197 case OMPC_reverse_offload: 13198 case OMPC_dynamic_allocators: 13199 case OMPC_atomic_default_mem_order: 13200 case OMPC_device_type: 13201 case OMPC_match: 13202 case OMPC_nontemporal: 13203 case OMPC_order: 13204 case OMPC_destroy: 13205 case OMPC_detach: 13206 case OMPC_inclusive: 13207 case OMPC_exclusive: 13208 case OMPC_uses_allocators: 13209 case OMPC_affinity: 13210 default: 13211 llvm_unreachable("Clause is not allowed."); 13212 } 13213 return Res; 13214 } 13215 13216 static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1, 13217 OpenMPScheduleClauseModifier M2, 13218 SourceLocation M1Loc, SourceLocation M2Loc) { 13219 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) { 13220 SmallVector<unsigned, 2> Excluded; 13221 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown) 13222 Excluded.push_back(M2); 13223 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) 13224 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic); 13225 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic) 13226 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic); 13227 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value) 13228 << getListOfPossibleValues(OMPC_schedule, 13229 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1, 13230 /*Last=*/OMPC_SCHEDULE_MODIFIER_last, 13231 Excluded) 13232 << getOpenMPClauseName(OMPC_schedule); 13233 return true; 13234 } 13235 return false; 13236 } 13237 13238 OMPClause *Sema::ActOnOpenMPScheduleClause( 13239 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2, 13240 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, 13241 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc, 13242 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) { 13243 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) || 13244 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc)) 13245 return nullptr; 13246 // OpenMP, 2.7.1, Loop Construct, Restrictions 13247 // Either the monotonic modifier or the nonmonotonic modifier can be specified 13248 // but not both. 13249 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) || 13250 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic && 13251 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) || 13252 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic && 13253 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) { 13254 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier) 13255 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2) 13256 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1); 13257 return nullptr; 13258 } 13259 if (Kind == OMPC_SCHEDULE_unknown) { 13260 std::string Values; 13261 if (M1Loc.isInvalid() && M2Loc.isInvalid()) { 13262 unsigned Exclude[] = {OMPC_SCHEDULE_unknown}; 13263 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0, 13264 /*Last=*/OMPC_SCHEDULE_MODIFIER_last, 13265 Exclude); 13266 } else { 13267 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0, 13268 /*Last=*/OMPC_SCHEDULE_unknown); 13269 } 13270 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 13271 << Values << getOpenMPClauseName(OMPC_schedule); 13272 return nullptr; 13273 } 13274 // OpenMP, 2.7.1, Loop Construct, Restrictions 13275 // The nonmonotonic modifier can only be specified with schedule(dynamic) or 13276 // schedule(guided). 13277 // OpenMP 5.0 does not have this restriction. 13278 if (LangOpts.OpenMP < 50 && 13279 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic || 13280 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) && 13281 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) { 13282 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc, 13283 diag::err_omp_schedule_nonmonotonic_static); 13284 return nullptr; 13285 } 13286 Expr *ValExpr = ChunkSize; 13287 Stmt *HelperValStmt = nullptr; 13288 if (ChunkSize) { 13289 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && 13290 !ChunkSize->isInstantiationDependent() && 13291 !ChunkSize->containsUnexpandedParameterPack()) { 13292 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc(); 13293 ExprResult Val = 13294 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); 13295 if (Val.isInvalid()) 13296 return nullptr; 13297 13298 ValExpr = Val.get(); 13299 13300 // OpenMP [2.7.1, Restrictions] 13301 // chunk_size must be a loop invariant integer expression with a positive 13302 // value. 13303 if (Optional<llvm::APSInt> Result = 13304 ValExpr->getIntegerConstantExpr(Context)) { 13305 if (Result->isSigned() && !Result->isStrictlyPositive()) { 13306 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) 13307 << "schedule" << 1 << ChunkSize->getSourceRange(); 13308 return nullptr; 13309 } 13310 } else if (getOpenMPCaptureRegionForClause( 13311 DSAStack->getCurrentDirective(), OMPC_schedule, 13312 LangOpts.OpenMP) != OMPD_unknown && 13313 !CurContext->isDependentContext()) { 13314 ValExpr = MakeFullExpr(ValExpr).get(); 13315 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 13316 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 13317 HelperValStmt = buildPreInits(Context, Captures); 13318 } 13319 } 13320 } 13321 13322 return new (Context) 13323 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind, 13324 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc); 13325 } 13326 13327 OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind, 13328 SourceLocation StartLoc, 13329 SourceLocation EndLoc) { 13330 OMPClause *Res = nullptr; 13331 switch (Kind) { 13332 case OMPC_ordered: 13333 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc); 13334 break; 13335 case OMPC_nowait: 13336 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc); 13337 break; 13338 case OMPC_untied: 13339 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc); 13340 break; 13341 case OMPC_mergeable: 13342 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc); 13343 break; 13344 case OMPC_read: 13345 Res = ActOnOpenMPReadClause(StartLoc, EndLoc); 13346 break; 13347 case OMPC_write: 13348 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc); 13349 break; 13350 case OMPC_update: 13351 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc); 13352 break; 13353 case OMPC_capture: 13354 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc); 13355 break; 13356 case OMPC_seq_cst: 13357 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc); 13358 break; 13359 case OMPC_acq_rel: 13360 Res = ActOnOpenMPAcqRelClause(StartLoc, EndLoc); 13361 break; 13362 case OMPC_acquire: 13363 Res = ActOnOpenMPAcquireClause(StartLoc, EndLoc); 13364 break; 13365 case OMPC_release: 13366 Res = ActOnOpenMPReleaseClause(StartLoc, EndLoc); 13367 break; 13368 case OMPC_relaxed: 13369 Res = ActOnOpenMPRelaxedClause(StartLoc, EndLoc); 13370 break; 13371 case OMPC_threads: 13372 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc); 13373 break; 13374 case OMPC_simd: 13375 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc); 13376 break; 13377 case OMPC_nogroup: 13378 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc); 13379 break; 13380 case OMPC_unified_address: 13381 Res = ActOnOpenMPUnifiedAddressClause(StartLoc, EndLoc); 13382 break; 13383 case OMPC_unified_shared_memory: 13384 Res = ActOnOpenMPUnifiedSharedMemoryClause(StartLoc, EndLoc); 13385 break; 13386 case OMPC_reverse_offload: 13387 Res = ActOnOpenMPReverseOffloadClause(StartLoc, EndLoc); 13388 break; 13389 case OMPC_dynamic_allocators: 13390 Res = ActOnOpenMPDynamicAllocatorsClause(StartLoc, EndLoc); 13391 break; 13392 case OMPC_destroy: 13393 Res = ActOnOpenMPDestroyClause(StartLoc, EndLoc); 13394 break; 13395 case OMPC_if: 13396 case OMPC_final: 13397 case OMPC_num_threads: 13398 case OMPC_safelen: 13399 case OMPC_simdlen: 13400 case OMPC_allocator: 13401 case OMPC_collapse: 13402 case OMPC_schedule: 13403 case OMPC_private: 13404 case OMPC_firstprivate: 13405 case OMPC_lastprivate: 13406 case OMPC_shared: 13407 case OMPC_reduction: 13408 case OMPC_task_reduction: 13409 case OMPC_in_reduction: 13410 case OMPC_linear: 13411 case OMPC_aligned: 13412 case OMPC_copyin: 13413 case OMPC_copyprivate: 13414 case OMPC_default: 13415 case OMPC_proc_bind: 13416 case OMPC_threadprivate: 13417 case OMPC_allocate: 13418 case OMPC_flush: 13419 case OMPC_depobj: 13420 case OMPC_depend: 13421 case OMPC_device: 13422 case OMPC_map: 13423 case OMPC_num_teams: 13424 case OMPC_thread_limit: 13425 case OMPC_priority: 13426 case OMPC_grainsize: 13427 case OMPC_num_tasks: 13428 case OMPC_hint: 13429 case OMPC_dist_schedule: 13430 case OMPC_defaultmap: 13431 case OMPC_unknown: 13432 case OMPC_uniform: 13433 case OMPC_to: 13434 case OMPC_from: 13435 case OMPC_use_device_ptr: 13436 case OMPC_use_device_addr: 13437 case OMPC_is_device_ptr: 13438 case OMPC_atomic_default_mem_order: 13439 case OMPC_device_type: 13440 case OMPC_match: 13441 case OMPC_nontemporal: 13442 case OMPC_order: 13443 case OMPC_detach: 13444 case OMPC_inclusive: 13445 case OMPC_exclusive: 13446 case OMPC_uses_allocators: 13447 case OMPC_affinity: 13448 default: 13449 llvm_unreachable("Clause is not allowed."); 13450 } 13451 return Res; 13452 } 13453 13454 OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc, 13455 SourceLocation EndLoc) { 13456 DSAStack->setNowaitRegion(); 13457 return new (Context) OMPNowaitClause(StartLoc, EndLoc); 13458 } 13459 13460 OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc, 13461 SourceLocation EndLoc) { 13462 return new (Context) OMPUntiedClause(StartLoc, EndLoc); 13463 } 13464 13465 OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc, 13466 SourceLocation EndLoc) { 13467 return new (Context) OMPMergeableClause(StartLoc, EndLoc); 13468 } 13469 13470 OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc, 13471 SourceLocation EndLoc) { 13472 return new (Context) OMPReadClause(StartLoc, EndLoc); 13473 } 13474 13475 OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc, 13476 SourceLocation EndLoc) { 13477 return new (Context) OMPWriteClause(StartLoc, EndLoc); 13478 } 13479 13480 OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc, 13481 SourceLocation EndLoc) { 13482 return OMPUpdateClause::Create(Context, StartLoc, EndLoc); 13483 } 13484 13485 OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc, 13486 SourceLocation EndLoc) { 13487 return new (Context) OMPCaptureClause(StartLoc, EndLoc); 13488 } 13489 13490 OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc, 13491 SourceLocation EndLoc) { 13492 return new (Context) OMPSeqCstClause(StartLoc, EndLoc); 13493 } 13494 13495 OMPClause *Sema::ActOnOpenMPAcqRelClause(SourceLocation StartLoc, 13496 SourceLocation EndLoc) { 13497 return new (Context) OMPAcqRelClause(StartLoc, EndLoc); 13498 } 13499 13500 OMPClause *Sema::ActOnOpenMPAcquireClause(SourceLocation StartLoc, 13501 SourceLocation EndLoc) { 13502 return new (Context) OMPAcquireClause(StartLoc, EndLoc); 13503 } 13504 13505 OMPClause *Sema::ActOnOpenMPReleaseClause(SourceLocation StartLoc, 13506 SourceLocation EndLoc) { 13507 return new (Context) OMPReleaseClause(StartLoc, EndLoc); 13508 } 13509 13510 OMPClause *Sema::ActOnOpenMPRelaxedClause(SourceLocation StartLoc, 13511 SourceLocation EndLoc) { 13512 return new (Context) OMPRelaxedClause(StartLoc, EndLoc); 13513 } 13514 13515 OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc, 13516 SourceLocation EndLoc) { 13517 return new (Context) OMPThreadsClause(StartLoc, EndLoc); 13518 } 13519 13520 OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc, 13521 SourceLocation EndLoc) { 13522 return new (Context) OMPSIMDClause(StartLoc, EndLoc); 13523 } 13524 13525 OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc, 13526 SourceLocation EndLoc) { 13527 return new (Context) OMPNogroupClause(StartLoc, EndLoc); 13528 } 13529 13530 OMPClause *Sema::ActOnOpenMPUnifiedAddressClause(SourceLocation StartLoc, 13531 SourceLocation EndLoc) { 13532 return new (Context) OMPUnifiedAddressClause(StartLoc, EndLoc); 13533 } 13534 13535 OMPClause *Sema::ActOnOpenMPUnifiedSharedMemoryClause(SourceLocation StartLoc, 13536 SourceLocation EndLoc) { 13537 return new (Context) OMPUnifiedSharedMemoryClause(StartLoc, EndLoc); 13538 } 13539 13540 OMPClause *Sema::ActOnOpenMPReverseOffloadClause(SourceLocation StartLoc, 13541 SourceLocation EndLoc) { 13542 return new (Context) OMPReverseOffloadClause(StartLoc, EndLoc); 13543 } 13544 13545 OMPClause *Sema::ActOnOpenMPDynamicAllocatorsClause(SourceLocation StartLoc, 13546 SourceLocation EndLoc) { 13547 return new (Context) OMPDynamicAllocatorsClause(StartLoc, EndLoc); 13548 } 13549 13550 OMPClause *Sema::ActOnOpenMPDestroyClause(SourceLocation StartLoc, 13551 SourceLocation EndLoc) { 13552 return new (Context) OMPDestroyClause(StartLoc, EndLoc); 13553 } 13554 13555 OMPClause *Sema::ActOnOpenMPVarListClause( 13556 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *DepModOrTailExpr, 13557 const OMPVarListLocTy &Locs, SourceLocation ColonLoc, 13558 CXXScopeSpec &ReductionOrMapperIdScopeSpec, 13559 DeclarationNameInfo &ReductionOrMapperId, int ExtraModifier, 13560 ArrayRef<OpenMPMapModifierKind> MapTypeModifiers, 13561 ArrayRef<SourceLocation> MapTypeModifiersLoc, bool IsMapTypeImplicit, 13562 SourceLocation ExtraModifierLoc, 13563 ArrayRef<OpenMPMotionModifierKind> MotionModifiers, 13564 ArrayRef<SourceLocation> MotionModifiersLoc) { 13565 SourceLocation StartLoc = Locs.StartLoc; 13566 SourceLocation LParenLoc = Locs.LParenLoc; 13567 SourceLocation EndLoc = Locs.EndLoc; 13568 OMPClause *Res = nullptr; 13569 switch (Kind) { 13570 case OMPC_private: 13571 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc); 13572 break; 13573 case OMPC_firstprivate: 13574 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 13575 break; 13576 case OMPC_lastprivate: 13577 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_LASTPRIVATE_unknown && 13578 "Unexpected lastprivate modifier."); 13579 Res = ActOnOpenMPLastprivateClause( 13580 VarList, static_cast<OpenMPLastprivateModifier>(ExtraModifier), 13581 ExtraModifierLoc, ColonLoc, StartLoc, LParenLoc, EndLoc); 13582 break; 13583 case OMPC_shared: 13584 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc); 13585 break; 13586 case OMPC_reduction: 13587 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_REDUCTION_unknown && 13588 "Unexpected lastprivate modifier."); 13589 Res = ActOnOpenMPReductionClause( 13590 VarList, static_cast<OpenMPReductionClauseModifier>(ExtraModifier), 13591 StartLoc, LParenLoc, ExtraModifierLoc, ColonLoc, EndLoc, 13592 ReductionOrMapperIdScopeSpec, ReductionOrMapperId); 13593 break; 13594 case OMPC_task_reduction: 13595 Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 13596 EndLoc, ReductionOrMapperIdScopeSpec, 13597 ReductionOrMapperId); 13598 break; 13599 case OMPC_in_reduction: 13600 Res = ActOnOpenMPInReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 13601 EndLoc, ReductionOrMapperIdScopeSpec, 13602 ReductionOrMapperId); 13603 break; 13604 case OMPC_linear: 13605 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_LINEAR_unknown && 13606 "Unexpected linear modifier."); 13607 Res = ActOnOpenMPLinearClause( 13608 VarList, DepModOrTailExpr, StartLoc, LParenLoc, 13609 static_cast<OpenMPLinearClauseKind>(ExtraModifier), ExtraModifierLoc, 13610 ColonLoc, EndLoc); 13611 break; 13612 case OMPC_aligned: 13613 Res = ActOnOpenMPAlignedClause(VarList, DepModOrTailExpr, StartLoc, 13614 LParenLoc, ColonLoc, EndLoc); 13615 break; 13616 case OMPC_copyin: 13617 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc); 13618 break; 13619 case OMPC_copyprivate: 13620 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 13621 break; 13622 case OMPC_flush: 13623 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc); 13624 break; 13625 case OMPC_depend: 13626 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_DEPEND_unknown && 13627 "Unexpected depend modifier."); 13628 Res = ActOnOpenMPDependClause( 13629 DepModOrTailExpr, static_cast<OpenMPDependClauseKind>(ExtraModifier), 13630 ExtraModifierLoc, ColonLoc, VarList, StartLoc, LParenLoc, EndLoc); 13631 break; 13632 case OMPC_map: 13633 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_MAP_unknown && 13634 "Unexpected map modifier."); 13635 Res = ActOnOpenMPMapClause( 13636 MapTypeModifiers, MapTypeModifiersLoc, ReductionOrMapperIdScopeSpec, 13637 ReductionOrMapperId, static_cast<OpenMPMapClauseKind>(ExtraModifier), 13638 IsMapTypeImplicit, ExtraModifierLoc, ColonLoc, VarList, Locs); 13639 break; 13640 case OMPC_to: 13641 Res = ActOnOpenMPToClause(MotionModifiers, MotionModifiersLoc, 13642 ReductionOrMapperIdScopeSpec, ReductionOrMapperId, 13643 ColonLoc, VarList, Locs); 13644 break; 13645 case OMPC_from: 13646 Res = ActOnOpenMPFromClause(MotionModifiers, MotionModifiersLoc, 13647 ReductionOrMapperIdScopeSpec, 13648 ReductionOrMapperId, ColonLoc, VarList, Locs); 13649 break; 13650 case OMPC_use_device_ptr: 13651 Res = ActOnOpenMPUseDevicePtrClause(VarList, Locs); 13652 break; 13653 case OMPC_use_device_addr: 13654 Res = ActOnOpenMPUseDeviceAddrClause(VarList, Locs); 13655 break; 13656 case OMPC_is_device_ptr: 13657 Res = ActOnOpenMPIsDevicePtrClause(VarList, Locs); 13658 break; 13659 case OMPC_allocate: 13660 Res = ActOnOpenMPAllocateClause(DepModOrTailExpr, VarList, StartLoc, 13661 LParenLoc, ColonLoc, EndLoc); 13662 break; 13663 case OMPC_nontemporal: 13664 Res = ActOnOpenMPNontemporalClause(VarList, StartLoc, LParenLoc, EndLoc); 13665 break; 13666 case OMPC_inclusive: 13667 Res = ActOnOpenMPInclusiveClause(VarList, StartLoc, LParenLoc, EndLoc); 13668 break; 13669 case OMPC_exclusive: 13670 Res = ActOnOpenMPExclusiveClause(VarList, StartLoc, LParenLoc, EndLoc); 13671 break; 13672 case OMPC_affinity: 13673 Res = ActOnOpenMPAffinityClause(StartLoc, LParenLoc, ColonLoc, EndLoc, 13674 DepModOrTailExpr, VarList); 13675 break; 13676 case OMPC_if: 13677 case OMPC_depobj: 13678 case OMPC_final: 13679 case OMPC_num_threads: 13680 case OMPC_safelen: 13681 case OMPC_simdlen: 13682 case OMPC_allocator: 13683 case OMPC_collapse: 13684 case OMPC_default: 13685 case OMPC_proc_bind: 13686 case OMPC_schedule: 13687 case OMPC_ordered: 13688 case OMPC_nowait: 13689 case OMPC_untied: 13690 case OMPC_mergeable: 13691 case OMPC_threadprivate: 13692 case OMPC_read: 13693 case OMPC_write: 13694 case OMPC_update: 13695 case OMPC_capture: 13696 case OMPC_seq_cst: 13697 case OMPC_acq_rel: 13698 case OMPC_acquire: 13699 case OMPC_release: 13700 case OMPC_relaxed: 13701 case OMPC_device: 13702 case OMPC_threads: 13703 case OMPC_simd: 13704 case OMPC_num_teams: 13705 case OMPC_thread_limit: 13706 case OMPC_priority: 13707 case OMPC_grainsize: 13708 case OMPC_nogroup: 13709 case OMPC_num_tasks: 13710 case OMPC_hint: 13711 case OMPC_dist_schedule: 13712 case OMPC_defaultmap: 13713 case OMPC_unknown: 13714 case OMPC_uniform: 13715 case OMPC_unified_address: 13716 case OMPC_unified_shared_memory: 13717 case OMPC_reverse_offload: 13718 case OMPC_dynamic_allocators: 13719 case OMPC_atomic_default_mem_order: 13720 case OMPC_device_type: 13721 case OMPC_match: 13722 case OMPC_order: 13723 case OMPC_destroy: 13724 case OMPC_detach: 13725 case OMPC_uses_allocators: 13726 default: 13727 llvm_unreachable("Clause is not allowed."); 13728 } 13729 return Res; 13730 } 13731 13732 ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK, 13733 ExprObjectKind OK, SourceLocation Loc) { 13734 ExprResult Res = BuildDeclRefExpr( 13735 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc); 13736 if (!Res.isUsable()) 13737 return ExprError(); 13738 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) { 13739 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get()); 13740 if (!Res.isUsable()) 13741 return ExprError(); 13742 } 13743 if (VK != VK_LValue && Res.get()->isGLValue()) { 13744 Res = DefaultLvalueConversion(Res.get()); 13745 if (!Res.isUsable()) 13746 return ExprError(); 13747 } 13748 return Res; 13749 } 13750 13751 OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList, 13752 SourceLocation StartLoc, 13753 SourceLocation LParenLoc, 13754 SourceLocation EndLoc) { 13755 SmallVector<Expr *, 8> Vars; 13756 SmallVector<Expr *, 8> PrivateCopies; 13757 for (Expr *RefExpr : VarList) { 13758 assert(RefExpr && "NULL expr in OpenMP private clause."); 13759 SourceLocation ELoc; 13760 SourceRange ERange; 13761 Expr *SimpleRefExpr = RefExpr; 13762 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 13763 if (Res.second) { 13764 // It will be analyzed later. 13765 Vars.push_back(RefExpr); 13766 PrivateCopies.push_back(nullptr); 13767 } 13768 ValueDecl *D = Res.first; 13769 if (!D) 13770 continue; 13771 13772 QualType Type = D->getType(); 13773 auto *VD = dyn_cast<VarDecl>(D); 13774 13775 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 13776 // A variable that appears in a private clause must not have an incomplete 13777 // type or a reference type. 13778 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type)) 13779 continue; 13780 Type = Type.getNonReferenceType(); 13781 13782 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions] 13783 // A variable that is privatized must not have a const-qualified type 13784 // unless it is of class type with a mutable member. This restriction does 13785 // not apply to the firstprivate clause. 13786 // 13787 // OpenMP 3.1 [2.9.3.3, private clause, Restrictions] 13788 // A variable that appears in a private clause must not have a 13789 // const-qualified type unless it is of class type with a mutable member. 13790 if (rejectConstNotMutableType(*this, D, Type, OMPC_private, ELoc)) 13791 continue; 13792 13793 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 13794 // in a Construct] 13795 // Variables with the predetermined data-sharing attributes may not be 13796 // listed in data-sharing attributes clauses, except for the cases 13797 // listed below. For these exceptions only, listing a predetermined 13798 // variable in a data-sharing attribute clause is allowed and overrides 13799 // the variable's predetermined data-sharing attributes. 13800 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 13801 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) { 13802 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 13803 << getOpenMPClauseName(OMPC_private); 13804 reportOriginalDsa(*this, DSAStack, D, DVar); 13805 continue; 13806 } 13807 13808 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 13809 // Variably modified types are not supported for tasks. 13810 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && 13811 isOpenMPTaskingDirective(CurrDir)) { 13812 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 13813 << getOpenMPClauseName(OMPC_private) << Type 13814 << getOpenMPDirectiveName(CurrDir); 13815 bool IsDecl = 13816 !VD || 13817 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 13818 Diag(D->getLocation(), 13819 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 13820 << D; 13821 continue; 13822 } 13823 13824 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 13825 // A list item cannot appear in both a map clause and a data-sharing 13826 // attribute clause on the same construct 13827 // 13828 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7] 13829 // A list item cannot appear in both a map clause and a data-sharing 13830 // attribute clause on the same construct unless the construct is a 13831 // combined construct. 13832 if ((LangOpts.OpenMP <= 45 && isOpenMPTargetExecutionDirective(CurrDir)) || 13833 CurrDir == OMPD_target) { 13834 OpenMPClauseKind ConflictKind; 13835 if (DSAStack->checkMappableExprComponentListsForDecl( 13836 VD, /*CurrentRegionOnly=*/true, 13837 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef, 13838 OpenMPClauseKind WhereFoundClauseKind) -> bool { 13839 ConflictKind = WhereFoundClauseKind; 13840 return true; 13841 })) { 13842 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 13843 << getOpenMPClauseName(OMPC_private) 13844 << getOpenMPClauseName(ConflictKind) 13845 << getOpenMPDirectiveName(CurrDir); 13846 reportOriginalDsa(*this, DSAStack, D, DVar); 13847 continue; 13848 } 13849 } 13850 13851 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1] 13852 // A variable of class type (or array thereof) that appears in a private 13853 // clause requires an accessible, unambiguous default constructor for the 13854 // class type. 13855 // Generate helper private variable and initialize it with the default 13856 // value. The address of the original variable is replaced by the address of 13857 // the new private variable in CodeGen. This new variable is not added to 13858 // IdResolver, so the code in the OpenMP region uses original variable for 13859 // proper diagnostics. 13860 Type = Type.getUnqualifiedType(); 13861 VarDecl *VDPrivate = 13862 buildVarDecl(*this, ELoc, Type, D->getName(), 13863 D->hasAttrs() ? &D->getAttrs() : nullptr, 13864 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 13865 ActOnUninitializedDecl(VDPrivate); 13866 if (VDPrivate->isInvalidDecl()) 13867 continue; 13868 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 13869 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc); 13870 13871 DeclRefExpr *Ref = nullptr; 13872 if (!VD && !CurContext->isDependentContext()) 13873 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 13874 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref); 13875 Vars.push_back((VD || CurContext->isDependentContext()) 13876 ? RefExpr->IgnoreParens() 13877 : Ref); 13878 PrivateCopies.push_back(VDPrivateRefExpr); 13879 } 13880 13881 if (Vars.empty()) 13882 return nullptr; 13883 13884 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 13885 PrivateCopies); 13886 } 13887 13888 namespace { 13889 class DiagsUninitializedSeveretyRAII { 13890 private: 13891 DiagnosticsEngine &Diags; 13892 SourceLocation SavedLoc; 13893 bool IsIgnored = false; 13894 13895 public: 13896 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc, 13897 bool IsIgnored) 13898 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) { 13899 if (!IsIgnored) { 13900 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init, 13901 /*Map*/ diag::Severity::Ignored, Loc); 13902 } 13903 } 13904 ~DiagsUninitializedSeveretyRAII() { 13905 if (!IsIgnored) 13906 Diags.popMappings(SavedLoc); 13907 } 13908 }; 13909 } 13910 13911 OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList, 13912 SourceLocation StartLoc, 13913 SourceLocation LParenLoc, 13914 SourceLocation EndLoc) { 13915 SmallVector<Expr *, 8> Vars; 13916 SmallVector<Expr *, 8> PrivateCopies; 13917 SmallVector<Expr *, 8> Inits; 13918 SmallVector<Decl *, 4> ExprCaptures; 13919 bool IsImplicitClause = 13920 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid(); 13921 SourceLocation ImplicitClauseLoc = DSAStack->getConstructLoc(); 13922 13923 for (Expr *RefExpr : VarList) { 13924 assert(RefExpr && "NULL expr in OpenMP firstprivate clause."); 13925 SourceLocation ELoc; 13926 SourceRange ERange; 13927 Expr *SimpleRefExpr = RefExpr; 13928 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 13929 if (Res.second) { 13930 // It will be analyzed later. 13931 Vars.push_back(RefExpr); 13932 PrivateCopies.push_back(nullptr); 13933 Inits.push_back(nullptr); 13934 } 13935 ValueDecl *D = Res.first; 13936 if (!D) 13937 continue; 13938 13939 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc; 13940 QualType Type = D->getType(); 13941 auto *VD = dyn_cast<VarDecl>(D); 13942 13943 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 13944 // A variable that appears in a private clause must not have an incomplete 13945 // type or a reference type. 13946 if (RequireCompleteType(ELoc, Type, 13947 diag::err_omp_firstprivate_incomplete_type)) 13948 continue; 13949 Type = Type.getNonReferenceType(); 13950 13951 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1] 13952 // A variable of class type (or array thereof) that appears in a private 13953 // clause requires an accessible, unambiguous copy constructor for the 13954 // class type. 13955 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType(); 13956 13957 // If an implicit firstprivate variable found it was checked already. 13958 DSAStackTy::DSAVarData TopDVar; 13959 if (!IsImplicitClause) { 13960 DSAStackTy::DSAVarData DVar = 13961 DSAStack->getTopDSA(D, /*FromParent=*/false); 13962 TopDVar = DVar; 13963 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 13964 bool IsConstant = ElemType.isConstant(Context); 13965 // OpenMP [2.4.13, Data-sharing Attribute Clauses] 13966 // A list item that specifies a given variable may not appear in more 13967 // than one clause on the same directive, except that a variable may be 13968 // specified in both firstprivate and lastprivate clauses. 13969 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3] 13970 // A list item may appear in a firstprivate or lastprivate clause but not 13971 // both. 13972 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate && 13973 (isOpenMPDistributeDirective(CurrDir) || 13974 DVar.CKind != OMPC_lastprivate) && 13975 DVar.RefExpr) { 13976 Diag(ELoc, diag::err_omp_wrong_dsa) 13977 << getOpenMPClauseName(DVar.CKind) 13978 << getOpenMPClauseName(OMPC_firstprivate); 13979 reportOriginalDsa(*this, DSAStack, D, DVar); 13980 continue; 13981 } 13982 13983 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 13984 // in a Construct] 13985 // Variables with the predetermined data-sharing attributes may not be 13986 // listed in data-sharing attributes clauses, except for the cases 13987 // listed below. For these exceptions only, listing a predetermined 13988 // variable in a data-sharing attribute clause is allowed and overrides 13989 // the variable's predetermined data-sharing attributes. 13990 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 13991 // in a Construct, C/C++, p.2] 13992 // Variables with const-qualified type having no mutable member may be 13993 // listed in a firstprivate clause, even if they are static data members. 13994 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr && 13995 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) { 13996 Diag(ELoc, diag::err_omp_wrong_dsa) 13997 << getOpenMPClauseName(DVar.CKind) 13998 << getOpenMPClauseName(OMPC_firstprivate); 13999 reportOriginalDsa(*this, DSAStack, D, DVar); 14000 continue; 14001 } 14002 14003 // OpenMP [2.9.3.4, Restrictions, p.2] 14004 // A list item that is private within a parallel region must not appear 14005 // in a firstprivate clause on a worksharing construct if any of the 14006 // worksharing regions arising from the worksharing construct ever bind 14007 // to any of the parallel regions arising from the parallel construct. 14008 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3] 14009 // A list item that is private within a teams region must not appear in a 14010 // firstprivate clause on a distribute construct if any of the distribute 14011 // regions arising from the distribute construct ever bind to any of the 14012 // teams regions arising from the teams construct. 14013 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3] 14014 // A list item that appears in a reduction clause of a teams construct 14015 // must not appear in a firstprivate clause on a distribute construct if 14016 // any of the distribute regions arising from the distribute construct 14017 // ever bind to any of the teams regions arising from the teams construct. 14018 if ((isOpenMPWorksharingDirective(CurrDir) || 14019 isOpenMPDistributeDirective(CurrDir)) && 14020 !isOpenMPParallelDirective(CurrDir) && 14021 !isOpenMPTeamsDirective(CurrDir)) { 14022 DVar = DSAStack->getImplicitDSA(D, true); 14023 if (DVar.CKind != OMPC_shared && 14024 (isOpenMPParallelDirective(DVar.DKind) || 14025 isOpenMPTeamsDirective(DVar.DKind) || 14026 DVar.DKind == OMPD_unknown)) { 14027 Diag(ELoc, diag::err_omp_required_access) 14028 << getOpenMPClauseName(OMPC_firstprivate) 14029 << getOpenMPClauseName(OMPC_shared); 14030 reportOriginalDsa(*this, DSAStack, D, DVar); 14031 continue; 14032 } 14033 } 14034 // OpenMP [2.9.3.4, Restrictions, p.3] 14035 // A list item that appears in a reduction clause of a parallel construct 14036 // must not appear in a firstprivate clause on a worksharing or task 14037 // construct if any of the worksharing or task regions arising from the 14038 // worksharing or task construct ever bind to any of the parallel regions 14039 // arising from the parallel construct. 14040 // OpenMP [2.9.3.4, Restrictions, p.4] 14041 // A list item that appears in a reduction clause in worksharing 14042 // construct must not appear in a firstprivate clause in a task construct 14043 // encountered during execution of any of the worksharing regions arising 14044 // from the worksharing construct. 14045 if (isOpenMPTaskingDirective(CurrDir)) { 14046 DVar = DSAStack->hasInnermostDSA( 14047 D, [](OpenMPClauseKind C) { return C == OMPC_reduction; }, 14048 [](OpenMPDirectiveKind K) { 14049 return isOpenMPParallelDirective(K) || 14050 isOpenMPWorksharingDirective(K) || 14051 isOpenMPTeamsDirective(K); 14052 }, 14053 /*FromParent=*/true); 14054 if (DVar.CKind == OMPC_reduction && 14055 (isOpenMPParallelDirective(DVar.DKind) || 14056 isOpenMPWorksharingDirective(DVar.DKind) || 14057 isOpenMPTeamsDirective(DVar.DKind))) { 14058 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate) 14059 << getOpenMPDirectiveName(DVar.DKind); 14060 reportOriginalDsa(*this, DSAStack, D, DVar); 14061 continue; 14062 } 14063 } 14064 14065 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 14066 // A list item cannot appear in both a map clause and a data-sharing 14067 // attribute clause on the same construct 14068 // 14069 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7] 14070 // A list item cannot appear in both a map clause and a data-sharing 14071 // attribute clause on the same construct unless the construct is a 14072 // combined construct. 14073 if ((LangOpts.OpenMP <= 45 && 14074 isOpenMPTargetExecutionDirective(CurrDir)) || 14075 CurrDir == OMPD_target) { 14076 OpenMPClauseKind ConflictKind; 14077 if (DSAStack->checkMappableExprComponentListsForDecl( 14078 VD, /*CurrentRegionOnly=*/true, 14079 [&ConflictKind]( 14080 OMPClauseMappableExprCommon::MappableExprComponentListRef, 14081 OpenMPClauseKind WhereFoundClauseKind) { 14082 ConflictKind = WhereFoundClauseKind; 14083 return true; 14084 })) { 14085 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 14086 << getOpenMPClauseName(OMPC_firstprivate) 14087 << getOpenMPClauseName(ConflictKind) 14088 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 14089 reportOriginalDsa(*this, DSAStack, D, DVar); 14090 continue; 14091 } 14092 } 14093 } 14094 14095 // Variably modified types are not supported for tasks. 14096 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && 14097 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) { 14098 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 14099 << getOpenMPClauseName(OMPC_firstprivate) << Type 14100 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 14101 bool IsDecl = 14102 !VD || 14103 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 14104 Diag(D->getLocation(), 14105 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 14106 << D; 14107 continue; 14108 } 14109 14110 Type = Type.getUnqualifiedType(); 14111 VarDecl *VDPrivate = 14112 buildVarDecl(*this, ELoc, Type, D->getName(), 14113 D->hasAttrs() ? &D->getAttrs() : nullptr, 14114 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 14115 // Generate helper private variable and initialize it with the value of the 14116 // original variable. The address of the original variable is replaced by 14117 // the address of the new private variable in the CodeGen. This new variable 14118 // is not added to IdResolver, so the code in the OpenMP region uses 14119 // original variable for proper diagnostics and variable capturing. 14120 Expr *VDInitRefExpr = nullptr; 14121 // For arrays generate initializer for single element and replace it by the 14122 // original array element in CodeGen. 14123 if (Type->isArrayType()) { 14124 VarDecl *VDInit = 14125 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName()); 14126 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc); 14127 Expr *Init = DefaultLvalueConversion(VDInitRefExpr).get(); 14128 ElemType = ElemType.getUnqualifiedType(); 14129 VarDecl *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, 14130 ".firstprivate.temp"); 14131 InitializedEntity Entity = 14132 InitializedEntity::InitializeVariable(VDInitTemp); 14133 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc); 14134 14135 InitializationSequence InitSeq(*this, Entity, Kind, Init); 14136 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init); 14137 if (Result.isInvalid()) 14138 VDPrivate->setInvalidDecl(); 14139 else 14140 VDPrivate->setInit(Result.getAs<Expr>()); 14141 // Remove temp variable declaration. 14142 Context.Deallocate(VDInitTemp); 14143 } else { 14144 VarDecl *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type, 14145 ".firstprivate.temp"); 14146 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(), 14147 RefExpr->getExprLoc()); 14148 AddInitializerToDecl(VDPrivate, 14149 DefaultLvalueConversion(VDInitRefExpr).get(), 14150 /*DirectInit=*/false); 14151 } 14152 if (VDPrivate->isInvalidDecl()) { 14153 if (IsImplicitClause) { 14154 Diag(RefExpr->getExprLoc(), 14155 diag::note_omp_task_predetermined_firstprivate_here); 14156 } 14157 continue; 14158 } 14159 CurContext->addDecl(VDPrivate); 14160 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 14161 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), 14162 RefExpr->getExprLoc()); 14163 DeclRefExpr *Ref = nullptr; 14164 if (!VD && !CurContext->isDependentContext()) { 14165 if (TopDVar.CKind == OMPC_lastprivate) { 14166 Ref = TopDVar.PrivateCopy; 14167 } else { 14168 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 14169 if (!isOpenMPCapturedDecl(D)) 14170 ExprCaptures.push_back(Ref->getDecl()); 14171 } 14172 } 14173 if (!IsImplicitClause) 14174 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 14175 Vars.push_back((VD || CurContext->isDependentContext()) 14176 ? RefExpr->IgnoreParens() 14177 : Ref); 14178 PrivateCopies.push_back(VDPrivateRefExpr); 14179 Inits.push_back(VDInitRefExpr); 14180 } 14181 14182 if (Vars.empty()) 14183 return nullptr; 14184 14185 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 14186 Vars, PrivateCopies, Inits, 14187 buildPreInits(Context, ExprCaptures)); 14188 } 14189 14190 OMPClause *Sema::ActOnOpenMPLastprivateClause( 14191 ArrayRef<Expr *> VarList, OpenMPLastprivateModifier LPKind, 14192 SourceLocation LPKindLoc, SourceLocation ColonLoc, SourceLocation StartLoc, 14193 SourceLocation LParenLoc, SourceLocation EndLoc) { 14194 if (LPKind == OMPC_LASTPRIVATE_unknown && LPKindLoc.isValid()) { 14195 assert(ColonLoc.isValid() && "Colon location must be valid."); 14196 Diag(LPKindLoc, diag::err_omp_unexpected_clause_value) 14197 << getListOfPossibleValues(OMPC_lastprivate, /*First=*/0, 14198 /*Last=*/OMPC_LASTPRIVATE_unknown) 14199 << getOpenMPClauseName(OMPC_lastprivate); 14200 return nullptr; 14201 } 14202 14203 SmallVector<Expr *, 8> Vars; 14204 SmallVector<Expr *, 8> SrcExprs; 14205 SmallVector<Expr *, 8> DstExprs; 14206 SmallVector<Expr *, 8> AssignmentOps; 14207 SmallVector<Decl *, 4> ExprCaptures; 14208 SmallVector<Expr *, 4> ExprPostUpdates; 14209 for (Expr *RefExpr : VarList) { 14210 assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); 14211 SourceLocation ELoc; 14212 SourceRange ERange; 14213 Expr *SimpleRefExpr = RefExpr; 14214 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 14215 if (Res.second) { 14216 // It will be analyzed later. 14217 Vars.push_back(RefExpr); 14218 SrcExprs.push_back(nullptr); 14219 DstExprs.push_back(nullptr); 14220 AssignmentOps.push_back(nullptr); 14221 } 14222 ValueDecl *D = Res.first; 14223 if (!D) 14224 continue; 14225 14226 QualType Type = D->getType(); 14227 auto *VD = dyn_cast<VarDecl>(D); 14228 14229 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2] 14230 // A variable that appears in a lastprivate clause must not have an 14231 // incomplete type or a reference type. 14232 if (RequireCompleteType(ELoc, Type, 14233 diag::err_omp_lastprivate_incomplete_type)) 14234 continue; 14235 Type = Type.getNonReferenceType(); 14236 14237 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions] 14238 // A variable that is privatized must not have a const-qualified type 14239 // unless it is of class type with a mutable member. This restriction does 14240 // not apply to the firstprivate clause. 14241 // 14242 // OpenMP 3.1 [2.9.3.5, lastprivate clause, Restrictions] 14243 // A variable that appears in a lastprivate clause must not have a 14244 // const-qualified type unless it is of class type with a mutable member. 14245 if (rejectConstNotMutableType(*this, D, Type, OMPC_lastprivate, ELoc)) 14246 continue; 14247 14248 // OpenMP 5.0 [2.19.4.5 lastprivate Clause, Restrictions] 14249 // A list item that appears in a lastprivate clause with the conditional 14250 // modifier must be a scalar variable. 14251 if (LPKind == OMPC_LASTPRIVATE_conditional && !Type->isScalarType()) { 14252 Diag(ELoc, diag::err_omp_lastprivate_conditional_non_scalar); 14253 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 14254 VarDecl::DeclarationOnly; 14255 Diag(D->getLocation(), 14256 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 14257 << D; 14258 continue; 14259 } 14260 14261 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 14262 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 14263 // in a Construct] 14264 // Variables with the predetermined data-sharing attributes may not be 14265 // listed in data-sharing attributes clauses, except for the cases 14266 // listed below. 14267 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3] 14268 // A list item may appear in a firstprivate or lastprivate clause but not 14269 // both. 14270 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 14271 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate && 14272 (isOpenMPDistributeDirective(CurrDir) || 14273 DVar.CKind != OMPC_firstprivate) && 14274 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) { 14275 Diag(ELoc, diag::err_omp_wrong_dsa) 14276 << getOpenMPClauseName(DVar.CKind) 14277 << getOpenMPClauseName(OMPC_lastprivate); 14278 reportOriginalDsa(*this, DSAStack, D, DVar); 14279 continue; 14280 } 14281 14282 // OpenMP [2.14.3.5, Restrictions, p.2] 14283 // A list item that is private within a parallel region, or that appears in 14284 // the reduction clause of a parallel construct, must not appear in a 14285 // lastprivate clause on a worksharing construct if any of the corresponding 14286 // worksharing regions ever binds to any of the corresponding parallel 14287 // regions. 14288 DSAStackTy::DSAVarData TopDVar = DVar; 14289 if (isOpenMPWorksharingDirective(CurrDir) && 14290 !isOpenMPParallelDirective(CurrDir) && 14291 !isOpenMPTeamsDirective(CurrDir)) { 14292 DVar = DSAStack->getImplicitDSA(D, true); 14293 if (DVar.CKind != OMPC_shared) { 14294 Diag(ELoc, diag::err_omp_required_access) 14295 << getOpenMPClauseName(OMPC_lastprivate) 14296 << getOpenMPClauseName(OMPC_shared); 14297 reportOriginalDsa(*this, DSAStack, D, DVar); 14298 continue; 14299 } 14300 } 14301 14302 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2] 14303 // A variable of class type (or array thereof) that appears in a 14304 // lastprivate clause requires an accessible, unambiguous default 14305 // constructor for the class type, unless the list item is also specified 14306 // in a firstprivate clause. 14307 // A variable of class type (or array thereof) that appears in a 14308 // lastprivate clause requires an accessible, unambiguous copy assignment 14309 // operator for the class type. 14310 Type = Context.getBaseElementType(Type).getNonReferenceType(); 14311 VarDecl *SrcVD = buildVarDecl(*this, ERange.getBegin(), 14312 Type.getUnqualifiedType(), ".lastprivate.src", 14313 D->hasAttrs() ? &D->getAttrs() : nullptr); 14314 DeclRefExpr *PseudoSrcExpr = 14315 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc); 14316 VarDecl *DstVD = 14317 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst", 14318 D->hasAttrs() ? &D->getAttrs() : nullptr); 14319 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc); 14320 // For arrays generate assignment operation for single element and replace 14321 // it by the original array element in CodeGen. 14322 ExprResult AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign, 14323 PseudoDstExpr, PseudoSrcExpr); 14324 if (AssignmentOp.isInvalid()) 14325 continue; 14326 AssignmentOp = 14327 ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false); 14328 if (AssignmentOp.isInvalid()) 14329 continue; 14330 14331 DeclRefExpr *Ref = nullptr; 14332 if (!VD && !CurContext->isDependentContext()) { 14333 if (TopDVar.CKind == OMPC_firstprivate) { 14334 Ref = TopDVar.PrivateCopy; 14335 } else { 14336 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 14337 if (!isOpenMPCapturedDecl(D)) 14338 ExprCaptures.push_back(Ref->getDecl()); 14339 } 14340 if (TopDVar.CKind == OMPC_firstprivate || 14341 (!isOpenMPCapturedDecl(D) && 14342 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) { 14343 ExprResult RefRes = DefaultLvalueConversion(Ref); 14344 if (!RefRes.isUsable()) 14345 continue; 14346 ExprResult PostUpdateRes = 14347 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr, 14348 RefRes.get()); 14349 if (!PostUpdateRes.isUsable()) 14350 continue; 14351 ExprPostUpdates.push_back( 14352 IgnoredValueConversions(PostUpdateRes.get()).get()); 14353 } 14354 } 14355 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref); 14356 Vars.push_back((VD || CurContext->isDependentContext()) 14357 ? RefExpr->IgnoreParens() 14358 : Ref); 14359 SrcExprs.push_back(PseudoSrcExpr); 14360 DstExprs.push_back(PseudoDstExpr); 14361 AssignmentOps.push_back(AssignmentOp.get()); 14362 } 14363 14364 if (Vars.empty()) 14365 return nullptr; 14366 14367 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 14368 Vars, SrcExprs, DstExprs, AssignmentOps, 14369 LPKind, LPKindLoc, ColonLoc, 14370 buildPreInits(Context, ExprCaptures), 14371 buildPostUpdate(*this, ExprPostUpdates)); 14372 } 14373 14374 OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList, 14375 SourceLocation StartLoc, 14376 SourceLocation LParenLoc, 14377 SourceLocation EndLoc) { 14378 SmallVector<Expr *, 8> Vars; 14379 for (Expr *RefExpr : VarList) { 14380 assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); 14381 SourceLocation ELoc; 14382 SourceRange ERange; 14383 Expr *SimpleRefExpr = RefExpr; 14384 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 14385 if (Res.second) { 14386 // It will be analyzed later. 14387 Vars.push_back(RefExpr); 14388 } 14389 ValueDecl *D = Res.first; 14390 if (!D) 14391 continue; 14392 14393 auto *VD = dyn_cast<VarDecl>(D); 14394 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 14395 // in a Construct] 14396 // Variables with the predetermined data-sharing attributes may not be 14397 // listed in data-sharing attributes clauses, except for the cases 14398 // listed below. For these exceptions only, listing a predetermined 14399 // variable in a data-sharing attribute clause is allowed and overrides 14400 // the variable's predetermined data-sharing attributes. 14401 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 14402 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared && 14403 DVar.RefExpr) { 14404 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 14405 << getOpenMPClauseName(OMPC_shared); 14406 reportOriginalDsa(*this, DSAStack, D, DVar); 14407 continue; 14408 } 14409 14410 DeclRefExpr *Ref = nullptr; 14411 if (!VD && isOpenMPCapturedDecl(D) && !CurContext->isDependentContext()) 14412 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 14413 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref); 14414 Vars.push_back((VD || !Ref || CurContext->isDependentContext()) 14415 ? RefExpr->IgnoreParens() 14416 : Ref); 14417 } 14418 14419 if (Vars.empty()) 14420 return nullptr; 14421 14422 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 14423 } 14424 14425 namespace { 14426 class DSARefChecker : public StmtVisitor<DSARefChecker, bool> { 14427 DSAStackTy *Stack; 14428 14429 public: 14430 bool VisitDeclRefExpr(DeclRefExpr *E) { 14431 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 14432 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false); 14433 if (DVar.CKind == OMPC_shared && !DVar.RefExpr) 14434 return false; 14435 if (DVar.CKind != OMPC_unknown) 14436 return true; 14437 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA( 14438 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) { return true; }, 14439 /*FromParent=*/true); 14440 return DVarPrivate.CKind != OMPC_unknown; 14441 } 14442 return false; 14443 } 14444 bool VisitStmt(Stmt *S) { 14445 for (Stmt *Child : S->children()) { 14446 if (Child && Visit(Child)) 14447 return true; 14448 } 14449 return false; 14450 } 14451 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {} 14452 }; 14453 } // namespace 14454 14455 namespace { 14456 // Transform MemberExpression for specified FieldDecl of current class to 14457 // DeclRefExpr to specified OMPCapturedExprDecl. 14458 class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> { 14459 typedef TreeTransform<TransformExprToCaptures> BaseTransform; 14460 ValueDecl *Field = nullptr; 14461 DeclRefExpr *CapturedExpr = nullptr; 14462 14463 public: 14464 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl) 14465 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {} 14466 14467 ExprResult TransformMemberExpr(MemberExpr *E) { 14468 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) && 14469 E->getMemberDecl() == Field) { 14470 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false); 14471 return CapturedExpr; 14472 } 14473 return BaseTransform::TransformMemberExpr(E); 14474 } 14475 DeclRefExpr *getCapturedExpr() { return CapturedExpr; } 14476 }; 14477 } // namespace 14478 14479 template <typename T, typename U> 14480 static T filterLookupForUDReductionAndMapper( 14481 SmallVectorImpl<U> &Lookups, const llvm::function_ref<T(ValueDecl *)> Gen) { 14482 for (U &Set : Lookups) { 14483 for (auto *D : Set) { 14484 if (T Res = Gen(cast<ValueDecl>(D))) 14485 return Res; 14486 } 14487 } 14488 return T(); 14489 } 14490 14491 static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D) { 14492 assert(!LookupResult::isVisible(SemaRef, D) && "not in slow case"); 14493 14494 for (auto RD : D->redecls()) { 14495 // Don't bother with extra checks if we already know this one isn't visible. 14496 if (RD == D) 14497 continue; 14498 14499 auto ND = cast<NamedDecl>(RD); 14500 if (LookupResult::isVisible(SemaRef, ND)) 14501 return ND; 14502 } 14503 14504 return nullptr; 14505 } 14506 14507 static void 14508 argumentDependentLookup(Sema &SemaRef, const DeclarationNameInfo &Id, 14509 SourceLocation Loc, QualType Ty, 14510 SmallVectorImpl<UnresolvedSet<8>> &Lookups) { 14511 // Find all of the associated namespaces and classes based on the 14512 // arguments we have. 14513 Sema::AssociatedNamespaceSet AssociatedNamespaces; 14514 Sema::AssociatedClassSet AssociatedClasses; 14515 OpaqueValueExpr OVE(Loc, Ty, VK_LValue); 14516 SemaRef.FindAssociatedClassesAndNamespaces(Loc, &OVE, AssociatedNamespaces, 14517 AssociatedClasses); 14518 14519 // C++ [basic.lookup.argdep]p3: 14520 // Let X be the lookup set produced by unqualified lookup (3.4.1) 14521 // and let Y be the lookup set produced by argument dependent 14522 // lookup (defined as follows). If X contains [...] then Y is 14523 // empty. Otherwise Y is the set of declarations found in the 14524 // namespaces associated with the argument types as described 14525 // below. The set of declarations found by the lookup of the name 14526 // is the union of X and Y. 14527 // 14528 // Here, we compute Y and add its members to the overloaded 14529 // candidate set. 14530 for (auto *NS : AssociatedNamespaces) { 14531 // When considering an associated namespace, the lookup is the 14532 // same as the lookup performed when the associated namespace is 14533 // used as a qualifier (3.4.3.2) except that: 14534 // 14535 // -- Any using-directives in the associated namespace are 14536 // ignored. 14537 // 14538 // -- Any namespace-scope friend functions declared in 14539 // associated classes are visible within their respective 14540 // namespaces even if they are not visible during an ordinary 14541 // lookup (11.4). 14542 DeclContext::lookup_result R = NS->lookup(Id.getName()); 14543 for (auto *D : R) { 14544 auto *Underlying = D; 14545 if (auto *USD = dyn_cast<UsingShadowDecl>(D)) 14546 Underlying = USD->getTargetDecl(); 14547 14548 if (!isa<OMPDeclareReductionDecl>(Underlying) && 14549 !isa<OMPDeclareMapperDecl>(Underlying)) 14550 continue; 14551 14552 if (!SemaRef.isVisible(D)) { 14553 D = findAcceptableDecl(SemaRef, D); 14554 if (!D) 14555 continue; 14556 if (auto *USD = dyn_cast<UsingShadowDecl>(D)) 14557 Underlying = USD->getTargetDecl(); 14558 } 14559 Lookups.emplace_back(); 14560 Lookups.back().addDecl(Underlying); 14561 } 14562 } 14563 } 14564 14565 static ExprResult 14566 buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range, 14567 Scope *S, CXXScopeSpec &ReductionIdScopeSpec, 14568 const DeclarationNameInfo &ReductionId, QualType Ty, 14569 CXXCastPath &BasePath, Expr *UnresolvedReduction) { 14570 if (ReductionIdScopeSpec.isInvalid()) 14571 return ExprError(); 14572 SmallVector<UnresolvedSet<8>, 4> Lookups; 14573 if (S) { 14574 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName); 14575 Lookup.suppressDiagnostics(); 14576 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) { 14577 NamedDecl *D = Lookup.getRepresentativeDecl(); 14578 do { 14579 S = S->getParent(); 14580 } while (S && !S->isDeclScope(D)); 14581 if (S) 14582 S = S->getParent(); 14583 Lookups.emplace_back(); 14584 Lookups.back().append(Lookup.begin(), Lookup.end()); 14585 Lookup.clear(); 14586 } 14587 } else if (auto *ULE = 14588 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) { 14589 Lookups.push_back(UnresolvedSet<8>()); 14590 Decl *PrevD = nullptr; 14591 for (NamedDecl *D : ULE->decls()) { 14592 if (D == PrevD) 14593 Lookups.push_back(UnresolvedSet<8>()); 14594 else if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(D)) 14595 Lookups.back().addDecl(DRD); 14596 PrevD = D; 14597 } 14598 } 14599 if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() || 14600 Ty->isInstantiationDependentType() || 14601 Ty->containsUnexpandedParameterPack() || 14602 filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) { 14603 return !D->isInvalidDecl() && 14604 (D->getType()->isDependentType() || 14605 D->getType()->isInstantiationDependentType() || 14606 D->getType()->containsUnexpandedParameterPack()); 14607 })) { 14608 UnresolvedSet<8> ResSet; 14609 for (const UnresolvedSet<8> &Set : Lookups) { 14610 if (Set.empty()) 14611 continue; 14612 ResSet.append(Set.begin(), Set.end()); 14613 // The last item marks the end of all declarations at the specified scope. 14614 ResSet.addDecl(Set[Set.size() - 1]); 14615 } 14616 return UnresolvedLookupExpr::Create( 14617 SemaRef.Context, /*NamingClass=*/nullptr, 14618 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId, 14619 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end()); 14620 } 14621 // Lookup inside the classes. 14622 // C++ [over.match.oper]p3: 14623 // For a unary operator @ with an operand of a type whose 14624 // cv-unqualified version is T1, and for a binary operator @ with 14625 // a left operand of a type whose cv-unqualified version is T1 and 14626 // a right operand of a type whose cv-unqualified version is T2, 14627 // three sets of candidate functions, designated member 14628 // candidates, non-member candidates and built-in candidates, are 14629 // constructed as follows: 14630 // -- If T1 is a complete class type or a class currently being 14631 // defined, the set of member candidates is the result of the 14632 // qualified lookup of T1::operator@ (13.3.1.1.1); otherwise, 14633 // the set of member candidates is empty. 14634 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName); 14635 Lookup.suppressDiagnostics(); 14636 if (const auto *TyRec = Ty->getAs<RecordType>()) { 14637 // Complete the type if it can be completed. 14638 // If the type is neither complete nor being defined, bail out now. 14639 if (SemaRef.isCompleteType(Loc, Ty) || TyRec->isBeingDefined() || 14640 TyRec->getDecl()->getDefinition()) { 14641 Lookup.clear(); 14642 SemaRef.LookupQualifiedName(Lookup, TyRec->getDecl()); 14643 if (Lookup.empty()) { 14644 Lookups.emplace_back(); 14645 Lookups.back().append(Lookup.begin(), Lookup.end()); 14646 } 14647 } 14648 } 14649 // Perform ADL. 14650 if (SemaRef.getLangOpts().CPlusPlus) 14651 argumentDependentLookup(SemaRef, ReductionId, Loc, Ty, Lookups); 14652 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 14653 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * { 14654 if (!D->isInvalidDecl() && 14655 SemaRef.Context.hasSameType(D->getType(), Ty)) 14656 return D; 14657 return nullptr; 14658 })) 14659 return SemaRef.BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(), 14660 VK_LValue, Loc); 14661 if (SemaRef.getLangOpts().CPlusPlus) { 14662 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 14663 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * { 14664 if (!D->isInvalidDecl() && 14665 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) && 14666 !Ty.isMoreQualifiedThan(D->getType())) 14667 return D; 14668 return nullptr; 14669 })) { 14670 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, 14671 /*DetectVirtual=*/false); 14672 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) { 14673 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType( 14674 VD->getType().getUnqualifiedType()))) { 14675 if (SemaRef.CheckBaseClassAccess( 14676 Loc, VD->getType(), Ty, Paths.front(), 14677 /*DiagID=*/0) != Sema::AR_inaccessible) { 14678 SemaRef.BuildBasePathArray(Paths, BasePath); 14679 return SemaRef.BuildDeclRefExpr( 14680 VD, VD->getType().getNonReferenceType(), VK_LValue, Loc); 14681 } 14682 } 14683 } 14684 } 14685 } 14686 if (ReductionIdScopeSpec.isSet()) { 14687 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) 14688 << Ty << Range; 14689 return ExprError(); 14690 } 14691 return ExprEmpty(); 14692 } 14693 14694 namespace { 14695 /// Data for the reduction-based clauses. 14696 struct ReductionData { 14697 /// List of original reduction items. 14698 SmallVector<Expr *, 8> Vars; 14699 /// List of private copies of the reduction items. 14700 SmallVector<Expr *, 8> Privates; 14701 /// LHS expressions for the reduction_op expressions. 14702 SmallVector<Expr *, 8> LHSs; 14703 /// RHS expressions for the reduction_op expressions. 14704 SmallVector<Expr *, 8> RHSs; 14705 /// Reduction operation expression. 14706 SmallVector<Expr *, 8> ReductionOps; 14707 /// inscan copy operation expressions. 14708 SmallVector<Expr *, 8> InscanCopyOps; 14709 /// inscan copy temp array expressions for prefix sums. 14710 SmallVector<Expr *, 8> InscanCopyArrayTemps; 14711 /// inscan copy temp array element expressions for prefix sums. 14712 SmallVector<Expr *, 8> InscanCopyArrayElems; 14713 /// Taskgroup descriptors for the corresponding reduction items in 14714 /// in_reduction clauses. 14715 SmallVector<Expr *, 8> TaskgroupDescriptors; 14716 /// List of captures for clause. 14717 SmallVector<Decl *, 4> ExprCaptures; 14718 /// List of postupdate expressions. 14719 SmallVector<Expr *, 4> ExprPostUpdates; 14720 /// Reduction modifier. 14721 unsigned RedModifier = 0; 14722 ReductionData() = delete; 14723 /// Reserves required memory for the reduction data. 14724 ReductionData(unsigned Size, unsigned Modifier = 0) : RedModifier(Modifier) { 14725 Vars.reserve(Size); 14726 Privates.reserve(Size); 14727 LHSs.reserve(Size); 14728 RHSs.reserve(Size); 14729 ReductionOps.reserve(Size); 14730 if (RedModifier == OMPC_REDUCTION_inscan) { 14731 InscanCopyOps.reserve(Size); 14732 InscanCopyArrayTemps.reserve(Size); 14733 InscanCopyArrayElems.reserve(Size); 14734 } 14735 TaskgroupDescriptors.reserve(Size); 14736 ExprCaptures.reserve(Size); 14737 ExprPostUpdates.reserve(Size); 14738 } 14739 /// Stores reduction item and reduction operation only (required for dependent 14740 /// reduction item). 14741 void push(Expr *Item, Expr *ReductionOp) { 14742 Vars.emplace_back(Item); 14743 Privates.emplace_back(nullptr); 14744 LHSs.emplace_back(nullptr); 14745 RHSs.emplace_back(nullptr); 14746 ReductionOps.emplace_back(ReductionOp); 14747 TaskgroupDescriptors.emplace_back(nullptr); 14748 if (RedModifier == OMPC_REDUCTION_inscan) { 14749 InscanCopyOps.push_back(nullptr); 14750 InscanCopyArrayTemps.push_back(nullptr); 14751 InscanCopyArrayElems.push_back(nullptr); 14752 } 14753 } 14754 /// Stores reduction data. 14755 void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp, 14756 Expr *TaskgroupDescriptor, Expr *CopyOp, Expr *CopyArrayTemp, 14757 Expr *CopyArrayElem) { 14758 Vars.emplace_back(Item); 14759 Privates.emplace_back(Private); 14760 LHSs.emplace_back(LHS); 14761 RHSs.emplace_back(RHS); 14762 ReductionOps.emplace_back(ReductionOp); 14763 TaskgroupDescriptors.emplace_back(TaskgroupDescriptor); 14764 if (RedModifier == OMPC_REDUCTION_inscan) { 14765 InscanCopyOps.push_back(CopyOp); 14766 InscanCopyArrayTemps.push_back(CopyArrayTemp); 14767 InscanCopyArrayElems.push_back(CopyArrayElem); 14768 } else { 14769 assert(CopyOp == nullptr && CopyArrayTemp == nullptr && 14770 CopyArrayElem == nullptr && 14771 "Copy operation must be used for inscan reductions only."); 14772 } 14773 } 14774 }; 14775 } // namespace 14776 14777 static bool checkOMPArraySectionConstantForReduction( 14778 ASTContext &Context, const OMPArraySectionExpr *OASE, bool &SingleElement, 14779 SmallVectorImpl<llvm::APSInt> &ArraySizes) { 14780 const Expr *Length = OASE->getLength(); 14781 if (Length == nullptr) { 14782 // For array sections of the form [1:] or [:], we would need to analyze 14783 // the lower bound... 14784 if (OASE->getColonLocFirst().isValid()) 14785 return false; 14786 14787 // This is an array subscript which has implicit length 1! 14788 SingleElement = true; 14789 ArraySizes.push_back(llvm::APSInt::get(1)); 14790 } else { 14791 Expr::EvalResult Result; 14792 if (!Length->EvaluateAsInt(Result, Context)) 14793 return false; 14794 14795 llvm::APSInt ConstantLengthValue = Result.Val.getInt(); 14796 SingleElement = (ConstantLengthValue.getSExtValue() == 1); 14797 ArraySizes.push_back(ConstantLengthValue); 14798 } 14799 14800 // Get the base of this array section and walk up from there. 14801 const Expr *Base = OASE->getBase()->IgnoreParenImpCasts(); 14802 14803 // We require length = 1 for all array sections except the right-most to 14804 // guarantee that the memory region is contiguous and has no holes in it. 14805 while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) { 14806 Length = TempOASE->getLength(); 14807 if (Length == nullptr) { 14808 // For array sections of the form [1:] or [:], we would need to analyze 14809 // the lower bound... 14810 if (OASE->getColonLocFirst().isValid()) 14811 return false; 14812 14813 // This is an array subscript which has implicit length 1! 14814 ArraySizes.push_back(llvm::APSInt::get(1)); 14815 } else { 14816 Expr::EvalResult Result; 14817 if (!Length->EvaluateAsInt(Result, Context)) 14818 return false; 14819 14820 llvm::APSInt ConstantLengthValue = Result.Val.getInt(); 14821 if (ConstantLengthValue.getSExtValue() != 1) 14822 return false; 14823 14824 ArraySizes.push_back(ConstantLengthValue); 14825 } 14826 Base = TempOASE->getBase()->IgnoreParenImpCasts(); 14827 } 14828 14829 // If we have a single element, we don't need to add the implicit lengths. 14830 if (!SingleElement) { 14831 while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) { 14832 // Has implicit length 1! 14833 ArraySizes.push_back(llvm::APSInt::get(1)); 14834 Base = TempASE->getBase()->IgnoreParenImpCasts(); 14835 } 14836 } 14837 14838 // This array section can be privatized as a single value or as a constant 14839 // sized array. 14840 return true; 14841 } 14842 14843 static bool actOnOMPReductionKindClause( 14844 Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind, 14845 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 14846 SourceLocation ColonLoc, SourceLocation EndLoc, 14847 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 14848 ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) { 14849 DeclarationName DN = ReductionId.getName(); 14850 OverloadedOperatorKind OOK = DN.getCXXOverloadedOperator(); 14851 BinaryOperatorKind BOK = BO_Comma; 14852 14853 ASTContext &Context = S.Context; 14854 // OpenMP [2.14.3.6, reduction clause] 14855 // C 14856 // reduction-identifier is either an identifier or one of the following 14857 // operators: +, -, *, &, |, ^, && and || 14858 // C++ 14859 // reduction-identifier is either an id-expression or one of the following 14860 // operators: +, -, *, &, |, ^, && and || 14861 switch (OOK) { 14862 case OO_Plus: 14863 case OO_Minus: 14864 BOK = BO_Add; 14865 break; 14866 case OO_Star: 14867 BOK = BO_Mul; 14868 break; 14869 case OO_Amp: 14870 BOK = BO_And; 14871 break; 14872 case OO_Pipe: 14873 BOK = BO_Or; 14874 break; 14875 case OO_Caret: 14876 BOK = BO_Xor; 14877 break; 14878 case OO_AmpAmp: 14879 BOK = BO_LAnd; 14880 break; 14881 case OO_PipePipe: 14882 BOK = BO_LOr; 14883 break; 14884 case OO_New: 14885 case OO_Delete: 14886 case OO_Array_New: 14887 case OO_Array_Delete: 14888 case OO_Slash: 14889 case OO_Percent: 14890 case OO_Tilde: 14891 case OO_Exclaim: 14892 case OO_Equal: 14893 case OO_Less: 14894 case OO_Greater: 14895 case OO_LessEqual: 14896 case OO_GreaterEqual: 14897 case OO_PlusEqual: 14898 case OO_MinusEqual: 14899 case OO_StarEqual: 14900 case OO_SlashEqual: 14901 case OO_PercentEqual: 14902 case OO_CaretEqual: 14903 case OO_AmpEqual: 14904 case OO_PipeEqual: 14905 case OO_LessLess: 14906 case OO_GreaterGreater: 14907 case OO_LessLessEqual: 14908 case OO_GreaterGreaterEqual: 14909 case OO_EqualEqual: 14910 case OO_ExclaimEqual: 14911 case OO_Spaceship: 14912 case OO_PlusPlus: 14913 case OO_MinusMinus: 14914 case OO_Comma: 14915 case OO_ArrowStar: 14916 case OO_Arrow: 14917 case OO_Call: 14918 case OO_Subscript: 14919 case OO_Conditional: 14920 case OO_Coawait: 14921 case NUM_OVERLOADED_OPERATORS: 14922 llvm_unreachable("Unexpected reduction identifier"); 14923 case OO_None: 14924 if (IdentifierInfo *II = DN.getAsIdentifierInfo()) { 14925 if (II->isStr("max")) 14926 BOK = BO_GT; 14927 else if (II->isStr("min")) 14928 BOK = BO_LT; 14929 } 14930 break; 14931 } 14932 SourceRange ReductionIdRange; 14933 if (ReductionIdScopeSpec.isValid()) 14934 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc()); 14935 else 14936 ReductionIdRange.setBegin(ReductionId.getBeginLoc()); 14937 ReductionIdRange.setEnd(ReductionId.getEndLoc()); 14938 14939 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end(); 14940 bool FirstIter = true; 14941 for (Expr *RefExpr : VarList) { 14942 assert(RefExpr && "nullptr expr in OpenMP reduction clause."); 14943 // OpenMP [2.1, C/C++] 14944 // A list item is a variable or array section, subject to the restrictions 14945 // specified in Section 2.4 on page 42 and in each of the sections 14946 // describing clauses and directives for which a list appears. 14947 // OpenMP [2.14.3.3, Restrictions, p.1] 14948 // A variable that is part of another variable (as an array or 14949 // structure element) cannot appear in a private clause. 14950 if (!FirstIter && IR != ER) 14951 ++IR; 14952 FirstIter = false; 14953 SourceLocation ELoc; 14954 SourceRange ERange; 14955 Expr *SimpleRefExpr = RefExpr; 14956 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange, 14957 /*AllowArraySection=*/true); 14958 if (Res.second) { 14959 // Try to find 'declare reduction' corresponding construct before using 14960 // builtin/overloaded operators. 14961 QualType Type = Context.DependentTy; 14962 CXXCastPath BasePath; 14963 ExprResult DeclareReductionRef = buildDeclareReductionRef( 14964 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec, 14965 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR); 14966 Expr *ReductionOp = nullptr; 14967 if (S.CurContext->isDependentContext() && 14968 (DeclareReductionRef.isUnset() || 14969 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) 14970 ReductionOp = DeclareReductionRef.get(); 14971 // It will be analyzed later. 14972 RD.push(RefExpr, ReductionOp); 14973 } 14974 ValueDecl *D = Res.first; 14975 if (!D) 14976 continue; 14977 14978 Expr *TaskgroupDescriptor = nullptr; 14979 QualType Type; 14980 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens()); 14981 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens()); 14982 if (ASE) { 14983 Type = ASE->getType().getNonReferenceType(); 14984 } else if (OASE) { 14985 QualType BaseType = 14986 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 14987 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 14988 Type = ATy->getElementType(); 14989 else 14990 Type = BaseType->getPointeeType(); 14991 Type = Type.getNonReferenceType(); 14992 } else { 14993 Type = Context.getBaseElementType(D->getType().getNonReferenceType()); 14994 } 14995 auto *VD = dyn_cast<VarDecl>(D); 14996 14997 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 14998 // A variable that appears in a private clause must not have an incomplete 14999 // type or a reference type. 15000 if (S.RequireCompleteType(ELoc, D->getType(), 15001 diag::err_omp_reduction_incomplete_type)) 15002 continue; 15003 // OpenMP [2.14.3.6, reduction clause, Restrictions] 15004 // A list item that appears in a reduction clause must not be 15005 // const-qualified. 15006 if (rejectConstNotMutableType(S, D, Type, ClauseKind, ELoc, 15007 /*AcceptIfMutable*/ false, ASE || OASE)) 15008 continue; 15009 15010 OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective(); 15011 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4] 15012 // If a list-item is a reference type then it must bind to the same object 15013 // for all threads of the team. 15014 if (!ASE && !OASE) { 15015 if (VD) { 15016 VarDecl *VDDef = VD->getDefinition(); 15017 if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) { 15018 DSARefChecker Check(Stack); 15019 if (Check.Visit(VDDef->getInit())) { 15020 S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg) 15021 << getOpenMPClauseName(ClauseKind) << ERange; 15022 S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef; 15023 continue; 15024 } 15025 } 15026 } 15027 15028 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 15029 // in a Construct] 15030 // Variables with the predetermined data-sharing attributes may not be 15031 // listed in data-sharing attributes clauses, except for the cases 15032 // listed below. For these exceptions only, listing a predetermined 15033 // variable in a data-sharing attribute clause is allowed and overrides 15034 // the variable's predetermined data-sharing attributes. 15035 // OpenMP [2.14.3.6, Restrictions, p.3] 15036 // Any number of reduction clauses can be specified on the directive, 15037 // but a list item can appear only once in the reduction clauses for that 15038 // directive. 15039 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false); 15040 if (DVar.CKind == OMPC_reduction) { 15041 S.Diag(ELoc, diag::err_omp_once_referenced) 15042 << getOpenMPClauseName(ClauseKind); 15043 if (DVar.RefExpr) 15044 S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced); 15045 continue; 15046 } 15047 if (DVar.CKind != OMPC_unknown) { 15048 S.Diag(ELoc, diag::err_omp_wrong_dsa) 15049 << getOpenMPClauseName(DVar.CKind) 15050 << getOpenMPClauseName(OMPC_reduction); 15051 reportOriginalDsa(S, Stack, D, DVar); 15052 continue; 15053 } 15054 15055 // OpenMP [2.14.3.6, Restrictions, p.1] 15056 // A list item that appears in a reduction clause of a worksharing 15057 // construct must be shared in the parallel regions to which any of the 15058 // worksharing regions arising from the worksharing construct bind. 15059 if (isOpenMPWorksharingDirective(CurrDir) && 15060 !isOpenMPParallelDirective(CurrDir) && 15061 !isOpenMPTeamsDirective(CurrDir)) { 15062 DVar = Stack->getImplicitDSA(D, true); 15063 if (DVar.CKind != OMPC_shared) { 15064 S.Diag(ELoc, diag::err_omp_required_access) 15065 << getOpenMPClauseName(OMPC_reduction) 15066 << getOpenMPClauseName(OMPC_shared); 15067 reportOriginalDsa(S, Stack, D, DVar); 15068 continue; 15069 } 15070 } 15071 } 15072 15073 // Try to find 'declare reduction' corresponding construct before using 15074 // builtin/overloaded operators. 15075 CXXCastPath BasePath; 15076 ExprResult DeclareReductionRef = buildDeclareReductionRef( 15077 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec, 15078 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR); 15079 if (DeclareReductionRef.isInvalid()) 15080 continue; 15081 if (S.CurContext->isDependentContext() && 15082 (DeclareReductionRef.isUnset() || 15083 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) { 15084 RD.push(RefExpr, DeclareReductionRef.get()); 15085 continue; 15086 } 15087 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) { 15088 // Not allowed reduction identifier is found. 15089 S.Diag(ReductionId.getBeginLoc(), 15090 diag::err_omp_unknown_reduction_identifier) 15091 << Type << ReductionIdRange; 15092 continue; 15093 } 15094 15095 // OpenMP [2.14.3.6, reduction clause, Restrictions] 15096 // The type of a list item that appears in a reduction clause must be valid 15097 // for the reduction-identifier. For a max or min reduction in C, the type 15098 // of the list item must be an allowed arithmetic data type: char, int, 15099 // float, double, or _Bool, possibly modified with long, short, signed, or 15100 // unsigned. For a max or min reduction in C++, the type of the list item 15101 // must be an allowed arithmetic data type: char, wchar_t, int, float, 15102 // double, or bool, possibly modified with long, short, signed, or unsigned. 15103 if (DeclareReductionRef.isUnset()) { 15104 if ((BOK == BO_GT || BOK == BO_LT) && 15105 !(Type->isScalarType() || 15106 (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) { 15107 S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg) 15108 << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus; 15109 if (!ASE && !OASE) { 15110 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 15111 VarDecl::DeclarationOnly; 15112 S.Diag(D->getLocation(), 15113 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 15114 << D; 15115 } 15116 continue; 15117 } 15118 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) && 15119 !S.getLangOpts().CPlusPlus && Type->isFloatingType()) { 15120 S.Diag(ELoc, diag::err_omp_clause_floating_type_arg) 15121 << getOpenMPClauseName(ClauseKind); 15122 if (!ASE && !OASE) { 15123 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 15124 VarDecl::DeclarationOnly; 15125 S.Diag(D->getLocation(), 15126 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 15127 << D; 15128 } 15129 continue; 15130 } 15131 } 15132 15133 Type = Type.getNonLValueExprType(Context).getUnqualifiedType(); 15134 VarDecl *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs", 15135 D->hasAttrs() ? &D->getAttrs() : nullptr); 15136 VarDecl *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(), 15137 D->hasAttrs() ? &D->getAttrs() : nullptr); 15138 QualType PrivateTy = Type; 15139 15140 // Try if we can determine constant lengths for all array sections and avoid 15141 // the VLA. 15142 bool ConstantLengthOASE = false; 15143 if (OASE) { 15144 bool SingleElement; 15145 llvm::SmallVector<llvm::APSInt, 4> ArraySizes; 15146 ConstantLengthOASE = checkOMPArraySectionConstantForReduction( 15147 Context, OASE, SingleElement, ArraySizes); 15148 15149 // If we don't have a single element, we must emit a constant array type. 15150 if (ConstantLengthOASE && !SingleElement) { 15151 for (llvm::APSInt &Size : ArraySizes) 15152 PrivateTy = Context.getConstantArrayType(PrivateTy, Size, nullptr, 15153 ArrayType::Normal, 15154 /*IndexTypeQuals=*/0); 15155 } 15156 } 15157 15158 if ((OASE && !ConstantLengthOASE) || 15159 (!OASE && !ASE && 15160 D->getType().getNonReferenceType()->isVariablyModifiedType())) { 15161 if (!Context.getTargetInfo().isVLASupported()) { 15162 if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective())) { 15163 S.Diag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE; 15164 S.Diag(ELoc, diag::note_vla_unsupported); 15165 continue; 15166 } else { 15167 S.targetDiag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE; 15168 S.targetDiag(ELoc, diag::note_vla_unsupported); 15169 } 15170 } 15171 // For arrays/array sections only: 15172 // Create pseudo array type for private copy. The size for this array will 15173 // be generated during codegen. 15174 // For array subscripts or single variables Private Ty is the same as Type 15175 // (type of the variable or single array element). 15176 PrivateTy = Context.getVariableArrayType( 15177 Type, 15178 new (Context) OpaqueValueExpr(ELoc, Context.getSizeType(), VK_RValue), 15179 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange()); 15180 } else if (!ASE && !OASE && 15181 Context.getAsArrayType(D->getType().getNonReferenceType())) { 15182 PrivateTy = D->getType().getNonReferenceType(); 15183 } 15184 // Private copy. 15185 VarDecl *PrivateVD = 15186 buildVarDecl(S, ELoc, PrivateTy, D->getName(), 15187 D->hasAttrs() ? &D->getAttrs() : nullptr, 15188 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 15189 // Add initializer for private variable. 15190 Expr *Init = nullptr; 15191 DeclRefExpr *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc); 15192 DeclRefExpr *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc); 15193 if (DeclareReductionRef.isUsable()) { 15194 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>(); 15195 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl()); 15196 if (DRD->getInitializer()) { 15197 S.ActOnUninitializedDecl(PrivateVD); 15198 Init = DRDRef; 15199 RHSVD->setInit(DRDRef); 15200 RHSVD->setInitStyle(VarDecl::CallInit); 15201 } 15202 } else { 15203 switch (BOK) { 15204 case BO_Add: 15205 case BO_Xor: 15206 case BO_Or: 15207 case BO_LOr: 15208 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'. 15209 if (Type->isScalarType() || Type->isAnyComplexType()) 15210 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get(); 15211 break; 15212 case BO_Mul: 15213 case BO_LAnd: 15214 if (Type->isScalarType() || Type->isAnyComplexType()) { 15215 // '*' and '&&' reduction ops - initializer is '1'. 15216 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get(); 15217 } 15218 break; 15219 case BO_And: { 15220 // '&' reduction op - initializer is '~0'. 15221 QualType OrigType = Type; 15222 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) 15223 Type = ComplexTy->getElementType(); 15224 if (Type->isRealFloatingType()) { 15225 llvm::APFloat InitValue = llvm::APFloat::getAllOnesValue( 15226 Context.getFloatTypeSemantics(Type), 15227 Context.getTypeSize(Type)); 15228 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, 15229 Type, ELoc); 15230 } else if (Type->isScalarType()) { 15231 uint64_t Size = Context.getTypeSize(Type); 15232 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0); 15233 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size); 15234 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); 15235 } 15236 if (Init && OrigType->isAnyComplexType()) { 15237 // Init = 0xFFFF + 0xFFFFi; 15238 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType); 15239 Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get(); 15240 } 15241 Type = OrigType; 15242 break; 15243 } 15244 case BO_LT: 15245 case BO_GT: { 15246 // 'min' reduction op - initializer is 'Largest representable number in 15247 // the reduction list item type'. 15248 // 'max' reduction op - initializer is 'Least representable number in 15249 // the reduction list item type'. 15250 if (Type->isIntegerType() || Type->isPointerType()) { 15251 bool IsSigned = Type->hasSignedIntegerRepresentation(); 15252 uint64_t Size = Context.getTypeSize(Type); 15253 QualType IntTy = 15254 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned); 15255 llvm::APInt InitValue = 15256 (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size) 15257 : llvm::APInt::getMinValue(Size) 15258 : IsSigned ? llvm::APInt::getSignedMaxValue(Size) 15259 : llvm::APInt::getMaxValue(Size); 15260 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); 15261 if (Type->isPointerType()) { 15262 // Cast to pointer type. 15263 ExprResult CastExpr = S.BuildCStyleCastExpr( 15264 ELoc, Context.getTrivialTypeSourceInfo(Type, ELoc), ELoc, Init); 15265 if (CastExpr.isInvalid()) 15266 continue; 15267 Init = CastExpr.get(); 15268 } 15269 } else if (Type->isRealFloatingType()) { 15270 llvm::APFloat InitValue = llvm::APFloat::getLargest( 15271 Context.getFloatTypeSemantics(Type), BOK != BO_LT); 15272 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, 15273 Type, ELoc); 15274 } 15275 break; 15276 } 15277 case BO_PtrMemD: 15278 case BO_PtrMemI: 15279 case BO_MulAssign: 15280 case BO_Div: 15281 case BO_Rem: 15282 case BO_Sub: 15283 case BO_Shl: 15284 case BO_Shr: 15285 case BO_LE: 15286 case BO_GE: 15287 case BO_EQ: 15288 case BO_NE: 15289 case BO_Cmp: 15290 case BO_AndAssign: 15291 case BO_XorAssign: 15292 case BO_OrAssign: 15293 case BO_Assign: 15294 case BO_AddAssign: 15295 case BO_SubAssign: 15296 case BO_DivAssign: 15297 case BO_RemAssign: 15298 case BO_ShlAssign: 15299 case BO_ShrAssign: 15300 case BO_Comma: 15301 llvm_unreachable("Unexpected reduction operation"); 15302 } 15303 } 15304 if (Init && DeclareReductionRef.isUnset()) { 15305 S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false); 15306 // Store initializer for single element in private copy. Will be used 15307 // during codegen. 15308 PrivateVD->setInit(RHSVD->getInit()); 15309 PrivateVD->setInitStyle(RHSVD->getInitStyle()); 15310 } else if (!Init) { 15311 S.ActOnUninitializedDecl(RHSVD); 15312 // Store initializer for single element in private copy. Will be used 15313 // during codegen. 15314 PrivateVD->setInit(RHSVD->getInit()); 15315 PrivateVD->setInitStyle(RHSVD->getInitStyle()); 15316 } 15317 if (RHSVD->isInvalidDecl()) 15318 continue; 15319 if (!RHSVD->hasInit() && 15320 (DeclareReductionRef.isUnset() || !S.LangOpts.CPlusPlus)) { 15321 S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible) 15322 << Type << ReductionIdRange; 15323 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 15324 VarDecl::DeclarationOnly; 15325 S.Diag(D->getLocation(), 15326 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 15327 << D; 15328 continue; 15329 } 15330 DeclRefExpr *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc); 15331 ExprResult ReductionOp; 15332 if (DeclareReductionRef.isUsable()) { 15333 QualType RedTy = DeclareReductionRef.get()->getType(); 15334 QualType PtrRedTy = Context.getPointerType(RedTy); 15335 ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE); 15336 ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE); 15337 if (!BasePath.empty()) { 15338 LHS = S.DefaultLvalueConversion(LHS.get()); 15339 RHS = S.DefaultLvalueConversion(RHS.get()); 15340 LHS = ImplicitCastExpr::Create(Context, PtrRedTy, 15341 CK_UncheckedDerivedToBase, LHS.get(), 15342 &BasePath, LHS.get()->getValueKind()); 15343 RHS = ImplicitCastExpr::Create(Context, PtrRedTy, 15344 CK_UncheckedDerivedToBase, RHS.get(), 15345 &BasePath, RHS.get()->getValueKind()); 15346 } 15347 FunctionProtoType::ExtProtoInfo EPI; 15348 QualType Params[] = {PtrRedTy, PtrRedTy}; 15349 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI); 15350 auto *OVE = new (Context) OpaqueValueExpr( 15351 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary, 15352 S.DefaultLvalueConversion(DeclareReductionRef.get()).get()); 15353 Expr *Args[] = {LHS.get(), RHS.get()}; 15354 ReductionOp = 15355 CallExpr::Create(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc, 15356 S.CurFPFeatureOverrides()); 15357 } else { 15358 ReductionOp = S.BuildBinOp( 15359 Stack->getCurScope(), ReductionId.getBeginLoc(), BOK, LHSDRE, RHSDRE); 15360 if (ReductionOp.isUsable()) { 15361 if (BOK != BO_LT && BOK != BO_GT) { 15362 ReductionOp = 15363 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(), 15364 BO_Assign, LHSDRE, ReductionOp.get()); 15365 } else { 15366 auto *ConditionalOp = new (Context) 15367 ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc, RHSDRE, 15368 Type, VK_LValue, OK_Ordinary); 15369 ReductionOp = 15370 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(), 15371 BO_Assign, LHSDRE, ConditionalOp); 15372 } 15373 if (ReductionOp.isUsable()) 15374 ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get(), 15375 /*DiscardedValue*/ false); 15376 } 15377 if (!ReductionOp.isUsable()) 15378 continue; 15379 } 15380 15381 // Add copy operations for inscan reductions. 15382 // LHS = RHS; 15383 ExprResult CopyOpRes, TempArrayRes, TempArrayElem; 15384 if (ClauseKind == OMPC_reduction && 15385 RD.RedModifier == OMPC_REDUCTION_inscan) { 15386 ExprResult RHS = S.DefaultLvalueConversion(RHSDRE); 15387 CopyOpRes = S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, LHSDRE, 15388 RHS.get()); 15389 if (!CopyOpRes.isUsable()) 15390 continue; 15391 CopyOpRes = 15392 S.ActOnFinishFullExpr(CopyOpRes.get(), /*DiscardedValue=*/true); 15393 if (!CopyOpRes.isUsable()) 15394 continue; 15395 // For simd directive and simd-based directives in simd mode no need to 15396 // construct temp array, need just a single temp element. 15397 if (Stack->getCurrentDirective() == OMPD_simd || 15398 (S.getLangOpts().OpenMPSimd && 15399 isOpenMPSimdDirective(Stack->getCurrentDirective()))) { 15400 VarDecl *TempArrayVD = 15401 buildVarDecl(S, ELoc, PrivateTy, D->getName(), 15402 D->hasAttrs() ? &D->getAttrs() : nullptr); 15403 // Add a constructor to the temp decl. 15404 S.ActOnUninitializedDecl(TempArrayVD); 15405 TempArrayRes = buildDeclRefExpr(S, TempArrayVD, PrivateTy, ELoc); 15406 } else { 15407 // Build temp array for prefix sum. 15408 auto *Dim = new (S.Context) 15409 OpaqueValueExpr(ELoc, S.Context.getSizeType(), VK_RValue); 15410 QualType ArrayTy = 15411 S.Context.getVariableArrayType(PrivateTy, Dim, ArrayType::Normal, 15412 /*IndexTypeQuals=*/0, {ELoc, ELoc}); 15413 VarDecl *TempArrayVD = 15414 buildVarDecl(S, ELoc, ArrayTy, D->getName(), 15415 D->hasAttrs() ? &D->getAttrs() : nullptr); 15416 // Add a constructor to the temp decl. 15417 S.ActOnUninitializedDecl(TempArrayVD); 15418 TempArrayRes = buildDeclRefExpr(S, TempArrayVD, ArrayTy, ELoc); 15419 TempArrayElem = 15420 S.DefaultFunctionArrayLvalueConversion(TempArrayRes.get()); 15421 auto *Idx = new (S.Context) 15422 OpaqueValueExpr(ELoc, S.Context.getSizeType(), VK_RValue); 15423 TempArrayElem = S.CreateBuiltinArraySubscriptExpr(TempArrayElem.get(), 15424 ELoc, Idx, ELoc); 15425 } 15426 } 15427 15428 // OpenMP [2.15.4.6, Restrictions, p.2] 15429 // A list item that appears in an in_reduction clause of a task construct 15430 // must appear in a task_reduction clause of a construct associated with a 15431 // taskgroup region that includes the participating task in its taskgroup 15432 // set. The construct associated with the innermost region that meets this 15433 // condition must specify the same reduction-identifier as the in_reduction 15434 // clause. 15435 if (ClauseKind == OMPC_in_reduction) { 15436 SourceRange ParentSR; 15437 BinaryOperatorKind ParentBOK; 15438 const Expr *ParentReductionOp = nullptr; 15439 Expr *ParentBOKTD = nullptr, *ParentReductionOpTD = nullptr; 15440 DSAStackTy::DSAVarData ParentBOKDSA = 15441 Stack->getTopMostTaskgroupReductionData(D, ParentSR, ParentBOK, 15442 ParentBOKTD); 15443 DSAStackTy::DSAVarData ParentReductionOpDSA = 15444 Stack->getTopMostTaskgroupReductionData( 15445 D, ParentSR, ParentReductionOp, ParentReductionOpTD); 15446 bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown; 15447 bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown; 15448 if ((DeclareReductionRef.isUnset() && IsParentReductionOp) || 15449 (DeclareReductionRef.isUsable() && IsParentBOK) || 15450 (IsParentBOK && BOK != ParentBOK) || IsParentReductionOp) { 15451 bool EmitError = true; 15452 if (IsParentReductionOp && DeclareReductionRef.isUsable()) { 15453 llvm::FoldingSetNodeID RedId, ParentRedId; 15454 ParentReductionOp->Profile(ParentRedId, Context, /*Canonical=*/true); 15455 DeclareReductionRef.get()->Profile(RedId, Context, 15456 /*Canonical=*/true); 15457 EmitError = RedId != ParentRedId; 15458 } 15459 if (EmitError) { 15460 S.Diag(ReductionId.getBeginLoc(), 15461 diag::err_omp_reduction_identifier_mismatch) 15462 << ReductionIdRange << RefExpr->getSourceRange(); 15463 S.Diag(ParentSR.getBegin(), 15464 diag::note_omp_previous_reduction_identifier) 15465 << ParentSR 15466 << (IsParentBOK ? ParentBOKDSA.RefExpr 15467 : ParentReductionOpDSA.RefExpr) 15468 ->getSourceRange(); 15469 continue; 15470 } 15471 } 15472 TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD; 15473 } 15474 15475 DeclRefExpr *Ref = nullptr; 15476 Expr *VarsExpr = RefExpr->IgnoreParens(); 15477 if (!VD && !S.CurContext->isDependentContext()) { 15478 if (ASE || OASE) { 15479 TransformExprToCaptures RebuildToCapture(S, D); 15480 VarsExpr = 15481 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get(); 15482 Ref = RebuildToCapture.getCapturedExpr(); 15483 } else { 15484 VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false); 15485 } 15486 if (!S.isOpenMPCapturedDecl(D)) { 15487 RD.ExprCaptures.emplace_back(Ref->getDecl()); 15488 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) { 15489 ExprResult RefRes = S.DefaultLvalueConversion(Ref); 15490 if (!RefRes.isUsable()) 15491 continue; 15492 ExprResult PostUpdateRes = 15493 S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr, 15494 RefRes.get()); 15495 if (!PostUpdateRes.isUsable()) 15496 continue; 15497 if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) || 15498 Stack->getCurrentDirective() == OMPD_taskgroup) { 15499 S.Diag(RefExpr->getExprLoc(), 15500 diag::err_omp_reduction_non_addressable_expression) 15501 << RefExpr->getSourceRange(); 15502 continue; 15503 } 15504 RD.ExprPostUpdates.emplace_back( 15505 S.IgnoredValueConversions(PostUpdateRes.get()).get()); 15506 } 15507 } 15508 } 15509 // All reduction items are still marked as reduction (to do not increase 15510 // code base size). 15511 unsigned Modifier = RD.RedModifier; 15512 // Consider task_reductions as reductions with task modifier. Required for 15513 // correct analysis of in_reduction clauses. 15514 if (CurrDir == OMPD_taskgroup && ClauseKind == OMPC_task_reduction) 15515 Modifier = OMPC_REDUCTION_task; 15516 Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref, Modifier); 15517 if (Modifier == OMPC_REDUCTION_task && 15518 (CurrDir == OMPD_taskgroup || 15519 ((isOpenMPParallelDirective(CurrDir) || 15520 isOpenMPWorksharingDirective(CurrDir)) && 15521 !isOpenMPSimdDirective(CurrDir)))) { 15522 if (DeclareReductionRef.isUsable()) 15523 Stack->addTaskgroupReductionData(D, ReductionIdRange, 15524 DeclareReductionRef.get()); 15525 else 15526 Stack->addTaskgroupReductionData(D, ReductionIdRange, BOK); 15527 } 15528 RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get(), 15529 TaskgroupDescriptor, CopyOpRes.get(), TempArrayRes.get(), 15530 TempArrayElem.get()); 15531 } 15532 return RD.Vars.empty(); 15533 } 15534 15535 OMPClause *Sema::ActOnOpenMPReductionClause( 15536 ArrayRef<Expr *> VarList, OpenMPReductionClauseModifier Modifier, 15537 SourceLocation StartLoc, SourceLocation LParenLoc, 15538 SourceLocation ModifierLoc, SourceLocation ColonLoc, SourceLocation EndLoc, 15539 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 15540 ArrayRef<Expr *> UnresolvedReductions) { 15541 if (ModifierLoc.isValid() && Modifier == OMPC_REDUCTION_unknown) { 15542 Diag(LParenLoc, diag::err_omp_unexpected_clause_value) 15543 << getListOfPossibleValues(OMPC_reduction, /*First=*/0, 15544 /*Last=*/OMPC_REDUCTION_unknown) 15545 << getOpenMPClauseName(OMPC_reduction); 15546 return nullptr; 15547 } 15548 // OpenMP 5.0, 2.19.5.4 reduction Clause, Restrictions 15549 // A reduction clause with the inscan reduction-modifier may only appear on a 15550 // worksharing-loop construct, a worksharing-loop SIMD construct, a simd 15551 // construct, a parallel worksharing-loop construct or a parallel 15552 // worksharing-loop SIMD construct. 15553 if (Modifier == OMPC_REDUCTION_inscan && 15554 (DSAStack->getCurrentDirective() != OMPD_for && 15555 DSAStack->getCurrentDirective() != OMPD_for_simd && 15556 DSAStack->getCurrentDirective() != OMPD_simd && 15557 DSAStack->getCurrentDirective() != OMPD_parallel_for && 15558 DSAStack->getCurrentDirective() != OMPD_parallel_for_simd)) { 15559 Diag(ModifierLoc, diag::err_omp_wrong_inscan_reduction); 15560 return nullptr; 15561 } 15562 15563 ReductionData RD(VarList.size(), Modifier); 15564 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList, 15565 StartLoc, LParenLoc, ColonLoc, EndLoc, 15566 ReductionIdScopeSpec, ReductionId, 15567 UnresolvedReductions, RD)) 15568 return nullptr; 15569 15570 return OMPReductionClause::Create( 15571 Context, StartLoc, LParenLoc, ModifierLoc, ColonLoc, EndLoc, Modifier, 15572 RD.Vars, ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 15573 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.InscanCopyOps, 15574 RD.InscanCopyArrayTemps, RD.InscanCopyArrayElems, 15575 buildPreInits(Context, RD.ExprCaptures), 15576 buildPostUpdate(*this, RD.ExprPostUpdates)); 15577 } 15578 15579 OMPClause *Sema::ActOnOpenMPTaskReductionClause( 15580 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 15581 SourceLocation ColonLoc, SourceLocation EndLoc, 15582 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 15583 ArrayRef<Expr *> UnresolvedReductions) { 15584 ReductionData RD(VarList.size()); 15585 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction, VarList, 15586 StartLoc, LParenLoc, ColonLoc, EndLoc, 15587 ReductionIdScopeSpec, ReductionId, 15588 UnresolvedReductions, RD)) 15589 return nullptr; 15590 15591 return OMPTaskReductionClause::Create( 15592 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 15593 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 15594 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, 15595 buildPreInits(Context, RD.ExprCaptures), 15596 buildPostUpdate(*this, RD.ExprPostUpdates)); 15597 } 15598 15599 OMPClause *Sema::ActOnOpenMPInReductionClause( 15600 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 15601 SourceLocation ColonLoc, SourceLocation EndLoc, 15602 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 15603 ArrayRef<Expr *> UnresolvedReductions) { 15604 ReductionData RD(VarList.size()); 15605 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_in_reduction, VarList, 15606 StartLoc, LParenLoc, ColonLoc, EndLoc, 15607 ReductionIdScopeSpec, ReductionId, 15608 UnresolvedReductions, RD)) 15609 return nullptr; 15610 15611 return OMPInReductionClause::Create( 15612 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 15613 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 15614 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.TaskgroupDescriptors, 15615 buildPreInits(Context, RD.ExprCaptures), 15616 buildPostUpdate(*this, RD.ExprPostUpdates)); 15617 } 15618 15619 bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind, 15620 SourceLocation LinLoc) { 15621 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) || 15622 LinKind == OMPC_LINEAR_unknown) { 15623 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus; 15624 return true; 15625 } 15626 return false; 15627 } 15628 15629 bool Sema::CheckOpenMPLinearDecl(const ValueDecl *D, SourceLocation ELoc, 15630 OpenMPLinearClauseKind LinKind, QualType Type, 15631 bool IsDeclareSimd) { 15632 const auto *VD = dyn_cast_or_null<VarDecl>(D); 15633 // A variable must not have an incomplete type or a reference type. 15634 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type)) 15635 return true; 15636 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) && 15637 !Type->isReferenceType()) { 15638 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference) 15639 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind); 15640 return true; 15641 } 15642 Type = Type.getNonReferenceType(); 15643 15644 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions] 15645 // A variable that is privatized must not have a const-qualified type 15646 // unless it is of class type with a mutable member. This restriction does 15647 // not apply to the firstprivate clause, nor to the linear clause on 15648 // declarative directives (like declare simd). 15649 if (!IsDeclareSimd && 15650 rejectConstNotMutableType(*this, D, Type, OMPC_linear, ELoc)) 15651 return true; 15652 15653 // A list item must be of integral or pointer type. 15654 Type = Type.getUnqualifiedType().getCanonicalType(); 15655 const auto *Ty = Type.getTypePtrOrNull(); 15656 if (!Ty || (LinKind != OMPC_LINEAR_ref && !Ty->isDependentType() && 15657 !Ty->isIntegralType(Context) && !Ty->isPointerType())) { 15658 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type; 15659 if (D) { 15660 bool IsDecl = 15661 !VD || 15662 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 15663 Diag(D->getLocation(), 15664 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 15665 << D; 15666 } 15667 return true; 15668 } 15669 return false; 15670 } 15671 15672 OMPClause *Sema::ActOnOpenMPLinearClause( 15673 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc, 15674 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind, 15675 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 15676 SmallVector<Expr *, 8> Vars; 15677 SmallVector<Expr *, 8> Privates; 15678 SmallVector<Expr *, 8> Inits; 15679 SmallVector<Decl *, 4> ExprCaptures; 15680 SmallVector<Expr *, 4> ExprPostUpdates; 15681 if (CheckOpenMPLinearModifier(LinKind, LinLoc)) 15682 LinKind = OMPC_LINEAR_val; 15683 for (Expr *RefExpr : VarList) { 15684 assert(RefExpr && "NULL expr in OpenMP linear clause."); 15685 SourceLocation ELoc; 15686 SourceRange ERange; 15687 Expr *SimpleRefExpr = RefExpr; 15688 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 15689 if (Res.second) { 15690 // It will be analyzed later. 15691 Vars.push_back(RefExpr); 15692 Privates.push_back(nullptr); 15693 Inits.push_back(nullptr); 15694 } 15695 ValueDecl *D = Res.first; 15696 if (!D) 15697 continue; 15698 15699 QualType Type = D->getType(); 15700 auto *VD = dyn_cast<VarDecl>(D); 15701 15702 // OpenMP [2.14.3.7, linear clause] 15703 // A list-item cannot appear in more than one linear clause. 15704 // A list-item that appears in a linear clause cannot appear in any 15705 // other data-sharing attribute clause. 15706 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 15707 if (DVar.RefExpr) { 15708 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 15709 << getOpenMPClauseName(OMPC_linear); 15710 reportOriginalDsa(*this, DSAStack, D, DVar); 15711 continue; 15712 } 15713 15714 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type)) 15715 continue; 15716 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType(); 15717 15718 // Build private copy of original var. 15719 VarDecl *Private = 15720 buildVarDecl(*this, ELoc, Type, D->getName(), 15721 D->hasAttrs() ? &D->getAttrs() : nullptr, 15722 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 15723 DeclRefExpr *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc); 15724 // Build var to save initial value. 15725 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start"); 15726 Expr *InitExpr; 15727 DeclRefExpr *Ref = nullptr; 15728 if (!VD && !CurContext->isDependentContext()) { 15729 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 15730 if (!isOpenMPCapturedDecl(D)) { 15731 ExprCaptures.push_back(Ref->getDecl()); 15732 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) { 15733 ExprResult RefRes = DefaultLvalueConversion(Ref); 15734 if (!RefRes.isUsable()) 15735 continue; 15736 ExprResult PostUpdateRes = 15737 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, 15738 SimpleRefExpr, RefRes.get()); 15739 if (!PostUpdateRes.isUsable()) 15740 continue; 15741 ExprPostUpdates.push_back( 15742 IgnoredValueConversions(PostUpdateRes.get()).get()); 15743 } 15744 } 15745 } 15746 if (LinKind == OMPC_LINEAR_uval) 15747 InitExpr = VD ? VD->getInit() : SimpleRefExpr; 15748 else 15749 InitExpr = VD ? SimpleRefExpr : Ref; 15750 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(), 15751 /*DirectInit=*/false); 15752 DeclRefExpr *InitRef = buildDeclRefExpr(*this, Init, Type, ELoc); 15753 15754 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref); 15755 Vars.push_back((VD || CurContext->isDependentContext()) 15756 ? RefExpr->IgnoreParens() 15757 : Ref); 15758 Privates.push_back(PrivateRef); 15759 Inits.push_back(InitRef); 15760 } 15761 15762 if (Vars.empty()) 15763 return nullptr; 15764 15765 Expr *StepExpr = Step; 15766 Expr *CalcStepExpr = nullptr; 15767 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && 15768 !Step->isInstantiationDependent() && 15769 !Step->containsUnexpandedParameterPack()) { 15770 SourceLocation StepLoc = Step->getBeginLoc(); 15771 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step); 15772 if (Val.isInvalid()) 15773 return nullptr; 15774 StepExpr = Val.get(); 15775 15776 // Build var to save the step value. 15777 VarDecl *SaveVar = 15778 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step"); 15779 ExprResult SaveRef = 15780 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc); 15781 ExprResult CalcStep = 15782 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr); 15783 CalcStep = ActOnFinishFullExpr(CalcStep.get(), /*DiscardedValue*/ false); 15784 15785 // Warn about zero linear step (it would be probably better specified as 15786 // making corresponding variables 'const'). 15787 if (Optional<llvm::APSInt> Result = 15788 StepExpr->getIntegerConstantExpr(Context)) { 15789 if (!Result->isNegative() && !Result->isStrictlyPositive()) 15790 Diag(StepLoc, diag::warn_omp_linear_step_zero) 15791 << Vars[0] << (Vars.size() > 1); 15792 } else if (CalcStep.isUsable()) { 15793 // Calculate the step beforehand instead of doing this on each iteration. 15794 // (This is not used if the number of iterations may be kfold-ed). 15795 CalcStepExpr = CalcStep.get(); 15796 } 15797 } 15798 15799 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc, 15800 ColonLoc, EndLoc, Vars, Privates, Inits, 15801 StepExpr, CalcStepExpr, 15802 buildPreInits(Context, ExprCaptures), 15803 buildPostUpdate(*this, ExprPostUpdates)); 15804 } 15805 15806 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, 15807 Expr *NumIterations, Sema &SemaRef, 15808 Scope *S, DSAStackTy *Stack) { 15809 // Walk the vars and build update/final expressions for the CodeGen. 15810 SmallVector<Expr *, 8> Updates; 15811 SmallVector<Expr *, 8> Finals; 15812 SmallVector<Expr *, 8> UsedExprs; 15813 Expr *Step = Clause.getStep(); 15814 Expr *CalcStep = Clause.getCalcStep(); 15815 // OpenMP [2.14.3.7, linear clause] 15816 // If linear-step is not specified it is assumed to be 1. 15817 if (!Step) 15818 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(); 15819 else if (CalcStep) 15820 Step = cast<BinaryOperator>(CalcStep)->getLHS(); 15821 bool HasErrors = false; 15822 auto CurInit = Clause.inits().begin(); 15823 auto CurPrivate = Clause.privates().begin(); 15824 OpenMPLinearClauseKind LinKind = Clause.getModifier(); 15825 for (Expr *RefExpr : Clause.varlists()) { 15826 SourceLocation ELoc; 15827 SourceRange ERange; 15828 Expr *SimpleRefExpr = RefExpr; 15829 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange); 15830 ValueDecl *D = Res.first; 15831 if (Res.second || !D) { 15832 Updates.push_back(nullptr); 15833 Finals.push_back(nullptr); 15834 HasErrors = true; 15835 continue; 15836 } 15837 auto &&Info = Stack->isLoopControlVariable(D); 15838 // OpenMP [2.15.11, distribute simd Construct] 15839 // A list item may not appear in a linear clause, unless it is the loop 15840 // iteration variable. 15841 if (isOpenMPDistributeDirective(Stack->getCurrentDirective()) && 15842 isOpenMPSimdDirective(Stack->getCurrentDirective()) && !Info.first) { 15843 SemaRef.Diag(ELoc, 15844 diag::err_omp_linear_distribute_var_non_loop_iteration); 15845 Updates.push_back(nullptr); 15846 Finals.push_back(nullptr); 15847 HasErrors = true; 15848 continue; 15849 } 15850 Expr *InitExpr = *CurInit; 15851 15852 // Build privatized reference to the current linear var. 15853 auto *DE = cast<DeclRefExpr>(SimpleRefExpr); 15854 Expr *CapturedRef; 15855 if (LinKind == OMPC_LINEAR_uval) 15856 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit(); 15857 else 15858 CapturedRef = 15859 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()), 15860 DE->getType().getUnqualifiedType(), DE->getExprLoc(), 15861 /*RefersToCapture=*/true); 15862 15863 // Build update: Var = InitExpr + IV * Step 15864 ExprResult Update; 15865 if (!Info.first) 15866 Update = buildCounterUpdate( 15867 SemaRef, S, RefExpr->getExprLoc(), *CurPrivate, InitExpr, IV, Step, 15868 /*Subtract=*/false, /*IsNonRectangularLB=*/false); 15869 else 15870 Update = *CurPrivate; 15871 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getBeginLoc(), 15872 /*DiscardedValue*/ false); 15873 15874 // Build final: Var = InitExpr + NumIterations * Step 15875 ExprResult Final; 15876 if (!Info.first) 15877 Final = 15878 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef, 15879 InitExpr, NumIterations, Step, /*Subtract=*/false, 15880 /*IsNonRectangularLB=*/false); 15881 else 15882 Final = *CurPrivate; 15883 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getBeginLoc(), 15884 /*DiscardedValue*/ false); 15885 15886 if (!Update.isUsable() || !Final.isUsable()) { 15887 Updates.push_back(nullptr); 15888 Finals.push_back(nullptr); 15889 UsedExprs.push_back(nullptr); 15890 HasErrors = true; 15891 } else { 15892 Updates.push_back(Update.get()); 15893 Finals.push_back(Final.get()); 15894 if (!Info.first) 15895 UsedExprs.push_back(SimpleRefExpr); 15896 } 15897 ++CurInit; 15898 ++CurPrivate; 15899 } 15900 if (Expr *S = Clause.getStep()) 15901 UsedExprs.push_back(S); 15902 // Fill the remaining part with the nullptr. 15903 UsedExprs.append(Clause.varlist_size() + 1 - UsedExprs.size(), nullptr); 15904 Clause.setUpdates(Updates); 15905 Clause.setFinals(Finals); 15906 Clause.setUsedExprs(UsedExprs); 15907 return HasErrors; 15908 } 15909 15910 OMPClause *Sema::ActOnOpenMPAlignedClause( 15911 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc, 15912 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 15913 SmallVector<Expr *, 8> Vars; 15914 for (Expr *RefExpr : VarList) { 15915 assert(RefExpr && "NULL expr in OpenMP linear clause."); 15916 SourceLocation ELoc; 15917 SourceRange ERange; 15918 Expr *SimpleRefExpr = RefExpr; 15919 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 15920 if (Res.second) { 15921 // It will be analyzed later. 15922 Vars.push_back(RefExpr); 15923 } 15924 ValueDecl *D = Res.first; 15925 if (!D) 15926 continue; 15927 15928 QualType QType = D->getType(); 15929 auto *VD = dyn_cast<VarDecl>(D); 15930 15931 // OpenMP [2.8.1, simd construct, Restrictions] 15932 // The type of list items appearing in the aligned clause must be 15933 // array, pointer, reference to array, or reference to pointer. 15934 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType(); 15935 const Type *Ty = QType.getTypePtrOrNull(); 15936 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) { 15937 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr) 15938 << QType << getLangOpts().CPlusPlus << ERange; 15939 bool IsDecl = 15940 !VD || 15941 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 15942 Diag(D->getLocation(), 15943 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 15944 << D; 15945 continue; 15946 } 15947 15948 // OpenMP [2.8.1, simd construct, Restrictions] 15949 // A list-item cannot appear in more than one aligned clause. 15950 if (const Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) { 15951 Diag(ELoc, diag::err_omp_used_in_clause_twice) 15952 << 0 << getOpenMPClauseName(OMPC_aligned) << ERange; 15953 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa) 15954 << getOpenMPClauseName(OMPC_aligned); 15955 continue; 15956 } 15957 15958 DeclRefExpr *Ref = nullptr; 15959 if (!VD && isOpenMPCapturedDecl(D)) 15960 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 15961 Vars.push_back(DefaultFunctionArrayConversion( 15962 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref) 15963 .get()); 15964 } 15965 15966 // OpenMP [2.8.1, simd construct, Description] 15967 // The parameter of the aligned clause, alignment, must be a constant 15968 // positive integer expression. 15969 // If no optional parameter is specified, implementation-defined default 15970 // alignments for SIMD instructions on the target platforms are assumed. 15971 if (Alignment != nullptr) { 15972 ExprResult AlignResult = 15973 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned); 15974 if (AlignResult.isInvalid()) 15975 return nullptr; 15976 Alignment = AlignResult.get(); 15977 } 15978 if (Vars.empty()) 15979 return nullptr; 15980 15981 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc, 15982 EndLoc, Vars, Alignment); 15983 } 15984 15985 OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList, 15986 SourceLocation StartLoc, 15987 SourceLocation LParenLoc, 15988 SourceLocation EndLoc) { 15989 SmallVector<Expr *, 8> Vars; 15990 SmallVector<Expr *, 8> SrcExprs; 15991 SmallVector<Expr *, 8> DstExprs; 15992 SmallVector<Expr *, 8> AssignmentOps; 15993 for (Expr *RefExpr : VarList) { 15994 assert(RefExpr && "NULL expr in OpenMP copyin clause."); 15995 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 15996 // It will be analyzed later. 15997 Vars.push_back(RefExpr); 15998 SrcExprs.push_back(nullptr); 15999 DstExprs.push_back(nullptr); 16000 AssignmentOps.push_back(nullptr); 16001 continue; 16002 } 16003 16004 SourceLocation ELoc = RefExpr->getExprLoc(); 16005 // OpenMP [2.1, C/C++] 16006 // A list item is a variable name. 16007 // OpenMP [2.14.4.1, Restrictions, p.1] 16008 // A list item that appears in a copyin clause must be threadprivate. 16009 auto *DE = dyn_cast<DeclRefExpr>(RefExpr); 16010 if (!DE || !isa<VarDecl>(DE->getDecl())) { 16011 Diag(ELoc, diag::err_omp_expected_var_name_member_expr) 16012 << 0 << RefExpr->getSourceRange(); 16013 continue; 16014 } 16015 16016 Decl *D = DE->getDecl(); 16017 auto *VD = cast<VarDecl>(D); 16018 16019 QualType Type = VD->getType(); 16020 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 16021 // It will be analyzed later. 16022 Vars.push_back(DE); 16023 SrcExprs.push_back(nullptr); 16024 DstExprs.push_back(nullptr); 16025 AssignmentOps.push_back(nullptr); 16026 continue; 16027 } 16028 16029 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1] 16030 // A list item that appears in a copyin clause must be threadprivate. 16031 if (!DSAStack->isThreadPrivate(VD)) { 16032 Diag(ELoc, diag::err_omp_required_access) 16033 << getOpenMPClauseName(OMPC_copyin) 16034 << getOpenMPDirectiveName(OMPD_threadprivate); 16035 continue; 16036 } 16037 16038 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 16039 // A variable of class type (or array thereof) that appears in a 16040 // copyin clause requires an accessible, unambiguous copy assignment 16041 // operator for the class type. 16042 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType(); 16043 VarDecl *SrcVD = 16044 buildVarDecl(*this, DE->getBeginLoc(), ElemType.getUnqualifiedType(), 16045 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr); 16046 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr( 16047 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc()); 16048 VarDecl *DstVD = 16049 buildVarDecl(*this, DE->getBeginLoc(), ElemType, ".copyin.dst", 16050 VD->hasAttrs() ? &VD->getAttrs() : nullptr); 16051 DeclRefExpr *PseudoDstExpr = 16052 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc()); 16053 // For arrays generate assignment operation for single element and replace 16054 // it by the original array element in CodeGen. 16055 ExprResult AssignmentOp = 16056 BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, PseudoDstExpr, 16057 PseudoSrcExpr); 16058 if (AssignmentOp.isInvalid()) 16059 continue; 16060 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(), 16061 /*DiscardedValue*/ false); 16062 if (AssignmentOp.isInvalid()) 16063 continue; 16064 16065 DSAStack->addDSA(VD, DE, OMPC_copyin); 16066 Vars.push_back(DE); 16067 SrcExprs.push_back(PseudoSrcExpr); 16068 DstExprs.push_back(PseudoDstExpr); 16069 AssignmentOps.push_back(AssignmentOp.get()); 16070 } 16071 16072 if (Vars.empty()) 16073 return nullptr; 16074 16075 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 16076 SrcExprs, DstExprs, AssignmentOps); 16077 } 16078 16079 OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList, 16080 SourceLocation StartLoc, 16081 SourceLocation LParenLoc, 16082 SourceLocation EndLoc) { 16083 SmallVector<Expr *, 8> Vars; 16084 SmallVector<Expr *, 8> SrcExprs; 16085 SmallVector<Expr *, 8> DstExprs; 16086 SmallVector<Expr *, 8> AssignmentOps; 16087 for (Expr *RefExpr : VarList) { 16088 assert(RefExpr && "NULL expr in OpenMP linear clause."); 16089 SourceLocation ELoc; 16090 SourceRange ERange; 16091 Expr *SimpleRefExpr = RefExpr; 16092 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 16093 if (Res.second) { 16094 // It will be analyzed later. 16095 Vars.push_back(RefExpr); 16096 SrcExprs.push_back(nullptr); 16097 DstExprs.push_back(nullptr); 16098 AssignmentOps.push_back(nullptr); 16099 } 16100 ValueDecl *D = Res.first; 16101 if (!D) 16102 continue; 16103 16104 QualType Type = D->getType(); 16105 auto *VD = dyn_cast<VarDecl>(D); 16106 16107 // OpenMP [2.14.4.2, Restrictions, p.2] 16108 // A list item that appears in a copyprivate clause may not appear in a 16109 // private or firstprivate clause on the single construct. 16110 if (!VD || !DSAStack->isThreadPrivate(VD)) { 16111 DSAStackTy::DSAVarData DVar = 16112 DSAStack->getTopDSA(D, /*FromParent=*/false); 16113 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate && 16114 DVar.RefExpr) { 16115 Diag(ELoc, diag::err_omp_wrong_dsa) 16116 << getOpenMPClauseName(DVar.CKind) 16117 << getOpenMPClauseName(OMPC_copyprivate); 16118 reportOriginalDsa(*this, DSAStack, D, DVar); 16119 continue; 16120 } 16121 16122 // OpenMP [2.11.4.2, Restrictions, p.1] 16123 // All list items that appear in a copyprivate clause must be either 16124 // threadprivate or private in the enclosing context. 16125 if (DVar.CKind == OMPC_unknown) { 16126 DVar = DSAStack->getImplicitDSA(D, false); 16127 if (DVar.CKind == OMPC_shared) { 16128 Diag(ELoc, diag::err_omp_required_access) 16129 << getOpenMPClauseName(OMPC_copyprivate) 16130 << "threadprivate or private in the enclosing context"; 16131 reportOriginalDsa(*this, DSAStack, D, DVar); 16132 continue; 16133 } 16134 } 16135 } 16136 16137 // Variably modified types are not supported. 16138 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) { 16139 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 16140 << getOpenMPClauseName(OMPC_copyprivate) << Type 16141 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 16142 bool IsDecl = 16143 !VD || 16144 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 16145 Diag(D->getLocation(), 16146 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 16147 << D; 16148 continue; 16149 } 16150 16151 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 16152 // A variable of class type (or array thereof) that appears in a 16153 // copyin clause requires an accessible, unambiguous copy assignment 16154 // operator for the class type. 16155 Type = Context.getBaseElementType(Type.getNonReferenceType()) 16156 .getUnqualifiedType(); 16157 VarDecl *SrcVD = 16158 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.src", 16159 D->hasAttrs() ? &D->getAttrs() : nullptr); 16160 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc); 16161 VarDecl *DstVD = 16162 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.dst", 16163 D->hasAttrs() ? &D->getAttrs() : nullptr); 16164 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc); 16165 ExprResult AssignmentOp = BuildBinOp( 16166 DSAStack->getCurScope(), ELoc, BO_Assign, PseudoDstExpr, PseudoSrcExpr); 16167 if (AssignmentOp.isInvalid()) 16168 continue; 16169 AssignmentOp = 16170 ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false); 16171 if (AssignmentOp.isInvalid()) 16172 continue; 16173 16174 // No need to mark vars as copyprivate, they are already threadprivate or 16175 // implicitly private. 16176 assert(VD || isOpenMPCapturedDecl(D)); 16177 Vars.push_back( 16178 VD ? RefExpr->IgnoreParens() 16179 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false)); 16180 SrcExprs.push_back(PseudoSrcExpr); 16181 DstExprs.push_back(PseudoDstExpr); 16182 AssignmentOps.push_back(AssignmentOp.get()); 16183 } 16184 16185 if (Vars.empty()) 16186 return nullptr; 16187 16188 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 16189 Vars, SrcExprs, DstExprs, AssignmentOps); 16190 } 16191 16192 OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList, 16193 SourceLocation StartLoc, 16194 SourceLocation LParenLoc, 16195 SourceLocation EndLoc) { 16196 if (VarList.empty()) 16197 return nullptr; 16198 16199 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList); 16200 } 16201 16202 /// Tries to find omp_depend_t. type. 16203 static bool findOMPDependT(Sema &S, SourceLocation Loc, DSAStackTy *Stack, 16204 bool Diagnose = true) { 16205 QualType OMPDependT = Stack->getOMPDependT(); 16206 if (!OMPDependT.isNull()) 16207 return true; 16208 IdentifierInfo *II = &S.PP.getIdentifierTable().get("omp_depend_t"); 16209 ParsedType PT = S.getTypeName(*II, Loc, S.getCurScope()); 16210 if (!PT.getAsOpaquePtr() || PT.get().isNull()) { 16211 if (Diagnose) 16212 S.Diag(Loc, diag::err_omp_implied_type_not_found) << "omp_depend_t"; 16213 return false; 16214 } 16215 Stack->setOMPDependT(PT.get()); 16216 return true; 16217 } 16218 16219 OMPClause *Sema::ActOnOpenMPDepobjClause(Expr *Depobj, SourceLocation StartLoc, 16220 SourceLocation LParenLoc, 16221 SourceLocation EndLoc) { 16222 if (!Depobj) 16223 return nullptr; 16224 16225 bool OMPDependTFound = findOMPDependT(*this, StartLoc, DSAStack); 16226 16227 // OpenMP 5.0, 2.17.10.1 depobj Construct 16228 // depobj is an lvalue expression of type omp_depend_t. 16229 if (!Depobj->isTypeDependent() && !Depobj->isValueDependent() && 16230 !Depobj->isInstantiationDependent() && 16231 !Depobj->containsUnexpandedParameterPack() && 16232 (OMPDependTFound && 16233 !Context.typesAreCompatible(DSAStack->getOMPDependT(), Depobj->getType(), 16234 /*CompareUnqualified=*/true))) { 16235 Diag(Depobj->getExprLoc(), diag::err_omp_expected_omp_depend_t_lvalue) 16236 << 0 << Depobj->getType() << Depobj->getSourceRange(); 16237 } 16238 16239 if (!Depobj->isLValue()) { 16240 Diag(Depobj->getExprLoc(), diag::err_omp_expected_omp_depend_t_lvalue) 16241 << 1 << Depobj->getSourceRange(); 16242 } 16243 16244 return OMPDepobjClause::Create(Context, StartLoc, LParenLoc, EndLoc, Depobj); 16245 } 16246 16247 OMPClause * 16248 Sema::ActOnOpenMPDependClause(Expr *DepModifier, OpenMPDependClauseKind DepKind, 16249 SourceLocation DepLoc, SourceLocation ColonLoc, 16250 ArrayRef<Expr *> VarList, SourceLocation StartLoc, 16251 SourceLocation LParenLoc, SourceLocation EndLoc) { 16252 if (DSAStack->getCurrentDirective() == OMPD_ordered && 16253 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) { 16254 Diag(DepLoc, diag::err_omp_unexpected_clause_value) 16255 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend); 16256 return nullptr; 16257 } 16258 if ((DSAStack->getCurrentDirective() != OMPD_ordered || 16259 DSAStack->getCurrentDirective() == OMPD_depobj) && 16260 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source || 16261 DepKind == OMPC_DEPEND_sink || 16262 ((LangOpts.OpenMP < 50 || 16263 DSAStack->getCurrentDirective() == OMPD_depobj) && 16264 DepKind == OMPC_DEPEND_depobj))) { 16265 SmallVector<unsigned, 3> Except; 16266 Except.push_back(OMPC_DEPEND_source); 16267 Except.push_back(OMPC_DEPEND_sink); 16268 if (LangOpts.OpenMP < 50 || DSAStack->getCurrentDirective() == OMPD_depobj) 16269 Except.push_back(OMPC_DEPEND_depobj); 16270 std::string Expected = (LangOpts.OpenMP >= 50 && !DepModifier) 16271 ? "depend modifier(iterator) or " 16272 : ""; 16273 Diag(DepLoc, diag::err_omp_unexpected_clause_value) 16274 << Expected + getListOfPossibleValues(OMPC_depend, /*First=*/0, 16275 /*Last=*/OMPC_DEPEND_unknown, 16276 Except) 16277 << getOpenMPClauseName(OMPC_depend); 16278 return nullptr; 16279 } 16280 if (DepModifier && 16281 (DepKind == OMPC_DEPEND_source || DepKind == OMPC_DEPEND_sink)) { 16282 Diag(DepModifier->getExprLoc(), 16283 diag::err_omp_depend_sink_source_with_modifier); 16284 return nullptr; 16285 } 16286 if (DepModifier && 16287 !DepModifier->getType()->isSpecificBuiltinType(BuiltinType::OMPIterator)) 16288 Diag(DepModifier->getExprLoc(), diag::err_omp_depend_modifier_not_iterator); 16289 16290 SmallVector<Expr *, 8> Vars; 16291 DSAStackTy::OperatorOffsetTy OpsOffs; 16292 llvm::APSInt DepCounter(/*BitWidth=*/32); 16293 llvm::APSInt TotalDepCount(/*BitWidth=*/32); 16294 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) { 16295 if (const Expr *OrderedCountExpr = 16296 DSAStack->getParentOrderedRegionParam().first) { 16297 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context); 16298 TotalDepCount.setIsUnsigned(/*Val=*/true); 16299 } 16300 } 16301 for (Expr *RefExpr : VarList) { 16302 assert(RefExpr && "NULL expr in OpenMP shared clause."); 16303 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 16304 // It will be analyzed later. 16305 Vars.push_back(RefExpr); 16306 continue; 16307 } 16308 16309 SourceLocation ELoc = RefExpr->getExprLoc(); 16310 Expr *SimpleExpr = RefExpr->IgnoreParenCasts(); 16311 if (DepKind == OMPC_DEPEND_sink) { 16312 if (DSAStack->getParentOrderedRegionParam().first && 16313 DepCounter >= TotalDepCount) { 16314 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr); 16315 continue; 16316 } 16317 ++DepCounter; 16318 // OpenMP [2.13.9, Summary] 16319 // depend(dependence-type : vec), where dependence-type is: 16320 // 'sink' and where vec is the iteration vector, which has the form: 16321 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn] 16322 // where n is the value specified by the ordered clause in the loop 16323 // directive, xi denotes the loop iteration variable of the i-th nested 16324 // loop associated with the loop directive, and di is a constant 16325 // non-negative integer. 16326 if (CurContext->isDependentContext()) { 16327 // It will be analyzed later. 16328 Vars.push_back(RefExpr); 16329 continue; 16330 } 16331 SimpleExpr = SimpleExpr->IgnoreImplicit(); 16332 OverloadedOperatorKind OOK = OO_None; 16333 SourceLocation OOLoc; 16334 Expr *LHS = SimpleExpr; 16335 Expr *RHS = nullptr; 16336 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) { 16337 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode()); 16338 OOLoc = BO->getOperatorLoc(); 16339 LHS = BO->getLHS()->IgnoreParenImpCasts(); 16340 RHS = BO->getRHS()->IgnoreParenImpCasts(); 16341 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) { 16342 OOK = OCE->getOperator(); 16343 OOLoc = OCE->getOperatorLoc(); 16344 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); 16345 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts(); 16346 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) { 16347 OOK = MCE->getMethodDecl() 16348 ->getNameInfo() 16349 .getName() 16350 .getCXXOverloadedOperator(); 16351 OOLoc = MCE->getCallee()->getExprLoc(); 16352 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts(); 16353 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); 16354 } 16355 SourceLocation ELoc; 16356 SourceRange ERange; 16357 auto Res = getPrivateItem(*this, LHS, ELoc, ERange); 16358 if (Res.second) { 16359 // It will be analyzed later. 16360 Vars.push_back(RefExpr); 16361 } 16362 ValueDecl *D = Res.first; 16363 if (!D) 16364 continue; 16365 16366 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) { 16367 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus); 16368 continue; 16369 } 16370 if (RHS) { 16371 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause( 16372 RHS, OMPC_depend, /*StrictlyPositive=*/false); 16373 if (RHSRes.isInvalid()) 16374 continue; 16375 } 16376 if (!CurContext->isDependentContext() && 16377 DSAStack->getParentOrderedRegionParam().first && 16378 DepCounter != DSAStack->isParentLoopControlVariable(D).first) { 16379 const ValueDecl *VD = 16380 DSAStack->getParentLoopControlVariable(DepCounter.getZExtValue()); 16381 if (VD) 16382 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) 16383 << 1 << VD; 16384 else 16385 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) << 0; 16386 continue; 16387 } 16388 OpsOffs.emplace_back(RHS, OOK); 16389 } else { 16390 bool OMPDependTFound = LangOpts.OpenMP >= 50; 16391 if (OMPDependTFound) 16392 OMPDependTFound = findOMPDependT(*this, StartLoc, DSAStack, 16393 DepKind == OMPC_DEPEND_depobj); 16394 if (DepKind == OMPC_DEPEND_depobj) { 16395 // OpenMP 5.0, 2.17.11 depend Clause, Restrictions, C/C++ 16396 // List items used in depend clauses with the depobj dependence type 16397 // must be expressions of the omp_depend_t type. 16398 if (!RefExpr->isValueDependent() && !RefExpr->isTypeDependent() && 16399 !RefExpr->isInstantiationDependent() && 16400 !RefExpr->containsUnexpandedParameterPack() && 16401 (OMPDependTFound && 16402 !Context.hasSameUnqualifiedType(DSAStack->getOMPDependT(), 16403 RefExpr->getType()))) { 16404 Diag(ELoc, diag::err_omp_expected_omp_depend_t_lvalue) 16405 << 0 << RefExpr->getType() << RefExpr->getSourceRange(); 16406 continue; 16407 } 16408 if (!RefExpr->isLValue()) { 16409 Diag(ELoc, diag::err_omp_expected_omp_depend_t_lvalue) 16410 << 1 << RefExpr->getType() << RefExpr->getSourceRange(); 16411 continue; 16412 } 16413 } else { 16414 // OpenMP 5.0 [2.17.11, Restrictions] 16415 // List items used in depend clauses cannot be zero-length array 16416 // sections. 16417 QualType ExprTy = RefExpr->getType().getNonReferenceType(); 16418 const auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr); 16419 if (OASE) { 16420 QualType BaseType = 16421 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 16422 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 16423 ExprTy = ATy->getElementType(); 16424 else 16425 ExprTy = BaseType->getPointeeType(); 16426 ExprTy = ExprTy.getNonReferenceType(); 16427 const Expr *Length = OASE->getLength(); 16428 Expr::EvalResult Result; 16429 if (Length && !Length->isValueDependent() && 16430 Length->EvaluateAsInt(Result, Context) && 16431 Result.Val.getInt().isNullValue()) { 16432 Diag(ELoc, 16433 diag::err_omp_depend_zero_length_array_section_not_allowed) 16434 << SimpleExpr->getSourceRange(); 16435 continue; 16436 } 16437 } 16438 16439 // OpenMP 5.0, 2.17.11 depend Clause, Restrictions, C/C++ 16440 // List items used in depend clauses with the in, out, inout or 16441 // mutexinoutset dependence types cannot be expressions of the 16442 // omp_depend_t type. 16443 if (!RefExpr->isValueDependent() && !RefExpr->isTypeDependent() && 16444 !RefExpr->isInstantiationDependent() && 16445 !RefExpr->containsUnexpandedParameterPack() && 16446 (OMPDependTFound && 16447 DSAStack->getOMPDependT().getTypePtr() == ExprTy.getTypePtr())) { 16448 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 16449 << (LangOpts.OpenMP >= 50 ? 1 : 0) << 1 16450 << RefExpr->getSourceRange(); 16451 continue; 16452 } 16453 16454 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr); 16455 if (!RefExpr->IgnoreParenImpCasts()->isLValue() || 16456 (ASE && !ASE->getBase()->isTypeDependent() && 16457 !ASE->getBase() 16458 ->getType() 16459 .getNonReferenceType() 16460 ->isPointerType() && 16461 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) { 16462 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 16463 << (LangOpts.OpenMP >= 50 ? 1 : 0) 16464 << (LangOpts.OpenMP >= 50 ? 1 : 0) << RefExpr->getSourceRange(); 16465 continue; 16466 } 16467 16468 ExprResult Res; 16469 { 16470 Sema::TentativeAnalysisScope Trap(*this); 16471 Res = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, 16472 RefExpr->IgnoreParenImpCasts()); 16473 } 16474 if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr) && 16475 !isa<OMPArrayShapingExpr>(SimpleExpr)) { 16476 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 16477 << (LangOpts.OpenMP >= 50 ? 1 : 0) 16478 << (LangOpts.OpenMP >= 50 ? 1 : 0) << RefExpr->getSourceRange(); 16479 continue; 16480 } 16481 } 16482 } 16483 Vars.push_back(RefExpr->IgnoreParenImpCasts()); 16484 } 16485 16486 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink && 16487 TotalDepCount > VarList.size() && 16488 DSAStack->getParentOrderedRegionParam().first && 16489 DSAStack->getParentLoopControlVariable(VarList.size() + 1)) { 16490 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration) 16491 << 1 << DSAStack->getParentLoopControlVariable(VarList.size() + 1); 16492 } 16493 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink && 16494 Vars.empty()) 16495 return nullptr; 16496 16497 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, 16498 DepModifier, DepKind, DepLoc, ColonLoc, 16499 Vars, TotalDepCount.getZExtValue()); 16500 if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) && 16501 DSAStack->isParentOrderedRegion()) 16502 DSAStack->addDoacrossDependClause(C, OpsOffs); 16503 return C; 16504 } 16505 16506 OMPClause *Sema::ActOnOpenMPDeviceClause(OpenMPDeviceClauseModifier Modifier, 16507 Expr *Device, SourceLocation StartLoc, 16508 SourceLocation LParenLoc, 16509 SourceLocation ModifierLoc, 16510 SourceLocation EndLoc) { 16511 assert((ModifierLoc.isInvalid() || LangOpts.OpenMP >= 50) && 16512 "Unexpected device modifier in OpenMP < 50."); 16513 16514 bool ErrorFound = false; 16515 if (ModifierLoc.isValid() && Modifier == OMPC_DEVICE_unknown) { 16516 std::string Values = 16517 getListOfPossibleValues(OMPC_device, /*First=*/0, OMPC_DEVICE_unknown); 16518 Diag(ModifierLoc, diag::err_omp_unexpected_clause_value) 16519 << Values << getOpenMPClauseName(OMPC_device); 16520 ErrorFound = true; 16521 } 16522 16523 Expr *ValExpr = Device; 16524 Stmt *HelperValStmt = nullptr; 16525 16526 // OpenMP [2.9.1, Restrictions] 16527 // The device expression must evaluate to a non-negative integer value. 16528 ErrorFound = !isNonNegativeIntegerValue(ValExpr, *this, OMPC_device, 16529 /*StrictlyPositive=*/false) || 16530 ErrorFound; 16531 if (ErrorFound) 16532 return nullptr; 16533 16534 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 16535 OpenMPDirectiveKind CaptureRegion = 16536 getOpenMPCaptureRegionForClause(DKind, OMPC_device, LangOpts.OpenMP); 16537 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 16538 ValExpr = MakeFullExpr(ValExpr).get(); 16539 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 16540 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 16541 HelperValStmt = buildPreInits(Context, Captures); 16542 } 16543 16544 return new (Context) 16545 OMPDeviceClause(Modifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc, 16546 LParenLoc, ModifierLoc, EndLoc); 16547 } 16548 16549 static bool checkTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef, 16550 DSAStackTy *Stack, QualType QTy, 16551 bool FullCheck = true) { 16552 NamedDecl *ND; 16553 if (QTy->isIncompleteType(&ND)) { 16554 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR; 16555 return false; 16556 } 16557 if (FullCheck && !SemaRef.CurContext->isDependentContext() && 16558 !QTy.isTriviallyCopyableType(SemaRef.Context)) 16559 SemaRef.Diag(SL, diag::warn_omp_non_trivial_type_mapped) << QTy << SR; 16560 return true; 16561 } 16562 16563 /// Return true if it can be proven that the provided array expression 16564 /// (array section or array subscript) does NOT specify the whole size of the 16565 /// array whose base type is \a BaseQTy. 16566 static bool checkArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef, 16567 const Expr *E, 16568 QualType BaseQTy) { 16569 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E); 16570 16571 // If this is an array subscript, it refers to the whole size if the size of 16572 // the dimension is constant and equals 1. Also, an array section assumes the 16573 // format of an array subscript if no colon is used. 16574 if (isa<ArraySubscriptExpr>(E) || 16575 (OASE && OASE->getColonLocFirst().isInvalid())) { 16576 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) 16577 return ATy->getSize().getSExtValue() != 1; 16578 // Size can't be evaluated statically. 16579 return false; 16580 } 16581 16582 assert(OASE && "Expecting array section if not an array subscript."); 16583 const Expr *LowerBound = OASE->getLowerBound(); 16584 const Expr *Length = OASE->getLength(); 16585 16586 // If there is a lower bound that does not evaluates to zero, we are not 16587 // covering the whole dimension. 16588 if (LowerBound) { 16589 Expr::EvalResult Result; 16590 if (!LowerBound->EvaluateAsInt(Result, SemaRef.getASTContext())) 16591 return false; // Can't get the integer value as a constant. 16592 16593 llvm::APSInt ConstLowerBound = Result.Val.getInt(); 16594 if (ConstLowerBound.getSExtValue()) 16595 return true; 16596 } 16597 16598 // If we don't have a length we covering the whole dimension. 16599 if (!Length) 16600 return false; 16601 16602 // If the base is a pointer, we don't have a way to get the size of the 16603 // pointee. 16604 if (BaseQTy->isPointerType()) 16605 return false; 16606 16607 // We can only check if the length is the same as the size of the dimension 16608 // if we have a constant array. 16609 const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()); 16610 if (!CATy) 16611 return false; 16612 16613 Expr::EvalResult Result; 16614 if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext())) 16615 return false; // Can't get the integer value as a constant. 16616 16617 llvm::APSInt ConstLength = Result.Val.getInt(); 16618 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue(); 16619 } 16620 16621 // Return true if it can be proven that the provided array expression (array 16622 // section or array subscript) does NOT specify a single element of the array 16623 // whose base type is \a BaseQTy. 16624 static bool checkArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef, 16625 const Expr *E, 16626 QualType BaseQTy) { 16627 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E); 16628 16629 // An array subscript always refer to a single element. Also, an array section 16630 // assumes the format of an array subscript if no colon is used. 16631 if (isa<ArraySubscriptExpr>(E) || 16632 (OASE && OASE->getColonLocFirst().isInvalid())) 16633 return false; 16634 16635 assert(OASE && "Expecting array section if not an array subscript."); 16636 const Expr *Length = OASE->getLength(); 16637 16638 // If we don't have a length we have to check if the array has unitary size 16639 // for this dimension. Also, we should always expect a length if the base type 16640 // is pointer. 16641 if (!Length) { 16642 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) 16643 return ATy->getSize().getSExtValue() != 1; 16644 // We cannot assume anything. 16645 return false; 16646 } 16647 16648 // Check if the length evaluates to 1. 16649 Expr::EvalResult Result; 16650 if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext())) 16651 return false; // Can't get the integer value as a constant. 16652 16653 llvm::APSInt ConstLength = Result.Val.getInt(); 16654 return ConstLength.getSExtValue() != 1; 16655 } 16656 16657 // The base of elements of list in a map clause have to be either: 16658 // - a reference to variable or field. 16659 // - a member expression. 16660 // - an array expression. 16661 // 16662 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the 16663 // reference to 'r'. 16664 // 16665 // If we have: 16666 // 16667 // struct SS { 16668 // Bla S; 16669 // foo() { 16670 // #pragma omp target map (S.Arr[:12]); 16671 // } 16672 // } 16673 // 16674 // We want to retrieve the member expression 'this->S'; 16675 16676 // OpenMP 5.0 [2.19.7.1, map Clause, Restrictions, p.2] 16677 // If a list item is an array section, it must specify contiguous storage. 16678 // 16679 // For this restriction it is sufficient that we make sure only references 16680 // to variables or fields and array expressions, and that no array sections 16681 // exist except in the rightmost expression (unless they cover the whole 16682 // dimension of the array). E.g. these would be invalid: 16683 // 16684 // r.ArrS[3:5].Arr[6:7] 16685 // 16686 // r.ArrS[3:5].x 16687 // 16688 // but these would be valid: 16689 // r.ArrS[3].Arr[6:7] 16690 // 16691 // r.ArrS[3].x 16692 namespace { 16693 class MapBaseChecker final : public StmtVisitor<MapBaseChecker, bool> { 16694 Sema &SemaRef; 16695 OpenMPClauseKind CKind = OMPC_unknown; 16696 OMPClauseMappableExprCommon::MappableExprComponentList &Components; 16697 bool NoDiagnose = false; 16698 const Expr *RelevantExpr = nullptr; 16699 bool AllowUnitySizeArraySection = true; 16700 bool AllowWholeSizeArraySection = true; 16701 SourceLocation ELoc; 16702 SourceRange ERange; 16703 16704 void emitErrorMsg() { 16705 // If nothing else worked, this is not a valid map clause expression. 16706 if (SemaRef.getLangOpts().OpenMP < 50) { 16707 SemaRef.Diag(ELoc, 16708 diag::err_omp_expected_named_var_member_or_array_expression) 16709 << ERange; 16710 } else { 16711 SemaRef.Diag(ELoc, diag::err_omp_non_lvalue_in_map_or_motion_clauses) 16712 << getOpenMPClauseName(CKind) << ERange; 16713 } 16714 } 16715 16716 public: 16717 bool VisitDeclRefExpr(DeclRefExpr *DRE) { 16718 if (!isa<VarDecl>(DRE->getDecl())) { 16719 emitErrorMsg(); 16720 return false; 16721 } 16722 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 16723 RelevantExpr = DRE; 16724 // Record the component. 16725 Components.emplace_back(DRE, DRE->getDecl()); 16726 return true; 16727 } 16728 16729 bool VisitMemberExpr(MemberExpr *ME) { 16730 Expr *E = ME; 16731 Expr *BaseE = ME->getBase()->IgnoreParenCasts(); 16732 16733 if (isa<CXXThisExpr>(BaseE)) { 16734 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 16735 // We found a base expression: this->Val. 16736 RelevantExpr = ME; 16737 } else { 16738 E = BaseE; 16739 } 16740 16741 if (!isa<FieldDecl>(ME->getMemberDecl())) { 16742 if (!NoDiagnose) { 16743 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field) 16744 << ME->getSourceRange(); 16745 return false; 16746 } 16747 if (RelevantExpr) 16748 return false; 16749 return Visit(E); 16750 } 16751 16752 auto *FD = cast<FieldDecl>(ME->getMemberDecl()); 16753 16754 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3] 16755 // A bit-field cannot appear in a map clause. 16756 // 16757 if (FD->isBitField()) { 16758 if (!NoDiagnose) { 16759 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause) 16760 << ME->getSourceRange() << getOpenMPClauseName(CKind); 16761 return false; 16762 } 16763 if (RelevantExpr) 16764 return false; 16765 return Visit(E); 16766 } 16767 16768 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 16769 // If the type of a list item is a reference to a type T then the type 16770 // will be considered to be T for all purposes of this clause. 16771 QualType CurType = BaseE->getType().getNonReferenceType(); 16772 16773 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2] 16774 // A list item cannot be a variable that is a member of a structure with 16775 // a union type. 16776 // 16777 if (CurType->isUnionType()) { 16778 if (!NoDiagnose) { 16779 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed) 16780 << ME->getSourceRange(); 16781 return false; 16782 } 16783 return RelevantExpr || Visit(E); 16784 } 16785 16786 // If we got a member expression, we should not expect any array section 16787 // before that: 16788 // 16789 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7] 16790 // If a list item is an element of a structure, only the rightmost symbol 16791 // of the variable reference can be an array section. 16792 // 16793 AllowUnitySizeArraySection = false; 16794 AllowWholeSizeArraySection = false; 16795 16796 // Record the component. 16797 Components.emplace_back(ME, FD); 16798 return RelevantExpr || Visit(E); 16799 } 16800 16801 bool VisitArraySubscriptExpr(ArraySubscriptExpr *AE) { 16802 Expr *E = AE->getBase()->IgnoreParenImpCasts(); 16803 16804 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) { 16805 if (!NoDiagnose) { 16806 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name) 16807 << 0 << AE->getSourceRange(); 16808 return false; 16809 } 16810 return RelevantExpr || Visit(E); 16811 } 16812 16813 // If we got an array subscript that express the whole dimension we 16814 // can have any array expressions before. If it only expressing part of 16815 // the dimension, we can only have unitary-size array expressions. 16816 if (checkArrayExpressionDoesNotReferToWholeSize(SemaRef, AE, 16817 E->getType())) 16818 AllowWholeSizeArraySection = false; 16819 16820 if (const auto *TE = dyn_cast<CXXThisExpr>(E->IgnoreParenCasts())) { 16821 Expr::EvalResult Result; 16822 if (!AE->getIdx()->isValueDependent() && 16823 AE->getIdx()->EvaluateAsInt(Result, SemaRef.getASTContext()) && 16824 !Result.Val.getInt().isNullValue()) { 16825 SemaRef.Diag(AE->getIdx()->getExprLoc(), 16826 diag::err_omp_invalid_map_this_expr); 16827 SemaRef.Diag(AE->getIdx()->getExprLoc(), 16828 diag::note_omp_invalid_subscript_on_this_ptr_map); 16829 } 16830 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 16831 RelevantExpr = TE; 16832 } 16833 16834 // Record the component - we don't have any declaration associated. 16835 Components.emplace_back(AE, nullptr); 16836 16837 return RelevantExpr || Visit(E); 16838 } 16839 16840 bool VisitOMPArraySectionExpr(OMPArraySectionExpr *OASE) { 16841 assert(!NoDiagnose && "Array sections cannot be implicitly mapped."); 16842 Expr *E = OASE->getBase()->IgnoreParenImpCasts(); 16843 QualType CurType = 16844 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType(); 16845 16846 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 16847 // If the type of a list item is a reference to a type T then the type 16848 // will be considered to be T for all purposes of this clause. 16849 if (CurType->isReferenceType()) 16850 CurType = CurType->getPointeeType(); 16851 16852 bool IsPointer = CurType->isAnyPointerType(); 16853 16854 if (!IsPointer && !CurType->isArrayType()) { 16855 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name) 16856 << 0 << OASE->getSourceRange(); 16857 return false; 16858 } 16859 16860 bool NotWhole = 16861 checkArrayExpressionDoesNotReferToWholeSize(SemaRef, OASE, CurType); 16862 bool NotUnity = 16863 checkArrayExpressionDoesNotReferToUnitySize(SemaRef, OASE, CurType); 16864 16865 if (AllowWholeSizeArraySection) { 16866 // Any array section is currently allowed. Allowing a whole size array 16867 // section implies allowing a unity array section as well. 16868 // 16869 // If this array section refers to the whole dimension we can still 16870 // accept other array sections before this one, except if the base is a 16871 // pointer. Otherwise, only unitary sections are accepted. 16872 if (NotWhole || IsPointer) 16873 AllowWholeSizeArraySection = false; 16874 } else if (AllowUnitySizeArraySection && NotUnity) { 16875 // A unity or whole array section is not allowed and that is not 16876 // compatible with the properties of the current array section. 16877 SemaRef.Diag( 16878 ELoc, diag::err_array_section_does_not_specify_contiguous_storage) 16879 << OASE->getSourceRange(); 16880 return false; 16881 } 16882 16883 if (const auto *TE = dyn_cast<CXXThisExpr>(E)) { 16884 Expr::EvalResult ResultR; 16885 Expr::EvalResult ResultL; 16886 if (!OASE->getLength()->isValueDependent() && 16887 OASE->getLength()->EvaluateAsInt(ResultR, SemaRef.getASTContext()) && 16888 !ResultR.Val.getInt().isOneValue()) { 16889 SemaRef.Diag(OASE->getLength()->getExprLoc(), 16890 diag::err_omp_invalid_map_this_expr); 16891 SemaRef.Diag(OASE->getLength()->getExprLoc(), 16892 diag::note_omp_invalid_length_on_this_ptr_mapping); 16893 } 16894 if (OASE->getLowerBound() && !OASE->getLowerBound()->isValueDependent() && 16895 OASE->getLowerBound()->EvaluateAsInt(ResultL, 16896 SemaRef.getASTContext()) && 16897 !ResultL.Val.getInt().isNullValue()) { 16898 SemaRef.Diag(OASE->getLowerBound()->getExprLoc(), 16899 diag::err_omp_invalid_map_this_expr); 16900 SemaRef.Diag(OASE->getLowerBound()->getExprLoc(), 16901 diag::note_omp_invalid_lower_bound_on_this_ptr_mapping); 16902 } 16903 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 16904 RelevantExpr = TE; 16905 } 16906 16907 // Record the component - we don't have any declaration associated. 16908 Components.emplace_back(OASE, nullptr); 16909 return RelevantExpr || Visit(E); 16910 } 16911 bool VisitOMPArrayShapingExpr(OMPArrayShapingExpr *E) { 16912 Expr *Base = E->getBase(); 16913 16914 // Record the component - we don't have any declaration associated. 16915 Components.emplace_back(E, nullptr); 16916 16917 return Visit(Base->IgnoreParenImpCasts()); 16918 } 16919 16920 bool VisitUnaryOperator(UnaryOperator *UO) { 16921 if (SemaRef.getLangOpts().OpenMP < 50 || !UO->isLValue() || 16922 UO->getOpcode() != UO_Deref) { 16923 emitErrorMsg(); 16924 return false; 16925 } 16926 if (!RelevantExpr) { 16927 // Record the component if haven't found base decl. 16928 Components.emplace_back(UO, nullptr); 16929 } 16930 return RelevantExpr || Visit(UO->getSubExpr()->IgnoreParenImpCasts()); 16931 } 16932 bool VisitBinaryOperator(BinaryOperator *BO) { 16933 if (SemaRef.getLangOpts().OpenMP < 50 || !BO->getType()->isPointerType()) { 16934 emitErrorMsg(); 16935 return false; 16936 } 16937 16938 // Pointer arithmetic is the only thing we expect to happen here so after we 16939 // make sure the binary operator is a pointer type, the we only thing need 16940 // to to is to visit the subtree that has the same type as root (so that we 16941 // know the other subtree is just an offset) 16942 Expr *LE = BO->getLHS()->IgnoreParenImpCasts(); 16943 Expr *RE = BO->getRHS()->IgnoreParenImpCasts(); 16944 Components.emplace_back(BO, nullptr); 16945 assert((LE->getType().getTypePtr() == BO->getType().getTypePtr() || 16946 RE->getType().getTypePtr() == BO->getType().getTypePtr()) && 16947 "Either LHS or RHS have base decl inside"); 16948 if (BO->getType().getTypePtr() == LE->getType().getTypePtr()) 16949 return RelevantExpr || Visit(LE); 16950 return RelevantExpr || Visit(RE); 16951 } 16952 bool VisitCXXThisExpr(CXXThisExpr *CTE) { 16953 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 16954 RelevantExpr = CTE; 16955 Components.emplace_back(CTE, nullptr); 16956 return true; 16957 } 16958 bool VisitStmt(Stmt *) { 16959 emitErrorMsg(); 16960 return false; 16961 } 16962 const Expr *getFoundBase() const { 16963 return RelevantExpr; 16964 } 16965 explicit MapBaseChecker( 16966 Sema &SemaRef, OpenMPClauseKind CKind, 16967 OMPClauseMappableExprCommon::MappableExprComponentList &Components, 16968 bool NoDiagnose, SourceLocation &ELoc, SourceRange &ERange) 16969 : SemaRef(SemaRef), CKind(CKind), Components(Components), 16970 NoDiagnose(NoDiagnose), ELoc(ELoc), ERange(ERange) {} 16971 }; 16972 } // namespace 16973 16974 /// Return the expression of the base of the mappable expression or null if it 16975 /// cannot be determined and do all the necessary checks to see if the expression 16976 /// is valid as a standalone mappable expression. In the process, record all the 16977 /// components of the expression. 16978 static const Expr *checkMapClauseExpressionBase( 16979 Sema &SemaRef, Expr *E, 16980 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents, 16981 OpenMPClauseKind CKind, bool NoDiagnose) { 16982 SourceLocation ELoc = E->getExprLoc(); 16983 SourceRange ERange = E->getSourceRange(); 16984 MapBaseChecker Checker(SemaRef, CKind, CurComponents, NoDiagnose, ELoc, 16985 ERange); 16986 if (Checker.Visit(E->IgnoreParens())) 16987 return Checker.getFoundBase(); 16988 return nullptr; 16989 } 16990 16991 // Return true if expression E associated with value VD has conflicts with other 16992 // map information. 16993 static bool checkMapConflicts( 16994 Sema &SemaRef, DSAStackTy *DSAS, const ValueDecl *VD, const Expr *E, 16995 bool CurrentRegionOnly, 16996 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents, 16997 OpenMPClauseKind CKind) { 16998 assert(VD && E); 16999 SourceLocation ELoc = E->getExprLoc(); 17000 SourceRange ERange = E->getSourceRange(); 17001 17002 // In order to easily check the conflicts we need to match each component of 17003 // the expression under test with the components of the expressions that are 17004 // already in the stack. 17005 17006 assert(!CurComponents.empty() && "Map clause expression with no components!"); 17007 assert(CurComponents.back().getAssociatedDeclaration() == VD && 17008 "Map clause expression with unexpected base!"); 17009 17010 // Variables to help detecting enclosing problems in data environment nests. 17011 bool IsEnclosedByDataEnvironmentExpr = false; 17012 const Expr *EnclosingExpr = nullptr; 17013 17014 bool FoundError = DSAS->checkMappableExprComponentListsForDecl( 17015 VD, CurrentRegionOnly, 17016 [&IsEnclosedByDataEnvironmentExpr, &SemaRef, VD, CurrentRegionOnly, ELoc, 17017 ERange, CKind, &EnclosingExpr, 17018 CurComponents](OMPClauseMappableExprCommon::MappableExprComponentListRef 17019 StackComponents, 17020 OpenMPClauseKind) { 17021 assert(!StackComponents.empty() && 17022 "Map clause expression with no components!"); 17023 assert(StackComponents.back().getAssociatedDeclaration() == VD && 17024 "Map clause expression with unexpected base!"); 17025 (void)VD; 17026 17027 // The whole expression in the stack. 17028 const Expr *RE = StackComponents.front().getAssociatedExpression(); 17029 17030 // Expressions must start from the same base. Here we detect at which 17031 // point both expressions diverge from each other and see if we can 17032 // detect if the memory referred to both expressions is contiguous and 17033 // do not overlap. 17034 auto CI = CurComponents.rbegin(); 17035 auto CE = CurComponents.rend(); 17036 auto SI = StackComponents.rbegin(); 17037 auto SE = StackComponents.rend(); 17038 for (; CI != CE && SI != SE; ++CI, ++SI) { 17039 17040 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3] 17041 // At most one list item can be an array item derived from a given 17042 // variable in map clauses of the same construct. 17043 if (CurrentRegionOnly && 17044 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) || 17045 isa<OMPArraySectionExpr>(CI->getAssociatedExpression()) || 17046 isa<OMPArrayShapingExpr>(CI->getAssociatedExpression())) && 17047 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) || 17048 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()) || 17049 isa<OMPArrayShapingExpr>(SI->getAssociatedExpression()))) { 17050 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(), 17051 diag::err_omp_multiple_array_items_in_map_clause) 17052 << CI->getAssociatedExpression()->getSourceRange(); 17053 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(), 17054 diag::note_used_here) 17055 << SI->getAssociatedExpression()->getSourceRange(); 17056 return true; 17057 } 17058 17059 // Do both expressions have the same kind? 17060 if (CI->getAssociatedExpression()->getStmtClass() != 17061 SI->getAssociatedExpression()->getStmtClass()) 17062 break; 17063 17064 // Are we dealing with different variables/fields? 17065 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration()) 17066 break; 17067 } 17068 // Check if the extra components of the expressions in the enclosing 17069 // data environment are redundant for the current base declaration. 17070 // If they are, the maps completely overlap, which is legal. 17071 for (; SI != SE; ++SI) { 17072 QualType Type; 17073 if (const auto *ASE = 17074 dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) { 17075 Type = ASE->getBase()->IgnoreParenImpCasts()->getType(); 17076 } else if (const auto *OASE = dyn_cast<OMPArraySectionExpr>( 17077 SI->getAssociatedExpression())) { 17078 const Expr *E = OASE->getBase()->IgnoreParenImpCasts(); 17079 Type = 17080 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType(); 17081 } else if (const auto *OASE = dyn_cast<OMPArrayShapingExpr>( 17082 SI->getAssociatedExpression())) { 17083 Type = OASE->getBase()->getType()->getPointeeType(); 17084 } 17085 if (Type.isNull() || Type->isAnyPointerType() || 17086 checkArrayExpressionDoesNotReferToWholeSize( 17087 SemaRef, SI->getAssociatedExpression(), Type)) 17088 break; 17089 } 17090 17091 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4] 17092 // List items of map clauses in the same construct must not share 17093 // original storage. 17094 // 17095 // If the expressions are exactly the same or one is a subset of the 17096 // other, it means they are sharing storage. 17097 if (CI == CE && SI == SE) { 17098 if (CurrentRegionOnly) { 17099 if (CKind == OMPC_map) { 17100 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange; 17101 } else { 17102 assert(CKind == OMPC_to || CKind == OMPC_from); 17103 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update) 17104 << ERange; 17105 } 17106 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 17107 << RE->getSourceRange(); 17108 return true; 17109 } 17110 // If we find the same expression in the enclosing data environment, 17111 // that is legal. 17112 IsEnclosedByDataEnvironmentExpr = true; 17113 return false; 17114 } 17115 17116 QualType DerivedType = 17117 std::prev(CI)->getAssociatedDeclaration()->getType(); 17118 SourceLocation DerivedLoc = 17119 std::prev(CI)->getAssociatedExpression()->getExprLoc(); 17120 17121 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 17122 // If the type of a list item is a reference to a type T then the type 17123 // will be considered to be T for all purposes of this clause. 17124 DerivedType = DerivedType.getNonReferenceType(); 17125 17126 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1] 17127 // A variable for which the type is pointer and an array section 17128 // derived from that variable must not appear as list items of map 17129 // clauses of the same construct. 17130 // 17131 // Also, cover one of the cases in: 17132 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5] 17133 // If any part of the original storage of a list item has corresponding 17134 // storage in the device data environment, all of the original storage 17135 // must have corresponding storage in the device data environment. 17136 // 17137 if (DerivedType->isAnyPointerType()) { 17138 if (CI == CE || SI == SE) { 17139 SemaRef.Diag( 17140 DerivedLoc, 17141 diag::err_omp_pointer_mapped_along_with_derived_section) 17142 << DerivedLoc; 17143 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 17144 << RE->getSourceRange(); 17145 return true; 17146 } 17147 if (CI->getAssociatedExpression()->getStmtClass() != 17148 SI->getAssociatedExpression()->getStmtClass() || 17149 CI->getAssociatedDeclaration()->getCanonicalDecl() == 17150 SI->getAssociatedDeclaration()->getCanonicalDecl()) { 17151 assert(CI != CE && SI != SE); 17152 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_dereferenced) 17153 << DerivedLoc; 17154 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 17155 << RE->getSourceRange(); 17156 return true; 17157 } 17158 } 17159 17160 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4] 17161 // List items of map clauses in the same construct must not share 17162 // original storage. 17163 // 17164 // An expression is a subset of the other. 17165 if (CurrentRegionOnly && (CI == CE || SI == SE)) { 17166 if (CKind == OMPC_map) { 17167 if (CI != CE || SI != SE) { 17168 // Allow constructs like this: map(s, s.ptr[0:1]), where s.ptr is 17169 // a pointer. 17170 auto Begin = 17171 CI != CE ? CurComponents.begin() : StackComponents.begin(); 17172 auto End = CI != CE ? CurComponents.end() : StackComponents.end(); 17173 auto It = Begin; 17174 while (It != End && !It->getAssociatedDeclaration()) 17175 std::advance(It, 1); 17176 assert(It != End && 17177 "Expected at least one component with the declaration."); 17178 if (It != Begin && It->getAssociatedDeclaration() 17179 ->getType() 17180 .getCanonicalType() 17181 ->isAnyPointerType()) { 17182 IsEnclosedByDataEnvironmentExpr = false; 17183 EnclosingExpr = nullptr; 17184 return false; 17185 } 17186 } 17187 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange; 17188 } else { 17189 assert(CKind == OMPC_to || CKind == OMPC_from); 17190 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update) 17191 << ERange; 17192 } 17193 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 17194 << RE->getSourceRange(); 17195 return true; 17196 } 17197 17198 // The current expression uses the same base as other expression in the 17199 // data environment but does not contain it completely. 17200 if (!CurrentRegionOnly && SI != SE) 17201 EnclosingExpr = RE; 17202 17203 // The current expression is a subset of the expression in the data 17204 // environment. 17205 IsEnclosedByDataEnvironmentExpr |= 17206 (!CurrentRegionOnly && CI != CE && SI == SE); 17207 17208 return false; 17209 }); 17210 17211 if (CurrentRegionOnly) 17212 return FoundError; 17213 17214 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5] 17215 // If any part of the original storage of a list item has corresponding 17216 // storage in the device data environment, all of the original storage must 17217 // have corresponding storage in the device data environment. 17218 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6] 17219 // If a list item is an element of a structure, and a different element of 17220 // the structure has a corresponding list item in the device data environment 17221 // prior to a task encountering the construct associated with the map clause, 17222 // then the list item must also have a corresponding list item in the device 17223 // data environment prior to the task encountering the construct. 17224 // 17225 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) { 17226 SemaRef.Diag(ELoc, 17227 diag::err_omp_original_storage_is_shared_and_does_not_contain) 17228 << ERange; 17229 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here) 17230 << EnclosingExpr->getSourceRange(); 17231 return true; 17232 } 17233 17234 return FoundError; 17235 } 17236 17237 // Look up the user-defined mapper given the mapper name and mapped type, and 17238 // build a reference to it. 17239 static ExprResult buildUserDefinedMapperRef(Sema &SemaRef, Scope *S, 17240 CXXScopeSpec &MapperIdScopeSpec, 17241 const DeclarationNameInfo &MapperId, 17242 QualType Type, 17243 Expr *UnresolvedMapper) { 17244 if (MapperIdScopeSpec.isInvalid()) 17245 return ExprError(); 17246 // Get the actual type for the array type. 17247 if (Type->isArrayType()) { 17248 assert(Type->getAsArrayTypeUnsafe() && "Expect to get a valid array type"); 17249 Type = Type->getAsArrayTypeUnsafe()->getElementType().getCanonicalType(); 17250 } 17251 // Find all user-defined mappers with the given MapperId. 17252 SmallVector<UnresolvedSet<8>, 4> Lookups; 17253 LookupResult Lookup(SemaRef, MapperId, Sema::LookupOMPMapperName); 17254 Lookup.suppressDiagnostics(); 17255 if (S) { 17256 while (S && SemaRef.LookupParsedName(Lookup, S, &MapperIdScopeSpec)) { 17257 NamedDecl *D = Lookup.getRepresentativeDecl(); 17258 while (S && !S->isDeclScope(D)) 17259 S = S->getParent(); 17260 if (S) 17261 S = S->getParent(); 17262 Lookups.emplace_back(); 17263 Lookups.back().append(Lookup.begin(), Lookup.end()); 17264 Lookup.clear(); 17265 } 17266 } else if (auto *ULE = cast_or_null<UnresolvedLookupExpr>(UnresolvedMapper)) { 17267 // Extract the user-defined mappers with the given MapperId. 17268 Lookups.push_back(UnresolvedSet<8>()); 17269 for (NamedDecl *D : ULE->decls()) { 17270 auto *DMD = cast<OMPDeclareMapperDecl>(D); 17271 assert(DMD && "Expect valid OMPDeclareMapperDecl during instantiation."); 17272 Lookups.back().addDecl(DMD); 17273 } 17274 } 17275 // Defer the lookup for dependent types. The results will be passed through 17276 // UnresolvedMapper on instantiation. 17277 if (SemaRef.CurContext->isDependentContext() || Type->isDependentType() || 17278 Type->isInstantiationDependentType() || 17279 Type->containsUnexpandedParameterPack() || 17280 filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) { 17281 return !D->isInvalidDecl() && 17282 (D->getType()->isDependentType() || 17283 D->getType()->isInstantiationDependentType() || 17284 D->getType()->containsUnexpandedParameterPack()); 17285 })) { 17286 UnresolvedSet<8> URS; 17287 for (const UnresolvedSet<8> &Set : Lookups) { 17288 if (Set.empty()) 17289 continue; 17290 URS.append(Set.begin(), Set.end()); 17291 } 17292 return UnresolvedLookupExpr::Create( 17293 SemaRef.Context, /*NamingClass=*/nullptr, 17294 MapperIdScopeSpec.getWithLocInContext(SemaRef.Context), MapperId, 17295 /*ADL=*/false, /*Overloaded=*/true, URS.begin(), URS.end()); 17296 } 17297 SourceLocation Loc = MapperId.getLoc(); 17298 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions 17299 // The type must be of struct, union or class type in C and C++ 17300 if (!Type->isStructureOrClassType() && !Type->isUnionType() && 17301 (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default")) { 17302 SemaRef.Diag(Loc, diag::err_omp_mapper_wrong_type); 17303 return ExprError(); 17304 } 17305 // Perform argument dependent lookup. 17306 if (SemaRef.getLangOpts().CPlusPlus && !MapperIdScopeSpec.isSet()) 17307 argumentDependentLookup(SemaRef, MapperId, Loc, Type, Lookups); 17308 // Return the first user-defined mapper with the desired type. 17309 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 17310 Lookups, [&SemaRef, Type](ValueDecl *D) -> ValueDecl * { 17311 if (!D->isInvalidDecl() && 17312 SemaRef.Context.hasSameType(D->getType(), Type)) 17313 return D; 17314 return nullptr; 17315 })) 17316 return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc); 17317 // Find the first user-defined mapper with a type derived from the desired 17318 // type. 17319 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 17320 Lookups, [&SemaRef, Type, Loc](ValueDecl *D) -> ValueDecl * { 17321 if (!D->isInvalidDecl() && 17322 SemaRef.IsDerivedFrom(Loc, Type, D->getType()) && 17323 !Type.isMoreQualifiedThan(D->getType())) 17324 return D; 17325 return nullptr; 17326 })) { 17327 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, 17328 /*DetectVirtual=*/false); 17329 if (SemaRef.IsDerivedFrom(Loc, Type, VD->getType(), Paths)) { 17330 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType( 17331 VD->getType().getUnqualifiedType()))) { 17332 if (SemaRef.CheckBaseClassAccess( 17333 Loc, VD->getType(), Type, Paths.front(), 17334 /*DiagID=*/0) != Sema::AR_inaccessible) { 17335 return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc); 17336 } 17337 } 17338 } 17339 } 17340 // Report error if a mapper is specified, but cannot be found. 17341 if (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default") { 17342 SemaRef.Diag(Loc, diag::err_omp_invalid_mapper) 17343 << Type << MapperId.getName(); 17344 return ExprError(); 17345 } 17346 return ExprEmpty(); 17347 } 17348 17349 namespace { 17350 // Utility struct that gathers all the related lists associated with a mappable 17351 // expression. 17352 struct MappableVarListInfo { 17353 // The list of expressions. 17354 ArrayRef<Expr *> VarList; 17355 // The list of processed expressions. 17356 SmallVector<Expr *, 16> ProcessedVarList; 17357 // The mappble components for each expression. 17358 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents; 17359 // The base declaration of the variable. 17360 SmallVector<ValueDecl *, 16> VarBaseDeclarations; 17361 // The reference to the user-defined mapper associated with every expression. 17362 SmallVector<Expr *, 16> UDMapperList; 17363 17364 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) { 17365 // We have a list of components and base declarations for each entry in the 17366 // variable list. 17367 VarComponents.reserve(VarList.size()); 17368 VarBaseDeclarations.reserve(VarList.size()); 17369 } 17370 }; 17371 } 17372 17373 // Check the validity of the provided variable list for the provided clause kind 17374 // \a CKind. In the check process the valid expressions, mappable expression 17375 // components, variables, and user-defined mappers are extracted and used to 17376 // fill \a ProcessedVarList, \a VarComponents, \a VarBaseDeclarations, and \a 17377 // UDMapperList in MVLI. \a MapType, \a IsMapTypeImplicit, \a MapperIdScopeSpec, 17378 // and \a MapperId are expected to be valid if the clause kind is 'map'. 17379 static void checkMappableExpressionList( 17380 Sema &SemaRef, DSAStackTy *DSAS, OpenMPClauseKind CKind, 17381 MappableVarListInfo &MVLI, SourceLocation StartLoc, 17382 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo MapperId, 17383 ArrayRef<Expr *> UnresolvedMappers, 17384 OpenMPMapClauseKind MapType = OMPC_MAP_unknown, 17385 bool IsMapTypeImplicit = false) { 17386 // We only expect mappable expressions in 'to', 'from', and 'map' clauses. 17387 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) && 17388 "Unexpected clause kind with mappable expressions!"); 17389 17390 // If the identifier of user-defined mapper is not specified, it is "default". 17391 // We do not change the actual name in this clause to distinguish whether a 17392 // mapper is specified explicitly, i.e., it is not explicitly specified when 17393 // MapperId.getName() is empty. 17394 if (!MapperId.getName() || MapperId.getName().isEmpty()) { 17395 auto &DeclNames = SemaRef.getASTContext().DeclarationNames; 17396 MapperId.setName(DeclNames.getIdentifier( 17397 &SemaRef.getASTContext().Idents.get("default"))); 17398 } 17399 17400 // Iterators to find the current unresolved mapper expression. 17401 auto UMIt = UnresolvedMappers.begin(), UMEnd = UnresolvedMappers.end(); 17402 bool UpdateUMIt = false; 17403 Expr *UnresolvedMapper = nullptr; 17404 17405 // Keep track of the mappable components and base declarations in this clause. 17406 // Each entry in the list is going to have a list of components associated. We 17407 // record each set of the components so that we can build the clause later on. 17408 // In the end we should have the same amount of declarations and component 17409 // lists. 17410 17411 for (Expr *RE : MVLI.VarList) { 17412 assert(RE && "Null expr in omp to/from/map clause"); 17413 SourceLocation ELoc = RE->getExprLoc(); 17414 17415 // Find the current unresolved mapper expression. 17416 if (UpdateUMIt && UMIt != UMEnd) { 17417 UMIt++; 17418 assert( 17419 UMIt != UMEnd && 17420 "Expect the size of UnresolvedMappers to match with that of VarList"); 17421 } 17422 UpdateUMIt = true; 17423 if (UMIt != UMEnd) 17424 UnresolvedMapper = *UMIt; 17425 17426 const Expr *VE = RE->IgnoreParenLValueCasts(); 17427 17428 if (VE->isValueDependent() || VE->isTypeDependent() || 17429 VE->isInstantiationDependent() || 17430 VE->containsUnexpandedParameterPack()) { 17431 // Try to find the associated user-defined mapper. 17432 ExprResult ER = buildUserDefinedMapperRef( 17433 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId, 17434 VE->getType().getCanonicalType(), UnresolvedMapper); 17435 if (ER.isInvalid()) 17436 continue; 17437 MVLI.UDMapperList.push_back(ER.get()); 17438 // We can only analyze this information once the missing information is 17439 // resolved. 17440 MVLI.ProcessedVarList.push_back(RE); 17441 continue; 17442 } 17443 17444 Expr *SimpleExpr = RE->IgnoreParenCasts(); 17445 17446 if (!RE->isLValue()) { 17447 if (SemaRef.getLangOpts().OpenMP < 50) { 17448 SemaRef.Diag( 17449 ELoc, diag::err_omp_expected_named_var_member_or_array_expression) 17450 << RE->getSourceRange(); 17451 } else { 17452 SemaRef.Diag(ELoc, diag::err_omp_non_lvalue_in_map_or_motion_clauses) 17453 << getOpenMPClauseName(CKind) << RE->getSourceRange(); 17454 } 17455 continue; 17456 } 17457 17458 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents; 17459 ValueDecl *CurDeclaration = nullptr; 17460 17461 // Obtain the array or member expression bases if required. Also, fill the 17462 // components array with all the components identified in the process. 17463 const Expr *BE = checkMapClauseExpressionBase( 17464 SemaRef, SimpleExpr, CurComponents, CKind, /*NoDiagnose=*/false); 17465 if (!BE) 17466 continue; 17467 17468 assert(!CurComponents.empty() && 17469 "Invalid mappable expression information."); 17470 17471 if (const auto *TE = dyn_cast<CXXThisExpr>(BE)) { 17472 // Add store "this" pointer to class in DSAStackTy for future checking 17473 DSAS->addMappedClassesQualTypes(TE->getType()); 17474 // Try to find the associated user-defined mapper. 17475 ExprResult ER = buildUserDefinedMapperRef( 17476 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId, 17477 VE->getType().getCanonicalType(), UnresolvedMapper); 17478 if (ER.isInvalid()) 17479 continue; 17480 MVLI.UDMapperList.push_back(ER.get()); 17481 // Skip restriction checking for variable or field declarations 17482 MVLI.ProcessedVarList.push_back(RE); 17483 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 17484 MVLI.VarComponents.back().append(CurComponents.begin(), 17485 CurComponents.end()); 17486 MVLI.VarBaseDeclarations.push_back(nullptr); 17487 continue; 17488 } 17489 17490 // For the following checks, we rely on the base declaration which is 17491 // expected to be associated with the last component. The declaration is 17492 // expected to be a variable or a field (if 'this' is being mapped). 17493 CurDeclaration = CurComponents.back().getAssociatedDeclaration(); 17494 assert(CurDeclaration && "Null decl on map clause."); 17495 assert( 17496 CurDeclaration->isCanonicalDecl() && 17497 "Expecting components to have associated only canonical declarations."); 17498 17499 auto *VD = dyn_cast<VarDecl>(CurDeclaration); 17500 const auto *FD = dyn_cast<FieldDecl>(CurDeclaration); 17501 17502 assert((VD || FD) && "Only variables or fields are expected here!"); 17503 (void)FD; 17504 17505 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10] 17506 // threadprivate variables cannot appear in a map clause. 17507 // OpenMP 4.5 [2.10.5, target update Construct] 17508 // threadprivate variables cannot appear in a from clause. 17509 if (VD && DSAS->isThreadPrivate(VD)) { 17510 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false); 17511 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause) 17512 << getOpenMPClauseName(CKind); 17513 reportOriginalDsa(SemaRef, DSAS, VD, DVar); 17514 continue; 17515 } 17516 17517 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9] 17518 // A list item cannot appear in both a map clause and a data-sharing 17519 // attribute clause on the same construct. 17520 17521 // Check conflicts with other map clause expressions. We check the conflicts 17522 // with the current construct separately from the enclosing data 17523 // environment, because the restrictions are different. We only have to 17524 // check conflicts across regions for the map clauses. 17525 if (checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr, 17526 /*CurrentRegionOnly=*/true, CurComponents, CKind)) 17527 break; 17528 if (CKind == OMPC_map && 17529 (SemaRef.getLangOpts().OpenMP <= 45 || StartLoc.isValid()) && 17530 checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr, 17531 /*CurrentRegionOnly=*/false, CurComponents, CKind)) 17532 break; 17533 17534 // OpenMP 4.5 [2.10.5, target update Construct] 17535 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 17536 // If the type of a list item is a reference to a type T then the type will 17537 // be considered to be T for all purposes of this clause. 17538 auto I = llvm::find_if( 17539 CurComponents, 17540 [](const OMPClauseMappableExprCommon::MappableComponent &MC) { 17541 return MC.getAssociatedDeclaration(); 17542 }); 17543 assert(I != CurComponents.end() && "Null decl on map clause."); 17544 QualType Type; 17545 auto *ASE = dyn_cast<ArraySubscriptExpr>(VE->IgnoreParens()); 17546 auto *OASE = dyn_cast<OMPArraySectionExpr>(VE->IgnoreParens()); 17547 auto *OAShE = dyn_cast<OMPArrayShapingExpr>(VE->IgnoreParens()); 17548 if (ASE) { 17549 Type = ASE->getType().getNonReferenceType(); 17550 } else if (OASE) { 17551 QualType BaseType = 17552 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 17553 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 17554 Type = ATy->getElementType(); 17555 else 17556 Type = BaseType->getPointeeType(); 17557 Type = Type.getNonReferenceType(); 17558 } else if (OAShE) { 17559 Type = OAShE->getBase()->getType()->getPointeeType(); 17560 } else { 17561 Type = VE->getType(); 17562 } 17563 17564 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4] 17565 // A list item in a to or from clause must have a mappable type. 17566 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9] 17567 // A list item must have a mappable type. 17568 if (!checkTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef, 17569 DSAS, Type)) 17570 continue; 17571 17572 Type = I->getAssociatedDeclaration()->getType().getNonReferenceType(); 17573 17574 if (CKind == OMPC_map) { 17575 // target enter data 17576 // OpenMP [2.10.2, Restrictions, p. 99] 17577 // A map-type must be specified in all map clauses and must be either 17578 // to or alloc. 17579 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective(); 17580 if (DKind == OMPD_target_enter_data && 17581 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) { 17582 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 17583 << (IsMapTypeImplicit ? 1 : 0) 17584 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 17585 << getOpenMPDirectiveName(DKind); 17586 continue; 17587 } 17588 17589 // target exit_data 17590 // OpenMP [2.10.3, Restrictions, p. 102] 17591 // A map-type must be specified in all map clauses and must be either 17592 // from, release, or delete. 17593 if (DKind == OMPD_target_exit_data && 17594 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release || 17595 MapType == OMPC_MAP_delete)) { 17596 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 17597 << (IsMapTypeImplicit ? 1 : 0) 17598 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 17599 << getOpenMPDirectiveName(DKind); 17600 continue; 17601 } 17602 17603 // target, target data 17604 // OpenMP 5.0 [2.12.2, Restrictions, p. 163] 17605 // OpenMP 5.0 [2.12.5, Restrictions, p. 174] 17606 // A map-type in a map clause must be to, from, tofrom or alloc 17607 if ((DKind == OMPD_target_data || 17608 isOpenMPTargetExecutionDirective(DKind)) && 17609 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_from || 17610 MapType == OMPC_MAP_tofrom || MapType == OMPC_MAP_alloc)) { 17611 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 17612 << (IsMapTypeImplicit ? 1 : 0) 17613 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 17614 << getOpenMPDirectiveName(DKind); 17615 continue; 17616 } 17617 17618 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 17619 // A list item cannot appear in both a map clause and a data-sharing 17620 // attribute clause on the same construct 17621 // 17622 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7] 17623 // A list item cannot appear in both a map clause and a data-sharing 17624 // attribute clause on the same construct unless the construct is a 17625 // combined construct. 17626 if (VD && ((SemaRef.LangOpts.OpenMP <= 45 && 17627 isOpenMPTargetExecutionDirective(DKind)) || 17628 DKind == OMPD_target)) { 17629 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false); 17630 if (isOpenMPPrivate(DVar.CKind)) { 17631 SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 17632 << getOpenMPClauseName(DVar.CKind) 17633 << getOpenMPClauseName(OMPC_map) 17634 << getOpenMPDirectiveName(DSAS->getCurrentDirective()); 17635 reportOriginalDsa(SemaRef, DSAS, CurDeclaration, DVar); 17636 continue; 17637 } 17638 } 17639 } 17640 17641 // Try to find the associated user-defined mapper. 17642 ExprResult ER = buildUserDefinedMapperRef( 17643 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId, 17644 Type.getCanonicalType(), UnresolvedMapper); 17645 if (ER.isInvalid()) 17646 continue; 17647 MVLI.UDMapperList.push_back(ER.get()); 17648 17649 // Save the current expression. 17650 MVLI.ProcessedVarList.push_back(RE); 17651 17652 // Store the components in the stack so that they can be used to check 17653 // against other clauses later on. 17654 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents, 17655 /*WhereFoundClauseKind=*/OMPC_map); 17656 17657 // Save the components and declaration to create the clause. For purposes of 17658 // the clause creation, any component list that has has base 'this' uses 17659 // null as base declaration. 17660 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 17661 MVLI.VarComponents.back().append(CurComponents.begin(), 17662 CurComponents.end()); 17663 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr 17664 : CurDeclaration); 17665 } 17666 } 17667 17668 OMPClause *Sema::ActOnOpenMPMapClause( 17669 ArrayRef<OpenMPMapModifierKind> MapTypeModifiers, 17670 ArrayRef<SourceLocation> MapTypeModifiersLoc, 17671 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId, 17672 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, SourceLocation MapLoc, 17673 SourceLocation ColonLoc, ArrayRef<Expr *> VarList, 17674 const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) { 17675 OpenMPMapModifierKind Modifiers[] = { 17676 OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown, 17677 OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown}; 17678 SourceLocation ModifiersLoc[NumberOfOMPMapClauseModifiers]; 17679 17680 // Process map-type-modifiers, flag errors for duplicate modifiers. 17681 unsigned Count = 0; 17682 for (unsigned I = 0, E = MapTypeModifiers.size(); I < E; ++I) { 17683 if (MapTypeModifiers[I] != OMPC_MAP_MODIFIER_unknown && 17684 llvm::find(Modifiers, MapTypeModifiers[I]) != std::end(Modifiers)) { 17685 Diag(MapTypeModifiersLoc[I], diag::err_omp_duplicate_map_type_modifier); 17686 continue; 17687 } 17688 assert(Count < NumberOfOMPMapClauseModifiers && 17689 "Modifiers exceed the allowed number of map type modifiers"); 17690 Modifiers[Count] = MapTypeModifiers[I]; 17691 ModifiersLoc[Count] = MapTypeModifiersLoc[I]; 17692 ++Count; 17693 } 17694 17695 MappableVarListInfo MVLI(VarList); 17696 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, Locs.StartLoc, 17697 MapperIdScopeSpec, MapperId, UnresolvedMappers, 17698 MapType, IsMapTypeImplicit); 17699 17700 // We need to produce a map clause even if we don't have variables so that 17701 // other diagnostics related with non-existing map clauses are accurate. 17702 return OMPMapClause::Create(Context, Locs, MVLI.ProcessedVarList, 17703 MVLI.VarBaseDeclarations, MVLI.VarComponents, 17704 MVLI.UDMapperList, Modifiers, ModifiersLoc, 17705 MapperIdScopeSpec.getWithLocInContext(Context), 17706 MapperId, MapType, IsMapTypeImplicit, MapLoc); 17707 } 17708 17709 QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc, 17710 TypeResult ParsedType) { 17711 assert(ParsedType.isUsable()); 17712 17713 QualType ReductionType = GetTypeFromParser(ParsedType.get()); 17714 if (ReductionType.isNull()) 17715 return QualType(); 17716 17717 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++ 17718 // A type name in a declare reduction directive cannot be a function type, an 17719 // array type, a reference type, or a type qualified with const, volatile or 17720 // restrict. 17721 if (ReductionType.hasQualifiers()) { 17722 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0; 17723 return QualType(); 17724 } 17725 17726 if (ReductionType->isFunctionType()) { 17727 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1; 17728 return QualType(); 17729 } 17730 if (ReductionType->isReferenceType()) { 17731 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2; 17732 return QualType(); 17733 } 17734 if (ReductionType->isArrayType()) { 17735 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3; 17736 return QualType(); 17737 } 17738 return ReductionType; 17739 } 17740 17741 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart( 17742 Scope *S, DeclContext *DC, DeclarationName Name, 17743 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes, 17744 AccessSpecifier AS, Decl *PrevDeclInScope) { 17745 SmallVector<Decl *, 8> Decls; 17746 Decls.reserve(ReductionTypes.size()); 17747 17748 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName, 17749 forRedeclarationInCurContext()); 17750 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions 17751 // A reduction-identifier may not be re-declared in the current scope for the 17752 // same type or for a type that is compatible according to the base language 17753 // rules. 17754 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes; 17755 OMPDeclareReductionDecl *PrevDRD = nullptr; 17756 bool InCompoundScope = true; 17757 if (S != nullptr) { 17758 // Find previous declaration with the same name not referenced in other 17759 // declarations. 17760 FunctionScopeInfo *ParentFn = getEnclosingFunction(); 17761 InCompoundScope = 17762 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty(); 17763 LookupName(Lookup, S); 17764 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false, 17765 /*AllowInlineNamespace=*/false); 17766 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious; 17767 LookupResult::Filter Filter = Lookup.makeFilter(); 17768 while (Filter.hasNext()) { 17769 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next()); 17770 if (InCompoundScope) { 17771 auto I = UsedAsPrevious.find(PrevDecl); 17772 if (I == UsedAsPrevious.end()) 17773 UsedAsPrevious[PrevDecl] = false; 17774 if (OMPDeclareReductionDecl *D = PrevDecl->getPrevDeclInScope()) 17775 UsedAsPrevious[D] = true; 17776 } 17777 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] = 17778 PrevDecl->getLocation(); 17779 } 17780 Filter.done(); 17781 if (InCompoundScope) { 17782 for (const auto &PrevData : UsedAsPrevious) { 17783 if (!PrevData.second) { 17784 PrevDRD = PrevData.first; 17785 break; 17786 } 17787 } 17788 } 17789 } else if (PrevDeclInScope != nullptr) { 17790 auto *PrevDRDInScope = PrevDRD = 17791 cast<OMPDeclareReductionDecl>(PrevDeclInScope); 17792 do { 17793 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] = 17794 PrevDRDInScope->getLocation(); 17795 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope(); 17796 } while (PrevDRDInScope != nullptr); 17797 } 17798 for (const auto &TyData : ReductionTypes) { 17799 const auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType()); 17800 bool Invalid = false; 17801 if (I != PreviousRedeclTypes.end()) { 17802 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition) 17803 << TyData.first; 17804 Diag(I->second, diag::note_previous_definition); 17805 Invalid = true; 17806 } 17807 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second; 17808 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second, 17809 Name, TyData.first, PrevDRD); 17810 DC->addDecl(DRD); 17811 DRD->setAccess(AS); 17812 Decls.push_back(DRD); 17813 if (Invalid) 17814 DRD->setInvalidDecl(); 17815 else 17816 PrevDRD = DRD; 17817 } 17818 17819 return DeclGroupPtrTy::make( 17820 DeclGroupRef::Create(Context, Decls.begin(), Decls.size())); 17821 } 17822 17823 void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) { 17824 auto *DRD = cast<OMPDeclareReductionDecl>(D); 17825 17826 // Enter new function scope. 17827 PushFunctionScope(); 17828 setFunctionHasBranchProtectedScope(); 17829 getCurFunction()->setHasOMPDeclareReductionCombiner(); 17830 17831 if (S != nullptr) 17832 PushDeclContext(S, DRD); 17833 else 17834 CurContext = DRD; 17835 17836 PushExpressionEvaluationContext( 17837 ExpressionEvaluationContext::PotentiallyEvaluated); 17838 17839 QualType ReductionType = DRD->getType(); 17840 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will 17841 // be replaced by '*omp_parm' during codegen. This required because 'omp_in' 17842 // uses semantics of argument handles by value, but it should be passed by 17843 // reference. C lang does not support references, so pass all parameters as 17844 // pointers. 17845 // Create 'T omp_in;' variable. 17846 VarDecl *OmpInParm = 17847 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in"); 17848 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will 17849 // be replaced by '*omp_parm' during codegen. This required because 'omp_out' 17850 // uses semantics of argument handles by value, but it should be passed by 17851 // reference. C lang does not support references, so pass all parameters as 17852 // pointers. 17853 // Create 'T omp_out;' variable. 17854 VarDecl *OmpOutParm = 17855 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out"); 17856 if (S != nullptr) { 17857 PushOnScopeChains(OmpInParm, S); 17858 PushOnScopeChains(OmpOutParm, S); 17859 } else { 17860 DRD->addDecl(OmpInParm); 17861 DRD->addDecl(OmpOutParm); 17862 } 17863 Expr *InE = 17864 ::buildDeclRefExpr(*this, OmpInParm, ReductionType, D->getLocation()); 17865 Expr *OutE = 17866 ::buildDeclRefExpr(*this, OmpOutParm, ReductionType, D->getLocation()); 17867 DRD->setCombinerData(InE, OutE); 17868 } 17869 17870 void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) { 17871 auto *DRD = cast<OMPDeclareReductionDecl>(D); 17872 DiscardCleanupsInEvaluationContext(); 17873 PopExpressionEvaluationContext(); 17874 17875 PopDeclContext(); 17876 PopFunctionScopeInfo(); 17877 17878 if (Combiner != nullptr) 17879 DRD->setCombiner(Combiner); 17880 else 17881 DRD->setInvalidDecl(); 17882 } 17883 17884 VarDecl *Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) { 17885 auto *DRD = cast<OMPDeclareReductionDecl>(D); 17886 17887 // Enter new function scope. 17888 PushFunctionScope(); 17889 setFunctionHasBranchProtectedScope(); 17890 17891 if (S != nullptr) 17892 PushDeclContext(S, DRD); 17893 else 17894 CurContext = DRD; 17895 17896 PushExpressionEvaluationContext( 17897 ExpressionEvaluationContext::PotentiallyEvaluated); 17898 17899 QualType ReductionType = DRD->getType(); 17900 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will 17901 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv' 17902 // uses semantics of argument handles by value, but it should be passed by 17903 // reference. C lang does not support references, so pass all parameters as 17904 // pointers. 17905 // Create 'T omp_priv;' variable. 17906 VarDecl *OmpPrivParm = 17907 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv"); 17908 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will 17909 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig' 17910 // uses semantics of argument handles by value, but it should be passed by 17911 // reference. C lang does not support references, so pass all parameters as 17912 // pointers. 17913 // Create 'T omp_orig;' variable. 17914 VarDecl *OmpOrigParm = 17915 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig"); 17916 if (S != nullptr) { 17917 PushOnScopeChains(OmpPrivParm, S); 17918 PushOnScopeChains(OmpOrigParm, S); 17919 } else { 17920 DRD->addDecl(OmpPrivParm); 17921 DRD->addDecl(OmpOrigParm); 17922 } 17923 Expr *OrigE = 17924 ::buildDeclRefExpr(*this, OmpOrigParm, ReductionType, D->getLocation()); 17925 Expr *PrivE = 17926 ::buildDeclRefExpr(*this, OmpPrivParm, ReductionType, D->getLocation()); 17927 DRD->setInitializerData(OrigE, PrivE); 17928 return OmpPrivParm; 17929 } 17930 17931 void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, Expr *Initializer, 17932 VarDecl *OmpPrivParm) { 17933 auto *DRD = cast<OMPDeclareReductionDecl>(D); 17934 DiscardCleanupsInEvaluationContext(); 17935 PopExpressionEvaluationContext(); 17936 17937 PopDeclContext(); 17938 PopFunctionScopeInfo(); 17939 17940 if (Initializer != nullptr) { 17941 DRD->setInitializer(Initializer, OMPDeclareReductionDecl::CallInit); 17942 } else if (OmpPrivParm->hasInit()) { 17943 DRD->setInitializer(OmpPrivParm->getInit(), 17944 OmpPrivParm->isDirectInit() 17945 ? OMPDeclareReductionDecl::DirectInit 17946 : OMPDeclareReductionDecl::CopyInit); 17947 } else { 17948 DRD->setInvalidDecl(); 17949 } 17950 } 17951 17952 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd( 17953 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) { 17954 for (Decl *D : DeclReductions.get()) { 17955 if (IsValid) { 17956 if (S) 17957 PushOnScopeChains(cast<OMPDeclareReductionDecl>(D), S, 17958 /*AddToContext=*/false); 17959 } else { 17960 D->setInvalidDecl(); 17961 } 17962 } 17963 return DeclReductions; 17964 } 17965 17966 TypeResult Sema::ActOnOpenMPDeclareMapperVarDecl(Scope *S, Declarator &D) { 17967 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); 17968 QualType T = TInfo->getType(); 17969 if (D.isInvalidType()) 17970 return true; 17971 17972 if (getLangOpts().CPlusPlus) { 17973 // Check that there are no default arguments (C++ only). 17974 CheckExtraCXXDefaultArguments(D); 17975 } 17976 17977 return CreateParsedType(T, TInfo); 17978 } 17979 17980 QualType Sema::ActOnOpenMPDeclareMapperType(SourceLocation TyLoc, 17981 TypeResult ParsedType) { 17982 assert(ParsedType.isUsable() && "Expect usable parsed mapper type"); 17983 17984 QualType MapperType = GetTypeFromParser(ParsedType.get()); 17985 assert(!MapperType.isNull() && "Expect valid mapper type"); 17986 17987 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions 17988 // The type must be of struct, union or class type in C and C++ 17989 if (!MapperType->isStructureOrClassType() && !MapperType->isUnionType()) { 17990 Diag(TyLoc, diag::err_omp_mapper_wrong_type); 17991 return QualType(); 17992 } 17993 return MapperType; 17994 } 17995 17996 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareMapperDirective( 17997 Scope *S, DeclContext *DC, DeclarationName Name, QualType MapperType, 17998 SourceLocation StartLoc, DeclarationName VN, AccessSpecifier AS, 17999 Expr *MapperVarRef, ArrayRef<OMPClause *> Clauses, Decl *PrevDeclInScope) { 18000 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPMapperName, 18001 forRedeclarationInCurContext()); 18002 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions 18003 // A mapper-identifier may not be redeclared in the current scope for the 18004 // same type or for a type that is compatible according to the base language 18005 // rules. 18006 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes; 18007 OMPDeclareMapperDecl *PrevDMD = nullptr; 18008 bool InCompoundScope = true; 18009 if (S != nullptr) { 18010 // Find previous declaration with the same name not referenced in other 18011 // declarations. 18012 FunctionScopeInfo *ParentFn = getEnclosingFunction(); 18013 InCompoundScope = 18014 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty(); 18015 LookupName(Lookup, S); 18016 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false, 18017 /*AllowInlineNamespace=*/false); 18018 llvm::DenseMap<OMPDeclareMapperDecl *, bool> UsedAsPrevious; 18019 LookupResult::Filter Filter = Lookup.makeFilter(); 18020 while (Filter.hasNext()) { 18021 auto *PrevDecl = cast<OMPDeclareMapperDecl>(Filter.next()); 18022 if (InCompoundScope) { 18023 auto I = UsedAsPrevious.find(PrevDecl); 18024 if (I == UsedAsPrevious.end()) 18025 UsedAsPrevious[PrevDecl] = false; 18026 if (OMPDeclareMapperDecl *D = PrevDecl->getPrevDeclInScope()) 18027 UsedAsPrevious[D] = true; 18028 } 18029 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] = 18030 PrevDecl->getLocation(); 18031 } 18032 Filter.done(); 18033 if (InCompoundScope) { 18034 for (const auto &PrevData : UsedAsPrevious) { 18035 if (!PrevData.second) { 18036 PrevDMD = PrevData.first; 18037 break; 18038 } 18039 } 18040 } 18041 } else if (PrevDeclInScope) { 18042 auto *PrevDMDInScope = PrevDMD = 18043 cast<OMPDeclareMapperDecl>(PrevDeclInScope); 18044 do { 18045 PreviousRedeclTypes[PrevDMDInScope->getType().getCanonicalType()] = 18046 PrevDMDInScope->getLocation(); 18047 PrevDMDInScope = PrevDMDInScope->getPrevDeclInScope(); 18048 } while (PrevDMDInScope != nullptr); 18049 } 18050 const auto I = PreviousRedeclTypes.find(MapperType.getCanonicalType()); 18051 bool Invalid = false; 18052 if (I != PreviousRedeclTypes.end()) { 18053 Diag(StartLoc, diag::err_omp_declare_mapper_redefinition) 18054 << MapperType << Name; 18055 Diag(I->second, diag::note_previous_definition); 18056 Invalid = true; 18057 } 18058 auto *DMD = OMPDeclareMapperDecl::Create(Context, DC, StartLoc, Name, 18059 MapperType, VN, Clauses, PrevDMD); 18060 if (S) 18061 PushOnScopeChains(DMD, S); 18062 else 18063 DC->addDecl(DMD); 18064 DMD->setAccess(AS); 18065 if (Invalid) 18066 DMD->setInvalidDecl(); 18067 18068 auto *VD = cast<DeclRefExpr>(MapperVarRef)->getDecl(); 18069 VD->setDeclContext(DMD); 18070 VD->setLexicalDeclContext(DMD); 18071 DMD->addDecl(VD); 18072 DMD->setMapperVarRef(MapperVarRef); 18073 18074 return DeclGroupPtrTy::make(DeclGroupRef(DMD)); 18075 } 18076 18077 ExprResult 18078 Sema::ActOnOpenMPDeclareMapperDirectiveVarDecl(Scope *S, QualType MapperType, 18079 SourceLocation StartLoc, 18080 DeclarationName VN) { 18081 TypeSourceInfo *TInfo = 18082 Context.getTrivialTypeSourceInfo(MapperType, StartLoc); 18083 auto *VD = VarDecl::Create(Context, Context.getTranslationUnitDecl(), 18084 StartLoc, StartLoc, VN.getAsIdentifierInfo(), 18085 MapperType, TInfo, SC_None); 18086 if (S) 18087 PushOnScopeChains(VD, S, /*AddToContext=*/false); 18088 Expr *E = buildDeclRefExpr(*this, VD, MapperType, StartLoc); 18089 DSAStack->addDeclareMapperVarRef(E); 18090 return E; 18091 } 18092 18093 bool Sema::isOpenMPDeclareMapperVarDeclAllowed(const VarDecl *VD) const { 18094 assert(LangOpts.OpenMP && "Expected OpenMP mode."); 18095 const Expr *Ref = DSAStack->getDeclareMapperVarRef(); 18096 if (const auto *DRE = cast_or_null<DeclRefExpr>(Ref)) 18097 return VD->getCanonicalDecl() == DRE->getDecl()->getCanonicalDecl(); 18098 return true; 18099 } 18100 18101 const ValueDecl *Sema::getOpenMPDeclareMapperVarName() const { 18102 assert(LangOpts.OpenMP && "Expected OpenMP mode."); 18103 return cast<DeclRefExpr>(DSAStack->getDeclareMapperVarRef())->getDecl(); 18104 } 18105 18106 OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams, 18107 SourceLocation StartLoc, 18108 SourceLocation LParenLoc, 18109 SourceLocation EndLoc) { 18110 Expr *ValExpr = NumTeams; 18111 Stmt *HelperValStmt = nullptr; 18112 18113 // OpenMP [teams Constrcut, Restrictions] 18114 // The num_teams expression must evaluate to a positive integer value. 18115 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams, 18116 /*StrictlyPositive=*/true)) 18117 return nullptr; 18118 18119 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 18120 OpenMPDirectiveKind CaptureRegion = 18121 getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams, LangOpts.OpenMP); 18122 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 18123 ValExpr = MakeFullExpr(ValExpr).get(); 18124 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 18125 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 18126 HelperValStmt = buildPreInits(Context, Captures); 18127 } 18128 18129 return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion, 18130 StartLoc, LParenLoc, EndLoc); 18131 } 18132 18133 OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit, 18134 SourceLocation StartLoc, 18135 SourceLocation LParenLoc, 18136 SourceLocation EndLoc) { 18137 Expr *ValExpr = ThreadLimit; 18138 Stmt *HelperValStmt = nullptr; 18139 18140 // OpenMP [teams Constrcut, Restrictions] 18141 // The thread_limit expression must evaluate to a positive integer value. 18142 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit, 18143 /*StrictlyPositive=*/true)) 18144 return nullptr; 18145 18146 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 18147 OpenMPDirectiveKind CaptureRegion = getOpenMPCaptureRegionForClause( 18148 DKind, OMPC_thread_limit, LangOpts.OpenMP); 18149 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 18150 ValExpr = MakeFullExpr(ValExpr).get(); 18151 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 18152 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 18153 HelperValStmt = buildPreInits(Context, Captures); 18154 } 18155 18156 return new (Context) OMPThreadLimitClause( 18157 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc); 18158 } 18159 18160 OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority, 18161 SourceLocation StartLoc, 18162 SourceLocation LParenLoc, 18163 SourceLocation EndLoc) { 18164 Expr *ValExpr = Priority; 18165 Stmt *HelperValStmt = nullptr; 18166 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 18167 18168 // OpenMP [2.9.1, task Constrcut] 18169 // The priority-value is a non-negative numerical scalar expression. 18170 if (!isNonNegativeIntegerValue( 18171 ValExpr, *this, OMPC_priority, 18172 /*StrictlyPositive=*/false, /*BuildCapture=*/true, 18173 DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt)) 18174 return nullptr; 18175 18176 return new (Context) OMPPriorityClause(ValExpr, HelperValStmt, CaptureRegion, 18177 StartLoc, LParenLoc, EndLoc); 18178 } 18179 18180 OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize, 18181 SourceLocation StartLoc, 18182 SourceLocation LParenLoc, 18183 SourceLocation EndLoc) { 18184 Expr *ValExpr = Grainsize; 18185 Stmt *HelperValStmt = nullptr; 18186 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 18187 18188 // OpenMP [2.9.2, taskloop Constrcut] 18189 // The parameter of the grainsize clause must be a positive integer 18190 // expression. 18191 if (!isNonNegativeIntegerValue( 18192 ValExpr, *this, OMPC_grainsize, 18193 /*StrictlyPositive=*/true, /*BuildCapture=*/true, 18194 DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt)) 18195 return nullptr; 18196 18197 return new (Context) OMPGrainsizeClause(ValExpr, HelperValStmt, CaptureRegion, 18198 StartLoc, LParenLoc, EndLoc); 18199 } 18200 18201 OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks, 18202 SourceLocation StartLoc, 18203 SourceLocation LParenLoc, 18204 SourceLocation EndLoc) { 18205 Expr *ValExpr = NumTasks; 18206 Stmt *HelperValStmt = nullptr; 18207 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 18208 18209 // OpenMP [2.9.2, taskloop Constrcut] 18210 // The parameter of the num_tasks clause must be a positive integer 18211 // expression. 18212 if (!isNonNegativeIntegerValue( 18213 ValExpr, *this, OMPC_num_tasks, 18214 /*StrictlyPositive=*/true, /*BuildCapture=*/true, 18215 DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt)) 18216 return nullptr; 18217 18218 return new (Context) OMPNumTasksClause(ValExpr, HelperValStmt, CaptureRegion, 18219 StartLoc, LParenLoc, EndLoc); 18220 } 18221 18222 OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc, 18223 SourceLocation LParenLoc, 18224 SourceLocation EndLoc) { 18225 // OpenMP [2.13.2, critical construct, Description] 18226 // ... where hint-expression is an integer constant expression that evaluates 18227 // to a valid lock hint. 18228 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint); 18229 if (HintExpr.isInvalid()) 18230 return nullptr; 18231 return new (Context) 18232 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc); 18233 } 18234 18235 /// Tries to find omp_event_handle_t type. 18236 static bool findOMPEventHandleT(Sema &S, SourceLocation Loc, 18237 DSAStackTy *Stack) { 18238 QualType OMPEventHandleT = Stack->getOMPEventHandleT(); 18239 if (!OMPEventHandleT.isNull()) 18240 return true; 18241 IdentifierInfo *II = &S.PP.getIdentifierTable().get("omp_event_handle_t"); 18242 ParsedType PT = S.getTypeName(*II, Loc, S.getCurScope()); 18243 if (!PT.getAsOpaquePtr() || PT.get().isNull()) { 18244 S.Diag(Loc, diag::err_omp_implied_type_not_found) << "omp_event_handle_t"; 18245 return false; 18246 } 18247 Stack->setOMPEventHandleT(PT.get()); 18248 return true; 18249 } 18250 18251 OMPClause *Sema::ActOnOpenMPDetachClause(Expr *Evt, SourceLocation StartLoc, 18252 SourceLocation LParenLoc, 18253 SourceLocation EndLoc) { 18254 if (!Evt->isValueDependent() && !Evt->isTypeDependent() && 18255 !Evt->isInstantiationDependent() && 18256 !Evt->containsUnexpandedParameterPack()) { 18257 if (!findOMPEventHandleT(*this, Evt->getExprLoc(), DSAStack)) 18258 return nullptr; 18259 // OpenMP 5.0, 2.10.1 task Construct. 18260 // event-handle is a variable of the omp_event_handle_t type. 18261 auto *Ref = dyn_cast<DeclRefExpr>(Evt->IgnoreParenImpCasts()); 18262 if (!Ref) { 18263 Diag(Evt->getExprLoc(), diag::err_omp_var_expected) 18264 << "omp_event_handle_t" << 0 << Evt->getSourceRange(); 18265 return nullptr; 18266 } 18267 auto *VD = dyn_cast_or_null<VarDecl>(Ref->getDecl()); 18268 if (!VD) { 18269 Diag(Evt->getExprLoc(), diag::err_omp_var_expected) 18270 << "omp_event_handle_t" << 0 << Evt->getSourceRange(); 18271 return nullptr; 18272 } 18273 if (!Context.hasSameUnqualifiedType(DSAStack->getOMPEventHandleT(), 18274 VD->getType()) || 18275 VD->getType().isConstant(Context)) { 18276 Diag(Evt->getExprLoc(), diag::err_omp_var_expected) 18277 << "omp_event_handle_t" << 1 << VD->getType() 18278 << Evt->getSourceRange(); 18279 return nullptr; 18280 } 18281 // OpenMP 5.0, 2.10.1 task Construct 18282 // [detach clause]... The event-handle will be considered as if it was 18283 // specified on a firstprivate clause. 18284 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, /*FromParent=*/false); 18285 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate && 18286 DVar.RefExpr) { 18287 Diag(Evt->getExprLoc(), diag::err_omp_wrong_dsa) 18288 << getOpenMPClauseName(DVar.CKind) 18289 << getOpenMPClauseName(OMPC_firstprivate); 18290 reportOriginalDsa(*this, DSAStack, VD, DVar); 18291 return nullptr; 18292 } 18293 } 18294 18295 return new (Context) OMPDetachClause(Evt, StartLoc, LParenLoc, EndLoc); 18296 } 18297 18298 OMPClause *Sema::ActOnOpenMPDistScheduleClause( 18299 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, 18300 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc, 18301 SourceLocation EndLoc) { 18302 if (Kind == OMPC_DIST_SCHEDULE_unknown) { 18303 std::string Values; 18304 Values += "'"; 18305 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0); 18306 Values += "'"; 18307 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 18308 << Values << getOpenMPClauseName(OMPC_dist_schedule); 18309 return nullptr; 18310 } 18311 Expr *ValExpr = ChunkSize; 18312 Stmt *HelperValStmt = nullptr; 18313 if (ChunkSize) { 18314 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && 18315 !ChunkSize->isInstantiationDependent() && 18316 !ChunkSize->containsUnexpandedParameterPack()) { 18317 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc(); 18318 ExprResult Val = 18319 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); 18320 if (Val.isInvalid()) 18321 return nullptr; 18322 18323 ValExpr = Val.get(); 18324 18325 // OpenMP [2.7.1, Restrictions] 18326 // chunk_size must be a loop invariant integer expression with a positive 18327 // value. 18328 if (Optional<llvm::APSInt> Result = 18329 ValExpr->getIntegerConstantExpr(Context)) { 18330 if (Result->isSigned() && !Result->isStrictlyPositive()) { 18331 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) 18332 << "dist_schedule" << ChunkSize->getSourceRange(); 18333 return nullptr; 18334 } 18335 } else if (getOpenMPCaptureRegionForClause( 18336 DSAStack->getCurrentDirective(), OMPC_dist_schedule, 18337 LangOpts.OpenMP) != OMPD_unknown && 18338 !CurContext->isDependentContext()) { 18339 ValExpr = MakeFullExpr(ValExpr).get(); 18340 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 18341 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 18342 HelperValStmt = buildPreInits(Context, Captures); 18343 } 18344 } 18345 } 18346 18347 return new (Context) 18348 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, 18349 Kind, ValExpr, HelperValStmt); 18350 } 18351 18352 OMPClause *Sema::ActOnOpenMPDefaultmapClause( 18353 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind, 18354 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc, 18355 SourceLocation KindLoc, SourceLocation EndLoc) { 18356 if (getLangOpts().OpenMP < 50) { 18357 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || 18358 Kind != OMPC_DEFAULTMAP_scalar) { 18359 std::string Value; 18360 SourceLocation Loc; 18361 Value += "'"; 18362 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) { 18363 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap, 18364 OMPC_DEFAULTMAP_MODIFIER_tofrom); 18365 Loc = MLoc; 18366 } else { 18367 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap, 18368 OMPC_DEFAULTMAP_scalar); 18369 Loc = KindLoc; 18370 } 18371 Value += "'"; 18372 Diag(Loc, diag::err_omp_unexpected_clause_value) 18373 << Value << getOpenMPClauseName(OMPC_defaultmap); 18374 return nullptr; 18375 } 18376 } else { 18377 bool isDefaultmapModifier = (M != OMPC_DEFAULTMAP_MODIFIER_unknown); 18378 bool isDefaultmapKind = (Kind != OMPC_DEFAULTMAP_unknown) || 18379 (LangOpts.OpenMP >= 50 && KindLoc.isInvalid()); 18380 if (!isDefaultmapKind || !isDefaultmapModifier) { 18381 std::string ModifierValue = "'alloc', 'from', 'to', 'tofrom', " 18382 "'firstprivate', 'none', 'default'"; 18383 std::string KindValue = "'scalar', 'aggregate', 'pointer'"; 18384 if (!isDefaultmapKind && isDefaultmapModifier) { 18385 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 18386 << KindValue << getOpenMPClauseName(OMPC_defaultmap); 18387 } else if (isDefaultmapKind && !isDefaultmapModifier) { 18388 Diag(MLoc, diag::err_omp_unexpected_clause_value) 18389 << ModifierValue << getOpenMPClauseName(OMPC_defaultmap); 18390 } else { 18391 Diag(MLoc, diag::err_omp_unexpected_clause_value) 18392 << ModifierValue << getOpenMPClauseName(OMPC_defaultmap); 18393 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 18394 << KindValue << getOpenMPClauseName(OMPC_defaultmap); 18395 } 18396 return nullptr; 18397 } 18398 18399 // OpenMP [5.0, 2.12.5, Restrictions, p. 174] 18400 // At most one defaultmap clause for each category can appear on the 18401 // directive. 18402 if (DSAStack->checkDefaultmapCategory(Kind)) { 18403 Diag(StartLoc, diag::err_omp_one_defaultmap_each_category); 18404 return nullptr; 18405 } 18406 } 18407 if (Kind == OMPC_DEFAULTMAP_unknown) { 18408 // Variable category is not specified - mark all categories. 18409 DSAStack->setDefaultDMAAttr(M, OMPC_DEFAULTMAP_aggregate, StartLoc); 18410 DSAStack->setDefaultDMAAttr(M, OMPC_DEFAULTMAP_scalar, StartLoc); 18411 DSAStack->setDefaultDMAAttr(M, OMPC_DEFAULTMAP_pointer, StartLoc); 18412 } else { 18413 DSAStack->setDefaultDMAAttr(M, Kind, StartLoc); 18414 } 18415 18416 return new (Context) 18417 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M); 18418 } 18419 18420 bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) { 18421 DeclContext *CurLexicalContext = getCurLexicalContext(); 18422 if (!CurLexicalContext->isFileContext() && 18423 !CurLexicalContext->isExternCContext() && 18424 !CurLexicalContext->isExternCXXContext() && 18425 !isa<CXXRecordDecl>(CurLexicalContext) && 18426 !isa<ClassTemplateDecl>(CurLexicalContext) && 18427 !isa<ClassTemplatePartialSpecializationDecl>(CurLexicalContext) && 18428 !isa<ClassTemplateSpecializationDecl>(CurLexicalContext)) { 18429 Diag(Loc, diag::err_omp_region_not_file_context); 18430 return false; 18431 } 18432 ++DeclareTargetNestingLevel; 18433 return true; 18434 } 18435 18436 void Sema::ActOnFinishOpenMPDeclareTargetDirective() { 18437 assert(DeclareTargetNestingLevel > 0 && 18438 "Unexpected ActOnFinishOpenMPDeclareTargetDirective"); 18439 --DeclareTargetNestingLevel; 18440 } 18441 18442 NamedDecl * 18443 Sema::lookupOpenMPDeclareTargetName(Scope *CurScope, CXXScopeSpec &ScopeSpec, 18444 const DeclarationNameInfo &Id, 18445 NamedDeclSetType &SameDirectiveDecls) { 18446 LookupResult Lookup(*this, Id, LookupOrdinaryName); 18447 LookupParsedName(Lookup, CurScope, &ScopeSpec, true); 18448 18449 if (Lookup.isAmbiguous()) 18450 return nullptr; 18451 Lookup.suppressDiagnostics(); 18452 18453 if (!Lookup.isSingleResult()) { 18454 VarOrFuncDeclFilterCCC CCC(*this); 18455 if (TypoCorrection Corrected = 18456 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, CCC, 18457 CTK_ErrorRecovery)) { 18458 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest) 18459 << Id.getName()); 18460 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl()); 18461 return nullptr; 18462 } 18463 18464 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName(); 18465 return nullptr; 18466 } 18467 18468 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>(); 18469 if (!isa<VarDecl>(ND) && !isa<FunctionDecl>(ND) && 18470 !isa<FunctionTemplateDecl>(ND)) { 18471 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName(); 18472 return nullptr; 18473 } 18474 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl()))) 18475 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName(); 18476 return ND; 18477 } 18478 18479 void Sema::ActOnOpenMPDeclareTargetName( 18480 NamedDecl *ND, SourceLocation Loc, OMPDeclareTargetDeclAttr::MapTypeTy MT, 18481 OMPDeclareTargetDeclAttr::DevTypeTy DT) { 18482 assert((isa<VarDecl>(ND) || isa<FunctionDecl>(ND) || 18483 isa<FunctionTemplateDecl>(ND)) && 18484 "Expected variable, function or function template."); 18485 18486 // Diagnose marking after use as it may lead to incorrect diagnosis and 18487 // codegen. 18488 if (LangOpts.OpenMP >= 50 && 18489 (ND->isUsed(/*CheckUsedAttr=*/false) || ND->isReferenced())) 18490 Diag(Loc, diag::warn_omp_declare_target_after_first_use); 18491 18492 Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy = 18493 OMPDeclareTargetDeclAttr::getDeviceType(cast<ValueDecl>(ND)); 18494 if (DevTy.hasValue() && *DevTy != DT) { 18495 Diag(Loc, diag::err_omp_device_type_mismatch) 18496 << OMPDeclareTargetDeclAttr::ConvertDevTypeTyToStr(DT) 18497 << OMPDeclareTargetDeclAttr::ConvertDevTypeTyToStr(*DevTy); 18498 return; 18499 } 18500 Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 18501 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(cast<ValueDecl>(ND)); 18502 if (!Res) { 18503 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT, DT, 18504 SourceRange(Loc, Loc)); 18505 ND->addAttr(A); 18506 if (ASTMutationListener *ML = Context.getASTMutationListener()) 18507 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A); 18508 checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Loc); 18509 } else if (*Res != MT) { 18510 Diag(Loc, diag::err_omp_declare_target_to_and_link) << ND; 18511 } 18512 } 18513 18514 static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR, 18515 Sema &SemaRef, Decl *D) { 18516 if (!D || !isa<VarDecl>(D)) 18517 return; 18518 auto *VD = cast<VarDecl>(D); 18519 Optional<OMPDeclareTargetDeclAttr::MapTypeTy> MapTy = 18520 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); 18521 if (SemaRef.LangOpts.OpenMP >= 50 && 18522 (SemaRef.getCurLambda(/*IgnoreNonLambdaCapturingScope=*/true) || 18523 SemaRef.getCurBlock() || SemaRef.getCurCapturedRegion()) && 18524 VD->hasGlobalStorage()) { 18525 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> MapTy = 18526 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); 18527 if (!MapTy || *MapTy != OMPDeclareTargetDeclAttr::MT_To) { 18528 // OpenMP 5.0, 2.12.7 declare target Directive, Restrictions 18529 // If a lambda declaration and definition appears between a 18530 // declare target directive and the matching end declare target 18531 // directive, all variables that are captured by the lambda 18532 // expression must also appear in a to clause. 18533 SemaRef.Diag(VD->getLocation(), 18534 diag::err_omp_lambda_capture_in_declare_target_not_to); 18535 SemaRef.Diag(SL, diag::note_var_explicitly_captured_here) 18536 << VD << 0 << SR; 18537 return; 18538 } 18539 } 18540 if (MapTy.hasValue()) 18541 return; 18542 SemaRef.Diag(VD->getLocation(), diag::warn_omp_not_in_target_context); 18543 SemaRef.Diag(SL, diag::note_used_here) << SR; 18544 } 18545 18546 static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR, 18547 Sema &SemaRef, DSAStackTy *Stack, 18548 ValueDecl *VD) { 18549 return OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD) || 18550 checkTypeMappable(SL, SR, SemaRef, Stack, VD->getType(), 18551 /*FullCheck=*/false); 18552 } 18553 18554 void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D, 18555 SourceLocation IdLoc) { 18556 if (!D || D->isInvalidDecl()) 18557 return; 18558 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange(); 18559 SourceLocation SL = E ? E->getBeginLoc() : D->getLocation(); 18560 if (auto *VD = dyn_cast<VarDecl>(D)) { 18561 // Only global variables can be marked as declare target. 18562 if (!VD->isFileVarDecl() && !VD->isStaticLocal() && 18563 !VD->isStaticDataMember()) 18564 return; 18565 // 2.10.6: threadprivate variable cannot appear in a declare target 18566 // directive. 18567 if (DSAStack->isThreadPrivate(VD)) { 18568 Diag(SL, diag::err_omp_threadprivate_in_target); 18569 reportOriginalDsa(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false)); 18570 return; 18571 } 18572 } 18573 if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(D)) 18574 D = FTD->getTemplatedDecl(); 18575 if (auto *FD = dyn_cast<FunctionDecl>(D)) { 18576 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 18577 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(FD); 18578 if (IdLoc.isValid() && Res && *Res == OMPDeclareTargetDeclAttr::MT_Link) { 18579 Diag(IdLoc, diag::err_omp_function_in_link_clause); 18580 Diag(FD->getLocation(), diag::note_defined_here) << FD; 18581 return; 18582 } 18583 } 18584 if (auto *VD = dyn_cast<ValueDecl>(D)) { 18585 // Problem if any with var declared with incomplete type will be reported 18586 // as normal, so no need to check it here. 18587 if ((E || !VD->getType()->isIncompleteType()) && 18588 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) 18589 return; 18590 if (!E && !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) { 18591 // Checking declaration inside declare target region. 18592 if (isa<VarDecl>(D) || isa<FunctionDecl>(D) || 18593 isa<FunctionTemplateDecl>(D)) { 18594 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit( 18595 Context, OMPDeclareTargetDeclAttr::MT_To, 18596 OMPDeclareTargetDeclAttr::DT_Any, SourceRange(IdLoc, IdLoc)); 18597 D->addAttr(A); 18598 if (ASTMutationListener *ML = Context.getASTMutationListener()) 18599 ML->DeclarationMarkedOpenMPDeclareTarget(D, A); 18600 } 18601 return; 18602 } 18603 } 18604 if (!E) 18605 return; 18606 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D); 18607 } 18608 18609 OMPClause *Sema::ActOnOpenMPToClause( 18610 ArrayRef<OpenMPMotionModifierKind> MotionModifiers, 18611 ArrayRef<SourceLocation> MotionModifiersLoc, 18612 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId, 18613 SourceLocation ColonLoc, ArrayRef<Expr *> VarList, 18614 const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) { 18615 OpenMPMotionModifierKind Modifiers[] = {OMPC_MOTION_MODIFIER_unknown, 18616 OMPC_MOTION_MODIFIER_unknown}; 18617 SourceLocation ModifiersLoc[NumberOfOMPMotionModifiers]; 18618 18619 // Process motion-modifiers, flag errors for duplicate modifiers. 18620 unsigned Count = 0; 18621 for (unsigned I = 0, E = MotionModifiers.size(); I < E; ++I) { 18622 if (MotionModifiers[I] != OMPC_MOTION_MODIFIER_unknown && 18623 llvm::find(Modifiers, MotionModifiers[I]) != std::end(Modifiers)) { 18624 Diag(MotionModifiersLoc[I], diag::err_omp_duplicate_motion_modifier); 18625 continue; 18626 } 18627 assert(Count < NumberOfOMPMotionModifiers && 18628 "Modifiers exceed the allowed number of motion modifiers"); 18629 Modifiers[Count] = MotionModifiers[I]; 18630 ModifiersLoc[Count] = MotionModifiersLoc[I]; 18631 ++Count; 18632 } 18633 18634 MappableVarListInfo MVLI(VarList); 18635 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, Locs.StartLoc, 18636 MapperIdScopeSpec, MapperId, UnresolvedMappers); 18637 if (MVLI.ProcessedVarList.empty()) 18638 return nullptr; 18639 18640 return OMPToClause::Create( 18641 Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, 18642 MVLI.VarComponents, MVLI.UDMapperList, Modifiers, ModifiersLoc, 18643 MapperIdScopeSpec.getWithLocInContext(Context), MapperId); 18644 } 18645 18646 OMPClause *Sema::ActOnOpenMPFromClause( 18647 ArrayRef<OpenMPMotionModifierKind> MotionModifiers, 18648 ArrayRef<SourceLocation> MotionModifiersLoc, 18649 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId, 18650 SourceLocation ColonLoc, ArrayRef<Expr *> VarList, 18651 const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) { 18652 OpenMPMotionModifierKind Modifiers[] = {OMPC_MOTION_MODIFIER_unknown, 18653 OMPC_MOTION_MODIFIER_unknown}; 18654 SourceLocation ModifiersLoc[NumberOfOMPMotionModifiers]; 18655 18656 // Process motion-modifiers, flag errors for duplicate modifiers. 18657 unsigned Count = 0; 18658 for (unsigned I = 0, E = MotionModifiers.size(); I < E; ++I) { 18659 if (MotionModifiers[I] != OMPC_MOTION_MODIFIER_unknown && 18660 llvm::find(Modifiers, MotionModifiers[I]) != std::end(Modifiers)) { 18661 Diag(MotionModifiersLoc[I], diag::err_omp_duplicate_motion_modifier); 18662 continue; 18663 } 18664 assert(Count < NumberOfOMPMotionModifiers && 18665 "Modifiers exceed the allowed number of motion modifiers"); 18666 Modifiers[Count] = MotionModifiers[I]; 18667 ModifiersLoc[Count] = MotionModifiersLoc[I]; 18668 ++Count; 18669 } 18670 18671 MappableVarListInfo MVLI(VarList); 18672 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, Locs.StartLoc, 18673 MapperIdScopeSpec, MapperId, UnresolvedMappers); 18674 if (MVLI.ProcessedVarList.empty()) 18675 return nullptr; 18676 18677 return OMPFromClause::Create( 18678 Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, 18679 MVLI.VarComponents, MVLI.UDMapperList, Modifiers, ModifiersLoc, 18680 MapperIdScopeSpec.getWithLocInContext(Context), MapperId); 18681 } 18682 18683 OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList, 18684 const OMPVarListLocTy &Locs) { 18685 MappableVarListInfo MVLI(VarList); 18686 SmallVector<Expr *, 8> PrivateCopies; 18687 SmallVector<Expr *, 8> Inits; 18688 18689 for (Expr *RefExpr : VarList) { 18690 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause."); 18691 SourceLocation ELoc; 18692 SourceRange ERange; 18693 Expr *SimpleRefExpr = RefExpr; 18694 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 18695 if (Res.second) { 18696 // It will be analyzed later. 18697 MVLI.ProcessedVarList.push_back(RefExpr); 18698 PrivateCopies.push_back(nullptr); 18699 Inits.push_back(nullptr); 18700 } 18701 ValueDecl *D = Res.first; 18702 if (!D) 18703 continue; 18704 18705 QualType Type = D->getType(); 18706 Type = Type.getNonReferenceType().getUnqualifiedType(); 18707 18708 auto *VD = dyn_cast<VarDecl>(D); 18709 18710 // Item should be a pointer or reference to pointer. 18711 if (!Type->isPointerType()) { 18712 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer) 18713 << 0 << RefExpr->getSourceRange(); 18714 continue; 18715 } 18716 18717 // Build the private variable and the expression that refers to it. 18718 auto VDPrivate = 18719 buildVarDecl(*this, ELoc, Type, D->getName(), 18720 D->hasAttrs() ? &D->getAttrs() : nullptr, 18721 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 18722 if (VDPrivate->isInvalidDecl()) 18723 continue; 18724 18725 CurContext->addDecl(VDPrivate); 18726 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 18727 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc); 18728 18729 // Add temporary variable to initialize the private copy of the pointer. 18730 VarDecl *VDInit = 18731 buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp"); 18732 DeclRefExpr *VDInitRefExpr = buildDeclRefExpr( 18733 *this, VDInit, RefExpr->getType(), RefExpr->getExprLoc()); 18734 AddInitializerToDecl(VDPrivate, 18735 DefaultLvalueConversion(VDInitRefExpr).get(), 18736 /*DirectInit=*/false); 18737 18738 // If required, build a capture to implement the privatization initialized 18739 // with the current list item value. 18740 DeclRefExpr *Ref = nullptr; 18741 if (!VD) 18742 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 18743 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref); 18744 PrivateCopies.push_back(VDPrivateRefExpr); 18745 Inits.push_back(VDInitRefExpr); 18746 18747 // We need to add a data sharing attribute for this variable to make sure it 18748 // is correctly captured. A variable that shows up in a use_device_ptr has 18749 // similar properties of a first private variable. 18750 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 18751 18752 // Create a mappable component for the list item. List items in this clause 18753 // only need a component. 18754 MVLI.VarBaseDeclarations.push_back(D); 18755 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 18756 MVLI.VarComponents.back().push_back( 18757 OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D)); 18758 } 18759 18760 if (MVLI.ProcessedVarList.empty()) 18761 return nullptr; 18762 18763 return OMPUseDevicePtrClause::Create( 18764 Context, Locs, MVLI.ProcessedVarList, PrivateCopies, Inits, 18765 MVLI.VarBaseDeclarations, MVLI.VarComponents); 18766 } 18767 18768 OMPClause *Sema::ActOnOpenMPUseDeviceAddrClause(ArrayRef<Expr *> VarList, 18769 const OMPVarListLocTy &Locs) { 18770 MappableVarListInfo MVLI(VarList); 18771 18772 for (Expr *RefExpr : VarList) { 18773 assert(RefExpr && "NULL expr in OpenMP use_device_addr clause."); 18774 SourceLocation ELoc; 18775 SourceRange ERange; 18776 Expr *SimpleRefExpr = RefExpr; 18777 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, 18778 /*AllowArraySection=*/true); 18779 if (Res.second) { 18780 // It will be analyzed later. 18781 MVLI.ProcessedVarList.push_back(RefExpr); 18782 } 18783 ValueDecl *D = Res.first; 18784 if (!D) 18785 continue; 18786 auto *VD = dyn_cast<VarDecl>(D); 18787 18788 // If required, build a capture to implement the privatization initialized 18789 // with the current list item value. 18790 DeclRefExpr *Ref = nullptr; 18791 if (!VD) 18792 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 18793 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref); 18794 18795 // We need to add a data sharing attribute for this variable to make sure it 18796 // is correctly captured. A variable that shows up in a use_device_addr has 18797 // similar properties of a first private variable. 18798 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 18799 18800 // Create a mappable component for the list item. List items in this clause 18801 // only need a component. 18802 MVLI.VarBaseDeclarations.push_back(D); 18803 MVLI.VarComponents.emplace_back(); 18804 Expr *Component = SimpleRefExpr; 18805 if (VD && (isa<OMPArraySectionExpr>(RefExpr->IgnoreParenImpCasts()) || 18806 isa<ArraySubscriptExpr>(RefExpr->IgnoreParenImpCasts()))) 18807 Component = DefaultFunctionArrayLvalueConversion(SimpleRefExpr).get(); 18808 MVLI.VarComponents.back().push_back( 18809 OMPClauseMappableExprCommon::MappableComponent(Component, D)); 18810 } 18811 18812 if (MVLI.ProcessedVarList.empty()) 18813 return nullptr; 18814 18815 return OMPUseDeviceAddrClause::Create(Context, Locs, MVLI.ProcessedVarList, 18816 MVLI.VarBaseDeclarations, 18817 MVLI.VarComponents); 18818 } 18819 18820 OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList, 18821 const OMPVarListLocTy &Locs) { 18822 MappableVarListInfo MVLI(VarList); 18823 for (Expr *RefExpr : VarList) { 18824 assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause."); 18825 SourceLocation ELoc; 18826 SourceRange ERange; 18827 Expr *SimpleRefExpr = RefExpr; 18828 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 18829 if (Res.second) { 18830 // It will be analyzed later. 18831 MVLI.ProcessedVarList.push_back(RefExpr); 18832 } 18833 ValueDecl *D = Res.first; 18834 if (!D) 18835 continue; 18836 18837 QualType Type = D->getType(); 18838 // item should be a pointer or array or reference to pointer or array 18839 if (!Type.getNonReferenceType()->isPointerType() && 18840 !Type.getNonReferenceType()->isArrayType()) { 18841 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr) 18842 << 0 << RefExpr->getSourceRange(); 18843 continue; 18844 } 18845 18846 // Check if the declaration in the clause does not show up in any data 18847 // sharing attribute. 18848 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 18849 if (isOpenMPPrivate(DVar.CKind)) { 18850 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 18851 << getOpenMPClauseName(DVar.CKind) 18852 << getOpenMPClauseName(OMPC_is_device_ptr) 18853 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 18854 reportOriginalDsa(*this, DSAStack, D, DVar); 18855 continue; 18856 } 18857 18858 const Expr *ConflictExpr; 18859 if (DSAStack->checkMappableExprComponentListsForDecl( 18860 D, /*CurrentRegionOnly=*/true, 18861 [&ConflictExpr]( 18862 OMPClauseMappableExprCommon::MappableExprComponentListRef R, 18863 OpenMPClauseKind) -> bool { 18864 ConflictExpr = R.front().getAssociatedExpression(); 18865 return true; 18866 })) { 18867 Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange(); 18868 Diag(ConflictExpr->getExprLoc(), diag::note_used_here) 18869 << ConflictExpr->getSourceRange(); 18870 continue; 18871 } 18872 18873 // Store the components in the stack so that they can be used to check 18874 // against other clauses later on. 18875 OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D); 18876 DSAStack->addMappableExpressionComponents( 18877 D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr); 18878 18879 // Record the expression we've just processed. 18880 MVLI.ProcessedVarList.push_back(SimpleRefExpr); 18881 18882 // Create a mappable component for the list item. List items in this clause 18883 // only need a component. We use a null declaration to signal fields in 18884 // 'this'. 18885 assert((isa<DeclRefExpr>(SimpleRefExpr) || 18886 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) && 18887 "Unexpected device pointer expression!"); 18888 MVLI.VarBaseDeclarations.push_back( 18889 isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr); 18890 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 18891 MVLI.VarComponents.back().push_back(MC); 18892 } 18893 18894 if (MVLI.ProcessedVarList.empty()) 18895 return nullptr; 18896 18897 return OMPIsDevicePtrClause::Create(Context, Locs, MVLI.ProcessedVarList, 18898 MVLI.VarBaseDeclarations, 18899 MVLI.VarComponents); 18900 } 18901 18902 OMPClause *Sema::ActOnOpenMPAllocateClause( 18903 Expr *Allocator, ArrayRef<Expr *> VarList, SourceLocation StartLoc, 18904 SourceLocation ColonLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 18905 if (Allocator) { 18906 // OpenMP [2.11.4 allocate Clause, Description] 18907 // allocator is an expression of omp_allocator_handle_t type. 18908 if (!findOMPAllocatorHandleT(*this, Allocator->getExprLoc(), DSAStack)) 18909 return nullptr; 18910 18911 ExprResult AllocatorRes = DefaultLvalueConversion(Allocator); 18912 if (AllocatorRes.isInvalid()) 18913 return nullptr; 18914 AllocatorRes = PerformImplicitConversion(AllocatorRes.get(), 18915 DSAStack->getOMPAllocatorHandleT(), 18916 Sema::AA_Initializing, 18917 /*AllowExplicit=*/true); 18918 if (AllocatorRes.isInvalid()) 18919 return nullptr; 18920 Allocator = AllocatorRes.get(); 18921 } else { 18922 // OpenMP 5.0, 2.11.4 allocate Clause, Restrictions. 18923 // allocate clauses that appear on a target construct or on constructs in a 18924 // target region must specify an allocator expression unless a requires 18925 // directive with the dynamic_allocators clause is present in the same 18926 // compilation unit. 18927 if (LangOpts.OpenMPIsDevice && 18928 !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>()) 18929 targetDiag(StartLoc, diag::err_expected_allocator_expression); 18930 } 18931 // Analyze and build list of variables. 18932 SmallVector<Expr *, 8> Vars; 18933 for (Expr *RefExpr : VarList) { 18934 assert(RefExpr && "NULL expr in OpenMP private clause."); 18935 SourceLocation ELoc; 18936 SourceRange ERange; 18937 Expr *SimpleRefExpr = RefExpr; 18938 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 18939 if (Res.second) { 18940 // It will be analyzed later. 18941 Vars.push_back(RefExpr); 18942 } 18943 ValueDecl *D = Res.first; 18944 if (!D) 18945 continue; 18946 18947 auto *VD = dyn_cast<VarDecl>(D); 18948 DeclRefExpr *Ref = nullptr; 18949 if (!VD && !CurContext->isDependentContext()) 18950 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 18951 Vars.push_back((VD || CurContext->isDependentContext()) 18952 ? RefExpr->IgnoreParens() 18953 : Ref); 18954 } 18955 18956 if (Vars.empty()) 18957 return nullptr; 18958 18959 if (Allocator) 18960 DSAStack->addInnerAllocatorExpr(Allocator); 18961 return OMPAllocateClause::Create(Context, StartLoc, LParenLoc, Allocator, 18962 ColonLoc, EndLoc, Vars); 18963 } 18964 18965 OMPClause *Sema::ActOnOpenMPNontemporalClause(ArrayRef<Expr *> VarList, 18966 SourceLocation StartLoc, 18967 SourceLocation LParenLoc, 18968 SourceLocation EndLoc) { 18969 SmallVector<Expr *, 8> Vars; 18970 for (Expr *RefExpr : VarList) { 18971 assert(RefExpr && "NULL expr in OpenMP nontemporal clause."); 18972 SourceLocation ELoc; 18973 SourceRange ERange; 18974 Expr *SimpleRefExpr = RefExpr; 18975 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 18976 if (Res.second) 18977 // It will be analyzed later. 18978 Vars.push_back(RefExpr); 18979 ValueDecl *D = Res.first; 18980 if (!D) 18981 continue; 18982 18983 // OpenMP 5.0, 2.9.3.1 simd Construct, Restrictions. 18984 // A list-item cannot appear in more than one nontemporal clause. 18985 if (const Expr *PrevRef = 18986 DSAStack->addUniqueNontemporal(D, SimpleRefExpr)) { 18987 Diag(ELoc, diag::err_omp_used_in_clause_twice) 18988 << 0 << getOpenMPClauseName(OMPC_nontemporal) << ERange; 18989 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa) 18990 << getOpenMPClauseName(OMPC_nontemporal); 18991 continue; 18992 } 18993 18994 Vars.push_back(RefExpr); 18995 } 18996 18997 if (Vars.empty()) 18998 return nullptr; 18999 19000 return OMPNontemporalClause::Create(Context, StartLoc, LParenLoc, EndLoc, 19001 Vars); 19002 } 19003 19004 OMPClause *Sema::ActOnOpenMPInclusiveClause(ArrayRef<Expr *> VarList, 19005 SourceLocation StartLoc, 19006 SourceLocation LParenLoc, 19007 SourceLocation EndLoc) { 19008 SmallVector<Expr *, 8> Vars; 19009 for (Expr *RefExpr : VarList) { 19010 assert(RefExpr && "NULL expr in OpenMP nontemporal clause."); 19011 SourceLocation ELoc; 19012 SourceRange ERange; 19013 Expr *SimpleRefExpr = RefExpr; 19014 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, 19015 /*AllowArraySection=*/true); 19016 if (Res.second) 19017 // It will be analyzed later. 19018 Vars.push_back(RefExpr); 19019 ValueDecl *D = Res.first; 19020 if (!D) 19021 continue; 19022 19023 const DSAStackTy::DSAVarData DVar = 19024 DSAStack->getTopDSA(D, /*FromParent=*/true); 19025 // OpenMP 5.0, 2.9.6, scan Directive, Restrictions. 19026 // A list item that appears in the inclusive or exclusive clause must appear 19027 // in a reduction clause with the inscan modifier on the enclosing 19028 // worksharing-loop, worksharing-loop SIMD, or simd construct. 19029 if (DVar.CKind != OMPC_reduction || 19030 DVar.Modifier != OMPC_REDUCTION_inscan) 19031 Diag(ELoc, diag::err_omp_inclusive_exclusive_not_reduction) 19032 << RefExpr->getSourceRange(); 19033 19034 if (DSAStack->getParentDirective() != OMPD_unknown) 19035 DSAStack->markDeclAsUsedInScanDirective(D); 19036 Vars.push_back(RefExpr); 19037 } 19038 19039 if (Vars.empty()) 19040 return nullptr; 19041 19042 return OMPInclusiveClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 19043 } 19044 19045 OMPClause *Sema::ActOnOpenMPExclusiveClause(ArrayRef<Expr *> VarList, 19046 SourceLocation StartLoc, 19047 SourceLocation LParenLoc, 19048 SourceLocation EndLoc) { 19049 SmallVector<Expr *, 8> Vars; 19050 for (Expr *RefExpr : VarList) { 19051 assert(RefExpr && "NULL expr in OpenMP nontemporal clause."); 19052 SourceLocation ELoc; 19053 SourceRange ERange; 19054 Expr *SimpleRefExpr = RefExpr; 19055 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, 19056 /*AllowArraySection=*/true); 19057 if (Res.second) 19058 // It will be analyzed later. 19059 Vars.push_back(RefExpr); 19060 ValueDecl *D = Res.first; 19061 if (!D) 19062 continue; 19063 19064 OpenMPDirectiveKind ParentDirective = DSAStack->getParentDirective(); 19065 DSAStackTy::DSAVarData DVar; 19066 if (ParentDirective != OMPD_unknown) 19067 DVar = DSAStack->getTopDSA(D, /*FromParent=*/true); 19068 // OpenMP 5.0, 2.9.6, scan Directive, Restrictions. 19069 // A list item that appears in the inclusive or exclusive clause must appear 19070 // in a reduction clause with the inscan modifier on the enclosing 19071 // worksharing-loop, worksharing-loop SIMD, or simd construct. 19072 if (ParentDirective == OMPD_unknown || DVar.CKind != OMPC_reduction || 19073 DVar.Modifier != OMPC_REDUCTION_inscan) { 19074 Diag(ELoc, diag::err_omp_inclusive_exclusive_not_reduction) 19075 << RefExpr->getSourceRange(); 19076 } else { 19077 DSAStack->markDeclAsUsedInScanDirective(D); 19078 } 19079 Vars.push_back(RefExpr); 19080 } 19081 19082 if (Vars.empty()) 19083 return nullptr; 19084 19085 return OMPExclusiveClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 19086 } 19087 19088 /// Tries to find omp_alloctrait_t type. 19089 static bool findOMPAlloctraitT(Sema &S, SourceLocation Loc, DSAStackTy *Stack) { 19090 QualType OMPAlloctraitT = Stack->getOMPAlloctraitT(); 19091 if (!OMPAlloctraitT.isNull()) 19092 return true; 19093 IdentifierInfo &II = S.PP.getIdentifierTable().get("omp_alloctrait_t"); 19094 ParsedType PT = S.getTypeName(II, Loc, S.getCurScope()); 19095 if (!PT.getAsOpaquePtr() || PT.get().isNull()) { 19096 S.Diag(Loc, diag::err_omp_implied_type_not_found) << "omp_alloctrait_t"; 19097 return false; 19098 } 19099 Stack->setOMPAlloctraitT(PT.get()); 19100 return true; 19101 } 19102 19103 OMPClause *Sema::ActOnOpenMPUsesAllocatorClause( 19104 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc, 19105 ArrayRef<UsesAllocatorsData> Data) { 19106 // OpenMP [2.12.5, target Construct] 19107 // allocator is an identifier of omp_allocator_handle_t type. 19108 if (!findOMPAllocatorHandleT(*this, StartLoc, DSAStack)) 19109 return nullptr; 19110 // OpenMP [2.12.5, target Construct] 19111 // allocator-traits-array is an identifier of const omp_alloctrait_t * type. 19112 if (llvm::any_of( 19113 Data, 19114 [](const UsesAllocatorsData &D) { return D.AllocatorTraits; }) && 19115 !findOMPAlloctraitT(*this, StartLoc, DSAStack)) 19116 return nullptr; 19117 llvm::SmallPtrSet<CanonicalDeclPtr<Decl>, 4> PredefinedAllocators; 19118 for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) { 19119 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I); 19120 StringRef Allocator = 19121 OMPAllocateDeclAttr::ConvertAllocatorTypeTyToStr(AllocatorKind); 19122 DeclarationName AllocatorName = &Context.Idents.get(Allocator); 19123 PredefinedAllocators.insert(LookupSingleName( 19124 TUScope, AllocatorName, StartLoc, Sema::LookupAnyName)); 19125 } 19126 19127 SmallVector<OMPUsesAllocatorsClause::Data, 4> NewData; 19128 for (const UsesAllocatorsData &D : Data) { 19129 Expr *AllocatorExpr = nullptr; 19130 // Check allocator expression. 19131 if (D.Allocator->isTypeDependent()) { 19132 AllocatorExpr = D.Allocator; 19133 } else { 19134 // Traits were specified - need to assign new allocator to the specified 19135 // allocator, so it must be an lvalue. 19136 AllocatorExpr = D.Allocator->IgnoreParenImpCasts(); 19137 auto *DRE = dyn_cast<DeclRefExpr>(AllocatorExpr); 19138 bool IsPredefinedAllocator = false; 19139 if (DRE) 19140 IsPredefinedAllocator = PredefinedAllocators.count(DRE->getDecl()); 19141 if (!DRE || 19142 !(Context.hasSameUnqualifiedType( 19143 AllocatorExpr->getType(), DSAStack->getOMPAllocatorHandleT()) || 19144 Context.typesAreCompatible(AllocatorExpr->getType(), 19145 DSAStack->getOMPAllocatorHandleT(), 19146 /*CompareUnqualified=*/true)) || 19147 (!IsPredefinedAllocator && 19148 (AllocatorExpr->getType().isConstant(Context) || 19149 !AllocatorExpr->isLValue()))) { 19150 Diag(D.Allocator->getExprLoc(), diag::err_omp_var_expected) 19151 << "omp_allocator_handle_t" << (DRE ? 1 : 0) 19152 << AllocatorExpr->getType() << D.Allocator->getSourceRange(); 19153 continue; 19154 } 19155 // OpenMP [2.12.5, target Construct] 19156 // Predefined allocators appearing in a uses_allocators clause cannot have 19157 // traits specified. 19158 if (IsPredefinedAllocator && D.AllocatorTraits) { 19159 Diag(D.AllocatorTraits->getExprLoc(), 19160 diag::err_omp_predefined_allocator_with_traits) 19161 << D.AllocatorTraits->getSourceRange(); 19162 Diag(D.Allocator->getExprLoc(), diag::note_omp_predefined_allocator) 19163 << cast<NamedDecl>(DRE->getDecl())->getName() 19164 << D.Allocator->getSourceRange(); 19165 continue; 19166 } 19167 // OpenMP [2.12.5, target Construct] 19168 // Non-predefined allocators appearing in a uses_allocators clause must 19169 // have traits specified. 19170 if (!IsPredefinedAllocator && !D.AllocatorTraits) { 19171 Diag(D.Allocator->getExprLoc(), 19172 diag::err_omp_nonpredefined_allocator_without_traits); 19173 continue; 19174 } 19175 // No allocator traits - just convert it to rvalue. 19176 if (!D.AllocatorTraits) 19177 AllocatorExpr = DefaultLvalueConversion(AllocatorExpr).get(); 19178 DSAStack->addUsesAllocatorsDecl( 19179 DRE->getDecl(), 19180 IsPredefinedAllocator 19181 ? DSAStackTy::UsesAllocatorsDeclKind::PredefinedAllocator 19182 : DSAStackTy::UsesAllocatorsDeclKind::UserDefinedAllocator); 19183 } 19184 Expr *AllocatorTraitsExpr = nullptr; 19185 if (D.AllocatorTraits) { 19186 if (D.AllocatorTraits->isTypeDependent()) { 19187 AllocatorTraitsExpr = D.AllocatorTraits; 19188 } else { 19189 // OpenMP [2.12.5, target Construct] 19190 // Arrays that contain allocator traits that appear in a uses_allocators 19191 // clause must be constant arrays, have constant values and be defined 19192 // in the same scope as the construct in which the clause appears. 19193 AllocatorTraitsExpr = D.AllocatorTraits->IgnoreParenImpCasts(); 19194 // Check that traits expr is a constant array. 19195 QualType TraitTy; 19196 if (const ArrayType *Ty = 19197 AllocatorTraitsExpr->getType()->getAsArrayTypeUnsafe()) 19198 if (const auto *ConstArrayTy = dyn_cast<ConstantArrayType>(Ty)) 19199 TraitTy = ConstArrayTy->getElementType(); 19200 if (TraitTy.isNull() || 19201 !(Context.hasSameUnqualifiedType(TraitTy, 19202 DSAStack->getOMPAlloctraitT()) || 19203 Context.typesAreCompatible(TraitTy, DSAStack->getOMPAlloctraitT(), 19204 /*CompareUnqualified=*/true))) { 19205 Diag(D.AllocatorTraits->getExprLoc(), 19206 diag::err_omp_expected_array_alloctraits) 19207 << AllocatorTraitsExpr->getType(); 19208 continue; 19209 } 19210 // Do not map by default allocator traits if it is a standalone 19211 // variable. 19212 if (auto *DRE = dyn_cast<DeclRefExpr>(AllocatorTraitsExpr)) 19213 DSAStack->addUsesAllocatorsDecl( 19214 DRE->getDecl(), 19215 DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait); 19216 } 19217 } 19218 OMPUsesAllocatorsClause::Data &NewD = NewData.emplace_back(); 19219 NewD.Allocator = AllocatorExpr; 19220 NewD.AllocatorTraits = AllocatorTraitsExpr; 19221 NewD.LParenLoc = D.LParenLoc; 19222 NewD.RParenLoc = D.RParenLoc; 19223 } 19224 return OMPUsesAllocatorsClause::Create(Context, StartLoc, LParenLoc, EndLoc, 19225 NewData); 19226 } 19227 19228 OMPClause *Sema::ActOnOpenMPAffinityClause( 19229 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc, 19230 SourceLocation EndLoc, Expr *Modifier, ArrayRef<Expr *> Locators) { 19231 SmallVector<Expr *, 8> Vars; 19232 for (Expr *RefExpr : Locators) { 19233 assert(RefExpr && "NULL expr in OpenMP shared clause."); 19234 if (isa<DependentScopeDeclRefExpr>(RefExpr) || RefExpr->isTypeDependent()) { 19235 // It will be analyzed later. 19236 Vars.push_back(RefExpr); 19237 continue; 19238 } 19239 19240 SourceLocation ELoc = RefExpr->getExprLoc(); 19241 Expr *SimpleExpr = RefExpr->IgnoreParenImpCasts(); 19242 19243 if (!SimpleExpr->isLValue()) { 19244 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 19245 << 1 << 0 << RefExpr->getSourceRange(); 19246 continue; 19247 } 19248 19249 ExprResult Res; 19250 { 19251 Sema::TentativeAnalysisScope Trap(*this); 19252 Res = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, SimpleExpr); 19253 } 19254 if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr) && 19255 !isa<OMPArrayShapingExpr>(SimpleExpr)) { 19256 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 19257 << 1 << 0 << RefExpr->getSourceRange(); 19258 continue; 19259 } 19260 Vars.push_back(SimpleExpr); 19261 } 19262 19263 return OMPAffinityClause::Create(Context, StartLoc, LParenLoc, ColonLoc, 19264 EndLoc, Modifier, Vars); 19265 } 19266