1 //===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 /// \file 9 /// This file implements semantic analysis for OpenMP directives and 10 /// clauses. 11 /// 12 //===----------------------------------------------------------------------===// 13 14 #include "TreeTransform.h" 15 #include "clang/AST/ASTContext.h" 16 #include "clang/AST/ASTMutationListener.h" 17 #include "clang/AST/CXXInheritance.h" 18 #include "clang/AST/Decl.h" 19 #include "clang/AST/DeclCXX.h" 20 #include "clang/AST/DeclOpenMP.h" 21 #include "clang/AST/OpenMPClause.h" 22 #include "clang/AST/StmtCXX.h" 23 #include "clang/AST/StmtOpenMP.h" 24 #include "clang/AST/StmtVisitor.h" 25 #include "clang/AST/TypeOrdering.h" 26 #include "clang/Basic/DiagnosticSema.h" 27 #include "clang/Basic/OpenMPKinds.h" 28 #include "clang/Basic/PartialDiagnostic.h" 29 #include "clang/Basic/TargetInfo.h" 30 #include "clang/Sema/Initialization.h" 31 #include "clang/Sema/Lookup.h" 32 #include "clang/Sema/Scope.h" 33 #include "clang/Sema/ScopeInfo.h" 34 #include "clang/Sema/SemaInternal.h" 35 #include "llvm/ADT/IndexedMap.h" 36 #include "llvm/ADT/PointerEmbeddedInt.h" 37 #include "llvm/ADT/STLExtras.h" 38 #include "llvm/Frontend/OpenMP/OMPConstants.h" 39 #include <set> 40 41 using namespace clang; 42 using namespace llvm::omp; 43 44 //===----------------------------------------------------------------------===// 45 // Stack of data-sharing attributes for variables 46 //===----------------------------------------------------------------------===// 47 48 static const Expr *checkMapClauseExpressionBase( 49 Sema &SemaRef, Expr *E, 50 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents, 51 OpenMPClauseKind CKind, bool NoDiagnose); 52 53 namespace { 54 /// Default data sharing attributes, which can be applied to directive. 55 enum DefaultDataSharingAttributes { 56 DSA_unspecified = 0, /// Data sharing attribute not specified. 57 DSA_none = 1 << 0, /// Default data sharing attribute 'none'. 58 DSA_shared = 1 << 1, /// Default data sharing attribute 'shared'. 59 DSA_firstprivate = 1 << 2, /// Default data sharing attribute 'firstprivate'. 60 }; 61 62 /// Stack for tracking declarations used in OpenMP directives and 63 /// clauses and their data-sharing attributes. 64 class DSAStackTy { 65 public: 66 struct DSAVarData { 67 OpenMPDirectiveKind DKind = OMPD_unknown; 68 OpenMPClauseKind CKind = OMPC_unknown; 69 unsigned Modifier = 0; 70 const Expr *RefExpr = nullptr; 71 DeclRefExpr *PrivateCopy = nullptr; 72 SourceLocation ImplicitDSALoc; 73 bool AppliedToPointee = false; 74 DSAVarData() = default; 75 DSAVarData(OpenMPDirectiveKind DKind, OpenMPClauseKind CKind, 76 const Expr *RefExpr, DeclRefExpr *PrivateCopy, 77 SourceLocation ImplicitDSALoc, unsigned Modifier, 78 bool AppliedToPointee) 79 : DKind(DKind), CKind(CKind), Modifier(Modifier), RefExpr(RefExpr), 80 PrivateCopy(PrivateCopy), ImplicitDSALoc(ImplicitDSALoc), 81 AppliedToPointee(AppliedToPointee) {} 82 }; 83 using OperatorOffsetTy = 84 llvm::SmallVector<std::pair<Expr *, OverloadedOperatorKind>, 4>; 85 using DoacrossDependMapTy = 86 llvm::DenseMap<OMPDependClause *, OperatorOffsetTy>; 87 /// Kind of the declaration used in the uses_allocators clauses. 88 enum class UsesAllocatorsDeclKind { 89 /// Predefined allocator 90 PredefinedAllocator, 91 /// User-defined allocator 92 UserDefinedAllocator, 93 /// The declaration that represent allocator trait 94 AllocatorTrait, 95 }; 96 97 private: 98 struct DSAInfo { 99 OpenMPClauseKind Attributes = OMPC_unknown; 100 unsigned Modifier = 0; 101 /// Pointer to a reference expression and a flag which shows that the 102 /// variable is marked as lastprivate(true) or not (false). 103 llvm::PointerIntPair<const Expr *, 1, bool> RefExpr; 104 DeclRefExpr *PrivateCopy = nullptr; 105 /// true if the attribute is applied to the pointee, not the variable 106 /// itself. 107 bool AppliedToPointee = false; 108 }; 109 using DeclSAMapTy = llvm::SmallDenseMap<const ValueDecl *, DSAInfo, 8>; 110 using UsedRefMapTy = llvm::SmallDenseMap<const ValueDecl *, const Expr *, 8>; 111 using LCDeclInfo = std::pair<unsigned, VarDecl *>; 112 using LoopControlVariablesMapTy = 113 llvm::SmallDenseMap<const ValueDecl *, LCDeclInfo, 8>; 114 /// Struct that associates a component with the clause kind where they are 115 /// found. 116 struct MappedExprComponentTy { 117 OMPClauseMappableExprCommon::MappableExprComponentLists Components; 118 OpenMPClauseKind Kind = OMPC_unknown; 119 }; 120 using MappedExprComponentsTy = 121 llvm::DenseMap<const ValueDecl *, MappedExprComponentTy>; 122 using CriticalsWithHintsTy = 123 llvm::StringMap<std::pair<const OMPCriticalDirective *, llvm::APSInt>>; 124 struct ReductionData { 125 using BOKPtrType = llvm::PointerEmbeddedInt<BinaryOperatorKind, 16>; 126 SourceRange ReductionRange; 127 llvm::PointerUnion<const Expr *, BOKPtrType> ReductionOp; 128 ReductionData() = default; 129 void set(BinaryOperatorKind BO, SourceRange RR) { 130 ReductionRange = RR; 131 ReductionOp = BO; 132 } 133 void set(const Expr *RefExpr, SourceRange RR) { 134 ReductionRange = RR; 135 ReductionOp = RefExpr; 136 } 137 }; 138 using DeclReductionMapTy = 139 llvm::SmallDenseMap<const ValueDecl *, ReductionData, 4>; 140 struct DefaultmapInfo { 141 OpenMPDefaultmapClauseModifier ImplicitBehavior = 142 OMPC_DEFAULTMAP_MODIFIER_unknown; 143 SourceLocation SLoc; 144 DefaultmapInfo() = default; 145 DefaultmapInfo(OpenMPDefaultmapClauseModifier M, SourceLocation Loc) 146 : ImplicitBehavior(M), SLoc(Loc) {} 147 }; 148 149 struct SharingMapTy { 150 DeclSAMapTy SharingMap; 151 DeclReductionMapTy ReductionMap; 152 UsedRefMapTy AlignedMap; 153 UsedRefMapTy NontemporalMap; 154 MappedExprComponentsTy MappedExprComponents; 155 LoopControlVariablesMapTy LCVMap; 156 DefaultDataSharingAttributes DefaultAttr = DSA_unspecified; 157 SourceLocation DefaultAttrLoc; 158 DefaultmapInfo DefaultmapMap[OMPC_DEFAULTMAP_unknown]; 159 OpenMPDirectiveKind Directive = OMPD_unknown; 160 DeclarationNameInfo DirectiveName; 161 Scope *CurScope = nullptr; 162 DeclContext *Context = nullptr; 163 SourceLocation ConstructLoc; 164 /// Set of 'depend' clauses with 'sink|source' dependence kind. Required to 165 /// get the data (loop counters etc.) about enclosing loop-based construct. 166 /// This data is required during codegen. 167 DoacrossDependMapTy DoacrossDepends; 168 /// First argument (Expr *) contains optional argument of the 169 /// 'ordered' clause, the second one is true if the regions has 'ordered' 170 /// clause, false otherwise. 171 llvm::Optional<std::pair<const Expr *, OMPOrderedClause *>> OrderedRegion; 172 unsigned AssociatedLoops = 1; 173 bool HasMutipleLoops = false; 174 const Decl *PossiblyLoopCounter = nullptr; 175 bool NowaitRegion = false; 176 bool CancelRegion = false; 177 bool LoopStart = false; 178 bool BodyComplete = false; 179 SourceLocation PrevScanLocation; 180 SourceLocation PrevOrderedLocation; 181 SourceLocation InnerTeamsRegionLoc; 182 /// Reference to the taskgroup task_reduction reference expression. 183 Expr *TaskgroupReductionRef = nullptr; 184 llvm::DenseSet<QualType> MappedClassesQualTypes; 185 SmallVector<Expr *, 4> InnerUsedAllocators; 186 llvm::DenseSet<CanonicalDeclPtr<Decl>> ImplicitTaskFirstprivates; 187 /// List of globals marked as declare target link in this target region 188 /// (isOpenMPTargetExecutionDirective(Directive) == true). 189 llvm::SmallVector<DeclRefExpr *, 4> DeclareTargetLinkVarDecls; 190 /// List of decls used in inclusive/exclusive clauses of the scan directive. 191 llvm::DenseSet<CanonicalDeclPtr<Decl>> UsedInScanDirective; 192 llvm::DenseMap<CanonicalDeclPtr<const Decl>, UsesAllocatorsDeclKind> 193 UsesAllocatorsDecls; 194 Expr *DeclareMapperVar = nullptr; 195 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name, 196 Scope *CurScope, SourceLocation Loc) 197 : Directive(DKind), DirectiveName(Name), CurScope(CurScope), 198 ConstructLoc(Loc) {} 199 SharingMapTy() = default; 200 }; 201 202 using StackTy = SmallVector<SharingMapTy, 4>; 203 204 /// Stack of used declaration and their data-sharing attributes. 205 DeclSAMapTy Threadprivates; 206 const FunctionScopeInfo *CurrentNonCapturingFunctionScope = nullptr; 207 SmallVector<std::pair<StackTy, const FunctionScopeInfo *>, 4> Stack; 208 /// true, if check for DSA must be from parent directive, false, if 209 /// from current directive. 210 OpenMPClauseKind ClauseKindMode = OMPC_unknown; 211 Sema &SemaRef; 212 bool ForceCapturing = false; 213 /// true if all the variables in the target executable directives must be 214 /// captured by reference. 215 bool ForceCaptureByReferenceInTargetExecutable = false; 216 CriticalsWithHintsTy Criticals; 217 unsigned IgnoredStackElements = 0; 218 219 /// Iterators over the stack iterate in order from innermost to outermost 220 /// directive. 221 using const_iterator = StackTy::const_reverse_iterator; 222 const_iterator begin() const { 223 return Stack.empty() ? const_iterator() 224 : Stack.back().first.rbegin() + IgnoredStackElements; 225 } 226 const_iterator end() const { 227 return Stack.empty() ? const_iterator() : Stack.back().first.rend(); 228 } 229 using iterator = StackTy::reverse_iterator; 230 iterator begin() { 231 return Stack.empty() ? iterator() 232 : Stack.back().first.rbegin() + IgnoredStackElements; 233 } 234 iterator end() { 235 return Stack.empty() ? iterator() : Stack.back().first.rend(); 236 } 237 238 // Convenience operations to get at the elements of the stack. 239 240 bool isStackEmpty() const { 241 return Stack.empty() || 242 Stack.back().second != CurrentNonCapturingFunctionScope || 243 Stack.back().first.size() <= IgnoredStackElements; 244 } 245 size_t getStackSize() const { 246 return isStackEmpty() ? 0 247 : Stack.back().first.size() - IgnoredStackElements; 248 } 249 250 SharingMapTy *getTopOfStackOrNull() { 251 size_t Size = getStackSize(); 252 if (Size == 0) 253 return nullptr; 254 return &Stack.back().first[Size - 1]; 255 } 256 const SharingMapTy *getTopOfStackOrNull() const { 257 return const_cast<DSAStackTy&>(*this).getTopOfStackOrNull(); 258 } 259 SharingMapTy &getTopOfStack() { 260 assert(!isStackEmpty() && "no current directive"); 261 return *getTopOfStackOrNull(); 262 } 263 const SharingMapTy &getTopOfStack() const { 264 return const_cast<DSAStackTy&>(*this).getTopOfStack(); 265 } 266 267 SharingMapTy *getSecondOnStackOrNull() { 268 size_t Size = getStackSize(); 269 if (Size <= 1) 270 return nullptr; 271 return &Stack.back().first[Size - 2]; 272 } 273 const SharingMapTy *getSecondOnStackOrNull() const { 274 return const_cast<DSAStackTy&>(*this).getSecondOnStackOrNull(); 275 } 276 277 /// Get the stack element at a certain level (previously returned by 278 /// \c getNestingLevel). 279 /// 280 /// Note that nesting levels count from outermost to innermost, and this is 281 /// the reverse of our iteration order where new inner levels are pushed at 282 /// the front of the stack. 283 SharingMapTy &getStackElemAtLevel(unsigned Level) { 284 assert(Level < getStackSize() && "no such stack element"); 285 return Stack.back().first[Level]; 286 } 287 const SharingMapTy &getStackElemAtLevel(unsigned Level) const { 288 return const_cast<DSAStackTy&>(*this).getStackElemAtLevel(Level); 289 } 290 291 DSAVarData getDSA(const_iterator &Iter, ValueDecl *D) const; 292 293 /// Checks if the variable is a local for OpenMP region. 294 bool isOpenMPLocal(VarDecl *D, const_iterator Iter) const; 295 296 /// Vector of previously declared requires directives 297 SmallVector<const OMPRequiresDecl *, 2> RequiresDecls; 298 /// omp_allocator_handle_t type. 299 QualType OMPAllocatorHandleT; 300 /// omp_depend_t type. 301 QualType OMPDependT; 302 /// omp_event_handle_t type. 303 QualType OMPEventHandleT; 304 /// omp_alloctrait_t type. 305 QualType OMPAlloctraitT; 306 /// Expression for the predefined allocators. 307 Expr *OMPPredefinedAllocators[OMPAllocateDeclAttr::OMPUserDefinedMemAlloc] = { 308 nullptr}; 309 /// Vector of previously encountered target directives 310 SmallVector<SourceLocation, 2> TargetLocations; 311 SourceLocation AtomicLocation; 312 313 public: 314 explicit DSAStackTy(Sema &S) : SemaRef(S) {} 315 316 /// Sets omp_allocator_handle_t type. 317 void setOMPAllocatorHandleT(QualType Ty) { OMPAllocatorHandleT = Ty; } 318 /// Gets omp_allocator_handle_t type. 319 QualType getOMPAllocatorHandleT() const { return OMPAllocatorHandleT; } 320 /// Sets omp_alloctrait_t type. 321 void setOMPAlloctraitT(QualType Ty) { OMPAlloctraitT = Ty; } 322 /// Gets omp_alloctrait_t type. 323 QualType getOMPAlloctraitT() const { return OMPAlloctraitT; } 324 /// Sets the given default allocator. 325 void setAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, 326 Expr *Allocator) { 327 OMPPredefinedAllocators[AllocatorKind] = Allocator; 328 } 329 /// Returns the specified default allocator. 330 Expr *getAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind) const { 331 return OMPPredefinedAllocators[AllocatorKind]; 332 } 333 /// Sets omp_depend_t type. 334 void setOMPDependT(QualType Ty) { OMPDependT = Ty; } 335 /// Gets omp_depend_t type. 336 QualType getOMPDependT() const { return OMPDependT; } 337 338 /// Sets omp_event_handle_t type. 339 void setOMPEventHandleT(QualType Ty) { OMPEventHandleT = Ty; } 340 /// Gets omp_event_handle_t type. 341 QualType getOMPEventHandleT() const { return OMPEventHandleT; } 342 343 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; } 344 OpenMPClauseKind getClauseParsingMode() const { 345 assert(isClauseParsingMode() && "Must be in clause parsing mode."); 346 return ClauseKindMode; 347 } 348 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; } 349 350 bool isBodyComplete() const { 351 const SharingMapTy *Top = getTopOfStackOrNull(); 352 return Top && Top->BodyComplete; 353 } 354 void setBodyComplete() { 355 getTopOfStack().BodyComplete = true; 356 } 357 358 bool isForceVarCapturing() const { return ForceCapturing; } 359 void setForceVarCapturing(bool V) { ForceCapturing = V; } 360 361 void setForceCaptureByReferenceInTargetExecutable(bool V) { 362 ForceCaptureByReferenceInTargetExecutable = V; 363 } 364 bool isForceCaptureByReferenceInTargetExecutable() const { 365 return ForceCaptureByReferenceInTargetExecutable; 366 } 367 368 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName, 369 Scope *CurScope, SourceLocation Loc) { 370 assert(!IgnoredStackElements && 371 "cannot change stack while ignoring elements"); 372 if (Stack.empty() || 373 Stack.back().second != CurrentNonCapturingFunctionScope) 374 Stack.emplace_back(StackTy(), CurrentNonCapturingFunctionScope); 375 Stack.back().first.emplace_back(DKind, DirName, CurScope, Loc); 376 Stack.back().first.back().DefaultAttrLoc = Loc; 377 } 378 379 void pop() { 380 assert(!IgnoredStackElements && 381 "cannot change stack while ignoring elements"); 382 assert(!Stack.back().first.empty() && 383 "Data-sharing attributes stack is empty!"); 384 Stack.back().first.pop_back(); 385 } 386 387 /// RAII object to temporarily leave the scope of a directive when we want to 388 /// logically operate in its parent. 389 class ParentDirectiveScope { 390 DSAStackTy &Self; 391 bool Active; 392 public: 393 ParentDirectiveScope(DSAStackTy &Self, bool Activate) 394 : Self(Self), Active(false) { 395 if (Activate) 396 enable(); 397 } 398 ~ParentDirectiveScope() { disable(); } 399 void disable() { 400 if (Active) { 401 --Self.IgnoredStackElements; 402 Active = false; 403 } 404 } 405 void enable() { 406 if (!Active) { 407 ++Self.IgnoredStackElements; 408 Active = true; 409 } 410 } 411 }; 412 413 /// Marks that we're started loop parsing. 414 void loopInit() { 415 assert(isOpenMPLoopDirective(getCurrentDirective()) && 416 "Expected loop-based directive."); 417 getTopOfStack().LoopStart = true; 418 } 419 /// Start capturing of the variables in the loop context. 420 void loopStart() { 421 assert(isOpenMPLoopDirective(getCurrentDirective()) && 422 "Expected loop-based directive."); 423 getTopOfStack().LoopStart = false; 424 } 425 /// true, if variables are captured, false otherwise. 426 bool isLoopStarted() const { 427 assert(isOpenMPLoopDirective(getCurrentDirective()) && 428 "Expected loop-based directive."); 429 return !getTopOfStack().LoopStart; 430 } 431 /// Marks (or clears) declaration as possibly loop counter. 432 void resetPossibleLoopCounter(const Decl *D = nullptr) { 433 getTopOfStack().PossiblyLoopCounter = 434 D ? D->getCanonicalDecl() : D; 435 } 436 /// Gets the possible loop counter decl. 437 const Decl *getPossiblyLoopCunter() const { 438 return getTopOfStack().PossiblyLoopCounter; 439 } 440 /// Start new OpenMP region stack in new non-capturing function. 441 void pushFunction() { 442 assert(!IgnoredStackElements && 443 "cannot change stack while ignoring elements"); 444 const FunctionScopeInfo *CurFnScope = SemaRef.getCurFunction(); 445 assert(!isa<CapturingScopeInfo>(CurFnScope)); 446 CurrentNonCapturingFunctionScope = CurFnScope; 447 } 448 /// Pop region stack for non-capturing function. 449 void popFunction(const FunctionScopeInfo *OldFSI) { 450 assert(!IgnoredStackElements && 451 "cannot change stack while ignoring elements"); 452 if (!Stack.empty() && Stack.back().second == OldFSI) { 453 assert(Stack.back().first.empty()); 454 Stack.pop_back(); 455 } 456 CurrentNonCapturingFunctionScope = nullptr; 457 for (const FunctionScopeInfo *FSI : llvm::reverse(SemaRef.FunctionScopes)) { 458 if (!isa<CapturingScopeInfo>(FSI)) { 459 CurrentNonCapturingFunctionScope = FSI; 460 break; 461 } 462 } 463 } 464 465 void addCriticalWithHint(const OMPCriticalDirective *D, llvm::APSInt Hint) { 466 Criticals.try_emplace(D->getDirectiveName().getAsString(), D, Hint); 467 } 468 const std::pair<const OMPCriticalDirective *, llvm::APSInt> 469 getCriticalWithHint(const DeclarationNameInfo &Name) const { 470 auto I = Criticals.find(Name.getAsString()); 471 if (I != Criticals.end()) 472 return I->second; 473 return std::make_pair(nullptr, llvm::APSInt()); 474 } 475 /// If 'aligned' declaration for given variable \a D was not seen yet, 476 /// add it and return NULL; otherwise return previous occurrence's expression 477 /// for diagnostics. 478 const Expr *addUniqueAligned(const ValueDecl *D, const Expr *NewDE); 479 /// If 'nontemporal' declaration for given variable \a D was not seen yet, 480 /// add it and return NULL; otherwise return previous occurrence's expression 481 /// for diagnostics. 482 const Expr *addUniqueNontemporal(const ValueDecl *D, const Expr *NewDE); 483 484 /// Register specified variable as loop control variable. 485 void addLoopControlVariable(const ValueDecl *D, VarDecl *Capture); 486 /// Check if the specified variable is a loop control variable for 487 /// current region. 488 /// \return The index of the loop control variable in the list of associated 489 /// for-loops (from outer to inner). 490 const LCDeclInfo isLoopControlVariable(const ValueDecl *D) const; 491 /// Check if the specified variable is a loop control variable for 492 /// parent region. 493 /// \return The index of the loop control variable in the list of associated 494 /// for-loops (from outer to inner). 495 const LCDeclInfo isParentLoopControlVariable(const ValueDecl *D) const; 496 /// Check if the specified variable is a loop control variable for 497 /// current region. 498 /// \return The index of the loop control variable in the list of associated 499 /// for-loops (from outer to inner). 500 const LCDeclInfo isLoopControlVariable(const ValueDecl *D, 501 unsigned Level) const; 502 /// Get the loop control variable for the I-th loop (or nullptr) in 503 /// parent directive. 504 const ValueDecl *getParentLoopControlVariable(unsigned I) const; 505 506 /// Marks the specified decl \p D as used in scan directive. 507 void markDeclAsUsedInScanDirective(ValueDecl *D) { 508 if (SharingMapTy *Stack = getSecondOnStackOrNull()) 509 Stack->UsedInScanDirective.insert(D); 510 } 511 512 /// Checks if the specified declaration was used in the inner scan directive. 513 bool isUsedInScanDirective(ValueDecl *D) const { 514 if (const SharingMapTy *Stack = getTopOfStackOrNull()) 515 return Stack->UsedInScanDirective.count(D) > 0; 516 return false; 517 } 518 519 /// Adds explicit data sharing attribute to the specified declaration. 520 void addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A, 521 DeclRefExpr *PrivateCopy = nullptr, unsigned Modifier = 0, 522 bool AppliedToPointee = false); 523 524 /// Adds additional information for the reduction items with the reduction id 525 /// represented as an operator. 526 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 527 BinaryOperatorKind BOK); 528 /// Adds additional information for the reduction items with the reduction id 529 /// represented as reduction identifier. 530 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 531 const Expr *ReductionRef); 532 /// Returns the location and reduction operation from the innermost parent 533 /// region for the given \p D. 534 const DSAVarData 535 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR, 536 BinaryOperatorKind &BOK, 537 Expr *&TaskgroupDescriptor) const; 538 /// Returns the location and reduction operation from the innermost parent 539 /// region for the given \p D. 540 const DSAVarData 541 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR, 542 const Expr *&ReductionRef, 543 Expr *&TaskgroupDescriptor) const; 544 /// Return reduction reference expression for the current taskgroup or 545 /// parallel/worksharing directives with task reductions. 546 Expr *getTaskgroupReductionRef() const { 547 assert((getTopOfStack().Directive == OMPD_taskgroup || 548 ((isOpenMPParallelDirective(getTopOfStack().Directive) || 549 isOpenMPWorksharingDirective(getTopOfStack().Directive)) && 550 !isOpenMPSimdDirective(getTopOfStack().Directive))) && 551 "taskgroup reference expression requested for non taskgroup or " 552 "parallel/worksharing directive."); 553 return getTopOfStack().TaskgroupReductionRef; 554 } 555 /// Checks if the given \p VD declaration is actually a taskgroup reduction 556 /// descriptor variable at the \p Level of OpenMP regions. 557 bool isTaskgroupReductionRef(const ValueDecl *VD, unsigned Level) const { 558 return getStackElemAtLevel(Level).TaskgroupReductionRef && 559 cast<DeclRefExpr>(getStackElemAtLevel(Level).TaskgroupReductionRef) 560 ->getDecl() == VD; 561 } 562 563 /// Returns data sharing attributes from top of the stack for the 564 /// specified declaration. 565 const DSAVarData getTopDSA(ValueDecl *D, bool FromParent); 566 /// Returns data-sharing attributes for the specified declaration. 567 const DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent) const; 568 /// Returns data-sharing attributes for the specified declaration. 569 const DSAVarData getImplicitDSA(ValueDecl *D, unsigned Level) const; 570 /// Checks if the specified variables has data-sharing attributes which 571 /// match specified \a CPred predicate in any directive which matches \a DPred 572 /// predicate. 573 const DSAVarData 574 hasDSA(ValueDecl *D, 575 const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred, 576 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 577 bool FromParent) const; 578 /// Checks if the specified variables has data-sharing attributes which 579 /// match specified \a CPred predicate in any innermost directive which 580 /// matches \a DPred predicate. 581 const DSAVarData 582 hasInnermostDSA(ValueDecl *D, 583 const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred, 584 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 585 bool FromParent) const; 586 /// Checks if the specified variables has explicit data-sharing 587 /// attributes which match specified \a CPred predicate at the specified 588 /// OpenMP region. 589 bool 590 hasExplicitDSA(const ValueDecl *D, 591 const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred, 592 unsigned Level, bool NotLastprivate = false) const; 593 594 /// Returns true if the directive at level \Level matches in the 595 /// specified \a DPred predicate. 596 bool hasExplicitDirective( 597 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 598 unsigned Level) const; 599 600 /// Finds a directive which matches specified \a DPred predicate. 601 bool hasDirective( 602 const llvm::function_ref<bool( 603 OpenMPDirectiveKind, const DeclarationNameInfo &, SourceLocation)> 604 DPred, 605 bool FromParent) const; 606 607 /// Returns currently analyzed directive. 608 OpenMPDirectiveKind getCurrentDirective() const { 609 const SharingMapTy *Top = getTopOfStackOrNull(); 610 return Top ? Top->Directive : OMPD_unknown; 611 } 612 /// Returns directive kind at specified level. 613 OpenMPDirectiveKind getDirective(unsigned Level) const { 614 assert(!isStackEmpty() && "No directive at specified level."); 615 return getStackElemAtLevel(Level).Directive; 616 } 617 /// Returns the capture region at the specified level. 618 OpenMPDirectiveKind getCaptureRegion(unsigned Level, 619 unsigned OpenMPCaptureLevel) const { 620 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 621 getOpenMPCaptureRegions(CaptureRegions, getDirective(Level)); 622 return CaptureRegions[OpenMPCaptureLevel]; 623 } 624 /// Returns parent directive. 625 OpenMPDirectiveKind getParentDirective() const { 626 const SharingMapTy *Parent = getSecondOnStackOrNull(); 627 return Parent ? Parent->Directive : OMPD_unknown; 628 } 629 630 /// Add requires decl to internal vector 631 void addRequiresDecl(OMPRequiresDecl *RD) { 632 RequiresDecls.push_back(RD); 633 } 634 635 /// Checks if the defined 'requires' directive has specified type of clause. 636 template <typename ClauseType> 637 bool hasRequiresDeclWithClause() const { 638 return llvm::any_of(RequiresDecls, [](const OMPRequiresDecl *D) { 639 return llvm::any_of(D->clauselists(), [](const OMPClause *C) { 640 return isa<ClauseType>(C); 641 }); 642 }); 643 } 644 645 /// Checks for a duplicate clause amongst previously declared requires 646 /// directives 647 bool hasDuplicateRequiresClause(ArrayRef<OMPClause *> ClauseList) const { 648 bool IsDuplicate = false; 649 for (OMPClause *CNew : ClauseList) { 650 for (const OMPRequiresDecl *D : RequiresDecls) { 651 for (const OMPClause *CPrev : D->clauselists()) { 652 if (CNew->getClauseKind() == CPrev->getClauseKind()) { 653 SemaRef.Diag(CNew->getBeginLoc(), 654 diag::err_omp_requires_clause_redeclaration) 655 << getOpenMPClauseName(CNew->getClauseKind()); 656 SemaRef.Diag(CPrev->getBeginLoc(), 657 diag::note_omp_requires_previous_clause) 658 << getOpenMPClauseName(CPrev->getClauseKind()); 659 IsDuplicate = true; 660 } 661 } 662 } 663 } 664 return IsDuplicate; 665 } 666 667 /// Add location of previously encountered target to internal vector 668 void addTargetDirLocation(SourceLocation LocStart) { 669 TargetLocations.push_back(LocStart); 670 } 671 672 /// Add location for the first encountered atomicc directive. 673 void addAtomicDirectiveLoc(SourceLocation Loc) { 674 if (AtomicLocation.isInvalid()) 675 AtomicLocation = Loc; 676 } 677 678 /// Returns the location of the first encountered atomic directive in the 679 /// module. 680 SourceLocation getAtomicDirectiveLoc() const { 681 return AtomicLocation; 682 } 683 684 // Return previously encountered target region locations. 685 ArrayRef<SourceLocation> getEncounteredTargetLocs() const { 686 return TargetLocations; 687 } 688 689 /// Set default data sharing attribute to none. 690 void setDefaultDSANone(SourceLocation Loc) { 691 getTopOfStack().DefaultAttr = DSA_none; 692 getTopOfStack().DefaultAttrLoc = Loc; 693 } 694 /// Set default data sharing attribute to shared. 695 void setDefaultDSAShared(SourceLocation Loc) { 696 getTopOfStack().DefaultAttr = DSA_shared; 697 getTopOfStack().DefaultAttrLoc = Loc; 698 } 699 /// Set default data sharing attribute to firstprivate. 700 void setDefaultDSAFirstPrivate(SourceLocation Loc) { 701 getTopOfStack().DefaultAttr = DSA_firstprivate; 702 getTopOfStack().DefaultAttrLoc = Loc; 703 } 704 /// Set default data mapping attribute to Modifier:Kind 705 void setDefaultDMAAttr(OpenMPDefaultmapClauseModifier M, 706 OpenMPDefaultmapClauseKind Kind, 707 SourceLocation Loc) { 708 DefaultmapInfo &DMI = getTopOfStack().DefaultmapMap[Kind]; 709 DMI.ImplicitBehavior = M; 710 DMI.SLoc = Loc; 711 } 712 /// Check whether the implicit-behavior has been set in defaultmap 713 bool checkDefaultmapCategory(OpenMPDefaultmapClauseKind VariableCategory) { 714 if (VariableCategory == OMPC_DEFAULTMAP_unknown) 715 return getTopOfStack() 716 .DefaultmapMap[OMPC_DEFAULTMAP_aggregate] 717 .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown || 718 getTopOfStack() 719 .DefaultmapMap[OMPC_DEFAULTMAP_scalar] 720 .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown || 721 getTopOfStack() 722 .DefaultmapMap[OMPC_DEFAULTMAP_pointer] 723 .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown; 724 return getTopOfStack().DefaultmapMap[VariableCategory].ImplicitBehavior != 725 OMPC_DEFAULTMAP_MODIFIER_unknown; 726 } 727 728 DefaultDataSharingAttributes getDefaultDSA(unsigned Level) const { 729 return getStackSize() <= Level ? DSA_unspecified 730 : getStackElemAtLevel(Level).DefaultAttr; 731 } 732 DefaultDataSharingAttributes getDefaultDSA() const { 733 return isStackEmpty() ? DSA_unspecified 734 : getTopOfStack().DefaultAttr; 735 } 736 SourceLocation getDefaultDSALocation() const { 737 return isStackEmpty() ? SourceLocation() 738 : getTopOfStack().DefaultAttrLoc; 739 } 740 OpenMPDefaultmapClauseModifier 741 getDefaultmapModifier(OpenMPDefaultmapClauseKind Kind) const { 742 return isStackEmpty() 743 ? OMPC_DEFAULTMAP_MODIFIER_unknown 744 : getTopOfStack().DefaultmapMap[Kind].ImplicitBehavior; 745 } 746 OpenMPDefaultmapClauseModifier 747 getDefaultmapModifierAtLevel(unsigned Level, 748 OpenMPDefaultmapClauseKind Kind) const { 749 return getStackElemAtLevel(Level).DefaultmapMap[Kind].ImplicitBehavior; 750 } 751 bool isDefaultmapCapturedByRef(unsigned Level, 752 OpenMPDefaultmapClauseKind Kind) const { 753 OpenMPDefaultmapClauseModifier M = 754 getDefaultmapModifierAtLevel(Level, Kind); 755 if (Kind == OMPC_DEFAULTMAP_scalar || Kind == OMPC_DEFAULTMAP_pointer) { 756 return (M == OMPC_DEFAULTMAP_MODIFIER_alloc) || 757 (M == OMPC_DEFAULTMAP_MODIFIER_to) || 758 (M == OMPC_DEFAULTMAP_MODIFIER_from) || 759 (M == OMPC_DEFAULTMAP_MODIFIER_tofrom); 760 } 761 return true; 762 } 763 static bool mustBeFirstprivateBase(OpenMPDefaultmapClauseModifier M, 764 OpenMPDefaultmapClauseKind Kind) { 765 switch (Kind) { 766 case OMPC_DEFAULTMAP_scalar: 767 case OMPC_DEFAULTMAP_pointer: 768 return (M == OMPC_DEFAULTMAP_MODIFIER_unknown) || 769 (M == OMPC_DEFAULTMAP_MODIFIER_firstprivate) || 770 (M == OMPC_DEFAULTMAP_MODIFIER_default); 771 case OMPC_DEFAULTMAP_aggregate: 772 return M == OMPC_DEFAULTMAP_MODIFIER_firstprivate; 773 default: 774 break; 775 } 776 llvm_unreachable("Unexpected OpenMPDefaultmapClauseKind enum"); 777 } 778 bool mustBeFirstprivateAtLevel(unsigned Level, 779 OpenMPDefaultmapClauseKind Kind) const { 780 OpenMPDefaultmapClauseModifier M = 781 getDefaultmapModifierAtLevel(Level, Kind); 782 return mustBeFirstprivateBase(M, Kind); 783 } 784 bool mustBeFirstprivate(OpenMPDefaultmapClauseKind Kind) const { 785 OpenMPDefaultmapClauseModifier M = getDefaultmapModifier(Kind); 786 return mustBeFirstprivateBase(M, Kind); 787 } 788 789 /// Checks if the specified variable is a threadprivate. 790 bool isThreadPrivate(VarDecl *D) { 791 const DSAVarData DVar = getTopDSA(D, false); 792 return isOpenMPThreadPrivate(DVar.CKind); 793 } 794 795 /// Marks current region as ordered (it has an 'ordered' clause). 796 void setOrderedRegion(bool IsOrdered, const Expr *Param, 797 OMPOrderedClause *Clause) { 798 if (IsOrdered) 799 getTopOfStack().OrderedRegion.emplace(Param, Clause); 800 else 801 getTopOfStack().OrderedRegion.reset(); 802 } 803 /// Returns true, if region is ordered (has associated 'ordered' clause), 804 /// false - otherwise. 805 bool isOrderedRegion() const { 806 if (const SharingMapTy *Top = getTopOfStackOrNull()) 807 return Top->OrderedRegion.hasValue(); 808 return false; 809 } 810 /// Returns optional parameter for the ordered region. 811 std::pair<const Expr *, OMPOrderedClause *> getOrderedRegionParam() const { 812 if (const SharingMapTy *Top = getTopOfStackOrNull()) 813 if (Top->OrderedRegion.hasValue()) 814 return Top->OrderedRegion.getValue(); 815 return std::make_pair(nullptr, nullptr); 816 } 817 /// Returns true, if parent region is ordered (has associated 818 /// 'ordered' clause), false - otherwise. 819 bool isParentOrderedRegion() const { 820 if (const SharingMapTy *Parent = getSecondOnStackOrNull()) 821 return Parent->OrderedRegion.hasValue(); 822 return false; 823 } 824 /// Returns optional parameter for the ordered region. 825 std::pair<const Expr *, OMPOrderedClause *> 826 getParentOrderedRegionParam() const { 827 if (const SharingMapTy *Parent = getSecondOnStackOrNull()) 828 if (Parent->OrderedRegion.hasValue()) 829 return Parent->OrderedRegion.getValue(); 830 return std::make_pair(nullptr, nullptr); 831 } 832 /// Marks current region as nowait (it has a 'nowait' clause). 833 void setNowaitRegion(bool IsNowait = true) { 834 getTopOfStack().NowaitRegion = IsNowait; 835 } 836 /// Returns true, if parent region is nowait (has associated 837 /// 'nowait' clause), false - otherwise. 838 bool isParentNowaitRegion() const { 839 if (const SharingMapTy *Parent = getSecondOnStackOrNull()) 840 return Parent->NowaitRegion; 841 return false; 842 } 843 /// Marks parent region as cancel region. 844 void setParentCancelRegion(bool Cancel = true) { 845 if (SharingMapTy *Parent = getSecondOnStackOrNull()) 846 Parent->CancelRegion |= Cancel; 847 } 848 /// Return true if current region has inner cancel construct. 849 bool isCancelRegion() const { 850 const SharingMapTy *Top = getTopOfStackOrNull(); 851 return Top ? Top->CancelRegion : false; 852 } 853 854 /// Mark that parent region already has scan directive. 855 void setParentHasScanDirective(SourceLocation Loc) { 856 if (SharingMapTy *Parent = getSecondOnStackOrNull()) 857 Parent->PrevScanLocation = Loc; 858 } 859 /// Return true if current region has inner cancel construct. 860 bool doesParentHasScanDirective() const { 861 const SharingMapTy *Top = getSecondOnStackOrNull(); 862 return Top ? Top->PrevScanLocation.isValid() : false; 863 } 864 /// Return true if current region has inner cancel construct. 865 SourceLocation getParentScanDirectiveLoc() const { 866 const SharingMapTy *Top = getSecondOnStackOrNull(); 867 return Top ? Top->PrevScanLocation : SourceLocation(); 868 } 869 /// Mark that parent region already has ordered directive. 870 void setParentHasOrderedDirective(SourceLocation Loc) { 871 if (SharingMapTy *Parent = getSecondOnStackOrNull()) 872 Parent->PrevOrderedLocation = Loc; 873 } 874 /// Return true if current region has inner ordered construct. 875 bool doesParentHasOrderedDirective() const { 876 const SharingMapTy *Top = getSecondOnStackOrNull(); 877 return Top ? Top->PrevOrderedLocation.isValid() : false; 878 } 879 /// Returns the location of the previously specified ordered directive. 880 SourceLocation getParentOrderedDirectiveLoc() const { 881 const SharingMapTy *Top = getSecondOnStackOrNull(); 882 return Top ? Top->PrevOrderedLocation : SourceLocation(); 883 } 884 885 /// Set collapse value for the region. 886 void setAssociatedLoops(unsigned Val) { 887 getTopOfStack().AssociatedLoops = Val; 888 if (Val > 1) 889 getTopOfStack().HasMutipleLoops = true; 890 } 891 /// Return collapse value for region. 892 unsigned getAssociatedLoops() const { 893 const SharingMapTy *Top = getTopOfStackOrNull(); 894 return Top ? Top->AssociatedLoops : 0; 895 } 896 /// Returns true if the construct is associated with multiple loops. 897 bool hasMutipleLoops() const { 898 const SharingMapTy *Top = getTopOfStackOrNull(); 899 return Top ? Top->HasMutipleLoops : false; 900 } 901 902 /// Marks current target region as one with closely nested teams 903 /// region. 904 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) { 905 if (SharingMapTy *Parent = getSecondOnStackOrNull()) 906 Parent->InnerTeamsRegionLoc = TeamsRegionLoc; 907 } 908 /// Returns true, if current region has closely nested teams region. 909 bool hasInnerTeamsRegion() const { 910 return getInnerTeamsRegionLoc().isValid(); 911 } 912 /// Returns location of the nested teams region (if any). 913 SourceLocation getInnerTeamsRegionLoc() const { 914 const SharingMapTy *Top = getTopOfStackOrNull(); 915 return Top ? Top->InnerTeamsRegionLoc : SourceLocation(); 916 } 917 918 Scope *getCurScope() const { 919 const SharingMapTy *Top = getTopOfStackOrNull(); 920 return Top ? Top->CurScope : nullptr; 921 } 922 void setContext(DeclContext *DC) { getTopOfStack().Context = DC; } 923 SourceLocation getConstructLoc() const { 924 const SharingMapTy *Top = getTopOfStackOrNull(); 925 return Top ? Top->ConstructLoc : SourceLocation(); 926 } 927 928 /// Do the check specified in \a Check to all component lists and return true 929 /// if any issue is found. 930 bool checkMappableExprComponentListsForDecl( 931 const ValueDecl *VD, bool CurrentRegionOnly, 932 const llvm::function_ref< 933 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef, 934 OpenMPClauseKind)> 935 Check) const { 936 if (isStackEmpty()) 937 return false; 938 auto SI = begin(); 939 auto SE = end(); 940 941 if (SI == SE) 942 return false; 943 944 if (CurrentRegionOnly) 945 SE = std::next(SI); 946 else 947 std::advance(SI, 1); 948 949 for (; SI != SE; ++SI) { 950 auto MI = SI->MappedExprComponents.find(VD); 951 if (MI != SI->MappedExprComponents.end()) 952 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L : 953 MI->second.Components) 954 if (Check(L, MI->second.Kind)) 955 return true; 956 } 957 return false; 958 } 959 960 /// Do the check specified in \a Check to all component lists at a given level 961 /// and return true if any issue is found. 962 bool checkMappableExprComponentListsForDeclAtLevel( 963 const ValueDecl *VD, unsigned Level, 964 const llvm::function_ref< 965 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef, 966 OpenMPClauseKind)> 967 Check) const { 968 if (getStackSize() <= Level) 969 return false; 970 971 const SharingMapTy &StackElem = getStackElemAtLevel(Level); 972 auto MI = StackElem.MappedExprComponents.find(VD); 973 if (MI != StackElem.MappedExprComponents.end()) 974 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L : 975 MI->second.Components) 976 if (Check(L, MI->second.Kind)) 977 return true; 978 return false; 979 } 980 981 /// Create a new mappable expression component list associated with a given 982 /// declaration and initialize it with the provided list of components. 983 void addMappableExpressionComponents( 984 const ValueDecl *VD, 985 OMPClauseMappableExprCommon::MappableExprComponentListRef Components, 986 OpenMPClauseKind WhereFoundClauseKind) { 987 MappedExprComponentTy &MEC = getTopOfStack().MappedExprComponents[VD]; 988 // Create new entry and append the new components there. 989 MEC.Components.resize(MEC.Components.size() + 1); 990 MEC.Components.back().append(Components.begin(), Components.end()); 991 MEC.Kind = WhereFoundClauseKind; 992 } 993 994 unsigned getNestingLevel() const { 995 assert(!isStackEmpty()); 996 return getStackSize() - 1; 997 } 998 void addDoacrossDependClause(OMPDependClause *C, 999 const OperatorOffsetTy &OpsOffs) { 1000 SharingMapTy *Parent = getSecondOnStackOrNull(); 1001 assert(Parent && isOpenMPWorksharingDirective(Parent->Directive)); 1002 Parent->DoacrossDepends.try_emplace(C, OpsOffs); 1003 } 1004 llvm::iterator_range<DoacrossDependMapTy::const_iterator> 1005 getDoacrossDependClauses() const { 1006 const SharingMapTy &StackElem = getTopOfStack(); 1007 if (isOpenMPWorksharingDirective(StackElem.Directive)) { 1008 const DoacrossDependMapTy &Ref = StackElem.DoacrossDepends; 1009 return llvm::make_range(Ref.begin(), Ref.end()); 1010 } 1011 return llvm::make_range(StackElem.DoacrossDepends.end(), 1012 StackElem.DoacrossDepends.end()); 1013 } 1014 1015 // Store types of classes which have been explicitly mapped 1016 void addMappedClassesQualTypes(QualType QT) { 1017 SharingMapTy &StackElem = getTopOfStack(); 1018 StackElem.MappedClassesQualTypes.insert(QT); 1019 } 1020 1021 // Return set of mapped classes types 1022 bool isClassPreviouslyMapped(QualType QT) const { 1023 const SharingMapTy &StackElem = getTopOfStack(); 1024 return StackElem.MappedClassesQualTypes.count(QT) != 0; 1025 } 1026 1027 /// Adds global declare target to the parent target region. 1028 void addToParentTargetRegionLinkGlobals(DeclRefExpr *E) { 1029 assert(*OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration( 1030 E->getDecl()) == OMPDeclareTargetDeclAttr::MT_Link && 1031 "Expected declare target link global."); 1032 for (auto &Elem : *this) { 1033 if (isOpenMPTargetExecutionDirective(Elem.Directive)) { 1034 Elem.DeclareTargetLinkVarDecls.push_back(E); 1035 return; 1036 } 1037 } 1038 } 1039 1040 /// Returns the list of globals with declare target link if current directive 1041 /// is target. 1042 ArrayRef<DeclRefExpr *> getLinkGlobals() const { 1043 assert(isOpenMPTargetExecutionDirective(getCurrentDirective()) && 1044 "Expected target executable directive."); 1045 return getTopOfStack().DeclareTargetLinkVarDecls; 1046 } 1047 1048 /// Adds list of allocators expressions. 1049 void addInnerAllocatorExpr(Expr *E) { 1050 getTopOfStack().InnerUsedAllocators.push_back(E); 1051 } 1052 /// Return list of used allocators. 1053 ArrayRef<Expr *> getInnerAllocators() const { 1054 return getTopOfStack().InnerUsedAllocators; 1055 } 1056 /// Marks the declaration as implicitly firstprivate nin the task-based 1057 /// regions. 1058 void addImplicitTaskFirstprivate(unsigned Level, Decl *D) { 1059 getStackElemAtLevel(Level).ImplicitTaskFirstprivates.insert(D); 1060 } 1061 /// Checks if the decl is implicitly firstprivate in the task-based region. 1062 bool isImplicitTaskFirstprivate(Decl *D) const { 1063 return getTopOfStack().ImplicitTaskFirstprivates.count(D) > 0; 1064 } 1065 1066 /// Marks decl as used in uses_allocators clause as the allocator. 1067 void addUsesAllocatorsDecl(const Decl *D, UsesAllocatorsDeclKind Kind) { 1068 getTopOfStack().UsesAllocatorsDecls.try_emplace(D, Kind); 1069 } 1070 /// Checks if specified decl is used in uses allocator clause as the 1071 /// allocator. 1072 Optional<UsesAllocatorsDeclKind> isUsesAllocatorsDecl(unsigned Level, 1073 const Decl *D) const { 1074 const SharingMapTy &StackElem = getTopOfStack(); 1075 auto I = StackElem.UsesAllocatorsDecls.find(D); 1076 if (I == StackElem.UsesAllocatorsDecls.end()) 1077 return None; 1078 return I->getSecond(); 1079 } 1080 Optional<UsesAllocatorsDeclKind> isUsesAllocatorsDecl(const Decl *D) const { 1081 const SharingMapTy &StackElem = getTopOfStack(); 1082 auto I = StackElem.UsesAllocatorsDecls.find(D); 1083 if (I == StackElem.UsesAllocatorsDecls.end()) 1084 return None; 1085 return I->getSecond(); 1086 } 1087 1088 void addDeclareMapperVarRef(Expr *Ref) { 1089 SharingMapTy &StackElem = getTopOfStack(); 1090 StackElem.DeclareMapperVar = Ref; 1091 } 1092 const Expr *getDeclareMapperVarRef() const { 1093 const SharingMapTy *Top = getTopOfStackOrNull(); 1094 return Top ? Top->DeclareMapperVar : nullptr; 1095 } 1096 }; 1097 1098 bool isImplicitTaskingRegion(OpenMPDirectiveKind DKind) { 1099 return isOpenMPParallelDirective(DKind) || isOpenMPTeamsDirective(DKind); 1100 } 1101 1102 bool isImplicitOrExplicitTaskingRegion(OpenMPDirectiveKind DKind) { 1103 return isImplicitTaskingRegion(DKind) || isOpenMPTaskingDirective(DKind) || 1104 DKind == OMPD_unknown; 1105 } 1106 1107 } // namespace 1108 1109 static const Expr *getExprAsWritten(const Expr *E) { 1110 if (const auto *FE = dyn_cast<FullExpr>(E)) 1111 E = FE->getSubExpr(); 1112 1113 if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E)) 1114 E = MTE->getSubExpr(); 1115 1116 while (const auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E)) 1117 E = Binder->getSubExpr(); 1118 1119 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E)) 1120 E = ICE->getSubExprAsWritten(); 1121 return E->IgnoreParens(); 1122 } 1123 1124 static Expr *getExprAsWritten(Expr *E) { 1125 return const_cast<Expr *>(getExprAsWritten(const_cast<const Expr *>(E))); 1126 } 1127 1128 static const ValueDecl *getCanonicalDecl(const ValueDecl *D) { 1129 if (const auto *CED = dyn_cast<OMPCapturedExprDecl>(D)) 1130 if (const auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 1131 D = ME->getMemberDecl(); 1132 const auto *VD = dyn_cast<VarDecl>(D); 1133 const auto *FD = dyn_cast<FieldDecl>(D); 1134 if (VD != nullptr) { 1135 VD = VD->getCanonicalDecl(); 1136 D = VD; 1137 } else { 1138 assert(FD); 1139 FD = FD->getCanonicalDecl(); 1140 D = FD; 1141 } 1142 return D; 1143 } 1144 1145 static ValueDecl *getCanonicalDecl(ValueDecl *D) { 1146 return const_cast<ValueDecl *>( 1147 getCanonicalDecl(const_cast<const ValueDecl *>(D))); 1148 } 1149 1150 DSAStackTy::DSAVarData DSAStackTy::getDSA(const_iterator &Iter, 1151 ValueDecl *D) const { 1152 D = getCanonicalDecl(D); 1153 auto *VD = dyn_cast<VarDecl>(D); 1154 const auto *FD = dyn_cast<FieldDecl>(D); 1155 DSAVarData DVar; 1156 if (Iter == end()) { 1157 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1158 // in a region but not in construct] 1159 // File-scope or namespace-scope variables referenced in called routines 1160 // in the region are shared unless they appear in a threadprivate 1161 // directive. 1162 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(VD)) 1163 DVar.CKind = OMPC_shared; 1164 1165 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced 1166 // in a region but not in construct] 1167 // Variables with static storage duration that are declared in called 1168 // routines in the region are shared. 1169 if (VD && VD->hasGlobalStorage()) 1170 DVar.CKind = OMPC_shared; 1171 1172 // Non-static data members are shared by default. 1173 if (FD) 1174 DVar.CKind = OMPC_shared; 1175 1176 return DVar; 1177 } 1178 1179 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1180 // in a Construct, C/C++, predetermined, p.1] 1181 // Variables with automatic storage duration that are declared in a scope 1182 // inside the construct are private. 1183 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() && 1184 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) { 1185 DVar.CKind = OMPC_private; 1186 return DVar; 1187 } 1188 1189 DVar.DKind = Iter->Directive; 1190 // Explicitly specified attributes and local variables with predetermined 1191 // attributes. 1192 if (Iter->SharingMap.count(D)) { 1193 const DSAInfo &Data = Iter->SharingMap.lookup(D); 1194 DVar.RefExpr = Data.RefExpr.getPointer(); 1195 DVar.PrivateCopy = Data.PrivateCopy; 1196 DVar.CKind = Data.Attributes; 1197 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 1198 DVar.Modifier = Data.Modifier; 1199 DVar.AppliedToPointee = Data.AppliedToPointee; 1200 return DVar; 1201 } 1202 1203 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1204 // in a Construct, C/C++, implicitly determined, p.1] 1205 // In a parallel or task construct, the data-sharing attributes of these 1206 // variables are determined by the default clause, if present. 1207 switch (Iter->DefaultAttr) { 1208 case DSA_shared: 1209 DVar.CKind = OMPC_shared; 1210 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 1211 return DVar; 1212 case DSA_none: 1213 return DVar; 1214 case DSA_firstprivate: 1215 if (VD->getStorageDuration() == SD_Static && 1216 VD->getDeclContext()->isFileContext()) { 1217 DVar.CKind = OMPC_unknown; 1218 } else { 1219 DVar.CKind = OMPC_firstprivate; 1220 } 1221 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 1222 return DVar; 1223 case DSA_unspecified: 1224 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1225 // in a Construct, implicitly determined, p.2] 1226 // In a parallel construct, if no default clause is present, these 1227 // variables are shared. 1228 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 1229 if ((isOpenMPParallelDirective(DVar.DKind) && 1230 !isOpenMPTaskLoopDirective(DVar.DKind)) || 1231 isOpenMPTeamsDirective(DVar.DKind)) { 1232 DVar.CKind = OMPC_shared; 1233 return DVar; 1234 } 1235 1236 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1237 // in a Construct, implicitly determined, p.4] 1238 // In a task construct, if no default clause is present, a variable that in 1239 // the enclosing context is determined to be shared by all implicit tasks 1240 // bound to the current team is shared. 1241 if (isOpenMPTaskingDirective(DVar.DKind)) { 1242 DSAVarData DVarTemp; 1243 const_iterator I = Iter, E = end(); 1244 do { 1245 ++I; 1246 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables 1247 // Referenced in a Construct, implicitly determined, p.6] 1248 // In a task construct, if no default clause is present, a variable 1249 // whose data-sharing attribute is not determined by the rules above is 1250 // firstprivate. 1251 DVarTemp = getDSA(I, D); 1252 if (DVarTemp.CKind != OMPC_shared) { 1253 DVar.RefExpr = nullptr; 1254 DVar.CKind = OMPC_firstprivate; 1255 return DVar; 1256 } 1257 } while (I != E && !isImplicitTaskingRegion(I->Directive)); 1258 DVar.CKind = 1259 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared; 1260 return DVar; 1261 } 1262 } 1263 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1264 // in a Construct, implicitly determined, p.3] 1265 // For constructs other than task, if no default clause is present, these 1266 // variables inherit their data-sharing attributes from the enclosing 1267 // context. 1268 return getDSA(++Iter, D); 1269 } 1270 1271 const Expr *DSAStackTy::addUniqueAligned(const ValueDecl *D, 1272 const Expr *NewDE) { 1273 assert(!isStackEmpty() && "Data sharing attributes stack is empty"); 1274 D = getCanonicalDecl(D); 1275 SharingMapTy &StackElem = getTopOfStack(); 1276 auto It = StackElem.AlignedMap.find(D); 1277 if (It == StackElem.AlignedMap.end()) { 1278 assert(NewDE && "Unexpected nullptr expr to be added into aligned map"); 1279 StackElem.AlignedMap[D] = NewDE; 1280 return nullptr; 1281 } 1282 assert(It->second && "Unexpected nullptr expr in the aligned map"); 1283 return It->second; 1284 } 1285 1286 const Expr *DSAStackTy::addUniqueNontemporal(const ValueDecl *D, 1287 const Expr *NewDE) { 1288 assert(!isStackEmpty() && "Data sharing attributes stack is empty"); 1289 D = getCanonicalDecl(D); 1290 SharingMapTy &StackElem = getTopOfStack(); 1291 auto It = StackElem.NontemporalMap.find(D); 1292 if (It == StackElem.NontemporalMap.end()) { 1293 assert(NewDE && "Unexpected nullptr expr to be added into aligned map"); 1294 StackElem.NontemporalMap[D] = NewDE; 1295 return nullptr; 1296 } 1297 assert(It->second && "Unexpected nullptr expr in the aligned map"); 1298 return It->second; 1299 } 1300 1301 void DSAStackTy::addLoopControlVariable(const ValueDecl *D, VarDecl *Capture) { 1302 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1303 D = getCanonicalDecl(D); 1304 SharingMapTy &StackElem = getTopOfStack(); 1305 StackElem.LCVMap.try_emplace( 1306 D, LCDeclInfo(StackElem.LCVMap.size() + 1, Capture)); 1307 } 1308 1309 const DSAStackTy::LCDeclInfo 1310 DSAStackTy::isLoopControlVariable(const ValueDecl *D) const { 1311 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1312 D = getCanonicalDecl(D); 1313 const SharingMapTy &StackElem = getTopOfStack(); 1314 auto It = StackElem.LCVMap.find(D); 1315 if (It != StackElem.LCVMap.end()) 1316 return It->second; 1317 return {0, nullptr}; 1318 } 1319 1320 const DSAStackTy::LCDeclInfo 1321 DSAStackTy::isLoopControlVariable(const ValueDecl *D, unsigned Level) const { 1322 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1323 D = getCanonicalDecl(D); 1324 for (unsigned I = Level + 1; I > 0; --I) { 1325 const SharingMapTy &StackElem = getStackElemAtLevel(I - 1); 1326 auto It = StackElem.LCVMap.find(D); 1327 if (It != StackElem.LCVMap.end()) 1328 return It->second; 1329 } 1330 return {0, nullptr}; 1331 } 1332 1333 const DSAStackTy::LCDeclInfo 1334 DSAStackTy::isParentLoopControlVariable(const ValueDecl *D) const { 1335 const SharingMapTy *Parent = getSecondOnStackOrNull(); 1336 assert(Parent && "Data-sharing attributes stack is empty"); 1337 D = getCanonicalDecl(D); 1338 auto It = Parent->LCVMap.find(D); 1339 if (It != Parent->LCVMap.end()) 1340 return It->second; 1341 return {0, nullptr}; 1342 } 1343 1344 const ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) const { 1345 const SharingMapTy *Parent = getSecondOnStackOrNull(); 1346 assert(Parent && "Data-sharing attributes stack is empty"); 1347 if (Parent->LCVMap.size() < I) 1348 return nullptr; 1349 for (const auto &Pair : Parent->LCVMap) 1350 if (Pair.second.first == I) 1351 return Pair.first; 1352 return nullptr; 1353 } 1354 1355 void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A, 1356 DeclRefExpr *PrivateCopy, unsigned Modifier, 1357 bool AppliedToPointee) { 1358 D = getCanonicalDecl(D); 1359 if (A == OMPC_threadprivate) { 1360 DSAInfo &Data = Threadprivates[D]; 1361 Data.Attributes = A; 1362 Data.RefExpr.setPointer(E); 1363 Data.PrivateCopy = nullptr; 1364 Data.Modifier = Modifier; 1365 } else { 1366 DSAInfo &Data = getTopOfStack().SharingMap[D]; 1367 assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) || 1368 (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) || 1369 (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) || 1370 (isLoopControlVariable(D).first && A == OMPC_private)); 1371 Data.Modifier = Modifier; 1372 if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) { 1373 Data.RefExpr.setInt(/*IntVal=*/true); 1374 return; 1375 } 1376 const bool IsLastprivate = 1377 A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate; 1378 Data.Attributes = A; 1379 Data.RefExpr.setPointerAndInt(E, IsLastprivate); 1380 Data.PrivateCopy = PrivateCopy; 1381 Data.AppliedToPointee = AppliedToPointee; 1382 if (PrivateCopy) { 1383 DSAInfo &Data = getTopOfStack().SharingMap[PrivateCopy->getDecl()]; 1384 Data.Modifier = Modifier; 1385 Data.Attributes = A; 1386 Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate); 1387 Data.PrivateCopy = nullptr; 1388 Data.AppliedToPointee = AppliedToPointee; 1389 } 1390 } 1391 } 1392 1393 /// Build a variable declaration for OpenMP loop iteration variable. 1394 static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type, 1395 StringRef Name, const AttrVec *Attrs = nullptr, 1396 DeclRefExpr *OrigRef = nullptr) { 1397 DeclContext *DC = SemaRef.CurContext; 1398 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name); 1399 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc); 1400 auto *Decl = 1401 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None); 1402 if (Attrs) { 1403 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end()); 1404 I != E; ++I) 1405 Decl->addAttr(*I); 1406 } 1407 Decl->setImplicit(); 1408 if (OrigRef) { 1409 Decl->addAttr( 1410 OMPReferencedVarAttr::CreateImplicit(SemaRef.Context, OrigRef)); 1411 } 1412 return Decl; 1413 } 1414 1415 static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty, 1416 SourceLocation Loc, 1417 bool RefersToCapture = false) { 1418 D->setReferenced(); 1419 D->markUsed(S.Context); 1420 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(), 1421 SourceLocation(), D, RefersToCapture, Loc, Ty, 1422 VK_LValue); 1423 } 1424 1425 void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 1426 BinaryOperatorKind BOK) { 1427 D = getCanonicalDecl(D); 1428 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1429 assert( 1430 getTopOfStack().SharingMap[D].Attributes == OMPC_reduction && 1431 "Additional reduction info may be specified only for reduction items."); 1432 ReductionData &ReductionData = getTopOfStack().ReductionMap[D]; 1433 assert(ReductionData.ReductionRange.isInvalid() && 1434 (getTopOfStack().Directive == OMPD_taskgroup || 1435 ((isOpenMPParallelDirective(getTopOfStack().Directive) || 1436 isOpenMPWorksharingDirective(getTopOfStack().Directive)) && 1437 !isOpenMPSimdDirective(getTopOfStack().Directive))) && 1438 "Additional reduction info may be specified only once for reduction " 1439 "items."); 1440 ReductionData.set(BOK, SR); 1441 Expr *&TaskgroupReductionRef = 1442 getTopOfStack().TaskgroupReductionRef; 1443 if (!TaskgroupReductionRef) { 1444 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(), 1445 SemaRef.Context.VoidPtrTy, ".task_red."); 1446 TaskgroupReductionRef = 1447 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin()); 1448 } 1449 } 1450 1451 void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 1452 const Expr *ReductionRef) { 1453 D = getCanonicalDecl(D); 1454 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1455 assert( 1456 getTopOfStack().SharingMap[D].Attributes == OMPC_reduction && 1457 "Additional reduction info may be specified only for reduction items."); 1458 ReductionData &ReductionData = getTopOfStack().ReductionMap[D]; 1459 assert(ReductionData.ReductionRange.isInvalid() && 1460 (getTopOfStack().Directive == OMPD_taskgroup || 1461 ((isOpenMPParallelDirective(getTopOfStack().Directive) || 1462 isOpenMPWorksharingDirective(getTopOfStack().Directive)) && 1463 !isOpenMPSimdDirective(getTopOfStack().Directive))) && 1464 "Additional reduction info may be specified only once for reduction " 1465 "items."); 1466 ReductionData.set(ReductionRef, SR); 1467 Expr *&TaskgroupReductionRef = 1468 getTopOfStack().TaskgroupReductionRef; 1469 if (!TaskgroupReductionRef) { 1470 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(), 1471 SemaRef.Context.VoidPtrTy, ".task_red."); 1472 TaskgroupReductionRef = 1473 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin()); 1474 } 1475 } 1476 1477 const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData( 1478 const ValueDecl *D, SourceRange &SR, BinaryOperatorKind &BOK, 1479 Expr *&TaskgroupDescriptor) const { 1480 D = getCanonicalDecl(D); 1481 assert(!isStackEmpty() && "Data-sharing attributes stack is empty."); 1482 for (const_iterator I = begin() + 1, E = end(); I != E; ++I) { 1483 const DSAInfo &Data = I->SharingMap.lookup(D); 1484 if (Data.Attributes != OMPC_reduction || 1485 Data.Modifier != OMPC_REDUCTION_task) 1486 continue; 1487 const ReductionData &ReductionData = I->ReductionMap.lookup(D); 1488 if (!ReductionData.ReductionOp || 1489 ReductionData.ReductionOp.is<const Expr *>()) 1490 return DSAVarData(); 1491 SR = ReductionData.ReductionRange; 1492 BOK = ReductionData.ReductionOp.get<ReductionData::BOKPtrType>(); 1493 assert(I->TaskgroupReductionRef && "taskgroup reduction reference " 1494 "expression for the descriptor is not " 1495 "set."); 1496 TaskgroupDescriptor = I->TaskgroupReductionRef; 1497 return DSAVarData(I->Directive, OMPC_reduction, Data.RefExpr.getPointer(), 1498 Data.PrivateCopy, I->DefaultAttrLoc, OMPC_REDUCTION_task, 1499 /*AppliedToPointee=*/false); 1500 } 1501 return DSAVarData(); 1502 } 1503 1504 const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData( 1505 const ValueDecl *D, SourceRange &SR, const Expr *&ReductionRef, 1506 Expr *&TaskgroupDescriptor) const { 1507 D = getCanonicalDecl(D); 1508 assert(!isStackEmpty() && "Data-sharing attributes stack is empty."); 1509 for (const_iterator I = begin() + 1, E = end(); I != E; ++I) { 1510 const DSAInfo &Data = I->SharingMap.lookup(D); 1511 if (Data.Attributes != OMPC_reduction || 1512 Data.Modifier != OMPC_REDUCTION_task) 1513 continue; 1514 const ReductionData &ReductionData = I->ReductionMap.lookup(D); 1515 if (!ReductionData.ReductionOp || 1516 !ReductionData.ReductionOp.is<const Expr *>()) 1517 return DSAVarData(); 1518 SR = ReductionData.ReductionRange; 1519 ReductionRef = ReductionData.ReductionOp.get<const Expr *>(); 1520 assert(I->TaskgroupReductionRef && "taskgroup reduction reference " 1521 "expression for the descriptor is not " 1522 "set."); 1523 TaskgroupDescriptor = I->TaskgroupReductionRef; 1524 return DSAVarData(I->Directive, OMPC_reduction, Data.RefExpr.getPointer(), 1525 Data.PrivateCopy, I->DefaultAttrLoc, OMPC_REDUCTION_task, 1526 /*AppliedToPointee=*/false); 1527 } 1528 return DSAVarData(); 1529 } 1530 1531 bool DSAStackTy::isOpenMPLocal(VarDecl *D, const_iterator I) const { 1532 D = D->getCanonicalDecl(); 1533 for (const_iterator E = end(); I != E; ++I) { 1534 if (isImplicitOrExplicitTaskingRegion(I->Directive) || 1535 isOpenMPTargetExecutionDirective(I->Directive)) { 1536 if (I->CurScope) { 1537 Scope *TopScope = I->CurScope->getParent(); 1538 Scope *CurScope = getCurScope(); 1539 while (CurScope && CurScope != TopScope && !CurScope->isDeclScope(D)) 1540 CurScope = CurScope->getParent(); 1541 return CurScope != TopScope; 1542 } 1543 for (DeclContext *DC = D->getDeclContext(); DC; DC = DC->getParent()) 1544 if (I->Context == DC) 1545 return true; 1546 return false; 1547 } 1548 } 1549 return false; 1550 } 1551 1552 static bool isConstNotMutableType(Sema &SemaRef, QualType Type, 1553 bool AcceptIfMutable = true, 1554 bool *IsClassType = nullptr) { 1555 ASTContext &Context = SemaRef.getASTContext(); 1556 Type = Type.getNonReferenceType().getCanonicalType(); 1557 bool IsConstant = Type.isConstant(Context); 1558 Type = Context.getBaseElementType(Type); 1559 const CXXRecordDecl *RD = AcceptIfMutable && SemaRef.getLangOpts().CPlusPlus 1560 ? Type->getAsCXXRecordDecl() 1561 : nullptr; 1562 if (const auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD)) 1563 if (const ClassTemplateDecl *CTD = CTSD->getSpecializedTemplate()) 1564 RD = CTD->getTemplatedDecl(); 1565 if (IsClassType) 1566 *IsClassType = RD; 1567 return IsConstant && !(SemaRef.getLangOpts().CPlusPlus && RD && 1568 RD->hasDefinition() && RD->hasMutableFields()); 1569 } 1570 1571 static bool rejectConstNotMutableType(Sema &SemaRef, const ValueDecl *D, 1572 QualType Type, OpenMPClauseKind CKind, 1573 SourceLocation ELoc, 1574 bool AcceptIfMutable = true, 1575 bool ListItemNotVar = false) { 1576 ASTContext &Context = SemaRef.getASTContext(); 1577 bool IsClassType; 1578 if (isConstNotMutableType(SemaRef, Type, AcceptIfMutable, &IsClassType)) { 1579 unsigned Diag = ListItemNotVar 1580 ? diag::err_omp_const_list_item 1581 : IsClassType ? diag::err_omp_const_not_mutable_variable 1582 : diag::err_omp_const_variable; 1583 SemaRef.Diag(ELoc, Diag) << getOpenMPClauseName(CKind); 1584 if (!ListItemNotVar && D) { 1585 const VarDecl *VD = dyn_cast<VarDecl>(D); 1586 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 1587 VarDecl::DeclarationOnly; 1588 SemaRef.Diag(D->getLocation(), 1589 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1590 << D; 1591 } 1592 return true; 1593 } 1594 return false; 1595 } 1596 1597 const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, 1598 bool FromParent) { 1599 D = getCanonicalDecl(D); 1600 DSAVarData DVar; 1601 1602 auto *VD = dyn_cast<VarDecl>(D); 1603 auto TI = Threadprivates.find(D); 1604 if (TI != Threadprivates.end()) { 1605 DVar.RefExpr = TI->getSecond().RefExpr.getPointer(); 1606 DVar.CKind = OMPC_threadprivate; 1607 DVar.Modifier = TI->getSecond().Modifier; 1608 return DVar; 1609 } 1610 if (VD && VD->hasAttr<OMPThreadPrivateDeclAttr>()) { 1611 DVar.RefExpr = buildDeclRefExpr( 1612 SemaRef, VD, D->getType().getNonReferenceType(), 1613 VD->getAttr<OMPThreadPrivateDeclAttr>()->getLocation()); 1614 DVar.CKind = OMPC_threadprivate; 1615 addDSA(D, DVar.RefExpr, OMPC_threadprivate); 1616 return DVar; 1617 } 1618 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1619 // in a Construct, C/C++, predetermined, p.1] 1620 // Variables appearing in threadprivate directives are threadprivate. 1621 if ((VD && VD->getTLSKind() != VarDecl::TLS_None && 1622 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() && 1623 SemaRef.getLangOpts().OpenMPUseTLS && 1624 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) || 1625 (VD && VD->getStorageClass() == SC_Register && 1626 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) { 1627 DVar.RefExpr = buildDeclRefExpr( 1628 SemaRef, VD, D->getType().getNonReferenceType(), D->getLocation()); 1629 DVar.CKind = OMPC_threadprivate; 1630 addDSA(D, DVar.RefExpr, OMPC_threadprivate); 1631 return DVar; 1632 } 1633 if (SemaRef.getLangOpts().OpenMPCUDAMode && VD && 1634 VD->isLocalVarDeclOrParm() && !isStackEmpty() && 1635 !isLoopControlVariable(D).first) { 1636 const_iterator IterTarget = 1637 std::find_if(begin(), end(), [](const SharingMapTy &Data) { 1638 return isOpenMPTargetExecutionDirective(Data.Directive); 1639 }); 1640 if (IterTarget != end()) { 1641 const_iterator ParentIterTarget = IterTarget + 1; 1642 for (const_iterator Iter = begin(); 1643 Iter != ParentIterTarget; ++Iter) { 1644 if (isOpenMPLocal(VD, Iter)) { 1645 DVar.RefExpr = 1646 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(), 1647 D->getLocation()); 1648 DVar.CKind = OMPC_threadprivate; 1649 return DVar; 1650 } 1651 } 1652 if (!isClauseParsingMode() || IterTarget != begin()) { 1653 auto DSAIter = IterTarget->SharingMap.find(D); 1654 if (DSAIter != IterTarget->SharingMap.end() && 1655 isOpenMPPrivate(DSAIter->getSecond().Attributes)) { 1656 DVar.RefExpr = DSAIter->getSecond().RefExpr.getPointer(); 1657 DVar.CKind = OMPC_threadprivate; 1658 return DVar; 1659 } 1660 const_iterator End = end(); 1661 if (!SemaRef.isOpenMPCapturedByRef( 1662 D, std::distance(ParentIterTarget, End), 1663 /*OpenMPCaptureLevel=*/0)) { 1664 DVar.RefExpr = 1665 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(), 1666 IterTarget->ConstructLoc); 1667 DVar.CKind = OMPC_threadprivate; 1668 return DVar; 1669 } 1670 } 1671 } 1672 } 1673 1674 if (isStackEmpty()) 1675 // Not in OpenMP execution region and top scope was already checked. 1676 return DVar; 1677 1678 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1679 // in a Construct, C/C++, predetermined, p.4] 1680 // Static data members are shared. 1681 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1682 // in a Construct, C/C++, predetermined, p.7] 1683 // Variables with static storage duration that are declared in a scope 1684 // inside the construct are shared. 1685 if (VD && VD->isStaticDataMember()) { 1686 // Check for explicitly specified attributes. 1687 const_iterator I = begin(); 1688 const_iterator EndI = end(); 1689 if (FromParent && I != EndI) 1690 ++I; 1691 if (I != EndI) { 1692 auto It = I->SharingMap.find(D); 1693 if (It != I->SharingMap.end()) { 1694 const DSAInfo &Data = It->getSecond(); 1695 DVar.RefExpr = Data.RefExpr.getPointer(); 1696 DVar.PrivateCopy = Data.PrivateCopy; 1697 DVar.CKind = Data.Attributes; 1698 DVar.ImplicitDSALoc = I->DefaultAttrLoc; 1699 DVar.DKind = I->Directive; 1700 DVar.Modifier = Data.Modifier; 1701 DVar.AppliedToPointee = Data.AppliedToPointee; 1702 return DVar; 1703 } 1704 } 1705 1706 DVar.CKind = OMPC_shared; 1707 return DVar; 1708 } 1709 1710 auto &&MatchesAlways = [](OpenMPDirectiveKind) { return true; }; 1711 // The predetermined shared attribute for const-qualified types having no 1712 // mutable members was removed after OpenMP 3.1. 1713 if (SemaRef.LangOpts.OpenMP <= 31) { 1714 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1715 // in a Construct, C/C++, predetermined, p.6] 1716 // Variables with const qualified type having no mutable member are 1717 // shared. 1718 if (isConstNotMutableType(SemaRef, D->getType())) { 1719 // Variables with const-qualified type having no mutable member may be 1720 // listed in a firstprivate clause, even if they are static data members. 1721 DSAVarData DVarTemp = hasInnermostDSA( 1722 D, 1723 [](OpenMPClauseKind C, bool) { 1724 return C == OMPC_firstprivate || C == OMPC_shared; 1725 }, 1726 MatchesAlways, FromParent); 1727 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr) 1728 return DVarTemp; 1729 1730 DVar.CKind = OMPC_shared; 1731 return DVar; 1732 } 1733 } 1734 1735 // Explicitly specified attributes and local variables with predetermined 1736 // attributes. 1737 const_iterator I = begin(); 1738 const_iterator EndI = end(); 1739 if (FromParent && I != EndI) 1740 ++I; 1741 if (I == EndI) 1742 return DVar; 1743 auto It = I->SharingMap.find(D); 1744 if (It != I->SharingMap.end()) { 1745 const DSAInfo &Data = It->getSecond(); 1746 DVar.RefExpr = Data.RefExpr.getPointer(); 1747 DVar.PrivateCopy = Data.PrivateCopy; 1748 DVar.CKind = Data.Attributes; 1749 DVar.ImplicitDSALoc = I->DefaultAttrLoc; 1750 DVar.DKind = I->Directive; 1751 DVar.Modifier = Data.Modifier; 1752 DVar.AppliedToPointee = Data.AppliedToPointee; 1753 } 1754 1755 return DVar; 1756 } 1757 1758 const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D, 1759 bool FromParent) const { 1760 if (isStackEmpty()) { 1761 const_iterator I; 1762 return getDSA(I, D); 1763 } 1764 D = getCanonicalDecl(D); 1765 const_iterator StartI = begin(); 1766 const_iterator EndI = end(); 1767 if (FromParent && StartI != EndI) 1768 ++StartI; 1769 return getDSA(StartI, D); 1770 } 1771 1772 const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D, 1773 unsigned Level) const { 1774 if (getStackSize() <= Level) 1775 return DSAVarData(); 1776 D = getCanonicalDecl(D); 1777 const_iterator StartI = std::next(begin(), getStackSize() - 1 - Level); 1778 return getDSA(StartI, D); 1779 } 1780 1781 const DSAStackTy::DSAVarData 1782 DSAStackTy::hasDSA(ValueDecl *D, 1783 const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred, 1784 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1785 bool FromParent) const { 1786 if (isStackEmpty()) 1787 return {}; 1788 D = getCanonicalDecl(D); 1789 const_iterator I = begin(); 1790 const_iterator EndI = end(); 1791 if (FromParent && I != EndI) 1792 ++I; 1793 for (; I != EndI; ++I) { 1794 if (!DPred(I->Directive) && 1795 !isImplicitOrExplicitTaskingRegion(I->Directive)) 1796 continue; 1797 const_iterator NewI = I; 1798 DSAVarData DVar = getDSA(NewI, D); 1799 if (I == NewI && CPred(DVar.CKind, DVar.AppliedToPointee)) 1800 return DVar; 1801 } 1802 return {}; 1803 } 1804 1805 const DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA( 1806 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred, 1807 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1808 bool FromParent) const { 1809 if (isStackEmpty()) 1810 return {}; 1811 D = getCanonicalDecl(D); 1812 const_iterator StartI = begin(); 1813 const_iterator EndI = end(); 1814 if (FromParent && StartI != EndI) 1815 ++StartI; 1816 if (StartI == EndI || !DPred(StartI->Directive)) 1817 return {}; 1818 const_iterator NewI = StartI; 1819 DSAVarData DVar = getDSA(NewI, D); 1820 return (NewI == StartI && CPred(DVar.CKind, DVar.AppliedToPointee)) 1821 ? DVar 1822 : DSAVarData(); 1823 } 1824 1825 bool DSAStackTy::hasExplicitDSA( 1826 const ValueDecl *D, 1827 const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred, 1828 unsigned Level, bool NotLastprivate) const { 1829 if (getStackSize() <= Level) 1830 return false; 1831 D = getCanonicalDecl(D); 1832 const SharingMapTy &StackElem = getStackElemAtLevel(Level); 1833 auto I = StackElem.SharingMap.find(D); 1834 if (I != StackElem.SharingMap.end() && I->getSecond().RefExpr.getPointer() && 1835 CPred(I->getSecond().Attributes, I->getSecond().AppliedToPointee) && 1836 (!NotLastprivate || !I->getSecond().RefExpr.getInt())) 1837 return true; 1838 // Check predetermined rules for the loop control variables. 1839 auto LI = StackElem.LCVMap.find(D); 1840 if (LI != StackElem.LCVMap.end()) 1841 return CPred(OMPC_private, /*AppliedToPointee=*/false); 1842 return false; 1843 } 1844 1845 bool DSAStackTy::hasExplicitDirective( 1846 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1847 unsigned Level) const { 1848 if (getStackSize() <= Level) 1849 return false; 1850 const SharingMapTy &StackElem = getStackElemAtLevel(Level); 1851 return DPred(StackElem.Directive); 1852 } 1853 1854 bool DSAStackTy::hasDirective( 1855 const llvm::function_ref<bool(OpenMPDirectiveKind, 1856 const DeclarationNameInfo &, SourceLocation)> 1857 DPred, 1858 bool FromParent) const { 1859 // We look only in the enclosing region. 1860 size_t Skip = FromParent ? 2 : 1; 1861 for (const_iterator I = begin() + std::min(Skip, getStackSize()), E = end(); 1862 I != E; ++I) { 1863 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc)) 1864 return true; 1865 } 1866 return false; 1867 } 1868 1869 void Sema::InitDataSharingAttributesStack() { 1870 VarDataSharingAttributesStack = new DSAStackTy(*this); 1871 } 1872 1873 #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack) 1874 1875 void Sema::pushOpenMPFunctionRegion() { 1876 DSAStack->pushFunction(); 1877 } 1878 1879 void Sema::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) { 1880 DSAStack->popFunction(OldFSI); 1881 } 1882 1883 static bool isOpenMPDeviceDelayedContext(Sema &S) { 1884 assert(S.LangOpts.OpenMP && S.LangOpts.OpenMPIsDevice && 1885 "Expected OpenMP device compilation."); 1886 return !S.isInOpenMPTargetExecutionDirective() && 1887 !S.isInOpenMPDeclareTargetContext(); 1888 } 1889 1890 namespace { 1891 /// Status of the function emission on the host/device. 1892 enum class FunctionEmissionStatus { 1893 Emitted, 1894 Discarded, 1895 Unknown, 1896 }; 1897 } // anonymous namespace 1898 1899 Sema::SemaDiagnosticBuilder Sema::diagIfOpenMPDeviceCode(SourceLocation Loc, 1900 unsigned DiagID) { 1901 assert(LangOpts.OpenMP && LangOpts.OpenMPIsDevice && 1902 "Expected OpenMP device compilation."); 1903 1904 FunctionDecl *FD = getCurFunctionDecl(); 1905 SemaDiagnosticBuilder::Kind Kind = SemaDiagnosticBuilder::K_Nop; 1906 if (FD) { 1907 FunctionEmissionStatus FES = getEmissionStatus(FD); 1908 switch (FES) { 1909 case FunctionEmissionStatus::Emitted: 1910 Kind = SemaDiagnosticBuilder::K_Immediate; 1911 break; 1912 case FunctionEmissionStatus::Unknown: 1913 Kind = isOpenMPDeviceDelayedContext(*this) 1914 ? SemaDiagnosticBuilder::K_Deferred 1915 : SemaDiagnosticBuilder::K_Immediate; 1916 break; 1917 case FunctionEmissionStatus::TemplateDiscarded: 1918 case FunctionEmissionStatus::OMPDiscarded: 1919 Kind = SemaDiagnosticBuilder::K_Nop; 1920 break; 1921 case FunctionEmissionStatus::CUDADiscarded: 1922 llvm_unreachable("CUDADiscarded unexpected in OpenMP device compilation"); 1923 break; 1924 } 1925 } 1926 1927 return SemaDiagnosticBuilder(Kind, Loc, DiagID, getCurFunctionDecl(), *this); 1928 } 1929 1930 Sema::SemaDiagnosticBuilder Sema::diagIfOpenMPHostCode(SourceLocation Loc, 1931 unsigned DiagID) { 1932 assert(LangOpts.OpenMP && !LangOpts.OpenMPIsDevice && 1933 "Expected OpenMP host compilation."); 1934 FunctionEmissionStatus FES = getEmissionStatus(getCurFunctionDecl()); 1935 SemaDiagnosticBuilder::Kind Kind = SemaDiagnosticBuilder::K_Nop; 1936 switch (FES) { 1937 case FunctionEmissionStatus::Emitted: 1938 Kind = SemaDiagnosticBuilder::K_Immediate; 1939 break; 1940 case FunctionEmissionStatus::Unknown: 1941 Kind = SemaDiagnosticBuilder::K_Deferred; 1942 break; 1943 case FunctionEmissionStatus::TemplateDiscarded: 1944 case FunctionEmissionStatus::OMPDiscarded: 1945 case FunctionEmissionStatus::CUDADiscarded: 1946 Kind = SemaDiagnosticBuilder::K_Nop; 1947 break; 1948 } 1949 1950 return SemaDiagnosticBuilder(Kind, Loc, DiagID, getCurFunctionDecl(), *this); 1951 } 1952 1953 static OpenMPDefaultmapClauseKind 1954 getVariableCategoryFromDecl(const LangOptions &LO, const ValueDecl *VD) { 1955 if (LO.OpenMP <= 45) { 1956 if (VD->getType().getNonReferenceType()->isScalarType()) 1957 return OMPC_DEFAULTMAP_scalar; 1958 return OMPC_DEFAULTMAP_aggregate; 1959 } 1960 if (VD->getType().getNonReferenceType()->isAnyPointerType()) 1961 return OMPC_DEFAULTMAP_pointer; 1962 if (VD->getType().getNonReferenceType()->isScalarType()) 1963 return OMPC_DEFAULTMAP_scalar; 1964 return OMPC_DEFAULTMAP_aggregate; 1965 } 1966 1967 bool Sema::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level, 1968 unsigned OpenMPCaptureLevel) const { 1969 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1970 1971 ASTContext &Ctx = getASTContext(); 1972 bool IsByRef = true; 1973 1974 // Find the directive that is associated with the provided scope. 1975 D = cast<ValueDecl>(D->getCanonicalDecl()); 1976 QualType Ty = D->getType(); 1977 1978 bool IsVariableUsedInMapClause = false; 1979 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) { 1980 // This table summarizes how a given variable should be passed to the device 1981 // given its type and the clauses where it appears. This table is based on 1982 // the description in OpenMP 4.5 [2.10.4, target Construct] and 1983 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses]. 1984 // 1985 // ========================================================================= 1986 // | type | defaultmap | pvt | first | is_device_ptr | map | res. | 1987 // | |(tofrom:scalar)| | pvt | | | | 1988 // ========================================================================= 1989 // | scl | | | | - | | bycopy| 1990 // | scl | | - | x | - | - | bycopy| 1991 // | scl | | x | - | - | - | null | 1992 // | scl | x | | | - | | byref | 1993 // | scl | x | - | x | - | - | bycopy| 1994 // | scl | x | x | - | - | - | null | 1995 // | scl | | - | - | - | x | byref | 1996 // | scl | x | - | - | - | x | byref | 1997 // 1998 // | agg | n.a. | | | - | | byref | 1999 // | agg | n.a. | - | x | - | - | byref | 2000 // | agg | n.a. | x | - | - | - | null | 2001 // | agg | n.a. | - | - | - | x | byref | 2002 // | agg | n.a. | - | - | - | x[] | byref | 2003 // 2004 // | ptr | n.a. | | | - | | bycopy| 2005 // | ptr | n.a. | - | x | - | - | bycopy| 2006 // | ptr | n.a. | x | - | - | - | null | 2007 // | ptr | n.a. | - | - | - | x | byref | 2008 // | ptr | n.a. | - | - | - | x[] | bycopy| 2009 // | ptr | n.a. | - | - | x | | bycopy| 2010 // | ptr | n.a. | - | - | x | x | bycopy| 2011 // | ptr | n.a. | - | - | x | x[] | bycopy| 2012 // ========================================================================= 2013 // Legend: 2014 // scl - scalar 2015 // ptr - pointer 2016 // agg - aggregate 2017 // x - applies 2018 // - - invalid in this combination 2019 // [] - mapped with an array section 2020 // byref - should be mapped by reference 2021 // byval - should be mapped by value 2022 // null - initialize a local variable to null on the device 2023 // 2024 // Observations: 2025 // - All scalar declarations that show up in a map clause have to be passed 2026 // by reference, because they may have been mapped in the enclosing data 2027 // environment. 2028 // - If the scalar value does not fit the size of uintptr, it has to be 2029 // passed by reference, regardless the result in the table above. 2030 // - For pointers mapped by value that have either an implicit map or an 2031 // array section, the runtime library may pass the NULL value to the 2032 // device instead of the value passed to it by the compiler. 2033 2034 if (Ty->isReferenceType()) 2035 Ty = Ty->castAs<ReferenceType>()->getPointeeType(); 2036 2037 // Locate map clauses and see if the variable being captured is referred to 2038 // in any of those clauses. Here we only care about variables, not fields, 2039 // because fields are part of aggregates. 2040 bool IsVariableAssociatedWithSection = false; 2041 2042 DSAStack->checkMappableExprComponentListsForDeclAtLevel( 2043 D, Level, 2044 [&IsVariableUsedInMapClause, &IsVariableAssociatedWithSection, D]( 2045 OMPClauseMappableExprCommon::MappableExprComponentListRef 2046 MapExprComponents, 2047 OpenMPClauseKind WhereFoundClauseKind) { 2048 // Only the map clause information influences how a variable is 2049 // captured. E.g. is_device_ptr does not require changing the default 2050 // behavior. 2051 if (WhereFoundClauseKind != OMPC_map) 2052 return false; 2053 2054 auto EI = MapExprComponents.rbegin(); 2055 auto EE = MapExprComponents.rend(); 2056 2057 assert(EI != EE && "Invalid map expression!"); 2058 2059 if (isa<DeclRefExpr>(EI->getAssociatedExpression())) 2060 IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D; 2061 2062 ++EI; 2063 if (EI == EE) 2064 return false; 2065 2066 if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) || 2067 isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) || 2068 isa<MemberExpr>(EI->getAssociatedExpression()) || 2069 isa<OMPArrayShapingExpr>(EI->getAssociatedExpression())) { 2070 IsVariableAssociatedWithSection = true; 2071 // There is nothing more we need to know about this variable. 2072 return true; 2073 } 2074 2075 // Keep looking for more map info. 2076 return false; 2077 }); 2078 2079 if (IsVariableUsedInMapClause) { 2080 // If variable is identified in a map clause it is always captured by 2081 // reference except if it is a pointer that is dereferenced somehow. 2082 IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection); 2083 } else { 2084 // By default, all the data that has a scalar type is mapped by copy 2085 // (except for reduction variables). 2086 // Defaultmap scalar is mutual exclusive to defaultmap pointer 2087 IsByRef = (DSAStack->isForceCaptureByReferenceInTargetExecutable() && 2088 !Ty->isAnyPointerType()) || 2089 !Ty->isScalarType() || 2090 DSAStack->isDefaultmapCapturedByRef( 2091 Level, getVariableCategoryFromDecl(LangOpts, D)) || 2092 DSAStack->hasExplicitDSA( 2093 D, 2094 [](OpenMPClauseKind K, bool AppliedToPointee) { 2095 return K == OMPC_reduction && !AppliedToPointee; 2096 }, 2097 Level); 2098 } 2099 } 2100 2101 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) { 2102 IsByRef = 2103 ((IsVariableUsedInMapClause && 2104 DSAStack->getCaptureRegion(Level, OpenMPCaptureLevel) == 2105 OMPD_target) || 2106 !(DSAStack->hasExplicitDSA( 2107 D, 2108 [](OpenMPClauseKind K, bool AppliedToPointee) -> bool { 2109 return K == OMPC_firstprivate || 2110 (K == OMPC_reduction && AppliedToPointee); 2111 }, 2112 Level, /*NotLastprivate=*/true) || 2113 DSAStack->isUsesAllocatorsDecl(Level, D))) && 2114 // If the variable is artificial and must be captured by value - try to 2115 // capture by value. 2116 !(isa<OMPCapturedExprDecl>(D) && !D->hasAttr<OMPCaptureNoInitAttr>() && 2117 !cast<OMPCapturedExprDecl>(D)->getInit()->isGLValue()) && 2118 // If the variable is implicitly firstprivate and scalar - capture by 2119 // copy 2120 !(DSAStack->getDefaultDSA() == DSA_firstprivate && 2121 !DSAStack->hasExplicitDSA( 2122 D, [](OpenMPClauseKind K, bool) { return K != OMPC_unknown; }, 2123 Level) && 2124 !DSAStack->isLoopControlVariable(D, Level).first); 2125 } 2126 2127 // When passing data by copy, we need to make sure it fits the uintptr size 2128 // and alignment, because the runtime library only deals with uintptr types. 2129 // If it does not fit the uintptr size, we need to pass the data by reference 2130 // instead. 2131 if (!IsByRef && 2132 (Ctx.getTypeSizeInChars(Ty) > 2133 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) || 2134 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) { 2135 IsByRef = true; 2136 } 2137 2138 return IsByRef; 2139 } 2140 2141 unsigned Sema::getOpenMPNestingLevel() const { 2142 assert(getLangOpts().OpenMP); 2143 return DSAStack->getNestingLevel(); 2144 } 2145 2146 bool Sema::isInOpenMPTargetExecutionDirective() const { 2147 return (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) && 2148 !DSAStack->isClauseParsingMode()) || 2149 DSAStack->hasDirective( 2150 [](OpenMPDirectiveKind K, const DeclarationNameInfo &, 2151 SourceLocation) -> bool { 2152 return isOpenMPTargetExecutionDirective(K); 2153 }, 2154 false); 2155 } 2156 2157 VarDecl *Sema::isOpenMPCapturedDecl(ValueDecl *D, bool CheckScopeInfo, 2158 unsigned StopAt) { 2159 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 2160 D = getCanonicalDecl(D); 2161 2162 auto *VD = dyn_cast<VarDecl>(D); 2163 // Do not capture constexpr variables. 2164 if (VD && VD->isConstexpr()) 2165 return nullptr; 2166 2167 // If we want to determine whether the variable should be captured from the 2168 // perspective of the current capturing scope, and we've already left all the 2169 // capturing scopes of the top directive on the stack, check from the 2170 // perspective of its parent directive (if any) instead. 2171 DSAStackTy::ParentDirectiveScope InParentDirectiveRAII( 2172 *DSAStack, CheckScopeInfo && DSAStack->isBodyComplete()); 2173 2174 // If we are attempting to capture a global variable in a directive with 2175 // 'target' we return true so that this global is also mapped to the device. 2176 // 2177 if (VD && !VD->hasLocalStorage() && 2178 (getCurCapturedRegion() || getCurBlock() || getCurLambda())) { 2179 if (isInOpenMPDeclareTargetContext()) { 2180 // Try to mark variable as declare target if it is used in capturing 2181 // regions. 2182 if (LangOpts.OpenMP <= 45 && 2183 !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) 2184 checkDeclIsAllowedInOpenMPTarget(nullptr, VD); 2185 return nullptr; 2186 } 2187 if (isInOpenMPTargetExecutionDirective()) { 2188 // If the declaration is enclosed in a 'declare target' directive, 2189 // then it should not be captured. 2190 // 2191 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) 2192 return nullptr; 2193 CapturedRegionScopeInfo *CSI = nullptr; 2194 for (FunctionScopeInfo *FSI : llvm::drop_begin( 2195 llvm::reverse(FunctionScopes), 2196 CheckScopeInfo ? (FunctionScopes.size() - (StopAt + 1)) : 0)) { 2197 if (!isa<CapturingScopeInfo>(FSI)) 2198 return nullptr; 2199 if (auto *RSI = dyn_cast<CapturedRegionScopeInfo>(FSI)) 2200 if (RSI->CapRegionKind == CR_OpenMP) { 2201 CSI = RSI; 2202 break; 2203 } 2204 } 2205 assert(CSI && "Failed to find CapturedRegionScopeInfo"); 2206 SmallVector<OpenMPDirectiveKind, 4> Regions; 2207 getOpenMPCaptureRegions(Regions, 2208 DSAStack->getDirective(CSI->OpenMPLevel)); 2209 if (Regions[CSI->OpenMPCaptureLevel] != OMPD_task) 2210 return VD; 2211 } 2212 } 2213 2214 if (CheckScopeInfo) { 2215 bool OpenMPFound = false; 2216 for (unsigned I = StopAt + 1; I > 0; --I) { 2217 FunctionScopeInfo *FSI = FunctionScopes[I - 1]; 2218 if(!isa<CapturingScopeInfo>(FSI)) 2219 return nullptr; 2220 if (auto *RSI = dyn_cast<CapturedRegionScopeInfo>(FSI)) 2221 if (RSI->CapRegionKind == CR_OpenMP) { 2222 OpenMPFound = true; 2223 break; 2224 } 2225 } 2226 if (!OpenMPFound) 2227 return nullptr; 2228 } 2229 2230 if (DSAStack->getCurrentDirective() != OMPD_unknown && 2231 (!DSAStack->isClauseParsingMode() || 2232 DSAStack->getParentDirective() != OMPD_unknown)) { 2233 auto &&Info = DSAStack->isLoopControlVariable(D); 2234 if (Info.first || 2235 (VD && VD->hasLocalStorage() && 2236 isImplicitOrExplicitTaskingRegion(DSAStack->getCurrentDirective())) || 2237 (VD && DSAStack->isForceVarCapturing())) 2238 return VD ? VD : Info.second; 2239 DSAStackTy::DSAVarData DVarTop = 2240 DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode()); 2241 if (DVarTop.CKind != OMPC_unknown && isOpenMPPrivate(DVarTop.CKind) && 2242 (!VD || VD->hasLocalStorage() || !DVarTop.AppliedToPointee)) 2243 return VD ? VD : cast<VarDecl>(DVarTop.PrivateCopy->getDecl()); 2244 // Threadprivate variables must not be captured. 2245 if (isOpenMPThreadPrivate(DVarTop.CKind)) 2246 return nullptr; 2247 // The variable is not private or it is the variable in the directive with 2248 // default(none) clause and not used in any clause. 2249 DSAStackTy::DSAVarData DVarPrivate = DSAStack->hasDSA( 2250 D, 2251 [](OpenMPClauseKind C, bool AppliedToPointee) { 2252 return isOpenMPPrivate(C) && !AppliedToPointee; 2253 }, 2254 [](OpenMPDirectiveKind) { return true; }, 2255 DSAStack->isClauseParsingMode()); 2256 // Global shared must not be captured. 2257 if (VD && !VD->hasLocalStorage() && DVarPrivate.CKind == OMPC_unknown && 2258 ((DSAStack->getDefaultDSA() != DSA_none && 2259 DSAStack->getDefaultDSA() != DSA_firstprivate) || 2260 DVarTop.CKind == OMPC_shared)) 2261 return nullptr; 2262 if (DVarPrivate.CKind != OMPC_unknown || 2263 (VD && (DSAStack->getDefaultDSA() == DSA_none || 2264 DSAStack->getDefaultDSA() == DSA_firstprivate))) 2265 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl()); 2266 } 2267 return nullptr; 2268 } 2269 2270 void Sema::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex, 2271 unsigned Level) const { 2272 FunctionScopesIndex -= getOpenMPCaptureLevels(DSAStack->getDirective(Level)); 2273 } 2274 2275 void Sema::startOpenMPLoop() { 2276 assert(LangOpts.OpenMP && "OpenMP must be enabled."); 2277 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) 2278 DSAStack->loopInit(); 2279 } 2280 2281 void Sema::startOpenMPCXXRangeFor() { 2282 assert(LangOpts.OpenMP && "OpenMP must be enabled."); 2283 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { 2284 DSAStack->resetPossibleLoopCounter(); 2285 DSAStack->loopStart(); 2286 } 2287 } 2288 2289 OpenMPClauseKind Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level, 2290 unsigned CapLevel) const { 2291 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 2292 if (DSAStack->hasExplicitDirective( 2293 [](OpenMPDirectiveKind K) { return isOpenMPTaskingDirective(K); }, 2294 Level)) { 2295 bool IsTriviallyCopyable = 2296 D->getType().getNonReferenceType().isTriviallyCopyableType(Context) && 2297 !D->getType() 2298 .getNonReferenceType() 2299 .getCanonicalType() 2300 ->getAsCXXRecordDecl(); 2301 OpenMPDirectiveKind DKind = DSAStack->getDirective(Level); 2302 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 2303 getOpenMPCaptureRegions(CaptureRegions, DKind); 2304 if (isOpenMPTaskingDirective(CaptureRegions[CapLevel]) && 2305 (IsTriviallyCopyable || 2306 !isOpenMPTaskLoopDirective(CaptureRegions[CapLevel]))) { 2307 if (DSAStack->hasExplicitDSA( 2308 D, 2309 [](OpenMPClauseKind K, bool) { return K == OMPC_firstprivate; }, 2310 Level, /*NotLastprivate=*/true)) 2311 return OMPC_firstprivate; 2312 DSAStackTy::DSAVarData DVar = DSAStack->getImplicitDSA(D, Level); 2313 if (DVar.CKind != OMPC_shared && 2314 !DSAStack->isLoopControlVariable(D, Level).first && !DVar.RefExpr) { 2315 DSAStack->addImplicitTaskFirstprivate(Level, D); 2316 return OMPC_firstprivate; 2317 } 2318 } 2319 } 2320 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { 2321 if (DSAStack->getAssociatedLoops() > 0 && 2322 !DSAStack->isLoopStarted()) { 2323 DSAStack->resetPossibleLoopCounter(D); 2324 DSAStack->loopStart(); 2325 return OMPC_private; 2326 } 2327 if ((DSAStack->getPossiblyLoopCunter() == D->getCanonicalDecl() || 2328 DSAStack->isLoopControlVariable(D).first) && 2329 !DSAStack->hasExplicitDSA( 2330 D, [](OpenMPClauseKind K, bool) { return K != OMPC_private; }, 2331 Level) && 2332 !isOpenMPSimdDirective(DSAStack->getCurrentDirective())) 2333 return OMPC_private; 2334 } 2335 if (const auto *VD = dyn_cast<VarDecl>(D)) { 2336 if (DSAStack->isThreadPrivate(const_cast<VarDecl *>(VD)) && 2337 DSAStack->isForceVarCapturing() && 2338 !DSAStack->hasExplicitDSA( 2339 D, [](OpenMPClauseKind K, bool) { return K == OMPC_copyin; }, 2340 Level)) 2341 return OMPC_private; 2342 } 2343 // User-defined allocators are private since they must be defined in the 2344 // context of target region. 2345 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level) && 2346 DSAStack->isUsesAllocatorsDecl(Level, D).getValueOr( 2347 DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait) == 2348 DSAStackTy::UsesAllocatorsDeclKind::UserDefinedAllocator) 2349 return OMPC_private; 2350 return (DSAStack->hasExplicitDSA( 2351 D, [](OpenMPClauseKind K, bool) { return K == OMPC_private; }, 2352 Level) || 2353 (DSAStack->isClauseParsingMode() && 2354 DSAStack->getClauseParsingMode() == OMPC_private) || 2355 // Consider taskgroup reduction descriptor variable a private 2356 // to avoid possible capture in the region. 2357 (DSAStack->hasExplicitDirective( 2358 [](OpenMPDirectiveKind K) { 2359 return K == OMPD_taskgroup || 2360 ((isOpenMPParallelDirective(K) || 2361 isOpenMPWorksharingDirective(K)) && 2362 !isOpenMPSimdDirective(K)); 2363 }, 2364 Level) && 2365 DSAStack->isTaskgroupReductionRef(D, Level))) 2366 ? OMPC_private 2367 : OMPC_unknown; 2368 } 2369 2370 void Sema::setOpenMPCaptureKind(FieldDecl *FD, const ValueDecl *D, 2371 unsigned Level) { 2372 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 2373 D = getCanonicalDecl(D); 2374 OpenMPClauseKind OMPC = OMPC_unknown; 2375 for (unsigned I = DSAStack->getNestingLevel() + 1; I > Level; --I) { 2376 const unsigned NewLevel = I - 1; 2377 if (DSAStack->hasExplicitDSA( 2378 D, 2379 [&OMPC](const OpenMPClauseKind K, bool AppliedToPointee) { 2380 if (isOpenMPPrivate(K) && !AppliedToPointee) { 2381 OMPC = K; 2382 return true; 2383 } 2384 return false; 2385 }, 2386 NewLevel)) 2387 break; 2388 if (DSAStack->checkMappableExprComponentListsForDeclAtLevel( 2389 D, NewLevel, 2390 [](OMPClauseMappableExprCommon::MappableExprComponentListRef, 2391 OpenMPClauseKind) { return true; })) { 2392 OMPC = OMPC_map; 2393 break; 2394 } 2395 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, 2396 NewLevel)) { 2397 OMPC = OMPC_map; 2398 if (DSAStack->mustBeFirstprivateAtLevel( 2399 NewLevel, getVariableCategoryFromDecl(LangOpts, D))) 2400 OMPC = OMPC_firstprivate; 2401 break; 2402 } 2403 } 2404 if (OMPC != OMPC_unknown) 2405 FD->addAttr(OMPCaptureKindAttr::CreateImplicit(Context, unsigned(OMPC))); 2406 } 2407 2408 bool Sema::isOpenMPTargetCapturedDecl(const ValueDecl *D, unsigned Level, 2409 unsigned CaptureLevel) const { 2410 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 2411 // Return true if the current level is no longer enclosed in a target region. 2412 2413 SmallVector<OpenMPDirectiveKind, 4> Regions; 2414 getOpenMPCaptureRegions(Regions, DSAStack->getDirective(Level)); 2415 const auto *VD = dyn_cast<VarDecl>(D); 2416 return VD && !VD->hasLocalStorage() && 2417 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, 2418 Level) && 2419 Regions[CaptureLevel] != OMPD_task; 2420 } 2421 2422 bool Sema::isOpenMPGlobalCapturedDecl(ValueDecl *D, unsigned Level, 2423 unsigned CaptureLevel) const { 2424 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 2425 // Return true if the current level is no longer enclosed in a target region. 2426 2427 if (const auto *VD = dyn_cast<VarDecl>(D)) { 2428 if (!VD->hasLocalStorage()) { 2429 if (isInOpenMPTargetExecutionDirective()) 2430 return true; 2431 DSAStackTy::DSAVarData TopDVar = 2432 DSAStack->getTopDSA(D, /*FromParent=*/false); 2433 unsigned NumLevels = 2434 getOpenMPCaptureLevels(DSAStack->getDirective(Level)); 2435 if (Level == 0) 2436 return (NumLevels == CaptureLevel + 1) && TopDVar.CKind != OMPC_shared; 2437 do { 2438 --Level; 2439 DSAStackTy::DSAVarData DVar = DSAStack->getImplicitDSA(D, Level); 2440 if (DVar.CKind != OMPC_shared) 2441 return true; 2442 } while (Level > 0); 2443 } 2444 } 2445 return true; 2446 } 2447 2448 void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; } 2449 2450 void Sema::ActOnOpenMPBeginDeclareVariant(SourceLocation Loc, 2451 OMPTraitInfo &TI) { 2452 OMPDeclareVariantScopes.push_back(OMPDeclareVariantScope(TI)); 2453 } 2454 2455 void Sema::ActOnOpenMPEndDeclareVariant() { 2456 assert(isInOpenMPDeclareVariantScope() && 2457 "Not in OpenMP declare variant scope!"); 2458 2459 OMPDeclareVariantScopes.pop_back(); 2460 } 2461 2462 void Sema::finalizeOpenMPDelayedAnalysis(const FunctionDecl *Caller, 2463 const FunctionDecl *Callee, 2464 SourceLocation Loc) { 2465 assert(LangOpts.OpenMP && "Expected OpenMP compilation mode."); 2466 Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy = 2467 OMPDeclareTargetDeclAttr::getDeviceType(Caller->getMostRecentDecl()); 2468 // Ignore host functions during device analyzis. 2469 if (LangOpts.OpenMPIsDevice && DevTy && 2470 *DevTy == OMPDeclareTargetDeclAttr::DT_Host) 2471 return; 2472 // Ignore nohost functions during host analyzis. 2473 if (!LangOpts.OpenMPIsDevice && DevTy && 2474 *DevTy == OMPDeclareTargetDeclAttr::DT_NoHost) 2475 return; 2476 const FunctionDecl *FD = Callee->getMostRecentDecl(); 2477 DevTy = OMPDeclareTargetDeclAttr::getDeviceType(FD); 2478 if (LangOpts.OpenMPIsDevice && DevTy && 2479 *DevTy == OMPDeclareTargetDeclAttr::DT_Host) { 2480 // Diagnose host function called during device codegen. 2481 StringRef HostDevTy = 2482 getOpenMPSimpleClauseTypeName(OMPC_device_type, OMPC_DEVICE_TYPE_host); 2483 Diag(Loc, diag::err_omp_wrong_device_function_call) << HostDevTy << 0; 2484 Diag(*OMPDeclareTargetDeclAttr::getLocation(FD), 2485 diag::note_omp_marked_device_type_here) 2486 << HostDevTy; 2487 return; 2488 } 2489 if (!LangOpts.OpenMPIsDevice && DevTy && 2490 *DevTy == OMPDeclareTargetDeclAttr::DT_NoHost) { 2491 // Diagnose nohost function called during host codegen. 2492 StringRef NoHostDevTy = getOpenMPSimpleClauseTypeName( 2493 OMPC_device_type, OMPC_DEVICE_TYPE_nohost); 2494 Diag(Loc, diag::err_omp_wrong_device_function_call) << NoHostDevTy << 1; 2495 Diag(*OMPDeclareTargetDeclAttr::getLocation(FD), 2496 diag::note_omp_marked_device_type_here) 2497 << NoHostDevTy; 2498 } 2499 } 2500 2501 void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind, 2502 const DeclarationNameInfo &DirName, 2503 Scope *CurScope, SourceLocation Loc) { 2504 DSAStack->push(DKind, DirName, CurScope, Loc); 2505 PushExpressionEvaluationContext( 2506 ExpressionEvaluationContext::PotentiallyEvaluated); 2507 } 2508 2509 void Sema::StartOpenMPClause(OpenMPClauseKind K) { 2510 DSAStack->setClauseParsingMode(K); 2511 } 2512 2513 void Sema::EndOpenMPClause() { 2514 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown); 2515 } 2516 2517 static std::pair<ValueDecl *, bool> 2518 getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc, 2519 SourceRange &ERange, bool AllowArraySection = false); 2520 2521 /// Check consistency of the reduction clauses. 2522 static void checkReductionClauses(Sema &S, DSAStackTy *Stack, 2523 ArrayRef<OMPClause *> Clauses) { 2524 bool InscanFound = false; 2525 SourceLocation InscanLoc; 2526 // OpenMP 5.0, 2.19.5.4 reduction Clause, Restrictions. 2527 // A reduction clause without the inscan reduction-modifier may not appear on 2528 // a construct on which a reduction clause with the inscan reduction-modifier 2529 // appears. 2530 for (OMPClause *C : Clauses) { 2531 if (C->getClauseKind() != OMPC_reduction) 2532 continue; 2533 auto *RC = cast<OMPReductionClause>(C); 2534 if (RC->getModifier() == OMPC_REDUCTION_inscan) { 2535 InscanFound = true; 2536 InscanLoc = RC->getModifierLoc(); 2537 continue; 2538 } 2539 if (RC->getModifier() == OMPC_REDUCTION_task) { 2540 // OpenMP 5.0, 2.19.5.4 reduction Clause. 2541 // A reduction clause with the task reduction-modifier may only appear on 2542 // a parallel construct, a worksharing construct or a combined or 2543 // composite construct for which any of the aforementioned constructs is a 2544 // constituent construct and simd or loop are not constituent constructs. 2545 OpenMPDirectiveKind CurDir = Stack->getCurrentDirective(); 2546 if (!(isOpenMPParallelDirective(CurDir) || 2547 isOpenMPWorksharingDirective(CurDir)) || 2548 isOpenMPSimdDirective(CurDir)) 2549 S.Diag(RC->getModifierLoc(), 2550 diag::err_omp_reduction_task_not_parallel_or_worksharing); 2551 continue; 2552 } 2553 } 2554 if (InscanFound) { 2555 for (OMPClause *C : Clauses) { 2556 if (C->getClauseKind() != OMPC_reduction) 2557 continue; 2558 auto *RC = cast<OMPReductionClause>(C); 2559 if (RC->getModifier() != OMPC_REDUCTION_inscan) { 2560 S.Diag(RC->getModifier() == OMPC_REDUCTION_unknown 2561 ? RC->getBeginLoc() 2562 : RC->getModifierLoc(), 2563 diag::err_omp_inscan_reduction_expected); 2564 S.Diag(InscanLoc, diag::note_omp_previous_inscan_reduction); 2565 continue; 2566 } 2567 for (Expr *Ref : RC->varlists()) { 2568 assert(Ref && "NULL expr in OpenMP nontemporal clause."); 2569 SourceLocation ELoc; 2570 SourceRange ERange; 2571 Expr *SimpleRefExpr = Ref; 2572 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange, 2573 /*AllowArraySection=*/true); 2574 ValueDecl *D = Res.first; 2575 if (!D) 2576 continue; 2577 if (!Stack->isUsedInScanDirective(getCanonicalDecl(D))) { 2578 S.Diag(Ref->getExprLoc(), 2579 diag::err_omp_reduction_not_inclusive_exclusive) 2580 << Ref->getSourceRange(); 2581 } 2582 } 2583 } 2584 } 2585 } 2586 2587 static void checkAllocateClauses(Sema &S, DSAStackTy *Stack, 2588 ArrayRef<OMPClause *> Clauses); 2589 static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr, 2590 bool WithInit); 2591 2592 static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack, 2593 const ValueDecl *D, 2594 const DSAStackTy::DSAVarData &DVar, 2595 bool IsLoopIterVar = false); 2596 2597 void Sema::EndOpenMPDSABlock(Stmt *CurDirective) { 2598 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1] 2599 // A variable of class type (or array thereof) that appears in a lastprivate 2600 // clause requires an accessible, unambiguous default constructor for the 2601 // class type, unless the list item is also specified in a firstprivate 2602 // clause. 2603 if (const auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) { 2604 for (OMPClause *C : D->clauses()) { 2605 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) { 2606 SmallVector<Expr *, 8> PrivateCopies; 2607 for (Expr *DE : Clause->varlists()) { 2608 if (DE->isValueDependent() || DE->isTypeDependent()) { 2609 PrivateCopies.push_back(nullptr); 2610 continue; 2611 } 2612 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens()); 2613 auto *VD = cast<VarDecl>(DRE->getDecl()); 2614 QualType Type = VD->getType().getNonReferenceType(); 2615 const DSAStackTy::DSAVarData DVar = 2616 DSAStack->getTopDSA(VD, /*FromParent=*/false); 2617 if (DVar.CKind == OMPC_lastprivate) { 2618 // Generate helper private variable and initialize it with the 2619 // default value. The address of the original variable is replaced 2620 // by the address of the new private variable in CodeGen. This new 2621 // variable is not added to IdResolver, so the code in the OpenMP 2622 // region uses original variable for proper diagnostics. 2623 VarDecl *VDPrivate = buildVarDecl( 2624 *this, DE->getExprLoc(), Type.getUnqualifiedType(), 2625 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr, DRE); 2626 ActOnUninitializedDecl(VDPrivate); 2627 if (VDPrivate->isInvalidDecl()) { 2628 PrivateCopies.push_back(nullptr); 2629 continue; 2630 } 2631 PrivateCopies.push_back(buildDeclRefExpr( 2632 *this, VDPrivate, DE->getType(), DE->getExprLoc())); 2633 } else { 2634 // The variable is also a firstprivate, so initialization sequence 2635 // for private copy is generated already. 2636 PrivateCopies.push_back(nullptr); 2637 } 2638 } 2639 Clause->setPrivateCopies(PrivateCopies); 2640 continue; 2641 } 2642 // Finalize nontemporal clause by handling private copies, if any. 2643 if (auto *Clause = dyn_cast<OMPNontemporalClause>(C)) { 2644 SmallVector<Expr *, 8> PrivateRefs; 2645 for (Expr *RefExpr : Clause->varlists()) { 2646 assert(RefExpr && "NULL expr in OpenMP nontemporal clause."); 2647 SourceLocation ELoc; 2648 SourceRange ERange; 2649 Expr *SimpleRefExpr = RefExpr; 2650 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 2651 if (Res.second) 2652 // It will be analyzed later. 2653 PrivateRefs.push_back(RefExpr); 2654 ValueDecl *D = Res.first; 2655 if (!D) 2656 continue; 2657 2658 const DSAStackTy::DSAVarData DVar = 2659 DSAStack->getTopDSA(D, /*FromParent=*/false); 2660 PrivateRefs.push_back(DVar.PrivateCopy ? DVar.PrivateCopy 2661 : SimpleRefExpr); 2662 } 2663 Clause->setPrivateRefs(PrivateRefs); 2664 continue; 2665 } 2666 if (auto *Clause = dyn_cast<OMPUsesAllocatorsClause>(C)) { 2667 for (unsigned I = 0, E = Clause->getNumberOfAllocators(); I < E; ++I) { 2668 OMPUsesAllocatorsClause::Data D = Clause->getAllocatorData(I); 2669 auto *DRE = dyn_cast<DeclRefExpr>(D.Allocator->IgnoreParenImpCasts()); 2670 if (!DRE) 2671 continue; 2672 ValueDecl *VD = DRE->getDecl(); 2673 if (!VD || !isa<VarDecl>(VD)) 2674 continue; 2675 DSAStackTy::DSAVarData DVar = 2676 DSAStack->getTopDSA(VD, /*FromParent=*/false); 2677 // OpenMP [2.12.5, target Construct] 2678 // Memory allocators that appear in a uses_allocators clause cannot 2679 // appear in other data-sharing attribute clauses or data-mapping 2680 // attribute clauses in the same construct. 2681 Expr *MapExpr = nullptr; 2682 if (DVar.RefExpr || 2683 DSAStack->checkMappableExprComponentListsForDecl( 2684 VD, /*CurrentRegionOnly=*/true, 2685 [VD, &MapExpr]( 2686 OMPClauseMappableExprCommon::MappableExprComponentListRef 2687 MapExprComponents, 2688 OpenMPClauseKind C) { 2689 auto MI = MapExprComponents.rbegin(); 2690 auto ME = MapExprComponents.rend(); 2691 if (MI != ME && 2692 MI->getAssociatedDeclaration()->getCanonicalDecl() == 2693 VD->getCanonicalDecl()) { 2694 MapExpr = MI->getAssociatedExpression(); 2695 return true; 2696 } 2697 return false; 2698 })) { 2699 Diag(D.Allocator->getExprLoc(), 2700 diag::err_omp_allocator_used_in_clauses) 2701 << D.Allocator->getSourceRange(); 2702 if (DVar.RefExpr) 2703 reportOriginalDsa(*this, DSAStack, VD, DVar); 2704 else 2705 Diag(MapExpr->getExprLoc(), diag::note_used_here) 2706 << MapExpr->getSourceRange(); 2707 } 2708 } 2709 continue; 2710 } 2711 } 2712 // Check allocate clauses. 2713 if (!CurContext->isDependentContext()) 2714 checkAllocateClauses(*this, DSAStack, D->clauses()); 2715 checkReductionClauses(*this, DSAStack, D->clauses()); 2716 } 2717 2718 DSAStack->pop(); 2719 DiscardCleanupsInEvaluationContext(); 2720 PopExpressionEvaluationContext(); 2721 } 2722 2723 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, 2724 Expr *NumIterations, Sema &SemaRef, 2725 Scope *S, DSAStackTy *Stack); 2726 2727 namespace { 2728 2729 class VarDeclFilterCCC final : public CorrectionCandidateCallback { 2730 private: 2731 Sema &SemaRef; 2732 2733 public: 2734 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {} 2735 bool ValidateCandidate(const TypoCorrection &Candidate) override { 2736 NamedDecl *ND = Candidate.getCorrectionDecl(); 2737 if (const auto *VD = dyn_cast_or_null<VarDecl>(ND)) { 2738 return VD->hasGlobalStorage() && 2739 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), 2740 SemaRef.getCurScope()); 2741 } 2742 return false; 2743 } 2744 2745 std::unique_ptr<CorrectionCandidateCallback> clone() override { 2746 return std::make_unique<VarDeclFilterCCC>(*this); 2747 } 2748 2749 }; 2750 2751 class VarOrFuncDeclFilterCCC final : public CorrectionCandidateCallback { 2752 private: 2753 Sema &SemaRef; 2754 2755 public: 2756 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {} 2757 bool ValidateCandidate(const TypoCorrection &Candidate) override { 2758 NamedDecl *ND = Candidate.getCorrectionDecl(); 2759 if (ND && ((isa<VarDecl>(ND) && ND->getKind() == Decl::Var) || 2760 isa<FunctionDecl>(ND))) { 2761 return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), 2762 SemaRef.getCurScope()); 2763 } 2764 return false; 2765 } 2766 2767 std::unique_ptr<CorrectionCandidateCallback> clone() override { 2768 return std::make_unique<VarOrFuncDeclFilterCCC>(*this); 2769 } 2770 }; 2771 2772 } // namespace 2773 2774 ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope, 2775 CXXScopeSpec &ScopeSpec, 2776 const DeclarationNameInfo &Id, 2777 OpenMPDirectiveKind Kind) { 2778 LookupResult Lookup(*this, Id, LookupOrdinaryName); 2779 LookupParsedName(Lookup, CurScope, &ScopeSpec, true); 2780 2781 if (Lookup.isAmbiguous()) 2782 return ExprError(); 2783 2784 VarDecl *VD; 2785 if (!Lookup.isSingleResult()) { 2786 VarDeclFilterCCC CCC(*this); 2787 if (TypoCorrection Corrected = 2788 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, CCC, 2789 CTK_ErrorRecovery)) { 2790 diagnoseTypo(Corrected, 2791 PDiag(Lookup.empty() 2792 ? diag::err_undeclared_var_use_suggest 2793 : diag::err_omp_expected_var_arg_suggest) 2794 << Id.getName()); 2795 VD = Corrected.getCorrectionDeclAs<VarDecl>(); 2796 } else { 2797 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use 2798 : diag::err_omp_expected_var_arg) 2799 << Id.getName(); 2800 return ExprError(); 2801 } 2802 } else if (!(VD = Lookup.getAsSingle<VarDecl>())) { 2803 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName(); 2804 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at); 2805 return ExprError(); 2806 } 2807 Lookup.suppressDiagnostics(); 2808 2809 // OpenMP [2.9.2, Syntax, C/C++] 2810 // Variables must be file-scope, namespace-scope, or static block-scope. 2811 if (Kind == OMPD_threadprivate && !VD->hasGlobalStorage()) { 2812 Diag(Id.getLoc(), diag::err_omp_global_var_arg) 2813 << getOpenMPDirectiveName(Kind) << !VD->isStaticLocal(); 2814 bool IsDecl = 2815 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2816 Diag(VD->getLocation(), 2817 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2818 << VD; 2819 return ExprError(); 2820 } 2821 2822 VarDecl *CanonicalVD = VD->getCanonicalDecl(); 2823 NamedDecl *ND = CanonicalVD; 2824 // OpenMP [2.9.2, Restrictions, C/C++, p.2] 2825 // A threadprivate directive for file-scope variables must appear outside 2826 // any definition or declaration. 2827 if (CanonicalVD->getDeclContext()->isTranslationUnit() && 2828 !getCurLexicalContext()->isTranslationUnit()) { 2829 Diag(Id.getLoc(), diag::err_omp_var_scope) 2830 << getOpenMPDirectiveName(Kind) << VD; 2831 bool IsDecl = 2832 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2833 Diag(VD->getLocation(), 2834 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2835 << VD; 2836 return ExprError(); 2837 } 2838 // OpenMP [2.9.2, Restrictions, C/C++, p.3] 2839 // A threadprivate directive for static class member variables must appear 2840 // in the class definition, in the same scope in which the member 2841 // variables are declared. 2842 if (CanonicalVD->isStaticDataMember() && 2843 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) { 2844 Diag(Id.getLoc(), diag::err_omp_var_scope) 2845 << getOpenMPDirectiveName(Kind) << VD; 2846 bool IsDecl = 2847 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2848 Diag(VD->getLocation(), 2849 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2850 << VD; 2851 return ExprError(); 2852 } 2853 // OpenMP [2.9.2, Restrictions, C/C++, p.4] 2854 // A threadprivate directive for namespace-scope variables must appear 2855 // outside any definition or declaration other than the namespace 2856 // definition itself. 2857 if (CanonicalVD->getDeclContext()->isNamespace() && 2858 (!getCurLexicalContext()->isFileContext() || 2859 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) { 2860 Diag(Id.getLoc(), diag::err_omp_var_scope) 2861 << getOpenMPDirectiveName(Kind) << VD; 2862 bool IsDecl = 2863 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2864 Diag(VD->getLocation(), 2865 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2866 << VD; 2867 return ExprError(); 2868 } 2869 // OpenMP [2.9.2, Restrictions, C/C++, p.6] 2870 // A threadprivate directive for static block-scope variables must appear 2871 // in the scope of the variable and not in a nested scope. 2872 if (CanonicalVD->isLocalVarDecl() && CurScope && 2873 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) { 2874 Diag(Id.getLoc(), diag::err_omp_var_scope) 2875 << getOpenMPDirectiveName(Kind) << VD; 2876 bool IsDecl = 2877 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2878 Diag(VD->getLocation(), 2879 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2880 << VD; 2881 return ExprError(); 2882 } 2883 2884 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6] 2885 // A threadprivate directive must lexically precede all references to any 2886 // of the variables in its list. 2887 if (Kind == OMPD_threadprivate && VD->isUsed() && 2888 !DSAStack->isThreadPrivate(VD)) { 2889 Diag(Id.getLoc(), diag::err_omp_var_used) 2890 << getOpenMPDirectiveName(Kind) << VD; 2891 return ExprError(); 2892 } 2893 2894 QualType ExprType = VD->getType().getNonReferenceType(); 2895 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(), 2896 SourceLocation(), VD, 2897 /*RefersToEnclosingVariableOrCapture=*/false, 2898 Id.getLoc(), ExprType, VK_LValue); 2899 } 2900 2901 Sema::DeclGroupPtrTy 2902 Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc, 2903 ArrayRef<Expr *> VarList) { 2904 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) { 2905 CurContext->addDecl(D); 2906 return DeclGroupPtrTy::make(DeclGroupRef(D)); 2907 } 2908 return nullptr; 2909 } 2910 2911 namespace { 2912 class LocalVarRefChecker final 2913 : public ConstStmtVisitor<LocalVarRefChecker, bool> { 2914 Sema &SemaRef; 2915 2916 public: 2917 bool VisitDeclRefExpr(const DeclRefExpr *E) { 2918 if (const auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 2919 if (VD->hasLocalStorage()) { 2920 SemaRef.Diag(E->getBeginLoc(), 2921 diag::err_omp_local_var_in_threadprivate_init) 2922 << E->getSourceRange(); 2923 SemaRef.Diag(VD->getLocation(), diag::note_defined_here) 2924 << VD << VD->getSourceRange(); 2925 return true; 2926 } 2927 } 2928 return false; 2929 } 2930 bool VisitStmt(const Stmt *S) { 2931 for (const Stmt *Child : S->children()) { 2932 if (Child && Visit(Child)) 2933 return true; 2934 } 2935 return false; 2936 } 2937 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {} 2938 }; 2939 } // namespace 2940 2941 OMPThreadPrivateDecl * 2942 Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) { 2943 SmallVector<Expr *, 8> Vars; 2944 for (Expr *RefExpr : VarList) { 2945 auto *DE = cast<DeclRefExpr>(RefExpr); 2946 auto *VD = cast<VarDecl>(DE->getDecl()); 2947 SourceLocation ILoc = DE->getExprLoc(); 2948 2949 // Mark variable as used. 2950 VD->setReferenced(); 2951 VD->markUsed(Context); 2952 2953 QualType QType = VD->getType(); 2954 if (QType->isDependentType() || QType->isInstantiationDependentType()) { 2955 // It will be analyzed later. 2956 Vars.push_back(DE); 2957 continue; 2958 } 2959 2960 // OpenMP [2.9.2, Restrictions, C/C++, p.10] 2961 // A threadprivate variable must not have an incomplete type. 2962 if (RequireCompleteType(ILoc, VD->getType(), 2963 diag::err_omp_threadprivate_incomplete_type)) { 2964 continue; 2965 } 2966 2967 // OpenMP [2.9.2, Restrictions, C/C++, p.10] 2968 // A threadprivate variable must not have a reference type. 2969 if (VD->getType()->isReferenceType()) { 2970 Diag(ILoc, diag::err_omp_ref_type_arg) 2971 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType(); 2972 bool IsDecl = 2973 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2974 Diag(VD->getLocation(), 2975 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2976 << VD; 2977 continue; 2978 } 2979 2980 // Check if this is a TLS variable. If TLS is not being supported, produce 2981 // the corresponding diagnostic. 2982 if ((VD->getTLSKind() != VarDecl::TLS_None && 2983 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() && 2984 getLangOpts().OpenMPUseTLS && 2985 getASTContext().getTargetInfo().isTLSSupported())) || 2986 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() && 2987 !VD->isLocalVarDecl())) { 2988 Diag(ILoc, diag::err_omp_var_thread_local) 2989 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1); 2990 bool IsDecl = 2991 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2992 Diag(VD->getLocation(), 2993 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2994 << VD; 2995 continue; 2996 } 2997 2998 // Check if initial value of threadprivate variable reference variable with 2999 // local storage (it is not supported by runtime). 3000 if (const Expr *Init = VD->getAnyInitializer()) { 3001 LocalVarRefChecker Checker(*this); 3002 if (Checker.Visit(Init)) 3003 continue; 3004 } 3005 3006 Vars.push_back(RefExpr); 3007 DSAStack->addDSA(VD, DE, OMPC_threadprivate); 3008 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit( 3009 Context, SourceRange(Loc, Loc))); 3010 if (ASTMutationListener *ML = Context.getASTMutationListener()) 3011 ML->DeclarationMarkedOpenMPThreadPrivate(VD); 3012 } 3013 OMPThreadPrivateDecl *D = nullptr; 3014 if (!Vars.empty()) { 3015 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc, 3016 Vars); 3017 D->setAccess(AS_public); 3018 } 3019 return D; 3020 } 3021 3022 static OMPAllocateDeclAttr::AllocatorTypeTy 3023 getAllocatorKind(Sema &S, DSAStackTy *Stack, Expr *Allocator) { 3024 if (!Allocator) 3025 return OMPAllocateDeclAttr::OMPNullMemAlloc; 3026 if (Allocator->isTypeDependent() || Allocator->isValueDependent() || 3027 Allocator->isInstantiationDependent() || 3028 Allocator->containsUnexpandedParameterPack()) 3029 return OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; 3030 auto AllocatorKindRes = OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; 3031 const Expr *AE = Allocator->IgnoreParenImpCasts(); 3032 for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) { 3033 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I); 3034 const Expr *DefAllocator = Stack->getAllocator(AllocatorKind); 3035 llvm::FoldingSetNodeID AEId, DAEId; 3036 AE->Profile(AEId, S.getASTContext(), /*Canonical=*/true); 3037 DefAllocator->Profile(DAEId, S.getASTContext(), /*Canonical=*/true); 3038 if (AEId == DAEId) { 3039 AllocatorKindRes = AllocatorKind; 3040 break; 3041 } 3042 } 3043 return AllocatorKindRes; 3044 } 3045 3046 static bool checkPreviousOMPAllocateAttribute( 3047 Sema &S, DSAStackTy *Stack, Expr *RefExpr, VarDecl *VD, 3048 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, Expr *Allocator) { 3049 if (!VD->hasAttr<OMPAllocateDeclAttr>()) 3050 return false; 3051 const auto *A = VD->getAttr<OMPAllocateDeclAttr>(); 3052 Expr *PrevAllocator = A->getAllocator(); 3053 OMPAllocateDeclAttr::AllocatorTypeTy PrevAllocatorKind = 3054 getAllocatorKind(S, Stack, PrevAllocator); 3055 bool AllocatorsMatch = AllocatorKind == PrevAllocatorKind; 3056 if (AllocatorsMatch && 3057 AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc && 3058 Allocator && PrevAllocator) { 3059 const Expr *AE = Allocator->IgnoreParenImpCasts(); 3060 const Expr *PAE = PrevAllocator->IgnoreParenImpCasts(); 3061 llvm::FoldingSetNodeID AEId, PAEId; 3062 AE->Profile(AEId, S.Context, /*Canonical=*/true); 3063 PAE->Profile(PAEId, S.Context, /*Canonical=*/true); 3064 AllocatorsMatch = AEId == PAEId; 3065 } 3066 if (!AllocatorsMatch) { 3067 SmallString<256> AllocatorBuffer; 3068 llvm::raw_svector_ostream AllocatorStream(AllocatorBuffer); 3069 if (Allocator) 3070 Allocator->printPretty(AllocatorStream, nullptr, S.getPrintingPolicy()); 3071 SmallString<256> PrevAllocatorBuffer; 3072 llvm::raw_svector_ostream PrevAllocatorStream(PrevAllocatorBuffer); 3073 if (PrevAllocator) 3074 PrevAllocator->printPretty(PrevAllocatorStream, nullptr, 3075 S.getPrintingPolicy()); 3076 3077 SourceLocation AllocatorLoc = 3078 Allocator ? Allocator->getExprLoc() : RefExpr->getExprLoc(); 3079 SourceRange AllocatorRange = 3080 Allocator ? Allocator->getSourceRange() : RefExpr->getSourceRange(); 3081 SourceLocation PrevAllocatorLoc = 3082 PrevAllocator ? PrevAllocator->getExprLoc() : A->getLocation(); 3083 SourceRange PrevAllocatorRange = 3084 PrevAllocator ? PrevAllocator->getSourceRange() : A->getRange(); 3085 S.Diag(AllocatorLoc, diag::warn_omp_used_different_allocator) 3086 << (Allocator ? 1 : 0) << AllocatorStream.str() 3087 << (PrevAllocator ? 1 : 0) << PrevAllocatorStream.str() 3088 << AllocatorRange; 3089 S.Diag(PrevAllocatorLoc, diag::note_omp_previous_allocator) 3090 << PrevAllocatorRange; 3091 return true; 3092 } 3093 return false; 3094 } 3095 3096 static void 3097 applyOMPAllocateAttribute(Sema &S, VarDecl *VD, 3098 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, 3099 Expr *Allocator, SourceRange SR) { 3100 if (VD->hasAttr<OMPAllocateDeclAttr>()) 3101 return; 3102 if (Allocator && 3103 (Allocator->isTypeDependent() || Allocator->isValueDependent() || 3104 Allocator->isInstantiationDependent() || 3105 Allocator->containsUnexpandedParameterPack())) 3106 return; 3107 auto *A = OMPAllocateDeclAttr::CreateImplicit(S.Context, AllocatorKind, 3108 Allocator, SR); 3109 VD->addAttr(A); 3110 if (ASTMutationListener *ML = S.Context.getASTMutationListener()) 3111 ML->DeclarationMarkedOpenMPAllocate(VD, A); 3112 } 3113 3114 Sema::DeclGroupPtrTy Sema::ActOnOpenMPAllocateDirective( 3115 SourceLocation Loc, ArrayRef<Expr *> VarList, 3116 ArrayRef<OMPClause *> Clauses, DeclContext *Owner) { 3117 assert(Clauses.size() <= 1 && "Expected at most one clause."); 3118 Expr *Allocator = nullptr; 3119 if (Clauses.empty()) { 3120 // OpenMP 5.0, 2.11.3 allocate Directive, Restrictions. 3121 // allocate directives that appear in a target region must specify an 3122 // allocator clause unless a requires directive with the dynamic_allocators 3123 // clause is present in the same compilation unit. 3124 if (LangOpts.OpenMPIsDevice && 3125 !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>()) 3126 targetDiag(Loc, diag::err_expected_allocator_clause); 3127 } else { 3128 Allocator = cast<OMPAllocatorClause>(Clauses.back())->getAllocator(); 3129 } 3130 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind = 3131 getAllocatorKind(*this, DSAStack, Allocator); 3132 SmallVector<Expr *, 8> Vars; 3133 for (Expr *RefExpr : VarList) { 3134 auto *DE = cast<DeclRefExpr>(RefExpr); 3135 auto *VD = cast<VarDecl>(DE->getDecl()); 3136 3137 // Check if this is a TLS variable or global register. 3138 if (VD->getTLSKind() != VarDecl::TLS_None || 3139 VD->hasAttr<OMPThreadPrivateDeclAttr>() || 3140 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() && 3141 !VD->isLocalVarDecl())) 3142 continue; 3143 3144 // If the used several times in the allocate directive, the same allocator 3145 // must be used. 3146 if (checkPreviousOMPAllocateAttribute(*this, DSAStack, RefExpr, VD, 3147 AllocatorKind, Allocator)) 3148 continue; 3149 3150 // OpenMP, 2.11.3 allocate Directive, Restrictions, C / C++ 3151 // If a list item has a static storage type, the allocator expression in the 3152 // allocator clause must be a constant expression that evaluates to one of 3153 // the predefined memory allocator values. 3154 if (Allocator && VD->hasGlobalStorage()) { 3155 if (AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc) { 3156 Diag(Allocator->getExprLoc(), 3157 diag::err_omp_expected_predefined_allocator) 3158 << Allocator->getSourceRange(); 3159 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 3160 VarDecl::DeclarationOnly; 3161 Diag(VD->getLocation(), 3162 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 3163 << VD; 3164 continue; 3165 } 3166 } 3167 3168 Vars.push_back(RefExpr); 3169 applyOMPAllocateAttribute(*this, VD, AllocatorKind, Allocator, 3170 DE->getSourceRange()); 3171 } 3172 if (Vars.empty()) 3173 return nullptr; 3174 if (!Owner) 3175 Owner = getCurLexicalContext(); 3176 auto *D = OMPAllocateDecl::Create(Context, Owner, Loc, Vars, Clauses); 3177 D->setAccess(AS_public); 3178 Owner->addDecl(D); 3179 return DeclGroupPtrTy::make(DeclGroupRef(D)); 3180 } 3181 3182 Sema::DeclGroupPtrTy 3183 Sema::ActOnOpenMPRequiresDirective(SourceLocation Loc, 3184 ArrayRef<OMPClause *> ClauseList) { 3185 OMPRequiresDecl *D = nullptr; 3186 if (!CurContext->isFileContext()) { 3187 Diag(Loc, diag::err_omp_invalid_scope) << "requires"; 3188 } else { 3189 D = CheckOMPRequiresDecl(Loc, ClauseList); 3190 if (D) { 3191 CurContext->addDecl(D); 3192 DSAStack->addRequiresDecl(D); 3193 } 3194 } 3195 return DeclGroupPtrTy::make(DeclGroupRef(D)); 3196 } 3197 3198 OMPRequiresDecl *Sema::CheckOMPRequiresDecl(SourceLocation Loc, 3199 ArrayRef<OMPClause *> ClauseList) { 3200 /// For target specific clauses, the requires directive cannot be 3201 /// specified after the handling of any of the target regions in the 3202 /// current compilation unit. 3203 ArrayRef<SourceLocation> TargetLocations = 3204 DSAStack->getEncounteredTargetLocs(); 3205 SourceLocation AtomicLoc = DSAStack->getAtomicDirectiveLoc(); 3206 if (!TargetLocations.empty() || !AtomicLoc.isInvalid()) { 3207 for (const OMPClause *CNew : ClauseList) { 3208 // Check if any of the requires clauses affect target regions. 3209 if (isa<OMPUnifiedSharedMemoryClause>(CNew) || 3210 isa<OMPUnifiedAddressClause>(CNew) || 3211 isa<OMPReverseOffloadClause>(CNew) || 3212 isa<OMPDynamicAllocatorsClause>(CNew)) { 3213 Diag(Loc, diag::err_omp_directive_before_requires) 3214 << "target" << getOpenMPClauseName(CNew->getClauseKind()); 3215 for (SourceLocation TargetLoc : TargetLocations) { 3216 Diag(TargetLoc, diag::note_omp_requires_encountered_directive) 3217 << "target"; 3218 } 3219 } else if (!AtomicLoc.isInvalid() && 3220 isa<OMPAtomicDefaultMemOrderClause>(CNew)) { 3221 Diag(Loc, diag::err_omp_directive_before_requires) 3222 << "atomic" << getOpenMPClauseName(CNew->getClauseKind()); 3223 Diag(AtomicLoc, diag::note_omp_requires_encountered_directive) 3224 << "atomic"; 3225 } 3226 } 3227 } 3228 3229 if (!DSAStack->hasDuplicateRequiresClause(ClauseList)) 3230 return OMPRequiresDecl::Create(Context, getCurLexicalContext(), Loc, 3231 ClauseList); 3232 return nullptr; 3233 } 3234 3235 static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack, 3236 const ValueDecl *D, 3237 const DSAStackTy::DSAVarData &DVar, 3238 bool IsLoopIterVar) { 3239 if (DVar.RefExpr) { 3240 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa) 3241 << getOpenMPClauseName(DVar.CKind); 3242 return; 3243 } 3244 enum { 3245 PDSA_StaticMemberShared, 3246 PDSA_StaticLocalVarShared, 3247 PDSA_LoopIterVarPrivate, 3248 PDSA_LoopIterVarLinear, 3249 PDSA_LoopIterVarLastprivate, 3250 PDSA_ConstVarShared, 3251 PDSA_GlobalVarShared, 3252 PDSA_TaskVarFirstprivate, 3253 PDSA_LocalVarPrivate, 3254 PDSA_Implicit 3255 } Reason = PDSA_Implicit; 3256 bool ReportHint = false; 3257 auto ReportLoc = D->getLocation(); 3258 auto *VD = dyn_cast<VarDecl>(D); 3259 if (IsLoopIterVar) { 3260 if (DVar.CKind == OMPC_private) 3261 Reason = PDSA_LoopIterVarPrivate; 3262 else if (DVar.CKind == OMPC_lastprivate) 3263 Reason = PDSA_LoopIterVarLastprivate; 3264 else 3265 Reason = PDSA_LoopIterVarLinear; 3266 } else if (isOpenMPTaskingDirective(DVar.DKind) && 3267 DVar.CKind == OMPC_firstprivate) { 3268 Reason = PDSA_TaskVarFirstprivate; 3269 ReportLoc = DVar.ImplicitDSALoc; 3270 } else if (VD && VD->isStaticLocal()) 3271 Reason = PDSA_StaticLocalVarShared; 3272 else if (VD && VD->isStaticDataMember()) 3273 Reason = PDSA_StaticMemberShared; 3274 else if (VD && VD->isFileVarDecl()) 3275 Reason = PDSA_GlobalVarShared; 3276 else if (D->getType().isConstant(SemaRef.getASTContext())) 3277 Reason = PDSA_ConstVarShared; 3278 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) { 3279 ReportHint = true; 3280 Reason = PDSA_LocalVarPrivate; 3281 } 3282 if (Reason != PDSA_Implicit) { 3283 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa) 3284 << Reason << ReportHint 3285 << getOpenMPDirectiveName(Stack->getCurrentDirective()); 3286 } else if (DVar.ImplicitDSALoc.isValid()) { 3287 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa) 3288 << getOpenMPClauseName(DVar.CKind); 3289 } 3290 } 3291 3292 static OpenMPMapClauseKind 3293 getMapClauseKindFromModifier(OpenMPDefaultmapClauseModifier M, 3294 bool IsAggregateOrDeclareTarget) { 3295 OpenMPMapClauseKind Kind = OMPC_MAP_unknown; 3296 switch (M) { 3297 case OMPC_DEFAULTMAP_MODIFIER_alloc: 3298 Kind = OMPC_MAP_alloc; 3299 break; 3300 case OMPC_DEFAULTMAP_MODIFIER_to: 3301 Kind = OMPC_MAP_to; 3302 break; 3303 case OMPC_DEFAULTMAP_MODIFIER_from: 3304 Kind = OMPC_MAP_from; 3305 break; 3306 case OMPC_DEFAULTMAP_MODIFIER_tofrom: 3307 Kind = OMPC_MAP_tofrom; 3308 break; 3309 case OMPC_DEFAULTMAP_MODIFIER_firstprivate: 3310 case OMPC_DEFAULTMAP_MODIFIER_last: 3311 llvm_unreachable("Unexpected defaultmap implicit behavior"); 3312 case OMPC_DEFAULTMAP_MODIFIER_none: 3313 case OMPC_DEFAULTMAP_MODIFIER_default: 3314 case OMPC_DEFAULTMAP_MODIFIER_unknown: 3315 // IsAggregateOrDeclareTarget could be true if: 3316 // 1. the implicit behavior for aggregate is tofrom 3317 // 2. it's a declare target link 3318 if (IsAggregateOrDeclareTarget) { 3319 Kind = OMPC_MAP_tofrom; 3320 break; 3321 } 3322 llvm_unreachable("Unexpected defaultmap implicit behavior"); 3323 } 3324 assert(Kind != OMPC_MAP_unknown && "Expect map kind to be known"); 3325 return Kind; 3326 } 3327 3328 namespace { 3329 class DSAAttrChecker final : public StmtVisitor<DSAAttrChecker, void> { 3330 DSAStackTy *Stack; 3331 Sema &SemaRef; 3332 bool ErrorFound = false; 3333 bool TryCaptureCXXThisMembers = false; 3334 CapturedStmt *CS = nullptr; 3335 llvm::SmallVector<Expr *, 4> ImplicitFirstprivate; 3336 llvm::SmallVector<Expr *, 4> ImplicitMap[OMPC_MAP_delete]; 3337 Sema::VarsWithInheritedDSAType VarsWithInheritedDSA; 3338 llvm::SmallDenseSet<const ValueDecl *, 4> ImplicitDeclarations; 3339 3340 void VisitSubCaptures(OMPExecutableDirective *S) { 3341 // Check implicitly captured variables. 3342 if (!S->hasAssociatedStmt() || !S->getAssociatedStmt() || 3343 S->getDirectiveKind() == OMPD_atomic || 3344 S->getDirectiveKind() == OMPD_critical || 3345 S->getDirectiveKind() == OMPD_section || 3346 S->getDirectiveKind() == OMPD_master) 3347 return; 3348 visitSubCaptures(S->getInnermostCapturedStmt()); 3349 // Try to capture inner this->member references to generate correct mappings 3350 // and diagnostics. 3351 if (TryCaptureCXXThisMembers || 3352 (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) && 3353 llvm::any_of(S->getInnermostCapturedStmt()->captures(), 3354 [](const CapturedStmt::Capture &C) { 3355 return C.capturesThis(); 3356 }))) { 3357 bool SavedTryCaptureCXXThisMembers = TryCaptureCXXThisMembers; 3358 TryCaptureCXXThisMembers = true; 3359 Visit(S->getInnermostCapturedStmt()->getCapturedStmt()); 3360 TryCaptureCXXThisMembers = SavedTryCaptureCXXThisMembers; 3361 } 3362 // In tasks firstprivates are not captured anymore, need to analyze them 3363 // explicitly. 3364 if (isOpenMPTaskingDirective(S->getDirectiveKind()) && 3365 !isOpenMPTaskLoopDirective(S->getDirectiveKind())) { 3366 for (OMPClause *C : S->clauses()) 3367 if (auto *FC = dyn_cast<OMPFirstprivateClause>(C)) { 3368 for (Expr *Ref : FC->varlists()) 3369 Visit(Ref); 3370 } 3371 } 3372 } 3373 3374 public: 3375 void VisitDeclRefExpr(DeclRefExpr *E) { 3376 if (TryCaptureCXXThisMembers || E->isTypeDependent() || 3377 E->isValueDependent() || E->containsUnexpandedParameterPack() || 3378 E->isInstantiationDependent()) 3379 return; 3380 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 3381 // Check the datasharing rules for the expressions in the clauses. 3382 if (!CS) { 3383 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD)) 3384 if (!CED->hasAttr<OMPCaptureNoInitAttr>()) { 3385 Visit(CED->getInit()); 3386 return; 3387 } 3388 } else if (VD->isImplicit() || isa<OMPCapturedExprDecl>(VD)) 3389 // Do not analyze internal variables and do not enclose them into 3390 // implicit clauses. 3391 return; 3392 VD = VD->getCanonicalDecl(); 3393 // Skip internally declared variables. 3394 if (VD->hasLocalStorage() && CS && !CS->capturesVariable(VD) && 3395 !Stack->isImplicitTaskFirstprivate(VD)) 3396 return; 3397 // Skip allocators in uses_allocators clauses. 3398 if (Stack->isUsesAllocatorsDecl(VD).hasValue()) 3399 return; 3400 3401 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false); 3402 // Check if the variable has explicit DSA set and stop analysis if it so. 3403 if (DVar.RefExpr || !ImplicitDeclarations.insert(VD).second) 3404 return; 3405 3406 // Skip internally declared static variables. 3407 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 3408 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); 3409 if (VD->hasGlobalStorage() && CS && !CS->capturesVariable(VD) && 3410 (Stack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() || 3411 !Res || *Res != OMPDeclareTargetDeclAttr::MT_Link) && 3412 !Stack->isImplicitTaskFirstprivate(VD)) 3413 return; 3414 3415 SourceLocation ELoc = E->getExprLoc(); 3416 OpenMPDirectiveKind DKind = Stack->getCurrentDirective(); 3417 // The default(none) clause requires that each variable that is referenced 3418 // in the construct, and does not have a predetermined data-sharing 3419 // attribute, must have its data-sharing attribute explicitly determined 3420 // by being listed in a data-sharing attribute clause. 3421 if (DVar.CKind == OMPC_unknown && 3422 (Stack->getDefaultDSA() == DSA_none || 3423 Stack->getDefaultDSA() == DSA_firstprivate) && 3424 isImplicitOrExplicitTaskingRegion(DKind) && 3425 VarsWithInheritedDSA.count(VD) == 0) { 3426 bool InheritedDSA = Stack->getDefaultDSA() == DSA_none; 3427 if (!InheritedDSA && Stack->getDefaultDSA() == DSA_firstprivate) { 3428 DSAStackTy::DSAVarData DVar = 3429 Stack->getImplicitDSA(VD, /*FromParent=*/false); 3430 InheritedDSA = DVar.CKind == OMPC_unknown; 3431 } 3432 if (InheritedDSA) 3433 VarsWithInheritedDSA[VD] = E; 3434 return; 3435 } 3436 3437 // OpenMP 5.0 [2.19.7.2, defaultmap clause, Description] 3438 // If implicit-behavior is none, each variable referenced in the 3439 // construct that does not have a predetermined data-sharing attribute 3440 // and does not appear in a to or link clause on a declare target 3441 // directive must be listed in a data-mapping attribute clause, a 3442 // data-haring attribute clause (including a data-sharing attribute 3443 // clause on a combined construct where target. is one of the 3444 // constituent constructs), or an is_device_ptr clause. 3445 OpenMPDefaultmapClauseKind ClauseKind = 3446 getVariableCategoryFromDecl(SemaRef.getLangOpts(), VD); 3447 if (SemaRef.getLangOpts().OpenMP >= 50) { 3448 bool IsModifierNone = Stack->getDefaultmapModifier(ClauseKind) == 3449 OMPC_DEFAULTMAP_MODIFIER_none; 3450 if (DVar.CKind == OMPC_unknown && IsModifierNone && 3451 VarsWithInheritedDSA.count(VD) == 0 && !Res) { 3452 // Only check for data-mapping attribute and is_device_ptr here 3453 // since we have already make sure that the declaration does not 3454 // have a data-sharing attribute above 3455 if (!Stack->checkMappableExprComponentListsForDecl( 3456 VD, /*CurrentRegionOnly=*/true, 3457 [VD](OMPClauseMappableExprCommon::MappableExprComponentListRef 3458 MapExprComponents, 3459 OpenMPClauseKind) { 3460 auto MI = MapExprComponents.rbegin(); 3461 auto ME = MapExprComponents.rend(); 3462 return MI != ME && MI->getAssociatedDeclaration() == VD; 3463 })) { 3464 VarsWithInheritedDSA[VD] = E; 3465 return; 3466 } 3467 } 3468 } 3469 3470 if (isOpenMPTargetExecutionDirective(DKind) && 3471 !Stack->isLoopControlVariable(VD).first) { 3472 if (!Stack->checkMappableExprComponentListsForDecl( 3473 VD, /*CurrentRegionOnly=*/true, 3474 [](OMPClauseMappableExprCommon::MappableExprComponentListRef 3475 StackComponents, 3476 OpenMPClauseKind) { 3477 // Variable is used if it has been marked as an array, array 3478 // section, array shaping or the variable iself. 3479 return StackComponents.size() == 1 || 3480 std::all_of( 3481 std::next(StackComponents.rbegin()), 3482 StackComponents.rend(), 3483 [](const OMPClauseMappableExprCommon:: 3484 MappableComponent &MC) { 3485 return MC.getAssociatedDeclaration() == 3486 nullptr && 3487 (isa<OMPArraySectionExpr>( 3488 MC.getAssociatedExpression()) || 3489 isa<OMPArrayShapingExpr>( 3490 MC.getAssociatedExpression()) || 3491 isa<ArraySubscriptExpr>( 3492 MC.getAssociatedExpression())); 3493 }); 3494 })) { 3495 bool IsFirstprivate = false; 3496 // By default lambdas are captured as firstprivates. 3497 if (const auto *RD = 3498 VD->getType().getNonReferenceType()->getAsCXXRecordDecl()) 3499 IsFirstprivate = RD->isLambda(); 3500 IsFirstprivate = 3501 IsFirstprivate || (Stack->mustBeFirstprivate(ClauseKind) && !Res); 3502 if (IsFirstprivate) { 3503 ImplicitFirstprivate.emplace_back(E); 3504 } else { 3505 OpenMPDefaultmapClauseModifier M = 3506 Stack->getDefaultmapModifier(ClauseKind); 3507 OpenMPMapClauseKind Kind = getMapClauseKindFromModifier( 3508 M, ClauseKind == OMPC_DEFAULTMAP_aggregate || Res); 3509 ImplicitMap[Kind].emplace_back(E); 3510 } 3511 return; 3512 } 3513 } 3514 3515 // OpenMP [2.9.3.6, Restrictions, p.2] 3516 // A list item that appears in a reduction clause of the innermost 3517 // enclosing worksharing or parallel construct may not be accessed in an 3518 // explicit task. 3519 DVar = Stack->hasInnermostDSA( 3520 VD, 3521 [](OpenMPClauseKind C, bool AppliedToPointee) { 3522 return C == OMPC_reduction && !AppliedToPointee; 3523 }, 3524 [](OpenMPDirectiveKind K) { 3525 return isOpenMPParallelDirective(K) || 3526 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K); 3527 }, 3528 /*FromParent=*/true); 3529 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) { 3530 ErrorFound = true; 3531 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); 3532 reportOriginalDsa(SemaRef, Stack, VD, DVar); 3533 return; 3534 } 3535 3536 // Define implicit data-sharing attributes for task. 3537 DVar = Stack->getImplicitDSA(VD, /*FromParent=*/false); 3538 if (((isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared) || 3539 (Stack->getDefaultDSA() == DSA_firstprivate && 3540 DVar.CKind == OMPC_firstprivate && !DVar.RefExpr)) && 3541 !Stack->isLoopControlVariable(VD).first) { 3542 ImplicitFirstprivate.push_back(E); 3543 return; 3544 } 3545 3546 // Store implicitly used globals with declare target link for parent 3547 // target. 3548 if (!isOpenMPTargetExecutionDirective(DKind) && Res && 3549 *Res == OMPDeclareTargetDeclAttr::MT_Link) { 3550 Stack->addToParentTargetRegionLinkGlobals(E); 3551 return; 3552 } 3553 } 3554 } 3555 void VisitMemberExpr(MemberExpr *E) { 3556 if (E->isTypeDependent() || E->isValueDependent() || 3557 E->containsUnexpandedParameterPack() || E->isInstantiationDependent()) 3558 return; 3559 auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); 3560 OpenMPDirectiveKind DKind = Stack->getCurrentDirective(); 3561 if (auto *TE = dyn_cast<CXXThisExpr>(E->getBase()->IgnoreParenCasts())) { 3562 if (!FD) 3563 return; 3564 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(FD, /*FromParent=*/false); 3565 // Check if the variable has explicit DSA set and stop analysis if it 3566 // so. 3567 if (DVar.RefExpr || !ImplicitDeclarations.insert(FD).second) 3568 return; 3569 3570 if (isOpenMPTargetExecutionDirective(DKind) && 3571 !Stack->isLoopControlVariable(FD).first && 3572 !Stack->checkMappableExprComponentListsForDecl( 3573 FD, /*CurrentRegionOnly=*/true, 3574 [](OMPClauseMappableExprCommon::MappableExprComponentListRef 3575 StackComponents, 3576 OpenMPClauseKind) { 3577 return isa<CXXThisExpr>( 3578 cast<MemberExpr>( 3579 StackComponents.back().getAssociatedExpression()) 3580 ->getBase() 3581 ->IgnoreParens()); 3582 })) { 3583 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3] 3584 // A bit-field cannot appear in a map clause. 3585 // 3586 if (FD->isBitField()) 3587 return; 3588 3589 // Check to see if the member expression is referencing a class that 3590 // has already been explicitly mapped 3591 if (Stack->isClassPreviouslyMapped(TE->getType())) 3592 return; 3593 3594 OpenMPDefaultmapClauseModifier Modifier = 3595 Stack->getDefaultmapModifier(OMPC_DEFAULTMAP_aggregate); 3596 OpenMPMapClauseKind Kind = getMapClauseKindFromModifier( 3597 Modifier, /*IsAggregateOrDeclareTarget*/ true); 3598 ImplicitMap[Kind].emplace_back(E); 3599 return; 3600 } 3601 3602 SourceLocation ELoc = E->getExprLoc(); 3603 // OpenMP [2.9.3.6, Restrictions, p.2] 3604 // A list item that appears in a reduction clause of the innermost 3605 // enclosing worksharing or parallel construct may not be accessed in 3606 // an explicit task. 3607 DVar = Stack->hasInnermostDSA( 3608 FD, 3609 [](OpenMPClauseKind C, bool AppliedToPointee) { 3610 return C == OMPC_reduction && !AppliedToPointee; 3611 }, 3612 [](OpenMPDirectiveKind K) { 3613 return isOpenMPParallelDirective(K) || 3614 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K); 3615 }, 3616 /*FromParent=*/true); 3617 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) { 3618 ErrorFound = true; 3619 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); 3620 reportOriginalDsa(SemaRef, Stack, FD, DVar); 3621 return; 3622 } 3623 3624 // Define implicit data-sharing attributes for task. 3625 DVar = Stack->getImplicitDSA(FD, /*FromParent=*/false); 3626 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared && 3627 !Stack->isLoopControlVariable(FD).first) { 3628 // Check if there is a captured expression for the current field in the 3629 // region. Do not mark it as firstprivate unless there is no captured 3630 // expression. 3631 // TODO: try to make it firstprivate. 3632 if (DVar.CKind != OMPC_unknown) 3633 ImplicitFirstprivate.push_back(E); 3634 } 3635 return; 3636 } 3637 if (isOpenMPTargetExecutionDirective(DKind)) { 3638 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents; 3639 if (!checkMapClauseExpressionBase(SemaRef, E, CurComponents, OMPC_map, 3640 /*NoDiagnose=*/true)) 3641 return; 3642 const auto *VD = cast<ValueDecl>( 3643 CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl()); 3644 if (!Stack->checkMappableExprComponentListsForDecl( 3645 VD, /*CurrentRegionOnly=*/true, 3646 [&CurComponents]( 3647 OMPClauseMappableExprCommon::MappableExprComponentListRef 3648 StackComponents, 3649 OpenMPClauseKind) { 3650 auto CCI = CurComponents.rbegin(); 3651 auto CCE = CurComponents.rend(); 3652 for (const auto &SC : llvm::reverse(StackComponents)) { 3653 // Do both expressions have the same kind? 3654 if (CCI->getAssociatedExpression()->getStmtClass() != 3655 SC.getAssociatedExpression()->getStmtClass()) 3656 if (!((isa<OMPArraySectionExpr>( 3657 SC.getAssociatedExpression()) || 3658 isa<OMPArrayShapingExpr>( 3659 SC.getAssociatedExpression())) && 3660 isa<ArraySubscriptExpr>( 3661 CCI->getAssociatedExpression()))) 3662 return false; 3663 3664 const Decl *CCD = CCI->getAssociatedDeclaration(); 3665 const Decl *SCD = SC.getAssociatedDeclaration(); 3666 CCD = CCD ? CCD->getCanonicalDecl() : nullptr; 3667 SCD = SCD ? SCD->getCanonicalDecl() : nullptr; 3668 if (SCD != CCD) 3669 return false; 3670 std::advance(CCI, 1); 3671 if (CCI == CCE) 3672 break; 3673 } 3674 return true; 3675 })) { 3676 Visit(E->getBase()); 3677 } 3678 } else if (!TryCaptureCXXThisMembers) { 3679 Visit(E->getBase()); 3680 } 3681 } 3682 void VisitOMPExecutableDirective(OMPExecutableDirective *S) { 3683 for (OMPClause *C : S->clauses()) { 3684 // Skip analysis of arguments of implicitly defined firstprivate clause 3685 // for task|target directives. 3686 // Skip analysis of arguments of implicitly defined map clause for target 3687 // directives. 3688 if (C && !((isa<OMPFirstprivateClause>(C) || isa<OMPMapClause>(C)) && 3689 C->isImplicit())) { 3690 for (Stmt *CC : C->children()) { 3691 if (CC) 3692 Visit(CC); 3693 } 3694 } 3695 } 3696 // Check implicitly captured variables. 3697 VisitSubCaptures(S); 3698 } 3699 void VisitStmt(Stmt *S) { 3700 for (Stmt *C : S->children()) { 3701 if (C) { 3702 // Check implicitly captured variables in the task-based directives to 3703 // check if they must be firstprivatized. 3704 Visit(C); 3705 } 3706 } 3707 } 3708 3709 void visitSubCaptures(CapturedStmt *S) { 3710 for (const CapturedStmt::Capture &Cap : S->captures()) { 3711 if (!Cap.capturesVariable() && !Cap.capturesVariableByCopy()) 3712 continue; 3713 VarDecl *VD = Cap.getCapturedVar(); 3714 // Do not try to map the variable if it or its sub-component was mapped 3715 // already. 3716 if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) && 3717 Stack->checkMappableExprComponentListsForDecl( 3718 VD, /*CurrentRegionOnly=*/true, 3719 [](OMPClauseMappableExprCommon::MappableExprComponentListRef, 3720 OpenMPClauseKind) { return true; })) 3721 continue; 3722 DeclRefExpr *DRE = buildDeclRefExpr( 3723 SemaRef, VD, VD->getType().getNonLValueExprType(SemaRef.Context), 3724 Cap.getLocation(), /*RefersToCapture=*/true); 3725 Visit(DRE); 3726 } 3727 } 3728 bool isErrorFound() const { return ErrorFound; } 3729 ArrayRef<Expr *> getImplicitFirstprivate() const { 3730 return ImplicitFirstprivate; 3731 } 3732 ArrayRef<Expr *> getImplicitMap(OpenMPDefaultmapClauseKind Kind) const { 3733 return ImplicitMap[Kind]; 3734 } 3735 const Sema::VarsWithInheritedDSAType &getVarsWithInheritedDSA() const { 3736 return VarsWithInheritedDSA; 3737 } 3738 3739 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS) 3740 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) { 3741 // Process declare target link variables for the target directives. 3742 if (isOpenMPTargetExecutionDirective(S->getCurrentDirective())) { 3743 for (DeclRefExpr *E : Stack->getLinkGlobals()) 3744 Visit(E); 3745 } 3746 } 3747 }; 3748 } // namespace 3749 3750 void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) { 3751 switch (DKind) { 3752 case OMPD_parallel: 3753 case OMPD_parallel_for: 3754 case OMPD_parallel_for_simd: 3755 case OMPD_parallel_sections: 3756 case OMPD_parallel_master: 3757 case OMPD_teams: 3758 case OMPD_teams_distribute: 3759 case OMPD_teams_distribute_simd: { 3760 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3761 QualType KmpInt32PtrTy = 3762 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3763 Sema::CapturedParamNameType Params[] = { 3764 std::make_pair(".global_tid.", KmpInt32PtrTy), 3765 std::make_pair(".bound_tid.", KmpInt32PtrTy), 3766 std::make_pair(StringRef(), QualType()) // __context with shared vars 3767 }; 3768 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3769 Params); 3770 break; 3771 } 3772 case OMPD_target_teams: 3773 case OMPD_target_parallel: 3774 case OMPD_target_parallel_for: 3775 case OMPD_target_parallel_for_simd: 3776 case OMPD_target_teams_distribute: 3777 case OMPD_target_teams_distribute_simd: { 3778 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3779 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 3780 QualType KmpInt32PtrTy = 3781 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3782 QualType Args[] = {VoidPtrTy}; 3783 FunctionProtoType::ExtProtoInfo EPI; 3784 EPI.Variadic = true; 3785 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 3786 Sema::CapturedParamNameType Params[] = { 3787 std::make_pair(".global_tid.", KmpInt32Ty), 3788 std::make_pair(".part_id.", KmpInt32PtrTy), 3789 std::make_pair(".privates.", VoidPtrTy), 3790 std::make_pair( 3791 ".copy_fn.", 3792 Context.getPointerType(CopyFnType).withConst().withRestrict()), 3793 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 3794 std::make_pair(StringRef(), QualType()) // __context with shared vars 3795 }; 3796 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3797 Params, /*OpenMPCaptureLevel=*/0); 3798 // Mark this captured region as inlined, because we don't use outlined 3799 // function directly. 3800 getCurCapturedRegion()->TheCapturedDecl->addAttr( 3801 AlwaysInlineAttr::CreateImplicit( 3802 Context, {}, AttributeCommonInfo::AS_Keyword, 3803 AlwaysInlineAttr::Keyword_forceinline)); 3804 Sema::CapturedParamNameType ParamsTarget[] = { 3805 std::make_pair(StringRef(), QualType()) // __context with shared vars 3806 }; 3807 // Start a captured region for 'target' with no implicit parameters. 3808 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3809 ParamsTarget, /*OpenMPCaptureLevel=*/1); 3810 Sema::CapturedParamNameType ParamsTeamsOrParallel[] = { 3811 std::make_pair(".global_tid.", KmpInt32PtrTy), 3812 std::make_pair(".bound_tid.", KmpInt32PtrTy), 3813 std::make_pair(StringRef(), QualType()) // __context with shared vars 3814 }; 3815 // Start a captured region for 'teams' or 'parallel'. Both regions have 3816 // the same implicit parameters. 3817 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3818 ParamsTeamsOrParallel, /*OpenMPCaptureLevel=*/2); 3819 break; 3820 } 3821 case OMPD_target: 3822 case OMPD_target_simd: { 3823 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3824 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 3825 QualType KmpInt32PtrTy = 3826 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3827 QualType Args[] = {VoidPtrTy}; 3828 FunctionProtoType::ExtProtoInfo EPI; 3829 EPI.Variadic = true; 3830 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 3831 Sema::CapturedParamNameType Params[] = { 3832 std::make_pair(".global_tid.", KmpInt32Ty), 3833 std::make_pair(".part_id.", KmpInt32PtrTy), 3834 std::make_pair(".privates.", VoidPtrTy), 3835 std::make_pair( 3836 ".copy_fn.", 3837 Context.getPointerType(CopyFnType).withConst().withRestrict()), 3838 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 3839 std::make_pair(StringRef(), QualType()) // __context with shared vars 3840 }; 3841 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3842 Params, /*OpenMPCaptureLevel=*/0); 3843 // Mark this captured region as inlined, because we don't use outlined 3844 // function directly. 3845 getCurCapturedRegion()->TheCapturedDecl->addAttr( 3846 AlwaysInlineAttr::CreateImplicit( 3847 Context, {}, AttributeCommonInfo::AS_Keyword, 3848 AlwaysInlineAttr::Keyword_forceinline)); 3849 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3850 std::make_pair(StringRef(), QualType()), 3851 /*OpenMPCaptureLevel=*/1); 3852 break; 3853 } 3854 case OMPD_atomic: 3855 case OMPD_critical: 3856 case OMPD_section: 3857 case OMPD_master: 3858 break; 3859 case OMPD_simd: 3860 case OMPD_for: 3861 case OMPD_for_simd: 3862 case OMPD_sections: 3863 case OMPD_single: 3864 case OMPD_taskgroup: 3865 case OMPD_distribute: 3866 case OMPD_distribute_simd: 3867 case OMPD_ordered: 3868 case OMPD_target_data: { 3869 Sema::CapturedParamNameType Params[] = { 3870 std::make_pair(StringRef(), QualType()) // __context with shared vars 3871 }; 3872 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3873 Params); 3874 break; 3875 } 3876 case OMPD_task: { 3877 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3878 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 3879 QualType KmpInt32PtrTy = 3880 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3881 QualType Args[] = {VoidPtrTy}; 3882 FunctionProtoType::ExtProtoInfo EPI; 3883 EPI.Variadic = true; 3884 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 3885 Sema::CapturedParamNameType Params[] = { 3886 std::make_pair(".global_tid.", KmpInt32Ty), 3887 std::make_pair(".part_id.", KmpInt32PtrTy), 3888 std::make_pair(".privates.", VoidPtrTy), 3889 std::make_pair( 3890 ".copy_fn.", 3891 Context.getPointerType(CopyFnType).withConst().withRestrict()), 3892 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 3893 std::make_pair(StringRef(), QualType()) // __context with shared vars 3894 }; 3895 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3896 Params); 3897 // Mark this captured region as inlined, because we don't use outlined 3898 // function directly. 3899 getCurCapturedRegion()->TheCapturedDecl->addAttr( 3900 AlwaysInlineAttr::CreateImplicit( 3901 Context, {}, AttributeCommonInfo::AS_Keyword, 3902 AlwaysInlineAttr::Keyword_forceinline)); 3903 break; 3904 } 3905 case OMPD_taskloop: 3906 case OMPD_taskloop_simd: 3907 case OMPD_master_taskloop: 3908 case OMPD_master_taskloop_simd: { 3909 QualType KmpInt32Ty = 3910 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1) 3911 .withConst(); 3912 QualType KmpUInt64Ty = 3913 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0) 3914 .withConst(); 3915 QualType KmpInt64Ty = 3916 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1) 3917 .withConst(); 3918 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 3919 QualType KmpInt32PtrTy = 3920 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3921 QualType Args[] = {VoidPtrTy}; 3922 FunctionProtoType::ExtProtoInfo EPI; 3923 EPI.Variadic = true; 3924 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 3925 Sema::CapturedParamNameType Params[] = { 3926 std::make_pair(".global_tid.", KmpInt32Ty), 3927 std::make_pair(".part_id.", KmpInt32PtrTy), 3928 std::make_pair(".privates.", VoidPtrTy), 3929 std::make_pair( 3930 ".copy_fn.", 3931 Context.getPointerType(CopyFnType).withConst().withRestrict()), 3932 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 3933 std::make_pair(".lb.", KmpUInt64Ty), 3934 std::make_pair(".ub.", KmpUInt64Ty), 3935 std::make_pair(".st.", KmpInt64Ty), 3936 std::make_pair(".liter.", KmpInt32Ty), 3937 std::make_pair(".reductions.", VoidPtrTy), 3938 std::make_pair(StringRef(), QualType()) // __context with shared vars 3939 }; 3940 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3941 Params); 3942 // Mark this captured region as inlined, because we don't use outlined 3943 // function directly. 3944 getCurCapturedRegion()->TheCapturedDecl->addAttr( 3945 AlwaysInlineAttr::CreateImplicit( 3946 Context, {}, AttributeCommonInfo::AS_Keyword, 3947 AlwaysInlineAttr::Keyword_forceinline)); 3948 break; 3949 } 3950 case OMPD_parallel_master_taskloop: 3951 case OMPD_parallel_master_taskloop_simd: { 3952 QualType KmpInt32Ty = 3953 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1) 3954 .withConst(); 3955 QualType KmpUInt64Ty = 3956 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0) 3957 .withConst(); 3958 QualType KmpInt64Ty = 3959 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1) 3960 .withConst(); 3961 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 3962 QualType KmpInt32PtrTy = 3963 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3964 Sema::CapturedParamNameType ParamsParallel[] = { 3965 std::make_pair(".global_tid.", KmpInt32PtrTy), 3966 std::make_pair(".bound_tid.", KmpInt32PtrTy), 3967 std::make_pair(StringRef(), QualType()) // __context with shared vars 3968 }; 3969 // Start a captured region for 'parallel'. 3970 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3971 ParamsParallel, /*OpenMPCaptureLevel=*/0); 3972 QualType Args[] = {VoidPtrTy}; 3973 FunctionProtoType::ExtProtoInfo EPI; 3974 EPI.Variadic = true; 3975 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 3976 Sema::CapturedParamNameType Params[] = { 3977 std::make_pair(".global_tid.", KmpInt32Ty), 3978 std::make_pair(".part_id.", KmpInt32PtrTy), 3979 std::make_pair(".privates.", VoidPtrTy), 3980 std::make_pair( 3981 ".copy_fn.", 3982 Context.getPointerType(CopyFnType).withConst().withRestrict()), 3983 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 3984 std::make_pair(".lb.", KmpUInt64Ty), 3985 std::make_pair(".ub.", KmpUInt64Ty), 3986 std::make_pair(".st.", KmpInt64Ty), 3987 std::make_pair(".liter.", KmpInt32Ty), 3988 std::make_pair(".reductions.", VoidPtrTy), 3989 std::make_pair(StringRef(), QualType()) // __context with shared vars 3990 }; 3991 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3992 Params, /*OpenMPCaptureLevel=*/1); 3993 // Mark this captured region as inlined, because we don't use outlined 3994 // function directly. 3995 getCurCapturedRegion()->TheCapturedDecl->addAttr( 3996 AlwaysInlineAttr::CreateImplicit( 3997 Context, {}, AttributeCommonInfo::AS_Keyword, 3998 AlwaysInlineAttr::Keyword_forceinline)); 3999 break; 4000 } 4001 case OMPD_distribute_parallel_for_simd: 4002 case OMPD_distribute_parallel_for: { 4003 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 4004 QualType KmpInt32PtrTy = 4005 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4006 Sema::CapturedParamNameType Params[] = { 4007 std::make_pair(".global_tid.", KmpInt32PtrTy), 4008 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4009 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 4010 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 4011 std::make_pair(StringRef(), QualType()) // __context with shared vars 4012 }; 4013 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4014 Params); 4015 break; 4016 } 4017 case OMPD_target_teams_distribute_parallel_for: 4018 case OMPD_target_teams_distribute_parallel_for_simd: { 4019 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 4020 QualType KmpInt32PtrTy = 4021 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4022 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 4023 4024 QualType Args[] = {VoidPtrTy}; 4025 FunctionProtoType::ExtProtoInfo EPI; 4026 EPI.Variadic = true; 4027 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 4028 Sema::CapturedParamNameType Params[] = { 4029 std::make_pair(".global_tid.", KmpInt32Ty), 4030 std::make_pair(".part_id.", KmpInt32PtrTy), 4031 std::make_pair(".privates.", VoidPtrTy), 4032 std::make_pair( 4033 ".copy_fn.", 4034 Context.getPointerType(CopyFnType).withConst().withRestrict()), 4035 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 4036 std::make_pair(StringRef(), QualType()) // __context with shared vars 4037 }; 4038 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4039 Params, /*OpenMPCaptureLevel=*/0); 4040 // Mark this captured region as inlined, because we don't use outlined 4041 // function directly. 4042 getCurCapturedRegion()->TheCapturedDecl->addAttr( 4043 AlwaysInlineAttr::CreateImplicit( 4044 Context, {}, AttributeCommonInfo::AS_Keyword, 4045 AlwaysInlineAttr::Keyword_forceinline)); 4046 Sema::CapturedParamNameType ParamsTarget[] = { 4047 std::make_pair(StringRef(), QualType()) // __context with shared vars 4048 }; 4049 // Start a captured region for 'target' with no implicit parameters. 4050 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4051 ParamsTarget, /*OpenMPCaptureLevel=*/1); 4052 4053 Sema::CapturedParamNameType ParamsTeams[] = { 4054 std::make_pair(".global_tid.", KmpInt32PtrTy), 4055 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4056 std::make_pair(StringRef(), QualType()) // __context with shared vars 4057 }; 4058 // Start a captured region for 'target' with no implicit parameters. 4059 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4060 ParamsTeams, /*OpenMPCaptureLevel=*/2); 4061 4062 Sema::CapturedParamNameType ParamsParallel[] = { 4063 std::make_pair(".global_tid.", KmpInt32PtrTy), 4064 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4065 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 4066 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 4067 std::make_pair(StringRef(), QualType()) // __context with shared vars 4068 }; 4069 // Start a captured region for 'teams' or 'parallel'. Both regions have 4070 // the same implicit parameters. 4071 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4072 ParamsParallel, /*OpenMPCaptureLevel=*/3); 4073 break; 4074 } 4075 4076 case OMPD_teams_distribute_parallel_for: 4077 case OMPD_teams_distribute_parallel_for_simd: { 4078 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 4079 QualType KmpInt32PtrTy = 4080 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4081 4082 Sema::CapturedParamNameType ParamsTeams[] = { 4083 std::make_pair(".global_tid.", KmpInt32PtrTy), 4084 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4085 std::make_pair(StringRef(), QualType()) // __context with shared vars 4086 }; 4087 // Start a captured region for 'target' with no implicit parameters. 4088 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4089 ParamsTeams, /*OpenMPCaptureLevel=*/0); 4090 4091 Sema::CapturedParamNameType ParamsParallel[] = { 4092 std::make_pair(".global_tid.", KmpInt32PtrTy), 4093 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4094 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 4095 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 4096 std::make_pair(StringRef(), QualType()) // __context with shared vars 4097 }; 4098 // Start a captured region for 'teams' or 'parallel'. Both regions have 4099 // the same implicit parameters. 4100 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4101 ParamsParallel, /*OpenMPCaptureLevel=*/1); 4102 break; 4103 } 4104 case OMPD_target_update: 4105 case OMPD_target_enter_data: 4106 case OMPD_target_exit_data: { 4107 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 4108 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 4109 QualType KmpInt32PtrTy = 4110 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4111 QualType Args[] = {VoidPtrTy}; 4112 FunctionProtoType::ExtProtoInfo EPI; 4113 EPI.Variadic = true; 4114 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 4115 Sema::CapturedParamNameType Params[] = { 4116 std::make_pair(".global_tid.", KmpInt32Ty), 4117 std::make_pair(".part_id.", KmpInt32PtrTy), 4118 std::make_pair(".privates.", VoidPtrTy), 4119 std::make_pair( 4120 ".copy_fn.", 4121 Context.getPointerType(CopyFnType).withConst().withRestrict()), 4122 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 4123 std::make_pair(StringRef(), QualType()) // __context with shared vars 4124 }; 4125 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4126 Params); 4127 // Mark this captured region as inlined, because we don't use outlined 4128 // function directly. 4129 getCurCapturedRegion()->TheCapturedDecl->addAttr( 4130 AlwaysInlineAttr::CreateImplicit( 4131 Context, {}, AttributeCommonInfo::AS_Keyword, 4132 AlwaysInlineAttr::Keyword_forceinline)); 4133 break; 4134 } 4135 case OMPD_threadprivate: 4136 case OMPD_allocate: 4137 case OMPD_taskyield: 4138 case OMPD_barrier: 4139 case OMPD_taskwait: 4140 case OMPD_cancellation_point: 4141 case OMPD_cancel: 4142 case OMPD_flush: 4143 case OMPD_depobj: 4144 case OMPD_scan: 4145 case OMPD_declare_reduction: 4146 case OMPD_declare_mapper: 4147 case OMPD_declare_simd: 4148 case OMPD_declare_target: 4149 case OMPD_end_declare_target: 4150 case OMPD_requires: 4151 case OMPD_declare_variant: 4152 case OMPD_begin_declare_variant: 4153 case OMPD_end_declare_variant: 4154 llvm_unreachable("OpenMP Directive is not allowed"); 4155 case OMPD_unknown: 4156 default: 4157 llvm_unreachable("Unknown OpenMP directive"); 4158 } 4159 DSAStack->setContext(CurContext); 4160 } 4161 4162 int Sema::getNumberOfConstructScopes(unsigned Level) const { 4163 return getOpenMPCaptureLevels(DSAStack->getDirective(Level)); 4164 } 4165 4166 int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) { 4167 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 4168 getOpenMPCaptureRegions(CaptureRegions, DKind); 4169 return CaptureRegions.size(); 4170 } 4171 4172 static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id, 4173 Expr *CaptureExpr, bool WithInit, 4174 bool AsExpression) { 4175 assert(CaptureExpr); 4176 ASTContext &C = S.getASTContext(); 4177 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts(); 4178 QualType Ty = Init->getType(); 4179 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) { 4180 if (S.getLangOpts().CPlusPlus) { 4181 Ty = C.getLValueReferenceType(Ty); 4182 } else { 4183 Ty = C.getPointerType(Ty); 4184 ExprResult Res = 4185 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init); 4186 if (!Res.isUsable()) 4187 return nullptr; 4188 Init = Res.get(); 4189 } 4190 WithInit = true; 4191 } 4192 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty, 4193 CaptureExpr->getBeginLoc()); 4194 if (!WithInit) 4195 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C)); 4196 S.CurContext->addHiddenDecl(CED); 4197 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false); 4198 return CED; 4199 } 4200 4201 static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr, 4202 bool WithInit) { 4203 OMPCapturedExprDecl *CD; 4204 if (VarDecl *VD = S.isOpenMPCapturedDecl(D)) 4205 CD = cast<OMPCapturedExprDecl>(VD); 4206 else 4207 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit, 4208 /*AsExpression=*/false); 4209 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(), 4210 CaptureExpr->getExprLoc()); 4211 } 4212 4213 static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) { 4214 CaptureExpr = S.DefaultLvalueConversion(CaptureExpr).get(); 4215 if (!Ref) { 4216 OMPCapturedExprDecl *CD = buildCaptureDecl( 4217 S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr, 4218 /*WithInit=*/true, /*AsExpression=*/true); 4219 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(), 4220 CaptureExpr->getExprLoc()); 4221 } 4222 ExprResult Res = Ref; 4223 if (!S.getLangOpts().CPlusPlus && 4224 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() && 4225 Ref->getType()->isPointerType()) { 4226 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref); 4227 if (!Res.isUsable()) 4228 return ExprError(); 4229 } 4230 return S.DefaultLvalueConversion(Res.get()); 4231 } 4232 4233 namespace { 4234 // OpenMP directives parsed in this section are represented as a 4235 // CapturedStatement with an associated statement. If a syntax error 4236 // is detected during the parsing of the associated statement, the 4237 // compiler must abort processing and close the CapturedStatement. 4238 // 4239 // Combined directives such as 'target parallel' have more than one 4240 // nested CapturedStatements. This RAII ensures that we unwind out 4241 // of all the nested CapturedStatements when an error is found. 4242 class CaptureRegionUnwinderRAII { 4243 private: 4244 Sema &S; 4245 bool &ErrorFound; 4246 OpenMPDirectiveKind DKind = OMPD_unknown; 4247 4248 public: 4249 CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound, 4250 OpenMPDirectiveKind DKind) 4251 : S(S), ErrorFound(ErrorFound), DKind(DKind) {} 4252 ~CaptureRegionUnwinderRAII() { 4253 if (ErrorFound) { 4254 int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind); 4255 while (--ThisCaptureLevel >= 0) 4256 S.ActOnCapturedRegionError(); 4257 } 4258 } 4259 }; 4260 } // namespace 4261 4262 void Sema::tryCaptureOpenMPLambdas(ValueDecl *V) { 4263 // Capture variables captured by reference in lambdas for target-based 4264 // directives. 4265 if (!CurContext->isDependentContext() && 4266 (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) || 4267 isOpenMPTargetDataManagementDirective( 4268 DSAStack->getCurrentDirective()))) { 4269 QualType Type = V->getType(); 4270 if (const auto *RD = Type.getCanonicalType() 4271 .getNonReferenceType() 4272 ->getAsCXXRecordDecl()) { 4273 bool SavedForceCaptureByReferenceInTargetExecutable = 4274 DSAStack->isForceCaptureByReferenceInTargetExecutable(); 4275 DSAStack->setForceCaptureByReferenceInTargetExecutable( 4276 /*V=*/true); 4277 if (RD->isLambda()) { 4278 llvm::DenseMap<const VarDecl *, FieldDecl *> Captures; 4279 FieldDecl *ThisCapture; 4280 RD->getCaptureFields(Captures, ThisCapture); 4281 for (const LambdaCapture &LC : RD->captures()) { 4282 if (LC.getCaptureKind() == LCK_ByRef) { 4283 VarDecl *VD = LC.getCapturedVar(); 4284 DeclContext *VDC = VD->getDeclContext(); 4285 if (!VDC->Encloses(CurContext)) 4286 continue; 4287 MarkVariableReferenced(LC.getLocation(), VD); 4288 } else if (LC.getCaptureKind() == LCK_This) { 4289 QualType ThisTy = getCurrentThisType(); 4290 if (!ThisTy.isNull() && 4291 Context.typesAreCompatible(ThisTy, ThisCapture->getType())) 4292 CheckCXXThisCapture(LC.getLocation()); 4293 } 4294 } 4295 } 4296 DSAStack->setForceCaptureByReferenceInTargetExecutable( 4297 SavedForceCaptureByReferenceInTargetExecutable); 4298 } 4299 } 4300 } 4301 4302 static bool checkOrderedOrderSpecified(Sema &S, 4303 const ArrayRef<OMPClause *> Clauses) { 4304 const OMPOrderedClause *Ordered = nullptr; 4305 const OMPOrderClause *Order = nullptr; 4306 4307 for (const OMPClause *Clause : Clauses) { 4308 if (Clause->getClauseKind() == OMPC_ordered) 4309 Ordered = cast<OMPOrderedClause>(Clause); 4310 else if (Clause->getClauseKind() == OMPC_order) { 4311 Order = cast<OMPOrderClause>(Clause); 4312 if (Order->getKind() != OMPC_ORDER_concurrent) 4313 Order = nullptr; 4314 } 4315 if (Ordered && Order) 4316 break; 4317 } 4318 4319 if (Ordered && Order) { 4320 S.Diag(Order->getKindKwLoc(), 4321 diag::err_omp_simple_clause_incompatible_with_ordered) 4322 << getOpenMPClauseName(OMPC_order) 4323 << getOpenMPSimpleClauseTypeName(OMPC_order, OMPC_ORDER_concurrent) 4324 << SourceRange(Order->getBeginLoc(), Order->getEndLoc()); 4325 S.Diag(Ordered->getBeginLoc(), diag::note_omp_ordered_param) 4326 << 0 << SourceRange(Ordered->getBeginLoc(), Ordered->getEndLoc()); 4327 return true; 4328 } 4329 return false; 4330 } 4331 4332 StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S, 4333 ArrayRef<OMPClause *> Clauses) { 4334 if (DSAStack->getCurrentDirective() == OMPD_atomic || 4335 DSAStack->getCurrentDirective() == OMPD_critical || 4336 DSAStack->getCurrentDirective() == OMPD_section || 4337 DSAStack->getCurrentDirective() == OMPD_master) 4338 return S; 4339 4340 bool ErrorFound = false; 4341 CaptureRegionUnwinderRAII CaptureRegionUnwinder( 4342 *this, ErrorFound, DSAStack->getCurrentDirective()); 4343 if (!S.isUsable()) { 4344 ErrorFound = true; 4345 return StmtError(); 4346 } 4347 4348 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 4349 getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective()); 4350 OMPOrderedClause *OC = nullptr; 4351 OMPScheduleClause *SC = nullptr; 4352 SmallVector<const OMPLinearClause *, 4> LCs; 4353 SmallVector<const OMPClauseWithPreInit *, 4> PICs; 4354 // This is required for proper codegen. 4355 for (OMPClause *Clause : Clauses) { 4356 if (!LangOpts.OpenMPSimd && 4357 isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) && 4358 Clause->getClauseKind() == OMPC_in_reduction) { 4359 // Capture taskgroup task_reduction descriptors inside the tasking regions 4360 // with the corresponding in_reduction items. 4361 auto *IRC = cast<OMPInReductionClause>(Clause); 4362 for (Expr *E : IRC->taskgroup_descriptors()) 4363 if (E) 4364 MarkDeclarationsReferencedInExpr(E); 4365 } 4366 if (isOpenMPPrivate(Clause->getClauseKind()) || 4367 Clause->getClauseKind() == OMPC_copyprivate || 4368 (getLangOpts().OpenMPUseTLS && 4369 getASTContext().getTargetInfo().isTLSSupported() && 4370 Clause->getClauseKind() == OMPC_copyin)) { 4371 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin); 4372 // Mark all variables in private list clauses as used in inner region. 4373 for (Stmt *VarRef : Clause->children()) { 4374 if (auto *E = cast_or_null<Expr>(VarRef)) { 4375 MarkDeclarationsReferencedInExpr(E); 4376 } 4377 } 4378 DSAStack->setForceVarCapturing(/*V=*/false); 4379 } else if (CaptureRegions.size() > 1 || 4380 CaptureRegions.back() != OMPD_unknown) { 4381 if (auto *C = OMPClauseWithPreInit::get(Clause)) 4382 PICs.push_back(C); 4383 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) { 4384 if (Expr *E = C->getPostUpdateExpr()) 4385 MarkDeclarationsReferencedInExpr(E); 4386 } 4387 } 4388 if (Clause->getClauseKind() == OMPC_schedule) 4389 SC = cast<OMPScheduleClause>(Clause); 4390 else if (Clause->getClauseKind() == OMPC_ordered) 4391 OC = cast<OMPOrderedClause>(Clause); 4392 else if (Clause->getClauseKind() == OMPC_linear) 4393 LCs.push_back(cast<OMPLinearClause>(Clause)); 4394 } 4395 // Capture allocator expressions if used. 4396 for (Expr *E : DSAStack->getInnerAllocators()) 4397 MarkDeclarationsReferencedInExpr(E); 4398 // OpenMP, 2.7.1 Loop Construct, Restrictions 4399 // The nonmonotonic modifier cannot be specified if an ordered clause is 4400 // specified. 4401 if (SC && 4402 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic || 4403 SC->getSecondScheduleModifier() == 4404 OMPC_SCHEDULE_MODIFIER_nonmonotonic) && 4405 OC) { 4406 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic 4407 ? SC->getFirstScheduleModifierLoc() 4408 : SC->getSecondScheduleModifierLoc(), 4409 diag::err_omp_simple_clause_incompatible_with_ordered) 4410 << getOpenMPClauseName(OMPC_schedule) 4411 << getOpenMPSimpleClauseTypeName(OMPC_schedule, 4412 OMPC_SCHEDULE_MODIFIER_nonmonotonic) 4413 << SourceRange(OC->getBeginLoc(), OC->getEndLoc()); 4414 ErrorFound = true; 4415 } 4416 // OpenMP 5.0, 2.9.2 Worksharing-Loop Construct, Restrictions. 4417 // If an order(concurrent) clause is present, an ordered clause may not appear 4418 // on the same directive. 4419 if (checkOrderedOrderSpecified(*this, Clauses)) 4420 ErrorFound = true; 4421 if (!LCs.empty() && OC && OC->getNumForLoops()) { 4422 for (const OMPLinearClause *C : LCs) { 4423 Diag(C->getBeginLoc(), diag::err_omp_linear_ordered) 4424 << SourceRange(OC->getBeginLoc(), OC->getEndLoc()); 4425 } 4426 ErrorFound = true; 4427 } 4428 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) && 4429 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC && 4430 OC->getNumForLoops()) { 4431 Diag(OC->getBeginLoc(), diag::err_omp_ordered_simd) 4432 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 4433 ErrorFound = true; 4434 } 4435 if (ErrorFound) { 4436 return StmtError(); 4437 } 4438 StmtResult SR = S; 4439 unsigned CompletedRegions = 0; 4440 for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(CaptureRegions)) { 4441 // Mark all variables in private list clauses as used in inner region. 4442 // Required for proper codegen of combined directives. 4443 // TODO: add processing for other clauses. 4444 if (ThisCaptureRegion != OMPD_unknown) { 4445 for (const clang::OMPClauseWithPreInit *C : PICs) { 4446 OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion(); 4447 // Find the particular capture region for the clause if the 4448 // directive is a combined one with multiple capture regions. 4449 // If the directive is not a combined one, the capture region 4450 // associated with the clause is OMPD_unknown and is generated 4451 // only once. 4452 if (CaptureRegion == ThisCaptureRegion || 4453 CaptureRegion == OMPD_unknown) { 4454 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) { 4455 for (Decl *D : DS->decls()) 4456 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D)); 4457 } 4458 } 4459 } 4460 } 4461 if (ThisCaptureRegion == OMPD_target) { 4462 // Capture allocator traits in the target region. They are used implicitly 4463 // and, thus, are not captured by default. 4464 for (OMPClause *C : Clauses) { 4465 if (const auto *UAC = dyn_cast<OMPUsesAllocatorsClause>(C)) { 4466 for (unsigned I = 0, End = UAC->getNumberOfAllocators(); I < End; 4467 ++I) { 4468 OMPUsesAllocatorsClause::Data D = UAC->getAllocatorData(I); 4469 if (Expr *E = D.AllocatorTraits) 4470 MarkDeclarationsReferencedInExpr(E); 4471 } 4472 continue; 4473 } 4474 } 4475 } 4476 if (++CompletedRegions == CaptureRegions.size()) 4477 DSAStack->setBodyComplete(); 4478 SR = ActOnCapturedRegionEnd(SR.get()); 4479 } 4480 return SR; 4481 } 4482 4483 static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion, 4484 OpenMPDirectiveKind CancelRegion, 4485 SourceLocation StartLoc) { 4486 // CancelRegion is only needed for cancel and cancellation_point. 4487 if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point) 4488 return false; 4489 4490 if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for || 4491 CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup) 4492 return false; 4493 4494 SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region) 4495 << getOpenMPDirectiveName(CancelRegion); 4496 return true; 4497 } 4498 4499 static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack, 4500 OpenMPDirectiveKind CurrentRegion, 4501 const DeclarationNameInfo &CurrentName, 4502 OpenMPDirectiveKind CancelRegion, 4503 SourceLocation StartLoc) { 4504 if (Stack->getCurScope()) { 4505 OpenMPDirectiveKind ParentRegion = Stack->getParentDirective(); 4506 OpenMPDirectiveKind OffendingRegion = ParentRegion; 4507 bool NestingProhibited = false; 4508 bool CloseNesting = true; 4509 bool OrphanSeen = false; 4510 enum { 4511 NoRecommend, 4512 ShouldBeInParallelRegion, 4513 ShouldBeInOrderedRegion, 4514 ShouldBeInTargetRegion, 4515 ShouldBeInTeamsRegion, 4516 ShouldBeInLoopSimdRegion, 4517 } Recommend = NoRecommend; 4518 if (isOpenMPSimdDirective(ParentRegion) && 4519 ((SemaRef.LangOpts.OpenMP <= 45 && CurrentRegion != OMPD_ordered) || 4520 (SemaRef.LangOpts.OpenMP >= 50 && CurrentRegion != OMPD_ordered && 4521 CurrentRegion != OMPD_simd && CurrentRegion != OMPD_atomic && 4522 CurrentRegion != OMPD_scan))) { 4523 // OpenMP [2.16, Nesting of Regions] 4524 // OpenMP constructs may not be nested inside a simd region. 4525 // OpenMP [2.8.1,simd Construct, Restrictions] 4526 // An ordered construct with the simd clause is the only OpenMP 4527 // construct that can appear in the simd region. 4528 // Allowing a SIMD construct nested in another SIMD construct is an 4529 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning 4530 // message. 4531 // OpenMP 5.0 [2.9.3.1, simd Construct, Restrictions] 4532 // The only OpenMP constructs that can be encountered during execution of 4533 // a simd region are the atomic construct, the loop construct, the simd 4534 // construct and the ordered construct with the simd clause. 4535 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd) 4536 ? diag::err_omp_prohibited_region_simd 4537 : diag::warn_omp_nesting_simd) 4538 << (SemaRef.LangOpts.OpenMP >= 50 ? 1 : 0); 4539 return CurrentRegion != OMPD_simd; 4540 } 4541 if (ParentRegion == OMPD_atomic) { 4542 // OpenMP [2.16, Nesting of Regions] 4543 // OpenMP constructs may not be nested inside an atomic region. 4544 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic); 4545 return true; 4546 } 4547 if (CurrentRegion == OMPD_section) { 4548 // OpenMP [2.7.2, sections Construct, Restrictions] 4549 // Orphaned section directives are prohibited. That is, the section 4550 // directives must appear within the sections construct and must not be 4551 // encountered elsewhere in the sections region. 4552 if (ParentRegion != OMPD_sections && 4553 ParentRegion != OMPD_parallel_sections) { 4554 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive) 4555 << (ParentRegion != OMPD_unknown) 4556 << getOpenMPDirectiveName(ParentRegion); 4557 return true; 4558 } 4559 return false; 4560 } 4561 // Allow some constructs (except teams and cancellation constructs) to be 4562 // orphaned (they could be used in functions, called from OpenMP regions 4563 // with the required preconditions). 4564 if (ParentRegion == OMPD_unknown && 4565 !isOpenMPNestingTeamsDirective(CurrentRegion) && 4566 CurrentRegion != OMPD_cancellation_point && 4567 CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_scan) 4568 return false; 4569 if (CurrentRegion == OMPD_cancellation_point || 4570 CurrentRegion == OMPD_cancel) { 4571 // OpenMP [2.16, Nesting of Regions] 4572 // A cancellation point construct for which construct-type-clause is 4573 // taskgroup must be nested inside a task construct. A cancellation 4574 // point construct for which construct-type-clause is not taskgroup must 4575 // be closely nested inside an OpenMP construct that matches the type 4576 // specified in construct-type-clause. 4577 // A cancel construct for which construct-type-clause is taskgroup must be 4578 // nested inside a task construct. A cancel construct for which 4579 // construct-type-clause is not taskgroup must be closely nested inside an 4580 // OpenMP construct that matches the type specified in 4581 // construct-type-clause. 4582 NestingProhibited = 4583 !((CancelRegion == OMPD_parallel && 4584 (ParentRegion == OMPD_parallel || 4585 ParentRegion == OMPD_target_parallel)) || 4586 (CancelRegion == OMPD_for && 4587 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for || 4588 ParentRegion == OMPD_target_parallel_for || 4589 ParentRegion == OMPD_distribute_parallel_for || 4590 ParentRegion == OMPD_teams_distribute_parallel_for || 4591 ParentRegion == OMPD_target_teams_distribute_parallel_for)) || 4592 (CancelRegion == OMPD_taskgroup && 4593 (ParentRegion == OMPD_task || 4594 (SemaRef.getLangOpts().OpenMP >= 50 && 4595 (ParentRegion == OMPD_taskloop || 4596 ParentRegion == OMPD_master_taskloop || 4597 ParentRegion == OMPD_parallel_master_taskloop)))) || 4598 (CancelRegion == OMPD_sections && 4599 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections || 4600 ParentRegion == OMPD_parallel_sections))); 4601 OrphanSeen = ParentRegion == OMPD_unknown; 4602 } else if (CurrentRegion == OMPD_master) { 4603 // OpenMP [2.16, Nesting of Regions] 4604 // A master region may not be closely nested inside a worksharing, 4605 // atomic, or explicit task region. 4606 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 4607 isOpenMPTaskingDirective(ParentRegion); 4608 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) { 4609 // OpenMP [2.16, Nesting of Regions] 4610 // A critical region may not be nested (closely or otherwise) inside a 4611 // critical region with the same name. Note that this restriction is not 4612 // sufficient to prevent deadlock. 4613 SourceLocation PreviousCriticalLoc; 4614 bool DeadLock = Stack->hasDirective( 4615 [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K, 4616 const DeclarationNameInfo &DNI, 4617 SourceLocation Loc) { 4618 if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) { 4619 PreviousCriticalLoc = Loc; 4620 return true; 4621 } 4622 return false; 4623 }, 4624 false /* skip top directive */); 4625 if (DeadLock) { 4626 SemaRef.Diag(StartLoc, 4627 diag::err_omp_prohibited_region_critical_same_name) 4628 << CurrentName.getName(); 4629 if (PreviousCriticalLoc.isValid()) 4630 SemaRef.Diag(PreviousCriticalLoc, 4631 diag::note_omp_previous_critical_region); 4632 return true; 4633 } 4634 } else if (CurrentRegion == OMPD_barrier) { 4635 // OpenMP [2.16, Nesting of Regions] 4636 // A barrier region may not be closely nested inside a worksharing, 4637 // explicit task, critical, ordered, atomic, or master region. 4638 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 4639 isOpenMPTaskingDirective(ParentRegion) || 4640 ParentRegion == OMPD_master || 4641 ParentRegion == OMPD_parallel_master || 4642 ParentRegion == OMPD_critical || 4643 ParentRegion == OMPD_ordered; 4644 } else if (isOpenMPWorksharingDirective(CurrentRegion) && 4645 !isOpenMPParallelDirective(CurrentRegion) && 4646 !isOpenMPTeamsDirective(CurrentRegion)) { 4647 // OpenMP [2.16, Nesting of Regions] 4648 // A worksharing region may not be closely nested inside a worksharing, 4649 // explicit task, critical, ordered, atomic, or master region. 4650 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 4651 isOpenMPTaskingDirective(ParentRegion) || 4652 ParentRegion == OMPD_master || 4653 ParentRegion == OMPD_parallel_master || 4654 ParentRegion == OMPD_critical || 4655 ParentRegion == OMPD_ordered; 4656 Recommend = ShouldBeInParallelRegion; 4657 } else if (CurrentRegion == OMPD_ordered) { 4658 // OpenMP [2.16, Nesting of Regions] 4659 // An ordered region may not be closely nested inside a critical, 4660 // atomic, or explicit task region. 4661 // An ordered region must be closely nested inside a loop region (or 4662 // parallel loop region) with an ordered clause. 4663 // OpenMP [2.8.1,simd Construct, Restrictions] 4664 // An ordered construct with the simd clause is the only OpenMP construct 4665 // that can appear in the simd region. 4666 NestingProhibited = ParentRegion == OMPD_critical || 4667 isOpenMPTaskingDirective(ParentRegion) || 4668 !(isOpenMPSimdDirective(ParentRegion) || 4669 Stack->isParentOrderedRegion()); 4670 Recommend = ShouldBeInOrderedRegion; 4671 } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) { 4672 // OpenMP [2.16, Nesting of Regions] 4673 // If specified, a teams construct must be contained within a target 4674 // construct. 4675 NestingProhibited = 4676 (SemaRef.LangOpts.OpenMP <= 45 && ParentRegion != OMPD_target) || 4677 (SemaRef.LangOpts.OpenMP >= 50 && ParentRegion != OMPD_unknown && 4678 ParentRegion != OMPD_target); 4679 OrphanSeen = ParentRegion == OMPD_unknown; 4680 Recommend = ShouldBeInTargetRegion; 4681 } else if (CurrentRegion == OMPD_scan) { 4682 // OpenMP [2.16, Nesting of Regions] 4683 // If specified, a teams construct must be contained within a target 4684 // construct. 4685 NestingProhibited = 4686 SemaRef.LangOpts.OpenMP < 50 || 4687 (ParentRegion != OMPD_simd && ParentRegion != OMPD_for && 4688 ParentRegion != OMPD_for_simd && ParentRegion != OMPD_parallel_for && 4689 ParentRegion != OMPD_parallel_for_simd); 4690 OrphanSeen = ParentRegion == OMPD_unknown; 4691 Recommend = ShouldBeInLoopSimdRegion; 4692 } 4693 if (!NestingProhibited && 4694 !isOpenMPTargetExecutionDirective(CurrentRegion) && 4695 !isOpenMPTargetDataManagementDirective(CurrentRegion) && 4696 (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) { 4697 // OpenMP [2.16, Nesting of Regions] 4698 // distribute, parallel, parallel sections, parallel workshare, and the 4699 // parallel loop and parallel loop SIMD constructs are the only OpenMP 4700 // constructs that can be closely nested in the teams region. 4701 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) && 4702 !isOpenMPDistributeDirective(CurrentRegion); 4703 Recommend = ShouldBeInParallelRegion; 4704 } 4705 if (!NestingProhibited && 4706 isOpenMPNestingDistributeDirective(CurrentRegion)) { 4707 // OpenMP 4.5 [2.17 Nesting of Regions] 4708 // The region associated with the distribute construct must be strictly 4709 // nested inside a teams region 4710 NestingProhibited = 4711 (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams); 4712 Recommend = ShouldBeInTeamsRegion; 4713 } 4714 if (!NestingProhibited && 4715 (isOpenMPTargetExecutionDirective(CurrentRegion) || 4716 isOpenMPTargetDataManagementDirective(CurrentRegion))) { 4717 // OpenMP 4.5 [2.17 Nesting of Regions] 4718 // If a target, target update, target data, target enter data, or 4719 // target exit data construct is encountered during execution of a 4720 // target region, the behavior is unspecified. 4721 NestingProhibited = Stack->hasDirective( 4722 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &, 4723 SourceLocation) { 4724 if (isOpenMPTargetExecutionDirective(K)) { 4725 OffendingRegion = K; 4726 return true; 4727 } 4728 return false; 4729 }, 4730 false /* don't skip top directive */); 4731 CloseNesting = false; 4732 } 4733 if (NestingProhibited) { 4734 if (OrphanSeen) { 4735 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive) 4736 << getOpenMPDirectiveName(CurrentRegion) << Recommend; 4737 } else { 4738 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region) 4739 << CloseNesting << getOpenMPDirectiveName(OffendingRegion) 4740 << Recommend << getOpenMPDirectiveName(CurrentRegion); 4741 } 4742 return true; 4743 } 4744 } 4745 return false; 4746 } 4747 4748 struct Kind2Unsigned { 4749 using argument_type = OpenMPDirectiveKind; 4750 unsigned operator()(argument_type DK) { return unsigned(DK); } 4751 }; 4752 static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind, 4753 ArrayRef<OMPClause *> Clauses, 4754 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) { 4755 bool ErrorFound = false; 4756 unsigned NamedModifiersNumber = 0; 4757 llvm::IndexedMap<const OMPIfClause *, Kind2Unsigned> FoundNameModifiers; 4758 FoundNameModifiers.resize(llvm::omp::Directive_enumSize + 1); 4759 SmallVector<SourceLocation, 4> NameModifierLoc; 4760 for (const OMPClause *C : Clauses) { 4761 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) { 4762 // At most one if clause without a directive-name-modifier can appear on 4763 // the directive. 4764 OpenMPDirectiveKind CurNM = IC->getNameModifier(); 4765 if (FoundNameModifiers[CurNM]) { 4766 S.Diag(C->getBeginLoc(), diag::err_omp_more_one_clause) 4767 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if) 4768 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM); 4769 ErrorFound = true; 4770 } else if (CurNM != OMPD_unknown) { 4771 NameModifierLoc.push_back(IC->getNameModifierLoc()); 4772 ++NamedModifiersNumber; 4773 } 4774 FoundNameModifiers[CurNM] = IC; 4775 if (CurNM == OMPD_unknown) 4776 continue; 4777 // Check if the specified name modifier is allowed for the current 4778 // directive. 4779 // At most one if clause with the particular directive-name-modifier can 4780 // appear on the directive. 4781 bool MatchFound = false; 4782 for (auto NM : AllowedNameModifiers) { 4783 if (CurNM == NM) { 4784 MatchFound = true; 4785 break; 4786 } 4787 } 4788 if (!MatchFound) { 4789 S.Diag(IC->getNameModifierLoc(), 4790 diag::err_omp_wrong_if_directive_name_modifier) 4791 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind); 4792 ErrorFound = true; 4793 } 4794 } 4795 } 4796 // If any if clause on the directive includes a directive-name-modifier then 4797 // all if clauses on the directive must include a directive-name-modifier. 4798 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) { 4799 if (NamedModifiersNumber == AllowedNameModifiers.size()) { 4800 S.Diag(FoundNameModifiers[OMPD_unknown]->getBeginLoc(), 4801 diag::err_omp_no_more_if_clause); 4802 } else { 4803 std::string Values; 4804 std::string Sep(", "); 4805 unsigned AllowedCnt = 0; 4806 unsigned TotalAllowedNum = 4807 AllowedNameModifiers.size() - NamedModifiersNumber; 4808 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End; 4809 ++Cnt) { 4810 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt]; 4811 if (!FoundNameModifiers[NM]) { 4812 Values += "'"; 4813 Values += getOpenMPDirectiveName(NM); 4814 Values += "'"; 4815 if (AllowedCnt + 2 == TotalAllowedNum) 4816 Values += " or "; 4817 else if (AllowedCnt + 1 != TotalAllowedNum) 4818 Values += Sep; 4819 ++AllowedCnt; 4820 } 4821 } 4822 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getBeginLoc(), 4823 diag::err_omp_unnamed_if_clause) 4824 << (TotalAllowedNum > 1) << Values; 4825 } 4826 for (SourceLocation Loc : NameModifierLoc) { 4827 S.Diag(Loc, diag::note_omp_previous_named_if_clause); 4828 } 4829 ErrorFound = true; 4830 } 4831 return ErrorFound; 4832 } 4833 4834 static std::pair<ValueDecl *, bool> getPrivateItem(Sema &S, Expr *&RefExpr, 4835 SourceLocation &ELoc, 4836 SourceRange &ERange, 4837 bool AllowArraySection) { 4838 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() || 4839 RefExpr->containsUnexpandedParameterPack()) 4840 return std::make_pair(nullptr, true); 4841 4842 // OpenMP [3.1, C/C++] 4843 // A list item is a variable name. 4844 // OpenMP [2.9.3.3, Restrictions, p.1] 4845 // A variable that is part of another variable (as an array or 4846 // structure element) cannot appear in a private clause. 4847 RefExpr = RefExpr->IgnoreParens(); 4848 enum { 4849 NoArrayExpr = -1, 4850 ArraySubscript = 0, 4851 OMPArraySection = 1 4852 } IsArrayExpr = NoArrayExpr; 4853 if (AllowArraySection) { 4854 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) { 4855 Expr *Base = ASE->getBase()->IgnoreParenImpCasts(); 4856 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 4857 Base = TempASE->getBase()->IgnoreParenImpCasts(); 4858 RefExpr = Base; 4859 IsArrayExpr = ArraySubscript; 4860 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) { 4861 Expr *Base = OASE->getBase()->IgnoreParenImpCasts(); 4862 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) 4863 Base = TempOASE->getBase()->IgnoreParenImpCasts(); 4864 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 4865 Base = TempASE->getBase()->IgnoreParenImpCasts(); 4866 RefExpr = Base; 4867 IsArrayExpr = OMPArraySection; 4868 } 4869 } 4870 ELoc = RefExpr->getExprLoc(); 4871 ERange = RefExpr->getSourceRange(); 4872 RefExpr = RefExpr->IgnoreParenImpCasts(); 4873 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr); 4874 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr); 4875 if ((!DE || !isa<VarDecl>(DE->getDecl())) && 4876 (S.getCurrentThisType().isNull() || !ME || 4877 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) || 4878 !isa<FieldDecl>(ME->getMemberDecl()))) { 4879 if (IsArrayExpr != NoArrayExpr) { 4880 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr 4881 << ERange; 4882 } else { 4883 S.Diag(ELoc, 4884 AllowArraySection 4885 ? diag::err_omp_expected_var_name_member_expr_or_array_item 4886 : diag::err_omp_expected_var_name_member_expr) 4887 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange; 4888 } 4889 return std::make_pair(nullptr, false); 4890 } 4891 return std::make_pair( 4892 getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false); 4893 } 4894 4895 namespace { 4896 /// Checks if the allocator is used in uses_allocators clause to be allowed in 4897 /// target regions. 4898 class AllocatorChecker final : public ConstStmtVisitor<AllocatorChecker, bool> { 4899 DSAStackTy *S = nullptr; 4900 4901 public: 4902 bool VisitDeclRefExpr(const DeclRefExpr *E) { 4903 return S->isUsesAllocatorsDecl(E->getDecl()) 4904 .getValueOr( 4905 DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait) == 4906 DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait; 4907 } 4908 bool VisitStmt(const Stmt *S) { 4909 for (const Stmt *Child : S->children()) { 4910 if (Child && Visit(Child)) 4911 return true; 4912 } 4913 return false; 4914 } 4915 explicit AllocatorChecker(DSAStackTy *S) : S(S) {} 4916 }; 4917 } // namespace 4918 4919 static void checkAllocateClauses(Sema &S, DSAStackTy *Stack, 4920 ArrayRef<OMPClause *> Clauses) { 4921 assert(!S.CurContext->isDependentContext() && 4922 "Expected non-dependent context."); 4923 auto AllocateRange = 4924 llvm::make_filter_range(Clauses, OMPAllocateClause::classof); 4925 llvm::DenseMap<CanonicalDeclPtr<Decl>, CanonicalDeclPtr<VarDecl>> 4926 DeclToCopy; 4927 auto PrivateRange = llvm::make_filter_range(Clauses, [](const OMPClause *C) { 4928 return isOpenMPPrivate(C->getClauseKind()); 4929 }); 4930 for (OMPClause *Cl : PrivateRange) { 4931 MutableArrayRef<Expr *>::iterator I, It, Et; 4932 if (Cl->getClauseKind() == OMPC_private) { 4933 auto *PC = cast<OMPPrivateClause>(Cl); 4934 I = PC->private_copies().begin(); 4935 It = PC->varlist_begin(); 4936 Et = PC->varlist_end(); 4937 } else if (Cl->getClauseKind() == OMPC_firstprivate) { 4938 auto *PC = cast<OMPFirstprivateClause>(Cl); 4939 I = PC->private_copies().begin(); 4940 It = PC->varlist_begin(); 4941 Et = PC->varlist_end(); 4942 } else if (Cl->getClauseKind() == OMPC_lastprivate) { 4943 auto *PC = cast<OMPLastprivateClause>(Cl); 4944 I = PC->private_copies().begin(); 4945 It = PC->varlist_begin(); 4946 Et = PC->varlist_end(); 4947 } else if (Cl->getClauseKind() == OMPC_linear) { 4948 auto *PC = cast<OMPLinearClause>(Cl); 4949 I = PC->privates().begin(); 4950 It = PC->varlist_begin(); 4951 Et = PC->varlist_end(); 4952 } else if (Cl->getClauseKind() == OMPC_reduction) { 4953 auto *PC = cast<OMPReductionClause>(Cl); 4954 I = PC->privates().begin(); 4955 It = PC->varlist_begin(); 4956 Et = PC->varlist_end(); 4957 } else if (Cl->getClauseKind() == OMPC_task_reduction) { 4958 auto *PC = cast<OMPTaskReductionClause>(Cl); 4959 I = PC->privates().begin(); 4960 It = PC->varlist_begin(); 4961 Et = PC->varlist_end(); 4962 } else if (Cl->getClauseKind() == OMPC_in_reduction) { 4963 auto *PC = cast<OMPInReductionClause>(Cl); 4964 I = PC->privates().begin(); 4965 It = PC->varlist_begin(); 4966 Et = PC->varlist_end(); 4967 } else { 4968 llvm_unreachable("Expected private clause."); 4969 } 4970 for (Expr *E : llvm::make_range(It, Et)) { 4971 if (!*I) { 4972 ++I; 4973 continue; 4974 } 4975 SourceLocation ELoc; 4976 SourceRange ERange; 4977 Expr *SimpleRefExpr = E; 4978 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange, 4979 /*AllowArraySection=*/true); 4980 DeclToCopy.try_emplace(Res.first, 4981 cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl())); 4982 ++I; 4983 } 4984 } 4985 for (OMPClause *C : AllocateRange) { 4986 auto *AC = cast<OMPAllocateClause>(C); 4987 if (S.getLangOpts().OpenMP >= 50 && 4988 !Stack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>() && 4989 isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) && 4990 AC->getAllocator()) { 4991 Expr *Allocator = AC->getAllocator(); 4992 // OpenMP, 2.12.5 target Construct 4993 // Memory allocators that do not appear in a uses_allocators clause cannot 4994 // appear as an allocator in an allocate clause or be used in the target 4995 // region unless a requires directive with the dynamic_allocators clause 4996 // is present in the same compilation unit. 4997 AllocatorChecker Checker(Stack); 4998 if (Checker.Visit(Allocator)) 4999 S.Diag(Allocator->getExprLoc(), 5000 diag::err_omp_allocator_not_in_uses_allocators) 5001 << Allocator->getSourceRange(); 5002 } 5003 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind = 5004 getAllocatorKind(S, Stack, AC->getAllocator()); 5005 // OpenMP, 2.11.4 allocate Clause, Restrictions. 5006 // For task, taskloop or target directives, allocation requests to memory 5007 // allocators with the trait access set to thread result in unspecified 5008 // behavior. 5009 if (AllocatorKind == OMPAllocateDeclAttr::OMPThreadMemAlloc && 5010 (isOpenMPTaskingDirective(Stack->getCurrentDirective()) || 5011 isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()))) { 5012 S.Diag(AC->getAllocator()->getExprLoc(), 5013 diag::warn_omp_allocate_thread_on_task_target_directive) 5014 << getOpenMPDirectiveName(Stack->getCurrentDirective()); 5015 } 5016 for (Expr *E : AC->varlists()) { 5017 SourceLocation ELoc; 5018 SourceRange ERange; 5019 Expr *SimpleRefExpr = E; 5020 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange); 5021 ValueDecl *VD = Res.first; 5022 DSAStackTy::DSAVarData Data = Stack->getTopDSA(VD, /*FromParent=*/false); 5023 if (!isOpenMPPrivate(Data.CKind)) { 5024 S.Diag(E->getExprLoc(), 5025 diag::err_omp_expected_private_copy_for_allocate); 5026 continue; 5027 } 5028 VarDecl *PrivateVD = DeclToCopy[VD]; 5029 if (checkPreviousOMPAllocateAttribute(S, Stack, E, PrivateVD, 5030 AllocatorKind, AC->getAllocator())) 5031 continue; 5032 applyOMPAllocateAttribute(S, PrivateVD, AllocatorKind, AC->getAllocator(), 5033 E->getSourceRange()); 5034 } 5035 } 5036 } 5037 5038 StmtResult Sema::ActOnOpenMPExecutableDirective( 5039 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName, 5040 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses, 5041 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { 5042 StmtResult Res = StmtError(); 5043 // First check CancelRegion which is then used in checkNestingOfRegions. 5044 if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) || 5045 checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion, 5046 StartLoc)) 5047 return StmtError(); 5048 5049 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit; 5050 VarsWithInheritedDSAType VarsWithInheritedDSA; 5051 bool ErrorFound = false; 5052 ClausesWithImplicit.append(Clauses.begin(), Clauses.end()); 5053 if (AStmt && !CurContext->isDependentContext() && Kind != OMPD_atomic && 5054 Kind != OMPD_critical && Kind != OMPD_section && Kind != OMPD_master) { 5055 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5056 5057 // Check default data sharing attributes for referenced variables. 5058 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt)); 5059 int ThisCaptureLevel = getOpenMPCaptureLevels(Kind); 5060 Stmt *S = AStmt; 5061 while (--ThisCaptureLevel >= 0) 5062 S = cast<CapturedStmt>(S)->getCapturedStmt(); 5063 DSAChecker.Visit(S); 5064 if (!isOpenMPTargetDataManagementDirective(Kind) && 5065 !isOpenMPTaskingDirective(Kind)) { 5066 // Visit subcaptures to generate implicit clauses for captured vars. 5067 auto *CS = cast<CapturedStmt>(AStmt); 5068 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 5069 getOpenMPCaptureRegions(CaptureRegions, Kind); 5070 // Ignore outer tasking regions for target directives. 5071 if (CaptureRegions.size() > 1 && CaptureRegions.front() == OMPD_task) 5072 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 5073 DSAChecker.visitSubCaptures(CS); 5074 } 5075 if (DSAChecker.isErrorFound()) 5076 return StmtError(); 5077 // Generate list of implicitly defined firstprivate variables. 5078 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA(); 5079 5080 SmallVector<Expr *, 4> ImplicitFirstprivates( 5081 DSAChecker.getImplicitFirstprivate().begin(), 5082 DSAChecker.getImplicitFirstprivate().end()); 5083 SmallVector<Expr *, 4> ImplicitMaps[OMPC_MAP_delete]; 5084 for (unsigned I = 0; I < OMPC_MAP_delete; ++I) { 5085 ArrayRef<Expr *> ImplicitMap = 5086 DSAChecker.getImplicitMap(static_cast<OpenMPDefaultmapClauseKind>(I)); 5087 ImplicitMaps[I].append(ImplicitMap.begin(), ImplicitMap.end()); 5088 } 5089 // Mark taskgroup task_reduction descriptors as implicitly firstprivate. 5090 for (OMPClause *C : Clauses) { 5091 if (auto *IRC = dyn_cast<OMPInReductionClause>(C)) { 5092 for (Expr *E : IRC->taskgroup_descriptors()) 5093 if (E) 5094 ImplicitFirstprivates.emplace_back(E); 5095 } 5096 // OpenMP 5.0, 2.10.1 task Construct 5097 // [detach clause]... The event-handle will be considered as if it was 5098 // specified on a firstprivate clause. 5099 if (auto *DC = dyn_cast<OMPDetachClause>(C)) 5100 ImplicitFirstprivates.push_back(DC->getEventHandler()); 5101 } 5102 if (!ImplicitFirstprivates.empty()) { 5103 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause( 5104 ImplicitFirstprivates, SourceLocation(), SourceLocation(), 5105 SourceLocation())) { 5106 ClausesWithImplicit.push_back(Implicit); 5107 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() != 5108 ImplicitFirstprivates.size(); 5109 } else { 5110 ErrorFound = true; 5111 } 5112 } 5113 int ClauseKindCnt = -1; 5114 for (ArrayRef<Expr *> ImplicitMap : ImplicitMaps) { 5115 ++ClauseKindCnt; 5116 if (ImplicitMap.empty()) 5117 continue; 5118 CXXScopeSpec MapperIdScopeSpec; 5119 DeclarationNameInfo MapperId; 5120 auto Kind = static_cast<OpenMPMapClauseKind>(ClauseKindCnt); 5121 if (OMPClause *Implicit = ActOnOpenMPMapClause( 5122 llvm::None, llvm::None, MapperIdScopeSpec, MapperId, Kind, 5123 /*IsMapTypeImplicit=*/true, SourceLocation(), SourceLocation(), 5124 ImplicitMap, OMPVarListLocTy())) { 5125 ClausesWithImplicit.emplace_back(Implicit); 5126 ErrorFound |= 5127 cast<OMPMapClause>(Implicit)->varlist_size() != ImplicitMap.size(); 5128 } else { 5129 ErrorFound = true; 5130 } 5131 } 5132 } 5133 5134 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers; 5135 switch (Kind) { 5136 case OMPD_parallel: 5137 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc, 5138 EndLoc); 5139 AllowedNameModifiers.push_back(OMPD_parallel); 5140 break; 5141 case OMPD_simd: 5142 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, 5143 VarsWithInheritedDSA); 5144 if (LangOpts.OpenMP >= 50) 5145 AllowedNameModifiers.push_back(OMPD_simd); 5146 break; 5147 case OMPD_for: 5148 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, 5149 VarsWithInheritedDSA); 5150 break; 5151 case OMPD_for_simd: 5152 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 5153 EndLoc, VarsWithInheritedDSA); 5154 if (LangOpts.OpenMP >= 50) 5155 AllowedNameModifiers.push_back(OMPD_simd); 5156 break; 5157 case OMPD_sections: 5158 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc, 5159 EndLoc); 5160 break; 5161 case OMPD_section: 5162 assert(ClausesWithImplicit.empty() && 5163 "No clauses are allowed for 'omp section' directive"); 5164 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc); 5165 break; 5166 case OMPD_single: 5167 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc, 5168 EndLoc); 5169 break; 5170 case OMPD_master: 5171 assert(ClausesWithImplicit.empty() && 5172 "No clauses are allowed for 'omp master' directive"); 5173 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc); 5174 break; 5175 case OMPD_critical: 5176 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt, 5177 StartLoc, EndLoc); 5178 break; 5179 case OMPD_parallel_for: 5180 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc, 5181 EndLoc, VarsWithInheritedDSA); 5182 AllowedNameModifiers.push_back(OMPD_parallel); 5183 break; 5184 case OMPD_parallel_for_simd: 5185 Res = ActOnOpenMPParallelForSimdDirective( 5186 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5187 AllowedNameModifiers.push_back(OMPD_parallel); 5188 if (LangOpts.OpenMP >= 50) 5189 AllowedNameModifiers.push_back(OMPD_simd); 5190 break; 5191 case OMPD_parallel_master: 5192 Res = ActOnOpenMPParallelMasterDirective(ClausesWithImplicit, AStmt, 5193 StartLoc, EndLoc); 5194 AllowedNameModifiers.push_back(OMPD_parallel); 5195 break; 5196 case OMPD_parallel_sections: 5197 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt, 5198 StartLoc, EndLoc); 5199 AllowedNameModifiers.push_back(OMPD_parallel); 5200 break; 5201 case OMPD_task: 5202 Res = 5203 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 5204 AllowedNameModifiers.push_back(OMPD_task); 5205 break; 5206 case OMPD_taskyield: 5207 assert(ClausesWithImplicit.empty() && 5208 "No clauses are allowed for 'omp taskyield' directive"); 5209 assert(AStmt == nullptr && 5210 "No associated statement allowed for 'omp taskyield' directive"); 5211 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc); 5212 break; 5213 case OMPD_barrier: 5214 assert(ClausesWithImplicit.empty() && 5215 "No clauses are allowed for 'omp barrier' directive"); 5216 assert(AStmt == nullptr && 5217 "No associated statement allowed for 'omp barrier' directive"); 5218 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc); 5219 break; 5220 case OMPD_taskwait: 5221 assert(ClausesWithImplicit.empty() && 5222 "No clauses are allowed for 'omp taskwait' directive"); 5223 assert(AStmt == nullptr && 5224 "No associated statement allowed for 'omp taskwait' directive"); 5225 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc); 5226 break; 5227 case OMPD_taskgroup: 5228 Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc, 5229 EndLoc); 5230 break; 5231 case OMPD_flush: 5232 assert(AStmt == nullptr && 5233 "No associated statement allowed for 'omp flush' directive"); 5234 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc); 5235 break; 5236 case OMPD_depobj: 5237 assert(AStmt == nullptr && 5238 "No associated statement allowed for 'omp depobj' directive"); 5239 Res = ActOnOpenMPDepobjDirective(ClausesWithImplicit, StartLoc, EndLoc); 5240 break; 5241 case OMPD_scan: 5242 assert(AStmt == nullptr && 5243 "No associated statement allowed for 'omp scan' directive"); 5244 Res = ActOnOpenMPScanDirective(ClausesWithImplicit, StartLoc, EndLoc); 5245 break; 5246 case OMPD_ordered: 5247 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc, 5248 EndLoc); 5249 break; 5250 case OMPD_atomic: 5251 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc, 5252 EndLoc); 5253 break; 5254 case OMPD_teams: 5255 Res = 5256 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 5257 break; 5258 case OMPD_target: 5259 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc, 5260 EndLoc); 5261 AllowedNameModifiers.push_back(OMPD_target); 5262 break; 5263 case OMPD_target_parallel: 5264 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt, 5265 StartLoc, EndLoc); 5266 AllowedNameModifiers.push_back(OMPD_target); 5267 AllowedNameModifiers.push_back(OMPD_parallel); 5268 break; 5269 case OMPD_target_parallel_for: 5270 Res = ActOnOpenMPTargetParallelForDirective( 5271 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5272 AllowedNameModifiers.push_back(OMPD_target); 5273 AllowedNameModifiers.push_back(OMPD_parallel); 5274 break; 5275 case OMPD_cancellation_point: 5276 assert(ClausesWithImplicit.empty() && 5277 "No clauses are allowed for 'omp cancellation point' directive"); 5278 assert(AStmt == nullptr && "No associated statement allowed for 'omp " 5279 "cancellation point' directive"); 5280 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion); 5281 break; 5282 case OMPD_cancel: 5283 assert(AStmt == nullptr && 5284 "No associated statement allowed for 'omp cancel' directive"); 5285 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc, 5286 CancelRegion); 5287 AllowedNameModifiers.push_back(OMPD_cancel); 5288 break; 5289 case OMPD_target_data: 5290 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc, 5291 EndLoc); 5292 AllowedNameModifiers.push_back(OMPD_target_data); 5293 break; 5294 case OMPD_target_enter_data: 5295 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc, 5296 EndLoc, AStmt); 5297 AllowedNameModifiers.push_back(OMPD_target_enter_data); 5298 break; 5299 case OMPD_target_exit_data: 5300 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc, 5301 EndLoc, AStmt); 5302 AllowedNameModifiers.push_back(OMPD_target_exit_data); 5303 break; 5304 case OMPD_taskloop: 5305 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc, 5306 EndLoc, VarsWithInheritedDSA); 5307 AllowedNameModifiers.push_back(OMPD_taskloop); 5308 break; 5309 case OMPD_taskloop_simd: 5310 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 5311 EndLoc, VarsWithInheritedDSA); 5312 AllowedNameModifiers.push_back(OMPD_taskloop); 5313 if (LangOpts.OpenMP >= 50) 5314 AllowedNameModifiers.push_back(OMPD_simd); 5315 break; 5316 case OMPD_master_taskloop: 5317 Res = ActOnOpenMPMasterTaskLoopDirective( 5318 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5319 AllowedNameModifiers.push_back(OMPD_taskloop); 5320 break; 5321 case OMPD_master_taskloop_simd: 5322 Res = ActOnOpenMPMasterTaskLoopSimdDirective( 5323 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5324 AllowedNameModifiers.push_back(OMPD_taskloop); 5325 if (LangOpts.OpenMP >= 50) 5326 AllowedNameModifiers.push_back(OMPD_simd); 5327 break; 5328 case OMPD_parallel_master_taskloop: 5329 Res = ActOnOpenMPParallelMasterTaskLoopDirective( 5330 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5331 AllowedNameModifiers.push_back(OMPD_taskloop); 5332 AllowedNameModifiers.push_back(OMPD_parallel); 5333 break; 5334 case OMPD_parallel_master_taskloop_simd: 5335 Res = ActOnOpenMPParallelMasterTaskLoopSimdDirective( 5336 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5337 AllowedNameModifiers.push_back(OMPD_taskloop); 5338 AllowedNameModifiers.push_back(OMPD_parallel); 5339 if (LangOpts.OpenMP >= 50) 5340 AllowedNameModifiers.push_back(OMPD_simd); 5341 break; 5342 case OMPD_distribute: 5343 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc, 5344 EndLoc, VarsWithInheritedDSA); 5345 break; 5346 case OMPD_target_update: 5347 Res = ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc, 5348 EndLoc, AStmt); 5349 AllowedNameModifiers.push_back(OMPD_target_update); 5350 break; 5351 case OMPD_distribute_parallel_for: 5352 Res = ActOnOpenMPDistributeParallelForDirective( 5353 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5354 AllowedNameModifiers.push_back(OMPD_parallel); 5355 break; 5356 case OMPD_distribute_parallel_for_simd: 5357 Res = ActOnOpenMPDistributeParallelForSimdDirective( 5358 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5359 AllowedNameModifiers.push_back(OMPD_parallel); 5360 if (LangOpts.OpenMP >= 50) 5361 AllowedNameModifiers.push_back(OMPD_simd); 5362 break; 5363 case OMPD_distribute_simd: 5364 Res = ActOnOpenMPDistributeSimdDirective( 5365 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5366 if (LangOpts.OpenMP >= 50) 5367 AllowedNameModifiers.push_back(OMPD_simd); 5368 break; 5369 case OMPD_target_parallel_for_simd: 5370 Res = ActOnOpenMPTargetParallelForSimdDirective( 5371 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5372 AllowedNameModifiers.push_back(OMPD_target); 5373 AllowedNameModifiers.push_back(OMPD_parallel); 5374 if (LangOpts.OpenMP >= 50) 5375 AllowedNameModifiers.push_back(OMPD_simd); 5376 break; 5377 case OMPD_target_simd: 5378 Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 5379 EndLoc, VarsWithInheritedDSA); 5380 AllowedNameModifiers.push_back(OMPD_target); 5381 if (LangOpts.OpenMP >= 50) 5382 AllowedNameModifiers.push_back(OMPD_simd); 5383 break; 5384 case OMPD_teams_distribute: 5385 Res = ActOnOpenMPTeamsDistributeDirective( 5386 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5387 break; 5388 case OMPD_teams_distribute_simd: 5389 Res = ActOnOpenMPTeamsDistributeSimdDirective( 5390 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5391 if (LangOpts.OpenMP >= 50) 5392 AllowedNameModifiers.push_back(OMPD_simd); 5393 break; 5394 case OMPD_teams_distribute_parallel_for_simd: 5395 Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective( 5396 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5397 AllowedNameModifiers.push_back(OMPD_parallel); 5398 if (LangOpts.OpenMP >= 50) 5399 AllowedNameModifiers.push_back(OMPD_simd); 5400 break; 5401 case OMPD_teams_distribute_parallel_for: 5402 Res = ActOnOpenMPTeamsDistributeParallelForDirective( 5403 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5404 AllowedNameModifiers.push_back(OMPD_parallel); 5405 break; 5406 case OMPD_target_teams: 5407 Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, 5408 EndLoc); 5409 AllowedNameModifiers.push_back(OMPD_target); 5410 break; 5411 case OMPD_target_teams_distribute: 5412 Res = ActOnOpenMPTargetTeamsDistributeDirective( 5413 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5414 AllowedNameModifiers.push_back(OMPD_target); 5415 break; 5416 case OMPD_target_teams_distribute_parallel_for: 5417 Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective( 5418 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5419 AllowedNameModifiers.push_back(OMPD_target); 5420 AllowedNameModifiers.push_back(OMPD_parallel); 5421 break; 5422 case OMPD_target_teams_distribute_parallel_for_simd: 5423 Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective( 5424 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5425 AllowedNameModifiers.push_back(OMPD_target); 5426 AllowedNameModifiers.push_back(OMPD_parallel); 5427 if (LangOpts.OpenMP >= 50) 5428 AllowedNameModifiers.push_back(OMPD_simd); 5429 break; 5430 case OMPD_target_teams_distribute_simd: 5431 Res = ActOnOpenMPTargetTeamsDistributeSimdDirective( 5432 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5433 AllowedNameModifiers.push_back(OMPD_target); 5434 if (LangOpts.OpenMP >= 50) 5435 AllowedNameModifiers.push_back(OMPD_simd); 5436 break; 5437 case OMPD_declare_target: 5438 case OMPD_end_declare_target: 5439 case OMPD_threadprivate: 5440 case OMPD_allocate: 5441 case OMPD_declare_reduction: 5442 case OMPD_declare_mapper: 5443 case OMPD_declare_simd: 5444 case OMPD_requires: 5445 case OMPD_declare_variant: 5446 case OMPD_begin_declare_variant: 5447 case OMPD_end_declare_variant: 5448 llvm_unreachable("OpenMP Directive is not allowed"); 5449 case OMPD_unknown: 5450 default: 5451 llvm_unreachable("Unknown OpenMP directive"); 5452 } 5453 5454 ErrorFound = Res.isInvalid() || ErrorFound; 5455 5456 // Check variables in the clauses if default(none) or 5457 // default(firstprivate) was specified. 5458 if (DSAStack->getDefaultDSA() == DSA_none || 5459 DSAStack->getDefaultDSA() == DSA_firstprivate) { 5460 DSAAttrChecker DSAChecker(DSAStack, *this, nullptr); 5461 for (OMPClause *C : Clauses) { 5462 switch (C->getClauseKind()) { 5463 case OMPC_num_threads: 5464 case OMPC_dist_schedule: 5465 // Do not analyse if no parent teams directive. 5466 if (isOpenMPTeamsDirective(Kind)) 5467 break; 5468 continue; 5469 case OMPC_if: 5470 if (isOpenMPTeamsDirective(Kind) && 5471 cast<OMPIfClause>(C)->getNameModifier() != OMPD_target) 5472 break; 5473 if (isOpenMPParallelDirective(Kind) && 5474 isOpenMPTaskLoopDirective(Kind) && 5475 cast<OMPIfClause>(C)->getNameModifier() != OMPD_parallel) 5476 break; 5477 continue; 5478 case OMPC_schedule: 5479 case OMPC_detach: 5480 break; 5481 case OMPC_grainsize: 5482 case OMPC_num_tasks: 5483 case OMPC_final: 5484 case OMPC_priority: 5485 // Do not analyze if no parent parallel directive. 5486 if (isOpenMPParallelDirective(Kind)) 5487 break; 5488 continue; 5489 case OMPC_ordered: 5490 case OMPC_device: 5491 case OMPC_num_teams: 5492 case OMPC_thread_limit: 5493 case OMPC_hint: 5494 case OMPC_collapse: 5495 case OMPC_safelen: 5496 case OMPC_simdlen: 5497 case OMPC_default: 5498 case OMPC_proc_bind: 5499 case OMPC_private: 5500 case OMPC_firstprivate: 5501 case OMPC_lastprivate: 5502 case OMPC_shared: 5503 case OMPC_reduction: 5504 case OMPC_task_reduction: 5505 case OMPC_in_reduction: 5506 case OMPC_linear: 5507 case OMPC_aligned: 5508 case OMPC_copyin: 5509 case OMPC_copyprivate: 5510 case OMPC_nowait: 5511 case OMPC_untied: 5512 case OMPC_mergeable: 5513 case OMPC_allocate: 5514 case OMPC_read: 5515 case OMPC_write: 5516 case OMPC_update: 5517 case OMPC_capture: 5518 case OMPC_seq_cst: 5519 case OMPC_acq_rel: 5520 case OMPC_acquire: 5521 case OMPC_release: 5522 case OMPC_relaxed: 5523 case OMPC_depend: 5524 case OMPC_threads: 5525 case OMPC_simd: 5526 case OMPC_map: 5527 case OMPC_nogroup: 5528 case OMPC_defaultmap: 5529 case OMPC_to: 5530 case OMPC_from: 5531 case OMPC_use_device_ptr: 5532 case OMPC_use_device_addr: 5533 case OMPC_is_device_ptr: 5534 case OMPC_nontemporal: 5535 case OMPC_order: 5536 case OMPC_destroy: 5537 case OMPC_inclusive: 5538 case OMPC_exclusive: 5539 case OMPC_uses_allocators: 5540 case OMPC_affinity: 5541 continue; 5542 case OMPC_allocator: 5543 case OMPC_flush: 5544 case OMPC_depobj: 5545 case OMPC_threadprivate: 5546 case OMPC_uniform: 5547 case OMPC_unknown: 5548 case OMPC_unified_address: 5549 case OMPC_unified_shared_memory: 5550 case OMPC_reverse_offload: 5551 case OMPC_dynamic_allocators: 5552 case OMPC_atomic_default_mem_order: 5553 case OMPC_device_type: 5554 case OMPC_match: 5555 default: 5556 llvm_unreachable("Unexpected clause"); 5557 } 5558 for (Stmt *CC : C->children()) { 5559 if (CC) 5560 DSAChecker.Visit(CC); 5561 } 5562 } 5563 for (const auto &P : DSAChecker.getVarsWithInheritedDSA()) 5564 VarsWithInheritedDSA[P.getFirst()] = P.getSecond(); 5565 } 5566 for (const auto &P : VarsWithInheritedDSA) { 5567 if (P.getFirst()->isImplicit() || isa<OMPCapturedExprDecl>(P.getFirst())) 5568 continue; 5569 ErrorFound = true; 5570 if (DSAStack->getDefaultDSA() == DSA_none || 5571 DSAStack->getDefaultDSA() == DSA_firstprivate) { 5572 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable) 5573 << P.first << P.second->getSourceRange(); 5574 Diag(DSAStack->getDefaultDSALocation(), diag::note_omp_default_dsa_none); 5575 } else if (getLangOpts().OpenMP >= 50) { 5576 Diag(P.second->getExprLoc(), 5577 diag::err_omp_defaultmap_no_attr_for_variable) 5578 << P.first << P.second->getSourceRange(); 5579 Diag(DSAStack->getDefaultDSALocation(), 5580 diag::note_omp_defaultmap_attr_none); 5581 } 5582 } 5583 5584 if (!AllowedNameModifiers.empty()) 5585 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) || 5586 ErrorFound; 5587 5588 if (ErrorFound) 5589 return StmtError(); 5590 5591 if (!CurContext->isDependentContext() && 5592 isOpenMPTargetExecutionDirective(Kind) && 5593 !(DSAStack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() || 5594 DSAStack->hasRequiresDeclWithClause<OMPUnifiedAddressClause>() || 5595 DSAStack->hasRequiresDeclWithClause<OMPReverseOffloadClause>() || 5596 DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())) { 5597 // Register target to DSA Stack. 5598 DSAStack->addTargetDirLocation(StartLoc); 5599 } 5600 5601 return Res; 5602 } 5603 5604 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective( 5605 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen, 5606 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds, 5607 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears, 5608 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) { 5609 assert(Aligneds.size() == Alignments.size()); 5610 assert(Linears.size() == LinModifiers.size()); 5611 assert(Linears.size() == Steps.size()); 5612 if (!DG || DG.get().isNull()) 5613 return DeclGroupPtrTy(); 5614 5615 const int SimdId = 0; 5616 if (!DG.get().isSingleDecl()) { 5617 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd_variant) 5618 << SimdId; 5619 return DG; 5620 } 5621 Decl *ADecl = DG.get().getSingleDecl(); 5622 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl)) 5623 ADecl = FTD->getTemplatedDecl(); 5624 5625 auto *FD = dyn_cast<FunctionDecl>(ADecl); 5626 if (!FD) { 5627 Diag(ADecl->getLocation(), diag::err_omp_function_expected) << SimdId; 5628 return DeclGroupPtrTy(); 5629 } 5630 5631 // OpenMP [2.8.2, declare simd construct, Description] 5632 // The parameter of the simdlen clause must be a constant positive integer 5633 // expression. 5634 ExprResult SL; 5635 if (Simdlen) 5636 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen); 5637 // OpenMP [2.8.2, declare simd construct, Description] 5638 // The special this pointer can be used as if was one of the arguments to the 5639 // function in any of the linear, aligned, or uniform clauses. 5640 // The uniform clause declares one or more arguments to have an invariant 5641 // value for all concurrent invocations of the function in the execution of a 5642 // single SIMD loop. 5643 llvm::DenseMap<const Decl *, const Expr *> UniformedArgs; 5644 const Expr *UniformedLinearThis = nullptr; 5645 for (const Expr *E : Uniforms) { 5646 E = E->IgnoreParenImpCasts(); 5647 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 5648 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) 5649 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 5650 FD->getParamDecl(PVD->getFunctionScopeIndex()) 5651 ->getCanonicalDecl() == PVD->getCanonicalDecl()) { 5652 UniformedArgs.try_emplace(PVD->getCanonicalDecl(), E); 5653 continue; 5654 } 5655 if (isa<CXXThisExpr>(E)) { 5656 UniformedLinearThis = E; 5657 continue; 5658 } 5659 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 5660 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 5661 } 5662 // OpenMP [2.8.2, declare simd construct, Description] 5663 // The aligned clause declares that the object to which each list item points 5664 // is aligned to the number of bytes expressed in the optional parameter of 5665 // the aligned clause. 5666 // The special this pointer can be used as if was one of the arguments to the 5667 // function in any of the linear, aligned, or uniform clauses. 5668 // The type of list items appearing in the aligned clause must be array, 5669 // pointer, reference to array, or reference to pointer. 5670 llvm::DenseMap<const Decl *, const Expr *> AlignedArgs; 5671 const Expr *AlignedThis = nullptr; 5672 for (const Expr *E : Aligneds) { 5673 E = E->IgnoreParenImpCasts(); 5674 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 5675 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 5676 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 5677 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 5678 FD->getParamDecl(PVD->getFunctionScopeIndex()) 5679 ->getCanonicalDecl() == CanonPVD) { 5680 // OpenMP [2.8.1, simd construct, Restrictions] 5681 // A list-item cannot appear in more than one aligned clause. 5682 if (AlignedArgs.count(CanonPVD) > 0) { 5683 Diag(E->getExprLoc(), diag::err_omp_used_in_clause_twice) 5684 << 1 << getOpenMPClauseName(OMPC_aligned) 5685 << E->getSourceRange(); 5686 Diag(AlignedArgs[CanonPVD]->getExprLoc(), 5687 diag::note_omp_explicit_dsa) 5688 << getOpenMPClauseName(OMPC_aligned); 5689 continue; 5690 } 5691 AlignedArgs[CanonPVD] = E; 5692 QualType QTy = PVD->getType() 5693 .getNonReferenceType() 5694 .getUnqualifiedType() 5695 .getCanonicalType(); 5696 const Type *Ty = QTy.getTypePtrOrNull(); 5697 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) { 5698 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr) 5699 << QTy << getLangOpts().CPlusPlus << E->getSourceRange(); 5700 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD; 5701 } 5702 continue; 5703 } 5704 } 5705 if (isa<CXXThisExpr>(E)) { 5706 if (AlignedThis) { 5707 Diag(E->getExprLoc(), diag::err_omp_used_in_clause_twice) 5708 << 2 << getOpenMPClauseName(OMPC_aligned) << E->getSourceRange(); 5709 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa) 5710 << getOpenMPClauseName(OMPC_aligned); 5711 } 5712 AlignedThis = E; 5713 continue; 5714 } 5715 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 5716 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 5717 } 5718 // The optional parameter of the aligned clause, alignment, must be a constant 5719 // positive integer expression. If no optional parameter is specified, 5720 // implementation-defined default alignments for SIMD instructions on the 5721 // target platforms are assumed. 5722 SmallVector<const Expr *, 4> NewAligns; 5723 for (Expr *E : Alignments) { 5724 ExprResult Align; 5725 if (E) 5726 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned); 5727 NewAligns.push_back(Align.get()); 5728 } 5729 // OpenMP [2.8.2, declare simd construct, Description] 5730 // The linear clause declares one or more list items to be private to a SIMD 5731 // lane and to have a linear relationship with respect to the iteration space 5732 // of a loop. 5733 // The special this pointer can be used as if was one of the arguments to the 5734 // function in any of the linear, aligned, or uniform clauses. 5735 // When a linear-step expression is specified in a linear clause it must be 5736 // either a constant integer expression or an integer-typed parameter that is 5737 // specified in a uniform clause on the directive. 5738 llvm::DenseMap<const Decl *, const Expr *> LinearArgs; 5739 const bool IsUniformedThis = UniformedLinearThis != nullptr; 5740 auto MI = LinModifiers.begin(); 5741 for (const Expr *E : Linears) { 5742 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI); 5743 ++MI; 5744 E = E->IgnoreParenImpCasts(); 5745 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 5746 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 5747 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 5748 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 5749 FD->getParamDecl(PVD->getFunctionScopeIndex()) 5750 ->getCanonicalDecl() == CanonPVD) { 5751 // OpenMP [2.15.3.7, linear Clause, Restrictions] 5752 // A list-item cannot appear in more than one linear clause. 5753 if (LinearArgs.count(CanonPVD) > 0) { 5754 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 5755 << getOpenMPClauseName(OMPC_linear) 5756 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange(); 5757 Diag(LinearArgs[CanonPVD]->getExprLoc(), 5758 diag::note_omp_explicit_dsa) 5759 << getOpenMPClauseName(OMPC_linear); 5760 continue; 5761 } 5762 // Each argument can appear in at most one uniform or linear clause. 5763 if (UniformedArgs.count(CanonPVD) > 0) { 5764 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 5765 << getOpenMPClauseName(OMPC_linear) 5766 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange(); 5767 Diag(UniformedArgs[CanonPVD]->getExprLoc(), 5768 diag::note_omp_explicit_dsa) 5769 << getOpenMPClauseName(OMPC_uniform); 5770 continue; 5771 } 5772 LinearArgs[CanonPVD] = E; 5773 if (E->isValueDependent() || E->isTypeDependent() || 5774 E->isInstantiationDependent() || 5775 E->containsUnexpandedParameterPack()) 5776 continue; 5777 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind, 5778 PVD->getOriginalType(), 5779 /*IsDeclareSimd=*/true); 5780 continue; 5781 } 5782 } 5783 if (isa<CXXThisExpr>(E)) { 5784 if (UniformedLinearThis) { 5785 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 5786 << getOpenMPClauseName(OMPC_linear) 5787 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear) 5788 << E->getSourceRange(); 5789 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa) 5790 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform 5791 : OMPC_linear); 5792 continue; 5793 } 5794 UniformedLinearThis = E; 5795 if (E->isValueDependent() || E->isTypeDependent() || 5796 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 5797 continue; 5798 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind, 5799 E->getType(), /*IsDeclareSimd=*/true); 5800 continue; 5801 } 5802 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 5803 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 5804 } 5805 Expr *Step = nullptr; 5806 Expr *NewStep = nullptr; 5807 SmallVector<Expr *, 4> NewSteps; 5808 for (Expr *E : Steps) { 5809 // Skip the same step expression, it was checked already. 5810 if (Step == E || !E) { 5811 NewSteps.push_back(E ? NewStep : nullptr); 5812 continue; 5813 } 5814 Step = E; 5815 if (const auto *DRE = dyn_cast<DeclRefExpr>(Step)) 5816 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 5817 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 5818 if (UniformedArgs.count(CanonPVD) == 0) { 5819 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param) 5820 << Step->getSourceRange(); 5821 } else if (E->isValueDependent() || E->isTypeDependent() || 5822 E->isInstantiationDependent() || 5823 E->containsUnexpandedParameterPack() || 5824 CanonPVD->getType()->hasIntegerRepresentation()) { 5825 NewSteps.push_back(Step); 5826 } else { 5827 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param) 5828 << Step->getSourceRange(); 5829 } 5830 continue; 5831 } 5832 NewStep = Step; 5833 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && 5834 !Step->isInstantiationDependent() && 5835 !Step->containsUnexpandedParameterPack()) { 5836 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step) 5837 .get(); 5838 if (NewStep) 5839 NewStep = 5840 VerifyIntegerConstantExpression(NewStep, /*FIXME*/ AllowFold).get(); 5841 } 5842 NewSteps.push_back(NewStep); 5843 } 5844 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit( 5845 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()), 5846 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(), 5847 const_cast<Expr **>(NewAligns.data()), NewAligns.size(), 5848 const_cast<Expr **>(Linears.data()), Linears.size(), 5849 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(), 5850 NewSteps.data(), NewSteps.size(), SR); 5851 ADecl->addAttr(NewAttr); 5852 return DG; 5853 } 5854 5855 static void setPrototype(Sema &S, FunctionDecl *FD, FunctionDecl *FDWithProto, 5856 QualType NewType) { 5857 assert(NewType->isFunctionProtoType() && 5858 "Expected function type with prototype."); 5859 assert(FD->getType()->isFunctionNoProtoType() && 5860 "Expected function with type with no prototype."); 5861 assert(FDWithProto->getType()->isFunctionProtoType() && 5862 "Expected function with prototype."); 5863 // Synthesize parameters with the same types. 5864 FD->setType(NewType); 5865 SmallVector<ParmVarDecl *, 16> Params; 5866 for (const ParmVarDecl *P : FDWithProto->parameters()) { 5867 auto *Param = ParmVarDecl::Create(S.getASTContext(), FD, SourceLocation(), 5868 SourceLocation(), nullptr, P->getType(), 5869 /*TInfo=*/nullptr, SC_None, nullptr); 5870 Param->setScopeInfo(0, Params.size()); 5871 Param->setImplicit(); 5872 Params.push_back(Param); 5873 } 5874 5875 FD->setParams(Params); 5876 } 5877 5878 Sema::OMPDeclareVariantScope::OMPDeclareVariantScope(OMPTraitInfo &TI) 5879 : TI(&TI), NameSuffix(TI.getMangledName()) {} 5880 5881 void Sema::ActOnStartOfFunctionDefinitionInOpenMPDeclareVariantScope( 5882 Scope *S, Declarator &D, MultiTemplateParamsArg TemplateParamLists, 5883 SmallVectorImpl<FunctionDecl *> &Bases) { 5884 if (!D.getIdentifier()) 5885 return; 5886 5887 OMPDeclareVariantScope &DVScope = OMPDeclareVariantScopes.back(); 5888 5889 // Template specialization is an extension, check if we do it. 5890 bool IsTemplated = !TemplateParamLists.empty(); 5891 if (IsTemplated & 5892 !DVScope.TI->isExtensionActive( 5893 llvm::omp::TraitProperty::implementation_extension_allow_templates)) 5894 return; 5895 5896 IdentifierInfo *BaseII = D.getIdentifier(); 5897 LookupResult Lookup(*this, DeclarationName(BaseII), D.getIdentifierLoc(), 5898 LookupOrdinaryName); 5899 LookupParsedName(Lookup, S, &D.getCXXScopeSpec()); 5900 5901 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); 5902 QualType FType = TInfo->getType(); 5903 5904 bool IsConstexpr = D.getDeclSpec().getConstexprSpecifier() == CSK_constexpr; 5905 bool IsConsteval = D.getDeclSpec().getConstexprSpecifier() == CSK_consteval; 5906 5907 for (auto *Candidate : Lookup) { 5908 auto *CandidateDecl = Candidate->getUnderlyingDecl(); 5909 FunctionDecl *UDecl = nullptr; 5910 if (IsTemplated && isa<FunctionTemplateDecl>(CandidateDecl)) 5911 UDecl = cast<FunctionTemplateDecl>(CandidateDecl)->getTemplatedDecl(); 5912 else if (!IsTemplated) 5913 UDecl = dyn_cast<FunctionDecl>(CandidateDecl); 5914 if (!UDecl) 5915 continue; 5916 5917 // Don't specialize constexpr/consteval functions with 5918 // non-constexpr/consteval functions. 5919 if (UDecl->isConstexpr() && !IsConstexpr) 5920 continue; 5921 if (UDecl->isConsteval() && !IsConsteval) 5922 continue; 5923 5924 QualType UDeclTy = UDecl->getType(); 5925 if (!UDeclTy->isDependentType()) { 5926 QualType NewType = Context.mergeFunctionTypes( 5927 FType, UDeclTy, /* OfBlockPointer */ false, 5928 /* Unqualified */ false, /* AllowCXX */ true); 5929 if (NewType.isNull()) 5930 continue; 5931 } 5932 5933 // Found a base! 5934 Bases.push_back(UDecl); 5935 } 5936 5937 bool UseImplicitBase = !DVScope.TI->isExtensionActive( 5938 llvm::omp::TraitProperty::implementation_extension_disable_implicit_base); 5939 // If no base was found we create a declaration that we use as base. 5940 if (Bases.empty() && UseImplicitBase) { 5941 D.setFunctionDefinitionKind(FDK_Declaration); 5942 Decl *BaseD = HandleDeclarator(S, D, TemplateParamLists); 5943 BaseD->setImplicit(true); 5944 if (auto *BaseTemplD = dyn_cast<FunctionTemplateDecl>(BaseD)) 5945 Bases.push_back(BaseTemplD->getTemplatedDecl()); 5946 else 5947 Bases.push_back(cast<FunctionDecl>(BaseD)); 5948 } 5949 5950 std::string MangledName; 5951 MangledName += D.getIdentifier()->getName(); 5952 MangledName += getOpenMPVariantManglingSeparatorStr(); 5953 MangledName += DVScope.NameSuffix; 5954 IdentifierInfo &VariantII = Context.Idents.get(MangledName); 5955 5956 VariantII.setMangledOpenMPVariantName(true); 5957 D.SetIdentifier(&VariantII, D.getBeginLoc()); 5958 } 5959 5960 void Sema::ActOnFinishedFunctionDefinitionInOpenMPDeclareVariantScope( 5961 Decl *D, SmallVectorImpl<FunctionDecl *> &Bases) { 5962 // Do not mark function as is used to prevent its emission if this is the 5963 // only place where it is used. 5964 EnterExpressionEvaluationContext Unevaluated( 5965 *this, Sema::ExpressionEvaluationContext::Unevaluated); 5966 5967 FunctionDecl *FD = nullptr; 5968 if (auto *UTemplDecl = dyn_cast<FunctionTemplateDecl>(D)) 5969 FD = UTemplDecl->getTemplatedDecl(); 5970 else 5971 FD = cast<FunctionDecl>(D); 5972 auto *VariantFuncRef = DeclRefExpr::Create( 5973 Context, NestedNameSpecifierLoc(), SourceLocation(), FD, 5974 /* RefersToEnclosingVariableOrCapture */ false, 5975 /* NameLoc */ FD->getLocation(), FD->getType(), ExprValueKind::VK_RValue); 5976 5977 OMPDeclareVariantScope &DVScope = OMPDeclareVariantScopes.back(); 5978 auto *OMPDeclareVariantA = OMPDeclareVariantAttr::CreateImplicit( 5979 Context, VariantFuncRef, DVScope.TI); 5980 for (FunctionDecl *BaseFD : Bases) 5981 BaseFD->addAttr(OMPDeclareVariantA); 5982 } 5983 5984 ExprResult Sema::ActOnOpenMPCall(ExprResult Call, Scope *Scope, 5985 SourceLocation LParenLoc, 5986 MultiExprArg ArgExprs, 5987 SourceLocation RParenLoc, Expr *ExecConfig) { 5988 // The common case is a regular call we do not want to specialize at all. Try 5989 // to make that case fast by bailing early. 5990 CallExpr *CE = dyn_cast<CallExpr>(Call.get()); 5991 if (!CE) 5992 return Call; 5993 5994 FunctionDecl *CalleeFnDecl = CE->getDirectCallee(); 5995 if (!CalleeFnDecl) 5996 return Call; 5997 5998 if (!CalleeFnDecl->hasAttr<OMPDeclareVariantAttr>()) 5999 return Call; 6000 6001 ASTContext &Context = getASTContext(); 6002 std::function<void(StringRef)> DiagUnknownTrait = [this, 6003 CE](StringRef ISATrait) { 6004 // TODO Track the selector locations in a way that is accessible here to 6005 // improve the diagnostic location. 6006 Diag(CE->getBeginLoc(), diag::warn_unknown_declare_variant_isa_trait) 6007 << ISATrait; 6008 }; 6009 TargetOMPContext OMPCtx(Context, std::move(DiagUnknownTrait), 6010 getCurFunctionDecl()); 6011 6012 QualType CalleeFnType = CalleeFnDecl->getType(); 6013 6014 SmallVector<Expr *, 4> Exprs; 6015 SmallVector<VariantMatchInfo, 4> VMIs; 6016 while (CalleeFnDecl) { 6017 for (OMPDeclareVariantAttr *A : 6018 CalleeFnDecl->specific_attrs<OMPDeclareVariantAttr>()) { 6019 Expr *VariantRef = A->getVariantFuncRef(); 6020 6021 VariantMatchInfo VMI; 6022 OMPTraitInfo &TI = A->getTraitInfo(); 6023 TI.getAsVariantMatchInfo(Context, VMI); 6024 if (!isVariantApplicableInContext(VMI, OMPCtx, 6025 /* DeviceSetOnly */ false)) 6026 continue; 6027 6028 VMIs.push_back(VMI); 6029 Exprs.push_back(VariantRef); 6030 } 6031 6032 CalleeFnDecl = CalleeFnDecl->getPreviousDecl(); 6033 } 6034 6035 ExprResult NewCall; 6036 do { 6037 int BestIdx = getBestVariantMatchForContext(VMIs, OMPCtx); 6038 if (BestIdx < 0) 6039 return Call; 6040 Expr *BestExpr = cast<DeclRefExpr>(Exprs[BestIdx]); 6041 Decl *BestDecl = cast<DeclRefExpr>(BestExpr)->getDecl(); 6042 6043 { 6044 // Try to build a (member) call expression for the current best applicable 6045 // variant expression. We allow this to fail in which case we continue 6046 // with the next best variant expression. The fail case is part of the 6047 // implementation defined behavior in the OpenMP standard when it talks 6048 // about what differences in the function prototypes: "Any differences 6049 // that the specific OpenMP context requires in the prototype of the 6050 // variant from the base function prototype are implementation defined." 6051 // This wording is there to allow the specialized variant to have a 6052 // different type than the base function. This is intended and OK but if 6053 // we cannot create a call the difference is not in the "implementation 6054 // defined range" we allow. 6055 Sema::TentativeAnalysisScope Trap(*this); 6056 6057 if (auto *SpecializedMethod = dyn_cast<CXXMethodDecl>(BestDecl)) { 6058 auto *MemberCall = dyn_cast<CXXMemberCallExpr>(CE); 6059 BestExpr = MemberExpr::CreateImplicit( 6060 Context, MemberCall->getImplicitObjectArgument(), 6061 /* IsArrow */ false, SpecializedMethod, Context.BoundMemberTy, 6062 MemberCall->getValueKind(), MemberCall->getObjectKind()); 6063 } 6064 NewCall = BuildCallExpr(Scope, BestExpr, LParenLoc, ArgExprs, RParenLoc, 6065 ExecConfig); 6066 if (NewCall.isUsable()) { 6067 if (CallExpr *NCE = dyn_cast<CallExpr>(NewCall.get())) { 6068 FunctionDecl *NewCalleeFnDecl = NCE->getDirectCallee(); 6069 QualType NewType = Context.mergeFunctionTypes( 6070 CalleeFnType, NewCalleeFnDecl->getType(), 6071 /* OfBlockPointer */ false, 6072 /* Unqualified */ false, /* AllowCXX */ true); 6073 if (!NewType.isNull()) 6074 break; 6075 // Don't use the call if the function type was not compatible. 6076 NewCall = nullptr; 6077 } 6078 } 6079 } 6080 6081 VMIs.erase(VMIs.begin() + BestIdx); 6082 Exprs.erase(Exprs.begin() + BestIdx); 6083 } while (!VMIs.empty()); 6084 6085 if (!NewCall.isUsable()) 6086 return Call; 6087 return PseudoObjectExpr::Create(Context, CE, {NewCall.get()}, 0); 6088 } 6089 6090 Optional<std::pair<FunctionDecl *, Expr *>> 6091 Sema::checkOpenMPDeclareVariantFunction(Sema::DeclGroupPtrTy DG, 6092 Expr *VariantRef, OMPTraitInfo &TI, 6093 SourceRange SR) { 6094 if (!DG || DG.get().isNull()) 6095 return None; 6096 6097 const int VariantId = 1; 6098 // Must be applied only to single decl. 6099 if (!DG.get().isSingleDecl()) { 6100 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd_variant) 6101 << VariantId << SR; 6102 return None; 6103 } 6104 Decl *ADecl = DG.get().getSingleDecl(); 6105 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl)) 6106 ADecl = FTD->getTemplatedDecl(); 6107 6108 // Decl must be a function. 6109 auto *FD = dyn_cast<FunctionDecl>(ADecl); 6110 if (!FD) { 6111 Diag(ADecl->getLocation(), diag::err_omp_function_expected) 6112 << VariantId << SR; 6113 return None; 6114 } 6115 6116 auto &&HasMultiVersionAttributes = [](const FunctionDecl *FD) { 6117 return FD->hasAttrs() && 6118 (FD->hasAttr<CPUDispatchAttr>() || FD->hasAttr<CPUSpecificAttr>() || 6119 FD->hasAttr<TargetAttr>()); 6120 }; 6121 // OpenMP is not compatible with CPU-specific attributes. 6122 if (HasMultiVersionAttributes(FD)) { 6123 Diag(FD->getLocation(), diag::err_omp_declare_variant_incompat_attributes) 6124 << SR; 6125 return None; 6126 } 6127 6128 // Allow #pragma omp declare variant only if the function is not used. 6129 if (FD->isUsed(false)) 6130 Diag(SR.getBegin(), diag::warn_omp_declare_variant_after_used) 6131 << FD->getLocation(); 6132 6133 // Check if the function was emitted already. 6134 const FunctionDecl *Definition; 6135 if (!FD->isThisDeclarationADefinition() && FD->isDefined(Definition) && 6136 (LangOpts.EmitAllDecls || Context.DeclMustBeEmitted(Definition))) 6137 Diag(SR.getBegin(), diag::warn_omp_declare_variant_after_emitted) 6138 << FD->getLocation(); 6139 6140 // The VariantRef must point to function. 6141 if (!VariantRef) { 6142 Diag(SR.getBegin(), diag::err_omp_function_expected) << VariantId; 6143 return None; 6144 } 6145 6146 auto ShouldDelayChecks = [](Expr *&E, bool) { 6147 return E && (E->isTypeDependent() || E->isValueDependent() || 6148 E->containsUnexpandedParameterPack() || 6149 E->isInstantiationDependent()); 6150 }; 6151 // Do not check templates, wait until instantiation. 6152 if (FD->isDependentContext() || ShouldDelayChecks(VariantRef, false) || 6153 TI.anyScoreOrCondition(ShouldDelayChecks)) 6154 return std::make_pair(FD, VariantRef); 6155 6156 // Deal with non-constant score and user condition expressions. 6157 auto HandleNonConstantScoresAndConditions = [this](Expr *&E, 6158 bool IsScore) -> bool { 6159 if (!E || E->isIntegerConstantExpr(Context)) 6160 return false; 6161 6162 if (IsScore) { 6163 // We warn on non-constant scores and pretend they were not present. 6164 Diag(E->getExprLoc(), diag::warn_omp_declare_variant_score_not_constant) 6165 << E; 6166 E = nullptr; 6167 } else { 6168 // We could replace a non-constant user condition with "false" but we 6169 // will soon need to handle these anyway for the dynamic version of 6170 // OpenMP context selectors. 6171 Diag(E->getExprLoc(), 6172 diag::err_omp_declare_variant_user_condition_not_constant) 6173 << E; 6174 } 6175 return true; 6176 }; 6177 if (TI.anyScoreOrCondition(HandleNonConstantScoresAndConditions)) 6178 return None; 6179 6180 // Convert VariantRef expression to the type of the original function to 6181 // resolve possible conflicts. 6182 ExprResult VariantRefCast = VariantRef; 6183 if (LangOpts.CPlusPlus) { 6184 QualType FnPtrType; 6185 auto *Method = dyn_cast<CXXMethodDecl>(FD); 6186 if (Method && !Method->isStatic()) { 6187 const Type *ClassType = 6188 Context.getTypeDeclType(Method->getParent()).getTypePtr(); 6189 FnPtrType = Context.getMemberPointerType(FD->getType(), ClassType); 6190 ExprResult ER; 6191 { 6192 // Build adrr_of unary op to correctly handle type checks for member 6193 // functions. 6194 Sema::TentativeAnalysisScope Trap(*this); 6195 ER = CreateBuiltinUnaryOp(VariantRef->getBeginLoc(), UO_AddrOf, 6196 VariantRef); 6197 } 6198 if (!ER.isUsable()) { 6199 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected) 6200 << VariantId << VariantRef->getSourceRange(); 6201 return None; 6202 } 6203 VariantRef = ER.get(); 6204 } else { 6205 FnPtrType = Context.getPointerType(FD->getType()); 6206 } 6207 QualType VarianPtrType = Context.getPointerType(VariantRef->getType()); 6208 if (VarianPtrType.getUnqualifiedType() != FnPtrType.getUnqualifiedType()) { 6209 ImplicitConversionSequence ICS = TryImplicitConversion( 6210 VariantRef, FnPtrType.getUnqualifiedType(), 6211 /*SuppressUserConversions=*/false, AllowedExplicit::None, 6212 /*InOverloadResolution=*/false, 6213 /*CStyle=*/false, 6214 /*AllowObjCWritebackConversion=*/false); 6215 if (ICS.isFailure()) { 6216 Diag(VariantRef->getExprLoc(), 6217 diag::err_omp_declare_variant_incompat_types) 6218 << VariantRef->getType() 6219 << ((Method && !Method->isStatic()) ? FnPtrType : FD->getType()) 6220 << VariantRef->getSourceRange(); 6221 return None; 6222 } 6223 VariantRefCast = PerformImplicitConversion( 6224 VariantRef, FnPtrType.getUnqualifiedType(), AA_Converting); 6225 if (!VariantRefCast.isUsable()) 6226 return None; 6227 } 6228 // Drop previously built artificial addr_of unary op for member functions. 6229 if (Method && !Method->isStatic()) { 6230 Expr *PossibleAddrOfVariantRef = VariantRefCast.get(); 6231 if (auto *UO = dyn_cast<UnaryOperator>( 6232 PossibleAddrOfVariantRef->IgnoreImplicit())) 6233 VariantRefCast = UO->getSubExpr(); 6234 } 6235 } 6236 6237 ExprResult ER = CheckPlaceholderExpr(VariantRefCast.get()); 6238 if (!ER.isUsable() || 6239 !ER.get()->IgnoreParenImpCasts()->getType()->isFunctionType()) { 6240 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected) 6241 << VariantId << VariantRef->getSourceRange(); 6242 return None; 6243 } 6244 6245 // The VariantRef must point to function. 6246 auto *DRE = dyn_cast<DeclRefExpr>(ER.get()->IgnoreParenImpCasts()); 6247 if (!DRE) { 6248 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected) 6249 << VariantId << VariantRef->getSourceRange(); 6250 return None; 6251 } 6252 auto *NewFD = dyn_cast_or_null<FunctionDecl>(DRE->getDecl()); 6253 if (!NewFD) { 6254 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected) 6255 << VariantId << VariantRef->getSourceRange(); 6256 return None; 6257 } 6258 6259 // Check if function types are compatible in C. 6260 if (!LangOpts.CPlusPlus) { 6261 QualType NewType = 6262 Context.mergeFunctionTypes(FD->getType(), NewFD->getType()); 6263 if (NewType.isNull()) { 6264 Diag(VariantRef->getExprLoc(), 6265 diag::err_omp_declare_variant_incompat_types) 6266 << NewFD->getType() << FD->getType() << VariantRef->getSourceRange(); 6267 return None; 6268 } 6269 if (NewType->isFunctionProtoType()) { 6270 if (FD->getType()->isFunctionNoProtoType()) 6271 setPrototype(*this, FD, NewFD, NewType); 6272 else if (NewFD->getType()->isFunctionNoProtoType()) 6273 setPrototype(*this, NewFD, FD, NewType); 6274 } 6275 } 6276 6277 // Check if variant function is not marked with declare variant directive. 6278 if (NewFD->hasAttrs() && NewFD->hasAttr<OMPDeclareVariantAttr>()) { 6279 Diag(VariantRef->getExprLoc(), 6280 diag::warn_omp_declare_variant_marked_as_declare_variant) 6281 << VariantRef->getSourceRange(); 6282 SourceRange SR = 6283 NewFD->specific_attr_begin<OMPDeclareVariantAttr>()->getRange(); 6284 Diag(SR.getBegin(), diag::note_omp_marked_declare_variant_here) << SR; 6285 return None; 6286 } 6287 6288 enum DoesntSupport { 6289 VirtFuncs = 1, 6290 Constructors = 3, 6291 Destructors = 4, 6292 DeletedFuncs = 5, 6293 DefaultedFuncs = 6, 6294 ConstexprFuncs = 7, 6295 ConstevalFuncs = 8, 6296 }; 6297 if (const auto *CXXFD = dyn_cast<CXXMethodDecl>(FD)) { 6298 if (CXXFD->isVirtual()) { 6299 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 6300 << VirtFuncs; 6301 return None; 6302 } 6303 6304 if (isa<CXXConstructorDecl>(FD)) { 6305 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 6306 << Constructors; 6307 return None; 6308 } 6309 6310 if (isa<CXXDestructorDecl>(FD)) { 6311 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 6312 << Destructors; 6313 return None; 6314 } 6315 } 6316 6317 if (FD->isDeleted()) { 6318 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 6319 << DeletedFuncs; 6320 return None; 6321 } 6322 6323 if (FD->isDefaulted()) { 6324 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 6325 << DefaultedFuncs; 6326 return None; 6327 } 6328 6329 if (FD->isConstexpr()) { 6330 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 6331 << (NewFD->isConsteval() ? ConstevalFuncs : ConstexprFuncs); 6332 return None; 6333 } 6334 6335 // Check general compatibility. 6336 if (areMultiversionVariantFunctionsCompatible( 6337 FD, NewFD, PartialDiagnostic::NullDiagnostic(), 6338 PartialDiagnosticAt(SourceLocation(), 6339 PartialDiagnostic::NullDiagnostic()), 6340 PartialDiagnosticAt( 6341 VariantRef->getExprLoc(), 6342 PDiag(diag::err_omp_declare_variant_doesnt_support)), 6343 PartialDiagnosticAt(VariantRef->getExprLoc(), 6344 PDiag(diag::err_omp_declare_variant_diff) 6345 << FD->getLocation()), 6346 /*TemplatesSupported=*/true, /*ConstexprSupported=*/false, 6347 /*CLinkageMayDiffer=*/true)) 6348 return None; 6349 return std::make_pair(FD, cast<Expr>(DRE)); 6350 } 6351 6352 void Sema::ActOnOpenMPDeclareVariantDirective(FunctionDecl *FD, 6353 Expr *VariantRef, 6354 OMPTraitInfo &TI, 6355 SourceRange SR) { 6356 auto *NewAttr = 6357 OMPDeclareVariantAttr::CreateImplicit(Context, VariantRef, &TI, SR); 6358 FD->addAttr(NewAttr); 6359 } 6360 6361 StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses, 6362 Stmt *AStmt, 6363 SourceLocation StartLoc, 6364 SourceLocation EndLoc) { 6365 if (!AStmt) 6366 return StmtError(); 6367 6368 auto *CS = cast<CapturedStmt>(AStmt); 6369 // 1.2.2 OpenMP Language Terminology 6370 // Structured block - An executable statement with a single entry at the 6371 // top and a single exit at the bottom. 6372 // The point of exit cannot be a branch out of the structured block. 6373 // longjmp() and throw() must not violate the entry/exit criteria. 6374 CS->getCapturedDecl()->setNothrow(); 6375 6376 setFunctionHasBranchProtectedScope(); 6377 6378 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 6379 DSAStack->getTaskgroupReductionRef(), 6380 DSAStack->isCancelRegion()); 6381 } 6382 6383 namespace { 6384 /// Iteration space of a single for loop. 6385 struct LoopIterationSpace final { 6386 /// True if the condition operator is the strict compare operator (<, > or 6387 /// !=). 6388 bool IsStrictCompare = false; 6389 /// Condition of the loop. 6390 Expr *PreCond = nullptr; 6391 /// This expression calculates the number of iterations in the loop. 6392 /// It is always possible to calculate it before starting the loop. 6393 Expr *NumIterations = nullptr; 6394 /// The loop counter variable. 6395 Expr *CounterVar = nullptr; 6396 /// Private loop counter variable. 6397 Expr *PrivateCounterVar = nullptr; 6398 /// This is initializer for the initial value of #CounterVar. 6399 Expr *CounterInit = nullptr; 6400 /// This is step for the #CounterVar used to generate its update: 6401 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration. 6402 Expr *CounterStep = nullptr; 6403 /// Should step be subtracted? 6404 bool Subtract = false; 6405 /// Source range of the loop init. 6406 SourceRange InitSrcRange; 6407 /// Source range of the loop condition. 6408 SourceRange CondSrcRange; 6409 /// Source range of the loop increment. 6410 SourceRange IncSrcRange; 6411 /// Minimum value that can have the loop control variable. Used to support 6412 /// non-rectangular loops. Applied only for LCV with the non-iterator types, 6413 /// since only such variables can be used in non-loop invariant expressions. 6414 Expr *MinValue = nullptr; 6415 /// Maximum value that can have the loop control variable. Used to support 6416 /// non-rectangular loops. Applied only for LCV with the non-iterator type, 6417 /// since only such variables can be used in non-loop invariant expressions. 6418 Expr *MaxValue = nullptr; 6419 /// true, if the lower bound depends on the outer loop control var. 6420 bool IsNonRectangularLB = false; 6421 /// true, if the upper bound depends on the outer loop control var. 6422 bool IsNonRectangularUB = false; 6423 /// Index of the loop this loop depends on and forms non-rectangular loop 6424 /// nest. 6425 unsigned LoopDependentIdx = 0; 6426 /// Final condition for the non-rectangular loop nest support. It is used to 6427 /// check that the number of iterations for this particular counter must be 6428 /// finished. 6429 Expr *FinalCondition = nullptr; 6430 }; 6431 6432 /// Helper class for checking canonical form of the OpenMP loops and 6433 /// extracting iteration space of each loop in the loop nest, that will be used 6434 /// for IR generation. 6435 class OpenMPIterationSpaceChecker { 6436 /// Reference to Sema. 6437 Sema &SemaRef; 6438 /// Data-sharing stack. 6439 DSAStackTy &Stack; 6440 /// A location for diagnostics (when there is no some better location). 6441 SourceLocation DefaultLoc; 6442 /// A location for diagnostics (when increment is not compatible). 6443 SourceLocation ConditionLoc; 6444 /// A source location for referring to loop init later. 6445 SourceRange InitSrcRange; 6446 /// A source location for referring to condition later. 6447 SourceRange ConditionSrcRange; 6448 /// A source location for referring to increment later. 6449 SourceRange IncrementSrcRange; 6450 /// Loop variable. 6451 ValueDecl *LCDecl = nullptr; 6452 /// Reference to loop variable. 6453 Expr *LCRef = nullptr; 6454 /// Lower bound (initializer for the var). 6455 Expr *LB = nullptr; 6456 /// Upper bound. 6457 Expr *UB = nullptr; 6458 /// Loop step (increment). 6459 Expr *Step = nullptr; 6460 /// This flag is true when condition is one of: 6461 /// Var < UB 6462 /// Var <= UB 6463 /// UB > Var 6464 /// UB >= Var 6465 /// This will have no value when the condition is != 6466 llvm::Optional<bool> TestIsLessOp; 6467 /// This flag is true when condition is strict ( < or > ). 6468 bool TestIsStrictOp = false; 6469 /// This flag is true when step is subtracted on each iteration. 6470 bool SubtractStep = false; 6471 /// The outer loop counter this loop depends on (if any). 6472 const ValueDecl *DepDecl = nullptr; 6473 /// Contains number of loop (starts from 1) on which loop counter init 6474 /// expression of this loop depends on. 6475 Optional<unsigned> InitDependOnLC; 6476 /// Contains number of loop (starts from 1) on which loop counter condition 6477 /// expression of this loop depends on. 6478 Optional<unsigned> CondDependOnLC; 6479 /// Checks if the provide statement depends on the loop counter. 6480 Optional<unsigned> doesDependOnLoopCounter(const Stmt *S, bool IsInitializer); 6481 /// Original condition required for checking of the exit condition for 6482 /// non-rectangular loop. 6483 Expr *Condition = nullptr; 6484 6485 public: 6486 OpenMPIterationSpaceChecker(Sema &SemaRef, DSAStackTy &Stack, 6487 SourceLocation DefaultLoc) 6488 : SemaRef(SemaRef), Stack(Stack), DefaultLoc(DefaultLoc), 6489 ConditionLoc(DefaultLoc) {} 6490 /// Check init-expr for canonical loop form and save loop counter 6491 /// variable - #Var and its initialization value - #LB. 6492 bool checkAndSetInit(Stmt *S, bool EmitDiags = true); 6493 /// Check test-expr for canonical form, save upper-bound (#UB), flags 6494 /// for less/greater and for strict/non-strict comparison. 6495 bool checkAndSetCond(Expr *S); 6496 /// Check incr-expr for canonical loop form and return true if it 6497 /// does not conform, otherwise save loop step (#Step). 6498 bool checkAndSetInc(Expr *S); 6499 /// Return the loop counter variable. 6500 ValueDecl *getLoopDecl() const { return LCDecl; } 6501 /// Return the reference expression to loop counter variable. 6502 Expr *getLoopDeclRefExpr() const { return LCRef; } 6503 /// Source range of the loop init. 6504 SourceRange getInitSrcRange() const { return InitSrcRange; } 6505 /// Source range of the loop condition. 6506 SourceRange getConditionSrcRange() const { return ConditionSrcRange; } 6507 /// Source range of the loop increment. 6508 SourceRange getIncrementSrcRange() const { return IncrementSrcRange; } 6509 /// True if the step should be subtracted. 6510 bool shouldSubtractStep() const { return SubtractStep; } 6511 /// True, if the compare operator is strict (<, > or !=). 6512 bool isStrictTestOp() const { return TestIsStrictOp; } 6513 /// Build the expression to calculate the number of iterations. 6514 Expr *buildNumIterations( 6515 Scope *S, ArrayRef<LoopIterationSpace> ResultIterSpaces, bool LimitedType, 6516 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 6517 /// Build the precondition expression for the loops. 6518 Expr * 6519 buildPreCond(Scope *S, Expr *Cond, 6520 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 6521 /// Build reference expression to the counter be used for codegen. 6522 DeclRefExpr * 6523 buildCounterVar(llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 6524 DSAStackTy &DSA) const; 6525 /// Build reference expression to the private counter be used for 6526 /// codegen. 6527 Expr *buildPrivateCounterVar() const; 6528 /// Build initialization of the counter be used for codegen. 6529 Expr *buildCounterInit() const; 6530 /// Build step of the counter be used for codegen. 6531 Expr *buildCounterStep() const; 6532 /// Build loop data with counter value for depend clauses in ordered 6533 /// directives. 6534 Expr * 6535 buildOrderedLoopData(Scope *S, Expr *Counter, 6536 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 6537 SourceLocation Loc, Expr *Inc = nullptr, 6538 OverloadedOperatorKind OOK = OO_Amp); 6539 /// Builds the minimum value for the loop counter. 6540 std::pair<Expr *, Expr *> buildMinMaxValues( 6541 Scope *S, llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 6542 /// Builds final condition for the non-rectangular loops. 6543 Expr *buildFinalCondition(Scope *S) const; 6544 /// Return true if any expression is dependent. 6545 bool dependent() const; 6546 /// Returns true if the initializer forms non-rectangular loop. 6547 bool doesInitDependOnLC() const { return InitDependOnLC.hasValue(); } 6548 /// Returns true if the condition forms non-rectangular loop. 6549 bool doesCondDependOnLC() const { return CondDependOnLC.hasValue(); } 6550 /// Returns index of the loop we depend on (starting from 1), or 0 otherwise. 6551 unsigned getLoopDependentIdx() const { 6552 return InitDependOnLC.getValueOr(CondDependOnLC.getValueOr(0)); 6553 } 6554 6555 private: 6556 /// Check the right-hand side of an assignment in the increment 6557 /// expression. 6558 bool checkAndSetIncRHS(Expr *RHS); 6559 /// Helper to set loop counter variable and its initializer. 6560 bool setLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB, 6561 bool EmitDiags); 6562 /// Helper to set upper bound. 6563 bool setUB(Expr *NewUB, llvm::Optional<bool> LessOp, bool StrictOp, 6564 SourceRange SR, SourceLocation SL); 6565 /// Helper to set loop increment. 6566 bool setStep(Expr *NewStep, bool Subtract); 6567 }; 6568 6569 bool OpenMPIterationSpaceChecker::dependent() const { 6570 if (!LCDecl) { 6571 assert(!LB && !UB && !Step); 6572 return false; 6573 } 6574 return LCDecl->getType()->isDependentType() || 6575 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) || 6576 (Step && Step->isValueDependent()); 6577 } 6578 6579 bool OpenMPIterationSpaceChecker::setLCDeclAndLB(ValueDecl *NewLCDecl, 6580 Expr *NewLCRefExpr, 6581 Expr *NewLB, bool EmitDiags) { 6582 // State consistency checking to ensure correct usage. 6583 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr && 6584 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp); 6585 if (!NewLCDecl || !NewLB) 6586 return true; 6587 LCDecl = getCanonicalDecl(NewLCDecl); 6588 LCRef = NewLCRefExpr; 6589 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB)) 6590 if (const CXXConstructorDecl *Ctor = CE->getConstructor()) 6591 if ((Ctor->isCopyOrMoveConstructor() || 6592 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && 6593 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) 6594 NewLB = CE->getArg(0)->IgnoreParenImpCasts(); 6595 LB = NewLB; 6596 if (EmitDiags) 6597 InitDependOnLC = doesDependOnLoopCounter(LB, /*IsInitializer=*/true); 6598 return false; 6599 } 6600 6601 bool OpenMPIterationSpaceChecker::setUB(Expr *NewUB, 6602 llvm::Optional<bool> LessOp, 6603 bool StrictOp, SourceRange SR, 6604 SourceLocation SL) { 6605 // State consistency checking to ensure correct usage. 6606 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr && 6607 Step == nullptr && !TestIsLessOp && !TestIsStrictOp); 6608 if (!NewUB) 6609 return true; 6610 UB = NewUB; 6611 if (LessOp) 6612 TestIsLessOp = LessOp; 6613 TestIsStrictOp = StrictOp; 6614 ConditionSrcRange = SR; 6615 ConditionLoc = SL; 6616 CondDependOnLC = doesDependOnLoopCounter(UB, /*IsInitializer=*/false); 6617 return false; 6618 } 6619 6620 bool OpenMPIterationSpaceChecker::setStep(Expr *NewStep, bool Subtract) { 6621 // State consistency checking to ensure correct usage. 6622 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr); 6623 if (!NewStep) 6624 return true; 6625 if (!NewStep->isValueDependent()) { 6626 // Check that the step is integer expression. 6627 SourceLocation StepLoc = NewStep->getBeginLoc(); 6628 ExprResult Val = SemaRef.PerformOpenMPImplicitIntegerConversion( 6629 StepLoc, getExprAsWritten(NewStep)); 6630 if (Val.isInvalid()) 6631 return true; 6632 NewStep = Val.get(); 6633 6634 // OpenMP [2.6, Canonical Loop Form, Restrictions] 6635 // If test-expr is of form var relational-op b and relational-op is < or 6636 // <= then incr-expr must cause var to increase on each iteration of the 6637 // loop. If test-expr is of form var relational-op b and relational-op is 6638 // > or >= then incr-expr must cause var to decrease on each iteration of 6639 // the loop. 6640 // If test-expr is of form b relational-op var and relational-op is < or 6641 // <= then incr-expr must cause var to decrease on each iteration of the 6642 // loop. If test-expr is of form b relational-op var and relational-op is 6643 // > or >= then incr-expr must cause var to increase on each iteration of 6644 // the loop. 6645 Optional<llvm::APSInt> Result = 6646 NewStep->getIntegerConstantExpr(SemaRef.Context); 6647 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation(); 6648 bool IsConstNeg = 6649 Result && Result->isSigned() && (Subtract != Result->isNegative()); 6650 bool IsConstPos = 6651 Result && Result->isSigned() && (Subtract == Result->isNegative()); 6652 bool IsConstZero = Result && !Result->getBoolValue(); 6653 6654 // != with increment is treated as <; != with decrement is treated as > 6655 if (!TestIsLessOp.hasValue()) 6656 TestIsLessOp = IsConstPos || (IsUnsigned && !Subtract); 6657 if (UB && (IsConstZero || 6658 (TestIsLessOp.getValue() ? 6659 (IsConstNeg || (IsUnsigned && Subtract)) : 6660 (IsConstPos || (IsUnsigned && !Subtract))))) { 6661 SemaRef.Diag(NewStep->getExprLoc(), 6662 diag::err_omp_loop_incr_not_compatible) 6663 << LCDecl << TestIsLessOp.getValue() << NewStep->getSourceRange(); 6664 SemaRef.Diag(ConditionLoc, 6665 diag::note_omp_loop_cond_requres_compatible_incr) 6666 << TestIsLessOp.getValue() << ConditionSrcRange; 6667 return true; 6668 } 6669 if (TestIsLessOp.getValue() == Subtract) { 6670 NewStep = 6671 SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep) 6672 .get(); 6673 Subtract = !Subtract; 6674 } 6675 } 6676 6677 Step = NewStep; 6678 SubtractStep = Subtract; 6679 return false; 6680 } 6681 6682 namespace { 6683 /// Checker for the non-rectangular loops. Checks if the initializer or 6684 /// condition expression references loop counter variable. 6685 class LoopCounterRefChecker final 6686 : public ConstStmtVisitor<LoopCounterRefChecker, bool> { 6687 Sema &SemaRef; 6688 DSAStackTy &Stack; 6689 const ValueDecl *CurLCDecl = nullptr; 6690 const ValueDecl *DepDecl = nullptr; 6691 const ValueDecl *PrevDepDecl = nullptr; 6692 bool IsInitializer = true; 6693 unsigned BaseLoopId = 0; 6694 bool checkDecl(const Expr *E, const ValueDecl *VD) { 6695 if (getCanonicalDecl(VD) == getCanonicalDecl(CurLCDecl)) { 6696 SemaRef.Diag(E->getExprLoc(), diag::err_omp_stmt_depends_on_loop_counter) 6697 << (IsInitializer ? 0 : 1); 6698 return false; 6699 } 6700 const auto &&Data = Stack.isLoopControlVariable(VD); 6701 // OpenMP, 2.9.1 Canonical Loop Form, Restrictions. 6702 // The type of the loop iterator on which we depend may not have a random 6703 // access iterator type. 6704 if (Data.first && VD->getType()->isRecordType()) { 6705 SmallString<128> Name; 6706 llvm::raw_svector_ostream OS(Name); 6707 VD->getNameForDiagnostic(OS, SemaRef.getPrintingPolicy(), 6708 /*Qualified=*/true); 6709 SemaRef.Diag(E->getExprLoc(), 6710 diag::err_omp_wrong_dependency_iterator_type) 6711 << OS.str(); 6712 SemaRef.Diag(VD->getLocation(), diag::note_previous_decl) << VD; 6713 return false; 6714 } 6715 if (Data.first && 6716 (DepDecl || (PrevDepDecl && 6717 getCanonicalDecl(VD) != getCanonicalDecl(PrevDepDecl)))) { 6718 if (!DepDecl && PrevDepDecl) 6719 DepDecl = PrevDepDecl; 6720 SmallString<128> Name; 6721 llvm::raw_svector_ostream OS(Name); 6722 DepDecl->getNameForDiagnostic(OS, SemaRef.getPrintingPolicy(), 6723 /*Qualified=*/true); 6724 SemaRef.Diag(E->getExprLoc(), 6725 diag::err_omp_invariant_or_linear_dependency) 6726 << OS.str(); 6727 return false; 6728 } 6729 if (Data.first) { 6730 DepDecl = VD; 6731 BaseLoopId = Data.first; 6732 } 6733 return Data.first; 6734 } 6735 6736 public: 6737 bool VisitDeclRefExpr(const DeclRefExpr *E) { 6738 const ValueDecl *VD = E->getDecl(); 6739 if (isa<VarDecl>(VD)) 6740 return checkDecl(E, VD); 6741 return false; 6742 } 6743 bool VisitMemberExpr(const MemberExpr *E) { 6744 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) { 6745 const ValueDecl *VD = E->getMemberDecl(); 6746 if (isa<VarDecl>(VD) || isa<FieldDecl>(VD)) 6747 return checkDecl(E, VD); 6748 } 6749 return false; 6750 } 6751 bool VisitStmt(const Stmt *S) { 6752 bool Res = false; 6753 for (const Stmt *Child : S->children()) 6754 Res = (Child && Visit(Child)) || Res; 6755 return Res; 6756 } 6757 explicit LoopCounterRefChecker(Sema &SemaRef, DSAStackTy &Stack, 6758 const ValueDecl *CurLCDecl, bool IsInitializer, 6759 const ValueDecl *PrevDepDecl = nullptr) 6760 : SemaRef(SemaRef), Stack(Stack), CurLCDecl(CurLCDecl), 6761 PrevDepDecl(PrevDepDecl), IsInitializer(IsInitializer) {} 6762 unsigned getBaseLoopId() const { 6763 assert(CurLCDecl && "Expected loop dependency."); 6764 return BaseLoopId; 6765 } 6766 const ValueDecl *getDepDecl() const { 6767 assert(CurLCDecl && "Expected loop dependency."); 6768 return DepDecl; 6769 } 6770 }; 6771 } // namespace 6772 6773 Optional<unsigned> 6774 OpenMPIterationSpaceChecker::doesDependOnLoopCounter(const Stmt *S, 6775 bool IsInitializer) { 6776 // Check for the non-rectangular loops. 6777 LoopCounterRefChecker LoopStmtChecker(SemaRef, Stack, LCDecl, IsInitializer, 6778 DepDecl); 6779 if (LoopStmtChecker.Visit(S)) { 6780 DepDecl = LoopStmtChecker.getDepDecl(); 6781 return LoopStmtChecker.getBaseLoopId(); 6782 } 6783 return llvm::None; 6784 } 6785 6786 bool OpenMPIterationSpaceChecker::checkAndSetInit(Stmt *S, bool EmitDiags) { 6787 // Check init-expr for canonical loop form and save loop counter 6788 // variable - #Var and its initialization value - #LB. 6789 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following: 6790 // var = lb 6791 // integer-type var = lb 6792 // random-access-iterator-type var = lb 6793 // pointer-type var = lb 6794 // 6795 if (!S) { 6796 if (EmitDiags) { 6797 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init); 6798 } 6799 return true; 6800 } 6801 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S)) 6802 if (!ExprTemp->cleanupsHaveSideEffects()) 6803 S = ExprTemp->getSubExpr(); 6804 6805 InitSrcRange = S->getSourceRange(); 6806 if (Expr *E = dyn_cast<Expr>(S)) 6807 S = E->IgnoreParens(); 6808 if (auto *BO = dyn_cast<BinaryOperator>(S)) { 6809 if (BO->getOpcode() == BO_Assign) { 6810 Expr *LHS = BO->getLHS()->IgnoreParens(); 6811 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) { 6812 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) 6813 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 6814 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 6815 EmitDiags); 6816 return setLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS(), EmitDiags); 6817 } 6818 if (auto *ME = dyn_cast<MemberExpr>(LHS)) { 6819 if (ME->isArrow() && 6820 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 6821 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 6822 EmitDiags); 6823 } 6824 } 6825 } else if (auto *DS = dyn_cast<DeclStmt>(S)) { 6826 if (DS->isSingleDecl()) { 6827 if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) { 6828 if (Var->hasInit() && !Var->getType()->isReferenceType()) { 6829 // Accept non-canonical init form here but emit ext. warning. 6830 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags) 6831 SemaRef.Diag(S->getBeginLoc(), 6832 diag::ext_omp_loop_not_canonical_init) 6833 << S->getSourceRange(); 6834 return setLCDeclAndLB( 6835 Var, 6836 buildDeclRefExpr(SemaRef, Var, 6837 Var->getType().getNonReferenceType(), 6838 DS->getBeginLoc()), 6839 Var->getInit(), EmitDiags); 6840 } 6841 } 6842 } 6843 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 6844 if (CE->getOperator() == OO_Equal) { 6845 Expr *LHS = CE->getArg(0); 6846 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) { 6847 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) 6848 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 6849 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 6850 EmitDiags); 6851 return setLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1), EmitDiags); 6852 } 6853 if (auto *ME = dyn_cast<MemberExpr>(LHS)) { 6854 if (ME->isArrow() && 6855 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 6856 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 6857 EmitDiags); 6858 } 6859 } 6860 } 6861 6862 if (dependent() || SemaRef.CurContext->isDependentContext()) 6863 return false; 6864 if (EmitDiags) { 6865 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_init) 6866 << S->getSourceRange(); 6867 } 6868 return true; 6869 } 6870 6871 /// Ignore parenthesizes, implicit casts, copy constructor and return the 6872 /// variable (which may be the loop variable) if possible. 6873 static const ValueDecl *getInitLCDecl(const Expr *E) { 6874 if (!E) 6875 return nullptr; 6876 E = getExprAsWritten(E); 6877 if (const auto *CE = dyn_cast_or_null<CXXConstructExpr>(E)) 6878 if (const CXXConstructorDecl *Ctor = CE->getConstructor()) 6879 if ((Ctor->isCopyOrMoveConstructor() || 6880 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && 6881 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) 6882 E = CE->getArg(0)->IgnoreParenImpCasts(); 6883 if (const auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) { 6884 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) 6885 return getCanonicalDecl(VD); 6886 } 6887 if (const auto *ME = dyn_cast_or_null<MemberExpr>(E)) 6888 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 6889 return getCanonicalDecl(ME->getMemberDecl()); 6890 return nullptr; 6891 } 6892 6893 bool OpenMPIterationSpaceChecker::checkAndSetCond(Expr *S) { 6894 // Check test-expr for canonical form, save upper-bound UB, flags for 6895 // less/greater and for strict/non-strict comparison. 6896 // OpenMP [2.9] Canonical loop form. Test-expr may be one of the following: 6897 // var relational-op b 6898 // b relational-op var 6899 // 6900 bool IneqCondIsCanonical = SemaRef.getLangOpts().OpenMP >= 50; 6901 if (!S) { 6902 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) 6903 << (IneqCondIsCanonical ? 1 : 0) << LCDecl; 6904 return true; 6905 } 6906 Condition = S; 6907 S = getExprAsWritten(S); 6908 SourceLocation CondLoc = S->getBeginLoc(); 6909 if (auto *BO = dyn_cast<BinaryOperator>(S)) { 6910 if (BO->isRelationalOp()) { 6911 if (getInitLCDecl(BO->getLHS()) == LCDecl) 6912 return setUB(BO->getRHS(), 6913 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE), 6914 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT), 6915 BO->getSourceRange(), BO->getOperatorLoc()); 6916 if (getInitLCDecl(BO->getRHS()) == LCDecl) 6917 return setUB(BO->getLHS(), 6918 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE), 6919 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT), 6920 BO->getSourceRange(), BO->getOperatorLoc()); 6921 } else if (IneqCondIsCanonical && BO->getOpcode() == BO_NE) 6922 return setUB( 6923 getInitLCDecl(BO->getLHS()) == LCDecl ? BO->getRHS() : BO->getLHS(), 6924 /*LessOp=*/llvm::None, 6925 /*StrictOp=*/true, BO->getSourceRange(), BO->getOperatorLoc()); 6926 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 6927 if (CE->getNumArgs() == 2) { 6928 auto Op = CE->getOperator(); 6929 switch (Op) { 6930 case OO_Greater: 6931 case OO_GreaterEqual: 6932 case OO_Less: 6933 case OO_LessEqual: 6934 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 6935 return setUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual, 6936 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(), 6937 CE->getOperatorLoc()); 6938 if (getInitLCDecl(CE->getArg(1)) == LCDecl) 6939 return setUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual, 6940 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(), 6941 CE->getOperatorLoc()); 6942 break; 6943 case OO_ExclaimEqual: 6944 if (IneqCondIsCanonical) 6945 return setUB(getInitLCDecl(CE->getArg(0)) == LCDecl ? CE->getArg(1) 6946 : CE->getArg(0), 6947 /*LessOp=*/llvm::None, 6948 /*StrictOp=*/true, CE->getSourceRange(), 6949 CE->getOperatorLoc()); 6950 break; 6951 default: 6952 break; 6953 } 6954 } 6955 } 6956 if (dependent() || SemaRef.CurContext->isDependentContext()) 6957 return false; 6958 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond) 6959 << (IneqCondIsCanonical ? 1 : 0) << S->getSourceRange() << LCDecl; 6960 return true; 6961 } 6962 6963 bool OpenMPIterationSpaceChecker::checkAndSetIncRHS(Expr *RHS) { 6964 // RHS of canonical loop form increment can be: 6965 // var + incr 6966 // incr + var 6967 // var - incr 6968 // 6969 RHS = RHS->IgnoreParenImpCasts(); 6970 if (auto *BO = dyn_cast<BinaryOperator>(RHS)) { 6971 if (BO->isAdditiveOp()) { 6972 bool IsAdd = BO->getOpcode() == BO_Add; 6973 if (getInitLCDecl(BO->getLHS()) == LCDecl) 6974 return setStep(BO->getRHS(), !IsAdd); 6975 if (IsAdd && getInitLCDecl(BO->getRHS()) == LCDecl) 6976 return setStep(BO->getLHS(), /*Subtract=*/false); 6977 } 6978 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) { 6979 bool IsAdd = CE->getOperator() == OO_Plus; 6980 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) { 6981 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 6982 return setStep(CE->getArg(1), !IsAdd); 6983 if (IsAdd && getInitLCDecl(CE->getArg(1)) == LCDecl) 6984 return setStep(CE->getArg(0), /*Subtract=*/false); 6985 } 6986 } 6987 if (dependent() || SemaRef.CurContext->isDependentContext()) 6988 return false; 6989 SemaRef.Diag(RHS->getBeginLoc(), diag::err_omp_loop_not_canonical_incr) 6990 << RHS->getSourceRange() << LCDecl; 6991 return true; 6992 } 6993 6994 bool OpenMPIterationSpaceChecker::checkAndSetInc(Expr *S) { 6995 // Check incr-expr for canonical loop form and return true if it 6996 // does not conform. 6997 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following: 6998 // ++var 6999 // var++ 7000 // --var 7001 // var-- 7002 // var += incr 7003 // var -= incr 7004 // var = var + incr 7005 // var = incr + var 7006 // var = var - incr 7007 // 7008 if (!S) { 7009 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl; 7010 return true; 7011 } 7012 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S)) 7013 if (!ExprTemp->cleanupsHaveSideEffects()) 7014 S = ExprTemp->getSubExpr(); 7015 7016 IncrementSrcRange = S->getSourceRange(); 7017 S = S->IgnoreParens(); 7018 if (auto *UO = dyn_cast<UnaryOperator>(S)) { 7019 if (UO->isIncrementDecrementOp() && 7020 getInitLCDecl(UO->getSubExpr()) == LCDecl) 7021 return setStep(SemaRef 7022 .ActOnIntegerConstant(UO->getBeginLoc(), 7023 (UO->isDecrementOp() ? -1 : 1)) 7024 .get(), 7025 /*Subtract=*/false); 7026 } else if (auto *BO = dyn_cast<BinaryOperator>(S)) { 7027 switch (BO->getOpcode()) { 7028 case BO_AddAssign: 7029 case BO_SubAssign: 7030 if (getInitLCDecl(BO->getLHS()) == LCDecl) 7031 return setStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign); 7032 break; 7033 case BO_Assign: 7034 if (getInitLCDecl(BO->getLHS()) == LCDecl) 7035 return checkAndSetIncRHS(BO->getRHS()); 7036 break; 7037 default: 7038 break; 7039 } 7040 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 7041 switch (CE->getOperator()) { 7042 case OO_PlusPlus: 7043 case OO_MinusMinus: 7044 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 7045 return setStep(SemaRef 7046 .ActOnIntegerConstant( 7047 CE->getBeginLoc(), 7048 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)) 7049 .get(), 7050 /*Subtract=*/false); 7051 break; 7052 case OO_PlusEqual: 7053 case OO_MinusEqual: 7054 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 7055 return setStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual); 7056 break; 7057 case OO_Equal: 7058 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 7059 return checkAndSetIncRHS(CE->getArg(1)); 7060 break; 7061 default: 7062 break; 7063 } 7064 } 7065 if (dependent() || SemaRef.CurContext->isDependentContext()) 7066 return false; 7067 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_incr) 7068 << S->getSourceRange() << LCDecl; 7069 return true; 7070 } 7071 7072 static ExprResult 7073 tryBuildCapture(Sema &SemaRef, Expr *Capture, 7074 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 7075 if (SemaRef.CurContext->isDependentContext() || Capture->containsErrors()) 7076 return Capture; 7077 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects)) 7078 return SemaRef.PerformImplicitConversion( 7079 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting, 7080 /*AllowExplicit=*/true); 7081 auto I = Captures.find(Capture); 7082 if (I != Captures.end()) 7083 return buildCapture(SemaRef, Capture, I->second); 7084 DeclRefExpr *Ref = nullptr; 7085 ExprResult Res = buildCapture(SemaRef, Capture, Ref); 7086 Captures[Capture] = Ref; 7087 return Res; 7088 } 7089 7090 /// Calculate number of iterations, transforming to unsigned, if number of 7091 /// iterations may be larger than the original type. 7092 static Expr * 7093 calculateNumIters(Sema &SemaRef, Scope *S, SourceLocation DefaultLoc, 7094 Expr *Lower, Expr *Upper, Expr *Step, QualType LCTy, 7095 bool TestIsStrictOp, bool RoundToStep, 7096 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 7097 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures); 7098 if (!NewStep.isUsable()) 7099 return nullptr; 7100 llvm::APSInt LRes, SRes; 7101 bool IsLowerConst = false, IsStepConst = false; 7102 if (Optional<llvm::APSInt> Res = Lower->getIntegerConstantExpr(SemaRef.Context)) { 7103 LRes = *Res; 7104 IsLowerConst = true; 7105 } 7106 if (Optional<llvm::APSInt> Res = Step->getIntegerConstantExpr(SemaRef.Context)) { 7107 SRes = *Res; 7108 IsStepConst = true; 7109 } 7110 bool NoNeedToConvert = IsLowerConst && !RoundToStep && 7111 ((!TestIsStrictOp && LRes.isNonNegative()) || 7112 (TestIsStrictOp && LRes.isStrictlyPositive())); 7113 bool NeedToReorganize = false; 7114 // Check if any subexpressions in Lower -Step [+ 1] lead to overflow. 7115 if (!NoNeedToConvert && IsLowerConst && 7116 (TestIsStrictOp || (RoundToStep && IsStepConst))) { 7117 NoNeedToConvert = true; 7118 if (RoundToStep) { 7119 unsigned BW = LRes.getBitWidth() > SRes.getBitWidth() 7120 ? LRes.getBitWidth() 7121 : SRes.getBitWidth(); 7122 LRes = LRes.extend(BW + 1); 7123 LRes.setIsSigned(true); 7124 SRes = SRes.extend(BW + 1); 7125 SRes.setIsSigned(true); 7126 LRes -= SRes; 7127 NoNeedToConvert = LRes.trunc(BW).extend(BW + 1) == LRes; 7128 LRes = LRes.trunc(BW); 7129 } 7130 if (TestIsStrictOp) { 7131 unsigned BW = LRes.getBitWidth(); 7132 LRes = LRes.extend(BW + 1); 7133 LRes.setIsSigned(true); 7134 ++LRes; 7135 NoNeedToConvert = 7136 NoNeedToConvert && LRes.trunc(BW).extend(BW + 1) == LRes; 7137 // truncate to the original bitwidth. 7138 LRes = LRes.trunc(BW); 7139 } 7140 NeedToReorganize = NoNeedToConvert; 7141 } 7142 llvm::APSInt URes; 7143 bool IsUpperConst = false; 7144 if (Optional<llvm::APSInt> Res = Upper->getIntegerConstantExpr(SemaRef.Context)) { 7145 URes = *Res; 7146 IsUpperConst = true; 7147 } 7148 if (NoNeedToConvert && IsLowerConst && IsUpperConst && 7149 (!RoundToStep || IsStepConst)) { 7150 unsigned BW = LRes.getBitWidth() > URes.getBitWidth() ? LRes.getBitWidth() 7151 : URes.getBitWidth(); 7152 LRes = LRes.extend(BW + 1); 7153 LRes.setIsSigned(true); 7154 URes = URes.extend(BW + 1); 7155 URes.setIsSigned(true); 7156 URes -= LRes; 7157 NoNeedToConvert = URes.trunc(BW).extend(BW + 1) == URes; 7158 NeedToReorganize = NoNeedToConvert; 7159 } 7160 // If the boundaries are not constant or (Lower - Step [+ 1]) is not constant 7161 // or less than zero (Upper - (Lower - Step [+ 1]) may overflow) - promote to 7162 // unsigned. 7163 if ((!NoNeedToConvert || (LRes.isNegative() && !IsUpperConst)) && 7164 !LCTy->isDependentType() && LCTy->isIntegerType()) { 7165 QualType LowerTy = Lower->getType(); 7166 QualType UpperTy = Upper->getType(); 7167 uint64_t LowerSize = SemaRef.Context.getTypeSize(LowerTy); 7168 uint64_t UpperSize = SemaRef.Context.getTypeSize(UpperTy); 7169 if ((LowerSize <= UpperSize && UpperTy->hasSignedIntegerRepresentation()) || 7170 (LowerSize > UpperSize && LowerTy->hasSignedIntegerRepresentation())) { 7171 QualType CastType = SemaRef.Context.getIntTypeForBitwidth( 7172 LowerSize > UpperSize ? LowerSize : UpperSize, /*Signed=*/0); 7173 Upper = 7174 SemaRef 7175 .PerformImplicitConversion( 7176 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Upper).get(), 7177 CastType, Sema::AA_Converting) 7178 .get(); 7179 Lower = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Lower).get(); 7180 NewStep = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, NewStep.get()); 7181 } 7182 } 7183 if (!Lower || !Upper || NewStep.isInvalid()) 7184 return nullptr; 7185 7186 ExprResult Diff; 7187 // If need to reorganize, then calculate the form as Upper - (Lower - Step [+ 7188 // 1]). 7189 if (NeedToReorganize) { 7190 Diff = Lower; 7191 7192 if (RoundToStep) { 7193 // Lower - Step 7194 Diff = 7195 SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Diff.get(), NewStep.get()); 7196 if (!Diff.isUsable()) 7197 return nullptr; 7198 } 7199 7200 // Lower - Step [+ 1] 7201 if (TestIsStrictOp) 7202 Diff = SemaRef.BuildBinOp( 7203 S, DefaultLoc, BO_Add, Diff.get(), 7204 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 7205 if (!Diff.isUsable()) 7206 return nullptr; 7207 7208 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 7209 if (!Diff.isUsable()) 7210 return nullptr; 7211 7212 // Upper - (Lower - Step [+ 1]). 7213 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Diff.get()); 7214 if (!Diff.isUsable()) 7215 return nullptr; 7216 } else { 7217 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower); 7218 7219 if (!Diff.isUsable() && LCTy->getAsCXXRecordDecl()) { 7220 // BuildBinOp already emitted error, this one is to point user to upper 7221 // and lower bound, and to tell what is passed to 'operator-'. 7222 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx) 7223 << Upper->getSourceRange() << Lower->getSourceRange(); 7224 return nullptr; 7225 } 7226 7227 if (!Diff.isUsable()) 7228 return nullptr; 7229 7230 // Upper - Lower [- 1] 7231 if (TestIsStrictOp) 7232 Diff = SemaRef.BuildBinOp( 7233 S, DefaultLoc, BO_Sub, Diff.get(), 7234 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 7235 if (!Diff.isUsable()) 7236 return nullptr; 7237 7238 if (RoundToStep) { 7239 // Upper - Lower [- 1] + Step 7240 Diff = 7241 SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get()); 7242 if (!Diff.isUsable()) 7243 return nullptr; 7244 } 7245 } 7246 7247 // Parentheses (for dumping/debugging purposes only). 7248 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 7249 if (!Diff.isUsable()) 7250 return nullptr; 7251 7252 // (Upper - Lower [- 1] + Step) / Step or (Upper - Lower) / Step 7253 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get()); 7254 if (!Diff.isUsable()) 7255 return nullptr; 7256 7257 return Diff.get(); 7258 } 7259 7260 /// Build the expression to calculate the number of iterations. 7261 Expr *OpenMPIterationSpaceChecker::buildNumIterations( 7262 Scope *S, ArrayRef<LoopIterationSpace> ResultIterSpaces, bool LimitedType, 7263 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 7264 QualType VarType = LCDecl->getType().getNonReferenceType(); 7265 if (!VarType->isIntegerType() && !VarType->isPointerType() && 7266 !SemaRef.getLangOpts().CPlusPlus) 7267 return nullptr; 7268 Expr *LBVal = LB; 7269 Expr *UBVal = UB; 7270 // LB = TestIsLessOp.getValue() ? min(LB(MinVal), LB(MaxVal)) : 7271 // max(LB(MinVal), LB(MaxVal)) 7272 if (InitDependOnLC) { 7273 const LoopIterationSpace &IS = 7274 ResultIterSpaces[ResultIterSpaces.size() - 1 - 7275 InitDependOnLC.getValueOr( 7276 CondDependOnLC.getValueOr(0))]; 7277 if (!IS.MinValue || !IS.MaxValue) 7278 return nullptr; 7279 // OuterVar = Min 7280 ExprResult MinValue = 7281 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MinValue); 7282 if (!MinValue.isUsable()) 7283 return nullptr; 7284 7285 ExprResult LBMinVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign, 7286 IS.CounterVar, MinValue.get()); 7287 if (!LBMinVal.isUsable()) 7288 return nullptr; 7289 // OuterVar = Min, LBVal 7290 LBMinVal = 7291 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, LBMinVal.get(), LBVal); 7292 if (!LBMinVal.isUsable()) 7293 return nullptr; 7294 // (OuterVar = Min, LBVal) 7295 LBMinVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, LBMinVal.get()); 7296 if (!LBMinVal.isUsable()) 7297 return nullptr; 7298 7299 // OuterVar = Max 7300 ExprResult MaxValue = 7301 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MaxValue); 7302 if (!MaxValue.isUsable()) 7303 return nullptr; 7304 7305 ExprResult LBMaxVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign, 7306 IS.CounterVar, MaxValue.get()); 7307 if (!LBMaxVal.isUsable()) 7308 return nullptr; 7309 // OuterVar = Max, LBVal 7310 LBMaxVal = 7311 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, LBMaxVal.get(), LBVal); 7312 if (!LBMaxVal.isUsable()) 7313 return nullptr; 7314 // (OuterVar = Max, LBVal) 7315 LBMaxVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, LBMaxVal.get()); 7316 if (!LBMaxVal.isUsable()) 7317 return nullptr; 7318 7319 Expr *LBMin = tryBuildCapture(SemaRef, LBMinVal.get(), Captures).get(); 7320 Expr *LBMax = tryBuildCapture(SemaRef, LBMaxVal.get(), Captures).get(); 7321 if (!LBMin || !LBMax) 7322 return nullptr; 7323 // LB(MinVal) < LB(MaxVal) 7324 ExprResult MinLessMaxRes = 7325 SemaRef.BuildBinOp(S, DefaultLoc, BO_LT, LBMin, LBMax); 7326 if (!MinLessMaxRes.isUsable()) 7327 return nullptr; 7328 Expr *MinLessMax = 7329 tryBuildCapture(SemaRef, MinLessMaxRes.get(), Captures).get(); 7330 if (!MinLessMax) 7331 return nullptr; 7332 if (TestIsLessOp.getValue()) { 7333 // LB(MinVal) < LB(MaxVal) ? LB(MinVal) : LB(MaxVal) - min(LB(MinVal), 7334 // LB(MaxVal)) 7335 ExprResult MinLB = SemaRef.ActOnConditionalOp(DefaultLoc, DefaultLoc, 7336 MinLessMax, LBMin, LBMax); 7337 if (!MinLB.isUsable()) 7338 return nullptr; 7339 LBVal = MinLB.get(); 7340 } else { 7341 // LB(MinVal) < LB(MaxVal) ? LB(MaxVal) : LB(MinVal) - max(LB(MinVal), 7342 // LB(MaxVal)) 7343 ExprResult MaxLB = SemaRef.ActOnConditionalOp(DefaultLoc, DefaultLoc, 7344 MinLessMax, LBMax, LBMin); 7345 if (!MaxLB.isUsable()) 7346 return nullptr; 7347 LBVal = MaxLB.get(); 7348 } 7349 } 7350 // UB = TestIsLessOp.getValue() ? max(UB(MinVal), UB(MaxVal)) : 7351 // min(UB(MinVal), UB(MaxVal)) 7352 if (CondDependOnLC) { 7353 const LoopIterationSpace &IS = 7354 ResultIterSpaces[ResultIterSpaces.size() - 1 - 7355 InitDependOnLC.getValueOr( 7356 CondDependOnLC.getValueOr(0))]; 7357 if (!IS.MinValue || !IS.MaxValue) 7358 return nullptr; 7359 // OuterVar = Min 7360 ExprResult MinValue = 7361 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MinValue); 7362 if (!MinValue.isUsable()) 7363 return nullptr; 7364 7365 ExprResult UBMinVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign, 7366 IS.CounterVar, MinValue.get()); 7367 if (!UBMinVal.isUsable()) 7368 return nullptr; 7369 // OuterVar = Min, UBVal 7370 UBMinVal = 7371 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, UBMinVal.get(), UBVal); 7372 if (!UBMinVal.isUsable()) 7373 return nullptr; 7374 // (OuterVar = Min, UBVal) 7375 UBMinVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, UBMinVal.get()); 7376 if (!UBMinVal.isUsable()) 7377 return nullptr; 7378 7379 // OuterVar = Max 7380 ExprResult MaxValue = 7381 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MaxValue); 7382 if (!MaxValue.isUsable()) 7383 return nullptr; 7384 7385 ExprResult UBMaxVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign, 7386 IS.CounterVar, MaxValue.get()); 7387 if (!UBMaxVal.isUsable()) 7388 return nullptr; 7389 // OuterVar = Max, UBVal 7390 UBMaxVal = 7391 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, UBMaxVal.get(), UBVal); 7392 if (!UBMaxVal.isUsable()) 7393 return nullptr; 7394 // (OuterVar = Max, UBVal) 7395 UBMaxVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, UBMaxVal.get()); 7396 if (!UBMaxVal.isUsable()) 7397 return nullptr; 7398 7399 Expr *UBMin = tryBuildCapture(SemaRef, UBMinVal.get(), Captures).get(); 7400 Expr *UBMax = tryBuildCapture(SemaRef, UBMaxVal.get(), Captures).get(); 7401 if (!UBMin || !UBMax) 7402 return nullptr; 7403 // UB(MinVal) > UB(MaxVal) 7404 ExprResult MinGreaterMaxRes = 7405 SemaRef.BuildBinOp(S, DefaultLoc, BO_GT, UBMin, UBMax); 7406 if (!MinGreaterMaxRes.isUsable()) 7407 return nullptr; 7408 Expr *MinGreaterMax = 7409 tryBuildCapture(SemaRef, MinGreaterMaxRes.get(), Captures).get(); 7410 if (!MinGreaterMax) 7411 return nullptr; 7412 if (TestIsLessOp.getValue()) { 7413 // UB(MinVal) > UB(MaxVal) ? UB(MinVal) : UB(MaxVal) - max(UB(MinVal), 7414 // UB(MaxVal)) 7415 ExprResult MaxUB = SemaRef.ActOnConditionalOp( 7416 DefaultLoc, DefaultLoc, MinGreaterMax, UBMin, UBMax); 7417 if (!MaxUB.isUsable()) 7418 return nullptr; 7419 UBVal = MaxUB.get(); 7420 } else { 7421 // UB(MinVal) > UB(MaxVal) ? UB(MaxVal) : UB(MinVal) - min(UB(MinVal), 7422 // UB(MaxVal)) 7423 ExprResult MinUB = SemaRef.ActOnConditionalOp( 7424 DefaultLoc, DefaultLoc, MinGreaterMax, UBMax, UBMin); 7425 if (!MinUB.isUsable()) 7426 return nullptr; 7427 UBVal = MinUB.get(); 7428 } 7429 } 7430 Expr *UBExpr = TestIsLessOp.getValue() ? UBVal : LBVal; 7431 Expr *LBExpr = TestIsLessOp.getValue() ? LBVal : UBVal; 7432 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get(); 7433 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get(); 7434 if (!Upper || !Lower) 7435 return nullptr; 7436 7437 ExprResult Diff = 7438 calculateNumIters(SemaRef, S, DefaultLoc, Lower, Upper, Step, VarType, 7439 TestIsStrictOp, /*RoundToStep=*/true, Captures); 7440 if (!Diff.isUsable()) 7441 return nullptr; 7442 7443 // OpenMP runtime requires 32-bit or 64-bit loop variables. 7444 QualType Type = Diff.get()->getType(); 7445 ASTContext &C = SemaRef.Context; 7446 bool UseVarType = VarType->hasIntegerRepresentation() && 7447 C.getTypeSize(Type) > C.getTypeSize(VarType); 7448 if (!Type->isIntegerType() || UseVarType) { 7449 unsigned NewSize = 7450 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type); 7451 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation() 7452 : Type->hasSignedIntegerRepresentation(); 7453 Type = C.getIntTypeForBitwidth(NewSize, IsSigned); 7454 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) { 7455 Diff = SemaRef.PerformImplicitConversion( 7456 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true); 7457 if (!Diff.isUsable()) 7458 return nullptr; 7459 } 7460 } 7461 if (LimitedType) { 7462 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32; 7463 if (NewSize != C.getTypeSize(Type)) { 7464 if (NewSize < C.getTypeSize(Type)) { 7465 assert(NewSize == 64 && "incorrect loop var size"); 7466 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var) 7467 << InitSrcRange << ConditionSrcRange; 7468 } 7469 QualType NewType = C.getIntTypeForBitwidth( 7470 NewSize, Type->hasSignedIntegerRepresentation() || 7471 C.getTypeSize(Type) < NewSize); 7472 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) { 7473 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType, 7474 Sema::AA_Converting, true); 7475 if (!Diff.isUsable()) 7476 return nullptr; 7477 } 7478 } 7479 } 7480 7481 return Diff.get(); 7482 } 7483 7484 std::pair<Expr *, Expr *> OpenMPIterationSpaceChecker::buildMinMaxValues( 7485 Scope *S, llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 7486 // Do not build for iterators, they cannot be used in non-rectangular loop 7487 // nests. 7488 if (LCDecl->getType()->isRecordType()) 7489 return std::make_pair(nullptr, nullptr); 7490 // If we subtract, the min is in the condition, otherwise the min is in the 7491 // init value. 7492 Expr *MinExpr = nullptr; 7493 Expr *MaxExpr = nullptr; 7494 Expr *LBExpr = TestIsLessOp.getValue() ? LB : UB; 7495 Expr *UBExpr = TestIsLessOp.getValue() ? UB : LB; 7496 bool LBNonRect = TestIsLessOp.getValue() ? InitDependOnLC.hasValue() 7497 : CondDependOnLC.hasValue(); 7498 bool UBNonRect = TestIsLessOp.getValue() ? CondDependOnLC.hasValue() 7499 : InitDependOnLC.hasValue(); 7500 Expr *Lower = 7501 LBNonRect ? LBExpr : tryBuildCapture(SemaRef, LBExpr, Captures).get(); 7502 Expr *Upper = 7503 UBNonRect ? UBExpr : tryBuildCapture(SemaRef, UBExpr, Captures).get(); 7504 if (!Upper || !Lower) 7505 return std::make_pair(nullptr, nullptr); 7506 7507 if (TestIsLessOp.getValue()) 7508 MinExpr = Lower; 7509 else 7510 MaxExpr = Upper; 7511 7512 // Build minimum/maximum value based on number of iterations. 7513 QualType VarType = LCDecl->getType().getNonReferenceType(); 7514 7515 ExprResult Diff = 7516 calculateNumIters(SemaRef, S, DefaultLoc, Lower, Upper, Step, VarType, 7517 TestIsStrictOp, /*RoundToStep=*/false, Captures); 7518 if (!Diff.isUsable()) 7519 return std::make_pair(nullptr, nullptr); 7520 7521 // ((Upper - Lower [- 1]) / Step) * Step 7522 // Parentheses (for dumping/debugging purposes only). 7523 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 7524 if (!Diff.isUsable()) 7525 return std::make_pair(nullptr, nullptr); 7526 7527 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures); 7528 if (!NewStep.isUsable()) 7529 return std::make_pair(nullptr, nullptr); 7530 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Mul, Diff.get(), NewStep.get()); 7531 if (!Diff.isUsable()) 7532 return std::make_pair(nullptr, nullptr); 7533 7534 // Parentheses (for dumping/debugging purposes only). 7535 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 7536 if (!Diff.isUsable()) 7537 return std::make_pair(nullptr, nullptr); 7538 7539 // Convert to the ptrdiff_t, if original type is pointer. 7540 if (VarType->isAnyPointerType() && 7541 !SemaRef.Context.hasSameType( 7542 Diff.get()->getType(), 7543 SemaRef.Context.getUnsignedPointerDiffType())) { 7544 Diff = SemaRef.PerformImplicitConversion( 7545 Diff.get(), SemaRef.Context.getUnsignedPointerDiffType(), 7546 Sema::AA_Converting, /*AllowExplicit=*/true); 7547 } 7548 if (!Diff.isUsable()) 7549 return std::make_pair(nullptr, nullptr); 7550 7551 if (TestIsLessOp.getValue()) { 7552 // MinExpr = Lower; 7553 // MaxExpr = Lower + (((Upper - Lower [- 1]) / Step) * Step) 7554 Diff = SemaRef.BuildBinOp( 7555 S, DefaultLoc, BO_Add, 7556 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Lower).get(), 7557 Diff.get()); 7558 if (!Diff.isUsable()) 7559 return std::make_pair(nullptr, nullptr); 7560 } else { 7561 // MaxExpr = Upper; 7562 // MinExpr = Upper - (((Upper - Lower [- 1]) / Step) * Step) 7563 Diff = SemaRef.BuildBinOp( 7564 S, DefaultLoc, BO_Sub, 7565 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Upper).get(), 7566 Diff.get()); 7567 if (!Diff.isUsable()) 7568 return std::make_pair(nullptr, nullptr); 7569 } 7570 7571 // Convert to the original type. 7572 if (SemaRef.Context.hasSameType(Diff.get()->getType(), VarType)) 7573 Diff = SemaRef.PerformImplicitConversion(Diff.get(), VarType, 7574 Sema::AA_Converting, 7575 /*AllowExplicit=*/true); 7576 if (!Diff.isUsable()) 7577 return std::make_pair(nullptr, nullptr); 7578 7579 Diff = SemaRef.ActOnFinishFullExpr(Diff.get(), /*DiscardedValue=*/false); 7580 if (!Diff.isUsable()) 7581 return std::make_pair(nullptr, nullptr); 7582 7583 if (TestIsLessOp.getValue()) 7584 MaxExpr = Diff.get(); 7585 else 7586 MinExpr = Diff.get(); 7587 7588 return std::make_pair(MinExpr, MaxExpr); 7589 } 7590 7591 Expr *OpenMPIterationSpaceChecker::buildFinalCondition(Scope *S) const { 7592 if (InitDependOnLC || CondDependOnLC) 7593 return Condition; 7594 return nullptr; 7595 } 7596 7597 Expr *OpenMPIterationSpaceChecker::buildPreCond( 7598 Scope *S, Expr *Cond, 7599 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 7600 // Do not build a precondition when the condition/initialization is dependent 7601 // to prevent pessimistic early loop exit. 7602 // TODO: this can be improved by calculating min/max values but not sure that 7603 // it will be very effective. 7604 if (CondDependOnLC || InitDependOnLC) 7605 return SemaRef.PerformImplicitConversion( 7606 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(), 7607 SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting, 7608 /*AllowExplicit=*/true).get(); 7609 7610 // Try to build LB <op> UB, where <op> is <, >, <=, or >=. 7611 Sema::TentativeAnalysisScope Trap(SemaRef); 7612 7613 ExprResult NewLB = tryBuildCapture(SemaRef, LB, Captures); 7614 ExprResult NewUB = tryBuildCapture(SemaRef, UB, Captures); 7615 if (!NewLB.isUsable() || !NewUB.isUsable()) 7616 return nullptr; 7617 7618 ExprResult CondExpr = 7619 SemaRef.BuildBinOp(S, DefaultLoc, 7620 TestIsLessOp.getValue() ? 7621 (TestIsStrictOp ? BO_LT : BO_LE) : 7622 (TestIsStrictOp ? BO_GT : BO_GE), 7623 NewLB.get(), NewUB.get()); 7624 if (CondExpr.isUsable()) { 7625 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(), 7626 SemaRef.Context.BoolTy)) 7627 CondExpr = SemaRef.PerformImplicitConversion( 7628 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting, 7629 /*AllowExplicit=*/true); 7630 } 7631 7632 // Otherwise use original loop condition and evaluate it in runtime. 7633 return CondExpr.isUsable() ? CondExpr.get() : Cond; 7634 } 7635 7636 /// Build reference expression to the counter be used for codegen. 7637 DeclRefExpr *OpenMPIterationSpaceChecker::buildCounterVar( 7638 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 7639 DSAStackTy &DSA) const { 7640 auto *VD = dyn_cast<VarDecl>(LCDecl); 7641 if (!VD) { 7642 VD = SemaRef.isOpenMPCapturedDecl(LCDecl); 7643 DeclRefExpr *Ref = buildDeclRefExpr( 7644 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc); 7645 const DSAStackTy::DSAVarData Data = 7646 DSA.getTopDSA(LCDecl, /*FromParent=*/false); 7647 // If the loop control decl is explicitly marked as private, do not mark it 7648 // as captured again. 7649 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr) 7650 Captures.insert(std::make_pair(LCRef, Ref)); 7651 return Ref; 7652 } 7653 return cast<DeclRefExpr>(LCRef); 7654 } 7655 7656 Expr *OpenMPIterationSpaceChecker::buildPrivateCounterVar() const { 7657 if (LCDecl && !LCDecl->isInvalidDecl()) { 7658 QualType Type = LCDecl->getType().getNonReferenceType(); 7659 VarDecl *PrivateVar = buildVarDecl( 7660 SemaRef, DefaultLoc, Type, LCDecl->getName(), 7661 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr, 7662 isa<VarDecl>(LCDecl) 7663 ? buildDeclRefExpr(SemaRef, cast<VarDecl>(LCDecl), Type, DefaultLoc) 7664 : nullptr); 7665 if (PrivateVar->isInvalidDecl()) 7666 return nullptr; 7667 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc); 7668 } 7669 return nullptr; 7670 } 7671 7672 /// Build initialization of the counter to be used for codegen. 7673 Expr *OpenMPIterationSpaceChecker::buildCounterInit() const { return LB; } 7674 7675 /// Build step of the counter be used for codegen. 7676 Expr *OpenMPIterationSpaceChecker::buildCounterStep() const { return Step; } 7677 7678 Expr *OpenMPIterationSpaceChecker::buildOrderedLoopData( 7679 Scope *S, Expr *Counter, 7680 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, SourceLocation Loc, 7681 Expr *Inc, OverloadedOperatorKind OOK) { 7682 Expr *Cnt = SemaRef.DefaultLvalueConversion(Counter).get(); 7683 if (!Cnt) 7684 return nullptr; 7685 if (Inc) { 7686 assert((OOK == OO_Plus || OOK == OO_Minus) && 7687 "Expected only + or - operations for depend clauses."); 7688 BinaryOperatorKind BOK = (OOK == OO_Plus) ? BO_Add : BO_Sub; 7689 Cnt = SemaRef.BuildBinOp(S, Loc, BOK, Cnt, Inc).get(); 7690 if (!Cnt) 7691 return nullptr; 7692 } 7693 QualType VarType = LCDecl->getType().getNonReferenceType(); 7694 if (!VarType->isIntegerType() && !VarType->isPointerType() && 7695 !SemaRef.getLangOpts().CPlusPlus) 7696 return nullptr; 7697 // Upper - Lower 7698 Expr *Upper = TestIsLessOp.getValue() 7699 ? Cnt 7700 : tryBuildCapture(SemaRef, LB, Captures).get(); 7701 Expr *Lower = TestIsLessOp.getValue() 7702 ? tryBuildCapture(SemaRef, LB, Captures).get() 7703 : Cnt; 7704 if (!Upper || !Lower) 7705 return nullptr; 7706 7707 ExprResult Diff = calculateNumIters(SemaRef, S, DefaultLoc, Lower, Upper, 7708 Step, VarType, /*TestIsStrictOp=*/false, 7709 /*RoundToStep=*/false, Captures); 7710 if (!Diff.isUsable()) 7711 return nullptr; 7712 7713 return Diff.get(); 7714 } 7715 } // namespace 7716 7717 void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) { 7718 assert(getLangOpts().OpenMP && "OpenMP is not active."); 7719 assert(Init && "Expected loop in canonical form."); 7720 unsigned AssociatedLoops = DSAStack->getAssociatedLoops(); 7721 if (AssociatedLoops > 0 && 7722 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { 7723 DSAStack->loopStart(); 7724 OpenMPIterationSpaceChecker ISC(*this, *DSAStack, ForLoc); 7725 if (!ISC.checkAndSetInit(Init, /*EmitDiags=*/false)) { 7726 if (ValueDecl *D = ISC.getLoopDecl()) { 7727 auto *VD = dyn_cast<VarDecl>(D); 7728 DeclRefExpr *PrivateRef = nullptr; 7729 if (!VD) { 7730 if (VarDecl *Private = isOpenMPCapturedDecl(D)) { 7731 VD = Private; 7732 } else { 7733 PrivateRef = buildCapture(*this, D, ISC.getLoopDeclRefExpr(), 7734 /*WithInit=*/false); 7735 VD = cast<VarDecl>(PrivateRef->getDecl()); 7736 } 7737 } 7738 DSAStack->addLoopControlVariable(D, VD); 7739 const Decl *LD = DSAStack->getPossiblyLoopCunter(); 7740 if (LD != D->getCanonicalDecl()) { 7741 DSAStack->resetPossibleLoopCounter(); 7742 if (auto *Var = dyn_cast_or_null<VarDecl>(LD)) 7743 MarkDeclarationsReferencedInExpr( 7744 buildDeclRefExpr(*this, const_cast<VarDecl *>(Var), 7745 Var->getType().getNonLValueExprType(Context), 7746 ForLoc, /*RefersToCapture=*/true)); 7747 } 7748 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 7749 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables 7750 // Referenced in a Construct, C/C++]. The loop iteration variable in the 7751 // associated for-loop of a simd construct with just one associated 7752 // for-loop may be listed in a linear clause with a constant-linear-step 7753 // that is the increment of the associated for-loop. The loop iteration 7754 // variable(s) in the associated for-loop(s) of a for or parallel for 7755 // construct may be listed in a private or lastprivate clause. 7756 DSAStackTy::DSAVarData DVar = 7757 DSAStack->getTopDSA(D, /*FromParent=*/false); 7758 // If LoopVarRefExpr is nullptr it means the corresponding loop variable 7759 // is declared in the loop and it is predetermined as a private. 7760 Expr *LoopDeclRefExpr = ISC.getLoopDeclRefExpr(); 7761 OpenMPClauseKind PredeterminedCKind = 7762 isOpenMPSimdDirective(DKind) 7763 ? (DSAStack->hasMutipleLoops() ? OMPC_lastprivate : OMPC_linear) 7764 : OMPC_private; 7765 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && 7766 DVar.CKind != PredeterminedCKind && DVar.RefExpr && 7767 (LangOpts.OpenMP <= 45 || (DVar.CKind != OMPC_lastprivate && 7768 DVar.CKind != OMPC_private))) || 7769 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop || 7770 DKind == OMPD_master_taskloop || 7771 DKind == OMPD_parallel_master_taskloop || 7772 isOpenMPDistributeDirective(DKind)) && 7773 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && 7774 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) && 7775 (DVar.CKind != OMPC_private || DVar.RefExpr)) { 7776 Diag(Init->getBeginLoc(), diag::err_omp_loop_var_dsa) 7777 << getOpenMPClauseName(DVar.CKind) 7778 << getOpenMPDirectiveName(DKind) 7779 << getOpenMPClauseName(PredeterminedCKind); 7780 if (DVar.RefExpr == nullptr) 7781 DVar.CKind = PredeterminedCKind; 7782 reportOriginalDsa(*this, DSAStack, D, DVar, 7783 /*IsLoopIterVar=*/true); 7784 } else if (LoopDeclRefExpr) { 7785 // Make the loop iteration variable private (for worksharing 7786 // constructs), linear (for simd directives with the only one 7787 // associated loop) or lastprivate (for simd directives with several 7788 // collapsed or ordered loops). 7789 if (DVar.CKind == OMPC_unknown) 7790 DSAStack->addDSA(D, LoopDeclRefExpr, PredeterminedCKind, 7791 PrivateRef); 7792 } 7793 } 7794 } 7795 DSAStack->setAssociatedLoops(AssociatedLoops - 1); 7796 } 7797 } 7798 7799 /// Called on a for stmt to check and extract its iteration space 7800 /// for further processing (such as collapsing). 7801 static bool checkOpenMPIterationSpace( 7802 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA, 7803 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount, 7804 unsigned TotalNestedLoopCount, Expr *CollapseLoopCountExpr, 7805 Expr *OrderedLoopCountExpr, 7806 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA, 7807 llvm::MutableArrayRef<LoopIterationSpace> ResultIterSpaces, 7808 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 7809 // OpenMP [2.9.1, Canonical Loop Form] 7810 // for (init-expr; test-expr; incr-expr) structured-block 7811 // for (range-decl: range-expr) structured-block 7812 auto *For = dyn_cast_or_null<ForStmt>(S); 7813 auto *CXXFor = dyn_cast_or_null<CXXForRangeStmt>(S); 7814 // Ranged for is supported only in OpenMP 5.0. 7815 if (!For && (SemaRef.LangOpts.OpenMP <= 45 || !CXXFor)) { 7816 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_not_for) 7817 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr) 7818 << getOpenMPDirectiveName(DKind) << TotalNestedLoopCount 7819 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount; 7820 if (TotalNestedLoopCount > 1) { 7821 if (CollapseLoopCountExpr && OrderedLoopCountExpr) 7822 SemaRef.Diag(DSA.getConstructLoc(), 7823 diag::note_omp_collapse_ordered_expr) 7824 << 2 << CollapseLoopCountExpr->getSourceRange() 7825 << OrderedLoopCountExpr->getSourceRange(); 7826 else if (CollapseLoopCountExpr) 7827 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), 7828 diag::note_omp_collapse_ordered_expr) 7829 << 0 << CollapseLoopCountExpr->getSourceRange(); 7830 else 7831 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), 7832 diag::note_omp_collapse_ordered_expr) 7833 << 1 << OrderedLoopCountExpr->getSourceRange(); 7834 } 7835 return true; 7836 } 7837 assert(((For && For->getBody()) || (CXXFor && CXXFor->getBody())) && 7838 "No loop body."); 7839 7840 OpenMPIterationSpaceChecker ISC(SemaRef, DSA, 7841 For ? For->getForLoc() : CXXFor->getForLoc()); 7842 7843 // Check init. 7844 Stmt *Init = For ? For->getInit() : CXXFor->getBeginStmt(); 7845 if (ISC.checkAndSetInit(Init)) 7846 return true; 7847 7848 bool HasErrors = false; 7849 7850 // Check loop variable's type. 7851 if (ValueDecl *LCDecl = ISC.getLoopDecl()) { 7852 // OpenMP [2.6, Canonical Loop Form] 7853 // Var is one of the following: 7854 // A variable of signed or unsigned integer type. 7855 // For C++, a variable of a random access iterator type. 7856 // For C, a variable of a pointer type. 7857 QualType VarType = LCDecl->getType().getNonReferenceType(); 7858 if (!VarType->isDependentType() && !VarType->isIntegerType() && 7859 !VarType->isPointerType() && 7860 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) { 7861 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_variable_type) 7862 << SemaRef.getLangOpts().CPlusPlus; 7863 HasErrors = true; 7864 } 7865 7866 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in 7867 // a Construct 7868 // The loop iteration variable(s) in the associated for-loop(s) of a for or 7869 // parallel for construct is (are) private. 7870 // The loop iteration variable in the associated for-loop of a simd 7871 // construct with just one associated for-loop is linear with a 7872 // constant-linear-step that is the increment of the associated for-loop. 7873 // Exclude loop var from the list of variables with implicitly defined data 7874 // sharing attributes. 7875 VarsWithImplicitDSA.erase(LCDecl); 7876 7877 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars"); 7878 7879 // Check test-expr. 7880 HasErrors |= ISC.checkAndSetCond(For ? For->getCond() : CXXFor->getCond()); 7881 7882 // Check incr-expr. 7883 HasErrors |= ISC.checkAndSetInc(For ? For->getInc() : CXXFor->getInc()); 7884 } 7885 7886 if (ISC.dependent() || SemaRef.CurContext->isDependentContext() || HasErrors) 7887 return HasErrors; 7888 7889 // Build the loop's iteration space representation. 7890 ResultIterSpaces[CurrentNestedLoopCount].PreCond = ISC.buildPreCond( 7891 DSA.getCurScope(), For ? For->getCond() : CXXFor->getCond(), Captures); 7892 ResultIterSpaces[CurrentNestedLoopCount].NumIterations = 7893 ISC.buildNumIterations(DSA.getCurScope(), ResultIterSpaces, 7894 (isOpenMPWorksharingDirective(DKind) || 7895 isOpenMPTaskLoopDirective(DKind) || 7896 isOpenMPDistributeDirective(DKind)), 7897 Captures); 7898 ResultIterSpaces[CurrentNestedLoopCount].CounterVar = 7899 ISC.buildCounterVar(Captures, DSA); 7900 ResultIterSpaces[CurrentNestedLoopCount].PrivateCounterVar = 7901 ISC.buildPrivateCounterVar(); 7902 ResultIterSpaces[CurrentNestedLoopCount].CounterInit = ISC.buildCounterInit(); 7903 ResultIterSpaces[CurrentNestedLoopCount].CounterStep = ISC.buildCounterStep(); 7904 ResultIterSpaces[CurrentNestedLoopCount].InitSrcRange = ISC.getInitSrcRange(); 7905 ResultIterSpaces[CurrentNestedLoopCount].CondSrcRange = 7906 ISC.getConditionSrcRange(); 7907 ResultIterSpaces[CurrentNestedLoopCount].IncSrcRange = 7908 ISC.getIncrementSrcRange(); 7909 ResultIterSpaces[CurrentNestedLoopCount].Subtract = ISC.shouldSubtractStep(); 7910 ResultIterSpaces[CurrentNestedLoopCount].IsStrictCompare = 7911 ISC.isStrictTestOp(); 7912 std::tie(ResultIterSpaces[CurrentNestedLoopCount].MinValue, 7913 ResultIterSpaces[CurrentNestedLoopCount].MaxValue) = 7914 ISC.buildMinMaxValues(DSA.getCurScope(), Captures); 7915 ResultIterSpaces[CurrentNestedLoopCount].FinalCondition = 7916 ISC.buildFinalCondition(DSA.getCurScope()); 7917 ResultIterSpaces[CurrentNestedLoopCount].IsNonRectangularLB = 7918 ISC.doesInitDependOnLC(); 7919 ResultIterSpaces[CurrentNestedLoopCount].IsNonRectangularUB = 7920 ISC.doesCondDependOnLC(); 7921 ResultIterSpaces[CurrentNestedLoopCount].LoopDependentIdx = 7922 ISC.getLoopDependentIdx(); 7923 7924 HasErrors |= 7925 (ResultIterSpaces[CurrentNestedLoopCount].PreCond == nullptr || 7926 ResultIterSpaces[CurrentNestedLoopCount].NumIterations == nullptr || 7927 ResultIterSpaces[CurrentNestedLoopCount].CounterVar == nullptr || 7928 ResultIterSpaces[CurrentNestedLoopCount].PrivateCounterVar == nullptr || 7929 ResultIterSpaces[CurrentNestedLoopCount].CounterInit == nullptr || 7930 ResultIterSpaces[CurrentNestedLoopCount].CounterStep == nullptr); 7931 if (!HasErrors && DSA.isOrderedRegion()) { 7932 if (DSA.getOrderedRegionParam().second->getNumForLoops()) { 7933 if (CurrentNestedLoopCount < 7934 DSA.getOrderedRegionParam().second->getLoopNumIterations().size()) { 7935 DSA.getOrderedRegionParam().second->setLoopNumIterations( 7936 CurrentNestedLoopCount, 7937 ResultIterSpaces[CurrentNestedLoopCount].NumIterations); 7938 DSA.getOrderedRegionParam().second->setLoopCounter( 7939 CurrentNestedLoopCount, 7940 ResultIterSpaces[CurrentNestedLoopCount].CounterVar); 7941 } 7942 } 7943 for (auto &Pair : DSA.getDoacrossDependClauses()) { 7944 if (CurrentNestedLoopCount >= Pair.first->getNumLoops()) { 7945 // Erroneous case - clause has some problems. 7946 continue; 7947 } 7948 if (Pair.first->getDependencyKind() == OMPC_DEPEND_sink && 7949 Pair.second.size() <= CurrentNestedLoopCount) { 7950 // Erroneous case - clause has some problems. 7951 Pair.first->setLoopData(CurrentNestedLoopCount, nullptr); 7952 continue; 7953 } 7954 Expr *CntValue; 7955 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source) 7956 CntValue = ISC.buildOrderedLoopData( 7957 DSA.getCurScope(), 7958 ResultIterSpaces[CurrentNestedLoopCount].CounterVar, Captures, 7959 Pair.first->getDependencyLoc()); 7960 else 7961 CntValue = ISC.buildOrderedLoopData( 7962 DSA.getCurScope(), 7963 ResultIterSpaces[CurrentNestedLoopCount].CounterVar, Captures, 7964 Pair.first->getDependencyLoc(), 7965 Pair.second[CurrentNestedLoopCount].first, 7966 Pair.second[CurrentNestedLoopCount].second); 7967 Pair.first->setLoopData(CurrentNestedLoopCount, CntValue); 7968 } 7969 } 7970 7971 return HasErrors; 7972 } 7973 7974 /// Build 'VarRef = Start. 7975 static ExprResult 7976 buildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef, 7977 ExprResult Start, bool IsNonRectangularLB, 7978 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 7979 // Build 'VarRef = Start. 7980 ExprResult NewStart = IsNonRectangularLB 7981 ? Start.get() 7982 : tryBuildCapture(SemaRef, Start.get(), Captures); 7983 if (!NewStart.isUsable()) 7984 return ExprError(); 7985 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(), 7986 VarRef.get()->getType())) { 7987 NewStart = SemaRef.PerformImplicitConversion( 7988 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting, 7989 /*AllowExplicit=*/true); 7990 if (!NewStart.isUsable()) 7991 return ExprError(); 7992 } 7993 7994 ExprResult Init = 7995 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); 7996 return Init; 7997 } 7998 7999 /// Build 'VarRef = Start + Iter * Step'. 8000 static ExprResult buildCounterUpdate( 8001 Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef, 8002 ExprResult Start, ExprResult Iter, ExprResult Step, bool Subtract, 8003 bool IsNonRectangularLB, 8004 llvm::MapVector<const Expr *, DeclRefExpr *> *Captures = nullptr) { 8005 // Add parentheses (for debugging purposes only). 8006 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get()); 8007 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() || 8008 !Step.isUsable()) 8009 return ExprError(); 8010 8011 ExprResult NewStep = Step; 8012 if (Captures) 8013 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures); 8014 if (NewStep.isInvalid()) 8015 return ExprError(); 8016 ExprResult Update = 8017 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get()); 8018 if (!Update.isUsable()) 8019 return ExprError(); 8020 8021 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or 8022 // 'VarRef = Start (+|-) Iter * Step'. 8023 if (!Start.isUsable()) 8024 return ExprError(); 8025 ExprResult NewStart = SemaRef.ActOnParenExpr(Loc, Loc, Start.get()); 8026 if (!NewStart.isUsable()) 8027 return ExprError(); 8028 if (Captures && !IsNonRectangularLB) 8029 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures); 8030 if (NewStart.isInvalid()) 8031 return ExprError(); 8032 8033 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'. 8034 ExprResult SavedUpdate = Update; 8035 ExprResult UpdateVal; 8036 if (VarRef.get()->getType()->isOverloadableType() || 8037 NewStart.get()->getType()->isOverloadableType() || 8038 Update.get()->getType()->isOverloadableType()) { 8039 Sema::TentativeAnalysisScope Trap(SemaRef); 8040 8041 Update = 8042 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); 8043 if (Update.isUsable()) { 8044 UpdateVal = 8045 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign, 8046 VarRef.get(), SavedUpdate.get()); 8047 if (UpdateVal.isUsable()) { 8048 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(), 8049 UpdateVal.get()); 8050 } 8051 } 8052 } 8053 8054 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'. 8055 if (!Update.isUsable() || !UpdateVal.isUsable()) { 8056 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add, 8057 NewStart.get(), SavedUpdate.get()); 8058 if (!Update.isUsable()) 8059 return ExprError(); 8060 8061 if (!SemaRef.Context.hasSameType(Update.get()->getType(), 8062 VarRef.get()->getType())) { 8063 Update = SemaRef.PerformImplicitConversion( 8064 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true); 8065 if (!Update.isUsable()) 8066 return ExprError(); 8067 } 8068 8069 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get()); 8070 } 8071 return Update; 8072 } 8073 8074 /// Convert integer expression \a E to make it have at least \a Bits 8075 /// bits. 8076 static ExprResult widenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) { 8077 if (E == nullptr) 8078 return ExprError(); 8079 ASTContext &C = SemaRef.Context; 8080 QualType OldType = E->getType(); 8081 unsigned HasBits = C.getTypeSize(OldType); 8082 if (HasBits >= Bits) 8083 return ExprResult(E); 8084 // OK to convert to signed, because new type has more bits than old. 8085 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true); 8086 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting, 8087 true); 8088 } 8089 8090 /// Check if the given expression \a E is a constant integer that fits 8091 /// into \a Bits bits. 8092 static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) { 8093 if (E == nullptr) 8094 return false; 8095 if (Optional<llvm::APSInt> Result = 8096 E->getIntegerConstantExpr(SemaRef.Context)) 8097 return Signed ? Result->isSignedIntN(Bits) : Result->isIntN(Bits); 8098 return false; 8099 } 8100 8101 /// Build preinits statement for the given declarations. 8102 static Stmt *buildPreInits(ASTContext &Context, 8103 MutableArrayRef<Decl *> PreInits) { 8104 if (!PreInits.empty()) { 8105 return new (Context) DeclStmt( 8106 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()), 8107 SourceLocation(), SourceLocation()); 8108 } 8109 return nullptr; 8110 } 8111 8112 /// Build preinits statement for the given declarations. 8113 static Stmt * 8114 buildPreInits(ASTContext &Context, 8115 const llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 8116 if (!Captures.empty()) { 8117 SmallVector<Decl *, 16> PreInits; 8118 for (const auto &Pair : Captures) 8119 PreInits.push_back(Pair.second->getDecl()); 8120 return buildPreInits(Context, PreInits); 8121 } 8122 return nullptr; 8123 } 8124 8125 /// Build postupdate expression for the given list of postupdates expressions. 8126 static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) { 8127 Expr *PostUpdate = nullptr; 8128 if (!PostUpdates.empty()) { 8129 for (Expr *E : PostUpdates) { 8130 Expr *ConvE = S.BuildCStyleCastExpr( 8131 E->getExprLoc(), 8132 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy), 8133 E->getExprLoc(), E) 8134 .get(); 8135 PostUpdate = PostUpdate 8136 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma, 8137 PostUpdate, ConvE) 8138 .get() 8139 : ConvE; 8140 } 8141 } 8142 return PostUpdate; 8143 } 8144 8145 /// Called on a for stmt to check itself and nested loops (if any). 8146 /// \return Returns 0 if one of the collapsed stmts is not canonical for loop, 8147 /// number of collapsed loops otherwise. 8148 static unsigned 8149 checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr, 8150 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef, 8151 DSAStackTy &DSA, 8152 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA, 8153 OMPLoopDirective::HelperExprs &Built) { 8154 unsigned NestedLoopCount = 1; 8155 if (CollapseLoopCountExpr) { 8156 // Found 'collapse' clause - calculate collapse number. 8157 Expr::EvalResult Result; 8158 if (!CollapseLoopCountExpr->isValueDependent() && 8159 CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) { 8160 NestedLoopCount = Result.Val.getInt().getLimitedValue(); 8161 } else { 8162 Built.clear(/*Size=*/1); 8163 return 1; 8164 } 8165 } 8166 unsigned OrderedLoopCount = 1; 8167 if (OrderedLoopCountExpr) { 8168 // Found 'ordered' clause - calculate collapse number. 8169 Expr::EvalResult EVResult; 8170 if (!OrderedLoopCountExpr->isValueDependent() && 8171 OrderedLoopCountExpr->EvaluateAsInt(EVResult, 8172 SemaRef.getASTContext())) { 8173 llvm::APSInt Result = EVResult.Val.getInt(); 8174 if (Result.getLimitedValue() < NestedLoopCount) { 8175 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), 8176 diag::err_omp_wrong_ordered_loop_count) 8177 << OrderedLoopCountExpr->getSourceRange(); 8178 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), 8179 diag::note_collapse_loop_count) 8180 << CollapseLoopCountExpr->getSourceRange(); 8181 } 8182 OrderedLoopCount = Result.getLimitedValue(); 8183 } else { 8184 Built.clear(/*Size=*/1); 8185 return 1; 8186 } 8187 } 8188 // This is helper routine for loop directives (e.g., 'for', 'simd', 8189 // 'for simd', etc.). 8190 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 8191 SmallVector<LoopIterationSpace, 4> IterSpaces( 8192 std::max(OrderedLoopCount, NestedLoopCount)); 8193 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true); 8194 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) { 8195 if (checkOpenMPIterationSpace( 8196 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount, 8197 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr, 8198 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces, Captures)) 8199 return 0; 8200 // Move on to the next nested for loop, or to the loop body. 8201 // OpenMP [2.8.1, simd construct, Restrictions] 8202 // All loops associated with the construct must be perfectly nested; that 8203 // is, there must be no intervening code nor any OpenMP directive between 8204 // any two loops. 8205 if (auto *For = dyn_cast<ForStmt>(CurStmt)) { 8206 CurStmt = For->getBody(); 8207 } else { 8208 assert(isa<CXXForRangeStmt>(CurStmt) && 8209 "Expected canonical for or range-based for loops."); 8210 CurStmt = cast<CXXForRangeStmt>(CurStmt)->getBody(); 8211 } 8212 CurStmt = OMPLoopDirective::tryToFindNextInnerLoop( 8213 CurStmt, SemaRef.LangOpts.OpenMP >= 50); 8214 } 8215 for (unsigned Cnt = NestedLoopCount; Cnt < OrderedLoopCount; ++Cnt) { 8216 if (checkOpenMPIterationSpace( 8217 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount, 8218 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr, 8219 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces, Captures)) 8220 return 0; 8221 if (Cnt > 0 && IterSpaces[Cnt].CounterVar) { 8222 // Handle initialization of captured loop iterator variables. 8223 auto *DRE = cast<DeclRefExpr>(IterSpaces[Cnt].CounterVar); 8224 if (isa<OMPCapturedExprDecl>(DRE->getDecl())) { 8225 Captures[DRE] = DRE; 8226 } 8227 } 8228 // Move on to the next nested for loop, or to the loop body. 8229 // OpenMP [2.8.1, simd construct, Restrictions] 8230 // All loops associated with the construct must be perfectly nested; that 8231 // is, there must be no intervening code nor any OpenMP directive between 8232 // any two loops. 8233 if (auto *For = dyn_cast<ForStmt>(CurStmt)) { 8234 CurStmt = For->getBody(); 8235 } else { 8236 assert(isa<CXXForRangeStmt>(CurStmt) && 8237 "Expected canonical for or range-based for loops."); 8238 CurStmt = cast<CXXForRangeStmt>(CurStmt)->getBody(); 8239 } 8240 CurStmt = OMPLoopDirective::tryToFindNextInnerLoop( 8241 CurStmt, SemaRef.LangOpts.OpenMP >= 50); 8242 } 8243 8244 Built.clear(/* size */ NestedLoopCount); 8245 8246 if (SemaRef.CurContext->isDependentContext()) 8247 return NestedLoopCount; 8248 8249 // An example of what is generated for the following code: 8250 // 8251 // #pragma omp simd collapse(2) ordered(2) 8252 // for (i = 0; i < NI; ++i) 8253 // for (k = 0; k < NK; ++k) 8254 // for (j = J0; j < NJ; j+=2) { 8255 // <loop body> 8256 // } 8257 // 8258 // We generate the code below. 8259 // Note: the loop body may be outlined in CodeGen. 8260 // Note: some counters may be C++ classes, operator- is used to find number of 8261 // iterations and operator+= to calculate counter value. 8262 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32 8263 // or i64 is currently supported). 8264 // 8265 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2)) 8266 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) { 8267 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2); 8268 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2; 8269 // // similar updates for vars in clauses (e.g. 'linear') 8270 // <loop body (using local i and j)> 8271 // } 8272 // i = NI; // assign final values of counters 8273 // j = NJ; 8274 // 8275 8276 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are 8277 // the iteration counts of the collapsed for loops. 8278 // Precondition tests if there is at least one iteration (all conditions are 8279 // true). 8280 auto PreCond = ExprResult(IterSpaces[0].PreCond); 8281 Expr *N0 = IterSpaces[0].NumIterations; 8282 ExprResult LastIteration32 = 8283 widenIterationCount(/*Bits=*/32, 8284 SemaRef 8285 .PerformImplicitConversion( 8286 N0->IgnoreImpCasts(), N0->getType(), 8287 Sema::AA_Converting, /*AllowExplicit=*/true) 8288 .get(), 8289 SemaRef); 8290 ExprResult LastIteration64 = widenIterationCount( 8291 /*Bits=*/64, 8292 SemaRef 8293 .PerformImplicitConversion(N0->IgnoreImpCasts(), N0->getType(), 8294 Sema::AA_Converting, 8295 /*AllowExplicit=*/true) 8296 .get(), 8297 SemaRef); 8298 8299 if (!LastIteration32.isUsable() || !LastIteration64.isUsable()) 8300 return NestedLoopCount; 8301 8302 ASTContext &C = SemaRef.Context; 8303 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32; 8304 8305 Scope *CurScope = DSA.getCurScope(); 8306 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) { 8307 if (PreCond.isUsable()) { 8308 PreCond = 8309 SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd, 8310 PreCond.get(), IterSpaces[Cnt].PreCond); 8311 } 8312 Expr *N = IterSpaces[Cnt].NumIterations; 8313 SourceLocation Loc = N->getExprLoc(); 8314 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32; 8315 if (LastIteration32.isUsable()) 8316 LastIteration32 = SemaRef.BuildBinOp( 8317 CurScope, Loc, BO_Mul, LastIteration32.get(), 8318 SemaRef 8319 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), 8320 Sema::AA_Converting, 8321 /*AllowExplicit=*/true) 8322 .get()); 8323 if (LastIteration64.isUsable()) 8324 LastIteration64 = SemaRef.BuildBinOp( 8325 CurScope, Loc, BO_Mul, LastIteration64.get(), 8326 SemaRef 8327 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), 8328 Sema::AA_Converting, 8329 /*AllowExplicit=*/true) 8330 .get()); 8331 } 8332 8333 // Choose either the 32-bit or 64-bit version. 8334 ExprResult LastIteration = LastIteration64; 8335 if (SemaRef.getLangOpts().OpenMPOptimisticCollapse || 8336 (LastIteration32.isUsable() && 8337 C.getTypeSize(LastIteration32.get()->getType()) == 32 && 8338 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 || 8339 fitsInto( 8340 /*Bits=*/32, 8341 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(), 8342 LastIteration64.get(), SemaRef)))) 8343 LastIteration = LastIteration32; 8344 QualType VType = LastIteration.get()->getType(); 8345 QualType RealVType = VType; 8346 QualType StrideVType = VType; 8347 if (isOpenMPTaskLoopDirective(DKind)) { 8348 VType = 8349 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0); 8350 StrideVType = 8351 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1); 8352 } 8353 8354 if (!LastIteration.isUsable()) 8355 return 0; 8356 8357 // Save the number of iterations. 8358 ExprResult NumIterations = LastIteration; 8359 { 8360 LastIteration = SemaRef.BuildBinOp( 8361 CurScope, LastIteration.get()->getExprLoc(), BO_Sub, 8362 LastIteration.get(), 8363 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 8364 if (!LastIteration.isUsable()) 8365 return 0; 8366 } 8367 8368 // Calculate the last iteration number beforehand instead of doing this on 8369 // each iteration. Do not do this if the number of iterations may be kfold-ed. 8370 bool IsConstant = LastIteration.get()->isIntegerConstantExpr(SemaRef.Context); 8371 ExprResult CalcLastIteration; 8372 if (!IsConstant) { 8373 ExprResult SaveRef = 8374 tryBuildCapture(SemaRef, LastIteration.get(), Captures); 8375 LastIteration = SaveRef; 8376 8377 // Prepare SaveRef + 1. 8378 NumIterations = SemaRef.BuildBinOp( 8379 CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(), 8380 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 8381 if (!NumIterations.isUsable()) 8382 return 0; 8383 } 8384 8385 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin(); 8386 8387 // Build variables passed into runtime, necessary for worksharing directives. 8388 ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB; 8389 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || 8390 isOpenMPDistributeDirective(DKind)) { 8391 // Lower bound variable, initialized with zero. 8392 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb"); 8393 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc); 8394 SemaRef.AddInitializerToDecl(LBDecl, 8395 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 8396 /*DirectInit*/ false); 8397 8398 // Upper bound variable, initialized with last iteration number. 8399 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub"); 8400 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc); 8401 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(), 8402 /*DirectInit*/ false); 8403 8404 // A 32-bit variable-flag where runtime returns 1 for the last iteration. 8405 // This will be used to implement clause 'lastprivate'. 8406 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true); 8407 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last"); 8408 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc); 8409 SemaRef.AddInitializerToDecl(ILDecl, 8410 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 8411 /*DirectInit*/ false); 8412 8413 // Stride variable returned by runtime (we initialize it to 1 by default). 8414 VarDecl *STDecl = 8415 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride"); 8416 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc); 8417 SemaRef.AddInitializerToDecl(STDecl, 8418 SemaRef.ActOnIntegerConstant(InitLoc, 1).get(), 8419 /*DirectInit*/ false); 8420 8421 // Build expression: UB = min(UB, LastIteration) 8422 // It is necessary for CodeGen of directives with static scheduling. 8423 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT, 8424 UB.get(), LastIteration.get()); 8425 ExprResult CondOp = SemaRef.ActOnConditionalOp( 8426 LastIteration.get()->getExprLoc(), InitLoc, IsUBGreater.get(), 8427 LastIteration.get(), UB.get()); 8428 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(), 8429 CondOp.get()); 8430 EUB = SemaRef.ActOnFinishFullExpr(EUB.get(), /*DiscardedValue*/ false); 8431 8432 // If we have a combined directive that combines 'distribute', 'for' or 8433 // 'simd' we need to be able to access the bounds of the schedule of the 8434 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained 8435 // by scheduling 'distribute' have to be passed to the schedule of 'for'. 8436 if (isOpenMPLoopBoundSharingDirective(DKind)) { 8437 // Lower bound variable, initialized with zero. 8438 VarDecl *CombLBDecl = 8439 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb"); 8440 CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc); 8441 SemaRef.AddInitializerToDecl( 8442 CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 8443 /*DirectInit*/ false); 8444 8445 // Upper bound variable, initialized with last iteration number. 8446 VarDecl *CombUBDecl = 8447 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub"); 8448 CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc); 8449 SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(), 8450 /*DirectInit*/ false); 8451 8452 ExprResult CombIsUBGreater = SemaRef.BuildBinOp( 8453 CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get()); 8454 ExprResult CombCondOp = 8455 SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(), 8456 LastIteration.get(), CombUB.get()); 8457 CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(), 8458 CombCondOp.get()); 8459 CombEUB = 8460 SemaRef.ActOnFinishFullExpr(CombEUB.get(), /*DiscardedValue*/ false); 8461 8462 const CapturedDecl *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl(); 8463 // We expect to have at least 2 more parameters than the 'parallel' 8464 // directive does - the lower and upper bounds of the previous schedule. 8465 assert(CD->getNumParams() >= 4 && 8466 "Unexpected number of parameters in loop combined directive"); 8467 8468 // Set the proper type for the bounds given what we learned from the 8469 // enclosed loops. 8470 ImplicitParamDecl *PrevLBDecl = CD->getParam(/*PrevLB=*/2); 8471 ImplicitParamDecl *PrevUBDecl = CD->getParam(/*PrevUB=*/3); 8472 8473 // Previous lower and upper bounds are obtained from the region 8474 // parameters. 8475 PrevLB = 8476 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc); 8477 PrevUB = 8478 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc); 8479 } 8480 } 8481 8482 // Build the iteration variable and its initialization before loop. 8483 ExprResult IV; 8484 ExprResult Init, CombInit; 8485 { 8486 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv"); 8487 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc); 8488 Expr *RHS = 8489 (isOpenMPWorksharingDirective(DKind) || 8490 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)) 8491 ? LB.get() 8492 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get(); 8493 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS); 8494 Init = SemaRef.ActOnFinishFullExpr(Init.get(), /*DiscardedValue*/ false); 8495 8496 if (isOpenMPLoopBoundSharingDirective(DKind)) { 8497 Expr *CombRHS = 8498 (isOpenMPWorksharingDirective(DKind) || 8499 isOpenMPTaskLoopDirective(DKind) || 8500 isOpenMPDistributeDirective(DKind)) 8501 ? CombLB.get() 8502 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get(); 8503 CombInit = 8504 SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS); 8505 CombInit = 8506 SemaRef.ActOnFinishFullExpr(CombInit.get(), /*DiscardedValue*/ false); 8507 } 8508 } 8509 8510 bool UseStrictCompare = 8511 RealVType->hasUnsignedIntegerRepresentation() && 8512 llvm::all_of(IterSpaces, [](const LoopIterationSpace &LIS) { 8513 return LIS.IsStrictCompare; 8514 }); 8515 // Loop condition (IV < NumIterations) or (IV <= UB or IV < UB + 1 (for 8516 // unsigned IV)) for worksharing loops. 8517 SourceLocation CondLoc = AStmt->getBeginLoc(); 8518 Expr *BoundUB = UB.get(); 8519 if (UseStrictCompare) { 8520 BoundUB = 8521 SemaRef 8522 .BuildBinOp(CurScope, CondLoc, BO_Add, BoundUB, 8523 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()) 8524 .get(); 8525 BoundUB = 8526 SemaRef.ActOnFinishFullExpr(BoundUB, /*DiscardedValue*/ false).get(); 8527 } 8528 ExprResult Cond = 8529 (isOpenMPWorksharingDirective(DKind) || 8530 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)) 8531 ? SemaRef.BuildBinOp(CurScope, CondLoc, 8532 UseStrictCompare ? BO_LT : BO_LE, IV.get(), 8533 BoundUB) 8534 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(), 8535 NumIterations.get()); 8536 ExprResult CombDistCond; 8537 if (isOpenMPLoopBoundSharingDirective(DKind)) { 8538 CombDistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(), 8539 NumIterations.get()); 8540 } 8541 8542 ExprResult CombCond; 8543 if (isOpenMPLoopBoundSharingDirective(DKind)) { 8544 Expr *BoundCombUB = CombUB.get(); 8545 if (UseStrictCompare) { 8546 BoundCombUB = 8547 SemaRef 8548 .BuildBinOp( 8549 CurScope, CondLoc, BO_Add, BoundCombUB, 8550 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()) 8551 .get(); 8552 BoundCombUB = 8553 SemaRef.ActOnFinishFullExpr(BoundCombUB, /*DiscardedValue*/ false) 8554 .get(); 8555 } 8556 CombCond = 8557 SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, 8558 IV.get(), BoundCombUB); 8559 } 8560 // Loop increment (IV = IV + 1) 8561 SourceLocation IncLoc = AStmt->getBeginLoc(); 8562 ExprResult Inc = 8563 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(), 8564 SemaRef.ActOnIntegerConstant(IncLoc, 1).get()); 8565 if (!Inc.isUsable()) 8566 return 0; 8567 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get()); 8568 Inc = SemaRef.ActOnFinishFullExpr(Inc.get(), /*DiscardedValue*/ false); 8569 if (!Inc.isUsable()) 8570 return 0; 8571 8572 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST). 8573 // Used for directives with static scheduling. 8574 // In combined construct, add combined version that use CombLB and CombUB 8575 // base variables for the update 8576 ExprResult NextLB, NextUB, CombNextLB, CombNextUB; 8577 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || 8578 isOpenMPDistributeDirective(DKind)) { 8579 // LB + ST 8580 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get()); 8581 if (!NextLB.isUsable()) 8582 return 0; 8583 // LB = LB + ST 8584 NextLB = 8585 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get()); 8586 NextLB = 8587 SemaRef.ActOnFinishFullExpr(NextLB.get(), /*DiscardedValue*/ false); 8588 if (!NextLB.isUsable()) 8589 return 0; 8590 // UB + ST 8591 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get()); 8592 if (!NextUB.isUsable()) 8593 return 0; 8594 // UB = UB + ST 8595 NextUB = 8596 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get()); 8597 NextUB = 8598 SemaRef.ActOnFinishFullExpr(NextUB.get(), /*DiscardedValue*/ false); 8599 if (!NextUB.isUsable()) 8600 return 0; 8601 if (isOpenMPLoopBoundSharingDirective(DKind)) { 8602 CombNextLB = 8603 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get()); 8604 if (!NextLB.isUsable()) 8605 return 0; 8606 // LB = LB + ST 8607 CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(), 8608 CombNextLB.get()); 8609 CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get(), 8610 /*DiscardedValue*/ false); 8611 if (!CombNextLB.isUsable()) 8612 return 0; 8613 // UB + ST 8614 CombNextUB = 8615 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get()); 8616 if (!CombNextUB.isUsable()) 8617 return 0; 8618 // UB = UB + ST 8619 CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(), 8620 CombNextUB.get()); 8621 CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get(), 8622 /*DiscardedValue*/ false); 8623 if (!CombNextUB.isUsable()) 8624 return 0; 8625 } 8626 } 8627 8628 // Create increment expression for distribute loop when combined in a same 8629 // directive with for as IV = IV + ST; ensure upper bound expression based 8630 // on PrevUB instead of NumIterations - used to implement 'for' when found 8631 // in combination with 'distribute', like in 'distribute parallel for' 8632 SourceLocation DistIncLoc = AStmt->getBeginLoc(); 8633 ExprResult DistCond, DistInc, PrevEUB, ParForInDistCond; 8634 if (isOpenMPLoopBoundSharingDirective(DKind)) { 8635 DistCond = SemaRef.BuildBinOp( 8636 CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, IV.get(), BoundUB); 8637 assert(DistCond.isUsable() && "distribute cond expr was not built"); 8638 8639 DistInc = 8640 SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get()); 8641 assert(DistInc.isUsable() && "distribute inc expr was not built"); 8642 DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(), 8643 DistInc.get()); 8644 DistInc = 8645 SemaRef.ActOnFinishFullExpr(DistInc.get(), /*DiscardedValue*/ false); 8646 assert(DistInc.isUsable() && "distribute inc expr was not built"); 8647 8648 // Build expression: UB = min(UB, prevUB) for #for in composite or combined 8649 // construct 8650 SourceLocation DistEUBLoc = AStmt->getBeginLoc(); 8651 ExprResult IsUBGreater = 8652 SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, UB.get(), PrevUB.get()); 8653 ExprResult CondOp = SemaRef.ActOnConditionalOp( 8654 DistEUBLoc, DistEUBLoc, IsUBGreater.get(), PrevUB.get(), UB.get()); 8655 PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(), 8656 CondOp.get()); 8657 PrevEUB = 8658 SemaRef.ActOnFinishFullExpr(PrevEUB.get(), /*DiscardedValue*/ false); 8659 8660 // Build IV <= PrevUB or IV < PrevUB + 1 for unsigned IV to be used in 8661 // parallel for is in combination with a distribute directive with 8662 // schedule(static, 1) 8663 Expr *BoundPrevUB = PrevUB.get(); 8664 if (UseStrictCompare) { 8665 BoundPrevUB = 8666 SemaRef 8667 .BuildBinOp( 8668 CurScope, CondLoc, BO_Add, BoundPrevUB, 8669 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()) 8670 .get(); 8671 BoundPrevUB = 8672 SemaRef.ActOnFinishFullExpr(BoundPrevUB, /*DiscardedValue*/ false) 8673 .get(); 8674 } 8675 ParForInDistCond = 8676 SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, 8677 IV.get(), BoundPrevUB); 8678 } 8679 8680 // Build updates and final values of the loop counters. 8681 bool HasErrors = false; 8682 Built.Counters.resize(NestedLoopCount); 8683 Built.Inits.resize(NestedLoopCount); 8684 Built.Updates.resize(NestedLoopCount); 8685 Built.Finals.resize(NestedLoopCount); 8686 Built.DependentCounters.resize(NestedLoopCount); 8687 Built.DependentInits.resize(NestedLoopCount); 8688 Built.FinalsConditions.resize(NestedLoopCount); 8689 { 8690 // We implement the following algorithm for obtaining the 8691 // original loop iteration variable values based on the 8692 // value of the collapsed loop iteration variable IV. 8693 // 8694 // Let n+1 be the number of collapsed loops in the nest. 8695 // Iteration variables (I0, I1, .... In) 8696 // Iteration counts (N0, N1, ... Nn) 8697 // 8698 // Acc = IV; 8699 // 8700 // To compute Ik for loop k, 0 <= k <= n, generate: 8701 // Prod = N(k+1) * N(k+2) * ... * Nn; 8702 // Ik = Acc / Prod; 8703 // Acc -= Ik * Prod; 8704 // 8705 ExprResult Acc = IV; 8706 for (unsigned int Cnt = 0; Cnt < NestedLoopCount; ++Cnt) { 8707 LoopIterationSpace &IS = IterSpaces[Cnt]; 8708 SourceLocation UpdLoc = IS.IncSrcRange.getBegin(); 8709 ExprResult Iter; 8710 8711 // Compute prod 8712 ExprResult Prod = 8713 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(); 8714 for (unsigned int K = Cnt+1; K < NestedLoopCount; ++K) 8715 Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Prod.get(), 8716 IterSpaces[K].NumIterations); 8717 8718 // Iter = Acc / Prod 8719 // If there is at least one more inner loop to avoid 8720 // multiplication by 1. 8721 if (Cnt + 1 < NestedLoopCount) 8722 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, 8723 Acc.get(), Prod.get()); 8724 else 8725 Iter = Acc; 8726 if (!Iter.isUsable()) { 8727 HasErrors = true; 8728 break; 8729 } 8730 8731 // Update Acc: 8732 // Acc -= Iter * Prod 8733 // Check if there is at least one more inner loop to avoid 8734 // multiplication by 1. 8735 if (Cnt + 1 < NestedLoopCount) 8736 Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, 8737 Iter.get(), Prod.get()); 8738 else 8739 Prod = Iter; 8740 Acc = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Sub, 8741 Acc.get(), Prod.get()); 8742 8743 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step 8744 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()); 8745 DeclRefExpr *CounterVar = buildDeclRefExpr( 8746 SemaRef, VD, IS.CounterVar->getType(), IS.CounterVar->getExprLoc(), 8747 /*RefersToCapture=*/true); 8748 ExprResult Init = 8749 buildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar, 8750 IS.CounterInit, IS.IsNonRectangularLB, Captures); 8751 if (!Init.isUsable()) { 8752 HasErrors = true; 8753 break; 8754 } 8755 ExprResult Update = buildCounterUpdate( 8756 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter, 8757 IS.CounterStep, IS.Subtract, IS.IsNonRectangularLB, &Captures); 8758 if (!Update.isUsable()) { 8759 HasErrors = true; 8760 break; 8761 } 8762 8763 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step 8764 ExprResult Final = 8765 buildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar, 8766 IS.CounterInit, IS.NumIterations, IS.CounterStep, 8767 IS.Subtract, IS.IsNonRectangularLB, &Captures); 8768 if (!Final.isUsable()) { 8769 HasErrors = true; 8770 break; 8771 } 8772 8773 if (!Update.isUsable() || !Final.isUsable()) { 8774 HasErrors = true; 8775 break; 8776 } 8777 // Save results 8778 Built.Counters[Cnt] = IS.CounterVar; 8779 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar; 8780 Built.Inits[Cnt] = Init.get(); 8781 Built.Updates[Cnt] = Update.get(); 8782 Built.Finals[Cnt] = Final.get(); 8783 Built.DependentCounters[Cnt] = nullptr; 8784 Built.DependentInits[Cnt] = nullptr; 8785 Built.FinalsConditions[Cnt] = nullptr; 8786 if (IS.IsNonRectangularLB || IS.IsNonRectangularUB) { 8787 Built.DependentCounters[Cnt] = 8788 Built.Counters[NestedLoopCount - 1 - IS.LoopDependentIdx]; 8789 Built.DependentInits[Cnt] = 8790 Built.Inits[NestedLoopCount - 1 - IS.LoopDependentIdx]; 8791 Built.FinalsConditions[Cnt] = IS.FinalCondition; 8792 } 8793 } 8794 } 8795 8796 if (HasErrors) 8797 return 0; 8798 8799 // Save results 8800 Built.IterationVarRef = IV.get(); 8801 Built.LastIteration = LastIteration.get(); 8802 Built.NumIterations = NumIterations.get(); 8803 Built.CalcLastIteration = SemaRef 8804 .ActOnFinishFullExpr(CalcLastIteration.get(), 8805 /*DiscardedValue=*/false) 8806 .get(); 8807 Built.PreCond = PreCond.get(); 8808 Built.PreInits = buildPreInits(C, Captures); 8809 Built.Cond = Cond.get(); 8810 Built.Init = Init.get(); 8811 Built.Inc = Inc.get(); 8812 Built.LB = LB.get(); 8813 Built.UB = UB.get(); 8814 Built.IL = IL.get(); 8815 Built.ST = ST.get(); 8816 Built.EUB = EUB.get(); 8817 Built.NLB = NextLB.get(); 8818 Built.NUB = NextUB.get(); 8819 Built.PrevLB = PrevLB.get(); 8820 Built.PrevUB = PrevUB.get(); 8821 Built.DistInc = DistInc.get(); 8822 Built.PrevEUB = PrevEUB.get(); 8823 Built.DistCombinedFields.LB = CombLB.get(); 8824 Built.DistCombinedFields.UB = CombUB.get(); 8825 Built.DistCombinedFields.EUB = CombEUB.get(); 8826 Built.DistCombinedFields.Init = CombInit.get(); 8827 Built.DistCombinedFields.Cond = CombCond.get(); 8828 Built.DistCombinedFields.NLB = CombNextLB.get(); 8829 Built.DistCombinedFields.NUB = CombNextUB.get(); 8830 Built.DistCombinedFields.DistCond = CombDistCond.get(); 8831 Built.DistCombinedFields.ParForInDistCond = ParForInDistCond.get(); 8832 8833 return NestedLoopCount; 8834 } 8835 8836 static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) { 8837 auto CollapseClauses = 8838 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses); 8839 if (CollapseClauses.begin() != CollapseClauses.end()) 8840 return (*CollapseClauses.begin())->getNumForLoops(); 8841 return nullptr; 8842 } 8843 8844 static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) { 8845 auto OrderedClauses = 8846 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses); 8847 if (OrderedClauses.begin() != OrderedClauses.end()) 8848 return (*OrderedClauses.begin())->getNumForLoops(); 8849 return nullptr; 8850 } 8851 8852 static bool checkSimdlenSafelenSpecified(Sema &S, 8853 const ArrayRef<OMPClause *> Clauses) { 8854 const OMPSafelenClause *Safelen = nullptr; 8855 const OMPSimdlenClause *Simdlen = nullptr; 8856 8857 for (const OMPClause *Clause : Clauses) { 8858 if (Clause->getClauseKind() == OMPC_safelen) 8859 Safelen = cast<OMPSafelenClause>(Clause); 8860 else if (Clause->getClauseKind() == OMPC_simdlen) 8861 Simdlen = cast<OMPSimdlenClause>(Clause); 8862 if (Safelen && Simdlen) 8863 break; 8864 } 8865 8866 if (Simdlen && Safelen) { 8867 const Expr *SimdlenLength = Simdlen->getSimdlen(); 8868 const Expr *SafelenLength = Safelen->getSafelen(); 8869 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() || 8870 SimdlenLength->isInstantiationDependent() || 8871 SimdlenLength->containsUnexpandedParameterPack()) 8872 return false; 8873 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() || 8874 SafelenLength->isInstantiationDependent() || 8875 SafelenLength->containsUnexpandedParameterPack()) 8876 return false; 8877 Expr::EvalResult SimdlenResult, SafelenResult; 8878 SimdlenLength->EvaluateAsInt(SimdlenResult, S.Context); 8879 SafelenLength->EvaluateAsInt(SafelenResult, S.Context); 8880 llvm::APSInt SimdlenRes = SimdlenResult.Val.getInt(); 8881 llvm::APSInt SafelenRes = SafelenResult.Val.getInt(); 8882 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions] 8883 // If both simdlen and safelen clauses are specified, the value of the 8884 // simdlen parameter must be less than or equal to the value of the safelen 8885 // parameter. 8886 if (SimdlenRes > SafelenRes) { 8887 S.Diag(SimdlenLength->getExprLoc(), 8888 diag::err_omp_wrong_simdlen_safelen_values) 8889 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange(); 8890 return true; 8891 } 8892 } 8893 return false; 8894 } 8895 8896 StmtResult 8897 Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt, 8898 SourceLocation StartLoc, SourceLocation EndLoc, 8899 VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8900 if (!AStmt) 8901 return StmtError(); 8902 8903 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 8904 OMPLoopDirective::HelperExprs B; 8905 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 8906 // define the nested loops number. 8907 unsigned NestedLoopCount = checkOpenMPLoop( 8908 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), 8909 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 8910 if (NestedLoopCount == 0) 8911 return StmtError(); 8912 8913 assert((CurContext->isDependentContext() || B.builtAll()) && 8914 "omp simd loop exprs were not built"); 8915 8916 if (!CurContext->isDependentContext()) { 8917 // Finalize the clauses that need pre-built expressions for CodeGen. 8918 for (OMPClause *C : Clauses) { 8919 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 8920 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 8921 B.NumIterations, *this, CurScope, 8922 DSAStack)) 8923 return StmtError(); 8924 } 8925 } 8926 8927 if (checkSimdlenSafelenSpecified(*this, Clauses)) 8928 return StmtError(); 8929 8930 setFunctionHasBranchProtectedScope(); 8931 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 8932 Clauses, AStmt, B); 8933 } 8934 8935 StmtResult 8936 Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt, 8937 SourceLocation StartLoc, SourceLocation EndLoc, 8938 VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8939 if (!AStmt) 8940 return StmtError(); 8941 8942 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 8943 OMPLoopDirective::HelperExprs B; 8944 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 8945 // define the nested loops number. 8946 unsigned NestedLoopCount = checkOpenMPLoop( 8947 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), 8948 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 8949 if (NestedLoopCount == 0) 8950 return StmtError(); 8951 8952 assert((CurContext->isDependentContext() || B.builtAll()) && 8953 "omp for loop exprs were not built"); 8954 8955 if (!CurContext->isDependentContext()) { 8956 // Finalize the clauses that need pre-built expressions for CodeGen. 8957 for (OMPClause *C : Clauses) { 8958 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 8959 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 8960 B.NumIterations, *this, CurScope, 8961 DSAStack)) 8962 return StmtError(); 8963 } 8964 } 8965 8966 setFunctionHasBranchProtectedScope(); 8967 return OMPForDirective::Create( 8968 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 8969 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 8970 } 8971 8972 StmtResult Sema::ActOnOpenMPForSimdDirective( 8973 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8974 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8975 if (!AStmt) 8976 return StmtError(); 8977 8978 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 8979 OMPLoopDirective::HelperExprs B; 8980 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 8981 // define the nested loops number. 8982 unsigned NestedLoopCount = 8983 checkOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses), 8984 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 8985 VarsWithImplicitDSA, B); 8986 if (NestedLoopCount == 0) 8987 return StmtError(); 8988 8989 assert((CurContext->isDependentContext() || B.builtAll()) && 8990 "omp for simd loop exprs were not built"); 8991 8992 if (!CurContext->isDependentContext()) { 8993 // Finalize the clauses that need pre-built expressions for CodeGen. 8994 for (OMPClause *C : Clauses) { 8995 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 8996 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 8997 B.NumIterations, *this, CurScope, 8998 DSAStack)) 8999 return StmtError(); 9000 } 9001 } 9002 9003 if (checkSimdlenSafelenSpecified(*this, Clauses)) 9004 return StmtError(); 9005 9006 setFunctionHasBranchProtectedScope(); 9007 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 9008 Clauses, AStmt, B); 9009 } 9010 9011 StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses, 9012 Stmt *AStmt, 9013 SourceLocation StartLoc, 9014 SourceLocation EndLoc) { 9015 if (!AStmt) 9016 return StmtError(); 9017 9018 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9019 auto BaseStmt = AStmt; 9020 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 9021 BaseStmt = CS->getCapturedStmt(); 9022 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 9023 auto S = C->children(); 9024 if (S.begin() == S.end()) 9025 return StmtError(); 9026 // All associated statements must be '#pragma omp section' except for 9027 // the first one. 9028 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) { 9029 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 9030 if (SectionStmt) 9031 Diag(SectionStmt->getBeginLoc(), 9032 diag::err_omp_sections_substmt_not_section); 9033 return StmtError(); 9034 } 9035 cast<OMPSectionDirective>(SectionStmt) 9036 ->setHasCancel(DSAStack->isCancelRegion()); 9037 } 9038 } else { 9039 Diag(AStmt->getBeginLoc(), diag::err_omp_sections_not_compound_stmt); 9040 return StmtError(); 9041 } 9042 9043 setFunctionHasBranchProtectedScope(); 9044 9045 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 9046 DSAStack->getTaskgroupReductionRef(), 9047 DSAStack->isCancelRegion()); 9048 } 9049 9050 StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt, 9051 SourceLocation StartLoc, 9052 SourceLocation EndLoc) { 9053 if (!AStmt) 9054 return StmtError(); 9055 9056 setFunctionHasBranchProtectedScope(); 9057 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion()); 9058 9059 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt, 9060 DSAStack->isCancelRegion()); 9061 } 9062 9063 StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses, 9064 Stmt *AStmt, 9065 SourceLocation StartLoc, 9066 SourceLocation EndLoc) { 9067 if (!AStmt) 9068 return StmtError(); 9069 9070 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9071 9072 setFunctionHasBranchProtectedScope(); 9073 9074 // OpenMP [2.7.3, single Construct, Restrictions] 9075 // The copyprivate clause must not be used with the nowait clause. 9076 const OMPClause *Nowait = nullptr; 9077 const OMPClause *Copyprivate = nullptr; 9078 for (const OMPClause *Clause : Clauses) { 9079 if (Clause->getClauseKind() == OMPC_nowait) 9080 Nowait = Clause; 9081 else if (Clause->getClauseKind() == OMPC_copyprivate) 9082 Copyprivate = Clause; 9083 if (Copyprivate && Nowait) { 9084 Diag(Copyprivate->getBeginLoc(), 9085 diag::err_omp_single_copyprivate_with_nowait); 9086 Diag(Nowait->getBeginLoc(), diag::note_omp_nowait_clause_here); 9087 return StmtError(); 9088 } 9089 } 9090 9091 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 9092 } 9093 9094 StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt, 9095 SourceLocation StartLoc, 9096 SourceLocation EndLoc) { 9097 if (!AStmt) 9098 return StmtError(); 9099 9100 setFunctionHasBranchProtectedScope(); 9101 9102 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt); 9103 } 9104 9105 StmtResult Sema::ActOnOpenMPCriticalDirective( 9106 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses, 9107 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { 9108 if (!AStmt) 9109 return StmtError(); 9110 9111 bool ErrorFound = false; 9112 llvm::APSInt Hint; 9113 SourceLocation HintLoc; 9114 bool DependentHint = false; 9115 for (const OMPClause *C : Clauses) { 9116 if (C->getClauseKind() == OMPC_hint) { 9117 if (!DirName.getName()) { 9118 Diag(C->getBeginLoc(), diag::err_omp_hint_clause_no_name); 9119 ErrorFound = true; 9120 } 9121 Expr *E = cast<OMPHintClause>(C)->getHint(); 9122 if (E->isTypeDependent() || E->isValueDependent() || 9123 E->isInstantiationDependent()) { 9124 DependentHint = true; 9125 } else { 9126 Hint = E->EvaluateKnownConstInt(Context); 9127 HintLoc = C->getBeginLoc(); 9128 } 9129 } 9130 } 9131 if (ErrorFound) 9132 return StmtError(); 9133 const auto Pair = DSAStack->getCriticalWithHint(DirName); 9134 if (Pair.first && DirName.getName() && !DependentHint) { 9135 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) { 9136 Diag(StartLoc, diag::err_omp_critical_with_hint); 9137 if (HintLoc.isValid()) 9138 Diag(HintLoc, diag::note_omp_critical_hint_here) 9139 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false); 9140 else 9141 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0; 9142 if (const auto *C = Pair.first->getSingleClause<OMPHintClause>()) { 9143 Diag(C->getBeginLoc(), diag::note_omp_critical_hint_here) 9144 << 1 9145 << C->getHint()->EvaluateKnownConstInt(Context).toString( 9146 /*Radix=*/10, /*Signed=*/false); 9147 } else { 9148 Diag(Pair.first->getBeginLoc(), diag::note_omp_critical_no_hint) << 1; 9149 } 9150 } 9151 } 9152 9153 setFunctionHasBranchProtectedScope(); 9154 9155 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc, 9156 Clauses, AStmt); 9157 if (!Pair.first && DirName.getName() && !DependentHint) 9158 DSAStack->addCriticalWithHint(Dir, Hint); 9159 return Dir; 9160 } 9161 9162 StmtResult Sema::ActOnOpenMPParallelForDirective( 9163 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 9164 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 9165 if (!AStmt) 9166 return StmtError(); 9167 9168 auto *CS = cast<CapturedStmt>(AStmt); 9169 // 1.2.2 OpenMP Language Terminology 9170 // Structured block - An executable statement with a single entry at the 9171 // top and a single exit at the bottom. 9172 // The point of exit cannot be a branch out of the structured block. 9173 // longjmp() and throw() must not violate the entry/exit criteria. 9174 CS->getCapturedDecl()->setNothrow(); 9175 9176 OMPLoopDirective::HelperExprs B; 9177 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 9178 // define the nested loops number. 9179 unsigned NestedLoopCount = 9180 checkOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses), 9181 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 9182 VarsWithImplicitDSA, B); 9183 if (NestedLoopCount == 0) 9184 return StmtError(); 9185 9186 assert((CurContext->isDependentContext() || B.builtAll()) && 9187 "omp parallel for loop exprs were not built"); 9188 9189 if (!CurContext->isDependentContext()) { 9190 // Finalize the clauses that need pre-built expressions for CodeGen. 9191 for (OMPClause *C : Clauses) { 9192 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 9193 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 9194 B.NumIterations, *this, CurScope, 9195 DSAStack)) 9196 return StmtError(); 9197 } 9198 } 9199 9200 setFunctionHasBranchProtectedScope(); 9201 return OMPParallelForDirective::Create( 9202 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 9203 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 9204 } 9205 9206 StmtResult Sema::ActOnOpenMPParallelForSimdDirective( 9207 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 9208 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 9209 if (!AStmt) 9210 return StmtError(); 9211 9212 auto *CS = cast<CapturedStmt>(AStmt); 9213 // 1.2.2 OpenMP Language Terminology 9214 // Structured block - An executable statement with a single entry at the 9215 // top and a single exit at the bottom. 9216 // The point of exit cannot be a branch out of the structured block. 9217 // longjmp() and throw() must not violate the entry/exit criteria. 9218 CS->getCapturedDecl()->setNothrow(); 9219 9220 OMPLoopDirective::HelperExprs B; 9221 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 9222 // define the nested loops number. 9223 unsigned NestedLoopCount = 9224 checkOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses), 9225 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 9226 VarsWithImplicitDSA, B); 9227 if (NestedLoopCount == 0) 9228 return StmtError(); 9229 9230 if (!CurContext->isDependentContext()) { 9231 // Finalize the clauses that need pre-built expressions for CodeGen. 9232 for (OMPClause *C : Clauses) { 9233 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 9234 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 9235 B.NumIterations, *this, CurScope, 9236 DSAStack)) 9237 return StmtError(); 9238 } 9239 } 9240 9241 if (checkSimdlenSafelenSpecified(*this, Clauses)) 9242 return StmtError(); 9243 9244 setFunctionHasBranchProtectedScope(); 9245 return OMPParallelForSimdDirective::Create( 9246 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 9247 } 9248 9249 StmtResult 9250 Sema::ActOnOpenMPParallelMasterDirective(ArrayRef<OMPClause *> Clauses, 9251 Stmt *AStmt, SourceLocation StartLoc, 9252 SourceLocation EndLoc) { 9253 if (!AStmt) 9254 return StmtError(); 9255 9256 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9257 auto *CS = cast<CapturedStmt>(AStmt); 9258 // 1.2.2 OpenMP Language Terminology 9259 // Structured block - An executable statement with a single entry at the 9260 // top and a single exit at the bottom. 9261 // The point of exit cannot be a branch out of the structured block. 9262 // longjmp() and throw() must not violate the entry/exit criteria. 9263 CS->getCapturedDecl()->setNothrow(); 9264 9265 setFunctionHasBranchProtectedScope(); 9266 9267 return OMPParallelMasterDirective::Create( 9268 Context, StartLoc, EndLoc, Clauses, AStmt, 9269 DSAStack->getTaskgroupReductionRef()); 9270 } 9271 9272 StmtResult 9273 Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses, 9274 Stmt *AStmt, SourceLocation StartLoc, 9275 SourceLocation EndLoc) { 9276 if (!AStmt) 9277 return StmtError(); 9278 9279 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9280 auto BaseStmt = AStmt; 9281 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 9282 BaseStmt = CS->getCapturedStmt(); 9283 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 9284 auto S = C->children(); 9285 if (S.begin() == S.end()) 9286 return StmtError(); 9287 // All associated statements must be '#pragma omp section' except for 9288 // the first one. 9289 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) { 9290 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 9291 if (SectionStmt) 9292 Diag(SectionStmt->getBeginLoc(), 9293 diag::err_omp_parallel_sections_substmt_not_section); 9294 return StmtError(); 9295 } 9296 cast<OMPSectionDirective>(SectionStmt) 9297 ->setHasCancel(DSAStack->isCancelRegion()); 9298 } 9299 } else { 9300 Diag(AStmt->getBeginLoc(), 9301 diag::err_omp_parallel_sections_not_compound_stmt); 9302 return StmtError(); 9303 } 9304 9305 setFunctionHasBranchProtectedScope(); 9306 9307 return OMPParallelSectionsDirective::Create( 9308 Context, StartLoc, EndLoc, Clauses, AStmt, 9309 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 9310 } 9311 9312 /// detach and mergeable clauses are mutially exclusive, check for it. 9313 static bool checkDetachMergeableClauses(Sema &S, 9314 ArrayRef<OMPClause *> Clauses) { 9315 const OMPClause *PrevClause = nullptr; 9316 bool ErrorFound = false; 9317 for (const OMPClause *C : Clauses) { 9318 if (C->getClauseKind() == OMPC_detach || 9319 C->getClauseKind() == OMPC_mergeable) { 9320 if (!PrevClause) { 9321 PrevClause = C; 9322 } else if (PrevClause->getClauseKind() != C->getClauseKind()) { 9323 S.Diag(C->getBeginLoc(), diag::err_omp_clauses_mutually_exclusive) 9324 << getOpenMPClauseName(C->getClauseKind()) 9325 << getOpenMPClauseName(PrevClause->getClauseKind()); 9326 S.Diag(PrevClause->getBeginLoc(), diag::note_omp_previous_clause) 9327 << getOpenMPClauseName(PrevClause->getClauseKind()); 9328 ErrorFound = true; 9329 } 9330 } 9331 } 9332 return ErrorFound; 9333 } 9334 9335 StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses, 9336 Stmt *AStmt, SourceLocation StartLoc, 9337 SourceLocation EndLoc) { 9338 if (!AStmt) 9339 return StmtError(); 9340 9341 // OpenMP 5.0, 2.10.1 task Construct 9342 // If a detach clause appears on the directive, then a mergeable clause cannot 9343 // appear on the same directive. 9344 if (checkDetachMergeableClauses(*this, Clauses)) 9345 return StmtError(); 9346 9347 auto *CS = cast<CapturedStmt>(AStmt); 9348 // 1.2.2 OpenMP Language Terminology 9349 // Structured block - An executable statement with a single entry at the 9350 // top and a single exit at the bottom. 9351 // The point of exit cannot be a branch out of the structured block. 9352 // longjmp() and throw() must not violate the entry/exit criteria. 9353 CS->getCapturedDecl()->setNothrow(); 9354 9355 setFunctionHasBranchProtectedScope(); 9356 9357 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 9358 DSAStack->isCancelRegion()); 9359 } 9360 9361 StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc, 9362 SourceLocation EndLoc) { 9363 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc); 9364 } 9365 9366 StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc, 9367 SourceLocation EndLoc) { 9368 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc); 9369 } 9370 9371 StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc, 9372 SourceLocation EndLoc) { 9373 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc); 9374 } 9375 9376 StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses, 9377 Stmt *AStmt, 9378 SourceLocation StartLoc, 9379 SourceLocation EndLoc) { 9380 if (!AStmt) 9381 return StmtError(); 9382 9383 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9384 9385 setFunctionHasBranchProtectedScope(); 9386 9387 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses, 9388 AStmt, 9389 DSAStack->getTaskgroupReductionRef()); 9390 } 9391 9392 StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses, 9393 SourceLocation StartLoc, 9394 SourceLocation EndLoc) { 9395 OMPFlushClause *FC = nullptr; 9396 OMPClause *OrderClause = nullptr; 9397 for (OMPClause *C : Clauses) { 9398 if (C->getClauseKind() == OMPC_flush) 9399 FC = cast<OMPFlushClause>(C); 9400 else 9401 OrderClause = C; 9402 } 9403 OpenMPClauseKind MemOrderKind = OMPC_unknown; 9404 SourceLocation MemOrderLoc; 9405 for (const OMPClause *C : Clauses) { 9406 if (C->getClauseKind() == OMPC_acq_rel || 9407 C->getClauseKind() == OMPC_acquire || 9408 C->getClauseKind() == OMPC_release) { 9409 if (MemOrderKind != OMPC_unknown) { 9410 Diag(C->getBeginLoc(), diag::err_omp_several_mem_order_clauses) 9411 << getOpenMPDirectiveName(OMPD_flush) << 1 9412 << SourceRange(C->getBeginLoc(), C->getEndLoc()); 9413 Diag(MemOrderLoc, diag::note_omp_previous_mem_order_clause) 9414 << getOpenMPClauseName(MemOrderKind); 9415 } else { 9416 MemOrderKind = C->getClauseKind(); 9417 MemOrderLoc = C->getBeginLoc(); 9418 } 9419 } 9420 } 9421 if (FC && OrderClause) { 9422 Diag(FC->getLParenLoc(), diag::err_omp_flush_order_clause_and_list) 9423 << getOpenMPClauseName(OrderClause->getClauseKind()); 9424 Diag(OrderClause->getBeginLoc(), diag::note_omp_flush_order_clause_here) 9425 << getOpenMPClauseName(OrderClause->getClauseKind()); 9426 return StmtError(); 9427 } 9428 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses); 9429 } 9430 9431 StmtResult Sema::ActOnOpenMPDepobjDirective(ArrayRef<OMPClause *> Clauses, 9432 SourceLocation StartLoc, 9433 SourceLocation EndLoc) { 9434 if (Clauses.empty()) { 9435 Diag(StartLoc, diag::err_omp_depobj_expected); 9436 return StmtError(); 9437 } else if (Clauses[0]->getClauseKind() != OMPC_depobj) { 9438 Diag(Clauses[0]->getBeginLoc(), diag::err_omp_depobj_expected); 9439 return StmtError(); 9440 } 9441 // Only depobj expression and another single clause is allowed. 9442 if (Clauses.size() > 2) { 9443 Diag(Clauses[2]->getBeginLoc(), 9444 diag::err_omp_depobj_single_clause_expected); 9445 return StmtError(); 9446 } else if (Clauses.size() < 1) { 9447 Diag(Clauses[0]->getEndLoc(), diag::err_omp_depobj_single_clause_expected); 9448 return StmtError(); 9449 } 9450 return OMPDepobjDirective::Create(Context, StartLoc, EndLoc, Clauses); 9451 } 9452 9453 StmtResult Sema::ActOnOpenMPScanDirective(ArrayRef<OMPClause *> Clauses, 9454 SourceLocation StartLoc, 9455 SourceLocation EndLoc) { 9456 // Check that exactly one clause is specified. 9457 if (Clauses.size() != 1) { 9458 Diag(Clauses.empty() ? EndLoc : Clauses[1]->getBeginLoc(), 9459 diag::err_omp_scan_single_clause_expected); 9460 return StmtError(); 9461 } 9462 // Check that scan directive is used in the scopeof the OpenMP loop body. 9463 if (Scope *S = DSAStack->getCurScope()) { 9464 Scope *ParentS = S->getParent(); 9465 if (!ParentS || ParentS->getParent() != ParentS->getBreakParent() || 9466 !ParentS->getBreakParent()->isOpenMPLoopScope()) 9467 return StmtError(Diag(StartLoc, diag::err_omp_orphaned_device_directive) 9468 << getOpenMPDirectiveName(OMPD_scan) << 5); 9469 } 9470 // Check that only one instance of scan directives is used in the same outer 9471 // region. 9472 if (DSAStack->doesParentHasScanDirective()) { 9473 Diag(StartLoc, diag::err_omp_several_directives_in_region) << "scan"; 9474 Diag(DSAStack->getParentScanDirectiveLoc(), 9475 diag::note_omp_previous_directive) 9476 << "scan"; 9477 return StmtError(); 9478 } 9479 DSAStack->setParentHasScanDirective(StartLoc); 9480 return OMPScanDirective::Create(Context, StartLoc, EndLoc, Clauses); 9481 } 9482 9483 StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses, 9484 Stmt *AStmt, 9485 SourceLocation StartLoc, 9486 SourceLocation EndLoc) { 9487 const OMPClause *DependFound = nullptr; 9488 const OMPClause *DependSourceClause = nullptr; 9489 const OMPClause *DependSinkClause = nullptr; 9490 bool ErrorFound = false; 9491 const OMPThreadsClause *TC = nullptr; 9492 const OMPSIMDClause *SC = nullptr; 9493 for (const OMPClause *C : Clauses) { 9494 if (auto *DC = dyn_cast<OMPDependClause>(C)) { 9495 DependFound = C; 9496 if (DC->getDependencyKind() == OMPC_DEPEND_source) { 9497 if (DependSourceClause) { 9498 Diag(C->getBeginLoc(), diag::err_omp_more_one_clause) 9499 << getOpenMPDirectiveName(OMPD_ordered) 9500 << getOpenMPClauseName(OMPC_depend) << 2; 9501 ErrorFound = true; 9502 } else { 9503 DependSourceClause = C; 9504 } 9505 if (DependSinkClause) { 9506 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed) 9507 << 0; 9508 ErrorFound = true; 9509 } 9510 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) { 9511 if (DependSourceClause) { 9512 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed) 9513 << 1; 9514 ErrorFound = true; 9515 } 9516 DependSinkClause = C; 9517 } 9518 } else if (C->getClauseKind() == OMPC_threads) { 9519 TC = cast<OMPThreadsClause>(C); 9520 } else if (C->getClauseKind() == OMPC_simd) { 9521 SC = cast<OMPSIMDClause>(C); 9522 } 9523 } 9524 if (!ErrorFound && !SC && 9525 isOpenMPSimdDirective(DSAStack->getParentDirective())) { 9526 // OpenMP [2.8.1,simd Construct, Restrictions] 9527 // An ordered construct with the simd clause is the only OpenMP construct 9528 // that can appear in the simd region. 9529 Diag(StartLoc, diag::err_omp_prohibited_region_simd) 9530 << (LangOpts.OpenMP >= 50 ? 1 : 0); 9531 ErrorFound = true; 9532 } else if (DependFound && (TC || SC)) { 9533 Diag(DependFound->getBeginLoc(), diag::err_omp_depend_clause_thread_simd) 9534 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind()); 9535 ErrorFound = true; 9536 } else if (DependFound && !DSAStack->getParentOrderedRegionParam().first) { 9537 Diag(DependFound->getBeginLoc(), 9538 diag::err_omp_ordered_directive_without_param); 9539 ErrorFound = true; 9540 } else if (TC || Clauses.empty()) { 9541 if (const Expr *Param = DSAStack->getParentOrderedRegionParam().first) { 9542 SourceLocation ErrLoc = TC ? TC->getBeginLoc() : StartLoc; 9543 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param) 9544 << (TC != nullptr); 9545 Diag(Param->getBeginLoc(), diag::note_omp_ordered_param) << 1; 9546 ErrorFound = true; 9547 } 9548 } 9549 if ((!AStmt && !DependFound) || ErrorFound) 9550 return StmtError(); 9551 9552 // OpenMP 5.0, 2.17.9, ordered Construct, Restrictions. 9553 // During execution of an iteration of a worksharing-loop or a loop nest 9554 // within a worksharing-loop, simd, or worksharing-loop SIMD region, a thread 9555 // must not execute more than one ordered region corresponding to an ordered 9556 // construct without a depend clause. 9557 if (!DependFound) { 9558 if (DSAStack->doesParentHasOrderedDirective()) { 9559 Diag(StartLoc, diag::err_omp_several_directives_in_region) << "ordered"; 9560 Diag(DSAStack->getParentOrderedDirectiveLoc(), 9561 diag::note_omp_previous_directive) 9562 << "ordered"; 9563 return StmtError(); 9564 } 9565 DSAStack->setParentHasOrderedDirective(StartLoc); 9566 } 9567 9568 if (AStmt) { 9569 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9570 9571 setFunctionHasBranchProtectedScope(); 9572 } 9573 9574 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 9575 } 9576 9577 namespace { 9578 /// Helper class for checking expression in 'omp atomic [update]' 9579 /// construct. 9580 class OpenMPAtomicUpdateChecker { 9581 /// Error results for atomic update expressions. 9582 enum ExprAnalysisErrorCode { 9583 /// A statement is not an expression statement. 9584 NotAnExpression, 9585 /// Expression is not builtin binary or unary operation. 9586 NotABinaryOrUnaryExpression, 9587 /// Unary operation is not post-/pre- increment/decrement operation. 9588 NotAnUnaryIncDecExpression, 9589 /// An expression is not of scalar type. 9590 NotAScalarType, 9591 /// A binary operation is not an assignment operation. 9592 NotAnAssignmentOp, 9593 /// RHS part of the binary operation is not a binary expression. 9594 NotABinaryExpression, 9595 /// RHS part is not additive/multiplicative/shift/biwise binary 9596 /// expression. 9597 NotABinaryOperator, 9598 /// RHS binary operation does not have reference to the updated LHS 9599 /// part. 9600 NotAnUpdateExpression, 9601 /// No errors is found. 9602 NoError 9603 }; 9604 /// Reference to Sema. 9605 Sema &SemaRef; 9606 /// A location for note diagnostics (when error is found). 9607 SourceLocation NoteLoc; 9608 /// 'x' lvalue part of the source atomic expression. 9609 Expr *X; 9610 /// 'expr' rvalue part of the source atomic expression. 9611 Expr *E; 9612 /// Helper expression of the form 9613 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or 9614 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. 9615 Expr *UpdateExpr; 9616 /// Is 'x' a LHS in a RHS part of full update expression. It is 9617 /// important for non-associative operations. 9618 bool IsXLHSInRHSPart; 9619 BinaryOperatorKind Op; 9620 SourceLocation OpLoc; 9621 /// true if the source expression is a postfix unary operation, false 9622 /// if it is a prefix unary operation. 9623 bool IsPostfixUpdate; 9624 9625 public: 9626 OpenMPAtomicUpdateChecker(Sema &SemaRef) 9627 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr), 9628 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {} 9629 /// Check specified statement that it is suitable for 'atomic update' 9630 /// constructs and extract 'x', 'expr' and Operation from the original 9631 /// expression. If DiagId and NoteId == 0, then only check is performed 9632 /// without error notification. 9633 /// \param DiagId Diagnostic which should be emitted if error is found. 9634 /// \param NoteId Diagnostic note for the main error message. 9635 /// \return true if statement is not an update expression, false otherwise. 9636 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0); 9637 /// Return the 'x' lvalue part of the source atomic expression. 9638 Expr *getX() const { return X; } 9639 /// Return the 'expr' rvalue part of the source atomic expression. 9640 Expr *getExpr() const { return E; } 9641 /// Return the update expression used in calculation of the updated 9642 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or 9643 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. 9644 Expr *getUpdateExpr() const { return UpdateExpr; } 9645 /// Return true if 'x' is LHS in RHS part of full update expression, 9646 /// false otherwise. 9647 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; } 9648 9649 /// true if the source expression is a postfix unary operation, false 9650 /// if it is a prefix unary operation. 9651 bool isPostfixUpdate() const { return IsPostfixUpdate; } 9652 9653 private: 9654 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0, 9655 unsigned NoteId = 0); 9656 }; 9657 } // namespace 9658 9659 bool OpenMPAtomicUpdateChecker::checkBinaryOperation( 9660 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) { 9661 ExprAnalysisErrorCode ErrorFound = NoError; 9662 SourceLocation ErrorLoc, NoteLoc; 9663 SourceRange ErrorRange, NoteRange; 9664 // Allowed constructs are: 9665 // x = x binop expr; 9666 // x = expr binop x; 9667 if (AtomicBinOp->getOpcode() == BO_Assign) { 9668 X = AtomicBinOp->getLHS(); 9669 if (const auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>( 9670 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) { 9671 if (AtomicInnerBinOp->isMultiplicativeOp() || 9672 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() || 9673 AtomicInnerBinOp->isBitwiseOp()) { 9674 Op = AtomicInnerBinOp->getOpcode(); 9675 OpLoc = AtomicInnerBinOp->getOperatorLoc(); 9676 Expr *LHS = AtomicInnerBinOp->getLHS(); 9677 Expr *RHS = AtomicInnerBinOp->getRHS(); 9678 llvm::FoldingSetNodeID XId, LHSId, RHSId; 9679 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(), 9680 /*Canonical=*/true); 9681 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(), 9682 /*Canonical=*/true); 9683 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(), 9684 /*Canonical=*/true); 9685 if (XId == LHSId) { 9686 E = RHS; 9687 IsXLHSInRHSPart = true; 9688 } else if (XId == RHSId) { 9689 E = LHS; 9690 IsXLHSInRHSPart = false; 9691 } else { 9692 ErrorLoc = AtomicInnerBinOp->getExprLoc(); 9693 ErrorRange = AtomicInnerBinOp->getSourceRange(); 9694 NoteLoc = X->getExprLoc(); 9695 NoteRange = X->getSourceRange(); 9696 ErrorFound = NotAnUpdateExpression; 9697 } 9698 } else { 9699 ErrorLoc = AtomicInnerBinOp->getExprLoc(); 9700 ErrorRange = AtomicInnerBinOp->getSourceRange(); 9701 NoteLoc = AtomicInnerBinOp->getOperatorLoc(); 9702 NoteRange = SourceRange(NoteLoc, NoteLoc); 9703 ErrorFound = NotABinaryOperator; 9704 } 9705 } else { 9706 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc(); 9707 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange(); 9708 ErrorFound = NotABinaryExpression; 9709 } 9710 } else { 9711 ErrorLoc = AtomicBinOp->getExprLoc(); 9712 ErrorRange = AtomicBinOp->getSourceRange(); 9713 NoteLoc = AtomicBinOp->getOperatorLoc(); 9714 NoteRange = SourceRange(NoteLoc, NoteLoc); 9715 ErrorFound = NotAnAssignmentOp; 9716 } 9717 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { 9718 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; 9719 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; 9720 return true; 9721 } 9722 if (SemaRef.CurContext->isDependentContext()) 9723 E = X = UpdateExpr = nullptr; 9724 return ErrorFound != NoError; 9725 } 9726 9727 bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId, 9728 unsigned NoteId) { 9729 ExprAnalysisErrorCode ErrorFound = NoError; 9730 SourceLocation ErrorLoc, NoteLoc; 9731 SourceRange ErrorRange, NoteRange; 9732 // Allowed constructs are: 9733 // x++; 9734 // x--; 9735 // ++x; 9736 // --x; 9737 // x binop= expr; 9738 // x = x binop expr; 9739 // x = expr binop x; 9740 if (auto *AtomicBody = dyn_cast<Expr>(S)) { 9741 AtomicBody = AtomicBody->IgnoreParenImpCasts(); 9742 if (AtomicBody->getType()->isScalarType() || 9743 AtomicBody->isInstantiationDependent()) { 9744 if (const auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>( 9745 AtomicBody->IgnoreParenImpCasts())) { 9746 // Check for Compound Assignment Operation 9747 Op = BinaryOperator::getOpForCompoundAssignment( 9748 AtomicCompAssignOp->getOpcode()); 9749 OpLoc = AtomicCompAssignOp->getOperatorLoc(); 9750 E = AtomicCompAssignOp->getRHS(); 9751 X = AtomicCompAssignOp->getLHS()->IgnoreParens(); 9752 IsXLHSInRHSPart = true; 9753 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>( 9754 AtomicBody->IgnoreParenImpCasts())) { 9755 // Check for Binary Operation 9756 if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId)) 9757 return true; 9758 } else if (const auto *AtomicUnaryOp = dyn_cast<UnaryOperator>( 9759 AtomicBody->IgnoreParenImpCasts())) { 9760 // Check for Unary Operation 9761 if (AtomicUnaryOp->isIncrementDecrementOp()) { 9762 IsPostfixUpdate = AtomicUnaryOp->isPostfix(); 9763 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub; 9764 OpLoc = AtomicUnaryOp->getOperatorLoc(); 9765 X = AtomicUnaryOp->getSubExpr()->IgnoreParens(); 9766 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get(); 9767 IsXLHSInRHSPart = true; 9768 } else { 9769 ErrorFound = NotAnUnaryIncDecExpression; 9770 ErrorLoc = AtomicUnaryOp->getExprLoc(); 9771 ErrorRange = AtomicUnaryOp->getSourceRange(); 9772 NoteLoc = AtomicUnaryOp->getOperatorLoc(); 9773 NoteRange = SourceRange(NoteLoc, NoteLoc); 9774 } 9775 } else if (!AtomicBody->isInstantiationDependent()) { 9776 ErrorFound = NotABinaryOrUnaryExpression; 9777 NoteLoc = ErrorLoc = AtomicBody->getExprLoc(); 9778 NoteRange = ErrorRange = AtomicBody->getSourceRange(); 9779 } 9780 } else { 9781 ErrorFound = NotAScalarType; 9782 NoteLoc = ErrorLoc = AtomicBody->getBeginLoc(); 9783 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 9784 } 9785 } else { 9786 ErrorFound = NotAnExpression; 9787 NoteLoc = ErrorLoc = S->getBeginLoc(); 9788 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 9789 } 9790 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { 9791 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; 9792 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; 9793 return true; 9794 } 9795 if (SemaRef.CurContext->isDependentContext()) 9796 E = X = UpdateExpr = nullptr; 9797 if (ErrorFound == NoError && E && X) { 9798 // Build an update expression of form 'OpaqueValueExpr(x) binop 9799 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop 9800 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression. 9801 auto *OVEX = new (SemaRef.getASTContext()) 9802 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue); 9803 auto *OVEExpr = new (SemaRef.getASTContext()) 9804 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue); 9805 ExprResult Update = 9806 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr, 9807 IsXLHSInRHSPart ? OVEExpr : OVEX); 9808 if (Update.isInvalid()) 9809 return true; 9810 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(), 9811 Sema::AA_Casting); 9812 if (Update.isInvalid()) 9813 return true; 9814 UpdateExpr = Update.get(); 9815 } 9816 return ErrorFound != NoError; 9817 } 9818 9819 StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses, 9820 Stmt *AStmt, 9821 SourceLocation StartLoc, 9822 SourceLocation EndLoc) { 9823 // Register location of the first atomic directive. 9824 DSAStack->addAtomicDirectiveLoc(StartLoc); 9825 if (!AStmt) 9826 return StmtError(); 9827 9828 // 1.2.2 OpenMP Language Terminology 9829 // Structured block - An executable statement with a single entry at the 9830 // top and a single exit at the bottom. 9831 // The point of exit cannot be a branch out of the structured block. 9832 // longjmp() and throw() must not violate the entry/exit criteria. 9833 OpenMPClauseKind AtomicKind = OMPC_unknown; 9834 SourceLocation AtomicKindLoc; 9835 OpenMPClauseKind MemOrderKind = OMPC_unknown; 9836 SourceLocation MemOrderLoc; 9837 for (const OMPClause *C : Clauses) { 9838 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write || 9839 C->getClauseKind() == OMPC_update || 9840 C->getClauseKind() == OMPC_capture) { 9841 if (AtomicKind != OMPC_unknown) { 9842 Diag(C->getBeginLoc(), diag::err_omp_atomic_several_clauses) 9843 << SourceRange(C->getBeginLoc(), C->getEndLoc()); 9844 Diag(AtomicKindLoc, diag::note_omp_previous_mem_order_clause) 9845 << getOpenMPClauseName(AtomicKind); 9846 } else { 9847 AtomicKind = C->getClauseKind(); 9848 AtomicKindLoc = C->getBeginLoc(); 9849 } 9850 } 9851 if (C->getClauseKind() == OMPC_seq_cst || 9852 C->getClauseKind() == OMPC_acq_rel || 9853 C->getClauseKind() == OMPC_acquire || 9854 C->getClauseKind() == OMPC_release || 9855 C->getClauseKind() == OMPC_relaxed) { 9856 if (MemOrderKind != OMPC_unknown) { 9857 Diag(C->getBeginLoc(), diag::err_omp_several_mem_order_clauses) 9858 << getOpenMPDirectiveName(OMPD_atomic) << 0 9859 << SourceRange(C->getBeginLoc(), C->getEndLoc()); 9860 Diag(MemOrderLoc, diag::note_omp_previous_mem_order_clause) 9861 << getOpenMPClauseName(MemOrderKind); 9862 } else { 9863 MemOrderKind = C->getClauseKind(); 9864 MemOrderLoc = C->getBeginLoc(); 9865 } 9866 } 9867 } 9868 // OpenMP 5.0, 2.17.7 atomic Construct, Restrictions 9869 // If atomic-clause is read then memory-order-clause must not be acq_rel or 9870 // release. 9871 // If atomic-clause is write then memory-order-clause must not be acq_rel or 9872 // acquire. 9873 // If atomic-clause is update or not present then memory-order-clause must not 9874 // be acq_rel or acquire. 9875 if ((AtomicKind == OMPC_read && 9876 (MemOrderKind == OMPC_acq_rel || MemOrderKind == OMPC_release)) || 9877 ((AtomicKind == OMPC_write || AtomicKind == OMPC_update || 9878 AtomicKind == OMPC_unknown) && 9879 (MemOrderKind == OMPC_acq_rel || MemOrderKind == OMPC_acquire))) { 9880 SourceLocation Loc = AtomicKindLoc; 9881 if (AtomicKind == OMPC_unknown) 9882 Loc = StartLoc; 9883 Diag(Loc, diag::err_omp_atomic_incompatible_mem_order_clause) 9884 << getOpenMPClauseName(AtomicKind) 9885 << (AtomicKind == OMPC_unknown ? 1 : 0) 9886 << getOpenMPClauseName(MemOrderKind); 9887 Diag(MemOrderLoc, diag::note_omp_previous_mem_order_clause) 9888 << getOpenMPClauseName(MemOrderKind); 9889 } 9890 9891 Stmt *Body = AStmt; 9892 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body)) 9893 Body = EWC->getSubExpr(); 9894 9895 Expr *X = nullptr; 9896 Expr *V = nullptr; 9897 Expr *E = nullptr; 9898 Expr *UE = nullptr; 9899 bool IsXLHSInRHSPart = false; 9900 bool IsPostfixUpdate = false; 9901 // OpenMP [2.12.6, atomic Construct] 9902 // In the next expressions: 9903 // * x and v (as applicable) are both l-value expressions with scalar type. 9904 // * During the execution of an atomic region, multiple syntactic 9905 // occurrences of x must designate the same storage location. 9906 // * Neither of v and expr (as applicable) may access the storage location 9907 // designated by x. 9908 // * Neither of x and expr (as applicable) may access the storage location 9909 // designated by v. 9910 // * expr is an expression with scalar type. 9911 // * binop is one of +, *, -, /, &, ^, |, <<, or >>. 9912 // * binop, binop=, ++, and -- are not overloaded operators. 9913 // * The expression x binop expr must be numerically equivalent to x binop 9914 // (expr). This requirement is satisfied if the operators in expr have 9915 // precedence greater than binop, or by using parentheses around expr or 9916 // subexpressions of expr. 9917 // * The expression expr binop x must be numerically equivalent to (expr) 9918 // binop x. This requirement is satisfied if the operators in expr have 9919 // precedence equal to or greater than binop, or by using parentheses around 9920 // expr or subexpressions of expr. 9921 // * For forms that allow multiple occurrences of x, the number of times 9922 // that x is evaluated is unspecified. 9923 if (AtomicKind == OMPC_read) { 9924 enum { 9925 NotAnExpression, 9926 NotAnAssignmentOp, 9927 NotAScalarType, 9928 NotAnLValue, 9929 NoError 9930 } ErrorFound = NoError; 9931 SourceLocation ErrorLoc, NoteLoc; 9932 SourceRange ErrorRange, NoteRange; 9933 // If clause is read: 9934 // v = x; 9935 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 9936 const auto *AtomicBinOp = 9937 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 9938 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 9939 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); 9940 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts(); 9941 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && 9942 (V->isInstantiationDependent() || V->getType()->isScalarType())) { 9943 if (!X->isLValue() || !V->isLValue()) { 9944 const Expr *NotLValueExpr = X->isLValue() ? V : X; 9945 ErrorFound = NotAnLValue; 9946 ErrorLoc = AtomicBinOp->getExprLoc(); 9947 ErrorRange = AtomicBinOp->getSourceRange(); 9948 NoteLoc = NotLValueExpr->getExprLoc(); 9949 NoteRange = NotLValueExpr->getSourceRange(); 9950 } 9951 } else if (!X->isInstantiationDependent() || 9952 !V->isInstantiationDependent()) { 9953 const Expr *NotScalarExpr = 9954 (X->isInstantiationDependent() || X->getType()->isScalarType()) 9955 ? V 9956 : X; 9957 ErrorFound = NotAScalarType; 9958 ErrorLoc = AtomicBinOp->getExprLoc(); 9959 ErrorRange = AtomicBinOp->getSourceRange(); 9960 NoteLoc = NotScalarExpr->getExprLoc(); 9961 NoteRange = NotScalarExpr->getSourceRange(); 9962 } 9963 } else if (!AtomicBody->isInstantiationDependent()) { 9964 ErrorFound = NotAnAssignmentOp; 9965 ErrorLoc = AtomicBody->getExprLoc(); 9966 ErrorRange = AtomicBody->getSourceRange(); 9967 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 9968 : AtomicBody->getExprLoc(); 9969 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 9970 : AtomicBody->getSourceRange(); 9971 } 9972 } else { 9973 ErrorFound = NotAnExpression; 9974 NoteLoc = ErrorLoc = Body->getBeginLoc(); 9975 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 9976 } 9977 if (ErrorFound != NoError) { 9978 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement) 9979 << ErrorRange; 9980 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound 9981 << NoteRange; 9982 return StmtError(); 9983 } 9984 if (CurContext->isDependentContext()) 9985 V = X = nullptr; 9986 } else if (AtomicKind == OMPC_write) { 9987 enum { 9988 NotAnExpression, 9989 NotAnAssignmentOp, 9990 NotAScalarType, 9991 NotAnLValue, 9992 NoError 9993 } ErrorFound = NoError; 9994 SourceLocation ErrorLoc, NoteLoc; 9995 SourceRange ErrorRange, NoteRange; 9996 // If clause is write: 9997 // x = expr; 9998 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 9999 const auto *AtomicBinOp = 10000 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 10001 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 10002 X = AtomicBinOp->getLHS(); 10003 E = AtomicBinOp->getRHS(); 10004 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && 10005 (E->isInstantiationDependent() || E->getType()->isScalarType())) { 10006 if (!X->isLValue()) { 10007 ErrorFound = NotAnLValue; 10008 ErrorLoc = AtomicBinOp->getExprLoc(); 10009 ErrorRange = AtomicBinOp->getSourceRange(); 10010 NoteLoc = X->getExprLoc(); 10011 NoteRange = X->getSourceRange(); 10012 } 10013 } else if (!X->isInstantiationDependent() || 10014 !E->isInstantiationDependent()) { 10015 const Expr *NotScalarExpr = 10016 (X->isInstantiationDependent() || X->getType()->isScalarType()) 10017 ? E 10018 : X; 10019 ErrorFound = NotAScalarType; 10020 ErrorLoc = AtomicBinOp->getExprLoc(); 10021 ErrorRange = AtomicBinOp->getSourceRange(); 10022 NoteLoc = NotScalarExpr->getExprLoc(); 10023 NoteRange = NotScalarExpr->getSourceRange(); 10024 } 10025 } else if (!AtomicBody->isInstantiationDependent()) { 10026 ErrorFound = NotAnAssignmentOp; 10027 ErrorLoc = AtomicBody->getExprLoc(); 10028 ErrorRange = AtomicBody->getSourceRange(); 10029 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 10030 : AtomicBody->getExprLoc(); 10031 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 10032 : AtomicBody->getSourceRange(); 10033 } 10034 } else { 10035 ErrorFound = NotAnExpression; 10036 NoteLoc = ErrorLoc = Body->getBeginLoc(); 10037 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 10038 } 10039 if (ErrorFound != NoError) { 10040 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement) 10041 << ErrorRange; 10042 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound 10043 << NoteRange; 10044 return StmtError(); 10045 } 10046 if (CurContext->isDependentContext()) 10047 E = X = nullptr; 10048 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) { 10049 // If clause is update: 10050 // x++; 10051 // x--; 10052 // ++x; 10053 // --x; 10054 // x binop= expr; 10055 // x = x binop expr; 10056 // x = expr binop x; 10057 OpenMPAtomicUpdateChecker Checker(*this); 10058 if (Checker.checkStatement( 10059 Body, (AtomicKind == OMPC_update) 10060 ? diag::err_omp_atomic_update_not_expression_statement 10061 : diag::err_omp_atomic_not_expression_statement, 10062 diag::note_omp_atomic_update)) 10063 return StmtError(); 10064 if (!CurContext->isDependentContext()) { 10065 E = Checker.getExpr(); 10066 X = Checker.getX(); 10067 UE = Checker.getUpdateExpr(); 10068 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 10069 } 10070 } else if (AtomicKind == OMPC_capture) { 10071 enum { 10072 NotAnAssignmentOp, 10073 NotACompoundStatement, 10074 NotTwoSubstatements, 10075 NotASpecificExpression, 10076 NoError 10077 } ErrorFound = NoError; 10078 SourceLocation ErrorLoc, NoteLoc; 10079 SourceRange ErrorRange, NoteRange; 10080 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 10081 // If clause is a capture: 10082 // v = x++; 10083 // v = x--; 10084 // v = ++x; 10085 // v = --x; 10086 // v = x binop= expr; 10087 // v = x = x binop expr; 10088 // v = x = expr binop x; 10089 const auto *AtomicBinOp = 10090 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 10091 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 10092 V = AtomicBinOp->getLHS(); 10093 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); 10094 OpenMPAtomicUpdateChecker Checker(*this); 10095 if (Checker.checkStatement( 10096 Body, diag::err_omp_atomic_capture_not_expression_statement, 10097 diag::note_omp_atomic_update)) 10098 return StmtError(); 10099 E = Checker.getExpr(); 10100 X = Checker.getX(); 10101 UE = Checker.getUpdateExpr(); 10102 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 10103 IsPostfixUpdate = Checker.isPostfixUpdate(); 10104 } else if (!AtomicBody->isInstantiationDependent()) { 10105 ErrorLoc = AtomicBody->getExprLoc(); 10106 ErrorRange = AtomicBody->getSourceRange(); 10107 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 10108 : AtomicBody->getExprLoc(); 10109 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 10110 : AtomicBody->getSourceRange(); 10111 ErrorFound = NotAnAssignmentOp; 10112 } 10113 if (ErrorFound != NoError) { 10114 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement) 10115 << ErrorRange; 10116 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; 10117 return StmtError(); 10118 } 10119 if (CurContext->isDependentContext()) 10120 UE = V = E = X = nullptr; 10121 } else { 10122 // If clause is a capture: 10123 // { v = x; x = expr; } 10124 // { v = x; x++; } 10125 // { v = x; x--; } 10126 // { v = x; ++x; } 10127 // { v = x; --x; } 10128 // { v = x; x binop= expr; } 10129 // { v = x; x = x binop expr; } 10130 // { v = x; x = expr binop x; } 10131 // { x++; v = x; } 10132 // { x--; v = x; } 10133 // { ++x; v = x; } 10134 // { --x; v = x; } 10135 // { x binop= expr; v = x; } 10136 // { x = x binop expr; v = x; } 10137 // { x = expr binop x; v = x; } 10138 if (auto *CS = dyn_cast<CompoundStmt>(Body)) { 10139 // Check that this is { expr1; expr2; } 10140 if (CS->size() == 2) { 10141 Stmt *First = CS->body_front(); 10142 Stmt *Second = CS->body_back(); 10143 if (auto *EWC = dyn_cast<ExprWithCleanups>(First)) 10144 First = EWC->getSubExpr()->IgnoreParenImpCasts(); 10145 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second)) 10146 Second = EWC->getSubExpr()->IgnoreParenImpCasts(); 10147 // Need to find what subexpression is 'v' and what is 'x'. 10148 OpenMPAtomicUpdateChecker Checker(*this); 10149 bool IsUpdateExprFound = !Checker.checkStatement(Second); 10150 BinaryOperator *BinOp = nullptr; 10151 if (IsUpdateExprFound) { 10152 BinOp = dyn_cast<BinaryOperator>(First); 10153 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; 10154 } 10155 if (IsUpdateExprFound && !CurContext->isDependentContext()) { 10156 // { v = x; x++; } 10157 // { v = x; x--; } 10158 // { v = x; ++x; } 10159 // { v = x; --x; } 10160 // { v = x; x binop= expr; } 10161 // { v = x; x = x binop expr; } 10162 // { v = x; x = expr binop x; } 10163 // Check that the first expression has form v = x. 10164 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); 10165 llvm::FoldingSetNodeID XId, PossibleXId; 10166 Checker.getX()->Profile(XId, Context, /*Canonical=*/true); 10167 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); 10168 IsUpdateExprFound = XId == PossibleXId; 10169 if (IsUpdateExprFound) { 10170 V = BinOp->getLHS(); 10171 X = Checker.getX(); 10172 E = Checker.getExpr(); 10173 UE = Checker.getUpdateExpr(); 10174 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 10175 IsPostfixUpdate = true; 10176 } 10177 } 10178 if (!IsUpdateExprFound) { 10179 IsUpdateExprFound = !Checker.checkStatement(First); 10180 BinOp = nullptr; 10181 if (IsUpdateExprFound) { 10182 BinOp = dyn_cast<BinaryOperator>(Second); 10183 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; 10184 } 10185 if (IsUpdateExprFound && !CurContext->isDependentContext()) { 10186 // { x++; v = x; } 10187 // { x--; v = x; } 10188 // { ++x; v = x; } 10189 // { --x; v = x; } 10190 // { x binop= expr; v = x; } 10191 // { x = x binop expr; v = x; } 10192 // { x = expr binop x; v = x; } 10193 // Check that the second expression has form v = x. 10194 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); 10195 llvm::FoldingSetNodeID XId, PossibleXId; 10196 Checker.getX()->Profile(XId, Context, /*Canonical=*/true); 10197 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); 10198 IsUpdateExprFound = XId == PossibleXId; 10199 if (IsUpdateExprFound) { 10200 V = BinOp->getLHS(); 10201 X = Checker.getX(); 10202 E = Checker.getExpr(); 10203 UE = Checker.getUpdateExpr(); 10204 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 10205 IsPostfixUpdate = false; 10206 } 10207 } 10208 } 10209 if (!IsUpdateExprFound) { 10210 // { v = x; x = expr; } 10211 auto *FirstExpr = dyn_cast<Expr>(First); 10212 auto *SecondExpr = dyn_cast<Expr>(Second); 10213 if (!FirstExpr || !SecondExpr || 10214 !(FirstExpr->isInstantiationDependent() || 10215 SecondExpr->isInstantiationDependent())) { 10216 auto *FirstBinOp = dyn_cast<BinaryOperator>(First); 10217 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) { 10218 ErrorFound = NotAnAssignmentOp; 10219 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc() 10220 : First->getBeginLoc(); 10221 NoteRange = ErrorRange = FirstBinOp 10222 ? FirstBinOp->getSourceRange() 10223 : SourceRange(ErrorLoc, ErrorLoc); 10224 } else { 10225 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second); 10226 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) { 10227 ErrorFound = NotAnAssignmentOp; 10228 NoteLoc = ErrorLoc = SecondBinOp 10229 ? SecondBinOp->getOperatorLoc() 10230 : Second->getBeginLoc(); 10231 NoteRange = ErrorRange = 10232 SecondBinOp ? SecondBinOp->getSourceRange() 10233 : SourceRange(ErrorLoc, ErrorLoc); 10234 } else { 10235 Expr *PossibleXRHSInFirst = 10236 FirstBinOp->getRHS()->IgnoreParenImpCasts(); 10237 Expr *PossibleXLHSInSecond = 10238 SecondBinOp->getLHS()->IgnoreParenImpCasts(); 10239 llvm::FoldingSetNodeID X1Id, X2Id; 10240 PossibleXRHSInFirst->Profile(X1Id, Context, 10241 /*Canonical=*/true); 10242 PossibleXLHSInSecond->Profile(X2Id, Context, 10243 /*Canonical=*/true); 10244 IsUpdateExprFound = X1Id == X2Id; 10245 if (IsUpdateExprFound) { 10246 V = FirstBinOp->getLHS(); 10247 X = SecondBinOp->getLHS(); 10248 E = SecondBinOp->getRHS(); 10249 UE = nullptr; 10250 IsXLHSInRHSPart = false; 10251 IsPostfixUpdate = true; 10252 } else { 10253 ErrorFound = NotASpecificExpression; 10254 ErrorLoc = FirstBinOp->getExprLoc(); 10255 ErrorRange = FirstBinOp->getSourceRange(); 10256 NoteLoc = SecondBinOp->getLHS()->getExprLoc(); 10257 NoteRange = SecondBinOp->getRHS()->getSourceRange(); 10258 } 10259 } 10260 } 10261 } 10262 } 10263 } else { 10264 NoteLoc = ErrorLoc = Body->getBeginLoc(); 10265 NoteRange = ErrorRange = 10266 SourceRange(Body->getBeginLoc(), Body->getBeginLoc()); 10267 ErrorFound = NotTwoSubstatements; 10268 } 10269 } else { 10270 NoteLoc = ErrorLoc = Body->getBeginLoc(); 10271 NoteRange = ErrorRange = 10272 SourceRange(Body->getBeginLoc(), Body->getBeginLoc()); 10273 ErrorFound = NotACompoundStatement; 10274 } 10275 if (ErrorFound != NoError) { 10276 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement) 10277 << ErrorRange; 10278 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; 10279 return StmtError(); 10280 } 10281 if (CurContext->isDependentContext()) 10282 UE = V = E = X = nullptr; 10283 } 10284 } 10285 10286 setFunctionHasBranchProtectedScope(); 10287 10288 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 10289 X, V, E, UE, IsXLHSInRHSPart, 10290 IsPostfixUpdate); 10291 } 10292 10293 StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses, 10294 Stmt *AStmt, 10295 SourceLocation StartLoc, 10296 SourceLocation EndLoc) { 10297 if (!AStmt) 10298 return StmtError(); 10299 10300 auto *CS = cast<CapturedStmt>(AStmt); 10301 // 1.2.2 OpenMP Language Terminology 10302 // Structured block - An executable statement with a single entry at the 10303 // top and a single exit at the bottom. 10304 // The point of exit cannot be a branch out of the structured block. 10305 // longjmp() and throw() must not violate the entry/exit criteria. 10306 CS->getCapturedDecl()->setNothrow(); 10307 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target); 10308 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10309 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10310 // 1.2.2 OpenMP Language Terminology 10311 // Structured block - An executable statement with a single entry at the 10312 // top and a single exit at the bottom. 10313 // The point of exit cannot be a branch out of the structured block. 10314 // longjmp() and throw() must not violate the entry/exit criteria. 10315 CS->getCapturedDecl()->setNothrow(); 10316 } 10317 10318 // OpenMP [2.16, Nesting of Regions] 10319 // If specified, a teams construct must be contained within a target 10320 // construct. That target construct must contain no statements or directives 10321 // outside of the teams construct. 10322 if (DSAStack->hasInnerTeamsRegion()) { 10323 const Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true); 10324 bool OMPTeamsFound = true; 10325 if (const auto *CS = dyn_cast<CompoundStmt>(S)) { 10326 auto I = CS->body_begin(); 10327 while (I != CS->body_end()) { 10328 const auto *OED = dyn_cast<OMPExecutableDirective>(*I); 10329 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind()) || 10330 OMPTeamsFound) { 10331 10332 OMPTeamsFound = false; 10333 break; 10334 } 10335 ++I; 10336 } 10337 assert(I != CS->body_end() && "Not found statement"); 10338 S = *I; 10339 } else { 10340 const auto *OED = dyn_cast<OMPExecutableDirective>(S); 10341 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind()); 10342 } 10343 if (!OMPTeamsFound) { 10344 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams); 10345 Diag(DSAStack->getInnerTeamsRegionLoc(), 10346 diag::note_omp_nested_teams_construct_here); 10347 Diag(S->getBeginLoc(), diag::note_omp_nested_statement_here) 10348 << isa<OMPExecutableDirective>(S); 10349 return StmtError(); 10350 } 10351 } 10352 10353 setFunctionHasBranchProtectedScope(); 10354 10355 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 10356 } 10357 10358 StmtResult 10359 Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses, 10360 Stmt *AStmt, SourceLocation StartLoc, 10361 SourceLocation EndLoc) { 10362 if (!AStmt) 10363 return StmtError(); 10364 10365 auto *CS = cast<CapturedStmt>(AStmt); 10366 // 1.2.2 OpenMP Language Terminology 10367 // Structured block - An executable statement with a single entry at the 10368 // top and a single exit at the bottom. 10369 // The point of exit cannot be a branch out of the structured block. 10370 // longjmp() and throw() must not violate the entry/exit criteria. 10371 CS->getCapturedDecl()->setNothrow(); 10372 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel); 10373 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10374 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10375 // 1.2.2 OpenMP Language Terminology 10376 // Structured block - An executable statement with a single entry at the 10377 // top and a single exit at the bottom. 10378 // The point of exit cannot be a branch out of the structured block. 10379 // longjmp() and throw() must not violate the entry/exit criteria. 10380 CS->getCapturedDecl()->setNothrow(); 10381 } 10382 10383 setFunctionHasBranchProtectedScope(); 10384 10385 return OMPTargetParallelDirective::Create( 10386 Context, StartLoc, EndLoc, Clauses, AStmt, 10387 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 10388 } 10389 10390 StmtResult Sema::ActOnOpenMPTargetParallelForDirective( 10391 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10392 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10393 if (!AStmt) 10394 return StmtError(); 10395 10396 auto *CS = cast<CapturedStmt>(AStmt); 10397 // 1.2.2 OpenMP Language Terminology 10398 // Structured block - An executable statement with a single entry at the 10399 // top and a single exit at the bottom. 10400 // The point of exit cannot be a branch out of the structured block. 10401 // longjmp() and throw() must not violate the entry/exit criteria. 10402 CS->getCapturedDecl()->setNothrow(); 10403 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for); 10404 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10405 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10406 // 1.2.2 OpenMP Language Terminology 10407 // Structured block - An executable statement with a single entry at the 10408 // top and a single exit at the bottom. 10409 // The point of exit cannot be a branch out of the structured block. 10410 // longjmp() and throw() must not violate the entry/exit criteria. 10411 CS->getCapturedDecl()->setNothrow(); 10412 } 10413 10414 OMPLoopDirective::HelperExprs B; 10415 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 10416 // define the nested loops number. 10417 unsigned NestedLoopCount = 10418 checkOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses), 10419 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 10420 VarsWithImplicitDSA, B); 10421 if (NestedLoopCount == 0) 10422 return StmtError(); 10423 10424 assert((CurContext->isDependentContext() || B.builtAll()) && 10425 "omp target parallel for loop exprs were not built"); 10426 10427 if (!CurContext->isDependentContext()) { 10428 // Finalize the clauses that need pre-built expressions for CodeGen. 10429 for (OMPClause *C : Clauses) { 10430 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 10431 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 10432 B.NumIterations, *this, CurScope, 10433 DSAStack)) 10434 return StmtError(); 10435 } 10436 } 10437 10438 setFunctionHasBranchProtectedScope(); 10439 return OMPTargetParallelForDirective::Create( 10440 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 10441 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 10442 } 10443 10444 /// Check for existence of a map clause in the list of clauses. 10445 static bool hasClauses(ArrayRef<OMPClause *> Clauses, 10446 const OpenMPClauseKind K) { 10447 return llvm::any_of( 10448 Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; }); 10449 } 10450 10451 template <typename... Params> 10452 static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K, 10453 const Params... ClauseTypes) { 10454 return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...); 10455 } 10456 10457 StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses, 10458 Stmt *AStmt, 10459 SourceLocation StartLoc, 10460 SourceLocation EndLoc) { 10461 if (!AStmt) 10462 return StmtError(); 10463 10464 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10465 10466 // OpenMP [2.12.2, target data Construct, Restrictions] 10467 // At least one map, use_device_addr or use_device_ptr clause must appear on 10468 // the directive. 10469 if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr) && 10470 (LangOpts.OpenMP < 50 || !hasClauses(Clauses, OMPC_use_device_addr))) { 10471 StringRef Expected; 10472 if (LangOpts.OpenMP < 50) 10473 Expected = "'map' or 'use_device_ptr'"; 10474 else 10475 Expected = "'map', 'use_device_ptr', or 'use_device_addr'"; 10476 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 10477 << Expected << getOpenMPDirectiveName(OMPD_target_data); 10478 return StmtError(); 10479 } 10480 10481 setFunctionHasBranchProtectedScope(); 10482 10483 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 10484 AStmt); 10485 } 10486 10487 StmtResult 10488 Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses, 10489 SourceLocation StartLoc, 10490 SourceLocation EndLoc, Stmt *AStmt) { 10491 if (!AStmt) 10492 return StmtError(); 10493 10494 auto *CS = cast<CapturedStmt>(AStmt); 10495 // 1.2.2 OpenMP Language Terminology 10496 // Structured block - An executable statement with a single entry at the 10497 // top and a single exit at the bottom. 10498 // The point of exit cannot be a branch out of the structured block. 10499 // longjmp() and throw() must not violate the entry/exit criteria. 10500 CS->getCapturedDecl()->setNothrow(); 10501 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_enter_data); 10502 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10503 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10504 // 1.2.2 OpenMP Language Terminology 10505 // Structured block - An executable statement with a single entry at the 10506 // top and a single exit at the bottom. 10507 // The point of exit cannot be a branch out of the structured block. 10508 // longjmp() and throw() must not violate the entry/exit criteria. 10509 CS->getCapturedDecl()->setNothrow(); 10510 } 10511 10512 // OpenMP [2.10.2, Restrictions, p. 99] 10513 // At least one map clause must appear on the directive. 10514 if (!hasClauses(Clauses, OMPC_map)) { 10515 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 10516 << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data); 10517 return StmtError(); 10518 } 10519 10520 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 10521 AStmt); 10522 } 10523 10524 StmtResult 10525 Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses, 10526 SourceLocation StartLoc, 10527 SourceLocation EndLoc, Stmt *AStmt) { 10528 if (!AStmt) 10529 return StmtError(); 10530 10531 auto *CS = cast<CapturedStmt>(AStmt); 10532 // 1.2.2 OpenMP Language Terminology 10533 // Structured block - An executable statement with a single entry at the 10534 // top and a single exit at the bottom. 10535 // The point of exit cannot be a branch out of the structured block. 10536 // longjmp() and throw() must not violate the entry/exit criteria. 10537 CS->getCapturedDecl()->setNothrow(); 10538 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_exit_data); 10539 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10540 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10541 // 1.2.2 OpenMP Language Terminology 10542 // Structured block - An executable statement with a single entry at the 10543 // top and a single exit at the bottom. 10544 // The point of exit cannot be a branch out of the structured block. 10545 // longjmp() and throw() must not violate the entry/exit criteria. 10546 CS->getCapturedDecl()->setNothrow(); 10547 } 10548 10549 // OpenMP [2.10.3, Restrictions, p. 102] 10550 // At least one map clause must appear on the directive. 10551 if (!hasClauses(Clauses, OMPC_map)) { 10552 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 10553 << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data); 10554 return StmtError(); 10555 } 10556 10557 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 10558 AStmt); 10559 } 10560 10561 StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses, 10562 SourceLocation StartLoc, 10563 SourceLocation EndLoc, 10564 Stmt *AStmt) { 10565 if (!AStmt) 10566 return StmtError(); 10567 10568 auto *CS = cast<CapturedStmt>(AStmt); 10569 // 1.2.2 OpenMP Language Terminology 10570 // Structured block - An executable statement with a single entry at the 10571 // top and a single exit at the bottom. 10572 // The point of exit cannot be a branch out of the structured block. 10573 // longjmp() and throw() must not violate the entry/exit criteria. 10574 CS->getCapturedDecl()->setNothrow(); 10575 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_update); 10576 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10577 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10578 // 1.2.2 OpenMP Language Terminology 10579 // Structured block - An executable statement with a single entry at the 10580 // top and a single exit at the bottom. 10581 // The point of exit cannot be a branch out of the structured block. 10582 // longjmp() and throw() must not violate the entry/exit criteria. 10583 CS->getCapturedDecl()->setNothrow(); 10584 } 10585 10586 if (!hasClauses(Clauses, OMPC_to, OMPC_from)) { 10587 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required); 10588 return StmtError(); 10589 } 10590 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses, 10591 AStmt); 10592 } 10593 10594 StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses, 10595 Stmt *AStmt, SourceLocation StartLoc, 10596 SourceLocation EndLoc) { 10597 if (!AStmt) 10598 return StmtError(); 10599 10600 auto *CS = cast<CapturedStmt>(AStmt); 10601 // 1.2.2 OpenMP Language Terminology 10602 // Structured block - An executable statement with a single entry at the 10603 // top and a single exit at the bottom. 10604 // The point of exit cannot be a branch out of the structured block. 10605 // longjmp() and throw() must not violate the entry/exit criteria. 10606 CS->getCapturedDecl()->setNothrow(); 10607 10608 setFunctionHasBranchProtectedScope(); 10609 10610 DSAStack->setParentTeamsRegionLoc(StartLoc); 10611 10612 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 10613 } 10614 10615 StmtResult 10616 Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc, 10617 SourceLocation EndLoc, 10618 OpenMPDirectiveKind CancelRegion) { 10619 if (DSAStack->isParentNowaitRegion()) { 10620 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0; 10621 return StmtError(); 10622 } 10623 if (DSAStack->isParentOrderedRegion()) { 10624 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0; 10625 return StmtError(); 10626 } 10627 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc, 10628 CancelRegion); 10629 } 10630 10631 StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses, 10632 SourceLocation StartLoc, 10633 SourceLocation EndLoc, 10634 OpenMPDirectiveKind CancelRegion) { 10635 if (DSAStack->isParentNowaitRegion()) { 10636 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1; 10637 return StmtError(); 10638 } 10639 if (DSAStack->isParentOrderedRegion()) { 10640 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1; 10641 return StmtError(); 10642 } 10643 DSAStack->setParentCancelRegion(/*Cancel=*/true); 10644 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses, 10645 CancelRegion); 10646 } 10647 10648 static bool checkGrainsizeNumTasksClauses(Sema &S, 10649 ArrayRef<OMPClause *> Clauses) { 10650 const OMPClause *PrevClause = nullptr; 10651 bool ErrorFound = false; 10652 for (const OMPClause *C : Clauses) { 10653 if (C->getClauseKind() == OMPC_grainsize || 10654 C->getClauseKind() == OMPC_num_tasks) { 10655 if (!PrevClause) 10656 PrevClause = C; 10657 else if (PrevClause->getClauseKind() != C->getClauseKind()) { 10658 S.Diag(C->getBeginLoc(), diag::err_omp_clauses_mutually_exclusive) 10659 << getOpenMPClauseName(C->getClauseKind()) 10660 << getOpenMPClauseName(PrevClause->getClauseKind()); 10661 S.Diag(PrevClause->getBeginLoc(), diag::note_omp_previous_clause) 10662 << getOpenMPClauseName(PrevClause->getClauseKind()); 10663 ErrorFound = true; 10664 } 10665 } 10666 } 10667 return ErrorFound; 10668 } 10669 10670 static bool checkReductionClauseWithNogroup(Sema &S, 10671 ArrayRef<OMPClause *> Clauses) { 10672 const OMPClause *ReductionClause = nullptr; 10673 const OMPClause *NogroupClause = nullptr; 10674 for (const OMPClause *C : Clauses) { 10675 if (C->getClauseKind() == OMPC_reduction) { 10676 ReductionClause = C; 10677 if (NogroupClause) 10678 break; 10679 continue; 10680 } 10681 if (C->getClauseKind() == OMPC_nogroup) { 10682 NogroupClause = C; 10683 if (ReductionClause) 10684 break; 10685 continue; 10686 } 10687 } 10688 if (ReductionClause && NogroupClause) { 10689 S.Diag(ReductionClause->getBeginLoc(), diag::err_omp_reduction_with_nogroup) 10690 << SourceRange(NogroupClause->getBeginLoc(), 10691 NogroupClause->getEndLoc()); 10692 return true; 10693 } 10694 return false; 10695 } 10696 10697 StmtResult Sema::ActOnOpenMPTaskLoopDirective( 10698 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10699 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10700 if (!AStmt) 10701 return StmtError(); 10702 10703 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10704 OMPLoopDirective::HelperExprs B; 10705 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 10706 // define the nested loops number. 10707 unsigned NestedLoopCount = 10708 checkOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses), 10709 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 10710 VarsWithImplicitDSA, B); 10711 if (NestedLoopCount == 0) 10712 return StmtError(); 10713 10714 assert((CurContext->isDependentContext() || B.builtAll()) && 10715 "omp for loop exprs were not built"); 10716 10717 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10718 // The grainsize clause and num_tasks clause are mutually exclusive and may 10719 // not appear on the same taskloop directive. 10720 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 10721 return StmtError(); 10722 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10723 // If a reduction clause is present on the taskloop directive, the nogroup 10724 // clause must not be specified. 10725 if (checkReductionClauseWithNogroup(*this, Clauses)) 10726 return StmtError(); 10727 10728 setFunctionHasBranchProtectedScope(); 10729 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc, 10730 NestedLoopCount, Clauses, AStmt, B, 10731 DSAStack->isCancelRegion()); 10732 } 10733 10734 StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective( 10735 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10736 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10737 if (!AStmt) 10738 return StmtError(); 10739 10740 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10741 OMPLoopDirective::HelperExprs B; 10742 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 10743 // define the nested loops number. 10744 unsigned NestedLoopCount = 10745 checkOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses), 10746 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 10747 VarsWithImplicitDSA, B); 10748 if (NestedLoopCount == 0) 10749 return StmtError(); 10750 10751 assert((CurContext->isDependentContext() || B.builtAll()) && 10752 "omp for loop exprs were not built"); 10753 10754 if (!CurContext->isDependentContext()) { 10755 // Finalize the clauses that need pre-built expressions for CodeGen. 10756 for (OMPClause *C : Clauses) { 10757 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 10758 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 10759 B.NumIterations, *this, CurScope, 10760 DSAStack)) 10761 return StmtError(); 10762 } 10763 } 10764 10765 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10766 // The grainsize clause and num_tasks clause are mutually exclusive and may 10767 // not appear on the same taskloop directive. 10768 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 10769 return StmtError(); 10770 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10771 // If a reduction clause is present on the taskloop directive, the nogroup 10772 // clause must not be specified. 10773 if (checkReductionClauseWithNogroup(*this, Clauses)) 10774 return StmtError(); 10775 if (checkSimdlenSafelenSpecified(*this, Clauses)) 10776 return StmtError(); 10777 10778 setFunctionHasBranchProtectedScope(); 10779 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc, 10780 NestedLoopCount, Clauses, AStmt, B); 10781 } 10782 10783 StmtResult Sema::ActOnOpenMPMasterTaskLoopDirective( 10784 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10785 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10786 if (!AStmt) 10787 return StmtError(); 10788 10789 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10790 OMPLoopDirective::HelperExprs B; 10791 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 10792 // define the nested loops number. 10793 unsigned NestedLoopCount = 10794 checkOpenMPLoop(OMPD_master_taskloop, getCollapseNumberExpr(Clauses), 10795 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 10796 VarsWithImplicitDSA, B); 10797 if (NestedLoopCount == 0) 10798 return StmtError(); 10799 10800 assert((CurContext->isDependentContext() || B.builtAll()) && 10801 "omp for loop exprs were not built"); 10802 10803 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10804 // The grainsize clause and num_tasks clause are mutually exclusive and may 10805 // not appear on the same taskloop directive. 10806 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 10807 return StmtError(); 10808 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10809 // If a reduction clause is present on the taskloop directive, the nogroup 10810 // clause must not be specified. 10811 if (checkReductionClauseWithNogroup(*this, Clauses)) 10812 return StmtError(); 10813 10814 setFunctionHasBranchProtectedScope(); 10815 return OMPMasterTaskLoopDirective::Create(Context, StartLoc, EndLoc, 10816 NestedLoopCount, Clauses, AStmt, B, 10817 DSAStack->isCancelRegion()); 10818 } 10819 10820 StmtResult Sema::ActOnOpenMPMasterTaskLoopSimdDirective( 10821 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10822 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10823 if (!AStmt) 10824 return StmtError(); 10825 10826 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10827 OMPLoopDirective::HelperExprs B; 10828 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 10829 // define the nested loops number. 10830 unsigned NestedLoopCount = 10831 checkOpenMPLoop(OMPD_master_taskloop_simd, getCollapseNumberExpr(Clauses), 10832 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 10833 VarsWithImplicitDSA, B); 10834 if (NestedLoopCount == 0) 10835 return StmtError(); 10836 10837 assert((CurContext->isDependentContext() || B.builtAll()) && 10838 "omp for loop exprs were not built"); 10839 10840 if (!CurContext->isDependentContext()) { 10841 // Finalize the clauses that need pre-built expressions for CodeGen. 10842 for (OMPClause *C : Clauses) { 10843 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 10844 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 10845 B.NumIterations, *this, CurScope, 10846 DSAStack)) 10847 return StmtError(); 10848 } 10849 } 10850 10851 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10852 // The grainsize clause and num_tasks clause are mutually exclusive and may 10853 // not appear on the same taskloop directive. 10854 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 10855 return StmtError(); 10856 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10857 // If a reduction clause is present on the taskloop directive, the nogroup 10858 // clause must not be specified. 10859 if (checkReductionClauseWithNogroup(*this, Clauses)) 10860 return StmtError(); 10861 if (checkSimdlenSafelenSpecified(*this, Clauses)) 10862 return StmtError(); 10863 10864 setFunctionHasBranchProtectedScope(); 10865 return OMPMasterTaskLoopSimdDirective::Create( 10866 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 10867 } 10868 10869 StmtResult Sema::ActOnOpenMPParallelMasterTaskLoopDirective( 10870 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10871 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10872 if (!AStmt) 10873 return StmtError(); 10874 10875 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10876 auto *CS = cast<CapturedStmt>(AStmt); 10877 // 1.2.2 OpenMP Language Terminology 10878 // Structured block - An executable statement with a single entry at the 10879 // top and a single exit at the bottom. 10880 // The point of exit cannot be a branch out of the structured block. 10881 // longjmp() and throw() must not violate the entry/exit criteria. 10882 CS->getCapturedDecl()->setNothrow(); 10883 for (int ThisCaptureLevel = 10884 getOpenMPCaptureLevels(OMPD_parallel_master_taskloop); 10885 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10886 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10887 // 1.2.2 OpenMP Language Terminology 10888 // Structured block - An executable statement with a single entry at the 10889 // top and a single exit at the bottom. 10890 // The point of exit cannot be a branch out of the structured block. 10891 // longjmp() and throw() must not violate the entry/exit criteria. 10892 CS->getCapturedDecl()->setNothrow(); 10893 } 10894 10895 OMPLoopDirective::HelperExprs B; 10896 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 10897 // define the nested loops number. 10898 unsigned NestedLoopCount = checkOpenMPLoop( 10899 OMPD_parallel_master_taskloop, getCollapseNumberExpr(Clauses), 10900 /*OrderedLoopCountExpr=*/nullptr, CS, *this, *DSAStack, 10901 VarsWithImplicitDSA, B); 10902 if (NestedLoopCount == 0) 10903 return StmtError(); 10904 10905 assert((CurContext->isDependentContext() || B.builtAll()) && 10906 "omp for loop exprs were not built"); 10907 10908 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10909 // The grainsize clause and num_tasks clause are mutually exclusive and may 10910 // not appear on the same taskloop directive. 10911 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 10912 return StmtError(); 10913 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10914 // If a reduction clause is present on the taskloop directive, the nogroup 10915 // clause must not be specified. 10916 if (checkReductionClauseWithNogroup(*this, Clauses)) 10917 return StmtError(); 10918 10919 setFunctionHasBranchProtectedScope(); 10920 return OMPParallelMasterTaskLoopDirective::Create( 10921 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 10922 DSAStack->isCancelRegion()); 10923 } 10924 10925 StmtResult Sema::ActOnOpenMPParallelMasterTaskLoopSimdDirective( 10926 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10927 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10928 if (!AStmt) 10929 return StmtError(); 10930 10931 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10932 auto *CS = cast<CapturedStmt>(AStmt); 10933 // 1.2.2 OpenMP Language Terminology 10934 // Structured block - An executable statement with a single entry at the 10935 // top and a single exit at the bottom. 10936 // The point of exit cannot be a branch out of the structured block. 10937 // longjmp() and throw() must not violate the entry/exit criteria. 10938 CS->getCapturedDecl()->setNothrow(); 10939 for (int ThisCaptureLevel = 10940 getOpenMPCaptureLevels(OMPD_parallel_master_taskloop_simd); 10941 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10942 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10943 // 1.2.2 OpenMP Language Terminology 10944 // Structured block - An executable statement with a single entry at the 10945 // top and a single exit at the bottom. 10946 // The point of exit cannot be a branch out of the structured block. 10947 // longjmp() and throw() must not violate the entry/exit criteria. 10948 CS->getCapturedDecl()->setNothrow(); 10949 } 10950 10951 OMPLoopDirective::HelperExprs B; 10952 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 10953 // define the nested loops number. 10954 unsigned NestedLoopCount = checkOpenMPLoop( 10955 OMPD_parallel_master_taskloop_simd, getCollapseNumberExpr(Clauses), 10956 /*OrderedLoopCountExpr=*/nullptr, CS, *this, *DSAStack, 10957 VarsWithImplicitDSA, B); 10958 if (NestedLoopCount == 0) 10959 return StmtError(); 10960 10961 assert((CurContext->isDependentContext() || B.builtAll()) && 10962 "omp for loop exprs were not built"); 10963 10964 if (!CurContext->isDependentContext()) { 10965 // Finalize the clauses that need pre-built expressions for CodeGen. 10966 for (OMPClause *C : Clauses) { 10967 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 10968 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 10969 B.NumIterations, *this, CurScope, 10970 DSAStack)) 10971 return StmtError(); 10972 } 10973 } 10974 10975 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10976 // The grainsize clause and num_tasks clause are mutually exclusive and may 10977 // not appear on the same taskloop directive. 10978 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 10979 return StmtError(); 10980 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10981 // If a reduction clause is present on the taskloop directive, the nogroup 10982 // clause must not be specified. 10983 if (checkReductionClauseWithNogroup(*this, Clauses)) 10984 return StmtError(); 10985 if (checkSimdlenSafelenSpecified(*this, Clauses)) 10986 return StmtError(); 10987 10988 setFunctionHasBranchProtectedScope(); 10989 return OMPParallelMasterTaskLoopSimdDirective::Create( 10990 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 10991 } 10992 10993 StmtResult Sema::ActOnOpenMPDistributeDirective( 10994 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10995 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10996 if (!AStmt) 10997 return StmtError(); 10998 10999 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11000 OMPLoopDirective::HelperExprs B; 11001 // In presence of clause 'collapse' with number of loops, it will 11002 // define the nested loops number. 11003 unsigned NestedLoopCount = 11004 checkOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses), 11005 nullptr /*ordered not a clause on distribute*/, AStmt, 11006 *this, *DSAStack, VarsWithImplicitDSA, B); 11007 if (NestedLoopCount == 0) 11008 return StmtError(); 11009 11010 assert((CurContext->isDependentContext() || B.builtAll()) && 11011 "omp for loop exprs were not built"); 11012 11013 setFunctionHasBranchProtectedScope(); 11014 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc, 11015 NestedLoopCount, Clauses, AStmt, B); 11016 } 11017 11018 StmtResult Sema::ActOnOpenMPDistributeParallelForDirective( 11019 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11020 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11021 if (!AStmt) 11022 return StmtError(); 11023 11024 auto *CS = cast<CapturedStmt>(AStmt); 11025 // 1.2.2 OpenMP Language Terminology 11026 // Structured block - An executable statement with a single entry at the 11027 // top and a single exit at the bottom. 11028 // The point of exit cannot be a branch out of the structured block. 11029 // longjmp() and throw() must not violate the entry/exit criteria. 11030 CS->getCapturedDecl()->setNothrow(); 11031 for (int ThisCaptureLevel = 11032 getOpenMPCaptureLevels(OMPD_distribute_parallel_for); 11033 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11034 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11035 // 1.2.2 OpenMP Language Terminology 11036 // Structured block - An executable statement with a single entry at the 11037 // top and a single exit at the bottom. 11038 // The point of exit cannot be a branch out of the structured block. 11039 // longjmp() and throw() must not violate the entry/exit criteria. 11040 CS->getCapturedDecl()->setNothrow(); 11041 } 11042 11043 OMPLoopDirective::HelperExprs B; 11044 // In presence of clause 'collapse' with number of loops, it will 11045 // define the nested loops number. 11046 unsigned NestedLoopCount = checkOpenMPLoop( 11047 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses), 11048 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 11049 VarsWithImplicitDSA, B); 11050 if (NestedLoopCount == 0) 11051 return StmtError(); 11052 11053 assert((CurContext->isDependentContext() || B.builtAll()) && 11054 "omp for loop exprs were not built"); 11055 11056 setFunctionHasBranchProtectedScope(); 11057 return OMPDistributeParallelForDirective::Create( 11058 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 11059 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 11060 } 11061 11062 StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective( 11063 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11064 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11065 if (!AStmt) 11066 return StmtError(); 11067 11068 auto *CS = cast<CapturedStmt>(AStmt); 11069 // 1.2.2 OpenMP Language Terminology 11070 // Structured block - An executable statement with a single entry at the 11071 // top and a single exit at the bottom. 11072 // The point of exit cannot be a branch out of the structured block. 11073 // longjmp() and throw() must not violate the entry/exit criteria. 11074 CS->getCapturedDecl()->setNothrow(); 11075 for (int ThisCaptureLevel = 11076 getOpenMPCaptureLevels(OMPD_distribute_parallel_for_simd); 11077 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11078 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11079 // 1.2.2 OpenMP Language Terminology 11080 // Structured block - An executable statement with a single entry at the 11081 // top and a single exit at the bottom. 11082 // The point of exit cannot be a branch out of the structured block. 11083 // longjmp() and throw() must not violate the entry/exit criteria. 11084 CS->getCapturedDecl()->setNothrow(); 11085 } 11086 11087 OMPLoopDirective::HelperExprs B; 11088 // In presence of clause 'collapse' with number of loops, it will 11089 // define the nested loops number. 11090 unsigned NestedLoopCount = checkOpenMPLoop( 11091 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses), 11092 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 11093 VarsWithImplicitDSA, B); 11094 if (NestedLoopCount == 0) 11095 return StmtError(); 11096 11097 assert((CurContext->isDependentContext() || B.builtAll()) && 11098 "omp for loop exprs were not built"); 11099 11100 if (!CurContext->isDependentContext()) { 11101 // Finalize the clauses that need pre-built expressions for CodeGen. 11102 for (OMPClause *C : Clauses) { 11103 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11104 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11105 B.NumIterations, *this, CurScope, 11106 DSAStack)) 11107 return StmtError(); 11108 } 11109 } 11110 11111 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11112 return StmtError(); 11113 11114 setFunctionHasBranchProtectedScope(); 11115 return OMPDistributeParallelForSimdDirective::Create( 11116 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 11117 } 11118 11119 StmtResult Sema::ActOnOpenMPDistributeSimdDirective( 11120 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11121 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11122 if (!AStmt) 11123 return StmtError(); 11124 11125 auto *CS = cast<CapturedStmt>(AStmt); 11126 // 1.2.2 OpenMP Language Terminology 11127 // Structured block - An executable statement with a single entry at the 11128 // top and a single exit at the bottom. 11129 // The point of exit cannot be a branch out of the structured block. 11130 // longjmp() and throw() must not violate the entry/exit criteria. 11131 CS->getCapturedDecl()->setNothrow(); 11132 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_distribute_simd); 11133 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11134 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11135 // 1.2.2 OpenMP Language Terminology 11136 // Structured block - An executable statement with a single entry at the 11137 // top and a single exit at the bottom. 11138 // The point of exit cannot be a branch out of the structured block. 11139 // longjmp() and throw() must not violate the entry/exit criteria. 11140 CS->getCapturedDecl()->setNothrow(); 11141 } 11142 11143 OMPLoopDirective::HelperExprs B; 11144 // In presence of clause 'collapse' with number of loops, it will 11145 // define the nested loops number. 11146 unsigned NestedLoopCount = 11147 checkOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses), 11148 nullptr /*ordered not a clause on distribute*/, CS, *this, 11149 *DSAStack, VarsWithImplicitDSA, B); 11150 if (NestedLoopCount == 0) 11151 return StmtError(); 11152 11153 assert((CurContext->isDependentContext() || B.builtAll()) && 11154 "omp for loop exprs were not built"); 11155 11156 if (!CurContext->isDependentContext()) { 11157 // Finalize the clauses that need pre-built expressions for CodeGen. 11158 for (OMPClause *C : Clauses) { 11159 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11160 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11161 B.NumIterations, *this, CurScope, 11162 DSAStack)) 11163 return StmtError(); 11164 } 11165 } 11166 11167 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11168 return StmtError(); 11169 11170 setFunctionHasBranchProtectedScope(); 11171 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc, 11172 NestedLoopCount, Clauses, AStmt, B); 11173 } 11174 11175 StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective( 11176 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11177 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11178 if (!AStmt) 11179 return StmtError(); 11180 11181 auto *CS = cast<CapturedStmt>(AStmt); 11182 // 1.2.2 OpenMP Language Terminology 11183 // Structured block - An executable statement with a single entry at the 11184 // top and a single exit at the bottom. 11185 // The point of exit cannot be a branch out of the structured block. 11186 // longjmp() and throw() must not violate the entry/exit criteria. 11187 CS->getCapturedDecl()->setNothrow(); 11188 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for); 11189 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11190 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11191 // 1.2.2 OpenMP Language Terminology 11192 // Structured block - An executable statement with a single entry at the 11193 // top and a single exit at the bottom. 11194 // The point of exit cannot be a branch out of the structured block. 11195 // longjmp() and throw() must not violate the entry/exit criteria. 11196 CS->getCapturedDecl()->setNothrow(); 11197 } 11198 11199 OMPLoopDirective::HelperExprs B; 11200 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11201 // define the nested loops number. 11202 unsigned NestedLoopCount = checkOpenMPLoop( 11203 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses), 11204 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 11205 VarsWithImplicitDSA, B); 11206 if (NestedLoopCount == 0) 11207 return StmtError(); 11208 11209 assert((CurContext->isDependentContext() || B.builtAll()) && 11210 "omp target parallel for simd loop exprs were not built"); 11211 11212 if (!CurContext->isDependentContext()) { 11213 // Finalize the clauses that need pre-built expressions for CodeGen. 11214 for (OMPClause *C : Clauses) { 11215 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11216 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11217 B.NumIterations, *this, CurScope, 11218 DSAStack)) 11219 return StmtError(); 11220 } 11221 } 11222 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11223 return StmtError(); 11224 11225 setFunctionHasBranchProtectedScope(); 11226 return OMPTargetParallelForSimdDirective::Create( 11227 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 11228 } 11229 11230 StmtResult Sema::ActOnOpenMPTargetSimdDirective( 11231 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11232 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11233 if (!AStmt) 11234 return StmtError(); 11235 11236 auto *CS = cast<CapturedStmt>(AStmt); 11237 // 1.2.2 OpenMP Language Terminology 11238 // Structured block - An executable statement with a single entry at the 11239 // top and a single exit at the bottom. 11240 // The point of exit cannot be a branch out of the structured block. 11241 // longjmp() and throw() must not violate the entry/exit criteria. 11242 CS->getCapturedDecl()->setNothrow(); 11243 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_simd); 11244 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11245 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11246 // 1.2.2 OpenMP Language Terminology 11247 // Structured block - An executable statement with a single entry at the 11248 // top and a single exit at the bottom. 11249 // The point of exit cannot be a branch out of the structured block. 11250 // longjmp() and throw() must not violate the entry/exit criteria. 11251 CS->getCapturedDecl()->setNothrow(); 11252 } 11253 11254 OMPLoopDirective::HelperExprs B; 11255 // In presence of clause 'collapse' with number of loops, it will define the 11256 // nested loops number. 11257 unsigned NestedLoopCount = 11258 checkOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses), 11259 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 11260 VarsWithImplicitDSA, B); 11261 if (NestedLoopCount == 0) 11262 return StmtError(); 11263 11264 assert((CurContext->isDependentContext() || B.builtAll()) && 11265 "omp target simd loop exprs were not built"); 11266 11267 if (!CurContext->isDependentContext()) { 11268 // Finalize the clauses that need pre-built expressions for CodeGen. 11269 for (OMPClause *C : Clauses) { 11270 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11271 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11272 B.NumIterations, *this, CurScope, 11273 DSAStack)) 11274 return StmtError(); 11275 } 11276 } 11277 11278 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11279 return StmtError(); 11280 11281 setFunctionHasBranchProtectedScope(); 11282 return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc, 11283 NestedLoopCount, Clauses, AStmt, B); 11284 } 11285 11286 StmtResult Sema::ActOnOpenMPTeamsDistributeDirective( 11287 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11288 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11289 if (!AStmt) 11290 return StmtError(); 11291 11292 auto *CS = cast<CapturedStmt>(AStmt); 11293 // 1.2.2 OpenMP Language Terminology 11294 // Structured block - An executable statement with a single entry at the 11295 // top and a single exit at the bottom. 11296 // The point of exit cannot be a branch out of the structured block. 11297 // longjmp() and throw() must not violate the entry/exit criteria. 11298 CS->getCapturedDecl()->setNothrow(); 11299 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_teams_distribute); 11300 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11301 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11302 // 1.2.2 OpenMP Language Terminology 11303 // Structured block - An executable statement with a single entry at the 11304 // top and a single exit at the bottom. 11305 // The point of exit cannot be a branch out of the structured block. 11306 // longjmp() and throw() must not violate the entry/exit criteria. 11307 CS->getCapturedDecl()->setNothrow(); 11308 } 11309 11310 OMPLoopDirective::HelperExprs B; 11311 // In presence of clause 'collapse' with number of loops, it will 11312 // define the nested loops number. 11313 unsigned NestedLoopCount = 11314 checkOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses), 11315 nullptr /*ordered not a clause on distribute*/, CS, *this, 11316 *DSAStack, VarsWithImplicitDSA, B); 11317 if (NestedLoopCount == 0) 11318 return StmtError(); 11319 11320 assert((CurContext->isDependentContext() || B.builtAll()) && 11321 "omp teams distribute loop exprs were not built"); 11322 11323 setFunctionHasBranchProtectedScope(); 11324 11325 DSAStack->setParentTeamsRegionLoc(StartLoc); 11326 11327 return OMPTeamsDistributeDirective::Create( 11328 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 11329 } 11330 11331 StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective( 11332 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11333 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11334 if (!AStmt) 11335 return StmtError(); 11336 11337 auto *CS = cast<CapturedStmt>(AStmt); 11338 // 1.2.2 OpenMP Language Terminology 11339 // Structured block - An executable statement with a single entry at the 11340 // top and a single exit at the bottom. 11341 // The point of exit cannot be a branch out of the structured block. 11342 // longjmp() and throw() must not violate the entry/exit criteria. 11343 CS->getCapturedDecl()->setNothrow(); 11344 for (int ThisCaptureLevel = 11345 getOpenMPCaptureLevels(OMPD_teams_distribute_simd); 11346 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11347 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11348 // 1.2.2 OpenMP Language Terminology 11349 // Structured block - An executable statement with a single entry at the 11350 // top and a single exit at the bottom. 11351 // The point of exit cannot be a branch out of the structured block. 11352 // longjmp() and throw() must not violate the entry/exit criteria. 11353 CS->getCapturedDecl()->setNothrow(); 11354 } 11355 11356 OMPLoopDirective::HelperExprs B; 11357 // In presence of clause 'collapse' with number of loops, it will 11358 // define the nested loops number. 11359 unsigned NestedLoopCount = checkOpenMPLoop( 11360 OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses), 11361 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 11362 VarsWithImplicitDSA, B); 11363 11364 if (NestedLoopCount == 0) 11365 return StmtError(); 11366 11367 assert((CurContext->isDependentContext() || B.builtAll()) && 11368 "omp teams distribute simd loop exprs were not built"); 11369 11370 if (!CurContext->isDependentContext()) { 11371 // Finalize the clauses that need pre-built expressions for CodeGen. 11372 for (OMPClause *C : Clauses) { 11373 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11374 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11375 B.NumIterations, *this, CurScope, 11376 DSAStack)) 11377 return StmtError(); 11378 } 11379 } 11380 11381 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11382 return StmtError(); 11383 11384 setFunctionHasBranchProtectedScope(); 11385 11386 DSAStack->setParentTeamsRegionLoc(StartLoc); 11387 11388 return OMPTeamsDistributeSimdDirective::Create( 11389 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 11390 } 11391 11392 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective( 11393 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11394 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11395 if (!AStmt) 11396 return StmtError(); 11397 11398 auto *CS = cast<CapturedStmt>(AStmt); 11399 // 1.2.2 OpenMP Language Terminology 11400 // Structured block - An executable statement with a single entry at the 11401 // top and a single exit at the bottom. 11402 // The point of exit cannot be a branch out of the structured block. 11403 // longjmp() and throw() must not violate the entry/exit criteria. 11404 CS->getCapturedDecl()->setNothrow(); 11405 11406 for (int ThisCaptureLevel = 11407 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for_simd); 11408 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11409 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11410 // 1.2.2 OpenMP Language Terminology 11411 // Structured block - An executable statement with a single entry at the 11412 // top and a single exit at the bottom. 11413 // The point of exit cannot be a branch out of the structured block. 11414 // longjmp() and throw() must not violate the entry/exit criteria. 11415 CS->getCapturedDecl()->setNothrow(); 11416 } 11417 11418 OMPLoopDirective::HelperExprs B; 11419 // In presence of clause 'collapse' with number of loops, it will 11420 // define the nested loops number. 11421 unsigned NestedLoopCount = checkOpenMPLoop( 11422 OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses), 11423 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 11424 VarsWithImplicitDSA, B); 11425 11426 if (NestedLoopCount == 0) 11427 return StmtError(); 11428 11429 assert((CurContext->isDependentContext() || B.builtAll()) && 11430 "omp for loop exprs were not built"); 11431 11432 if (!CurContext->isDependentContext()) { 11433 // Finalize the clauses that need pre-built expressions for CodeGen. 11434 for (OMPClause *C : Clauses) { 11435 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11436 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11437 B.NumIterations, *this, CurScope, 11438 DSAStack)) 11439 return StmtError(); 11440 } 11441 } 11442 11443 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11444 return StmtError(); 11445 11446 setFunctionHasBranchProtectedScope(); 11447 11448 DSAStack->setParentTeamsRegionLoc(StartLoc); 11449 11450 return OMPTeamsDistributeParallelForSimdDirective::Create( 11451 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 11452 } 11453 11454 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective( 11455 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11456 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11457 if (!AStmt) 11458 return StmtError(); 11459 11460 auto *CS = cast<CapturedStmt>(AStmt); 11461 // 1.2.2 OpenMP Language Terminology 11462 // Structured block - An executable statement with a single entry at the 11463 // top and a single exit at the bottom. 11464 // The point of exit cannot be a branch out of the structured block. 11465 // longjmp() and throw() must not violate the entry/exit criteria. 11466 CS->getCapturedDecl()->setNothrow(); 11467 11468 for (int ThisCaptureLevel = 11469 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for); 11470 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11471 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11472 // 1.2.2 OpenMP Language Terminology 11473 // Structured block - An executable statement with a single entry at the 11474 // top and a single exit at the bottom. 11475 // The point of exit cannot be a branch out of the structured block. 11476 // longjmp() and throw() must not violate the entry/exit criteria. 11477 CS->getCapturedDecl()->setNothrow(); 11478 } 11479 11480 OMPLoopDirective::HelperExprs B; 11481 // In presence of clause 'collapse' with number of loops, it will 11482 // define the nested loops number. 11483 unsigned NestedLoopCount = checkOpenMPLoop( 11484 OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses), 11485 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 11486 VarsWithImplicitDSA, B); 11487 11488 if (NestedLoopCount == 0) 11489 return StmtError(); 11490 11491 assert((CurContext->isDependentContext() || B.builtAll()) && 11492 "omp for loop exprs were not built"); 11493 11494 setFunctionHasBranchProtectedScope(); 11495 11496 DSAStack->setParentTeamsRegionLoc(StartLoc); 11497 11498 return OMPTeamsDistributeParallelForDirective::Create( 11499 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 11500 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 11501 } 11502 11503 StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses, 11504 Stmt *AStmt, 11505 SourceLocation StartLoc, 11506 SourceLocation EndLoc) { 11507 if (!AStmt) 11508 return StmtError(); 11509 11510 auto *CS = cast<CapturedStmt>(AStmt); 11511 // 1.2.2 OpenMP Language Terminology 11512 // Structured block - An executable statement with a single entry at the 11513 // top and a single exit at the bottom. 11514 // The point of exit cannot be a branch out of the structured block. 11515 // longjmp() and throw() must not violate the entry/exit criteria. 11516 CS->getCapturedDecl()->setNothrow(); 11517 11518 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_teams); 11519 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11520 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11521 // 1.2.2 OpenMP Language Terminology 11522 // Structured block - An executable statement with a single entry at the 11523 // top and a single exit at the bottom. 11524 // The point of exit cannot be a branch out of the structured block. 11525 // longjmp() and throw() must not violate the entry/exit criteria. 11526 CS->getCapturedDecl()->setNothrow(); 11527 } 11528 setFunctionHasBranchProtectedScope(); 11529 11530 return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, 11531 AStmt); 11532 } 11533 11534 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective( 11535 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11536 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11537 if (!AStmt) 11538 return StmtError(); 11539 11540 auto *CS = cast<CapturedStmt>(AStmt); 11541 // 1.2.2 OpenMP Language Terminology 11542 // Structured block - An executable statement with a single entry at the 11543 // top and a single exit at the bottom. 11544 // The point of exit cannot be a branch out of the structured block. 11545 // longjmp() and throw() must not violate the entry/exit criteria. 11546 CS->getCapturedDecl()->setNothrow(); 11547 for (int ThisCaptureLevel = 11548 getOpenMPCaptureLevels(OMPD_target_teams_distribute); 11549 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11550 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11551 // 1.2.2 OpenMP Language Terminology 11552 // Structured block - An executable statement with a single entry at the 11553 // top and a single exit at the bottom. 11554 // The point of exit cannot be a branch out of the structured block. 11555 // longjmp() and throw() must not violate the entry/exit criteria. 11556 CS->getCapturedDecl()->setNothrow(); 11557 } 11558 11559 OMPLoopDirective::HelperExprs B; 11560 // In presence of clause 'collapse' with number of loops, it will 11561 // define the nested loops number. 11562 unsigned NestedLoopCount = checkOpenMPLoop( 11563 OMPD_target_teams_distribute, getCollapseNumberExpr(Clauses), 11564 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 11565 VarsWithImplicitDSA, B); 11566 if (NestedLoopCount == 0) 11567 return StmtError(); 11568 11569 assert((CurContext->isDependentContext() || B.builtAll()) && 11570 "omp target teams distribute loop exprs were not built"); 11571 11572 setFunctionHasBranchProtectedScope(); 11573 return OMPTargetTeamsDistributeDirective::Create( 11574 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 11575 } 11576 11577 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective( 11578 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11579 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11580 if (!AStmt) 11581 return StmtError(); 11582 11583 auto *CS = cast<CapturedStmt>(AStmt); 11584 // 1.2.2 OpenMP Language Terminology 11585 // Structured block - An executable statement with a single entry at the 11586 // top and a single exit at the bottom. 11587 // The point of exit cannot be a branch out of the structured block. 11588 // longjmp() and throw() must not violate the entry/exit criteria. 11589 CS->getCapturedDecl()->setNothrow(); 11590 for (int ThisCaptureLevel = 11591 getOpenMPCaptureLevels(OMPD_target_teams_distribute_parallel_for); 11592 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11593 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11594 // 1.2.2 OpenMP Language Terminology 11595 // Structured block - An executable statement with a single entry at the 11596 // top and a single exit at the bottom. 11597 // The point of exit cannot be a branch out of the structured block. 11598 // longjmp() and throw() must not violate the entry/exit criteria. 11599 CS->getCapturedDecl()->setNothrow(); 11600 } 11601 11602 OMPLoopDirective::HelperExprs B; 11603 // In presence of clause 'collapse' with number of loops, it will 11604 // define the nested loops number. 11605 unsigned NestedLoopCount = checkOpenMPLoop( 11606 OMPD_target_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses), 11607 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 11608 VarsWithImplicitDSA, B); 11609 if (NestedLoopCount == 0) 11610 return StmtError(); 11611 11612 assert((CurContext->isDependentContext() || B.builtAll()) && 11613 "omp target teams distribute parallel for loop exprs were not built"); 11614 11615 if (!CurContext->isDependentContext()) { 11616 // Finalize the clauses that need pre-built expressions for CodeGen. 11617 for (OMPClause *C : Clauses) { 11618 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11619 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11620 B.NumIterations, *this, CurScope, 11621 DSAStack)) 11622 return StmtError(); 11623 } 11624 } 11625 11626 setFunctionHasBranchProtectedScope(); 11627 return OMPTargetTeamsDistributeParallelForDirective::Create( 11628 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 11629 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 11630 } 11631 11632 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective( 11633 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11634 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11635 if (!AStmt) 11636 return StmtError(); 11637 11638 auto *CS = cast<CapturedStmt>(AStmt); 11639 // 1.2.2 OpenMP Language Terminology 11640 // Structured block - An executable statement with a single entry at the 11641 // top and a single exit at the bottom. 11642 // The point of exit cannot be a branch out of the structured block. 11643 // longjmp() and throw() must not violate the entry/exit criteria. 11644 CS->getCapturedDecl()->setNothrow(); 11645 for (int ThisCaptureLevel = getOpenMPCaptureLevels( 11646 OMPD_target_teams_distribute_parallel_for_simd); 11647 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11648 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11649 // 1.2.2 OpenMP Language Terminology 11650 // Structured block - An executable statement with a single entry at the 11651 // top and a single exit at the bottom. 11652 // The point of exit cannot be a branch out of the structured block. 11653 // longjmp() and throw() must not violate the entry/exit criteria. 11654 CS->getCapturedDecl()->setNothrow(); 11655 } 11656 11657 OMPLoopDirective::HelperExprs B; 11658 // In presence of clause 'collapse' with number of loops, it will 11659 // define the nested loops number. 11660 unsigned NestedLoopCount = 11661 checkOpenMPLoop(OMPD_target_teams_distribute_parallel_for_simd, 11662 getCollapseNumberExpr(Clauses), 11663 nullptr /*ordered not a clause on distribute*/, CS, *this, 11664 *DSAStack, VarsWithImplicitDSA, B); 11665 if (NestedLoopCount == 0) 11666 return StmtError(); 11667 11668 assert((CurContext->isDependentContext() || B.builtAll()) && 11669 "omp target teams distribute parallel for simd loop exprs were not " 11670 "built"); 11671 11672 if (!CurContext->isDependentContext()) { 11673 // Finalize the clauses that need pre-built expressions for CodeGen. 11674 for (OMPClause *C : Clauses) { 11675 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11676 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11677 B.NumIterations, *this, CurScope, 11678 DSAStack)) 11679 return StmtError(); 11680 } 11681 } 11682 11683 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11684 return StmtError(); 11685 11686 setFunctionHasBranchProtectedScope(); 11687 return OMPTargetTeamsDistributeParallelForSimdDirective::Create( 11688 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 11689 } 11690 11691 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective( 11692 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11693 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11694 if (!AStmt) 11695 return StmtError(); 11696 11697 auto *CS = cast<CapturedStmt>(AStmt); 11698 // 1.2.2 OpenMP Language Terminology 11699 // Structured block - An executable statement with a single entry at the 11700 // top and a single exit at the bottom. 11701 // The point of exit cannot be a branch out of the structured block. 11702 // longjmp() and throw() must not violate the entry/exit criteria. 11703 CS->getCapturedDecl()->setNothrow(); 11704 for (int ThisCaptureLevel = 11705 getOpenMPCaptureLevels(OMPD_target_teams_distribute_simd); 11706 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11707 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11708 // 1.2.2 OpenMP Language Terminology 11709 // Structured block - An executable statement with a single entry at the 11710 // top and a single exit at the bottom. 11711 // The point of exit cannot be a branch out of the structured block. 11712 // longjmp() and throw() must not violate the entry/exit criteria. 11713 CS->getCapturedDecl()->setNothrow(); 11714 } 11715 11716 OMPLoopDirective::HelperExprs B; 11717 // In presence of clause 'collapse' with number of loops, it will 11718 // define the nested loops number. 11719 unsigned NestedLoopCount = checkOpenMPLoop( 11720 OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses), 11721 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 11722 VarsWithImplicitDSA, B); 11723 if (NestedLoopCount == 0) 11724 return StmtError(); 11725 11726 assert((CurContext->isDependentContext() || B.builtAll()) && 11727 "omp target teams distribute simd loop exprs were not built"); 11728 11729 if (!CurContext->isDependentContext()) { 11730 // Finalize the clauses that need pre-built expressions for CodeGen. 11731 for (OMPClause *C : Clauses) { 11732 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11733 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11734 B.NumIterations, *this, CurScope, 11735 DSAStack)) 11736 return StmtError(); 11737 } 11738 } 11739 11740 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11741 return StmtError(); 11742 11743 setFunctionHasBranchProtectedScope(); 11744 return OMPTargetTeamsDistributeSimdDirective::Create( 11745 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 11746 } 11747 11748 OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr, 11749 SourceLocation StartLoc, 11750 SourceLocation LParenLoc, 11751 SourceLocation EndLoc) { 11752 OMPClause *Res = nullptr; 11753 switch (Kind) { 11754 case OMPC_final: 11755 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc); 11756 break; 11757 case OMPC_num_threads: 11758 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc); 11759 break; 11760 case OMPC_safelen: 11761 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc); 11762 break; 11763 case OMPC_simdlen: 11764 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc); 11765 break; 11766 case OMPC_allocator: 11767 Res = ActOnOpenMPAllocatorClause(Expr, StartLoc, LParenLoc, EndLoc); 11768 break; 11769 case OMPC_collapse: 11770 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc); 11771 break; 11772 case OMPC_ordered: 11773 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr); 11774 break; 11775 case OMPC_num_teams: 11776 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc); 11777 break; 11778 case OMPC_thread_limit: 11779 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc); 11780 break; 11781 case OMPC_priority: 11782 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc); 11783 break; 11784 case OMPC_grainsize: 11785 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc); 11786 break; 11787 case OMPC_num_tasks: 11788 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc); 11789 break; 11790 case OMPC_hint: 11791 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc); 11792 break; 11793 case OMPC_depobj: 11794 Res = ActOnOpenMPDepobjClause(Expr, StartLoc, LParenLoc, EndLoc); 11795 break; 11796 case OMPC_detach: 11797 Res = ActOnOpenMPDetachClause(Expr, StartLoc, LParenLoc, EndLoc); 11798 break; 11799 case OMPC_device: 11800 case OMPC_if: 11801 case OMPC_default: 11802 case OMPC_proc_bind: 11803 case OMPC_schedule: 11804 case OMPC_private: 11805 case OMPC_firstprivate: 11806 case OMPC_lastprivate: 11807 case OMPC_shared: 11808 case OMPC_reduction: 11809 case OMPC_task_reduction: 11810 case OMPC_in_reduction: 11811 case OMPC_linear: 11812 case OMPC_aligned: 11813 case OMPC_copyin: 11814 case OMPC_copyprivate: 11815 case OMPC_nowait: 11816 case OMPC_untied: 11817 case OMPC_mergeable: 11818 case OMPC_threadprivate: 11819 case OMPC_allocate: 11820 case OMPC_flush: 11821 case OMPC_read: 11822 case OMPC_write: 11823 case OMPC_update: 11824 case OMPC_capture: 11825 case OMPC_seq_cst: 11826 case OMPC_acq_rel: 11827 case OMPC_acquire: 11828 case OMPC_release: 11829 case OMPC_relaxed: 11830 case OMPC_depend: 11831 case OMPC_threads: 11832 case OMPC_simd: 11833 case OMPC_map: 11834 case OMPC_nogroup: 11835 case OMPC_dist_schedule: 11836 case OMPC_defaultmap: 11837 case OMPC_unknown: 11838 case OMPC_uniform: 11839 case OMPC_to: 11840 case OMPC_from: 11841 case OMPC_use_device_ptr: 11842 case OMPC_use_device_addr: 11843 case OMPC_is_device_ptr: 11844 case OMPC_unified_address: 11845 case OMPC_unified_shared_memory: 11846 case OMPC_reverse_offload: 11847 case OMPC_dynamic_allocators: 11848 case OMPC_atomic_default_mem_order: 11849 case OMPC_device_type: 11850 case OMPC_match: 11851 case OMPC_nontemporal: 11852 case OMPC_order: 11853 case OMPC_destroy: 11854 case OMPC_inclusive: 11855 case OMPC_exclusive: 11856 case OMPC_uses_allocators: 11857 case OMPC_affinity: 11858 default: 11859 llvm_unreachable("Clause is not allowed."); 11860 } 11861 return Res; 11862 } 11863 11864 // An OpenMP directive such as 'target parallel' has two captured regions: 11865 // for the 'target' and 'parallel' respectively. This function returns 11866 // the region in which to capture expressions associated with a clause. 11867 // A return value of OMPD_unknown signifies that the expression should not 11868 // be captured. 11869 static OpenMPDirectiveKind getOpenMPCaptureRegionForClause( 11870 OpenMPDirectiveKind DKind, OpenMPClauseKind CKind, unsigned OpenMPVersion, 11871 OpenMPDirectiveKind NameModifier = OMPD_unknown) { 11872 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 11873 switch (CKind) { 11874 case OMPC_if: 11875 switch (DKind) { 11876 case OMPD_target_parallel_for_simd: 11877 if (OpenMPVersion >= 50 && 11878 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) { 11879 CaptureRegion = OMPD_parallel; 11880 break; 11881 } 11882 LLVM_FALLTHROUGH; 11883 case OMPD_target_parallel: 11884 case OMPD_target_parallel_for: 11885 // If this clause applies to the nested 'parallel' region, capture within 11886 // the 'target' region, otherwise do not capture. 11887 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel) 11888 CaptureRegion = OMPD_target; 11889 break; 11890 case OMPD_target_teams_distribute_parallel_for_simd: 11891 if (OpenMPVersion >= 50 && 11892 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) { 11893 CaptureRegion = OMPD_parallel; 11894 break; 11895 } 11896 LLVM_FALLTHROUGH; 11897 case OMPD_target_teams_distribute_parallel_for: 11898 // If this clause applies to the nested 'parallel' region, capture within 11899 // the 'teams' region, otherwise do not capture. 11900 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel) 11901 CaptureRegion = OMPD_teams; 11902 break; 11903 case OMPD_teams_distribute_parallel_for_simd: 11904 if (OpenMPVersion >= 50 && 11905 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) { 11906 CaptureRegion = OMPD_parallel; 11907 break; 11908 } 11909 LLVM_FALLTHROUGH; 11910 case OMPD_teams_distribute_parallel_for: 11911 CaptureRegion = OMPD_teams; 11912 break; 11913 case OMPD_target_update: 11914 case OMPD_target_enter_data: 11915 case OMPD_target_exit_data: 11916 CaptureRegion = OMPD_task; 11917 break; 11918 case OMPD_parallel_master_taskloop: 11919 if (NameModifier == OMPD_unknown || NameModifier == OMPD_taskloop) 11920 CaptureRegion = OMPD_parallel; 11921 break; 11922 case OMPD_parallel_master_taskloop_simd: 11923 if ((OpenMPVersion <= 45 && NameModifier == OMPD_unknown) || 11924 NameModifier == OMPD_taskloop) { 11925 CaptureRegion = OMPD_parallel; 11926 break; 11927 } 11928 if (OpenMPVersion <= 45) 11929 break; 11930 if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd) 11931 CaptureRegion = OMPD_taskloop; 11932 break; 11933 case OMPD_parallel_for_simd: 11934 if (OpenMPVersion <= 45) 11935 break; 11936 if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd) 11937 CaptureRegion = OMPD_parallel; 11938 break; 11939 case OMPD_taskloop_simd: 11940 case OMPD_master_taskloop_simd: 11941 if (OpenMPVersion <= 45) 11942 break; 11943 if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd) 11944 CaptureRegion = OMPD_taskloop; 11945 break; 11946 case OMPD_distribute_parallel_for_simd: 11947 if (OpenMPVersion <= 45) 11948 break; 11949 if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd) 11950 CaptureRegion = OMPD_parallel; 11951 break; 11952 case OMPD_target_simd: 11953 if (OpenMPVersion >= 50 && 11954 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) 11955 CaptureRegion = OMPD_target; 11956 break; 11957 case OMPD_teams_distribute_simd: 11958 case OMPD_target_teams_distribute_simd: 11959 if (OpenMPVersion >= 50 && 11960 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) 11961 CaptureRegion = OMPD_teams; 11962 break; 11963 case OMPD_cancel: 11964 case OMPD_parallel: 11965 case OMPD_parallel_master: 11966 case OMPD_parallel_sections: 11967 case OMPD_parallel_for: 11968 case OMPD_target: 11969 case OMPD_target_teams: 11970 case OMPD_target_teams_distribute: 11971 case OMPD_distribute_parallel_for: 11972 case OMPD_task: 11973 case OMPD_taskloop: 11974 case OMPD_master_taskloop: 11975 case OMPD_target_data: 11976 case OMPD_simd: 11977 case OMPD_for_simd: 11978 case OMPD_distribute_simd: 11979 // Do not capture if-clause expressions. 11980 break; 11981 case OMPD_threadprivate: 11982 case OMPD_allocate: 11983 case OMPD_taskyield: 11984 case OMPD_barrier: 11985 case OMPD_taskwait: 11986 case OMPD_cancellation_point: 11987 case OMPD_flush: 11988 case OMPD_depobj: 11989 case OMPD_scan: 11990 case OMPD_declare_reduction: 11991 case OMPD_declare_mapper: 11992 case OMPD_declare_simd: 11993 case OMPD_declare_variant: 11994 case OMPD_begin_declare_variant: 11995 case OMPD_end_declare_variant: 11996 case OMPD_declare_target: 11997 case OMPD_end_declare_target: 11998 case OMPD_teams: 11999 case OMPD_for: 12000 case OMPD_sections: 12001 case OMPD_section: 12002 case OMPD_single: 12003 case OMPD_master: 12004 case OMPD_critical: 12005 case OMPD_taskgroup: 12006 case OMPD_distribute: 12007 case OMPD_ordered: 12008 case OMPD_atomic: 12009 case OMPD_teams_distribute: 12010 case OMPD_requires: 12011 llvm_unreachable("Unexpected OpenMP directive with if-clause"); 12012 case OMPD_unknown: 12013 default: 12014 llvm_unreachable("Unknown OpenMP directive"); 12015 } 12016 break; 12017 case OMPC_num_threads: 12018 switch (DKind) { 12019 case OMPD_target_parallel: 12020 case OMPD_target_parallel_for: 12021 case OMPD_target_parallel_for_simd: 12022 CaptureRegion = OMPD_target; 12023 break; 12024 case OMPD_teams_distribute_parallel_for: 12025 case OMPD_teams_distribute_parallel_for_simd: 12026 case OMPD_target_teams_distribute_parallel_for: 12027 case OMPD_target_teams_distribute_parallel_for_simd: 12028 CaptureRegion = OMPD_teams; 12029 break; 12030 case OMPD_parallel: 12031 case OMPD_parallel_master: 12032 case OMPD_parallel_sections: 12033 case OMPD_parallel_for: 12034 case OMPD_parallel_for_simd: 12035 case OMPD_distribute_parallel_for: 12036 case OMPD_distribute_parallel_for_simd: 12037 case OMPD_parallel_master_taskloop: 12038 case OMPD_parallel_master_taskloop_simd: 12039 // Do not capture num_threads-clause expressions. 12040 break; 12041 case OMPD_target_data: 12042 case OMPD_target_enter_data: 12043 case OMPD_target_exit_data: 12044 case OMPD_target_update: 12045 case OMPD_target: 12046 case OMPD_target_simd: 12047 case OMPD_target_teams: 12048 case OMPD_target_teams_distribute: 12049 case OMPD_target_teams_distribute_simd: 12050 case OMPD_cancel: 12051 case OMPD_task: 12052 case OMPD_taskloop: 12053 case OMPD_taskloop_simd: 12054 case OMPD_master_taskloop: 12055 case OMPD_master_taskloop_simd: 12056 case OMPD_threadprivate: 12057 case OMPD_allocate: 12058 case OMPD_taskyield: 12059 case OMPD_barrier: 12060 case OMPD_taskwait: 12061 case OMPD_cancellation_point: 12062 case OMPD_flush: 12063 case OMPD_depobj: 12064 case OMPD_scan: 12065 case OMPD_declare_reduction: 12066 case OMPD_declare_mapper: 12067 case OMPD_declare_simd: 12068 case OMPD_declare_variant: 12069 case OMPD_begin_declare_variant: 12070 case OMPD_end_declare_variant: 12071 case OMPD_declare_target: 12072 case OMPD_end_declare_target: 12073 case OMPD_teams: 12074 case OMPD_simd: 12075 case OMPD_for: 12076 case OMPD_for_simd: 12077 case OMPD_sections: 12078 case OMPD_section: 12079 case OMPD_single: 12080 case OMPD_master: 12081 case OMPD_critical: 12082 case OMPD_taskgroup: 12083 case OMPD_distribute: 12084 case OMPD_ordered: 12085 case OMPD_atomic: 12086 case OMPD_distribute_simd: 12087 case OMPD_teams_distribute: 12088 case OMPD_teams_distribute_simd: 12089 case OMPD_requires: 12090 llvm_unreachable("Unexpected OpenMP directive with num_threads-clause"); 12091 case OMPD_unknown: 12092 default: 12093 llvm_unreachable("Unknown OpenMP directive"); 12094 } 12095 break; 12096 case OMPC_num_teams: 12097 switch (DKind) { 12098 case OMPD_target_teams: 12099 case OMPD_target_teams_distribute: 12100 case OMPD_target_teams_distribute_simd: 12101 case OMPD_target_teams_distribute_parallel_for: 12102 case OMPD_target_teams_distribute_parallel_for_simd: 12103 CaptureRegion = OMPD_target; 12104 break; 12105 case OMPD_teams_distribute_parallel_for: 12106 case OMPD_teams_distribute_parallel_for_simd: 12107 case OMPD_teams: 12108 case OMPD_teams_distribute: 12109 case OMPD_teams_distribute_simd: 12110 // Do not capture num_teams-clause expressions. 12111 break; 12112 case OMPD_distribute_parallel_for: 12113 case OMPD_distribute_parallel_for_simd: 12114 case OMPD_task: 12115 case OMPD_taskloop: 12116 case OMPD_taskloop_simd: 12117 case OMPD_master_taskloop: 12118 case OMPD_master_taskloop_simd: 12119 case OMPD_parallel_master_taskloop: 12120 case OMPD_parallel_master_taskloop_simd: 12121 case OMPD_target_data: 12122 case OMPD_target_enter_data: 12123 case OMPD_target_exit_data: 12124 case OMPD_target_update: 12125 case OMPD_cancel: 12126 case OMPD_parallel: 12127 case OMPD_parallel_master: 12128 case OMPD_parallel_sections: 12129 case OMPD_parallel_for: 12130 case OMPD_parallel_for_simd: 12131 case OMPD_target: 12132 case OMPD_target_simd: 12133 case OMPD_target_parallel: 12134 case OMPD_target_parallel_for: 12135 case OMPD_target_parallel_for_simd: 12136 case OMPD_threadprivate: 12137 case OMPD_allocate: 12138 case OMPD_taskyield: 12139 case OMPD_barrier: 12140 case OMPD_taskwait: 12141 case OMPD_cancellation_point: 12142 case OMPD_flush: 12143 case OMPD_depobj: 12144 case OMPD_scan: 12145 case OMPD_declare_reduction: 12146 case OMPD_declare_mapper: 12147 case OMPD_declare_simd: 12148 case OMPD_declare_variant: 12149 case OMPD_begin_declare_variant: 12150 case OMPD_end_declare_variant: 12151 case OMPD_declare_target: 12152 case OMPD_end_declare_target: 12153 case OMPD_simd: 12154 case OMPD_for: 12155 case OMPD_for_simd: 12156 case OMPD_sections: 12157 case OMPD_section: 12158 case OMPD_single: 12159 case OMPD_master: 12160 case OMPD_critical: 12161 case OMPD_taskgroup: 12162 case OMPD_distribute: 12163 case OMPD_ordered: 12164 case OMPD_atomic: 12165 case OMPD_distribute_simd: 12166 case OMPD_requires: 12167 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause"); 12168 case OMPD_unknown: 12169 default: 12170 llvm_unreachable("Unknown OpenMP directive"); 12171 } 12172 break; 12173 case OMPC_thread_limit: 12174 switch (DKind) { 12175 case OMPD_target_teams: 12176 case OMPD_target_teams_distribute: 12177 case OMPD_target_teams_distribute_simd: 12178 case OMPD_target_teams_distribute_parallel_for: 12179 case OMPD_target_teams_distribute_parallel_for_simd: 12180 CaptureRegion = OMPD_target; 12181 break; 12182 case OMPD_teams_distribute_parallel_for: 12183 case OMPD_teams_distribute_parallel_for_simd: 12184 case OMPD_teams: 12185 case OMPD_teams_distribute: 12186 case OMPD_teams_distribute_simd: 12187 // Do not capture thread_limit-clause expressions. 12188 break; 12189 case OMPD_distribute_parallel_for: 12190 case OMPD_distribute_parallel_for_simd: 12191 case OMPD_task: 12192 case OMPD_taskloop: 12193 case OMPD_taskloop_simd: 12194 case OMPD_master_taskloop: 12195 case OMPD_master_taskloop_simd: 12196 case OMPD_parallel_master_taskloop: 12197 case OMPD_parallel_master_taskloop_simd: 12198 case OMPD_target_data: 12199 case OMPD_target_enter_data: 12200 case OMPD_target_exit_data: 12201 case OMPD_target_update: 12202 case OMPD_cancel: 12203 case OMPD_parallel: 12204 case OMPD_parallel_master: 12205 case OMPD_parallel_sections: 12206 case OMPD_parallel_for: 12207 case OMPD_parallel_for_simd: 12208 case OMPD_target: 12209 case OMPD_target_simd: 12210 case OMPD_target_parallel: 12211 case OMPD_target_parallel_for: 12212 case OMPD_target_parallel_for_simd: 12213 case OMPD_threadprivate: 12214 case OMPD_allocate: 12215 case OMPD_taskyield: 12216 case OMPD_barrier: 12217 case OMPD_taskwait: 12218 case OMPD_cancellation_point: 12219 case OMPD_flush: 12220 case OMPD_depobj: 12221 case OMPD_scan: 12222 case OMPD_declare_reduction: 12223 case OMPD_declare_mapper: 12224 case OMPD_declare_simd: 12225 case OMPD_declare_variant: 12226 case OMPD_begin_declare_variant: 12227 case OMPD_end_declare_variant: 12228 case OMPD_declare_target: 12229 case OMPD_end_declare_target: 12230 case OMPD_simd: 12231 case OMPD_for: 12232 case OMPD_for_simd: 12233 case OMPD_sections: 12234 case OMPD_section: 12235 case OMPD_single: 12236 case OMPD_master: 12237 case OMPD_critical: 12238 case OMPD_taskgroup: 12239 case OMPD_distribute: 12240 case OMPD_ordered: 12241 case OMPD_atomic: 12242 case OMPD_distribute_simd: 12243 case OMPD_requires: 12244 llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause"); 12245 case OMPD_unknown: 12246 default: 12247 llvm_unreachable("Unknown OpenMP directive"); 12248 } 12249 break; 12250 case OMPC_schedule: 12251 switch (DKind) { 12252 case OMPD_parallel_for: 12253 case OMPD_parallel_for_simd: 12254 case OMPD_distribute_parallel_for: 12255 case OMPD_distribute_parallel_for_simd: 12256 case OMPD_teams_distribute_parallel_for: 12257 case OMPD_teams_distribute_parallel_for_simd: 12258 case OMPD_target_parallel_for: 12259 case OMPD_target_parallel_for_simd: 12260 case OMPD_target_teams_distribute_parallel_for: 12261 case OMPD_target_teams_distribute_parallel_for_simd: 12262 CaptureRegion = OMPD_parallel; 12263 break; 12264 case OMPD_for: 12265 case OMPD_for_simd: 12266 // Do not capture schedule-clause expressions. 12267 break; 12268 case OMPD_task: 12269 case OMPD_taskloop: 12270 case OMPD_taskloop_simd: 12271 case OMPD_master_taskloop: 12272 case OMPD_master_taskloop_simd: 12273 case OMPD_parallel_master_taskloop: 12274 case OMPD_parallel_master_taskloop_simd: 12275 case OMPD_target_data: 12276 case OMPD_target_enter_data: 12277 case OMPD_target_exit_data: 12278 case OMPD_target_update: 12279 case OMPD_teams: 12280 case OMPD_teams_distribute: 12281 case OMPD_teams_distribute_simd: 12282 case OMPD_target_teams_distribute: 12283 case OMPD_target_teams_distribute_simd: 12284 case OMPD_target: 12285 case OMPD_target_simd: 12286 case OMPD_target_parallel: 12287 case OMPD_cancel: 12288 case OMPD_parallel: 12289 case OMPD_parallel_master: 12290 case OMPD_parallel_sections: 12291 case OMPD_threadprivate: 12292 case OMPD_allocate: 12293 case OMPD_taskyield: 12294 case OMPD_barrier: 12295 case OMPD_taskwait: 12296 case OMPD_cancellation_point: 12297 case OMPD_flush: 12298 case OMPD_depobj: 12299 case OMPD_scan: 12300 case OMPD_declare_reduction: 12301 case OMPD_declare_mapper: 12302 case OMPD_declare_simd: 12303 case OMPD_declare_variant: 12304 case OMPD_begin_declare_variant: 12305 case OMPD_end_declare_variant: 12306 case OMPD_declare_target: 12307 case OMPD_end_declare_target: 12308 case OMPD_simd: 12309 case OMPD_sections: 12310 case OMPD_section: 12311 case OMPD_single: 12312 case OMPD_master: 12313 case OMPD_critical: 12314 case OMPD_taskgroup: 12315 case OMPD_distribute: 12316 case OMPD_ordered: 12317 case OMPD_atomic: 12318 case OMPD_distribute_simd: 12319 case OMPD_target_teams: 12320 case OMPD_requires: 12321 llvm_unreachable("Unexpected OpenMP directive with schedule clause"); 12322 case OMPD_unknown: 12323 default: 12324 llvm_unreachable("Unknown OpenMP directive"); 12325 } 12326 break; 12327 case OMPC_dist_schedule: 12328 switch (DKind) { 12329 case OMPD_teams_distribute_parallel_for: 12330 case OMPD_teams_distribute_parallel_for_simd: 12331 case OMPD_teams_distribute: 12332 case OMPD_teams_distribute_simd: 12333 case OMPD_target_teams_distribute_parallel_for: 12334 case OMPD_target_teams_distribute_parallel_for_simd: 12335 case OMPD_target_teams_distribute: 12336 case OMPD_target_teams_distribute_simd: 12337 CaptureRegion = OMPD_teams; 12338 break; 12339 case OMPD_distribute_parallel_for: 12340 case OMPD_distribute_parallel_for_simd: 12341 case OMPD_distribute: 12342 case OMPD_distribute_simd: 12343 // Do not capture thread_limit-clause expressions. 12344 break; 12345 case OMPD_parallel_for: 12346 case OMPD_parallel_for_simd: 12347 case OMPD_target_parallel_for_simd: 12348 case OMPD_target_parallel_for: 12349 case OMPD_task: 12350 case OMPD_taskloop: 12351 case OMPD_taskloop_simd: 12352 case OMPD_master_taskloop: 12353 case OMPD_master_taskloop_simd: 12354 case OMPD_parallel_master_taskloop: 12355 case OMPD_parallel_master_taskloop_simd: 12356 case OMPD_target_data: 12357 case OMPD_target_enter_data: 12358 case OMPD_target_exit_data: 12359 case OMPD_target_update: 12360 case OMPD_teams: 12361 case OMPD_target: 12362 case OMPD_target_simd: 12363 case OMPD_target_parallel: 12364 case OMPD_cancel: 12365 case OMPD_parallel: 12366 case OMPD_parallel_master: 12367 case OMPD_parallel_sections: 12368 case OMPD_threadprivate: 12369 case OMPD_allocate: 12370 case OMPD_taskyield: 12371 case OMPD_barrier: 12372 case OMPD_taskwait: 12373 case OMPD_cancellation_point: 12374 case OMPD_flush: 12375 case OMPD_depobj: 12376 case OMPD_scan: 12377 case OMPD_declare_reduction: 12378 case OMPD_declare_mapper: 12379 case OMPD_declare_simd: 12380 case OMPD_declare_variant: 12381 case OMPD_begin_declare_variant: 12382 case OMPD_end_declare_variant: 12383 case OMPD_declare_target: 12384 case OMPD_end_declare_target: 12385 case OMPD_simd: 12386 case OMPD_for: 12387 case OMPD_for_simd: 12388 case OMPD_sections: 12389 case OMPD_section: 12390 case OMPD_single: 12391 case OMPD_master: 12392 case OMPD_critical: 12393 case OMPD_taskgroup: 12394 case OMPD_ordered: 12395 case OMPD_atomic: 12396 case OMPD_target_teams: 12397 case OMPD_requires: 12398 llvm_unreachable("Unexpected OpenMP directive with schedule clause"); 12399 case OMPD_unknown: 12400 default: 12401 llvm_unreachable("Unknown OpenMP directive"); 12402 } 12403 break; 12404 case OMPC_device: 12405 switch (DKind) { 12406 case OMPD_target_update: 12407 case OMPD_target_enter_data: 12408 case OMPD_target_exit_data: 12409 case OMPD_target: 12410 case OMPD_target_simd: 12411 case OMPD_target_teams: 12412 case OMPD_target_parallel: 12413 case OMPD_target_teams_distribute: 12414 case OMPD_target_teams_distribute_simd: 12415 case OMPD_target_parallel_for: 12416 case OMPD_target_parallel_for_simd: 12417 case OMPD_target_teams_distribute_parallel_for: 12418 case OMPD_target_teams_distribute_parallel_for_simd: 12419 CaptureRegion = OMPD_task; 12420 break; 12421 case OMPD_target_data: 12422 // Do not capture device-clause expressions. 12423 break; 12424 case OMPD_teams_distribute_parallel_for: 12425 case OMPD_teams_distribute_parallel_for_simd: 12426 case OMPD_teams: 12427 case OMPD_teams_distribute: 12428 case OMPD_teams_distribute_simd: 12429 case OMPD_distribute_parallel_for: 12430 case OMPD_distribute_parallel_for_simd: 12431 case OMPD_task: 12432 case OMPD_taskloop: 12433 case OMPD_taskloop_simd: 12434 case OMPD_master_taskloop: 12435 case OMPD_master_taskloop_simd: 12436 case OMPD_parallel_master_taskloop: 12437 case OMPD_parallel_master_taskloop_simd: 12438 case OMPD_cancel: 12439 case OMPD_parallel: 12440 case OMPD_parallel_master: 12441 case OMPD_parallel_sections: 12442 case OMPD_parallel_for: 12443 case OMPD_parallel_for_simd: 12444 case OMPD_threadprivate: 12445 case OMPD_allocate: 12446 case OMPD_taskyield: 12447 case OMPD_barrier: 12448 case OMPD_taskwait: 12449 case OMPD_cancellation_point: 12450 case OMPD_flush: 12451 case OMPD_depobj: 12452 case OMPD_scan: 12453 case OMPD_declare_reduction: 12454 case OMPD_declare_mapper: 12455 case OMPD_declare_simd: 12456 case OMPD_declare_variant: 12457 case OMPD_begin_declare_variant: 12458 case OMPD_end_declare_variant: 12459 case OMPD_declare_target: 12460 case OMPD_end_declare_target: 12461 case OMPD_simd: 12462 case OMPD_for: 12463 case OMPD_for_simd: 12464 case OMPD_sections: 12465 case OMPD_section: 12466 case OMPD_single: 12467 case OMPD_master: 12468 case OMPD_critical: 12469 case OMPD_taskgroup: 12470 case OMPD_distribute: 12471 case OMPD_ordered: 12472 case OMPD_atomic: 12473 case OMPD_distribute_simd: 12474 case OMPD_requires: 12475 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause"); 12476 case OMPD_unknown: 12477 default: 12478 llvm_unreachable("Unknown OpenMP directive"); 12479 } 12480 break; 12481 case OMPC_grainsize: 12482 case OMPC_num_tasks: 12483 case OMPC_final: 12484 case OMPC_priority: 12485 switch (DKind) { 12486 case OMPD_task: 12487 case OMPD_taskloop: 12488 case OMPD_taskloop_simd: 12489 case OMPD_master_taskloop: 12490 case OMPD_master_taskloop_simd: 12491 break; 12492 case OMPD_parallel_master_taskloop: 12493 case OMPD_parallel_master_taskloop_simd: 12494 CaptureRegion = OMPD_parallel; 12495 break; 12496 case OMPD_target_update: 12497 case OMPD_target_enter_data: 12498 case OMPD_target_exit_data: 12499 case OMPD_target: 12500 case OMPD_target_simd: 12501 case OMPD_target_teams: 12502 case OMPD_target_parallel: 12503 case OMPD_target_teams_distribute: 12504 case OMPD_target_teams_distribute_simd: 12505 case OMPD_target_parallel_for: 12506 case OMPD_target_parallel_for_simd: 12507 case OMPD_target_teams_distribute_parallel_for: 12508 case OMPD_target_teams_distribute_parallel_for_simd: 12509 case OMPD_target_data: 12510 case OMPD_teams_distribute_parallel_for: 12511 case OMPD_teams_distribute_parallel_for_simd: 12512 case OMPD_teams: 12513 case OMPD_teams_distribute: 12514 case OMPD_teams_distribute_simd: 12515 case OMPD_distribute_parallel_for: 12516 case OMPD_distribute_parallel_for_simd: 12517 case OMPD_cancel: 12518 case OMPD_parallel: 12519 case OMPD_parallel_master: 12520 case OMPD_parallel_sections: 12521 case OMPD_parallel_for: 12522 case OMPD_parallel_for_simd: 12523 case OMPD_threadprivate: 12524 case OMPD_allocate: 12525 case OMPD_taskyield: 12526 case OMPD_barrier: 12527 case OMPD_taskwait: 12528 case OMPD_cancellation_point: 12529 case OMPD_flush: 12530 case OMPD_depobj: 12531 case OMPD_scan: 12532 case OMPD_declare_reduction: 12533 case OMPD_declare_mapper: 12534 case OMPD_declare_simd: 12535 case OMPD_declare_variant: 12536 case OMPD_begin_declare_variant: 12537 case OMPD_end_declare_variant: 12538 case OMPD_declare_target: 12539 case OMPD_end_declare_target: 12540 case OMPD_simd: 12541 case OMPD_for: 12542 case OMPD_for_simd: 12543 case OMPD_sections: 12544 case OMPD_section: 12545 case OMPD_single: 12546 case OMPD_master: 12547 case OMPD_critical: 12548 case OMPD_taskgroup: 12549 case OMPD_distribute: 12550 case OMPD_ordered: 12551 case OMPD_atomic: 12552 case OMPD_distribute_simd: 12553 case OMPD_requires: 12554 llvm_unreachable("Unexpected OpenMP directive with grainsize-clause"); 12555 case OMPD_unknown: 12556 default: 12557 llvm_unreachable("Unknown OpenMP directive"); 12558 } 12559 break; 12560 case OMPC_firstprivate: 12561 case OMPC_lastprivate: 12562 case OMPC_reduction: 12563 case OMPC_task_reduction: 12564 case OMPC_in_reduction: 12565 case OMPC_linear: 12566 case OMPC_default: 12567 case OMPC_proc_bind: 12568 case OMPC_safelen: 12569 case OMPC_simdlen: 12570 case OMPC_allocator: 12571 case OMPC_collapse: 12572 case OMPC_private: 12573 case OMPC_shared: 12574 case OMPC_aligned: 12575 case OMPC_copyin: 12576 case OMPC_copyprivate: 12577 case OMPC_ordered: 12578 case OMPC_nowait: 12579 case OMPC_untied: 12580 case OMPC_mergeable: 12581 case OMPC_threadprivate: 12582 case OMPC_allocate: 12583 case OMPC_flush: 12584 case OMPC_depobj: 12585 case OMPC_read: 12586 case OMPC_write: 12587 case OMPC_update: 12588 case OMPC_capture: 12589 case OMPC_seq_cst: 12590 case OMPC_acq_rel: 12591 case OMPC_acquire: 12592 case OMPC_release: 12593 case OMPC_relaxed: 12594 case OMPC_depend: 12595 case OMPC_threads: 12596 case OMPC_simd: 12597 case OMPC_map: 12598 case OMPC_nogroup: 12599 case OMPC_hint: 12600 case OMPC_defaultmap: 12601 case OMPC_unknown: 12602 case OMPC_uniform: 12603 case OMPC_to: 12604 case OMPC_from: 12605 case OMPC_use_device_ptr: 12606 case OMPC_use_device_addr: 12607 case OMPC_is_device_ptr: 12608 case OMPC_unified_address: 12609 case OMPC_unified_shared_memory: 12610 case OMPC_reverse_offload: 12611 case OMPC_dynamic_allocators: 12612 case OMPC_atomic_default_mem_order: 12613 case OMPC_device_type: 12614 case OMPC_match: 12615 case OMPC_nontemporal: 12616 case OMPC_order: 12617 case OMPC_destroy: 12618 case OMPC_detach: 12619 case OMPC_inclusive: 12620 case OMPC_exclusive: 12621 case OMPC_uses_allocators: 12622 case OMPC_affinity: 12623 default: 12624 llvm_unreachable("Unexpected OpenMP clause."); 12625 } 12626 return CaptureRegion; 12627 } 12628 12629 OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier, 12630 Expr *Condition, SourceLocation StartLoc, 12631 SourceLocation LParenLoc, 12632 SourceLocation NameModifierLoc, 12633 SourceLocation ColonLoc, 12634 SourceLocation EndLoc) { 12635 Expr *ValExpr = Condition; 12636 Stmt *HelperValStmt = nullptr; 12637 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 12638 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 12639 !Condition->isInstantiationDependent() && 12640 !Condition->containsUnexpandedParameterPack()) { 12641 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 12642 if (Val.isInvalid()) 12643 return nullptr; 12644 12645 ValExpr = Val.get(); 12646 12647 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 12648 CaptureRegion = getOpenMPCaptureRegionForClause( 12649 DKind, OMPC_if, LangOpts.OpenMP, NameModifier); 12650 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 12651 ValExpr = MakeFullExpr(ValExpr).get(); 12652 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 12653 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 12654 HelperValStmt = buildPreInits(Context, Captures); 12655 } 12656 } 12657 12658 return new (Context) 12659 OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc, 12660 LParenLoc, NameModifierLoc, ColonLoc, EndLoc); 12661 } 12662 12663 OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition, 12664 SourceLocation StartLoc, 12665 SourceLocation LParenLoc, 12666 SourceLocation EndLoc) { 12667 Expr *ValExpr = Condition; 12668 Stmt *HelperValStmt = nullptr; 12669 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 12670 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 12671 !Condition->isInstantiationDependent() && 12672 !Condition->containsUnexpandedParameterPack()) { 12673 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 12674 if (Val.isInvalid()) 12675 return nullptr; 12676 12677 ValExpr = MakeFullExpr(Val.get()).get(); 12678 12679 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 12680 CaptureRegion = 12681 getOpenMPCaptureRegionForClause(DKind, OMPC_final, LangOpts.OpenMP); 12682 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 12683 ValExpr = MakeFullExpr(ValExpr).get(); 12684 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 12685 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 12686 HelperValStmt = buildPreInits(Context, Captures); 12687 } 12688 } 12689 12690 return new (Context) OMPFinalClause(ValExpr, HelperValStmt, CaptureRegion, 12691 StartLoc, LParenLoc, EndLoc); 12692 } 12693 12694 ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc, 12695 Expr *Op) { 12696 if (!Op) 12697 return ExprError(); 12698 12699 class IntConvertDiagnoser : public ICEConvertDiagnoser { 12700 public: 12701 IntConvertDiagnoser() 12702 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {} 12703 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, 12704 QualType T) override { 12705 return S.Diag(Loc, diag::err_omp_not_integral) << T; 12706 } 12707 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc, 12708 QualType T) override { 12709 return S.Diag(Loc, diag::err_omp_incomplete_type) << T; 12710 } 12711 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc, 12712 QualType T, 12713 QualType ConvTy) override { 12714 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy; 12715 } 12716 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv, 12717 QualType ConvTy) override { 12718 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 12719 << ConvTy->isEnumeralType() << ConvTy; 12720 } 12721 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc, 12722 QualType T) override { 12723 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T; 12724 } 12725 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv, 12726 QualType ConvTy) override { 12727 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 12728 << ConvTy->isEnumeralType() << ConvTy; 12729 } 12730 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType, 12731 QualType) override { 12732 llvm_unreachable("conversion functions are permitted"); 12733 } 12734 } ConvertDiagnoser; 12735 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser); 12736 } 12737 12738 static bool 12739 isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef, OpenMPClauseKind CKind, 12740 bool StrictlyPositive, bool BuildCapture = false, 12741 OpenMPDirectiveKind DKind = OMPD_unknown, 12742 OpenMPDirectiveKind *CaptureRegion = nullptr, 12743 Stmt **HelperValStmt = nullptr) { 12744 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() && 12745 !ValExpr->isInstantiationDependent()) { 12746 SourceLocation Loc = ValExpr->getExprLoc(); 12747 ExprResult Value = 12748 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr); 12749 if (Value.isInvalid()) 12750 return false; 12751 12752 ValExpr = Value.get(); 12753 // The expression must evaluate to a non-negative integer value. 12754 if (Optional<llvm::APSInt> Result = 12755 ValExpr->getIntegerConstantExpr(SemaRef.Context)) { 12756 if (Result->isSigned() && 12757 !((!StrictlyPositive && Result->isNonNegative()) || 12758 (StrictlyPositive && Result->isStrictlyPositive()))) { 12759 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause) 12760 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) 12761 << ValExpr->getSourceRange(); 12762 return false; 12763 } 12764 } 12765 if (!BuildCapture) 12766 return true; 12767 *CaptureRegion = 12768 getOpenMPCaptureRegionForClause(DKind, CKind, SemaRef.LangOpts.OpenMP); 12769 if (*CaptureRegion != OMPD_unknown && 12770 !SemaRef.CurContext->isDependentContext()) { 12771 ValExpr = SemaRef.MakeFullExpr(ValExpr).get(); 12772 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 12773 ValExpr = tryBuildCapture(SemaRef, ValExpr, Captures).get(); 12774 *HelperValStmt = buildPreInits(SemaRef.Context, Captures); 12775 } 12776 } 12777 return true; 12778 } 12779 12780 OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads, 12781 SourceLocation StartLoc, 12782 SourceLocation LParenLoc, 12783 SourceLocation EndLoc) { 12784 Expr *ValExpr = NumThreads; 12785 Stmt *HelperValStmt = nullptr; 12786 12787 // OpenMP [2.5, Restrictions] 12788 // The num_threads expression must evaluate to a positive integer value. 12789 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads, 12790 /*StrictlyPositive=*/true)) 12791 return nullptr; 12792 12793 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 12794 OpenMPDirectiveKind CaptureRegion = 12795 getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads, LangOpts.OpenMP); 12796 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 12797 ValExpr = MakeFullExpr(ValExpr).get(); 12798 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 12799 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 12800 HelperValStmt = buildPreInits(Context, Captures); 12801 } 12802 12803 return new (Context) OMPNumThreadsClause( 12804 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc); 12805 } 12806 12807 ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E, 12808 OpenMPClauseKind CKind, 12809 bool StrictlyPositive) { 12810 if (!E) 12811 return ExprError(); 12812 if (E->isValueDependent() || E->isTypeDependent() || 12813 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 12814 return E; 12815 llvm::APSInt Result; 12816 ExprResult ICE = 12817 VerifyIntegerConstantExpression(E, &Result, /*FIXME*/ AllowFold); 12818 if (ICE.isInvalid()) 12819 return ExprError(); 12820 if ((StrictlyPositive && !Result.isStrictlyPositive()) || 12821 (!StrictlyPositive && !Result.isNonNegative())) { 12822 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause) 12823 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) 12824 << E->getSourceRange(); 12825 return ExprError(); 12826 } 12827 if (CKind == OMPC_aligned && !Result.isPowerOf2()) { 12828 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two) 12829 << E->getSourceRange(); 12830 return ExprError(); 12831 } 12832 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1) 12833 DSAStack->setAssociatedLoops(Result.getExtValue()); 12834 else if (CKind == OMPC_ordered) 12835 DSAStack->setAssociatedLoops(Result.getExtValue()); 12836 return ICE; 12837 } 12838 12839 OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc, 12840 SourceLocation LParenLoc, 12841 SourceLocation EndLoc) { 12842 // OpenMP [2.8.1, simd construct, Description] 12843 // The parameter of the safelen clause must be a constant 12844 // positive integer expression. 12845 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen); 12846 if (Safelen.isInvalid()) 12847 return nullptr; 12848 return new (Context) 12849 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc); 12850 } 12851 12852 OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc, 12853 SourceLocation LParenLoc, 12854 SourceLocation EndLoc) { 12855 // OpenMP [2.8.1, simd construct, Description] 12856 // The parameter of the simdlen clause must be a constant 12857 // positive integer expression. 12858 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen); 12859 if (Simdlen.isInvalid()) 12860 return nullptr; 12861 return new (Context) 12862 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc); 12863 } 12864 12865 /// Tries to find omp_allocator_handle_t type. 12866 static bool findOMPAllocatorHandleT(Sema &S, SourceLocation Loc, 12867 DSAStackTy *Stack) { 12868 QualType OMPAllocatorHandleT = Stack->getOMPAllocatorHandleT(); 12869 if (!OMPAllocatorHandleT.isNull()) 12870 return true; 12871 // Build the predefined allocator expressions. 12872 bool ErrorFound = false; 12873 for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) { 12874 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I); 12875 StringRef Allocator = 12876 OMPAllocateDeclAttr::ConvertAllocatorTypeTyToStr(AllocatorKind); 12877 DeclarationName AllocatorName = &S.getASTContext().Idents.get(Allocator); 12878 auto *VD = dyn_cast_or_null<ValueDecl>( 12879 S.LookupSingleName(S.TUScope, AllocatorName, Loc, Sema::LookupAnyName)); 12880 if (!VD) { 12881 ErrorFound = true; 12882 break; 12883 } 12884 QualType AllocatorType = 12885 VD->getType().getNonLValueExprType(S.getASTContext()); 12886 ExprResult Res = S.BuildDeclRefExpr(VD, AllocatorType, VK_LValue, Loc); 12887 if (!Res.isUsable()) { 12888 ErrorFound = true; 12889 break; 12890 } 12891 if (OMPAllocatorHandleT.isNull()) 12892 OMPAllocatorHandleT = AllocatorType; 12893 if (!S.getASTContext().hasSameType(OMPAllocatorHandleT, AllocatorType)) { 12894 ErrorFound = true; 12895 break; 12896 } 12897 Stack->setAllocator(AllocatorKind, Res.get()); 12898 } 12899 if (ErrorFound) { 12900 S.Diag(Loc, diag::err_omp_implied_type_not_found) 12901 << "omp_allocator_handle_t"; 12902 return false; 12903 } 12904 OMPAllocatorHandleT.addConst(); 12905 Stack->setOMPAllocatorHandleT(OMPAllocatorHandleT); 12906 return true; 12907 } 12908 12909 OMPClause *Sema::ActOnOpenMPAllocatorClause(Expr *A, SourceLocation StartLoc, 12910 SourceLocation LParenLoc, 12911 SourceLocation EndLoc) { 12912 // OpenMP [2.11.3, allocate Directive, Description] 12913 // allocator is an expression of omp_allocator_handle_t type. 12914 if (!findOMPAllocatorHandleT(*this, A->getExprLoc(), DSAStack)) 12915 return nullptr; 12916 12917 ExprResult Allocator = DefaultLvalueConversion(A); 12918 if (Allocator.isInvalid()) 12919 return nullptr; 12920 Allocator = PerformImplicitConversion(Allocator.get(), 12921 DSAStack->getOMPAllocatorHandleT(), 12922 Sema::AA_Initializing, 12923 /*AllowExplicit=*/true); 12924 if (Allocator.isInvalid()) 12925 return nullptr; 12926 return new (Context) 12927 OMPAllocatorClause(Allocator.get(), StartLoc, LParenLoc, EndLoc); 12928 } 12929 12930 OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops, 12931 SourceLocation StartLoc, 12932 SourceLocation LParenLoc, 12933 SourceLocation EndLoc) { 12934 // OpenMP [2.7.1, loop construct, Description] 12935 // OpenMP [2.8.1, simd construct, Description] 12936 // OpenMP [2.9.6, distribute construct, Description] 12937 // The parameter of the collapse clause must be a constant 12938 // positive integer expression. 12939 ExprResult NumForLoopsResult = 12940 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse); 12941 if (NumForLoopsResult.isInvalid()) 12942 return nullptr; 12943 return new (Context) 12944 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc); 12945 } 12946 12947 OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc, 12948 SourceLocation EndLoc, 12949 SourceLocation LParenLoc, 12950 Expr *NumForLoops) { 12951 // OpenMP [2.7.1, loop construct, Description] 12952 // OpenMP [2.8.1, simd construct, Description] 12953 // OpenMP [2.9.6, distribute construct, Description] 12954 // The parameter of the ordered clause must be a constant 12955 // positive integer expression if any. 12956 if (NumForLoops && LParenLoc.isValid()) { 12957 ExprResult NumForLoopsResult = 12958 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered); 12959 if (NumForLoopsResult.isInvalid()) 12960 return nullptr; 12961 NumForLoops = NumForLoopsResult.get(); 12962 } else { 12963 NumForLoops = nullptr; 12964 } 12965 auto *Clause = OMPOrderedClause::Create( 12966 Context, NumForLoops, NumForLoops ? DSAStack->getAssociatedLoops() : 0, 12967 StartLoc, LParenLoc, EndLoc); 12968 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops, Clause); 12969 return Clause; 12970 } 12971 12972 OMPClause *Sema::ActOnOpenMPSimpleClause( 12973 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc, 12974 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 12975 OMPClause *Res = nullptr; 12976 switch (Kind) { 12977 case OMPC_default: 12978 Res = ActOnOpenMPDefaultClause(static_cast<DefaultKind>(Argument), 12979 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 12980 break; 12981 case OMPC_proc_bind: 12982 Res = ActOnOpenMPProcBindClause(static_cast<ProcBindKind>(Argument), 12983 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 12984 break; 12985 case OMPC_atomic_default_mem_order: 12986 Res = ActOnOpenMPAtomicDefaultMemOrderClause( 12987 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Argument), 12988 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 12989 break; 12990 case OMPC_order: 12991 Res = ActOnOpenMPOrderClause(static_cast<OpenMPOrderClauseKind>(Argument), 12992 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 12993 break; 12994 case OMPC_update: 12995 Res = ActOnOpenMPUpdateClause(static_cast<OpenMPDependClauseKind>(Argument), 12996 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 12997 break; 12998 case OMPC_if: 12999 case OMPC_final: 13000 case OMPC_num_threads: 13001 case OMPC_safelen: 13002 case OMPC_simdlen: 13003 case OMPC_allocator: 13004 case OMPC_collapse: 13005 case OMPC_schedule: 13006 case OMPC_private: 13007 case OMPC_firstprivate: 13008 case OMPC_lastprivate: 13009 case OMPC_shared: 13010 case OMPC_reduction: 13011 case OMPC_task_reduction: 13012 case OMPC_in_reduction: 13013 case OMPC_linear: 13014 case OMPC_aligned: 13015 case OMPC_copyin: 13016 case OMPC_copyprivate: 13017 case OMPC_ordered: 13018 case OMPC_nowait: 13019 case OMPC_untied: 13020 case OMPC_mergeable: 13021 case OMPC_threadprivate: 13022 case OMPC_allocate: 13023 case OMPC_flush: 13024 case OMPC_depobj: 13025 case OMPC_read: 13026 case OMPC_write: 13027 case OMPC_capture: 13028 case OMPC_seq_cst: 13029 case OMPC_acq_rel: 13030 case OMPC_acquire: 13031 case OMPC_release: 13032 case OMPC_relaxed: 13033 case OMPC_depend: 13034 case OMPC_device: 13035 case OMPC_threads: 13036 case OMPC_simd: 13037 case OMPC_map: 13038 case OMPC_num_teams: 13039 case OMPC_thread_limit: 13040 case OMPC_priority: 13041 case OMPC_grainsize: 13042 case OMPC_nogroup: 13043 case OMPC_num_tasks: 13044 case OMPC_hint: 13045 case OMPC_dist_schedule: 13046 case OMPC_defaultmap: 13047 case OMPC_unknown: 13048 case OMPC_uniform: 13049 case OMPC_to: 13050 case OMPC_from: 13051 case OMPC_use_device_ptr: 13052 case OMPC_use_device_addr: 13053 case OMPC_is_device_ptr: 13054 case OMPC_unified_address: 13055 case OMPC_unified_shared_memory: 13056 case OMPC_reverse_offload: 13057 case OMPC_dynamic_allocators: 13058 case OMPC_device_type: 13059 case OMPC_match: 13060 case OMPC_nontemporal: 13061 case OMPC_destroy: 13062 case OMPC_detach: 13063 case OMPC_inclusive: 13064 case OMPC_exclusive: 13065 case OMPC_uses_allocators: 13066 case OMPC_affinity: 13067 default: 13068 llvm_unreachable("Clause is not allowed."); 13069 } 13070 return Res; 13071 } 13072 13073 static std::string 13074 getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last, 13075 ArrayRef<unsigned> Exclude = llvm::None) { 13076 SmallString<256> Buffer; 13077 llvm::raw_svector_ostream Out(Buffer); 13078 unsigned Skipped = Exclude.size(); 13079 auto S = Exclude.begin(), E = Exclude.end(); 13080 for (unsigned I = First; I < Last; ++I) { 13081 if (std::find(S, E, I) != E) { 13082 --Skipped; 13083 continue; 13084 } 13085 Out << "'" << getOpenMPSimpleClauseTypeName(K, I) << "'"; 13086 if (I + Skipped + 2 == Last) 13087 Out << " or "; 13088 else if (I + Skipped + 1 != Last) 13089 Out << ", "; 13090 } 13091 return std::string(Out.str()); 13092 } 13093 13094 OMPClause *Sema::ActOnOpenMPDefaultClause(DefaultKind Kind, 13095 SourceLocation KindKwLoc, 13096 SourceLocation StartLoc, 13097 SourceLocation LParenLoc, 13098 SourceLocation EndLoc) { 13099 if (Kind == OMP_DEFAULT_unknown) { 13100 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 13101 << getListOfPossibleValues(OMPC_default, /*First=*/0, 13102 /*Last=*/unsigned(OMP_DEFAULT_unknown)) 13103 << getOpenMPClauseName(OMPC_default); 13104 return nullptr; 13105 } 13106 13107 switch (Kind) { 13108 case OMP_DEFAULT_none: 13109 DSAStack->setDefaultDSANone(KindKwLoc); 13110 break; 13111 case OMP_DEFAULT_shared: 13112 DSAStack->setDefaultDSAShared(KindKwLoc); 13113 break; 13114 case OMP_DEFAULT_firstprivate: 13115 DSAStack->setDefaultDSAFirstPrivate(KindKwLoc); 13116 break; 13117 default: 13118 llvm_unreachable("DSA unexpected in OpenMP default clause"); 13119 } 13120 13121 return new (Context) 13122 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 13123 } 13124 13125 OMPClause *Sema::ActOnOpenMPProcBindClause(ProcBindKind Kind, 13126 SourceLocation KindKwLoc, 13127 SourceLocation StartLoc, 13128 SourceLocation LParenLoc, 13129 SourceLocation EndLoc) { 13130 if (Kind == OMP_PROC_BIND_unknown) { 13131 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 13132 << getListOfPossibleValues(OMPC_proc_bind, 13133 /*First=*/unsigned(OMP_PROC_BIND_master), 13134 /*Last=*/5) 13135 << getOpenMPClauseName(OMPC_proc_bind); 13136 return nullptr; 13137 } 13138 return new (Context) 13139 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 13140 } 13141 13142 OMPClause *Sema::ActOnOpenMPAtomicDefaultMemOrderClause( 13143 OpenMPAtomicDefaultMemOrderClauseKind Kind, SourceLocation KindKwLoc, 13144 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 13145 if (Kind == OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) { 13146 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 13147 << getListOfPossibleValues( 13148 OMPC_atomic_default_mem_order, /*First=*/0, 13149 /*Last=*/OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) 13150 << getOpenMPClauseName(OMPC_atomic_default_mem_order); 13151 return nullptr; 13152 } 13153 return new (Context) OMPAtomicDefaultMemOrderClause(Kind, KindKwLoc, StartLoc, 13154 LParenLoc, EndLoc); 13155 } 13156 13157 OMPClause *Sema::ActOnOpenMPOrderClause(OpenMPOrderClauseKind Kind, 13158 SourceLocation KindKwLoc, 13159 SourceLocation StartLoc, 13160 SourceLocation LParenLoc, 13161 SourceLocation EndLoc) { 13162 if (Kind == OMPC_ORDER_unknown) { 13163 static_assert(OMPC_ORDER_unknown > 0, 13164 "OMPC_ORDER_unknown not greater than 0"); 13165 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 13166 << getListOfPossibleValues(OMPC_order, /*First=*/0, 13167 /*Last=*/OMPC_ORDER_unknown) 13168 << getOpenMPClauseName(OMPC_order); 13169 return nullptr; 13170 } 13171 return new (Context) 13172 OMPOrderClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 13173 } 13174 13175 OMPClause *Sema::ActOnOpenMPUpdateClause(OpenMPDependClauseKind Kind, 13176 SourceLocation KindKwLoc, 13177 SourceLocation StartLoc, 13178 SourceLocation LParenLoc, 13179 SourceLocation EndLoc) { 13180 if (Kind == OMPC_DEPEND_unknown || Kind == OMPC_DEPEND_source || 13181 Kind == OMPC_DEPEND_sink || Kind == OMPC_DEPEND_depobj) { 13182 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink, 13183 OMPC_DEPEND_depobj}; 13184 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 13185 << getListOfPossibleValues(OMPC_depend, /*First=*/0, 13186 /*Last=*/OMPC_DEPEND_unknown, Except) 13187 << getOpenMPClauseName(OMPC_update); 13188 return nullptr; 13189 } 13190 return OMPUpdateClause::Create(Context, StartLoc, LParenLoc, KindKwLoc, Kind, 13191 EndLoc); 13192 } 13193 13194 OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause( 13195 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr, 13196 SourceLocation StartLoc, SourceLocation LParenLoc, 13197 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc, 13198 SourceLocation EndLoc) { 13199 OMPClause *Res = nullptr; 13200 switch (Kind) { 13201 case OMPC_schedule: 13202 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements }; 13203 assert(Argument.size() == NumberOfElements && 13204 ArgumentLoc.size() == NumberOfElements); 13205 Res = ActOnOpenMPScheduleClause( 13206 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]), 13207 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]), 13208 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr, 13209 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2], 13210 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc); 13211 break; 13212 case OMPC_if: 13213 assert(Argument.size() == 1 && ArgumentLoc.size() == 1); 13214 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()), 13215 Expr, StartLoc, LParenLoc, ArgumentLoc.back(), 13216 DelimLoc, EndLoc); 13217 break; 13218 case OMPC_dist_schedule: 13219 Res = ActOnOpenMPDistScheduleClause( 13220 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr, 13221 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc); 13222 break; 13223 case OMPC_defaultmap: 13224 enum { Modifier, DefaultmapKind }; 13225 Res = ActOnOpenMPDefaultmapClause( 13226 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]), 13227 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]), 13228 StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind], 13229 EndLoc); 13230 break; 13231 case OMPC_device: 13232 assert(Argument.size() == 1 && ArgumentLoc.size() == 1); 13233 Res = ActOnOpenMPDeviceClause( 13234 static_cast<OpenMPDeviceClauseModifier>(Argument.back()), Expr, 13235 StartLoc, LParenLoc, ArgumentLoc.back(), EndLoc); 13236 break; 13237 case OMPC_final: 13238 case OMPC_num_threads: 13239 case OMPC_safelen: 13240 case OMPC_simdlen: 13241 case OMPC_allocator: 13242 case OMPC_collapse: 13243 case OMPC_default: 13244 case OMPC_proc_bind: 13245 case OMPC_private: 13246 case OMPC_firstprivate: 13247 case OMPC_lastprivate: 13248 case OMPC_shared: 13249 case OMPC_reduction: 13250 case OMPC_task_reduction: 13251 case OMPC_in_reduction: 13252 case OMPC_linear: 13253 case OMPC_aligned: 13254 case OMPC_copyin: 13255 case OMPC_copyprivate: 13256 case OMPC_ordered: 13257 case OMPC_nowait: 13258 case OMPC_untied: 13259 case OMPC_mergeable: 13260 case OMPC_threadprivate: 13261 case OMPC_allocate: 13262 case OMPC_flush: 13263 case OMPC_depobj: 13264 case OMPC_read: 13265 case OMPC_write: 13266 case OMPC_update: 13267 case OMPC_capture: 13268 case OMPC_seq_cst: 13269 case OMPC_acq_rel: 13270 case OMPC_acquire: 13271 case OMPC_release: 13272 case OMPC_relaxed: 13273 case OMPC_depend: 13274 case OMPC_threads: 13275 case OMPC_simd: 13276 case OMPC_map: 13277 case OMPC_num_teams: 13278 case OMPC_thread_limit: 13279 case OMPC_priority: 13280 case OMPC_grainsize: 13281 case OMPC_nogroup: 13282 case OMPC_num_tasks: 13283 case OMPC_hint: 13284 case OMPC_unknown: 13285 case OMPC_uniform: 13286 case OMPC_to: 13287 case OMPC_from: 13288 case OMPC_use_device_ptr: 13289 case OMPC_use_device_addr: 13290 case OMPC_is_device_ptr: 13291 case OMPC_unified_address: 13292 case OMPC_unified_shared_memory: 13293 case OMPC_reverse_offload: 13294 case OMPC_dynamic_allocators: 13295 case OMPC_atomic_default_mem_order: 13296 case OMPC_device_type: 13297 case OMPC_match: 13298 case OMPC_nontemporal: 13299 case OMPC_order: 13300 case OMPC_destroy: 13301 case OMPC_detach: 13302 case OMPC_inclusive: 13303 case OMPC_exclusive: 13304 case OMPC_uses_allocators: 13305 case OMPC_affinity: 13306 default: 13307 llvm_unreachable("Clause is not allowed."); 13308 } 13309 return Res; 13310 } 13311 13312 static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1, 13313 OpenMPScheduleClauseModifier M2, 13314 SourceLocation M1Loc, SourceLocation M2Loc) { 13315 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) { 13316 SmallVector<unsigned, 2> Excluded; 13317 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown) 13318 Excluded.push_back(M2); 13319 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) 13320 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic); 13321 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic) 13322 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic); 13323 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value) 13324 << getListOfPossibleValues(OMPC_schedule, 13325 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1, 13326 /*Last=*/OMPC_SCHEDULE_MODIFIER_last, 13327 Excluded) 13328 << getOpenMPClauseName(OMPC_schedule); 13329 return true; 13330 } 13331 return false; 13332 } 13333 13334 OMPClause *Sema::ActOnOpenMPScheduleClause( 13335 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2, 13336 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, 13337 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc, 13338 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) { 13339 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) || 13340 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc)) 13341 return nullptr; 13342 // OpenMP, 2.7.1, Loop Construct, Restrictions 13343 // Either the monotonic modifier or the nonmonotonic modifier can be specified 13344 // but not both. 13345 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) || 13346 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic && 13347 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) || 13348 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic && 13349 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) { 13350 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier) 13351 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2) 13352 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1); 13353 return nullptr; 13354 } 13355 if (Kind == OMPC_SCHEDULE_unknown) { 13356 std::string Values; 13357 if (M1Loc.isInvalid() && M2Loc.isInvalid()) { 13358 unsigned Exclude[] = {OMPC_SCHEDULE_unknown}; 13359 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0, 13360 /*Last=*/OMPC_SCHEDULE_MODIFIER_last, 13361 Exclude); 13362 } else { 13363 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0, 13364 /*Last=*/OMPC_SCHEDULE_unknown); 13365 } 13366 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 13367 << Values << getOpenMPClauseName(OMPC_schedule); 13368 return nullptr; 13369 } 13370 // OpenMP, 2.7.1, Loop Construct, Restrictions 13371 // The nonmonotonic modifier can only be specified with schedule(dynamic) or 13372 // schedule(guided). 13373 // OpenMP 5.0 does not have this restriction. 13374 if (LangOpts.OpenMP < 50 && 13375 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic || 13376 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) && 13377 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) { 13378 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc, 13379 diag::err_omp_schedule_nonmonotonic_static); 13380 return nullptr; 13381 } 13382 Expr *ValExpr = ChunkSize; 13383 Stmt *HelperValStmt = nullptr; 13384 if (ChunkSize) { 13385 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && 13386 !ChunkSize->isInstantiationDependent() && 13387 !ChunkSize->containsUnexpandedParameterPack()) { 13388 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc(); 13389 ExprResult Val = 13390 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); 13391 if (Val.isInvalid()) 13392 return nullptr; 13393 13394 ValExpr = Val.get(); 13395 13396 // OpenMP [2.7.1, Restrictions] 13397 // chunk_size must be a loop invariant integer expression with a positive 13398 // value. 13399 if (Optional<llvm::APSInt> Result = 13400 ValExpr->getIntegerConstantExpr(Context)) { 13401 if (Result->isSigned() && !Result->isStrictlyPositive()) { 13402 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) 13403 << "schedule" << 1 << ChunkSize->getSourceRange(); 13404 return nullptr; 13405 } 13406 } else if (getOpenMPCaptureRegionForClause( 13407 DSAStack->getCurrentDirective(), OMPC_schedule, 13408 LangOpts.OpenMP) != OMPD_unknown && 13409 !CurContext->isDependentContext()) { 13410 ValExpr = MakeFullExpr(ValExpr).get(); 13411 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 13412 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 13413 HelperValStmt = buildPreInits(Context, Captures); 13414 } 13415 } 13416 } 13417 13418 return new (Context) 13419 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind, 13420 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc); 13421 } 13422 13423 OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind, 13424 SourceLocation StartLoc, 13425 SourceLocation EndLoc) { 13426 OMPClause *Res = nullptr; 13427 switch (Kind) { 13428 case OMPC_ordered: 13429 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc); 13430 break; 13431 case OMPC_nowait: 13432 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc); 13433 break; 13434 case OMPC_untied: 13435 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc); 13436 break; 13437 case OMPC_mergeable: 13438 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc); 13439 break; 13440 case OMPC_read: 13441 Res = ActOnOpenMPReadClause(StartLoc, EndLoc); 13442 break; 13443 case OMPC_write: 13444 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc); 13445 break; 13446 case OMPC_update: 13447 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc); 13448 break; 13449 case OMPC_capture: 13450 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc); 13451 break; 13452 case OMPC_seq_cst: 13453 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc); 13454 break; 13455 case OMPC_acq_rel: 13456 Res = ActOnOpenMPAcqRelClause(StartLoc, EndLoc); 13457 break; 13458 case OMPC_acquire: 13459 Res = ActOnOpenMPAcquireClause(StartLoc, EndLoc); 13460 break; 13461 case OMPC_release: 13462 Res = ActOnOpenMPReleaseClause(StartLoc, EndLoc); 13463 break; 13464 case OMPC_relaxed: 13465 Res = ActOnOpenMPRelaxedClause(StartLoc, EndLoc); 13466 break; 13467 case OMPC_threads: 13468 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc); 13469 break; 13470 case OMPC_simd: 13471 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc); 13472 break; 13473 case OMPC_nogroup: 13474 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc); 13475 break; 13476 case OMPC_unified_address: 13477 Res = ActOnOpenMPUnifiedAddressClause(StartLoc, EndLoc); 13478 break; 13479 case OMPC_unified_shared_memory: 13480 Res = ActOnOpenMPUnifiedSharedMemoryClause(StartLoc, EndLoc); 13481 break; 13482 case OMPC_reverse_offload: 13483 Res = ActOnOpenMPReverseOffloadClause(StartLoc, EndLoc); 13484 break; 13485 case OMPC_dynamic_allocators: 13486 Res = ActOnOpenMPDynamicAllocatorsClause(StartLoc, EndLoc); 13487 break; 13488 case OMPC_destroy: 13489 Res = ActOnOpenMPDestroyClause(StartLoc, EndLoc); 13490 break; 13491 case OMPC_if: 13492 case OMPC_final: 13493 case OMPC_num_threads: 13494 case OMPC_safelen: 13495 case OMPC_simdlen: 13496 case OMPC_allocator: 13497 case OMPC_collapse: 13498 case OMPC_schedule: 13499 case OMPC_private: 13500 case OMPC_firstprivate: 13501 case OMPC_lastprivate: 13502 case OMPC_shared: 13503 case OMPC_reduction: 13504 case OMPC_task_reduction: 13505 case OMPC_in_reduction: 13506 case OMPC_linear: 13507 case OMPC_aligned: 13508 case OMPC_copyin: 13509 case OMPC_copyprivate: 13510 case OMPC_default: 13511 case OMPC_proc_bind: 13512 case OMPC_threadprivate: 13513 case OMPC_allocate: 13514 case OMPC_flush: 13515 case OMPC_depobj: 13516 case OMPC_depend: 13517 case OMPC_device: 13518 case OMPC_map: 13519 case OMPC_num_teams: 13520 case OMPC_thread_limit: 13521 case OMPC_priority: 13522 case OMPC_grainsize: 13523 case OMPC_num_tasks: 13524 case OMPC_hint: 13525 case OMPC_dist_schedule: 13526 case OMPC_defaultmap: 13527 case OMPC_unknown: 13528 case OMPC_uniform: 13529 case OMPC_to: 13530 case OMPC_from: 13531 case OMPC_use_device_ptr: 13532 case OMPC_use_device_addr: 13533 case OMPC_is_device_ptr: 13534 case OMPC_atomic_default_mem_order: 13535 case OMPC_device_type: 13536 case OMPC_match: 13537 case OMPC_nontemporal: 13538 case OMPC_order: 13539 case OMPC_detach: 13540 case OMPC_inclusive: 13541 case OMPC_exclusive: 13542 case OMPC_uses_allocators: 13543 case OMPC_affinity: 13544 default: 13545 llvm_unreachable("Clause is not allowed."); 13546 } 13547 return Res; 13548 } 13549 13550 OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc, 13551 SourceLocation EndLoc) { 13552 DSAStack->setNowaitRegion(); 13553 return new (Context) OMPNowaitClause(StartLoc, EndLoc); 13554 } 13555 13556 OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc, 13557 SourceLocation EndLoc) { 13558 return new (Context) OMPUntiedClause(StartLoc, EndLoc); 13559 } 13560 13561 OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc, 13562 SourceLocation EndLoc) { 13563 return new (Context) OMPMergeableClause(StartLoc, EndLoc); 13564 } 13565 13566 OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc, 13567 SourceLocation EndLoc) { 13568 return new (Context) OMPReadClause(StartLoc, EndLoc); 13569 } 13570 13571 OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc, 13572 SourceLocation EndLoc) { 13573 return new (Context) OMPWriteClause(StartLoc, EndLoc); 13574 } 13575 13576 OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc, 13577 SourceLocation EndLoc) { 13578 return OMPUpdateClause::Create(Context, StartLoc, EndLoc); 13579 } 13580 13581 OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc, 13582 SourceLocation EndLoc) { 13583 return new (Context) OMPCaptureClause(StartLoc, EndLoc); 13584 } 13585 13586 OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc, 13587 SourceLocation EndLoc) { 13588 return new (Context) OMPSeqCstClause(StartLoc, EndLoc); 13589 } 13590 13591 OMPClause *Sema::ActOnOpenMPAcqRelClause(SourceLocation StartLoc, 13592 SourceLocation EndLoc) { 13593 return new (Context) OMPAcqRelClause(StartLoc, EndLoc); 13594 } 13595 13596 OMPClause *Sema::ActOnOpenMPAcquireClause(SourceLocation StartLoc, 13597 SourceLocation EndLoc) { 13598 return new (Context) OMPAcquireClause(StartLoc, EndLoc); 13599 } 13600 13601 OMPClause *Sema::ActOnOpenMPReleaseClause(SourceLocation StartLoc, 13602 SourceLocation EndLoc) { 13603 return new (Context) OMPReleaseClause(StartLoc, EndLoc); 13604 } 13605 13606 OMPClause *Sema::ActOnOpenMPRelaxedClause(SourceLocation StartLoc, 13607 SourceLocation EndLoc) { 13608 return new (Context) OMPRelaxedClause(StartLoc, EndLoc); 13609 } 13610 13611 OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc, 13612 SourceLocation EndLoc) { 13613 return new (Context) OMPThreadsClause(StartLoc, EndLoc); 13614 } 13615 13616 OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc, 13617 SourceLocation EndLoc) { 13618 return new (Context) OMPSIMDClause(StartLoc, EndLoc); 13619 } 13620 13621 OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc, 13622 SourceLocation EndLoc) { 13623 return new (Context) OMPNogroupClause(StartLoc, EndLoc); 13624 } 13625 13626 OMPClause *Sema::ActOnOpenMPUnifiedAddressClause(SourceLocation StartLoc, 13627 SourceLocation EndLoc) { 13628 return new (Context) OMPUnifiedAddressClause(StartLoc, EndLoc); 13629 } 13630 13631 OMPClause *Sema::ActOnOpenMPUnifiedSharedMemoryClause(SourceLocation StartLoc, 13632 SourceLocation EndLoc) { 13633 return new (Context) OMPUnifiedSharedMemoryClause(StartLoc, EndLoc); 13634 } 13635 13636 OMPClause *Sema::ActOnOpenMPReverseOffloadClause(SourceLocation StartLoc, 13637 SourceLocation EndLoc) { 13638 return new (Context) OMPReverseOffloadClause(StartLoc, EndLoc); 13639 } 13640 13641 OMPClause *Sema::ActOnOpenMPDynamicAllocatorsClause(SourceLocation StartLoc, 13642 SourceLocation EndLoc) { 13643 return new (Context) OMPDynamicAllocatorsClause(StartLoc, EndLoc); 13644 } 13645 13646 OMPClause *Sema::ActOnOpenMPDestroyClause(SourceLocation StartLoc, 13647 SourceLocation EndLoc) { 13648 return new (Context) OMPDestroyClause(StartLoc, EndLoc); 13649 } 13650 13651 OMPClause *Sema::ActOnOpenMPVarListClause( 13652 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *DepModOrTailExpr, 13653 const OMPVarListLocTy &Locs, SourceLocation ColonLoc, 13654 CXXScopeSpec &ReductionOrMapperIdScopeSpec, 13655 DeclarationNameInfo &ReductionOrMapperId, int ExtraModifier, 13656 ArrayRef<OpenMPMapModifierKind> MapTypeModifiers, 13657 ArrayRef<SourceLocation> MapTypeModifiersLoc, bool IsMapTypeImplicit, 13658 SourceLocation ExtraModifierLoc, 13659 ArrayRef<OpenMPMotionModifierKind> MotionModifiers, 13660 ArrayRef<SourceLocation> MotionModifiersLoc) { 13661 SourceLocation StartLoc = Locs.StartLoc; 13662 SourceLocation LParenLoc = Locs.LParenLoc; 13663 SourceLocation EndLoc = Locs.EndLoc; 13664 OMPClause *Res = nullptr; 13665 switch (Kind) { 13666 case OMPC_private: 13667 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc); 13668 break; 13669 case OMPC_firstprivate: 13670 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 13671 break; 13672 case OMPC_lastprivate: 13673 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_LASTPRIVATE_unknown && 13674 "Unexpected lastprivate modifier."); 13675 Res = ActOnOpenMPLastprivateClause( 13676 VarList, static_cast<OpenMPLastprivateModifier>(ExtraModifier), 13677 ExtraModifierLoc, ColonLoc, StartLoc, LParenLoc, EndLoc); 13678 break; 13679 case OMPC_shared: 13680 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc); 13681 break; 13682 case OMPC_reduction: 13683 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_REDUCTION_unknown && 13684 "Unexpected lastprivate modifier."); 13685 Res = ActOnOpenMPReductionClause( 13686 VarList, static_cast<OpenMPReductionClauseModifier>(ExtraModifier), 13687 StartLoc, LParenLoc, ExtraModifierLoc, ColonLoc, EndLoc, 13688 ReductionOrMapperIdScopeSpec, ReductionOrMapperId); 13689 break; 13690 case OMPC_task_reduction: 13691 Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 13692 EndLoc, ReductionOrMapperIdScopeSpec, 13693 ReductionOrMapperId); 13694 break; 13695 case OMPC_in_reduction: 13696 Res = ActOnOpenMPInReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 13697 EndLoc, ReductionOrMapperIdScopeSpec, 13698 ReductionOrMapperId); 13699 break; 13700 case OMPC_linear: 13701 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_LINEAR_unknown && 13702 "Unexpected linear modifier."); 13703 Res = ActOnOpenMPLinearClause( 13704 VarList, DepModOrTailExpr, StartLoc, LParenLoc, 13705 static_cast<OpenMPLinearClauseKind>(ExtraModifier), ExtraModifierLoc, 13706 ColonLoc, EndLoc); 13707 break; 13708 case OMPC_aligned: 13709 Res = ActOnOpenMPAlignedClause(VarList, DepModOrTailExpr, StartLoc, 13710 LParenLoc, ColonLoc, EndLoc); 13711 break; 13712 case OMPC_copyin: 13713 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc); 13714 break; 13715 case OMPC_copyprivate: 13716 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 13717 break; 13718 case OMPC_flush: 13719 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc); 13720 break; 13721 case OMPC_depend: 13722 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_DEPEND_unknown && 13723 "Unexpected depend modifier."); 13724 Res = ActOnOpenMPDependClause( 13725 DepModOrTailExpr, static_cast<OpenMPDependClauseKind>(ExtraModifier), 13726 ExtraModifierLoc, ColonLoc, VarList, StartLoc, LParenLoc, EndLoc); 13727 break; 13728 case OMPC_map: 13729 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_MAP_unknown && 13730 "Unexpected map modifier."); 13731 Res = ActOnOpenMPMapClause( 13732 MapTypeModifiers, MapTypeModifiersLoc, ReductionOrMapperIdScopeSpec, 13733 ReductionOrMapperId, static_cast<OpenMPMapClauseKind>(ExtraModifier), 13734 IsMapTypeImplicit, ExtraModifierLoc, ColonLoc, VarList, Locs); 13735 break; 13736 case OMPC_to: 13737 Res = ActOnOpenMPToClause(MotionModifiers, MotionModifiersLoc, 13738 ReductionOrMapperIdScopeSpec, ReductionOrMapperId, 13739 ColonLoc, VarList, Locs); 13740 break; 13741 case OMPC_from: 13742 Res = ActOnOpenMPFromClause(MotionModifiers, MotionModifiersLoc, 13743 ReductionOrMapperIdScopeSpec, 13744 ReductionOrMapperId, ColonLoc, VarList, Locs); 13745 break; 13746 case OMPC_use_device_ptr: 13747 Res = ActOnOpenMPUseDevicePtrClause(VarList, Locs); 13748 break; 13749 case OMPC_use_device_addr: 13750 Res = ActOnOpenMPUseDeviceAddrClause(VarList, Locs); 13751 break; 13752 case OMPC_is_device_ptr: 13753 Res = ActOnOpenMPIsDevicePtrClause(VarList, Locs); 13754 break; 13755 case OMPC_allocate: 13756 Res = ActOnOpenMPAllocateClause(DepModOrTailExpr, VarList, StartLoc, 13757 LParenLoc, ColonLoc, EndLoc); 13758 break; 13759 case OMPC_nontemporal: 13760 Res = ActOnOpenMPNontemporalClause(VarList, StartLoc, LParenLoc, EndLoc); 13761 break; 13762 case OMPC_inclusive: 13763 Res = ActOnOpenMPInclusiveClause(VarList, StartLoc, LParenLoc, EndLoc); 13764 break; 13765 case OMPC_exclusive: 13766 Res = ActOnOpenMPExclusiveClause(VarList, StartLoc, LParenLoc, EndLoc); 13767 break; 13768 case OMPC_affinity: 13769 Res = ActOnOpenMPAffinityClause(StartLoc, LParenLoc, ColonLoc, EndLoc, 13770 DepModOrTailExpr, VarList); 13771 break; 13772 case OMPC_if: 13773 case OMPC_depobj: 13774 case OMPC_final: 13775 case OMPC_num_threads: 13776 case OMPC_safelen: 13777 case OMPC_simdlen: 13778 case OMPC_allocator: 13779 case OMPC_collapse: 13780 case OMPC_default: 13781 case OMPC_proc_bind: 13782 case OMPC_schedule: 13783 case OMPC_ordered: 13784 case OMPC_nowait: 13785 case OMPC_untied: 13786 case OMPC_mergeable: 13787 case OMPC_threadprivate: 13788 case OMPC_read: 13789 case OMPC_write: 13790 case OMPC_update: 13791 case OMPC_capture: 13792 case OMPC_seq_cst: 13793 case OMPC_acq_rel: 13794 case OMPC_acquire: 13795 case OMPC_release: 13796 case OMPC_relaxed: 13797 case OMPC_device: 13798 case OMPC_threads: 13799 case OMPC_simd: 13800 case OMPC_num_teams: 13801 case OMPC_thread_limit: 13802 case OMPC_priority: 13803 case OMPC_grainsize: 13804 case OMPC_nogroup: 13805 case OMPC_num_tasks: 13806 case OMPC_hint: 13807 case OMPC_dist_schedule: 13808 case OMPC_defaultmap: 13809 case OMPC_unknown: 13810 case OMPC_uniform: 13811 case OMPC_unified_address: 13812 case OMPC_unified_shared_memory: 13813 case OMPC_reverse_offload: 13814 case OMPC_dynamic_allocators: 13815 case OMPC_atomic_default_mem_order: 13816 case OMPC_device_type: 13817 case OMPC_match: 13818 case OMPC_order: 13819 case OMPC_destroy: 13820 case OMPC_detach: 13821 case OMPC_uses_allocators: 13822 default: 13823 llvm_unreachable("Clause is not allowed."); 13824 } 13825 return Res; 13826 } 13827 13828 ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK, 13829 ExprObjectKind OK, SourceLocation Loc) { 13830 ExprResult Res = BuildDeclRefExpr( 13831 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc); 13832 if (!Res.isUsable()) 13833 return ExprError(); 13834 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) { 13835 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get()); 13836 if (!Res.isUsable()) 13837 return ExprError(); 13838 } 13839 if (VK != VK_LValue && Res.get()->isGLValue()) { 13840 Res = DefaultLvalueConversion(Res.get()); 13841 if (!Res.isUsable()) 13842 return ExprError(); 13843 } 13844 return Res; 13845 } 13846 13847 OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList, 13848 SourceLocation StartLoc, 13849 SourceLocation LParenLoc, 13850 SourceLocation EndLoc) { 13851 SmallVector<Expr *, 8> Vars; 13852 SmallVector<Expr *, 8> PrivateCopies; 13853 for (Expr *RefExpr : VarList) { 13854 assert(RefExpr && "NULL expr in OpenMP private clause."); 13855 SourceLocation ELoc; 13856 SourceRange ERange; 13857 Expr *SimpleRefExpr = RefExpr; 13858 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 13859 if (Res.second) { 13860 // It will be analyzed later. 13861 Vars.push_back(RefExpr); 13862 PrivateCopies.push_back(nullptr); 13863 } 13864 ValueDecl *D = Res.first; 13865 if (!D) 13866 continue; 13867 13868 QualType Type = D->getType(); 13869 auto *VD = dyn_cast<VarDecl>(D); 13870 13871 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 13872 // A variable that appears in a private clause must not have an incomplete 13873 // type or a reference type. 13874 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type)) 13875 continue; 13876 Type = Type.getNonReferenceType(); 13877 13878 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions] 13879 // A variable that is privatized must not have a const-qualified type 13880 // unless it is of class type with a mutable member. This restriction does 13881 // not apply to the firstprivate clause. 13882 // 13883 // OpenMP 3.1 [2.9.3.3, private clause, Restrictions] 13884 // A variable that appears in a private clause must not have a 13885 // const-qualified type unless it is of class type with a mutable member. 13886 if (rejectConstNotMutableType(*this, D, Type, OMPC_private, ELoc)) 13887 continue; 13888 13889 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 13890 // in a Construct] 13891 // Variables with the predetermined data-sharing attributes may not be 13892 // listed in data-sharing attributes clauses, except for the cases 13893 // listed below. For these exceptions only, listing a predetermined 13894 // variable in a data-sharing attribute clause is allowed and overrides 13895 // the variable's predetermined data-sharing attributes. 13896 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 13897 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) { 13898 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 13899 << getOpenMPClauseName(OMPC_private); 13900 reportOriginalDsa(*this, DSAStack, D, DVar); 13901 continue; 13902 } 13903 13904 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 13905 // Variably modified types are not supported for tasks. 13906 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && 13907 isOpenMPTaskingDirective(CurrDir)) { 13908 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 13909 << getOpenMPClauseName(OMPC_private) << Type 13910 << getOpenMPDirectiveName(CurrDir); 13911 bool IsDecl = 13912 !VD || 13913 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 13914 Diag(D->getLocation(), 13915 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 13916 << D; 13917 continue; 13918 } 13919 13920 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 13921 // A list item cannot appear in both a map clause and a data-sharing 13922 // attribute clause on the same construct 13923 // 13924 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7] 13925 // A list item cannot appear in both a map clause and a data-sharing 13926 // attribute clause on the same construct unless the construct is a 13927 // combined construct. 13928 if ((LangOpts.OpenMP <= 45 && isOpenMPTargetExecutionDirective(CurrDir)) || 13929 CurrDir == OMPD_target) { 13930 OpenMPClauseKind ConflictKind; 13931 if (DSAStack->checkMappableExprComponentListsForDecl( 13932 VD, /*CurrentRegionOnly=*/true, 13933 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef, 13934 OpenMPClauseKind WhereFoundClauseKind) -> bool { 13935 ConflictKind = WhereFoundClauseKind; 13936 return true; 13937 })) { 13938 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 13939 << getOpenMPClauseName(OMPC_private) 13940 << getOpenMPClauseName(ConflictKind) 13941 << getOpenMPDirectiveName(CurrDir); 13942 reportOriginalDsa(*this, DSAStack, D, DVar); 13943 continue; 13944 } 13945 } 13946 13947 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1] 13948 // A variable of class type (or array thereof) that appears in a private 13949 // clause requires an accessible, unambiguous default constructor for the 13950 // class type. 13951 // Generate helper private variable and initialize it with the default 13952 // value. The address of the original variable is replaced by the address of 13953 // the new private variable in CodeGen. This new variable is not added to 13954 // IdResolver, so the code in the OpenMP region uses original variable for 13955 // proper diagnostics. 13956 Type = Type.getUnqualifiedType(); 13957 VarDecl *VDPrivate = 13958 buildVarDecl(*this, ELoc, Type, D->getName(), 13959 D->hasAttrs() ? &D->getAttrs() : nullptr, 13960 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 13961 ActOnUninitializedDecl(VDPrivate); 13962 if (VDPrivate->isInvalidDecl()) 13963 continue; 13964 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 13965 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc); 13966 13967 DeclRefExpr *Ref = nullptr; 13968 if (!VD && !CurContext->isDependentContext()) 13969 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 13970 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref); 13971 Vars.push_back((VD || CurContext->isDependentContext()) 13972 ? RefExpr->IgnoreParens() 13973 : Ref); 13974 PrivateCopies.push_back(VDPrivateRefExpr); 13975 } 13976 13977 if (Vars.empty()) 13978 return nullptr; 13979 13980 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 13981 PrivateCopies); 13982 } 13983 13984 namespace { 13985 class DiagsUninitializedSeveretyRAII { 13986 private: 13987 DiagnosticsEngine &Diags; 13988 SourceLocation SavedLoc; 13989 bool IsIgnored = false; 13990 13991 public: 13992 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc, 13993 bool IsIgnored) 13994 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) { 13995 if (!IsIgnored) { 13996 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init, 13997 /*Map*/ diag::Severity::Ignored, Loc); 13998 } 13999 } 14000 ~DiagsUninitializedSeveretyRAII() { 14001 if (!IsIgnored) 14002 Diags.popMappings(SavedLoc); 14003 } 14004 }; 14005 } 14006 14007 OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList, 14008 SourceLocation StartLoc, 14009 SourceLocation LParenLoc, 14010 SourceLocation EndLoc) { 14011 SmallVector<Expr *, 8> Vars; 14012 SmallVector<Expr *, 8> PrivateCopies; 14013 SmallVector<Expr *, 8> Inits; 14014 SmallVector<Decl *, 4> ExprCaptures; 14015 bool IsImplicitClause = 14016 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid(); 14017 SourceLocation ImplicitClauseLoc = DSAStack->getConstructLoc(); 14018 14019 for (Expr *RefExpr : VarList) { 14020 assert(RefExpr && "NULL expr in OpenMP firstprivate clause."); 14021 SourceLocation ELoc; 14022 SourceRange ERange; 14023 Expr *SimpleRefExpr = RefExpr; 14024 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 14025 if (Res.second) { 14026 // It will be analyzed later. 14027 Vars.push_back(RefExpr); 14028 PrivateCopies.push_back(nullptr); 14029 Inits.push_back(nullptr); 14030 } 14031 ValueDecl *D = Res.first; 14032 if (!D) 14033 continue; 14034 14035 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc; 14036 QualType Type = D->getType(); 14037 auto *VD = dyn_cast<VarDecl>(D); 14038 14039 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 14040 // A variable that appears in a private clause must not have an incomplete 14041 // type or a reference type. 14042 if (RequireCompleteType(ELoc, Type, 14043 diag::err_omp_firstprivate_incomplete_type)) 14044 continue; 14045 Type = Type.getNonReferenceType(); 14046 14047 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1] 14048 // A variable of class type (or array thereof) that appears in a private 14049 // clause requires an accessible, unambiguous copy constructor for the 14050 // class type. 14051 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType(); 14052 14053 // If an implicit firstprivate variable found it was checked already. 14054 DSAStackTy::DSAVarData TopDVar; 14055 if (!IsImplicitClause) { 14056 DSAStackTy::DSAVarData DVar = 14057 DSAStack->getTopDSA(D, /*FromParent=*/false); 14058 TopDVar = DVar; 14059 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 14060 bool IsConstant = ElemType.isConstant(Context); 14061 // OpenMP [2.4.13, Data-sharing Attribute Clauses] 14062 // A list item that specifies a given variable may not appear in more 14063 // than one clause on the same directive, except that a variable may be 14064 // specified in both firstprivate and lastprivate clauses. 14065 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3] 14066 // A list item may appear in a firstprivate or lastprivate clause but not 14067 // both. 14068 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate && 14069 (isOpenMPDistributeDirective(CurrDir) || 14070 DVar.CKind != OMPC_lastprivate) && 14071 DVar.RefExpr) { 14072 Diag(ELoc, diag::err_omp_wrong_dsa) 14073 << getOpenMPClauseName(DVar.CKind) 14074 << getOpenMPClauseName(OMPC_firstprivate); 14075 reportOriginalDsa(*this, DSAStack, D, DVar); 14076 continue; 14077 } 14078 14079 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 14080 // in a Construct] 14081 // Variables with the predetermined data-sharing attributes may not be 14082 // listed in data-sharing attributes clauses, except for the cases 14083 // listed below. For these exceptions only, listing a predetermined 14084 // variable in a data-sharing attribute clause is allowed and overrides 14085 // the variable's predetermined data-sharing attributes. 14086 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 14087 // in a Construct, C/C++, p.2] 14088 // Variables with const-qualified type having no mutable member may be 14089 // listed in a firstprivate clause, even if they are static data members. 14090 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr && 14091 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) { 14092 Diag(ELoc, diag::err_omp_wrong_dsa) 14093 << getOpenMPClauseName(DVar.CKind) 14094 << getOpenMPClauseName(OMPC_firstprivate); 14095 reportOriginalDsa(*this, DSAStack, D, DVar); 14096 continue; 14097 } 14098 14099 // OpenMP [2.9.3.4, Restrictions, p.2] 14100 // A list item that is private within a parallel region must not appear 14101 // in a firstprivate clause on a worksharing construct if any of the 14102 // worksharing regions arising from the worksharing construct ever bind 14103 // to any of the parallel regions arising from the parallel construct. 14104 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3] 14105 // A list item that is private within a teams region must not appear in a 14106 // firstprivate clause on a distribute construct if any of the distribute 14107 // regions arising from the distribute construct ever bind to any of the 14108 // teams regions arising from the teams construct. 14109 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3] 14110 // A list item that appears in a reduction clause of a teams construct 14111 // must not appear in a firstprivate clause on a distribute construct if 14112 // any of the distribute regions arising from the distribute construct 14113 // ever bind to any of the teams regions arising from the teams construct. 14114 if ((isOpenMPWorksharingDirective(CurrDir) || 14115 isOpenMPDistributeDirective(CurrDir)) && 14116 !isOpenMPParallelDirective(CurrDir) && 14117 !isOpenMPTeamsDirective(CurrDir)) { 14118 DVar = DSAStack->getImplicitDSA(D, true); 14119 if (DVar.CKind != OMPC_shared && 14120 (isOpenMPParallelDirective(DVar.DKind) || 14121 isOpenMPTeamsDirective(DVar.DKind) || 14122 DVar.DKind == OMPD_unknown)) { 14123 Diag(ELoc, diag::err_omp_required_access) 14124 << getOpenMPClauseName(OMPC_firstprivate) 14125 << getOpenMPClauseName(OMPC_shared); 14126 reportOriginalDsa(*this, DSAStack, D, DVar); 14127 continue; 14128 } 14129 } 14130 // OpenMP [2.9.3.4, Restrictions, p.3] 14131 // A list item that appears in a reduction clause of a parallel construct 14132 // must not appear in a firstprivate clause on a worksharing or task 14133 // construct if any of the worksharing or task regions arising from the 14134 // worksharing or task construct ever bind to any of the parallel regions 14135 // arising from the parallel construct. 14136 // OpenMP [2.9.3.4, Restrictions, p.4] 14137 // A list item that appears in a reduction clause in worksharing 14138 // construct must not appear in a firstprivate clause in a task construct 14139 // encountered during execution of any of the worksharing regions arising 14140 // from the worksharing construct. 14141 if (isOpenMPTaskingDirective(CurrDir)) { 14142 DVar = DSAStack->hasInnermostDSA( 14143 D, 14144 [](OpenMPClauseKind C, bool AppliedToPointee) { 14145 return C == OMPC_reduction && !AppliedToPointee; 14146 }, 14147 [](OpenMPDirectiveKind K) { 14148 return isOpenMPParallelDirective(K) || 14149 isOpenMPWorksharingDirective(K) || 14150 isOpenMPTeamsDirective(K); 14151 }, 14152 /*FromParent=*/true); 14153 if (DVar.CKind == OMPC_reduction && 14154 (isOpenMPParallelDirective(DVar.DKind) || 14155 isOpenMPWorksharingDirective(DVar.DKind) || 14156 isOpenMPTeamsDirective(DVar.DKind))) { 14157 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate) 14158 << getOpenMPDirectiveName(DVar.DKind); 14159 reportOriginalDsa(*this, DSAStack, D, DVar); 14160 continue; 14161 } 14162 } 14163 14164 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 14165 // A list item cannot appear in both a map clause and a data-sharing 14166 // attribute clause on the same construct 14167 // 14168 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7] 14169 // A list item cannot appear in both a map clause and a data-sharing 14170 // attribute clause on the same construct unless the construct is a 14171 // combined construct. 14172 if ((LangOpts.OpenMP <= 45 && 14173 isOpenMPTargetExecutionDirective(CurrDir)) || 14174 CurrDir == OMPD_target) { 14175 OpenMPClauseKind ConflictKind; 14176 if (DSAStack->checkMappableExprComponentListsForDecl( 14177 VD, /*CurrentRegionOnly=*/true, 14178 [&ConflictKind]( 14179 OMPClauseMappableExprCommon::MappableExprComponentListRef, 14180 OpenMPClauseKind WhereFoundClauseKind) { 14181 ConflictKind = WhereFoundClauseKind; 14182 return true; 14183 })) { 14184 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 14185 << getOpenMPClauseName(OMPC_firstprivate) 14186 << getOpenMPClauseName(ConflictKind) 14187 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 14188 reportOriginalDsa(*this, DSAStack, D, DVar); 14189 continue; 14190 } 14191 } 14192 } 14193 14194 // Variably modified types are not supported for tasks. 14195 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && 14196 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) { 14197 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 14198 << getOpenMPClauseName(OMPC_firstprivate) << Type 14199 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 14200 bool IsDecl = 14201 !VD || 14202 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 14203 Diag(D->getLocation(), 14204 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 14205 << D; 14206 continue; 14207 } 14208 14209 Type = Type.getUnqualifiedType(); 14210 VarDecl *VDPrivate = 14211 buildVarDecl(*this, ELoc, Type, D->getName(), 14212 D->hasAttrs() ? &D->getAttrs() : nullptr, 14213 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 14214 // Generate helper private variable and initialize it with the value of the 14215 // original variable. The address of the original variable is replaced by 14216 // the address of the new private variable in the CodeGen. This new variable 14217 // is not added to IdResolver, so the code in the OpenMP region uses 14218 // original variable for proper diagnostics and variable capturing. 14219 Expr *VDInitRefExpr = nullptr; 14220 // For arrays generate initializer for single element and replace it by the 14221 // original array element in CodeGen. 14222 if (Type->isArrayType()) { 14223 VarDecl *VDInit = 14224 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName()); 14225 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc); 14226 Expr *Init = DefaultLvalueConversion(VDInitRefExpr).get(); 14227 ElemType = ElemType.getUnqualifiedType(); 14228 VarDecl *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, 14229 ".firstprivate.temp"); 14230 InitializedEntity Entity = 14231 InitializedEntity::InitializeVariable(VDInitTemp); 14232 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc); 14233 14234 InitializationSequence InitSeq(*this, Entity, Kind, Init); 14235 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init); 14236 if (Result.isInvalid()) 14237 VDPrivate->setInvalidDecl(); 14238 else 14239 VDPrivate->setInit(Result.getAs<Expr>()); 14240 // Remove temp variable declaration. 14241 Context.Deallocate(VDInitTemp); 14242 } else { 14243 VarDecl *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type, 14244 ".firstprivate.temp"); 14245 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(), 14246 RefExpr->getExprLoc()); 14247 AddInitializerToDecl(VDPrivate, 14248 DefaultLvalueConversion(VDInitRefExpr).get(), 14249 /*DirectInit=*/false); 14250 } 14251 if (VDPrivate->isInvalidDecl()) { 14252 if (IsImplicitClause) { 14253 Diag(RefExpr->getExprLoc(), 14254 diag::note_omp_task_predetermined_firstprivate_here); 14255 } 14256 continue; 14257 } 14258 CurContext->addDecl(VDPrivate); 14259 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 14260 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), 14261 RefExpr->getExprLoc()); 14262 DeclRefExpr *Ref = nullptr; 14263 if (!VD && !CurContext->isDependentContext()) { 14264 if (TopDVar.CKind == OMPC_lastprivate) { 14265 Ref = TopDVar.PrivateCopy; 14266 } else { 14267 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 14268 if (!isOpenMPCapturedDecl(D)) 14269 ExprCaptures.push_back(Ref->getDecl()); 14270 } 14271 } 14272 if (!IsImplicitClause) 14273 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 14274 Vars.push_back((VD || CurContext->isDependentContext()) 14275 ? RefExpr->IgnoreParens() 14276 : Ref); 14277 PrivateCopies.push_back(VDPrivateRefExpr); 14278 Inits.push_back(VDInitRefExpr); 14279 } 14280 14281 if (Vars.empty()) 14282 return nullptr; 14283 14284 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 14285 Vars, PrivateCopies, Inits, 14286 buildPreInits(Context, ExprCaptures)); 14287 } 14288 14289 OMPClause *Sema::ActOnOpenMPLastprivateClause( 14290 ArrayRef<Expr *> VarList, OpenMPLastprivateModifier LPKind, 14291 SourceLocation LPKindLoc, SourceLocation ColonLoc, SourceLocation StartLoc, 14292 SourceLocation LParenLoc, SourceLocation EndLoc) { 14293 if (LPKind == OMPC_LASTPRIVATE_unknown && LPKindLoc.isValid()) { 14294 assert(ColonLoc.isValid() && "Colon location must be valid."); 14295 Diag(LPKindLoc, diag::err_omp_unexpected_clause_value) 14296 << getListOfPossibleValues(OMPC_lastprivate, /*First=*/0, 14297 /*Last=*/OMPC_LASTPRIVATE_unknown) 14298 << getOpenMPClauseName(OMPC_lastprivate); 14299 return nullptr; 14300 } 14301 14302 SmallVector<Expr *, 8> Vars; 14303 SmallVector<Expr *, 8> SrcExprs; 14304 SmallVector<Expr *, 8> DstExprs; 14305 SmallVector<Expr *, 8> AssignmentOps; 14306 SmallVector<Decl *, 4> ExprCaptures; 14307 SmallVector<Expr *, 4> ExprPostUpdates; 14308 for (Expr *RefExpr : VarList) { 14309 assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); 14310 SourceLocation ELoc; 14311 SourceRange ERange; 14312 Expr *SimpleRefExpr = RefExpr; 14313 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 14314 if (Res.second) { 14315 // It will be analyzed later. 14316 Vars.push_back(RefExpr); 14317 SrcExprs.push_back(nullptr); 14318 DstExprs.push_back(nullptr); 14319 AssignmentOps.push_back(nullptr); 14320 } 14321 ValueDecl *D = Res.first; 14322 if (!D) 14323 continue; 14324 14325 QualType Type = D->getType(); 14326 auto *VD = dyn_cast<VarDecl>(D); 14327 14328 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2] 14329 // A variable that appears in a lastprivate clause must not have an 14330 // incomplete type or a reference type. 14331 if (RequireCompleteType(ELoc, Type, 14332 diag::err_omp_lastprivate_incomplete_type)) 14333 continue; 14334 Type = Type.getNonReferenceType(); 14335 14336 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions] 14337 // A variable that is privatized must not have a const-qualified type 14338 // unless it is of class type with a mutable member. This restriction does 14339 // not apply to the firstprivate clause. 14340 // 14341 // OpenMP 3.1 [2.9.3.5, lastprivate clause, Restrictions] 14342 // A variable that appears in a lastprivate clause must not have a 14343 // const-qualified type unless it is of class type with a mutable member. 14344 if (rejectConstNotMutableType(*this, D, Type, OMPC_lastprivate, ELoc)) 14345 continue; 14346 14347 // OpenMP 5.0 [2.19.4.5 lastprivate Clause, Restrictions] 14348 // A list item that appears in a lastprivate clause with the conditional 14349 // modifier must be a scalar variable. 14350 if (LPKind == OMPC_LASTPRIVATE_conditional && !Type->isScalarType()) { 14351 Diag(ELoc, diag::err_omp_lastprivate_conditional_non_scalar); 14352 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 14353 VarDecl::DeclarationOnly; 14354 Diag(D->getLocation(), 14355 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 14356 << D; 14357 continue; 14358 } 14359 14360 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 14361 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 14362 // in a Construct] 14363 // Variables with the predetermined data-sharing attributes may not be 14364 // listed in data-sharing attributes clauses, except for the cases 14365 // listed below. 14366 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3] 14367 // A list item may appear in a firstprivate or lastprivate clause but not 14368 // both. 14369 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 14370 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate && 14371 (isOpenMPDistributeDirective(CurrDir) || 14372 DVar.CKind != OMPC_firstprivate) && 14373 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) { 14374 Diag(ELoc, diag::err_omp_wrong_dsa) 14375 << getOpenMPClauseName(DVar.CKind) 14376 << getOpenMPClauseName(OMPC_lastprivate); 14377 reportOriginalDsa(*this, DSAStack, D, DVar); 14378 continue; 14379 } 14380 14381 // OpenMP [2.14.3.5, Restrictions, p.2] 14382 // A list item that is private within a parallel region, or that appears in 14383 // the reduction clause of a parallel construct, must not appear in a 14384 // lastprivate clause on a worksharing construct if any of the corresponding 14385 // worksharing regions ever binds to any of the corresponding parallel 14386 // regions. 14387 DSAStackTy::DSAVarData TopDVar = DVar; 14388 if (isOpenMPWorksharingDirective(CurrDir) && 14389 !isOpenMPParallelDirective(CurrDir) && 14390 !isOpenMPTeamsDirective(CurrDir)) { 14391 DVar = DSAStack->getImplicitDSA(D, true); 14392 if (DVar.CKind != OMPC_shared) { 14393 Diag(ELoc, diag::err_omp_required_access) 14394 << getOpenMPClauseName(OMPC_lastprivate) 14395 << getOpenMPClauseName(OMPC_shared); 14396 reportOriginalDsa(*this, DSAStack, D, DVar); 14397 continue; 14398 } 14399 } 14400 14401 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2] 14402 // A variable of class type (or array thereof) that appears in a 14403 // lastprivate clause requires an accessible, unambiguous default 14404 // constructor for the class type, unless the list item is also specified 14405 // in a firstprivate clause. 14406 // A variable of class type (or array thereof) that appears in a 14407 // lastprivate clause requires an accessible, unambiguous copy assignment 14408 // operator for the class type. 14409 Type = Context.getBaseElementType(Type).getNonReferenceType(); 14410 VarDecl *SrcVD = buildVarDecl(*this, ERange.getBegin(), 14411 Type.getUnqualifiedType(), ".lastprivate.src", 14412 D->hasAttrs() ? &D->getAttrs() : nullptr); 14413 DeclRefExpr *PseudoSrcExpr = 14414 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc); 14415 VarDecl *DstVD = 14416 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst", 14417 D->hasAttrs() ? &D->getAttrs() : nullptr); 14418 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc); 14419 // For arrays generate assignment operation for single element and replace 14420 // it by the original array element in CodeGen. 14421 ExprResult AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign, 14422 PseudoDstExpr, PseudoSrcExpr); 14423 if (AssignmentOp.isInvalid()) 14424 continue; 14425 AssignmentOp = 14426 ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false); 14427 if (AssignmentOp.isInvalid()) 14428 continue; 14429 14430 DeclRefExpr *Ref = nullptr; 14431 if (!VD && !CurContext->isDependentContext()) { 14432 if (TopDVar.CKind == OMPC_firstprivate) { 14433 Ref = TopDVar.PrivateCopy; 14434 } else { 14435 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 14436 if (!isOpenMPCapturedDecl(D)) 14437 ExprCaptures.push_back(Ref->getDecl()); 14438 } 14439 if ((TopDVar.CKind == OMPC_firstprivate && !TopDVar.PrivateCopy) || 14440 (!isOpenMPCapturedDecl(D) && 14441 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) { 14442 ExprResult RefRes = DefaultLvalueConversion(Ref); 14443 if (!RefRes.isUsable()) 14444 continue; 14445 ExprResult PostUpdateRes = 14446 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr, 14447 RefRes.get()); 14448 if (!PostUpdateRes.isUsable()) 14449 continue; 14450 ExprPostUpdates.push_back( 14451 IgnoredValueConversions(PostUpdateRes.get()).get()); 14452 } 14453 } 14454 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref); 14455 Vars.push_back((VD || CurContext->isDependentContext()) 14456 ? RefExpr->IgnoreParens() 14457 : Ref); 14458 SrcExprs.push_back(PseudoSrcExpr); 14459 DstExprs.push_back(PseudoDstExpr); 14460 AssignmentOps.push_back(AssignmentOp.get()); 14461 } 14462 14463 if (Vars.empty()) 14464 return nullptr; 14465 14466 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 14467 Vars, SrcExprs, DstExprs, AssignmentOps, 14468 LPKind, LPKindLoc, ColonLoc, 14469 buildPreInits(Context, ExprCaptures), 14470 buildPostUpdate(*this, ExprPostUpdates)); 14471 } 14472 14473 OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList, 14474 SourceLocation StartLoc, 14475 SourceLocation LParenLoc, 14476 SourceLocation EndLoc) { 14477 SmallVector<Expr *, 8> Vars; 14478 for (Expr *RefExpr : VarList) { 14479 assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); 14480 SourceLocation ELoc; 14481 SourceRange ERange; 14482 Expr *SimpleRefExpr = RefExpr; 14483 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 14484 if (Res.second) { 14485 // It will be analyzed later. 14486 Vars.push_back(RefExpr); 14487 } 14488 ValueDecl *D = Res.first; 14489 if (!D) 14490 continue; 14491 14492 auto *VD = dyn_cast<VarDecl>(D); 14493 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 14494 // in a Construct] 14495 // Variables with the predetermined data-sharing attributes may not be 14496 // listed in data-sharing attributes clauses, except for the cases 14497 // listed below. For these exceptions only, listing a predetermined 14498 // variable in a data-sharing attribute clause is allowed and overrides 14499 // the variable's predetermined data-sharing attributes. 14500 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 14501 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared && 14502 DVar.RefExpr) { 14503 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 14504 << getOpenMPClauseName(OMPC_shared); 14505 reportOriginalDsa(*this, DSAStack, D, DVar); 14506 continue; 14507 } 14508 14509 DeclRefExpr *Ref = nullptr; 14510 if (!VD && isOpenMPCapturedDecl(D) && !CurContext->isDependentContext()) 14511 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 14512 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref); 14513 Vars.push_back((VD || !Ref || CurContext->isDependentContext()) 14514 ? RefExpr->IgnoreParens() 14515 : Ref); 14516 } 14517 14518 if (Vars.empty()) 14519 return nullptr; 14520 14521 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 14522 } 14523 14524 namespace { 14525 class DSARefChecker : public StmtVisitor<DSARefChecker, bool> { 14526 DSAStackTy *Stack; 14527 14528 public: 14529 bool VisitDeclRefExpr(DeclRefExpr *E) { 14530 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 14531 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false); 14532 if (DVar.CKind == OMPC_shared && !DVar.RefExpr) 14533 return false; 14534 if (DVar.CKind != OMPC_unknown) 14535 return true; 14536 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA( 14537 VD, 14538 [](OpenMPClauseKind C, bool AppliedToPointee) { 14539 return isOpenMPPrivate(C) && !AppliedToPointee; 14540 }, 14541 [](OpenMPDirectiveKind) { return true; }, 14542 /*FromParent=*/true); 14543 return DVarPrivate.CKind != OMPC_unknown; 14544 } 14545 return false; 14546 } 14547 bool VisitStmt(Stmt *S) { 14548 for (Stmt *Child : S->children()) { 14549 if (Child && Visit(Child)) 14550 return true; 14551 } 14552 return false; 14553 } 14554 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {} 14555 }; 14556 } // namespace 14557 14558 namespace { 14559 // Transform MemberExpression for specified FieldDecl of current class to 14560 // DeclRefExpr to specified OMPCapturedExprDecl. 14561 class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> { 14562 typedef TreeTransform<TransformExprToCaptures> BaseTransform; 14563 ValueDecl *Field = nullptr; 14564 DeclRefExpr *CapturedExpr = nullptr; 14565 14566 public: 14567 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl) 14568 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {} 14569 14570 ExprResult TransformMemberExpr(MemberExpr *E) { 14571 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) && 14572 E->getMemberDecl() == Field) { 14573 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false); 14574 return CapturedExpr; 14575 } 14576 return BaseTransform::TransformMemberExpr(E); 14577 } 14578 DeclRefExpr *getCapturedExpr() { return CapturedExpr; } 14579 }; 14580 } // namespace 14581 14582 template <typename T, typename U> 14583 static T filterLookupForUDReductionAndMapper( 14584 SmallVectorImpl<U> &Lookups, const llvm::function_ref<T(ValueDecl *)> Gen) { 14585 for (U &Set : Lookups) { 14586 for (auto *D : Set) { 14587 if (T Res = Gen(cast<ValueDecl>(D))) 14588 return Res; 14589 } 14590 } 14591 return T(); 14592 } 14593 14594 static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D) { 14595 assert(!LookupResult::isVisible(SemaRef, D) && "not in slow case"); 14596 14597 for (auto RD : D->redecls()) { 14598 // Don't bother with extra checks if we already know this one isn't visible. 14599 if (RD == D) 14600 continue; 14601 14602 auto ND = cast<NamedDecl>(RD); 14603 if (LookupResult::isVisible(SemaRef, ND)) 14604 return ND; 14605 } 14606 14607 return nullptr; 14608 } 14609 14610 static void 14611 argumentDependentLookup(Sema &SemaRef, const DeclarationNameInfo &Id, 14612 SourceLocation Loc, QualType Ty, 14613 SmallVectorImpl<UnresolvedSet<8>> &Lookups) { 14614 // Find all of the associated namespaces and classes based on the 14615 // arguments we have. 14616 Sema::AssociatedNamespaceSet AssociatedNamespaces; 14617 Sema::AssociatedClassSet AssociatedClasses; 14618 OpaqueValueExpr OVE(Loc, Ty, VK_LValue); 14619 SemaRef.FindAssociatedClassesAndNamespaces(Loc, &OVE, AssociatedNamespaces, 14620 AssociatedClasses); 14621 14622 // C++ [basic.lookup.argdep]p3: 14623 // Let X be the lookup set produced by unqualified lookup (3.4.1) 14624 // and let Y be the lookup set produced by argument dependent 14625 // lookup (defined as follows). If X contains [...] then Y is 14626 // empty. Otherwise Y is the set of declarations found in the 14627 // namespaces associated with the argument types as described 14628 // below. The set of declarations found by the lookup of the name 14629 // is the union of X and Y. 14630 // 14631 // Here, we compute Y and add its members to the overloaded 14632 // candidate set. 14633 for (auto *NS : AssociatedNamespaces) { 14634 // When considering an associated namespace, the lookup is the 14635 // same as the lookup performed when the associated namespace is 14636 // used as a qualifier (3.4.3.2) except that: 14637 // 14638 // -- Any using-directives in the associated namespace are 14639 // ignored. 14640 // 14641 // -- Any namespace-scope friend functions declared in 14642 // associated classes are visible within their respective 14643 // namespaces even if they are not visible during an ordinary 14644 // lookup (11.4). 14645 DeclContext::lookup_result R = NS->lookup(Id.getName()); 14646 for (auto *D : R) { 14647 auto *Underlying = D; 14648 if (auto *USD = dyn_cast<UsingShadowDecl>(D)) 14649 Underlying = USD->getTargetDecl(); 14650 14651 if (!isa<OMPDeclareReductionDecl>(Underlying) && 14652 !isa<OMPDeclareMapperDecl>(Underlying)) 14653 continue; 14654 14655 if (!SemaRef.isVisible(D)) { 14656 D = findAcceptableDecl(SemaRef, D); 14657 if (!D) 14658 continue; 14659 if (auto *USD = dyn_cast<UsingShadowDecl>(D)) 14660 Underlying = USD->getTargetDecl(); 14661 } 14662 Lookups.emplace_back(); 14663 Lookups.back().addDecl(Underlying); 14664 } 14665 } 14666 } 14667 14668 static ExprResult 14669 buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range, 14670 Scope *S, CXXScopeSpec &ReductionIdScopeSpec, 14671 const DeclarationNameInfo &ReductionId, QualType Ty, 14672 CXXCastPath &BasePath, Expr *UnresolvedReduction) { 14673 if (ReductionIdScopeSpec.isInvalid()) 14674 return ExprError(); 14675 SmallVector<UnresolvedSet<8>, 4> Lookups; 14676 if (S) { 14677 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName); 14678 Lookup.suppressDiagnostics(); 14679 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) { 14680 NamedDecl *D = Lookup.getRepresentativeDecl(); 14681 do { 14682 S = S->getParent(); 14683 } while (S && !S->isDeclScope(D)); 14684 if (S) 14685 S = S->getParent(); 14686 Lookups.emplace_back(); 14687 Lookups.back().append(Lookup.begin(), Lookup.end()); 14688 Lookup.clear(); 14689 } 14690 } else if (auto *ULE = 14691 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) { 14692 Lookups.push_back(UnresolvedSet<8>()); 14693 Decl *PrevD = nullptr; 14694 for (NamedDecl *D : ULE->decls()) { 14695 if (D == PrevD) 14696 Lookups.push_back(UnresolvedSet<8>()); 14697 else if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(D)) 14698 Lookups.back().addDecl(DRD); 14699 PrevD = D; 14700 } 14701 } 14702 if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() || 14703 Ty->isInstantiationDependentType() || 14704 Ty->containsUnexpandedParameterPack() || 14705 filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) { 14706 return !D->isInvalidDecl() && 14707 (D->getType()->isDependentType() || 14708 D->getType()->isInstantiationDependentType() || 14709 D->getType()->containsUnexpandedParameterPack()); 14710 })) { 14711 UnresolvedSet<8> ResSet; 14712 for (const UnresolvedSet<8> &Set : Lookups) { 14713 if (Set.empty()) 14714 continue; 14715 ResSet.append(Set.begin(), Set.end()); 14716 // The last item marks the end of all declarations at the specified scope. 14717 ResSet.addDecl(Set[Set.size() - 1]); 14718 } 14719 return UnresolvedLookupExpr::Create( 14720 SemaRef.Context, /*NamingClass=*/nullptr, 14721 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId, 14722 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end()); 14723 } 14724 // Lookup inside the classes. 14725 // C++ [over.match.oper]p3: 14726 // For a unary operator @ with an operand of a type whose 14727 // cv-unqualified version is T1, and for a binary operator @ with 14728 // a left operand of a type whose cv-unqualified version is T1 and 14729 // a right operand of a type whose cv-unqualified version is T2, 14730 // three sets of candidate functions, designated member 14731 // candidates, non-member candidates and built-in candidates, are 14732 // constructed as follows: 14733 // -- If T1 is a complete class type or a class currently being 14734 // defined, the set of member candidates is the result of the 14735 // qualified lookup of T1::operator@ (13.3.1.1.1); otherwise, 14736 // the set of member candidates is empty. 14737 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName); 14738 Lookup.suppressDiagnostics(); 14739 if (const auto *TyRec = Ty->getAs<RecordType>()) { 14740 // Complete the type if it can be completed. 14741 // If the type is neither complete nor being defined, bail out now. 14742 if (SemaRef.isCompleteType(Loc, Ty) || TyRec->isBeingDefined() || 14743 TyRec->getDecl()->getDefinition()) { 14744 Lookup.clear(); 14745 SemaRef.LookupQualifiedName(Lookup, TyRec->getDecl()); 14746 if (Lookup.empty()) { 14747 Lookups.emplace_back(); 14748 Lookups.back().append(Lookup.begin(), Lookup.end()); 14749 } 14750 } 14751 } 14752 // Perform ADL. 14753 if (SemaRef.getLangOpts().CPlusPlus) 14754 argumentDependentLookup(SemaRef, ReductionId, Loc, Ty, Lookups); 14755 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 14756 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * { 14757 if (!D->isInvalidDecl() && 14758 SemaRef.Context.hasSameType(D->getType(), Ty)) 14759 return D; 14760 return nullptr; 14761 })) 14762 return SemaRef.BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(), 14763 VK_LValue, Loc); 14764 if (SemaRef.getLangOpts().CPlusPlus) { 14765 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 14766 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * { 14767 if (!D->isInvalidDecl() && 14768 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) && 14769 !Ty.isMoreQualifiedThan(D->getType())) 14770 return D; 14771 return nullptr; 14772 })) { 14773 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, 14774 /*DetectVirtual=*/false); 14775 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) { 14776 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType( 14777 VD->getType().getUnqualifiedType()))) { 14778 if (SemaRef.CheckBaseClassAccess( 14779 Loc, VD->getType(), Ty, Paths.front(), 14780 /*DiagID=*/0) != Sema::AR_inaccessible) { 14781 SemaRef.BuildBasePathArray(Paths, BasePath); 14782 return SemaRef.BuildDeclRefExpr( 14783 VD, VD->getType().getNonReferenceType(), VK_LValue, Loc); 14784 } 14785 } 14786 } 14787 } 14788 } 14789 if (ReductionIdScopeSpec.isSet()) { 14790 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) 14791 << Ty << Range; 14792 return ExprError(); 14793 } 14794 return ExprEmpty(); 14795 } 14796 14797 namespace { 14798 /// Data for the reduction-based clauses. 14799 struct ReductionData { 14800 /// List of original reduction items. 14801 SmallVector<Expr *, 8> Vars; 14802 /// List of private copies of the reduction items. 14803 SmallVector<Expr *, 8> Privates; 14804 /// LHS expressions for the reduction_op expressions. 14805 SmallVector<Expr *, 8> LHSs; 14806 /// RHS expressions for the reduction_op expressions. 14807 SmallVector<Expr *, 8> RHSs; 14808 /// Reduction operation expression. 14809 SmallVector<Expr *, 8> ReductionOps; 14810 /// inscan copy operation expressions. 14811 SmallVector<Expr *, 8> InscanCopyOps; 14812 /// inscan copy temp array expressions for prefix sums. 14813 SmallVector<Expr *, 8> InscanCopyArrayTemps; 14814 /// inscan copy temp array element expressions for prefix sums. 14815 SmallVector<Expr *, 8> InscanCopyArrayElems; 14816 /// Taskgroup descriptors for the corresponding reduction items in 14817 /// in_reduction clauses. 14818 SmallVector<Expr *, 8> TaskgroupDescriptors; 14819 /// List of captures for clause. 14820 SmallVector<Decl *, 4> ExprCaptures; 14821 /// List of postupdate expressions. 14822 SmallVector<Expr *, 4> ExprPostUpdates; 14823 /// Reduction modifier. 14824 unsigned RedModifier = 0; 14825 ReductionData() = delete; 14826 /// Reserves required memory for the reduction data. 14827 ReductionData(unsigned Size, unsigned Modifier = 0) : RedModifier(Modifier) { 14828 Vars.reserve(Size); 14829 Privates.reserve(Size); 14830 LHSs.reserve(Size); 14831 RHSs.reserve(Size); 14832 ReductionOps.reserve(Size); 14833 if (RedModifier == OMPC_REDUCTION_inscan) { 14834 InscanCopyOps.reserve(Size); 14835 InscanCopyArrayTemps.reserve(Size); 14836 InscanCopyArrayElems.reserve(Size); 14837 } 14838 TaskgroupDescriptors.reserve(Size); 14839 ExprCaptures.reserve(Size); 14840 ExprPostUpdates.reserve(Size); 14841 } 14842 /// Stores reduction item and reduction operation only (required for dependent 14843 /// reduction item). 14844 void push(Expr *Item, Expr *ReductionOp) { 14845 Vars.emplace_back(Item); 14846 Privates.emplace_back(nullptr); 14847 LHSs.emplace_back(nullptr); 14848 RHSs.emplace_back(nullptr); 14849 ReductionOps.emplace_back(ReductionOp); 14850 TaskgroupDescriptors.emplace_back(nullptr); 14851 if (RedModifier == OMPC_REDUCTION_inscan) { 14852 InscanCopyOps.push_back(nullptr); 14853 InscanCopyArrayTemps.push_back(nullptr); 14854 InscanCopyArrayElems.push_back(nullptr); 14855 } 14856 } 14857 /// Stores reduction data. 14858 void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp, 14859 Expr *TaskgroupDescriptor, Expr *CopyOp, Expr *CopyArrayTemp, 14860 Expr *CopyArrayElem) { 14861 Vars.emplace_back(Item); 14862 Privates.emplace_back(Private); 14863 LHSs.emplace_back(LHS); 14864 RHSs.emplace_back(RHS); 14865 ReductionOps.emplace_back(ReductionOp); 14866 TaskgroupDescriptors.emplace_back(TaskgroupDescriptor); 14867 if (RedModifier == OMPC_REDUCTION_inscan) { 14868 InscanCopyOps.push_back(CopyOp); 14869 InscanCopyArrayTemps.push_back(CopyArrayTemp); 14870 InscanCopyArrayElems.push_back(CopyArrayElem); 14871 } else { 14872 assert(CopyOp == nullptr && CopyArrayTemp == nullptr && 14873 CopyArrayElem == nullptr && 14874 "Copy operation must be used for inscan reductions only."); 14875 } 14876 } 14877 }; 14878 } // namespace 14879 14880 static bool checkOMPArraySectionConstantForReduction( 14881 ASTContext &Context, const OMPArraySectionExpr *OASE, bool &SingleElement, 14882 SmallVectorImpl<llvm::APSInt> &ArraySizes) { 14883 const Expr *Length = OASE->getLength(); 14884 if (Length == nullptr) { 14885 // For array sections of the form [1:] or [:], we would need to analyze 14886 // the lower bound... 14887 if (OASE->getColonLocFirst().isValid()) 14888 return false; 14889 14890 // This is an array subscript which has implicit length 1! 14891 SingleElement = true; 14892 ArraySizes.push_back(llvm::APSInt::get(1)); 14893 } else { 14894 Expr::EvalResult Result; 14895 if (!Length->EvaluateAsInt(Result, Context)) 14896 return false; 14897 14898 llvm::APSInt ConstantLengthValue = Result.Val.getInt(); 14899 SingleElement = (ConstantLengthValue.getSExtValue() == 1); 14900 ArraySizes.push_back(ConstantLengthValue); 14901 } 14902 14903 // Get the base of this array section and walk up from there. 14904 const Expr *Base = OASE->getBase()->IgnoreParenImpCasts(); 14905 14906 // We require length = 1 for all array sections except the right-most to 14907 // guarantee that the memory region is contiguous and has no holes in it. 14908 while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) { 14909 Length = TempOASE->getLength(); 14910 if (Length == nullptr) { 14911 // For array sections of the form [1:] or [:], we would need to analyze 14912 // the lower bound... 14913 if (OASE->getColonLocFirst().isValid()) 14914 return false; 14915 14916 // This is an array subscript which has implicit length 1! 14917 ArraySizes.push_back(llvm::APSInt::get(1)); 14918 } else { 14919 Expr::EvalResult Result; 14920 if (!Length->EvaluateAsInt(Result, Context)) 14921 return false; 14922 14923 llvm::APSInt ConstantLengthValue = Result.Val.getInt(); 14924 if (ConstantLengthValue.getSExtValue() != 1) 14925 return false; 14926 14927 ArraySizes.push_back(ConstantLengthValue); 14928 } 14929 Base = TempOASE->getBase()->IgnoreParenImpCasts(); 14930 } 14931 14932 // If we have a single element, we don't need to add the implicit lengths. 14933 if (!SingleElement) { 14934 while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) { 14935 // Has implicit length 1! 14936 ArraySizes.push_back(llvm::APSInt::get(1)); 14937 Base = TempASE->getBase()->IgnoreParenImpCasts(); 14938 } 14939 } 14940 14941 // This array section can be privatized as a single value or as a constant 14942 // sized array. 14943 return true; 14944 } 14945 14946 static bool actOnOMPReductionKindClause( 14947 Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind, 14948 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 14949 SourceLocation ColonLoc, SourceLocation EndLoc, 14950 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 14951 ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) { 14952 DeclarationName DN = ReductionId.getName(); 14953 OverloadedOperatorKind OOK = DN.getCXXOverloadedOperator(); 14954 BinaryOperatorKind BOK = BO_Comma; 14955 14956 ASTContext &Context = S.Context; 14957 // OpenMP [2.14.3.6, reduction clause] 14958 // C 14959 // reduction-identifier is either an identifier or one of the following 14960 // operators: +, -, *, &, |, ^, && and || 14961 // C++ 14962 // reduction-identifier is either an id-expression or one of the following 14963 // operators: +, -, *, &, |, ^, && and || 14964 switch (OOK) { 14965 case OO_Plus: 14966 case OO_Minus: 14967 BOK = BO_Add; 14968 break; 14969 case OO_Star: 14970 BOK = BO_Mul; 14971 break; 14972 case OO_Amp: 14973 BOK = BO_And; 14974 break; 14975 case OO_Pipe: 14976 BOK = BO_Or; 14977 break; 14978 case OO_Caret: 14979 BOK = BO_Xor; 14980 break; 14981 case OO_AmpAmp: 14982 BOK = BO_LAnd; 14983 break; 14984 case OO_PipePipe: 14985 BOK = BO_LOr; 14986 break; 14987 case OO_New: 14988 case OO_Delete: 14989 case OO_Array_New: 14990 case OO_Array_Delete: 14991 case OO_Slash: 14992 case OO_Percent: 14993 case OO_Tilde: 14994 case OO_Exclaim: 14995 case OO_Equal: 14996 case OO_Less: 14997 case OO_Greater: 14998 case OO_LessEqual: 14999 case OO_GreaterEqual: 15000 case OO_PlusEqual: 15001 case OO_MinusEqual: 15002 case OO_StarEqual: 15003 case OO_SlashEqual: 15004 case OO_PercentEqual: 15005 case OO_CaretEqual: 15006 case OO_AmpEqual: 15007 case OO_PipeEqual: 15008 case OO_LessLess: 15009 case OO_GreaterGreater: 15010 case OO_LessLessEqual: 15011 case OO_GreaterGreaterEqual: 15012 case OO_EqualEqual: 15013 case OO_ExclaimEqual: 15014 case OO_Spaceship: 15015 case OO_PlusPlus: 15016 case OO_MinusMinus: 15017 case OO_Comma: 15018 case OO_ArrowStar: 15019 case OO_Arrow: 15020 case OO_Call: 15021 case OO_Subscript: 15022 case OO_Conditional: 15023 case OO_Coawait: 15024 case NUM_OVERLOADED_OPERATORS: 15025 llvm_unreachable("Unexpected reduction identifier"); 15026 case OO_None: 15027 if (IdentifierInfo *II = DN.getAsIdentifierInfo()) { 15028 if (II->isStr("max")) 15029 BOK = BO_GT; 15030 else if (II->isStr("min")) 15031 BOK = BO_LT; 15032 } 15033 break; 15034 } 15035 SourceRange ReductionIdRange; 15036 if (ReductionIdScopeSpec.isValid()) 15037 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc()); 15038 else 15039 ReductionIdRange.setBegin(ReductionId.getBeginLoc()); 15040 ReductionIdRange.setEnd(ReductionId.getEndLoc()); 15041 15042 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end(); 15043 bool FirstIter = true; 15044 for (Expr *RefExpr : VarList) { 15045 assert(RefExpr && "nullptr expr in OpenMP reduction clause."); 15046 // OpenMP [2.1, C/C++] 15047 // A list item is a variable or array section, subject to the restrictions 15048 // specified in Section 2.4 on page 42 and in each of the sections 15049 // describing clauses and directives for which a list appears. 15050 // OpenMP [2.14.3.3, Restrictions, p.1] 15051 // A variable that is part of another variable (as an array or 15052 // structure element) cannot appear in a private clause. 15053 if (!FirstIter && IR != ER) 15054 ++IR; 15055 FirstIter = false; 15056 SourceLocation ELoc; 15057 SourceRange ERange; 15058 Expr *SimpleRefExpr = RefExpr; 15059 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange, 15060 /*AllowArraySection=*/true); 15061 if (Res.second) { 15062 // Try to find 'declare reduction' corresponding construct before using 15063 // builtin/overloaded operators. 15064 QualType Type = Context.DependentTy; 15065 CXXCastPath BasePath; 15066 ExprResult DeclareReductionRef = buildDeclareReductionRef( 15067 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec, 15068 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR); 15069 Expr *ReductionOp = nullptr; 15070 if (S.CurContext->isDependentContext() && 15071 (DeclareReductionRef.isUnset() || 15072 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) 15073 ReductionOp = DeclareReductionRef.get(); 15074 // It will be analyzed later. 15075 RD.push(RefExpr, ReductionOp); 15076 } 15077 ValueDecl *D = Res.first; 15078 if (!D) 15079 continue; 15080 15081 Expr *TaskgroupDescriptor = nullptr; 15082 QualType Type; 15083 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens()); 15084 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens()); 15085 if (ASE) { 15086 Type = ASE->getType().getNonReferenceType(); 15087 } else if (OASE) { 15088 QualType BaseType = 15089 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 15090 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 15091 Type = ATy->getElementType(); 15092 else 15093 Type = BaseType->getPointeeType(); 15094 Type = Type.getNonReferenceType(); 15095 } else { 15096 Type = Context.getBaseElementType(D->getType().getNonReferenceType()); 15097 } 15098 auto *VD = dyn_cast<VarDecl>(D); 15099 15100 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 15101 // A variable that appears in a private clause must not have an incomplete 15102 // type or a reference type. 15103 if (S.RequireCompleteType(ELoc, D->getType(), 15104 diag::err_omp_reduction_incomplete_type)) 15105 continue; 15106 // OpenMP [2.14.3.6, reduction clause, Restrictions] 15107 // A list item that appears in a reduction clause must not be 15108 // const-qualified. 15109 if (rejectConstNotMutableType(S, D, Type, ClauseKind, ELoc, 15110 /*AcceptIfMutable*/ false, ASE || OASE)) 15111 continue; 15112 15113 OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective(); 15114 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4] 15115 // If a list-item is a reference type then it must bind to the same object 15116 // for all threads of the team. 15117 if (!ASE && !OASE) { 15118 if (VD) { 15119 VarDecl *VDDef = VD->getDefinition(); 15120 if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) { 15121 DSARefChecker Check(Stack); 15122 if (Check.Visit(VDDef->getInit())) { 15123 S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg) 15124 << getOpenMPClauseName(ClauseKind) << ERange; 15125 S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef; 15126 continue; 15127 } 15128 } 15129 } 15130 15131 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 15132 // in a Construct] 15133 // Variables with the predetermined data-sharing attributes may not be 15134 // listed in data-sharing attributes clauses, except for the cases 15135 // listed below. For these exceptions only, listing a predetermined 15136 // variable in a data-sharing attribute clause is allowed and overrides 15137 // the variable's predetermined data-sharing attributes. 15138 // OpenMP [2.14.3.6, Restrictions, p.3] 15139 // Any number of reduction clauses can be specified on the directive, 15140 // but a list item can appear only once in the reduction clauses for that 15141 // directive. 15142 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false); 15143 if (DVar.CKind == OMPC_reduction) { 15144 S.Diag(ELoc, diag::err_omp_once_referenced) 15145 << getOpenMPClauseName(ClauseKind); 15146 if (DVar.RefExpr) 15147 S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced); 15148 continue; 15149 } 15150 if (DVar.CKind != OMPC_unknown) { 15151 S.Diag(ELoc, diag::err_omp_wrong_dsa) 15152 << getOpenMPClauseName(DVar.CKind) 15153 << getOpenMPClauseName(OMPC_reduction); 15154 reportOriginalDsa(S, Stack, D, DVar); 15155 continue; 15156 } 15157 15158 // OpenMP [2.14.3.6, Restrictions, p.1] 15159 // A list item that appears in a reduction clause of a worksharing 15160 // construct must be shared in the parallel regions to which any of the 15161 // worksharing regions arising from the worksharing construct bind. 15162 if (isOpenMPWorksharingDirective(CurrDir) && 15163 !isOpenMPParallelDirective(CurrDir) && 15164 !isOpenMPTeamsDirective(CurrDir)) { 15165 DVar = Stack->getImplicitDSA(D, true); 15166 if (DVar.CKind != OMPC_shared) { 15167 S.Diag(ELoc, diag::err_omp_required_access) 15168 << getOpenMPClauseName(OMPC_reduction) 15169 << getOpenMPClauseName(OMPC_shared); 15170 reportOriginalDsa(S, Stack, D, DVar); 15171 continue; 15172 } 15173 } 15174 } else { 15175 // Threadprivates cannot be shared between threads, so dignose if the base 15176 // is a threadprivate variable. 15177 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false); 15178 if (DVar.CKind == OMPC_threadprivate) { 15179 S.Diag(ELoc, diag::err_omp_wrong_dsa) 15180 << getOpenMPClauseName(DVar.CKind) 15181 << getOpenMPClauseName(OMPC_reduction); 15182 reportOriginalDsa(S, Stack, D, DVar); 15183 continue; 15184 } 15185 } 15186 15187 // Try to find 'declare reduction' corresponding construct before using 15188 // builtin/overloaded operators. 15189 CXXCastPath BasePath; 15190 ExprResult DeclareReductionRef = buildDeclareReductionRef( 15191 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec, 15192 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR); 15193 if (DeclareReductionRef.isInvalid()) 15194 continue; 15195 if (S.CurContext->isDependentContext() && 15196 (DeclareReductionRef.isUnset() || 15197 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) { 15198 RD.push(RefExpr, DeclareReductionRef.get()); 15199 continue; 15200 } 15201 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) { 15202 // Not allowed reduction identifier is found. 15203 S.Diag(ReductionId.getBeginLoc(), 15204 diag::err_omp_unknown_reduction_identifier) 15205 << Type << ReductionIdRange; 15206 continue; 15207 } 15208 15209 // OpenMP [2.14.3.6, reduction clause, Restrictions] 15210 // The type of a list item that appears in a reduction clause must be valid 15211 // for the reduction-identifier. For a max or min reduction in C, the type 15212 // of the list item must be an allowed arithmetic data type: char, int, 15213 // float, double, or _Bool, possibly modified with long, short, signed, or 15214 // unsigned. For a max or min reduction in C++, the type of the list item 15215 // must be an allowed arithmetic data type: char, wchar_t, int, float, 15216 // double, or bool, possibly modified with long, short, signed, or unsigned. 15217 if (DeclareReductionRef.isUnset()) { 15218 if ((BOK == BO_GT || BOK == BO_LT) && 15219 !(Type->isScalarType() || 15220 (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) { 15221 S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg) 15222 << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus; 15223 if (!ASE && !OASE) { 15224 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 15225 VarDecl::DeclarationOnly; 15226 S.Diag(D->getLocation(), 15227 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 15228 << D; 15229 } 15230 continue; 15231 } 15232 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) && 15233 !S.getLangOpts().CPlusPlus && Type->isFloatingType()) { 15234 S.Diag(ELoc, diag::err_omp_clause_floating_type_arg) 15235 << getOpenMPClauseName(ClauseKind); 15236 if (!ASE && !OASE) { 15237 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 15238 VarDecl::DeclarationOnly; 15239 S.Diag(D->getLocation(), 15240 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 15241 << D; 15242 } 15243 continue; 15244 } 15245 } 15246 15247 Type = Type.getNonLValueExprType(Context).getUnqualifiedType(); 15248 VarDecl *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs", 15249 D->hasAttrs() ? &D->getAttrs() : nullptr); 15250 VarDecl *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(), 15251 D->hasAttrs() ? &D->getAttrs() : nullptr); 15252 QualType PrivateTy = Type; 15253 15254 // Try if we can determine constant lengths for all array sections and avoid 15255 // the VLA. 15256 bool ConstantLengthOASE = false; 15257 if (OASE) { 15258 bool SingleElement; 15259 llvm::SmallVector<llvm::APSInt, 4> ArraySizes; 15260 ConstantLengthOASE = checkOMPArraySectionConstantForReduction( 15261 Context, OASE, SingleElement, ArraySizes); 15262 15263 // If we don't have a single element, we must emit a constant array type. 15264 if (ConstantLengthOASE && !SingleElement) { 15265 for (llvm::APSInt &Size : ArraySizes) 15266 PrivateTy = Context.getConstantArrayType(PrivateTy, Size, nullptr, 15267 ArrayType::Normal, 15268 /*IndexTypeQuals=*/0); 15269 } 15270 } 15271 15272 if ((OASE && !ConstantLengthOASE) || 15273 (!OASE && !ASE && 15274 D->getType().getNonReferenceType()->isVariablyModifiedType())) { 15275 if (!Context.getTargetInfo().isVLASupported()) { 15276 if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective())) { 15277 S.Diag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE; 15278 S.Diag(ELoc, diag::note_vla_unsupported); 15279 continue; 15280 } else { 15281 S.targetDiag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE; 15282 S.targetDiag(ELoc, diag::note_vla_unsupported); 15283 } 15284 } 15285 // For arrays/array sections only: 15286 // Create pseudo array type for private copy. The size for this array will 15287 // be generated during codegen. 15288 // For array subscripts or single variables Private Ty is the same as Type 15289 // (type of the variable or single array element). 15290 PrivateTy = Context.getVariableArrayType( 15291 Type, 15292 new (Context) OpaqueValueExpr(ELoc, Context.getSizeType(), VK_RValue), 15293 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange()); 15294 } else if (!ASE && !OASE && 15295 Context.getAsArrayType(D->getType().getNonReferenceType())) { 15296 PrivateTy = D->getType().getNonReferenceType(); 15297 } 15298 // Private copy. 15299 VarDecl *PrivateVD = 15300 buildVarDecl(S, ELoc, PrivateTy, D->getName(), 15301 D->hasAttrs() ? &D->getAttrs() : nullptr, 15302 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 15303 // Add initializer for private variable. 15304 Expr *Init = nullptr; 15305 DeclRefExpr *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc); 15306 DeclRefExpr *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc); 15307 if (DeclareReductionRef.isUsable()) { 15308 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>(); 15309 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl()); 15310 if (DRD->getInitializer()) { 15311 S.ActOnUninitializedDecl(PrivateVD); 15312 Init = DRDRef; 15313 RHSVD->setInit(DRDRef); 15314 RHSVD->setInitStyle(VarDecl::CallInit); 15315 } 15316 } else { 15317 switch (BOK) { 15318 case BO_Add: 15319 case BO_Xor: 15320 case BO_Or: 15321 case BO_LOr: 15322 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'. 15323 if (Type->isScalarType() || Type->isAnyComplexType()) 15324 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get(); 15325 break; 15326 case BO_Mul: 15327 case BO_LAnd: 15328 if (Type->isScalarType() || Type->isAnyComplexType()) { 15329 // '*' and '&&' reduction ops - initializer is '1'. 15330 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get(); 15331 } 15332 break; 15333 case BO_And: { 15334 // '&' reduction op - initializer is '~0'. 15335 QualType OrigType = Type; 15336 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) 15337 Type = ComplexTy->getElementType(); 15338 if (Type->isRealFloatingType()) { 15339 llvm::APFloat InitValue = llvm::APFloat::getAllOnesValue( 15340 Context.getFloatTypeSemantics(Type), 15341 Context.getTypeSize(Type)); 15342 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, 15343 Type, ELoc); 15344 } else if (Type->isScalarType()) { 15345 uint64_t Size = Context.getTypeSize(Type); 15346 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0); 15347 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size); 15348 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); 15349 } 15350 if (Init && OrigType->isAnyComplexType()) { 15351 // Init = 0xFFFF + 0xFFFFi; 15352 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType); 15353 Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get(); 15354 } 15355 Type = OrigType; 15356 break; 15357 } 15358 case BO_LT: 15359 case BO_GT: { 15360 // 'min' reduction op - initializer is 'Largest representable number in 15361 // the reduction list item type'. 15362 // 'max' reduction op - initializer is 'Least representable number in 15363 // the reduction list item type'. 15364 if (Type->isIntegerType() || Type->isPointerType()) { 15365 bool IsSigned = Type->hasSignedIntegerRepresentation(); 15366 uint64_t Size = Context.getTypeSize(Type); 15367 QualType IntTy = 15368 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned); 15369 llvm::APInt InitValue = 15370 (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size) 15371 : llvm::APInt::getMinValue(Size) 15372 : IsSigned ? llvm::APInt::getSignedMaxValue(Size) 15373 : llvm::APInt::getMaxValue(Size); 15374 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); 15375 if (Type->isPointerType()) { 15376 // Cast to pointer type. 15377 ExprResult CastExpr = S.BuildCStyleCastExpr( 15378 ELoc, Context.getTrivialTypeSourceInfo(Type, ELoc), ELoc, Init); 15379 if (CastExpr.isInvalid()) 15380 continue; 15381 Init = CastExpr.get(); 15382 } 15383 } else if (Type->isRealFloatingType()) { 15384 llvm::APFloat InitValue = llvm::APFloat::getLargest( 15385 Context.getFloatTypeSemantics(Type), BOK != BO_LT); 15386 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, 15387 Type, ELoc); 15388 } 15389 break; 15390 } 15391 case BO_PtrMemD: 15392 case BO_PtrMemI: 15393 case BO_MulAssign: 15394 case BO_Div: 15395 case BO_Rem: 15396 case BO_Sub: 15397 case BO_Shl: 15398 case BO_Shr: 15399 case BO_LE: 15400 case BO_GE: 15401 case BO_EQ: 15402 case BO_NE: 15403 case BO_Cmp: 15404 case BO_AndAssign: 15405 case BO_XorAssign: 15406 case BO_OrAssign: 15407 case BO_Assign: 15408 case BO_AddAssign: 15409 case BO_SubAssign: 15410 case BO_DivAssign: 15411 case BO_RemAssign: 15412 case BO_ShlAssign: 15413 case BO_ShrAssign: 15414 case BO_Comma: 15415 llvm_unreachable("Unexpected reduction operation"); 15416 } 15417 } 15418 if (Init && DeclareReductionRef.isUnset()) { 15419 S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false); 15420 // Store initializer for single element in private copy. Will be used 15421 // during codegen. 15422 PrivateVD->setInit(RHSVD->getInit()); 15423 PrivateVD->setInitStyle(RHSVD->getInitStyle()); 15424 } else if (!Init) { 15425 S.ActOnUninitializedDecl(RHSVD); 15426 // Store initializer for single element in private copy. Will be used 15427 // during codegen. 15428 PrivateVD->setInit(RHSVD->getInit()); 15429 PrivateVD->setInitStyle(RHSVD->getInitStyle()); 15430 } 15431 if (RHSVD->isInvalidDecl()) 15432 continue; 15433 if (!RHSVD->hasInit() && 15434 (DeclareReductionRef.isUnset() || !S.LangOpts.CPlusPlus)) { 15435 S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible) 15436 << Type << ReductionIdRange; 15437 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 15438 VarDecl::DeclarationOnly; 15439 S.Diag(D->getLocation(), 15440 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 15441 << D; 15442 continue; 15443 } 15444 DeclRefExpr *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc); 15445 ExprResult ReductionOp; 15446 if (DeclareReductionRef.isUsable()) { 15447 QualType RedTy = DeclareReductionRef.get()->getType(); 15448 QualType PtrRedTy = Context.getPointerType(RedTy); 15449 ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE); 15450 ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE); 15451 if (!BasePath.empty()) { 15452 LHS = S.DefaultLvalueConversion(LHS.get()); 15453 RHS = S.DefaultLvalueConversion(RHS.get()); 15454 LHS = ImplicitCastExpr::Create( 15455 Context, PtrRedTy, CK_UncheckedDerivedToBase, LHS.get(), &BasePath, 15456 LHS.get()->getValueKind(), FPOptionsOverride()); 15457 RHS = ImplicitCastExpr::Create( 15458 Context, PtrRedTy, CK_UncheckedDerivedToBase, RHS.get(), &BasePath, 15459 RHS.get()->getValueKind(), FPOptionsOverride()); 15460 } 15461 FunctionProtoType::ExtProtoInfo EPI; 15462 QualType Params[] = {PtrRedTy, PtrRedTy}; 15463 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI); 15464 auto *OVE = new (Context) OpaqueValueExpr( 15465 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary, 15466 S.DefaultLvalueConversion(DeclareReductionRef.get()).get()); 15467 Expr *Args[] = {LHS.get(), RHS.get()}; 15468 ReductionOp = 15469 CallExpr::Create(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc, 15470 S.CurFPFeatureOverrides()); 15471 } else { 15472 ReductionOp = S.BuildBinOp( 15473 Stack->getCurScope(), ReductionId.getBeginLoc(), BOK, LHSDRE, RHSDRE); 15474 if (ReductionOp.isUsable()) { 15475 if (BOK != BO_LT && BOK != BO_GT) { 15476 ReductionOp = 15477 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(), 15478 BO_Assign, LHSDRE, ReductionOp.get()); 15479 } else { 15480 auto *ConditionalOp = new (Context) 15481 ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc, RHSDRE, 15482 Type, VK_LValue, OK_Ordinary); 15483 ReductionOp = 15484 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(), 15485 BO_Assign, LHSDRE, ConditionalOp); 15486 } 15487 if (ReductionOp.isUsable()) 15488 ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get(), 15489 /*DiscardedValue*/ false); 15490 } 15491 if (!ReductionOp.isUsable()) 15492 continue; 15493 } 15494 15495 // Add copy operations for inscan reductions. 15496 // LHS = RHS; 15497 ExprResult CopyOpRes, TempArrayRes, TempArrayElem; 15498 if (ClauseKind == OMPC_reduction && 15499 RD.RedModifier == OMPC_REDUCTION_inscan) { 15500 ExprResult RHS = S.DefaultLvalueConversion(RHSDRE); 15501 CopyOpRes = S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, LHSDRE, 15502 RHS.get()); 15503 if (!CopyOpRes.isUsable()) 15504 continue; 15505 CopyOpRes = 15506 S.ActOnFinishFullExpr(CopyOpRes.get(), /*DiscardedValue=*/true); 15507 if (!CopyOpRes.isUsable()) 15508 continue; 15509 // For simd directive and simd-based directives in simd mode no need to 15510 // construct temp array, need just a single temp element. 15511 if (Stack->getCurrentDirective() == OMPD_simd || 15512 (S.getLangOpts().OpenMPSimd && 15513 isOpenMPSimdDirective(Stack->getCurrentDirective()))) { 15514 VarDecl *TempArrayVD = 15515 buildVarDecl(S, ELoc, PrivateTy, D->getName(), 15516 D->hasAttrs() ? &D->getAttrs() : nullptr); 15517 // Add a constructor to the temp decl. 15518 S.ActOnUninitializedDecl(TempArrayVD); 15519 TempArrayRes = buildDeclRefExpr(S, TempArrayVD, PrivateTy, ELoc); 15520 } else { 15521 // Build temp array for prefix sum. 15522 auto *Dim = new (S.Context) 15523 OpaqueValueExpr(ELoc, S.Context.getSizeType(), VK_RValue); 15524 QualType ArrayTy = 15525 S.Context.getVariableArrayType(PrivateTy, Dim, ArrayType::Normal, 15526 /*IndexTypeQuals=*/0, {ELoc, ELoc}); 15527 VarDecl *TempArrayVD = 15528 buildVarDecl(S, ELoc, ArrayTy, D->getName(), 15529 D->hasAttrs() ? &D->getAttrs() : nullptr); 15530 // Add a constructor to the temp decl. 15531 S.ActOnUninitializedDecl(TempArrayVD); 15532 TempArrayRes = buildDeclRefExpr(S, TempArrayVD, ArrayTy, ELoc); 15533 TempArrayElem = 15534 S.DefaultFunctionArrayLvalueConversion(TempArrayRes.get()); 15535 auto *Idx = new (S.Context) 15536 OpaqueValueExpr(ELoc, S.Context.getSizeType(), VK_RValue); 15537 TempArrayElem = S.CreateBuiltinArraySubscriptExpr(TempArrayElem.get(), 15538 ELoc, Idx, ELoc); 15539 } 15540 } 15541 15542 // OpenMP [2.15.4.6, Restrictions, p.2] 15543 // A list item that appears in an in_reduction clause of a task construct 15544 // must appear in a task_reduction clause of a construct associated with a 15545 // taskgroup region that includes the participating task in its taskgroup 15546 // set. The construct associated with the innermost region that meets this 15547 // condition must specify the same reduction-identifier as the in_reduction 15548 // clause. 15549 if (ClauseKind == OMPC_in_reduction) { 15550 SourceRange ParentSR; 15551 BinaryOperatorKind ParentBOK; 15552 const Expr *ParentReductionOp = nullptr; 15553 Expr *ParentBOKTD = nullptr, *ParentReductionOpTD = nullptr; 15554 DSAStackTy::DSAVarData ParentBOKDSA = 15555 Stack->getTopMostTaskgroupReductionData(D, ParentSR, ParentBOK, 15556 ParentBOKTD); 15557 DSAStackTy::DSAVarData ParentReductionOpDSA = 15558 Stack->getTopMostTaskgroupReductionData( 15559 D, ParentSR, ParentReductionOp, ParentReductionOpTD); 15560 bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown; 15561 bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown; 15562 if ((DeclareReductionRef.isUnset() && IsParentReductionOp) || 15563 (DeclareReductionRef.isUsable() && IsParentBOK) || 15564 (IsParentBOK && BOK != ParentBOK) || IsParentReductionOp) { 15565 bool EmitError = true; 15566 if (IsParentReductionOp && DeclareReductionRef.isUsable()) { 15567 llvm::FoldingSetNodeID RedId, ParentRedId; 15568 ParentReductionOp->Profile(ParentRedId, Context, /*Canonical=*/true); 15569 DeclareReductionRef.get()->Profile(RedId, Context, 15570 /*Canonical=*/true); 15571 EmitError = RedId != ParentRedId; 15572 } 15573 if (EmitError) { 15574 S.Diag(ReductionId.getBeginLoc(), 15575 diag::err_omp_reduction_identifier_mismatch) 15576 << ReductionIdRange << RefExpr->getSourceRange(); 15577 S.Diag(ParentSR.getBegin(), 15578 diag::note_omp_previous_reduction_identifier) 15579 << ParentSR 15580 << (IsParentBOK ? ParentBOKDSA.RefExpr 15581 : ParentReductionOpDSA.RefExpr) 15582 ->getSourceRange(); 15583 continue; 15584 } 15585 } 15586 TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD; 15587 } 15588 15589 DeclRefExpr *Ref = nullptr; 15590 Expr *VarsExpr = RefExpr->IgnoreParens(); 15591 if (!VD && !S.CurContext->isDependentContext()) { 15592 if (ASE || OASE) { 15593 TransformExprToCaptures RebuildToCapture(S, D); 15594 VarsExpr = 15595 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get(); 15596 Ref = RebuildToCapture.getCapturedExpr(); 15597 } else { 15598 VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false); 15599 } 15600 if (!S.isOpenMPCapturedDecl(D)) { 15601 RD.ExprCaptures.emplace_back(Ref->getDecl()); 15602 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) { 15603 ExprResult RefRes = S.DefaultLvalueConversion(Ref); 15604 if (!RefRes.isUsable()) 15605 continue; 15606 ExprResult PostUpdateRes = 15607 S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr, 15608 RefRes.get()); 15609 if (!PostUpdateRes.isUsable()) 15610 continue; 15611 if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) || 15612 Stack->getCurrentDirective() == OMPD_taskgroup) { 15613 S.Diag(RefExpr->getExprLoc(), 15614 diag::err_omp_reduction_non_addressable_expression) 15615 << RefExpr->getSourceRange(); 15616 continue; 15617 } 15618 RD.ExprPostUpdates.emplace_back( 15619 S.IgnoredValueConversions(PostUpdateRes.get()).get()); 15620 } 15621 } 15622 } 15623 // All reduction items are still marked as reduction (to do not increase 15624 // code base size). 15625 unsigned Modifier = RD.RedModifier; 15626 // Consider task_reductions as reductions with task modifier. Required for 15627 // correct analysis of in_reduction clauses. 15628 if (CurrDir == OMPD_taskgroup && ClauseKind == OMPC_task_reduction) 15629 Modifier = OMPC_REDUCTION_task; 15630 Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref, Modifier, 15631 ASE || OASE); 15632 if (Modifier == OMPC_REDUCTION_task && 15633 (CurrDir == OMPD_taskgroup || 15634 ((isOpenMPParallelDirective(CurrDir) || 15635 isOpenMPWorksharingDirective(CurrDir)) && 15636 !isOpenMPSimdDirective(CurrDir)))) { 15637 if (DeclareReductionRef.isUsable()) 15638 Stack->addTaskgroupReductionData(D, ReductionIdRange, 15639 DeclareReductionRef.get()); 15640 else 15641 Stack->addTaskgroupReductionData(D, ReductionIdRange, BOK); 15642 } 15643 RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get(), 15644 TaskgroupDescriptor, CopyOpRes.get(), TempArrayRes.get(), 15645 TempArrayElem.get()); 15646 } 15647 return RD.Vars.empty(); 15648 } 15649 15650 OMPClause *Sema::ActOnOpenMPReductionClause( 15651 ArrayRef<Expr *> VarList, OpenMPReductionClauseModifier Modifier, 15652 SourceLocation StartLoc, SourceLocation LParenLoc, 15653 SourceLocation ModifierLoc, SourceLocation ColonLoc, SourceLocation EndLoc, 15654 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 15655 ArrayRef<Expr *> UnresolvedReductions) { 15656 if (ModifierLoc.isValid() && Modifier == OMPC_REDUCTION_unknown) { 15657 Diag(LParenLoc, diag::err_omp_unexpected_clause_value) 15658 << getListOfPossibleValues(OMPC_reduction, /*First=*/0, 15659 /*Last=*/OMPC_REDUCTION_unknown) 15660 << getOpenMPClauseName(OMPC_reduction); 15661 return nullptr; 15662 } 15663 // OpenMP 5.0, 2.19.5.4 reduction Clause, Restrictions 15664 // A reduction clause with the inscan reduction-modifier may only appear on a 15665 // worksharing-loop construct, a worksharing-loop SIMD construct, a simd 15666 // construct, a parallel worksharing-loop construct or a parallel 15667 // worksharing-loop SIMD construct. 15668 if (Modifier == OMPC_REDUCTION_inscan && 15669 (DSAStack->getCurrentDirective() != OMPD_for && 15670 DSAStack->getCurrentDirective() != OMPD_for_simd && 15671 DSAStack->getCurrentDirective() != OMPD_simd && 15672 DSAStack->getCurrentDirective() != OMPD_parallel_for && 15673 DSAStack->getCurrentDirective() != OMPD_parallel_for_simd)) { 15674 Diag(ModifierLoc, diag::err_omp_wrong_inscan_reduction); 15675 return nullptr; 15676 } 15677 15678 ReductionData RD(VarList.size(), Modifier); 15679 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList, 15680 StartLoc, LParenLoc, ColonLoc, EndLoc, 15681 ReductionIdScopeSpec, ReductionId, 15682 UnresolvedReductions, RD)) 15683 return nullptr; 15684 15685 return OMPReductionClause::Create( 15686 Context, StartLoc, LParenLoc, ModifierLoc, ColonLoc, EndLoc, Modifier, 15687 RD.Vars, ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 15688 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.InscanCopyOps, 15689 RD.InscanCopyArrayTemps, RD.InscanCopyArrayElems, 15690 buildPreInits(Context, RD.ExprCaptures), 15691 buildPostUpdate(*this, RD.ExprPostUpdates)); 15692 } 15693 15694 OMPClause *Sema::ActOnOpenMPTaskReductionClause( 15695 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 15696 SourceLocation ColonLoc, SourceLocation EndLoc, 15697 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 15698 ArrayRef<Expr *> UnresolvedReductions) { 15699 ReductionData RD(VarList.size()); 15700 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction, VarList, 15701 StartLoc, LParenLoc, ColonLoc, EndLoc, 15702 ReductionIdScopeSpec, ReductionId, 15703 UnresolvedReductions, RD)) 15704 return nullptr; 15705 15706 return OMPTaskReductionClause::Create( 15707 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 15708 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 15709 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, 15710 buildPreInits(Context, RD.ExprCaptures), 15711 buildPostUpdate(*this, RD.ExprPostUpdates)); 15712 } 15713 15714 OMPClause *Sema::ActOnOpenMPInReductionClause( 15715 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 15716 SourceLocation ColonLoc, SourceLocation EndLoc, 15717 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 15718 ArrayRef<Expr *> UnresolvedReductions) { 15719 ReductionData RD(VarList.size()); 15720 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_in_reduction, VarList, 15721 StartLoc, LParenLoc, ColonLoc, EndLoc, 15722 ReductionIdScopeSpec, ReductionId, 15723 UnresolvedReductions, RD)) 15724 return nullptr; 15725 15726 return OMPInReductionClause::Create( 15727 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 15728 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 15729 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.TaskgroupDescriptors, 15730 buildPreInits(Context, RD.ExprCaptures), 15731 buildPostUpdate(*this, RD.ExprPostUpdates)); 15732 } 15733 15734 bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind, 15735 SourceLocation LinLoc) { 15736 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) || 15737 LinKind == OMPC_LINEAR_unknown) { 15738 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus; 15739 return true; 15740 } 15741 return false; 15742 } 15743 15744 bool Sema::CheckOpenMPLinearDecl(const ValueDecl *D, SourceLocation ELoc, 15745 OpenMPLinearClauseKind LinKind, QualType Type, 15746 bool IsDeclareSimd) { 15747 const auto *VD = dyn_cast_or_null<VarDecl>(D); 15748 // A variable must not have an incomplete type or a reference type. 15749 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type)) 15750 return true; 15751 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) && 15752 !Type->isReferenceType()) { 15753 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference) 15754 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind); 15755 return true; 15756 } 15757 Type = Type.getNonReferenceType(); 15758 15759 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions] 15760 // A variable that is privatized must not have a const-qualified type 15761 // unless it is of class type with a mutable member. This restriction does 15762 // not apply to the firstprivate clause, nor to the linear clause on 15763 // declarative directives (like declare simd). 15764 if (!IsDeclareSimd && 15765 rejectConstNotMutableType(*this, D, Type, OMPC_linear, ELoc)) 15766 return true; 15767 15768 // A list item must be of integral or pointer type. 15769 Type = Type.getUnqualifiedType().getCanonicalType(); 15770 const auto *Ty = Type.getTypePtrOrNull(); 15771 if (!Ty || (LinKind != OMPC_LINEAR_ref && !Ty->isDependentType() && 15772 !Ty->isIntegralType(Context) && !Ty->isPointerType())) { 15773 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type; 15774 if (D) { 15775 bool IsDecl = 15776 !VD || 15777 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 15778 Diag(D->getLocation(), 15779 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 15780 << D; 15781 } 15782 return true; 15783 } 15784 return false; 15785 } 15786 15787 OMPClause *Sema::ActOnOpenMPLinearClause( 15788 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc, 15789 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind, 15790 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 15791 SmallVector<Expr *, 8> Vars; 15792 SmallVector<Expr *, 8> Privates; 15793 SmallVector<Expr *, 8> Inits; 15794 SmallVector<Decl *, 4> ExprCaptures; 15795 SmallVector<Expr *, 4> ExprPostUpdates; 15796 if (CheckOpenMPLinearModifier(LinKind, LinLoc)) 15797 LinKind = OMPC_LINEAR_val; 15798 for (Expr *RefExpr : VarList) { 15799 assert(RefExpr && "NULL expr in OpenMP linear clause."); 15800 SourceLocation ELoc; 15801 SourceRange ERange; 15802 Expr *SimpleRefExpr = RefExpr; 15803 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 15804 if (Res.second) { 15805 // It will be analyzed later. 15806 Vars.push_back(RefExpr); 15807 Privates.push_back(nullptr); 15808 Inits.push_back(nullptr); 15809 } 15810 ValueDecl *D = Res.first; 15811 if (!D) 15812 continue; 15813 15814 QualType Type = D->getType(); 15815 auto *VD = dyn_cast<VarDecl>(D); 15816 15817 // OpenMP [2.14.3.7, linear clause] 15818 // A list-item cannot appear in more than one linear clause. 15819 // A list-item that appears in a linear clause cannot appear in any 15820 // other data-sharing attribute clause. 15821 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 15822 if (DVar.RefExpr) { 15823 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 15824 << getOpenMPClauseName(OMPC_linear); 15825 reportOriginalDsa(*this, DSAStack, D, DVar); 15826 continue; 15827 } 15828 15829 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type)) 15830 continue; 15831 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType(); 15832 15833 // Build private copy of original var. 15834 VarDecl *Private = 15835 buildVarDecl(*this, ELoc, Type, D->getName(), 15836 D->hasAttrs() ? &D->getAttrs() : nullptr, 15837 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 15838 DeclRefExpr *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc); 15839 // Build var to save initial value. 15840 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start"); 15841 Expr *InitExpr; 15842 DeclRefExpr *Ref = nullptr; 15843 if (!VD && !CurContext->isDependentContext()) { 15844 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 15845 if (!isOpenMPCapturedDecl(D)) { 15846 ExprCaptures.push_back(Ref->getDecl()); 15847 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) { 15848 ExprResult RefRes = DefaultLvalueConversion(Ref); 15849 if (!RefRes.isUsable()) 15850 continue; 15851 ExprResult PostUpdateRes = 15852 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, 15853 SimpleRefExpr, RefRes.get()); 15854 if (!PostUpdateRes.isUsable()) 15855 continue; 15856 ExprPostUpdates.push_back( 15857 IgnoredValueConversions(PostUpdateRes.get()).get()); 15858 } 15859 } 15860 } 15861 if (LinKind == OMPC_LINEAR_uval) 15862 InitExpr = VD ? VD->getInit() : SimpleRefExpr; 15863 else 15864 InitExpr = VD ? SimpleRefExpr : Ref; 15865 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(), 15866 /*DirectInit=*/false); 15867 DeclRefExpr *InitRef = buildDeclRefExpr(*this, Init, Type, ELoc); 15868 15869 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref); 15870 Vars.push_back((VD || CurContext->isDependentContext()) 15871 ? RefExpr->IgnoreParens() 15872 : Ref); 15873 Privates.push_back(PrivateRef); 15874 Inits.push_back(InitRef); 15875 } 15876 15877 if (Vars.empty()) 15878 return nullptr; 15879 15880 Expr *StepExpr = Step; 15881 Expr *CalcStepExpr = nullptr; 15882 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && 15883 !Step->isInstantiationDependent() && 15884 !Step->containsUnexpandedParameterPack()) { 15885 SourceLocation StepLoc = Step->getBeginLoc(); 15886 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step); 15887 if (Val.isInvalid()) 15888 return nullptr; 15889 StepExpr = Val.get(); 15890 15891 // Build var to save the step value. 15892 VarDecl *SaveVar = 15893 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step"); 15894 ExprResult SaveRef = 15895 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc); 15896 ExprResult CalcStep = 15897 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr); 15898 CalcStep = ActOnFinishFullExpr(CalcStep.get(), /*DiscardedValue*/ false); 15899 15900 // Warn about zero linear step (it would be probably better specified as 15901 // making corresponding variables 'const'). 15902 if (Optional<llvm::APSInt> Result = 15903 StepExpr->getIntegerConstantExpr(Context)) { 15904 if (!Result->isNegative() && !Result->isStrictlyPositive()) 15905 Diag(StepLoc, diag::warn_omp_linear_step_zero) 15906 << Vars[0] << (Vars.size() > 1); 15907 } else if (CalcStep.isUsable()) { 15908 // Calculate the step beforehand instead of doing this on each iteration. 15909 // (This is not used if the number of iterations may be kfold-ed). 15910 CalcStepExpr = CalcStep.get(); 15911 } 15912 } 15913 15914 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc, 15915 ColonLoc, EndLoc, Vars, Privates, Inits, 15916 StepExpr, CalcStepExpr, 15917 buildPreInits(Context, ExprCaptures), 15918 buildPostUpdate(*this, ExprPostUpdates)); 15919 } 15920 15921 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, 15922 Expr *NumIterations, Sema &SemaRef, 15923 Scope *S, DSAStackTy *Stack) { 15924 // Walk the vars and build update/final expressions for the CodeGen. 15925 SmallVector<Expr *, 8> Updates; 15926 SmallVector<Expr *, 8> Finals; 15927 SmallVector<Expr *, 8> UsedExprs; 15928 Expr *Step = Clause.getStep(); 15929 Expr *CalcStep = Clause.getCalcStep(); 15930 // OpenMP [2.14.3.7, linear clause] 15931 // If linear-step is not specified it is assumed to be 1. 15932 if (!Step) 15933 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(); 15934 else if (CalcStep) 15935 Step = cast<BinaryOperator>(CalcStep)->getLHS(); 15936 bool HasErrors = false; 15937 auto CurInit = Clause.inits().begin(); 15938 auto CurPrivate = Clause.privates().begin(); 15939 OpenMPLinearClauseKind LinKind = Clause.getModifier(); 15940 for (Expr *RefExpr : Clause.varlists()) { 15941 SourceLocation ELoc; 15942 SourceRange ERange; 15943 Expr *SimpleRefExpr = RefExpr; 15944 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange); 15945 ValueDecl *D = Res.first; 15946 if (Res.second || !D) { 15947 Updates.push_back(nullptr); 15948 Finals.push_back(nullptr); 15949 HasErrors = true; 15950 continue; 15951 } 15952 auto &&Info = Stack->isLoopControlVariable(D); 15953 // OpenMP [2.15.11, distribute simd Construct] 15954 // A list item may not appear in a linear clause, unless it is the loop 15955 // iteration variable. 15956 if (isOpenMPDistributeDirective(Stack->getCurrentDirective()) && 15957 isOpenMPSimdDirective(Stack->getCurrentDirective()) && !Info.first) { 15958 SemaRef.Diag(ELoc, 15959 diag::err_omp_linear_distribute_var_non_loop_iteration); 15960 Updates.push_back(nullptr); 15961 Finals.push_back(nullptr); 15962 HasErrors = true; 15963 continue; 15964 } 15965 Expr *InitExpr = *CurInit; 15966 15967 // Build privatized reference to the current linear var. 15968 auto *DE = cast<DeclRefExpr>(SimpleRefExpr); 15969 Expr *CapturedRef; 15970 if (LinKind == OMPC_LINEAR_uval) 15971 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit(); 15972 else 15973 CapturedRef = 15974 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()), 15975 DE->getType().getUnqualifiedType(), DE->getExprLoc(), 15976 /*RefersToCapture=*/true); 15977 15978 // Build update: Var = InitExpr + IV * Step 15979 ExprResult Update; 15980 if (!Info.first) 15981 Update = buildCounterUpdate( 15982 SemaRef, S, RefExpr->getExprLoc(), *CurPrivate, InitExpr, IV, Step, 15983 /*Subtract=*/false, /*IsNonRectangularLB=*/false); 15984 else 15985 Update = *CurPrivate; 15986 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getBeginLoc(), 15987 /*DiscardedValue*/ false); 15988 15989 // Build final: Var = InitExpr + NumIterations * Step 15990 ExprResult Final; 15991 if (!Info.first) 15992 Final = 15993 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef, 15994 InitExpr, NumIterations, Step, /*Subtract=*/false, 15995 /*IsNonRectangularLB=*/false); 15996 else 15997 Final = *CurPrivate; 15998 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getBeginLoc(), 15999 /*DiscardedValue*/ false); 16000 16001 if (!Update.isUsable() || !Final.isUsable()) { 16002 Updates.push_back(nullptr); 16003 Finals.push_back(nullptr); 16004 UsedExprs.push_back(nullptr); 16005 HasErrors = true; 16006 } else { 16007 Updates.push_back(Update.get()); 16008 Finals.push_back(Final.get()); 16009 if (!Info.first) 16010 UsedExprs.push_back(SimpleRefExpr); 16011 } 16012 ++CurInit; 16013 ++CurPrivate; 16014 } 16015 if (Expr *S = Clause.getStep()) 16016 UsedExprs.push_back(S); 16017 // Fill the remaining part with the nullptr. 16018 UsedExprs.append(Clause.varlist_size() + 1 - UsedExprs.size(), nullptr); 16019 Clause.setUpdates(Updates); 16020 Clause.setFinals(Finals); 16021 Clause.setUsedExprs(UsedExprs); 16022 return HasErrors; 16023 } 16024 16025 OMPClause *Sema::ActOnOpenMPAlignedClause( 16026 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc, 16027 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 16028 SmallVector<Expr *, 8> Vars; 16029 for (Expr *RefExpr : VarList) { 16030 assert(RefExpr && "NULL expr in OpenMP linear clause."); 16031 SourceLocation ELoc; 16032 SourceRange ERange; 16033 Expr *SimpleRefExpr = RefExpr; 16034 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 16035 if (Res.second) { 16036 // It will be analyzed later. 16037 Vars.push_back(RefExpr); 16038 } 16039 ValueDecl *D = Res.first; 16040 if (!D) 16041 continue; 16042 16043 QualType QType = D->getType(); 16044 auto *VD = dyn_cast<VarDecl>(D); 16045 16046 // OpenMP [2.8.1, simd construct, Restrictions] 16047 // The type of list items appearing in the aligned clause must be 16048 // array, pointer, reference to array, or reference to pointer. 16049 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType(); 16050 const Type *Ty = QType.getTypePtrOrNull(); 16051 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) { 16052 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr) 16053 << QType << getLangOpts().CPlusPlus << ERange; 16054 bool IsDecl = 16055 !VD || 16056 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 16057 Diag(D->getLocation(), 16058 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 16059 << D; 16060 continue; 16061 } 16062 16063 // OpenMP [2.8.1, simd construct, Restrictions] 16064 // A list-item cannot appear in more than one aligned clause. 16065 if (const Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) { 16066 Diag(ELoc, diag::err_omp_used_in_clause_twice) 16067 << 0 << getOpenMPClauseName(OMPC_aligned) << ERange; 16068 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa) 16069 << getOpenMPClauseName(OMPC_aligned); 16070 continue; 16071 } 16072 16073 DeclRefExpr *Ref = nullptr; 16074 if (!VD && isOpenMPCapturedDecl(D)) 16075 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 16076 Vars.push_back(DefaultFunctionArrayConversion( 16077 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref) 16078 .get()); 16079 } 16080 16081 // OpenMP [2.8.1, simd construct, Description] 16082 // The parameter of the aligned clause, alignment, must be a constant 16083 // positive integer expression. 16084 // If no optional parameter is specified, implementation-defined default 16085 // alignments for SIMD instructions on the target platforms are assumed. 16086 if (Alignment != nullptr) { 16087 ExprResult AlignResult = 16088 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned); 16089 if (AlignResult.isInvalid()) 16090 return nullptr; 16091 Alignment = AlignResult.get(); 16092 } 16093 if (Vars.empty()) 16094 return nullptr; 16095 16096 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc, 16097 EndLoc, Vars, Alignment); 16098 } 16099 16100 OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList, 16101 SourceLocation StartLoc, 16102 SourceLocation LParenLoc, 16103 SourceLocation EndLoc) { 16104 SmallVector<Expr *, 8> Vars; 16105 SmallVector<Expr *, 8> SrcExprs; 16106 SmallVector<Expr *, 8> DstExprs; 16107 SmallVector<Expr *, 8> AssignmentOps; 16108 for (Expr *RefExpr : VarList) { 16109 assert(RefExpr && "NULL expr in OpenMP copyin clause."); 16110 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 16111 // It will be analyzed later. 16112 Vars.push_back(RefExpr); 16113 SrcExprs.push_back(nullptr); 16114 DstExprs.push_back(nullptr); 16115 AssignmentOps.push_back(nullptr); 16116 continue; 16117 } 16118 16119 SourceLocation ELoc = RefExpr->getExprLoc(); 16120 // OpenMP [2.1, C/C++] 16121 // A list item is a variable name. 16122 // OpenMP [2.14.4.1, Restrictions, p.1] 16123 // A list item that appears in a copyin clause must be threadprivate. 16124 auto *DE = dyn_cast<DeclRefExpr>(RefExpr); 16125 if (!DE || !isa<VarDecl>(DE->getDecl())) { 16126 Diag(ELoc, diag::err_omp_expected_var_name_member_expr) 16127 << 0 << RefExpr->getSourceRange(); 16128 continue; 16129 } 16130 16131 Decl *D = DE->getDecl(); 16132 auto *VD = cast<VarDecl>(D); 16133 16134 QualType Type = VD->getType(); 16135 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 16136 // It will be analyzed later. 16137 Vars.push_back(DE); 16138 SrcExprs.push_back(nullptr); 16139 DstExprs.push_back(nullptr); 16140 AssignmentOps.push_back(nullptr); 16141 continue; 16142 } 16143 16144 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1] 16145 // A list item that appears in a copyin clause must be threadprivate. 16146 if (!DSAStack->isThreadPrivate(VD)) { 16147 Diag(ELoc, diag::err_omp_required_access) 16148 << getOpenMPClauseName(OMPC_copyin) 16149 << getOpenMPDirectiveName(OMPD_threadprivate); 16150 continue; 16151 } 16152 16153 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 16154 // A variable of class type (or array thereof) that appears in a 16155 // copyin clause requires an accessible, unambiguous copy assignment 16156 // operator for the class type. 16157 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType(); 16158 VarDecl *SrcVD = 16159 buildVarDecl(*this, DE->getBeginLoc(), ElemType.getUnqualifiedType(), 16160 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr); 16161 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr( 16162 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc()); 16163 VarDecl *DstVD = 16164 buildVarDecl(*this, DE->getBeginLoc(), ElemType, ".copyin.dst", 16165 VD->hasAttrs() ? &VD->getAttrs() : nullptr); 16166 DeclRefExpr *PseudoDstExpr = 16167 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc()); 16168 // For arrays generate assignment operation for single element and replace 16169 // it by the original array element in CodeGen. 16170 ExprResult AssignmentOp = 16171 BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, PseudoDstExpr, 16172 PseudoSrcExpr); 16173 if (AssignmentOp.isInvalid()) 16174 continue; 16175 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(), 16176 /*DiscardedValue*/ false); 16177 if (AssignmentOp.isInvalid()) 16178 continue; 16179 16180 DSAStack->addDSA(VD, DE, OMPC_copyin); 16181 Vars.push_back(DE); 16182 SrcExprs.push_back(PseudoSrcExpr); 16183 DstExprs.push_back(PseudoDstExpr); 16184 AssignmentOps.push_back(AssignmentOp.get()); 16185 } 16186 16187 if (Vars.empty()) 16188 return nullptr; 16189 16190 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 16191 SrcExprs, DstExprs, AssignmentOps); 16192 } 16193 16194 OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList, 16195 SourceLocation StartLoc, 16196 SourceLocation LParenLoc, 16197 SourceLocation EndLoc) { 16198 SmallVector<Expr *, 8> Vars; 16199 SmallVector<Expr *, 8> SrcExprs; 16200 SmallVector<Expr *, 8> DstExprs; 16201 SmallVector<Expr *, 8> AssignmentOps; 16202 for (Expr *RefExpr : VarList) { 16203 assert(RefExpr && "NULL expr in OpenMP linear clause."); 16204 SourceLocation ELoc; 16205 SourceRange ERange; 16206 Expr *SimpleRefExpr = RefExpr; 16207 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 16208 if (Res.second) { 16209 // It will be analyzed later. 16210 Vars.push_back(RefExpr); 16211 SrcExprs.push_back(nullptr); 16212 DstExprs.push_back(nullptr); 16213 AssignmentOps.push_back(nullptr); 16214 } 16215 ValueDecl *D = Res.first; 16216 if (!D) 16217 continue; 16218 16219 QualType Type = D->getType(); 16220 auto *VD = dyn_cast<VarDecl>(D); 16221 16222 // OpenMP [2.14.4.2, Restrictions, p.2] 16223 // A list item that appears in a copyprivate clause may not appear in a 16224 // private or firstprivate clause on the single construct. 16225 if (!VD || !DSAStack->isThreadPrivate(VD)) { 16226 DSAStackTy::DSAVarData DVar = 16227 DSAStack->getTopDSA(D, /*FromParent=*/false); 16228 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate && 16229 DVar.RefExpr) { 16230 Diag(ELoc, diag::err_omp_wrong_dsa) 16231 << getOpenMPClauseName(DVar.CKind) 16232 << getOpenMPClauseName(OMPC_copyprivate); 16233 reportOriginalDsa(*this, DSAStack, D, DVar); 16234 continue; 16235 } 16236 16237 // OpenMP [2.11.4.2, Restrictions, p.1] 16238 // All list items that appear in a copyprivate clause must be either 16239 // threadprivate or private in the enclosing context. 16240 if (DVar.CKind == OMPC_unknown) { 16241 DVar = DSAStack->getImplicitDSA(D, false); 16242 if (DVar.CKind == OMPC_shared) { 16243 Diag(ELoc, diag::err_omp_required_access) 16244 << getOpenMPClauseName(OMPC_copyprivate) 16245 << "threadprivate or private in the enclosing context"; 16246 reportOriginalDsa(*this, DSAStack, D, DVar); 16247 continue; 16248 } 16249 } 16250 } 16251 16252 // Variably modified types are not supported. 16253 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) { 16254 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 16255 << getOpenMPClauseName(OMPC_copyprivate) << Type 16256 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 16257 bool IsDecl = 16258 !VD || 16259 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 16260 Diag(D->getLocation(), 16261 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 16262 << D; 16263 continue; 16264 } 16265 16266 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 16267 // A variable of class type (or array thereof) that appears in a 16268 // copyin clause requires an accessible, unambiguous copy assignment 16269 // operator for the class type. 16270 Type = Context.getBaseElementType(Type.getNonReferenceType()) 16271 .getUnqualifiedType(); 16272 VarDecl *SrcVD = 16273 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.src", 16274 D->hasAttrs() ? &D->getAttrs() : nullptr); 16275 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc); 16276 VarDecl *DstVD = 16277 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.dst", 16278 D->hasAttrs() ? &D->getAttrs() : nullptr); 16279 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc); 16280 ExprResult AssignmentOp = BuildBinOp( 16281 DSAStack->getCurScope(), ELoc, BO_Assign, PseudoDstExpr, PseudoSrcExpr); 16282 if (AssignmentOp.isInvalid()) 16283 continue; 16284 AssignmentOp = 16285 ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false); 16286 if (AssignmentOp.isInvalid()) 16287 continue; 16288 16289 // No need to mark vars as copyprivate, they are already threadprivate or 16290 // implicitly private. 16291 assert(VD || isOpenMPCapturedDecl(D)); 16292 Vars.push_back( 16293 VD ? RefExpr->IgnoreParens() 16294 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false)); 16295 SrcExprs.push_back(PseudoSrcExpr); 16296 DstExprs.push_back(PseudoDstExpr); 16297 AssignmentOps.push_back(AssignmentOp.get()); 16298 } 16299 16300 if (Vars.empty()) 16301 return nullptr; 16302 16303 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 16304 Vars, SrcExprs, DstExprs, AssignmentOps); 16305 } 16306 16307 OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList, 16308 SourceLocation StartLoc, 16309 SourceLocation LParenLoc, 16310 SourceLocation EndLoc) { 16311 if (VarList.empty()) 16312 return nullptr; 16313 16314 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList); 16315 } 16316 16317 /// Tries to find omp_depend_t. type. 16318 static bool findOMPDependT(Sema &S, SourceLocation Loc, DSAStackTy *Stack, 16319 bool Diagnose = true) { 16320 QualType OMPDependT = Stack->getOMPDependT(); 16321 if (!OMPDependT.isNull()) 16322 return true; 16323 IdentifierInfo *II = &S.PP.getIdentifierTable().get("omp_depend_t"); 16324 ParsedType PT = S.getTypeName(*II, Loc, S.getCurScope()); 16325 if (!PT.getAsOpaquePtr() || PT.get().isNull()) { 16326 if (Diagnose) 16327 S.Diag(Loc, diag::err_omp_implied_type_not_found) << "omp_depend_t"; 16328 return false; 16329 } 16330 Stack->setOMPDependT(PT.get()); 16331 return true; 16332 } 16333 16334 OMPClause *Sema::ActOnOpenMPDepobjClause(Expr *Depobj, SourceLocation StartLoc, 16335 SourceLocation LParenLoc, 16336 SourceLocation EndLoc) { 16337 if (!Depobj) 16338 return nullptr; 16339 16340 bool OMPDependTFound = findOMPDependT(*this, StartLoc, DSAStack); 16341 16342 // OpenMP 5.0, 2.17.10.1 depobj Construct 16343 // depobj is an lvalue expression of type omp_depend_t. 16344 if (!Depobj->isTypeDependent() && !Depobj->isValueDependent() && 16345 !Depobj->isInstantiationDependent() && 16346 !Depobj->containsUnexpandedParameterPack() && 16347 (OMPDependTFound && 16348 !Context.typesAreCompatible(DSAStack->getOMPDependT(), Depobj->getType(), 16349 /*CompareUnqualified=*/true))) { 16350 Diag(Depobj->getExprLoc(), diag::err_omp_expected_omp_depend_t_lvalue) 16351 << 0 << Depobj->getType() << Depobj->getSourceRange(); 16352 } 16353 16354 if (!Depobj->isLValue()) { 16355 Diag(Depobj->getExprLoc(), diag::err_omp_expected_omp_depend_t_lvalue) 16356 << 1 << Depobj->getSourceRange(); 16357 } 16358 16359 return OMPDepobjClause::Create(Context, StartLoc, LParenLoc, EndLoc, Depobj); 16360 } 16361 16362 OMPClause * 16363 Sema::ActOnOpenMPDependClause(Expr *DepModifier, OpenMPDependClauseKind DepKind, 16364 SourceLocation DepLoc, SourceLocation ColonLoc, 16365 ArrayRef<Expr *> VarList, SourceLocation StartLoc, 16366 SourceLocation LParenLoc, SourceLocation EndLoc) { 16367 if (DSAStack->getCurrentDirective() == OMPD_ordered && 16368 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) { 16369 Diag(DepLoc, diag::err_omp_unexpected_clause_value) 16370 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend); 16371 return nullptr; 16372 } 16373 if ((DSAStack->getCurrentDirective() != OMPD_ordered || 16374 DSAStack->getCurrentDirective() == OMPD_depobj) && 16375 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source || 16376 DepKind == OMPC_DEPEND_sink || 16377 ((LangOpts.OpenMP < 50 || 16378 DSAStack->getCurrentDirective() == OMPD_depobj) && 16379 DepKind == OMPC_DEPEND_depobj))) { 16380 SmallVector<unsigned, 3> Except; 16381 Except.push_back(OMPC_DEPEND_source); 16382 Except.push_back(OMPC_DEPEND_sink); 16383 if (LangOpts.OpenMP < 50 || DSAStack->getCurrentDirective() == OMPD_depobj) 16384 Except.push_back(OMPC_DEPEND_depobj); 16385 std::string Expected = (LangOpts.OpenMP >= 50 && !DepModifier) 16386 ? "depend modifier(iterator) or " 16387 : ""; 16388 Diag(DepLoc, diag::err_omp_unexpected_clause_value) 16389 << Expected + getListOfPossibleValues(OMPC_depend, /*First=*/0, 16390 /*Last=*/OMPC_DEPEND_unknown, 16391 Except) 16392 << getOpenMPClauseName(OMPC_depend); 16393 return nullptr; 16394 } 16395 if (DepModifier && 16396 (DepKind == OMPC_DEPEND_source || DepKind == OMPC_DEPEND_sink)) { 16397 Diag(DepModifier->getExprLoc(), 16398 diag::err_omp_depend_sink_source_with_modifier); 16399 return nullptr; 16400 } 16401 if (DepModifier && 16402 !DepModifier->getType()->isSpecificBuiltinType(BuiltinType::OMPIterator)) 16403 Diag(DepModifier->getExprLoc(), diag::err_omp_depend_modifier_not_iterator); 16404 16405 SmallVector<Expr *, 8> Vars; 16406 DSAStackTy::OperatorOffsetTy OpsOffs; 16407 llvm::APSInt DepCounter(/*BitWidth=*/32); 16408 llvm::APSInt TotalDepCount(/*BitWidth=*/32); 16409 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) { 16410 if (const Expr *OrderedCountExpr = 16411 DSAStack->getParentOrderedRegionParam().first) { 16412 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context); 16413 TotalDepCount.setIsUnsigned(/*Val=*/true); 16414 } 16415 } 16416 for (Expr *RefExpr : VarList) { 16417 assert(RefExpr && "NULL expr in OpenMP shared clause."); 16418 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 16419 // It will be analyzed later. 16420 Vars.push_back(RefExpr); 16421 continue; 16422 } 16423 16424 SourceLocation ELoc = RefExpr->getExprLoc(); 16425 Expr *SimpleExpr = RefExpr->IgnoreParenCasts(); 16426 if (DepKind == OMPC_DEPEND_sink) { 16427 if (DSAStack->getParentOrderedRegionParam().first && 16428 DepCounter >= TotalDepCount) { 16429 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr); 16430 continue; 16431 } 16432 ++DepCounter; 16433 // OpenMP [2.13.9, Summary] 16434 // depend(dependence-type : vec), where dependence-type is: 16435 // 'sink' and where vec is the iteration vector, which has the form: 16436 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn] 16437 // where n is the value specified by the ordered clause in the loop 16438 // directive, xi denotes the loop iteration variable of the i-th nested 16439 // loop associated with the loop directive, and di is a constant 16440 // non-negative integer. 16441 if (CurContext->isDependentContext()) { 16442 // It will be analyzed later. 16443 Vars.push_back(RefExpr); 16444 continue; 16445 } 16446 SimpleExpr = SimpleExpr->IgnoreImplicit(); 16447 OverloadedOperatorKind OOK = OO_None; 16448 SourceLocation OOLoc; 16449 Expr *LHS = SimpleExpr; 16450 Expr *RHS = nullptr; 16451 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) { 16452 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode()); 16453 OOLoc = BO->getOperatorLoc(); 16454 LHS = BO->getLHS()->IgnoreParenImpCasts(); 16455 RHS = BO->getRHS()->IgnoreParenImpCasts(); 16456 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) { 16457 OOK = OCE->getOperator(); 16458 OOLoc = OCE->getOperatorLoc(); 16459 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); 16460 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts(); 16461 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) { 16462 OOK = MCE->getMethodDecl() 16463 ->getNameInfo() 16464 .getName() 16465 .getCXXOverloadedOperator(); 16466 OOLoc = MCE->getCallee()->getExprLoc(); 16467 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts(); 16468 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); 16469 } 16470 SourceLocation ELoc; 16471 SourceRange ERange; 16472 auto Res = getPrivateItem(*this, LHS, ELoc, ERange); 16473 if (Res.second) { 16474 // It will be analyzed later. 16475 Vars.push_back(RefExpr); 16476 } 16477 ValueDecl *D = Res.first; 16478 if (!D) 16479 continue; 16480 16481 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) { 16482 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus); 16483 continue; 16484 } 16485 if (RHS) { 16486 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause( 16487 RHS, OMPC_depend, /*StrictlyPositive=*/false); 16488 if (RHSRes.isInvalid()) 16489 continue; 16490 } 16491 if (!CurContext->isDependentContext() && 16492 DSAStack->getParentOrderedRegionParam().first && 16493 DepCounter != DSAStack->isParentLoopControlVariable(D).first) { 16494 const ValueDecl *VD = 16495 DSAStack->getParentLoopControlVariable(DepCounter.getZExtValue()); 16496 if (VD) 16497 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) 16498 << 1 << VD; 16499 else 16500 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) << 0; 16501 continue; 16502 } 16503 OpsOffs.emplace_back(RHS, OOK); 16504 } else { 16505 bool OMPDependTFound = LangOpts.OpenMP >= 50; 16506 if (OMPDependTFound) 16507 OMPDependTFound = findOMPDependT(*this, StartLoc, DSAStack, 16508 DepKind == OMPC_DEPEND_depobj); 16509 if (DepKind == OMPC_DEPEND_depobj) { 16510 // OpenMP 5.0, 2.17.11 depend Clause, Restrictions, C/C++ 16511 // List items used in depend clauses with the depobj dependence type 16512 // must be expressions of the omp_depend_t type. 16513 if (!RefExpr->isValueDependent() && !RefExpr->isTypeDependent() && 16514 !RefExpr->isInstantiationDependent() && 16515 !RefExpr->containsUnexpandedParameterPack() && 16516 (OMPDependTFound && 16517 !Context.hasSameUnqualifiedType(DSAStack->getOMPDependT(), 16518 RefExpr->getType()))) { 16519 Diag(ELoc, diag::err_omp_expected_omp_depend_t_lvalue) 16520 << 0 << RefExpr->getType() << RefExpr->getSourceRange(); 16521 continue; 16522 } 16523 if (!RefExpr->isLValue()) { 16524 Diag(ELoc, diag::err_omp_expected_omp_depend_t_lvalue) 16525 << 1 << RefExpr->getType() << RefExpr->getSourceRange(); 16526 continue; 16527 } 16528 } else { 16529 // OpenMP 5.0 [2.17.11, Restrictions] 16530 // List items used in depend clauses cannot be zero-length array 16531 // sections. 16532 QualType ExprTy = RefExpr->getType().getNonReferenceType(); 16533 const auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr); 16534 if (OASE) { 16535 QualType BaseType = 16536 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 16537 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 16538 ExprTy = ATy->getElementType(); 16539 else 16540 ExprTy = BaseType->getPointeeType(); 16541 ExprTy = ExprTy.getNonReferenceType(); 16542 const Expr *Length = OASE->getLength(); 16543 Expr::EvalResult Result; 16544 if (Length && !Length->isValueDependent() && 16545 Length->EvaluateAsInt(Result, Context) && 16546 Result.Val.getInt().isNullValue()) { 16547 Diag(ELoc, 16548 diag::err_omp_depend_zero_length_array_section_not_allowed) 16549 << SimpleExpr->getSourceRange(); 16550 continue; 16551 } 16552 } 16553 16554 // OpenMP 5.0, 2.17.11 depend Clause, Restrictions, C/C++ 16555 // List items used in depend clauses with the in, out, inout or 16556 // mutexinoutset dependence types cannot be expressions of the 16557 // omp_depend_t type. 16558 if (!RefExpr->isValueDependent() && !RefExpr->isTypeDependent() && 16559 !RefExpr->isInstantiationDependent() && 16560 !RefExpr->containsUnexpandedParameterPack() && 16561 (OMPDependTFound && 16562 DSAStack->getOMPDependT().getTypePtr() == ExprTy.getTypePtr())) { 16563 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 16564 << (LangOpts.OpenMP >= 50 ? 1 : 0) << 1 16565 << RefExpr->getSourceRange(); 16566 continue; 16567 } 16568 16569 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr); 16570 if (!RefExpr->IgnoreParenImpCasts()->isLValue() || 16571 (ASE && !ASE->getBase()->isTypeDependent() && 16572 !ASE->getBase() 16573 ->getType() 16574 .getNonReferenceType() 16575 ->isPointerType() && 16576 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) { 16577 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 16578 << (LangOpts.OpenMP >= 50 ? 1 : 0) 16579 << (LangOpts.OpenMP >= 50 ? 1 : 0) << RefExpr->getSourceRange(); 16580 continue; 16581 } 16582 16583 ExprResult Res; 16584 { 16585 Sema::TentativeAnalysisScope Trap(*this); 16586 Res = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, 16587 RefExpr->IgnoreParenImpCasts()); 16588 } 16589 if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr) && 16590 !isa<OMPArrayShapingExpr>(SimpleExpr)) { 16591 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 16592 << (LangOpts.OpenMP >= 50 ? 1 : 0) 16593 << (LangOpts.OpenMP >= 50 ? 1 : 0) << RefExpr->getSourceRange(); 16594 continue; 16595 } 16596 } 16597 } 16598 Vars.push_back(RefExpr->IgnoreParenImpCasts()); 16599 } 16600 16601 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink && 16602 TotalDepCount > VarList.size() && 16603 DSAStack->getParentOrderedRegionParam().first && 16604 DSAStack->getParentLoopControlVariable(VarList.size() + 1)) { 16605 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration) 16606 << 1 << DSAStack->getParentLoopControlVariable(VarList.size() + 1); 16607 } 16608 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink && 16609 Vars.empty()) 16610 return nullptr; 16611 16612 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, 16613 DepModifier, DepKind, DepLoc, ColonLoc, 16614 Vars, TotalDepCount.getZExtValue()); 16615 if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) && 16616 DSAStack->isParentOrderedRegion()) 16617 DSAStack->addDoacrossDependClause(C, OpsOffs); 16618 return C; 16619 } 16620 16621 OMPClause *Sema::ActOnOpenMPDeviceClause(OpenMPDeviceClauseModifier Modifier, 16622 Expr *Device, SourceLocation StartLoc, 16623 SourceLocation LParenLoc, 16624 SourceLocation ModifierLoc, 16625 SourceLocation EndLoc) { 16626 assert((ModifierLoc.isInvalid() || LangOpts.OpenMP >= 50) && 16627 "Unexpected device modifier in OpenMP < 50."); 16628 16629 bool ErrorFound = false; 16630 if (ModifierLoc.isValid() && Modifier == OMPC_DEVICE_unknown) { 16631 std::string Values = 16632 getListOfPossibleValues(OMPC_device, /*First=*/0, OMPC_DEVICE_unknown); 16633 Diag(ModifierLoc, diag::err_omp_unexpected_clause_value) 16634 << Values << getOpenMPClauseName(OMPC_device); 16635 ErrorFound = true; 16636 } 16637 16638 Expr *ValExpr = Device; 16639 Stmt *HelperValStmt = nullptr; 16640 16641 // OpenMP [2.9.1, Restrictions] 16642 // The device expression must evaluate to a non-negative integer value. 16643 ErrorFound = !isNonNegativeIntegerValue(ValExpr, *this, OMPC_device, 16644 /*StrictlyPositive=*/false) || 16645 ErrorFound; 16646 if (ErrorFound) 16647 return nullptr; 16648 16649 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 16650 OpenMPDirectiveKind CaptureRegion = 16651 getOpenMPCaptureRegionForClause(DKind, OMPC_device, LangOpts.OpenMP); 16652 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 16653 ValExpr = MakeFullExpr(ValExpr).get(); 16654 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 16655 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 16656 HelperValStmt = buildPreInits(Context, Captures); 16657 } 16658 16659 return new (Context) 16660 OMPDeviceClause(Modifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc, 16661 LParenLoc, ModifierLoc, EndLoc); 16662 } 16663 16664 static bool checkTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef, 16665 DSAStackTy *Stack, QualType QTy, 16666 bool FullCheck = true) { 16667 NamedDecl *ND; 16668 if (QTy->isIncompleteType(&ND)) { 16669 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR; 16670 return false; 16671 } 16672 if (FullCheck && !SemaRef.CurContext->isDependentContext() && 16673 !QTy.isTriviallyCopyableType(SemaRef.Context)) 16674 SemaRef.Diag(SL, diag::warn_omp_non_trivial_type_mapped) << QTy << SR; 16675 return true; 16676 } 16677 16678 /// Return true if it can be proven that the provided array expression 16679 /// (array section or array subscript) does NOT specify the whole size of the 16680 /// array whose base type is \a BaseQTy. 16681 static bool checkArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef, 16682 const Expr *E, 16683 QualType BaseQTy) { 16684 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E); 16685 16686 // If this is an array subscript, it refers to the whole size if the size of 16687 // the dimension is constant and equals 1. Also, an array section assumes the 16688 // format of an array subscript if no colon is used. 16689 if (isa<ArraySubscriptExpr>(E) || 16690 (OASE && OASE->getColonLocFirst().isInvalid())) { 16691 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) 16692 return ATy->getSize().getSExtValue() != 1; 16693 // Size can't be evaluated statically. 16694 return false; 16695 } 16696 16697 assert(OASE && "Expecting array section if not an array subscript."); 16698 const Expr *LowerBound = OASE->getLowerBound(); 16699 const Expr *Length = OASE->getLength(); 16700 16701 // If there is a lower bound that does not evaluates to zero, we are not 16702 // covering the whole dimension. 16703 if (LowerBound) { 16704 Expr::EvalResult Result; 16705 if (!LowerBound->EvaluateAsInt(Result, SemaRef.getASTContext())) 16706 return false; // Can't get the integer value as a constant. 16707 16708 llvm::APSInt ConstLowerBound = Result.Val.getInt(); 16709 if (ConstLowerBound.getSExtValue()) 16710 return true; 16711 } 16712 16713 // If we don't have a length we covering the whole dimension. 16714 if (!Length) 16715 return false; 16716 16717 // If the base is a pointer, we don't have a way to get the size of the 16718 // pointee. 16719 if (BaseQTy->isPointerType()) 16720 return false; 16721 16722 // We can only check if the length is the same as the size of the dimension 16723 // if we have a constant array. 16724 const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()); 16725 if (!CATy) 16726 return false; 16727 16728 Expr::EvalResult Result; 16729 if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext())) 16730 return false; // Can't get the integer value as a constant. 16731 16732 llvm::APSInt ConstLength = Result.Val.getInt(); 16733 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue(); 16734 } 16735 16736 // Return true if it can be proven that the provided array expression (array 16737 // section or array subscript) does NOT specify a single element of the array 16738 // whose base type is \a BaseQTy. 16739 static bool checkArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef, 16740 const Expr *E, 16741 QualType BaseQTy) { 16742 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E); 16743 16744 // An array subscript always refer to a single element. Also, an array section 16745 // assumes the format of an array subscript if no colon is used. 16746 if (isa<ArraySubscriptExpr>(E) || 16747 (OASE && OASE->getColonLocFirst().isInvalid())) 16748 return false; 16749 16750 assert(OASE && "Expecting array section if not an array subscript."); 16751 const Expr *Length = OASE->getLength(); 16752 16753 // If we don't have a length we have to check if the array has unitary size 16754 // for this dimension. Also, we should always expect a length if the base type 16755 // is pointer. 16756 if (!Length) { 16757 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) 16758 return ATy->getSize().getSExtValue() != 1; 16759 // We cannot assume anything. 16760 return false; 16761 } 16762 16763 // Check if the length evaluates to 1. 16764 Expr::EvalResult Result; 16765 if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext())) 16766 return false; // Can't get the integer value as a constant. 16767 16768 llvm::APSInt ConstLength = Result.Val.getInt(); 16769 return ConstLength.getSExtValue() != 1; 16770 } 16771 16772 // The base of elements of list in a map clause have to be either: 16773 // - a reference to variable or field. 16774 // - a member expression. 16775 // - an array expression. 16776 // 16777 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the 16778 // reference to 'r'. 16779 // 16780 // If we have: 16781 // 16782 // struct SS { 16783 // Bla S; 16784 // foo() { 16785 // #pragma omp target map (S.Arr[:12]); 16786 // } 16787 // } 16788 // 16789 // We want to retrieve the member expression 'this->S'; 16790 16791 // OpenMP 5.0 [2.19.7.1, map Clause, Restrictions, p.2] 16792 // If a list item is an array section, it must specify contiguous storage. 16793 // 16794 // For this restriction it is sufficient that we make sure only references 16795 // to variables or fields and array expressions, and that no array sections 16796 // exist except in the rightmost expression (unless they cover the whole 16797 // dimension of the array). E.g. these would be invalid: 16798 // 16799 // r.ArrS[3:5].Arr[6:7] 16800 // 16801 // r.ArrS[3:5].x 16802 // 16803 // but these would be valid: 16804 // r.ArrS[3].Arr[6:7] 16805 // 16806 // r.ArrS[3].x 16807 namespace { 16808 class MapBaseChecker final : public StmtVisitor<MapBaseChecker, bool> { 16809 Sema &SemaRef; 16810 OpenMPClauseKind CKind = OMPC_unknown; 16811 OMPClauseMappableExprCommon::MappableExprComponentList &Components; 16812 bool NoDiagnose = false; 16813 const Expr *RelevantExpr = nullptr; 16814 bool AllowUnitySizeArraySection = true; 16815 bool AllowWholeSizeArraySection = true; 16816 SourceLocation ELoc; 16817 SourceRange ERange; 16818 16819 void emitErrorMsg() { 16820 // If nothing else worked, this is not a valid map clause expression. 16821 if (SemaRef.getLangOpts().OpenMP < 50) { 16822 SemaRef.Diag(ELoc, 16823 diag::err_omp_expected_named_var_member_or_array_expression) 16824 << ERange; 16825 } else { 16826 SemaRef.Diag(ELoc, diag::err_omp_non_lvalue_in_map_or_motion_clauses) 16827 << getOpenMPClauseName(CKind) << ERange; 16828 } 16829 } 16830 16831 public: 16832 bool VisitDeclRefExpr(DeclRefExpr *DRE) { 16833 if (!isa<VarDecl>(DRE->getDecl())) { 16834 emitErrorMsg(); 16835 return false; 16836 } 16837 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 16838 RelevantExpr = DRE; 16839 // Record the component. 16840 Components.emplace_back(DRE, DRE->getDecl()); 16841 return true; 16842 } 16843 16844 bool VisitMemberExpr(MemberExpr *ME) { 16845 Expr *E = ME; 16846 Expr *BaseE = ME->getBase()->IgnoreParenCasts(); 16847 16848 if (isa<CXXThisExpr>(BaseE)) { 16849 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 16850 // We found a base expression: this->Val. 16851 RelevantExpr = ME; 16852 } else { 16853 E = BaseE; 16854 } 16855 16856 if (!isa<FieldDecl>(ME->getMemberDecl())) { 16857 if (!NoDiagnose) { 16858 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field) 16859 << ME->getSourceRange(); 16860 return false; 16861 } 16862 if (RelevantExpr) 16863 return false; 16864 return Visit(E); 16865 } 16866 16867 auto *FD = cast<FieldDecl>(ME->getMemberDecl()); 16868 16869 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3] 16870 // A bit-field cannot appear in a map clause. 16871 // 16872 if (FD->isBitField()) { 16873 if (!NoDiagnose) { 16874 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause) 16875 << ME->getSourceRange() << getOpenMPClauseName(CKind); 16876 return false; 16877 } 16878 if (RelevantExpr) 16879 return false; 16880 return Visit(E); 16881 } 16882 16883 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 16884 // If the type of a list item is a reference to a type T then the type 16885 // will be considered to be T for all purposes of this clause. 16886 QualType CurType = BaseE->getType().getNonReferenceType(); 16887 16888 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2] 16889 // A list item cannot be a variable that is a member of a structure with 16890 // a union type. 16891 // 16892 if (CurType->isUnionType()) { 16893 if (!NoDiagnose) { 16894 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed) 16895 << ME->getSourceRange(); 16896 return false; 16897 } 16898 return RelevantExpr || Visit(E); 16899 } 16900 16901 // If we got a member expression, we should not expect any array section 16902 // before that: 16903 // 16904 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7] 16905 // If a list item is an element of a structure, only the rightmost symbol 16906 // of the variable reference can be an array section. 16907 // 16908 AllowUnitySizeArraySection = false; 16909 AllowWholeSizeArraySection = false; 16910 16911 // Record the component. 16912 Components.emplace_back(ME, FD); 16913 return RelevantExpr || Visit(E); 16914 } 16915 16916 bool VisitArraySubscriptExpr(ArraySubscriptExpr *AE) { 16917 Expr *E = AE->getBase()->IgnoreParenImpCasts(); 16918 16919 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) { 16920 if (!NoDiagnose) { 16921 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name) 16922 << 0 << AE->getSourceRange(); 16923 return false; 16924 } 16925 return RelevantExpr || Visit(E); 16926 } 16927 16928 // If we got an array subscript that express the whole dimension we 16929 // can have any array expressions before. If it only expressing part of 16930 // the dimension, we can only have unitary-size array expressions. 16931 if (checkArrayExpressionDoesNotReferToWholeSize(SemaRef, AE, 16932 E->getType())) 16933 AllowWholeSizeArraySection = false; 16934 16935 if (const auto *TE = dyn_cast<CXXThisExpr>(E->IgnoreParenCasts())) { 16936 Expr::EvalResult Result; 16937 if (!AE->getIdx()->isValueDependent() && 16938 AE->getIdx()->EvaluateAsInt(Result, SemaRef.getASTContext()) && 16939 !Result.Val.getInt().isNullValue()) { 16940 SemaRef.Diag(AE->getIdx()->getExprLoc(), 16941 diag::err_omp_invalid_map_this_expr); 16942 SemaRef.Diag(AE->getIdx()->getExprLoc(), 16943 diag::note_omp_invalid_subscript_on_this_ptr_map); 16944 } 16945 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 16946 RelevantExpr = TE; 16947 } 16948 16949 // Record the component - we don't have any declaration associated. 16950 Components.emplace_back(AE, nullptr); 16951 16952 return RelevantExpr || Visit(E); 16953 } 16954 16955 bool VisitOMPArraySectionExpr(OMPArraySectionExpr *OASE) { 16956 assert(!NoDiagnose && "Array sections cannot be implicitly mapped."); 16957 Expr *E = OASE->getBase()->IgnoreParenImpCasts(); 16958 QualType CurType = 16959 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType(); 16960 16961 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 16962 // If the type of a list item is a reference to a type T then the type 16963 // will be considered to be T for all purposes of this clause. 16964 if (CurType->isReferenceType()) 16965 CurType = CurType->getPointeeType(); 16966 16967 bool IsPointer = CurType->isAnyPointerType(); 16968 16969 if (!IsPointer && !CurType->isArrayType()) { 16970 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name) 16971 << 0 << OASE->getSourceRange(); 16972 return false; 16973 } 16974 16975 bool NotWhole = 16976 checkArrayExpressionDoesNotReferToWholeSize(SemaRef, OASE, CurType); 16977 bool NotUnity = 16978 checkArrayExpressionDoesNotReferToUnitySize(SemaRef, OASE, CurType); 16979 16980 if (AllowWholeSizeArraySection) { 16981 // Any array section is currently allowed. Allowing a whole size array 16982 // section implies allowing a unity array section as well. 16983 // 16984 // If this array section refers to the whole dimension we can still 16985 // accept other array sections before this one, except if the base is a 16986 // pointer. Otherwise, only unitary sections are accepted. 16987 if (NotWhole || IsPointer) 16988 AllowWholeSizeArraySection = false; 16989 } else if (AllowUnitySizeArraySection && NotUnity) { 16990 // A unity or whole array section is not allowed and that is not 16991 // compatible with the properties of the current array section. 16992 SemaRef.Diag( 16993 ELoc, diag::err_array_section_does_not_specify_contiguous_storage) 16994 << OASE->getSourceRange(); 16995 return false; 16996 } 16997 16998 if (const auto *TE = dyn_cast<CXXThisExpr>(E)) { 16999 Expr::EvalResult ResultR; 17000 Expr::EvalResult ResultL; 17001 if (!OASE->getLength()->isValueDependent() && 17002 OASE->getLength()->EvaluateAsInt(ResultR, SemaRef.getASTContext()) && 17003 !ResultR.Val.getInt().isOneValue()) { 17004 SemaRef.Diag(OASE->getLength()->getExprLoc(), 17005 diag::err_omp_invalid_map_this_expr); 17006 SemaRef.Diag(OASE->getLength()->getExprLoc(), 17007 diag::note_omp_invalid_length_on_this_ptr_mapping); 17008 } 17009 if (OASE->getLowerBound() && !OASE->getLowerBound()->isValueDependent() && 17010 OASE->getLowerBound()->EvaluateAsInt(ResultL, 17011 SemaRef.getASTContext()) && 17012 !ResultL.Val.getInt().isNullValue()) { 17013 SemaRef.Diag(OASE->getLowerBound()->getExprLoc(), 17014 diag::err_omp_invalid_map_this_expr); 17015 SemaRef.Diag(OASE->getLowerBound()->getExprLoc(), 17016 diag::note_omp_invalid_lower_bound_on_this_ptr_mapping); 17017 } 17018 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 17019 RelevantExpr = TE; 17020 } 17021 17022 // Record the component - we don't have any declaration associated. 17023 Components.emplace_back(OASE, nullptr); 17024 return RelevantExpr || Visit(E); 17025 } 17026 bool VisitOMPArrayShapingExpr(OMPArrayShapingExpr *E) { 17027 Expr *Base = E->getBase(); 17028 17029 // Record the component - we don't have any declaration associated. 17030 Components.emplace_back(E, nullptr); 17031 17032 return Visit(Base->IgnoreParenImpCasts()); 17033 } 17034 17035 bool VisitUnaryOperator(UnaryOperator *UO) { 17036 if (SemaRef.getLangOpts().OpenMP < 50 || !UO->isLValue() || 17037 UO->getOpcode() != UO_Deref) { 17038 emitErrorMsg(); 17039 return false; 17040 } 17041 if (!RelevantExpr) { 17042 // Record the component if haven't found base decl. 17043 Components.emplace_back(UO, nullptr); 17044 } 17045 return RelevantExpr || Visit(UO->getSubExpr()->IgnoreParenImpCasts()); 17046 } 17047 bool VisitBinaryOperator(BinaryOperator *BO) { 17048 if (SemaRef.getLangOpts().OpenMP < 50 || !BO->getType()->isPointerType()) { 17049 emitErrorMsg(); 17050 return false; 17051 } 17052 17053 // Pointer arithmetic is the only thing we expect to happen here so after we 17054 // make sure the binary operator is a pointer type, the we only thing need 17055 // to to is to visit the subtree that has the same type as root (so that we 17056 // know the other subtree is just an offset) 17057 Expr *LE = BO->getLHS()->IgnoreParenImpCasts(); 17058 Expr *RE = BO->getRHS()->IgnoreParenImpCasts(); 17059 Components.emplace_back(BO, nullptr); 17060 assert((LE->getType().getTypePtr() == BO->getType().getTypePtr() || 17061 RE->getType().getTypePtr() == BO->getType().getTypePtr()) && 17062 "Either LHS or RHS have base decl inside"); 17063 if (BO->getType().getTypePtr() == LE->getType().getTypePtr()) 17064 return RelevantExpr || Visit(LE); 17065 return RelevantExpr || Visit(RE); 17066 } 17067 bool VisitCXXThisExpr(CXXThisExpr *CTE) { 17068 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 17069 RelevantExpr = CTE; 17070 Components.emplace_back(CTE, nullptr); 17071 return true; 17072 } 17073 bool VisitCXXOperatorCallExpr(CXXOperatorCallExpr *COCE) { 17074 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 17075 Components.emplace_back(COCE, nullptr); 17076 return true; 17077 } 17078 bool VisitStmt(Stmt *) { 17079 emitErrorMsg(); 17080 return false; 17081 } 17082 const Expr *getFoundBase() const { 17083 return RelevantExpr; 17084 } 17085 explicit MapBaseChecker( 17086 Sema &SemaRef, OpenMPClauseKind CKind, 17087 OMPClauseMappableExprCommon::MappableExprComponentList &Components, 17088 bool NoDiagnose, SourceLocation &ELoc, SourceRange &ERange) 17089 : SemaRef(SemaRef), CKind(CKind), Components(Components), 17090 NoDiagnose(NoDiagnose), ELoc(ELoc), ERange(ERange) {} 17091 }; 17092 } // namespace 17093 17094 /// Return the expression of the base of the mappable expression or null if it 17095 /// cannot be determined and do all the necessary checks to see if the expression 17096 /// is valid as a standalone mappable expression. In the process, record all the 17097 /// components of the expression. 17098 static const Expr *checkMapClauseExpressionBase( 17099 Sema &SemaRef, Expr *E, 17100 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents, 17101 OpenMPClauseKind CKind, bool NoDiagnose) { 17102 SourceLocation ELoc = E->getExprLoc(); 17103 SourceRange ERange = E->getSourceRange(); 17104 MapBaseChecker Checker(SemaRef, CKind, CurComponents, NoDiagnose, ELoc, 17105 ERange); 17106 if (Checker.Visit(E->IgnoreParens())) 17107 return Checker.getFoundBase(); 17108 return nullptr; 17109 } 17110 17111 // Return true if expression E associated with value VD has conflicts with other 17112 // map information. 17113 static bool checkMapConflicts( 17114 Sema &SemaRef, DSAStackTy *DSAS, const ValueDecl *VD, const Expr *E, 17115 bool CurrentRegionOnly, 17116 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents, 17117 OpenMPClauseKind CKind) { 17118 assert(VD && E); 17119 SourceLocation ELoc = E->getExprLoc(); 17120 SourceRange ERange = E->getSourceRange(); 17121 17122 // In order to easily check the conflicts we need to match each component of 17123 // the expression under test with the components of the expressions that are 17124 // already in the stack. 17125 17126 assert(!CurComponents.empty() && "Map clause expression with no components!"); 17127 assert(CurComponents.back().getAssociatedDeclaration() == VD && 17128 "Map clause expression with unexpected base!"); 17129 17130 // Variables to help detecting enclosing problems in data environment nests. 17131 bool IsEnclosedByDataEnvironmentExpr = false; 17132 const Expr *EnclosingExpr = nullptr; 17133 17134 bool FoundError = DSAS->checkMappableExprComponentListsForDecl( 17135 VD, CurrentRegionOnly, 17136 [&IsEnclosedByDataEnvironmentExpr, &SemaRef, VD, CurrentRegionOnly, ELoc, 17137 ERange, CKind, &EnclosingExpr, 17138 CurComponents](OMPClauseMappableExprCommon::MappableExprComponentListRef 17139 StackComponents, 17140 OpenMPClauseKind) { 17141 assert(!StackComponents.empty() && 17142 "Map clause expression with no components!"); 17143 assert(StackComponents.back().getAssociatedDeclaration() == VD && 17144 "Map clause expression with unexpected base!"); 17145 (void)VD; 17146 17147 // The whole expression in the stack. 17148 const Expr *RE = StackComponents.front().getAssociatedExpression(); 17149 17150 // Expressions must start from the same base. Here we detect at which 17151 // point both expressions diverge from each other and see if we can 17152 // detect if the memory referred to both expressions is contiguous and 17153 // do not overlap. 17154 auto CI = CurComponents.rbegin(); 17155 auto CE = CurComponents.rend(); 17156 auto SI = StackComponents.rbegin(); 17157 auto SE = StackComponents.rend(); 17158 for (; CI != CE && SI != SE; ++CI, ++SI) { 17159 17160 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3] 17161 // At most one list item can be an array item derived from a given 17162 // variable in map clauses of the same construct. 17163 if (CurrentRegionOnly && 17164 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) || 17165 isa<OMPArraySectionExpr>(CI->getAssociatedExpression()) || 17166 isa<OMPArrayShapingExpr>(CI->getAssociatedExpression())) && 17167 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) || 17168 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()) || 17169 isa<OMPArrayShapingExpr>(SI->getAssociatedExpression()))) { 17170 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(), 17171 diag::err_omp_multiple_array_items_in_map_clause) 17172 << CI->getAssociatedExpression()->getSourceRange(); 17173 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(), 17174 diag::note_used_here) 17175 << SI->getAssociatedExpression()->getSourceRange(); 17176 return true; 17177 } 17178 17179 // Do both expressions have the same kind? 17180 if (CI->getAssociatedExpression()->getStmtClass() != 17181 SI->getAssociatedExpression()->getStmtClass()) 17182 break; 17183 17184 // Are we dealing with different variables/fields? 17185 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration()) 17186 break; 17187 } 17188 // Check if the extra components of the expressions in the enclosing 17189 // data environment are redundant for the current base declaration. 17190 // If they are, the maps completely overlap, which is legal. 17191 for (; SI != SE; ++SI) { 17192 QualType Type; 17193 if (const auto *ASE = 17194 dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) { 17195 Type = ASE->getBase()->IgnoreParenImpCasts()->getType(); 17196 } else if (const auto *OASE = dyn_cast<OMPArraySectionExpr>( 17197 SI->getAssociatedExpression())) { 17198 const Expr *E = OASE->getBase()->IgnoreParenImpCasts(); 17199 Type = 17200 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType(); 17201 } else if (const auto *OASE = dyn_cast<OMPArrayShapingExpr>( 17202 SI->getAssociatedExpression())) { 17203 Type = OASE->getBase()->getType()->getPointeeType(); 17204 } 17205 if (Type.isNull() || Type->isAnyPointerType() || 17206 checkArrayExpressionDoesNotReferToWholeSize( 17207 SemaRef, SI->getAssociatedExpression(), Type)) 17208 break; 17209 } 17210 17211 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4] 17212 // List items of map clauses in the same construct must not share 17213 // original storage. 17214 // 17215 // If the expressions are exactly the same or one is a subset of the 17216 // other, it means they are sharing storage. 17217 if (CI == CE && SI == SE) { 17218 if (CurrentRegionOnly) { 17219 if (CKind == OMPC_map) { 17220 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange; 17221 } else { 17222 assert(CKind == OMPC_to || CKind == OMPC_from); 17223 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update) 17224 << ERange; 17225 } 17226 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 17227 << RE->getSourceRange(); 17228 return true; 17229 } 17230 // If we find the same expression in the enclosing data environment, 17231 // that is legal. 17232 IsEnclosedByDataEnvironmentExpr = true; 17233 return false; 17234 } 17235 17236 QualType DerivedType = 17237 std::prev(CI)->getAssociatedDeclaration()->getType(); 17238 SourceLocation DerivedLoc = 17239 std::prev(CI)->getAssociatedExpression()->getExprLoc(); 17240 17241 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 17242 // If the type of a list item is a reference to a type T then the type 17243 // will be considered to be T for all purposes of this clause. 17244 DerivedType = DerivedType.getNonReferenceType(); 17245 17246 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1] 17247 // A variable for which the type is pointer and an array section 17248 // derived from that variable must not appear as list items of map 17249 // clauses of the same construct. 17250 // 17251 // Also, cover one of the cases in: 17252 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5] 17253 // If any part of the original storage of a list item has corresponding 17254 // storage in the device data environment, all of the original storage 17255 // must have corresponding storage in the device data environment. 17256 // 17257 if (DerivedType->isAnyPointerType()) { 17258 if (CI == CE || SI == SE) { 17259 SemaRef.Diag( 17260 DerivedLoc, 17261 diag::err_omp_pointer_mapped_along_with_derived_section) 17262 << DerivedLoc; 17263 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 17264 << RE->getSourceRange(); 17265 return true; 17266 } 17267 if (CI->getAssociatedExpression()->getStmtClass() != 17268 SI->getAssociatedExpression()->getStmtClass() || 17269 CI->getAssociatedDeclaration()->getCanonicalDecl() == 17270 SI->getAssociatedDeclaration()->getCanonicalDecl()) { 17271 assert(CI != CE && SI != SE); 17272 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_dereferenced) 17273 << DerivedLoc; 17274 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 17275 << RE->getSourceRange(); 17276 return true; 17277 } 17278 } 17279 17280 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4] 17281 // List items of map clauses in the same construct must not share 17282 // original storage. 17283 // 17284 // An expression is a subset of the other. 17285 if (CurrentRegionOnly && (CI == CE || SI == SE)) { 17286 if (CKind == OMPC_map) { 17287 if (CI != CE || SI != SE) { 17288 // Allow constructs like this: map(s, s.ptr[0:1]), where s.ptr is 17289 // a pointer. 17290 auto Begin = 17291 CI != CE ? CurComponents.begin() : StackComponents.begin(); 17292 auto End = CI != CE ? CurComponents.end() : StackComponents.end(); 17293 auto It = Begin; 17294 while (It != End && !It->getAssociatedDeclaration()) 17295 std::advance(It, 1); 17296 assert(It != End && 17297 "Expected at least one component with the declaration."); 17298 if (It != Begin && It->getAssociatedDeclaration() 17299 ->getType() 17300 .getCanonicalType() 17301 ->isAnyPointerType()) { 17302 IsEnclosedByDataEnvironmentExpr = false; 17303 EnclosingExpr = nullptr; 17304 return false; 17305 } 17306 } 17307 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange; 17308 } else { 17309 assert(CKind == OMPC_to || CKind == OMPC_from); 17310 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update) 17311 << ERange; 17312 } 17313 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 17314 << RE->getSourceRange(); 17315 return true; 17316 } 17317 17318 // The current expression uses the same base as other expression in the 17319 // data environment but does not contain it completely. 17320 if (!CurrentRegionOnly && SI != SE) 17321 EnclosingExpr = RE; 17322 17323 // The current expression is a subset of the expression in the data 17324 // environment. 17325 IsEnclosedByDataEnvironmentExpr |= 17326 (!CurrentRegionOnly && CI != CE && SI == SE); 17327 17328 return false; 17329 }); 17330 17331 if (CurrentRegionOnly) 17332 return FoundError; 17333 17334 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5] 17335 // If any part of the original storage of a list item has corresponding 17336 // storage in the device data environment, all of the original storage must 17337 // have corresponding storage in the device data environment. 17338 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6] 17339 // If a list item is an element of a structure, and a different element of 17340 // the structure has a corresponding list item in the device data environment 17341 // prior to a task encountering the construct associated with the map clause, 17342 // then the list item must also have a corresponding list item in the device 17343 // data environment prior to the task encountering the construct. 17344 // 17345 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) { 17346 SemaRef.Diag(ELoc, 17347 diag::err_omp_original_storage_is_shared_and_does_not_contain) 17348 << ERange; 17349 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here) 17350 << EnclosingExpr->getSourceRange(); 17351 return true; 17352 } 17353 17354 return FoundError; 17355 } 17356 17357 // Look up the user-defined mapper given the mapper name and mapped type, and 17358 // build a reference to it. 17359 static ExprResult buildUserDefinedMapperRef(Sema &SemaRef, Scope *S, 17360 CXXScopeSpec &MapperIdScopeSpec, 17361 const DeclarationNameInfo &MapperId, 17362 QualType Type, 17363 Expr *UnresolvedMapper) { 17364 if (MapperIdScopeSpec.isInvalid()) 17365 return ExprError(); 17366 // Get the actual type for the array type. 17367 if (Type->isArrayType()) { 17368 assert(Type->getAsArrayTypeUnsafe() && "Expect to get a valid array type"); 17369 Type = Type->getAsArrayTypeUnsafe()->getElementType().getCanonicalType(); 17370 } 17371 // Find all user-defined mappers with the given MapperId. 17372 SmallVector<UnresolvedSet<8>, 4> Lookups; 17373 LookupResult Lookup(SemaRef, MapperId, Sema::LookupOMPMapperName); 17374 Lookup.suppressDiagnostics(); 17375 if (S) { 17376 while (S && SemaRef.LookupParsedName(Lookup, S, &MapperIdScopeSpec)) { 17377 NamedDecl *D = Lookup.getRepresentativeDecl(); 17378 while (S && !S->isDeclScope(D)) 17379 S = S->getParent(); 17380 if (S) 17381 S = S->getParent(); 17382 Lookups.emplace_back(); 17383 Lookups.back().append(Lookup.begin(), Lookup.end()); 17384 Lookup.clear(); 17385 } 17386 } else if (auto *ULE = cast_or_null<UnresolvedLookupExpr>(UnresolvedMapper)) { 17387 // Extract the user-defined mappers with the given MapperId. 17388 Lookups.push_back(UnresolvedSet<8>()); 17389 for (NamedDecl *D : ULE->decls()) { 17390 auto *DMD = cast<OMPDeclareMapperDecl>(D); 17391 assert(DMD && "Expect valid OMPDeclareMapperDecl during instantiation."); 17392 Lookups.back().addDecl(DMD); 17393 } 17394 } 17395 // Defer the lookup for dependent types. The results will be passed through 17396 // UnresolvedMapper on instantiation. 17397 if (SemaRef.CurContext->isDependentContext() || Type->isDependentType() || 17398 Type->isInstantiationDependentType() || 17399 Type->containsUnexpandedParameterPack() || 17400 filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) { 17401 return !D->isInvalidDecl() && 17402 (D->getType()->isDependentType() || 17403 D->getType()->isInstantiationDependentType() || 17404 D->getType()->containsUnexpandedParameterPack()); 17405 })) { 17406 UnresolvedSet<8> URS; 17407 for (const UnresolvedSet<8> &Set : Lookups) { 17408 if (Set.empty()) 17409 continue; 17410 URS.append(Set.begin(), Set.end()); 17411 } 17412 return UnresolvedLookupExpr::Create( 17413 SemaRef.Context, /*NamingClass=*/nullptr, 17414 MapperIdScopeSpec.getWithLocInContext(SemaRef.Context), MapperId, 17415 /*ADL=*/false, /*Overloaded=*/true, URS.begin(), URS.end()); 17416 } 17417 SourceLocation Loc = MapperId.getLoc(); 17418 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions 17419 // The type must be of struct, union or class type in C and C++ 17420 if (!Type->isStructureOrClassType() && !Type->isUnionType() && 17421 (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default")) { 17422 SemaRef.Diag(Loc, diag::err_omp_mapper_wrong_type); 17423 return ExprError(); 17424 } 17425 // Perform argument dependent lookup. 17426 if (SemaRef.getLangOpts().CPlusPlus && !MapperIdScopeSpec.isSet()) 17427 argumentDependentLookup(SemaRef, MapperId, Loc, Type, Lookups); 17428 // Return the first user-defined mapper with the desired type. 17429 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 17430 Lookups, [&SemaRef, Type](ValueDecl *D) -> ValueDecl * { 17431 if (!D->isInvalidDecl() && 17432 SemaRef.Context.hasSameType(D->getType(), Type)) 17433 return D; 17434 return nullptr; 17435 })) 17436 return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc); 17437 // Find the first user-defined mapper with a type derived from the desired 17438 // type. 17439 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 17440 Lookups, [&SemaRef, Type, Loc](ValueDecl *D) -> ValueDecl * { 17441 if (!D->isInvalidDecl() && 17442 SemaRef.IsDerivedFrom(Loc, Type, D->getType()) && 17443 !Type.isMoreQualifiedThan(D->getType())) 17444 return D; 17445 return nullptr; 17446 })) { 17447 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, 17448 /*DetectVirtual=*/false); 17449 if (SemaRef.IsDerivedFrom(Loc, Type, VD->getType(), Paths)) { 17450 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType( 17451 VD->getType().getUnqualifiedType()))) { 17452 if (SemaRef.CheckBaseClassAccess( 17453 Loc, VD->getType(), Type, Paths.front(), 17454 /*DiagID=*/0) != Sema::AR_inaccessible) { 17455 return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc); 17456 } 17457 } 17458 } 17459 } 17460 // Report error if a mapper is specified, but cannot be found. 17461 if (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default") { 17462 SemaRef.Diag(Loc, diag::err_omp_invalid_mapper) 17463 << Type << MapperId.getName(); 17464 return ExprError(); 17465 } 17466 return ExprEmpty(); 17467 } 17468 17469 namespace { 17470 // Utility struct that gathers all the related lists associated with a mappable 17471 // expression. 17472 struct MappableVarListInfo { 17473 // The list of expressions. 17474 ArrayRef<Expr *> VarList; 17475 // The list of processed expressions. 17476 SmallVector<Expr *, 16> ProcessedVarList; 17477 // The mappble components for each expression. 17478 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents; 17479 // The base declaration of the variable. 17480 SmallVector<ValueDecl *, 16> VarBaseDeclarations; 17481 // The reference to the user-defined mapper associated with every expression. 17482 SmallVector<Expr *, 16> UDMapperList; 17483 17484 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) { 17485 // We have a list of components and base declarations for each entry in the 17486 // variable list. 17487 VarComponents.reserve(VarList.size()); 17488 VarBaseDeclarations.reserve(VarList.size()); 17489 } 17490 }; 17491 } 17492 17493 // Check the validity of the provided variable list for the provided clause kind 17494 // \a CKind. In the check process the valid expressions, mappable expression 17495 // components, variables, and user-defined mappers are extracted and used to 17496 // fill \a ProcessedVarList, \a VarComponents, \a VarBaseDeclarations, and \a 17497 // UDMapperList in MVLI. \a MapType, \a IsMapTypeImplicit, \a MapperIdScopeSpec, 17498 // and \a MapperId are expected to be valid if the clause kind is 'map'. 17499 static void checkMappableExpressionList( 17500 Sema &SemaRef, DSAStackTy *DSAS, OpenMPClauseKind CKind, 17501 MappableVarListInfo &MVLI, SourceLocation StartLoc, 17502 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo MapperId, 17503 ArrayRef<Expr *> UnresolvedMappers, 17504 OpenMPMapClauseKind MapType = OMPC_MAP_unknown, 17505 bool IsMapTypeImplicit = false) { 17506 // We only expect mappable expressions in 'to', 'from', and 'map' clauses. 17507 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) && 17508 "Unexpected clause kind with mappable expressions!"); 17509 17510 // If the identifier of user-defined mapper is not specified, it is "default". 17511 // We do not change the actual name in this clause to distinguish whether a 17512 // mapper is specified explicitly, i.e., it is not explicitly specified when 17513 // MapperId.getName() is empty. 17514 if (!MapperId.getName() || MapperId.getName().isEmpty()) { 17515 auto &DeclNames = SemaRef.getASTContext().DeclarationNames; 17516 MapperId.setName(DeclNames.getIdentifier( 17517 &SemaRef.getASTContext().Idents.get("default"))); 17518 } 17519 17520 // Iterators to find the current unresolved mapper expression. 17521 auto UMIt = UnresolvedMappers.begin(), UMEnd = UnresolvedMappers.end(); 17522 bool UpdateUMIt = false; 17523 Expr *UnresolvedMapper = nullptr; 17524 17525 // Keep track of the mappable components and base declarations in this clause. 17526 // Each entry in the list is going to have a list of components associated. We 17527 // record each set of the components so that we can build the clause later on. 17528 // In the end we should have the same amount of declarations and component 17529 // lists. 17530 17531 for (Expr *RE : MVLI.VarList) { 17532 assert(RE && "Null expr in omp to/from/map clause"); 17533 SourceLocation ELoc = RE->getExprLoc(); 17534 17535 // Find the current unresolved mapper expression. 17536 if (UpdateUMIt && UMIt != UMEnd) { 17537 UMIt++; 17538 assert( 17539 UMIt != UMEnd && 17540 "Expect the size of UnresolvedMappers to match with that of VarList"); 17541 } 17542 UpdateUMIt = true; 17543 if (UMIt != UMEnd) 17544 UnresolvedMapper = *UMIt; 17545 17546 const Expr *VE = RE->IgnoreParenLValueCasts(); 17547 17548 if (VE->isValueDependent() || VE->isTypeDependent() || 17549 VE->isInstantiationDependent() || 17550 VE->containsUnexpandedParameterPack()) { 17551 // Try to find the associated user-defined mapper. 17552 ExprResult ER = buildUserDefinedMapperRef( 17553 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId, 17554 VE->getType().getCanonicalType(), UnresolvedMapper); 17555 if (ER.isInvalid()) 17556 continue; 17557 MVLI.UDMapperList.push_back(ER.get()); 17558 // We can only analyze this information once the missing information is 17559 // resolved. 17560 MVLI.ProcessedVarList.push_back(RE); 17561 continue; 17562 } 17563 17564 Expr *SimpleExpr = RE->IgnoreParenCasts(); 17565 17566 if (!RE->isLValue()) { 17567 if (SemaRef.getLangOpts().OpenMP < 50) { 17568 SemaRef.Diag( 17569 ELoc, diag::err_omp_expected_named_var_member_or_array_expression) 17570 << RE->getSourceRange(); 17571 } else { 17572 SemaRef.Diag(ELoc, diag::err_omp_non_lvalue_in_map_or_motion_clauses) 17573 << getOpenMPClauseName(CKind) << RE->getSourceRange(); 17574 } 17575 continue; 17576 } 17577 17578 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents; 17579 ValueDecl *CurDeclaration = nullptr; 17580 17581 // Obtain the array or member expression bases if required. Also, fill the 17582 // components array with all the components identified in the process. 17583 const Expr *BE = checkMapClauseExpressionBase( 17584 SemaRef, SimpleExpr, CurComponents, CKind, /*NoDiagnose=*/false); 17585 if (!BE) 17586 continue; 17587 17588 assert(!CurComponents.empty() && 17589 "Invalid mappable expression information."); 17590 17591 if (const auto *TE = dyn_cast<CXXThisExpr>(BE)) { 17592 // Add store "this" pointer to class in DSAStackTy for future checking 17593 DSAS->addMappedClassesQualTypes(TE->getType()); 17594 // Try to find the associated user-defined mapper. 17595 ExprResult ER = buildUserDefinedMapperRef( 17596 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId, 17597 VE->getType().getCanonicalType(), UnresolvedMapper); 17598 if (ER.isInvalid()) 17599 continue; 17600 MVLI.UDMapperList.push_back(ER.get()); 17601 // Skip restriction checking for variable or field declarations 17602 MVLI.ProcessedVarList.push_back(RE); 17603 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 17604 MVLI.VarComponents.back().append(CurComponents.begin(), 17605 CurComponents.end()); 17606 MVLI.VarBaseDeclarations.push_back(nullptr); 17607 continue; 17608 } 17609 17610 // For the following checks, we rely on the base declaration which is 17611 // expected to be associated with the last component. The declaration is 17612 // expected to be a variable or a field (if 'this' is being mapped). 17613 CurDeclaration = CurComponents.back().getAssociatedDeclaration(); 17614 assert(CurDeclaration && "Null decl on map clause."); 17615 assert( 17616 CurDeclaration->isCanonicalDecl() && 17617 "Expecting components to have associated only canonical declarations."); 17618 17619 auto *VD = dyn_cast<VarDecl>(CurDeclaration); 17620 const auto *FD = dyn_cast<FieldDecl>(CurDeclaration); 17621 17622 assert((VD || FD) && "Only variables or fields are expected here!"); 17623 (void)FD; 17624 17625 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10] 17626 // threadprivate variables cannot appear in a map clause. 17627 // OpenMP 4.5 [2.10.5, target update Construct] 17628 // threadprivate variables cannot appear in a from clause. 17629 if (VD && DSAS->isThreadPrivate(VD)) { 17630 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false); 17631 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause) 17632 << getOpenMPClauseName(CKind); 17633 reportOriginalDsa(SemaRef, DSAS, VD, DVar); 17634 continue; 17635 } 17636 17637 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9] 17638 // A list item cannot appear in both a map clause and a data-sharing 17639 // attribute clause on the same construct. 17640 17641 // Check conflicts with other map clause expressions. We check the conflicts 17642 // with the current construct separately from the enclosing data 17643 // environment, because the restrictions are different. We only have to 17644 // check conflicts across regions for the map clauses. 17645 if (checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr, 17646 /*CurrentRegionOnly=*/true, CurComponents, CKind)) 17647 break; 17648 if (CKind == OMPC_map && 17649 (SemaRef.getLangOpts().OpenMP <= 45 || StartLoc.isValid()) && 17650 checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr, 17651 /*CurrentRegionOnly=*/false, CurComponents, CKind)) 17652 break; 17653 17654 // OpenMP 4.5 [2.10.5, target update Construct] 17655 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 17656 // If the type of a list item is a reference to a type T then the type will 17657 // be considered to be T for all purposes of this clause. 17658 auto I = llvm::find_if( 17659 CurComponents, 17660 [](const OMPClauseMappableExprCommon::MappableComponent &MC) { 17661 return MC.getAssociatedDeclaration(); 17662 }); 17663 assert(I != CurComponents.end() && "Null decl on map clause."); 17664 QualType Type; 17665 auto *ASE = dyn_cast<ArraySubscriptExpr>(VE->IgnoreParens()); 17666 auto *OASE = dyn_cast<OMPArraySectionExpr>(VE->IgnoreParens()); 17667 auto *OAShE = dyn_cast<OMPArrayShapingExpr>(VE->IgnoreParens()); 17668 if (ASE) { 17669 Type = ASE->getType().getNonReferenceType(); 17670 } else if (OASE) { 17671 QualType BaseType = 17672 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 17673 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 17674 Type = ATy->getElementType(); 17675 else 17676 Type = BaseType->getPointeeType(); 17677 Type = Type.getNonReferenceType(); 17678 } else if (OAShE) { 17679 Type = OAShE->getBase()->getType()->getPointeeType(); 17680 } else { 17681 Type = VE->getType(); 17682 } 17683 17684 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4] 17685 // A list item in a to or from clause must have a mappable type. 17686 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9] 17687 // A list item must have a mappable type. 17688 if (!checkTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef, 17689 DSAS, Type)) 17690 continue; 17691 17692 Type = I->getAssociatedDeclaration()->getType().getNonReferenceType(); 17693 17694 if (CKind == OMPC_map) { 17695 // target enter data 17696 // OpenMP [2.10.2, Restrictions, p. 99] 17697 // A map-type must be specified in all map clauses and must be either 17698 // to or alloc. 17699 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective(); 17700 if (DKind == OMPD_target_enter_data && 17701 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) { 17702 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 17703 << (IsMapTypeImplicit ? 1 : 0) 17704 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 17705 << getOpenMPDirectiveName(DKind); 17706 continue; 17707 } 17708 17709 // target exit_data 17710 // OpenMP [2.10.3, Restrictions, p. 102] 17711 // A map-type must be specified in all map clauses and must be either 17712 // from, release, or delete. 17713 if (DKind == OMPD_target_exit_data && 17714 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release || 17715 MapType == OMPC_MAP_delete)) { 17716 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 17717 << (IsMapTypeImplicit ? 1 : 0) 17718 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 17719 << getOpenMPDirectiveName(DKind); 17720 continue; 17721 } 17722 17723 // target, target data 17724 // OpenMP 5.0 [2.12.2, Restrictions, p. 163] 17725 // OpenMP 5.0 [2.12.5, Restrictions, p. 174] 17726 // A map-type in a map clause must be to, from, tofrom or alloc 17727 if ((DKind == OMPD_target_data || 17728 isOpenMPTargetExecutionDirective(DKind)) && 17729 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_from || 17730 MapType == OMPC_MAP_tofrom || MapType == OMPC_MAP_alloc)) { 17731 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 17732 << (IsMapTypeImplicit ? 1 : 0) 17733 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 17734 << getOpenMPDirectiveName(DKind); 17735 continue; 17736 } 17737 17738 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 17739 // A list item cannot appear in both a map clause and a data-sharing 17740 // attribute clause on the same construct 17741 // 17742 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7] 17743 // A list item cannot appear in both a map clause and a data-sharing 17744 // attribute clause on the same construct unless the construct is a 17745 // combined construct. 17746 if (VD && ((SemaRef.LangOpts.OpenMP <= 45 && 17747 isOpenMPTargetExecutionDirective(DKind)) || 17748 DKind == OMPD_target)) { 17749 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false); 17750 if (isOpenMPPrivate(DVar.CKind)) { 17751 SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 17752 << getOpenMPClauseName(DVar.CKind) 17753 << getOpenMPClauseName(OMPC_map) 17754 << getOpenMPDirectiveName(DSAS->getCurrentDirective()); 17755 reportOriginalDsa(SemaRef, DSAS, CurDeclaration, DVar); 17756 continue; 17757 } 17758 } 17759 } 17760 17761 // Try to find the associated user-defined mapper. 17762 ExprResult ER = buildUserDefinedMapperRef( 17763 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId, 17764 Type.getCanonicalType(), UnresolvedMapper); 17765 if (ER.isInvalid()) 17766 continue; 17767 MVLI.UDMapperList.push_back(ER.get()); 17768 17769 // Save the current expression. 17770 MVLI.ProcessedVarList.push_back(RE); 17771 17772 // Store the components in the stack so that they can be used to check 17773 // against other clauses later on. 17774 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents, 17775 /*WhereFoundClauseKind=*/OMPC_map); 17776 17777 // Save the components and declaration to create the clause. For purposes of 17778 // the clause creation, any component list that has has base 'this' uses 17779 // null as base declaration. 17780 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 17781 MVLI.VarComponents.back().append(CurComponents.begin(), 17782 CurComponents.end()); 17783 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr 17784 : CurDeclaration); 17785 } 17786 } 17787 17788 OMPClause *Sema::ActOnOpenMPMapClause( 17789 ArrayRef<OpenMPMapModifierKind> MapTypeModifiers, 17790 ArrayRef<SourceLocation> MapTypeModifiersLoc, 17791 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId, 17792 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, SourceLocation MapLoc, 17793 SourceLocation ColonLoc, ArrayRef<Expr *> VarList, 17794 const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) { 17795 OpenMPMapModifierKind Modifiers[] = { 17796 OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown, 17797 OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown}; 17798 SourceLocation ModifiersLoc[NumberOfOMPMapClauseModifiers]; 17799 17800 // Process map-type-modifiers, flag errors for duplicate modifiers. 17801 unsigned Count = 0; 17802 for (unsigned I = 0, E = MapTypeModifiers.size(); I < E; ++I) { 17803 if (MapTypeModifiers[I] != OMPC_MAP_MODIFIER_unknown && 17804 llvm::find(Modifiers, MapTypeModifiers[I]) != std::end(Modifiers)) { 17805 Diag(MapTypeModifiersLoc[I], diag::err_omp_duplicate_map_type_modifier); 17806 continue; 17807 } 17808 assert(Count < NumberOfOMPMapClauseModifiers && 17809 "Modifiers exceed the allowed number of map type modifiers"); 17810 Modifiers[Count] = MapTypeModifiers[I]; 17811 ModifiersLoc[Count] = MapTypeModifiersLoc[I]; 17812 ++Count; 17813 } 17814 17815 MappableVarListInfo MVLI(VarList); 17816 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, Locs.StartLoc, 17817 MapperIdScopeSpec, MapperId, UnresolvedMappers, 17818 MapType, IsMapTypeImplicit); 17819 17820 // We need to produce a map clause even if we don't have variables so that 17821 // other diagnostics related with non-existing map clauses are accurate. 17822 return OMPMapClause::Create(Context, Locs, MVLI.ProcessedVarList, 17823 MVLI.VarBaseDeclarations, MVLI.VarComponents, 17824 MVLI.UDMapperList, Modifiers, ModifiersLoc, 17825 MapperIdScopeSpec.getWithLocInContext(Context), 17826 MapperId, MapType, IsMapTypeImplicit, MapLoc); 17827 } 17828 17829 QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc, 17830 TypeResult ParsedType) { 17831 assert(ParsedType.isUsable()); 17832 17833 QualType ReductionType = GetTypeFromParser(ParsedType.get()); 17834 if (ReductionType.isNull()) 17835 return QualType(); 17836 17837 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++ 17838 // A type name in a declare reduction directive cannot be a function type, an 17839 // array type, a reference type, or a type qualified with const, volatile or 17840 // restrict. 17841 if (ReductionType.hasQualifiers()) { 17842 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0; 17843 return QualType(); 17844 } 17845 17846 if (ReductionType->isFunctionType()) { 17847 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1; 17848 return QualType(); 17849 } 17850 if (ReductionType->isReferenceType()) { 17851 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2; 17852 return QualType(); 17853 } 17854 if (ReductionType->isArrayType()) { 17855 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3; 17856 return QualType(); 17857 } 17858 return ReductionType; 17859 } 17860 17861 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart( 17862 Scope *S, DeclContext *DC, DeclarationName Name, 17863 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes, 17864 AccessSpecifier AS, Decl *PrevDeclInScope) { 17865 SmallVector<Decl *, 8> Decls; 17866 Decls.reserve(ReductionTypes.size()); 17867 17868 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName, 17869 forRedeclarationInCurContext()); 17870 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions 17871 // A reduction-identifier may not be re-declared in the current scope for the 17872 // same type or for a type that is compatible according to the base language 17873 // rules. 17874 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes; 17875 OMPDeclareReductionDecl *PrevDRD = nullptr; 17876 bool InCompoundScope = true; 17877 if (S != nullptr) { 17878 // Find previous declaration with the same name not referenced in other 17879 // declarations. 17880 FunctionScopeInfo *ParentFn = getEnclosingFunction(); 17881 InCompoundScope = 17882 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty(); 17883 LookupName(Lookup, S); 17884 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false, 17885 /*AllowInlineNamespace=*/false); 17886 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious; 17887 LookupResult::Filter Filter = Lookup.makeFilter(); 17888 while (Filter.hasNext()) { 17889 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next()); 17890 if (InCompoundScope) { 17891 auto I = UsedAsPrevious.find(PrevDecl); 17892 if (I == UsedAsPrevious.end()) 17893 UsedAsPrevious[PrevDecl] = false; 17894 if (OMPDeclareReductionDecl *D = PrevDecl->getPrevDeclInScope()) 17895 UsedAsPrevious[D] = true; 17896 } 17897 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] = 17898 PrevDecl->getLocation(); 17899 } 17900 Filter.done(); 17901 if (InCompoundScope) { 17902 for (const auto &PrevData : UsedAsPrevious) { 17903 if (!PrevData.second) { 17904 PrevDRD = PrevData.first; 17905 break; 17906 } 17907 } 17908 } 17909 } else if (PrevDeclInScope != nullptr) { 17910 auto *PrevDRDInScope = PrevDRD = 17911 cast<OMPDeclareReductionDecl>(PrevDeclInScope); 17912 do { 17913 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] = 17914 PrevDRDInScope->getLocation(); 17915 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope(); 17916 } while (PrevDRDInScope != nullptr); 17917 } 17918 for (const auto &TyData : ReductionTypes) { 17919 const auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType()); 17920 bool Invalid = false; 17921 if (I != PreviousRedeclTypes.end()) { 17922 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition) 17923 << TyData.first; 17924 Diag(I->second, diag::note_previous_definition); 17925 Invalid = true; 17926 } 17927 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second; 17928 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second, 17929 Name, TyData.first, PrevDRD); 17930 DC->addDecl(DRD); 17931 DRD->setAccess(AS); 17932 Decls.push_back(DRD); 17933 if (Invalid) 17934 DRD->setInvalidDecl(); 17935 else 17936 PrevDRD = DRD; 17937 } 17938 17939 return DeclGroupPtrTy::make( 17940 DeclGroupRef::Create(Context, Decls.begin(), Decls.size())); 17941 } 17942 17943 void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) { 17944 auto *DRD = cast<OMPDeclareReductionDecl>(D); 17945 17946 // Enter new function scope. 17947 PushFunctionScope(); 17948 setFunctionHasBranchProtectedScope(); 17949 getCurFunction()->setHasOMPDeclareReductionCombiner(); 17950 17951 if (S != nullptr) 17952 PushDeclContext(S, DRD); 17953 else 17954 CurContext = DRD; 17955 17956 PushExpressionEvaluationContext( 17957 ExpressionEvaluationContext::PotentiallyEvaluated); 17958 17959 QualType ReductionType = DRD->getType(); 17960 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will 17961 // be replaced by '*omp_parm' during codegen. This required because 'omp_in' 17962 // uses semantics of argument handles by value, but it should be passed by 17963 // reference. C lang does not support references, so pass all parameters as 17964 // pointers. 17965 // Create 'T omp_in;' variable. 17966 VarDecl *OmpInParm = 17967 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in"); 17968 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will 17969 // be replaced by '*omp_parm' during codegen. This required because 'omp_out' 17970 // uses semantics of argument handles by value, but it should be passed by 17971 // reference. C lang does not support references, so pass all parameters as 17972 // pointers. 17973 // Create 'T omp_out;' variable. 17974 VarDecl *OmpOutParm = 17975 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out"); 17976 if (S != nullptr) { 17977 PushOnScopeChains(OmpInParm, S); 17978 PushOnScopeChains(OmpOutParm, S); 17979 } else { 17980 DRD->addDecl(OmpInParm); 17981 DRD->addDecl(OmpOutParm); 17982 } 17983 Expr *InE = 17984 ::buildDeclRefExpr(*this, OmpInParm, ReductionType, D->getLocation()); 17985 Expr *OutE = 17986 ::buildDeclRefExpr(*this, OmpOutParm, ReductionType, D->getLocation()); 17987 DRD->setCombinerData(InE, OutE); 17988 } 17989 17990 void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) { 17991 auto *DRD = cast<OMPDeclareReductionDecl>(D); 17992 DiscardCleanupsInEvaluationContext(); 17993 PopExpressionEvaluationContext(); 17994 17995 PopDeclContext(); 17996 PopFunctionScopeInfo(); 17997 17998 if (Combiner != nullptr) 17999 DRD->setCombiner(Combiner); 18000 else 18001 DRD->setInvalidDecl(); 18002 } 18003 18004 VarDecl *Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) { 18005 auto *DRD = cast<OMPDeclareReductionDecl>(D); 18006 18007 // Enter new function scope. 18008 PushFunctionScope(); 18009 setFunctionHasBranchProtectedScope(); 18010 18011 if (S != nullptr) 18012 PushDeclContext(S, DRD); 18013 else 18014 CurContext = DRD; 18015 18016 PushExpressionEvaluationContext( 18017 ExpressionEvaluationContext::PotentiallyEvaluated); 18018 18019 QualType ReductionType = DRD->getType(); 18020 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will 18021 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv' 18022 // uses semantics of argument handles by value, but it should be passed by 18023 // reference. C lang does not support references, so pass all parameters as 18024 // pointers. 18025 // Create 'T omp_priv;' variable. 18026 VarDecl *OmpPrivParm = 18027 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv"); 18028 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will 18029 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig' 18030 // uses semantics of argument handles by value, but it should be passed by 18031 // reference. C lang does not support references, so pass all parameters as 18032 // pointers. 18033 // Create 'T omp_orig;' variable. 18034 VarDecl *OmpOrigParm = 18035 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig"); 18036 if (S != nullptr) { 18037 PushOnScopeChains(OmpPrivParm, S); 18038 PushOnScopeChains(OmpOrigParm, S); 18039 } else { 18040 DRD->addDecl(OmpPrivParm); 18041 DRD->addDecl(OmpOrigParm); 18042 } 18043 Expr *OrigE = 18044 ::buildDeclRefExpr(*this, OmpOrigParm, ReductionType, D->getLocation()); 18045 Expr *PrivE = 18046 ::buildDeclRefExpr(*this, OmpPrivParm, ReductionType, D->getLocation()); 18047 DRD->setInitializerData(OrigE, PrivE); 18048 return OmpPrivParm; 18049 } 18050 18051 void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, Expr *Initializer, 18052 VarDecl *OmpPrivParm) { 18053 auto *DRD = cast<OMPDeclareReductionDecl>(D); 18054 DiscardCleanupsInEvaluationContext(); 18055 PopExpressionEvaluationContext(); 18056 18057 PopDeclContext(); 18058 PopFunctionScopeInfo(); 18059 18060 if (Initializer != nullptr) { 18061 DRD->setInitializer(Initializer, OMPDeclareReductionDecl::CallInit); 18062 } else if (OmpPrivParm->hasInit()) { 18063 DRD->setInitializer(OmpPrivParm->getInit(), 18064 OmpPrivParm->isDirectInit() 18065 ? OMPDeclareReductionDecl::DirectInit 18066 : OMPDeclareReductionDecl::CopyInit); 18067 } else { 18068 DRD->setInvalidDecl(); 18069 } 18070 } 18071 18072 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd( 18073 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) { 18074 for (Decl *D : DeclReductions.get()) { 18075 if (IsValid) { 18076 if (S) 18077 PushOnScopeChains(cast<OMPDeclareReductionDecl>(D), S, 18078 /*AddToContext=*/false); 18079 } else { 18080 D->setInvalidDecl(); 18081 } 18082 } 18083 return DeclReductions; 18084 } 18085 18086 TypeResult Sema::ActOnOpenMPDeclareMapperVarDecl(Scope *S, Declarator &D) { 18087 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); 18088 QualType T = TInfo->getType(); 18089 if (D.isInvalidType()) 18090 return true; 18091 18092 if (getLangOpts().CPlusPlus) { 18093 // Check that there are no default arguments (C++ only). 18094 CheckExtraCXXDefaultArguments(D); 18095 } 18096 18097 return CreateParsedType(T, TInfo); 18098 } 18099 18100 QualType Sema::ActOnOpenMPDeclareMapperType(SourceLocation TyLoc, 18101 TypeResult ParsedType) { 18102 assert(ParsedType.isUsable() && "Expect usable parsed mapper type"); 18103 18104 QualType MapperType = GetTypeFromParser(ParsedType.get()); 18105 assert(!MapperType.isNull() && "Expect valid mapper type"); 18106 18107 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions 18108 // The type must be of struct, union or class type in C and C++ 18109 if (!MapperType->isStructureOrClassType() && !MapperType->isUnionType()) { 18110 Diag(TyLoc, diag::err_omp_mapper_wrong_type); 18111 return QualType(); 18112 } 18113 return MapperType; 18114 } 18115 18116 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareMapperDirective( 18117 Scope *S, DeclContext *DC, DeclarationName Name, QualType MapperType, 18118 SourceLocation StartLoc, DeclarationName VN, AccessSpecifier AS, 18119 Expr *MapperVarRef, ArrayRef<OMPClause *> Clauses, Decl *PrevDeclInScope) { 18120 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPMapperName, 18121 forRedeclarationInCurContext()); 18122 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions 18123 // A mapper-identifier may not be redeclared in the current scope for the 18124 // same type or for a type that is compatible according to the base language 18125 // rules. 18126 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes; 18127 OMPDeclareMapperDecl *PrevDMD = nullptr; 18128 bool InCompoundScope = true; 18129 if (S != nullptr) { 18130 // Find previous declaration with the same name not referenced in other 18131 // declarations. 18132 FunctionScopeInfo *ParentFn = getEnclosingFunction(); 18133 InCompoundScope = 18134 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty(); 18135 LookupName(Lookup, S); 18136 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false, 18137 /*AllowInlineNamespace=*/false); 18138 llvm::DenseMap<OMPDeclareMapperDecl *, bool> UsedAsPrevious; 18139 LookupResult::Filter Filter = Lookup.makeFilter(); 18140 while (Filter.hasNext()) { 18141 auto *PrevDecl = cast<OMPDeclareMapperDecl>(Filter.next()); 18142 if (InCompoundScope) { 18143 auto I = UsedAsPrevious.find(PrevDecl); 18144 if (I == UsedAsPrevious.end()) 18145 UsedAsPrevious[PrevDecl] = false; 18146 if (OMPDeclareMapperDecl *D = PrevDecl->getPrevDeclInScope()) 18147 UsedAsPrevious[D] = true; 18148 } 18149 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] = 18150 PrevDecl->getLocation(); 18151 } 18152 Filter.done(); 18153 if (InCompoundScope) { 18154 for (const auto &PrevData : UsedAsPrevious) { 18155 if (!PrevData.second) { 18156 PrevDMD = PrevData.first; 18157 break; 18158 } 18159 } 18160 } 18161 } else if (PrevDeclInScope) { 18162 auto *PrevDMDInScope = PrevDMD = 18163 cast<OMPDeclareMapperDecl>(PrevDeclInScope); 18164 do { 18165 PreviousRedeclTypes[PrevDMDInScope->getType().getCanonicalType()] = 18166 PrevDMDInScope->getLocation(); 18167 PrevDMDInScope = PrevDMDInScope->getPrevDeclInScope(); 18168 } while (PrevDMDInScope != nullptr); 18169 } 18170 const auto I = PreviousRedeclTypes.find(MapperType.getCanonicalType()); 18171 bool Invalid = false; 18172 if (I != PreviousRedeclTypes.end()) { 18173 Diag(StartLoc, diag::err_omp_declare_mapper_redefinition) 18174 << MapperType << Name; 18175 Diag(I->second, diag::note_previous_definition); 18176 Invalid = true; 18177 } 18178 auto *DMD = OMPDeclareMapperDecl::Create(Context, DC, StartLoc, Name, 18179 MapperType, VN, Clauses, PrevDMD); 18180 if (S) 18181 PushOnScopeChains(DMD, S); 18182 else 18183 DC->addDecl(DMD); 18184 DMD->setAccess(AS); 18185 if (Invalid) 18186 DMD->setInvalidDecl(); 18187 18188 auto *VD = cast<DeclRefExpr>(MapperVarRef)->getDecl(); 18189 VD->setDeclContext(DMD); 18190 VD->setLexicalDeclContext(DMD); 18191 DMD->addDecl(VD); 18192 DMD->setMapperVarRef(MapperVarRef); 18193 18194 return DeclGroupPtrTy::make(DeclGroupRef(DMD)); 18195 } 18196 18197 ExprResult 18198 Sema::ActOnOpenMPDeclareMapperDirectiveVarDecl(Scope *S, QualType MapperType, 18199 SourceLocation StartLoc, 18200 DeclarationName VN) { 18201 TypeSourceInfo *TInfo = 18202 Context.getTrivialTypeSourceInfo(MapperType, StartLoc); 18203 auto *VD = VarDecl::Create(Context, Context.getTranslationUnitDecl(), 18204 StartLoc, StartLoc, VN.getAsIdentifierInfo(), 18205 MapperType, TInfo, SC_None); 18206 if (S) 18207 PushOnScopeChains(VD, S, /*AddToContext=*/false); 18208 Expr *E = buildDeclRefExpr(*this, VD, MapperType, StartLoc); 18209 DSAStack->addDeclareMapperVarRef(E); 18210 return E; 18211 } 18212 18213 bool Sema::isOpenMPDeclareMapperVarDeclAllowed(const VarDecl *VD) const { 18214 assert(LangOpts.OpenMP && "Expected OpenMP mode."); 18215 const Expr *Ref = DSAStack->getDeclareMapperVarRef(); 18216 if (const auto *DRE = cast_or_null<DeclRefExpr>(Ref)) 18217 return VD->getCanonicalDecl() == DRE->getDecl()->getCanonicalDecl(); 18218 return true; 18219 } 18220 18221 const ValueDecl *Sema::getOpenMPDeclareMapperVarName() const { 18222 assert(LangOpts.OpenMP && "Expected OpenMP mode."); 18223 return cast<DeclRefExpr>(DSAStack->getDeclareMapperVarRef())->getDecl(); 18224 } 18225 18226 OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams, 18227 SourceLocation StartLoc, 18228 SourceLocation LParenLoc, 18229 SourceLocation EndLoc) { 18230 Expr *ValExpr = NumTeams; 18231 Stmt *HelperValStmt = nullptr; 18232 18233 // OpenMP [teams Constrcut, Restrictions] 18234 // The num_teams expression must evaluate to a positive integer value. 18235 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams, 18236 /*StrictlyPositive=*/true)) 18237 return nullptr; 18238 18239 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 18240 OpenMPDirectiveKind CaptureRegion = 18241 getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams, LangOpts.OpenMP); 18242 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 18243 ValExpr = MakeFullExpr(ValExpr).get(); 18244 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 18245 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 18246 HelperValStmt = buildPreInits(Context, Captures); 18247 } 18248 18249 return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion, 18250 StartLoc, LParenLoc, EndLoc); 18251 } 18252 18253 OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit, 18254 SourceLocation StartLoc, 18255 SourceLocation LParenLoc, 18256 SourceLocation EndLoc) { 18257 Expr *ValExpr = ThreadLimit; 18258 Stmt *HelperValStmt = nullptr; 18259 18260 // OpenMP [teams Constrcut, Restrictions] 18261 // The thread_limit expression must evaluate to a positive integer value. 18262 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit, 18263 /*StrictlyPositive=*/true)) 18264 return nullptr; 18265 18266 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 18267 OpenMPDirectiveKind CaptureRegion = getOpenMPCaptureRegionForClause( 18268 DKind, OMPC_thread_limit, LangOpts.OpenMP); 18269 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 18270 ValExpr = MakeFullExpr(ValExpr).get(); 18271 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 18272 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 18273 HelperValStmt = buildPreInits(Context, Captures); 18274 } 18275 18276 return new (Context) OMPThreadLimitClause( 18277 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc); 18278 } 18279 18280 OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority, 18281 SourceLocation StartLoc, 18282 SourceLocation LParenLoc, 18283 SourceLocation EndLoc) { 18284 Expr *ValExpr = Priority; 18285 Stmt *HelperValStmt = nullptr; 18286 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 18287 18288 // OpenMP [2.9.1, task Constrcut] 18289 // The priority-value is a non-negative numerical scalar expression. 18290 if (!isNonNegativeIntegerValue( 18291 ValExpr, *this, OMPC_priority, 18292 /*StrictlyPositive=*/false, /*BuildCapture=*/true, 18293 DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt)) 18294 return nullptr; 18295 18296 return new (Context) OMPPriorityClause(ValExpr, HelperValStmt, CaptureRegion, 18297 StartLoc, LParenLoc, EndLoc); 18298 } 18299 18300 OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize, 18301 SourceLocation StartLoc, 18302 SourceLocation LParenLoc, 18303 SourceLocation EndLoc) { 18304 Expr *ValExpr = Grainsize; 18305 Stmt *HelperValStmt = nullptr; 18306 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 18307 18308 // OpenMP [2.9.2, taskloop Constrcut] 18309 // The parameter of the grainsize clause must be a positive integer 18310 // expression. 18311 if (!isNonNegativeIntegerValue( 18312 ValExpr, *this, OMPC_grainsize, 18313 /*StrictlyPositive=*/true, /*BuildCapture=*/true, 18314 DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt)) 18315 return nullptr; 18316 18317 return new (Context) OMPGrainsizeClause(ValExpr, HelperValStmt, CaptureRegion, 18318 StartLoc, LParenLoc, EndLoc); 18319 } 18320 18321 OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks, 18322 SourceLocation StartLoc, 18323 SourceLocation LParenLoc, 18324 SourceLocation EndLoc) { 18325 Expr *ValExpr = NumTasks; 18326 Stmt *HelperValStmt = nullptr; 18327 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 18328 18329 // OpenMP [2.9.2, taskloop Constrcut] 18330 // The parameter of the num_tasks clause must be a positive integer 18331 // expression. 18332 if (!isNonNegativeIntegerValue( 18333 ValExpr, *this, OMPC_num_tasks, 18334 /*StrictlyPositive=*/true, /*BuildCapture=*/true, 18335 DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt)) 18336 return nullptr; 18337 18338 return new (Context) OMPNumTasksClause(ValExpr, HelperValStmt, CaptureRegion, 18339 StartLoc, LParenLoc, EndLoc); 18340 } 18341 18342 OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc, 18343 SourceLocation LParenLoc, 18344 SourceLocation EndLoc) { 18345 // OpenMP [2.13.2, critical construct, Description] 18346 // ... where hint-expression is an integer constant expression that evaluates 18347 // to a valid lock hint. 18348 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint); 18349 if (HintExpr.isInvalid()) 18350 return nullptr; 18351 return new (Context) 18352 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc); 18353 } 18354 18355 /// Tries to find omp_event_handle_t type. 18356 static bool findOMPEventHandleT(Sema &S, SourceLocation Loc, 18357 DSAStackTy *Stack) { 18358 QualType OMPEventHandleT = Stack->getOMPEventHandleT(); 18359 if (!OMPEventHandleT.isNull()) 18360 return true; 18361 IdentifierInfo *II = &S.PP.getIdentifierTable().get("omp_event_handle_t"); 18362 ParsedType PT = S.getTypeName(*II, Loc, S.getCurScope()); 18363 if (!PT.getAsOpaquePtr() || PT.get().isNull()) { 18364 S.Diag(Loc, diag::err_omp_implied_type_not_found) << "omp_event_handle_t"; 18365 return false; 18366 } 18367 Stack->setOMPEventHandleT(PT.get()); 18368 return true; 18369 } 18370 18371 OMPClause *Sema::ActOnOpenMPDetachClause(Expr *Evt, SourceLocation StartLoc, 18372 SourceLocation LParenLoc, 18373 SourceLocation EndLoc) { 18374 if (!Evt->isValueDependent() && !Evt->isTypeDependent() && 18375 !Evt->isInstantiationDependent() && 18376 !Evt->containsUnexpandedParameterPack()) { 18377 if (!findOMPEventHandleT(*this, Evt->getExprLoc(), DSAStack)) 18378 return nullptr; 18379 // OpenMP 5.0, 2.10.1 task Construct. 18380 // event-handle is a variable of the omp_event_handle_t type. 18381 auto *Ref = dyn_cast<DeclRefExpr>(Evt->IgnoreParenImpCasts()); 18382 if (!Ref) { 18383 Diag(Evt->getExprLoc(), diag::err_omp_var_expected) 18384 << "omp_event_handle_t" << 0 << Evt->getSourceRange(); 18385 return nullptr; 18386 } 18387 auto *VD = dyn_cast_or_null<VarDecl>(Ref->getDecl()); 18388 if (!VD) { 18389 Diag(Evt->getExprLoc(), diag::err_omp_var_expected) 18390 << "omp_event_handle_t" << 0 << Evt->getSourceRange(); 18391 return nullptr; 18392 } 18393 if (!Context.hasSameUnqualifiedType(DSAStack->getOMPEventHandleT(), 18394 VD->getType()) || 18395 VD->getType().isConstant(Context)) { 18396 Diag(Evt->getExprLoc(), diag::err_omp_var_expected) 18397 << "omp_event_handle_t" << 1 << VD->getType() 18398 << Evt->getSourceRange(); 18399 return nullptr; 18400 } 18401 // OpenMP 5.0, 2.10.1 task Construct 18402 // [detach clause]... The event-handle will be considered as if it was 18403 // specified on a firstprivate clause. 18404 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, /*FromParent=*/false); 18405 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate && 18406 DVar.RefExpr) { 18407 Diag(Evt->getExprLoc(), diag::err_omp_wrong_dsa) 18408 << getOpenMPClauseName(DVar.CKind) 18409 << getOpenMPClauseName(OMPC_firstprivate); 18410 reportOriginalDsa(*this, DSAStack, VD, DVar); 18411 return nullptr; 18412 } 18413 } 18414 18415 return new (Context) OMPDetachClause(Evt, StartLoc, LParenLoc, EndLoc); 18416 } 18417 18418 OMPClause *Sema::ActOnOpenMPDistScheduleClause( 18419 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, 18420 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc, 18421 SourceLocation EndLoc) { 18422 if (Kind == OMPC_DIST_SCHEDULE_unknown) { 18423 std::string Values; 18424 Values += "'"; 18425 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0); 18426 Values += "'"; 18427 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 18428 << Values << getOpenMPClauseName(OMPC_dist_schedule); 18429 return nullptr; 18430 } 18431 Expr *ValExpr = ChunkSize; 18432 Stmt *HelperValStmt = nullptr; 18433 if (ChunkSize) { 18434 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && 18435 !ChunkSize->isInstantiationDependent() && 18436 !ChunkSize->containsUnexpandedParameterPack()) { 18437 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc(); 18438 ExprResult Val = 18439 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); 18440 if (Val.isInvalid()) 18441 return nullptr; 18442 18443 ValExpr = Val.get(); 18444 18445 // OpenMP [2.7.1, Restrictions] 18446 // chunk_size must be a loop invariant integer expression with a positive 18447 // value. 18448 if (Optional<llvm::APSInt> Result = 18449 ValExpr->getIntegerConstantExpr(Context)) { 18450 if (Result->isSigned() && !Result->isStrictlyPositive()) { 18451 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) 18452 << "dist_schedule" << ChunkSize->getSourceRange(); 18453 return nullptr; 18454 } 18455 } else if (getOpenMPCaptureRegionForClause( 18456 DSAStack->getCurrentDirective(), OMPC_dist_schedule, 18457 LangOpts.OpenMP) != OMPD_unknown && 18458 !CurContext->isDependentContext()) { 18459 ValExpr = MakeFullExpr(ValExpr).get(); 18460 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 18461 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 18462 HelperValStmt = buildPreInits(Context, Captures); 18463 } 18464 } 18465 } 18466 18467 return new (Context) 18468 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, 18469 Kind, ValExpr, HelperValStmt); 18470 } 18471 18472 OMPClause *Sema::ActOnOpenMPDefaultmapClause( 18473 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind, 18474 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc, 18475 SourceLocation KindLoc, SourceLocation EndLoc) { 18476 if (getLangOpts().OpenMP < 50) { 18477 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || 18478 Kind != OMPC_DEFAULTMAP_scalar) { 18479 std::string Value; 18480 SourceLocation Loc; 18481 Value += "'"; 18482 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) { 18483 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap, 18484 OMPC_DEFAULTMAP_MODIFIER_tofrom); 18485 Loc = MLoc; 18486 } else { 18487 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap, 18488 OMPC_DEFAULTMAP_scalar); 18489 Loc = KindLoc; 18490 } 18491 Value += "'"; 18492 Diag(Loc, diag::err_omp_unexpected_clause_value) 18493 << Value << getOpenMPClauseName(OMPC_defaultmap); 18494 return nullptr; 18495 } 18496 } else { 18497 bool isDefaultmapModifier = (M != OMPC_DEFAULTMAP_MODIFIER_unknown); 18498 bool isDefaultmapKind = (Kind != OMPC_DEFAULTMAP_unknown) || 18499 (LangOpts.OpenMP >= 50 && KindLoc.isInvalid()); 18500 if (!isDefaultmapKind || !isDefaultmapModifier) { 18501 std::string ModifierValue = "'alloc', 'from', 'to', 'tofrom', " 18502 "'firstprivate', 'none', 'default'"; 18503 std::string KindValue = "'scalar', 'aggregate', 'pointer'"; 18504 if (!isDefaultmapKind && isDefaultmapModifier) { 18505 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 18506 << KindValue << getOpenMPClauseName(OMPC_defaultmap); 18507 } else if (isDefaultmapKind && !isDefaultmapModifier) { 18508 Diag(MLoc, diag::err_omp_unexpected_clause_value) 18509 << ModifierValue << getOpenMPClauseName(OMPC_defaultmap); 18510 } else { 18511 Diag(MLoc, diag::err_omp_unexpected_clause_value) 18512 << ModifierValue << getOpenMPClauseName(OMPC_defaultmap); 18513 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 18514 << KindValue << getOpenMPClauseName(OMPC_defaultmap); 18515 } 18516 return nullptr; 18517 } 18518 18519 // OpenMP [5.0, 2.12.5, Restrictions, p. 174] 18520 // At most one defaultmap clause for each category can appear on the 18521 // directive. 18522 if (DSAStack->checkDefaultmapCategory(Kind)) { 18523 Diag(StartLoc, diag::err_omp_one_defaultmap_each_category); 18524 return nullptr; 18525 } 18526 } 18527 if (Kind == OMPC_DEFAULTMAP_unknown) { 18528 // Variable category is not specified - mark all categories. 18529 DSAStack->setDefaultDMAAttr(M, OMPC_DEFAULTMAP_aggregate, StartLoc); 18530 DSAStack->setDefaultDMAAttr(M, OMPC_DEFAULTMAP_scalar, StartLoc); 18531 DSAStack->setDefaultDMAAttr(M, OMPC_DEFAULTMAP_pointer, StartLoc); 18532 } else { 18533 DSAStack->setDefaultDMAAttr(M, Kind, StartLoc); 18534 } 18535 18536 return new (Context) 18537 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M); 18538 } 18539 18540 bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) { 18541 DeclContext *CurLexicalContext = getCurLexicalContext(); 18542 if (!CurLexicalContext->isFileContext() && 18543 !CurLexicalContext->isExternCContext() && 18544 !CurLexicalContext->isExternCXXContext() && 18545 !isa<CXXRecordDecl>(CurLexicalContext) && 18546 !isa<ClassTemplateDecl>(CurLexicalContext) && 18547 !isa<ClassTemplatePartialSpecializationDecl>(CurLexicalContext) && 18548 !isa<ClassTemplateSpecializationDecl>(CurLexicalContext)) { 18549 Diag(Loc, diag::err_omp_region_not_file_context); 18550 return false; 18551 } 18552 DeclareTargetNesting.push_back(Loc); 18553 return true; 18554 } 18555 18556 void Sema::ActOnFinishOpenMPDeclareTargetDirective() { 18557 assert(!DeclareTargetNesting.empty() && 18558 "Unexpected ActOnFinishOpenMPDeclareTargetDirective"); 18559 DeclareTargetNesting.pop_back(); 18560 } 18561 18562 NamedDecl * 18563 Sema::lookupOpenMPDeclareTargetName(Scope *CurScope, CXXScopeSpec &ScopeSpec, 18564 const DeclarationNameInfo &Id, 18565 NamedDeclSetType &SameDirectiveDecls) { 18566 LookupResult Lookup(*this, Id, LookupOrdinaryName); 18567 LookupParsedName(Lookup, CurScope, &ScopeSpec, true); 18568 18569 if (Lookup.isAmbiguous()) 18570 return nullptr; 18571 Lookup.suppressDiagnostics(); 18572 18573 if (!Lookup.isSingleResult()) { 18574 VarOrFuncDeclFilterCCC CCC(*this); 18575 if (TypoCorrection Corrected = 18576 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, CCC, 18577 CTK_ErrorRecovery)) { 18578 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest) 18579 << Id.getName()); 18580 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl()); 18581 return nullptr; 18582 } 18583 18584 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName(); 18585 return nullptr; 18586 } 18587 18588 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>(); 18589 if (!isa<VarDecl>(ND) && !isa<FunctionDecl>(ND) && 18590 !isa<FunctionTemplateDecl>(ND)) { 18591 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName(); 18592 return nullptr; 18593 } 18594 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl()))) 18595 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName(); 18596 return ND; 18597 } 18598 18599 void Sema::ActOnOpenMPDeclareTargetName( 18600 NamedDecl *ND, SourceLocation Loc, OMPDeclareTargetDeclAttr::MapTypeTy MT, 18601 OMPDeclareTargetDeclAttr::DevTypeTy DT) { 18602 assert((isa<VarDecl>(ND) || isa<FunctionDecl>(ND) || 18603 isa<FunctionTemplateDecl>(ND)) && 18604 "Expected variable, function or function template."); 18605 18606 // Diagnose marking after use as it may lead to incorrect diagnosis and 18607 // codegen. 18608 if (LangOpts.OpenMP >= 50 && 18609 (ND->isUsed(/*CheckUsedAttr=*/false) || ND->isReferenced())) 18610 Diag(Loc, diag::warn_omp_declare_target_after_first_use); 18611 18612 auto *VD = cast<ValueDecl>(ND); 18613 Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy = 18614 OMPDeclareTargetDeclAttr::getDeviceType(VD); 18615 Optional<SourceLocation> AttrLoc = OMPDeclareTargetDeclAttr::getLocation(VD); 18616 if (DevTy.hasValue() && *DevTy != DT && 18617 (DeclareTargetNesting.empty() || 18618 *AttrLoc != DeclareTargetNesting.back())) { 18619 Diag(Loc, diag::err_omp_device_type_mismatch) 18620 << OMPDeclareTargetDeclAttr::ConvertDevTypeTyToStr(DT) 18621 << OMPDeclareTargetDeclAttr::ConvertDevTypeTyToStr(*DevTy); 18622 return; 18623 } 18624 Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 18625 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); 18626 if (!Res || (!DeclareTargetNesting.empty() && 18627 *AttrLoc == DeclareTargetNesting.back())) { 18628 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit( 18629 Context, MT, DT, DeclareTargetNesting.size() + 1, 18630 SourceRange(Loc, Loc)); 18631 ND->addAttr(A); 18632 if (ASTMutationListener *ML = Context.getASTMutationListener()) 18633 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A); 18634 checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Loc); 18635 } else if (*Res != MT) { 18636 Diag(Loc, diag::err_omp_declare_target_to_and_link) << ND; 18637 } 18638 } 18639 18640 static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR, 18641 Sema &SemaRef, Decl *D) { 18642 if (!D || !isa<VarDecl>(D)) 18643 return; 18644 auto *VD = cast<VarDecl>(D); 18645 Optional<OMPDeclareTargetDeclAttr::MapTypeTy> MapTy = 18646 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); 18647 if (SemaRef.LangOpts.OpenMP >= 50 && 18648 (SemaRef.getCurLambda(/*IgnoreNonLambdaCapturingScope=*/true) || 18649 SemaRef.getCurBlock() || SemaRef.getCurCapturedRegion()) && 18650 VD->hasGlobalStorage()) { 18651 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> MapTy = 18652 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); 18653 if (!MapTy || *MapTy != OMPDeclareTargetDeclAttr::MT_To) { 18654 // OpenMP 5.0, 2.12.7 declare target Directive, Restrictions 18655 // If a lambda declaration and definition appears between a 18656 // declare target directive and the matching end declare target 18657 // directive, all variables that are captured by the lambda 18658 // expression must also appear in a to clause. 18659 SemaRef.Diag(VD->getLocation(), 18660 diag::err_omp_lambda_capture_in_declare_target_not_to); 18661 SemaRef.Diag(SL, diag::note_var_explicitly_captured_here) 18662 << VD << 0 << SR; 18663 return; 18664 } 18665 } 18666 if (MapTy.hasValue()) 18667 return; 18668 SemaRef.Diag(VD->getLocation(), diag::warn_omp_not_in_target_context); 18669 SemaRef.Diag(SL, diag::note_used_here) << SR; 18670 } 18671 18672 static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR, 18673 Sema &SemaRef, DSAStackTy *Stack, 18674 ValueDecl *VD) { 18675 return OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD) || 18676 checkTypeMappable(SL, SR, SemaRef, Stack, VD->getType(), 18677 /*FullCheck=*/false); 18678 } 18679 18680 void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D, 18681 SourceLocation IdLoc) { 18682 if (!D || D->isInvalidDecl()) 18683 return; 18684 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange(); 18685 SourceLocation SL = E ? E->getBeginLoc() : D->getLocation(); 18686 if (auto *VD = dyn_cast<VarDecl>(D)) { 18687 // Only global variables can be marked as declare target. 18688 if (!VD->isFileVarDecl() && !VD->isStaticLocal() && 18689 !VD->isStaticDataMember()) 18690 return; 18691 // 2.10.6: threadprivate variable cannot appear in a declare target 18692 // directive. 18693 if (DSAStack->isThreadPrivate(VD)) { 18694 Diag(SL, diag::err_omp_threadprivate_in_target); 18695 reportOriginalDsa(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false)); 18696 return; 18697 } 18698 } 18699 if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(D)) 18700 D = FTD->getTemplatedDecl(); 18701 if (auto *FD = dyn_cast<FunctionDecl>(D)) { 18702 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 18703 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(FD); 18704 if (IdLoc.isValid() && Res && *Res == OMPDeclareTargetDeclAttr::MT_Link) { 18705 Diag(IdLoc, diag::err_omp_function_in_link_clause); 18706 Diag(FD->getLocation(), diag::note_defined_here) << FD; 18707 return; 18708 } 18709 } 18710 if (auto *VD = dyn_cast<ValueDecl>(D)) { 18711 // Problem if any with var declared with incomplete type will be reported 18712 // as normal, so no need to check it here. 18713 if ((E || !VD->getType()->isIncompleteType()) && 18714 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) 18715 return; 18716 if (!E && !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) { 18717 // Checking declaration inside declare target region. 18718 if (isa<VarDecl>(D) || isa<FunctionDecl>(D) || 18719 isa<FunctionTemplateDecl>(D)) { 18720 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit( 18721 Context, OMPDeclareTargetDeclAttr::MT_To, 18722 OMPDeclareTargetDeclAttr::DT_Any, DeclareTargetNesting.size(), 18723 SourceRange(DeclareTargetNesting.back(), 18724 DeclareTargetNesting.back())); 18725 D->addAttr(A); 18726 if (ASTMutationListener *ML = Context.getASTMutationListener()) 18727 ML->DeclarationMarkedOpenMPDeclareTarget(D, A); 18728 } 18729 return; 18730 } 18731 } 18732 if (!E) 18733 return; 18734 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D); 18735 } 18736 18737 OMPClause *Sema::ActOnOpenMPToClause( 18738 ArrayRef<OpenMPMotionModifierKind> MotionModifiers, 18739 ArrayRef<SourceLocation> MotionModifiersLoc, 18740 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId, 18741 SourceLocation ColonLoc, ArrayRef<Expr *> VarList, 18742 const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) { 18743 OpenMPMotionModifierKind Modifiers[] = {OMPC_MOTION_MODIFIER_unknown, 18744 OMPC_MOTION_MODIFIER_unknown}; 18745 SourceLocation ModifiersLoc[NumberOfOMPMotionModifiers]; 18746 18747 // Process motion-modifiers, flag errors for duplicate modifiers. 18748 unsigned Count = 0; 18749 for (unsigned I = 0, E = MotionModifiers.size(); I < E; ++I) { 18750 if (MotionModifiers[I] != OMPC_MOTION_MODIFIER_unknown && 18751 llvm::find(Modifiers, MotionModifiers[I]) != std::end(Modifiers)) { 18752 Diag(MotionModifiersLoc[I], diag::err_omp_duplicate_motion_modifier); 18753 continue; 18754 } 18755 assert(Count < NumberOfOMPMotionModifiers && 18756 "Modifiers exceed the allowed number of motion modifiers"); 18757 Modifiers[Count] = MotionModifiers[I]; 18758 ModifiersLoc[Count] = MotionModifiersLoc[I]; 18759 ++Count; 18760 } 18761 18762 MappableVarListInfo MVLI(VarList); 18763 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, Locs.StartLoc, 18764 MapperIdScopeSpec, MapperId, UnresolvedMappers); 18765 if (MVLI.ProcessedVarList.empty()) 18766 return nullptr; 18767 18768 return OMPToClause::Create( 18769 Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, 18770 MVLI.VarComponents, MVLI.UDMapperList, Modifiers, ModifiersLoc, 18771 MapperIdScopeSpec.getWithLocInContext(Context), MapperId); 18772 } 18773 18774 OMPClause *Sema::ActOnOpenMPFromClause( 18775 ArrayRef<OpenMPMotionModifierKind> MotionModifiers, 18776 ArrayRef<SourceLocation> MotionModifiersLoc, 18777 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId, 18778 SourceLocation ColonLoc, ArrayRef<Expr *> VarList, 18779 const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) { 18780 OpenMPMotionModifierKind Modifiers[] = {OMPC_MOTION_MODIFIER_unknown, 18781 OMPC_MOTION_MODIFIER_unknown}; 18782 SourceLocation ModifiersLoc[NumberOfOMPMotionModifiers]; 18783 18784 // Process motion-modifiers, flag errors for duplicate modifiers. 18785 unsigned Count = 0; 18786 for (unsigned I = 0, E = MotionModifiers.size(); I < E; ++I) { 18787 if (MotionModifiers[I] != OMPC_MOTION_MODIFIER_unknown && 18788 llvm::find(Modifiers, MotionModifiers[I]) != std::end(Modifiers)) { 18789 Diag(MotionModifiersLoc[I], diag::err_omp_duplicate_motion_modifier); 18790 continue; 18791 } 18792 assert(Count < NumberOfOMPMotionModifiers && 18793 "Modifiers exceed the allowed number of motion modifiers"); 18794 Modifiers[Count] = MotionModifiers[I]; 18795 ModifiersLoc[Count] = MotionModifiersLoc[I]; 18796 ++Count; 18797 } 18798 18799 MappableVarListInfo MVLI(VarList); 18800 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, Locs.StartLoc, 18801 MapperIdScopeSpec, MapperId, UnresolvedMappers); 18802 if (MVLI.ProcessedVarList.empty()) 18803 return nullptr; 18804 18805 return OMPFromClause::Create( 18806 Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, 18807 MVLI.VarComponents, MVLI.UDMapperList, Modifiers, ModifiersLoc, 18808 MapperIdScopeSpec.getWithLocInContext(Context), MapperId); 18809 } 18810 18811 OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList, 18812 const OMPVarListLocTy &Locs) { 18813 MappableVarListInfo MVLI(VarList); 18814 SmallVector<Expr *, 8> PrivateCopies; 18815 SmallVector<Expr *, 8> Inits; 18816 18817 for (Expr *RefExpr : VarList) { 18818 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause."); 18819 SourceLocation ELoc; 18820 SourceRange ERange; 18821 Expr *SimpleRefExpr = RefExpr; 18822 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 18823 if (Res.second) { 18824 // It will be analyzed later. 18825 MVLI.ProcessedVarList.push_back(RefExpr); 18826 PrivateCopies.push_back(nullptr); 18827 Inits.push_back(nullptr); 18828 } 18829 ValueDecl *D = Res.first; 18830 if (!D) 18831 continue; 18832 18833 QualType Type = D->getType(); 18834 Type = Type.getNonReferenceType().getUnqualifiedType(); 18835 18836 auto *VD = dyn_cast<VarDecl>(D); 18837 18838 // Item should be a pointer or reference to pointer. 18839 if (!Type->isPointerType()) { 18840 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer) 18841 << 0 << RefExpr->getSourceRange(); 18842 continue; 18843 } 18844 18845 // Build the private variable and the expression that refers to it. 18846 auto VDPrivate = 18847 buildVarDecl(*this, ELoc, Type, D->getName(), 18848 D->hasAttrs() ? &D->getAttrs() : nullptr, 18849 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 18850 if (VDPrivate->isInvalidDecl()) 18851 continue; 18852 18853 CurContext->addDecl(VDPrivate); 18854 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 18855 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc); 18856 18857 // Add temporary variable to initialize the private copy of the pointer. 18858 VarDecl *VDInit = 18859 buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp"); 18860 DeclRefExpr *VDInitRefExpr = buildDeclRefExpr( 18861 *this, VDInit, RefExpr->getType(), RefExpr->getExprLoc()); 18862 AddInitializerToDecl(VDPrivate, 18863 DefaultLvalueConversion(VDInitRefExpr).get(), 18864 /*DirectInit=*/false); 18865 18866 // If required, build a capture to implement the privatization initialized 18867 // with the current list item value. 18868 DeclRefExpr *Ref = nullptr; 18869 if (!VD) 18870 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 18871 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref); 18872 PrivateCopies.push_back(VDPrivateRefExpr); 18873 Inits.push_back(VDInitRefExpr); 18874 18875 // We need to add a data sharing attribute for this variable to make sure it 18876 // is correctly captured. A variable that shows up in a use_device_ptr has 18877 // similar properties of a first private variable. 18878 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 18879 18880 // Create a mappable component for the list item. List items in this clause 18881 // only need a component. 18882 MVLI.VarBaseDeclarations.push_back(D); 18883 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 18884 MVLI.VarComponents.back().push_back( 18885 OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D)); 18886 } 18887 18888 if (MVLI.ProcessedVarList.empty()) 18889 return nullptr; 18890 18891 return OMPUseDevicePtrClause::Create( 18892 Context, Locs, MVLI.ProcessedVarList, PrivateCopies, Inits, 18893 MVLI.VarBaseDeclarations, MVLI.VarComponents); 18894 } 18895 18896 OMPClause *Sema::ActOnOpenMPUseDeviceAddrClause(ArrayRef<Expr *> VarList, 18897 const OMPVarListLocTy &Locs) { 18898 MappableVarListInfo MVLI(VarList); 18899 18900 for (Expr *RefExpr : VarList) { 18901 assert(RefExpr && "NULL expr in OpenMP use_device_addr clause."); 18902 SourceLocation ELoc; 18903 SourceRange ERange; 18904 Expr *SimpleRefExpr = RefExpr; 18905 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, 18906 /*AllowArraySection=*/true); 18907 if (Res.second) { 18908 // It will be analyzed later. 18909 MVLI.ProcessedVarList.push_back(RefExpr); 18910 } 18911 ValueDecl *D = Res.first; 18912 if (!D) 18913 continue; 18914 auto *VD = dyn_cast<VarDecl>(D); 18915 18916 // If required, build a capture to implement the privatization initialized 18917 // with the current list item value. 18918 DeclRefExpr *Ref = nullptr; 18919 if (!VD) 18920 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 18921 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref); 18922 18923 // We need to add a data sharing attribute for this variable to make sure it 18924 // is correctly captured. A variable that shows up in a use_device_addr has 18925 // similar properties of a first private variable. 18926 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 18927 18928 // Create a mappable component for the list item. List items in this clause 18929 // only need a component. 18930 MVLI.VarBaseDeclarations.push_back(D); 18931 MVLI.VarComponents.emplace_back(); 18932 Expr *Component = SimpleRefExpr; 18933 if (VD && (isa<OMPArraySectionExpr>(RefExpr->IgnoreParenImpCasts()) || 18934 isa<ArraySubscriptExpr>(RefExpr->IgnoreParenImpCasts()))) 18935 Component = DefaultFunctionArrayLvalueConversion(SimpleRefExpr).get(); 18936 MVLI.VarComponents.back().push_back( 18937 OMPClauseMappableExprCommon::MappableComponent(Component, D)); 18938 } 18939 18940 if (MVLI.ProcessedVarList.empty()) 18941 return nullptr; 18942 18943 return OMPUseDeviceAddrClause::Create(Context, Locs, MVLI.ProcessedVarList, 18944 MVLI.VarBaseDeclarations, 18945 MVLI.VarComponents); 18946 } 18947 18948 OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList, 18949 const OMPVarListLocTy &Locs) { 18950 MappableVarListInfo MVLI(VarList); 18951 for (Expr *RefExpr : VarList) { 18952 assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause."); 18953 SourceLocation ELoc; 18954 SourceRange ERange; 18955 Expr *SimpleRefExpr = RefExpr; 18956 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 18957 if (Res.second) { 18958 // It will be analyzed later. 18959 MVLI.ProcessedVarList.push_back(RefExpr); 18960 } 18961 ValueDecl *D = Res.first; 18962 if (!D) 18963 continue; 18964 18965 QualType Type = D->getType(); 18966 // item should be a pointer or array or reference to pointer or array 18967 if (!Type.getNonReferenceType()->isPointerType() && 18968 !Type.getNonReferenceType()->isArrayType()) { 18969 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr) 18970 << 0 << RefExpr->getSourceRange(); 18971 continue; 18972 } 18973 18974 // Check if the declaration in the clause does not show up in any data 18975 // sharing attribute. 18976 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 18977 if (isOpenMPPrivate(DVar.CKind)) { 18978 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 18979 << getOpenMPClauseName(DVar.CKind) 18980 << getOpenMPClauseName(OMPC_is_device_ptr) 18981 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 18982 reportOriginalDsa(*this, DSAStack, D, DVar); 18983 continue; 18984 } 18985 18986 const Expr *ConflictExpr; 18987 if (DSAStack->checkMappableExprComponentListsForDecl( 18988 D, /*CurrentRegionOnly=*/true, 18989 [&ConflictExpr]( 18990 OMPClauseMappableExprCommon::MappableExprComponentListRef R, 18991 OpenMPClauseKind) -> bool { 18992 ConflictExpr = R.front().getAssociatedExpression(); 18993 return true; 18994 })) { 18995 Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange(); 18996 Diag(ConflictExpr->getExprLoc(), diag::note_used_here) 18997 << ConflictExpr->getSourceRange(); 18998 continue; 18999 } 19000 19001 // Store the components in the stack so that they can be used to check 19002 // against other clauses later on. 19003 OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D); 19004 DSAStack->addMappableExpressionComponents( 19005 D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr); 19006 19007 // Record the expression we've just processed. 19008 MVLI.ProcessedVarList.push_back(SimpleRefExpr); 19009 19010 // Create a mappable component for the list item. List items in this clause 19011 // only need a component. We use a null declaration to signal fields in 19012 // 'this'. 19013 assert((isa<DeclRefExpr>(SimpleRefExpr) || 19014 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) && 19015 "Unexpected device pointer expression!"); 19016 MVLI.VarBaseDeclarations.push_back( 19017 isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr); 19018 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 19019 MVLI.VarComponents.back().push_back(MC); 19020 } 19021 19022 if (MVLI.ProcessedVarList.empty()) 19023 return nullptr; 19024 19025 return OMPIsDevicePtrClause::Create(Context, Locs, MVLI.ProcessedVarList, 19026 MVLI.VarBaseDeclarations, 19027 MVLI.VarComponents); 19028 } 19029 19030 OMPClause *Sema::ActOnOpenMPAllocateClause( 19031 Expr *Allocator, ArrayRef<Expr *> VarList, SourceLocation StartLoc, 19032 SourceLocation ColonLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 19033 if (Allocator) { 19034 // OpenMP [2.11.4 allocate Clause, Description] 19035 // allocator is an expression of omp_allocator_handle_t type. 19036 if (!findOMPAllocatorHandleT(*this, Allocator->getExprLoc(), DSAStack)) 19037 return nullptr; 19038 19039 ExprResult AllocatorRes = DefaultLvalueConversion(Allocator); 19040 if (AllocatorRes.isInvalid()) 19041 return nullptr; 19042 AllocatorRes = PerformImplicitConversion(AllocatorRes.get(), 19043 DSAStack->getOMPAllocatorHandleT(), 19044 Sema::AA_Initializing, 19045 /*AllowExplicit=*/true); 19046 if (AllocatorRes.isInvalid()) 19047 return nullptr; 19048 Allocator = AllocatorRes.get(); 19049 } else { 19050 // OpenMP 5.0, 2.11.4 allocate Clause, Restrictions. 19051 // allocate clauses that appear on a target construct or on constructs in a 19052 // target region must specify an allocator expression unless a requires 19053 // directive with the dynamic_allocators clause is present in the same 19054 // compilation unit. 19055 if (LangOpts.OpenMPIsDevice && 19056 !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>()) 19057 targetDiag(StartLoc, diag::err_expected_allocator_expression); 19058 } 19059 // Analyze and build list of variables. 19060 SmallVector<Expr *, 8> Vars; 19061 for (Expr *RefExpr : VarList) { 19062 assert(RefExpr && "NULL expr in OpenMP private clause."); 19063 SourceLocation ELoc; 19064 SourceRange ERange; 19065 Expr *SimpleRefExpr = RefExpr; 19066 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 19067 if (Res.second) { 19068 // It will be analyzed later. 19069 Vars.push_back(RefExpr); 19070 } 19071 ValueDecl *D = Res.first; 19072 if (!D) 19073 continue; 19074 19075 auto *VD = dyn_cast<VarDecl>(D); 19076 DeclRefExpr *Ref = nullptr; 19077 if (!VD && !CurContext->isDependentContext()) 19078 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 19079 Vars.push_back((VD || CurContext->isDependentContext()) 19080 ? RefExpr->IgnoreParens() 19081 : Ref); 19082 } 19083 19084 if (Vars.empty()) 19085 return nullptr; 19086 19087 if (Allocator) 19088 DSAStack->addInnerAllocatorExpr(Allocator); 19089 return OMPAllocateClause::Create(Context, StartLoc, LParenLoc, Allocator, 19090 ColonLoc, EndLoc, Vars); 19091 } 19092 19093 OMPClause *Sema::ActOnOpenMPNontemporalClause(ArrayRef<Expr *> VarList, 19094 SourceLocation StartLoc, 19095 SourceLocation LParenLoc, 19096 SourceLocation EndLoc) { 19097 SmallVector<Expr *, 8> Vars; 19098 for (Expr *RefExpr : VarList) { 19099 assert(RefExpr && "NULL expr in OpenMP nontemporal clause."); 19100 SourceLocation ELoc; 19101 SourceRange ERange; 19102 Expr *SimpleRefExpr = RefExpr; 19103 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 19104 if (Res.second) 19105 // It will be analyzed later. 19106 Vars.push_back(RefExpr); 19107 ValueDecl *D = Res.first; 19108 if (!D) 19109 continue; 19110 19111 // OpenMP 5.0, 2.9.3.1 simd Construct, Restrictions. 19112 // A list-item cannot appear in more than one nontemporal clause. 19113 if (const Expr *PrevRef = 19114 DSAStack->addUniqueNontemporal(D, SimpleRefExpr)) { 19115 Diag(ELoc, diag::err_omp_used_in_clause_twice) 19116 << 0 << getOpenMPClauseName(OMPC_nontemporal) << ERange; 19117 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa) 19118 << getOpenMPClauseName(OMPC_nontemporal); 19119 continue; 19120 } 19121 19122 Vars.push_back(RefExpr); 19123 } 19124 19125 if (Vars.empty()) 19126 return nullptr; 19127 19128 return OMPNontemporalClause::Create(Context, StartLoc, LParenLoc, EndLoc, 19129 Vars); 19130 } 19131 19132 OMPClause *Sema::ActOnOpenMPInclusiveClause(ArrayRef<Expr *> VarList, 19133 SourceLocation StartLoc, 19134 SourceLocation LParenLoc, 19135 SourceLocation EndLoc) { 19136 SmallVector<Expr *, 8> Vars; 19137 for (Expr *RefExpr : VarList) { 19138 assert(RefExpr && "NULL expr in OpenMP nontemporal clause."); 19139 SourceLocation ELoc; 19140 SourceRange ERange; 19141 Expr *SimpleRefExpr = RefExpr; 19142 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, 19143 /*AllowArraySection=*/true); 19144 if (Res.second) 19145 // It will be analyzed later. 19146 Vars.push_back(RefExpr); 19147 ValueDecl *D = Res.first; 19148 if (!D) 19149 continue; 19150 19151 const DSAStackTy::DSAVarData DVar = 19152 DSAStack->getTopDSA(D, /*FromParent=*/true); 19153 // OpenMP 5.0, 2.9.6, scan Directive, Restrictions. 19154 // A list item that appears in the inclusive or exclusive clause must appear 19155 // in a reduction clause with the inscan modifier on the enclosing 19156 // worksharing-loop, worksharing-loop SIMD, or simd construct. 19157 if (DVar.CKind != OMPC_reduction || 19158 DVar.Modifier != OMPC_REDUCTION_inscan) 19159 Diag(ELoc, diag::err_omp_inclusive_exclusive_not_reduction) 19160 << RefExpr->getSourceRange(); 19161 19162 if (DSAStack->getParentDirective() != OMPD_unknown) 19163 DSAStack->markDeclAsUsedInScanDirective(D); 19164 Vars.push_back(RefExpr); 19165 } 19166 19167 if (Vars.empty()) 19168 return nullptr; 19169 19170 return OMPInclusiveClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 19171 } 19172 19173 OMPClause *Sema::ActOnOpenMPExclusiveClause(ArrayRef<Expr *> VarList, 19174 SourceLocation StartLoc, 19175 SourceLocation LParenLoc, 19176 SourceLocation EndLoc) { 19177 SmallVector<Expr *, 8> Vars; 19178 for (Expr *RefExpr : VarList) { 19179 assert(RefExpr && "NULL expr in OpenMP nontemporal clause."); 19180 SourceLocation ELoc; 19181 SourceRange ERange; 19182 Expr *SimpleRefExpr = RefExpr; 19183 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, 19184 /*AllowArraySection=*/true); 19185 if (Res.second) 19186 // It will be analyzed later. 19187 Vars.push_back(RefExpr); 19188 ValueDecl *D = Res.first; 19189 if (!D) 19190 continue; 19191 19192 OpenMPDirectiveKind ParentDirective = DSAStack->getParentDirective(); 19193 DSAStackTy::DSAVarData DVar; 19194 if (ParentDirective != OMPD_unknown) 19195 DVar = DSAStack->getTopDSA(D, /*FromParent=*/true); 19196 // OpenMP 5.0, 2.9.6, scan Directive, Restrictions. 19197 // A list item that appears in the inclusive or exclusive clause must appear 19198 // in a reduction clause with the inscan modifier on the enclosing 19199 // worksharing-loop, worksharing-loop SIMD, or simd construct. 19200 if (ParentDirective == OMPD_unknown || DVar.CKind != OMPC_reduction || 19201 DVar.Modifier != OMPC_REDUCTION_inscan) { 19202 Diag(ELoc, diag::err_omp_inclusive_exclusive_not_reduction) 19203 << RefExpr->getSourceRange(); 19204 } else { 19205 DSAStack->markDeclAsUsedInScanDirective(D); 19206 } 19207 Vars.push_back(RefExpr); 19208 } 19209 19210 if (Vars.empty()) 19211 return nullptr; 19212 19213 return OMPExclusiveClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 19214 } 19215 19216 /// Tries to find omp_alloctrait_t type. 19217 static bool findOMPAlloctraitT(Sema &S, SourceLocation Loc, DSAStackTy *Stack) { 19218 QualType OMPAlloctraitT = Stack->getOMPAlloctraitT(); 19219 if (!OMPAlloctraitT.isNull()) 19220 return true; 19221 IdentifierInfo &II = S.PP.getIdentifierTable().get("omp_alloctrait_t"); 19222 ParsedType PT = S.getTypeName(II, Loc, S.getCurScope()); 19223 if (!PT.getAsOpaquePtr() || PT.get().isNull()) { 19224 S.Diag(Loc, diag::err_omp_implied_type_not_found) << "omp_alloctrait_t"; 19225 return false; 19226 } 19227 Stack->setOMPAlloctraitT(PT.get()); 19228 return true; 19229 } 19230 19231 OMPClause *Sema::ActOnOpenMPUsesAllocatorClause( 19232 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc, 19233 ArrayRef<UsesAllocatorsData> Data) { 19234 // OpenMP [2.12.5, target Construct] 19235 // allocator is an identifier of omp_allocator_handle_t type. 19236 if (!findOMPAllocatorHandleT(*this, StartLoc, DSAStack)) 19237 return nullptr; 19238 // OpenMP [2.12.5, target Construct] 19239 // allocator-traits-array is an identifier of const omp_alloctrait_t * type. 19240 if (llvm::any_of( 19241 Data, 19242 [](const UsesAllocatorsData &D) { return D.AllocatorTraits; }) && 19243 !findOMPAlloctraitT(*this, StartLoc, DSAStack)) 19244 return nullptr; 19245 llvm::SmallPtrSet<CanonicalDeclPtr<Decl>, 4> PredefinedAllocators; 19246 for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) { 19247 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I); 19248 StringRef Allocator = 19249 OMPAllocateDeclAttr::ConvertAllocatorTypeTyToStr(AllocatorKind); 19250 DeclarationName AllocatorName = &Context.Idents.get(Allocator); 19251 PredefinedAllocators.insert(LookupSingleName( 19252 TUScope, AllocatorName, StartLoc, Sema::LookupAnyName)); 19253 } 19254 19255 SmallVector<OMPUsesAllocatorsClause::Data, 4> NewData; 19256 for (const UsesAllocatorsData &D : Data) { 19257 Expr *AllocatorExpr = nullptr; 19258 // Check allocator expression. 19259 if (D.Allocator->isTypeDependent()) { 19260 AllocatorExpr = D.Allocator; 19261 } else { 19262 // Traits were specified - need to assign new allocator to the specified 19263 // allocator, so it must be an lvalue. 19264 AllocatorExpr = D.Allocator->IgnoreParenImpCasts(); 19265 auto *DRE = dyn_cast<DeclRefExpr>(AllocatorExpr); 19266 bool IsPredefinedAllocator = false; 19267 if (DRE) 19268 IsPredefinedAllocator = PredefinedAllocators.count(DRE->getDecl()); 19269 if (!DRE || 19270 !(Context.hasSameUnqualifiedType( 19271 AllocatorExpr->getType(), DSAStack->getOMPAllocatorHandleT()) || 19272 Context.typesAreCompatible(AllocatorExpr->getType(), 19273 DSAStack->getOMPAllocatorHandleT(), 19274 /*CompareUnqualified=*/true)) || 19275 (!IsPredefinedAllocator && 19276 (AllocatorExpr->getType().isConstant(Context) || 19277 !AllocatorExpr->isLValue()))) { 19278 Diag(D.Allocator->getExprLoc(), diag::err_omp_var_expected) 19279 << "omp_allocator_handle_t" << (DRE ? 1 : 0) 19280 << AllocatorExpr->getType() << D.Allocator->getSourceRange(); 19281 continue; 19282 } 19283 // OpenMP [2.12.5, target Construct] 19284 // Predefined allocators appearing in a uses_allocators clause cannot have 19285 // traits specified. 19286 if (IsPredefinedAllocator && D.AllocatorTraits) { 19287 Diag(D.AllocatorTraits->getExprLoc(), 19288 diag::err_omp_predefined_allocator_with_traits) 19289 << D.AllocatorTraits->getSourceRange(); 19290 Diag(D.Allocator->getExprLoc(), diag::note_omp_predefined_allocator) 19291 << cast<NamedDecl>(DRE->getDecl())->getName() 19292 << D.Allocator->getSourceRange(); 19293 continue; 19294 } 19295 // OpenMP [2.12.5, target Construct] 19296 // Non-predefined allocators appearing in a uses_allocators clause must 19297 // have traits specified. 19298 if (!IsPredefinedAllocator && !D.AllocatorTraits) { 19299 Diag(D.Allocator->getExprLoc(), 19300 diag::err_omp_nonpredefined_allocator_without_traits); 19301 continue; 19302 } 19303 // No allocator traits - just convert it to rvalue. 19304 if (!D.AllocatorTraits) 19305 AllocatorExpr = DefaultLvalueConversion(AllocatorExpr).get(); 19306 DSAStack->addUsesAllocatorsDecl( 19307 DRE->getDecl(), 19308 IsPredefinedAllocator 19309 ? DSAStackTy::UsesAllocatorsDeclKind::PredefinedAllocator 19310 : DSAStackTy::UsesAllocatorsDeclKind::UserDefinedAllocator); 19311 } 19312 Expr *AllocatorTraitsExpr = nullptr; 19313 if (D.AllocatorTraits) { 19314 if (D.AllocatorTraits->isTypeDependent()) { 19315 AllocatorTraitsExpr = D.AllocatorTraits; 19316 } else { 19317 // OpenMP [2.12.5, target Construct] 19318 // Arrays that contain allocator traits that appear in a uses_allocators 19319 // clause must be constant arrays, have constant values and be defined 19320 // in the same scope as the construct in which the clause appears. 19321 AllocatorTraitsExpr = D.AllocatorTraits->IgnoreParenImpCasts(); 19322 // Check that traits expr is a constant array. 19323 QualType TraitTy; 19324 if (const ArrayType *Ty = 19325 AllocatorTraitsExpr->getType()->getAsArrayTypeUnsafe()) 19326 if (const auto *ConstArrayTy = dyn_cast<ConstantArrayType>(Ty)) 19327 TraitTy = ConstArrayTy->getElementType(); 19328 if (TraitTy.isNull() || 19329 !(Context.hasSameUnqualifiedType(TraitTy, 19330 DSAStack->getOMPAlloctraitT()) || 19331 Context.typesAreCompatible(TraitTy, DSAStack->getOMPAlloctraitT(), 19332 /*CompareUnqualified=*/true))) { 19333 Diag(D.AllocatorTraits->getExprLoc(), 19334 diag::err_omp_expected_array_alloctraits) 19335 << AllocatorTraitsExpr->getType(); 19336 continue; 19337 } 19338 // Do not map by default allocator traits if it is a standalone 19339 // variable. 19340 if (auto *DRE = dyn_cast<DeclRefExpr>(AllocatorTraitsExpr)) 19341 DSAStack->addUsesAllocatorsDecl( 19342 DRE->getDecl(), 19343 DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait); 19344 } 19345 } 19346 OMPUsesAllocatorsClause::Data &NewD = NewData.emplace_back(); 19347 NewD.Allocator = AllocatorExpr; 19348 NewD.AllocatorTraits = AllocatorTraitsExpr; 19349 NewD.LParenLoc = D.LParenLoc; 19350 NewD.RParenLoc = D.RParenLoc; 19351 } 19352 return OMPUsesAllocatorsClause::Create(Context, StartLoc, LParenLoc, EndLoc, 19353 NewData); 19354 } 19355 19356 OMPClause *Sema::ActOnOpenMPAffinityClause( 19357 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc, 19358 SourceLocation EndLoc, Expr *Modifier, ArrayRef<Expr *> Locators) { 19359 SmallVector<Expr *, 8> Vars; 19360 for (Expr *RefExpr : Locators) { 19361 assert(RefExpr && "NULL expr in OpenMP shared clause."); 19362 if (isa<DependentScopeDeclRefExpr>(RefExpr) || RefExpr->isTypeDependent()) { 19363 // It will be analyzed later. 19364 Vars.push_back(RefExpr); 19365 continue; 19366 } 19367 19368 SourceLocation ELoc = RefExpr->getExprLoc(); 19369 Expr *SimpleExpr = RefExpr->IgnoreParenImpCasts(); 19370 19371 if (!SimpleExpr->isLValue()) { 19372 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 19373 << 1 << 0 << RefExpr->getSourceRange(); 19374 continue; 19375 } 19376 19377 ExprResult Res; 19378 { 19379 Sema::TentativeAnalysisScope Trap(*this); 19380 Res = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, SimpleExpr); 19381 } 19382 if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr) && 19383 !isa<OMPArrayShapingExpr>(SimpleExpr)) { 19384 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 19385 << 1 << 0 << RefExpr->getSourceRange(); 19386 continue; 19387 } 19388 Vars.push_back(SimpleExpr); 19389 } 19390 19391 return OMPAffinityClause::Create(Context, StartLoc, LParenLoc, ColonLoc, 19392 EndLoc, Modifier, Vars); 19393 } 19394