1 //===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 /// \file 9 /// This file implements semantic analysis for OpenMP directives and 10 /// clauses. 11 /// 12 //===----------------------------------------------------------------------===// 13 14 #include "TreeTransform.h" 15 #include "clang/AST/ASTContext.h" 16 #include "clang/AST/ASTMutationListener.h" 17 #include "clang/AST/CXXInheritance.h" 18 #include "clang/AST/Decl.h" 19 #include "clang/AST/DeclCXX.h" 20 #include "clang/AST/DeclOpenMP.h" 21 #include "clang/AST/StmtCXX.h" 22 #include "clang/AST/StmtOpenMP.h" 23 #include "clang/AST/StmtVisitor.h" 24 #include "clang/AST/TypeOrdering.h" 25 #include "clang/Basic/DiagnosticSema.h" 26 #include "clang/Basic/OpenMPKinds.h" 27 #include "clang/Basic/PartialDiagnostic.h" 28 #include "clang/Basic/TargetInfo.h" 29 #include "clang/Sema/Initialization.h" 30 #include "clang/Sema/Lookup.h" 31 #include "clang/Sema/Scope.h" 32 #include "clang/Sema/ScopeInfo.h" 33 #include "clang/Sema/SemaInternal.h" 34 #include "llvm/ADT/IndexedMap.h" 35 #include "llvm/ADT/PointerEmbeddedInt.h" 36 #include "llvm/ADT/STLExtras.h" 37 #include "llvm/Frontend/OpenMP/OMPConstants.h" 38 using namespace clang; 39 using namespace llvm::omp; 40 41 //===----------------------------------------------------------------------===// 42 // Stack of data-sharing attributes for variables 43 //===----------------------------------------------------------------------===// 44 45 static const Expr *checkMapClauseExpressionBase( 46 Sema &SemaRef, Expr *E, 47 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents, 48 OpenMPClauseKind CKind, bool NoDiagnose); 49 50 namespace { 51 /// Default data sharing attributes, which can be applied to directive. 52 enum DefaultDataSharingAttributes { 53 DSA_unspecified = 0, /// Data sharing attribute not specified. 54 DSA_none = 1 << 0, /// Default data sharing attribute 'none'. 55 DSA_shared = 1 << 1, /// Default data sharing attribute 'shared'. 56 }; 57 58 /// Stack for tracking declarations used in OpenMP directives and 59 /// clauses and their data-sharing attributes. 60 class DSAStackTy { 61 public: 62 struct DSAVarData { 63 OpenMPDirectiveKind DKind = OMPD_unknown; 64 OpenMPClauseKind CKind = OMPC_unknown; 65 const Expr *RefExpr = nullptr; 66 DeclRefExpr *PrivateCopy = nullptr; 67 SourceLocation ImplicitDSALoc; 68 DSAVarData() = default; 69 DSAVarData(OpenMPDirectiveKind DKind, OpenMPClauseKind CKind, 70 const Expr *RefExpr, DeclRefExpr *PrivateCopy, 71 SourceLocation ImplicitDSALoc) 72 : DKind(DKind), CKind(CKind), RefExpr(RefExpr), 73 PrivateCopy(PrivateCopy), ImplicitDSALoc(ImplicitDSALoc) {} 74 }; 75 using OperatorOffsetTy = 76 llvm::SmallVector<std::pair<Expr *, OverloadedOperatorKind>, 4>; 77 using DoacrossDependMapTy = 78 llvm::DenseMap<OMPDependClause *, OperatorOffsetTy>; 79 80 private: 81 struct DSAInfo { 82 OpenMPClauseKind Attributes = OMPC_unknown; 83 /// Pointer to a reference expression and a flag which shows that the 84 /// variable is marked as lastprivate(true) or not (false). 85 llvm::PointerIntPair<const Expr *, 1, bool> RefExpr; 86 DeclRefExpr *PrivateCopy = nullptr; 87 }; 88 using DeclSAMapTy = llvm::SmallDenseMap<const ValueDecl *, DSAInfo, 8>; 89 using UsedRefMapTy = llvm::SmallDenseMap<const ValueDecl *, const Expr *, 8>; 90 using LCDeclInfo = std::pair<unsigned, VarDecl *>; 91 using LoopControlVariablesMapTy = 92 llvm::SmallDenseMap<const ValueDecl *, LCDeclInfo, 8>; 93 /// Struct that associates a component with the clause kind where they are 94 /// found. 95 struct MappedExprComponentTy { 96 OMPClauseMappableExprCommon::MappableExprComponentLists Components; 97 OpenMPClauseKind Kind = OMPC_unknown; 98 }; 99 using MappedExprComponentsTy = 100 llvm::DenseMap<const ValueDecl *, MappedExprComponentTy>; 101 using CriticalsWithHintsTy = 102 llvm::StringMap<std::pair<const OMPCriticalDirective *, llvm::APSInt>>; 103 struct ReductionData { 104 using BOKPtrType = llvm::PointerEmbeddedInt<BinaryOperatorKind, 16>; 105 SourceRange ReductionRange; 106 llvm::PointerUnion<const Expr *, BOKPtrType> ReductionOp; 107 ReductionData() = default; 108 void set(BinaryOperatorKind BO, SourceRange RR) { 109 ReductionRange = RR; 110 ReductionOp = BO; 111 } 112 void set(const Expr *RefExpr, SourceRange RR) { 113 ReductionRange = RR; 114 ReductionOp = RefExpr; 115 } 116 }; 117 using DeclReductionMapTy = 118 llvm::SmallDenseMap<const ValueDecl *, ReductionData, 4>; 119 struct DefaultmapInfo { 120 OpenMPDefaultmapClauseModifier ImplicitBehavior = 121 OMPC_DEFAULTMAP_MODIFIER_unknown; 122 SourceLocation SLoc; 123 DefaultmapInfo() = default; 124 DefaultmapInfo(OpenMPDefaultmapClauseModifier M, SourceLocation Loc) 125 : ImplicitBehavior(M), SLoc(Loc) {} 126 }; 127 128 struct SharingMapTy { 129 DeclSAMapTy SharingMap; 130 DeclReductionMapTy ReductionMap; 131 UsedRefMapTy AlignedMap; 132 UsedRefMapTy NontemporalMap; 133 MappedExprComponentsTy MappedExprComponents; 134 LoopControlVariablesMapTy LCVMap; 135 DefaultDataSharingAttributes DefaultAttr = DSA_unspecified; 136 SourceLocation DefaultAttrLoc; 137 DefaultmapInfo DefaultmapMap[OMPC_DEFAULTMAP_unknown]; 138 OpenMPDirectiveKind Directive = OMPD_unknown; 139 DeclarationNameInfo DirectiveName; 140 Scope *CurScope = nullptr; 141 SourceLocation ConstructLoc; 142 /// Set of 'depend' clauses with 'sink|source' dependence kind. Required to 143 /// get the data (loop counters etc.) about enclosing loop-based construct. 144 /// This data is required during codegen. 145 DoacrossDependMapTy DoacrossDepends; 146 /// First argument (Expr *) contains optional argument of the 147 /// 'ordered' clause, the second one is true if the regions has 'ordered' 148 /// clause, false otherwise. 149 llvm::Optional<std::pair<const Expr *, OMPOrderedClause *>> OrderedRegion; 150 unsigned AssociatedLoops = 1; 151 bool HasMutipleLoops = false; 152 const Decl *PossiblyLoopCounter = nullptr; 153 bool NowaitRegion = false; 154 bool CancelRegion = false; 155 bool LoopStart = false; 156 bool BodyComplete = false; 157 SourceLocation InnerTeamsRegionLoc; 158 /// Reference to the taskgroup task_reduction reference expression. 159 Expr *TaskgroupReductionRef = nullptr; 160 llvm::DenseSet<QualType> MappedClassesQualTypes; 161 SmallVector<Expr *, 4> InnerUsedAllocators; 162 llvm::DenseSet<CanonicalDeclPtr<Decl>> ImplicitTaskFirstprivates; 163 /// List of globals marked as declare target link in this target region 164 /// (isOpenMPTargetExecutionDirective(Directive) == true). 165 llvm::SmallVector<DeclRefExpr *, 4> DeclareTargetLinkVarDecls; 166 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name, 167 Scope *CurScope, SourceLocation Loc) 168 : Directive(DKind), DirectiveName(Name), CurScope(CurScope), 169 ConstructLoc(Loc) {} 170 SharingMapTy() = default; 171 }; 172 173 using StackTy = SmallVector<SharingMapTy, 4>; 174 175 /// Stack of used declaration and their data-sharing attributes. 176 DeclSAMapTy Threadprivates; 177 const FunctionScopeInfo *CurrentNonCapturingFunctionScope = nullptr; 178 SmallVector<std::pair<StackTy, const FunctionScopeInfo *>, 4> Stack; 179 /// true, if check for DSA must be from parent directive, false, if 180 /// from current directive. 181 OpenMPClauseKind ClauseKindMode = OMPC_unknown; 182 Sema &SemaRef; 183 bool ForceCapturing = false; 184 /// true if all the variables in the target executable directives must be 185 /// captured by reference. 186 bool ForceCaptureByReferenceInTargetExecutable = false; 187 CriticalsWithHintsTy Criticals; 188 unsigned IgnoredStackElements = 0; 189 190 /// Iterators over the stack iterate in order from innermost to outermost 191 /// directive. 192 using const_iterator = StackTy::const_reverse_iterator; 193 const_iterator begin() const { 194 return Stack.empty() ? const_iterator() 195 : Stack.back().first.rbegin() + IgnoredStackElements; 196 } 197 const_iterator end() const { 198 return Stack.empty() ? const_iterator() : Stack.back().first.rend(); 199 } 200 using iterator = StackTy::reverse_iterator; 201 iterator begin() { 202 return Stack.empty() ? iterator() 203 : Stack.back().first.rbegin() + IgnoredStackElements; 204 } 205 iterator end() { 206 return Stack.empty() ? iterator() : Stack.back().first.rend(); 207 } 208 209 // Convenience operations to get at the elements of the stack. 210 211 bool isStackEmpty() const { 212 return Stack.empty() || 213 Stack.back().second != CurrentNonCapturingFunctionScope || 214 Stack.back().first.size() <= IgnoredStackElements; 215 } 216 size_t getStackSize() const { 217 return isStackEmpty() ? 0 218 : Stack.back().first.size() - IgnoredStackElements; 219 } 220 221 SharingMapTy *getTopOfStackOrNull() { 222 size_t Size = getStackSize(); 223 if (Size == 0) 224 return nullptr; 225 return &Stack.back().first[Size - 1]; 226 } 227 const SharingMapTy *getTopOfStackOrNull() const { 228 return const_cast<DSAStackTy&>(*this).getTopOfStackOrNull(); 229 } 230 SharingMapTy &getTopOfStack() { 231 assert(!isStackEmpty() && "no current directive"); 232 return *getTopOfStackOrNull(); 233 } 234 const SharingMapTy &getTopOfStack() const { 235 return const_cast<DSAStackTy&>(*this).getTopOfStack(); 236 } 237 238 SharingMapTy *getSecondOnStackOrNull() { 239 size_t Size = getStackSize(); 240 if (Size <= 1) 241 return nullptr; 242 return &Stack.back().first[Size - 2]; 243 } 244 const SharingMapTy *getSecondOnStackOrNull() const { 245 return const_cast<DSAStackTy&>(*this).getSecondOnStackOrNull(); 246 } 247 248 /// Get the stack element at a certain level (previously returned by 249 /// \c getNestingLevel). 250 /// 251 /// Note that nesting levels count from outermost to innermost, and this is 252 /// the reverse of our iteration order where new inner levels are pushed at 253 /// the front of the stack. 254 SharingMapTy &getStackElemAtLevel(unsigned Level) { 255 assert(Level < getStackSize() && "no such stack element"); 256 return Stack.back().first[Level]; 257 } 258 const SharingMapTy &getStackElemAtLevel(unsigned Level) const { 259 return const_cast<DSAStackTy&>(*this).getStackElemAtLevel(Level); 260 } 261 262 DSAVarData getDSA(const_iterator &Iter, ValueDecl *D) const; 263 264 /// Checks if the variable is a local for OpenMP region. 265 bool isOpenMPLocal(VarDecl *D, const_iterator Iter) const; 266 267 /// Vector of previously declared requires directives 268 SmallVector<const OMPRequiresDecl *, 2> RequiresDecls; 269 /// omp_allocator_handle_t type. 270 QualType OMPAllocatorHandleT; 271 /// omp_depend_t type. 272 QualType OMPDependT; 273 /// Expression for the predefined allocators. 274 Expr *OMPPredefinedAllocators[OMPAllocateDeclAttr::OMPUserDefinedMemAlloc] = { 275 nullptr}; 276 /// Vector of previously encountered target directives 277 SmallVector<SourceLocation, 2> TargetLocations; 278 SourceLocation AtomicLocation; 279 280 public: 281 explicit DSAStackTy(Sema &S) : SemaRef(S) {} 282 283 /// Sets omp_allocator_handle_t type. 284 void setOMPAllocatorHandleT(QualType Ty) { OMPAllocatorHandleT = Ty; } 285 /// Gets omp_allocator_handle_t type. 286 QualType getOMPAllocatorHandleT() const { return OMPAllocatorHandleT; } 287 /// Sets the given default allocator. 288 void setAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, 289 Expr *Allocator) { 290 OMPPredefinedAllocators[AllocatorKind] = Allocator; 291 } 292 /// Returns the specified default allocator. 293 Expr *getAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind) const { 294 return OMPPredefinedAllocators[AllocatorKind]; 295 } 296 /// Sets omp_depend_t type. 297 void setOMPDependT(QualType Ty) { OMPDependT = Ty; } 298 /// Gets omp_depend_t type. 299 QualType getOMPDependT() const { return OMPDependT; } 300 301 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; } 302 OpenMPClauseKind getClauseParsingMode() const { 303 assert(isClauseParsingMode() && "Must be in clause parsing mode."); 304 return ClauseKindMode; 305 } 306 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; } 307 308 bool isBodyComplete() const { 309 const SharingMapTy *Top = getTopOfStackOrNull(); 310 return Top && Top->BodyComplete; 311 } 312 void setBodyComplete() { 313 getTopOfStack().BodyComplete = true; 314 } 315 316 bool isForceVarCapturing() const { return ForceCapturing; } 317 void setForceVarCapturing(bool V) { ForceCapturing = V; } 318 319 void setForceCaptureByReferenceInTargetExecutable(bool V) { 320 ForceCaptureByReferenceInTargetExecutable = V; 321 } 322 bool isForceCaptureByReferenceInTargetExecutable() const { 323 return ForceCaptureByReferenceInTargetExecutable; 324 } 325 326 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName, 327 Scope *CurScope, SourceLocation Loc) { 328 assert(!IgnoredStackElements && 329 "cannot change stack while ignoring elements"); 330 if (Stack.empty() || 331 Stack.back().second != CurrentNonCapturingFunctionScope) 332 Stack.emplace_back(StackTy(), CurrentNonCapturingFunctionScope); 333 Stack.back().first.emplace_back(DKind, DirName, CurScope, Loc); 334 Stack.back().first.back().DefaultAttrLoc = Loc; 335 } 336 337 void pop() { 338 assert(!IgnoredStackElements && 339 "cannot change stack while ignoring elements"); 340 assert(!Stack.back().first.empty() && 341 "Data-sharing attributes stack is empty!"); 342 Stack.back().first.pop_back(); 343 } 344 345 /// RAII object to temporarily leave the scope of a directive when we want to 346 /// logically operate in its parent. 347 class ParentDirectiveScope { 348 DSAStackTy &Self; 349 bool Active; 350 public: 351 ParentDirectiveScope(DSAStackTy &Self, bool Activate) 352 : Self(Self), Active(false) { 353 if (Activate) 354 enable(); 355 } 356 ~ParentDirectiveScope() { disable(); } 357 void disable() { 358 if (Active) { 359 --Self.IgnoredStackElements; 360 Active = false; 361 } 362 } 363 void enable() { 364 if (!Active) { 365 ++Self.IgnoredStackElements; 366 Active = true; 367 } 368 } 369 }; 370 371 /// Marks that we're started loop parsing. 372 void loopInit() { 373 assert(isOpenMPLoopDirective(getCurrentDirective()) && 374 "Expected loop-based directive."); 375 getTopOfStack().LoopStart = true; 376 } 377 /// Start capturing of the variables in the loop context. 378 void loopStart() { 379 assert(isOpenMPLoopDirective(getCurrentDirective()) && 380 "Expected loop-based directive."); 381 getTopOfStack().LoopStart = false; 382 } 383 /// true, if variables are captured, false otherwise. 384 bool isLoopStarted() const { 385 assert(isOpenMPLoopDirective(getCurrentDirective()) && 386 "Expected loop-based directive."); 387 return !getTopOfStack().LoopStart; 388 } 389 /// Marks (or clears) declaration as possibly loop counter. 390 void resetPossibleLoopCounter(const Decl *D = nullptr) { 391 getTopOfStack().PossiblyLoopCounter = 392 D ? D->getCanonicalDecl() : D; 393 } 394 /// Gets the possible loop counter decl. 395 const Decl *getPossiblyLoopCunter() const { 396 return getTopOfStack().PossiblyLoopCounter; 397 } 398 /// Start new OpenMP region stack in new non-capturing function. 399 void pushFunction() { 400 assert(!IgnoredStackElements && 401 "cannot change stack while ignoring elements"); 402 const FunctionScopeInfo *CurFnScope = SemaRef.getCurFunction(); 403 assert(!isa<CapturingScopeInfo>(CurFnScope)); 404 CurrentNonCapturingFunctionScope = CurFnScope; 405 } 406 /// Pop region stack for non-capturing function. 407 void popFunction(const FunctionScopeInfo *OldFSI) { 408 assert(!IgnoredStackElements && 409 "cannot change stack while ignoring elements"); 410 if (!Stack.empty() && Stack.back().second == OldFSI) { 411 assert(Stack.back().first.empty()); 412 Stack.pop_back(); 413 } 414 CurrentNonCapturingFunctionScope = nullptr; 415 for (const FunctionScopeInfo *FSI : llvm::reverse(SemaRef.FunctionScopes)) { 416 if (!isa<CapturingScopeInfo>(FSI)) { 417 CurrentNonCapturingFunctionScope = FSI; 418 break; 419 } 420 } 421 } 422 423 void addCriticalWithHint(const OMPCriticalDirective *D, llvm::APSInt Hint) { 424 Criticals.try_emplace(D->getDirectiveName().getAsString(), D, Hint); 425 } 426 const std::pair<const OMPCriticalDirective *, llvm::APSInt> 427 getCriticalWithHint(const DeclarationNameInfo &Name) const { 428 auto I = Criticals.find(Name.getAsString()); 429 if (I != Criticals.end()) 430 return I->second; 431 return std::make_pair(nullptr, llvm::APSInt()); 432 } 433 /// If 'aligned' declaration for given variable \a D was not seen yet, 434 /// add it and return NULL; otherwise return previous occurrence's expression 435 /// for diagnostics. 436 const Expr *addUniqueAligned(const ValueDecl *D, const Expr *NewDE); 437 /// If 'nontemporal' declaration for given variable \a D was not seen yet, 438 /// add it and return NULL; otherwise return previous occurrence's expression 439 /// for diagnostics. 440 const Expr *addUniqueNontemporal(const ValueDecl *D, const Expr *NewDE); 441 442 /// Register specified variable as loop control variable. 443 void addLoopControlVariable(const ValueDecl *D, VarDecl *Capture); 444 /// Check if the specified variable is a loop control variable for 445 /// current region. 446 /// \return The index of the loop control variable in the list of associated 447 /// for-loops (from outer to inner). 448 const LCDeclInfo isLoopControlVariable(const ValueDecl *D) const; 449 /// Check if the specified variable is a loop control variable for 450 /// parent region. 451 /// \return The index of the loop control variable in the list of associated 452 /// for-loops (from outer to inner). 453 const LCDeclInfo isParentLoopControlVariable(const ValueDecl *D) const; 454 /// Check if the specified variable is a loop control variable for 455 /// current region. 456 /// \return The index of the loop control variable in the list of associated 457 /// for-loops (from outer to inner). 458 const LCDeclInfo isLoopControlVariable(const ValueDecl *D, 459 unsigned Level) const; 460 /// Get the loop control variable for the I-th loop (or nullptr) in 461 /// parent directive. 462 const ValueDecl *getParentLoopControlVariable(unsigned I) const; 463 464 /// Adds explicit data sharing attribute to the specified declaration. 465 void addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A, 466 DeclRefExpr *PrivateCopy = nullptr); 467 468 /// Adds additional information for the reduction items with the reduction id 469 /// represented as an operator. 470 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 471 BinaryOperatorKind BOK); 472 /// Adds additional information for the reduction items with the reduction id 473 /// represented as reduction identifier. 474 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 475 const Expr *ReductionRef); 476 /// Returns the location and reduction operation from the innermost parent 477 /// region for the given \p D. 478 const DSAVarData 479 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR, 480 BinaryOperatorKind &BOK, 481 Expr *&TaskgroupDescriptor) const; 482 /// Returns the location and reduction operation from the innermost parent 483 /// region for the given \p D. 484 const DSAVarData 485 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR, 486 const Expr *&ReductionRef, 487 Expr *&TaskgroupDescriptor) const; 488 /// Return reduction reference expression for the current taskgroup. 489 Expr *getTaskgroupReductionRef() const { 490 assert(getTopOfStack().Directive == OMPD_taskgroup && 491 "taskgroup reference expression requested for non taskgroup " 492 "directive."); 493 return getTopOfStack().TaskgroupReductionRef; 494 } 495 /// Checks if the given \p VD declaration is actually a taskgroup reduction 496 /// descriptor variable at the \p Level of OpenMP regions. 497 bool isTaskgroupReductionRef(const ValueDecl *VD, unsigned Level) const { 498 return getStackElemAtLevel(Level).TaskgroupReductionRef && 499 cast<DeclRefExpr>(getStackElemAtLevel(Level).TaskgroupReductionRef) 500 ->getDecl() == VD; 501 } 502 503 /// Returns data sharing attributes from top of the stack for the 504 /// specified declaration. 505 const DSAVarData getTopDSA(ValueDecl *D, bool FromParent); 506 /// Returns data-sharing attributes for the specified declaration. 507 const DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent) const; 508 /// Returns data-sharing attributes for the specified declaration. 509 const DSAVarData getImplicitDSA(ValueDecl *D, unsigned Level) const; 510 /// Checks if the specified variables has data-sharing attributes which 511 /// match specified \a CPred predicate in any directive which matches \a DPred 512 /// predicate. 513 const DSAVarData 514 hasDSA(ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 515 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 516 bool FromParent) const; 517 /// Checks if the specified variables has data-sharing attributes which 518 /// match specified \a CPred predicate in any innermost directive which 519 /// matches \a DPred predicate. 520 const DSAVarData 521 hasInnermostDSA(ValueDecl *D, 522 const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 523 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 524 bool FromParent) const; 525 /// Checks if the specified variables has explicit data-sharing 526 /// attributes which match specified \a CPred predicate at the specified 527 /// OpenMP region. 528 bool hasExplicitDSA(const ValueDecl *D, 529 const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 530 unsigned Level, bool NotLastprivate = false) const; 531 532 /// Returns true if the directive at level \Level matches in the 533 /// specified \a DPred predicate. 534 bool hasExplicitDirective( 535 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 536 unsigned Level) const; 537 538 /// Finds a directive which matches specified \a DPred predicate. 539 bool hasDirective( 540 const llvm::function_ref<bool( 541 OpenMPDirectiveKind, const DeclarationNameInfo &, SourceLocation)> 542 DPred, 543 bool FromParent) const; 544 545 /// Returns currently analyzed directive. 546 OpenMPDirectiveKind getCurrentDirective() const { 547 const SharingMapTy *Top = getTopOfStackOrNull(); 548 return Top ? Top->Directive : OMPD_unknown; 549 } 550 /// Returns directive kind at specified level. 551 OpenMPDirectiveKind getDirective(unsigned Level) const { 552 assert(!isStackEmpty() && "No directive at specified level."); 553 return getStackElemAtLevel(Level).Directive; 554 } 555 /// Returns the capture region at the specified level. 556 OpenMPDirectiveKind getCaptureRegion(unsigned Level, 557 unsigned OpenMPCaptureLevel) const { 558 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 559 getOpenMPCaptureRegions(CaptureRegions, getDirective(Level)); 560 return CaptureRegions[OpenMPCaptureLevel]; 561 } 562 /// Returns parent directive. 563 OpenMPDirectiveKind getParentDirective() const { 564 const SharingMapTy *Parent = getSecondOnStackOrNull(); 565 return Parent ? Parent->Directive : OMPD_unknown; 566 } 567 568 /// Add requires decl to internal vector 569 void addRequiresDecl(OMPRequiresDecl *RD) { 570 RequiresDecls.push_back(RD); 571 } 572 573 /// Checks if the defined 'requires' directive has specified type of clause. 574 template <typename ClauseType> 575 bool hasRequiresDeclWithClause() const { 576 return llvm::any_of(RequiresDecls, [](const OMPRequiresDecl *D) { 577 return llvm::any_of(D->clauselists(), [](const OMPClause *C) { 578 return isa<ClauseType>(C); 579 }); 580 }); 581 } 582 583 /// Checks for a duplicate clause amongst previously declared requires 584 /// directives 585 bool hasDuplicateRequiresClause(ArrayRef<OMPClause *> ClauseList) const { 586 bool IsDuplicate = false; 587 for (OMPClause *CNew : ClauseList) { 588 for (const OMPRequiresDecl *D : RequiresDecls) { 589 for (const OMPClause *CPrev : D->clauselists()) { 590 if (CNew->getClauseKind() == CPrev->getClauseKind()) { 591 SemaRef.Diag(CNew->getBeginLoc(), 592 diag::err_omp_requires_clause_redeclaration) 593 << getOpenMPClauseName(CNew->getClauseKind()); 594 SemaRef.Diag(CPrev->getBeginLoc(), 595 diag::note_omp_requires_previous_clause) 596 << getOpenMPClauseName(CPrev->getClauseKind()); 597 IsDuplicate = true; 598 } 599 } 600 } 601 } 602 return IsDuplicate; 603 } 604 605 /// Add location of previously encountered target to internal vector 606 void addTargetDirLocation(SourceLocation LocStart) { 607 TargetLocations.push_back(LocStart); 608 } 609 610 /// Add location for the first encountered atomicc directive. 611 void addAtomicDirectiveLoc(SourceLocation Loc) { 612 if (AtomicLocation.isInvalid()) 613 AtomicLocation = Loc; 614 } 615 616 /// Returns the location of the first encountered atomic directive in the 617 /// module. 618 SourceLocation getAtomicDirectiveLoc() const { 619 return AtomicLocation; 620 } 621 622 // Return previously encountered target region locations. 623 ArrayRef<SourceLocation> getEncounteredTargetLocs() const { 624 return TargetLocations; 625 } 626 627 /// Set default data sharing attribute to none. 628 void setDefaultDSANone(SourceLocation Loc) { 629 getTopOfStack().DefaultAttr = DSA_none; 630 getTopOfStack().DefaultAttrLoc = Loc; 631 } 632 /// Set default data sharing attribute to shared. 633 void setDefaultDSAShared(SourceLocation Loc) { 634 getTopOfStack().DefaultAttr = DSA_shared; 635 getTopOfStack().DefaultAttrLoc = Loc; 636 } 637 /// Set default data mapping attribute to Modifier:Kind 638 void setDefaultDMAAttr(OpenMPDefaultmapClauseModifier M, 639 OpenMPDefaultmapClauseKind Kind, 640 SourceLocation Loc) { 641 DefaultmapInfo &DMI = getTopOfStack().DefaultmapMap[Kind]; 642 DMI.ImplicitBehavior = M; 643 DMI.SLoc = Loc; 644 } 645 /// Check whether the implicit-behavior has been set in defaultmap 646 bool checkDefaultmapCategory(OpenMPDefaultmapClauseKind VariableCategory) { 647 return getTopOfStack().DefaultmapMap[VariableCategory].ImplicitBehavior != 648 OMPC_DEFAULTMAP_MODIFIER_unknown; 649 } 650 651 DefaultDataSharingAttributes getDefaultDSA(unsigned Level) const { 652 return getStackSize() <= Level ? DSA_unspecified 653 : getStackElemAtLevel(Level).DefaultAttr; 654 } 655 DefaultDataSharingAttributes getDefaultDSA() const { 656 return isStackEmpty() ? DSA_unspecified 657 : getTopOfStack().DefaultAttr; 658 } 659 SourceLocation getDefaultDSALocation() const { 660 return isStackEmpty() ? SourceLocation() 661 : getTopOfStack().DefaultAttrLoc; 662 } 663 OpenMPDefaultmapClauseModifier 664 getDefaultmapModifier(OpenMPDefaultmapClauseKind Kind) const { 665 return isStackEmpty() 666 ? OMPC_DEFAULTMAP_MODIFIER_unknown 667 : getTopOfStack().DefaultmapMap[Kind].ImplicitBehavior; 668 } 669 OpenMPDefaultmapClauseModifier 670 getDefaultmapModifierAtLevel(unsigned Level, 671 OpenMPDefaultmapClauseKind Kind) const { 672 return getStackElemAtLevel(Level).DefaultmapMap[Kind].ImplicitBehavior; 673 } 674 bool isDefaultmapCapturedByRef(unsigned Level, 675 OpenMPDefaultmapClauseKind Kind) const { 676 OpenMPDefaultmapClauseModifier M = 677 getDefaultmapModifierAtLevel(Level, Kind); 678 if (Kind == OMPC_DEFAULTMAP_scalar || Kind == OMPC_DEFAULTMAP_pointer) { 679 return (M == OMPC_DEFAULTMAP_MODIFIER_alloc) || 680 (M == OMPC_DEFAULTMAP_MODIFIER_to) || 681 (M == OMPC_DEFAULTMAP_MODIFIER_from) || 682 (M == OMPC_DEFAULTMAP_MODIFIER_tofrom); 683 } 684 return true; 685 } 686 static bool mustBeFirstprivateBase(OpenMPDefaultmapClauseModifier M, 687 OpenMPDefaultmapClauseKind Kind) { 688 switch (Kind) { 689 case OMPC_DEFAULTMAP_scalar: 690 case OMPC_DEFAULTMAP_pointer: 691 return (M == OMPC_DEFAULTMAP_MODIFIER_unknown) || 692 (M == OMPC_DEFAULTMAP_MODIFIER_firstprivate) || 693 (M == OMPC_DEFAULTMAP_MODIFIER_default); 694 case OMPC_DEFAULTMAP_aggregate: 695 return M == OMPC_DEFAULTMAP_MODIFIER_firstprivate; 696 default: 697 break; 698 } 699 llvm_unreachable("Unexpected OpenMPDefaultmapClauseKind enum"); 700 } 701 bool mustBeFirstprivateAtLevel(unsigned Level, 702 OpenMPDefaultmapClauseKind Kind) const { 703 OpenMPDefaultmapClauseModifier M = 704 getDefaultmapModifierAtLevel(Level, Kind); 705 return mustBeFirstprivateBase(M, Kind); 706 } 707 bool mustBeFirstprivate(OpenMPDefaultmapClauseKind Kind) const { 708 OpenMPDefaultmapClauseModifier M = getDefaultmapModifier(Kind); 709 return mustBeFirstprivateBase(M, Kind); 710 } 711 712 /// Checks if the specified variable is a threadprivate. 713 bool isThreadPrivate(VarDecl *D) { 714 const DSAVarData DVar = getTopDSA(D, false); 715 return isOpenMPThreadPrivate(DVar.CKind); 716 } 717 718 /// Marks current region as ordered (it has an 'ordered' clause). 719 void setOrderedRegion(bool IsOrdered, const Expr *Param, 720 OMPOrderedClause *Clause) { 721 if (IsOrdered) 722 getTopOfStack().OrderedRegion.emplace(Param, Clause); 723 else 724 getTopOfStack().OrderedRegion.reset(); 725 } 726 /// Returns true, if region is ordered (has associated 'ordered' clause), 727 /// false - otherwise. 728 bool isOrderedRegion() const { 729 if (const SharingMapTy *Top = getTopOfStackOrNull()) 730 return Top->OrderedRegion.hasValue(); 731 return false; 732 } 733 /// Returns optional parameter for the ordered region. 734 std::pair<const Expr *, OMPOrderedClause *> getOrderedRegionParam() const { 735 if (const SharingMapTy *Top = getTopOfStackOrNull()) 736 if (Top->OrderedRegion.hasValue()) 737 return Top->OrderedRegion.getValue(); 738 return std::make_pair(nullptr, nullptr); 739 } 740 /// Returns true, if parent region is ordered (has associated 741 /// 'ordered' clause), false - otherwise. 742 bool isParentOrderedRegion() const { 743 if (const SharingMapTy *Parent = getSecondOnStackOrNull()) 744 return Parent->OrderedRegion.hasValue(); 745 return false; 746 } 747 /// Returns optional parameter for the ordered region. 748 std::pair<const Expr *, OMPOrderedClause *> 749 getParentOrderedRegionParam() const { 750 if (const SharingMapTy *Parent = getSecondOnStackOrNull()) 751 if (Parent->OrderedRegion.hasValue()) 752 return Parent->OrderedRegion.getValue(); 753 return std::make_pair(nullptr, nullptr); 754 } 755 /// Marks current region as nowait (it has a 'nowait' clause). 756 void setNowaitRegion(bool IsNowait = true) { 757 getTopOfStack().NowaitRegion = IsNowait; 758 } 759 /// Returns true, if parent region is nowait (has associated 760 /// 'nowait' clause), false - otherwise. 761 bool isParentNowaitRegion() const { 762 if (const SharingMapTy *Parent = getSecondOnStackOrNull()) 763 return Parent->NowaitRegion; 764 return false; 765 } 766 /// Marks parent region as cancel region. 767 void setParentCancelRegion(bool Cancel = true) { 768 if (SharingMapTy *Parent = getSecondOnStackOrNull()) 769 Parent->CancelRegion |= Cancel; 770 } 771 /// Return true if current region has inner cancel construct. 772 bool isCancelRegion() const { 773 const SharingMapTy *Top = getTopOfStackOrNull(); 774 return Top ? Top->CancelRegion : false; 775 } 776 777 /// Set collapse value for the region. 778 void setAssociatedLoops(unsigned Val) { 779 getTopOfStack().AssociatedLoops = Val; 780 if (Val > 1) 781 getTopOfStack().HasMutipleLoops = true; 782 } 783 /// Return collapse value for region. 784 unsigned getAssociatedLoops() const { 785 const SharingMapTy *Top = getTopOfStackOrNull(); 786 return Top ? Top->AssociatedLoops : 0; 787 } 788 /// Returns true if the construct is associated with multiple loops. 789 bool hasMutipleLoops() const { 790 const SharingMapTy *Top = getTopOfStackOrNull(); 791 return Top ? Top->HasMutipleLoops : false; 792 } 793 794 /// Marks current target region as one with closely nested teams 795 /// region. 796 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) { 797 if (SharingMapTy *Parent = getSecondOnStackOrNull()) 798 Parent->InnerTeamsRegionLoc = TeamsRegionLoc; 799 } 800 /// Returns true, if current region has closely nested teams region. 801 bool hasInnerTeamsRegion() const { 802 return getInnerTeamsRegionLoc().isValid(); 803 } 804 /// Returns location of the nested teams region (if any). 805 SourceLocation getInnerTeamsRegionLoc() const { 806 const SharingMapTy *Top = getTopOfStackOrNull(); 807 return Top ? Top->InnerTeamsRegionLoc : SourceLocation(); 808 } 809 810 Scope *getCurScope() const { 811 const SharingMapTy *Top = getTopOfStackOrNull(); 812 return Top ? Top->CurScope : nullptr; 813 } 814 SourceLocation getConstructLoc() const { 815 const SharingMapTy *Top = getTopOfStackOrNull(); 816 return Top ? Top->ConstructLoc : SourceLocation(); 817 } 818 819 /// Do the check specified in \a Check to all component lists and return true 820 /// if any issue is found. 821 bool checkMappableExprComponentListsForDecl( 822 const ValueDecl *VD, bool CurrentRegionOnly, 823 const llvm::function_ref< 824 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef, 825 OpenMPClauseKind)> 826 Check) const { 827 if (isStackEmpty()) 828 return false; 829 auto SI = begin(); 830 auto SE = end(); 831 832 if (SI == SE) 833 return false; 834 835 if (CurrentRegionOnly) 836 SE = std::next(SI); 837 else 838 std::advance(SI, 1); 839 840 for (; SI != SE; ++SI) { 841 auto MI = SI->MappedExprComponents.find(VD); 842 if (MI != SI->MappedExprComponents.end()) 843 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L : 844 MI->second.Components) 845 if (Check(L, MI->second.Kind)) 846 return true; 847 } 848 return false; 849 } 850 851 /// Do the check specified in \a Check to all component lists at a given level 852 /// and return true if any issue is found. 853 bool checkMappableExprComponentListsForDeclAtLevel( 854 const ValueDecl *VD, unsigned Level, 855 const llvm::function_ref< 856 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef, 857 OpenMPClauseKind)> 858 Check) const { 859 if (getStackSize() <= Level) 860 return false; 861 862 const SharingMapTy &StackElem = getStackElemAtLevel(Level); 863 auto MI = StackElem.MappedExprComponents.find(VD); 864 if (MI != StackElem.MappedExprComponents.end()) 865 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L : 866 MI->second.Components) 867 if (Check(L, MI->second.Kind)) 868 return true; 869 return false; 870 } 871 872 /// Create a new mappable expression component list associated with a given 873 /// declaration and initialize it with the provided list of components. 874 void addMappableExpressionComponents( 875 const ValueDecl *VD, 876 OMPClauseMappableExprCommon::MappableExprComponentListRef Components, 877 OpenMPClauseKind WhereFoundClauseKind) { 878 MappedExprComponentTy &MEC = getTopOfStack().MappedExprComponents[VD]; 879 // Create new entry and append the new components there. 880 MEC.Components.resize(MEC.Components.size() + 1); 881 MEC.Components.back().append(Components.begin(), Components.end()); 882 MEC.Kind = WhereFoundClauseKind; 883 } 884 885 unsigned getNestingLevel() const { 886 assert(!isStackEmpty()); 887 return getStackSize() - 1; 888 } 889 void addDoacrossDependClause(OMPDependClause *C, 890 const OperatorOffsetTy &OpsOffs) { 891 SharingMapTy *Parent = getSecondOnStackOrNull(); 892 assert(Parent && isOpenMPWorksharingDirective(Parent->Directive)); 893 Parent->DoacrossDepends.try_emplace(C, OpsOffs); 894 } 895 llvm::iterator_range<DoacrossDependMapTy::const_iterator> 896 getDoacrossDependClauses() const { 897 const SharingMapTy &StackElem = getTopOfStack(); 898 if (isOpenMPWorksharingDirective(StackElem.Directive)) { 899 const DoacrossDependMapTy &Ref = StackElem.DoacrossDepends; 900 return llvm::make_range(Ref.begin(), Ref.end()); 901 } 902 return llvm::make_range(StackElem.DoacrossDepends.end(), 903 StackElem.DoacrossDepends.end()); 904 } 905 906 // Store types of classes which have been explicitly mapped 907 void addMappedClassesQualTypes(QualType QT) { 908 SharingMapTy &StackElem = getTopOfStack(); 909 StackElem.MappedClassesQualTypes.insert(QT); 910 } 911 912 // Return set of mapped classes types 913 bool isClassPreviouslyMapped(QualType QT) const { 914 const SharingMapTy &StackElem = getTopOfStack(); 915 return StackElem.MappedClassesQualTypes.count(QT) != 0; 916 } 917 918 /// Adds global declare target to the parent target region. 919 void addToParentTargetRegionLinkGlobals(DeclRefExpr *E) { 920 assert(*OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration( 921 E->getDecl()) == OMPDeclareTargetDeclAttr::MT_Link && 922 "Expected declare target link global."); 923 for (auto &Elem : *this) { 924 if (isOpenMPTargetExecutionDirective(Elem.Directive)) { 925 Elem.DeclareTargetLinkVarDecls.push_back(E); 926 return; 927 } 928 } 929 } 930 931 /// Returns the list of globals with declare target link if current directive 932 /// is target. 933 ArrayRef<DeclRefExpr *> getLinkGlobals() const { 934 assert(isOpenMPTargetExecutionDirective(getCurrentDirective()) && 935 "Expected target executable directive."); 936 return getTopOfStack().DeclareTargetLinkVarDecls; 937 } 938 939 /// Adds list of allocators expressions. 940 void addInnerAllocatorExpr(Expr *E) { 941 getTopOfStack().InnerUsedAllocators.push_back(E); 942 } 943 /// Return list of used allocators. 944 ArrayRef<Expr *> getInnerAllocators() const { 945 return getTopOfStack().InnerUsedAllocators; 946 } 947 /// Marks the declaration as implicitly firstprivate nin the task-based 948 /// regions. 949 void addImplicitTaskFirstprivate(unsigned Level, Decl *D) { 950 getStackElemAtLevel(Level).ImplicitTaskFirstprivates.insert(D); 951 } 952 /// Checks if the decl is implicitly firstprivate in the task-based region. 953 bool isImplicitTaskFirstprivate(Decl *D) const { 954 return getTopOfStack().ImplicitTaskFirstprivates.count(D) > 0; 955 } 956 }; 957 958 bool isImplicitTaskingRegion(OpenMPDirectiveKind DKind) { 959 return isOpenMPParallelDirective(DKind) || isOpenMPTeamsDirective(DKind); 960 } 961 962 bool isImplicitOrExplicitTaskingRegion(OpenMPDirectiveKind DKind) { 963 return isImplicitTaskingRegion(DKind) || isOpenMPTaskingDirective(DKind) || 964 DKind == OMPD_unknown; 965 } 966 967 } // namespace 968 969 static const Expr *getExprAsWritten(const Expr *E) { 970 if (const auto *FE = dyn_cast<FullExpr>(E)) 971 E = FE->getSubExpr(); 972 973 if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E)) 974 E = MTE->getSubExpr(); 975 976 while (const auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E)) 977 E = Binder->getSubExpr(); 978 979 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E)) 980 E = ICE->getSubExprAsWritten(); 981 return E->IgnoreParens(); 982 } 983 984 static Expr *getExprAsWritten(Expr *E) { 985 return const_cast<Expr *>(getExprAsWritten(const_cast<const Expr *>(E))); 986 } 987 988 static const ValueDecl *getCanonicalDecl(const ValueDecl *D) { 989 if (const auto *CED = dyn_cast<OMPCapturedExprDecl>(D)) 990 if (const auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 991 D = ME->getMemberDecl(); 992 const auto *VD = dyn_cast<VarDecl>(D); 993 const auto *FD = dyn_cast<FieldDecl>(D); 994 if (VD != nullptr) { 995 VD = VD->getCanonicalDecl(); 996 D = VD; 997 } else { 998 assert(FD); 999 FD = FD->getCanonicalDecl(); 1000 D = FD; 1001 } 1002 return D; 1003 } 1004 1005 static ValueDecl *getCanonicalDecl(ValueDecl *D) { 1006 return const_cast<ValueDecl *>( 1007 getCanonicalDecl(const_cast<const ValueDecl *>(D))); 1008 } 1009 1010 DSAStackTy::DSAVarData DSAStackTy::getDSA(const_iterator &Iter, 1011 ValueDecl *D) const { 1012 D = getCanonicalDecl(D); 1013 auto *VD = dyn_cast<VarDecl>(D); 1014 const auto *FD = dyn_cast<FieldDecl>(D); 1015 DSAVarData DVar; 1016 if (Iter == end()) { 1017 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1018 // in a region but not in construct] 1019 // File-scope or namespace-scope variables referenced in called routines 1020 // in the region are shared unless they appear in a threadprivate 1021 // directive. 1022 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(VD)) 1023 DVar.CKind = OMPC_shared; 1024 1025 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced 1026 // in a region but not in construct] 1027 // Variables with static storage duration that are declared in called 1028 // routines in the region are shared. 1029 if (VD && VD->hasGlobalStorage()) 1030 DVar.CKind = OMPC_shared; 1031 1032 // Non-static data members are shared by default. 1033 if (FD) 1034 DVar.CKind = OMPC_shared; 1035 1036 return DVar; 1037 } 1038 1039 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1040 // in a Construct, C/C++, predetermined, p.1] 1041 // Variables with automatic storage duration that are declared in a scope 1042 // inside the construct are private. 1043 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() && 1044 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) { 1045 DVar.CKind = OMPC_private; 1046 return DVar; 1047 } 1048 1049 DVar.DKind = Iter->Directive; 1050 // Explicitly specified attributes and local variables with predetermined 1051 // attributes. 1052 if (Iter->SharingMap.count(D)) { 1053 const DSAInfo &Data = Iter->SharingMap.lookup(D); 1054 DVar.RefExpr = Data.RefExpr.getPointer(); 1055 DVar.PrivateCopy = Data.PrivateCopy; 1056 DVar.CKind = Data.Attributes; 1057 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 1058 return DVar; 1059 } 1060 1061 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1062 // in a Construct, C/C++, implicitly determined, p.1] 1063 // In a parallel or task construct, the data-sharing attributes of these 1064 // variables are determined by the default clause, if present. 1065 switch (Iter->DefaultAttr) { 1066 case DSA_shared: 1067 DVar.CKind = OMPC_shared; 1068 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 1069 return DVar; 1070 case DSA_none: 1071 return DVar; 1072 case DSA_unspecified: 1073 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1074 // in a Construct, implicitly determined, p.2] 1075 // In a parallel construct, if no default clause is present, these 1076 // variables are shared. 1077 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 1078 if ((isOpenMPParallelDirective(DVar.DKind) && 1079 !isOpenMPTaskLoopDirective(DVar.DKind)) || 1080 isOpenMPTeamsDirective(DVar.DKind)) { 1081 DVar.CKind = OMPC_shared; 1082 return DVar; 1083 } 1084 1085 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1086 // in a Construct, implicitly determined, p.4] 1087 // In a task construct, if no default clause is present, a variable that in 1088 // the enclosing context is determined to be shared by all implicit tasks 1089 // bound to the current team is shared. 1090 if (isOpenMPTaskingDirective(DVar.DKind)) { 1091 DSAVarData DVarTemp; 1092 const_iterator I = Iter, E = end(); 1093 do { 1094 ++I; 1095 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables 1096 // Referenced in a Construct, implicitly determined, p.6] 1097 // In a task construct, if no default clause is present, a variable 1098 // whose data-sharing attribute is not determined by the rules above is 1099 // firstprivate. 1100 DVarTemp = getDSA(I, D); 1101 if (DVarTemp.CKind != OMPC_shared) { 1102 DVar.RefExpr = nullptr; 1103 DVar.CKind = OMPC_firstprivate; 1104 return DVar; 1105 } 1106 } while (I != E && !isImplicitTaskingRegion(I->Directive)); 1107 DVar.CKind = 1108 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared; 1109 return DVar; 1110 } 1111 } 1112 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1113 // in a Construct, implicitly determined, p.3] 1114 // For constructs other than task, if no default clause is present, these 1115 // variables inherit their data-sharing attributes from the enclosing 1116 // context. 1117 return getDSA(++Iter, D); 1118 } 1119 1120 const Expr *DSAStackTy::addUniqueAligned(const ValueDecl *D, 1121 const Expr *NewDE) { 1122 assert(!isStackEmpty() && "Data sharing attributes stack is empty"); 1123 D = getCanonicalDecl(D); 1124 SharingMapTy &StackElem = getTopOfStack(); 1125 auto It = StackElem.AlignedMap.find(D); 1126 if (It == StackElem.AlignedMap.end()) { 1127 assert(NewDE && "Unexpected nullptr expr to be added into aligned map"); 1128 StackElem.AlignedMap[D] = NewDE; 1129 return nullptr; 1130 } 1131 assert(It->second && "Unexpected nullptr expr in the aligned map"); 1132 return It->second; 1133 } 1134 1135 const Expr *DSAStackTy::addUniqueNontemporal(const ValueDecl *D, 1136 const Expr *NewDE) { 1137 assert(!isStackEmpty() && "Data sharing attributes stack is empty"); 1138 D = getCanonicalDecl(D); 1139 SharingMapTy &StackElem = getTopOfStack(); 1140 auto It = StackElem.NontemporalMap.find(D); 1141 if (It == StackElem.NontemporalMap.end()) { 1142 assert(NewDE && "Unexpected nullptr expr to be added into aligned map"); 1143 StackElem.NontemporalMap[D] = NewDE; 1144 return nullptr; 1145 } 1146 assert(It->second && "Unexpected nullptr expr in the aligned map"); 1147 return It->second; 1148 } 1149 1150 void DSAStackTy::addLoopControlVariable(const ValueDecl *D, VarDecl *Capture) { 1151 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1152 D = getCanonicalDecl(D); 1153 SharingMapTy &StackElem = getTopOfStack(); 1154 StackElem.LCVMap.try_emplace( 1155 D, LCDeclInfo(StackElem.LCVMap.size() + 1, Capture)); 1156 } 1157 1158 const DSAStackTy::LCDeclInfo 1159 DSAStackTy::isLoopControlVariable(const ValueDecl *D) const { 1160 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1161 D = getCanonicalDecl(D); 1162 const SharingMapTy &StackElem = getTopOfStack(); 1163 auto It = StackElem.LCVMap.find(D); 1164 if (It != StackElem.LCVMap.end()) 1165 return It->second; 1166 return {0, nullptr}; 1167 } 1168 1169 const DSAStackTy::LCDeclInfo 1170 DSAStackTy::isLoopControlVariable(const ValueDecl *D, unsigned Level) const { 1171 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1172 D = getCanonicalDecl(D); 1173 for (unsigned I = Level + 1; I > 0; --I) { 1174 const SharingMapTy &StackElem = getStackElemAtLevel(I - 1); 1175 auto It = StackElem.LCVMap.find(D); 1176 if (It != StackElem.LCVMap.end()) 1177 return It->second; 1178 } 1179 return {0, nullptr}; 1180 } 1181 1182 const DSAStackTy::LCDeclInfo 1183 DSAStackTy::isParentLoopControlVariable(const ValueDecl *D) const { 1184 const SharingMapTy *Parent = getSecondOnStackOrNull(); 1185 assert(Parent && "Data-sharing attributes stack is empty"); 1186 D = getCanonicalDecl(D); 1187 auto It = Parent->LCVMap.find(D); 1188 if (It != Parent->LCVMap.end()) 1189 return It->second; 1190 return {0, nullptr}; 1191 } 1192 1193 const ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) const { 1194 const SharingMapTy *Parent = getSecondOnStackOrNull(); 1195 assert(Parent && "Data-sharing attributes stack is empty"); 1196 if (Parent->LCVMap.size() < I) 1197 return nullptr; 1198 for (const auto &Pair : Parent->LCVMap) 1199 if (Pair.second.first == I) 1200 return Pair.first; 1201 return nullptr; 1202 } 1203 1204 void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A, 1205 DeclRefExpr *PrivateCopy) { 1206 D = getCanonicalDecl(D); 1207 if (A == OMPC_threadprivate) { 1208 DSAInfo &Data = Threadprivates[D]; 1209 Data.Attributes = A; 1210 Data.RefExpr.setPointer(E); 1211 Data.PrivateCopy = nullptr; 1212 } else { 1213 DSAInfo &Data = getTopOfStack().SharingMap[D]; 1214 assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) || 1215 (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) || 1216 (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) || 1217 (isLoopControlVariable(D).first && A == OMPC_private)); 1218 if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) { 1219 Data.RefExpr.setInt(/*IntVal=*/true); 1220 return; 1221 } 1222 const bool IsLastprivate = 1223 A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate; 1224 Data.Attributes = A; 1225 Data.RefExpr.setPointerAndInt(E, IsLastprivate); 1226 Data.PrivateCopy = PrivateCopy; 1227 if (PrivateCopy) { 1228 DSAInfo &Data = getTopOfStack().SharingMap[PrivateCopy->getDecl()]; 1229 Data.Attributes = A; 1230 Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate); 1231 Data.PrivateCopy = nullptr; 1232 } 1233 } 1234 } 1235 1236 /// Build a variable declaration for OpenMP loop iteration variable. 1237 static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type, 1238 StringRef Name, const AttrVec *Attrs = nullptr, 1239 DeclRefExpr *OrigRef = nullptr) { 1240 DeclContext *DC = SemaRef.CurContext; 1241 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name); 1242 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc); 1243 auto *Decl = 1244 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None); 1245 if (Attrs) { 1246 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end()); 1247 I != E; ++I) 1248 Decl->addAttr(*I); 1249 } 1250 Decl->setImplicit(); 1251 if (OrigRef) { 1252 Decl->addAttr( 1253 OMPReferencedVarAttr::CreateImplicit(SemaRef.Context, OrigRef)); 1254 } 1255 return Decl; 1256 } 1257 1258 static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty, 1259 SourceLocation Loc, 1260 bool RefersToCapture = false) { 1261 D->setReferenced(); 1262 D->markUsed(S.Context); 1263 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(), 1264 SourceLocation(), D, RefersToCapture, Loc, Ty, 1265 VK_LValue); 1266 } 1267 1268 void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 1269 BinaryOperatorKind BOK) { 1270 D = getCanonicalDecl(D); 1271 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1272 assert( 1273 getTopOfStack().SharingMap[D].Attributes == OMPC_reduction && 1274 "Additional reduction info may be specified only for reduction items."); 1275 ReductionData &ReductionData = getTopOfStack().ReductionMap[D]; 1276 assert(ReductionData.ReductionRange.isInvalid() && 1277 getTopOfStack().Directive == OMPD_taskgroup && 1278 "Additional reduction info may be specified only once for reduction " 1279 "items."); 1280 ReductionData.set(BOK, SR); 1281 Expr *&TaskgroupReductionRef = 1282 getTopOfStack().TaskgroupReductionRef; 1283 if (!TaskgroupReductionRef) { 1284 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(), 1285 SemaRef.Context.VoidPtrTy, ".task_red."); 1286 TaskgroupReductionRef = 1287 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin()); 1288 } 1289 } 1290 1291 void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 1292 const Expr *ReductionRef) { 1293 D = getCanonicalDecl(D); 1294 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1295 assert( 1296 getTopOfStack().SharingMap[D].Attributes == OMPC_reduction && 1297 "Additional reduction info may be specified only for reduction items."); 1298 ReductionData &ReductionData = getTopOfStack().ReductionMap[D]; 1299 assert(ReductionData.ReductionRange.isInvalid() && 1300 getTopOfStack().Directive == OMPD_taskgroup && 1301 "Additional reduction info may be specified only once for reduction " 1302 "items."); 1303 ReductionData.set(ReductionRef, SR); 1304 Expr *&TaskgroupReductionRef = 1305 getTopOfStack().TaskgroupReductionRef; 1306 if (!TaskgroupReductionRef) { 1307 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(), 1308 SemaRef.Context.VoidPtrTy, ".task_red."); 1309 TaskgroupReductionRef = 1310 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin()); 1311 } 1312 } 1313 1314 const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData( 1315 const ValueDecl *D, SourceRange &SR, BinaryOperatorKind &BOK, 1316 Expr *&TaskgroupDescriptor) const { 1317 D = getCanonicalDecl(D); 1318 assert(!isStackEmpty() && "Data-sharing attributes stack is empty."); 1319 for (const_iterator I = begin() + 1, E = end(); I != E; ++I) { 1320 const DSAInfo &Data = I->SharingMap.lookup(D); 1321 if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup) 1322 continue; 1323 const ReductionData &ReductionData = I->ReductionMap.lookup(D); 1324 if (!ReductionData.ReductionOp || 1325 ReductionData.ReductionOp.is<const Expr *>()) 1326 return DSAVarData(); 1327 SR = ReductionData.ReductionRange; 1328 BOK = ReductionData.ReductionOp.get<ReductionData::BOKPtrType>(); 1329 assert(I->TaskgroupReductionRef && "taskgroup reduction reference " 1330 "expression for the descriptor is not " 1331 "set."); 1332 TaskgroupDescriptor = I->TaskgroupReductionRef; 1333 return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(), 1334 Data.PrivateCopy, I->DefaultAttrLoc); 1335 } 1336 return DSAVarData(); 1337 } 1338 1339 const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData( 1340 const ValueDecl *D, SourceRange &SR, const Expr *&ReductionRef, 1341 Expr *&TaskgroupDescriptor) const { 1342 D = getCanonicalDecl(D); 1343 assert(!isStackEmpty() && "Data-sharing attributes stack is empty."); 1344 for (const_iterator I = begin() + 1, E = end(); I != E; ++I) { 1345 const DSAInfo &Data = I->SharingMap.lookup(D); 1346 if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup) 1347 continue; 1348 const ReductionData &ReductionData = I->ReductionMap.lookup(D); 1349 if (!ReductionData.ReductionOp || 1350 !ReductionData.ReductionOp.is<const Expr *>()) 1351 return DSAVarData(); 1352 SR = ReductionData.ReductionRange; 1353 ReductionRef = ReductionData.ReductionOp.get<const Expr *>(); 1354 assert(I->TaskgroupReductionRef && "taskgroup reduction reference " 1355 "expression for the descriptor is not " 1356 "set."); 1357 TaskgroupDescriptor = I->TaskgroupReductionRef; 1358 return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(), 1359 Data.PrivateCopy, I->DefaultAttrLoc); 1360 } 1361 return DSAVarData(); 1362 } 1363 1364 bool DSAStackTy::isOpenMPLocal(VarDecl *D, const_iterator I) const { 1365 D = D->getCanonicalDecl(); 1366 for (const_iterator E = end(); I != E; ++I) { 1367 if (isImplicitOrExplicitTaskingRegion(I->Directive) || 1368 isOpenMPTargetExecutionDirective(I->Directive)) { 1369 Scope *TopScope = I->CurScope ? I->CurScope->getParent() : nullptr; 1370 Scope *CurScope = getCurScope(); 1371 while (CurScope && CurScope != TopScope && !CurScope->isDeclScope(D)) 1372 CurScope = CurScope->getParent(); 1373 return CurScope != TopScope; 1374 } 1375 } 1376 return false; 1377 } 1378 1379 static bool isConstNotMutableType(Sema &SemaRef, QualType Type, 1380 bool AcceptIfMutable = true, 1381 bool *IsClassType = nullptr) { 1382 ASTContext &Context = SemaRef.getASTContext(); 1383 Type = Type.getNonReferenceType().getCanonicalType(); 1384 bool IsConstant = Type.isConstant(Context); 1385 Type = Context.getBaseElementType(Type); 1386 const CXXRecordDecl *RD = AcceptIfMutable && SemaRef.getLangOpts().CPlusPlus 1387 ? Type->getAsCXXRecordDecl() 1388 : nullptr; 1389 if (const auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD)) 1390 if (const ClassTemplateDecl *CTD = CTSD->getSpecializedTemplate()) 1391 RD = CTD->getTemplatedDecl(); 1392 if (IsClassType) 1393 *IsClassType = RD; 1394 return IsConstant && !(SemaRef.getLangOpts().CPlusPlus && RD && 1395 RD->hasDefinition() && RD->hasMutableFields()); 1396 } 1397 1398 static bool rejectConstNotMutableType(Sema &SemaRef, const ValueDecl *D, 1399 QualType Type, OpenMPClauseKind CKind, 1400 SourceLocation ELoc, 1401 bool AcceptIfMutable = true, 1402 bool ListItemNotVar = false) { 1403 ASTContext &Context = SemaRef.getASTContext(); 1404 bool IsClassType; 1405 if (isConstNotMutableType(SemaRef, Type, AcceptIfMutable, &IsClassType)) { 1406 unsigned Diag = ListItemNotVar 1407 ? diag::err_omp_const_list_item 1408 : IsClassType ? diag::err_omp_const_not_mutable_variable 1409 : diag::err_omp_const_variable; 1410 SemaRef.Diag(ELoc, Diag) << getOpenMPClauseName(CKind); 1411 if (!ListItemNotVar && D) { 1412 const VarDecl *VD = dyn_cast<VarDecl>(D); 1413 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 1414 VarDecl::DeclarationOnly; 1415 SemaRef.Diag(D->getLocation(), 1416 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1417 << D; 1418 } 1419 return true; 1420 } 1421 return false; 1422 } 1423 1424 const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, 1425 bool FromParent) { 1426 D = getCanonicalDecl(D); 1427 DSAVarData DVar; 1428 1429 auto *VD = dyn_cast<VarDecl>(D); 1430 auto TI = Threadprivates.find(D); 1431 if (TI != Threadprivates.end()) { 1432 DVar.RefExpr = TI->getSecond().RefExpr.getPointer(); 1433 DVar.CKind = OMPC_threadprivate; 1434 return DVar; 1435 } 1436 if (VD && VD->hasAttr<OMPThreadPrivateDeclAttr>()) { 1437 DVar.RefExpr = buildDeclRefExpr( 1438 SemaRef, VD, D->getType().getNonReferenceType(), 1439 VD->getAttr<OMPThreadPrivateDeclAttr>()->getLocation()); 1440 DVar.CKind = OMPC_threadprivate; 1441 addDSA(D, DVar.RefExpr, OMPC_threadprivate); 1442 return DVar; 1443 } 1444 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1445 // in a Construct, C/C++, predetermined, p.1] 1446 // Variables appearing in threadprivate directives are threadprivate. 1447 if ((VD && VD->getTLSKind() != VarDecl::TLS_None && 1448 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() && 1449 SemaRef.getLangOpts().OpenMPUseTLS && 1450 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) || 1451 (VD && VD->getStorageClass() == SC_Register && 1452 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) { 1453 DVar.RefExpr = buildDeclRefExpr( 1454 SemaRef, VD, D->getType().getNonReferenceType(), D->getLocation()); 1455 DVar.CKind = OMPC_threadprivate; 1456 addDSA(D, DVar.RefExpr, OMPC_threadprivate); 1457 return DVar; 1458 } 1459 if (SemaRef.getLangOpts().OpenMPCUDAMode && VD && 1460 VD->isLocalVarDeclOrParm() && !isStackEmpty() && 1461 !isLoopControlVariable(D).first) { 1462 const_iterator IterTarget = 1463 std::find_if(begin(), end(), [](const SharingMapTy &Data) { 1464 return isOpenMPTargetExecutionDirective(Data.Directive); 1465 }); 1466 if (IterTarget != end()) { 1467 const_iterator ParentIterTarget = IterTarget + 1; 1468 for (const_iterator Iter = begin(); 1469 Iter != ParentIterTarget; ++Iter) { 1470 if (isOpenMPLocal(VD, Iter)) { 1471 DVar.RefExpr = 1472 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(), 1473 D->getLocation()); 1474 DVar.CKind = OMPC_threadprivate; 1475 return DVar; 1476 } 1477 } 1478 if (!isClauseParsingMode() || IterTarget != begin()) { 1479 auto DSAIter = IterTarget->SharingMap.find(D); 1480 if (DSAIter != IterTarget->SharingMap.end() && 1481 isOpenMPPrivate(DSAIter->getSecond().Attributes)) { 1482 DVar.RefExpr = DSAIter->getSecond().RefExpr.getPointer(); 1483 DVar.CKind = OMPC_threadprivate; 1484 return DVar; 1485 } 1486 const_iterator End = end(); 1487 if (!SemaRef.isOpenMPCapturedByRef( 1488 D, std::distance(ParentIterTarget, End), 1489 /*OpenMPCaptureLevel=*/0)) { 1490 DVar.RefExpr = 1491 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(), 1492 IterTarget->ConstructLoc); 1493 DVar.CKind = OMPC_threadprivate; 1494 return DVar; 1495 } 1496 } 1497 } 1498 } 1499 1500 if (isStackEmpty()) 1501 // Not in OpenMP execution region and top scope was already checked. 1502 return DVar; 1503 1504 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1505 // in a Construct, C/C++, predetermined, p.4] 1506 // Static data members are shared. 1507 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1508 // in a Construct, C/C++, predetermined, p.7] 1509 // Variables with static storage duration that are declared in a scope 1510 // inside the construct are shared. 1511 if (VD && VD->isStaticDataMember()) { 1512 // Check for explicitly specified attributes. 1513 const_iterator I = begin(); 1514 const_iterator EndI = end(); 1515 if (FromParent && I != EndI) 1516 ++I; 1517 auto It = I->SharingMap.find(D); 1518 if (It != I->SharingMap.end()) { 1519 const DSAInfo &Data = It->getSecond(); 1520 DVar.RefExpr = Data.RefExpr.getPointer(); 1521 DVar.PrivateCopy = Data.PrivateCopy; 1522 DVar.CKind = Data.Attributes; 1523 DVar.ImplicitDSALoc = I->DefaultAttrLoc; 1524 DVar.DKind = I->Directive; 1525 return DVar; 1526 } 1527 1528 DVar.CKind = OMPC_shared; 1529 return DVar; 1530 } 1531 1532 auto &&MatchesAlways = [](OpenMPDirectiveKind) { return true; }; 1533 // The predetermined shared attribute for const-qualified types having no 1534 // mutable members was removed after OpenMP 3.1. 1535 if (SemaRef.LangOpts.OpenMP <= 31) { 1536 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1537 // in a Construct, C/C++, predetermined, p.6] 1538 // Variables with const qualified type having no mutable member are 1539 // shared. 1540 if (isConstNotMutableType(SemaRef, D->getType())) { 1541 // Variables with const-qualified type having no mutable member may be 1542 // listed in a firstprivate clause, even if they are static data members. 1543 DSAVarData DVarTemp = hasInnermostDSA( 1544 D, 1545 [](OpenMPClauseKind C) { 1546 return C == OMPC_firstprivate || C == OMPC_shared; 1547 }, 1548 MatchesAlways, FromParent); 1549 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr) 1550 return DVarTemp; 1551 1552 DVar.CKind = OMPC_shared; 1553 return DVar; 1554 } 1555 } 1556 1557 // Explicitly specified attributes and local variables with predetermined 1558 // attributes. 1559 const_iterator I = begin(); 1560 const_iterator EndI = end(); 1561 if (FromParent && I != EndI) 1562 ++I; 1563 auto It = I->SharingMap.find(D); 1564 if (It != I->SharingMap.end()) { 1565 const DSAInfo &Data = It->getSecond(); 1566 DVar.RefExpr = Data.RefExpr.getPointer(); 1567 DVar.PrivateCopy = Data.PrivateCopy; 1568 DVar.CKind = Data.Attributes; 1569 DVar.ImplicitDSALoc = I->DefaultAttrLoc; 1570 DVar.DKind = I->Directive; 1571 } 1572 1573 return DVar; 1574 } 1575 1576 const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D, 1577 bool FromParent) const { 1578 if (isStackEmpty()) { 1579 const_iterator I; 1580 return getDSA(I, D); 1581 } 1582 D = getCanonicalDecl(D); 1583 const_iterator StartI = begin(); 1584 const_iterator EndI = end(); 1585 if (FromParent && StartI != EndI) 1586 ++StartI; 1587 return getDSA(StartI, D); 1588 } 1589 1590 const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D, 1591 unsigned Level) const { 1592 if (getStackSize() <= Level) 1593 return DSAVarData(); 1594 D = getCanonicalDecl(D); 1595 const_iterator StartI = std::next(begin(), getStackSize() - 1 - Level); 1596 return getDSA(StartI, D); 1597 } 1598 1599 const DSAStackTy::DSAVarData 1600 DSAStackTy::hasDSA(ValueDecl *D, 1601 const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 1602 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1603 bool FromParent) const { 1604 if (isStackEmpty()) 1605 return {}; 1606 D = getCanonicalDecl(D); 1607 const_iterator I = begin(); 1608 const_iterator EndI = end(); 1609 if (FromParent && I != EndI) 1610 ++I; 1611 for (; I != EndI; ++I) { 1612 if (!DPred(I->Directive) && 1613 !isImplicitOrExplicitTaskingRegion(I->Directive)) 1614 continue; 1615 const_iterator NewI = I; 1616 DSAVarData DVar = getDSA(NewI, D); 1617 if (I == NewI && CPred(DVar.CKind)) 1618 return DVar; 1619 } 1620 return {}; 1621 } 1622 1623 const DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA( 1624 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 1625 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1626 bool FromParent) const { 1627 if (isStackEmpty()) 1628 return {}; 1629 D = getCanonicalDecl(D); 1630 const_iterator StartI = begin(); 1631 const_iterator EndI = end(); 1632 if (FromParent && StartI != EndI) 1633 ++StartI; 1634 if (StartI == EndI || !DPred(StartI->Directive)) 1635 return {}; 1636 const_iterator NewI = StartI; 1637 DSAVarData DVar = getDSA(NewI, D); 1638 return (NewI == StartI && CPred(DVar.CKind)) ? DVar : DSAVarData(); 1639 } 1640 1641 bool DSAStackTy::hasExplicitDSA( 1642 const ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 1643 unsigned Level, bool NotLastprivate) const { 1644 if (getStackSize() <= Level) 1645 return false; 1646 D = getCanonicalDecl(D); 1647 const SharingMapTy &StackElem = getStackElemAtLevel(Level); 1648 auto I = StackElem.SharingMap.find(D); 1649 if (I != StackElem.SharingMap.end() && 1650 I->getSecond().RefExpr.getPointer() && 1651 CPred(I->getSecond().Attributes) && 1652 (!NotLastprivate || !I->getSecond().RefExpr.getInt())) 1653 return true; 1654 // Check predetermined rules for the loop control variables. 1655 auto LI = StackElem.LCVMap.find(D); 1656 if (LI != StackElem.LCVMap.end()) 1657 return CPred(OMPC_private); 1658 return false; 1659 } 1660 1661 bool DSAStackTy::hasExplicitDirective( 1662 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1663 unsigned Level) const { 1664 if (getStackSize() <= Level) 1665 return false; 1666 const SharingMapTy &StackElem = getStackElemAtLevel(Level); 1667 return DPred(StackElem.Directive); 1668 } 1669 1670 bool DSAStackTy::hasDirective( 1671 const llvm::function_ref<bool(OpenMPDirectiveKind, 1672 const DeclarationNameInfo &, SourceLocation)> 1673 DPred, 1674 bool FromParent) const { 1675 // We look only in the enclosing region. 1676 size_t Skip = FromParent ? 2 : 1; 1677 for (const_iterator I = begin() + std::min(Skip, getStackSize()), E = end(); 1678 I != E; ++I) { 1679 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc)) 1680 return true; 1681 } 1682 return false; 1683 } 1684 1685 void Sema::InitDataSharingAttributesStack() { 1686 VarDataSharingAttributesStack = new DSAStackTy(*this); 1687 } 1688 1689 #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack) 1690 1691 void Sema::pushOpenMPFunctionRegion() { 1692 DSAStack->pushFunction(); 1693 } 1694 1695 void Sema::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) { 1696 DSAStack->popFunction(OldFSI); 1697 } 1698 1699 static bool isOpenMPDeviceDelayedContext(Sema &S) { 1700 assert(S.LangOpts.OpenMP && S.LangOpts.OpenMPIsDevice && 1701 "Expected OpenMP device compilation."); 1702 return !S.isInOpenMPTargetExecutionDirective() && 1703 !S.isInOpenMPDeclareTargetContext(); 1704 } 1705 1706 namespace { 1707 /// Status of the function emission on the host/device. 1708 enum class FunctionEmissionStatus { 1709 Emitted, 1710 Discarded, 1711 Unknown, 1712 }; 1713 } // anonymous namespace 1714 1715 Sema::DeviceDiagBuilder Sema::diagIfOpenMPDeviceCode(SourceLocation Loc, 1716 unsigned DiagID) { 1717 assert(LangOpts.OpenMP && LangOpts.OpenMPIsDevice && 1718 "Expected OpenMP device compilation."); 1719 FunctionEmissionStatus FES = getEmissionStatus(getCurFunctionDecl()); 1720 DeviceDiagBuilder::Kind Kind = DeviceDiagBuilder::K_Nop; 1721 switch (FES) { 1722 case FunctionEmissionStatus::Emitted: 1723 Kind = DeviceDiagBuilder::K_Immediate; 1724 break; 1725 case FunctionEmissionStatus::Unknown: 1726 Kind = isOpenMPDeviceDelayedContext(*this) ? DeviceDiagBuilder::K_Deferred 1727 : DeviceDiagBuilder::K_Immediate; 1728 break; 1729 case FunctionEmissionStatus::TemplateDiscarded: 1730 case FunctionEmissionStatus::OMPDiscarded: 1731 Kind = DeviceDiagBuilder::K_Nop; 1732 break; 1733 case FunctionEmissionStatus::CUDADiscarded: 1734 llvm_unreachable("CUDADiscarded unexpected in OpenMP device compilation"); 1735 break; 1736 } 1737 1738 return DeviceDiagBuilder(Kind, Loc, DiagID, getCurFunctionDecl(), *this); 1739 } 1740 1741 Sema::DeviceDiagBuilder Sema::diagIfOpenMPHostCode(SourceLocation Loc, 1742 unsigned DiagID) { 1743 assert(LangOpts.OpenMP && !LangOpts.OpenMPIsDevice && 1744 "Expected OpenMP host compilation."); 1745 FunctionEmissionStatus FES = getEmissionStatus(getCurFunctionDecl()); 1746 DeviceDiagBuilder::Kind Kind = DeviceDiagBuilder::K_Nop; 1747 switch (FES) { 1748 case FunctionEmissionStatus::Emitted: 1749 Kind = DeviceDiagBuilder::K_Immediate; 1750 break; 1751 case FunctionEmissionStatus::Unknown: 1752 Kind = DeviceDiagBuilder::K_Deferred; 1753 break; 1754 case FunctionEmissionStatus::TemplateDiscarded: 1755 case FunctionEmissionStatus::OMPDiscarded: 1756 case FunctionEmissionStatus::CUDADiscarded: 1757 Kind = DeviceDiagBuilder::K_Nop; 1758 break; 1759 } 1760 1761 return DeviceDiagBuilder(Kind, Loc, DiagID, getCurFunctionDecl(), *this); 1762 } 1763 1764 void Sema::checkOpenMPDeviceFunction(SourceLocation Loc, FunctionDecl *Callee, 1765 bool CheckForDelayedContext) { 1766 assert(LangOpts.OpenMP && LangOpts.OpenMPIsDevice && 1767 "Expected OpenMP device compilation."); 1768 assert(Callee && "Callee may not be null."); 1769 Callee = Callee->getMostRecentDecl(); 1770 FunctionDecl *Caller = getCurFunctionDecl(); 1771 1772 // host only function are not available on the device. 1773 if (Caller) { 1774 FunctionEmissionStatus CallerS = getEmissionStatus(Caller); 1775 FunctionEmissionStatus CalleeS = getEmissionStatus(Callee); 1776 assert(CallerS != FunctionEmissionStatus::CUDADiscarded && 1777 CalleeS != FunctionEmissionStatus::CUDADiscarded && 1778 "CUDADiscarded unexpected in OpenMP device function check"); 1779 if ((CallerS == FunctionEmissionStatus::Emitted || 1780 (!isOpenMPDeviceDelayedContext(*this) && 1781 CallerS == FunctionEmissionStatus::Unknown)) && 1782 CalleeS == FunctionEmissionStatus::OMPDiscarded) { 1783 StringRef HostDevTy = getOpenMPSimpleClauseTypeName( 1784 OMPC_device_type, OMPC_DEVICE_TYPE_host); 1785 Diag(Loc, diag::err_omp_wrong_device_function_call) << HostDevTy << 0; 1786 Diag(Callee->getAttr<OMPDeclareTargetDeclAttr>()->getLocation(), 1787 diag::note_omp_marked_device_type_here) 1788 << HostDevTy; 1789 return; 1790 } 1791 } 1792 // If the caller is known-emitted, mark the callee as known-emitted. 1793 // Otherwise, mark the call in our call graph so we can traverse it later. 1794 if ((CheckForDelayedContext && !isOpenMPDeviceDelayedContext(*this)) || 1795 (!Caller && !CheckForDelayedContext) || 1796 (Caller && getEmissionStatus(Caller) == FunctionEmissionStatus::Emitted)) 1797 markKnownEmitted(*this, Caller, Callee, Loc, 1798 [CheckForDelayedContext](Sema &S, FunctionDecl *FD) { 1799 return CheckForDelayedContext && 1800 S.getEmissionStatus(FD) == 1801 FunctionEmissionStatus::Emitted; 1802 }); 1803 else if (Caller) 1804 DeviceCallGraph[Caller].insert({Callee, Loc}); 1805 } 1806 1807 void Sema::checkOpenMPHostFunction(SourceLocation Loc, FunctionDecl *Callee, 1808 bool CheckCaller) { 1809 assert(LangOpts.OpenMP && !LangOpts.OpenMPIsDevice && 1810 "Expected OpenMP host compilation."); 1811 assert(Callee && "Callee may not be null."); 1812 Callee = Callee->getMostRecentDecl(); 1813 FunctionDecl *Caller = getCurFunctionDecl(); 1814 1815 // device only function are not available on the host. 1816 if (Caller) { 1817 FunctionEmissionStatus CallerS = getEmissionStatus(Caller); 1818 FunctionEmissionStatus CalleeS = getEmissionStatus(Callee); 1819 assert( 1820 (LangOpts.CUDA || (CallerS != FunctionEmissionStatus::CUDADiscarded && 1821 CalleeS != FunctionEmissionStatus::CUDADiscarded)) && 1822 "CUDADiscarded unexpected in OpenMP host function check"); 1823 if (CallerS == FunctionEmissionStatus::Emitted && 1824 CalleeS == FunctionEmissionStatus::OMPDiscarded) { 1825 StringRef NoHostDevTy = getOpenMPSimpleClauseTypeName( 1826 OMPC_device_type, OMPC_DEVICE_TYPE_nohost); 1827 Diag(Loc, diag::err_omp_wrong_device_function_call) << NoHostDevTy << 1; 1828 Diag(Callee->getAttr<OMPDeclareTargetDeclAttr>()->getLocation(), 1829 diag::note_omp_marked_device_type_here) 1830 << NoHostDevTy; 1831 return; 1832 } 1833 } 1834 // If the caller is known-emitted, mark the callee as known-emitted. 1835 // Otherwise, mark the call in our call graph so we can traverse it later. 1836 if (!shouldIgnoreInHostDeviceCheck(Callee)) { 1837 if ((!CheckCaller && !Caller) || 1838 (Caller && 1839 getEmissionStatus(Caller) == FunctionEmissionStatus::Emitted)) 1840 markKnownEmitted( 1841 *this, Caller, Callee, Loc, [CheckCaller](Sema &S, FunctionDecl *FD) { 1842 return CheckCaller && 1843 S.getEmissionStatus(FD) == FunctionEmissionStatus::Emitted; 1844 }); 1845 else if (Caller) 1846 DeviceCallGraph[Caller].insert({Callee, Loc}); 1847 } 1848 } 1849 1850 void Sema::checkOpenMPDeviceExpr(const Expr *E) { 1851 assert(getLangOpts().OpenMP && getLangOpts().OpenMPIsDevice && 1852 "OpenMP device compilation mode is expected."); 1853 QualType Ty = E->getType(); 1854 if ((Ty->isFloat16Type() && !Context.getTargetInfo().hasFloat16Type()) || 1855 ((Ty->isFloat128Type() || 1856 (Ty->isRealFloatingType() && Context.getTypeSize(Ty) == 128)) && 1857 !Context.getTargetInfo().hasFloat128Type()) || 1858 (Ty->isIntegerType() && Context.getTypeSize(Ty) == 128 && 1859 !Context.getTargetInfo().hasInt128Type())) 1860 targetDiag(E->getExprLoc(), diag::err_omp_unsupported_type) 1861 << static_cast<unsigned>(Context.getTypeSize(Ty)) << Ty 1862 << Context.getTargetInfo().getTriple().str() << E->getSourceRange(); 1863 } 1864 1865 static OpenMPDefaultmapClauseKind 1866 getVariableCategoryFromDecl(const LangOptions &LO, const ValueDecl *VD) { 1867 if (LO.OpenMP <= 45) { 1868 if (VD->getType().getNonReferenceType()->isScalarType()) 1869 return OMPC_DEFAULTMAP_scalar; 1870 return OMPC_DEFAULTMAP_aggregate; 1871 } 1872 if (VD->getType().getNonReferenceType()->isAnyPointerType()) 1873 return OMPC_DEFAULTMAP_pointer; 1874 if (VD->getType().getNonReferenceType()->isScalarType()) 1875 return OMPC_DEFAULTMAP_scalar; 1876 return OMPC_DEFAULTMAP_aggregate; 1877 } 1878 1879 bool Sema::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level, 1880 unsigned OpenMPCaptureLevel) const { 1881 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1882 1883 ASTContext &Ctx = getASTContext(); 1884 bool IsByRef = true; 1885 1886 // Find the directive that is associated with the provided scope. 1887 D = cast<ValueDecl>(D->getCanonicalDecl()); 1888 QualType Ty = D->getType(); 1889 1890 bool IsVariableUsedInMapClause = false; 1891 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) { 1892 // This table summarizes how a given variable should be passed to the device 1893 // given its type and the clauses where it appears. This table is based on 1894 // the description in OpenMP 4.5 [2.10.4, target Construct] and 1895 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses]. 1896 // 1897 // ========================================================================= 1898 // | type | defaultmap | pvt | first | is_device_ptr | map | res. | 1899 // | |(tofrom:scalar)| | pvt | | | | 1900 // ========================================================================= 1901 // | scl | | | | - | | bycopy| 1902 // | scl | | - | x | - | - | bycopy| 1903 // | scl | | x | - | - | - | null | 1904 // | scl | x | | | - | | byref | 1905 // | scl | x | - | x | - | - | bycopy| 1906 // | scl | x | x | - | - | - | null | 1907 // | scl | | - | - | - | x | byref | 1908 // | scl | x | - | - | - | x | byref | 1909 // 1910 // | agg | n.a. | | | - | | byref | 1911 // | agg | n.a. | - | x | - | - | byref | 1912 // | agg | n.a. | x | - | - | - | null | 1913 // | agg | n.a. | - | - | - | x | byref | 1914 // | agg | n.a. | - | - | - | x[] | byref | 1915 // 1916 // | ptr | n.a. | | | - | | bycopy| 1917 // | ptr | n.a. | - | x | - | - | bycopy| 1918 // | ptr | n.a. | x | - | - | - | null | 1919 // | ptr | n.a. | - | - | - | x | byref | 1920 // | ptr | n.a. | - | - | - | x[] | bycopy| 1921 // | ptr | n.a. | - | - | x | | bycopy| 1922 // | ptr | n.a. | - | - | x | x | bycopy| 1923 // | ptr | n.a. | - | - | x | x[] | bycopy| 1924 // ========================================================================= 1925 // Legend: 1926 // scl - scalar 1927 // ptr - pointer 1928 // agg - aggregate 1929 // x - applies 1930 // - - invalid in this combination 1931 // [] - mapped with an array section 1932 // byref - should be mapped by reference 1933 // byval - should be mapped by value 1934 // null - initialize a local variable to null on the device 1935 // 1936 // Observations: 1937 // - All scalar declarations that show up in a map clause have to be passed 1938 // by reference, because they may have been mapped in the enclosing data 1939 // environment. 1940 // - If the scalar value does not fit the size of uintptr, it has to be 1941 // passed by reference, regardless the result in the table above. 1942 // - For pointers mapped by value that have either an implicit map or an 1943 // array section, the runtime library may pass the NULL value to the 1944 // device instead of the value passed to it by the compiler. 1945 1946 if (Ty->isReferenceType()) 1947 Ty = Ty->castAs<ReferenceType>()->getPointeeType(); 1948 1949 // Locate map clauses and see if the variable being captured is referred to 1950 // in any of those clauses. Here we only care about variables, not fields, 1951 // because fields are part of aggregates. 1952 bool IsVariableAssociatedWithSection = false; 1953 1954 DSAStack->checkMappableExprComponentListsForDeclAtLevel( 1955 D, Level, 1956 [&IsVariableUsedInMapClause, &IsVariableAssociatedWithSection, D]( 1957 OMPClauseMappableExprCommon::MappableExprComponentListRef 1958 MapExprComponents, 1959 OpenMPClauseKind WhereFoundClauseKind) { 1960 // Only the map clause information influences how a variable is 1961 // captured. E.g. is_device_ptr does not require changing the default 1962 // behavior. 1963 if (WhereFoundClauseKind != OMPC_map) 1964 return false; 1965 1966 auto EI = MapExprComponents.rbegin(); 1967 auto EE = MapExprComponents.rend(); 1968 1969 assert(EI != EE && "Invalid map expression!"); 1970 1971 if (isa<DeclRefExpr>(EI->getAssociatedExpression())) 1972 IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D; 1973 1974 ++EI; 1975 if (EI == EE) 1976 return false; 1977 1978 if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) || 1979 isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) || 1980 isa<MemberExpr>(EI->getAssociatedExpression())) { 1981 IsVariableAssociatedWithSection = true; 1982 // There is nothing more we need to know about this variable. 1983 return true; 1984 } 1985 1986 // Keep looking for more map info. 1987 return false; 1988 }); 1989 1990 if (IsVariableUsedInMapClause) { 1991 // If variable is identified in a map clause it is always captured by 1992 // reference except if it is a pointer that is dereferenced somehow. 1993 IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection); 1994 } else { 1995 // By default, all the data that has a scalar type is mapped by copy 1996 // (except for reduction variables). 1997 // Defaultmap scalar is mutual exclusive to defaultmap pointer 1998 IsByRef = 1999 (DSAStack->isForceCaptureByReferenceInTargetExecutable() && 2000 !Ty->isAnyPointerType()) || 2001 !Ty->isScalarType() || 2002 DSAStack->isDefaultmapCapturedByRef( 2003 Level, getVariableCategoryFromDecl(LangOpts, D)) || 2004 DSAStack->hasExplicitDSA( 2005 D, [](OpenMPClauseKind K) { return K == OMPC_reduction; }, Level); 2006 } 2007 } 2008 2009 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) { 2010 IsByRef = 2011 ((IsVariableUsedInMapClause && 2012 DSAStack->getCaptureRegion(Level, OpenMPCaptureLevel) == 2013 OMPD_target) || 2014 !DSAStack->hasExplicitDSA( 2015 D, 2016 [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; }, 2017 Level, /*NotLastprivate=*/true)) && 2018 // If the variable is artificial and must be captured by value - try to 2019 // capture by value. 2020 !(isa<OMPCapturedExprDecl>(D) && !D->hasAttr<OMPCaptureNoInitAttr>() && 2021 !cast<OMPCapturedExprDecl>(D)->getInit()->isGLValue()); 2022 } 2023 2024 // When passing data by copy, we need to make sure it fits the uintptr size 2025 // and alignment, because the runtime library only deals with uintptr types. 2026 // If it does not fit the uintptr size, we need to pass the data by reference 2027 // instead. 2028 if (!IsByRef && 2029 (Ctx.getTypeSizeInChars(Ty) > 2030 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) || 2031 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) { 2032 IsByRef = true; 2033 } 2034 2035 return IsByRef; 2036 } 2037 2038 unsigned Sema::getOpenMPNestingLevel() const { 2039 assert(getLangOpts().OpenMP); 2040 return DSAStack->getNestingLevel(); 2041 } 2042 2043 bool Sema::isInOpenMPTargetExecutionDirective() const { 2044 return (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) && 2045 !DSAStack->isClauseParsingMode()) || 2046 DSAStack->hasDirective( 2047 [](OpenMPDirectiveKind K, const DeclarationNameInfo &, 2048 SourceLocation) -> bool { 2049 return isOpenMPTargetExecutionDirective(K); 2050 }, 2051 false); 2052 } 2053 2054 VarDecl *Sema::isOpenMPCapturedDecl(ValueDecl *D, bool CheckScopeInfo, 2055 unsigned StopAt) { 2056 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 2057 D = getCanonicalDecl(D); 2058 2059 auto *VD = dyn_cast<VarDecl>(D); 2060 // Do not capture constexpr variables. 2061 if (VD && VD->isConstexpr()) 2062 return nullptr; 2063 2064 // If we want to determine whether the variable should be captured from the 2065 // perspective of the current capturing scope, and we've already left all the 2066 // capturing scopes of the top directive on the stack, check from the 2067 // perspective of its parent directive (if any) instead. 2068 DSAStackTy::ParentDirectiveScope InParentDirectiveRAII( 2069 *DSAStack, CheckScopeInfo && DSAStack->isBodyComplete()); 2070 2071 // If we are attempting to capture a global variable in a directive with 2072 // 'target' we return true so that this global is also mapped to the device. 2073 // 2074 if (VD && !VD->hasLocalStorage() && 2075 (getCurCapturedRegion() || getCurBlock() || getCurLambda())) { 2076 if (isInOpenMPDeclareTargetContext()) { 2077 // Try to mark variable as declare target if it is used in capturing 2078 // regions. 2079 if (LangOpts.OpenMP <= 45 && 2080 !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) 2081 checkDeclIsAllowedInOpenMPTarget(nullptr, VD); 2082 return nullptr; 2083 } else if (isInOpenMPTargetExecutionDirective()) { 2084 // If the declaration is enclosed in a 'declare target' directive, 2085 // then it should not be captured. 2086 // 2087 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) 2088 return nullptr; 2089 CapturedRegionScopeInfo *CSI = nullptr; 2090 for (FunctionScopeInfo *FSI : llvm::drop_begin( 2091 llvm::reverse(FunctionScopes), 2092 CheckScopeInfo ? (FunctionScopes.size() - (StopAt + 1)) : 0)) { 2093 if (!isa<CapturingScopeInfo>(FSI)) 2094 return nullptr; 2095 if (auto *RSI = dyn_cast<CapturedRegionScopeInfo>(FSI)) 2096 if (RSI->CapRegionKind == CR_OpenMP) { 2097 CSI = RSI; 2098 break; 2099 } 2100 } 2101 SmallVector<OpenMPDirectiveKind, 4> Regions; 2102 getOpenMPCaptureRegions(Regions, 2103 DSAStack->getDirective(CSI->OpenMPLevel)); 2104 if (Regions[CSI->OpenMPCaptureLevel] != OMPD_task) 2105 return VD; 2106 } 2107 } 2108 2109 if (CheckScopeInfo) { 2110 bool OpenMPFound = false; 2111 for (unsigned I = StopAt + 1; I > 0; --I) { 2112 FunctionScopeInfo *FSI = FunctionScopes[I - 1]; 2113 if(!isa<CapturingScopeInfo>(FSI)) 2114 return nullptr; 2115 if (auto *RSI = dyn_cast<CapturedRegionScopeInfo>(FSI)) 2116 if (RSI->CapRegionKind == CR_OpenMP) { 2117 OpenMPFound = true; 2118 break; 2119 } 2120 } 2121 if (!OpenMPFound) 2122 return nullptr; 2123 } 2124 2125 if (DSAStack->getCurrentDirective() != OMPD_unknown && 2126 (!DSAStack->isClauseParsingMode() || 2127 DSAStack->getParentDirective() != OMPD_unknown)) { 2128 auto &&Info = DSAStack->isLoopControlVariable(D); 2129 if (Info.first || 2130 (VD && VD->hasLocalStorage() && 2131 isImplicitOrExplicitTaskingRegion(DSAStack->getCurrentDirective())) || 2132 (VD && DSAStack->isForceVarCapturing())) 2133 return VD ? VD : Info.second; 2134 DSAStackTy::DSAVarData DVarPrivate = 2135 DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode()); 2136 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind)) 2137 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl()); 2138 // Threadprivate variables must not be captured. 2139 if (isOpenMPThreadPrivate(DVarPrivate.CKind)) 2140 return nullptr; 2141 // The variable is not private or it is the variable in the directive with 2142 // default(none) clause and not used in any clause. 2143 DVarPrivate = DSAStack->hasDSA(D, isOpenMPPrivate, 2144 [](OpenMPDirectiveKind) { return true; }, 2145 DSAStack->isClauseParsingMode()); 2146 if (DVarPrivate.CKind != OMPC_unknown || 2147 (VD && DSAStack->getDefaultDSA() == DSA_none)) 2148 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl()); 2149 } 2150 return nullptr; 2151 } 2152 2153 void Sema::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex, 2154 unsigned Level) const { 2155 FunctionScopesIndex -= getOpenMPCaptureLevels(DSAStack->getDirective(Level)); 2156 } 2157 2158 void Sema::startOpenMPLoop() { 2159 assert(LangOpts.OpenMP && "OpenMP must be enabled."); 2160 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) 2161 DSAStack->loopInit(); 2162 } 2163 2164 void Sema::startOpenMPCXXRangeFor() { 2165 assert(LangOpts.OpenMP && "OpenMP must be enabled."); 2166 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { 2167 DSAStack->resetPossibleLoopCounter(); 2168 DSAStack->loopStart(); 2169 } 2170 } 2171 2172 OpenMPClauseKind Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level, 2173 unsigned CapLevel) const { 2174 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 2175 if (DSAStack->hasExplicitDirective( 2176 [](OpenMPDirectiveKind K) { return isOpenMPTaskingDirective(K); }, 2177 Level)) { 2178 bool IsTriviallyCopyable = 2179 D->getType().getNonReferenceType().isTriviallyCopyableType(Context); 2180 OpenMPDirectiveKind DKind = DSAStack->getDirective(Level); 2181 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 2182 getOpenMPCaptureRegions(CaptureRegions, DKind); 2183 if (isOpenMPTaskingDirective(CaptureRegions[CapLevel]) && 2184 (IsTriviallyCopyable || 2185 !isOpenMPTaskLoopDirective(CaptureRegions[CapLevel]))) { 2186 if (DSAStack->hasExplicitDSA( 2187 D, [](OpenMPClauseKind K) { return K == OMPC_firstprivate; }, 2188 Level, /*NotLastprivate=*/true)) 2189 return OMPC_firstprivate; 2190 DSAStackTy::DSAVarData DVar = DSAStack->getImplicitDSA(D, Level); 2191 if (DVar.CKind != OMPC_shared && 2192 !DSAStack->isLoopControlVariable(D, Level).first && !DVar.RefExpr) { 2193 DSAStack->addImplicitTaskFirstprivate(Level, D); 2194 return OMPC_firstprivate; 2195 } 2196 } 2197 } 2198 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { 2199 if (DSAStack->getAssociatedLoops() > 0 && 2200 !DSAStack->isLoopStarted()) { 2201 DSAStack->resetPossibleLoopCounter(D); 2202 DSAStack->loopStart(); 2203 return OMPC_private; 2204 } 2205 if ((DSAStack->getPossiblyLoopCunter() == D->getCanonicalDecl() || 2206 DSAStack->isLoopControlVariable(D).first) && 2207 !DSAStack->hasExplicitDSA( 2208 D, [](OpenMPClauseKind K) { return K != OMPC_private; }, Level) && 2209 !isOpenMPSimdDirective(DSAStack->getCurrentDirective())) 2210 return OMPC_private; 2211 } 2212 if (const auto *VD = dyn_cast<VarDecl>(D)) { 2213 if (DSAStack->isThreadPrivate(const_cast<VarDecl *>(VD)) && 2214 DSAStack->isForceVarCapturing() && 2215 !DSAStack->hasExplicitDSA( 2216 D, [](OpenMPClauseKind K) { return K == OMPC_copyin; }, Level)) 2217 return OMPC_private; 2218 } 2219 return (DSAStack->hasExplicitDSA( 2220 D, [](OpenMPClauseKind K) { return K == OMPC_private; }, Level) || 2221 (DSAStack->isClauseParsingMode() && 2222 DSAStack->getClauseParsingMode() == OMPC_private) || 2223 // Consider taskgroup reduction descriptor variable a private 2224 // to avoid possible capture in the region. 2225 (DSAStack->hasExplicitDirective( 2226 [](OpenMPDirectiveKind K) { return K == OMPD_taskgroup; }, 2227 Level) && 2228 DSAStack->isTaskgroupReductionRef(D, Level))) 2229 ? OMPC_private 2230 : OMPC_unknown; 2231 } 2232 2233 void Sema::setOpenMPCaptureKind(FieldDecl *FD, const ValueDecl *D, 2234 unsigned Level) { 2235 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 2236 D = getCanonicalDecl(D); 2237 OpenMPClauseKind OMPC = OMPC_unknown; 2238 for (unsigned I = DSAStack->getNestingLevel() + 1; I > Level; --I) { 2239 const unsigned NewLevel = I - 1; 2240 if (DSAStack->hasExplicitDSA(D, 2241 [&OMPC](const OpenMPClauseKind K) { 2242 if (isOpenMPPrivate(K)) { 2243 OMPC = K; 2244 return true; 2245 } 2246 return false; 2247 }, 2248 NewLevel)) 2249 break; 2250 if (DSAStack->checkMappableExprComponentListsForDeclAtLevel( 2251 D, NewLevel, 2252 [](OMPClauseMappableExprCommon::MappableExprComponentListRef, 2253 OpenMPClauseKind) { return true; })) { 2254 OMPC = OMPC_map; 2255 break; 2256 } 2257 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, 2258 NewLevel)) { 2259 OMPC = OMPC_map; 2260 if (DSAStack->mustBeFirstprivateAtLevel( 2261 NewLevel, getVariableCategoryFromDecl(LangOpts, D))) 2262 OMPC = OMPC_firstprivate; 2263 break; 2264 } 2265 } 2266 if (OMPC != OMPC_unknown) 2267 FD->addAttr(OMPCaptureKindAttr::CreateImplicit(Context, OMPC)); 2268 } 2269 2270 bool Sema::isOpenMPTargetCapturedDecl(const ValueDecl *D, unsigned Level, 2271 unsigned CaptureLevel) const { 2272 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 2273 // Return true if the current level is no longer enclosed in a target region. 2274 2275 SmallVector<OpenMPDirectiveKind, 4> Regions; 2276 getOpenMPCaptureRegions(Regions, DSAStack->getDirective(Level)); 2277 const auto *VD = dyn_cast<VarDecl>(D); 2278 return VD && !VD->hasLocalStorage() && 2279 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, 2280 Level) && 2281 Regions[CaptureLevel] != OMPD_task; 2282 } 2283 2284 bool Sema::isOpenMPGlobalCapturedDecl(ValueDecl *D, unsigned Level, 2285 unsigned CaptureLevel) const { 2286 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 2287 // Return true if the current level is no longer enclosed in a target region. 2288 2289 if (const auto *VD = dyn_cast<VarDecl>(D)) { 2290 if (!VD->hasLocalStorage()) { 2291 DSAStackTy::DSAVarData TopDVar = 2292 DSAStack->getTopDSA(D, /*FromParent=*/false); 2293 unsigned NumLevels = 2294 getOpenMPCaptureLevels(DSAStack->getDirective(Level)); 2295 if (Level == 0) 2296 return (NumLevels == CaptureLevel + 1) && TopDVar.CKind != OMPC_shared; 2297 DSAStackTy::DSAVarData DVar = DSAStack->getImplicitDSA(D, Level - 1); 2298 return DVar.CKind != OMPC_shared || 2299 isOpenMPGlobalCapturedDecl( 2300 D, Level - 1, 2301 getOpenMPCaptureLevels(DSAStack->getDirective(Level - 1)) - 1); 2302 } 2303 } 2304 return true; 2305 } 2306 2307 void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; } 2308 2309 void Sema::finalizeOpenMPDelayedAnalysis() { 2310 assert(LangOpts.OpenMP && "Expected OpenMP compilation mode."); 2311 // Diagnose implicit declare target functions and their callees. 2312 for (const auto &CallerCallees : DeviceCallGraph) { 2313 Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy = 2314 OMPDeclareTargetDeclAttr::getDeviceType( 2315 CallerCallees.getFirst()->getMostRecentDecl()); 2316 // Ignore host functions during device analyzis. 2317 if (LangOpts.OpenMPIsDevice && DevTy && 2318 *DevTy == OMPDeclareTargetDeclAttr::DT_Host) 2319 continue; 2320 // Ignore nohost functions during host analyzis. 2321 if (!LangOpts.OpenMPIsDevice && DevTy && 2322 *DevTy == OMPDeclareTargetDeclAttr::DT_NoHost) 2323 continue; 2324 for (const std::pair<CanonicalDeclPtr<FunctionDecl>, SourceLocation> 2325 &Callee : CallerCallees.getSecond()) { 2326 const FunctionDecl *FD = Callee.first->getMostRecentDecl(); 2327 Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy = 2328 OMPDeclareTargetDeclAttr::getDeviceType(FD); 2329 if (LangOpts.OpenMPIsDevice && DevTy && 2330 *DevTy == OMPDeclareTargetDeclAttr::DT_Host) { 2331 // Diagnose host function called during device codegen. 2332 StringRef HostDevTy = getOpenMPSimpleClauseTypeName( 2333 OMPC_device_type, OMPC_DEVICE_TYPE_host); 2334 Diag(Callee.second, diag::err_omp_wrong_device_function_call) 2335 << HostDevTy << 0; 2336 Diag(FD->getAttr<OMPDeclareTargetDeclAttr>()->getLocation(), 2337 diag::note_omp_marked_device_type_here) 2338 << HostDevTy; 2339 continue; 2340 } 2341 if (!LangOpts.OpenMPIsDevice && DevTy && 2342 *DevTy == OMPDeclareTargetDeclAttr::DT_NoHost) { 2343 // Diagnose nohost function called during host codegen. 2344 StringRef NoHostDevTy = getOpenMPSimpleClauseTypeName( 2345 OMPC_device_type, OMPC_DEVICE_TYPE_nohost); 2346 Diag(Callee.second, diag::err_omp_wrong_device_function_call) 2347 << NoHostDevTy << 1; 2348 Diag(FD->getAttr<OMPDeclareTargetDeclAttr>()->getLocation(), 2349 diag::note_omp_marked_device_type_here) 2350 << NoHostDevTy; 2351 continue; 2352 } 2353 } 2354 } 2355 } 2356 2357 void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind, 2358 const DeclarationNameInfo &DirName, 2359 Scope *CurScope, SourceLocation Loc) { 2360 DSAStack->push(DKind, DirName, CurScope, Loc); 2361 PushExpressionEvaluationContext( 2362 ExpressionEvaluationContext::PotentiallyEvaluated); 2363 } 2364 2365 void Sema::StartOpenMPClause(OpenMPClauseKind K) { 2366 DSAStack->setClauseParsingMode(K); 2367 } 2368 2369 void Sema::EndOpenMPClause() { 2370 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown); 2371 } 2372 2373 static void checkAllocateClauses(Sema &S, DSAStackTy *Stack, 2374 ArrayRef<OMPClause *> Clauses); 2375 static std::pair<ValueDecl *, bool> 2376 getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc, 2377 SourceRange &ERange, bool AllowArraySection = false); 2378 static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr, 2379 bool WithInit); 2380 2381 void Sema::EndOpenMPDSABlock(Stmt *CurDirective) { 2382 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1] 2383 // A variable of class type (or array thereof) that appears in a lastprivate 2384 // clause requires an accessible, unambiguous default constructor for the 2385 // class type, unless the list item is also specified in a firstprivate 2386 // clause. 2387 if (const auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) { 2388 for (OMPClause *C : D->clauses()) { 2389 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) { 2390 SmallVector<Expr *, 8> PrivateCopies; 2391 for (Expr *DE : Clause->varlists()) { 2392 if (DE->isValueDependent() || DE->isTypeDependent()) { 2393 PrivateCopies.push_back(nullptr); 2394 continue; 2395 } 2396 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens()); 2397 auto *VD = cast<VarDecl>(DRE->getDecl()); 2398 QualType Type = VD->getType().getNonReferenceType(); 2399 const DSAStackTy::DSAVarData DVar = 2400 DSAStack->getTopDSA(VD, /*FromParent=*/false); 2401 if (DVar.CKind == OMPC_lastprivate) { 2402 // Generate helper private variable and initialize it with the 2403 // default value. The address of the original variable is replaced 2404 // by the address of the new private variable in CodeGen. This new 2405 // variable is not added to IdResolver, so the code in the OpenMP 2406 // region uses original variable for proper diagnostics. 2407 VarDecl *VDPrivate = buildVarDecl( 2408 *this, DE->getExprLoc(), Type.getUnqualifiedType(), 2409 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr, DRE); 2410 ActOnUninitializedDecl(VDPrivate); 2411 if (VDPrivate->isInvalidDecl()) { 2412 PrivateCopies.push_back(nullptr); 2413 continue; 2414 } 2415 PrivateCopies.push_back(buildDeclRefExpr( 2416 *this, VDPrivate, DE->getType(), DE->getExprLoc())); 2417 } else { 2418 // The variable is also a firstprivate, so initialization sequence 2419 // for private copy is generated already. 2420 PrivateCopies.push_back(nullptr); 2421 } 2422 } 2423 Clause->setPrivateCopies(PrivateCopies); 2424 continue; 2425 } 2426 // Finalize nontemporal clause by handling private copies, if any. 2427 if (auto *Clause = dyn_cast<OMPNontemporalClause>(C)) { 2428 SmallVector<Expr *, 8> PrivateRefs; 2429 for (Expr *RefExpr : Clause->varlists()) { 2430 assert(RefExpr && "NULL expr in OpenMP nontemporal clause."); 2431 SourceLocation ELoc; 2432 SourceRange ERange; 2433 Expr *SimpleRefExpr = RefExpr; 2434 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 2435 if (Res.second) 2436 // It will be analyzed later. 2437 PrivateRefs.push_back(RefExpr); 2438 ValueDecl *D = Res.first; 2439 if (!D) 2440 continue; 2441 2442 const DSAStackTy::DSAVarData DVar = 2443 DSAStack->getTopDSA(D, /*FromParent=*/false); 2444 PrivateRefs.push_back(DVar.PrivateCopy ? DVar.PrivateCopy 2445 : SimpleRefExpr); 2446 } 2447 Clause->setPrivateRefs(PrivateRefs); 2448 continue; 2449 } 2450 } 2451 // Check allocate clauses. 2452 if (!CurContext->isDependentContext()) 2453 checkAllocateClauses(*this, DSAStack, D->clauses()); 2454 } 2455 2456 DSAStack->pop(); 2457 DiscardCleanupsInEvaluationContext(); 2458 PopExpressionEvaluationContext(); 2459 } 2460 2461 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, 2462 Expr *NumIterations, Sema &SemaRef, 2463 Scope *S, DSAStackTy *Stack); 2464 2465 namespace { 2466 2467 class VarDeclFilterCCC final : public CorrectionCandidateCallback { 2468 private: 2469 Sema &SemaRef; 2470 2471 public: 2472 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {} 2473 bool ValidateCandidate(const TypoCorrection &Candidate) override { 2474 NamedDecl *ND = Candidate.getCorrectionDecl(); 2475 if (const auto *VD = dyn_cast_or_null<VarDecl>(ND)) { 2476 return VD->hasGlobalStorage() && 2477 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), 2478 SemaRef.getCurScope()); 2479 } 2480 return false; 2481 } 2482 2483 std::unique_ptr<CorrectionCandidateCallback> clone() override { 2484 return std::make_unique<VarDeclFilterCCC>(*this); 2485 } 2486 2487 }; 2488 2489 class VarOrFuncDeclFilterCCC final : public CorrectionCandidateCallback { 2490 private: 2491 Sema &SemaRef; 2492 2493 public: 2494 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {} 2495 bool ValidateCandidate(const TypoCorrection &Candidate) override { 2496 NamedDecl *ND = Candidate.getCorrectionDecl(); 2497 if (ND && ((isa<VarDecl>(ND) && ND->getKind() == Decl::Var) || 2498 isa<FunctionDecl>(ND))) { 2499 return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), 2500 SemaRef.getCurScope()); 2501 } 2502 return false; 2503 } 2504 2505 std::unique_ptr<CorrectionCandidateCallback> clone() override { 2506 return std::make_unique<VarOrFuncDeclFilterCCC>(*this); 2507 } 2508 }; 2509 2510 } // namespace 2511 2512 ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope, 2513 CXXScopeSpec &ScopeSpec, 2514 const DeclarationNameInfo &Id, 2515 OpenMPDirectiveKind Kind) { 2516 LookupResult Lookup(*this, Id, LookupOrdinaryName); 2517 LookupParsedName(Lookup, CurScope, &ScopeSpec, true); 2518 2519 if (Lookup.isAmbiguous()) 2520 return ExprError(); 2521 2522 VarDecl *VD; 2523 if (!Lookup.isSingleResult()) { 2524 VarDeclFilterCCC CCC(*this); 2525 if (TypoCorrection Corrected = 2526 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, CCC, 2527 CTK_ErrorRecovery)) { 2528 diagnoseTypo(Corrected, 2529 PDiag(Lookup.empty() 2530 ? diag::err_undeclared_var_use_suggest 2531 : diag::err_omp_expected_var_arg_suggest) 2532 << Id.getName()); 2533 VD = Corrected.getCorrectionDeclAs<VarDecl>(); 2534 } else { 2535 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use 2536 : diag::err_omp_expected_var_arg) 2537 << Id.getName(); 2538 return ExprError(); 2539 } 2540 } else if (!(VD = Lookup.getAsSingle<VarDecl>())) { 2541 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName(); 2542 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at); 2543 return ExprError(); 2544 } 2545 Lookup.suppressDiagnostics(); 2546 2547 // OpenMP [2.9.2, Syntax, C/C++] 2548 // Variables must be file-scope, namespace-scope, or static block-scope. 2549 if (Kind == OMPD_threadprivate && !VD->hasGlobalStorage()) { 2550 Diag(Id.getLoc(), diag::err_omp_global_var_arg) 2551 << getOpenMPDirectiveName(Kind) << !VD->isStaticLocal(); 2552 bool IsDecl = 2553 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2554 Diag(VD->getLocation(), 2555 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2556 << VD; 2557 return ExprError(); 2558 } 2559 2560 VarDecl *CanonicalVD = VD->getCanonicalDecl(); 2561 NamedDecl *ND = CanonicalVD; 2562 // OpenMP [2.9.2, Restrictions, C/C++, p.2] 2563 // A threadprivate directive for file-scope variables must appear outside 2564 // any definition or declaration. 2565 if (CanonicalVD->getDeclContext()->isTranslationUnit() && 2566 !getCurLexicalContext()->isTranslationUnit()) { 2567 Diag(Id.getLoc(), diag::err_omp_var_scope) 2568 << getOpenMPDirectiveName(Kind) << VD; 2569 bool IsDecl = 2570 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2571 Diag(VD->getLocation(), 2572 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2573 << VD; 2574 return ExprError(); 2575 } 2576 // OpenMP [2.9.2, Restrictions, C/C++, p.3] 2577 // A threadprivate directive for static class member variables must appear 2578 // in the class definition, in the same scope in which the member 2579 // variables are declared. 2580 if (CanonicalVD->isStaticDataMember() && 2581 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) { 2582 Diag(Id.getLoc(), diag::err_omp_var_scope) 2583 << getOpenMPDirectiveName(Kind) << VD; 2584 bool IsDecl = 2585 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2586 Diag(VD->getLocation(), 2587 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2588 << VD; 2589 return ExprError(); 2590 } 2591 // OpenMP [2.9.2, Restrictions, C/C++, p.4] 2592 // A threadprivate directive for namespace-scope variables must appear 2593 // outside any definition or declaration other than the namespace 2594 // definition itself. 2595 if (CanonicalVD->getDeclContext()->isNamespace() && 2596 (!getCurLexicalContext()->isFileContext() || 2597 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) { 2598 Diag(Id.getLoc(), diag::err_omp_var_scope) 2599 << getOpenMPDirectiveName(Kind) << VD; 2600 bool IsDecl = 2601 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2602 Diag(VD->getLocation(), 2603 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2604 << VD; 2605 return ExprError(); 2606 } 2607 // OpenMP [2.9.2, Restrictions, C/C++, p.6] 2608 // A threadprivate directive for static block-scope variables must appear 2609 // in the scope of the variable and not in a nested scope. 2610 if (CanonicalVD->isLocalVarDecl() && CurScope && 2611 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) { 2612 Diag(Id.getLoc(), diag::err_omp_var_scope) 2613 << getOpenMPDirectiveName(Kind) << VD; 2614 bool IsDecl = 2615 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2616 Diag(VD->getLocation(), 2617 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2618 << VD; 2619 return ExprError(); 2620 } 2621 2622 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6] 2623 // A threadprivate directive must lexically precede all references to any 2624 // of the variables in its list. 2625 if (Kind == OMPD_threadprivate && VD->isUsed() && 2626 !DSAStack->isThreadPrivate(VD)) { 2627 Diag(Id.getLoc(), diag::err_omp_var_used) 2628 << getOpenMPDirectiveName(Kind) << VD; 2629 return ExprError(); 2630 } 2631 2632 QualType ExprType = VD->getType().getNonReferenceType(); 2633 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(), 2634 SourceLocation(), VD, 2635 /*RefersToEnclosingVariableOrCapture=*/false, 2636 Id.getLoc(), ExprType, VK_LValue); 2637 } 2638 2639 Sema::DeclGroupPtrTy 2640 Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc, 2641 ArrayRef<Expr *> VarList) { 2642 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) { 2643 CurContext->addDecl(D); 2644 return DeclGroupPtrTy::make(DeclGroupRef(D)); 2645 } 2646 return nullptr; 2647 } 2648 2649 namespace { 2650 class LocalVarRefChecker final 2651 : public ConstStmtVisitor<LocalVarRefChecker, bool> { 2652 Sema &SemaRef; 2653 2654 public: 2655 bool VisitDeclRefExpr(const DeclRefExpr *E) { 2656 if (const auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 2657 if (VD->hasLocalStorage()) { 2658 SemaRef.Diag(E->getBeginLoc(), 2659 diag::err_omp_local_var_in_threadprivate_init) 2660 << E->getSourceRange(); 2661 SemaRef.Diag(VD->getLocation(), diag::note_defined_here) 2662 << VD << VD->getSourceRange(); 2663 return true; 2664 } 2665 } 2666 return false; 2667 } 2668 bool VisitStmt(const Stmt *S) { 2669 for (const Stmt *Child : S->children()) { 2670 if (Child && Visit(Child)) 2671 return true; 2672 } 2673 return false; 2674 } 2675 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {} 2676 }; 2677 } // namespace 2678 2679 OMPThreadPrivateDecl * 2680 Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) { 2681 SmallVector<Expr *, 8> Vars; 2682 for (Expr *RefExpr : VarList) { 2683 auto *DE = cast<DeclRefExpr>(RefExpr); 2684 auto *VD = cast<VarDecl>(DE->getDecl()); 2685 SourceLocation ILoc = DE->getExprLoc(); 2686 2687 // Mark variable as used. 2688 VD->setReferenced(); 2689 VD->markUsed(Context); 2690 2691 QualType QType = VD->getType(); 2692 if (QType->isDependentType() || QType->isInstantiationDependentType()) { 2693 // It will be analyzed later. 2694 Vars.push_back(DE); 2695 continue; 2696 } 2697 2698 // OpenMP [2.9.2, Restrictions, C/C++, p.10] 2699 // A threadprivate variable must not have an incomplete type. 2700 if (RequireCompleteType(ILoc, VD->getType(), 2701 diag::err_omp_threadprivate_incomplete_type)) { 2702 continue; 2703 } 2704 2705 // OpenMP [2.9.2, Restrictions, C/C++, p.10] 2706 // A threadprivate variable must not have a reference type. 2707 if (VD->getType()->isReferenceType()) { 2708 Diag(ILoc, diag::err_omp_ref_type_arg) 2709 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType(); 2710 bool IsDecl = 2711 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2712 Diag(VD->getLocation(), 2713 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2714 << VD; 2715 continue; 2716 } 2717 2718 // Check if this is a TLS variable. If TLS is not being supported, produce 2719 // the corresponding diagnostic. 2720 if ((VD->getTLSKind() != VarDecl::TLS_None && 2721 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() && 2722 getLangOpts().OpenMPUseTLS && 2723 getASTContext().getTargetInfo().isTLSSupported())) || 2724 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() && 2725 !VD->isLocalVarDecl())) { 2726 Diag(ILoc, diag::err_omp_var_thread_local) 2727 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1); 2728 bool IsDecl = 2729 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2730 Diag(VD->getLocation(), 2731 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2732 << VD; 2733 continue; 2734 } 2735 2736 // Check if initial value of threadprivate variable reference variable with 2737 // local storage (it is not supported by runtime). 2738 if (const Expr *Init = VD->getAnyInitializer()) { 2739 LocalVarRefChecker Checker(*this); 2740 if (Checker.Visit(Init)) 2741 continue; 2742 } 2743 2744 Vars.push_back(RefExpr); 2745 DSAStack->addDSA(VD, DE, OMPC_threadprivate); 2746 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit( 2747 Context, SourceRange(Loc, Loc))); 2748 if (ASTMutationListener *ML = Context.getASTMutationListener()) 2749 ML->DeclarationMarkedOpenMPThreadPrivate(VD); 2750 } 2751 OMPThreadPrivateDecl *D = nullptr; 2752 if (!Vars.empty()) { 2753 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc, 2754 Vars); 2755 D->setAccess(AS_public); 2756 } 2757 return D; 2758 } 2759 2760 static OMPAllocateDeclAttr::AllocatorTypeTy 2761 getAllocatorKind(Sema &S, DSAStackTy *Stack, Expr *Allocator) { 2762 if (!Allocator) 2763 return OMPAllocateDeclAttr::OMPDefaultMemAlloc; 2764 if (Allocator->isTypeDependent() || Allocator->isValueDependent() || 2765 Allocator->isInstantiationDependent() || 2766 Allocator->containsUnexpandedParameterPack()) 2767 return OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; 2768 auto AllocatorKindRes = OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; 2769 const Expr *AE = Allocator->IgnoreParenImpCasts(); 2770 for (int I = OMPAllocateDeclAttr::OMPDefaultMemAlloc; 2771 I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) { 2772 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I); 2773 const Expr *DefAllocator = Stack->getAllocator(AllocatorKind); 2774 llvm::FoldingSetNodeID AEId, DAEId; 2775 AE->Profile(AEId, S.getASTContext(), /*Canonical=*/true); 2776 DefAllocator->Profile(DAEId, S.getASTContext(), /*Canonical=*/true); 2777 if (AEId == DAEId) { 2778 AllocatorKindRes = AllocatorKind; 2779 break; 2780 } 2781 } 2782 return AllocatorKindRes; 2783 } 2784 2785 static bool checkPreviousOMPAllocateAttribute( 2786 Sema &S, DSAStackTy *Stack, Expr *RefExpr, VarDecl *VD, 2787 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, Expr *Allocator) { 2788 if (!VD->hasAttr<OMPAllocateDeclAttr>()) 2789 return false; 2790 const auto *A = VD->getAttr<OMPAllocateDeclAttr>(); 2791 Expr *PrevAllocator = A->getAllocator(); 2792 OMPAllocateDeclAttr::AllocatorTypeTy PrevAllocatorKind = 2793 getAllocatorKind(S, Stack, PrevAllocator); 2794 bool AllocatorsMatch = AllocatorKind == PrevAllocatorKind; 2795 if (AllocatorsMatch && 2796 AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc && 2797 Allocator && PrevAllocator) { 2798 const Expr *AE = Allocator->IgnoreParenImpCasts(); 2799 const Expr *PAE = PrevAllocator->IgnoreParenImpCasts(); 2800 llvm::FoldingSetNodeID AEId, PAEId; 2801 AE->Profile(AEId, S.Context, /*Canonical=*/true); 2802 PAE->Profile(PAEId, S.Context, /*Canonical=*/true); 2803 AllocatorsMatch = AEId == PAEId; 2804 } 2805 if (!AllocatorsMatch) { 2806 SmallString<256> AllocatorBuffer; 2807 llvm::raw_svector_ostream AllocatorStream(AllocatorBuffer); 2808 if (Allocator) 2809 Allocator->printPretty(AllocatorStream, nullptr, S.getPrintingPolicy()); 2810 SmallString<256> PrevAllocatorBuffer; 2811 llvm::raw_svector_ostream PrevAllocatorStream(PrevAllocatorBuffer); 2812 if (PrevAllocator) 2813 PrevAllocator->printPretty(PrevAllocatorStream, nullptr, 2814 S.getPrintingPolicy()); 2815 2816 SourceLocation AllocatorLoc = 2817 Allocator ? Allocator->getExprLoc() : RefExpr->getExprLoc(); 2818 SourceRange AllocatorRange = 2819 Allocator ? Allocator->getSourceRange() : RefExpr->getSourceRange(); 2820 SourceLocation PrevAllocatorLoc = 2821 PrevAllocator ? PrevAllocator->getExprLoc() : A->getLocation(); 2822 SourceRange PrevAllocatorRange = 2823 PrevAllocator ? PrevAllocator->getSourceRange() : A->getRange(); 2824 S.Diag(AllocatorLoc, diag::warn_omp_used_different_allocator) 2825 << (Allocator ? 1 : 0) << AllocatorStream.str() 2826 << (PrevAllocator ? 1 : 0) << PrevAllocatorStream.str() 2827 << AllocatorRange; 2828 S.Diag(PrevAllocatorLoc, diag::note_omp_previous_allocator) 2829 << PrevAllocatorRange; 2830 return true; 2831 } 2832 return false; 2833 } 2834 2835 static void 2836 applyOMPAllocateAttribute(Sema &S, VarDecl *VD, 2837 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, 2838 Expr *Allocator, SourceRange SR) { 2839 if (VD->hasAttr<OMPAllocateDeclAttr>()) 2840 return; 2841 if (Allocator && 2842 (Allocator->isTypeDependent() || Allocator->isValueDependent() || 2843 Allocator->isInstantiationDependent() || 2844 Allocator->containsUnexpandedParameterPack())) 2845 return; 2846 auto *A = OMPAllocateDeclAttr::CreateImplicit(S.Context, AllocatorKind, 2847 Allocator, SR); 2848 VD->addAttr(A); 2849 if (ASTMutationListener *ML = S.Context.getASTMutationListener()) 2850 ML->DeclarationMarkedOpenMPAllocate(VD, A); 2851 } 2852 2853 Sema::DeclGroupPtrTy Sema::ActOnOpenMPAllocateDirective( 2854 SourceLocation Loc, ArrayRef<Expr *> VarList, 2855 ArrayRef<OMPClause *> Clauses, DeclContext *Owner) { 2856 assert(Clauses.size() <= 1 && "Expected at most one clause."); 2857 Expr *Allocator = nullptr; 2858 if (Clauses.empty()) { 2859 // OpenMP 5.0, 2.11.3 allocate Directive, Restrictions. 2860 // allocate directives that appear in a target region must specify an 2861 // allocator clause unless a requires directive with the dynamic_allocators 2862 // clause is present in the same compilation unit. 2863 if (LangOpts.OpenMPIsDevice && 2864 !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>()) 2865 targetDiag(Loc, diag::err_expected_allocator_clause); 2866 } else { 2867 Allocator = cast<OMPAllocatorClause>(Clauses.back())->getAllocator(); 2868 } 2869 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind = 2870 getAllocatorKind(*this, DSAStack, Allocator); 2871 SmallVector<Expr *, 8> Vars; 2872 for (Expr *RefExpr : VarList) { 2873 auto *DE = cast<DeclRefExpr>(RefExpr); 2874 auto *VD = cast<VarDecl>(DE->getDecl()); 2875 2876 // Check if this is a TLS variable or global register. 2877 if (VD->getTLSKind() != VarDecl::TLS_None || 2878 VD->hasAttr<OMPThreadPrivateDeclAttr>() || 2879 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() && 2880 !VD->isLocalVarDecl())) 2881 continue; 2882 2883 // If the used several times in the allocate directive, the same allocator 2884 // must be used. 2885 if (checkPreviousOMPAllocateAttribute(*this, DSAStack, RefExpr, VD, 2886 AllocatorKind, Allocator)) 2887 continue; 2888 2889 // OpenMP, 2.11.3 allocate Directive, Restrictions, C / C++ 2890 // If a list item has a static storage type, the allocator expression in the 2891 // allocator clause must be a constant expression that evaluates to one of 2892 // the predefined memory allocator values. 2893 if (Allocator && VD->hasGlobalStorage()) { 2894 if (AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc) { 2895 Diag(Allocator->getExprLoc(), 2896 diag::err_omp_expected_predefined_allocator) 2897 << Allocator->getSourceRange(); 2898 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 2899 VarDecl::DeclarationOnly; 2900 Diag(VD->getLocation(), 2901 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2902 << VD; 2903 continue; 2904 } 2905 } 2906 2907 Vars.push_back(RefExpr); 2908 applyOMPAllocateAttribute(*this, VD, AllocatorKind, Allocator, 2909 DE->getSourceRange()); 2910 } 2911 if (Vars.empty()) 2912 return nullptr; 2913 if (!Owner) 2914 Owner = getCurLexicalContext(); 2915 auto *D = OMPAllocateDecl::Create(Context, Owner, Loc, Vars, Clauses); 2916 D->setAccess(AS_public); 2917 Owner->addDecl(D); 2918 return DeclGroupPtrTy::make(DeclGroupRef(D)); 2919 } 2920 2921 Sema::DeclGroupPtrTy 2922 Sema::ActOnOpenMPRequiresDirective(SourceLocation Loc, 2923 ArrayRef<OMPClause *> ClauseList) { 2924 OMPRequiresDecl *D = nullptr; 2925 if (!CurContext->isFileContext()) { 2926 Diag(Loc, diag::err_omp_invalid_scope) << "requires"; 2927 } else { 2928 D = CheckOMPRequiresDecl(Loc, ClauseList); 2929 if (D) { 2930 CurContext->addDecl(D); 2931 DSAStack->addRequiresDecl(D); 2932 } 2933 } 2934 return DeclGroupPtrTy::make(DeclGroupRef(D)); 2935 } 2936 2937 OMPRequiresDecl *Sema::CheckOMPRequiresDecl(SourceLocation Loc, 2938 ArrayRef<OMPClause *> ClauseList) { 2939 /// For target specific clauses, the requires directive cannot be 2940 /// specified after the handling of any of the target regions in the 2941 /// current compilation unit. 2942 ArrayRef<SourceLocation> TargetLocations = 2943 DSAStack->getEncounteredTargetLocs(); 2944 SourceLocation AtomicLoc = DSAStack->getAtomicDirectiveLoc(); 2945 if (!TargetLocations.empty() || !AtomicLoc.isInvalid()) { 2946 for (const OMPClause *CNew : ClauseList) { 2947 // Check if any of the requires clauses affect target regions. 2948 if (isa<OMPUnifiedSharedMemoryClause>(CNew) || 2949 isa<OMPUnifiedAddressClause>(CNew) || 2950 isa<OMPReverseOffloadClause>(CNew) || 2951 isa<OMPDynamicAllocatorsClause>(CNew)) { 2952 Diag(Loc, diag::err_omp_directive_before_requires) 2953 << "target" << getOpenMPClauseName(CNew->getClauseKind()); 2954 for (SourceLocation TargetLoc : TargetLocations) { 2955 Diag(TargetLoc, diag::note_omp_requires_encountered_directive) 2956 << "target"; 2957 } 2958 } else if (!AtomicLoc.isInvalid() && 2959 isa<OMPAtomicDefaultMemOrderClause>(CNew)) { 2960 Diag(Loc, diag::err_omp_directive_before_requires) 2961 << "atomic" << getOpenMPClauseName(CNew->getClauseKind()); 2962 Diag(AtomicLoc, diag::note_omp_requires_encountered_directive) 2963 << "atomic"; 2964 } 2965 } 2966 } 2967 2968 if (!DSAStack->hasDuplicateRequiresClause(ClauseList)) 2969 return OMPRequiresDecl::Create(Context, getCurLexicalContext(), Loc, 2970 ClauseList); 2971 return nullptr; 2972 } 2973 2974 static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack, 2975 const ValueDecl *D, 2976 const DSAStackTy::DSAVarData &DVar, 2977 bool IsLoopIterVar = false) { 2978 if (DVar.RefExpr) { 2979 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa) 2980 << getOpenMPClauseName(DVar.CKind); 2981 return; 2982 } 2983 enum { 2984 PDSA_StaticMemberShared, 2985 PDSA_StaticLocalVarShared, 2986 PDSA_LoopIterVarPrivate, 2987 PDSA_LoopIterVarLinear, 2988 PDSA_LoopIterVarLastprivate, 2989 PDSA_ConstVarShared, 2990 PDSA_GlobalVarShared, 2991 PDSA_TaskVarFirstprivate, 2992 PDSA_LocalVarPrivate, 2993 PDSA_Implicit 2994 } Reason = PDSA_Implicit; 2995 bool ReportHint = false; 2996 auto ReportLoc = D->getLocation(); 2997 auto *VD = dyn_cast<VarDecl>(D); 2998 if (IsLoopIterVar) { 2999 if (DVar.CKind == OMPC_private) 3000 Reason = PDSA_LoopIterVarPrivate; 3001 else if (DVar.CKind == OMPC_lastprivate) 3002 Reason = PDSA_LoopIterVarLastprivate; 3003 else 3004 Reason = PDSA_LoopIterVarLinear; 3005 } else if (isOpenMPTaskingDirective(DVar.DKind) && 3006 DVar.CKind == OMPC_firstprivate) { 3007 Reason = PDSA_TaskVarFirstprivate; 3008 ReportLoc = DVar.ImplicitDSALoc; 3009 } else if (VD && VD->isStaticLocal()) 3010 Reason = PDSA_StaticLocalVarShared; 3011 else if (VD && VD->isStaticDataMember()) 3012 Reason = PDSA_StaticMemberShared; 3013 else if (VD && VD->isFileVarDecl()) 3014 Reason = PDSA_GlobalVarShared; 3015 else if (D->getType().isConstant(SemaRef.getASTContext())) 3016 Reason = PDSA_ConstVarShared; 3017 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) { 3018 ReportHint = true; 3019 Reason = PDSA_LocalVarPrivate; 3020 } 3021 if (Reason != PDSA_Implicit) { 3022 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa) 3023 << Reason << ReportHint 3024 << getOpenMPDirectiveName(Stack->getCurrentDirective()); 3025 } else if (DVar.ImplicitDSALoc.isValid()) { 3026 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa) 3027 << getOpenMPClauseName(DVar.CKind); 3028 } 3029 } 3030 3031 static OpenMPMapClauseKind 3032 getMapClauseKindFromModifier(OpenMPDefaultmapClauseModifier M, 3033 bool IsAggregateOrDeclareTarget) { 3034 OpenMPMapClauseKind Kind = OMPC_MAP_unknown; 3035 switch (M) { 3036 case OMPC_DEFAULTMAP_MODIFIER_alloc: 3037 Kind = OMPC_MAP_alloc; 3038 break; 3039 case OMPC_DEFAULTMAP_MODIFIER_to: 3040 Kind = OMPC_MAP_to; 3041 break; 3042 case OMPC_DEFAULTMAP_MODIFIER_from: 3043 Kind = OMPC_MAP_from; 3044 break; 3045 case OMPC_DEFAULTMAP_MODIFIER_tofrom: 3046 Kind = OMPC_MAP_tofrom; 3047 break; 3048 case OMPC_DEFAULTMAP_MODIFIER_firstprivate: 3049 case OMPC_DEFAULTMAP_MODIFIER_last: 3050 llvm_unreachable("Unexpected defaultmap implicit behavior"); 3051 case OMPC_DEFAULTMAP_MODIFIER_none: 3052 case OMPC_DEFAULTMAP_MODIFIER_default: 3053 case OMPC_DEFAULTMAP_MODIFIER_unknown: 3054 // IsAggregateOrDeclareTarget could be true if: 3055 // 1. the implicit behavior for aggregate is tofrom 3056 // 2. it's a declare target link 3057 if (IsAggregateOrDeclareTarget) { 3058 Kind = OMPC_MAP_tofrom; 3059 break; 3060 } 3061 llvm_unreachable("Unexpected defaultmap implicit behavior"); 3062 } 3063 assert(Kind != OMPC_MAP_unknown && "Expect map kind to be known"); 3064 return Kind; 3065 } 3066 3067 namespace { 3068 class DSAAttrChecker final : public StmtVisitor<DSAAttrChecker, void> { 3069 DSAStackTy *Stack; 3070 Sema &SemaRef; 3071 bool ErrorFound = false; 3072 bool TryCaptureCXXThisMembers = false; 3073 CapturedStmt *CS = nullptr; 3074 llvm::SmallVector<Expr *, 4> ImplicitFirstprivate; 3075 llvm::SmallVector<Expr *, 4> ImplicitMap[OMPC_MAP_delete]; 3076 Sema::VarsWithInheritedDSAType VarsWithInheritedDSA; 3077 llvm::SmallDenseSet<const ValueDecl *, 4> ImplicitDeclarations; 3078 3079 void VisitSubCaptures(OMPExecutableDirective *S) { 3080 // Check implicitly captured variables. 3081 if (!S->hasAssociatedStmt() || !S->getAssociatedStmt()) 3082 return; 3083 visitSubCaptures(S->getInnermostCapturedStmt()); 3084 // Try to capture inner this->member references to generate correct mappings 3085 // and diagnostics. 3086 if (TryCaptureCXXThisMembers || 3087 (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) && 3088 llvm::any_of(S->getInnermostCapturedStmt()->captures(), 3089 [](const CapturedStmt::Capture &C) { 3090 return C.capturesThis(); 3091 }))) { 3092 bool SavedTryCaptureCXXThisMembers = TryCaptureCXXThisMembers; 3093 TryCaptureCXXThisMembers = true; 3094 Visit(S->getInnermostCapturedStmt()->getCapturedStmt()); 3095 TryCaptureCXXThisMembers = SavedTryCaptureCXXThisMembers; 3096 } 3097 // In tasks firstprivates are not captured anymore, need to analyze them 3098 // explicitly. 3099 if (isOpenMPTaskingDirective(S->getDirectiveKind()) && 3100 !isOpenMPTaskLoopDirective(S->getDirectiveKind())) { 3101 for (OMPClause *C : S->clauses()) 3102 if (auto *FC = dyn_cast<OMPFirstprivateClause>(C)) { 3103 for (Expr *Ref : FC->varlists()) 3104 Visit(Ref); 3105 } 3106 } 3107 } 3108 3109 public: 3110 void VisitDeclRefExpr(DeclRefExpr *E) { 3111 if (TryCaptureCXXThisMembers || E->isTypeDependent() || 3112 E->isValueDependent() || E->containsUnexpandedParameterPack() || 3113 E->isInstantiationDependent()) 3114 return; 3115 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 3116 // Check the datasharing rules for the expressions in the clauses. 3117 if (!CS) { 3118 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD)) 3119 if (!CED->hasAttr<OMPCaptureNoInitAttr>()) { 3120 Visit(CED->getInit()); 3121 return; 3122 } 3123 } else if (VD->isImplicit() || isa<OMPCapturedExprDecl>(VD)) 3124 // Do not analyze internal variables and do not enclose them into 3125 // implicit clauses. 3126 return; 3127 VD = VD->getCanonicalDecl(); 3128 // Skip internally declared variables. 3129 if (VD->hasLocalStorage() && CS && !CS->capturesVariable(VD) && 3130 !Stack->isImplicitTaskFirstprivate(VD)) 3131 return; 3132 3133 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false); 3134 // Check if the variable has explicit DSA set and stop analysis if it so. 3135 if (DVar.RefExpr || !ImplicitDeclarations.insert(VD).second) 3136 return; 3137 3138 // Skip internally declared static variables. 3139 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 3140 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); 3141 if (VD->hasGlobalStorage() && CS && !CS->capturesVariable(VD) && 3142 (Stack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() || 3143 !Res || *Res != OMPDeclareTargetDeclAttr::MT_Link) && 3144 !Stack->isImplicitTaskFirstprivate(VD)) 3145 return; 3146 3147 SourceLocation ELoc = E->getExprLoc(); 3148 OpenMPDirectiveKind DKind = Stack->getCurrentDirective(); 3149 // The default(none) clause requires that each variable that is referenced 3150 // in the construct, and does not have a predetermined data-sharing 3151 // attribute, must have its data-sharing attribute explicitly determined 3152 // by being listed in a data-sharing attribute clause. 3153 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none && 3154 isImplicitOrExplicitTaskingRegion(DKind) && 3155 VarsWithInheritedDSA.count(VD) == 0) { 3156 VarsWithInheritedDSA[VD] = E; 3157 return; 3158 } 3159 3160 // OpenMP 5.0 [2.19.7.2, defaultmap clause, Description] 3161 // If implicit-behavior is none, each variable referenced in the 3162 // construct that does not have a predetermined data-sharing attribute 3163 // and does not appear in a to or link clause on a declare target 3164 // directive must be listed in a data-mapping attribute clause, a 3165 // data-haring attribute clause (including a data-sharing attribute 3166 // clause on a combined construct where target. is one of the 3167 // constituent constructs), or an is_device_ptr clause. 3168 OpenMPDefaultmapClauseKind ClauseKind = 3169 getVariableCategoryFromDecl(SemaRef.getLangOpts(), VD); 3170 if (SemaRef.getLangOpts().OpenMP >= 50) { 3171 bool IsModifierNone = Stack->getDefaultmapModifier(ClauseKind) == 3172 OMPC_DEFAULTMAP_MODIFIER_none; 3173 if (DVar.CKind == OMPC_unknown && IsModifierNone && 3174 VarsWithInheritedDSA.count(VD) == 0 && !Res) { 3175 // Only check for data-mapping attribute and is_device_ptr here 3176 // since we have already make sure that the declaration does not 3177 // have a data-sharing attribute above 3178 if (!Stack->checkMappableExprComponentListsForDecl( 3179 VD, /*CurrentRegionOnly=*/true, 3180 [VD](OMPClauseMappableExprCommon::MappableExprComponentListRef 3181 MapExprComponents, 3182 OpenMPClauseKind) { 3183 auto MI = MapExprComponents.rbegin(); 3184 auto ME = MapExprComponents.rend(); 3185 return MI != ME && MI->getAssociatedDeclaration() == VD; 3186 })) { 3187 VarsWithInheritedDSA[VD] = E; 3188 return; 3189 } 3190 } 3191 } 3192 3193 if (isOpenMPTargetExecutionDirective(DKind) && 3194 !Stack->isLoopControlVariable(VD).first) { 3195 if (!Stack->checkMappableExprComponentListsForDecl( 3196 VD, /*CurrentRegionOnly=*/true, 3197 [](OMPClauseMappableExprCommon::MappableExprComponentListRef 3198 StackComponents, 3199 OpenMPClauseKind) { 3200 // Variable is used if it has been marked as an array, array 3201 // section or the variable iself. 3202 return StackComponents.size() == 1 || 3203 std::all_of( 3204 std::next(StackComponents.rbegin()), 3205 StackComponents.rend(), 3206 [](const OMPClauseMappableExprCommon:: 3207 MappableComponent &MC) { 3208 return MC.getAssociatedDeclaration() == 3209 nullptr && 3210 (isa<OMPArraySectionExpr>( 3211 MC.getAssociatedExpression()) || 3212 isa<ArraySubscriptExpr>( 3213 MC.getAssociatedExpression())); 3214 }); 3215 })) { 3216 bool IsFirstprivate = false; 3217 // By default lambdas are captured as firstprivates. 3218 if (const auto *RD = 3219 VD->getType().getNonReferenceType()->getAsCXXRecordDecl()) 3220 IsFirstprivate = RD->isLambda(); 3221 IsFirstprivate = 3222 IsFirstprivate || (Stack->mustBeFirstprivate(ClauseKind) && !Res); 3223 if (IsFirstprivate) { 3224 ImplicitFirstprivate.emplace_back(E); 3225 } else { 3226 OpenMPDefaultmapClauseModifier M = 3227 Stack->getDefaultmapModifier(ClauseKind); 3228 OpenMPMapClauseKind Kind = getMapClauseKindFromModifier( 3229 M, ClauseKind == OMPC_DEFAULTMAP_aggregate || Res); 3230 ImplicitMap[Kind].emplace_back(E); 3231 } 3232 return; 3233 } 3234 } 3235 3236 // OpenMP [2.9.3.6, Restrictions, p.2] 3237 // A list item that appears in a reduction clause of the innermost 3238 // enclosing worksharing or parallel construct may not be accessed in an 3239 // explicit task. 3240 DVar = Stack->hasInnermostDSA( 3241 VD, [](OpenMPClauseKind C) { return C == OMPC_reduction; }, 3242 [](OpenMPDirectiveKind K) { 3243 return isOpenMPParallelDirective(K) || 3244 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K); 3245 }, 3246 /*FromParent=*/true); 3247 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) { 3248 ErrorFound = true; 3249 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); 3250 reportOriginalDsa(SemaRef, Stack, VD, DVar); 3251 return; 3252 } 3253 3254 // Define implicit data-sharing attributes for task. 3255 DVar = Stack->getImplicitDSA(VD, /*FromParent=*/false); 3256 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared && 3257 !Stack->isLoopControlVariable(VD).first) { 3258 ImplicitFirstprivate.push_back(E); 3259 return; 3260 } 3261 3262 // Store implicitly used globals with declare target link for parent 3263 // target. 3264 if (!isOpenMPTargetExecutionDirective(DKind) && Res && 3265 *Res == OMPDeclareTargetDeclAttr::MT_Link) { 3266 Stack->addToParentTargetRegionLinkGlobals(E); 3267 return; 3268 } 3269 } 3270 } 3271 void VisitMemberExpr(MemberExpr *E) { 3272 if (E->isTypeDependent() || E->isValueDependent() || 3273 E->containsUnexpandedParameterPack() || E->isInstantiationDependent()) 3274 return; 3275 auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); 3276 OpenMPDirectiveKind DKind = Stack->getCurrentDirective(); 3277 if (auto *TE = dyn_cast<CXXThisExpr>(E->getBase()->IgnoreParenCasts())) { 3278 if (!FD) 3279 return; 3280 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(FD, /*FromParent=*/false); 3281 // Check if the variable has explicit DSA set and stop analysis if it 3282 // so. 3283 if (DVar.RefExpr || !ImplicitDeclarations.insert(FD).second) 3284 return; 3285 3286 if (isOpenMPTargetExecutionDirective(DKind) && 3287 !Stack->isLoopControlVariable(FD).first && 3288 !Stack->checkMappableExprComponentListsForDecl( 3289 FD, /*CurrentRegionOnly=*/true, 3290 [](OMPClauseMappableExprCommon::MappableExprComponentListRef 3291 StackComponents, 3292 OpenMPClauseKind) { 3293 return isa<CXXThisExpr>( 3294 cast<MemberExpr>( 3295 StackComponents.back().getAssociatedExpression()) 3296 ->getBase() 3297 ->IgnoreParens()); 3298 })) { 3299 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3] 3300 // A bit-field cannot appear in a map clause. 3301 // 3302 if (FD->isBitField()) 3303 return; 3304 3305 // Check to see if the member expression is referencing a class that 3306 // has already been explicitly mapped 3307 if (Stack->isClassPreviouslyMapped(TE->getType())) 3308 return; 3309 3310 OpenMPDefaultmapClauseModifier Modifier = 3311 Stack->getDefaultmapModifier(OMPC_DEFAULTMAP_aggregate); 3312 OpenMPMapClauseKind Kind = getMapClauseKindFromModifier( 3313 Modifier, /*IsAggregateOrDeclareTarget*/ true); 3314 ImplicitMap[Kind].emplace_back(E); 3315 return; 3316 } 3317 3318 SourceLocation ELoc = E->getExprLoc(); 3319 // OpenMP [2.9.3.6, Restrictions, p.2] 3320 // A list item that appears in a reduction clause of the innermost 3321 // enclosing worksharing or parallel construct may not be accessed in 3322 // an explicit task. 3323 DVar = Stack->hasInnermostDSA( 3324 FD, [](OpenMPClauseKind C) { return C == OMPC_reduction; }, 3325 [](OpenMPDirectiveKind K) { 3326 return isOpenMPParallelDirective(K) || 3327 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K); 3328 }, 3329 /*FromParent=*/true); 3330 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) { 3331 ErrorFound = true; 3332 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); 3333 reportOriginalDsa(SemaRef, Stack, FD, DVar); 3334 return; 3335 } 3336 3337 // Define implicit data-sharing attributes for task. 3338 DVar = Stack->getImplicitDSA(FD, /*FromParent=*/false); 3339 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared && 3340 !Stack->isLoopControlVariable(FD).first) { 3341 // Check if there is a captured expression for the current field in the 3342 // region. Do not mark it as firstprivate unless there is no captured 3343 // expression. 3344 // TODO: try to make it firstprivate. 3345 if (DVar.CKind != OMPC_unknown) 3346 ImplicitFirstprivate.push_back(E); 3347 } 3348 return; 3349 } 3350 if (isOpenMPTargetExecutionDirective(DKind)) { 3351 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents; 3352 if (!checkMapClauseExpressionBase(SemaRef, E, CurComponents, OMPC_map, 3353 /*NoDiagnose=*/true)) 3354 return; 3355 const auto *VD = cast<ValueDecl>( 3356 CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl()); 3357 if (!Stack->checkMappableExprComponentListsForDecl( 3358 VD, /*CurrentRegionOnly=*/true, 3359 [&CurComponents]( 3360 OMPClauseMappableExprCommon::MappableExprComponentListRef 3361 StackComponents, 3362 OpenMPClauseKind) { 3363 auto CCI = CurComponents.rbegin(); 3364 auto CCE = CurComponents.rend(); 3365 for (const auto &SC : llvm::reverse(StackComponents)) { 3366 // Do both expressions have the same kind? 3367 if (CCI->getAssociatedExpression()->getStmtClass() != 3368 SC.getAssociatedExpression()->getStmtClass()) 3369 if (!(isa<OMPArraySectionExpr>( 3370 SC.getAssociatedExpression()) && 3371 isa<ArraySubscriptExpr>( 3372 CCI->getAssociatedExpression()))) 3373 return false; 3374 3375 const Decl *CCD = CCI->getAssociatedDeclaration(); 3376 const Decl *SCD = SC.getAssociatedDeclaration(); 3377 CCD = CCD ? CCD->getCanonicalDecl() : nullptr; 3378 SCD = SCD ? SCD->getCanonicalDecl() : nullptr; 3379 if (SCD != CCD) 3380 return false; 3381 std::advance(CCI, 1); 3382 if (CCI == CCE) 3383 break; 3384 } 3385 return true; 3386 })) { 3387 Visit(E->getBase()); 3388 } 3389 } else if (!TryCaptureCXXThisMembers) { 3390 Visit(E->getBase()); 3391 } 3392 } 3393 void VisitOMPExecutableDirective(OMPExecutableDirective *S) { 3394 for (OMPClause *C : S->clauses()) { 3395 // Skip analysis of arguments of implicitly defined firstprivate clause 3396 // for task|target directives. 3397 // Skip analysis of arguments of implicitly defined map clause for target 3398 // directives. 3399 if (C && !((isa<OMPFirstprivateClause>(C) || isa<OMPMapClause>(C)) && 3400 C->isImplicit())) { 3401 for (Stmt *CC : C->children()) { 3402 if (CC) 3403 Visit(CC); 3404 } 3405 } 3406 } 3407 // Check implicitly captured variables. 3408 VisitSubCaptures(S); 3409 } 3410 void VisitStmt(Stmt *S) { 3411 for (Stmt *C : S->children()) { 3412 if (C) { 3413 // Check implicitly captured variables in the task-based directives to 3414 // check if they must be firstprivatized. 3415 Visit(C); 3416 } 3417 } 3418 } 3419 3420 void visitSubCaptures(CapturedStmt *S) { 3421 for (const CapturedStmt::Capture &Cap : S->captures()) { 3422 if (!Cap.capturesVariable() && !Cap.capturesVariableByCopy()) 3423 continue; 3424 VarDecl *VD = Cap.getCapturedVar(); 3425 // Do not try to map the variable if it or its sub-component was mapped 3426 // already. 3427 if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) && 3428 Stack->checkMappableExprComponentListsForDecl( 3429 VD, /*CurrentRegionOnly=*/true, 3430 [](OMPClauseMappableExprCommon::MappableExprComponentListRef, 3431 OpenMPClauseKind) { return true; })) 3432 continue; 3433 DeclRefExpr *DRE = buildDeclRefExpr( 3434 SemaRef, VD, VD->getType().getNonLValueExprType(SemaRef.Context), 3435 Cap.getLocation(), /*RefersToCapture=*/true); 3436 Visit(DRE); 3437 } 3438 } 3439 bool isErrorFound() const { return ErrorFound; } 3440 ArrayRef<Expr *> getImplicitFirstprivate() const { 3441 return ImplicitFirstprivate; 3442 } 3443 ArrayRef<Expr *> getImplicitMap(OpenMPDefaultmapClauseKind Kind) const { 3444 return ImplicitMap[Kind]; 3445 } 3446 const Sema::VarsWithInheritedDSAType &getVarsWithInheritedDSA() const { 3447 return VarsWithInheritedDSA; 3448 } 3449 3450 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS) 3451 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) { 3452 // Process declare target link variables for the target directives. 3453 if (isOpenMPTargetExecutionDirective(S->getCurrentDirective())) { 3454 for (DeclRefExpr *E : Stack->getLinkGlobals()) 3455 Visit(E); 3456 } 3457 } 3458 }; 3459 } // namespace 3460 3461 void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) { 3462 switch (DKind) { 3463 case OMPD_parallel: 3464 case OMPD_parallel_for: 3465 case OMPD_parallel_for_simd: 3466 case OMPD_parallel_sections: 3467 case OMPD_parallel_master: 3468 case OMPD_teams: 3469 case OMPD_teams_distribute: 3470 case OMPD_teams_distribute_simd: { 3471 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3472 QualType KmpInt32PtrTy = 3473 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3474 Sema::CapturedParamNameType Params[] = { 3475 std::make_pair(".global_tid.", KmpInt32PtrTy), 3476 std::make_pair(".bound_tid.", KmpInt32PtrTy), 3477 std::make_pair(StringRef(), QualType()) // __context with shared vars 3478 }; 3479 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3480 Params); 3481 break; 3482 } 3483 case OMPD_target_teams: 3484 case OMPD_target_parallel: 3485 case OMPD_target_parallel_for: 3486 case OMPD_target_parallel_for_simd: 3487 case OMPD_target_teams_distribute: 3488 case OMPD_target_teams_distribute_simd: { 3489 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3490 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 3491 QualType KmpInt32PtrTy = 3492 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3493 QualType Args[] = {VoidPtrTy}; 3494 FunctionProtoType::ExtProtoInfo EPI; 3495 EPI.Variadic = true; 3496 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 3497 Sema::CapturedParamNameType Params[] = { 3498 std::make_pair(".global_tid.", KmpInt32Ty), 3499 std::make_pair(".part_id.", KmpInt32PtrTy), 3500 std::make_pair(".privates.", VoidPtrTy), 3501 std::make_pair( 3502 ".copy_fn.", 3503 Context.getPointerType(CopyFnType).withConst().withRestrict()), 3504 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 3505 std::make_pair(StringRef(), QualType()) // __context with shared vars 3506 }; 3507 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3508 Params, /*OpenMPCaptureLevel=*/0); 3509 // Mark this captured region as inlined, because we don't use outlined 3510 // function directly. 3511 getCurCapturedRegion()->TheCapturedDecl->addAttr( 3512 AlwaysInlineAttr::CreateImplicit( 3513 Context, {}, AttributeCommonInfo::AS_Keyword, 3514 AlwaysInlineAttr::Keyword_forceinline)); 3515 Sema::CapturedParamNameType ParamsTarget[] = { 3516 std::make_pair(StringRef(), QualType()) // __context with shared vars 3517 }; 3518 // Start a captured region for 'target' with no implicit parameters. 3519 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3520 ParamsTarget, /*OpenMPCaptureLevel=*/1); 3521 Sema::CapturedParamNameType ParamsTeamsOrParallel[] = { 3522 std::make_pair(".global_tid.", KmpInt32PtrTy), 3523 std::make_pair(".bound_tid.", KmpInt32PtrTy), 3524 std::make_pair(StringRef(), QualType()) // __context with shared vars 3525 }; 3526 // Start a captured region for 'teams' or 'parallel'. Both regions have 3527 // the same implicit parameters. 3528 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3529 ParamsTeamsOrParallel, /*OpenMPCaptureLevel=*/2); 3530 break; 3531 } 3532 case OMPD_target: 3533 case OMPD_target_simd: { 3534 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3535 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 3536 QualType KmpInt32PtrTy = 3537 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3538 QualType Args[] = {VoidPtrTy}; 3539 FunctionProtoType::ExtProtoInfo EPI; 3540 EPI.Variadic = true; 3541 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 3542 Sema::CapturedParamNameType Params[] = { 3543 std::make_pair(".global_tid.", KmpInt32Ty), 3544 std::make_pair(".part_id.", KmpInt32PtrTy), 3545 std::make_pair(".privates.", VoidPtrTy), 3546 std::make_pair( 3547 ".copy_fn.", 3548 Context.getPointerType(CopyFnType).withConst().withRestrict()), 3549 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 3550 std::make_pair(StringRef(), QualType()) // __context with shared vars 3551 }; 3552 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3553 Params, /*OpenMPCaptureLevel=*/0); 3554 // Mark this captured region as inlined, because we don't use outlined 3555 // function directly. 3556 getCurCapturedRegion()->TheCapturedDecl->addAttr( 3557 AlwaysInlineAttr::CreateImplicit( 3558 Context, {}, AttributeCommonInfo::AS_Keyword, 3559 AlwaysInlineAttr::Keyword_forceinline)); 3560 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3561 std::make_pair(StringRef(), QualType()), 3562 /*OpenMPCaptureLevel=*/1); 3563 break; 3564 } 3565 case OMPD_simd: 3566 case OMPD_for: 3567 case OMPD_for_simd: 3568 case OMPD_sections: 3569 case OMPD_section: 3570 case OMPD_single: 3571 case OMPD_master: 3572 case OMPD_critical: 3573 case OMPD_taskgroup: 3574 case OMPD_distribute: 3575 case OMPD_distribute_simd: 3576 case OMPD_ordered: 3577 case OMPD_atomic: 3578 case OMPD_target_data: { 3579 Sema::CapturedParamNameType Params[] = { 3580 std::make_pair(StringRef(), QualType()) // __context with shared vars 3581 }; 3582 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3583 Params); 3584 break; 3585 } 3586 case OMPD_task: { 3587 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3588 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 3589 QualType KmpInt32PtrTy = 3590 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3591 QualType Args[] = {VoidPtrTy}; 3592 FunctionProtoType::ExtProtoInfo EPI; 3593 EPI.Variadic = true; 3594 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 3595 Sema::CapturedParamNameType Params[] = { 3596 std::make_pair(".global_tid.", KmpInt32Ty), 3597 std::make_pair(".part_id.", KmpInt32PtrTy), 3598 std::make_pair(".privates.", VoidPtrTy), 3599 std::make_pair( 3600 ".copy_fn.", 3601 Context.getPointerType(CopyFnType).withConst().withRestrict()), 3602 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 3603 std::make_pair(StringRef(), QualType()) // __context with shared vars 3604 }; 3605 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3606 Params); 3607 // Mark this captured region as inlined, because we don't use outlined 3608 // function directly. 3609 getCurCapturedRegion()->TheCapturedDecl->addAttr( 3610 AlwaysInlineAttr::CreateImplicit( 3611 Context, {}, AttributeCommonInfo::AS_Keyword, 3612 AlwaysInlineAttr::Keyword_forceinline)); 3613 break; 3614 } 3615 case OMPD_taskloop: 3616 case OMPD_taskloop_simd: 3617 case OMPD_master_taskloop: 3618 case OMPD_master_taskloop_simd: { 3619 QualType KmpInt32Ty = 3620 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1) 3621 .withConst(); 3622 QualType KmpUInt64Ty = 3623 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0) 3624 .withConst(); 3625 QualType KmpInt64Ty = 3626 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1) 3627 .withConst(); 3628 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 3629 QualType KmpInt32PtrTy = 3630 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3631 QualType Args[] = {VoidPtrTy}; 3632 FunctionProtoType::ExtProtoInfo EPI; 3633 EPI.Variadic = true; 3634 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 3635 Sema::CapturedParamNameType Params[] = { 3636 std::make_pair(".global_tid.", KmpInt32Ty), 3637 std::make_pair(".part_id.", KmpInt32PtrTy), 3638 std::make_pair(".privates.", VoidPtrTy), 3639 std::make_pair( 3640 ".copy_fn.", 3641 Context.getPointerType(CopyFnType).withConst().withRestrict()), 3642 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 3643 std::make_pair(".lb.", KmpUInt64Ty), 3644 std::make_pair(".ub.", KmpUInt64Ty), 3645 std::make_pair(".st.", KmpInt64Ty), 3646 std::make_pair(".liter.", KmpInt32Ty), 3647 std::make_pair(".reductions.", VoidPtrTy), 3648 std::make_pair(StringRef(), QualType()) // __context with shared vars 3649 }; 3650 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3651 Params); 3652 // Mark this captured region as inlined, because we don't use outlined 3653 // function directly. 3654 getCurCapturedRegion()->TheCapturedDecl->addAttr( 3655 AlwaysInlineAttr::CreateImplicit( 3656 Context, {}, AttributeCommonInfo::AS_Keyword, 3657 AlwaysInlineAttr::Keyword_forceinline)); 3658 break; 3659 } 3660 case OMPD_parallel_master_taskloop: 3661 case OMPD_parallel_master_taskloop_simd: { 3662 QualType KmpInt32Ty = 3663 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1) 3664 .withConst(); 3665 QualType KmpUInt64Ty = 3666 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0) 3667 .withConst(); 3668 QualType KmpInt64Ty = 3669 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1) 3670 .withConst(); 3671 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 3672 QualType KmpInt32PtrTy = 3673 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3674 Sema::CapturedParamNameType ParamsParallel[] = { 3675 std::make_pair(".global_tid.", KmpInt32PtrTy), 3676 std::make_pair(".bound_tid.", KmpInt32PtrTy), 3677 std::make_pair(StringRef(), QualType()) // __context with shared vars 3678 }; 3679 // Start a captured region for 'parallel'. 3680 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3681 ParamsParallel, /*OpenMPCaptureLevel=*/0); 3682 QualType Args[] = {VoidPtrTy}; 3683 FunctionProtoType::ExtProtoInfo EPI; 3684 EPI.Variadic = true; 3685 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 3686 Sema::CapturedParamNameType Params[] = { 3687 std::make_pair(".global_tid.", KmpInt32Ty), 3688 std::make_pair(".part_id.", KmpInt32PtrTy), 3689 std::make_pair(".privates.", VoidPtrTy), 3690 std::make_pair( 3691 ".copy_fn.", 3692 Context.getPointerType(CopyFnType).withConst().withRestrict()), 3693 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 3694 std::make_pair(".lb.", KmpUInt64Ty), 3695 std::make_pair(".ub.", KmpUInt64Ty), 3696 std::make_pair(".st.", KmpInt64Ty), 3697 std::make_pair(".liter.", KmpInt32Ty), 3698 std::make_pair(".reductions.", VoidPtrTy), 3699 std::make_pair(StringRef(), QualType()) // __context with shared vars 3700 }; 3701 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3702 Params, /*OpenMPCaptureLevel=*/1); 3703 // Mark this captured region as inlined, because we don't use outlined 3704 // function directly. 3705 getCurCapturedRegion()->TheCapturedDecl->addAttr( 3706 AlwaysInlineAttr::CreateImplicit( 3707 Context, {}, AttributeCommonInfo::AS_Keyword, 3708 AlwaysInlineAttr::Keyword_forceinline)); 3709 break; 3710 } 3711 case OMPD_distribute_parallel_for_simd: 3712 case OMPD_distribute_parallel_for: { 3713 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3714 QualType KmpInt32PtrTy = 3715 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3716 Sema::CapturedParamNameType Params[] = { 3717 std::make_pair(".global_tid.", KmpInt32PtrTy), 3718 std::make_pair(".bound_tid.", KmpInt32PtrTy), 3719 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 3720 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 3721 std::make_pair(StringRef(), QualType()) // __context with shared vars 3722 }; 3723 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3724 Params); 3725 break; 3726 } 3727 case OMPD_target_teams_distribute_parallel_for: 3728 case OMPD_target_teams_distribute_parallel_for_simd: { 3729 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3730 QualType KmpInt32PtrTy = 3731 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3732 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 3733 3734 QualType Args[] = {VoidPtrTy}; 3735 FunctionProtoType::ExtProtoInfo EPI; 3736 EPI.Variadic = true; 3737 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 3738 Sema::CapturedParamNameType Params[] = { 3739 std::make_pair(".global_tid.", KmpInt32Ty), 3740 std::make_pair(".part_id.", KmpInt32PtrTy), 3741 std::make_pair(".privates.", VoidPtrTy), 3742 std::make_pair( 3743 ".copy_fn.", 3744 Context.getPointerType(CopyFnType).withConst().withRestrict()), 3745 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 3746 std::make_pair(StringRef(), QualType()) // __context with shared vars 3747 }; 3748 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3749 Params, /*OpenMPCaptureLevel=*/0); 3750 // Mark this captured region as inlined, because we don't use outlined 3751 // function directly. 3752 getCurCapturedRegion()->TheCapturedDecl->addAttr( 3753 AlwaysInlineAttr::CreateImplicit( 3754 Context, {}, AttributeCommonInfo::AS_Keyword, 3755 AlwaysInlineAttr::Keyword_forceinline)); 3756 Sema::CapturedParamNameType ParamsTarget[] = { 3757 std::make_pair(StringRef(), QualType()) // __context with shared vars 3758 }; 3759 // Start a captured region for 'target' with no implicit parameters. 3760 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3761 ParamsTarget, /*OpenMPCaptureLevel=*/1); 3762 3763 Sema::CapturedParamNameType ParamsTeams[] = { 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 // Start a captured region for 'target' with no implicit parameters. 3769 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3770 ParamsTeams, /*OpenMPCaptureLevel=*/2); 3771 3772 Sema::CapturedParamNameType ParamsParallel[] = { 3773 std::make_pair(".global_tid.", KmpInt32PtrTy), 3774 std::make_pair(".bound_tid.", KmpInt32PtrTy), 3775 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 3776 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 3777 std::make_pair(StringRef(), QualType()) // __context with shared vars 3778 }; 3779 // Start a captured region for 'teams' or 'parallel'. Both regions have 3780 // the same implicit parameters. 3781 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3782 ParamsParallel, /*OpenMPCaptureLevel=*/3); 3783 break; 3784 } 3785 3786 case OMPD_teams_distribute_parallel_for: 3787 case OMPD_teams_distribute_parallel_for_simd: { 3788 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3789 QualType KmpInt32PtrTy = 3790 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3791 3792 Sema::CapturedParamNameType ParamsTeams[] = { 3793 std::make_pair(".global_tid.", KmpInt32PtrTy), 3794 std::make_pair(".bound_tid.", KmpInt32PtrTy), 3795 std::make_pair(StringRef(), QualType()) // __context with shared vars 3796 }; 3797 // Start a captured region for 'target' with no implicit parameters. 3798 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3799 ParamsTeams, /*OpenMPCaptureLevel=*/0); 3800 3801 Sema::CapturedParamNameType ParamsParallel[] = { 3802 std::make_pair(".global_tid.", KmpInt32PtrTy), 3803 std::make_pair(".bound_tid.", KmpInt32PtrTy), 3804 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 3805 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 3806 std::make_pair(StringRef(), QualType()) // __context with shared vars 3807 }; 3808 // Start a captured region for 'teams' or 'parallel'. Both regions have 3809 // the same implicit parameters. 3810 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3811 ParamsParallel, /*OpenMPCaptureLevel=*/1); 3812 break; 3813 } 3814 case OMPD_target_update: 3815 case OMPD_target_enter_data: 3816 case OMPD_target_exit_data: { 3817 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3818 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 3819 QualType KmpInt32PtrTy = 3820 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3821 QualType Args[] = {VoidPtrTy}; 3822 FunctionProtoType::ExtProtoInfo EPI; 3823 EPI.Variadic = true; 3824 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 3825 Sema::CapturedParamNameType Params[] = { 3826 std::make_pair(".global_tid.", KmpInt32Ty), 3827 std::make_pair(".part_id.", KmpInt32PtrTy), 3828 std::make_pair(".privates.", VoidPtrTy), 3829 std::make_pair( 3830 ".copy_fn.", 3831 Context.getPointerType(CopyFnType).withConst().withRestrict()), 3832 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 3833 std::make_pair(StringRef(), QualType()) // __context with shared vars 3834 }; 3835 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3836 Params); 3837 // Mark this captured region as inlined, because we don't use outlined 3838 // function directly. 3839 getCurCapturedRegion()->TheCapturedDecl->addAttr( 3840 AlwaysInlineAttr::CreateImplicit( 3841 Context, {}, AttributeCommonInfo::AS_Keyword, 3842 AlwaysInlineAttr::Keyword_forceinline)); 3843 break; 3844 } 3845 case OMPD_threadprivate: 3846 case OMPD_allocate: 3847 case OMPD_taskyield: 3848 case OMPD_barrier: 3849 case OMPD_taskwait: 3850 case OMPD_cancellation_point: 3851 case OMPD_cancel: 3852 case OMPD_flush: 3853 case OMPD_depobj: 3854 case OMPD_declare_reduction: 3855 case OMPD_declare_mapper: 3856 case OMPD_declare_simd: 3857 case OMPD_declare_target: 3858 case OMPD_end_declare_target: 3859 case OMPD_requires: 3860 case OMPD_declare_variant: 3861 llvm_unreachable("OpenMP Directive is not allowed"); 3862 case OMPD_unknown: 3863 llvm_unreachable("Unknown OpenMP directive"); 3864 } 3865 } 3866 3867 int Sema::getNumberOfConstructScopes(unsigned Level) const { 3868 return getOpenMPCaptureLevels(DSAStack->getDirective(Level)); 3869 } 3870 3871 int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) { 3872 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 3873 getOpenMPCaptureRegions(CaptureRegions, DKind); 3874 return CaptureRegions.size(); 3875 } 3876 3877 static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id, 3878 Expr *CaptureExpr, bool WithInit, 3879 bool AsExpression) { 3880 assert(CaptureExpr); 3881 ASTContext &C = S.getASTContext(); 3882 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts(); 3883 QualType Ty = Init->getType(); 3884 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) { 3885 if (S.getLangOpts().CPlusPlus) { 3886 Ty = C.getLValueReferenceType(Ty); 3887 } else { 3888 Ty = C.getPointerType(Ty); 3889 ExprResult Res = 3890 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init); 3891 if (!Res.isUsable()) 3892 return nullptr; 3893 Init = Res.get(); 3894 } 3895 WithInit = true; 3896 } 3897 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty, 3898 CaptureExpr->getBeginLoc()); 3899 if (!WithInit) 3900 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C)); 3901 S.CurContext->addHiddenDecl(CED); 3902 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false); 3903 return CED; 3904 } 3905 3906 static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr, 3907 bool WithInit) { 3908 OMPCapturedExprDecl *CD; 3909 if (VarDecl *VD = S.isOpenMPCapturedDecl(D)) 3910 CD = cast<OMPCapturedExprDecl>(VD); 3911 else 3912 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit, 3913 /*AsExpression=*/false); 3914 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(), 3915 CaptureExpr->getExprLoc()); 3916 } 3917 3918 static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) { 3919 CaptureExpr = S.DefaultLvalueConversion(CaptureExpr).get(); 3920 if (!Ref) { 3921 OMPCapturedExprDecl *CD = buildCaptureDecl( 3922 S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr, 3923 /*WithInit=*/true, /*AsExpression=*/true); 3924 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(), 3925 CaptureExpr->getExprLoc()); 3926 } 3927 ExprResult Res = Ref; 3928 if (!S.getLangOpts().CPlusPlus && 3929 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() && 3930 Ref->getType()->isPointerType()) { 3931 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref); 3932 if (!Res.isUsable()) 3933 return ExprError(); 3934 } 3935 return S.DefaultLvalueConversion(Res.get()); 3936 } 3937 3938 namespace { 3939 // OpenMP directives parsed in this section are represented as a 3940 // CapturedStatement with an associated statement. If a syntax error 3941 // is detected during the parsing of the associated statement, the 3942 // compiler must abort processing and close the CapturedStatement. 3943 // 3944 // Combined directives such as 'target parallel' have more than one 3945 // nested CapturedStatements. This RAII ensures that we unwind out 3946 // of all the nested CapturedStatements when an error is found. 3947 class CaptureRegionUnwinderRAII { 3948 private: 3949 Sema &S; 3950 bool &ErrorFound; 3951 OpenMPDirectiveKind DKind = OMPD_unknown; 3952 3953 public: 3954 CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound, 3955 OpenMPDirectiveKind DKind) 3956 : S(S), ErrorFound(ErrorFound), DKind(DKind) {} 3957 ~CaptureRegionUnwinderRAII() { 3958 if (ErrorFound) { 3959 int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind); 3960 while (--ThisCaptureLevel >= 0) 3961 S.ActOnCapturedRegionError(); 3962 } 3963 } 3964 }; 3965 } // namespace 3966 3967 void Sema::tryCaptureOpenMPLambdas(ValueDecl *V) { 3968 // Capture variables captured by reference in lambdas for target-based 3969 // directives. 3970 if (!CurContext->isDependentContext() && 3971 (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) || 3972 isOpenMPTargetDataManagementDirective( 3973 DSAStack->getCurrentDirective()))) { 3974 QualType Type = V->getType(); 3975 if (const auto *RD = Type.getCanonicalType() 3976 .getNonReferenceType() 3977 ->getAsCXXRecordDecl()) { 3978 bool SavedForceCaptureByReferenceInTargetExecutable = 3979 DSAStack->isForceCaptureByReferenceInTargetExecutable(); 3980 DSAStack->setForceCaptureByReferenceInTargetExecutable( 3981 /*V=*/true); 3982 if (RD->isLambda()) { 3983 llvm::DenseMap<const VarDecl *, FieldDecl *> Captures; 3984 FieldDecl *ThisCapture; 3985 RD->getCaptureFields(Captures, ThisCapture); 3986 for (const LambdaCapture &LC : RD->captures()) { 3987 if (LC.getCaptureKind() == LCK_ByRef) { 3988 VarDecl *VD = LC.getCapturedVar(); 3989 DeclContext *VDC = VD->getDeclContext(); 3990 if (!VDC->Encloses(CurContext)) 3991 continue; 3992 MarkVariableReferenced(LC.getLocation(), VD); 3993 } else if (LC.getCaptureKind() == LCK_This) { 3994 QualType ThisTy = getCurrentThisType(); 3995 if (!ThisTy.isNull() && 3996 Context.typesAreCompatible(ThisTy, ThisCapture->getType())) 3997 CheckCXXThisCapture(LC.getLocation()); 3998 } 3999 } 4000 } 4001 DSAStack->setForceCaptureByReferenceInTargetExecutable( 4002 SavedForceCaptureByReferenceInTargetExecutable); 4003 } 4004 } 4005 } 4006 4007 static bool checkOrderedOrderSpecified(Sema &S, 4008 const ArrayRef<OMPClause *> Clauses) { 4009 const OMPOrderedClause *Ordered = nullptr; 4010 const OMPOrderClause *Order = nullptr; 4011 4012 for (const OMPClause *Clause : Clauses) { 4013 if (Clause->getClauseKind() == OMPC_ordered) 4014 Ordered = cast<OMPOrderedClause>(Clause); 4015 else if (Clause->getClauseKind() == OMPC_order) { 4016 Order = cast<OMPOrderClause>(Clause); 4017 if (Order->getKind() != OMPC_ORDER_concurrent) 4018 Order = nullptr; 4019 } 4020 if (Ordered && Order) 4021 break; 4022 } 4023 4024 if (Ordered && Order) { 4025 S.Diag(Order->getKindKwLoc(), 4026 diag::err_omp_simple_clause_incompatible_with_ordered) 4027 << getOpenMPClauseName(OMPC_order) 4028 << getOpenMPSimpleClauseTypeName(OMPC_order, OMPC_ORDER_concurrent) 4029 << SourceRange(Order->getBeginLoc(), Order->getEndLoc()); 4030 S.Diag(Ordered->getBeginLoc(), diag::note_omp_ordered_param) 4031 << 0 << SourceRange(Ordered->getBeginLoc(), Ordered->getEndLoc()); 4032 return true; 4033 } 4034 return false; 4035 } 4036 4037 StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S, 4038 ArrayRef<OMPClause *> Clauses) { 4039 bool ErrorFound = false; 4040 CaptureRegionUnwinderRAII CaptureRegionUnwinder( 4041 *this, ErrorFound, DSAStack->getCurrentDirective()); 4042 if (!S.isUsable()) { 4043 ErrorFound = true; 4044 return StmtError(); 4045 } 4046 4047 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 4048 getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective()); 4049 OMPOrderedClause *OC = nullptr; 4050 OMPScheduleClause *SC = nullptr; 4051 SmallVector<const OMPLinearClause *, 4> LCs; 4052 SmallVector<const OMPClauseWithPreInit *, 4> PICs; 4053 // This is required for proper codegen. 4054 for (OMPClause *Clause : Clauses) { 4055 if (isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) && 4056 Clause->getClauseKind() == OMPC_in_reduction) { 4057 // Capture taskgroup task_reduction descriptors inside the tasking regions 4058 // with the corresponding in_reduction items. 4059 auto *IRC = cast<OMPInReductionClause>(Clause); 4060 for (Expr *E : IRC->taskgroup_descriptors()) 4061 if (E) 4062 MarkDeclarationsReferencedInExpr(E); 4063 } 4064 if (isOpenMPPrivate(Clause->getClauseKind()) || 4065 Clause->getClauseKind() == OMPC_copyprivate || 4066 (getLangOpts().OpenMPUseTLS && 4067 getASTContext().getTargetInfo().isTLSSupported() && 4068 Clause->getClauseKind() == OMPC_copyin)) { 4069 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin); 4070 // Mark all variables in private list clauses as used in inner region. 4071 for (Stmt *VarRef : Clause->children()) { 4072 if (auto *E = cast_or_null<Expr>(VarRef)) { 4073 MarkDeclarationsReferencedInExpr(E); 4074 } 4075 } 4076 DSAStack->setForceVarCapturing(/*V=*/false); 4077 } else if (CaptureRegions.size() > 1 || 4078 CaptureRegions.back() != OMPD_unknown) { 4079 if (auto *C = OMPClauseWithPreInit::get(Clause)) 4080 PICs.push_back(C); 4081 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) { 4082 if (Expr *E = C->getPostUpdateExpr()) 4083 MarkDeclarationsReferencedInExpr(E); 4084 } 4085 } 4086 if (Clause->getClauseKind() == OMPC_schedule) 4087 SC = cast<OMPScheduleClause>(Clause); 4088 else if (Clause->getClauseKind() == OMPC_ordered) 4089 OC = cast<OMPOrderedClause>(Clause); 4090 else if (Clause->getClauseKind() == OMPC_linear) 4091 LCs.push_back(cast<OMPLinearClause>(Clause)); 4092 } 4093 // Capture allocator expressions if used. 4094 for (Expr *E : DSAStack->getInnerAllocators()) 4095 MarkDeclarationsReferencedInExpr(E); 4096 // OpenMP, 2.7.1 Loop Construct, Restrictions 4097 // The nonmonotonic modifier cannot be specified if an ordered clause is 4098 // specified. 4099 if (SC && 4100 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic || 4101 SC->getSecondScheduleModifier() == 4102 OMPC_SCHEDULE_MODIFIER_nonmonotonic) && 4103 OC) { 4104 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic 4105 ? SC->getFirstScheduleModifierLoc() 4106 : SC->getSecondScheduleModifierLoc(), 4107 diag::err_omp_simple_clause_incompatible_with_ordered) 4108 << getOpenMPClauseName(OMPC_schedule) 4109 << getOpenMPSimpleClauseTypeName(OMPC_schedule, 4110 OMPC_SCHEDULE_MODIFIER_nonmonotonic) 4111 << SourceRange(OC->getBeginLoc(), OC->getEndLoc()); 4112 ErrorFound = true; 4113 } 4114 // OpenMP 5.0, 2.9.2 Worksharing-Loop Construct, Restrictions. 4115 // If an order(concurrent) clause is present, an ordered clause may not appear 4116 // on the same directive. 4117 if (checkOrderedOrderSpecified(*this, Clauses)) 4118 ErrorFound = true; 4119 if (!LCs.empty() && OC && OC->getNumForLoops()) { 4120 for (const OMPLinearClause *C : LCs) { 4121 Diag(C->getBeginLoc(), diag::err_omp_linear_ordered) 4122 << SourceRange(OC->getBeginLoc(), OC->getEndLoc()); 4123 } 4124 ErrorFound = true; 4125 } 4126 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) && 4127 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC && 4128 OC->getNumForLoops()) { 4129 Diag(OC->getBeginLoc(), diag::err_omp_ordered_simd) 4130 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 4131 ErrorFound = true; 4132 } 4133 if (ErrorFound) { 4134 return StmtError(); 4135 } 4136 StmtResult SR = S; 4137 unsigned CompletedRegions = 0; 4138 for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(CaptureRegions)) { 4139 // Mark all variables in private list clauses as used in inner region. 4140 // Required for proper codegen of combined directives. 4141 // TODO: add processing for other clauses. 4142 if (ThisCaptureRegion != OMPD_unknown) { 4143 for (const clang::OMPClauseWithPreInit *C : PICs) { 4144 OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion(); 4145 // Find the particular capture region for the clause if the 4146 // directive is a combined one with multiple capture regions. 4147 // If the directive is not a combined one, the capture region 4148 // associated with the clause is OMPD_unknown and is generated 4149 // only once. 4150 if (CaptureRegion == ThisCaptureRegion || 4151 CaptureRegion == OMPD_unknown) { 4152 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) { 4153 for (Decl *D : DS->decls()) 4154 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D)); 4155 } 4156 } 4157 } 4158 } 4159 if (++CompletedRegions == CaptureRegions.size()) 4160 DSAStack->setBodyComplete(); 4161 SR = ActOnCapturedRegionEnd(SR.get()); 4162 } 4163 return SR; 4164 } 4165 4166 static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion, 4167 OpenMPDirectiveKind CancelRegion, 4168 SourceLocation StartLoc) { 4169 // CancelRegion is only needed for cancel and cancellation_point. 4170 if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point) 4171 return false; 4172 4173 if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for || 4174 CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup) 4175 return false; 4176 4177 SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region) 4178 << getOpenMPDirectiveName(CancelRegion); 4179 return true; 4180 } 4181 4182 static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack, 4183 OpenMPDirectiveKind CurrentRegion, 4184 const DeclarationNameInfo &CurrentName, 4185 OpenMPDirectiveKind CancelRegion, 4186 SourceLocation StartLoc) { 4187 if (Stack->getCurScope()) { 4188 OpenMPDirectiveKind ParentRegion = Stack->getParentDirective(); 4189 OpenMPDirectiveKind OffendingRegion = ParentRegion; 4190 bool NestingProhibited = false; 4191 bool CloseNesting = true; 4192 bool OrphanSeen = false; 4193 enum { 4194 NoRecommend, 4195 ShouldBeInParallelRegion, 4196 ShouldBeInOrderedRegion, 4197 ShouldBeInTargetRegion, 4198 ShouldBeInTeamsRegion 4199 } Recommend = NoRecommend; 4200 if (isOpenMPSimdDirective(ParentRegion) && 4201 ((SemaRef.LangOpts.OpenMP <= 45 && CurrentRegion != OMPD_ordered) || 4202 (SemaRef.LangOpts.OpenMP >= 50 && CurrentRegion != OMPD_ordered && 4203 CurrentRegion != OMPD_simd && CurrentRegion != OMPD_atomic))) { 4204 // OpenMP [2.16, Nesting of Regions] 4205 // OpenMP constructs may not be nested inside a simd region. 4206 // OpenMP [2.8.1,simd Construct, Restrictions] 4207 // An ordered construct with the simd clause is the only OpenMP 4208 // construct that can appear in the simd region. 4209 // Allowing a SIMD construct nested in another SIMD construct is an 4210 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning 4211 // message. 4212 // OpenMP 5.0 [2.9.3.1, simd Construct, Restrictions] 4213 // The only OpenMP constructs that can be encountered during execution of 4214 // a simd region are the atomic construct, the loop construct, the simd 4215 // construct and the ordered construct with the simd clause. 4216 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd) 4217 ? diag::err_omp_prohibited_region_simd 4218 : diag::warn_omp_nesting_simd) 4219 << (SemaRef.LangOpts.OpenMP >= 50 ? 1 : 0); 4220 return CurrentRegion != OMPD_simd; 4221 } 4222 if (ParentRegion == OMPD_atomic) { 4223 // OpenMP [2.16, Nesting of Regions] 4224 // OpenMP constructs may not be nested inside an atomic region. 4225 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic); 4226 return true; 4227 } 4228 if (CurrentRegion == OMPD_section) { 4229 // OpenMP [2.7.2, sections Construct, Restrictions] 4230 // Orphaned section directives are prohibited. That is, the section 4231 // directives must appear within the sections construct and must not be 4232 // encountered elsewhere in the sections region. 4233 if (ParentRegion != OMPD_sections && 4234 ParentRegion != OMPD_parallel_sections) { 4235 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive) 4236 << (ParentRegion != OMPD_unknown) 4237 << getOpenMPDirectiveName(ParentRegion); 4238 return true; 4239 } 4240 return false; 4241 } 4242 // Allow some constructs (except teams and cancellation constructs) to be 4243 // orphaned (they could be used in functions, called from OpenMP regions 4244 // with the required preconditions). 4245 if (ParentRegion == OMPD_unknown && 4246 !isOpenMPNestingTeamsDirective(CurrentRegion) && 4247 CurrentRegion != OMPD_cancellation_point && 4248 CurrentRegion != OMPD_cancel) 4249 return false; 4250 if (CurrentRegion == OMPD_cancellation_point || 4251 CurrentRegion == OMPD_cancel) { 4252 // OpenMP [2.16, Nesting of Regions] 4253 // A cancellation point construct for which construct-type-clause is 4254 // taskgroup must be nested inside a task construct. A cancellation 4255 // point construct for which construct-type-clause is not taskgroup must 4256 // be closely nested inside an OpenMP construct that matches the type 4257 // specified in construct-type-clause. 4258 // A cancel construct for which construct-type-clause is taskgroup must be 4259 // nested inside a task construct. A cancel construct for which 4260 // construct-type-clause is not taskgroup must be closely nested inside an 4261 // OpenMP construct that matches the type specified in 4262 // construct-type-clause. 4263 NestingProhibited = 4264 !((CancelRegion == OMPD_parallel && 4265 (ParentRegion == OMPD_parallel || 4266 ParentRegion == OMPD_target_parallel)) || 4267 (CancelRegion == OMPD_for && 4268 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for || 4269 ParentRegion == OMPD_target_parallel_for || 4270 ParentRegion == OMPD_distribute_parallel_for || 4271 ParentRegion == OMPD_teams_distribute_parallel_for || 4272 ParentRegion == OMPD_target_teams_distribute_parallel_for)) || 4273 (CancelRegion == OMPD_taskgroup && 4274 (ParentRegion == OMPD_task || 4275 (SemaRef.getLangOpts().OpenMP >= 50 && 4276 (ParentRegion == OMPD_taskloop || 4277 ParentRegion == OMPD_master_taskloop || 4278 ParentRegion == OMPD_parallel_master_taskloop)))) || 4279 (CancelRegion == OMPD_sections && 4280 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections || 4281 ParentRegion == OMPD_parallel_sections))); 4282 OrphanSeen = ParentRegion == OMPD_unknown; 4283 } else if (CurrentRegion == OMPD_master) { 4284 // OpenMP [2.16, Nesting of Regions] 4285 // A master region may not be closely nested inside a worksharing, 4286 // atomic, or explicit task region. 4287 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 4288 isOpenMPTaskingDirective(ParentRegion); 4289 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) { 4290 // OpenMP [2.16, Nesting of Regions] 4291 // A critical region may not be nested (closely or otherwise) inside a 4292 // critical region with the same name. Note that this restriction is not 4293 // sufficient to prevent deadlock. 4294 SourceLocation PreviousCriticalLoc; 4295 bool DeadLock = Stack->hasDirective( 4296 [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K, 4297 const DeclarationNameInfo &DNI, 4298 SourceLocation Loc) { 4299 if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) { 4300 PreviousCriticalLoc = Loc; 4301 return true; 4302 } 4303 return false; 4304 }, 4305 false /* skip top directive */); 4306 if (DeadLock) { 4307 SemaRef.Diag(StartLoc, 4308 diag::err_omp_prohibited_region_critical_same_name) 4309 << CurrentName.getName(); 4310 if (PreviousCriticalLoc.isValid()) 4311 SemaRef.Diag(PreviousCriticalLoc, 4312 diag::note_omp_previous_critical_region); 4313 return true; 4314 } 4315 } else if (CurrentRegion == OMPD_barrier) { 4316 // OpenMP [2.16, Nesting of Regions] 4317 // A barrier region may not be closely nested inside a worksharing, 4318 // explicit task, critical, ordered, atomic, or master region. 4319 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 4320 isOpenMPTaskingDirective(ParentRegion) || 4321 ParentRegion == OMPD_master || 4322 ParentRegion == OMPD_parallel_master || 4323 ParentRegion == OMPD_critical || 4324 ParentRegion == OMPD_ordered; 4325 } else if (isOpenMPWorksharingDirective(CurrentRegion) && 4326 !isOpenMPParallelDirective(CurrentRegion) && 4327 !isOpenMPTeamsDirective(CurrentRegion)) { 4328 // OpenMP [2.16, Nesting of Regions] 4329 // A worksharing region may not be closely nested inside a worksharing, 4330 // explicit task, critical, ordered, atomic, or master region. 4331 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 4332 isOpenMPTaskingDirective(ParentRegion) || 4333 ParentRegion == OMPD_master || 4334 ParentRegion == OMPD_parallel_master || 4335 ParentRegion == OMPD_critical || 4336 ParentRegion == OMPD_ordered; 4337 Recommend = ShouldBeInParallelRegion; 4338 } else if (CurrentRegion == OMPD_ordered) { 4339 // OpenMP [2.16, Nesting of Regions] 4340 // An ordered region may not be closely nested inside a critical, 4341 // atomic, or explicit task region. 4342 // An ordered region must be closely nested inside a loop region (or 4343 // parallel loop region) with an ordered clause. 4344 // OpenMP [2.8.1,simd Construct, Restrictions] 4345 // An ordered construct with the simd clause is the only OpenMP construct 4346 // that can appear in the simd region. 4347 NestingProhibited = ParentRegion == OMPD_critical || 4348 isOpenMPTaskingDirective(ParentRegion) || 4349 !(isOpenMPSimdDirective(ParentRegion) || 4350 Stack->isParentOrderedRegion()); 4351 Recommend = ShouldBeInOrderedRegion; 4352 } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) { 4353 // OpenMP [2.16, Nesting of Regions] 4354 // If specified, a teams construct must be contained within a target 4355 // construct. 4356 NestingProhibited = 4357 (SemaRef.LangOpts.OpenMP <= 45 && ParentRegion != OMPD_target) || 4358 (SemaRef.LangOpts.OpenMP >= 50 && ParentRegion != OMPD_unknown && 4359 ParentRegion != OMPD_target); 4360 OrphanSeen = ParentRegion == OMPD_unknown; 4361 Recommend = ShouldBeInTargetRegion; 4362 } 4363 if (!NestingProhibited && 4364 !isOpenMPTargetExecutionDirective(CurrentRegion) && 4365 !isOpenMPTargetDataManagementDirective(CurrentRegion) && 4366 (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) { 4367 // OpenMP [2.16, Nesting of Regions] 4368 // distribute, parallel, parallel sections, parallel workshare, and the 4369 // parallel loop and parallel loop SIMD constructs are the only OpenMP 4370 // constructs that can be closely nested in the teams region. 4371 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) && 4372 !isOpenMPDistributeDirective(CurrentRegion); 4373 Recommend = ShouldBeInParallelRegion; 4374 } 4375 if (!NestingProhibited && 4376 isOpenMPNestingDistributeDirective(CurrentRegion)) { 4377 // OpenMP 4.5 [2.17 Nesting of Regions] 4378 // The region associated with the distribute construct must be strictly 4379 // nested inside a teams region 4380 NestingProhibited = 4381 (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams); 4382 Recommend = ShouldBeInTeamsRegion; 4383 } 4384 if (!NestingProhibited && 4385 (isOpenMPTargetExecutionDirective(CurrentRegion) || 4386 isOpenMPTargetDataManagementDirective(CurrentRegion))) { 4387 // OpenMP 4.5 [2.17 Nesting of Regions] 4388 // If a target, target update, target data, target enter data, or 4389 // target exit data construct is encountered during execution of a 4390 // target region, the behavior is unspecified. 4391 NestingProhibited = Stack->hasDirective( 4392 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &, 4393 SourceLocation) { 4394 if (isOpenMPTargetExecutionDirective(K)) { 4395 OffendingRegion = K; 4396 return true; 4397 } 4398 return false; 4399 }, 4400 false /* don't skip top directive */); 4401 CloseNesting = false; 4402 } 4403 if (NestingProhibited) { 4404 if (OrphanSeen) { 4405 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive) 4406 << getOpenMPDirectiveName(CurrentRegion) << Recommend; 4407 } else { 4408 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region) 4409 << CloseNesting << getOpenMPDirectiveName(OffendingRegion) 4410 << Recommend << getOpenMPDirectiveName(CurrentRegion); 4411 } 4412 return true; 4413 } 4414 } 4415 return false; 4416 } 4417 4418 struct Kind2Unsigned { 4419 using argument_type = OpenMPDirectiveKind; 4420 unsigned operator()(argument_type DK) { return unsigned(DK); } 4421 }; 4422 static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind, 4423 ArrayRef<OMPClause *> Clauses, 4424 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) { 4425 bool ErrorFound = false; 4426 unsigned NamedModifiersNumber = 0; 4427 llvm::IndexedMap<const OMPIfClause *, Kind2Unsigned> FoundNameModifiers; 4428 FoundNameModifiers.resize(unsigned(OMPD_unknown) + 1); 4429 SmallVector<SourceLocation, 4> NameModifierLoc; 4430 for (const OMPClause *C : Clauses) { 4431 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) { 4432 // At most one if clause without a directive-name-modifier can appear on 4433 // the directive. 4434 OpenMPDirectiveKind CurNM = IC->getNameModifier(); 4435 if (FoundNameModifiers[CurNM]) { 4436 S.Diag(C->getBeginLoc(), diag::err_omp_more_one_clause) 4437 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if) 4438 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM); 4439 ErrorFound = true; 4440 } else if (CurNM != OMPD_unknown) { 4441 NameModifierLoc.push_back(IC->getNameModifierLoc()); 4442 ++NamedModifiersNumber; 4443 } 4444 FoundNameModifiers[CurNM] = IC; 4445 if (CurNM == OMPD_unknown) 4446 continue; 4447 // Check if the specified name modifier is allowed for the current 4448 // directive. 4449 // At most one if clause with the particular directive-name-modifier can 4450 // appear on the directive. 4451 bool MatchFound = false; 4452 for (auto NM : AllowedNameModifiers) { 4453 if (CurNM == NM) { 4454 MatchFound = true; 4455 break; 4456 } 4457 } 4458 if (!MatchFound) { 4459 S.Diag(IC->getNameModifierLoc(), 4460 diag::err_omp_wrong_if_directive_name_modifier) 4461 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind); 4462 ErrorFound = true; 4463 } 4464 } 4465 } 4466 // If any if clause on the directive includes a directive-name-modifier then 4467 // all if clauses on the directive must include a directive-name-modifier. 4468 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) { 4469 if (NamedModifiersNumber == AllowedNameModifiers.size()) { 4470 S.Diag(FoundNameModifiers[OMPD_unknown]->getBeginLoc(), 4471 diag::err_omp_no_more_if_clause); 4472 } else { 4473 std::string Values; 4474 std::string Sep(", "); 4475 unsigned AllowedCnt = 0; 4476 unsigned TotalAllowedNum = 4477 AllowedNameModifiers.size() - NamedModifiersNumber; 4478 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End; 4479 ++Cnt) { 4480 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt]; 4481 if (!FoundNameModifiers[NM]) { 4482 Values += "'"; 4483 Values += getOpenMPDirectiveName(NM); 4484 Values += "'"; 4485 if (AllowedCnt + 2 == TotalAllowedNum) 4486 Values += " or "; 4487 else if (AllowedCnt + 1 != TotalAllowedNum) 4488 Values += Sep; 4489 ++AllowedCnt; 4490 } 4491 } 4492 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getBeginLoc(), 4493 diag::err_omp_unnamed_if_clause) 4494 << (TotalAllowedNum > 1) << Values; 4495 } 4496 for (SourceLocation Loc : NameModifierLoc) { 4497 S.Diag(Loc, diag::note_omp_previous_named_if_clause); 4498 } 4499 ErrorFound = true; 4500 } 4501 return ErrorFound; 4502 } 4503 4504 static std::pair<ValueDecl *, bool> getPrivateItem(Sema &S, Expr *&RefExpr, 4505 SourceLocation &ELoc, 4506 SourceRange &ERange, 4507 bool AllowArraySection) { 4508 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() || 4509 RefExpr->containsUnexpandedParameterPack()) 4510 return std::make_pair(nullptr, true); 4511 4512 // OpenMP [3.1, C/C++] 4513 // A list item is a variable name. 4514 // OpenMP [2.9.3.3, Restrictions, p.1] 4515 // A variable that is part of another variable (as an array or 4516 // structure element) cannot appear in a private clause. 4517 RefExpr = RefExpr->IgnoreParens(); 4518 enum { 4519 NoArrayExpr = -1, 4520 ArraySubscript = 0, 4521 OMPArraySection = 1 4522 } IsArrayExpr = NoArrayExpr; 4523 if (AllowArraySection) { 4524 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) { 4525 Expr *Base = ASE->getBase()->IgnoreParenImpCasts(); 4526 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 4527 Base = TempASE->getBase()->IgnoreParenImpCasts(); 4528 RefExpr = Base; 4529 IsArrayExpr = ArraySubscript; 4530 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) { 4531 Expr *Base = OASE->getBase()->IgnoreParenImpCasts(); 4532 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) 4533 Base = TempOASE->getBase()->IgnoreParenImpCasts(); 4534 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 4535 Base = TempASE->getBase()->IgnoreParenImpCasts(); 4536 RefExpr = Base; 4537 IsArrayExpr = OMPArraySection; 4538 } 4539 } 4540 ELoc = RefExpr->getExprLoc(); 4541 ERange = RefExpr->getSourceRange(); 4542 RefExpr = RefExpr->IgnoreParenImpCasts(); 4543 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr); 4544 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr); 4545 if ((!DE || !isa<VarDecl>(DE->getDecl())) && 4546 (S.getCurrentThisType().isNull() || !ME || 4547 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) || 4548 !isa<FieldDecl>(ME->getMemberDecl()))) { 4549 if (IsArrayExpr != NoArrayExpr) { 4550 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr 4551 << ERange; 4552 } else { 4553 S.Diag(ELoc, 4554 AllowArraySection 4555 ? diag::err_omp_expected_var_name_member_expr_or_array_item 4556 : diag::err_omp_expected_var_name_member_expr) 4557 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange; 4558 } 4559 return std::make_pair(nullptr, false); 4560 } 4561 return std::make_pair( 4562 getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false); 4563 } 4564 4565 static void checkAllocateClauses(Sema &S, DSAStackTy *Stack, 4566 ArrayRef<OMPClause *> Clauses) { 4567 assert(!S.CurContext->isDependentContext() && 4568 "Expected non-dependent context."); 4569 auto AllocateRange = 4570 llvm::make_filter_range(Clauses, OMPAllocateClause::classof); 4571 llvm::DenseMap<CanonicalDeclPtr<Decl>, CanonicalDeclPtr<VarDecl>> 4572 DeclToCopy; 4573 auto PrivateRange = llvm::make_filter_range(Clauses, [](const OMPClause *C) { 4574 return isOpenMPPrivate(C->getClauseKind()); 4575 }); 4576 for (OMPClause *Cl : PrivateRange) { 4577 MutableArrayRef<Expr *>::iterator I, It, Et; 4578 if (Cl->getClauseKind() == OMPC_private) { 4579 auto *PC = cast<OMPPrivateClause>(Cl); 4580 I = PC->private_copies().begin(); 4581 It = PC->varlist_begin(); 4582 Et = PC->varlist_end(); 4583 } else if (Cl->getClauseKind() == OMPC_firstprivate) { 4584 auto *PC = cast<OMPFirstprivateClause>(Cl); 4585 I = PC->private_copies().begin(); 4586 It = PC->varlist_begin(); 4587 Et = PC->varlist_end(); 4588 } else if (Cl->getClauseKind() == OMPC_lastprivate) { 4589 auto *PC = cast<OMPLastprivateClause>(Cl); 4590 I = PC->private_copies().begin(); 4591 It = PC->varlist_begin(); 4592 Et = PC->varlist_end(); 4593 } else if (Cl->getClauseKind() == OMPC_linear) { 4594 auto *PC = cast<OMPLinearClause>(Cl); 4595 I = PC->privates().begin(); 4596 It = PC->varlist_begin(); 4597 Et = PC->varlist_end(); 4598 } else if (Cl->getClauseKind() == OMPC_reduction) { 4599 auto *PC = cast<OMPReductionClause>(Cl); 4600 I = PC->privates().begin(); 4601 It = PC->varlist_begin(); 4602 Et = PC->varlist_end(); 4603 } else if (Cl->getClauseKind() == OMPC_task_reduction) { 4604 auto *PC = cast<OMPTaskReductionClause>(Cl); 4605 I = PC->privates().begin(); 4606 It = PC->varlist_begin(); 4607 Et = PC->varlist_end(); 4608 } else if (Cl->getClauseKind() == OMPC_in_reduction) { 4609 auto *PC = cast<OMPInReductionClause>(Cl); 4610 I = PC->privates().begin(); 4611 It = PC->varlist_begin(); 4612 Et = PC->varlist_end(); 4613 } else { 4614 llvm_unreachable("Expected private clause."); 4615 } 4616 for (Expr *E : llvm::make_range(It, Et)) { 4617 if (!*I) { 4618 ++I; 4619 continue; 4620 } 4621 SourceLocation ELoc; 4622 SourceRange ERange; 4623 Expr *SimpleRefExpr = E; 4624 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange, 4625 /*AllowArraySection=*/true); 4626 DeclToCopy.try_emplace(Res.first, 4627 cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl())); 4628 ++I; 4629 } 4630 } 4631 for (OMPClause *C : AllocateRange) { 4632 auto *AC = cast<OMPAllocateClause>(C); 4633 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind = 4634 getAllocatorKind(S, Stack, AC->getAllocator()); 4635 // OpenMP, 2.11.4 allocate Clause, Restrictions. 4636 // For task, taskloop or target directives, allocation requests to memory 4637 // allocators with the trait access set to thread result in unspecified 4638 // behavior. 4639 if (AllocatorKind == OMPAllocateDeclAttr::OMPThreadMemAlloc && 4640 (isOpenMPTaskingDirective(Stack->getCurrentDirective()) || 4641 isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()))) { 4642 S.Diag(AC->getAllocator()->getExprLoc(), 4643 diag::warn_omp_allocate_thread_on_task_target_directive) 4644 << getOpenMPDirectiveName(Stack->getCurrentDirective()); 4645 } 4646 for (Expr *E : AC->varlists()) { 4647 SourceLocation ELoc; 4648 SourceRange ERange; 4649 Expr *SimpleRefExpr = E; 4650 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange); 4651 ValueDecl *VD = Res.first; 4652 DSAStackTy::DSAVarData Data = Stack->getTopDSA(VD, /*FromParent=*/false); 4653 if (!isOpenMPPrivate(Data.CKind)) { 4654 S.Diag(E->getExprLoc(), 4655 diag::err_omp_expected_private_copy_for_allocate); 4656 continue; 4657 } 4658 VarDecl *PrivateVD = DeclToCopy[VD]; 4659 if (checkPreviousOMPAllocateAttribute(S, Stack, E, PrivateVD, 4660 AllocatorKind, AC->getAllocator())) 4661 continue; 4662 applyOMPAllocateAttribute(S, PrivateVD, AllocatorKind, AC->getAllocator(), 4663 E->getSourceRange()); 4664 } 4665 } 4666 } 4667 4668 StmtResult Sema::ActOnOpenMPExecutableDirective( 4669 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName, 4670 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses, 4671 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { 4672 StmtResult Res = StmtError(); 4673 // First check CancelRegion which is then used in checkNestingOfRegions. 4674 if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) || 4675 checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion, 4676 StartLoc)) 4677 return StmtError(); 4678 4679 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit; 4680 VarsWithInheritedDSAType VarsWithInheritedDSA; 4681 bool ErrorFound = false; 4682 ClausesWithImplicit.append(Clauses.begin(), Clauses.end()); 4683 if (AStmt && !CurContext->isDependentContext()) { 4684 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 4685 4686 // Check default data sharing attributes for referenced variables. 4687 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt)); 4688 int ThisCaptureLevel = getOpenMPCaptureLevels(Kind); 4689 Stmt *S = AStmt; 4690 while (--ThisCaptureLevel >= 0) 4691 S = cast<CapturedStmt>(S)->getCapturedStmt(); 4692 DSAChecker.Visit(S); 4693 if (!isOpenMPTargetDataManagementDirective(Kind) && 4694 !isOpenMPTaskingDirective(Kind)) { 4695 // Visit subcaptures to generate implicit clauses for captured vars. 4696 auto *CS = cast<CapturedStmt>(AStmt); 4697 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 4698 getOpenMPCaptureRegions(CaptureRegions, Kind); 4699 // Ignore outer tasking regions for target directives. 4700 if (CaptureRegions.size() > 1 && CaptureRegions.front() == OMPD_task) 4701 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 4702 DSAChecker.visitSubCaptures(CS); 4703 } 4704 if (DSAChecker.isErrorFound()) 4705 return StmtError(); 4706 // Generate list of implicitly defined firstprivate variables. 4707 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA(); 4708 4709 SmallVector<Expr *, 4> ImplicitFirstprivates( 4710 DSAChecker.getImplicitFirstprivate().begin(), 4711 DSAChecker.getImplicitFirstprivate().end()); 4712 SmallVector<Expr *, 4> ImplicitMaps[OMPC_MAP_delete]; 4713 for (unsigned I = 0; I < OMPC_MAP_delete; ++I) { 4714 ArrayRef<Expr *> ImplicitMap = 4715 DSAChecker.getImplicitMap(static_cast<OpenMPDefaultmapClauseKind>(I)); 4716 ImplicitMaps[I].append(ImplicitMap.begin(), ImplicitMap.end()); 4717 } 4718 // Mark taskgroup task_reduction descriptors as implicitly firstprivate. 4719 for (OMPClause *C : Clauses) { 4720 if (auto *IRC = dyn_cast<OMPInReductionClause>(C)) { 4721 for (Expr *E : IRC->taskgroup_descriptors()) 4722 if (E) 4723 ImplicitFirstprivates.emplace_back(E); 4724 } 4725 } 4726 if (!ImplicitFirstprivates.empty()) { 4727 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause( 4728 ImplicitFirstprivates, SourceLocation(), SourceLocation(), 4729 SourceLocation())) { 4730 ClausesWithImplicit.push_back(Implicit); 4731 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() != 4732 ImplicitFirstprivates.size(); 4733 } else { 4734 ErrorFound = true; 4735 } 4736 } 4737 int ClauseKindCnt = -1; 4738 for (ArrayRef<Expr *> ImplicitMap : ImplicitMaps) { 4739 ++ClauseKindCnt; 4740 if (ImplicitMap.empty()) 4741 continue; 4742 CXXScopeSpec MapperIdScopeSpec; 4743 DeclarationNameInfo MapperId; 4744 auto Kind = static_cast<OpenMPMapClauseKind>(ClauseKindCnt); 4745 if (OMPClause *Implicit = ActOnOpenMPMapClause( 4746 llvm::None, llvm::None, MapperIdScopeSpec, MapperId, Kind, 4747 /*IsMapTypeImplicit=*/true, SourceLocation(), SourceLocation(), 4748 ImplicitMap, OMPVarListLocTy())) { 4749 ClausesWithImplicit.emplace_back(Implicit); 4750 ErrorFound |= 4751 cast<OMPMapClause>(Implicit)->varlist_size() != ImplicitMap.size(); 4752 } else { 4753 ErrorFound = true; 4754 } 4755 } 4756 } 4757 4758 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers; 4759 switch (Kind) { 4760 case OMPD_parallel: 4761 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc, 4762 EndLoc); 4763 AllowedNameModifiers.push_back(OMPD_parallel); 4764 break; 4765 case OMPD_simd: 4766 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, 4767 VarsWithInheritedDSA); 4768 if (LangOpts.OpenMP >= 50) 4769 AllowedNameModifiers.push_back(OMPD_simd); 4770 break; 4771 case OMPD_for: 4772 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, 4773 VarsWithInheritedDSA); 4774 break; 4775 case OMPD_for_simd: 4776 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 4777 EndLoc, VarsWithInheritedDSA); 4778 if (LangOpts.OpenMP >= 50) 4779 AllowedNameModifiers.push_back(OMPD_simd); 4780 break; 4781 case OMPD_sections: 4782 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc, 4783 EndLoc); 4784 break; 4785 case OMPD_section: 4786 assert(ClausesWithImplicit.empty() && 4787 "No clauses are allowed for 'omp section' directive"); 4788 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc); 4789 break; 4790 case OMPD_single: 4791 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc, 4792 EndLoc); 4793 break; 4794 case OMPD_master: 4795 assert(ClausesWithImplicit.empty() && 4796 "No clauses are allowed for 'omp master' directive"); 4797 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc); 4798 break; 4799 case OMPD_critical: 4800 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt, 4801 StartLoc, EndLoc); 4802 break; 4803 case OMPD_parallel_for: 4804 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc, 4805 EndLoc, VarsWithInheritedDSA); 4806 AllowedNameModifiers.push_back(OMPD_parallel); 4807 break; 4808 case OMPD_parallel_for_simd: 4809 Res = ActOnOpenMPParallelForSimdDirective( 4810 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 4811 AllowedNameModifiers.push_back(OMPD_parallel); 4812 if (LangOpts.OpenMP >= 50) 4813 AllowedNameModifiers.push_back(OMPD_simd); 4814 break; 4815 case OMPD_parallel_master: 4816 Res = ActOnOpenMPParallelMasterDirective(ClausesWithImplicit, AStmt, 4817 StartLoc, EndLoc); 4818 AllowedNameModifiers.push_back(OMPD_parallel); 4819 break; 4820 case OMPD_parallel_sections: 4821 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt, 4822 StartLoc, EndLoc); 4823 AllowedNameModifiers.push_back(OMPD_parallel); 4824 break; 4825 case OMPD_task: 4826 Res = 4827 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 4828 AllowedNameModifiers.push_back(OMPD_task); 4829 break; 4830 case OMPD_taskyield: 4831 assert(ClausesWithImplicit.empty() && 4832 "No clauses are allowed for 'omp taskyield' directive"); 4833 assert(AStmt == nullptr && 4834 "No associated statement allowed for 'omp taskyield' directive"); 4835 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc); 4836 break; 4837 case OMPD_barrier: 4838 assert(ClausesWithImplicit.empty() && 4839 "No clauses are allowed for 'omp barrier' directive"); 4840 assert(AStmt == nullptr && 4841 "No associated statement allowed for 'omp barrier' directive"); 4842 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc); 4843 break; 4844 case OMPD_taskwait: 4845 assert(ClausesWithImplicit.empty() && 4846 "No clauses are allowed for 'omp taskwait' directive"); 4847 assert(AStmt == nullptr && 4848 "No associated statement allowed for 'omp taskwait' directive"); 4849 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc); 4850 break; 4851 case OMPD_taskgroup: 4852 Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc, 4853 EndLoc); 4854 break; 4855 case OMPD_flush: 4856 assert(AStmt == nullptr && 4857 "No associated statement allowed for 'omp flush' directive"); 4858 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc); 4859 break; 4860 case OMPD_depobj: 4861 assert(AStmt == nullptr && 4862 "No associated statement allowed for 'omp depobj' directive"); 4863 Res = ActOnOpenMPDepobjDirective(ClausesWithImplicit, StartLoc, EndLoc); 4864 break; 4865 case OMPD_ordered: 4866 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc, 4867 EndLoc); 4868 break; 4869 case OMPD_atomic: 4870 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc, 4871 EndLoc); 4872 break; 4873 case OMPD_teams: 4874 Res = 4875 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 4876 break; 4877 case OMPD_target: 4878 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc, 4879 EndLoc); 4880 AllowedNameModifiers.push_back(OMPD_target); 4881 break; 4882 case OMPD_target_parallel: 4883 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt, 4884 StartLoc, EndLoc); 4885 AllowedNameModifiers.push_back(OMPD_target); 4886 AllowedNameModifiers.push_back(OMPD_parallel); 4887 break; 4888 case OMPD_target_parallel_for: 4889 Res = ActOnOpenMPTargetParallelForDirective( 4890 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 4891 AllowedNameModifiers.push_back(OMPD_target); 4892 AllowedNameModifiers.push_back(OMPD_parallel); 4893 break; 4894 case OMPD_cancellation_point: 4895 assert(ClausesWithImplicit.empty() && 4896 "No clauses are allowed for 'omp cancellation point' directive"); 4897 assert(AStmt == nullptr && "No associated statement allowed for 'omp " 4898 "cancellation point' directive"); 4899 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion); 4900 break; 4901 case OMPD_cancel: 4902 assert(AStmt == nullptr && 4903 "No associated statement allowed for 'omp cancel' directive"); 4904 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc, 4905 CancelRegion); 4906 AllowedNameModifiers.push_back(OMPD_cancel); 4907 break; 4908 case OMPD_target_data: 4909 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc, 4910 EndLoc); 4911 AllowedNameModifiers.push_back(OMPD_target_data); 4912 break; 4913 case OMPD_target_enter_data: 4914 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc, 4915 EndLoc, AStmt); 4916 AllowedNameModifiers.push_back(OMPD_target_enter_data); 4917 break; 4918 case OMPD_target_exit_data: 4919 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc, 4920 EndLoc, AStmt); 4921 AllowedNameModifiers.push_back(OMPD_target_exit_data); 4922 break; 4923 case OMPD_taskloop: 4924 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc, 4925 EndLoc, VarsWithInheritedDSA); 4926 AllowedNameModifiers.push_back(OMPD_taskloop); 4927 break; 4928 case OMPD_taskloop_simd: 4929 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 4930 EndLoc, VarsWithInheritedDSA); 4931 AllowedNameModifiers.push_back(OMPD_taskloop); 4932 if (LangOpts.OpenMP >= 50) 4933 AllowedNameModifiers.push_back(OMPD_simd); 4934 break; 4935 case OMPD_master_taskloop: 4936 Res = ActOnOpenMPMasterTaskLoopDirective( 4937 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 4938 AllowedNameModifiers.push_back(OMPD_taskloop); 4939 break; 4940 case OMPD_master_taskloop_simd: 4941 Res = ActOnOpenMPMasterTaskLoopSimdDirective( 4942 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 4943 AllowedNameModifiers.push_back(OMPD_taskloop); 4944 if (LangOpts.OpenMP >= 50) 4945 AllowedNameModifiers.push_back(OMPD_simd); 4946 break; 4947 case OMPD_parallel_master_taskloop: 4948 Res = ActOnOpenMPParallelMasterTaskLoopDirective( 4949 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 4950 AllowedNameModifiers.push_back(OMPD_taskloop); 4951 AllowedNameModifiers.push_back(OMPD_parallel); 4952 break; 4953 case OMPD_parallel_master_taskloop_simd: 4954 Res = ActOnOpenMPParallelMasterTaskLoopSimdDirective( 4955 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 4956 AllowedNameModifiers.push_back(OMPD_taskloop); 4957 AllowedNameModifiers.push_back(OMPD_parallel); 4958 if (LangOpts.OpenMP >= 50) 4959 AllowedNameModifiers.push_back(OMPD_simd); 4960 break; 4961 case OMPD_distribute: 4962 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc, 4963 EndLoc, VarsWithInheritedDSA); 4964 break; 4965 case OMPD_target_update: 4966 Res = ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc, 4967 EndLoc, AStmt); 4968 AllowedNameModifiers.push_back(OMPD_target_update); 4969 break; 4970 case OMPD_distribute_parallel_for: 4971 Res = ActOnOpenMPDistributeParallelForDirective( 4972 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 4973 AllowedNameModifiers.push_back(OMPD_parallel); 4974 break; 4975 case OMPD_distribute_parallel_for_simd: 4976 Res = ActOnOpenMPDistributeParallelForSimdDirective( 4977 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 4978 AllowedNameModifiers.push_back(OMPD_parallel); 4979 if (LangOpts.OpenMP >= 50) 4980 AllowedNameModifiers.push_back(OMPD_simd); 4981 break; 4982 case OMPD_distribute_simd: 4983 Res = ActOnOpenMPDistributeSimdDirective( 4984 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 4985 if (LangOpts.OpenMP >= 50) 4986 AllowedNameModifiers.push_back(OMPD_simd); 4987 break; 4988 case OMPD_target_parallel_for_simd: 4989 Res = ActOnOpenMPTargetParallelForSimdDirective( 4990 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 4991 AllowedNameModifiers.push_back(OMPD_target); 4992 AllowedNameModifiers.push_back(OMPD_parallel); 4993 if (LangOpts.OpenMP >= 50) 4994 AllowedNameModifiers.push_back(OMPD_simd); 4995 break; 4996 case OMPD_target_simd: 4997 Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 4998 EndLoc, VarsWithInheritedDSA); 4999 AllowedNameModifiers.push_back(OMPD_target); 5000 if (LangOpts.OpenMP >= 50) 5001 AllowedNameModifiers.push_back(OMPD_simd); 5002 break; 5003 case OMPD_teams_distribute: 5004 Res = ActOnOpenMPTeamsDistributeDirective( 5005 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5006 break; 5007 case OMPD_teams_distribute_simd: 5008 Res = ActOnOpenMPTeamsDistributeSimdDirective( 5009 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5010 if (LangOpts.OpenMP >= 50) 5011 AllowedNameModifiers.push_back(OMPD_simd); 5012 break; 5013 case OMPD_teams_distribute_parallel_for_simd: 5014 Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective( 5015 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5016 AllowedNameModifiers.push_back(OMPD_parallel); 5017 if (LangOpts.OpenMP >= 50) 5018 AllowedNameModifiers.push_back(OMPD_simd); 5019 break; 5020 case OMPD_teams_distribute_parallel_for: 5021 Res = ActOnOpenMPTeamsDistributeParallelForDirective( 5022 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5023 AllowedNameModifiers.push_back(OMPD_parallel); 5024 break; 5025 case OMPD_target_teams: 5026 Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, 5027 EndLoc); 5028 AllowedNameModifiers.push_back(OMPD_target); 5029 break; 5030 case OMPD_target_teams_distribute: 5031 Res = ActOnOpenMPTargetTeamsDistributeDirective( 5032 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5033 AllowedNameModifiers.push_back(OMPD_target); 5034 break; 5035 case OMPD_target_teams_distribute_parallel_for: 5036 Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective( 5037 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5038 AllowedNameModifiers.push_back(OMPD_target); 5039 AllowedNameModifiers.push_back(OMPD_parallel); 5040 break; 5041 case OMPD_target_teams_distribute_parallel_for_simd: 5042 Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective( 5043 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5044 AllowedNameModifiers.push_back(OMPD_target); 5045 AllowedNameModifiers.push_back(OMPD_parallel); 5046 if (LangOpts.OpenMP >= 50) 5047 AllowedNameModifiers.push_back(OMPD_simd); 5048 break; 5049 case OMPD_target_teams_distribute_simd: 5050 Res = ActOnOpenMPTargetTeamsDistributeSimdDirective( 5051 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5052 AllowedNameModifiers.push_back(OMPD_target); 5053 if (LangOpts.OpenMP >= 50) 5054 AllowedNameModifiers.push_back(OMPD_simd); 5055 break; 5056 case OMPD_declare_target: 5057 case OMPD_end_declare_target: 5058 case OMPD_threadprivate: 5059 case OMPD_allocate: 5060 case OMPD_declare_reduction: 5061 case OMPD_declare_mapper: 5062 case OMPD_declare_simd: 5063 case OMPD_requires: 5064 case OMPD_declare_variant: 5065 llvm_unreachable("OpenMP Directive is not allowed"); 5066 case OMPD_unknown: 5067 llvm_unreachable("Unknown OpenMP directive"); 5068 } 5069 5070 ErrorFound = Res.isInvalid() || ErrorFound; 5071 5072 // Check variables in the clauses if default(none) was specified. 5073 if (DSAStack->getDefaultDSA() == DSA_none) { 5074 DSAAttrChecker DSAChecker(DSAStack, *this, nullptr); 5075 for (OMPClause *C : Clauses) { 5076 switch (C->getClauseKind()) { 5077 case OMPC_num_threads: 5078 case OMPC_dist_schedule: 5079 // Do not analyse if no parent teams directive. 5080 if (isOpenMPTeamsDirective(Kind)) 5081 break; 5082 continue; 5083 case OMPC_if: 5084 if (isOpenMPTeamsDirective(Kind) && 5085 cast<OMPIfClause>(C)->getNameModifier() != OMPD_target) 5086 break; 5087 if (isOpenMPParallelDirective(Kind) && 5088 isOpenMPTaskLoopDirective(Kind) && 5089 cast<OMPIfClause>(C)->getNameModifier() != OMPD_parallel) 5090 break; 5091 continue; 5092 case OMPC_schedule: 5093 break; 5094 case OMPC_grainsize: 5095 case OMPC_num_tasks: 5096 case OMPC_final: 5097 case OMPC_priority: 5098 // Do not analyze if no parent parallel directive. 5099 if (isOpenMPParallelDirective(Kind)) 5100 break; 5101 continue; 5102 case OMPC_ordered: 5103 case OMPC_device: 5104 case OMPC_num_teams: 5105 case OMPC_thread_limit: 5106 case OMPC_hint: 5107 case OMPC_collapse: 5108 case OMPC_safelen: 5109 case OMPC_simdlen: 5110 case OMPC_default: 5111 case OMPC_proc_bind: 5112 case OMPC_private: 5113 case OMPC_firstprivate: 5114 case OMPC_lastprivate: 5115 case OMPC_shared: 5116 case OMPC_reduction: 5117 case OMPC_task_reduction: 5118 case OMPC_in_reduction: 5119 case OMPC_linear: 5120 case OMPC_aligned: 5121 case OMPC_copyin: 5122 case OMPC_copyprivate: 5123 case OMPC_nowait: 5124 case OMPC_untied: 5125 case OMPC_mergeable: 5126 case OMPC_allocate: 5127 case OMPC_read: 5128 case OMPC_write: 5129 case OMPC_update: 5130 case OMPC_capture: 5131 case OMPC_seq_cst: 5132 case OMPC_acq_rel: 5133 case OMPC_acquire: 5134 case OMPC_release: 5135 case OMPC_relaxed: 5136 case OMPC_depend: 5137 case OMPC_threads: 5138 case OMPC_simd: 5139 case OMPC_map: 5140 case OMPC_nogroup: 5141 case OMPC_defaultmap: 5142 case OMPC_to: 5143 case OMPC_from: 5144 case OMPC_use_device_ptr: 5145 case OMPC_is_device_ptr: 5146 case OMPC_nontemporal: 5147 case OMPC_order: 5148 case OMPC_destroy: 5149 continue; 5150 case OMPC_allocator: 5151 case OMPC_flush: 5152 case OMPC_depobj: 5153 case OMPC_threadprivate: 5154 case OMPC_uniform: 5155 case OMPC_unknown: 5156 case OMPC_unified_address: 5157 case OMPC_unified_shared_memory: 5158 case OMPC_reverse_offload: 5159 case OMPC_dynamic_allocators: 5160 case OMPC_atomic_default_mem_order: 5161 case OMPC_device_type: 5162 case OMPC_match: 5163 llvm_unreachable("Unexpected clause"); 5164 } 5165 for (Stmt *CC : C->children()) { 5166 if (CC) 5167 DSAChecker.Visit(CC); 5168 } 5169 } 5170 for (const auto &P : DSAChecker.getVarsWithInheritedDSA()) 5171 VarsWithInheritedDSA[P.getFirst()] = P.getSecond(); 5172 } 5173 for (const auto &P : VarsWithInheritedDSA) { 5174 if (P.getFirst()->isImplicit() || isa<OMPCapturedExprDecl>(P.getFirst())) 5175 continue; 5176 ErrorFound = true; 5177 if (DSAStack->getDefaultDSA() == DSA_none) { 5178 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable) 5179 << P.first << P.second->getSourceRange(); 5180 Diag(DSAStack->getDefaultDSALocation(), diag::note_omp_default_dsa_none); 5181 } else if (getLangOpts().OpenMP >= 50) { 5182 Diag(P.second->getExprLoc(), 5183 diag::err_omp_defaultmap_no_attr_for_variable) 5184 << P.first << P.second->getSourceRange(); 5185 Diag(DSAStack->getDefaultDSALocation(), 5186 diag::note_omp_defaultmap_attr_none); 5187 } 5188 } 5189 5190 if (!AllowedNameModifiers.empty()) 5191 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) || 5192 ErrorFound; 5193 5194 if (ErrorFound) 5195 return StmtError(); 5196 5197 if (!CurContext->isDependentContext() && 5198 isOpenMPTargetExecutionDirective(Kind) && 5199 !(DSAStack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() || 5200 DSAStack->hasRequiresDeclWithClause<OMPUnifiedAddressClause>() || 5201 DSAStack->hasRequiresDeclWithClause<OMPReverseOffloadClause>() || 5202 DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())) { 5203 // Register target to DSA Stack. 5204 DSAStack->addTargetDirLocation(StartLoc); 5205 } 5206 5207 return Res; 5208 } 5209 5210 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective( 5211 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen, 5212 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds, 5213 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears, 5214 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) { 5215 assert(Aligneds.size() == Alignments.size()); 5216 assert(Linears.size() == LinModifiers.size()); 5217 assert(Linears.size() == Steps.size()); 5218 if (!DG || DG.get().isNull()) 5219 return DeclGroupPtrTy(); 5220 5221 const int SimdId = 0; 5222 if (!DG.get().isSingleDecl()) { 5223 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd_variant) 5224 << SimdId; 5225 return DG; 5226 } 5227 Decl *ADecl = DG.get().getSingleDecl(); 5228 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl)) 5229 ADecl = FTD->getTemplatedDecl(); 5230 5231 auto *FD = dyn_cast<FunctionDecl>(ADecl); 5232 if (!FD) { 5233 Diag(ADecl->getLocation(), diag::err_omp_function_expected) << SimdId; 5234 return DeclGroupPtrTy(); 5235 } 5236 5237 // OpenMP [2.8.2, declare simd construct, Description] 5238 // The parameter of the simdlen clause must be a constant positive integer 5239 // expression. 5240 ExprResult SL; 5241 if (Simdlen) 5242 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen); 5243 // OpenMP [2.8.2, declare simd construct, Description] 5244 // The special this pointer can be used as if was one of the arguments to the 5245 // function in any of the linear, aligned, or uniform clauses. 5246 // The uniform clause declares one or more arguments to have an invariant 5247 // value for all concurrent invocations of the function in the execution of a 5248 // single SIMD loop. 5249 llvm::DenseMap<const Decl *, const Expr *> UniformedArgs; 5250 const Expr *UniformedLinearThis = nullptr; 5251 for (const Expr *E : Uniforms) { 5252 E = E->IgnoreParenImpCasts(); 5253 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 5254 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) 5255 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 5256 FD->getParamDecl(PVD->getFunctionScopeIndex()) 5257 ->getCanonicalDecl() == PVD->getCanonicalDecl()) { 5258 UniformedArgs.try_emplace(PVD->getCanonicalDecl(), E); 5259 continue; 5260 } 5261 if (isa<CXXThisExpr>(E)) { 5262 UniformedLinearThis = E; 5263 continue; 5264 } 5265 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 5266 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 5267 } 5268 // OpenMP [2.8.2, declare simd construct, Description] 5269 // The aligned clause declares that the object to which each list item points 5270 // is aligned to the number of bytes expressed in the optional parameter of 5271 // the aligned clause. 5272 // The special this pointer can be used as if was one of the arguments to the 5273 // function in any of the linear, aligned, or uniform clauses. 5274 // The type of list items appearing in the aligned clause must be array, 5275 // pointer, reference to array, or reference to pointer. 5276 llvm::DenseMap<const Decl *, const Expr *> AlignedArgs; 5277 const Expr *AlignedThis = nullptr; 5278 for (const Expr *E : Aligneds) { 5279 E = E->IgnoreParenImpCasts(); 5280 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 5281 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 5282 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 5283 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 5284 FD->getParamDecl(PVD->getFunctionScopeIndex()) 5285 ->getCanonicalDecl() == CanonPVD) { 5286 // OpenMP [2.8.1, simd construct, Restrictions] 5287 // A list-item cannot appear in more than one aligned clause. 5288 if (AlignedArgs.count(CanonPVD) > 0) { 5289 Diag(E->getExprLoc(), diag::err_omp_used_in_clause_twice) 5290 << 1 << getOpenMPClauseName(OMPC_aligned) 5291 << E->getSourceRange(); 5292 Diag(AlignedArgs[CanonPVD]->getExprLoc(), 5293 diag::note_omp_explicit_dsa) 5294 << getOpenMPClauseName(OMPC_aligned); 5295 continue; 5296 } 5297 AlignedArgs[CanonPVD] = E; 5298 QualType QTy = PVD->getType() 5299 .getNonReferenceType() 5300 .getUnqualifiedType() 5301 .getCanonicalType(); 5302 const Type *Ty = QTy.getTypePtrOrNull(); 5303 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) { 5304 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr) 5305 << QTy << getLangOpts().CPlusPlus << E->getSourceRange(); 5306 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD; 5307 } 5308 continue; 5309 } 5310 } 5311 if (isa<CXXThisExpr>(E)) { 5312 if (AlignedThis) { 5313 Diag(E->getExprLoc(), diag::err_omp_used_in_clause_twice) 5314 << 2 << getOpenMPClauseName(OMPC_aligned) << E->getSourceRange(); 5315 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa) 5316 << getOpenMPClauseName(OMPC_aligned); 5317 } 5318 AlignedThis = E; 5319 continue; 5320 } 5321 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 5322 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 5323 } 5324 // The optional parameter of the aligned clause, alignment, must be a constant 5325 // positive integer expression. If no optional parameter is specified, 5326 // implementation-defined default alignments for SIMD instructions on the 5327 // target platforms are assumed. 5328 SmallVector<const Expr *, 4> NewAligns; 5329 for (Expr *E : Alignments) { 5330 ExprResult Align; 5331 if (E) 5332 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned); 5333 NewAligns.push_back(Align.get()); 5334 } 5335 // OpenMP [2.8.2, declare simd construct, Description] 5336 // The linear clause declares one or more list items to be private to a SIMD 5337 // lane and to have a linear relationship with respect to the iteration space 5338 // of a loop. 5339 // The special this pointer can be used as if was one of the arguments to the 5340 // function in any of the linear, aligned, or uniform clauses. 5341 // When a linear-step expression is specified in a linear clause it must be 5342 // either a constant integer expression or an integer-typed parameter that is 5343 // specified in a uniform clause on the directive. 5344 llvm::DenseMap<const Decl *, const Expr *> LinearArgs; 5345 const bool IsUniformedThis = UniformedLinearThis != nullptr; 5346 auto MI = LinModifiers.begin(); 5347 for (const Expr *E : Linears) { 5348 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI); 5349 ++MI; 5350 E = E->IgnoreParenImpCasts(); 5351 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 5352 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 5353 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 5354 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 5355 FD->getParamDecl(PVD->getFunctionScopeIndex()) 5356 ->getCanonicalDecl() == CanonPVD) { 5357 // OpenMP [2.15.3.7, linear Clause, Restrictions] 5358 // A list-item cannot appear in more than one linear clause. 5359 if (LinearArgs.count(CanonPVD) > 0) { 5360 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 5361 << getOpenMPClauseName(OMPC_linear) 5362 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange(); 5363 Diag(LinearArgs[CanonPVD]->getExprLoc(), 5364 diag::note_omp_explicit_dsa) 5365 << getOpenMPClauseName(OMPC_linear); 5366 continue; 5367 } 5368 // Each argument can appear in at most one uniform or linear clause. 5369 if (UniformedArgs.count(CanonPVD) > 0) { 5370 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 5371 << getOpenMPClauseName(OMPC_linear) 5372 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange(); 5373 Diag(UniformedArgs[CanonPVD]->getExprLoc(), 5374 diag::note_omp_explicit_dsa) 5375 << getOpenMPClauseName(OMPC_uniform); 5376 continue; 5377 } 5378 LinearArgs[CanonPVD] = E; 5379 if (E->isValueDependent() || E->isTypeDependent() || 5380 E->isInstantiationDependent() || 5381 E->containsUnexpandedParameterPack()) 5382 continue; 5383 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind, 5384 PVD->getOriginalType(), 5385 /*IsDeclareSimd=*/true); 5386 continue; 5387 } 5388 } 5389 if (isa<CXXThisExpr>(E)) { 5390 if (UniformedLinearThis) { 5391 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 5392 << getOpenMPClauseName(OMPC_linear) 5393 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear) 5394 << E->getSourceRange(); 5395 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa) 5396 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform 5397 : OMPC_linear); 5398 continue; 5399 } 5400 UniformedLinearThis = E; 5401 if (E->isValueDependent() || E->isTypeDependent() || 5402 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 5403 continue; 5404 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind, 5405 E->getType(), /*IsDeclareSimd=*/true); 5406 continue; 5407 } 5408 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 5409 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 5410 } 5411 Expr *Step = nullptr; 5412 Expr *NewStep = nullptr; 5413 SmallVector<Expr *, 4> NewSteps; 5414 for (Expr *E : Steps) { 5415 // Skip the same step expression, it was checked already. 5416 if (Step == E || !E) { 5417 NewSteps.push_back(E ? NewStep : nullptr); 5418 continue; 5419 } 5420 Step = E; 5421 if (const auto *DRE = dyn_cast<DeclRefExpr>(Step)) 5422 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 5423 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 5424 if (UniformedArgs.count(CanonPVD) == 0) { 5425 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param) 5426 << Step->getSourceRange(); 5427 } else if (E->isValueDependent() || E->isTypeDependent() || 5428 E->isInstantiationDependent() || 5429 E->containsUnexpandedParameterPack() || 5430 CanonPVD->getType()->hasIntegerRepresentation()) { 5431 NewSteps.push_back(Step); 5432 } else { 5433 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param) 5434 << Step->getSourceRange(); 5435 } 5436 continue; 5437 } 5438 NewStep = Step; 5439 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && 5440 !Step->isInstantiationDependent() && 5441 !Step->containsUnexpandedParameterPack()) { 5442 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step) 5443 .get(); 5444 if (NewStep) 5445 NewStep = VerifyIntegerConstantExpression(NewStep).get(); 5446 } 5447 NewSteps.push_back(NewStep); 5448 } 5449 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit( 5450 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()), 5451 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(), 5452 const_cast<Expr **>(NewAligns.data()), NewAligns.size(), 5453 const_cast<Expr **>(Linears.data()), Linears.size(), 5454 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(), 5455 NewSteps.data(), NewSteps.size(), SR); 5456 ADecl->addAttr(NewAttr); 5457 return DG; 5458 } 5459 5460 static void setPrototype(Sema &S, FunctionDecl *FD, FunctionDecl *FDWithProto, 5461 QualType NewType) { 5462 assert(NewType->isFunctionProtoType() && 5463 "Expected function type with prototype."); 5464 assert(FD->getType()->isFunctionNoProtoType() && 5465 "Expected function with type with no prototype."); 5466 assert(FDWithProto->getType()->isFunctionProtoType() && 5467 "Expected function with prototype."); 5468 // Synthesize parameters with the same types. 5469 FD->setType(NewType); 5470 SmallVector<ParmVarDecl *, 16> Params; 5471 for (const ParmVarDecl *P : FDWithProto->parameters()) { 5472 auto *Param = ParmVarDecl::Create(S.getASTContext(), FD, SourceLocation(), 5473 SourceLocation(), nullptr, P->getType(), 5474 /*TInfo=*/nullptr, SC_None, nullptr); 5475 Param->setScopeInfo(0, Params.size()); 5476 Param->setImplicit(); 5477 Params.push_back(Param); 5478 } 5479 5480 FD->setParams(Params); 5481 } 5482 5483 Optional<std::pair<FunctionDecl *, Expr *>> 5484 Sema::checkOpenMPDeclareVariantFunction(Sema::DeclGroupPtrTy DG, 5485 Expr *VariantRef, OMPTraitInfo &TI, 5486 SourceRange SR) { 5487 if (!DG || DG.get().isNull()) 5488 return None; 5489 5490 const int VariantId = 1; 5491 // Must be applied only to single decl. 5492 if (!DG.get().isSingleDecl()) { 5493 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd_variant) 5494 << VariantId << SR; 5495 return None; 5496 } 5497 Decl *ADecl = DG.get().getSingleDecl(); 5498 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl)) 5499 ADecl = FTD->getTemplatedDecl(); 5500 5501 // Decl must be a function. 5502 auto *FD = dyn_cast<FunctionDecl>(ADecl); 5503 if (!FD) { 5504 Diag(ADecl->getLocation(), diag::err_omp_function_expected) 5505 << VariantId << SR; 5506 return None; 5507 } 5508 5509 auto &&HasMultiVersionAttributes = [](const FunctionDecl *FD) { 5510 return FD->hasAttrs() && 5511 (FD->hasAttr<CPUDispatchAttr>() || FD->hasAttr<CPUSpecificAttr>() || 5512 FD->hasAttr<TargetAttr>()); 5513 }; 5514 // OpenMP is not compatible with CPU-specific attributes. 5515 if (HasMultiVersionAttributes(FD)) { 5516 Diag(FD->getLocation(), diag::err_omp_declare_variant_incompat_attributes) 5517 << SR; 5518 return None; 5519 } 5520 5521 // Allow #pragma omp declare variant only if the function is not used. 5522 if (FD->isUsed(false)) 5523 Diag(SR.getBegin(), diag::warn_omp_declare_variant_after_used) 5524 << FD->getLocation(); 5525 5526 // Check if the function was emitted already. 5527 const FunctionDecl *Definition; 5528 if (!FD->isThisDeclarationADefinition() && FD->isDefined(Definition) && 5529 (LangOpts.EmitAllDecls || Context.DeclMustBeEmitted(Definition))) 5530 Diag(SR.getBegin(), diag::warn_omp_declare_variant_after_emitted) 5531 << FD->getLocation(); 5532 5533 // The VariantRef must point to function. 5534 if (!VariantRef) { 5535 Diag(SR.getBegin(), diag::err_omp_function_expected) << VariantId; 5536 return None; 5537 } 5538 5539 auto ShouldDelayChecks = [](Expr *&E, bool) { 5540 return E && (E->isTypeDependent() || E->isValueDependent() || 5541 E->containsUnexpandedParameterPack() || 5542 E->isInstantiationDependent()); 5543 }; 5544 // Do not check templates, wait until instantiation. 5545 if (FD->isDependentContext() || ShouldDelayChecks(VariantRef, false) || 5546 TI.anyScoreOrCondition(ShouldDelayChecks)) 5547 return std::make_pair(FD, VariantRef); 5548 5549 // Deal with non-constant score and user condition expressions. 5550 auto HandleNonConstantScoresAndConditions = [this](Expr *&E, 5551 bool IsScore) -> bool { 5552 llvm::APSInt Result; 5553 if (!E || E->isIntegerConstantExpr(Result, Context)) 5554 return false; 5555 5556 if (IsScore) { 5557 // We warn on non-constant scores and pretend they were not present. 5558 Diag(E->getExprLoc(), diag::warn_omp_declare_variant_score_not_constant) 5559 << E; 5560 E = nullptr; 5561 } else { 5562 // We could replace a non-constant user condition with "false" but we 5563 // will soon need to handle these anyway for the dynamic version of 5564 // OpenMP context selectors. 5565 Diag(E->getExprLoc(), 5566 diag::err_omp_declare_variant_user_condition_not_constant) 5567 << E; 5568 } 5569 return true; 5570 }; 5571 if (TI.anyScoreOrCondition(HandleNonConstantScoresAndConditions)) 5572 return None; 5573 5574 // Convert VariantRef expression to the type of the original function to 5575 // resolve possible conflicts. 5576 ExprResult VariantRefCast; 5577 if (LangOpts.CPlusPlus) { 5578 QualType FnPtrType; 5579 auto *Method = dyn_cast<CXXMethodDecl>(FD); 5580 if (Method && !Method->isStatic()) { 5581 const Type *ClassType = 5582 Context.getTypeDeclType(Method->getParent()).getTypePtr(); 5583 FnPtrType = Context.getMemberPointerType(FD->getType(), ClassType); 5584 ExprResult ER; 5585 { 5586 // Build adrr_of unary op to correctly handle type checks for member 5587 // functions. 5588 Sema::TentativeAnalysisScope Trap(*this); 5589 ER = CreateBuiltinUnaryOp(VariantRef->getBeginLoc(), UO_AddrOf, 5590 VariantRef); 5591 } 5592 if (!ER.isUsable()) { 5593 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected) 5594 << VariantId << VariantRef->getSourceRange(); 5595 return None; 5596 } 5597 VariantRef = ER.get(); 5598 } else { 5599 FnPtrType = Context.getPointerType(FD->getType()); 5600 } 5601 ImplicitConversionSequence ICS = 5602 TryImplicitConversion(VariantRef, FnPtrType.getUnqualifiedType(), 5603 /*SuppressUserConversions=*/false, 5604 AllowedExplicit::None, 5605 /*InOverloadResolution=*/false, 5606 /*CStyle=*/false, 5607 /*AllowObjCWritebackConversion=*/false); 5608 if (ICS.isFailure()) { 5609 Diag(VariantRef->getExprLoc(), 5610 diag::err_omp_declare_variant_incompat_types) 5611 << VariantRef->getType() 5612 << ((Method && !Method->isStatic()) ? FnPtrType : FD->getType()) 5613 << VariantRef->getSourceRange(); 5614 return None; 5615 } 5616 VariantRefCast = PerformImplicitConversion( 5617 VariantRef, FnPtrType.getUnqualifiedType(), AA_Converting); 5618 if (!VariantRefCast.isUsable()) 5619 return None; 5620 // Drop previously built artificial addr_of unary op for member functions. 5621 if (Method && !Method->isStatic()) { 5622 Expr *PossibleAddrOfVariantRef = VariantRefCast.get(); 5623 if (auto *UO = dyn_cast<UnaryOperator>( 5624 PossibleAddrOfVariantRef->IgnoreImplicit())) 5625 VariantRefCast = UO->getSubExpr(); 5626 } 5627 } else { 5628 VariantRefCast = VariantRef; 5629 } 5630 5631 ExprResult ER = CheckPlaceholderExpr(VariantRefCast.get()); 5632 if (!ER.isUsable() || 5633 !ER.get()->IgnoreParenImpCasts()->getType()->isFunctionType()) { 5634 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected) 5635 << VariantId << VariantRef->getSourceRange(); 5636 return None; 5637 } 5638 5639 // The VariantRef must point to function. 5640 auto *DRE = dyn_cast<DeclRefExpr>(ER.get()->IgnoreParenImpCasts()); 5641 if (!DRE) { 5642 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected) 5643 << VariantId << VariantRef->getSourceRange(); 5644 return None; 5645 } 5646 auto *NewFD = dyn_cast_or_null<FunctionDecl>(DRE->getDecl()); 5647 if (!NewFD) { 5648 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected) 5649 << VariantId << VariantRef->getSourceRange(); 5650 return None; 5651 } 5652 5653 // Check if function types are compatible in C. 5654 if (!LangOpts.CPlusPlus) { 5655 QualType NewType = 5656 Context.mergeFunctionTypes(FD->getType(), NewFD->getType()); 5657 if (NewType.isNull()) { 5658 Diag(VariantRef->getExprLoc(), 5659 diag::err_omp_declare_variant_incompat_types) 5660 << NewFD->getType() << FD->getType() << VariantRef->getSourceRange(); 5661 return None; 5662 } 5663 if (NewType->isFunctionProtoType()) { 5664 if (FD->getType()->isFunctionNoProtoType()) 5665 setPrototype(*this, FD, NewFD, NewType); 5666 else if (NewFD->getType()->isFunctionNoProtoType()) 5667 setPrototype(*this, NewFD, FD, NewType); 5668 } 5669 } 5670 5671 // Check if variant function is not marked with declare variant directive. 5672 if (NewFD->hasAttrs() && NewFD->hasAttr<OMPDeclareVariantAttr>()) { 5673 Diag(VariantRef->getExprLoc(), 5674 diag::warn_omp_declare_variant_marked_as_declare_variant) 5675 << VariantRef->getSourceRange(); 5676 SourceRange SR = 5677 NewFD->specific_attr_begin<OMPDeclareVariantAttr>()->getRange(); 5678 Diag(SR.getBegin(), diag::note_omp_marked_declare_variant_here) << SR; 5679 return None; 5680 } 5681 5682 enum DoesntSupport { 5683 VirtFuncs = 1, 5684 Constructors = 3, 5685 Destructors = 4, 5686 DeletedFuncs = 5, 5687 DefaultedFuncs = 6, 5688 ConstexprFuncs = 7, 5689 ConstevalFuncs = 8, 5690 }; 5691 if (const auto *CXXFD = dyn_cast<CXXMethodDecl>(FD)) { 5692 if (CXXFD->isVirtual()) { 5693 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 5694 << VirtFuncs; 5695 return None; 5696 } 5697 5698 if (isa<CXXConstructorDecl>(FD)) { 5699 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 5700 << Constructors; 5701 return None; 5702 } 5703 5704 if (isa<CXXDestructorDecl>(FD)) { 5705 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 5706 << Destructors; 5707 return None; 5708 } 5709 } 5710 5711 if (FD->isDeleted()) { 5712 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 5713 << DeletedFuncs; 5714 return None; 5715 } 5716 5717 if (FD->isDefaulted()) { 5718 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 5719 << DefaultedFuncs; 5720 return None; 5721 } 5722 5723 if (FD->isConstexpr()) { 5724 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 5725 << (NewFD->isConsteval() ? ConstevalFuncs : ConstexprFuncs); 5726 return None; 5727 } 5728 5729 // Check general compatibility. 5730 if (areMultiversionVariantFunctionsCompatible( 5731 FD, NewFD, PartialDiagnostic::NullDiagnostic(), 5732 PartialDiagnosticAt(SourceLocation(), 5733 PartialDiagnostic::NullDiagnostic()), 5734 PartialDiagnosticAt( 5735 VariantRef->getExprLoc(), 5736 PDiag(diag::err_omp_declare_variant_doesnt_support)), 5737 PartialDiagnosticAt(VariantRef->getExprLoc(), 5738 PDiag(diag::err_omp_declare_variant_diff) 5739 << FD->getLocation()), 5740 /*TemplatesSupported=*/true, /*ConstexprSupported=*/false, 5741 /*CLinkageMayDiffer=*/true)) 5742 return None; 5743 return std::make_pair(FD, cast<Expr>(DRE)); 5744 } 5745 5746 void Sema::ActOnOpenMPDeclareVariantDirective(FunctionDecl *FD, 5747 Expr *VariantRef, 5748 OMPTraitInfo &TI, 5749 SourceRange SR) { 5750 auto *NewAttr = 5751 OMPDeclareVariantAttr::CreateImplicit(Context, VariantRef, TI, SR); 5752 FD->addAttr(NewAttr); 5753 } 5754 5755 void Sema::markOpenMPDeclareVariantFuncsReferenced(SourceLocation Loc, 5756 FunctionDecl *Func, 5757 bool MightBeOdrUse) { 5758 assert(LangOpts.OpenMP && "Expected OpenMP mode."); 5759 5760 if (!Func->isDependentContext() && Func->hasAttrs()) { 5761 for (OMPDeclareVariantAttr *A : 5762 Func->specific_attrs<OMPDeclareVariantAttr>()) { 5763 // TODO: add checks for active OpenMP context where possible. 5764 Expr *VariantRef = A->getVariantFuncRef(); 5765 auto *DRE = cast<DeclRefExpr>(VariantRef->IgnoreParenImpCasts()); 5766 auto *F = cast<FunctionDecl>(DRE->getDecl()); 5767 if (!F->isDefined() && F->isTemplateInstantiation()) 5768 InstantiateFunctionDefinition(Loc, F->getFirstDecl()); 5769 MarkFunctionReferenced(Loc, F, MightBeOdrUse); 5770 } 5771 } 5772 } 5773 5774 StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses, 5775 Stmt *AStmt, 5776 SourceLocation StartLoc, 5777 SourceLocation EndLoc) { 5778 if (!AStmt) 5779 return StmtError(); 5780 5781 auto *CS = cast<CapturedStmt>(AStmt); 5782 // 1.2.2 OpenMP Language Terminology 5783 // Structured block - An executable statement with a single entry at the 5784 // top and a single exit at the bottom. 5785 // The point of exit cannot be a branch out of the structured block. 5786 // longjmp() and throw() must not violate the entry/exit criteria. 5787 CS->getCapturedDecl()->setNothrow(); 5788 5789 setFunctionHasBranchProtectedScope(); 5790 5791 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 5792 DSAStack->isCancelRegion()); 5793 } 5794 5795 namespace { 5796 /// Iteration space of a single for loop. 5797 struct LoopIterationSpace final { 5798 /// True if the condition operator is the strict compare operator (<, > or 5799 /// !=). 5800 bool IsStrictCompare = false; 5801 /// Condition of the loop. 5802 Expr *PreCond = nullptr; 5803 /// This expression calculates the number of iterations in the loop. 5804 /// It is always possible to calculate it before starting the loop. 5805 Expr *NumIterations = nullptr; 5806 /// The loop counter variable. 5807 Expr *CounterVar = nullptr; 5808 /// Private loop counter variable. 5809 Expr *PrivateCounterVar = nullptr; 5810 /// This is initializer for the initial value of #CounterVar. 5811 Expr *CounterInit = nullptr; 5812 /// This is step for the #CounterVar used to generate its update: 5813 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration. 5814 Expr *CounterStep = nullptr; 5815 /// Should step be subtracted? 5816 bool Subtract = false; 5817 /// Source range of the loop init. 5818 SourceRange InitSrcRange; 5819 /// Source range of the loop condition. 5820 SourceRange CondSrcRange; 5821 /// Source range of the loop increment. 5822 SourceRange IncSrcRange; 5823 /// Minimum value that can have the loop control variable. Used to support 5824 /// non-rectangular loops. Applied only for LCV with the non-iterator types, 5825 /// since only such variables can be used in non-loop invariant expressions. 5826 Expr *MinValue = nullptr; 5827 /// Maximum value that can have the loop control variable. Used to support 5828 /// non-rectangular loops. Applied only for LCV with the non-iterator type, 5829 /// since only such variables can be used in non-loop invariant expressions. 5830 Expr *MaxValue = nullptr; 5831 /// true, if the lower bound depends on the outer loop control var. 5832 bool IsNonRectangularLB = false; 5833 /// true, if the upper bound depends on the outer loop control var. 5834 bool IsNonRectangularUB = false; 5835 /// Index of the loop this loop depends on and forms non-rectangular loop 5836 /// nest. 5837 unsigned LoopDependentIdx = 0; 5838 /// Final condition for the non-rectangular loop nest support. It is used to 5839 /// check that the number of iterations for this particular counter must be 5840 /// finished. 5841 Expr *FinalCondition = nullptr; 5842 }; 5843 5844 /// Helper class for checking canonical form of the OpenMP loops and 5845 /// extracting iteration space of each loop in the loop nest, that will be used 5846 /// for IR generation. 5847 class OpenMPIterationSpaceChecker { 5848 /// Reference to Sema. 5849 Sema &SemaRef; 5850 /// Data-sharing stack. 5851 DSAStackTy &Stack; 5852 /// A location for diagnostics (when there is no some better location). 5853 SourceLocation DefaultLoc; 5854 /// A location for diagnostics (when increment is not compatible). 5855 SourceLocation ConditionLoc; 5856 /// A source location for referring to loop init later. 5857 SourceRange InitSrcRange; 5858 /// A source location for referring to condition later. 5859 SourceRange ConditionSrcRange; 5860 /// A source location for referring to increment later. 5861 SourceRange IncrementSrcRange; 5862 /// Loop variable. 5863 ValueDecl *LCDecl = nullptr; 5864 /// Reference to loop variable. 5865 Expr *LCRef = nullptr; 5866 /// Lower bound (initializer for the var). 5867 Expr *LB = nullptr; 5868 /// Upper bound. 5869 Expr *UB = nullptr; 5870 /// Loop step (increment). 5871 Expr *Step = nullptr; 5872 /// This flag is true when condition is one of: 5873 /// Var < UB 5874 /// Var <= UB 5875 /// UB > Var 5876 /// UB >= Var 5877 /// This will have no value when the condition is != 5878 llvm::Optional<bool> TestIsLessOp; 5879 /// This flag is true when condition is strict ( < or > ). 5880 bool TestIsStrictOp = false; 5881 /// This flag is true when step is subtracted on each iteration. 5882 bool SubtractStep = false; 5883 /// The outer loop counter this loop depends on (if any). 5884 const ValueDecl *DepDecl = nullptr; 5885 /// Contains number of loop (starts from 1) on which loop counter init 5886 /// expression of this loop depends on. 5887 Optional<unsigned> InitDependOnLC; 5888 /// Contains number of loop (starts from 1) on which loop counter condition 5889 /// expression of this loop depends on. 5890 Optional<unsigned> CondDependOnLC; 5891 /// Checks if the provide statement depends on the loop counter. 5892 Optional<unsigned> doesDependOnLoopCounter(const Stmt *S, bool IsInitializer); 5893 /// Original condition required for checking of the exit condition for 5894 /// non-rectangular loop. 5895 Expr *Condition = nullptr; 5896 5897 public: 5898 OpenMPIterationSpaceChecker(Sema &SemaRef, DSAStackTy &Stack, 5899 SourceLocation DefaultLoc) 5900 : SemaRef(SemaRef), Stack(Stack), DefaultLoc(DefaultLoc), 5901 ConditionLoc(DefaultLoc) {} 5902 /// Check init-expr for canonical loop form and save loop counter 5903 /// variable - #Var and its initialization value - #LB. 5904 bool checkAndSetInit(Stmt *S, bool EmitDiags = true); 5905 /// Check test-expr for canonical form, save upper-bound (#UB), flags 5906 /// for less/greater and for strict/non-strict comparison. 5907 bool checkAndSetCond(Expr *S); 5908 /// Check incr-expr for canonical loop form and return true if it 5909 /// does not conform, otherwise save loop step (#Step). 5910 bool checkAndSetInc(Expr *S); 5911 /// Return the loop counter variable. 5912 ValueDecl *getLoopDecl() const { return LCDecl; } 5913 /// Return the reference expression to loop counter variable. 5914 Expr *getLoopDeclRefExpr() const { return LCRef; } 5915 /// Source range of the loop init. 5916 SourceRange getInitSrcRange() const { return InitSrcRange; } 5917 /// Source range of the loop condition. 5918 SourceRange getConditionSrcRange() const { return ConditionSrcRange; } 5919 /// Source range of the loop increment. 5920 SourceRange getIncrementSrcRange() const { return IncrementSrcRange; } 5921 /// True if the step should be subtracted. 5922 bool shouldSubtractStep() const { return SubtractStep; } 5923 /// True, if the compare operator is strict (<, > or !=). 5924 bool isStrictTestOp() const { return TestIsStrictOp; } 5925 /// Build the expression to calculate the number of iterations. 5926 Expr *buildNumIterations( 5927 Scope *S, ArrayRef<LoopIterationSpace> ResultIterSpaces, bool LimitedType, 5928 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 5929 /// Build the precondition expression for the loops. 5930 Expr * 5931 buildPreCond(Scope *S, Expr *Cond, 5932 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 5933 /// Build reference expression to the counter be used for codegen. 5934 DeclRefExpr * 5935 buildCounterVar(llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 5936 DSAStackTy &DSA) const; 5937 /// Build reference expression to the private counter be used for 5938 /// codegen. 5939 Expr *buildPrivateCounterVar() const; 5940 /// Build initialization of the counter be used for codegen. 5941 Expr *buildCounterInit() const; 5942 /// Build step of the counter be used for codegen. 5943 Expr *buildCounterStep() const; 5944 /// Build loop data with counter value for depend clauses in ordered 5945 /// directives. 5946 Expr * 5947 buildOrderedLoopData(Scope *S, Expr *Counter, 5948 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 5949 SourceLocation Loc, Expr *Inc = nullptr, 5950 OverloadedOperatorKind OOK = OO_Amp); 5951 /// Builds the minimum value for the loop counter. 5952 std::pair<Expr *, Expr *> buildMinMaxValues( 5953 Scope *S, llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 5954 /// Builds final condition for the non-rectangular loops. 5955 Expr *buildFinalCondition(Scope *S) const; 5956 /// Return true if any expression is dependent. 5957 bool dependent() const; 5958 /// Returns true if the initializer forms non-rectangular loop. 5959 bool doesInitDependOnLC() const { return InitDependOnLC.hasValue(); } 5960 /// Returns true if the condition forms non-rectangular loop. 5961 bool doesCondDependOnLC() const { return CondDependOnLC.hasValue(); } 5962 /// Returns index of the loop we depend on (starting from 1), or 0 otherwise. 5963 unsigned getLoopDependentIdx() const { 5964 return InitDependOnLC.getValueOr(CondDependOnLC.getValueOr(0)); 5965 } 5966 5967 private: 5968 /// Check the right-hand side of an assignment in the increment 5969 /// expression. 5970 bool checkAndSetIncRHS(Expr *RHS); 5971 /// Helper to set loop counter variable and its initializer. 5972 bool setLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB, 5973 bool EmitDiags); 5974 /// Helper to set upper bound. 5975 bool setUB(Expr *NewUB, llvm::Optional<bool> LessOp, bool StrictOp, 5976 SourceRange SR, SourceLocation SL); 5977 /// Helper to set loop increment. 5978 bool setStep(Expr *NewStep, bool Subtract); 5979 }; 5980 5981 bool OpenMPIterationSpaceChecker::dependent() const { 5982 if (!LCDecl) { 5983 assert(!LB && !UB && !Step); 5984 return false; 5985 } 5986 return LCDecl->getType()->isDependentType() || 5987 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) || 5988 (Step && Step->isValueDependent()); 5989 } 5990 5991 bool OpenMPIterationSpaceChecker::setLCDeclAndLB(ValueDecl *NewLCDecl, 5992 Expr *NewLCRefExpr, 5993 Expr *NewLB, bool EmitDiags) { 5994 // State consistency checking to ensure correct usage. 5995 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr && 5996 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp); 5997 if (!NewLCDecl || !NewLB) 5998 return true; 5999 LCDecl = getCanonicalDecl(NewLCDecl); 6000 LCRef = NewLCRefExpr; 6001 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB)) 6002 if (const CXXConstructorDecl *Ctor = CE->getConstructor()) 6003 if ((Ctor->isCopyOrMoveConstructor() || 6004 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && 6005 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) 6006 NewLB = CE->getArg(0)->IgnoreParenImpCasts(); 6007 LB = NewLB; 6008 if (EmitDiags) 6009 InitDependOnLC = doesDependOnLoopCounter(LB, /*IsInitializer=*/true); 6010 return false; 6011 } 6012 6013 bool OpenMPIterationSpaceChecker::setUB(Expr *NewUB, 6014 llvm::Optional<bool> LessOp, 6015 bool StrictOp, SourceRange SR, 6016 SourceLocation SL) { 6017 // State consistency checking to ensure correct usage. 6018 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr && 6019 Step == nullptr && !TestIsLessOp && !TestIsStrictOp); 6020 if (!NewUB) 6021 return true; 6022 UB = NewUB; 6023 if (LessOp) 6024 TestIsLessOp = LessOp; 6025 TestIsStrictOp = StrictOp; 6026 ConditionSrcRange = SR; 6027 ConditionLoc = SL; 6028 CondDependOnLC = doesDependOnLoopCounter(UB, /*IsInitializer=*/false); 6029 return false; 6030 } 6031 6032 bool OpenMPIterationSpaceChecker::setStep(Expr *NewStep, bool Subtract) { 6033 // State consistency checking to ensure correct usage. 6034 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr); 6035 if (!NewStep) 6036 return true; 6037 if (!NewStep->isValueDependent()) { 6038 // Check that the step is integer expression. 6039 SourceLocation StepLoc = NewStep->getBeginLoc(); 6040 ExprResult Val = SemaRef.PerformOpenMPImplicitIntegerConversion( 6041 StepLoc, getExprAsWritten(NewStep)); 6042 if (Val.isInvalid()) 6043 return true; 6044 NewStep = Val.get(); 6045 6046 // OpenMP [2.6, Canonical Loop Form, Restrictions] 6047 // If test-expr is of form var relational-op b and relational-op is < or 6048 // <= then incr-expr must cause var to increase on each iteration of the 6049 // loop. If test-expr is of form var relational-op b and relational-op is 6050 // > or >= then incr-expr must cause var to decrease on each iteration of 6051 // the loop. 6052 // If test-expr is of form b relational-op var and relational-op is < or 6053 // <= then incr-expr must cause var to decrease on each iteration of the 6054 // loop. If test-expr is of form b relational-op var and relational-op is 6055 // > or >= then incr-expr must cause var to increase on each iteration of 6056 // the loop. 6057 llvm::APSInt Result; 6058 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context); 6059 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation(); 6060 bool IsConstNeg = 6061 IsConstant && Result.isSigned() && (Subtract != Result.isNegative()); 6062 bool IsConstPos = 6063 IsConstant && Result.isSigned() && (Subtract == Result.isNegative()); 6064 bool IsConstZero = IsConstant && !Result.getBoolValue(); 6065 6066 // != with increment is treated as <; != with decrement is treated as > 6067 if (!TestIsLessOp.hasValue()) 6068 TestIsLessOp = IsConstPos || (IsUnsigned && !Subtract); 6069 if (UB && (IsConstZero || 6070 (TestIsLessOp.getValue() ? 6071 (IsConstNeg || (IsUnsigned && Subtract)) : 6072 (IsConstPos || (IsUnsigned && !Subtract))))) { 6073 SemaRef.Diag(NewStep->getExprLoc(), 6074 diag::err_omp_loop_incr_not_compatible) 6075 << LCDecl << TestIsLessOp.getValue() << NewStep->getSourceRange(); 6076 SemaRef.Diag(ConditionLoc, 6077 diag::note_omp_loop_cond_requres_compatible_incr) 6078 << TestIsLessOp.getValue() << ConditionSrcRange; 6079 return true; 6080 } 6081 if (TestIsLessOp.getValue() == Subtract) { 6082 NewStep = 6083 SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep) 6084 .get(); 6085 Subtract = !Subtract; 6086 } 6087 } 6088 6089 Step = NewStep; 6090 SubtractStep = Subtract; 6091 return false; 6092 } 6093 6094 namespace { 6095 /// Checker for the non-rectangular loops. Checks if the initializer or 6096 /// condition expression references loop counter variable. 6097 class LoopCounterRefChecker final 6098 : public ConstStmtVisitor<LoopCounterRefChecker, bool> { 6099 Sema &SemaRef; 6100 DSAStackTy &Stack; 6101 const ValueDecl *CurLCDecl = nullptr; 6102 const ValueDecl *DepDecl = nullptr; 6103 const ValueDecl *PrevDepDecl = nullptr; 6104 bool IsInitializer = true; 6105 unsigned BaseLoopId = 0; 6106 bool checkDecl(const Expr *E, const ValueDecl *VD) { 6107 if (getCanonicalDecl(VD) == getCanonicalDecl(CurLCDecl)) { 6108 SemaRef.Diag(E->getExprLoc(), diag::err_omp_stmt_depends_on_loop_counter) 6109 << (IsInitializer ? 0 : 1); 6110 return false; 6111 } 6112 const auto &&Data = Stack.isLoopControlVariable(VD); 6113 // OpenMP, 2.9.1 Canonical Loop Form, Restrictions. 6114 // The type of the loop iterator on which we depend may not have a random 6115 // access iterator type. 6116 if (Data.first && VD->getType()->isRecordType()) { 6117 SmallString<128> Name; 6118 llvm::raw_svector_ostream OS(Name); 6119 VD->getNameForDiagnostic(OS, SemaRef.getPrintingPolicy(), 6120 /*Qualified=*/true); 6121 SemaRef.Diag(E->getExprLoc(), 6122 diag::err_omp_wrong_dependency_iterator_type) 6123 << OS.str(); 6124 SemaRef.Diag(VD->getLocation(), diag::note_previous_decl) << VD; 6125 return false; 6126 } 6127 if (Data.first && 6128 (DepDecl || (PrevDepDecl && 6129 getCanonicalDecl(VD) != getCanonicalDecl(PrevDepDecl)))) { 6130 if (!DepDecl && PrevDepDecl) 6131 DepDecl = PrevDepDecl; 6132 SmallString<128> Name; 6133 llvm::raw_svector_ostream OS(Name); 6134 DepDecl->getNameForDiagnostic(OS, SemaRef.getPrintingPolicy(), 6135 /*Qualified=*/true); 6136 SemaRef.Diag(E->getExprLoc(), 6137 diag::err_omp_invariant_or_linear_dependency) 6138 << OS.str(); 6139 return false; 6140 } 6141 if (Data.first) { 6142 DepDecl = VD; 6143 BaseLoopId = Data.first; 6144 } 6145 return Data.first; 6146 } 6147 6148 public: 6149 bool VisitDeclRefExpr(const DeclRefExpr *E) { 6150 const ValueDecl *VD = E->getDecl(); 6151 if (isa<VarDecl>(VD)) 6152 return checkDecl(E, VD); 6153 return false; 6154 } 6155 bool VisitMemberExpr(const MemberExpr *E) { 6156 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) { 6157 const ValueDecl *VD = E->getMemberDecl(); 6158 if (isa<VarDecl>(VD) || isa<FieldDecl>(VD)) 6159 return checkDecl(E, VD); 6160 } 6161 return false; 6162 } 6163 bool VisitStmt(const Stmt *S) { 6164 bool Res = false; 6165 for (const Stmt *Child : S->children()) 6166 Res = (Child && Visit(Child)) || Res; 6167 return Res; 6168 } 6169 explicit LoopCounterRefChecker(Sema &SemaRef, DSAStackTy &Stack, 6170 const ValueDecl *CurLCDecl, bool IsInitializer, 6171 const ValueDecl *PrevDepDecl = nullptr) 6172 : SemaRef(SemaRef), Stack(Stack), CurLCDecl(CurLCDecl), 6173 PrevDepDecl(PrevDepDecl), IsInitializer(IsInitializer) {} 6174 unsigned getBaseLoopId() const { 6175 assert(CurLCDecl && "Expected loop dependency."); 6176 return BaseLoopId; 6177 } 6178 const ValueDecl *getDepDecl() const { 6179 assert(CurLCDecl && "Expected loop dependency."); 6180 return DepDecl; 6181 } 6182 }; 6183 } // namespace 6184 6185 Optional<unsigned> 6186 OpenMPIterationSpaceChecker::doesDependOnLoopCounter(const Stmt *S, 6187 bool IsInitializer) { 6188 // Check for the non-rectangular loops. 6189 LoopCounterRefChecker LoopStmtChecker(SemaRef, Stack, LCDecl, IsInitializer, 6190 DepDecl); 6191 if (LoopStmtChecker.Visit(S)) { 6192 DepDecl = LoopStmtChecker.getDepDecl(); 6193 return LoopStmtChecker.getBaseLoopId(); 6194 } 6195 return llvm::None; 6196 } 6197 6198 bool OpenMPIterationSpaceChecker::checkAndSetInit(Stmt *S, bool EmitDiags) { 6199 // Check init-expr for canonical loop form and save loop counter 6200 // variable - #Var and its initialization value - #LB. 6201 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following: 6202 // var = lb 6203 // integer-type var = lb 6204 // random-access-iterator-type var = lb 6205 // pointer-type var = lb 6206 // 6207 if (!S) { 6208 if (EmitDiags) { 6209 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init); 6210 } 6211 return true; 6212 } 6213 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S)) 6214 if (!ExprTemp->cleanupsHaveSideEffects()) 6215 S = ExprTemp->getSubExpr(); 6216 6217 InitSrcRange = S->getSourceRange(); 6218 if (Expr *E = dyn_cast<Expr>(S)) 6219 S = E->IgnoreParens(); 6220 if (auto *BO = dyn_cast<BinaryOperator>(S)) { 6221 if (BO->getOpcode() == BO_Assign) { 6222 Expr *LHS = BO->getLHS()->IgnoreParens(); 6223 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) { 6224 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) 6225 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 6226 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 6227 EmitDiags); 6228 return setLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS(), EmitDiags); 6229 } 6230 if (auto *ME = dyn_cast<MemberExpr>(LHS)) { 6231 if (ME->isArrow() && 6232 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 6233 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 6234 EmitDiags); 6235 } 6236 } 6237 } else if (auto *DS = dyn_cast<DeclStmt>(S)) { 6238 if (DS->isSingleDecl()) { 6239 if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) { 6240 if (Var->hasInit() && !Var->getType()->isReferenceType()) { 6241 // Accept non-canonical init form here but emit ext. warning. 6242 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags) 6243 SemaRef.Diag(S->getBeginLoc(), 6244 diag::ext_omp_loop_not_canonical_init) 6245 << S->getSourceRange(); 6246 return setLCDeclAndLB( 6247 Var, 6248 buildDeclRefExpr(SemaRef, Var, 6249 Var->getType().getNonReferenceType(), 6250 DS->getBeginLoc()), 6251 Var->getInit(), EmitDiags); 6252 } 6253 } 6254 } 6255 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 6256 if (CE->getOperator() == OO_Equal) { 6257 Expr *LHS = CE->getArg(0); 6258 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) { 6259 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) 6260 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 6261 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 6262 EmitDiags); 6263 return setLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1), EmitDiags); 6264 } 6265 if (auto *ME = dyn_cast<MemberExpr>(LHS)) { 6266 if (ME->isArrow() && 6267 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 6268 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 6269 EmitDiags); 6270 } 6271 } 6272 } 6273 6274 if (dependent() || SemaRef.CurContext->isDependentContext()) 6275 return false; 6276 if (EmitDiags) { 6277 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_init) 6278 << S->getSourceRange(); 6279 } 6280 return true; 6281 } 6282 6283 /// Ignore parenthesizes, implicit casts, copy constructor and return the 6284 /// variable (which may be the loop variable) if possible. 6285 static const ValueDecl *getInitLCDecl(const Expr *E) { 6286 if (!E) 6287 return nullptr; 6288 E = getExprAsWritten(E); 6289 if (const auto *CE = dyn_cast_or_null<CXXConstructExpr>(E)) 6290 if (const CXXConstructorDecl *Ctor = CE->getConstructor()) 6291 if ((Ctor->isCopyOrMoveConstructor() || 6292 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && 6293 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) 6294 E = CE->getArg(0)->IgnoreParenImpCasts(); 6295 if (const auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) { 6296 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) 6297 return getCanonicalDecl(VD); 6298 } 6299 if (const auto *ME = dyn_cast_or_null<MemberExpr>(E)) 6300 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 6301 return getCanonicalDecl(ME->getMemberDecl()); 6302 return nullptr; 6303 } 6304 6305 bool OpenMPIterationSpaceChecker::checkAndSetCond(Expr *S) { 6306 // Check test-expr for canonical form, save upper-bound UB, flags for 6307 // less/greater and for strict/non-strict comparison. 6308 // OpenMP [2.9] Canonical loop form. Test-expr may be one of the following: 6309 // var relational-op b 6310 // b relational-op var 6311 // 6312 bool IneqCondIsCanonical = SemaRef.getLangOpts().OpenMP >= 50; 6313 if (!S) { 6314 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) 6315 << (IneqCondIsCanonical ? 1 : 0) << LCDecl; 6316 return true; 6317 } 6318 Condition = S; 6319 S = getExprAsWritten(S); 6320 SourceLocation CondLoc = S->getBeginLoc(); 6321 if (auto *BO = dyn_cast<BinaryOperator>(S)) { 6322 if (BO->isRelationalOp()) { 6323 if (getInitLCDecl(BO->getLHS()) == LCDecl) 6324 return setUB(BO->getRHS(), 6325 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE), 6326 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT), 6327 BO->getSourceRange(), BO->getOperatorLoc()); 6328 if (getInitLCDecl(BO->getRHS()) == LCDecl) 6329 return setUB(BO->getLHS(), 6330 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE), 6331 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT), 6332 BO->getSourceRange(), BO->getOperatorLoc()); 6333 } else if (IneqCondIsCanonical && BO->getOpcode() == BO_NE) 6334 return setUB( 6335 getInitLCDecl(BO->getLHS()) == LCDecl ? BO->getRHS() : BO->getLHS(), 6336 /*LessOp=*/llvm::None, 6337 /*StrictOp=*/true, BO->getSourceRange(), BO->getOperatorLoc()); 6338 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 6339 if (CE->getNumArgs() == 2) { 6340 auto Op = CE->getOperator(); 6341 switch (Op) { 6342 case OO_Greater: 6343 case OO_GreaterEqual: 6344 case OO_Less: 6345 case OO_LessEqual: 6346 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 6347 return setUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual, 6348 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(), 6349 CE->getOperatorLoc()); 6350 if (getInitLCDecl(CE->getArg(1)) == LCDecl) 6351 return setUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual, 6352 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(), 6353 CE->getOperatorLoc()); 6354 break; 6355 case OO_ExclaimEqual: 6356 if (IneqCondIsCanonical) 6357 return setUB(getInitLCDecl(CE->getArg(0)) == LCDecl ? CE->getArg(1) 6358 : CE->getArg(0), 6359 /*LessOp=*/llvm::None, 6360 /*StrictOp=*/true, CE->getSourceRange(), 6361 CE->getOperatorLoc()); 6362 break; 6363 default: 6364 break; 6365 } 6366 } 6367 } 6368 if (dependent() || SemaRef.CurContext->isDependentContext()) 6369 return false; 6370 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond) 6371 << (IneqCondIsCanonical ? 1 : 0) << S->getSourceRange() << LCDecl; 6372 return true; 6373 } 6374 6375 bool OpenMPIterationSpaceChecker::checkAndSetIncRHS(Expr *RHS) { 6376 // RHS of canonical loop form increment can be: 6377 // var + incr 6378 // incr + var 6379 // var - incr 6380 // 6381 RHS = RHS->IgnoreParenImpCasts(); 6382 if (auto *BO = dyn_cast<BinaryOperator>(RHS)) { 6383 if (BO->isAdditiveOp()) { 6384 bool IsAdd = BO->getOpcode() == BO_Add; 6385 if (getInitLCDecl(BO->getLHS()) == LCDecl) 6386 return setStep(BO->getRHS(), !IsAdd); 6387 if (IsAdd && getInitLCDecl(BO->getRHS()) == LCDecl) 6388 return setStep(BO->getLHS(), /*Subtract=*/false); 6389 } 6390 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) { 6391 bool IsAdd = CE->getOperator() == OO_Plus; 6392 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) { 6393 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 6394 return setStep(CE->getArg(1), !IsAdd); 6395 if (IsAdd && getInitLCDecl(CE->getArg(1)) == LCDecl) 6396 return setStep(CE->getArg(0), /*Subtract=*/false); 6397 } 6398 } 6399 if (dependent() || SemaRef.CurContext->isDependentContext()) 6400 return false; 6401 SemaRef.Diag(RHS->getBeginLoc(), diag::err_omp_loop_not_canonical_incr) 6402 << RHS->getSourceRange() << LCDecl; 6403 return true; 6404 } 6405 6406 bool OpenMPIterationSpaceChecker::checkAndSetInc(Expr *S) { 6407 // Check incr-expr for canonical loop form and return true if it 6408 // does not conform. 6409 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following: 6410 // ++var 6411 // var++ 6412 // --var 6413 // var-- 6414 // var += incr 6415 // var -= incr 6416 // var = var + incr 6417 // var = incr + var 6418 // var = var - incr 6419 // 6420 if (!S) { 6421 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl; 6422 return true; 6423 } 6424 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S)) 6425 if (!ExprTemp->cleanupsHaveSideEffects()) 6426 S = ExprTemp->getSubExpr(); 6427 6428 IncrementSrcRange = S->getSourceRange(); 6429 S = S->IgnoreParens(); 6430 if (auto *UO = dyn_cast<UnaryOperator>(S)) { 6431 if (UO->isIncrementDecrementOp() && 6432 getInitLCDecl(UO->getSubExpr()) == LCDecl) 6433 return setStep(SemaRef 6434 .ActOnIntegerConstant(UO->getBeginLoc(), 6435 (UO->isDecrementOp() ? -1 : 1)) 6436 .get(), 6437 /*Subtract=*/false); 6438 } else if (auto *BO = dyn_cast<BinaryOperator>(S)) { 6439 switch (BO->getOpcode()) { 6440 case BO_AddAssign: 6441 case BO_SubAssign: 6442 if (getInitLCDecl(BO->getLHS()) == LCDecl) 6443 return setStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign); 6444 break; 6445 case BO_Assign: 6446 if (getInitLCDecl(BO->getLHS()) == LCDecl) 6447 return checkAndSetIncRHS(BO->getRHS()); 6448 break; 6449 default: 6450 break; 6451 } 6452 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 6453 switch (CE->getOperator()) { 6454 case OO_PlusPlus: 6455 case OO_MinusMinus: 6456 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 6457 return setStep(SemaRef 6458 .ActOnIntegerConstant( 6459 CE->getBeginLoc(), 6460 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)) 6461 .get(), 6462 /*Subtract=*/false); 6463 break; 6464 case OO_PlusEqual: 6465 case OO_MinusEqual: 6466 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 6467 return setStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual); 6468 break; 6469 case OO_Equal: 6470 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 6471 return checkAndSetIncRHS(CE->getArg(1)); 6472 break; 6473 default: 6474 break; 6475 } 6476 } 6477 if (dependent() || SemaRef.CurContext->isDependentContext()) 6478 return false; 6479 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_incr) 6480 << S->getSourceRange() << LCDecl; 6481 return true; 6482 } 6483 6484 static ExprResult 6485 tryBuildCapture(Sema &SemaRef, Expr *Capture, 6486 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 6487 if (SemaRef.CurContext->isDependentContext()) 6488 return ExprResult(Capture); 6489 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects)) 6490 return SemaRef.PerformImplicitConversion( 6491 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting, 6492 /*AllowExplicit=*/true); 6493 auto I = Captures.find(Capture); 6494 if (I != Captures.end()) 6495 return buildCapture(SemaRef, Capture, I->second); 6496 DeclRefExpr *Ref = nullptr; 6497 ExprResult Res = buildCapture(SemaRef, Capture, Ref); 6498 Captures[Capture] = Ref; 6499 return Res; 6500 } 6501 6502 /// Build the expression to calculate the number of iterations. 6503 Expr *OpenMPIterationSpaceChecker::buildNumIterations( 6504 Scope *S, ArrayRef<LoopIterationSpace> ResultIterSpaces, bool LimitedType, 6505 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 6506 ExprResult Diff; 6507 QualType VarType = LCDecl->getType().getNonReferenceType(); 6508 if (VarType->isIntegerType() || VarType->isPointerType() || 6509 SemaRef.getLangOpts().CPlusPlus) { 6510 Expr *LBVal = LB; 6511 Expr *UBVal = UB; 6512 // LB = TestIsLessOp.getValue() ? min(LB(MinVal), LB(MaxVal)) : 6513 // max(LB(MinVal), LB(MaxVal)) 6514 if (InitDependOnLC) { 6515 const LoopIterationSpace &IS = 6516 ResultIterSpaces[ResultIterSpaces.size() - 1 - 6517 InitDependOnLC.getValueOr( 6518 CondDependOnLC.getValueOr(0))]; 6519 if (!IS.MinValue || !IS.MaxValue) 6520 return nullptr; 6521 // OuterVar = Min 6522 ExprResult MinValue = 6523 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MinValue); 6524 if (!MinValue.isUsable()) 6525 return nullptr; 6526 6527 ExprResult LBMinVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign, 6528 IS.CounterVar, MinValue.get()); 6529 if (!LBMinVal.isUsable()) 6530 return nullptr; 6531 // OuterVar = Min, LBVal 6532 LBMinVal = 6533 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, LBMinVal.get(), LBVal); 6534 if (!LBMinVal.isUsable()) 6535 return nullptr; 6536 // (OuterVar = Min, LBVal) 6537 LBMinVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, LBMinVal.get()); 6538 if (!LBMinVal.isUsable()) 6539 return nullptr; 6540 6541 // OuterVar = Max 6542 ExprResult MaxValue = 6543 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MaxValue); 6544 if (!MaxValue.isUsable()) 6545 return nullptr; 6546 6547 ExprResult LBMaxVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign, 6548 IS.CounterVar, MaxValue.get()); 6549 if (!LBMaxVal.isUsable()) 6550 return nullptr; 6551 // OuterVar = Max, LBVal 6552 LBMaxVal = 6553 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, LBMaxVal.get(), LBVal); 6554 if (!LBMaxVal.isUsable()) 6555 return nullptr; 6556 // (OuterVar = Max, LBVal) 6557 LBMaxVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, LBMaxVal.get()); 6558 if (!LBMaxVal.isUsable()) 6559 return nullptr; 6560 6561 Expr *LBMin = tryBuildCapture(SemaRef, LBMinVal.get(), Captures).get(); 6562 Expr *LBMax = tryBuildCapture(SemaRef, LBMaxVal.get(), Captures).get(); 6563 if (!LBMin || !LBMax) 6564 return nullptr; 6565 // LB(MinVal) < LB(MaxVal) 6566 ExprResult MinLessMaxRes = 6567 SemaRef.BuildBinOp(S, DefaultLoc, BO_LT, LBMin, LBMax); 6568 if (!MinLessMaxRes.isUsable()) 6569 return nullptr; 6570 Expr *MinLessMax = 6571 tryBuildCapture(SemaRef, MinLessMaxRes.get(), Captures).get(); 6572 if (!MinLessMax) 6573 return nullptr; 6574 if (TestIsLessOp.getValue()) { 6575 // LB(MinVal) < LB(MaxVal) ? LB(MinVal) : LB(MaxVal) - min(LB(MinVal), 6576 // LB(MaxVal)) 6577 ExprResult MinLB = SemaRef.ActOnConditionalOp(DefaultLoc, DefaultLoc, 6578 MinLessMax, LBMin, LBMax); 6579 if (!MinLB.isUsable()) 6580 return nullptr; 6581 LBVal = MinLB.get(); 6582 } else { 6583 // LB(MinVal) < LB(MaxVal) ? LB(MaxVal) : LB(MinVal) - max(LB(MinVal), 6584 // LB(MaxVal)) 6585 ExprResult MaxLB = SemaRef.ActOnConditionalOp(DefaultLoc, DefaultLoc, 6586 MinLessMax, LBMax, LBMin); 6587 if (!MaxLB.isUsable()) 6588 return nullptr; 6589 LBVal = MaxLB.get(); 6590 } 6591 } 6592 // UB = TestIsLessOp.getValue() ? max(UB(MinVal), UB(MaxVal)) : 6593 // min(UB(MinVal), UB(MaxVal)) 6594 if (CondDependOnLC) { 6595 const LoopIterationSpace &IS = 6596 ResultIterSpaces[ResultIterSpaces.size() - 1 - 6597 InitDependOnLC.getValueOr( 6598 CondDependOnLC.getValueOr(0))]; 6599 if (!IS.MinValue || !IS.MaxValue) 6600 return nullptr; 6601 // OuterVar = Min 6602 ExprResult MinValue = 6603 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MinValue); 6604 if (!MinValue.isUsable()) 6605 return nullptr; 6606 6607 ExprResult UBMinVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign, 6608 IS.CounterVar, MinValue.get()); 6609 if (!UBMinVal.isUsable()) 6610 return nullptr; 6611 // OuterVar = Min, UBVal 6612 UBMinVal = 6613 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, UBMinVal.get(), UBVal); 6614 if (!UBMinVal.isUsable()) 6615 return nullptr; 6616 // (OuterVar = Min, UBVal) 6617 UBMinVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, UBMinVal.get()); 6618 if (!UBMinVal.isUsable()) 6619 return nullptr; 6620 6621 // OuterVar = Max 6622 ExprResult MaxValue = 6623 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MaxValue); 6624 if (!MaxValue.isUsable()) 6625 return nullptr; 6626 6627 ExprResult UBMaxVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign, 6628 IS.CounterVar, MaxValue.get()); 6629 if (!UBMaxVal.isUsable()) 6630 return nullptr; 6631 // OuterVar = Max, UBVal 6632 UBMaxVal = 6633 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, UBMaxVal.get(), UBVal); 6634 if (!UBMaxVal.isUsable()) 6635 return nullptr; 6636 // (OuterVar = Max, UBVal) 6637 UBMaxVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, UBMaxVal.get()); 6638 if (!UBMaxVal.isUsable()) 6639 return nullptr; 6640 6641 Expr *UBMin = tryBuildCapture(SemaRef, UBMinVal.get(), Captures).get(); 6642 Expr *UBMax = tryBuildCapture(SemaRef, UBMaxVal.get(), Captures).get(); 6643 if (!UBMin || !UBMax) 6644 return nullptr; 6645 // UB(MinVal) > UB(MaxVal) 6646 ExprResult MinGreaterMaxRes = 6647 SemaRef.BuildBinOp(S, DefaultLoc, BO_GT, UBMin, UBMax); 6648 if (!MinGreaterMaxRes.isUsable()) 6649 return nullptr; 6650 Expr *MinGreaterMax = 6651 tryBuildCapture(SemaRef, MinGreaterMaxRes.get(), Captures).get(); 6652 if (!MinGreaterMax) 6653 return nullptr; 6654 if (TestIsLessOp.getValue()) { 6655 // UB(MinVal) > UB(MaxVal) ? UB(MinVal) : UB(MaxVal) - max(UB(MinVal), 6656 // UB(MaxVal)) 6657 ExprResult MaxUB = SemaRef.ActOnConditionalOp( 6658 DefaultLoc, DefaultLoc, MinGreaterMax, UBMin, UBMax); 6659 if (!MaxUB.isUsable()) 6660 return nullptr; 6661 UBVal = MaxUB.get(); 6662 } else { 6663 // UB(MinVal) > UB(MaxVal) ? UB(MaxVal) : UB(MinVal) - min(UB(MinVal), 6664 // UB(MaxVal)) 6665 ExprResult MinUB = SemaRef.ActOnConditionalOp( 6666 DefaultLoc, DefaultLoc, MinGreaterMax, UBMax, UBMin); 6667 if (!MinUB.isUsable()) 6668 return nullptr; 6669 UBVal = MinUB.get(); 6670 } 6671 } 6672 // Upper - Lower 6673 Expr *UBExpr = TestIsLessOp.getValue() ? UBVal : LBVal; 6674 Expr *LBExpr = TestIsLessOp.getValue() ? LBVal : UBVal; 6675 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get(); 6676 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get(); 6677 if (!Upper || !Lower) 6678 return nullptr; 6679 6680 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower); 6681 6682 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) { 6683 // BuildBinOp already emitted error, this one is to point user to upper 6684 // and lower bound, and to tell what is passed to 'operator-'. 6685 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx) 6686 << Upper->getSourceRange() << Lower->getSourceRange(); 6687 return nullptr; 6688 } 6689 } 6690 6691 if (!Diff.isUsable()) 6692 return nullptr; 6693 6694 // Upper - Lower [- 1] 6695 if (TestIsStrictOp) 6696 Diff = SemaRef.BuildBinOp( 6697 S, DefaultLoc, BO_Sub, Diff.get(), 6698 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 6699 if (!Diff.isUsable()) 6700 return nullptr; 6701 6702 // Upper - Lower [- 1] + Step 6703 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures); 6704 if (!NewStep.isUsable()) 6705 return nullptr; 6706 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get()); 6707 if (!Diff.isUsable()) 6708 return nullptr; 6709 6710 // Parentheses (for dumping/debugging purposes only). 6711 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 6712 if (!Diff.isUsable()) 6713 return nullptr; 6714 6715 // (Upper - Lower [- 1] + Step) / Step 6716 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get()); 6717 if (!Diff.isUsable()) 6718 return nullptr; 6719 6720 // OpenMP runtime requires 32-bit or 64-bit loop variables. 6721 QualType Type = Diff.get()->getType(); 6722 ASTContext &C = SemaRef.Context; 6723 bool UseVarType = VarType->hasIntegerRepresentation() && 6724 C.getTypeSize(Type) > C.getTypeSize(VarType); 6725 if (!Type->isIntegerType() || UseVarType) { 6726 unsigned NewSize = 6727 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type); 6728 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation() 6729 : Type->hasSignedIntegerRepresentation(); 6730 Type = C.getIntTypeForBitwidth(NewSize, IsSigned); 6731 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) { 6732 Diff = SemaRef.PerformImplicitConversion( 6733 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true); 6734 if (!Diff.isUsable()) 6735 return nullptr; 6736 } 6737 } 6738 if (LimitedType) { 6739 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32; 6740 if (NewSize != C.getTypeSize(Type)) { 6741 if (NewSize < C.getTypeSize(Type)) { 6742 assert(NewSize == 64 && "incorrect loop var size"); 6743 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var) 6744 << InitSrcRange << ConditionSrcRange; 6745 } 6746 QualType NewType = C.getIntTypeForBitwidth( 6747 NewSize, Type->hasSignedIntegerRepresentation() || 6748 C.getTypeSize(Type) < NewSize); 6749 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) { 6750 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType, 6751 Sema::AA_Converting, true); 6752 if (!Diff.isUsable()) 6753 return nullptr; 6754 } 6755 } 6756 } 6757 6758 return Diff.get(); 6759 } 6760 6761 std::pair<Expr *, Expr *> OpenMPIterationSpaceChecker::buildMinMaxValues( 6762 Scope *S, llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 6763 // Do not build for iterators, they cannot be used in non-rectangular loop 6764 // nests. 6765 if (LCDecl->getType()->isRecordType()) 6766 return std::make_pair(nullptr, nullptr); 6767 // If we subtract, the min is in the condition, otherwise the min is in the 6768 // init value. 6769 Expr *MinExpr = nullptr; 6770 Expr *MaxExpr = nullptr; 6771 Expr *LBExpr = TestIsLessOp.getValue() ? LB : UB; 6772 Expr *UBExpr = TestIsLessOp.getValue() ? UB : LB; 6773 bool LBNonRect = TestIsLessOp.getValue() ? InitDependOnLC.hasValue() 6774 : CondDependOnLC.hasValue(); 6775 bool UBNonRect = TestIsLessOp.getValue() ? CondDependOnLC.hasValue() 6776 : InitDependOnLC.hasValue(); 6777 Expr *Lower = 6778 LBNonRect ? LBExpr : tryBuildCapture(SemaRef, LBExpr, Captures).get(); 6779 Expr *Upper = 6780 UBNonRect ? UBExpr : tryBuildCapture(SemaRef, UBExpr, Captures).get(); 6781 if (!Upper || !Lower) 6782 return std::make_pair(nullptr, nullptr); 6783 6784 if (TestIsLessOp.getValue()) 6785 MinExpr = Lower; 6786 else 6787 MaxExpr = Upper; 6788 6789 // Build minimum/maximum value based on number of iterations. 6790 ExprResult Diff; 6791 QualType VarType = LCDecl->getType().getNonReferenceType(); 6792 6793 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower); 6794 if (!Diff.isUsable()) 6795 return std::make_pair(nullptr, nullptr); 6796 6797 // Upper - Lower [- 1] 6798 if (TestIsStrictOp) 6799 Diff = SemaRef.BuildBinOp( 6800 S, DefaultLoc, BO_Sub, Diff.get(), 6801 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 6802 if (!Diff.isUsable()) 6803 return std::make_pair(nullptr, nullptr); 6804 6805 // Upper - Lower [- 1] + Step 6806 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures); 6807 if (!NewStep.isUsable()) 6808 return std::make_pair(nullptr, nullptr); 6809 6810 // Parentheses (for dumping/debugging purposes only). 6811 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 6812 if (!Diff.isUsable()) 6813 return std::make_pair(nullptr, nullptr); 6814 6815 // (Upper - Lower [- 1]) / Step 6816 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get()); 6817 if (!Diff.isUsable()) 6818 return std::make_pair(nullptr, nullptr); 6819 6820 // ((Upper - Lower [- 1]) / Step) * Step 6821 // Parentheses (for dumping/debugging purposes only). 6822 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 6823 if (!Diff.isUsable()) 6824 return std::make_pair(nullptr, nullptr); 6825 6826 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Mul, Diff.get(), NewStep.get()); 6827 if (!Diff.isUsable()) 6828 return std::make_pair(nullptr, nullptr); 6829 6830 // Convert to the original type or ptrdiff_t, if original type is pointer. 6831 if (!VarType->isAnyPointerType() && 6832 !SemaRef.Context.hasSameType(Diff.get()->getType(), VarType)) { 6833 Diff = SemaRef.PerformImplicitConversion( 6834 Diff.get(), VarType, Sema::AA_Converting, /*AllowExplicit=*/true); 6835 } else if (VarType->isAnyPointerType() && 6836 !SemaRef.Context.hasSameType( 6837 Diff.get()->getType(), 6838 SemaRef.Context.getUnsignedPointerDiffType())) { 6839 Diff = SemaRef.PerformImplicitConversion( 6840 Diff.get(), SemaRef.Context.getUnsignedPointerDiffType(), 6841 Sema::AA_Converting, /*AllowExplicit=*/true); 6842 } 6843 if (!Diff.isUsable()) 6844 return std::make_pair(nullptr, nullptr); 6845 6846 // Parentheses (for dumping/debugging purposes only). 6847 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 6848 if (!Diff.isUsable()) 6849 return std::make_pair(nullptr, nullptr); 6850 6851 if (TestIsLessOp.getValue()) { 6852 // MinExpr = Lower; 6853 // MaxExpr = Lower + (((Upper - Lower [- 1]) / Step) * Step) 6854 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Lower, Diff.get()); 6855 if (!Diff.isUsable()) 6856 return std::make_pair(nullptr, nullptr); 6857 Diff = SemaRef.ActOnFinishFullExpr(Diff.get(), /*DiscardedValue*/ false); 6858 if (!Diff.isUsable()) 6859 return std::make_pair(nullptr, nullptr); 6860 MaxExpr = Diff.get(); 6861 } else { 6862 // MaxExpr = Upper; 6863 // MinExpr = Upper - (((Upper - Lower [- 1]) / Step) * Step) 6864 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Diff.get()); 6865 if (!Diff.isUsable()) 6866 return std::make_pair(nullptr, nullptr); 6867 Diff = SemaRef.ActOnFinishFullExpr(Diff.get(), /*DiscardedValue*/ false); 6868 if (!Diff.isUsable()) 6869 return std::make_pair(nullptr, nullptr); 6870 MinExpr = Diff.get(); 6871 } 6872 6873 return std::make_pair(MinExpr, MaxExpr); 6874 } 6875 6876 Expr *OpenMPIterationSpaceChecker::buildFinalCondition(Scope *S) const { 6877 if (InitDependOnLC || CondDependOnLC) 6878 return Condition; 6879 return nullptr; 6880 } 6881 6882 Expr *OpenMPIterationSpaceChecker::buildPreCond( 6883 Scope *S, Expr *Cond, 6884 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 6885 // Do not build a precondition when the condition/initialization is dependent 6886 // to prevent pessimistic early loop exit. 6887 // TODO: this can be improved by calculating min/max values but not sure that 6888 // it will be very effective. 6889 if (CondDependOnLC || InitDependOnLC) 6890 return SemaRef.PerformImplicitConversion( 6891 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(), 6892 SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting, 6893 /*AllowExplicit=*/true).get(); 6894 6895 // Try to build LB <op> UB, where <op> is <, >, <=, or >=. 6896 Sema::TentativeAnalysisScope Trap(SemaRef); 6897 6898 ExprResult NewLB = tryBuildCapture(SemaRef, LB, Captures); 6899 ExprResult NewUB = tryBuildCapture(SemaRef, UB, Captures); 6900 if (!NewLB.isUsable() || !NewUB.isUsable()) 6901 return nullptr; 6902 6903 ExprResult CondExpr = 6904 SemaRef.BuildBinOp(S, DefaultLoc, 6905 TestIsLessOp.getValue() ? 6906 (TestIsStrictOp ? BO_LT : BO_LE) : 6907 (TestIsStrictOp ? BO_GT : BO_GE), 6908 NewLB.get(), NewUB.get()); 6909 if (CondExpr.isUsable()) { 6910 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(), 6911 SemaRef.Context.BoolTy)) 6912 CondExpr = SemaRef.PerformImplicitConversion( 6913 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting, 6914 /*AllowExplicit=*/true); 6915 } 6916 6917 // Otherwise use original loop condition and evaluate it in runtime. 6918 return CondExpr.isUsable() ? CondExpr.get() : Cond; 6919 } 6920 6921 /// Build reference expression to the counter be used for codegen. 6922 DeclRefExpr *OpenMPIterationSpaceChecker::buildCounterVar( 6923 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 6924 DSAStackTy &DSA) const { 6925 auto *VD = dyn_cast<VarDecl>(LCDecl); 6926 if (!VD) { 6927 VD = SemaRef.isOpenMPCapturedDecl(LCDecl); 6928 DeclRefExpr *Ref = buildDeclRefExpr( 6929 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc); 6930 const DSAStackTy::DSAVarData Data = 6931 DSA.getTopDSA(LCDecl, /*FromParent=*/false); 6932 // If the loop control decl is explicitly marked as private, do not mark it 6933 // as captured again. 6934 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr) 6935 Captures.insert(std::make_pair(LCRef, Ref)); 6936 return Ref; 6937 } 6938 return cast<DeclRefExpr>(LCRef); 6939 } 6940 6941 Expr *OpenMPIterationSpaceChecker::buildPrivateCounterVar() const { 6942 if (LCDecl && !LCDecl->isInvalidDecl()) { 6943 QualType Type = LCDecl->getType().getNonReferenceType(); 6944 VarDecl *PrivateVar = buildVarDecl( 6945 SemaRef, DefaultLoc, Type, LCDecl->getName(), 6946 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr, 6947 isa<VarDecl>(LCDecl) 6948 ? buildDeclRefExpr(SemaRef, cast<VarDecl>(LCDecl), Type, DefaultLoc) 6949 : nullptr); 6950 if (PrivateVar->isInvalidDecl()) 6951 return nullptr; 6952 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc); 6953 } 6954 return nullptr; 6955 } 6956 6957 /// Build initialization of the counter to be used for codegen. 6958 Expr *OpenMPIterationSpaceChecker::buildCounterInit() const { return LB; } 6959 6960 /// Build step of the counter be used for codegen. 6961 Expr *OpenMPIterationSpaceChecker::buildCounterStep() const { return Step; } 6962 6963 Expr *OpenMPIterationSpaceChecker::buildOrderedLoopData( 6964 Scope *S, Expr *Counter, 6965 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, SourceLocation Loc, 6966 Expr *Inc, OverloadedOperatorKind OOK) { 6967 Expr *Cnt = SemaRef.DefaultLvalueConversion(Counter).get(); 6968 if (!Cnt) 6969 return nullptr; 6970 if (Inc) { 6971 assert((OOK == OO_Plus || OOK == OO_Minus) && 6972 "Expected only + or - operations for depend clauses."); 6973 BinaryOperatorKind BOK = (OOK == OO_Plus) ? BO_Add : BO_Sub; 6974 Cnt = SemaRef.BuildBinOp(S, Loc, BOK, Cnt, Inc).get(); 6975 if (!Cnt) 6976 return nullptr; 6977 } 6978 ExprResult Diff; 6979 QualType VarType = LCDecl->getType().getNonReferenceType(); 6980 if (VarType->isIntegerType() || VarType->isPointerType() || 6981 SemaRef.getLangOpts().CPlusPlus) { 6982 // Upper - Lower 6983 Expr *Upper = TestIsLessOp.getValue() 6984 ? Cnt 6985 : tryBuildCapture(SemaRef, LB, Captures).get(); 6986 Expr *Lower = TestIsLessOp.getValue() 6987 ? tryBuildCapture(SemaRef, LB, Captures).get() 6988 : Cnt; 6989 if (!Upper || !Lower) 6990 return nullptr; 6991 6992 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower); 6993 6994 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) { 6995 // BuildBinOp already emitted error, this one is to point user to upper 6996 // and lower bound, and to tell what is passed to 'operator-'. 6997 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx) 6998 << Upper->getSourceRange() << Lower->getSourceRange(); 6999 return nullptr; 7000 } 7001 } 7002 7003 if (!Diff.isUsable()) 7004 return nullptr; 7005 7006 // Parentheses (for dumping/debugging purposes only). 7007 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 7008 if (!Diff.isUsable()) 7009 return nullptr; 7010 7011 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures); 7012 if (!NewStep.isUsable()) 7013 return nullptr; 7014 // (Upper - Lower) / Step 7015 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get()); 7016 if (!Diff.isUsable()) 7017 return nullptr; 7018 7019 return Diff.get(); 7020 } 7021 } // namespace 7022 7023 void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) { 7024 assert(getLangOpts().OpenMP && "OpenMP is not active."); 7025 assert(Init && "Expected loop in canonical form."); 7026 unsigned AssociatedLoops = DSAStack->getAssociatedLoops(); 7027 if (AssociatedLoops > 0 && 7028 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { 7029 DSAStack->loopStart(); 7030 OpenMPIterationSpaceChecker ISC(*this, *DSAStack, ForLoc); 7031 if (!ISC.checkAndSetInit(Init, /*EmitDiags=*/false)) { 7032 if (ValueDecl *D = ISC.getLoopDecl()) { 7033 auto *VD = dyn_cast<VarDecl>(D); 7034 DeclRefExpr *PrivateRef = nullptr; 7035 if (!VD) { 7036 if (VarDecl *Private = isOpenMPCapturedDecl(D)) { 7037 VD = Private; 7038 } else { 7039 PrivateRef = buildCapture(*this, D, ISC.getLoopDeclRefExpr(), 7040 /*WithInit=*/false); 7041 VD = cast<VarDecl>(PrivateRef->getDecl()); 7042 } 7043 } 7044 DSAStack->addLoopControlVariable(D, VD); 7045 const Decl *LD = DSAStack->getPossiblyLoopCunter(); 7046 if (LD != D->getCanonicalDecl()) { 7047 DSAStack->resetPossibleLoopCounter(); 7048 if (auto *Var = dyn_cast_or_null<VarDecl>(LD)) 7049 MarkDeclarationsReferencedInExpr( 7050 buildDeclRefExpr(*this, const_cast<VarDecl *>(Var), 7051 Var->getType().getNonLValueExprType(Context), 7052 ForLoc, /*RefersToCapture=*/true)); 7053 } 7054 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 7055 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables 7056 // Referenced in a Construct, C/C++]. The loop iteration variable in the 7057 // associated for-loop of a simd construct with just one associated 7058 // for-loop may be listed in a linear clause with a constant-linear-step 7059 // that is the increment of the associated for-loop. The loop iteration 7060 // variable(s) in the associated for-loop(s) of a for or parallel for 7061 // construct may be listed in a private or lastprivate clause. 7062 DSAStackTy::DSAVarData DVar = 7063 DSAStack->getTopDSA(D, /*FromParent=*/false); 7064 // If LoopVarRefExpr is nullptr it means the corresponding loop variable 7065 // is declared in the loop and it is predetermined as a private. 7066 Expr *LoopDeclRefExpr = ISC.getLoopDeclRefExpr(); 7067 OpenMPClauseKind PredeterminedCKind = 7068 isOpenMPSimdDirective(DKind) 7069 ? (DSAStack->hasMutipleLoops() ? OMPC_lastprivate : OMPC_linear) 7070 : OMPC_private; 7071 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && 7072 DVar.CKind != PredeterminedCKind && DVar.RefExpr && 7073 (LangOpts.OpenMP <= 45 || (DVar.CKind != OMPC_lastprivate && 7074 DVar.CKind != OMPC_private))) || 7075 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop || 7076 DKind == OMPD_master_taskloop || 7077 DKind == OMPD_parallel_master_taskloop || 7078 isOpenMPDistributeDirective(DKind)) && 7079 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && 7080 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) && 7081 (DVar.CKind != OMPC_private || DVar.RefExpr)) { 7082 Diag(Init->getBeginLoc(), diag::err_omp_loop_var_dsa) 7083 << getOpenMPClauseName(DVar.CKind) 7084 << getOpenMPDirectiveName(DKind) 7085 << getOpenMPClauseName(PredeterminedCKind); 7086 if (DVar.RefExpr == nullptr) 7087 DVar.CKind = PredeterminedCKind; 7088 reportOriginalDsa(*this, DSAStack, D, DVar, 7089 /*IsLoopIterVar=*/true); 7090 } else if (LoopDeclRefExpr) { 7091 // Make the loop iteration variable private (for worksharing 7092 // constructs), linear (for simd directives with the only one 7093 // associated loop) or lastprivate (for simd directives with several 7094 // collapsed or ordered loops). 7095 if (DVar.CKind == OMPC_unknown) 7096 DSAStack->addDSA(D, LoopDeclRefExpr, PredeterminedCKind, 7097 PrivateRef); 7098 } 7099 } 7100 } 7101 DSAStack->setAssociatedLoops(AssociatedLoops - 1); 7102 } 7103 } 7104 7105 /// Called on a for stmt to check and extract its iteration space 7106 /// for further processing (such as collapsing). 7107 static bool checkOpenMPIterationSpace( 7108 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA, 7109 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount, 7110 unsigned TotalNestedLoopCount, Expr *CollapseLoopCountExpr, 7111 Expr *OrderedLoopCountExpr, 7112 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA, 7113 llvm::MutableArrayRef<LoopIterationSpace> ResultIterSpaces, 7114 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 7115 // OpenMP [2.9.1, Canonical Loop Form] 7116 // for (init-expr; test-expr; incr-expr) structured-block 7117 // for (range-decl: range-expr) structured-block 7118 auto *For = dyn_cast_or_null<ForStmt>(S); 7119 auto *CXXFor = dyn_cast_or_null<CXXForRangeStmt>(S); 7120 // Ranged for is supported only in OpenMP 5.0. 7121 if (!For && (SemaRef.LangOpts.OpenMP <= 45 || !CXXFor)) { 7122 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_not_for) 7123 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr) 7124 << getOpenMPDirectiveName(DKind) << TotalNestedLoopCount 7125 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount; 7126 if (TotalNestedLoopCount > 1) { 7127 if (CollapseLoopCountExpr && OrderedLoopCountExpr) 7128 SemaRef.Diag(DSA.getConstructLoc(), 7129 diag::note_omp_collapse_ordered_expr) 7130 << 2 << CollapseLoopCountExpr->getSourceRange() 7131 << OrderedLoopCountExpr->getSourceRange(); 7132 else if (CollapseLoopCountExpr) 7133 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), 7134 diag::note_omp_collapse_ordered_expr) 7135 << 0 << CollapseLoopCountExpr->getSourceRange(); 7136 else 7137 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), 7138 diag::note_omp_collapse_ordered_expr) 7139 << 1 << OrderedLoopCountExpr->getSourceRange(); 7140 } 7141 return true; 7142 } 7143 assert(((For && For->getBody()) || (CXXFor && CXXFor->getBody())) && 7144 "No loop body."); 7145 7146 OpenMPIterationSpaceChecker ISC(SemaRef, DSA, 7147 For ? For->getForLoc() : CXXFor->getForLoc()); 7148 7149 // Check init. 7150 Stmt *Init = For ? For->getInit() : CXXFor->getBeginStmt(); 7151 if (ISC.checkAndSetInit(Init)) 7152 return true; 7153 7154 bool HasErrors = false; 7155 7156 // Check loop variable's type. 7157 if (ValueDecl *LCDecl = ISC.getLoopDecl()) { 7158 // OpenMP [2.6, Canonical Loop Form] 7159 // Var is one of the following: 7160 // A variable of signed or unsigned integer type. 7161 // For C++, a variable of a random access iterator type. 7162 // For C, a variable of a pointer type. 7163 QualType VarType = LCDecl->getType().getNonReferenceType(); 7164 if (!VarType->isDependentType() && !VarType->isIntegerType() && 7165 !VarType->isPointerType() && 7166 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) { 7167 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_variable_type) 7168 << SemaRef.getLangOpts().CPlusPlus; 7169 HasErrors = true; 7170 } 7171 7172 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in 7173 // a Construct 7174 // The loop iteration variable(s) in the associated for-loop(s) of a for or 7175 // parallel for construct is (are) private. 7176 // The loop iteration variable in the associated for-loop of a simd 7177 // construct with just one associated for-loop is linear with a 7178 // constant-linear-step that is the increment of the associated for-loop. 7179 // Exclude loop var from the list of variables with implicitly defined data 7180 // sharing attributes. 7181 VarsWithImplicitDSA.erase(LCDecl); 7182 7183 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars"); 7184 7185 // Check test-expr. 7186 HasErrors |= ISC.checkAndSetCond(For ? For->getCond() : CXXFor->getCond()); 7187 7188 // Check incr-expr. 7189 HasErrors |= ISC.checkAndSetInc(For ? For->getInc() : CXXFor->getInc()); 7190 } 7191 7192 if (ISC.dependent() || SemaRef.CurContext->isDependentContext() || HasErrors) 7193 return HasErrors; 7194 7195 // Build the loop's iteration space representation. 7196 ResultIterSpaces[CurrentNestedLoopCount].PreCond = ISC.buildPreCond( 7197 DSA.getCurScope(), For ? For->getCond() : CXXFor->getCond(), Captures); 7198 ResultIterSpaces[CurrentNestedLoopCount].NumIterations = 7199 ISC.buildNumIterations(DSA.getCurScope(), ResultIterSpaces, 7200 (isOpenMPWorksharingDirective(DKind) || 7201 isOpenMPTaskLoopDirective(DKind) || 7202 isOpenMPDistributeDirective(DKind)), 7203 Captures); 7204 ResultIterSpaces[CurrentNestedLoopCount].CounterVar = 7205 ISC.buildCounterVar(Captures, DSA); 7206 ResultIterSpaces[CurrentNestedLoopCount].PrivateCounterVar = 7207 ISC.buildPrivateCounterVar(); 7208 ResultIterSpaces[CurrentNestedLoopCount].CounterInit = ISC.buildCounterInit(); 7209 ResultIterSpaces[CurrentNestedLoopCount].CounterStep = ISC.buildCounterStep(); 7210 ResultIterSpaces[CurrentNestedLoopCount].InitSrcRange = ISC.getInitSrcRange(); 7211 ResultIterSpaces[CurrentNestedLoopCount].CondSrcRange = 7212 ISC.getConditionSrcRange(); 7213 ResultIterSpaces[CurrentNestedLoopCount].IncSrcRange = 7214 ISC.getIncrementSrcRange(); 7215 ResultIterSpaces[CurrentNestedLoopCount].Subtract = ISC.shouldSubtractStep(); 7216 ResultIterSpaces[CurrentNestedLoopCount].IsStrictCompare = 7217 ISC.isStrictTestOp(); 7218 std::tie(ResultIterSpaces[CurrentNestedLoopCount].MinValue, 7219 ResultIterSpaces[CurrentNestedLoopCount].MaxValue) = 7220 ISC.buildMinMaxValues(DSA.getCurScope(), Captures); 7221 ResultIterSpaces[CurrentNestedLoopCount].FinalCondition = 7222 ISC.buildFinalCondition(DSA.getCurScope()); 7223 ResultIterSpaces[CurrentNestedLoopCount].IsNonRectangularLB = 7224 ISC.doesInitDependOnLC(); 7225 ResultIterSpaces[CurrentNestedLoopCount].IsNonRectangularUB = 7226 ISC.doesCondDependOnLC(); 7227 ResultIterSpaces[CurrentNestedLoopCount].LoopDependentIdx = 7228 ISC.getLoopDependentIdx(); 7229 7230 HasErrors |= 7231 (ResultIterSpaces[CurrentNestedLoopCount].PreCond == nullptr || 7232 ResultIterSpaces[CurrentNestedLoopCount].NumIterations == nullptr || 7233 ResultIterSpaces[CurrentNestedLoopCount].CounterVar == nullptr || 7234 ResultIterSpaces[CurrentNestedLoopCount].PrivateCounterVar == nullptr || 7235 ResultIterSpaces[CurrentNestedLoopCount].CounterInit == nullptr || 7236 ResultIterSpaces[CurrentNestedLoopCount].CounterStep == nullptr); 7237 if (!HasErrors && DSA.isOrderedRegion()) { 7238 if (DSA.getOrderedRegionParam().second->getNumForLoops()) { 7239 if (CurrentNestedLoopCount < 7240 DSA.getOrderedRegionParam().second->getLoopNumIterations().size()) { 7241 DSA.getOrderedRegionParam().second->setLoopNumIterations( 7242 CurrentNestedLoopCount, 7243 ResultIterSpaces[CurrentNestedLoopCount].NumIterations); 7244 DSA.getOrderedRegionParam().second->setLoopCounter( 7245 CurrentNestedLoopCount, 7246 ResultIterSpaces[CurrentNestedLoopCount].CounterVar); 7247 } 7248 } 7249 for (auto &Pair : DSA.getDoacrossDependClauses()) { 7250 if (CurrentNestedLoopCount >= Pair.first->getNumLoops()) { 7251 // Erroneous case - clause has some problems. 7252 continue; 7253 } 7254 if (Pair.first->getDependencyKind() == OMPC_DEPEND_sink && 7255 Pair.second.size() <= CurrentNestedLoopCount) { 7256 // Erroneous case - clause has some problems. 7257 Pair.first->setLoopData(CurrentNestedLoopCount, nullptr); 7258 continue; 7259 } 7260 Expr *CntValue; 7261 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source) 7262 CntValue = ISC.buildOrderedLoopData( 7263 DSA.getCurScope(), 7264 ResultIterSpaces[CurrentNestedLoopCount].CounterVar, Captures, 7265 Pair.first->getDependencyLoc()); 7266 else 7267 CntValue = ISC.buildOrderedLoopData( 7268 DSA.getCurScope(), 7269 ResultIterSpaces[CurrentNestedLoopCount].CounterVar, Captures, 7270 Pair.first->getDependencyLoc(), 7271 Pair.second[CurrentNestedLoopCount].first, 7272 Pair.second[CurrentNestedLoopCount].second); 7273 Pair.first->setLoopData(CurrentNestedLoopCount, CntValue); 7274 } 7275 } 7276 7277 return HasErrors; 7278 } 7279 7280 /// Build 'VarRef = Start. 7281 static ExprResult 7282 buildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef, 7283 ExprResult Start, bool IsNonRectangularLB, 7284 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 7285 // Build 'VarRef = Start. 7286 ExprResult NewStart = IsNonRectangularLB 7287 ? Start.get() 7288 : tryBuildCapture(SemaRef, Start.get(), Captures); 7289 if (!NewStart.isUsable()) 7290 return ExprError(); 7291 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(), 7292 VarRef.get()->getType())) { 7293 NewStart = SemaRef.PerformImplicitConversion( 7294 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting, 7295 /*AllowExplicit=*/true); 7296 if (!NewStart.isUsable()) 7297 return ExprError(); 7298 } 7299 7300 ExprResult Init = 7301 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); 7302 return Init; 7303 } 7304 7305 /// Build 'VarRef = Start + Iter * Step'. 7306 static ExprResult buildCounterUpdate( 7307 Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef, 7308 ExprResult Start, ExprResult Iter, ExprResult Step, bool Subtract, 7309 bool IsNonRectangularLB, 7310 llvm::MapVector<const Expr *, DeclRefExpr *> *Captures = nullptr) { 7311 // Add parentheses (for debugging purposes only). 7312 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get()); 7313 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() || 7314 !Step.isUsable()) 7315 return ExprError(); 7316 7317 ExprResult NewStep = Step; 7318 if (Captures) 7319 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures); 7320 if (NewStep.isInvalid()) 7321 return ExprError(); 7322 ExprResult Update = 7323 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get()); 7324 if (!Update.isUsable()) 7325 return ExprError(); 7326 7327 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or 7328 // 'VarRef = Start (+|-) Iter * Step'. 7329 if (!Start.isUsable()) 7330 return ExprError(); 7331 ExprResult NewStart = SemaRef.ActOnParenExpr(Loc, Loc, Start.get()); 7332 if (!NewStart.isUsable()) 7333 return ExprError(); 7334 if (Captures && !IsNonRectangularLB) 7335 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures); 7336 if (NewStart.isInvalid()) 7337 return ExprError(); 7338 7339 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'. 7340 ExprResult SavedUpdate = Update; 7341 ExprResult UpdateVal; 7342 if (VarRef.get()->getType()->isOverloadableType() || 7343 NewStart.get()->getType()->isOverloadableType() || 7344 Update.get()->getType()->isOverloadableType()) { 7345 Sema::TentativeAnalysisScope Trap(SemaRef); 7346 7347 Update = 7348 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); 7349 if (Update.isUsable()) { 7350 UpdateVal = 7351 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign, 7352 VarRef.get(), SavedUpdate.get()); 7353 if (UpdateVal.isUsable()) { 7354 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(), 7355 UpdateVal.get()); 7356 } 7357 } 7358 } 7359 7360 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'. 7361 if (!Update.isUsable() || !UpdateVal.isUsable()) { 7362 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add, 7363 NewStart.get(), SavedUpdate.get()); 7364 if (!Update.isUsable()) 7365 return ExprError(); 7366 7367 if (!SemaRef.Context.hasSameType(Update.get()->getType(), 7368 VarRef.get()->getType())) { 7369 Update = SemaRef.PerformImplicitConversion( 7370 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true); 7371 if (!Update.isUsable()) 7372 return ExprError(); 7373 } 7374 7375 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get()); 7376 } 7377 return Update; 7378 } 7379 7380 /// Convert integer expression \a E to make it have at least \a Bits 7381 /// bits. 7382 static ExprResult widenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) { 7383 if (E == nullptr) 7384 return ExprError(); 7385 ASTContext &C = SemaRef.Context; 7386 QualType OldType = E->getType(); 7387 unsigned HasBits = C.getTypeSize(OldType); 7388 if (HasBits >= Bits) 7389 return ExprResult(E); 7390 // OK to convert to signed, because new type has more bits than old. 7391 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true); 7392 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting, 7393 true); 7394 } 7395 7396 /// Check if the given expression \a E is a constant integer that fits 7397 /// into \a Bits bits. 7398 static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) { 7399 if (E == nullptr) 7400 return false; 7401 llvm::APSInt Result; 7402 if (E->isIntegerConstantExpr(Result, SemaRef.Context)) 7403 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits); 7404 return false; 7405 } 7406 7407 /// Build preinits statement for the given declarations. 7408 static Stmt *buildPreInits(ASTContext &Context, 7409 MutableArrayRef<Decl *> PreInits) { 7410 if (!PreInits.empty()) { 7411 return new (Context) DeclStmt( 7412 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()), 7413 SourceLocation(), SourceLocation()); 7414 } 7415 return nullptr; 7416 } 7417 7418 /// Build preinits statement for the given declarations. 7419 static Stmt * 7420 buildPreInits(ASTContext &Context, 7421 const llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 7422 if (!Captures.empty()) { 7423 SmallVector<Decl *, 16> PreInits; 7424 for (const auto &Pair : Captures) 7425 PreInits.push_back(Pair.second->getDecl()); 7426 return buildPreInits(Context, PreInits); 7427 } 7428 return nullptr; 7429 } 7430 7431 /// Build postupdate expression for the given list of postupdates expressions. 7432 static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) { 7433 Expr *PostUpdate = nullptr; 7434 if (!PostUpdates.empty()) { 7435 for (Expr *E : PostUpdates) { 7436 Expr *ConvE = S.BuildCStyleCastExpr( 7437 E->getExprLoc(), 7438 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy), 7439 E->getExprLoc(), E) 7440 .get(); 7441 PostUpdate = PostUpdate 7442 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma, 7443 PostUpdate, ConvE) 7444 .get() 7445 : ConvE; 7446 } 7447 } 7448 return PostUpdate; 7449 } 7450 7451 /// Called on a for stmt to check itself and nested loops (if any). 7452 /// \return Returns 0 if one of the collapsed stmts is not canonical for loop, 7453 /// number of collapsed loops otherwise. 7454 static unsigned 7455 checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr, 7456 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef, 7457 DSAStackTy &DSA, 7458 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA, 7459 OMPLoopDirective::HelperExprs &Built) { 7460 unsigned NestedLoopCount = 1; 7461 if (CollapseLoopCountExpr) { 7462 // Found 'collapse' clause - calculate collapse number. 7463 Expr::EvalResult Result; 7464 if (!CollapseLoopCountExpr->isValueDependent() && 7465 CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) { 7466 NestedLoopCount = Result.Val.getInt().getLimitedValue(); 7467 } else { 7468 Built.clear(/*Size=*/1); 7469 return 1; 7470 } 7471 } 7472 unsigned OrderedLoopCount = 1; 7473 if (OrderedLoopCountExpr) { 7474 // Found 'ordered' clause - calculate collapse number. 7475 Expr::EvalResult EVResult; 7476 if (!OrderedLoopCountExpr->isValueDependent() && 7477 OrderedLoopCountExpr->EvaluateAsInt(EVResult, 7478 SemaRef.getASTContext())) { 7479 llvm::APSInt Result = EVResult.Val.getInt(); 7480 if (Result.getLimitedValue() < NestedLoopCount) { 7481 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), 7482 diag::err_omp_wrong_ordered_loop_count) 7483 << OrderedLoopCountExpr->getSourceRange(); 7484 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), 7485 diag::note_collapse_loop_count) 7486 << CollapseLoopCountExpr->getSourceRange(); 7487 } 7488 OrderedLoopCount = Result.getLimitedValue(); 7489 } else { 7490 Built.clear(/*Size=*/1); 7491 return 1; 7492 } 7493 } 7494 // This is helper routine for loop directives (e.g., 'for', 'simd', 7495 // 'for simd', etc.). 7496 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 7497 SmallVector<LoopIterationSpace, 4> IterSpaces( 7498 std::max(OrderedLoopCount, NestedLoopCount)); 7499 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true); 7500 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) { 7501 if (checkOpenMPIterationSpace( 7502 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount, 7503 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr, 7504 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces, Captures)) 7505 return 0; 7506 // Move on to the next nested for loop, or to the loop body. 7507 // OpenMP [2.8.1, simd construct, Restrictions] 7508 // All loops associated with the construct must be perfectly nested; that 7509 // is, there must be no intervening code nor any OpenMP directive between 7510 // any two loops. 7511 if (auto *For = dyn_cast<ForStmt>(CurStmt)) { 7512 CurStmt = For->getBody(); 7513 } else { 7514 assert(isa<CXXForRangeStmt>(CurStmt) && 7515 "Expected canonical for or range-based for loops."); 7516 CurStmt = cast<CXXForRangeStmt>(CurStmt)->getBody(); 7517 } 7518 CurStmt = OMPLoopDirective::tryToFindNextInnerLoop( 7519 CurStmt, SemaRef.LangOpts.OpenMP >= 50); 7520 } 7521 for (unsigned Cnt = NestedLoopCount; Cnt < OrderedLoopCount; ++Cnt) { 7522 if (checkOpenMPIterationSpace( 7523 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount, 7524 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr, 7525 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces, Captures)) 7526 return 0; 7527 if (Cnt > 0 && IterSpaces[Cnt].CounterVar) { 7528 // Handle initialization of captured loop iterator variables. 7529 auto *DRE = cast<DeclRefExpr>(IterSpaces[Cnt].CounterVar); 7530 if (isa<OMPCapturedExprDecl>(DRE->getDecl())) { 7531 Captures[DRE] = DRE; 7532 } 7533 } 7534 // Move on to the next nested for loop, or to the loop body. 7535 // OpenMP [2.8.1, simd construct, Restrictions] 7536 // All loops associated with the construct must be perfectly nested; that 7537 // is, there must be no intervening code nor any OpenMP directive between 7538 // any two loops. 7539 if (auto *For = dyn_cast<ForStmt>(CurStmt)) { 7540 CurStmt = For->getBody(); 7541 } else { 7542 assert(isa<CXXForRangeStmt>(CurStmt) && 7543 "Expected canonical for or range-based for loops."); 7544 CurStmt = cast<CXXForRangeStmt>(CurStmt)->getBody(); 7545 } 7546 CurStmt = OMPLoopDirective::tryToFindNextInnerLoop( 7547 CurStmt, SemaRef.LangOpts.OpenMP >= 50); 7548 } 7549 7550 Built.clear(/* size */ NestedLoopCount); 7551 7552 if (SemaRef.CurContext->isDependentContext()) 7553 return NestedLoopCount; 7554 7555 // An example of what is generated for the following code: 7556 // 7557 // #pragma omp simd collapse(2) ordered(2) 7558 // for (i = 0; i < NI; ++i) 7559 // for (k = 0; k < NK; ++k) 7560 // for (j = J0; j < NJ; j+=2) { 7561 // <loop body> 7562 // } 7563 // 7564 // We generate the code below. 7565 // Note: the loop body may be outlined in CodeGen. 7566 // Note: some counters may be C++ classes, operator- is used to find number of 7567 // iterations and operator+= to calculate counter value. 7568 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32 7569 // or i64 is currently supported). 7570 // 7571 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2)) 7572 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) { 7573 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2); 7574 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2; 7575 // // similar updates for vars in clauses (e.g. 'linear') 7576 // <loop body (using local i and j)> 7577 // } 7578 // i = NI; // assign final values of counters 7579 // j = NJ; 7580 // 7581 7582 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are 7583 // the iteration counts of the collapsed for loops. 7584 // Precondition tests if there is at least one iteration (all conditions are 7585 // true). 7586 auto PreCond = ExprResult(IterSpaces[0].PreCond); 7587 Expr *N0 = IterSpaces[0].NumIterations; 7588 ExprResult LastIteration32 = 7589 widenIterationCount(/*Bits=*/32, 7590 SemaRef 7591 .PerformImplicitConversion( 7592 N0->IgnoreImpCasts(), N0->getType(), 7593 Sema::AA_Converting, /*AllowExplicit=*/true) 7594 .get(), 7595 SemaRef); 7596 ExprResult LastIteration64 = widenIterationCount( 7597 /*Bits=*/64, 7598 SemaRef 7599 .PerformImplicitConversion(N0->IgnoreImpCasts(), N0->getType(), 7600 Sema::AA_Converting, 7601 /*AllowExplicit=*/true) 7602 .get(), 7603 SemaRef); 7604 7605 if (!LastIteration32.isUsable() || !LastIteration64.isUsable()) 7606 return NestedLoopCount; 7607 7608 ASTContext &C = SemaRef.Context; 7609 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32; 7610 7611 Scope *CurScope = DSA.getCurScope(); 7612 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) { 7613 if (PreCond.isUsable()) { 7614 PreCond = 7615 SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd, 7616 PreCond.get(), IterSpaces[Cnt].PreCond); 7617 } 7618 Expr *N = IterSpaces[Cnt].NumIterations; 7619 SourceLocation Loc = N->getExprLoc(); 7620 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32; 7621 if (LastIteration32.isUsable()) 7622 LastIteration32 = SemaRef.BuildBinOp( 7623 CurScope, Loc, BO_Mul, LastIteration32.get(), 7624 SemaRef 7625 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), 7626 Sema::AA_Converting, 7627 /*AllowExplicit=*/true) 7628 .get()); 7629 if (LastIteration64.isUsable()) 7630 LastIteration64 = SemaRef.BuildBinOp( 7631 CurScope, Loc, BO_Mul, LastIteration64.get(), 7632 SemaRef 7633 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), 7634 Sema::AA_Converting, 7635 /*AllowExplicit=*/true) 7636 .get()); 7637 } 7638 7639 // Choose either the 32-bit or 64-bit version. 7640 ExprResult LastIteration = LastIteration64; 7641 if (SemaRef.getLangOpts().OpenMPOptimisticCollapse || 7642 (LastIteration32.isUsable() && 7643 C.getTypeSize(LastIteration32.get()->getType()) == 32 && 7644 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 || 7645 fitsInto( 7646 /*Bits=*/32, 7647 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(), 7648 LastIteration64.get(), SemaRef)))) 7649 LastIteration = LastIteration32; 7650 QualType VType = LastIteration.get()->getType(); 7651 QualType RealVType = VType; 7652 QualType StrideVType = VType; 7653 if (isOpenMPTaskLoopDirective(DKind)) { 7654 VType = 7655 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0); 7656 StrideVType = 7657 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1); 7658 } 7659 7660 if (!LastIteration.isUsable()) 7661 return 0; 7662 7663 // Save the number of iterations. 7664 ExprResult NumIterations = LastIteration; 7665 { 7666 LastIteration = SemaRef.BuildBinOp( 7667 CurScope, LastIteration.get()->getExprLoc(), BO_Sub, 7668 LastIteration.get(), 7669 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 7670 if (!LastIteration.isUsable()) 7671 return 0; 7672 } 7673 7674 // Calculate the last iteration number beforehand instead of doing this on 7675 // each iteration. Do not do this if the number of iterations may be kfold-ed. 7676 llvm::APSInt Result; 7677 bool IsConstant = 7678 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context); 7679 ExprResult CalcLastIteration; 7680 if (!IsConstant) { 7681 ExprResult SaveRef = 7682 tryBuildCapture(SemaRef, LastIteration.get(), Captures); 7683 LastIteration = SaveRef; 7684 7685 // Prepare SaveRef + 1. 7686 NumIterations = SemaRef.BuildBinOp( 7687 CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(), 7688 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 7689 if (!NumIterations.isUsable()) 7690 return 0; 7691 } 7692 7693 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin(); 7694 7695 // Build variables passed into runtime, necessary for worksharing directives. 7696 ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB; 7697 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || 7698 isOpenMPDistributeDirective(DKind)) { 7699 // Lower bound variable, initialized with zero. 7700 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb"); 7701 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc); 7702 SemaRef.AddInitializerToDecl(LBDecl, 7703 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 7704 /*DirectInit*/ false); 7705 7706 // Upper bound variable, initialized with last iteration number. 7707 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub"); 7708 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc); 7709 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(), 7710 /*DirectInit*/ false); 7711 7712 // A 32-bit variable-flag where runtime returns 1 for the last iteration. 7713 // This will be used to implement clause 'lastprivate'. 7714 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true); 7715 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last"); 7716 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc); 7717 SemaRef.AddInitializerToDecl(ILDecl, 7718 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 7719 /*DirectInit*/ false); 7720 7721 // Stride variable returned by runtime (we initialize it to 1 by default). 7722 VarDecl *STDecl = 7723 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride"); 7724 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc); 7725 SemaRef.AddInitializerToDecl(STDecl, 7726 SemaRef.ActOnIntegerConstant(InitLoc, 1).get(), 7727 /*DirectInit*/ false); 7728 7729 // Build expression: UB = min(UB, LastIteration) 7730 // It is necessary for CodeGen of directives with static scheduling. 7731 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT, 7732 UB.get(), LastIteration.get()); 7733 ExprResult CondOp = SemaRef.ActOnConditionalOp( 7734 LastIteration.get()->getExprLoc(), InitLoc, IsUBGreater.get(), 7735 LastIteration.get(), UB.get()); 7736 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(), 7737 CondOp.get()); 7738 EUB = SemaRef.ActOnFinishFullExpr(EUB.get(), /*DiscardedValue*/ false); 7739 7740 // If we have a combined directive that combines 'distribute', 'for' or 7741 // 'simd' we need to be able to access the bounds of the schedule of the 7742 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained 7743 // by scheduling 'distribute' have to be passed to the schedule of 'for'. 7744 if (isOpenMPLoopBoundSharingDirective(DKind)) { 7745 // Lower bound variable, initialized with zero. 7746 VarDecl *CombLBDecl = 7747 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb"); 7748 CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc); 7749 SemaRef.AddInitializerToDecl( 7750 CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 7751 /*DirectInit*/ false); 7752 7753 // Upper bound variable, initialized with last iteration number. 7754 VarDecl *CombUBDecl = 7755 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub"); 7756 CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc); 7757 SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(), 7758 /*DirectInit*/ false); 7759 7760 ExprResult CombIsUBGreater = SemaRef.BuildBinOp( 7761 CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get()); 7762 ExprResult CombCondOp = 7763 SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(), 7764 LastIteration.get(), CombUB.get()); 7765 CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(), 7766 CombCondOp.get()); 7767 CombEUB = 7768 SemaRef.ActOnFinishFullExpr(CombEUB.get(), /*DiscardedValue*/ false); 7769 7770 const CapturedDecl *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl(); 7771 // We expect to have at least 2 more parameters than the 'parallel' 7772 // directive does - the lower and upper bounds of the previous schedule. 7773 assert(CD->getNumParams() >= 4 && 7774 "Unexpected number of parameters in loop combined directive"); 7775 7776 // Set the proper type for the bounds given what we learned from the 7777 // enclosed loops. 7778 ImplicitParamDecl *PrevLBDecl = CD->getParam(/*PrevLB=*/2); 7779 ImplicitParamDecl *PrevUBDecl = CD->getParam(/*PrevUB=*/3); 7780 7781 // Previous lower and upper bounds are obtained from the region 7782 // parameters. 7783 PrevLB = 7784 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc); 7785 PrevUB = 7786 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc); 7787 } 7788 } 7789 7790 // Build the iteration variable and its initialization before loop. 7791 ExprResult IV; 7792 ExprResult Init, CombInit; 7793 { 7794 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv"); 7795 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc); 7796 Expr *RHS = 7797 (isOpenMPWorksharingDirective(DKind) || 7798 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)) 7799 ? LB.get() 7800 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get(); 7801 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS); 7802 Init = SemaRef.ActOnFinishFullExpr(Init.get(), /*DiscardedValue*/ false); 7803 7804 if (isOpenMPLoopBoundSharingDirective(DKind)) { 7805 Expr *CombRHS = 7806 (isOpenMPWorksharingDirective(DKind) || 7807 isOpenMPTaskLoopDirective(DKind) || 7808 isOpenMPDistributeDirective(DKind)) 7809 ? CombLB.get() 7810 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get(); 7811 CombInit = 7812 SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS); 7813 CombInit = 7814 SemaRef.ActOnFinishFullExpr(CombInit.get(), /*DiscardedValue*/ false); 7815 } 7816 } 7817 7818 bool UseStrictCompare = 7819 RealVType->hasUnsignedIntegerRepresentation() && 7820 llvm::all_of(IterSpaces, [](const LoopIterationSpace &LIS) { 7821 return LIS.IsStrictCompare; 7822 }); 7823 // Loop condition (IV < NumIterations) or (IV <= UB or IV < UB + 1 (for 7824 // unsigned IV)) for worksharing loops. 7825 SourceLocation CondLoc = AStmt->getBeginLoc(); 7826 Expr *BoundUB = UB.get(); 7827 if (UseStrictCompare) { 7828 BoundUB = 7829 SemaRef 7830 .BuildBinOp(CurScope, CondLoc, BO_Add, BoundUB, 7831 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()) 7832 .get(); 7833 BoundUB = 7834 SemaRef.ActOnFinishFullExpr(BoundUB, /*DiscardedValue*/ false).get(); 7835 } 7836 ExprResult Cond = 7837 (isOpenMPWorksharingDirective(DKind) || 7838 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)) 7839 ? SemaRef.BuildBinOp(CurScope, CondLoc, 7840 UseStrictCompare ? BO_LT : BO_LE, IV.get(), 7841 BoundUB) 7842 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(), 7843 NumIterations.get()); 7844 ExprResult CombDistCond; 7845 if (isOpenMPLoopBoundSharingDirective(DKind)) { 7846 CombDistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(), 7847 NumIterations.get()); 7848 } 7849 7850 ExprResult CombCond; 7851 if (isOpenMPLoopBoundSharingDirective(DKind)) { 7852 Expr *BoundCombUB = CombUB.get(); 7853 if (UseStrictCompare) { 7854 BoundCombUB = 7855 SemaRef 7856 .BuildBinOp( 7857 CurScope, CondLoc, BO_Add, BoundCombUB, 7858 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()) 7859 .get(); 7860 BoundCombUB = 7861 SemaRef.ActOnFinishFullExpr(BoundCombUB, /*DiscardedValue*/ false) 7862 .get(); 7863 } 7864 CombCond = 7865 SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, 7866 IV.get(), BoundCombUB); 7867 } 7868 // Loop increment (IV = IV + 1) 7869 SourceLocation IncLoc = AStmt->getBeginLoc(); 7870 ExprResult Inc = 7871 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(), 7872 SemaRef.ActOnIntegerConstant(IncLoc, 1).get()); 7873 if (!Inc.isUsable()) 7874 return 0; 7875 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get()); 7876 Inc = SemaRef.ActOnFinishFullExpr(Inc.get(), /*DiscardedValue*/ false); 7877 if (!Inc.isUsable()) 7878 return 0; 7879 7880 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST). 7881 // Used for directives with static scheduling. 7882 // In combined construct, add combined version that use CombLB and CombUB 7883 // base variables for the update 7884 ExprResult NextLB, NextUB, CombNextLB, CombNextUB; 7885 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || 7886 isOpenMPDistributeDirective(DKind)) { 7887 // LB + ST 7888 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get()); 7889 if (!NextLB.isUsable()) 7890 return 0; 7891 // LB = LB + ST 7892 NextLB = 7893 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get()); 7894 NextLB = 7895 SemaRef.ActOnFinishFullExpr(NextLB.get(), /*DiscardedValue*/ false); 7896 if (!NextLB.isUsable()) 7897 return 0; 7898 // UB + ST 7899 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get()); 7900 if (!NextUB.isUsable()) 7901 return 0; 7902 // UB = UB + ST 7903 NextUB = 7904 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get()); 7905 NextUB = 7906 SemaRef.ActOnFinishFullExpr(NextUB.get(), /*DiscardedValue*/ false); 7907 if (!NextUB.isUsable()) 7908 return 0; 7909 if (isOpenMPLoopBoundSharingDirective(DKind)) { 7910 CombNextLB = 7911 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get()); 7912 if (!NextLB.isUsable()) 7913 return 0; 7914 // LB = LB + ST 7915 CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(), 7916 CombNextLB.get()); 7917 CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get(), 7918 /*DiscardedValue*/ false); 7919 if (!CombNextLB.isUsable()) 7920 return 0; 7921 // UB + ST 7922 CombNextUB = 7923 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get()); 7924 if (!CombNextUB.isUsable()) 7925 return 0; 7926 // UB = UB + ST 7927 CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(), 7928 CombNextUB.get()); 7929 CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get(), 7930 /*DiscardedValue*/ false); 7931 if (!CombNextUB.isUsable()) 7932 return 0; 7933 } 7934 } 7935 7936 // Create increment expression for distribute loop when combined in a same 7937 // directive with for as IV = IV + ST; ensure upper bound expression based 7938 // on PrevUB instead of NumIterations - used to implement 'for' when found 7939 // in combination with 'distribute', like in 'distribute parallel for' 7940 SourceLocation DistIncLoc = AStmt->getBeginLoc(); 7941 ExprResult DistCond, DistInc, PrevEUB, ParForInDistCond; 7942 if (isOpenMPLoopBoundSharingDirective(DKind)) { 7943 DistCond = SemaRef.BuildBinOp( 7944 CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, IV.get(), BoundUB); 7945 assert(DistCond.isUsable() && "distribute cond expr was not built"); 7946 7947 DistInc = 7948 SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get()); 7949 assert(DistInc.isUsable() && "distribute inc expr was not built"); 7950 DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(), 7951 DistInc.get()); 7952 DistInc = 7953 SemaRef.ActOnFinishFullExpr(DistInc.get(), /*DiscardedValue*/ false); 7954 assert(DistInc.isUsable() && "distribute inc expr was not built"); 7955 7956 // Build expression: UB = min(UB, prevUB) for #for in composite or combined 7957 // construct 7958 SourceLocation DistEUBLoc = AStmt->getBeginLoc(); 7959 ExprResult IsUBGreater = 7960 SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, UB.get(), PrevUB.get()); 7961 ExprResult CondOp = SemaRef.ActOnConditionalOp( 7962 DistEUBLoc, DistEUBLoc, IsUBGreater.get(), PrevUB.get(), UB.get()); 7963 PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(), 7964 CondOp.get()); 7965 PrevEUB = 7966 SemaRef.ActOnFinishFullExpr(PrevEUB.get(), /*DiscardedValue*/ false); 7967 7968 // Build IV <= PrevUB or IV < PrevUB + 1 for unsigned IV to be used in 7969 // parallel for is in combination with a distribute directive with 7970 // schedule(static, 1) 7971 Expr *BoundPrevUB = PrevUB.get(); 7972 if (UseStrictCompare) { 7973 BoundPrevUB = 7974 SemaRef 7975 .BuildBinOp( 7976 CurScope, CondLoc, BO_Add, BoundPrevUB, 7977 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()) 7978 .get(); 7979 BoundPrevUB = 7980 SemaRef.ActOnFinishFullExpr(BoundPrevUB, /*DiscardedValue*/ false) 7981 .get(); 7982 } 7983 ParForInDistCond = 7984 SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, 7985 IV.get(), BoundPrevUB); 7986 } 7987 7988 // Build updates and final values of the loop counters. 7989 bool HasErrors = false; 7990 Built.Counters.resize(NestedLoopCount); 7991 Built.Inits.resize(NestedLoopCount); 7992 Built.Updates.resize(NestedLoopCount); 7993 Built.Finals.resize(NestedLoopCount); 7994 Built.DependentCounters.resize(NestedLoopCount); 7995 Built.DependentInits.resize(NestedLoopCount); 7996 Built.FinalsConditions.resize(NestedLoopCount); 7997 { 7998 // We implement the following algorithm for obtaining the 7999 // original loop iteration variable values based on the 8000 // value of the collapsed loop iteration variable IV. 8001 // 8002 // Let n+1 be the number of collapsed loops in the nest. 8003 // Iteration variables (I0, I1, .... In) 8004 // Iteration counts (N0, N1, ... Nn) 8005 // 8006 // Acc = IV; 8007 // 8008 // To compute Ik for loop k, 0 <= k <= n, generate: 8009 // Prod = N(k+1) * N(k+2) * ... * Nn; 8010 // Ik = Acc / Prod; 8011 // Acc -= Ik * Prod; 8012 // 8013 ExprResult Acc = IV; 8014 for (unsigned int Cnt = 0; Cnt < NestedLoopCount; ++Cnt) { 8015 LoopIterationSpace &IS = IterSpaces[Cnt]; 8016 SourceLocation UpdLoc = IS.IncSrcRange.getBegin(); 8017 ExprResult Iter; 8018 8019 // Compute prod 8020 ExprResult Prod = 8021 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(); 8022 for (unsigned int K = Cnt+1; K < NestedLoopCount; ++K) 8023 Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Prod.get(), 8024 IterSpaces[K].NumIterations); 8025 8026 // Iter = Acc / Prod 8027 // If there is at least one more inner loop to avoid 8028 // multiplication by 1. 8029 if (Cnt + 1 < NestedLoopCount) 8030 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, 8031 Acc.get(), Prod.get()); 8032 else 8033 Iter = Acc; 8034 if (!Iter.isUsable()) { 8035 HasErrors = true; 8036 break; 8037 } 8038 8039 // Update Acc: 8040 // Acc -= Iter * Prod 8041 // Check if there is at least one more inner loop to avoid 8042 // multiplication by 1. 8043 if (Cnt + 1 < NestedLoopCount) 8044 Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, 8045 Iter.get(), Prod.get()); 8046 else 8047 Prod = Iter; 8048 Acc = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Sub, 8049 Acc.get(), Prod.get()); 8050 8051 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step 8052 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()); 8053 DeclRefExpr *CounterVar = buildDeclRefExpr( 8054 SemaRef, VD, IS.CounterVar->getType(), IS.CounterVar->getExprLoc(), 8055 /*RefersToCapture=*/true); 8056 ExprResult Init = 8057 buildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar, 8058 IS.CounterInit, IS.IsNonRectangularLB, Captures); 8059 if (!Init.isUsable()) { 8060 HasErrors = true; 8061 break; 8062 } 8063 ExprResult Update = buildCounterUpdate( 8064 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter, 8065 IS.CounterStep, IS.Subtract, IS.IsNonRectangularLB, &Captures); 8066 if (!Update.isUsable()) { 8067 HasErrors = true; 8068 break; 8069 } 8070 8071 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step 8072 ExprResult Final = 8073 buildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar, 8074 IS.CounterInit, IS.NumIterations, IS.CounterStep, 8075 IS.Subtract, IS.IsNonRectangularLB, &Captures); 8076 if (!Final.isUsable()) { 8077 HasErrors = true; 8078 break; 8079 } 8080 8081 if (!Update.isUsable() || !Final.isUsable()) { 8082 HasErrors = true; 8083 break; 8084 } 8085 // Save results 8086 Built.Counters[Cnt] = IS.CounterVar; 8087 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar; 8088 Built.Inits[Cnt] = Init.get(); 8089 Built.Updates[Cnt] = Update.get(); 8090 Built.Finals[Cnt] = Final.get(); 8091 Built.DependentCounters[Cnt] = nullptr; 8092 Built.DependentInits[Cnt] = nullptr; 8093 Built.FinalsConditions[Cnt] = nullptr; 8094 if (IS.IsNonRectangularLB || IS.IsNonRectangularUB) { 8095 Built.DependentCounters[Cnt] = 8096 Built.Counters[NestedLoopCount - 1 - IS.LoopDependentIdx]; 8097 Built.DependentInits[Cnt] = 8098 Built.Inits[NestedLoopCount - 1 - IS.LoopDependentIdx]; 8099 Built.FinalsConditions[Cnt] = IS.FinalCondition; 8100 } 8101 } 8102 } 8103 8104 if (HasErrors) 8105 return 0; 8106 8107 // Save results 8108 Built.IterationVarRef = IV.get(); 8109 Built.LastIteration = LastIteration.get(); 8110 Built.NumIterations = NumIterations.get(); 8111 Built.CalcLastIteration = SemaRef 8112 .ActOnFinishFullExpr(CalcLastIteration.get(), 8113 /*DiscardedValue=*/false) 8114 .get(); 8115 Built.PreCond = PreCond.get(); 8116 Built.PreInits = buildPreInits(C, Captures); 8117 Built.Cond = Cond.get(); 8118 Built.Init = Init.get(); 8119 Built.Inc = Inc.get(); 8120 Built.LB = LB.get(); 8121 Built.UB = UB.get(); 8122 Built.IL = IL.get(); 8123 Built.ST = ST.get(); 8124 Built.EUB = EUB.get(); 8125 Built.NLB = NextLB.get(); 8126 Built.NUB = NextUB.get(); 8127 Built.PrevLB = PrevLB.get(); 8128 Built.PrevUB = PrevUB.get(); 8129 Built.DistInc = DistInc.get(); 8130 Built.PrevEUB = PrevEUB.get(); 8131 Built.DistCombinedFields.LB = CombLB.get(); 8132 Built.DistCombinedFields.UB = CombUB.get(); 8133 Built.DistCombinedFields.EUB = CombEUB.get(); 8134 Built.DistCombinedFields.Init = CombInit.get(); 8135 Built.DistCombinedFields.Cond = CombCond.get(); 8136 Built.DistCombinedFields.NLB = CombNextLB.get(); 8137 Built.DistCombinedFields.NUB = CombNextUB.get(); 8138 Built.DistCombinedFields.DistCond = CombDistCond.get(); 8139 Built.DistCombinedFields.ParForInDistCond = ParForInDistCond.get(); 8140 8141 return NestedLoopCount; 8142 } 8143 8144 static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) { 8145 auto CollapseClauses = 8146 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses); 8147 if (CollapseClauses.begin() != CollapseClauses.end()) 8148 return (*CollapseClauses.begin())->getNumForLoops(); 8149 return nullptr; 8150 } 8151 8152 static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) { 8153 auto OrderedClauses = 8154 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses); 8155 if (OrderedClauses.begin() != OrderedClauses.end()) 8156 return (*OrderedClauses.begin())->getNumForLoops(); 8157 return nullptr; 8158 } 8159 8160 static bool checkSimdlenSafelenSpecified(Sema &S, 8161 const ArrayRef<OMPClause *> Clauses) { 8162 const OMPSafelenClause *Safelen = nullptr; 8163 const OMPSimdlenClause *Simdlen = nullptr; 8164 8165 for (const OMPClause *Clause : Clauses) { 8166 if (Clause->getClauseKind() == OMPC_safelen) 8167 Safelen = cast<OMPSafelenClause>(Clause); 8168 else if (Clause->getClauseKind() == OMPC_simdlen) 8169 Simdlen = cast<OMPSimdlenClause>(Clause); 8170 if (Safelen && Simdlen) 8171 break; 8172 } 8173 8174 if (Simdlen && Safelen) { 8175 const Expr *SimdlenLength = Simdlen->getSimdlen(); 8176 const Expr *SafelenLength = Safelen->getSafelen(); 8177 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() || 8178 SimdlenLength->isInstantiationDependent() || 8179 SimdlenLength->containsUnexpandedParameterPack()) 8180 return false; 8181 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() || 8182 SafelenLength->isInstantiationDependent() || 8183 SafelenLength->containsUnexpandedParameterPack()) 8184 return false; 8185 Expr::EvalResult SimdlenResult, SafelenResult; 8186 SimdlenLength->EvaluateAsInt(SimdlenResult, S.Context); 8187 SafelenLength->EvaluateAsInt(SafelenResult, S.Context); 8188 llvm::APSInt SimdlenRes = SimdlenResult.Val.getInt(); 8189 llvm::APSInt SafelenRes = SafelenResult.Val.getInt(); 8190 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions] 8191 // If both simdlen and safelen clauses are specified, the value of the 8192 // simdlen parameter must be less than or equal to the value of the safelen 8193 // parameter. 8194 if (SimdlenRes > SafelenRes) { 8195 S.Diag(SimdlenLength->getExprLoc(), 8196 diag::err_omp_wrong_simdlen_safelen_values) 8197 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange(); 8198 return true; 8199 } 8200 } 8201 return false; 8202 } 8203 8204 StmtResult 8205 Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt, 8206 SourceLocation StartLoc, SourceLocation EndLoc, 8207 VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8208 if (!AStmt) 8209 return StmtError(); 8210 8211 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 8212 OMPLoopDirective::HelperExprs B; 8213 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 8214 // define the nested loops number. 8215 unsigned NestedLoopCount = checkOpenMPLoop( 8216 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), 8217 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 8218 if (NestedLoopCount == 0) 8219 return StmtError(); 8220 8221 assert((CurContext->isDependentContext() || B.builtAll()) && 8222 "omp simd loop exprs were not built"); 8223 8224 if (!CurContext->isDependentContext()) { 8225 // Finalize the clauses that need pre-built expressions for CodeGen. 8226 for (OMPClause *C : Clauses) { 8227 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 8228 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 8229 B.NumIterations, *this, CurScope, 8230 DSAStack)) 8231 return StmtError(); 8232 } 8233 } 8234 8235 if (checkSimdlenSafelenSpecified(*this, Clauses)) 8236 return StmtError(); 8237 8238 setFunctionHasBranchProtectedScope(); 8239 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 8240 Clauses, AStmt, B); 8241 } 8242 8243 StmtResult 8244 Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt, 8245 SourceLocation StartLoc, SourceLocation EndLoc, 8246 VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8247 if (!AStmt) 8248 return StmtError(); 8249 8250 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 8251 OMPLoopDirective::HelperExprs B; 8252 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 8253 // define the nested loops number. 8254 unsigned NestedLoopCount = checkOpenMPLoop( 8255 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), 8256 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 8257 if (NestedLoopCount == 0) 8258 return StmtError(); 8259 8260 assert((CurContext->isDependentContext() || B.builtAll()) && 8261 "omp for loop exprs were not built"); 8262 8263 if (!CurContext->isDependentContext()) { 8264 // Finalize the clauses that need pre-built expressions for CodeGen. 8265 for (OMPClause *C : Clauses) { 8266 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 8267 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 8268 B.NumIterations, *this, CurScope, 8269 DSAStack)) 8270 return StmtError(); 8271 } 8272 } 8273 8274 setFunctionHasBranchProtectedScope(); 8275 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 8276 Clauses, AStmt, B, DSAStack->isCancelRegion()); 8277 } 8278 8279 StmtResult Sema::ActOnOpenMPForSimdDirective( 8280 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8281 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8282 if (!AStmt) 8283 return StmtError(); 8284 8285 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 8286 OMPLoopDirective::HelperExprs B; 8287 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 8288 // define the nested loops number. 8289 unsigned NestedLoopCount = 8290 checkOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses), 8291 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 8292 VarsWithImplicitDSA, B); 8293 if (NestedLoopCount == 0) 8294 return StmtError(); 8295 8296 assert((CurContext->isDependentContext() || B.builtAll()) && 8297 "omp for simd loop exprs were not built"); 8298 8299 if (!CurContext->isDependentContext()) { 8300 // Finalize the clauses that need pre-built expressions for CodeGen. 8301 for (OMPClause *C : Clauses) { 8302 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 8303 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 8304 B.NumIterations, *this, CurScope, 8305 DSAStack)) 8306 return StmtError(); 8307 } 8308 } 8309 8310 if (checkSimdlenSafelenSpecified(*this, Clauses)) 8311 return StmtError(); 8312 8313 setFunctionHasBranchProtectedScope(); 8314 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 8315 Clauses, AStmt, B); 8316 } 8317 8318 StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses, 8319 Stmt *AStmt, 8320 SourceLocation StartLoc, 8321 SourceLocation EndLoc) { 8322 if (!AStmt) 8323 return StmtError(); 8324 8325 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 8326 auto BaseStmt = AStmt; 8327 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 8328 BaseStmt = CS->getCapturedStmt(); 8329 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 8330 auto S = C->children(); 8331 if (S.begin() == S.end()) 8332 return StmtError(); 8333 // All associated statements must be '#pragma omp section' except for 8334 // the first one. 8335 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) { 8336 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 8337 if (SectionStmt) 8338 Diag(SectionStmt->getBeginLoc(), 8339 diag::err_omp_sections_substmt_not_section); 8340 return StmtError(); 8341 } 8342 cast<OMPSectionDirective>(SectionStmt) 8343 ->setHasCancel(DSAStack->isCancelRegion()); 8344 } 8345 } else { 8346 Diag(AStmt->getBeginLoc(), diag::err_omp_sections_not_compound_stmt); 8347 return StmtError(); 8348 } 8349 8350 setFunctionHasBranchProtectedScope(); 8351 8352 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 8353 DSAStack->isCancelRegion()); 8354 } 8355 8356 StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt, 8357 SourceLocation StartLoc, 8358 SourceLocation EndLoc) { 8359 if (!AStmt) 8360 return StmtError(); 8361 8362 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 8363 8364 setFunctionHasBranchProtectedScope(); 8365 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion()); 8366 8367 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt, 8368 DSAStack->isCancelRegion()); 8369 } 8370 8371 StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses, 8372 Stmt *AStmt, 8373 SourceLocation StartLoc, 8374 SourceLocation EndLoc) { 8375 if (!AStmt) 8376 return StmtError(); 8377 8378 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 8379 8380 setFunctionHasBranchProtectedScope(); 8381 8382 // OpenMP [2.7.3, single Construct, Restrictions] 8383 // The copyprivate clause must not be used with the nowait clause. 8384 const OMPClause *Nowait = nullptr; 8385 const OMPClause *Copyprivate = nullptr; 8386 for (const OMPClause *Clause : Clauses) { 8387 if (Clause->getClauseKind() == OMPC_nowait) 8388 Nowait = Clause; 8389 else if (Clause->getClauseKind() == OMPC_copyprivate) 8390 Copyprivate = Clause; 8391 if (Copyprivate && Nowait) { 8392 Diag(Copyprivate->getBeginLoc(), 8393 diag::err_omp_single_copyprivate_with_nowait); 8394 Diag(Nowait->getBeginLoc(), diag::note_omp_nowait_clause_here); 8395 return StmtError(); 8396 } 8397 } 8398 8399 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 8400 } 8401 8402 StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt, 8403 SourceLocation StartLoc, 8404 SourceLocation EndLoc) { 8405 if (!AStmt) 8406 return StmtError(); 8407 8408 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 8409 8410 setFunctionHasBranchProtectedScope(); 8411 8412 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt); 8413 } 8414 8415 StmtResult Sema::ActOnOpenMPCriticalDirective( 8416 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses, 8417 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { 8418 if (!AStmt) 8419 return StmtError(); 8420 8421 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 8422 8423 bool ErrorFound = false; 8424 llvm::APSInt Hint; 8425 SourceLocation HintLoc; 8426 bool DependentHint = false; 8427 for (const OMPClause *C : Clauses) { 8428 if (C->getClauseKind() == OMPC_hint) { 8429 if (!DirName.getName()) { 8430 Diag(C->getBeginLoc(), diag::err_omp_hint_clause_no_name); 8431 ErrorFound = true; 8432 } 8433 Expr *E = cast<OMPHintClause>(C)->getHint(); 8434 if (E->isTypeDependent() || E->isValueDependent() || 8435 E->isInstantiationDependent()) { 8436 DependentHint = true; 8437 } else { 8438 Hint = E->EvaluateKnownConstInt(Context); 8439 HintLoc = C->getBeginLoc(); 8440 } 8441 } 8442 } 8443 if (ErrorFound) 8444 return StmtError(); 8445 const auto Pair = DSAStack->getCriticalWithHint(DirName); 8446 if (Pair.first && DirName.getName() && !DependentHint) { 8447 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) { 8448 Diag(StartLoc, diag::err_omp_critical_with_hint); 8449 if (HintLoc.isValid()) 8450 Diag(HintLoc, diag::note_omp_critical_hint_here) 8451 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false); 8452 else 8453 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0; 8454 if (const auto *C = Pair.first->getSingleClause<OMPHintClause>()) { 8455 Diag(C->getBeginLoc(), diag::note_omp_critical_hint_here) 8456 << 1 8457 << C->getHint()->EvaluateKnownConstInt(Context).toString( 8458 /*Radix=*/10, /*Signed=*/false); 8459 } else { 8460 Diag(Pair.first->getBeginLoc(), diag::note_omp_critical_no_hint) << 1; 8461 } 8462 } 8463 } 8464 8465 setFunctionHasBranchProtectedScope(); 8466 8467 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc, 8468 Clauses, AStmt); 8469 if (!Pair.first && DirName.getName() && !DependentHint) 8470 DSAStack->addCriticalWithHint(Dir, Hint); 8471 return Dir; 8472 } 8473 8474 StmtResult Sema::ActOnOpenMPParallelForDirective( 8475 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8476 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8477 if (!AStmt) 8478 return StmtError(); 8479 8480 auto *CS = cast<CapturedStmt>(AStmt); 8481 // 1.2.2 OpenMP Language Terminology 8482 // Structured block - An executable statement with a single entry at the 8483 // top and a single exit at the bottom. 8484 // The point of exit cannot be a branch out of the structured block. 8485 // longjmp() and throw() must not violate the entry/exit criteria. 8486 CS->getCapturedDecl()->setNothrow(); 8487 8488 OMPLoopDirective::HelperExprs B; 8489 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 8490 // define the nested loops number. 8491 unsigned NestedLoopCount = 8492 checkOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses), 8493 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 8494 VarsWithImplicitDSA, B); 8495 if (NestedLoopCount == 0) 8496 return StmtError(); 8497 8498 assert((CurContext->isDependentContext() || B.builtAll()) && 8499 "omp parallel for loop exprs were not built"); 8500 8501 if (!CurContext->isDependentContext()) { 8502 // Finalize the clauses that need pre-built expressions for CodeGen. 8503 for (OMPClause *C : Clauses) { 8504 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 8505 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 8506 B.NumIterations, *this, CurScope, 8507 DSAStack)) 8508 return StmtError(); 8509 } 8510 } 8511 8512 setFunctionHasBranchProtectedScope(); 8513 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc, 8514 NestedLoopCount, Clauses, AStmt, B, 8515 DSAStack->isCancelRegion()); 8516 } 8517 8518 StmtResult Sema::ActOnOpenMPParallelForSimdDirective( 8519 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8520 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8521 if (!AStmt) 8522 return StmtError(); 8523 8524 auto *CS = cast<CapturedStmt>(AStmt); 8525 // 1.2.2 OpenMP Language Terminology 8526 // Structured block - An executable statement with a single entry at the 8527 // top and a single exit at the bottom. 8528 // The point of exit cannot be a branch out of the structured block. 8529 // longjmp() and throw() must not violate the entry/exit criteria. 8530 CS->getCapturedDecl()->setNothrow(); 8531 8532 OMPLoopDirective::HelperExprs B; 8533 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 8534 // define the nested loops number. 8535 unsigned NestedLoopCount = 8536 checkOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses), 8537 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 8538 VarsWithImplicitDSA, B); 8539 if (NestedLoopCount == 0) 8540 return StmtError(); 8541 8542 if (!CurContext->isDependentContext()) { 8543 // Finalize the clauses that need pre-built expressions for CodeGen. 8544 for (OMPClause *C : Clauses) { 8545 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 8546 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 8547 B.NumIterations, *this, CurScope, 8548 DSAStack)) 8549 return StmtError(); 8550 } 8551 } 8552 8553 if (checkSimdlenSafelenSpecified(*this, Clauses)) 8554 return StmtError(); 8555 8556 setFunctionHasBranchProtectedScope(); 8557 return OMPParallelForSimdDirective::Create( 8558 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 8559 } 8560 8561 StmtResult 8562 Sema::ActOnOpenMPParallelMasterDirective(ArrayRef<OMPClause *> Clauses, 8563 Stmt *AStmt, SourceLocation StartLoc, 8564 SourceLocation EndLoc) { 8565 if (!AStmt) 8566 return StmtError(); 8567 8568 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 8569 auto *CS = cast<CapturedStmt>(AStmt); 8570 // 1.2.2 OpenMP Language Terminology 8571 // Structured block - An executable statement with a single entry at the 8572 // top and a single exit at the bottom. 8573 // The point of exit cannot be a branch out of the structured block. 8574 // longjmp() and throw() must not violate the entry/exit criteria. 8575 CS->getCapturedDecl()->setNothrow(); 8576 8577 setFunctionHasBranchProtectedScope(); 8578 8579 return OMPParallelMasterDirective::Create(Context, StartLoc, EndLoc, Clauses, 8580 AStmt); 8581 } 8582 8583 StmtResult 8584 Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses, 8585 Stmt *AStmt, SourceLocation StartLoc, 8586 SourceLocation EndLoc) { 8587 if (!AStmt) 8588 return StmtError(); 8589 8590 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 8591 auto BaseStmt = AStmt; 8592 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 8593 BaseStmt = CS->getCapturedStmt(); 8594 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 8595 auto S = C->children(); 8596 if (S.begin() == S.end()) 8597 return StmtError(); 8598 // All associated statements must be '#pragma omp section' except for 8599 // the first one. 8600 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) { 8601 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 8602 if (SectionStmt) 8603 Diag(SectionStmt->getBeginLoc(), 8604 diag::err_omp_parallel_sections_substmt_not_section); 8605 return StmtError(); 8606 } 8607 cast<OMPSectionDirective>(SectionStmt) 8608 ->setHasCancel(DSAStack->isCancelRegion()); 8609 } 8610 } else { 8611 Diag(AStmt->getBeginLoc(), 8612 diag::err_omp_parallel_sections_not_compound_stmt); 8613 return StmtError(); 8614 } 8615 8616 setFunctionHasBranchProtectedScope(); 8617 8618 return OMPParallelSectionsDirective::Create( 8619 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion()); 8620 } 8621 8622 StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses, 8623 Stmt *AStmt, SourceLocation StartLoc, 8624 SourceLocation EndLoc) { 8625 if (!AStmt) 8626 return StmtError(); 8627 8628 auto *CS = cast<CapturedStmt>(AStmt); 8629 // 1.2.2 OpenMP Language Terminology 8630 // Structured block - An executable statement with a single entry at the 8631 // top and a single exit at the bottom. 8632 // The point of exit cannot be a branch out of the structured block. 8633 // longjmp() and throw() must not violate the entry/exit criteria. 8634 CS->getCapturedDecl()->setNothrow(); 8635 8636 setFunctionHasBranchProtectedScope(); 8637 8638 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 8639 DSAStack->isCancelRegion()); 8640 } 8641 8642 StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc, 8643 SourceLocation EndLoc) { 8644 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc); 8645 } 8646 8647 StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc, 8648 SourceLocation EndLoc) { 8649 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc); 8650 } 8651 8652 StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc, 8653 SourceLocation EndLoc) { 8654 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc); 8655 } 8656 8657 StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses, 8658 Stmt *AStmt, 8659 SourceLocation StartLoc, 8660 SourceLocation EndLoc) { 8661 if (!AStmt) 8662 return StmtError(); 8663 8664 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 8665 8666 setFunctionHasBranchProtectedScope(); 8667 8668 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses, 8669 AStmt, 8670 DSAStack->getTaskgroupReductionRef()); 8671 } 8672 8673 StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses, 8674 SourceLocation StartLoc, 8675 SourceLocation EndLoc) { 8676 OMPFlushClause *FC = nullptr; 8677 OMPClause *OrderClause = nullptr; 8678 for (OMPClause *C : Clauses) { 8679 if (C->getClauseKind() == OMPC_flush) 8680 FC = cast<OMPFlushClause>(C); 8681 else 8682 OrderClause = C; 8683 } 8684 OpenMPClauseKind MemOrderKind = OMPC_unknown; 8685 SourceLocation MemOrderLoc; 8686 for (const OMPClause *C : Clauses) { 8687 if (C->getClauseKind() == OMPC_acq_rel || 8688 C->getClauseKind() == OMPC_acquire || 8689 C->getClauseKind() == OMPC_release) { 8690 if (MemOrderKind != OMPC_unknown) { 8691 Diag(C->getBeginLoc(), diag::err_omp_several_mem_order_clauses) 8692 << getOpenMPDirectiveName(OMPD_flush) << 1 8693 << SourceRange(C->getBeginLoc(), C->getEndLoc()); 8694 Diag(MemOrderLoc, diag::note_omp_previous_mem_order_clause) 8695 << getOpenMPClauseName(MemOrderKind); 8696 } else { 8697 MemOrderKind = C->getClauseKind(); 8698 MemOrderLoc = C->getBeginLoc(); 8699 } 8700 } 8701 } 8702 if (FC && OrderClause) { 8703 Diag(FC->getLParenLoc(), diag::err_omp_flush_order_clause_and_list) 8704 << getOpenMPClauseName(OrderClause->getClauseKind()); 8705 Diag(OrderClause->getBeginLoc(), diag::note_omp_flush_order_clause_here) 8706 << getOpenMPClauseName(OrderClause->getClauseKind()); 8707 return StmtError(); 8708 } 8709 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses); 8710 } 8711 8712 StmtResult Sema::ActOnOpenMPDepobjDirective(ArrayRef<OMPClause *> Clauses, 8713 SourceLocation StartLoc, 8714 SourceLocation EndLoc) { 8715 if (Clauses.empty()) { 8716 Diag(StartLoc, diag::err_omp_depobj_expected); 8717 return StmtError(); 8718 } else if (Clauses[0]->getClauseKind() != OMPC_depobj) { 8719 Diag(Clauses[0]->getBeginLoc(), diag::err_omp_depobj_expected); 8720 return StmtError(); 8721 } 8722 // Only depobj expression and another single clause is allowed. 8723 if (Clauses.size() > 2) { 8724 Diag(Clauses[2]->getBeginLoc(), 8725 diag::err_omp_depobj_single_clause_expected); 8726 return StmtError(); 8727 } else if (Clauses.size() < 1) { 8728 Diag(Clauses[0]->getEndLoc(), diag::err_omp_depobj_single_clause_expected); 8729 return StmtError(); 8730 } 8731 return OMPDepobjDirective::Create(Context, StartLoc, EndLoc, Clauses); 8732 } 8733 8734 StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses, 8735 Stmt *AStmt, 8736 SourceLocation StartLoc, 8737 SourceLocation EndLoc) { 8738 const OMPClause *DependFound = nullptr; 8739 const OMPClause *DependSourceClause = nullptr; 8740 const OMPClause *DependSinkClause = nullptr; 8741 bool ErrorFound = false; 8742 const OMPThreadsClause *TC = nullptr; 8743 const OMPSIMDClause *SC = nullptr; 8744 for (const OMPClause *C : Clauses) { 8745 if (auto *DC = dyn_cast<OMPDependClause>(C)) { 8746 DependFound = C; 8747 if (DC->getDependencyKind() == OMPC_DEPEND_source) { 8748 if (DependSourceClause) { 8749 Diag(C->getBeginLoc(), diag::err_omp_more_one_clause) 8750 << getOpenMPDirectiveName(OMPD_ordered) 8751 << getOpenMPClauseName(OMPC_depend) << 2; 8752 ErrorFound = true; 8753 } else { 8754 DependSourceClause = C; 8755 } 8756 if (DependSinkClause) { 8757 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed) 8758 << 0; 8759 ErrorFound = true; 8760 } 8761 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) { 8762 if (DependSourceClause) { 8763 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed) 8764 << 1; 8765 ErrorFound = true; 8766 } 8767 DependSinkClause = C; 8768 } 8769 } else if (C->getClauseKind() == OMPC_threads) { 8770 TC = cast<OMPThreadsClause>(C); 8771 } else if (C->getClauseKind() == OMPC_simd) { 8772 SC = cast<OMPSIMDClause>(C); 8773 } 8774 } 8775 if (!ErrorFound && !SC && 8776 isOpenMPSimdDirective(DSAStack->getParentDirective())) { 8777 // OpenMP [2.8.1,simd Construct, Restrictions] 8778 // An ordered construct with the simd clause is the only OpenMP construct 8779 // that can appear in the simd region. 8780 Diag(StartLoc, diag::err_omp_prohibited_region_simd) 8781 << (LangOpts.OpenMP >= 50 ? 1 : 0); 8782 ErrorFound = true; 8783 } else if (DependFound && (TC || SC)) { 8784 Diag(DependFound->getBeginLoc(), diag::err_omp_depend_clause_thread_simd) 8785 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind()); 8786 ErrorFound = true; 8787 } else if (DependFound && !DSAStack->getParentOrderedRegionParam().first) { 8788 Diag(DependFound->getBeginLoc(), 8789 diag::err_omp_ordered_directive_without_param); 8790 ErrorFound = true; 8791 } else if (TC || Clauses.empty()) { 8792 if (const Expr *Param = DSAStack->getParentOrderedRegionParam().first) { 8793 SourceLocation ErrLoc = TC ? TC->getBeginLoc() : StartLoc; 8794 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param) 8795 << (TC != nullptr); 8796 Diag(Param->getBeginLoc(), diag::note_omp_ordered_param) << 1; 8797 ErrorFound = true; 8798 } 8799 } 8800 if ((!AStmt && !DependFound) || ErrorFound) 8801 return StmtError(); 8802 8803 if (AStmt) { 8804 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 8805 8806 setFunctionHasBranchProtectedScope(); 8807 } 8808 8809 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 8810 } 8811 8812 namespace { 8813 /// Helper class for checking expression in 'omp atomic [update]' 8814 /// construct. 8815 class OpenMPAtomicUpdateChecker { 8816 /// Error results for atomic update expressions. 8817 enum ExprAnalysisErrorCode { 8818 /// A statement is not an expression statement. 8819 NotAnExpression, 8820 /// Expression is not builtin binary or unary operation. 8821 NotABinaryOrUnaryExpression, 8822 /// Unary operation is not post-/pre- increment/decrement operation. 8823 NotAnUnaryIncDecExpression, 8824 /// An expression is not of scalar type. 8825 NotAScalarType, 8826 /// A binary operation is not an assignment operation. 8827 NotAnAssignmentOp, 8828 /// RHS part of the binary operation is not a binary expression. 8829 NotABinaryExpression, 8830 /// RHS part is not additive/multiplicative/shift/biwise binary 8831 /// expression. 8832 NotABinaryOperator, 8833 /// RHS binary operation does not have reference to the updated LHS 8834 /// part. 8835 NotAnUpdateExpression, 8836 /// No errors is found. 8837 NoError 8838 }; 8839 /// Reference to Sema. 8840 Sema &SemaRef; 8841 /// A location for note diagnostics (when error is found). 8842 SourceLocation NoteLoc; 8843 /// 'x' lvalue part of the source atomic expression. 8844 Expr *X; 8845 /// 'expr' rvalue part of the source atomic expression. 8846 Expr *E; 8847 /// Helper expression of the form 8848 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or 8849 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. 8850 Expr *UpdateExpr; 8851 /// Is 'x' a LHS in a RHS part of full update expression. It is 8852 /// important for non-associative operations. 8853 bool IsXLHSInRHSPart; 8854 BinaryOperatorKind Op; 8855 SourceLocation OpLoc; 8856 /// true if the source expression is a postfix unary operation, false 8857 /// if it is a prefix unary operation. 8858 bool IsPostfixUpdate; 8859 8860 public: 8861 OpenMPAtomicUpdateChecker(Sema &SemaRef) 8862 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr), 8863 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {} 8864 /// Check specified statement that it is suitable for 'atomic update' 8865 /// constructs and extract 'x', 'expr' and Operation from the original 8866 /// expression. If DiagId and NoteId == 0, then only check is performed 8867 /// without error notification. 8868 /// \param DiagId Diagnostic which should be emitted if error is found. 8869 /// \param NoteId Diagnostic note for the main error message. 8870 /// \return true if statement is not an update expression, false otherwise. 8871 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0); 8872 /// Return the 'x' lvalue part of the source atomic expression. 8873 Expr *getX() const { return X; } 8874 /// Return the 'expr' rvalue part of the source atomic expression. 8875 Expr *getExpr() const { return E; } 8876 /// Return the update expression used in calculation of the updated 8877 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or 8878 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. 8879 Expr *getUpdateExpr() const { return UpdateExpr; } 8880 /// Return true if 'x' is LHS in RHS part of full update expression, 8881 /// false otherwise. 8882 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; } 8883 8884 /// true if the source expression is a postfix unary operation, false 8885 /// if it is a prefix unary operation. 8886 bool isPostfixUpdate() const { return IsPostfixUpdate; } 8887 8888 private: 8889 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0, 8890 unsigned NoteId = 0); 8891 }; 8892 } // namespace 8893 8894 bool OpenMPAtomicUpdateChecker::checkBinaryOperation( 8895 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) { 8896 ExprAnalysisErrorCode ErrorFound = NoError; 8897 SourceLocation ErrorLoc, NoteLoc; 8898 SourceRange ErrorRange, NoteRange; 8899 // Allowed constructs are: 8900 // x = x binop expr; 8901 // x = expr binop x; 8902 if (AtomicBinOp->getOpcode() == BO_Assign) { 8903 X = AtomicBinOp->getLHS(); 8904 if (const auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>( 8905 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) { 8906 if (AtomicInnerBinOp->isMultiplicativeOp() || 8907 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() || 8908 AtomicInnerBinOp->isBitwiseOp()) { 8909 Op = AtomicInnerBinOp->getOpcode(); 8910 OpLoc = AtomicInnerBinOp->getOperatorLoc(); 8911 Expr *LHS = AtomicInnerBinOp->getLHS(); 8912 Expr *RHS = AtomicInnerBinOp->getRHS(); 8913 llvm::FoldingSetNodeID XId, LHSId, RHSId; 8914 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(), 8915 /*Canonical=*/true); 8916 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(), 8917 /*Canonical=*/true); 8918 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(), 8919 /*Canonical=*/true); 8920 if (XId == LHSId) { 8921 E = RHS; 8922 IsXLHSInRHSPart = true; 8923 } else if (XId == RHSId) { 8924 E = LHS; 8925 IsXLHSInRHSPart = false; 8926 } else { 8927 ErrorLoc = AtomicInnerBinOp->getExprLoc(); 8928 ErrorRange = AtomicInnerBinOp->getSourceRange(); 8929 NoteLoc = X->getExprLoc(); 8930 NoteRange = X->getSourceRange(); 8931 ErrorFound = NotAnUpdateExpression; 8932 } 8933 } else { 8934 ErrorLoc = AtomicInnerBinOp->getExprLoc(); 8935 ErrorRange = AtomicInnerBinOp->getSourceRange(); 8936 NoteLoc = AtomicInnerBinOp->getOperatorLoc(); 8937 NoteRange = SourceRange(NoteLoc, NoteLoc); 8938 ErrorFound = NotABinaryOperator; 8939 } 8940 } else { 8941 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc(); 8942 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange(); 8943 ErrorFound = NotABinaryExpression; 8944 } 8945 } else { 8946 ErrorLoc = AtomicBinOp->getExprLoc(); 8947 ErrorRange = AtomicBinOp->getSourceRange(); 8948 NoteLoc = AtomicBinOp->getOperatorLoc(); 8949 NoteRange = SourceRange(NoteLoc, NoteLoc); 8950 ErrorFound = NotAnAssignmentOp; 8951 } 8952 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { 8953 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; 8954 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; 8955 return true; 8956 } 8957 if (SemaRef.CurContext->isDependentContext()) 8958 E = X = UpdateExpr = nullptr; 8959 return ErrorFound != NoError; 8960 } 8961 8962 bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId, 8963 unsigned NoteId) { 8964 ExprAnalysisErrorCode ErrorFound = NoError; 8965 SourceLocation ErrorLoc, NoteLoc; 8966 SourceRange ErrorRange, NoteRange; 8967 // Allowed constructs are: 8968 // x++; 8969 // x--; 8970 // ++x; 8971 // --x; 8972 // x binop= expr; 8973 // x = x binop expr; 8974 // x = expr binop x; 8975 if (auto *AtomicBody = dyn_cast<Expr>(S)) { 8976 AtomicBody = AtomicBody->IgnoreParenImpCasts(); 8977 if (AtomicBody->getType()->isScalarType() || 8978 AtomicBody->isInstantiationDependent()) { 8979 if (const auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>( 8980 AtomicBody->IgnoreParenImpCasts())) { 8981 // Check for Compound Assignment Operation 8982 Op = BinaryOperator::getOpForCompoundAssignment( 8983 AtomicCompAssignOp->getOpcode()); 8984 OpLoc = AtomicCompAssignOp->getOperatorLoc(); 8985 E = AtomicCompAssignOp->getRHS(); 8986 X = AtomicCompAssignOp->getLHS()->IgnoreParens(); 8987 IsXLHSInRHSPart = true; 8988 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>( 8989 AtomicBody->IgnoreParenImpCasts())) { 8990 // Check for Binary Operation 8991 if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId)) 8992 return true; 8993 } else if (const auto *AtomicUnaryOp = dyn_cast<UnaryOperator>( 8994 AtomicBody->IgnoreParenImpCasts())) { 8995 // Check for Unary Operation 8996 if (AtomicUnaryOp->isIncrementDecrementOp()) { 8997 IsPostfixUpdate = AtomicUnaryOp->isPostfix(); 8998 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub; 8999 OpLoc = AtomicUnaryOp->getOperatorLoc(); 9000 X = AtomicUnaryOp->getSubExpr()->IgnoreParens(); 9001 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get(); 9002 IsXLHSInRHSPart = true; 9003 } else { 9004 ErrorFound = NotAnUnaryIncDecExpression; 9005 ErrorLoc = AtomicUnaryOp->getExprLoc(); 9006 ErrorRange = AtomicUnaryOp->getSourceRange(); 9007 NoteLoc = AtomicUnaryOp->getOperatorLoc(); 9008 NoteRange = SourceRange(NoteLoc, NoteLoc); 9009 } 9010 } else if (!AtomicBody->isInstantiationDependent()) { 9011 ErrorFound = NotABinaryOrUnaryExpression; 9012 NoteLoc = ErrorLoc = AtomicBody->getExprLoc(); 9013 NoteRange = ErrorRange = AtomicBody->getSourceRange(); 9014 } 9015 } else { 9016 ErrorFound = NotAScalarType; 9017 NoteLoc = ErrorLoc = AtomicBody->getBeginLoc(); 9018 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 9019 } 9020 } else { 9021 ErrorFound = NotAnExpression; 9022 NoteLoc = ErrorLoc = S->getBeginLoc(); 9023 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 9024 } 9025 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { 9026 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; 9027 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; 9028 return true; 9029 } 9030 if (SemaRef.CurContext->isDependentContext()) 9031 E = X = UpdateExpr = nullptr; 9032 if (ErrorFound == NoError && E && X) { 9033 // Build an update expression of form 'OpaqueValueExpr(x) binop 9034 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop 9035 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression. 9036 auto *OVEX = new (SemaRef.getASTContext()) 9037 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue); 9038 auto *OVEExpr = new (SemaRef.getASTContext()) 9039 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue); 9040 ExprResult Update = 9041 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr, 9042 IsXLHSInRHSPart ? OVEExpr : OVEX); 9043 if (Update.isInvalid()) 9044 return true; 9045 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(), 9046 Sema::AA_Casting); 9047 if (Update.isInvalid()) 9048 return true; 9049 UpdateExpr = Update.get(); 9050 } 9051 return ErrorFound != NoError; 9052 } 9053 9054 StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses, 9055 Stmt *AStmt, 9056 SourceLocation StartLoc, 9057 SourceLocation EndLoc) { 9058 // Register location of the first atomic directive. 9059 DSAStack->addAtomicDirectiveLoc(StartLoc); 9060 if (!AStmt) 9061 return StmtError(); 9062 9063 auto *CS = cast<CapturedStmt>(AStmt); 9064 // 1.2.2 OpenMP Language Terminology 9065 // Structured block - An executable statement with a single entry at the 9066 // top and a single exit at the bottom. 9067 // The point of exit cannot be a branch out of the structured block. 9068 // longjmp() and throw() must not violate the entry/exit criteria. 9069 OpenMPClauseKind AtomicKind = OMPC_unknown; 9070 SourceLocation AtomicKindLoc; 9071 OpenMPClauseKind MemOrderKind = OMPC_unknown; 9072 SourceLocation MemOrderLoc; 9073 for (const OMPClause *C : Clauses) { 9074 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write || 9075 C->getClauseKind() == OMPC_update || 9076 C->getClauseKind() == OMPC_capture) { 9077 if (AtomicKind != OMPC_unknown) { 9078 Diag(C->getBeginLoc(), diag::err_omp_atomic_several_clauses) 9079 << SourceRange(C->getBeginLoc(), C->getEndLoc()); 9080 Diag(AtomicKindLoc, diag::note_omp_previous_mem_order_clause) 9081 << getOpenMPClauseName(AtomicKind); 9082 } else { 9083 AtomicKind = C->getClauseKind(); 9084 AtomicKindLoc = C->getBeginLoc(); 9085 } 9086 } 9087 if (C->getClauseKind() == OMPC_seq_cst || 9088 C->getClauseKind() == OMPC_acq_rel || 9089 C->getClauseKind() == OMPC_acquire || 9090 C->getClauseKind() == OMPC_release || 9091 C->getClauseKind() == OMPC_relaxed) { 9092 if (MemOrderKind != OMPC_unknown) { 9093 Diag(C->getBeginLoc(), diag::err_omp_several_mem_order_clauses) 9094 << getOpenMPDirectiveName(OMPD_atomic) << 0 9095 << SourceRange(C->getBeginLoc(), C->getEndLoc()); 9096 Diag(MemOrderLoc, diag::note_omp_previous_mem_order_clause) 9097 << getOpenMPClauseName(MemOrderKind); 9098 } else { 9099 MemOrderKind = C->getClauseKind(); 9100 MemOrderLoc = C->getBeginLoc(); 9101 } 9102 } 9103 } 9104 // OpenMP 5.0, 2.17.7 atomic Construct, Restrictions 9105 // If atomic-clause is read then memory-order-clause must not be acq_rel or 9106 // release. 9107 // If atomic-clause is write then memory-order-clause must not be acq_rel or 9108 // acquire. 9109 // If atomic-clause is update or not present then memory-order-clause must not 9110 // be acq_rel or acquire. 9111 if ((AtomicKind == OMPC_read && 9112 (MemOrderKind == OMPC_acq_rel || MemOrderKind == OMPC_release)) || 9113 ((AtomicKind == OMPC_write || AtomicKind == OMPC_update || 9114 AtomicKind == OMPC_unknown) && 9115 (MemOrderKind == OMPC_acq_rel || MemOrderKind == OMPC_acquire))) { 9116 SourceLocation Loc = AtomicKindLoc; 9117 if (AtomicKind == OMPC_unknown) 9118 Loc = StartLoc; 9119 Diag(Loc, diag::err_omp_atomic_incompatible_mem_order_clause) 9120 << getOpenMPClauseName(AtomicKind) 9121 << (AtomicKind == OMPC_unknown ? 1 : 0) 9122 << getOpenMPClauseName(MemOrderKind); 9123 Diag(MemOrderLoc, diag::note_omp_previous_mem_order_clause) 9124 << getOpenMPClauseName(MemOrderKind); 9125 } 9126 9127 Stmt *Body = CS->getCapturedStmt(); 9128 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body)) 9129 Body = EWC->getSubExpr(); 9130 9131 Expr *X = nullptr; 9132 Expr *V = nullptr; 9133 Expr *E = nullptr; 9134 Expr *UE = nullptr; 9135 bool IsXLHSInRHSPart = false; 9136 bool IsPostfixUpdate = false; 9137 // OpenMP [2.12.6, atomic Construct] 9138 // In the next expressions: 9139 // * x and v (as applicable) are both l-value expressions with scalar type. 9140 // * During the execution of an atomic region, multiple syntactic 9141 // occurrences of x must designate the same storage location. 9142 // * Neither of v and expr (as applicable) may access the storage location 9143 // designated by x. 9144 // * Neither of x and expr (as applicable) may access the storage location 9145 // designated by v. 9146 // * expr is an expression with scalar type. 9147 // * binop is one of +, *, -, /, &, ^, |, <<, or >>. 9148 // * binop, binop=, ++, and -- are not overloaded operators. 9149 // * The expression x binop expr must be numerically equivalent to x binop 9150 // (expr). This requirement is satisfied if the operators in expr have 9151 // precedence greater than binop, or by using parentheses around expr or 9152 // subexpressions of expr. 9153 // * The expression expr binop x must be numerically equivalent to (expr) 9154 // binop x. This requirement is satisfied if the operators in expr have 9155 // precedence equal to or greater than binop, or by using parentheses around 9156 // expr or subexpressions of expr. 9157 // * For forms that allow multiple occurrences of x, the number of times 9158 // that x is evaluated is unspecified. 9159 if (AtomicKind == OMPC_read) { 9160 enum { 9161 NotAnExpression, 9162 NotAnAssignmentOp, 9163 NotAScalarType, 9164 NotAnLValue, 9165 NoError 9166 } ErrorFound = NoError; 9167 SourceLocation ErrorLoc, NoteLoc; 9168 SourceRange ErrorRange, NoteRange; 9169 // If clause is read: 9170 // v = x; 9171 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 9172 const auto *AtomicBinOp = 9173 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 9174 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 9175 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); 9176 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts(); 9177 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && 9178 (V->isInstantiationDependent() || V->getType()->isScalarType())) { 9179 if (!X->isLValue() || !V->isLValue()) { 9180 const Expr *NotLValueExpr = X->isLValue() ? V : X; 9181 ErrorFound = NotAnLValue; 9182 ErrorLoc = AtomicBinOp->getExprLoc(); 9183 ErrorRange = AtomicBinOp->getSourceRange(); 9184 NoteLoc = NotLValueExpr->getExprLoc(); 9185 NoteRange = NotLValueExpr->getSourceRange(); 9186 } 9187 } else if (!X->isInstantiationDependent() || 9188 !V->isInstantiationDependent()) { 9189 const Expr *NotScalarExpr = 9190 (X->isInstantiationDependent() || X->getType()->isScalarType()) 9191 ? V 9192 : X; 9193 ErrorFound = NotAScalarType; 9194 ErrorLoc = AtomicBinOp->getExprLoc(); 9195 ErrorRange = AtomicBinOp->getSourceRange(); 9196 NoteLoc = NotScalarExpr->getExprLoc(); 9197 NoteRange = NotScalarExpr->getSourceRange(); 9198 } 9199 } else if (!AtomicBody->isInstantiationDependent()) { 9200 ErrorFound = NotAnAssignmentOp; 9201 ErrorLoc = AtomicBody->getExprLoc(); 9202 ErrorRange = AtomicBody->getSourceRange(); 9203 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 9204 : AtomicBody->getExprLoc(); 9205 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 9206 : AtomicBody->getSourceRange(); 9207 } 9208 } else { 9209 ErrorFound = NotAnExpression; 9210 NoteLoc = ErrorLoc = Body->getBeginLoc(); 9211 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 9212 } 9213 if (ErrorFound != NoError) { 9214 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement) 9215 << ErrorRange; 9216 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound 9217 << NoteRange; 9218 return StmtError(); 9219 } 9220 if (CurContext->isDependentContext()) 9221 V = X = nullptr; 9222 } else if (AtomicKind == OMPC_write) { 9223 enum { 9224 NotAnExpression, 9225 NotAnAssignmentOp, 9226 NotAScalarType, 9227 NotAnLValue, 9228 NoError 9229 } ErrorFound = NoError; 9230 SourceLocation ErrorLoc, NoteLoc; 9231 SourceRange ErrorRange, NoteRange; 9232 // If clause is write: 9233 // x = expr; 9234 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 9235 const auto *AtomicBinOp = 9236 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 9237 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 9238 X = AtomicBinOp->getLHS(); 9239 E = AtomicBinOp->getRHS(); 9240 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && 9241 (E->isInstantiationDependent() || E->getType()->isScalarType())) { 9242 if (!X->isLValue()) { 9243 ErrorFound = NotAnLValue; 9244 ErrorLoc = AtomicBinOp->getExprLoc(); 9245 ErrorRange = AtomicBinOp->getSourceRange(); 9246 NoteLoc = X->getExprLoc(); 9247 NoteRange = X->getSourceRange(); 9248 } 9249 } else if (!X->isInstantiationDependent() || 9250 !E->isInstantiationDependent()) { 9251 const Expr *NotScalarExpr = 9252 (X->isInstantiationDependent() || X->getType()->isScalarType()) 9253 ? E 9254 : X; 9255 ErrorFound = NotAScalarType; 9256 ErrorLoc = AtomicBinOp->getExprLoc(); 9257 ErrorRange = AtomicBinOp->getSourceRange(); 9258 NoteLoc = NotScalarExpr->getExprLoc(); 9259 NoteRange = NotScalarExpr->getSourceRange(); 9260 } 9261 } else if (!AtomicBody->isInstantiationDependent()) { 9262 ErrorFound = NotAnAssignmentOp; 9263 ErrorLoc = AtomicBody->getExprLoc(); 9264 ErrorRange = AtomicBody->getSourceRange(); 9265 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 9266 : AtomicBody->getExprLoc(); 9267 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 9268 : AtomicBody->getSourceRange(); 9269 } 9270 } else { 9271 ErrorFound = NotAnExpression; 9272 NoteLoc = ErrorLoc = Body->getBeginLoc(); 9273 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 9274 } 9275 if (ErrorFound != NoError) { 9276 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement) 9277 << ErrorRange; 9278 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound 9279 << NoteRange; 9280 return StmtError(); 9281 } 9282 if (CurContext->isDependentContext()) 9283 E = X = nullptr; 9284 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) { 9285 // If clause is update: 9286 // x++; 9287 // x--; 9288 // ++x; 9289 // --x; 9290 // x binop= expr; 9291 // x = x binop expr; 9292 // x = expr binop x; 9293 OpenMPAtomicUpdateChecker Checker(*this); 9294 if (Checker.checkStatement( 9295 Body, (AtomicKind == OMPC_update) 9296 ? diag::err_omp_atomic_update_not_expression_statement 9297 : diag::err_omp_atomic_not_expression_statement, 9298 diag::note_omp_atomic_update)) 9299 return StmtError(); 9300 if (!CurContext->isDependentContext()) { 9301 E = Checker.getExpr(); 9302 X = Checker.getX(); 9303 UE = Checker.getUpdateExpr(); 9304 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 9305 } 9306 } else if (AtomicKind == OMPC_capture) { 9307 enum { 9308 NotAnAssignmentOp, 9309 NotACompoundStatement, 9310 NotTwoSubstatements, 9311 NotASpecificExpression, 9312 NoError 9313 } ErrorFound = NoError; 9314 SourceLocation ErrorLoc, NoteLoc; 9315 SourceRange ErrorRange, NoteRange; 9316 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 9317 // If clause is a capture: 9318 // v = x++; 9319 // v = x--; 9320 // v = ++x; 9321 // v = --x; 9322 // v = x binop= expr; 9323 // v = x = x binop expr; 9324 // v = x = expr binop x; 9325 const auto *AtomicBinOp = 9326 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 9327 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 9328 V = AtomicBinOp->getLHS(); 9329 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); 9330 OpenMPAtomicUpdateChecker Checker(*this); 9331 if (Checker.checkStatement( 9332 Body, diag::err_omp_atomic_capture_not_expression_statement, 9333 diag::note_omp_atomic_update)) 9334 return StmtError(); 9335 E = Checker.getExpr(); 9336 X = Checker.getX(); 9337 UE = Checker.getUpdateExpr(); 9338 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 9339 IsPostfixUpdate = Checker.isPostfixUpdate(); 9340 } else if (!AtomicBody->isInstantiationDependent()) { 9341 ErrorLoc = AtomicBody->getExprLoc(); 9342 ErrorRange = AtomicBody->getSourceRange(); 9343 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 9344 : AtomicBody->getExprLoc(); 9345 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 9346 : AtomicBody->getSourceRange(); 9347 ErrorFound = NotAnAssignmentOp; 9348 } 9349 if (ErrorFound != NoError) { 9350 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement) 9351 << ErrorRange; 9352 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; 9353 return StmtError(); 9354 } 9355 if (CurContext->isDependentContext()) 9356 UE = V = E = X = nullptr; 9357 } else { 9358 // If clause is a capture: 9359 // { v = x; x = expr; } 9360 // { v = x; x++; } 9361 // { v = x; x--; } 9362 // { v = x; ++x; } 9363 // { v = x; --x; } 9364 // { v = x; x binop= expr; } 9365 // { v = x; x = x binop expr; } 9366 // { v = x; x = expr binop x; } 9367 // { x++; v = x; } 9368 // { x--; v = x; } 9369 // { ++x; v = x; } 9370 // { --x; v = x; } 9371 // { x binop= expr; v = x; } 9372 // { x = x binop expr; v = x; } 9373 // { x = expr binop x; v = x; } 9374 if (auto *CS = dyn_cast<CompoundStmt>(Body)) { 9375 // Check that this is { expr1; expr2; } 9376 if (CS->size() == 2) { 9377 Stmt *First = CS->body_front(); 9378 Stmt *Second = CS->body_back(); 9379 if (auto *EWC = dyn_cast<ExprWithCleanups>(First)) 9380 First = EWC->getSubExpr()->IgnoreParenImpCasts(); 9381 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second)) 9382 Second = EWC->getSubExpr()->IgnoreParenImpCasts(); 9383 // Need to find what subexpression is 'v' and what is 'x'. 9384 OpenMPAtomicUpdateChecker Checker(*this); 9385 bool IsUpdateExprFound = !Checker.checkStatement(Second); 9386 BinaryOperator *BinOp = nullptr; 9387 if (IsUpdateExprFound) { 9388 BinOp = dyn_cast<BinaryOperator>(First); 9389 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; 9390 } 9391 if (IsUpdateExprFound && !CurContext->isDependentContext()) { 9392 // { v = x; x++; } 9393 // { v = x; x--; } 9394 // { v = x; ++x; } 9395 // { v = x; --x; } 9396 // { v = x; x binop= expr; } 9397 // { v = x; x = x binop expr; } 9398 // { v = x; x = expr binop x; } 9399 // Check that the first expression has form v = x. 9400 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); 9401 llvm::FoldingSetNodeID XId, PossibleXId; 9402 Checker.getX()->Profile(XId, Context, /*Canonical=*/true); 9403 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); 9404 IsUpdateExprFound = XId == PossibleXId; 9405 if (IsUpdateExprFound) { 9406 V = BinOp->getLHS(); 9407 X = Checker.getX(); 9408 E = Checker.getExpr(); 9409 UE = Checker.getUpdateExpr(); 9410 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 9411 IsPostfixUpdate = true; 9412 } 9413 } 9414 if (!IsUpdateExprFound) { 9415 IsUpdateExprFound = !Checker.checkStatement(First); 9416 BinOp = nullptr; 9417 if (IsUpdateExprFound) { 9418 BinOp = dyn_cast<BinaryOperator>(Second); 9419 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; 9420 } 9421 if (IsUpdateExprFound && !CurContext->isDependentContext()) { 9422 // { x++; v = x; } 9423 // { x--; v = x; } 9424 // { ++x; v = x; } 9425 // { --x; v = x; } 9426 // { x binop= expr; v = x; } 9427 // { x = x binop expr; v = x; } 9428 // { x = expr binop x; v = x; } 9429 // Check that the second expression has form v = x. 9430 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); 9431 llvm::FoldingSetNodeID XId, PossibleXId; 9432 Checker.getX()->Profile(XId, Context, /*Canonical=*/true); 9433 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); 9434 IsUpdateExprFound = XId == PossibleXId; 9435 if (IsUpdateExprFound) { 9436 V = BinOp->getLHS(); 9437 X = Checker.getX(); 9438 E = Checker.getExpr(); 9439 UE = Checker.getUpdateExpr(); 9440 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 9441 IsPostfixUpdate = false; 9442 } 9443 } 9444 } 9445 if (!IsUpdateExprFound) { 9446 // { v = x; x = expr; } 9447 auto *FirstExpr = dyn_cast<Expr>(First); 9448 auto *SecondExpr = dyn_cast<Expr>(Second); 9449 if (!FirstExpr || !SecondExpr || 9450 !(FirstExpr->isInstantiationDependent() || 9451 SecondExpr->isInstantiationDependent())) { 9452 auto *FirstBinOp = dyn_cast<BinaryOperator>(First); 9453 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) { 9454 ErrorFound = NotAnAssignmentOp; 9455 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc() 9456 : First->getBeginLoc(); 9457 NoteRange = ErrorRange = FirstBinOp 9458 ? FirstBinOp->getSourceRange() 9459 : SourceRange(ErrorLoc, ErrorLoc); 9460 } else { 9461 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second); 9462 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) { 9463 ErrorFound = NotAnAssignmentOp; 9464 NoteLoc = ErrorLoc = SecondBinOp 9465 ? SecondBinOp->getOperatorLoc() 9466 : Second->getBeginLoc(); 9467 NoteRange = ErrorRange = 9468 SecondBinOp ? SecondBinOp->getSourceRange() 9469 : SourceRange(ErrorLoc, ErrorLoc); 9470 } else { 9471 Expr *PossibleXRHSInFirst = 9472 FirstBinOp->getRHS()->IgnoreParenImpCasts(); 9473 Expr *PossibleXLHSInSecond = 9474 SecondBinOp->getLHS()->IgnoreParenImpCasts(); 9475 llvm::FoldingSetNodeID X1Id, X2Id; 9476 PossibleXRHSInFirst->Profile(X1Id, Context, 9477 /*Canonical=*/true); 9478 PossibleXLHSInSecond->Profile(X2Id, Context, 9479 /*Canonical=*/true); 9480 IsUpdateExprFound = X1Id == X2Id; 9481 if (IsUpdateExprFound) { 9482 V = FirstBinOp->getLHS(); 9483 X = SecondBinOp->getLHS(); 9484 E = SecondBinOp->getRHS(); 9485 UE = nullptr; 9486 IsXLHSInRHSPart = false; 9487 IsPostfixUpdate = true; 9488 } else { 9489 ErrorFound = NotASpecificExpression; 9490 ErrorLoc = FirstBinOp->getExprLoc(); 9491 ErrorRange = FirstBinOp->getSourceRange(); 9492 NoteLoc = SecondBinOp->getLHS()->getExprLoc(); 9493 NoteRange = SecondBinOp->getRHS()->getSourceRange(); 9494 } 9495 } 9496 } 9497 } 9498 } 9499 } else { 9500 NoteLoc = ErrorLoc = Body->getBeginLoc(); 9501 NoteRange = ErrorRange = 9502 SourceRange(Body->getBeginLoc(), Body->getBeginLoc()); 9503 ErrorFound = NotTwoSubstatements; 9504 } 9505 } else { 9506 NoteLoc = ErrorLoc = Body->getBeginLoc(); 9507 NoteRange = ErrorRange = 9508 SourceRange(Body->getBeginLoc(), Body->getBeginLoc()); 9509 ErrorFound = NotACompoundStatement; 9510 } 9511 if (ErrorFound != NoError) { 9512 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement) 9513 << ErrorRange; 9514 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; 9515 return StmtError(); 9516 } 9517 if (CurContext->isDependentContext()) 9518 UE = V = E = X = nullptr; 9519 } 9520 } 9521 9522 setFunctionHasBranchProtectedScope(); 9523 9524 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 9525 X, V, E, UE, IsXLHSInRHSPart, 9526 IsPostfixUpdate); 9527 } 9528 9529 StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses, 9530 Stmt *AStmt, 9531 SourceLocation StartLoc, 9532 SourceLocation EndLoc) { 9533 if (!AStmt) 9534 return StmtError(); 9535 9536 auto *CS = cast<CapturedStmt>(AStmt); 9537 // 1.2.2 OpenMP Language Terminology 9538 // Structured block - An executable statement with a single entry at the 9539 // top and a single exit at the bottom. 9540 // The point of exit cannot be a branch out of the structured block. 9541 // longjmp() and throw() must not violate the entry/exit criteria. 9542 CS->getCapturedDecl()->setNothrow(); 9543 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target); 9544 ThisCaptureLevel > 1; --ThisCaptureLevel) { 9545 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 9546 // 1.2.2 OpenMP Language Terminology 9547 // Structured block - An executable statement with a single entry at the 9548 // top and a single exit at the bottom. 9549 // The point of exit cannot be a branch out of the structured block. 9550 // longjmp() and throw() must not violate the entry/exit criteria. 9551 CS->getCapturedDecl()->setNothrow(); 9552 } 9553 9554 // OpenMP [2.16, Nesting of Regions] 9555 // If specified, a teams construct must be contained within a target 9556 // construct. That target construct must contain no statements or directives 9557 // outside of the teams construct. 9558 if (DSAStack->hasInnerTeamsRegion()) { 9559 const Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true); 9560 bool OMPTeamsFound = true; 9561 if (const auto *CS = dyn_cast<CompoundStmt>(S)) { 9562 auto I = CS->body_begin(); 9563 while (I != CS->body_end()) { 9564 const auto *OED = dyn_cast<OMPExecutableDirective>(*I); 9565 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind()) || 9566 OMPTeamsFound) { 9567 9568 OMPTeamsFound = false; 9569 break; 9570 } 9571 ++I; 9572 } 9573 assert(I != CS->body_end() && "Not found statement"); 9574 S = *I; 9575 } else { 9576 const auto *OED = dyn_cast<OMPExecutableDirective>(S); 9577 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind()); 9578 } 9579 if (!OMPTeamsFound) { 9580 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams); 9581 Diag(DSAStack->getInnerTeamsRegionLoc(), 9582 diag::note_omp_nested_teams_construct_here); 9583 Diag(S->getBeginLoc(), diag::note_omp_nested_statement_here) 9584 << isa<OMPExecutableDirective>(S); 9585 return StmtError(); 9586 } 9587 } 9588 9589 setFunctionHasBranchProtectedScope(); 9590 9591 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 9592 } 9593 9594 StmtResult 9595 Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses, 9596 Stmt *AStmt, SourceLocation StartLoc, 9597 SourceLocation EndLoc) { 9598 if (!AStmt) 9599 return StmtError(); 9600 9601 auto *CS = cast<CapturedStmt>(AStmt); 9602 // 1.2.2 OpenMP Language Terminology 9603 // Structured block - An executable statement with a single entry at the 9604 // top and a single exit at the bottom. 9605 // The point of exit cannot be a branch out of the structured block. 9606 // longjmp() and throw() must not violate the entry/exit criteria. 9607 CS->getCapturedDecl()->setNothrow(); 9608 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel); 9609 ThisCaptureLevel > 1; --ThisCaptureLevel) { 9610 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 9611 // 1.2.2 OpenMP Language Terminology 9612 // Structured block - An executable statement with a single entry at the 9613 // top and a single exit at the bottom. 9614 // The point of exit cannot be a branch out of the structured block. 9615 // longjmp() and throw() must not violate the entry/exit criteria. 9616 CS->getCapturedDecl()->setNothrow(); 9617 } 9618 9619 setFunctionHasBranchProtectedScope(); 9620 9621 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, 9622 AStmt); 9623 } 9624 9625 StmtResult Sema::ActOnOpenMPTargetParallelForDirective( 9626 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 9627 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 9628 if (!AStmt) 9629 return StmtError(); 9630 9631 auto *CS = cast<CapturedStmt>(AStmt); 9632 // 1.2.2 OpenMP Language Terminology 9633 // Structured block - An executable statement with a single entry at the 9634 // top and a single exit at the bottom. 9635 // The point of exit cannot be a branch out of the structured block. 9636 // longjmp() and throw() must not violate the entry/exit criteria. 9637 CS->getCapturedDecl()->setNothrow(); 9638 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for); 9639 ThisCaptureLevel > 1; --ThisCaptureLevel) { 9640 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 9641 // 1.2.2 OpenMP Language Terminology 9642 // Structured block - An executable statement with a single entry at the 9643 // top and a single exit at the bottom. 9644 // The point of exit cannot be a branch out of the structured block. 9645 // longjmp() and throw() must not violate the entry/exit criteria. 9646 CS->getCapturedDecl()->setNothrow(); 9647 } 9648 9649 OMPLoopDirective::HelperExprs B; 9650 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 9651 // define the nested loops number. 9652 unsigned NestedLoopCount = 9653 checkOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses), 9654 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 9655 VarsWithImplicitDSA, B); 9656 if (NestedLoopCount == 0) 9657 return StmtError(); 9658 9659 assert((CurContext->isDependentContext() || B.builtAll()) && 9660 "omp target parallel for loop exprs were not built"); 9661 9662 if (!CurContext->isDependentContext()) { 9663 // Finalize the clauses that need pre-built expressions for CodeGen. 9664 for (OMPClause *C : Clauses) { 9665 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 9666 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 9667 B.NumIterations, *this, CurScope, 9668 DSAStack)) 9669 return StmtError(); 9670 } 9671 } 9672 9673 setFunctionHasBranchProtectedScope(); 9674 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc, 9675 NestedLoopCount, Clauses, AStmt, 9676 B, DSAStack->isCancelRegion()); 9677 } 9678 9679 /// Check for existence of a map clause in the list of clauses. 9680 static bool hasClauses(ArrayRef<OMPClause *> Clauses, 9681 const OpenMPClauseKind K) { 9682 return llvm::any_of( 9683 Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; }); 9684 } 9685 9686 template <typename... Params> 9687 static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K, 9688 const Params... ClauseTypes) { 9689 return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...); 9690 } 9691 9692 StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses, 9693 Stmt *AStmt, 9694 SourceLocation StartLoc, 9695 SourceLocation EndLoc) { 9696 if (!AStmt) 9697 return StmtError(); 9698 9699 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9700 9701 // OpenMP [2.10.1, Restrictions, p. 97] 9702 // At least one map clause must appear on the directive. 9703 if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr)) { 9704 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 9705 << "'map' or 'use_device_ptr'" 9706 << getOpenMPDirectiveName(OMPD_target_data); 9707 return StmtError(); 9708 } 9709 9710 setFunctionHasBranchProtectedScope(); 9711 9712 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 9713 AStmt); 9714 } 9715 9716 StmtResult 9717 Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses, 9718 SourceLocation StartLoc, 9719 SourceLocation EndLoc, Stmt *AStmt) { 9720 if (!AStmt) 9721 return StmtError(); 9722 9723 auto *CS = cast<CapturedStmt>(AStmt); 9724 // 1.2.2 OpenMP Language Terminology 9725 // Structured block - An executable statement with a single entry at the 9726 // top and a single exit at the bottom. 9727 // The point of exit cannot be a branch out of the structured block. 9728 // longjmp() and throw() must not violate the entry/exit criteria. 9729 CS->getCapturedDecl()->setNothrow(); 9730 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_enter_data); 9731 ThisCaptureLevel > 1; --ThisCaptureLevel) { 9732 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 9733 // 1.2.2 OpenMP Language Terminology 9734 // Structured block - An executable statement with a single entry at the 9735 // top and a single exit at the bottom. 9736 // The point of exit cannot be a branch out of the structured block. 9737 // longjmp() and throw() must not violate the entry/exit criteria. 9738 CS->getCapturedDecl()->setNothrow(); 9739 } 9740 9741 // OpenMP [2.10.2, Restrictions, p. 99] 9742 // At least one map clause must appear on the directive. 9743 if (!hasClauses(Clauses, OMPC_map)) { 9744 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 9745 << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data); 9746 return StmtError(); 9747 } 9748 9749 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 9750 AStmt); 9751 } 9752 9753 StmtResult 9754 Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses, 9755 SourceLocation StartLoc, 9756 SourceLocation EndLoc, Stmt *AStmt) { 9757 if (!AStmt) 9758 return StmtError(); 9759 9760 auto *CS = cast<CapturedStmt>(AStmt); 9761 // 1.2.2 OpenMP Language Terminology 9762 // Structured block - An executable statement with a single entry at the 9763 // top and a single exit at the bottom. 9764 // The point of exit cannot be a branch out of the structured block. 9765 // longjmp() and throw() must not violate the entry/exit criteria. 9766 CS->getCapturedDecl()->setNothrow(); 9767 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_exit_data); 9768 ThisCaptureLevel > 1; --ThisCaptureLevel) { 9769 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 9770 // 1.2.2 OpenMP Language Terminology 9771 // Structured block - An executable statement with a single entry at the 9772 // top and a single exit at the bottom. 9773 // The point of exit cannot be a branch out of the structured block. 9774 // longjmp() and throw() must not violate the entry/exit criteria. 9775 CS->getCapturedDecl()->setNothrow(); 9776 } 9777 9778 // OpenMP [2.10.3, Restrictions, p. 102] 9779 // At least one map clause must appear on the directive. 9780 if (!hasClauses(Clauses, OMPC_map)) { 9781 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 9782 << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data); 9783 return StmtError(); 9784 } 9785 9786 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 9787 AStmt); 9788 } 9789 9790 StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses, 9791 SourceLocation StartLoc, 9792 SourceLocation EndLoc, 9793 Stmt *AStmt) { 9794 if (!AStmt) 9795 return StmtError(); 9796 9797 auto *CS = cast<CapturedStmt>(AStmt); 9798 // 1.2.2 OpenMP Language Terminology 9799 // Structured block - An executable statement with a single entry at the 9800 // top and a single exit at the bottom. 9801 // The point of exit cannot be a branch out of the structured block. 9802 // longjmp() and throw() must not violate the entry/exit criteria. 9803 CS->getCapturedDecl()->setNothrow(); 9804 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_update); 9805 ThisCaptureLevel > 1; --ThisCaptureLevel) { 9806 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 9807 // 1.2.2 OpenMP Language Terminology 9808 // Structured block - An executable statement with a single entry at the 9809 // top and a single exit at the bottom. 9810 // The point of exit cannot be a branch out of the structured block. 9811 // longjmp() and throw() must not violate the entry/exit criteria. 9812 CS->getCapturedDecl()->setNothrow(); 9813 } 9814 9815 if (!hasClauses(Clauses, OMPC_to, OMPC_from)) { 9816 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required); 9817 return StmtError(); 9818 } 9819 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses, 9820 AStmt); 9821 } 9822 9823 StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses, 9824 Stmt *AStmt, SourceLocation StartLoc, 9825 SourceLocation EndLoc) { 9826 if (!AStmt) 9827 return StmtError(); 9828 9829 auto *CS = cast<CapturedStmt>(AStmt); 9830 // 1.2.2 OpenMP Language Terminology 9831 // Structured block - An executable statement with a single entry at the 9832 // top and a single exit at the bottom. 9833 // The point of exit cannot be a branch out of the structured block. 9834 // longjmp() and throw() must not violate the entry/exit criteria. 9835 CS->getCapturedDecl()->setNothrow(); 9836 9837 setFunctionHasBranchProtectedScope(); 9838 9839 DSAStack->setParentTeamsRegionLoc(StartLoc); 9840 9841 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 9842 } 9843 9844 StmtResult 9845 Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc, 9846 SourceLocation EndLoc, 9847 OpenMPDirectiveKind CancelRegion) { 9848 if (DSAStack->isParentNowaitRegion()) { 9849 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0; 9850 return StmtError(); 9851 } 9852 if (DSAStack->isParentOrderedRegion()) { 9853 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0; 9854 return StmtError(); 9855 } 9856 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc, 9857 CancelRegion); 9858 } 9859 9860 StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses, 9861 SourceLocation StartLoc, 9862 SourceLocation EndLoc, 9863 OpenMPDirectiveKind CancelRegion) { 9864 if (DSAStack->isParentNowaitRegion()) { 9865 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1; 9866 return StmtError(); 9867 } 9868 if (DSAStack->isParentOrderedRegion()) { 9869 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1; 9870 return StmtError(); 9871 } 9872 DSAStack->setParentCancelRegion(/*Cancel=*/true); 9873 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses, 9874 CancelRegion); 9875 } 9876 9877 static bool checkGrainsizeNumTasksClauses(Sema &S, 9878 ArrayRef<OMPClause *> Clauses) { 9879 const OMPClause *PrevClause = nullptr; 9880 bool ErrorFound = false; 9881 for (const OMPClause *C : Clauses) { 9882 if (C->getClauseKind() == OMPC_grainsize || 9883 C->getClauseKind() == OMPC_num_tasks) { 9884 if (!PrevClause) 9885 PrevClause = C; 9886 else if (PrevClause->getClauseKind() != C->getClauseKind()) { 9887 S.Diag(C->getBeginLoc(), 9888 diag::err_omp_grainsize_num_tasks_mutually_exclusive) 9889 << getOpenMPClauseName(C->getClauseKind()) 9890 << getOpenMPClauseName(PrevClause->getClauseKind()); 9891 S.Diag(PrevClause->getBeginLoc(), 9892 diag::note_omp_previous_grainsize_num_tasks) 9893 << getOpenMPClauseName(PrevClause->getClauseKind()); 9894 ErrorFound = true; 9895 } 9896 } 9897 } 9898 return ErrorFound; 9899 } 9900 9901 static bool checkReductionClauseWithNogroup(Sema &S, 9902 ArrayRef<OMPClause *> Clauses) { 9903 const OMPClause *ReductionClause = nullptr; 9904 const OMPClause *NogroupClause = nullptr; 9905 for (const OMPClause *C : Clauses) { 9906 if (C->getClauseKind() == OMPC_reduction) { 9907 ReductionClause = C; 9908 if (NogroupClause) 9909 break; 9910 continue; 9911 } 9912 if (C->getClauseKind() == OMPC_nogroup) { 9913 NogroupClause = C; 9914 if (ReductionClause) 9915 break; 9916 continue; 9917 } 9918 } 9919 if (ReductionClause && NogroupClause) { 9920 S.Diag(ReductionClause->getBeginLoc(), diag::err_omp_reduction_with_nogroup) 9921 << SourceRange(NogroupClause->getBeginLoc(), 9922 NogroupClause->getEndLoc()); 9923 return true; 9924 } 9925 return false; 9926 } 9927 9928 StmtResult Sema::ActOnOpenMPTaskLoopDirective( 9929 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 9930 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 9931 if (!AStmt) 9932 return StmtError(); 9933 9934 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9935 OMPLoopDirective::HelperExprs B; 9936 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 9937 // define the nested loops number. 9938 unsigned NestedLoopCount = 9939 checkOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses), 9940 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 9941 VarsWithImplicitDSA, B); 9942 if (NestedLoopCount == 0) 9943 return StmtError(); 9944 9945 assert((CurContext->isDependentContext() || B.builtAll()) && 9946 "omp for loop exprs were not built"); 9947 9948 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 9949 // The grainsize clause and num_tasks clause are mutually exclusive and may 9950 // not appear on the same taskloop directive. 9951 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 9952 return StmtError(); 9953 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 9954 // If a reduction clause is present on the taskloop directive, the nogroup 9955 // clause must not be specified. 9956 if (checkReductionClauseWithNogroup(*this, Clauses)) 9957 return StmtError(); 9958 9959 setFunctionHasBranchProtectedScope(); 9960 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc, 9961 NestedLoopCount, Clauses, AStmt, B, 9962 DSAStack->isCancelRegion()); 9963 } 9964 9965 StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective( 9966 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 9967 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 9968 if (!AStmt) 9969 return StmtError(); 9970 9971 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9972 OMPLoopDirective::HelperExprs B; 9973 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 9974 // define the nested loops number. 9975 unsigned NestedLoopCount = 9976 checkOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses), 9977 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 9978 VarsWithImplicitDSA, B); 9979 if (NestedLoopCount == 0) 9980 return StmtError(); 9981 9982 assert((CurContext->isDependentContext() || B.builtAll()) && 9983 "omp for loop exprs were not built"); 9984 9985 if (!CurContext->isDependentContext()) { 9986 // Finalize the clauses that need pre-built expressions for CodeGen. 9987 for (OMPClause *C : Clauses) { 9988 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 9989 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 9990 B.NumIterations, *this, CurScope, 9991 DSAStack)) 9992 return StmtError(); 9993 } 9994 } 9995 9996 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 9997 // The grainsize clause and num_tasks clause are mutually exclusive and may 9998 // not appear on the same taskloop directive. 9999 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 10000 return StmtError(); 10001 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10002 // If a reduction clause is present on the taskloop directive, the nogroup 10003 // clause must not be specified. 10004 if (checkReductionClauseWithNogroup(*this, Clauses)) 10005 return StmtError(); 10006 if (checkSimdlenSafelenSpecified(*this, Clauses)) 10007 return StmtError(); 10008 10009 setFunctionHasBranchProtectedScope(); 10010 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc, 10011 NestedLoopCount, Clauses, AStmt, B); 10012 } 10013 10014 StmtResult Sema::ActOnOpenMPMasterTaskLoopDirective( 10015 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10016 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10017 if (!AStmt) 10018 return StmtError(); 10019 10020 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10021 OMPLoopDirective::HelperExprs B; 10022 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 10023 // define the nested loops number. 10024 unsigned NestedLoopCount = 10025 checkOpenMPLoop(OMPD_master_taskloop, getCollapseNumberExpr(Clauses), 10026 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 10027 VarsWithImplicitDSA, B); 10028 if (NestedLoopCount == 0) 10029 return StmtError(); 10030 10031 assert((CurContext->isDependentContext() || B.builtAll()) && 10032 "omp for loop exprs were not built"); 10033 10034 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10035 // The grainsize clause and num_tasks clause are mutually exclusive and may 10036 // not appear on the same taskloop directive. 10037 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 10038 return StmtError(); 10039 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10040 // If a reduction clause is present on the taskloop directive, the nogroup 10041 // clause must not be specified. 10042 if (checkReductionClauseWithNogroup(*this, Clauses)) 10043 return StmtError(); 10044 10045 setFunctionHasBranchProtectedScope(); 10046 return OMPMasterTaskLoopDirective::Create(Context, StartLoc, EndLoc, 10047 NestedLoopCount, Clauses, AStmt, B, 10048 DSAStack->isCancelRegion()); 10049 } 10050 10051 StmtResult Sema::ActOnOpenMPMasterTaskLoopSimdDirective( 10052 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10053 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10054 if (!AStmt) 10055 return StmtError(); 10056 10057 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10058 OMPLoopDirective::HelperExprs B; 10059 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 10060 // define the nested loops number. 10061 unsigned NestedLoopCount = 10062 checkOpenMPLoop(OMPD_master_taskloop_simd, getCollapseNumberExpr(Clauses), 10063 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 10064 VarsWithImplicitDSA, B); 10065 if (NestedLoopCount == 0) 10066 return StmtError(); 10067 10068 assert((CurContext->isDependentContext() || B.builtAll()) && 10069 "omp for loop exprs were not built"); 10070 10071 if (!CurContext->isDependentContext()) { 10072 // Finalize the clauses that need pre-built expressions for CodeGen. 10073 for (OMPClause *C : Clauses) { 10074 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 10075 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 10076 B.NumIterations, *this, CurScope, 10077 DSAStack)) 10078 return StmtError(); 10079 } 10080 } 10081 10082 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10083 // The grainsize clause and num_tasks clause are mutually exclusive and may 10084 // not appear on the same taskloop directive. 10085 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 10086 return StmtError(); 10087 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10088 // If a reduction clause is present on the taskloop directive, the nogroup 10089 // clause must not be specified. 10090 if (checkReductionClauseWithNogroup(*this, Clauses)) 10091 return StmtError(); 10092 if (checkSimdlenSafelenSpecified(*this, Clauses)) 10093 return StmtError(); 10094 10095 setFunctionHasBranchProtectedScope(); 10096 return OMPMasterTaskLoopSimdDirective::Create( 10097 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 10098 } 10099 10100 StmtResult Sema::ActOnOpenMPParallelMasterTaskLoopDirective( 10101 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10102 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10103 if (!AStmt) 10104 return StmtError(); 10105 10106 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10107 auto *CS = cast<CapturedStmt>(AStmt); 10108 // 1.2.2 OpenMP Language Terminology 10109 // Structured block - An executable statement with a single entry at the 10110 // top and a single exit at the bottom. 10111 // The point of exit cannot be a branch out of the structured block. 10112 // longjmp() and throw() must not violate the entry/exit criteria. 10113 CS->getCapturedDecl()->setNothrow(); 10114 for (int ThisCaptureLevel = 10115 getOpenMPCaptureLevels(OMPD_parallel_master_taskloop); 10116 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10117 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10118 // 1.2.2 OpenMP Language Terminology 10119 // Structured block - An executable statement with a single entry at the 10120 // top and a single exit at the bottom. 10121 // The point of exit cannot be a branch out of the structured block. 10122 // longjmp() and throw() must not violate the entry/exit criteria. 10123 CS->getCapturedDecl()->setNothrow(); 10124 } 10125 10126 OMPLoopDirective::HelperExprs B; 10127 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 10128 // define the nested loops number. 10129 unsigned NestedLoopCount = checkOpenMPLoop( 10130 OMPD_parallel_master_taskloop, getCollapseNumberExpr(Clauses), 10131 /*OrderedLoopCountExpr=*/nullptr, CS, *this, *DSAStack, 10132 VarsWithImplicitDSA, B); 10133 if (NestedLoopCount == 0) 10134 return StmtError(); 10135 10136 assert((CurContext->isDependentContext() || B.builtAll()) && 10137 "omp for loop exprs were not built"); 10138 10139 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10140 // The grainsize clause and num_tasks clause are mutually exclusive and may 10141 // not appear on the same taskloop directive. 10142 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 10143 return StmtError(); 10144 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10145 // If a reduction clause is present on the taskloop directive, the nogroup 10146 // clause must not be specified. 10147 if (checkReductionClauseWithNogroup(*this, Clauses)) 10148 return StmtError(); 10149 10150 setFunctionHasBranchProtectedScope(); 10151 return OMPParallelMasterTaskLoopDirective::Create( 10152 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 10153 DSAStack->isCancelRegion()); 10154 } 10155 10156 StmtResult Sema::ActOnOpenMPParallelMasterTaskLoopSimdDirective( 10157 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10158 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10159 if (!AStmt) 10160 return StmtError(); 10161 10162 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10163 auto *CS = cast<CapturedStmt>(AStmt); 10164 // 1.2.2 OpenMP Language Terminology 10165 // Structured block - An executable statement with a single entry at the 10166 // top and a single exit at the bottom. 10167 // The point of exit cannot be a branch out of the structured block. 10168 // longjmp() and throw() must not violate the entry/exit criteria. 10169 CS->getCapturedDecl()->setNothrow(); 10170 for (int ThisCaptureLevel = 10171 getOpenMPCaptureLevels(OMPD_parallel_master_taskloop_simd); 10172 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10173 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10174 // 1.2.2 OpenMP Language Terminology 10175 // Structured block - An executable statement with a single entry at the 10176 // top and a single exit at the bottom. 10177 // The point of exit cannot be a branch out of the structured block. 10178 // longjmp() and throw() must not violate the entry/exit criteria. 10179 CS->getCapturedDecl()->setNothrow(); 10180 } 10181 10182 OMPLoopDirective::HelperExprs B; 10183 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 10184 // define the nested loops number. 10185 unsigned NestedLoopCount = checkOpenMPLoop( 10186 OMPD_parallel_master_taskloop_simd, getCollapseNumberExpr(Clauses), 10187 /*OrderedLoopCountExpr=*/nullptr, CS, *this, *DSAStack, 10188 VarsWithImplicitDSA, B); 10189 if (NestedLoopCount == 0) 10190 return StmtError(); 10191 10192 assert((CurContext->isDependentContext() || B.builtAll()) && 10193 "omp for loop exprs were not built"); 10194 10195 if (!CurContext->isDependentContext()) { 10196 // Finalize the clauses that need pre-built expressions for CodeGen. 10197 for (OMPClause *C : Clauses) { 10198 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 10199 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 10200 B.NumIterations, *this, CurScope, 10201 DSAStack)) 10202 return StmtError(); 10203 } 10204 } 10205 10206 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10207 // The grainsize clause and num_tasks clause are mutually exclusive and may 10208 // not appear on the same taskloop directive. 10209 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 10210 return StmtError(); 10211 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10212 // If a reduction clause is present on the taskloop directive, the nogroup 10213 // clause must not be specified. 10214 if (checkReductionClauseWithNogroup(*this, Clauses)) 10215 return StmtError(); 10216 if (checkSimdlenSafelenSpecified(*this, Clauses)) 10217 return StmtError(); 10218 10219 setFunctionHasBranchProtectedScope(); 10220 return OMPParallelMasterTaskLoopSimdDirective::Create( 10221 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 10222 } 10223 10224 StmtResult Sema::ActOnOpenMPDistributeDirective( 10225 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10226 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10227 if (!AStmt) 10228 return StmtError(); 10229 10230 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10231 OMPLoopDirective::HelperExprs B; 10232 // In presence of clause 'collapse' with number of loops, it will 10233 // define the nested loops number. 10234 unsigned NestedLoopCount = 10235 checkOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses), 10236 nullptr /*ordered not a clause on distribute*/, AStmt, 10237 *this, *DSAStack, VarsWithImplicitDSA, B); 10238 if (NestedLoopCount == 0) 10239 return StmtError(); 10240 10241 assert((CurContext->isDependentContext() || B.builtAll()) && 10242 "omp for loop exprs were not built"); 10243 10244 setFunctionHasBranchProtectedScope(); 10245 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc, 10246 NestedLoopCount, Clauses, AStmt, B); 10247 } 10248 10249 StmtResult Sema::ActOnOpenMPDistributeParallelForDirective( 10250 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10251 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10252 if (!AStmt) 10253 return StmtError(); 10254 10255 auto *CS = cast<CapturedStmt>(AStmt); 10256 // 1.2.2 OpenMP Language Terminology 10257 // Structured block - An executable statement with a single entry at the 10258 // top and a single exit at the bottom. 10259 // The point of exit cannot be a branch out of the structured block. 10260 // longjmp() and throw() must not violate the entry/exit criteria. 10261 CS->getCapturedDecl()->setNothrow(); 10262 for (int ThisCaptureLevel = 10263 getOpenMPCaptureLevels(OMPD_distribute_parallel_for); 10264 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10265 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10266 // 1.2.2 OpenMP Language Terminology 10267 // Structured block - An executable statement with a single entry at the 10268 // top and a single exit at the bottom. 10269 // The point of exit cannot be a branch out of the structured block. 10270 // longjmp() and throw() must not violate the entry/exit criteria. 10271 CS->getCapturedDecl()->setNothrow(); 10272 } 10273 10274 OMPLoopDirective::HelperExprs B; 10275 // In presence of clause 'collapse' with number of loops, it will 10276 // define the nested loops number. 10277 unsigned NestedLoopCount = checkOpenMPLoop( 10278 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses), 10279 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 10280 VarsWithImplicitDSA, B); 10281 if (NestedLoopCount == 0) 10282 return StmtError(); 10283 10284 assert((CurContext->isDependentContext() || B.builtAll()) && 10285 "omp for loop exprs were not built"); 10286 10287 setFunctionHasBranchProtectedScope(); 10288 return OMPDistributeParallelForDirective::Create( 10289 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 10290 DSAStack->isCancelRegion()); 10291 } 10292 10293 StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective( 10294 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10295 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10296 if (!AStmt) 10297 return StmtError(); 10298 10299 auto *CS = cast<CapturedStmt>(AStmt); 10300 // 1.2.2 OpenMP Language Terminology 10301 // Structured block - An executable statement with a single entry at the 10302 // top and a single exit at the bottom. 10303 // The point of exit cannot be a branch out of the structured block. 10304 // longjmp() and throw() must not violate the entry/exit criteria. 10305 CS->getCapturedDecl()->setNothrow(); 10306 for (int ThisCaptureLevel = 10307 getOpenMPCaptureLevels(OMPD_distribute_parallel_for_simd); 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 OMPLoopDirective::HelperExprs B; 10319 // In presence of clause 'collapse' with number of loops, it will 10320 // define the nested loops number. 10321 unsigned NestedLoopCount = checkOpenMPLoop( 10322 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses), 10323 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 10324 VarsWithImplicitDSA, B); 10325 if (NestedLoopCount == 0) 10326 return StmtError(); 10327 10328 assert((CurContext->isDependentContext() || B.builtAll()) && 10329 "omp for loop exprs were not built"); 10330 10331 if (!CurContext->isDependentContext()) { 10332 // Finalize the clauses that need pre-built expressions for CodeGen. 10333 for (OMPClause *C : Clauses) { 10334 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 10335 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 10336 B.NumIterations, *this, CurScope, 10337 DSAStack)) 10338 return StmtError(); 10339 } 10340 } 10341 10342 if (checkSimdlenSafelenSpecified(*this, Clauses)) 10343 return StmtError(); 10344 10345 setFunctionHasBranchProtectedScope(); 10346 return OMPDistributeParallelForSimdDirective::Create( 10347 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 10348 } 10349 10350 StmtResult Sema::ActOnOpenMPDistributeSimdDirective( 10351 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10352 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10353 if (!AStmt) 10354 return StmtError(); 10355 10356 auto *CS = cast<CapturedStmt>(AStmt); 10357 // 1.2.2 OpenMP Language Terminology 10358 // Structured block - An executable statement with a single entry at the 10359 // top and a single exit at the bottom. 10360 // The point of exit cannot be a branch out of the structured block. 10361 // longjmp() and throw() must not violate the entry/exit criteria. 10362 CS->getCapturedDecl()->setNothrow(); 10363 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_distribute_simd); 10364 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10365 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 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 } 10373 10374 OMPLoopDirective::HelperExprs B; 10375 // In presence of clause 'collapse' with number of loops, it will 10376 // define the nested loops number. 10377 unsigned NestedLoopCount = 10378 checkOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses), 10379 nullptr /*ordered not a clause on distribute*/, CS, *this, 10380 *DSAStack, VarsWithImplicitDSA, B); 10381 if (NestedLoopCount == 0) 10382 return StmtError(); 10383 10384 assert((CurContext->isDependentContext() || B.builtAll()) && 10385 "omp for loop exprs were not built"); 10386 10387 if (!CurContext->isDependentContext()) { 10388 // Finalize the clauses that need pre-built expressions for CodeGen. 10389 for (OMPClause *C : Clauses) { 10390 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 10391 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 10392 B.NumIterations, *this, CurScope, 10393 DSAStack)) 10394 return StmtError(); 10395 } 10396 } 10397 10398 if (checkSimdlenSafelenSpecified(*this, Clauses)) 10399 return StmtError(); 10400 10401 setFunctionHasBranchProtectedScope(); 10402 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc, 10403 NestedLoopCount, Clauses, AStmt, B); 10404 } 10405 10406 StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective( 10407 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10408 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10409 if (!AStmt) 10410 return StmtError(); 10411 10412 auto *CS = cast<CapturedStmt>(AStmt); 10413 // 1.2.2 OpenMP Language Terminology 10414 // Structured block - An executable statement with a single entry at the 10415 // top and a single exit at the bottom. 10416 // The point of exit cannot be a branch out of the structured block. 10417 // longjmp() and throw() must not violate the entry/exit criteria. 10418 CS->getCapturedDecl()->setNothrow(); 10419 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for); 10420 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10421 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10422 // 1.2.2 OpenMP Language Terminology 10423 // Structured block - An executable statement with a single entry at the 10424 // top and a single exit at the bottom. 10425 // The point of exit cannot be a branch out of the structured block. 10426 // longjmp() and throw() must not violate the entry/exit criteria. 10427 CS->getCapturedDecl()->setNothrow(); 10428 } 10429 10430 OMPLoopDirective::HelperExprs B; 10431 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 10432 // define the nested loops number. 10433 unsigned NestedLoopCount = checkOpenMPLoop( 10434 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses), 10435 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 10436 VarsWithImplicitDSA, B); 10437 if (NestedLoopCount == 0) 10438 return StmtError(); 10439 10440 assert((CurContext->isDependentContext() || B.builtAll()) && 10441 "omp target parallel for simd loop exprs were not built"); 10442 10443 if (!CurContext->isDependentContext()) { 10444 // Finalize the clauses that need pre-built expressions for CodeGen. 10445 for (OMPClause *C : Clauses) { 10446 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 10447 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 10448 B.NumIterations, *this, CurScope, 10449 DSAStack)) 10450 return StmtError(); 10451 } 10452 } 10453 if (checkSimdlenSafelenSpecified(*this, Clauses)) 10454 return StmtError(); 10455 10456 setFunctionHasBranchProtectedScope(); 10457 return OMPTargetParallelForSimdDirective::Create( 10458 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 10459 } 10460 10461 StmtResult Sema::ActOnOpenMPTargetSimdDirective( 10462 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10463 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10464 if (!AStmt) 10465 return StmtError(); 10466 10467 auto *CS = cast<CapturedStmt>(AStmt); 10468 // 1.2.2 OpenMP Language Terminology 10469 // Structured block - An executable statement with a single entry at the 10470 // top and a single exit at the bottom. 10471 // The point of exit cannot be a branch out of the structured block. 10472 // longjmp() and throw() must not violate the entry/exit criteria. 10473 CS->getCapturedDecl()->setNothrow(); 10474 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_simd); 10475 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10476 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10477 // 1.2.2 OpenMP Language Terminology 10478 // Structured block - An executable statement with a single entry at the 10479 // top and a single exit at the bottom. 10480 // The point of exit cannot be a branch out of the structured block. 10481 // longjmp() and throw() must not violate the entry/exit criteria. 10482 CS->getCapturedDecl()->setNothrow(); 10483 } 10484 10485 OMPLoopDirective::HelperExprs B; 10486 // In presence of clause 'collapse' with number of loops, it will define the 10487 // nested loops number. 10488 unsigned NestedLoopCount = 10489 checkOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses), 10490 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 10491 VarsWithImplicitDSA, B); 10492 if (NestedLoopCount == 0) 10493 return StmtError(); 10494 10495 assert((CurContext->isDependentContext() || B.builtAll()) && 10496 "omp target simd loop exprs were not built"); 10497 10498 if (!CurContext->isDependentContext()) { 10499 // Finalize the clauses that need pre-built expressions for CodeGen. 10500 for (OMPClause *C : Clauses) { 10501 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 10502 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 10503 B.NumIterations, *this, CurScope, 10504 DSAStack)) 10505 return StmtError(); 10506 } 10507 } 10508 10509 if (checkSimdlenSafelenSpecified(*this, Clauses)) 10510 return StmtError(); 10511 10512 setFunctionHasBranchProtectedScope(); 10513 return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc, 10514 NestedLoopCount, Clauses, AStmt, B); 10515 } 10516 10517 StmtResult Sema::ActOnOpenMPTeamsDistributeDirective( 10518 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10519 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10520 if (!AStmt) 10521 return StmtError(); 10522 10523 auto *CS = cast<CapturedStmt>(AStmt); 10524 // 1.2.2 OpenMP Language Terminology 10525 // Structured block - An executable statement with a single entry at the 10526 // top and a single exit at the bottom. 10527 // The point of exit cannot be a branch out of the structured block. 10528 // longjmp() and throw() must not violate the entry/exit criteria. 10529 CS->getCapturedDecl()->setNothrow(); 10530 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_teams_distribute); 10531 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10532 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10533 // 1.2.2 OpenMP Language Terminology 10534 // Structured block - An executable statement with a single entry at the 10535 // top and a single exit at the bottom. 10536 // The point of exit cannot be a branch out of the structured block. 10537 // longjmp() and throw() must not violate the entry/exit criteria. 10538 CS->getCapturedDecl()->setNothrow(); 10539 } 10540 10541 OMPLoopDirective::HelperExprs B; 10542 // In presence of clause 'collapse' with number of loops, it will 10543 // define the nested loops number. 10544 unsigned NestedLoopCount = 10545 checkOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses), 10546 nullptr /*ordered not a clause on distribute*/, CS, *this, 10547 *DSAStack, VarsWithImplicitDSA, B); 10548 if (NestedLoopCount == 0) 10549 return StmtError(); 10550 10551 assert((CurContext->isDependentContext() || B.builtAll()) && 10552 "omp teams distribute loop exprs were not built"); 10553 10554 setFunctionHasBranchProtectedScope(); 10555 10556 DSAStack->setParentTeamsRegionLoc(StartLoc); 10557 10558 return OMPTeamsDistributeDirective::Create( 10559 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 10560 } 10561 10562 StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective( 10563 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10564 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 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 = 10576 getOpenMPCaptureLevels(OMPD_teams_distribute_simd); 10577 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10578 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10579 // 1.2.2 OpenMP Language Terminology 10580 // Structured block - An executable statement with a single entry at the 10581 // top and a single exit at the bottom. 10582 // The point of exit cannot be a branch out of the structured block. 10583 // longjmp() and throw() must not violate the entry/exit criteria. 10584 CS->getCapturedDecl()->setNothrow(); 10585 } 10586 10587 OMPLoopDirective::HelperExprs B; 10588 // In presence of clause 'collapse' with number of loops, it will 10589 // define the nested loops number. 10590 unsigned NestedLoopCount = checkOpenMPLoop( 10591 OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses), 10592 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 10593 VarsWithImplicitDSA, B); 10594 10595 if (NestedLoopCount == 0) 10596 return StmtError(); 10597 10598 assert((CurContext->isDependentContext() || B.builtAll()) && 10599 "omp teams distribute simd loop exprs were not built"); 10600 10601 if (!CurContext->isDependentContext()) { 10602 // Finalize the clauses that need pre-built expressions for CodeGen. 10603 for (OMPClause *C : Clauses) { 10604 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 10605 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 10606 B.NumIterations, *this, CurScope, 10607 DSAStack)) 10608 return StmtError(); 10609 } 10610 } 10611 10612 if (checkSimdlenSafelenSpecified(*this, Clauses)) 10613 return StmtError(); 10614 10615 setFunctionHasBranchProtectedScope(); 10616 10617 DSAStack->setParentTeamsRegionLoc(StartLoc); 10618 10619 return OMPTeamsDistributeSimdDirective::Create( 10620 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 10621 } 10622 10623 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective( 10624 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10625 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10626 if (!AStmt) 10627 return StmtError(); 10628 10629 auto *CS = cast<CapturedStmt>(AStmt); 10630 // 1.2.2 OpenMP Language Terminology 10631 // Structured block - An executable statement with a single entry at the 10632 // top and a single exit at the bottom. 10633 // The point of exit cannot be a branch out of the structured block. 10634 // longjmp() and throw() must not violate the entry/exit criteria. 10635 CS->getCapturedDecl()->setNothrow(); 10636 10637 for (int ThisCaptureLevel = 10638 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for_simd); 10639 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10640 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10641 // 1.2.2 OpenMP Language Terminology 10642 // Structured block - An executable statement with a single entry at the 10643 // top and a single exit at the bottom. 10644 // The point of exit cannot be a branch out of the structured block. 10645 // longjmp() and throw() must not violate the entry/exit criteria. 10646 CS->getCapturedDecl()->setNothrow(); 10647 } 10648 10649 OMPLoopDirective::HelperExprs B; 10650 // In presence of clause 'collapse' with number of loops, it will 10651 // define the nested loops number. 10652 unsigned NestedLoopCount = checkOpenMPLoop( 10653 OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses), 10654 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 10655 VarsWithImplicitDSA, B); 10656 10657 if (NestedLoopCount == 0) 10658 return StmtError(); 10659 10660 assert((CurContext->isDependentContext() || B.builtAll()) && 10661 "omp for loop exprs were not built"); 10662 10663 if (!CurContext->isDependentContext()) { 10664 // Finalize the clauses that need pre-built expressions for CodeGen. 10665 for (OMPClause *C : Clauses) { 10666 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 10667 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 10668 B.NumIterations, *this, CurScope, 10669 DSAStack)) 10670 return StmtError(); 10671 } 10672 } 10673 10674 if (checkSimdlenSafelenSpecified(*this, Clauses)) 10675 return StmtError(); 10676 10677 setFunctionHasBranchProtectedScope(); 10678 10679 DSAStack->setParentTeamsRegionLoc(StartLoc); 10680 10681 return OMPTeamsDistributeParallelForSimdDirective::Create( 10682 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 10683 } 10684 10685 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective( 10686 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10687 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10688 if (!AStmt) 10689 return StmtError(); 10690 10691 auto *CS = cast<CapturedStmt>(AStmt); 10692 // 1.2.2 OpenMP Language Terminology 10693 // Structured block - An executable statement with a single entry at the 10694 // top and a single exit at the bottom. 10695 // The point of exit cannot be a branch out of the structured block. 10696 // longjmp() and throw() must not violate the entry/exit criteria. 10697 CS->getCapturedDecl()->setNothrow(); 10698 10699 for (int ThisCaptureLevel = 10700 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for); 10701 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10702 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10703 // 1.2.2 OpenMP Language Terminology 10704 // Structured block - An executable statement with a single entry at the 10705 // top and a single exit at the bottom. 10706 // The point of exit cannot be a branch out of the structured block. 10707 // longjmp() and throw() must not violate the entry/exit criteria. 10708 CS->getCapturedDecl()->setNothrow(); 10709 } 10710 10711 OMPLoopDirective::HelperExprs B; 10712 // In presence of clause 'collapse' with number of loops, it will 10713 // define the nested loops number. 10714 unsigned NestedLoopCount = checkOpenMPLoop( 10715 OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses), 10716 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 10717 VarsWithImplicitDSA, B); 10718 10719 if (NestedLoopCount == 0) 10720 return StmtError(); 10721 10722 assert((CurContext->isDependentContext() || B.builtAll()) && 10723 "omp for loop exprs were not built"); 10724 10725 setFunctionHasBranchProtectedScope(); 10726 10727 DSAStack->setParentTeamsRegionLoc(StartLoc); 10728 10729 return OMPTeamsDistributeParallelForDirective::Create( 10730 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 10731 DSAStack->isCancelRegion()); 10732 } 10733 10734 StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses, 10735 Stmt *AStmt, 10736 SourceLocation StartLoc, 10737 SourceLocation EndLoc) { 10738 if (!AStmt) 10739 return StmtError(); 10740 10741 auto *CS = cast<CapturedStmt>(AStmt); 10742 // 1.2.2 OpenMP Language Terminology 10743 // Structured block - An executable statement with a single entry at the 10744 // top and a single exit at the bottom. 10745 // The point of exit cannot be a branch out of the structured block. 10746 // longjmp() and throw() must not violate the entry/exit criteria. 10747 CS->getCapturedDecl()->setNothrow(); 10748 10749 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_teams); 10750 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10751 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10752 // 1.2.2 OpenMP Language Terminology 10753 // Structured block - An executable statement with a single entry at the 10754 // top and a single exit at the bottom. 10755 // The point of exit cannot be a branch out of the structured block. 10756 // longjmp() and throw() must not violate the entry/exit criteria. 10757 CS->getCapturedDecl()->setNothrow(); 10758 } 10759 setFunctionHasBranchProtectedScope(); 10760 10761 return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, 10762 AStmt); 10763 } 10764 10765 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective( 10766 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10767 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10768 if (!AStmt) 10769 return StmtError(); 10770 10771 auto *CS = cast<CapturedStmt>(AStmt); 10772 // 1.2.2 OpenMP Language Terminology 10773 // Structured block - An executable statement with a single entry at the 10774 // top and a single exit at the bottom. 10775 // The point of exit cannot be a branch out of the structured block. 10776 // longjmp() and throw() must not violate the entry/exit criteria. 10777 CS->getCapturedDecl()->setNothrow(); 10778 for (int ThisCaptureLevel = 10779 getOpenMPCaptureLevels(OMPD_target_teams_distribute); 10780 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10781 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10782 // 1.2.2 OpenMP Language Terminology 10783 // Structured block - An executable statement with a single entry at the 10784 // top and a single exit at the bottom. 10785 // The point of exit cannot be a branch out of the structured block. 10786 // longjmp() and throw() must not violate the entry/exit criteria. 10787 CS->getCapturedDecl()->setNothrow(); 10788 } 10789 10790 OMPLoopDirective::HelperExprs B; 10791 // In presence of clause 'collapse' with number of loops, it will 10792 // define the nested loops number. 10793 unsigned NestedLoopCount = checkOpenMPLoop( 10794 OMPD_target_teams_distribute, getCollapseNumberExpr(Clauses), 10795 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 10796 VarsWithImplicitDSA, B); 10797 if (NestedLoopCount == 0) 10798 return StmtError(); 10799 10800 assert((CurContext->isDependentContext() || B.builtAll()) && 10801 "omp target teams distribute loop exprs were not built"); 10802 10803 setFunctionHasBranchProtectedScope(); 10804 return OMPTargetTeamsDistributeDirective::Create( 10805 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 10806 } 10807 10808 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective( 10809 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10810 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10811 if (!AStmt) 10812 return StmtError(); 10813 10814 auto *CS = cast<CapturedStmt>(AStmt); 10815 // 1.2.2 OpenMP Language Terminology 10816 // Structured block - An executable statement with a single entry at the 10817 // top and a single exit at the bottom. 10818 // The point of exit cannot be a branch out of the structured block. 10819 // longjmp() and throw() must not violate the entry/exit criteria. 10820 CS->getCapturedDecl()->setNothrow(); 10821 for (int ThisCaptureLevel = 10822 getOpenMPCaptureLevels(OMPD_target_teams_distribute_parallel_for); 10823 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10824 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10825 // 1.2.2 OpenMP Language Terminology 10826 // Structured block - An executable statement with a single entry at the 10827 // top and a single exit at the bottom. 10828 // The point of exit cannot be a branch out of the structured block. 10829 // longjmp() and throw() must not violate the entry/exit criteria. 10830 CS->getCapturedDecl()->setNothrow(); 10831 } 10832 10833 OMPLoopDirective::HelperExprs B; 10834 // In presence of clause 'collapse' with number of loops, it will 10835 // define the nested loops number. 10836 unsigned NestedLoopCount = checkOpenMPLoop( 10837 OMPD_target_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses), 10838 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 10839 VarsWithImplicitDSA, B); 10840 if (NestedLoopCount == 0) 10841 return StmtError(); 10842 10843 assert((CurContext->isDependentContext() || B.builtAll()) && 10844 "omp target teams distribute parallel for loop exprs were not built"); 10845 10846 if (!CurContext->isDependentContext()) { 10847 // Finalize the clauses that need pre-built expressions for CodeGen. 10848 for (OMPClause *C : Clauses) { 10849 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 10850 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 10851 B.NumIterations, *this, CurScope, 10852 DSAStack)) 10853 return StmtError(); 10854 } 10855 } 10856 10857 setFunctionHasBranchProtectedScope(); 10858 return OMPTargetTeamsDistributeParallelForDirective::Create( 10859 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 10860 DSAStack->isCancelRegion()); 10861 } 10862 10863 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective( 10864 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10865 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10866 if (!AStmt) 10867 return StmtError(); 10868 10869 auto *CS = cast<CapturedStmt>(AStmt); 10870 // 1.2.2 OpenMP Language Terminology 10871 // Structured block - An executable statement with a single entry at the 10872 // top and a single exit at the bottom. 10873 // The point of exit cannot be a branch out of the structured block. 10874 // longjmp() and throw() must not violate the entry/exit criteria. 10875 CS->getCapturedDecl()->setNothrow(); 10876 for (int ThisCaptureLevel = getOpenMPCaptureLevels( 10877 OMPD_target_teams_distribute_parallel_for_simd); 10878 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10879 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10880 // 1.2.2 OpenMP Language Terminology 10881 // Structured block - An executable statement with a single entry at the 10882 // top and a single exit at the bottom. 10883 // The point of exit cannot be a branch out of the structured block. 10884 // longjmp() and throw() must not violate the entry/exit criteria. 10885 CS->getCapturedDecl()->setNothrow(); 10886 } 10887 10888 OMPLoopDirective::HelperExprs B; 10889 // In presence of clause 'collapse' with number of loops, it will 10890 // define the nested loops number. 10891 unsigned NestedLoopCount = 10892 checkOpenMPLoop(OMPD_target_teams_distribute_parallel_for_simd, 10893 getCollapseNumberExpr(Clauses), 10894 nullptr /*ordered not a clause on distribute*/, CS, *this, 10895 *DSAStack, VarsWithImplicitDSA, B); 10896 if (NestedLoopCount == 0) 10897 return StmtError(); 10898 10899 assert((CurContext->isDependentContext() || B.builtAll()) && 10900 "omp target teams distribute parallel for simd loop exprs were not " 10901 "built"); 10902 10903 if (!CurContext->isDependentContext()) { 10904 // Finalize the clauses that need pre-built expressions for CodeGen. 10905 for (OMPClause *C : Clauses) { 10906 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 10907 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 10908 B.NumIterations, *this, CurScope, 10909 DSAStack)) 10910 return StmtError(); 10911 } 10912 } 10913 10914 if (checkSimdlenSafelenSpecified(*this, Clauses)) 10915 return StmtError(); 10916 10917 setFunctionHasBranchProtectedScope(); 10918 return OMPTargetTeamsDistributeParallelForSimdDirective::Create( 10919 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 10920 } 10921 10922 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective( 10923 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10924 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10925 if (!AStmt) 10926 return StmtError(); 10927 10928 auto *CS = cast<CapturedStmt>(AStmt); 10929 // 1.2.2 OpenMP Language Terminology 10930 // Structured block - An executable statement with a single entry at the 10931 // top and a single exit at the bottom. 10932 // The point of exit cannot be a branch out of the structured block. 10933 // longjmp() and throw() must not violate the entry/exit criteria. 10934 CS->getCapturedDecl()->setNothrow(); 10935 for (int ThisCaptureLevel = 10936 getOpenMPCaptureLevels(OMPD_target_teams_distribute_simd); 10937 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10938 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10939 // 1.2.2 OpenMP Language Terminology 10940 // Structured block - An executable statement with a single entry at the 10941 // top and a single exit at the bottom. 10942 // The point of exit cannot be a branch out of the structured block. 10943 // longjmp() and throw() must not violate the entry/exit criteria. 10944 CS->getCapturedDecl()->setNothrow(); 10945 } 10946 10947 OMPLoopDirective::HelperExprs B; 10948 // In presence of clause 'collapse' with number of loops, it will 10949 // define the nested loops number. 10950 unsigned NestedLoopCount = checkOpenMPLoop( 10951 OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses), 10952 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 10953 VarsWithImplicitDSA, B); 10954 if (NestedLoopCount == 0) 10955 return StmtError(); 10956 10957 assert((CurContext->isDependentContext() || B.builtAll()) && 10958 "omp target teams distribute simd loop exprs were not built"); 10959 10960 if (!CurContext->isDependentContext()) { 10961 // Finalize the clauses that need pre-built expressions for CodeGen. 10962 for (OMPClause *C : Clauses) { 10963 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 10964 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 10965 B.NumIterations, *this, CurScope, 10966 DSAStack)) 10967 return StmtError(); 10968 } 10969 } 10970 10971 if (checkSimdlenSafelenSpecified(*this, Clauses)) 10972 return StmtError(); 10973 10974 setFunctionHasBranchProtectedScope(); 10975 return OMPTargetTeamsDistributeSimdDirective::Create( 10976 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 10977 } 10978 10979 OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr, 10980 SourceLocation StartLoc, 10981 SourceLocation LParenLoc, 10982 SourceLocation EndLoc) { 10983 OMPClause *Res = nullptr; 10984 switch (Kind) { 10985 case OMPC_final: 10986 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc); 10987 break; 10988 case OMPC_num_threads: 10989 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc); 10990 break; 10991 case OMPC_safelen: 10992 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc); 10993 break; 10994 case OMPC_simdlen: 10995 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc); 10996 break; 10997 case OMPC_allocator: 10998 Res = ActOnOpenMPAllocatorClause(Expr, StartLoc, LParenLoc, EndLoc); 10999 break; 11000 case OMPC_collapse: 11001 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc); 11002 break; 11003 case OMPC_ordered: 11004 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr); 11005 break; 11006 case OMPC_device: 11007 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc); 11008 break; 11009 case OMPC_num_teams: 11010 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc); 11011 break; 11012 case OMPC_thread_limit: 11013 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc); 11014 break; 11015 case OMPC_priority: 11016 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc); 11017 break; 11018 case OMPC_grainsize: 11019 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc); 11020 break; 11021 case OMPC_num_tasks: 11022 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc); 11023 break; 11024 case OMPC_hint: 11025 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc); 11026 break; 11027 case OMPC_depobj: 11028 Res = ActOnOpenMPDepobjClause(Expr, StartLoc, LParenLoc, EndLoc); 11029 break; 11030 case OMPC_if: 11031 case OMPC_default: 11032 case OMPC_proc_bind: 11033 case OMPC_schedule: 11034 case OMPC_private: 11035 case OMPC_firstprivate: 11036 case OMPC_lastprivate: 11037 case OMPC_shared: 11038 case OMPC_reduction: 11039 case OMPC_task_reduction: 11040 case OMPC_in_reduction: 11041 case OMPC_linear: 11042 case OMPC_aligned: 11043 case OMPC_copyin: 11044 case OMPC_copyprivate: 11045 case OMPC_nowait: 11046 case OMPC_untied: 11047 case OMPC_mergeable: 11048 case OMPC_threadprivate: 11049 case OMPC_allocate: 11050 case OMPC_flush: 11051 case OMPC_read: 11052 case OMPC_write: 11053 case OMPC_update: 11054 case OMPC_capture: 11055 case OMPC_seq_cst: 11056 case OMPC_acq_rel: 11057 case OMPC_acquire: 11058 case OMPC_release: 11059 case OMPC_relaxed: 11060 case OMPC_depend: 11061 case OMPC_threads: 11062 case OMPC_simd: 11063 case OMPC_map: 11064 case OMPC_nogroup: 11065 case OMPC_dist_schedule: 11066 case OMPC_defaultmap: 11067 case OMPC_unknown: 11068 case OMPC_uniform: 11069 case OMPC_to: 11070 case OMPC_from: 11071 case OMPC_use_device_ptr: 11072 case OMPC_is_device_ptr: 11073 case OMPC_unified_address: 11074 case OMPC_unified_shared_memory: 11075 case OMPC_reverse_offload: 11076 case OMPC_dynamic_allocators: 11077 case OMPC_atomic_default_mem_order: 11078 case OMPC_device_type: 11079 case OMPC_match: 11080 case OMPC_nontemporal: 11081 case OMPC_order: 11082 case OMPC_destroy: 11083 llvm_unreachable("Clause is not allowed."); 11084 } 11085 return Res; 11086 } 11087 11088 // An OpenMP directive such as 'target parallel' has two captured regions: 11089 // for the 'target' and 'parallel' respectively. This function returns 11090 // the region in which to capture expressions associated with a clause. 11091 // A return value of OMPD_unknown signifies that the expression should not 11092 // be captured. 11093 static OpenMPDirectiveKind getOpenMPCaptureRegionForClause( 11094 OpenMPDirectiveKind DKind, OpenMPClauseKind CKind, unsigned OpenMPVersion, 11095 OpenMPDirectiveKind NameModifier = OMPD_unknown) { 11096 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 11097 switch (CKind) { 11098 case OMPC_if: 11099 switch (DKind) { 11100 case OMPD_target_parallel_for_simd: 11101 if (OpenMPVersion >= 50 && 11102 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) { 11103 CaptureRegion = OMPD_parallel; 11104 break; 11105 } 11106 LLVM_FALLTHROUGH; 11107 case OMPD_target_parallel: 11108 case OMPD_target_parallel_for: 11109 // If this clause applies to the nested 'parallel' region, capture within 11110 // the 'target' region, otherwise do not capture. 11111 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel) 11112 CaptureRegion = OMPD_target; 11113 break; 11114 case OMPD_target_teams_distribute_parallel_for_simd: 11115 if (OpenMPVersion >= 50 && 11116 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) { 11117 CaptureRegion = OMPD_parallel; 11118 break; 11119 } 11120 LLVM_FALLTHROUGH; 11121 case OMPD_target_teams_distribute_parallel_for: 11122 // If this clause applies to the nested 'parallel' region, capture within 11123 // the 'teams' region, otherwise do not capture. 11124 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel) 11125 CaptureRegion = OMPD_teams; 11126 break; 11127 case OMPD_teams_distribute_parallel_for_simd: 11128 if (OpenMPVersion >= 50 && 11129 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) { 11130 CaptureRegion = OMPD_parallel; 11131 break; 11132 } 11133 LLVM_FALLTHROUGH; 11134 case OMPD_teams_distribute_parallel_for: 11135 CaptureRegion = OMPD_teams; 11136 break; 11137 case OMPD_target_update: 11138 case OMPD_target_enter_data: 11139 case OMPD_target_exit_data: 11140 CaptureRegion = OMPD_task; 11141 break; 11142 case OMPD_parallel_master_taskloop: 11143 if (NameModifier == OMPD_unknown || NameModifier == OMPD_taskloop) 11144 CaptureRegion = OMPD_parallel; 11145 break; 11146 case OMPD_parallel_master_taskloop_simd: 11147 if ((OpenMPVersion <= 45 && NameModifier == OMPD_unknown) || 11148 NameModifier == OMPD_taskloop) { 11149 CaptureRegion = OMPD_parallel; 11150 break; 11151 } 11152 if (OpenMPVersion <= 45) 11153 break; 11154 if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd) 11155 CaptureRegion = OMPD_taskloop; 11156 break; 11157 case OMPD_parallel_for_simd: 11158 if (OpenMPVersion <= 45) 11159 break; 11160 if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd) 11161 CaptureRegion = OMPD_parallel; 11162 break; 11163 case OMPD_taskloop_simd: 11164 case OMPD_master_taskloop_simd: 11165 if (OpenMPVersion <= 45) 11166 break; 11167 if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd) 11168 CaptureRegion = OMPD_taskloop; 11169 break; 11170 case OMPD_distribute_parallel_for_simd: 11171 if (OpenMPVersion <= 45) 11172 break; 11173 if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd) 11174 CaptureRegion = OMPD_parallel; 11175 break; 11176 case OMPD_target_simd: 11177 if (OpenMPVersion >= 50 && 11178 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) 11179 CaptureRegion = OMPD_target; 11180 break; 11181 case OMPD_teams_distribute_simd: 11182 case OMPD_target_teams_distribute_simd: 11183 if (OpenMPVersion >= 50 && 11184 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) 11185 CaptureRegion = OMPD_teams; 11186 break; 11187 case OMPD_cancel: 11188 case OMPD_parallel: 11189 case OMPD_parallel_master: 11190 case OMPD_parallel_sections: 11191 case OMPD_parallel_for: 11192 case OMPD_target: 11193 case OMPD_target_teams: 11194 case OMPD_target_teams_distribute: 11195 case OMPD_distribute_parallel_for: 11196 case OMPD_task: 11197 case OMPD_taskloop: 11198 case OMPD_master_taskloop: 11199 case OMPD_target_data: 11200 case OMPD_simd: 11201 case OMPD_for_simd: 11202 case OMPD_distribute_simd: 11203 // Do not capture if-clause expressions. 11204 break; 11205 case OMPD_threadprivate: 11206 case OMPD_allocate: 11207 case OMPD_taskyield: 11208 case OMPD_barrier: 11209 case OMPD_taskwait: 11210 case OMPD_cancellation_point: 11211 case OMPD_flush: 11212 case OMPD_depobj: 11213 case OMPD_declare_reduction: 11214 case OMPD_declare_mapper: 11215 case OMPD_declare_simd: 11216 case OMPD_declare_variant: 11217 case OMPD_declare_target: 11218 case OMPD_end_declare_target: 11219 case OMPD_teams: 11220 case OMPD_for: 11221 case OMPD_sections: 11222 case OMPD_section: 11223 case OMPD_single: 11224 case OMPD_master: 11225 case OMPD_critical: 11226 case OMPD_taskgroup: 11227 case OMPD_distribute: 11228 case OMPD_ordered: 11229 case OMPD_atomic: 11230 case OMPD_teams_distribute: 11231 case OMPD_requires: 11232 llvm_unreachable("Unexpected OpenMP directive with if-clause"); 11233 case OMPD_unknown: 11234 llvm_unreachable("Unknown OpenMP directive"); 11235 } 11236 break; 11237 case OMPC_num_threads: 11238 switch (DKind) { 11239 case OMPD_target_parallel: 11240 case OMPD_target_parallel_for: 11241 case OMPD_target_parallel_for_simd: 11242 CaptureRegion = OMPD_target; 11243 break; 11244 case OMPD_teams_distribute_parallel_for: 11245 case OMPD_teams_distribute_parallel_for_simd: 11246 case OMPD_target_teams_distribute_parallel_for: 11247 case OMPD_target_teams_distribute_parallel_for_simd: 11248 CaptureRegion = OMPD_teams; 11249 break; 11250 case OMPD_parallel: 11251 case OMPD_parallel_master: 11252 case OMPD_parallel_sections: 11253 case OMPD_parallel_for: 11254 case OMPD_parallel_for_simd: 11255 case OMPD_distribute_parallel_for: 11256 case OMPD_distribute_parallel_for_simd: 11257 case OMPD_parallel_master_taskloop: 11258 case OMPD_parallel_master_taskloop_simd: 11259 // Do not capture num_threads-clause expressions. 11260 break; 11261 case OMPD_target_data: 11262 case OMPD_target_enter_data: 11263 case OMPD_target_exit_data: 11264 case OMPD_target_update: 11265 case OMPD_target: 11266 case OMPD_target_simd: 11267 case OMPD_target_teams: 11268 case OMPD_target_teams_distribute: 11269 case OMPD_target_teams_distribute_simd: 11270 case OMPD_cancel: 11271 case OMPD_task: 11272 case OMPD_taskloop: 11273 case OMPD_taskloop_simd: 11274 case OMPD_master_taskloop: 11275 case OMPD_master_taskloop_simd: 11276 case OMPD_threadprivate: 11277 case OMPD_allocate: 11278 case OMPD_taskyield: 11279 case OMPD_barrier: 11280 case OMPD_taskwait: 11281 case OMPD_cancellation_point: 11282 case OMPD_flush: 11283 case OMPD_depobj: 11284 case OMPD_declare_reduction: 11285 case OMPD_declare_mapper: 11286 case OMPD_declare_simd: 11287 case OMPD_declare_variant: 11288 case OMPD_declare_target: 11289 case OMPD_end_declare_target: 11290 case OMPD_teams: 11291 case OMPD_simd: 11292 case OMPD_for: 11293 case OMPD_for_simd: 11294 case OMPD_sections: 11295 case OMPD_section: 11296 case OMPD_single: 11297 case OMPD_master: 11298 case OMPD_critical: 11299 case OMPD_taskgroup: 11300 case OMPD_distribute: 11301 case OMPD_ordered: 11302 case OMPD_atomic: 11303 case OMPD_distribute_simd: 11304 case OMPD_teams_distribute: 11305 case OMPD_teams_distribute_simd: 11306 case OMPD_requires: 11307 llvm_unreachable("Unexpected OpenMP directive with num_threads-clause"); 11308 case OMPD_unknown: 11309 llvm_unreachable("Unknown OpenMP directive"); 11310 } 11311 break; 11312 case OMPC_num_teams: 11313 switch (DKind) { 11314 case OMPD_target_teams: 11315 case OMPD_target_teams_distribute: 11316 case OMPD_target_teams_distribute_simd: 11317 case OMPD_target_teams_distribute_parallel_for: 11318 case OMPD_target_teams_distribute_parallel_for_simd: 11319 CaptureRegion = OMPD_target; 11320 break; 11321 case OMPD_teams_distribute_parallel_for: 11322 case OMPD_teams_distribute_parallel_for_simd: 11323 case OMPD_teams: 11324 case OMPD_teams_distribute: 11325 case OMPD_teams_distribute_simd: 11326 // Do not capture num_teams-clause expressions. 11327 break; 11328 case OMPD_distribute_parallel_for: 11329 case OMPD_distribute_parallel_for_simd: 11330 case OMPD_task: 11331 case OMPD_taskloop: 11332 case OMPD_taskloop_simd: 11333 case OMPD_master_taskloop: 11334 case OMPD_master_taskloop_simd: 11335 case OMPD_parallel_master_taskloop: 11336 case OMPD_parallel_master_taskloop_simd: 11337 case OMPD_target_data: 11338 case OMPD_target_enter_data: 11339 case OMPD_target_exit_data: 11340 case OMPD_target_update: 11341 case OMPD_cancel: 11342 case OMPD_parallel: 11343 case OMPD_parallel_master: 11344 case OMPD_parallel_sections: 11345 case OMPD_parallel_for: 11346 case OMPD_parallel_for_simd: 11347 case OMPD_target: 11348 case OMPD_target_simd: 11349 case OMPD_target_parallel: 11350 case OMPD_target_parallel_for: 11351 case OMPD_target_parallel_for_simd: 11352 case OMPD_threadprivate: 11353 case OMPD_allocate: 11354 case OMPD_taskyield: 11355 case OMPD_barrier: 11356 case OMPD_taskwait: 11357 case OMPD_cancellation_point: 11358 case OMPD_flush: 11359 case OMPD_depobj: 11360 case OMPD_declare_reduction: 11361 case OMPD_declare_mapper: 11362 case OMPD_declare_simd: 11363 case OMPD_declare_variant: 11364 case OMPD_declare_target: 11365 case OMPD_end_declare_target: 11366 case OMPD_simd: 11367 case OMPD_for: 11368 case OMPD_for_simd: 11369 case OMPD_sections: 11370 case OMPD_section: 11371 case OMPD_single: 11372 case OMPD_master: 11373 case OMPD_critical: 11374 case OMPD_taskgroup: 11375 case OMPD_distribute: 11376 case OMPD_ordered: 11377 case OMPD_atomic: 11378 case OMPD_distribute_simd: 11379 case OMPD_requires: 11380 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause"); 11381 case OMPD_unknown: 11382 llvm_unreachable("Unknown OpenMP directive"); 11383 } 11384 break; 11385 case OMPC_thread_limit: 11386 switch (DKind) { 11387 case OMPD_target_teams: 11388 case OMPD_target_teams_distribute: 11389 case OMPD_target_teams_distribute_simd: 11390 case OMPD_target_teams_distribute_parallel_for: 11391 case OMPD_target_teams_distribute_parallel_for_simd: 11392 CaptureRegion = OMPD_target; 11393 break; 11394 case OMPD_teams_distribute_parallel_for: 11395 case OMPD_teams_distribute_parallel_for_simd: 11396 case OMPD_teams: 11397 case OMPD_teams_distribute: 11398 case OMPD_teams_distribute_simd: 11399 // Do not capture thread_limit-clause expressions. 11400 break; 11401 case OMPD_distribute_parallel_for: 11402 case OMPD_distribute_parallel_for_simd: 11403 case OMPD_task: 11404 case OMPD_taskloop: 11405 case OMPD_taskloop_simd: 11406 case OMPD_master_taskloop: 11407 case OMPD_master_taskloop_simd: 11408 case OMPD_parallel_master_taskloop: 11409 case OMPD_parallel_master_taskloop_simd: 11410 case OMPD_target_data: 11411 case OMPD_target_enter_data: 11412 case OMPD_target_exit_data: 11413 case OMPD_target_update: 11414 case OMPD_cancel: 11415 case OMPD_parallel: 11416 case OMPD_parallel_master: 11417 case OMPD_parallel_sections: 11418 case OMPD_parallel_for: 11419 case OMPD_parallel_for_simd: 11420 case OMPD_target: 11421 case OMPD_target_simd: 11422 case OMPD_target_parallel: 11423 case OMPD_target_parallel_for: 11424 case OMPD_target_parallel_for_simd: 11425 case OMPD_threadprivate: 11426 case OMPD_allocate: 11427 case OMPD_taskyield: 11428 case OMPD_barrier: 11429 case OMPD_taskwait: 11430 case OMPD_cancellation_point: 11431 case OMPD_flush: 11432 case OMPD_depobj: 11433 case OMPD_declare_reduction: 11434 case OMPD_declare_mapper: 11435 case OMPD_declare_simd: 11436 case OMPD_declare_variant: 11437 case OMPD_declare_target: 11438 case OMPD_end_declare_target: 11439 case OMPD_simd: 11440 case OMPD_for: 11441 case OMPD_for_simd: 11442 case OMPD_sections: 11443 case OMPD_section: 11444 case OMPD_single: 11445 case OMPD_master: 11446 case OMPD_critical: 11447 case OMPD_taskgroup: 11448 case OMPD_distribute: 11449 case OMPD_ordered: 11450 case OMPD_atomic: 11451 case OMPD_distribute_simd: 11452 case OMPD_requires: 11453 llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause"); 11454 case OMPD_unknown: 11455 llvm_unreachable("Unknown OpenMP directive"); 11456 } 11457 break; 11458 case OMPC_schedule: 11459 switch (DKind) { 11460 case OMPD_parallel_for: 11461 case OMPD_parallel_for_simd: 11462 case OMPD_distribute_parallel_for: 11463 case OMPD_distribute_parallel_for_simd: 11464 case OMPD_teams_distribute_parallel_for: 11465 case OMPD_teams_distribute_parallel_for_simd: 11466 case OMPD_target_parallel_for: 11467 case OMPD_target_parallel_for_simd: 11468 case OMPD_target_teams_distribute_parallel_for: 11469 case OMPD_target_teams_distribute_parallel_for_simd: 11470 CaptureRegion = OMPD_parallel; 11471 break; 11472 case OMPD_for: 11473 case OMPD_for_simd: 11474 // Do not capture schedule-clause expressions. 11475 break; 11476 case OMPD_task: 11477 case OMPD_taskloop: 11478 case OMPD_taskloop_simd: 11479 case OMPD_master_taskloop: 11480 case OMPD_master_taskloop_simd: 11481 case OMPD_parallel_master_taskloop: 11482 case OMPD_parallel_master_taskloop_simd: 11483 case OMPD_target_data: 11484 case OMPD_target_enter_data: 11485 case OMPD_target_exit_data: 11486 case OMPD_target_update: 11487 case OMPD_teams: 11488 case OMPD_teams_distribute: 11489 case OMPD_teams_distribute_simd: 11490 case OMPD_target_teams_distribute: 11491 case OMPD_target_teams_distribute_simd: 11492 case OMPD_target: 11493 case OMPD_target_simd: 11494 case OMPD_target_parallel: 11495 case OMPD_cancel: 11496 case OMPD_parallel: 11497 case OMPD_parallel_master: 11498 case OMPD_parallel_sections: 11499 case OMPD_threadprivate: 11500 case OMPD_allocate: 11501 case OMPD_taskyield: 11502 case OMPD_barrier: 11503 case OMPD_taskwait: 11504 case OMPD_cancellation_point: 11505 case OMPD_flush: 11506 case OMPD_depobj: 11507 case OMPD_declare_reduction: 11508 case OMPD_declare_mapper: 11509 case OMPD_declare_simd: 11510 case OMPD_declare_variant: 11511 case OMPD_declare_target: 11512 case OMPD_end_declare_target: 11513 case OMPD_simd: 11514 case OMPD_sections: 11515 case OMPD_section: 11516 case OMPD_single: 11517 case OMPD_master: 11518 case OMPD_critical: 11519 case OMPD_taskgroup: 11520 case OMPD_distribute: 11521 case OMPD_ordered: 11522 case OMPD_atomic: 11523 case OMPD_distribute_simd: 11524 case OMPD_target_teams: 11525 case OMPD_requires: 11526 llvm_unreachable("Unexpected OpenMP directive with schedule clause"); 11527 case OMPD_unknown: 11528 llvm_unreachable("Unknown OpenMP directive"); 11529 } 11530 break; 11531 case OMPC_dist_schedule: 11532 switch (DKind) { 11533 case OMPD_teams_distribute_parallel_for: 11534 case OMPD_teams_distribute_parallel_for_simd: 11535 case OMPD_teams_distribute: 11536 case OMPD_teams_distribute_simd: 11537 case OMPD_target_teams_distribute_parallel_for: 11538 case OMPD_target_teams_distribute_parallel_for_simd: 11539 case OMPD_target_teams_distribute: 11540 case OMPD_target_teams_distribute_simd: 11541 CaptureRegion = OMPD_teams; 11542 break; 11543 case OMPD_distribute_parallel_for: 11544 case OMPD_distribute_parallel_for_simd: 11545 case OMPD_distribute: 11546 case OMPD_distribute_simd: 11547 // Do not capture thread_limit-clause expressions. 11548 break; 11549 case OMPD_parallel_for: 11550 case OMPD_parallel_for_simd: 11551 case OMPD_target_parallel_for_simd: 11552 case OMPD_target_parallel_for: 11553 case OMPD_task: 11554 case OMPD_taskloop: 11555 case OMPD_taskloop_simd: 11556 case OMPD_master_taskloop: 11557 case OMPD_master_taskloop_simd: 11558 case OMPD_parallel_master_taskloop: 11559 case OMPD_parallel_master_taskloop_simd: 11560 case OMPD_target_data: 11561 case OMPD_target_enter_data: 11562 case OMPD_target_exit_data: 11563 case OMPD_target_update: 11564 case OMPD_teams: 11565 case OMPD_target: 11566 case OMPD_target_simd: 11567 case OMPD_target_parallel: 11568 case OMPD_cancel: 11569 case OMPD_parallel: 11570 case OMPD_parallel_master: 11571 case OMPD_parallel_sections: 11572 case OMPD_threadprivate: 11573 case OMPD_allocate: 11574 case OMPD_taskyield: 11575 case OMPD_barrier: 11576 case OMPD_taskwait: 11577 case OMPD_cancellation_point: 11578 case OMPD_flush: 11579 case OMPD_depobj: 11580 case OMPD_declare_reduction: 11581 case OMPD_declare_mapper: 11582 case OMPD_declare_simd: 11583 case OMPD_declare_variant: 11584 case OMPD_declare_target: 11585 case OMPD_end_declare_target: 11586 case OMPD_simd: 11587 case OMPD_for: 11588 case OMPD_for_simd: 11589 case OMPD_sections: 11590 case OMPD_section: 11591 case OMPD_single: 11592 case OMPD_master: 11593 case OMPD_critical: 11594 case OMPD_taskgroup: 11595 case OMPD_ordered: 11596 case OMPD_atomic: 11597 case OMPD_target_teams: 11598 case OMPD_requires: 11599 llvm_unreachable("Unexpected OpenMP directive with schedule clause"); 11600 case OMPD_unknown: 11601 llvm_unreachable("Unknown OpenMP directive"); 11602 } 11603 break; 11604 case OMPC_device: 11605 switch (DKind) { 11606 case OMPD_target_update: 11607 case OMPD_target_enter_data: 11608 case OMPD_target_exit_data: 11609 case OMPD_target: 11610 case OMPD_target_simd: 11611 case OMPD_target_teams: 11612 case OMPD_target_parallel: 11613 case OMPD_target_teams_distribute: 11614 case OMPD_target_teams_distribute_simd: 11615 case OMPD_target_parallel_for: 11616 case OMPD_target_parallel_for_simd: 11617 case OMPD_target_teams_distribute_parallel_for: 11618 case OMPD_target_teams_distribute_parallel_for_simd: 11619 CaptureRegion = OMPD_task; 11620 break; 11621 case OMPD_target_data: 11622 // Do not capture device-clause expressions. 11623 break; 11624 case OMPD_teams_distribute_parallel_for: 11625 case OMPD_teams_distribute_parallel_for_simd: 11626 case OMPD_teams: 11627 case OMPD_teams_distribute: 11628 case OMPD_teams_distribute_simd: 11629 case OMPD_distribute_parallel_for: 11630 case OMPD_distribute_parallel_for_simd: 11631 case OMPD_task: 11632 case OMPD_taskloop: 11633 case OMPD_taskloop_simd: 11634 case OMPD_master_taskloop: 11635 case OMPD_master_taskloop_simd: 11636 case OMPD_parallel_master_taskloop: 11637 case OMPD_parallel_master_taskloop_simd: 11638 case OMPD_cancel: 11639 case OMPD_parallel: 11640 case OMPD_parallel_master: 11641 case OMPD_parallel_sections: 11642 case OMPD_parallel_for: 11643 case OMPD_parallel_for_simd: 11644 case OMPD_threadprivate: 11645 case OMPD_allocate: 11646 case OMPD_taskyield: 11647 case OMPD_barrier: 11648 case OMPD_taskwait: 11649 case OMPD_cancellation_point: 11650 case OMPD_flush: 11651 case OMPD_depobj: 11652 case OMPD_declare_reduction: 11653 case OMPD_declare_mapper: 11654 case OMPD_declare_simd: 11655 case OMPD_declare_variant: 11656 case OMPD_declare_target: 11657 case OMPD_end_declare_target: 11658 case OMPD_simd: 11659 case OMPD_for: 11660 case OMPD_for_simd: 11661 case OMPD_sections: 11662 case OMPD_section: 11663 case OMPD_single: 11664 case OMPD_master: 11665 case OMPD_critical: 11666 case OMPD_taskgroup: 11667 case OMPD_distribute: 11668 case OMPD_ordered: 11669 case OMPD_atomic: 11670 case OMPD_distribute_simd: 11671 case OMPD_requires: 11672 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause"); 11673 case OMPD_unknown: 11674 llvm_unreachable("Unknown OpenMP directive"); 11675 } 11676 break; 11677 case OMPC_grainsize: 11678 case OMPC_num_tasks: 11679 case OMPC_final: 11680 case OMPC_priority: 11681 switch (DKind) { 11682 case OMPD_task: 11683 case OMPD_taskloop: 11684 case OMPD_taskloop_simd: 11685 case OMPD_master_taskloop: 11686 case OMPD_master_taskloop_simd: 11687 break; 11688 case OMPD_parallel_master_taskloop: 11689 case OMPD_parallel_master_taskloop_simd: 11690 CaptureRegion = OMPD_parallel; 11691 break; 11692 case OMPD_target_update: 11693 case OMPD_target_enter_data: 11694 case OMPD_target_exit_data: 11695 case OMPD_target: 11696 case OMPD_target_simd: 11697 case OMPD_target_teams: 11698 case OMPD_target_parallel: 11699 case OMPD_target_teams_distribute: 11700 case OMPD_target_teams_distribute_simd: 11701 case OMPD_target_parallel_for: 11702 case OMPD_target_parallel_for_simd: 11703 case OMPD_target_teams_distribute_parallel_for: 11704 case OMPD_target_teams_distribute_parallel_for_simd: 11705 case OMPD_target_data: 11706 case OMPD_teams_distribute_parallel_for: 11707 case OMPD_teams_distribute_parallel_for_simd: 11708 case OMPD_teams: 11709 case OMPD_teams_distribute: 11710 case OMPD_teams_distribute_simd: 11711 case OMPD_distribute_parallel_for: 11712 case OMPD_distribute_parallel_for_simd: 11713 case OMPD_cancel: 11714 case OMPD_parallel: 11715 case OMPD_parallel_master: 11716 case OMPD_parallel_sections: 11717 case OMPD_parallel_for: 11718 case OMPD_parallel_for_simd: 11719 case OMPD_threadprivate: 11720 case OMPD_allocate: 11721 case OMPD_taskyield: 11722 case OMPD_barrier: 11723 case OMPD_taskwait: 11724 case OMPD_cancellation_point: 11725 case OMPD_flush: 11726 case OMPD_depobj: 11727 case OMPD_declare_reduction: 11728 case OMPD_declare_mapper: 11729 case OMPD_declare_simd: 11730 case OMPD_declare_variant: 11731 case OMPD_declare_target: 11732 case OMPD_end_declare_target: 11733 case OMPD_simd: 11734 case OMPD_for: 11735 case OMPD_for_simd: 11736 case OMPD_sections: 11737 case OMPD_section: 11738 case OMPD_single: 11739 case OMPD_master: 11740 case OMPD_critical: 11741 case OMPD_taskgroup: 11742 case OMPD_distribute: 11743 case OMPD_ordered: 11744 case OMPD_atomic: 11745 case OMPD_distribute_simd: 11746 case OMPD_requires: 11747 llvm_unreachable("Unexpected OpenMP directive with grainsize-clause"); 11748 case OMPD_unknown: 11749 llvm_unreachable("Unknown OpenMP directive"); 11750 } 11751 break; 11752 case OMPC_firstprivate: 11753 case OMPC_lastprivate: 11754 case OMPC_reduction: 11755 case OMPC_task_reduction: 11756 case OMPC_in_reduction: 11757 case OMPC_linear: 11758 case OMPC_default: 11759 case OMPC_proc_bind: 11760 case OMPC_safelen: 11761 case OMPC_simdlen: 11762 case OMPC_allocator: 11763 case OMPC_collapse: 11764 case OMPC_private: 11765 case OMPC_shared: 11766 case OMPC_aligned: 11767 case OMPC_copyin: 11768 case OMPC_copyprivate: 11769 case OMPC_ordered: 11770 case OMPC_nowait: 11771 case OMPC_untied: 11772 case OMPC_mergeable: 11773 case OMPC_threadprivate: 11774 case OMPC_allocate: 11775 case OMPC_flush: 11776 case OMPC_depobj: 11777 case OMPC_read: 11778 case OMPC_write: 11779 case OMPC_update: 11780 case OMPC_capture: 11781 case OMPC_seq_cst: 11782 case OMPC_acq_rel: 11783 case OMPC_acquire: 11784 case OMPC_release: 11785 case OMPC_relaxed: 11786 case OMPC_depend: 11787 case OMPC_threads: 11788 case OMPC_simd: 11789 case OMPC_map: 11790 case OMPC_nogroup: 11791 case OMPC_hint: 11792 case OMPC_defaultmap: 11793 case OMPC_unknown: 11794 case OMPC_uniform: 11795 case OMPC_to: 11796 case OMPC_from: 11797 case OMPC_use_device_ptr: 11798 case OMPC_is_device_ptr: 11799 case OMPC_unified_address: 11800 case OMPC_unified_shared_memory: 11801 case OMPC_reverse_offload: 11802 case OMPC_dynamic_allocators: 11803 case OMPC_atomic_default_mem_order: 11804 case OMPC_device_type: 11805 case OMPC_match: 11806 case OMPC_nontemporal: 11807 case OMPC_order: 11808 case OMPC_destroy: 11809 llvm_unreachable("Unexpected OpenMP clause."); 11810 } 11811 return CaptureRegion; 11812 } 11813 11814 OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier, 11815 Expr *Condition, SourceLocation StartLoc, 11816 SourceLocation LParenLoc, 11817 SourceLocation NameModifierLoc, 11818 SourceLocation ColonLoc, 11819 SourceLocation EndLoc) { 11820 Expr *ValExpr = Condition; 11821 Stmt *HelperValStmt = nullptr; 11822 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 11823 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 11824 !Condition->isInstantiationDependent() && 11825 !Condition->containsUnexpandedParameterPack()) { 11826 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 11827 if (Val.isInvalid()) 11828 return nullptr; 11829 11830 ValExpr = Val.get(); 11831 11832 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 11833 CaptureRegion = getOpenMPCaptureRegionForClause( 11834 DKind, OMPC_if, LangOpts.OpenMP, NameModifier); 11835 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 11836 ValExpr = MakeFullExpr(ValExpr).get(); 11837 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 11838 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 11839 HelperValStmt = buildPreInits(Context, Captures); 11840 } 11841 } 11842 11843 return new (Context) 11844 OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc, 11845 LParenLoc, NameModifierLoc, ColonLoc, EndLoc); 11846 } 11847 11848 OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition, 11849 SourceLocation StartLoc, 11850 SourceLocation LParenLoc, 11851 SourceLocation EndLoc) { 11852 Expr *ValExpr = Condition; 11853 Stmt *HelperValStmt = nullptr; 11854 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 11855 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 11856 !Condition->isInstantiationDependent() && 11857 !Condition->containsUnexpandedParameterPack()) { 11858 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 11859 if (Val.isInvalid()) 11860 return nullptr; 11861 11862 ValExpr = MakeFullExpr(Val.get()).get(); 11863 11864 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 11865 CaptureRegion = 11866 getOpenMPCaptureRegionForClause(DKind, OMPC_final, LangOpts.OpenMP); 11867 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 11868 ValExpr = MakeFullExpr(ValExpr).get(); 11869 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 11870 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 11871 HelperValStmt = buildPreInits(Context, Captures); 11872 } 11873 } 11874 11875 return new (Context) OMPFinalClause(ValExpr, HelperValStmt, CaptureRegion, 11876 StartLoc, LParenLoc, EndLoc); 11877 } 11878 11879 ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc, 11880 Expr *Op) { 11881 if (!Op) 11882 return ExprError(); 11883 11884 class IntConvertDiagnoser : public ICEConvertDiagnoser { 11885 public: 11886 IntConvertDiagnoser() 11887 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {} 11888 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, 11889 QualType T) override { 11890 return S.Diag(Loc, diag::err_omp_not_integral) << T; 11891 } 11892 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc, 11893 QualType T) override { 11894 return S.Diag(Loc, diag::err_omp_incomplete_type) << T; 11895 } 11896 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc, 11897 QualType T, 11898 QualType ConvTy) override { 11899 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy; 11900 } 11901 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv, 11902 QualType ConvTy) override { 11903 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 11904 << ConvTy->isEnumeralType() << ConvTy; 11905 } 11906 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc, 11907 QualType T) override { 11908 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T; 11909 } 11910 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv, 11911 QualType ConvTy) override { 11912 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 11913 << ConvTy->isEnumeralType() << ConvTy; 11914 } 11915 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType, 11916 QualType) override { 11917 llvm_unreachable("conversion functions are permitted"); 11918 } 11919 } ConvertDiagnoser; 11920 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser); 11921 } 11922 11923 static bool 11924 isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef, OpenMPClauseKind CKind, 11925 bool StrictlyPositive, bool BuildCapture = false, 11926 OpenMPDirectiveKind DKind = OMPD_unknown, 11927 OpenMPDirectiveKind *CaptureRegion = nullptr, 11928 Stmt **HelperValStmt = nullptr) { 11929 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() && 11930 !ValExpr->isInstantiationDependent()) { 11931 SourceLocation Loc = ValExpr->getExprLoc(); 11932 ExprResult Value = 11933 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr); 11934 if (Value.isInvalid()) 11935 return false; 11936 11937 ValExpr = Value.get(); 11938 // The expression must evaluate to a non-negative integer value. 11939 llvm::APSInt Result; 11940 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) && 11941 Result.isSigned() && 11942 !((!StrictlyPositive && Result.isNonNegative()) || 11943 (StrictlyPositive && Result.isStrictlyPositive()))) { 11944 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause) 11945 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) 11946 << ValExpr->getSourceRange(); 11947 return false; 11948 } 11949 if (!BuildCapture) 11950 return true; 11951 *CaptureRegion = 11952 getOpenMPCaptureRegionForClause(DKind, CKind, SemaRef.LangOpts.OpenMP); 11953 if (*CaptureRegion != OMPD_unknown && 11954 !SemaRef.CurContext->isDependentContext()) { 11955 ValExpr = SemaRef.MakeFullExpr(ValExpr).get(); 11956 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 11957 ValExpr = tryBuildCapture(SemaRef, ValExpr, Captures).get(); 11958 *HelperValStmt = buildPreInits(SemaRef.Context, Captures); 11959 } 11960 } 11961 return true; 11962 } 11963 11964 OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads, 11965 SourceLocation StartLoc, 11966 SourceLocation LParenLoc, 11967 SourceLocation EndLoc) { 11968 Expr *ValExpr = NumThreads; 11969 Stmt *HelperValStmt = nullptr; 11970 11971 // OpenMP [2.5, Restrictions] 11972 // The num_threads expression must evaluate to a positive integer value. 11973 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads, 11974 /*StrictlyPositive=*/true)) 11975 return nullptr; 11976 11977 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 11978 OpenMPDirectiveKind CaptureRegion = 11979 getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads, LangOpts.OpenMP); 11980 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 11981 ValExpr = MakeFullExpr(ValExpr).get(); 11982 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 11983 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 11984 HelperValStmt = buildPreInits(Context, Captures); 11985 } 11986 11987 return new (Context) OMPNumThreadsClause( 11988 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc); 11989 } 11990 11991 ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E, 11992 OpenMPClauseKind CKind, 11993 bool StrictlyPositive) { 11994 if (!E) 11995 return ExprError(); 11996 if (E->isValueDependent() || E->isTypeDependent() || 11997 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 11998 return E; 11999 llvm::APSInt Result; 12000 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result); 12001 if (ICE.isInvalid()) 12002 return ExprError(); 12003 if ((StrictlyPositive && !Result.isStrictlyPositive()) || 12004 (!StrictlyPositive && !Result.isNonNegative())) { 12005 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause) 12006 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) 12007 << E->getSourceRange(); 12008 return ExprError(); 12009 } 12010 if (CKind == OMPC_aligned && !Result.isPowerOf2()) { 12011 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two) 12012 << E->getSourceRange(); 12013 return ExprError(); 12014 } 12015 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1) 12016 DSAStack->setAssociatedLoops(Result.getExtValue()); 12017 else if (CKind == OMPC_ordered) 12018 DSAStack->setAssociatedLoops(Result.getExtValue()); 12019 return ICE; 12020 } 12021 12022 OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc, 12023 SourceLocation LParenLoc, 12024 SourceLocation EndLoc) { 12025 // OpenMP [2.8.1, simd construct, Description] 12026 // The parameter of the safelen clause must be a constant 12027 // positive integer expression. 12028 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen); 12029 if (Safelen.isInvalid()) 12030 return nullptr; 12031 return new (Context) 12032 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc); 12033 } 12034 12035 OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc, 12036 SourceLocation LParenLoc, 12037 SourceLocation EndLoc) { 12038 // OpenMP [2.8.1, simd construct, Description] 12039 // The parameter of the simdlen clause must be a constant 12040 // positive integer expression. 12041 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen); 12042 if (Simdlen.isInvalid()) 12043 return nullptr; 12044 return new (Context) 12045 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc); 12046 } 12047 12048 /// Tries to find omp_allocator_handle_t type. 12049 static bool findOMPAllocatorHandleT(Sema &S, SourceLocation Loc, 12050 DSAStackTy *Stack) { 12051 QualType OMPAllocatorHandleT = Stack->getOMPAllocatorHandleT(); 12052 if (!OMPAllocatorHandleT.isNull()) 12053 return true; 12054 // Build the predefined allocator expressions. 12055 bool ErrorFound = false; 12056 for (int I = OMPAllocateDeclAttr::OMPDefaultMemAlloc; 12057 I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) { 12058 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I); 12059 StringRef Allocator = 12060 OMPAllocateDeclAttr::ConvertAllocatorTypeTyToStr(AllocatorKind); 12061 DeclarationName AllocatorName = &S.getASTContext().Idents.get(Allocator); 12062 auto *VD = dyn_cast_or_null<ValueDecl>( 12063 S.LookupSingleName(S.TUScope, AllocatorName, Loc, Sema::LookupAnyName)); 12064 if (!VD) { 12065 ErrorFound = true; 12066 break; 12067 } 12068 QualType AllocatorType = 12069 VD->getType().getNonLValueExprType(S.getASTContext()); 12070 ExprResult Res = S.BuildDeclRefExpr(VD, AllocatorType, VK_LValue, Loc); 12071 if (!Res.isUsable()) { 12072 ErrorFound = true; 12073 break; 12074 } 12075 if (OMPAllocatorHandleT.isNull()) 12076 OMPAllocatorHandleT = AllocatorType; 12077 if (!S.getASTContext().hasSameType(OMPAllocatorHandleT, AllocatorType)) { 12078 ErrorFound = true; 12079 break; 12080 } 12081 Stack->setAllocator(AllocatorKind, Res.get()); 12082 } 12083 if (ErrorFound) { 12084 S.Diag(Loc, diag::err_omp_implied_type_not_found) 12085 << "omp_allocator_handle_t"; 12086 return false; 12087 } 12088 OMPAllocatorHandleT.addConst(); 12089 Stack->setOMPAllocatorHandleT(OMPAllocatorHandleT); 12090 return true; 12091 } 12092 12093 OMPClause *Sema::ActOnOpenMPAllocatorClause(Expr *A, SourceLocation StartLoc, 12094 SourceLocation LParenLoc, 12095 SourceLocation EndLoc) { 12096 // OpenMP [2.11.3, allocate Directive, Description] 12097 // allocator is an expression of omp_allocator_handle_t type. 12098 if (!findOMPAllocatorHandleT(*this, A->getExprLoc(), DSAStack)) 12099 return nullptr; 12100 12101 ExprResult Allocator = DefaultLvalueConversion(A); 12102 if (Allocator.isInvalid()) 12103 return nullptr; 12104 Allocator = PerformImplicitConversion(Allocator.get(), 12105 DSAStack->getOMPAllocatorHandleT(), 12106 Sema::AA_Initializing, 12107 /*AllowExplicit=*/true); 12108 if (Allocator.isInvalid()) 12109 return nullptr; 12110 return new (Context) 12111 OMPAllocatorClause(Allocator.get(), StartLoc, LParenLoc, EndLoc); 12112 } 12113 12114 OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops, 12115 SourceLocation StartLoc, 12116 SourceLocation LParenLoc, 12117 SourceLocation EndLoc) { 12118 // OpenMP [2.7.1, loop construct, Description] 12119 // OpenMP [2.8.1, simd construct, Description] 12120 // OpenMP [2.9.6, distribute construct, Description] 12121 // The parameter of the collapse clause must be a constant 12122 // positive integer expression. 12123 ExprResult NumForLoopsResult = 12124 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse); 12125 if (NumForLoopsResult.isInvalid()) 12126 return nullptr; 12127 return new (Context) 12128 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc); 12129 } 12130 12131 OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc, 12132 SourceLocation EndLoc, 12133 SourceLocation LParenLoc, 12134 Expr *NumForLoops) { 12135 // OpenMP [2.7.1, loop construct, Description] 12136 // OpenMP [2.8.1, simd construct, Description] 12137 // OpenMP [2.9.6, distribute construct, Description] 12138 // The parameter of the ordered clause must be a constant 12139 // positive integer expression if any. 12140 if (NumForLoops && LParenLoc.isValid()) { 12141 ExprResult NumForLoopsResult = 12142 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered); 12143 if (NumForLoopsResult.isInvalid()) 12144 return nullptr; 12145 NumForLoops = NumForLoopsResult.get(); 12146 } else { 12147 NumForLoops = nullptr; 12148 } 12149 auto *Clause = OMPOrderedClause::Create( 12150 Context, NumForLoops, NumForLoops ? DSAStack->getAssociatedLoops() : 0, 12151 StartLoc, LParenLoc, EndLoc); 12152 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops, Clause); 12153 return Clause; 12154 } 12155 12156 OMPClause *Sema::ActOnOpenMPSimpleClause( 12157 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc, 12158 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 12159 OMPClause *Res = nullptr; 12160 switch (Kind) { 12161 case OMPC_default: 12162 Res = ActOnOpenMPDefaultClause(static_cast<DefaultKind>(Argument), 12163 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 12164 break; 12165 case OMPC_proc_bind: 12166 Res = ActOnOpenMPProcBindClause(static_cast<ProcBindKind>(Argument), 12167 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 12168 break; 12169 case OMPC_atomic_default_mem_order: 12170 Res = ActOnOpenMPAtomicDefaultMemOrderClause( 12171 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Argument), 12172 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 12173 break; 12174 case OMPC_order: 12175 Res = ActOnOpenMPOrderClause(static_cast<OpenMPOrderClauseKind>(Argument), 12176 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 12177 break; 12178 case OMPC_update: 12179 Res = ActOnOpenMPUpdateClause(static_cast<OpenMPDependClauseKind>(Argument), 12180 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 12181 break; 12182 case OMPC_if: 12183 case OMPC_final: 12184 case OMPC_num_threads: 12185 case OMPC_safelen: 12186 case OMPC_simdlen: 12187 case OMPC_allocator: 12188 case OMPC_collapse: 12189 case OMPC_schedule: 12190 case OMPC_private: 12191 case OMPC_firstprivate: 12192 case OMPC_lastprivate: 12193 case OMPC_shared: 12194 case OMPC_reduction: 12195 case OMPC_task_reduction: 12196 case OMPC_in_reduction: 12197 case OMPC_linear: 12198 case OMPC_aligned: 12199 case OMPC_copyin: 12200 case OMPC_copyprivate: 12201 case OMPC_ordered: 12202 case OMPC_nowait: 12203 case OMPC_untied: 12204 case OMPC_mergeable: 12205 case OMPC_threadprivate: 12206 case OMPC_allocate: 12207 case OMPC_flush: 12208 case OMPC_depobj: 12209 case OMPC_read: 12210 case OMPC_write: 12211 case OMPC_capture: 12212 case OMPC_seq_cst: 12213 case OMPC_acq_rel: 12214 case OMPC_acquire: 12215 case OMPC_release: 12216 case OMPC_relaxed: 12217 case OMPC_depend: 12218 case OMPC_device: 12219 case OMPC_threads: 12220 case OMPC_simd: 12221 case OMPC_map: 12222 case OMPC_num_teams: 12223 case OMPC_thread_limit: 12224 case OMPC_priority: 12225 case OMPC_grainsize: 12226 case OMPC_nogroup: 12227 case OMPC_num_tasks: 12228 case OMPC_hint: 12229 case OMPC_dist_schedule: 12230 case OMPC_defaultmap: 12231 case OMPC_unknown: 12232 case OMPC_uniform: 12233 case OMPC_to: 12234 case OMPC_from: 12235 case OMPC_use_device_ptr: 12236 case OMPC_is_device_ptr: 12237 case OMPC_unified_address: 12238 case OMPC_unified_shared_memory: 12239 case OMPC_reverse_offload: 12240 case OMPC_dynamic_allocators: 12241 case OMPC_device_type: 12242 case OMPC_match: 12243 case OMPC_nontemporal: 12244 case OMPC_destroy: 12245 llvm_unreachable("Clause is not allowed."); 12246 } 12247 return Res; 12248 } 12249 12250 static std::string 12251 getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last, 12252 ArrayRef<unsigned> Exclude = llvm::None) { 12253 SmallString<256> Buffer; 12254 llvm::raw_svector_ostream Out(Buffer); 12255 unsigned Skipped = Exclude.size(); 12256 auto S = Exclude.begin(), E = Exclude.end(); 12257 for (unsigned I = First; I < Last; ++I) { 12258 if (std::find(S, E, I) != E) { 12259 --Skipped; 12260 continue; 12261 } 12262 Out << "'" << getOpenMPSimpleClauseTypeName(K, I) << "'"; 12263 if (I + Skipped + 2 == Last) 12264 Out << " or "; 12265 else if (I + Skipped + 1 != Last) 12266 Out << ", "; 12267 } 12268 return std::string(Out.str()); 12269 } 12270 12271 OMPClause *Sema::ActOnOpenMPDefaultClause(DefaultKind Kind, 12272 SourceLocation KindKwLoc, 12273 SourceLocation StartLoc, 12274 SourceLocation LParenLoc, 12275 SourceLocation EndLoc) { 12276 if (Kind == OMP_DEFAULT_unknown) { 12277 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 12278 << getListOfPossibleValues(OMPC_default, /*First=*/0, 12279 /*Last=*/unsigned(OMP_DEFAULT_unknown)) 12280 << getOpenMPClauseName(OMPC_default); 12281 return nullptr; 12282 } 12283 if (Kind == OMP_DEFAULT_none) 12284 DSAStack->setDefaultDSANone(KindKwLoc); 12285 else if (Kind == OMP_DEFAULT_shared) 12286 DSAStack->setDefaultDSAShared(KindKwLoc); 12287 12288 return new (Context) 12289 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 12290 } 12291 12292 OMPClause *Sema::ActOnOpenMPProcBindClause(ProcBindKind Kind, 12293 SourceLocation KindKwLoc, 12294 SourceLocation StartLoc, 12295 SourceLocation LParenLoc, 12296 SourceLocation EndLoc) { 12297 if (Kind == OMP_PROC_BIND_unknown) { 12298 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 12299 << getListOfPossibleValues(OMPC_proc_bind, 12300 /*First=*/unsigned(OMP_PROC_BIND_master), 12301 /*Last=*/5) 12302 << getOpenMPClauseName(OMPC_proc_bind); 12303 return nullptr; 12304 } 12305 return new (Context) 12306 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 12307 } 12308 12309 OMPClause *Sema::ActOnOpenMPAtomicDefaultMemOrderClause( 12310 OpenMPAtomicDefaultMemOrderClauseKind Kind, SourceLocation KindKwLoc, 12311 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 12312 if (Kind == OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) { 12313 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 12314 << getListOfPossibleValues( 12315 OMPC_atomic_default_mem_order, /*First=*/0, 12316 /*Last=*/OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) 12317 << getOpenMPClauseName(OMPC_atomic_default_mem_order); 12318 return nullptr; 12319 } 12320 return new (Context) OMPAtomicDefaultMemOrderClause(Kind, KindKwLoc, StartLoc, 12321 LParenLoc, EndLoc); 12322 } 12323 12324 OMPClause *Sema::ActOnOpenMPOrderClause(OpenMPOrderClauseKind Kind, 12325 SourceLocation KindKwLoc, 12326 SourceLocation StartLoc, 12327 SourceLocation LParenLoc, 12328 SourceLocation EndLoc) { 12329 if (Kind == OMPC_ORDER_unknown) { 12330 static_assert(OMPC_ORDER_unknown > 0, 12331 "OMPC_ORDER_unknown not greater than 0"); 12332 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 12333 << getListOfPossibleValues(OMPC_order, /*First=*/0, 12334 /*Last=*/OMPC_ORDER_unknown) 12335 << getOpenMPClauseName(OMPC_order); 12336 return nullptr; 12337 } 12338 return new (Context) 12339 OMPOrderClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 12340 } 12341 12342 OMPClause *Sema::ActOnOpenMPUpdateClause(OpenMPDependClauseKind Kind, 12343 SourceLocation KindKwLoc, 12344 SourceLocation StartLoc, 12345 SourceLocation LParenLoc, 12346 SourceLocation EndLoc) { 12347 if (Kind == OMPC_DEPEND_unknown || Kind == OMPC_DEPEND_source || 12348 Kind == OMPC_DEPEND_sink || Kind == OMPC_DEPEND_depobj) { 12349 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink, 12350 OMPC_DEPEND_depobj}; 12351 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 12352 << getListOfPossibleValues(OMPC_depend, /*First=*/0, 12353 /*Last=*/OMPC_DEPEND_unknown, Except) 12354 << getOpenMPClauseName(OMPC_update); 12355 return nullptr; 12356 } 12357 return OMPUpdateClause::Create(Context, StartLoc, LParenLoc, KindKwLoc, Kind, 12358 EndLoc); 12359 } 12360 12361 OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause( 12362 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr, 12363 SourceLocation StartLoc, SourceLocation LParenLoc, 12364 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc, 12365 SourceLocation EndLoc) { 12366 OMPClause *Res = nullptr; 12367 switch (Kind) { 12368 case OMPC_schedule: 12369 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements }; 12370 assert(Argument.size() == NumberOfElements && 12371 ArgumentLoc.size() == NumberOfElements); 12372 Res = ActOnOpenMPScheduleClause( 12373 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]), 12374 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]), 12375 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr, 12376 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2], 12377 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc); 12378 break; 12379 case OMPC_if: 12380 assert(Argument.size() == 1 && ArgumentLoc.size() == 1); 12381 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()), 12382 Expr, StartLoc, LParenLoc, ArgumentLoc.back(), 12383 DelimLoc, EndLoc); 12384 break; 12385 case OMPC_dist_schedule: 12386 Res = ActOnOpenMPDistScheduleClause( 12387 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr, 12388 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc); 12389 break; 12390 case OMPC_defaultmap: 12391 enum { Modifier, DefaultmapKind }; 12392 Res = ActOnOpenMPDefaultmapClause( 12393 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]), 12394 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]), 12395 StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind], 12396 EndLoc); 12397 break; 12398 case OMPC_final: 12399 case OMPC_num_threads: 12400 case OMPC_safelen: 12401 case OMPC_simdlen: 12402 case OMPC_allocator: 12403 case OMPC_collapse: 12404 case OMPC_default: 12405 case OMPC_proc_bind: 12406 case OMPC_private: 12407 case OMPC_firstprivate: 12408 case OMPC_lastprivate: 12409 case OMPC_shared: 12410 case OMPC_reduction: 12411 case OMPC_task_reduction: 12412 case OMPC_in_reduction: 12413 case OMPC_linear: 12414 case OMPC_aligned: 12415 case OMPC_copyin: 12416 case OMPC_copyprivate: 12417 case OMPC_ordered: 12418 case OMPC_nowait: 12419 case OMPC_untied: 12420 case OMPC_mergeable: 12421 case OMPC_threadprivate: 12422 case OMPC_allocate: 12423 case OMPC_flush: 12424 case OMPC_depobj: 12425 case OMPC_read: 12426 case OMPC_write: 12427 case OMPC_update: 12428 case OMPC_capture: 12429 case OMPC_seq_cst: 12430 case OMPC_acq_rel: 12431 case OMPC_acquire: 12432 case OMPC_release: 12433 case OMPC_relaxed: 12434 case OMPC_depend: 12435 case OMPC_device: 12436 case OMPC_threads: 12437 case OMPC_simd: 12438 case OMPC_map: 12439 case OMPC_num_teams: 12440 case OMPC_thread_limit: 12441 case OMPC_priority: 12442 case OMPC_grainsize: 12443 case OMPC_nogroup: 12444 case OMPC_num_tasks: 12445 case OMPC_hint: 12446 case OMPC_unknown: 12447 case OMPC_uniform: 12448 case OMPC_to: 12449 case OMPC_from: 12450 case OMPC_use_device_ptr: 12451 case OMPC_is_device_ptr: 12452 case OMPC_unified_address: 12453 case OMPC_unified_shared_memory: 12454 case OMPC_reverse_offload: 12455 case OMPC_dynamic_allocators: 12456 case OMPC_atomic_default_mem_order: 12457 case OMPC_device_type: 12458 case OMPC_match: 12459 case OMPC_nontemporal: 12460 case OMPC_order: 12461 case OMPC_destroy: 12462 llvm_unreachable("Clause is not allowed."); 12463 } 12464 return Res; 12465 } 12466 12467 static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1, 12468 OpenMPScheduleClauseModifier M2, 12469 SourceLocation M1Loc, SourceLocation M2Loc) { 12470 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) { 12471 SmallVector<unsigned, 2> Excluded; 12472 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown) 12473 Excluded.push_back(M2); 12474 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) 12475 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic); 12476 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic) 12477 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic); 12478 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value) 12479 << getListOfPossibleValues(OMPC_schedule, 12480 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1, 12481 /*Last=*/OMPC_SCHEDULE_MODIFIER_last, 12482 Excluded) 12483 << getOpenMPClauseName(OMPC_schedule); 12484 return true; 12485 } 12486 return false; 12487 } 12488 12489 OMPClause *Sema::ActOnOpenMPScheduleClause( 12490 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2, 12491 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, 12492 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc, 12493 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) { 12494 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) || 12495 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc)) 12496 return nullptr; 12497 // OpenMP, 2.7.1, Loop Construct, Restrictions 12498 // Either the monotonic modifier or the nonmonotonic modifier can be specified 12499 // but not both. 12500 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) || 12501 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic && 12502 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) || 12503 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic && 12504 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) { 12505 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier) 12506 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2) 12507 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1); 12508 return nullptr; 12509 } 12510 if (Kind == OMPC_SCHEDULE_unknown) { 12511 std::string Values; 12512 if (M1Loc.isInvalid() && M2Loc.isInvalid()) { 12513 unsigned Exclude[] = {OMPC_SCHEDULE_unknown}; 12514 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0, 12515 /*Last=*/OMPC_SCHEDULE_MODIFIER_last, 12516 Exclude); 12517 } else { 12518 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0, 12519 /*Last=*/OMPC_SCHEDULE_unknown); 12520 } 12521 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 12522 << Values << getOpenMPClauseName(OMPC_schedule); 12523 return nullptr; 12524 } 12525 // OpenMP, 2.7.1, Loop Construct, Restrictions 12526 // The nonmonotonic modifier can only be specified with schedule(dynamic) or 12527 // schedule(guided). 12528 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic || 12529 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) && 12530 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) { 12531 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc, 12532 diag::err_omp_schedule_nonmonotonic_static); 12533 return nullptr; 12534 } 12535 Expr *ValExpr = ChunkSize; 12536 Stmt *HelperValStmt = nullptr; 12537 if (ChunkSize) { 12538 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && 12539 !ChunkSize->isInstantiationDependent() && 12540 !ChunkSize->containsUnexpandedParameterPack()) { 12541 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc(); 12542 ExprResult Val = 12543 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); 12544 if (Val.isInvalid()) 12545 return nullptr; 12546 12547 ValExpr = Val.get(); 12548 12549 // OpenMP [2.7.1, Restrictions] 12550 // chunk_size must be a loop invariant integer expression with a positive 12551 // value. 12552 llvm::APSInt Result; 12553 if (ValExpr->isIntegerConstantExpr(Result, Context)) { 12554 if (Result.isSigned() && !Result.isStrictlyPositive()) { 12555 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) 12556 << "schedule" << 1 << ChunkSize->getSourceRange(); 12557 return nullptr; 12558 } 12559 } else if (getOpenMPCaptureRegionForClause( 12560 DSAStack->getCurrentDirective(), OMPC_schedule, 12561 LangOpts.OpenMP) != OMPD_unknown && 12562 !CurContext->isDependentContext()) { 12563 ValExpr = MakeFullExpr(ValExpr).get(); 12564 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 12565 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 12566 HelperValStmt = buildPreInits(Context, Captures); 12567 } 12568 } 12569 } 12570 12571 return new (Context) 12572 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind, 12573 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc); 12574 } 12575 12576 OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind, 12577 SourceLocation StartLoc, 12578 SourceLocation EndLoc) { 12579 OMPClause *Res = nullptr; 12580 switch (Kind) { 12581 case OMPC_ordered: 12582 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc); 12583 break; 12584 case OMPC_nowait: 12585 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc); 12586 break; 12587 case OMPC_untied: 12588 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc); 12589 break; 12590 case OMPC_mergeable: 12591 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc); 12592 break; 12593 case OMPC_read: 12594 Res = ActOnOpenMPReadClause(StartLoc, EndLoc); 12595 break; 12596 case OMPC_write: 12597 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc); 12598 break; 12599 case OMPC_update: 12600 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc); 12601 break; 12602 case OMPC_capture: 12603 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc); 12604 break; 12605 case OMPC_seq_cst: 12606 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc); 12607 break; 12608 case OMPC_acq_rel: 12609 Res = ActOnOpenMPAcqRelClause(StartLoc, EndLoc); 12610 break; 12611 case OMPC_acquire: 12612 Res = ActOnOpenMPAcquireClause(StartLoc, EndLoc); 12613 break; 12614 case OMPC_release: 12615 Res = ActOnOpenMPReleaseClause(StartLoc, EndLoc); 12616 break; 12617 case OMPC_relaxed: 12618 Res = ActOnOpenMPRelaxedClause(StartLoc, EndLoc); 12619 break; 12620 case OMPC_threads: 12621 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc); 12622 break; 12623 case OMPC_simd: 12624 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc); 12625 break; 12626 case OMPC_nogroup: 12627 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc); 12628 break; 12629 case OMPC_unified_address: 12630 Res = ActOnOpenMPUnifiedAddressClause(StartLoc, EndLoc); 12631 break; 12632 case OMPC_unified_shared_memory: 12633 Res = ActOnOpenMPUnifiedSharedMemoryClause(StartLoc, EndLoc); 12634 break; 12635 case OMPC_reverse_offload: 12636 Res = ActOnOpenMPReverseOffloadClause(StartLoc, EndLoc); 12637 break; 12638 case OMPC_dynamic_allocators: 12639 Res = ActOnOpenMPDynamicAllocatorsClause(StartLoc, EndLoc); 12640 break; 12641 case OMPC_destroy: 12642 Res = ActOnOpenMPDestroyClause(StartLoc, EndLoc); 12643 break; 12644 case OMPC_if: 12645 case OMPC_final: 12646 case OMPC_num_threads: 12647 case OMPC_safelen: 12648 case OMPC_simdlen: 12649 case OMPC_allocator: 12650 case OMPC_collapse: 12651 case OMPC_schedule: 12652 case OMPC_private: 12653 case OMPC_firstprivate: 12654 case OMPC_lastprivate: 12655 case OMPC_shared: 12656 case OMPC_reduction: 12657 case OMPC_task_reduction: 12658 case OMPC_in_reduction: 12659 case OMPC_linear: 12660 case OMPC_aligned: 12661 case OMPC_copyin: 12662 case OMPC_copyprivate: 12663 case OMPC_default: 12664 case OMPC_proc_bind: 12665 case OMPC_threadprivate: 12666 case OMPC_allocate: 12667 case OMPC_flush: 12668 case OMPC_depobj: 12669 case OMPC_depend: 12670 case OMPC_device: 12671 case OMPC_map: 12672 case OMPC_num_teams: 12673 case OMPC_thread_limit: 12674 case OMPC_priority: 12675 case OMPC_grainsize: 12676 case OMPC_num_tasks: 12677 case OMPC_hint: 12678 case OMPC_dist_schedule: 12679 case OMPC_defaultmap: 12680 case OMPC_unknown: 12681 case OMPC_uniform: 12682 case OMPC_to: 12683 case OMPC_from: 12684 case OMPC_use_device_ptr: 12685 case OMPC_is_device_ptr: 12686 case OMPC_atomic_default_mem_order: 12687 case OMPC_device_type: 12688 case OMPC_match: 12689 case OMPC_nontemporal: 12690 case OMPC_order: 12691 llvm_unreachable("Clause is not allowed."); 12692 } 12693 return Res; 12694 } 12695 12696 OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc, 12697 SourceLocation EndLoc) { 12698 DSAStack->setNowaitRegion(); 12699 return new (Context) OMPNowaitClause(StartLoc, EndLoc); 12700 } 12701 12702 OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc, 12703 SourceLocation EndLoc) { 12704 return new (Context) OMPUntiedClause(StartLoc, EndLoc); 12705 } 12706 12707 OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc, 12708 SourceLocation EndLoc) { 12709 return new (Context) OMPMergeableClause(StartLoc, EndLoc); 12710 } 12711 12712 OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc, 12713 SourceLocation EndLoc) { 12714 return new (Context) OMPReadClause(StartLoc, EndLoc); 12715 } 12716 12717 OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc, 12718 SourceLocation EndLoc) { 12719 return new (Context) OMPWriteClause(StartLoc, EndLoc); 12720 } 12721 12722 OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc, 12723 SourceLocation EndLoc) { 12724 return OMPUpdateClause::Create(Context, StartLoc, EndLoc); 12725 } 12726 12727 OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc, 12728 SourceLocation EndLoc) { 12729 return new (Context) OMPCaptureClause(StartLoc, EndLoc); 12730 } 12731 12732 OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc, 12733 SourceLocation EndLoc) { 12734 return new (Context) OMPSeqCstClause(StartLoc, EndLoc); 12735 } 12736 12737 OMPClause *Sema::ActOnOpenMPAcqRelClause(SourceLocation StartLoc, 12738 SourceLocation EndLoc) { 12739 return new (Context) OMPAcqRelClause(StartLoc, EndLoc); 12740 } 12741 12742 OMPClause *Sema::ActOnOpenMPAcquireClause(SourceLocation StartLoc, 12743 SourceLocation EndLoc) { 12744 return new (Context) OMPAcquireClause(StartLoc, EndLoc); 12745 } 12746 12747 OMPClause *Sema::ActOnOpenMPReleaseClause(SourceLocation StartLoc, 12748 SourceLocation EndLoc) { 12749 return new (Context) OMPReleaseClause(StartLoc, EndLoc); 12750 } 12751 12752 OMPClause *Sema::ActOnOpenMPRelaxedClause(SourceLocation StartLoc, 12753 SourceLocation EndLoc) { 12754 return new (Context) OMPRelaxedClause(StartLoc, EndLoc); 12755 } 12756 12757 OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc, 12758 SourceLocation EndLoc) { 12759 return new (Context) OMPThreadsClause(StartLoc, EndLoc); 12760 } 12761 12762 OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc, 12763 SourceLocation EndLoc) { 12764 return new (Context) OMPSIMDClause(StartLoc, EndLoc); 12765 } 12766 12767 OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc, 12768 SourceLocation EndLoc) { 12769 return new (Context) OMPNogroupClause(StartLoc, EndLoc); 12770 } 12771 12772 OMPClause *Sema::ActOnOpenMPUnifiedAddressClause(SourceLocation StartLoc, 12773 SourceLocation EndLoc) { 12774 return new (Context) OMPUnifiedAddressClause(StartLoc, EndLoc); 12775 } 12776 12777 OMPClause *Sema::ActOnOpenMPUnifiedSharedMemoryClause(SourceLocation StartLoc, 12778 SourceLocation EndLoc) { 12779 return new (Context) OMPUnifiedSharedMemoryClause(StartLoc, EndLoc); 12780 } 12781 12782 OMPClause *Sema::ActOnOpenMPReverseOffloadClause(SourceLocation StartLoc, 12783 SourceLocation EndLoc) { 12784 return new (Context) OMPReverseOffloadClause(StartLoc, EndLoc); 12785 } 12786 12787 OMPClause *Sema::ActOnOpenMPDynamicAllocatorsClause(SourceLocation StartLoc, 12788 SourceLocation EndLoc) { 12789 return new (Context) OMPDynamicAllocatorsClause(StartLoc, EndLoc); 12790 } 12791 12792 OMPClause *Sema::ActOnOpenMPDestroyClause(SourceLocation StartLoc, 12793 SourceLocation EndLoc) { 12794 return new (Context) OMPDestroyClause(StartLoc, EndLoc); 12795 } 12796 12797 OMPClause *Sema::ActOnOpenMPVarListClause( 12798 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr, 12799 const OMPVarListLocTy &Locs, SourceLocation ColonLoc, 12800 CXXScopeSpec &ReductionOrMapperIdScopeSpec, 12801 DeclarationNameInfo &ReductionOrMapperId, int ExtraModifier, 12802 ArrayRef<OpenMPMapModifierKind> MapTypeModifiers, 12803 ArrayRef<SourceLocation> MapTypeModifiersLoc, bool IsMapTypeImplicit, 12804 SourceLocation DepLinMapLastLoc) { 12805 SourceLocation StartLoc = Locs.StartLoc; 12806 SourceLocation LParenLoc = Locs.LParenLoc; 12807 SourceLocation EndLoc = Locs.EndLoc; 12808 OMPClause *Res = nullptr; 12809 switch (Kind) { 12810 case OMPC_private: 12811 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc); 12812 break; 12813 case OMPC_firstprivate: 12814 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 12815 break; 12816 case OMPC_lastprivate: 12817 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_LASTPRIVATE_unknown && 12818 "Unexpected lastprivate modifier."); 12819 Res = ActOnOpenMPLastprivateClause( 12820 VarList, static_cast<OpenMPLastprivateModifier>(ExtraModifier), 12821 DepLinMapLastLoc, ColonLoc, StartLoc, LParenLoc, EndLoc); 12822 break; 12823 case OMPC_shared: 12824 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc); 12825 break; 12826 case OMPC_reduction: 12827 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 12828 EndLoc, ReductionOrMapperIdScopeSpec, 12829 ReductionOrMapperId); 12830 break; 12831 case OMPC_task_reduction: 12832 Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 12833 EndLoc, ReductionOrMapperIdScopeSpec, 12834 ReductionOrMapperId); 12835 break; 12836 case OMPC_in_reduction: 12837 Res = ActOnOpenMPInReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 12838 EndLoc, ReductionOrMapperIdScopeSpec, 12839 ReductionOrMapperId); 12840 break; 12841 case OMPC_linear: 12842 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_LINEAR_unknown && 12843 "Unexpected linear modifier."); 12844 Res = ActOnOpenMPLinearClause( 12845 VarList, TailExpr, StartLoc, LParenLoc, 12846 static_cast<OpenMPLinearClauseKind>(ExtraModifier), DepLinMapLastLoc, 12847 ColonLoc, EndLoc); 12848 break; 12849 case OMPC_aligned: 12850 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc, 12851 ColonLoc, EndLoc); 12852 break; 12853 case OMPC_copyin: 12854 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc); 12855 break; 12856 case OMPC_copyprivate: 12857 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 12858 break; 12859 case OMPC_flush: 12860 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc); 12861 break; 12862 case OMPC_depend: 12863 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_DEPEND_unknown && 12864 "Unexpected depend modifier."); 12865 Res = ActOnOpenMPDependClause( 12866 static_cast<OpenMPDependClauseKind>(ExtraModifier), DepLinMapLastLoc, 12867 ColonLoc, VarList, StartLoc, LParenLoc, EndLoc); 12868 break; 12869 case OMPC_map: 12870 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_MAP_unknown && 12871 "Unexpected map modifier."); 12872 Res = ActOnOpenMPMapClause( 12873 MapTypeModifiers, MapTypeModifiersLoc, ReductionOrMapperIdScopeSpec, 12874 ReductionOrMapperId, static_cast<OpenMPMapClauseKind>(ExtraModifier), 12875 IsMapTypeImplicit, DepLinMapLastLoc, ColonLoc, VarList, Locs); 12876 break; 12877 case OMPC_to: 12878 Res = ActOnOpenMPToClause(VarList, ReductionOrMapperIdScopeSpec, 12879 ReductionOrMapperId, Locs); 12880 break; 12881 case OMPC_from: 12882 Res = ActOnOpenMPFromClause(VarList, ReductionOrMapperIdScopeSpec, 12883 ReductionOrMapperId, Locs); 12884 break; 12885 case OMPC_use_device_ptr: 12886 Res = ActOnOpenMPUseDevicePtrClause(VarList, Locs); 12887 break; 12888 case OMPC_is_device_ptr: 12889 Res = ActOnOpenMPIsDevicePtrClause(VarList, Locs); 12890 break; 12891 case OMPC_allocate: 12892 Res = ActOnOpenMPAllocateClause(TailExpr, VarList, StartLoc, LParenLoc, 12893 ColonLoc, EndLoc); 12894 break; 12895 case OMPC_nontemporal: 12896 Res = ActOnOpenMPNontemporalClause(VarList, StartLoc, LParenLoc, EndLoc); 12897 break; 12898 case OMPC_if: 12899 case OMPC_depobj: 12900 case OMPC_final: 12901 case OMPC_num_threads: 12902 case OMPC_safelen: 12903 case OMPC_simdlen: 12904 case OMPC_allocator: 12905 case OMPC_collapse: 12906 case OMPC_default: 12907 case OMPC_proc_bind: 12908 case OMPC_schedule: 12909 case OMPC_ordered: 12910 case OMPC_nowait: 12911 case OMPC_untied: 12912 case OMPC_mergeable: 12913 case OMPC_threadprivate: 12914 case OMPC_read: 12915 case OMPC_write: 12916 case OMPC_update: 12917 case OMPC_capture: 12918 case OMPC_seq_cst: 12919 case OMPC_acq_rel: 12920 case OMPC_acquire: 12921 case OMPC_release: 12922 case OMPC_relaxed: 12923 case OMPC_device: 12924 case OMPC_threads: 12925 case OMPC_simd: 12926 case OMPC_num_teams: 12927 case OMPC_thread_limit: 12928 case OMPC_priority: 12929 case OMPC_grainsize: 12930 case OMPC_nogroup: 12931 case OMPC_num_tasks: 12932 case OMPC_hint: 12933 case OMPC_dist_schedule: 12934 case OMPC_defaultmap: 12935 case OMPC_unknown: 12936 case OMPC_uniform: 12937 case OMPC_unified_address: 12938 case OMPC_unified_shared_memory: 12939 case OMPC_reverse_offload: 12940 case OMPC_dynamic_allocators: 12941 case OMPC_atomic_default_mem_order: 12942 case OMPC_device_type: 12943 case OMPC_match: 12944 case OMPC_order: 12945 case OMPC_destroy: 12946 llvm_unreachable("Clause is not allowed."); 12947 } 12948 return Res; 12949 } 12950 12951 ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK, 12952 ExprObjectKind OK, SourceLocation Loc) { 12953 ExprResult Res = BuildDeclRefExpr( 12954 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc); 12955 if (!Res.isUsable()) 12956 return ExprError(); 12957 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) { 12958 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get()); 12959 if (!Res.isUsable()) 12960 return ExprError(); 12961 } 12962 if (VK != VK_LValue && Res.get()->isGLValue()) { 12963 Res = DefaultLvalueConversion(Res.get()); 12964 if (!Res.isUsable()) 12965 return ExprError(); 12966 } 12967 return Res; 12968 } 12969 12970 OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList, 12971 SourceLocation StartLoc, 12972 SourceLocation LParenLoc, 12973 SourceLocation EndLoc) { 12974 SmallVector<Expr *, 8> Vars; 12975 SmallVector<Expr *, 8> PrivateCopies; 12976 for (Expr *RefExpr : VarList) { 12977 assert(RefExpr && "NULL expr in OpenMP private clause."); 12978 SourceLocation ELoc; 12979 SourceRange ERange; 12980 Expr *SimpleRefExpr = RefExpr; 12981 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 12982 if (Res.second) { 12983 // It will be analyzed later. 12984 Vars.push_back(RefExpr); 12985 PrivateCopies.push_back(nullptr); 12986 } 12987 ValueDecl *D = Res.first; 12988 if (!D) 12989 continue; 12990 12991 QualType Type = D->getType(); 12992 auto *VD = dyn_cast<VarDecl>(D); 12993 12994 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 12995 // A variable that appears in a private clause must not have an incomplete 12996 // type or a reference type. 12997 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type)) 12998 continue; 12999 Type = Type.getNonReferenceType(); 13000 13001 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions] 13002 // A variable that is privatized must not have a const-qualified type 13003 // unless it is of class type with a mutable member. This restriction does 13004 // not apply to the firstprivate clause. 13005 // 13006 // OpenMP 3.1 [2.9.3.3, private clause, Restrictions] 13007 // A variable that appears in a private clause must not have a 13008 // const-qualified type unless it is of class type with a mutable member. 13009 if (rejectConstNotMutableType(*this, D, Type, OMPC_private, ELoc)) 13010 continue; 13011 13012 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 13013 // in a Construct] 13014 // Variables with the predetermined data-sharing attributes may not be 13015 // listed in data-sharing attributes clauses, except for the cases 13016 // listed below. For these exceptions only, listing a predetermined 13017 // variable in a data-sharing attribute clause is allowed and overrides 13018 // the variable's predetermined data-sharing attributes. 13019 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 13020 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) { 13021 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 13022 << getOpenMPClauseName(OMPC_private); 13023 reportOriginalDsa(*this, DSAStack, D, DVar); 13024 continue; 13025 } 13026 13027 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 13028 // Variably modified types are not supported for tasks. 13029 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && 13030 isOpenMPTaskingDirective(CurrDir)) { 13031 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 13032 << getOpenMPClauseName(OMPC_private) << Type 13033 << getOpenMPDirectiveName(CurrDir); 13034 bool IsDecl = 13035 !VD || 13036 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 13037 Diag(D->getLocation(), 13038 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 13039 << D; 13040 continue; 13041 } 13042 13043 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 13044 // A list item cannot appear in both a map clause and a data-sharing 13045 // attribute clause on the same construct 13046 // 13047 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7] 13048 // A list item cannot appear in both a map clause and a data-sharing 13049 // attribute clause on the same construct unless the construct is a 13050 // combined construct. 13051 if ((LangOpts.OpenMP <= 45 && isOpenMPTargetExecutionDirective(CurrDir)) || 13052 CurrDir == OMPD_target) { 13053 OpenMPClauseKind ConflictKind; 13054 if (DSAStack->checkMappableExprComponentListsForDecl( 13055 VD, /*CurrentRegionOnly=*/true, 13056 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef, 13057 OpenMPClauseKind WhereFoundClauseKind) -> bool { 13058 ConflictKind = WhereFoundClauseKind; 13059 return true; 13060 })) { 13061 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 13062 << getOpenMPClauseName(OMPC_private) 13063 << getOpenMPClauseName(ConflictKind) 13064 << getOpenMPDirectiveName(CurrDir); 13065 reportOriginalDsa(*this, DSAStack, D, DVar); 13066 continue; 13067 } 13068 } 13069 13070 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1] 13071 // A variable of class type (or array thereof) that appears in a private 13072 // clause requires an accessible, unambiguous default constructor for the 13073 // class type. 13074 // Generate helper private variable and initialize it with the default 13075 // value. The address of the original variable is replaced by the address of 13076 // the new private variable in CodeGen. This new variable is not added to 13077 // IdResolver, so the code in the OpenMP region uses original variable for 13078 // proper diagnostics. 13079 Type = Type.getUnqualifiedType(); 13080 VarDecl *VDPrivate = 13081 buildVarDecl(*this, ELoc, Type, D->getName(), 13082 D->hasAttrs() ? &D->getAttrs() : nullptr, 13083 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 13084 ActOnUninitializedDecl(VDPrivate); 13085 if (VDPrivate->isInvalidDecl()) 13086 continue; 13087 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 13088 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc); 13089 13090 DeclRefExpr *Ref = nullptr; 13091 if (!VD && !CurContext->isDependentContext()) 13092 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 13093 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref); 13094 Vars.push_back((VD || CurContext->isDependentContext()) 13095 ? RefExpr->IgnoreParens() 13096 : Ref); 13097 PrivateCopies.push_back(VDPrivateRefExpr); 13098 } 13099 13100 if (Vars.empty()) 13101 return nullptr; 13102 13103 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 13104 PrivateCopies); 13105 } 13106 13107 namespace { 13108 class DiagsUninitializedSeveretyRAII { 13109 private: 13110 DiagnosticsEngine &Diags; 13111 SourceLocation SavedLoc; 13112 bool IsIgnored = false; 13113 13114 public: 13115 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc, 13116 bool IsIgnored) 13117 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) { 13118 if (!IsIgnored) { 13119 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init, 13120 /*Map*/ diag::Severity::Ignored, Loc); 13121 } 13122 } 13123 ~DiagsUninitializedSeveretyRAII() { 13124 if (!IsIgnored) 13125 Diags.popMappings(SavedLoc); 13126 } 13127 }; 13128 } 13129 13130 OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList, 13131 SourceLocation StartLoc, 13132 SourceLocation LParenLoc, 13133 SourceLocation EndLoc) { 13134 SmallVector<Expr *, 8> Vars; 13135 SmallVector<Expr *, 8> PrivateCopies; 13136 SmallVector<Expr *, 8> Inits; 13137 SmallVector<Decl *, 4> ExprCaptures; 13138 bool IsImplicitClause = 13139 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid(); 13140 SourceLocation ImplicitClauseLoc = DSAStack->getConstructLoc(); 13141 13142 for (Expr *RefExpr : VarList) { 13143 assert(RefExpr && "NULL expr in OpenMP firstprivate clause."); 13144 SourceLocation ELoc; 13145 SourceRange ERange; 13146 Expr *SimpleRefExpr = RefExpr; 13147 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 13148 if (Res.second) { 13149 // It will be analyzed later. 13150 Vars.push_back(RefExpr); 13151 PrivateCopies.push_back(nullptr); 13152 Inits.push_back(nullptr); 13153 } 13154 ValueDecl *D = Res.first; 13155 if (!D) 13156 continue; 13157 13158 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc; 13159 QualType Type = D->getType(); 13160 auto *VD = dyn_cast<VarDecl>(D); 13161 13162 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 13163 // A variable that appears in a private clause must not have an incomplete 13164 // type or a reference type. 13165 if (RequireCompleteType(ELoc, Type, 13166 diag::err_omp_firstprivate_incomplete_type)) 13167 continue; 13168 Type = Type.getNonReferenceType(); 13169 13170 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1] 13171 // A variable of class type (or array thereof) that appears in a private 13172 // clause requires an accessible, unambiguous copy constructor for the 13173 // class type. 13174 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType(); 13175 13176 // If an implicit firstprivate variable found it was checked already. 13177 DSAStackTy::DSAVarData TopDVar; 13178 if (!IsImplicitClause) { 13179 DSAStackTy::DSAVarData DVar = 13180 DSAStack->getTopDSA(D, /*FromParent=*/false); 13181 TopDVar = DVar; 13182 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 13183 bool IsConstant = ElemType.isConstant(Context); 13184 // OpenMP [2.4.13, Data-sharing Attribute Clauses] 13185 // A list item that specifies a given variable may not appear in more 13186 // than one clause on the same directive, except that a variable may be 13187 // specified in both firstprivate and lastprivate clauses. 13188 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3] 13189 // A list item may appear in a firstprivate or lastprivate clause but not 13190 // both. 13191 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate && 13192 (isOpenMPDistributeDirective(CurrDir) || 13193 DVar.CKind != OMPC_lastprivate) && 13194 DVar.RefExpr) { 13195 Diag(ELoc, diag::err_omp_wrong_dsa) 13196 << getOpenMPClauseName(DVar.CKind) 13197 << getOpenMPClauseName(OMPC_firstprivate); 13198 reportOriginalDsa(*this, DSAStack, D, DVar); 13199 continue; 13200 } 13201 13202 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 13203 // in a Construct] 13204 // Variables with the predetermined data-sharing attributes may not be 13205 // listed in data-sharing attributes clauses, except for the cases 13206 // listed below. For these exceptions only, listing a predetermined 13207 // variable in a data-sharing attribute clause is allowed and overrides 13208 // the variable's predetermined data-sharing attributes. 13209 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 13210 // in a Construct, C/C++, p.2] 13211 // Variables with const-qualified type having no mutable member may be 13212 // listed in a firstprivate clause, even if they are static data members. 13213 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr && 13214 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) { 13215 Diag(ELoc, diag::err_omp_wrong_dsa) 13216 << getOpenMPClauseName(DVar.CKind) 13217 << getOpenMPClauseName(OMPC_firstprivate); 13218 reportOriginalDsa(*this, DSAStack, D, DVar); 13219 continue; 13220 } 13221 13222 // OpenMP [2.9.3.4, Restrictions, p.2] 13223 // A list item that is private within a parallel region must not appear 13224 // in a firstprivate clause on a worksharing construct if any of the 13225 // worksharing regions arising from the worksharing construct ever bind 13226 // to any of the parallel regions arising from the parallel construct. 13227 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3] 13228 // A list item that is private within a teams region must not appear in a 13229 // firstprivate clause on a distribute construct if any of the distribute 13230 // regions arising from the distribute construct ever bind to any of the 13231 // teams regions arising from the teams construct. 13232 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3] 13233 // A list item that appears in a reduction clause of a teams construct 13234 // must not appear in a firstprivate clause on a distribute construct if 13235 // any of the distribute regions arising from the distribute construct 13236 // ever bind to any of the teams regions arising from the teams construct. 13237 if ((isOpenMPWorksharingDirective(CurrDir) || 13238 isOpenMPDistributeDirective(CurrDir)) && 13239 !isOpenMPParallelDirective(CurrDir) && 13240 !isOpenMPTeamsDirective(CurrDir)) { 13241 DVar = DSAStack->getImplicitDSA(D, true); 13242 if (DVar.CKind != OMPC_shared && 13243 (isOpenMPParallelDirective(DVar.DKind) || 13244 isOpenMPTeamsDirective(DVar.DKind) || 13245 DVar.DKind == OMPD_unknown)) { 13246 Diag(ELoc, diag::err_omp_required_access) 13247 << getOpenMPClauseName(OMPC_firstprivate) 13248 << getOpenMPClauseName(OMPC_shared); 13249 reportOriginalDsa(*this, DSAStack, D, DVar); 13250 continue; 13251 } 13252 } 13253 // OpenMP [2.9.3.4, Restrictions, p.3] 13254 // A list item that appears in a reduction clause of a parallel construct 13255 // must not appear in a firstprivate clause on a worksharing or task 13256 // construct if any of the worksharing or task regions arising from the 13257 // worksharing or task construct ever bind to any of the parallel regions 13258 // arising from the parallel construct. 13259 // OpenMP [2.9.3.4, Restrictions, p.4] 13260 // A list item that appears in a reduction clause in worksharing 13261 // construct must not appear in a firstprivate clause in a task construct 13262 // encountered during execution of any of the worksharing regions arising 13263 // from the worksharing construct. 13264 if (isOpenMPTaskingDirective(CurrDir)) { 13265 DVar = DSAStack->hasInnermostDSA( 13266 D, [](OpenMPClauseKind C) { return C == OMPC_reduction; }, 13267 [](OpenMPDirectiveKind K) { 13268 return isOpenMPParallelDirective(K) || 13269 isOpenMPWorksharingDirective(K) || 13270 isOpenMPTeamsDirective(K); 13271 }, 13272 /*FromParent=*/true); 13273 if (DVar.CKind == OMPC_reduction && 13274 (isOpenMPParallelDirective(DVar.DKind) || 13275 isOpenMPWorksharingDirective(DVar.DKind) || 13276 isOpenMPTeamsDirective(DVar.DKind))) { 13277 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate) 13278 << getOpenMPDirectiveName(DVar.DKind); 13279 reportOriginalDsa(*this, DSAStack, D, DVar); 13280 continue; 13281 } 13282 } 13283 13284 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 13285 // A list item cannot appear in both a map clause and a data-sharing 13286 // attribute clause on the same construct 13287 // 13288 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7] 13289 // A list item cannot appear in both a map clause and a data-sharing 13290 // attribute clause on the same construct unless the construct is a 13291 // combined construct. 13292 if ((LangOpts.OpenMP <= 45 && 13293 isOpenMPTargetExecutionDirective(CurrDir)) || 13294 CurrDir == OMPD_target) { 13295 OpenMPClauseKind ConflictKind; 13296 if (DSAStack->checkMappableExprComponentListsForDecl( 13297 VD, /*CurrentRegionOnly=*/true, 13298 [&ConflictKind]( 13299 OMPClauseMappableExprCommon::MappableExprComponentListRef, 13300 OpenMPClauseKind WhereFoundClauseKind) { 13301 ConflictKind = WhereFoundClauseKind; 13302 return true; 13303 })) { 13304 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 13305 << getOpenMPClauseName(OMPC_firstprivate) 13306 << getOpenMPClauseName(ConflictKind) 13307 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 13308 reportOriginalDsa(*this, DSAStack, D, DVar); 13309 continue; 13310 } 13311 } 13312 } 13313 13314 // Variably modified types are not supported for tasks. 13315 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && 13316 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) { 13317 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 13318 << getOpenMPClauseName(OMPC_firstprivate) << Type 13319 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 13320 bool IsDecl = 13321 !VD || 13322 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 13323 Diag(D->getLocation(), 13324 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 13325 << D; 13326 continue; 13327 } 13328 13329 Type = Type.getUnqualifiedType(); 13330 VarDecl *VDPrivate = 13331 buildVarDecl(*this, ELoc, Type, D->getName(), 13332 D->hasAttrs() ? &D->getAttrs() : nullptr, 13333 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 13334 // Generate helper private variable and initialize it with the value of the 13335 // original variable. The address of the original variable is replaced by 13336 // the address of the new private variable in the CodeGen. This new variable 13337 // is not added to IdResolver, so the code in the OpenMP region uses 13338 // original variable for proper diagnostics and variable capturing. 13339 Expr *VDInitRefExpr = nullptr; 13340 // For arrays generate initializer for single element and replace it by the 13341 // original array element in CodeGen. 13342 if (Type->isArrayType()) { 13343 VarDecl *VDInit = 13344 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName()); 13345 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc); 13346 Expr *Init = DefaultLvalueConversion(VDInitRefExpr).get(); 13347 ElemType = ElemType.getUnqualifiedType(); 13348 VarDecl *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, 13349 ".firstprivate.temp"); 13350 InitializedEntity Entity = 13351 InitializedEntity::InitializeVariable(VDInitTemp); 13352 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc); 13353 13354 InitializationSequence InitSeq(*this, Entity, Kind, Init); 13355 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init); 13356 if (Result.isInvalid()) 13357 VDPrivate->setInvalidDecl(); 13358 else 13359 VDPrivate->setInit(Result.getAs<Expr>()); 13360 // Remove temp variable declaration. 13361 Context.Deallocate(VDInitTemp); 13362 } else { 13363 VarDecl *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type, 13364 ".firstprivate.temp"); 13365 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(), 13366 RefExpr->getExprLoc()); 13367 AddInitializerToDecl(VDPrivate, 13368 DefaultLvalueConversion(VDInitRefExpr).get(), 13369 /*DirectInit=*/false); 13370 } 13371 if (VDPrivate->isInvalidDecl()) { 13372 if (IsImplicitClause) { 13373 Diag(RefExpr->getExprLoc(), 13374 diag::note_omp_task_predetermined_firstprivate_here); 13375 } 13376 continue; 13377 } 13378 CurContext->addDecl(VDPrivate); 13379 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 13380 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), 13381 RefExpr->getExprLoc()); 13382 DeclRefExpr *Ref = nullptr; 13383 if (!VD && !CurContext->isDependentContext()) { 13384 if (TopDVar.CKind == OMPC_lastprivate) { 13385 Ref = TopDVar.PrivateCopy; 13386 } else { 13387 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 13388 if (!isOpenMPCapturedDecl(D)) 13389 ExprCaptures.push_back(Ref->getDecl()); 13390 } 13391 } 13392 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 13393 Vars.push_back((VD || CurContext->isDependentContext()) 13394 ? RefExpr->IgnoreParens() 13395 : Ref); 13396 PrivateCopies.push_back(VDPrivateRefExpr); 13397 Inits.push_back(VDInitRefExpr); 13398 } 13399 13400 if (Vars.empty()) 13401 return nullptr; 13402 13403 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 13404 Vars, PrivateCopies, Inits, 13405 buildPreInits(Context, ExprCaptures)); 13406 } 13407 13408 OMPClause *Sema::ActOnOpenMPLastprivateClause( 13409 ArrayRef<Expr *> VarList, OpenMPLastprivateModifier LPKind, 13410 SourceLocation LPKindLoc, SourceLocation ColonLoc, SourceLocation StartLoc, 13411 SourceLocation LParenLoc, SourceLocation EndLoc) { 13412 if (LPKind == OMPC_LASTPRIVATE_unknown && LPKindLoc.isValid()) { 13413 assert(ColonLoc.isValid() && "Colon location must be valid."); 13414 Diag(LPKindLoc, diag::err_omp_unexpected_clause_value) 13415 << getListOfPossibleValues(OMPC_lastprivate, /*First=*/0, 13416 /*Last=*/OMPC_LASTPRIVATE_unknown) 13417 << getOpenMPClauseName(OMPC_lastprivate); 13418 return nullptr; 13419 } 13420 13421 SmallVector<Expr *, 8> Vars; 13422 SmallVector<Expr *, 8> SrcExprs; 13423 SmallVector<Expr *, 8> DstExprs; 13424 SmallVector<Expr *, 8> AssignmentOps; 13425 SmallVector<Decl *, 4> ExprCaptures; 13426 SmallVector<Expr *, 4> ExprPostUpdates; 13427 for (Expr *RefExpr : VarList) { 13428 assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); 13429 SourceLocation ELoc; 13430 SourceRange ERange; 13431 Expr *SimpleRefExpr = RefExpr; 13432 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 13433 if (Res.second) { 13434 // It will be analyzed later. 13435 Vars.push_back(RefExpr); 13436 SrcExprs.push_back(nullptr); 13437 DstExprs.push_back(nullptr); 13438 AssignmentOps.push_back(nullptr); 13439 } 13440 ValueDecl *D = Res.first; 13441 if (!D) 13442 continue; 13443 13444 QualType Type = D->getType(); 13445 auto *VD = dyn_cast<VarDecl>(D); 13446 13447 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2] 13448 // A variable that appears in a lastprivate clause must not have an 13449 // incomplete type or a reference type. 13450 if (RequireCompleteType(ELoc, Type, 13451 diag::err_omp_lastprivate_incomplete_type)) 13452 continue; 13453 Type = Type.getNonReferenceType(); 13454 13455 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions] 13456 // A variable that is privatized must not have a const-qualified type 13457 // unless it is of class type with a mutable member. This restriction does 13458 // not apply to the firstprivate clause. 13459 // 13460 // OpenMP 3.1 [2.9.3.5, lastprivate clause, Restrictions] 13461 // A variable that appears in a lastprivate clause must not have a 13462 // const-qualified type unless it is of class type with a mutable member. 13463 if (rejectConstNotMutableType(*this, D, Type, OMPC_lastprivate, ELoc)) 13464 continue; 13465 13466 // OpenMP 5.0 [2.19.4.5 lastprivate Clause, Restrictions] 13467 // A list item that appears in a lastprivate clause with the conditional 13468 // modifier must be a scalar variable. 13469 if (LPKind == OMPC_LASTPRIVATE_conditional && !Type->isScalarType()) { 13470 Diag(ELoc, diag::err_omp_lastprivate_conditional_non_scalar); 13471 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 13472 VarDecl::DeclarationOnly; 13473 Diag(D->getLocation(), 13474 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 13475 << D; 13476 continue; 13477 } 13478 13479 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 13480 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 13481 // in a Construct] 13482 // Variables with the predetermined data-sharing attributes may not be 13483 // listed in data-sharing attributes clauses, except for the cases 13484 // listed below. 13485 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3] 13486 // A list item may appear in a firstprivate or lastprivate clause but not 13487 // both. 13488 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 13489 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate && 13490 (isOpenMPDistributeDirective(CurrDir) || 13491 DVar.CKind != OMPC_firstprivate) && 13492 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) { 13493 Diag(ELoc, diag::err_omp_wrong_dsa) 13494 << getOpenMPClauseName(DVar.CKind) 13495 << getOpenMPClauseName(OMPC_lastprivate); 13496 reportOriginalDsa(*this, DSAStack, D, DVar); 13497 continue; 13498 } 13499 13500 // OpenMP [2.14.3.5, Restrictions, p.2] 13501 // A list item that is private within a parallel region, or that appears in 13502 // the reduction clause of a parallel construct, must not appear in a 13503 // lastprivate clause on a worksharing construct if any of the corresponding 13504 // worksharing regions ever binds to any of the corresponding parallel 13505 // regions. 13506 DSAStackTy::DSAVarData TopDVar = DVar; 13507 if (isOpenMPWorksharingDirective(CurrDir) && 13508 !isOpenMPParallelDirective(CurrDir) && 13509 !isOpenMPTeamsDirective(CurrDir)) { 13510 DVar = DSAStack->getImplicitDSA(D, true); 13511 if (DVar.CKind != OMPC_shared) { 13512 Diag(ELoc, diag::err_omp_required_access) 13513 << getOpenMPClauseName(OMPC_lastprivate) 13514 << getOpenMPClauseName(OMPC_shared); 13515 reportOriginalDsa(*this, DSAStack, D, DVar); 13516 continue; 13517 } 13518 } 13519 13520 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2] 13521 // A variable of class type (or array thereof) that appears in a 13522 // lastprivate clause requires an accessible, unambiguous default 13523 // constructor for the class type, unless the list item is also specified 13524 // in a firstprivate clause. 13525 // A variable of class type (or array thereof) that appears in a 13526 // lastprivate clause requires an accessible, unambiguous copy assignment 13527 // operator for the class type. 13528 Type = Context.getBaseElementType(Type).getNonReferenceType(); 13529 VarDecl *SrcVD = buildVarDecl(*this, ERange.getBegin(), 13530 Type.getUnqualifiedType(), ".lastprivate.src", 13531 D->hasAttrs() ? &D->getAttrs() : nullptr); 13532 DeclRefExpr *PseudoSrcExpr = 13533 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc); 13534 VarDecl *DstVD = 13535 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst", 13536 D->hasAttrs() ? &D->getAttrs() : nullptr); 13537 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc); 13538 // For arrays generate assignment operation for single element and replace 13539 // it by the original array element in CodeGen. 13540 ExprResult AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign, 13541 PseudoDstExpr, PseudoSrcExpr); 13542 if (AssignmentOp.isInvalid()) 13543 continue; 13544 AssignmentOp = 13545 ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false); 13546 if (AssignmentOp.isInvalid()) 13547 continue; 13548 13549 DeclRefExpr *Ref = nullptr; 13550 if (!VD && !CurContext->isDependentContext()) { 13551 if (TopDVar.CKind == OMPC_firstprivate) { 13552 Ref = TopDVar.PrivateCopy; 13553 } else { 13554 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 13555 if (!isOpenMPCapturedDecl(D)) 13556 ExprCaptures.push_back(Ref->getDecl()); 13557 } 13558 if (TopDVar.CKind == OMPC_firstprivate || 13559 (!isOpenMPCapturedDecl(D) && 13560 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) { 13561 ExprResult RefRes = DefaultLvalueConversion(Ref); 13562 if (!RefRes.isUsable()) 13563 continue; 13564 ExprResult PostUpdateRes = 13565 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr, 13566 RefRes.get()); 13567 if (!PostUpdateRes.isUsable()) 13568 continue; 13569 ExprPostUpdates.push_back( 13570 IgnoredValueConversions(PostUpdateRes.get()).get()); 13571 } 13572 } 13573 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref); 13574 Vars.push_back((VD || CurContext->isDependentContext()) 13575 ? RefExpr->IgnoreParens() 13576 : Ref); 13577 SrcExprs.push_back(PseudoSrcExpr); 13578 DstExprs.push_back(PseudoDstExpr); 13579 AssignmentOps.push_back(AssignmentOp.get()); 13580 } 13581 13582 if (Vars.empty()) 13583 return nullptr; 13584 13585 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 13586 Vars, SrcExprs, DstExprs, AssignmentOps, 13587 LPKind, LPKindLoc, ColonLoc, 13588 buildPreInits(Context, ExprCaptures), 13589 buildPostUpdate(*this, ExprPostUpdates)); 13590 } 13591 13592 OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList, 13593 SourceLocation StartLoc, 13594 SourceLocation LParenLoc, 13595 SourceLocation EndLoc) { 13596 SmallVector<Expr *, 8> Vars; 13597 for (Expr *RefExpr : VarList) { 13598 assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); 13599 SourceLocation ELoc; 13600 SourceRange ERange; 13601 Expr *SimpleRefExpr = RefExpr; 13602 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 13603 if (Res.second) { 13604 // It will be analyzed later. 13605 Vars.push_back(RefExpr); 13606 } 13607 ValueDecl *D = Res.first; 13608 if (!D) 13609 continue; 13610 13611 auto *VD = dyn_cast<VarDecl>(D); 13612 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 13613 // in a Construct] 13614 // Variables with the predetermined data-sharing attributes may not be 13615 // listed in data-sharing attributes clauses, except for the cases 13616 // listed below. For these exceptions only, listing a predetermined 13617 // variable in a data-sharing attribute clause is allowed and overrides 13618 // the variable's predetermined data-sharing attributes. 13619 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 13620 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared && 13621 DVar.RefExpr) { 13622 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 13623 << getOpenMPClauseName(OMPC_shared); 13624 reportOriginalDsa(*this, DSAStack, D, DVar); 13625 continue; 13626 } 13627 13628 DeclRefExpr *Ref = nullptr; 13629 if (!VD && isOpenMPCapturedDecl(D) && !CurContext->isDependentContext()) 13630 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 13631 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref); 13632 Vars.push_back((VD || !Ref || CurContext->isDependentContext()) 13633 ? RefExpr->IgnoreParens() 13634 : Ref); 13635 } 13636 13637 if (Vars.empty()) 13638 return nullptr; 13639 13640 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 13641 } 13642 13643 namespace { 13644 class DSARefChecker : public StmtVisitor<DSARefChecker, bool> { 13645 DSAStackTy *Stack; 13646 13647 public: 13648 bool VisitDeclRefExpr(DeclRefExpr *E) { 13649 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 13650 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false); 13651 if (DVar.CKind == OMPC_shared && !DVar.RefExpr) 13652 return false; 13653 if (DVar.CKind != OMPC_unknown) 13654 return true; 13655 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA( 13656 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) { return true; }, 13657 /*FromParent=*/true); 13658 return DVarPrivate.CKind != OMPC_unknown; 13659 } 13660 return false; 13661 } 13662 bool VisitStmt(Stmt *S) { 13663 for (Stmt *Child : S->children()) { 13664 if (Child && Visit(Child)) 13665 return true; 13666 } 13667 return false; 13668 } 13669 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {} 13670 }; 13671 } // namespace 13672 13673 namespace { 13674 // Transform MemberExpression for specified FieldDecl of current class to 13675 // DeclRefExpr to specified OMPCapturedExprDecl. 13676 class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> { 13677 typedef TreeTransform<TransformExprToCaptures> BaseTransform; 13678 ValueDecl *Field = nullptr; 13679 DeclRefExpr *CapturedExpr = nullptr; 13680 13681 public: 13682 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl) 13683 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {} 13684 13685 ExprResult TransformMemberExpr(MemberExpr *E) { 13686 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) && 13687 E->getMemberDecl() == Field) { 13688 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false); 13689 return CapturedExpr; 13690 } 13691 return BaseTransform::TransformMemberExpr(E); 13692 } 13693 DeclRefExpr *getCapturedExpr() { return CapturedExpr; } 13694 }; 13695 } // namespace 13696 13697 template <typename T, typename U> 13698 static T filterLookupForUDReductionAndMapper( 13699 SmallVectorImpl<U> &Lookups, const llvm::function_ref<T(ValueDecl *)> Gen) { 13700 for (U &Set : Lookups) { 13701 for (auto *D : Set) { 13702 if (T Res = Gen(cast<ValueDecl>(D))) 13703 return Res; 13704 } 13705 } 13706 return T(); 13707 } 13708 13709 static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D) { 13710 assert(!LookupResult::isVisible(SemaRef, D) && "not in slow case"); 13711 13712 for (auto RD : D->redecls()) { 13713 // Don't bother with extra checks if we already know this one isn't visible. 13714 if (RD == D) 13715 continue; 13716 13717 auto ND = cast<NamedDecl>(RD); 13718 if (LookupResult::isVisible(SemaRef, ND)) 13719 return ND; 13720 } 13721 13722 return nullptr; 13723 } 13724 13725 static void 13726 argumentDependentLookup(Sema &SemaRef, const DeclarationNameInfo &Id, 13727 SourceLocation Loc, QualType Ty, 13728 SmallVectorImpl<UnresolvedSet<8>> &Lookups) { 13729 // Find all of the associated namespaces and classes based on the 13730 // arguments we have. 13731 Sema::AssociatedNamespaceSet AssociatedNamespaces; 13732 Sema::AssociatedClassSet AssociatedClasses; 13733 OpaqueValueExpr OVE(Loc, Ty, VK_LValue); 13734 SemaRef.FindAssociatedClassesAndNamespaces(Loc, &OVE, AssociatedNamespaces, 13735 AssociatedClasses); 13736 13737 // C++ [basic.lookup.argdep]p3: 13738 // Let X be the lookup set produced by unqualified lookup (3.4.1) 13739 // and let Y be the lookup set produced by argument dependent 13740 // lookup (defined as follows). If X contains [...] then Y is 13741 // empty. Otherwise Y is the set of declarations found in the 13742 // namespaces associated with the argument types as described 13743 // below. The set of declarations found by the lookup of the name 13744 // is the union of X and Y. 13745 // 13746 // Here, we compute Y and add its members to the overloaded 13747 // candidate set. 13748 for (auto *NS : AssociatedNamespaces) { 13749 // When considering an associated namespace, the lookup is the 13750 // same as the lookup performed when the associated namespace is 13751 // used as a qualifier (3.4.3.2) except that: 13752 // 13753 // -- Any using-directives in the associated namespace are 13754 // ignored. 13755 // 13756 // -- Any namespace-scope friend functions declared in 13757 // associated classes are visible within their respective 13758 // namespaces even if they are not visible during an ordinary 13759 // lookup (11.4). 13760 DeclContext::lookup_result R = NS->lookup(Id.getName()); 13761 for (auto *D : R) { 13762 auto *Underlying = D; 13763 if (auto *USD = dyn_cast<UsingShadowDecl>(D)) 13764 Underlying = USD->getTargetDecl(); 13765 13766 if (!isa<OMPDeclareReductionDecl>(Underlying) && 13767 !isa<OMPDeclareMapperDecl>(Underlying)) 13768 continue; 13769 13770 if (!SemaRef.isVisible(D)) { 13771 D = findAcceptableDecl(SemaRef, D); 13772 if (!D) 13773 continue; 13774 if (auto *USD = dyn_cast<UsingShadowDecl>(D)) 13775 Underlying = USD->getTargetDecl(); 13776 } 13777 Lookups.emplace_back(); 13778 Lookups.back().addDecl(Underlying); 13779 } 13780 } 13781 } 13782 13783 static ExprResult 13784 buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range, 13785 Scope *S, CXXScopeSpec &ReductionIdScopeSpec, 13786 const DeclarationNameInfo &ReductionId, QualType Ty, 13787 CXXCastPath &BasePath, Expr *UnresolvedReduction) { 13788 if (ReductionIdScopeSpec.isInvalid()) 13789 return ExprError(); 13790 SmallVector<UnresolvedSet<8>, 4> Lookups; 13791 if (S) { 13792 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName); 13793 Lookup.suppressDiagnostics(); 13794 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) { 13795 NamedDecl *D = Lookup.getRepresentativeDecl(); 13796 do { 13797 S = S->getParent(); 13798 } while (S && !S->isDeclScope(D)); 13799 if (S) 13800 S = S->getParent(); 13801 Lookups.emplace_back(); 13802 Lookups.back().append(Lookup.begin(), Lookup.end()); 13803 Lookup.clear(); 13804 } 13805 } else if (auto *ULE = 13806 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) { 13807 Lookups.push_back(UnresolvedSet<8>()); 13808 Decl *PrevD = nullptr; 13809 for (NamedDecl *D : ULE->decls()) { 13810 if (D == PrevD) 13811 Lookups.push_back(UnresolvedSet<8>()); 13812 else if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(D)) 13813 Lookups.back().addDecl(DRD); 13814 PrevD = D; 13815 } 13816 } 13817 if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() || 13818 Ty->isInstantiationDependentType() || 13819 Ty->containsUnexpandedParameterPack() || 13820 filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) { 13821 return !D->isInvalidDecl() && 13822 (D->getType()->isDependentType() || 13823 D->getType()->isInstantiationDependentType() || 13824 D->getType()->containsUnexpandedParameterPack()); 13825 })) { 13826 UnresolvedSet<8> ResSet; 13827 for (const UnresolvedSet<8> &Set : Lookups) { 13828 if (Set.empty()) 13829 continue; 13830 ResSet.append(Set.begin(), Set.end()); 13831 // The last item marks the end of all declarations at the specified scope. 13832 ResSet.addDecl(Set[Set.size() - 1]); 13833 } 13834 return UnresolvedLookupExpr::Create( 13835 SemaRef.Context, /*NamingClass=*/nullptr, 13836 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId, 13837 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end()); 13838 } 13839 // Lookup inside the classes. 13840 // C++ [over.match.oper]p3: 13841 // For a unary operator @ with an operand of a type whose 13842 // cv-unqualified version is T1, and for a binary operator @ with 13843 // a left operand of a type whose cv-unqualified version is T1 and 13844 // a right operand of a type whose cv-unqualified version is T2, 13845 // three sets of candidate functions, designated member 13846 // candidates, non-member candidates and built-in candidates, are 13847 // constructed as follows: 13848 // -- If T1 is a complete class type or a class currently being 13849 // defined, the set of member candidates is the result of the 13850 // qualified lookup of T1::operator@ (13.3.1.1.1); otherwise, 13851 // the set of member candidates is empty. 13852 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName); 13853 Lookup.suppressDiagnostics(); 13854 if (const auto *TyRec = Ty->getAs<RecordType>()) { 13855 // Complete the type if it can be completed. 13856 // If the type is neither complete nor being defined, bail out now. 13857 if (SemaRef.isCompleteType(Loc, Ty) || TyRec->isBeingDefined() || 13858 TyRec->getDecl()->getDefinition()) { 13859 Lookup.clear(); 13860 SemaRef.LookupQualifiedName(Lookup, TyRec->getDecl()); 13861 if (Lookup.empty()) { 13862 Lookups.emplace_back(); 13863 Lookups.back().append(Lookup.begin(), Lookup.end()); 13864 } 13865 } 13866 } 13867 // Perform ADL. 13868 if (SemaRef.getLangOpts().CPlusPlus) 13869 argumentDependentLookup(SemaRef, ReductionId, Loc, Ty, Lookups); 13870 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 13871 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * { 13872 if (!D->isInvalidDecl() && 13873 SemaRef.Context.hasSameType(D->getType(), Ty)) 13874 return D; 13875 return nullptr; 13876 })) 13877 return SemaRef.BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(), 13878 VK_LValue, Loc); 13879 if (SemaRef.getLangOpts().CPlusPlus) { 13880 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 13881 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * { 13882 if (!D->isInvalidDecl() && 13883 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) && 13884 !Ty.isMoreQualifiedThan(D->getType())) 13885 return D; 13886 return nullptr; 13887 })) { 13888 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, 13889 /*DetectVirtual=*/false); 13890 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) { 13891 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType( 13892 VD->getType().getUnqualifiedType()))) { 13893 if (SemaRef.CheckBaseClassAccess( 13894 Loc, VD->getType(), Ty, Paths.front(), 13895 /*DiagID=*/0) != Sema::AR_inaccessible) { 13896 SemaRef.BuildBasePathArray(Paths, BasePath); 13897 return SemaRef.BuildDeclRefExpr( 13898 VD, VD->getType().getNonReferenceType(), VK_LValue, Loc); 13899 } 13900 } 13901 } 13902 } 13903 } 13904 if (ReductionIdScopeSpec.isSet()) { 13905 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) 13906 << Ty << Range; 13907 return ExprError(); 13908 } 13909 return ExprEmpty(); 13910 } 13911 13912 namespace { 13913 /// Data for the reduction-based clauses. 13914 struct ReductionData { 13915 /// List of original reduction items. 13916 SmallVector<Expr *, 8> Vars; 13917 /// List of private copies of the reduction items. 13918 SmallVector<Expr *, 8> Privates; 13919 /// LHS expressions for the reduction_op expressions. 13920 SmallVector<Expr *, 8> LHSs; 13921 /// RHS expressions for the reduction_op expressions. 13922 SmallVector<Expr *, 8> RHSs; 13923 /// Reduction operation expression. 13924 SmallVector<Expr *, 8> ReductionOps; 13925 /// Taskgroup descriptors for the corresponding reduction items in 13926 /// in_reduction clauses. 13927 SmallVector<Expr *, 8> TaskgroupDescriptors; 13928 /// List of captures for clause. 13929 SmallVector<Decl *, 4> ExprCaptures; 13930 /// List of postupdate expressions. 13931 SmallVector<Expr *, 4> ExprPostUpdates; 13932 ReductionData() = delete; 13933 /// Reserves required memory for the reduction data. 13934 ReductionData(unsigned Size) { 13935 Vars.reserve(Size); 13936 Privates.reserve(Size); 13937 LHSs.reserve(Size); 13938 RHSs.reserve(Size); 13939 ReductionOps.reserve(Size); 13940 TaskgroupDescriptors.reserve(Size); 13941 ExprCaptures.reserve(Size); 13942 ExprPostUpdates.reserve(Size); 13943 } 13944 /// Stores reduction item and reduction operation only (required for dependent 13945 /// reduction item). 13946 void push(Expr *Item, Expr *ReductionOp) { 13947 Vars.emplace_back(Item); 13948 Privates.emplace_back(nullptr); 13949 LHSs.emplace_back(nullptr); 13950 RHSs.emplace_back(nullptr); 13951 ReductionOps.emplace_back(ReductionOp); 13952 TaskgroupDescriptors.emplace_back(nullptr); 13953 } 13954 /// Stores reduction data. 13955 void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp, 13956 Expr *TaskgroupDescriptor) { 13957 Vars.emplace_back(Item); 13958 Privates.emplace_back(Private); 13959 LHSs.emplace_back(LHS); 13960 RHSs.emplace_back(RHS); 13961 ReductionOps.emplace_back(ReductionOp); 13962 TaskgroupDescriptors.emplace_back(TaskgroupDescriptor); 13963 } 13964 }; 13965 } // namespace 13966 13967 static bool checkOMPArraySectionConstantForReduction( 13968 ASTContext &Context, const OMPArraySectionExpr *OASE, bool &SingleElement, 13969 SmallVectorImpl<llvm::APSInt> &ArraySizes) { 13970 const Expr *Length = OASE->getLength(); 13971 if (Length == nullptr) { 13972 // For array sections of the form [1:] or [:], we would need to analyze 13973 // the lower bound... 13974 if (OASE->getColonLoc().isValid()) 13975 return false; 13976 13977 // This is an array subscript which has implicit length 1! 13978 SingleElement = true; 13979 ArraySizes.push_back(llvm::APSInt::get(1)); 13980 } else { 13981 Expr::EvalResult Result; 13982 if (!Length->EvaluateAsInt(Result, Context)) 13983 return false; 13984 13985 llvm::APSInt ConstantLengthValue = Result.Val.getInt(); 13986 SingleElement = (ConstantLengthValue.getSExtValue() == 1); 13987 ArraySizes.push_back(ConstantLengthValue); 13988 } 13989 13990 // Get the base of this array section and walk up from there. 13991 const Expr *Base = OASE->getBase()->IgnoreParenImpCasts(); 13992 13993 // We require length = 1 for all array sections except the right-most to 13994 // guarantee that the memory region is contiguous and has no holes in it. 13995 while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) { 13996 Length = TempOASE->getLength(); 13997 if (Length == nullptr) { 13998 // For array sections of the form [1:] or [:], we would need to analyze 13999 // the lower bound... 14000 if (OASE->getColonLoc().isValid()) 14001 return false; 14002 14003 // This is an array subscript which has implicit length 1! 14004 ArraySizes.push_back(llvm::APSInt::get(1)); 14005 } else { 14006 Expr::EvalResult Result; 14007 if (!Length->EvaluateAsInt(Result, Context)) 14008 return false; 14009 14010 llvm::APSInt ConstantLengthValue = Result.Val.getInt(); 14011 if (ConstantLengthValue.getSExtValue() != 1) 14012 return false; 14013 14014 ArraySizes.push_back(ConstantLengthValue); 14015 } 14016 Base = TempOASE->getBase()->IgnoreParenImpCasts(); 14017 } 14018 14019 // If we have a single element, we don't need to add the implicit lengths. 14020 if (!SingleElement) { 14021 while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) { 14022 // Has implicit length 1! 14023 ArraySizes.push_back(llvm::APSInt::get(1)); 14024 Base = TempASE->getBase()->IgnoreParenImpCasts(); 14025 } 14026 } 14027 14028 // This array section can be privatized as a single value or as a constant 14029 // sized array. 14030 return true; 14031 } 14032 14033 static bool actOnOMPReductionKindClause( 14034 Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind, 14035 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 14036 SourceLocation ColonLoc, SourceLocation EndLoc, 14037 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 14038 ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) { 14039 DeclarationName DN = ReductionId.getName(); 14040 OverloadedOperatorKind OOK = DN.getCXXOverloadedOperator(); 14041 BinaryOperatorKind BOK = BO_Comma; 14042 14043 ASTContext &Context = S.Context; 14044 // OpenMP [2.14.3.6, reduction clause] 14045 // C 14046 // reduction-identifier is either an identifier or one of the following 14047 // operators: +, -, *, &, |, ^, && and || 14048 // C++ 14049 // reduction-identifier is either an id-expression or one of the following 14050 // operators: +, -, *, &, |, ^, && and || 14051 switch (OOK) { 14052 case OO_Plus: 14053 case OO_Minus: 14054 BOK = BO_Add; 14055 break; 14056 case OO_Star: 14057 BOK = BO_Mul; 14058 break; 14059 case OO_Amp: 14060 BOK = BO_And; 14061 break; 14062 case OO_Pipe: 14063 BOK = BO_Or; 14064 break; 14065 case OO_Caret: 14066 BOK = BO_Xor; 14067 break; 14068 case OO_AmpAmp: 14069 BOK = BO_LAnd; 14070 break; 14071 case OO_PipePipe: 14072 BOK = BO_LOr; 14073 break; 14074 case OO_New: 14075 case OO_Delete: 14076 case OO_Array_New: 14077 case OO_Array_Delete: 14078 case OO_Slash: 14079 case OO_Percent: 14080 case OO_Tilde: 14081 case OO_Exclaim: 14082 case OO_Equal: 14083 case OO_Less: 14084 case OO_Greater: 14085 case OO_LessEqual: 14086 case OO_GreaterEqual: 14087 case OO_PlusEqual: 14088 case OO_MinusEqual: 14089 case OO_StarEqual: 14090 case OO_SlashEqual: 14091 case OO_PercentEqual: 14092 case OO_CaretEqual: 14093 case OO_AmpEqual: 14094 case OO_PipeEqual: 14095 case OO_LessLess: 14096 case OO_GreaterGreater: 14097 case OO_LessLessEqual: 14098 case OO_GreaterGreaterEqual: 14099 case OO_EqualEqual: 14100 case OO_ExclaimEqual: 14101 case OO_Spaceship: 14102 case OO_PlusPlus: 14103 case OO_MinusMinus: 14104 case OO_Comma: 14105 case OO_ArrowStar: 14106 case OO_Arrow: 14107 case OO_Call: 14108 case OO_Subscript: 14109 case OO_Conditional: 14110 case OO_Coawait: 14111 case NUM_OVERLOADED_OPERATORS: 14112 llvm_unreachable("Unexpected reduction identifier"); 14113 case OO_None: 14114 if (IdentifierInfo *II = DN.getAsIdentifierInfo()) { 14115 if (II->isStr("max")) 14116 BOK = BO_GT; 14117 else if (II->isStr("min")) 14118 BOK = BO_LT; 14119 } 14120 break; 14121 } 14122 SourceRange ReductionIdRange; 14123 if (ReductionIdScopeSpec.isValid()) 14124 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc()); 14125 else 14126 ReductionIdRange.setBegin(ReductionId.getBeginLoc()); 14127 ReductionIdRange.setEnd(ReductionId.getEndLoc()); 14128 14129 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end(); 14130 bool FirstIter = true; 14131 for (Expr *RefExpr : VarList) { 14132 assert(RefExpr && "nullptr expr in OpenMP reduction clause."); 14133 // OpenMP [2.1, C/C++] 14134 // A list item is a variable or array section, subject to the restrictions 14135 // specified in Section 2.4 on page 42 and in each of the sections 14136 // describing clauses and directives for which a list appears. 14137 // OpenMP [2.14.3.3, Restrictions, p.1] 14138 // A variable that is part of another variable (as an array or 14139 // structure element) cannot appear in a private clause. 14140 if (!FirstIter && IR != ER) 14141 ++IR; 14142 FirstIter = false; 14143 SourceLocation ELoc; 14144 SourceRange ERange; 14145 Expr *SimpleRefExpr = RefExpr; 14146 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange, 14147 /*AllowArraySection=*/true); 14148 if (Res.second) { 14149 // Try to find 'declare reduction' corresponding construct before using 14150 // builtin/overloaded operators. 14151 QualType Type = Context.DependentTy; 14152 CXXCastPath BasePath; 14153 ExprResult DeclareReductionRef = buildDeclareReductionRef( 14154 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec, 14155 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR); 14156 Expr *ReductionOp = nullptr; 14157 if (S.CurContext->isDependentContext() && 14158 (DeclareReductionRef.isUnset() || 14159 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) 14160 ReductionOp = DeclareReductionRef.get(); 14161 // It will be analyzed later. 14162 RD.push(RefExpr, ReductionOp); 14163 } 14164 ValueDecl *D = Res.first; 14165 if (!D) 14166 continue; 14167 14168 Expr *TaskgroupDescriptor = nullptr; 14169 QualType Type; 14170 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens()); 14171 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens()); 14172 if (ASE) { 14173 Type = ASE->getType().getNonReferenceType(); 14174 } else if (OASE) { 14175 QualType BaseType = 14176 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 14177 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 14178 Type = ATy->getElementType(); 14179 else 14180 Type = BaseType->getPointeeType(); 14181 Type = Type.getNonReferenceType(); 14182 } else { 14183 Type = Context.getBaseElementType(D->getType().getNonReferenceType()); 14184 } 14185 auto *VD = dyn_cast<VarDecl>(D); 14186 14187 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 14188 // A variable that appears in a private clause must not have an incomplete 14189 // type or a reference type. 14190 if (S.RequireCompleteType(ELoc, D->getType(), 14191 diag::err_omp_reduction_incomplete_type)) 14192 continue; 14193 // OpenMP [2.14.3.6, reduction clause, Restrictions] 14194 // A list item that appears in a reduction clause must not be 14195 // const-qualified. 14196 if (rejectConstNotMutableType(S, D, Type, ClauseKind, ELoc, 14197 /*AcceptIfMutable*/ false, ASE || OASE)) 14198 continue; 14199 14200 OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective(); 14201 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4] 14202 // If a list-item is a reference type then it must bind to the same object 14203 // for all threads of the team. 14204 if (!ASE && !OASE) { 14205 if (VD) { 14206 VarDecl *VDDef = VD->getDefinition(); 14207 if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) { 14208 DSARefChecker Check(Stack); 14209 if (Check.Visit(VDDef->getInit())) { 14210 S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg) 14211 << getOpenMPClauseName(ClauseKind) << ERange; 14212 S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef; 14213 continue; 14214 } 14215 } 14216 } 14217 14218 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 14219 // in a Construct] 14220 // Variables with the predetermined data-sharing attributes may not be 14221 // listed in data-sharing attributes clauses, except for the cases 14222 // listed below. For these exceptions only, listing a predetermined 14223 // variable in a data-sharing attribute clause is allowed and overrides 14224 // the variable's predetermined data-sharing attributes. 14225 // OpenMP [2.14.3.6, Restrictions, p.3] 14226 // Any number of reduction clauses can be specified on the directive, 14227 // but a list item can appear only once in the reduction clauses for that 14228 // directive. 14229 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false); 14230 if (DVar.CKind == OMPC_reduction) { 14231 S.Diag(ELoc, diag::err_omp_once_referenced) 14232 << getOpenMPClauseName(ClauseKind); 14233 if (DVar.RefExpr) 14234 S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced); 14235 continue; 14236 } 14237 if (DVar.CKind != OMPC_unknown) { 14238 S.Diag(ELoc, diag::err_omp_wrong_dsa) 14239 << getOpenMPClauseName(DVar.CKind) 14240 << getOpenMPClauseName(OMPC_reduction); 14241 reportOriginalDsa(S, Stack, D, DVar); 14242 continue; 14243 } 14244 14245 // OpenMP [2.14.3.6, Restrictions, p.1] 14246 // A list item that appears in a reduction clause of a worksharing 14247 // construct must be shared in the parallel regions to which any of the 14248 // worksharing regions arising from the worksharing construct bind. 14249 if (isOpenMPWorksharingDirective(CurrDir) && 14250 !isOpenMPParallelDirective(CurrDir) && 14251 !isOpenMPTeamsDirective(CurrDir)) { 14252 DVar = Stack->getImplicitDSA(D, true); 14253 if (DVar.CKind != OMPC_shared) { 14254 S.Diag(ELoc, diag::err_omp_required_access) 14255 << getOpenMPClauseName(OMPC_reduction) 14256 << getOpenMPClauseName(OMPC_shared); 14257 reportOriginalDsa(S, Stack, D, DVar); 14258 continue; 14259 } 14260 } 14261 } 14262 14263 // Try to find 'declare reduction' corresponding construct before using 14264 // builtin/overloaded operators. 14265 CXXCastPath BasePath; 14266 ExprResult DeclareReductionRef = buildDeclareReductionRef( 14267 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec, 14268 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR); 14269 if (DeclareReductionRef.isInvalid()) 14270 continue; 14271 if (S.CurContext->isDependentContext() && 14272 (DeclareReductionRef.isUnset() || 14273 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) { 14274 RD.push(RefExpr, DeclareReductionRef.get()); 14275 continue; 14276 } 14277 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) { 14278 // Not allowed reduction identifier is found. 14279 S.Diag(ReductionId.getBeginLoc(), 14280 diag::err_omp_unknown_reduction_identifier) 14281 << Type << ReductionIdRange; 14282 continue; 14283 } 14284 14285 // OpenMP [2.14.3.6, reduction clause, Restrictions] 14286 // The type of a list item that appears in a reduction clause must be valid 14287 // for the reduction-identifier. For a max or min reduction in C, the type 14288 // of the list item must be an allowed arithmetic data type: char, int, 14289 // float, double, or _Bool, possibly modified with long, short, signed, or 14290 // unsigned. For a max or min reduction in C++, the type of the list item 14291 // must be an allowed arithmetic data type: char, wchar_t, int, float, 14292 // double, or bool, possibly modified with long, short, signed, or unsigned. 14293 if (DeclareReductionRef.isUnset()) { 14294 if ((BOK == BO_GT || BOK == BO_LT) && 14295 !(Type->isScalarType() || 14296 (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) { 14297 S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg) 14298 << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus; 14299 if (!ASE && !OASE) { 14300 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 14301 VarDecl::DeclarationOnly; 14302 S.Diag(D->getLocation(), 14303 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 14304 << D; 14305 } 14306 continue; 14307 } 14308 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) && 14309 !S.getLangOpts().CPlusPlus && Type->isFloatingType()) { 14310 S.Diag(ELoc, diag::err_omp_clause_floating_type_arg) 14311 << getOpenMPClauseName(ClauseKind); 14312 if (!ASE && !OASE) { 14313 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 14314 VarDecl::DeclarationOnly; 14315 S.Diag(D->getLocation(), 14316 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 14317 << D; 14318 } 14319 continue; 14320 } 14321 } 14322 14323 Type = Type.getNonLValueExprType(Context).getUnqualifiedType(); 14324 VarDecl *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs", 14325 D->hasAttrs() ? &D->getAttrs() : nullptr); 14326 VarDecl *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(), 14327 D->hasAttrs() ? &D->getAttrs() : nullptr); 14328 QualType PrivateTy = Type; 14329 14330 // Try if we can determine constant lengths for all array sections and avoid 14331 // the VLA. 14332 bool ConstantLengthOASE = false; 14333 if (OASE) { 14334 bool SingleElement; 14335 llvm::SmallVector<llvm::APSInt, 4> ArraySizes; 14336 ConstantLengthOASE = checkOMPArraySectionConstantForReduction( 14337 Context, OASE, SingleElement, ArraySizes); 14338 14339 // If we don't have a single element, we must emit a constant array type. 14340 if (ConstantLengthOASE && !SingleElement) { 14341 for (llvm::APSInt &Size : ArraySizes) 14342 PrivateTy = Context.getConstantArrayType(PrivateTy, Size, nullptr, 14343 ArrayType::Normal, 14344 /*IndexTypeQuals=*/0); 14345 } 14346 } 14347 14348 if ((OASE && !ConstantLengthOASE) || 14349 (!OASE && !ASE && 14350 D->getType().getNonReferenceType()->isVariablyModifiedType())) { 14351 if (!Context.getTargetInfo().isVLASupported()) { 14352 if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective())) { 14353 S.Diag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE; 14354 S.Diag(ELoc, diag::note_vla_unsupported); 14355 } else { 14356 S.targetDiag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE; 14357 S.targetDiag(ELoc, diag::note_vla_unsupported); 14358 } 14359 continue; 14360 } 14361 // For arrays/array sections only: 14362 // Create pseudo array type for private copy. The size for this array will 14363 // be generated during codegen. 14364 // For array subscripts or single variables Private Ty is the same as Type 14365 // (type of the variable or single array element). 14366 PrivateTy = Context.getVariableArrayType( 14367 Type, 14368 new (Context) OpaqueValueExpr(ELoc, Context.getSizeType(), VK_RValue), 14369 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange()); 14370 } else if (!ASE && !OASE && 14371 Context.getAsArrayType(D->getType().getNonReferenceType())) { 14372 PrivateTy = D->getType().getNonReferenceType(); 14373 } 14374 // Private copy. 14375 VarDecl *PrivateVD = 14376 buildVarDecl(S, ELoc, PrivateTy, D->getName(), 14377 D->hasAttrs() ? &D->getAttrs() : nullptr, 14378 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 14379 // Add initializer for private variable. 14380 Expr *Init = nullptr; 14381 DeclRefExpr *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc); 14382 DeclRefExpr *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc); 14383 if (DeclareReductionRef.isUsable()) { 14384 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>(); 14385 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl()); 14386 if (DRD->getInitializer()) { 14387 Init = DRDRef; 14388 RHSVD->setInit(DRDRef); 14389 RHSVD->setInitStyle(VarDecl::CallInit); 14390 } 14391 } else { 14392 switch (BOK) { 14393 case BO_Add: 14394 case BO_Xor: 14395 case BO_Or: 14396 case BO_LOr: 14397 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'. 14398 if (Type->isScalarType() || Type->isAnyComplexType()) 14399 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get(); 14400 break; 14401 case BO_Mul: 14402 case BO_LAnd: 14403 if (Type->isScalarType() || Type->isAnyComplexType()) { 14404 // '*' and '&&' reduction ops - initializer is '1'. 14405 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get(); 14406 } 14407 break; 14408 case BO_And: { 14409 // '&' reduction op - initializer is '~0'. 14410 QualType OrigType = Type; 14411 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) 14412 Type = ComplexTy->getElementType(); 14413 if (Type->isRealFloatingType()) { 14414 llvm::APFloat InitValue = 14415 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type), 14416 /*isIEEE=*/true); 14417 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, 14418 Type, ELoc); 14419 } else if (Type->isScalarType()) { 14420 uint64_t Size = Context.getTypeSize(Type); 14421 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0); 14422 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size); 14423 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); 14424 } 14425 if (Init && OrigType->isAnyComplexType()) { 14426 // Init = 0xFFFF + 0xFFFFi; 14427 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType); 14428 Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get(); 14429 } 14430 Type = OrigType; 14431 break; 14432 } 14433 case BO_LT: 14434 case BO_GT: { 14435 // 'min' reduction op - initializer is 'Largest representable number in 14436 // the reduction list item type'. 14437 // 'max' reduction op - initializer is 'Least representable number in 14438 // the reduction list item type'. 14439 if (Type->isIntegerType() || Type->isPointerType()) { 14440 bool IsSigned = Type->hasSignedIntegerRepresentation(); 14441 uint64_t Size = Context.getTypeSize(Type); 14442 QualType IntTy = 14443 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned); 14444 llvm::APInt InitValue = 14445 (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size) 14446 : llvm::APInt::getMinValue(Size) 14447 : IsSigned ? llvm::APInt::getSignedMaxValue(Size) 14448 : llvm::APInt::getMaxValue(Size); 14449 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); 14450 if (Type->isPointerType()) { 14451 // Cast to pointer type. 14452 ExprResult CastExpr = S.BuildCStyleCastExpr( 14453 ELoc, Context.getTrivialTypeSourceInfo(Type, ELoc), ELoc, Init); 14454 if (CastExpr.isInvalid()) 14455 continue; 14456 Init = CastExpr.get(); 14457 } 14458 } else if (Type->isRealFloatingType()) { 14459 llvm::APFloat InitValue = llvm::APFloat::getLargest( 14460 Context.getFloatTypeSemantics(Type), BOK != BO_LT); 14461 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, 14462 Type, ELoc); 14463 } 14464 break; 14465 } 14466 case BO_PtrMemD: 14467 case BO_PtrMemI: 14468 case BO_MulAssign: 14469 case BO_Div: 14470 case BO_Rem: 14471 case BO_Sub: 14472 case BO_Shl: 14473 case BO_Shr: 14474 case BO_LE: 14475 case BO_GE: 14476 case BO_EQ: 14477 case BO_NE: 14478 case BO_Cmp: 14479 case BO_AndAssign: 14480 case BO_XorAssign: 14481 case BO_OrAssign: 14482 case BO_Assign: 14483 case BO_AddAssign: 14484 case BO_SubAssign: 14485 case BO_DivAssign: 14486 case BO_RemAssign: 14487 case BO_ShlAssign: 14488 case BO_ShrAssign: 14489 case BO_Comma: 14490 llvm_unreachable("Unexpected reduction operation"); 14491 } 14492 } 14493 if (Init && DeclareReductionRef.isUnset()) 14494 S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false); 14495 else if (!Init) 14496 S.ActOnUninitializedDecl(RHSVD); 14497 if (RHSVD->isInvalidDecl()) 14498 continue; 14499 if (!RHSVD->hasInit() && 14500 (DeclareReductionRef.isUnset() || !S.LangOpts.CPlusPlus)) { 14501 S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible) 14502 << Type << ReductionIdRange; 14503 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 14504 VarDecl::DeclarationOnly; 14505 S.Diag(D->getLocation(), 14506 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 14507 << D; 14508 continue; 14509 } 14510 // Store initializer for single element in private copy. Will be used during 14511 // codegen. 14512 PrivateVD->setInit(RHSVD->getInit()); 14513 PrivateVD->setInitStyle(RHSVD->getInitStyle()); 14514 DeclRefExpr *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc); 14515 ExprResult ReductionOp; 14516 if (DeclareReductionRef.isUsable()) { 14517 QualType RedTy = DeclareReductionRef.get()->getType(); 14518 QualType PtrRedTy = Context.getPointerType(RedTy); 14519 ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE); 14520 ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE); 14521 if (!BasePath.empty()) { 14522 LHS = S.DefaultLvalueConversion(LHS.get()); 14523 RHS = S.DefaultLvalueConversion(RHS.get()); 14524 LHS = ImplicitCastExpr::Create(Context, PtrRedTy, 14525 CK_UncheckedDerivedToBase, LHS.get(), 14526 &BasePath, LHS.get()->getValueKind()); 14527 RHS = ImplicitCastExpr::Create(Context, PtrRedTy, 14528 CK_UncheckedDerivedToBase, RHS.get(), 14529 &BasePath, RHS.get()->getValueKind()); 14530 } 14531 FunctionProtoType::ExtProtoInfo EPI; 14532 QualType Params[] = {PtrRedTy, PtrRedTy}; 14533 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI); 14534 auto *OVE = new (Context) OpaqueValueExpr( 14535 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary, 14536 S.DefaultLvalueConversion(DeclareReductionRef.get()).get()); 14537 Expr *Args[] = {LHS.get(), RHS.get()}; 14538 ReductionOp = 14539 CallExpr::Create(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc); 14540 } else { 14541 ReductionOp = S.BuildBinOp( 14542 Stack->getCurScope(), ReductionId.getBeginLoc(), BOK, LHSDRE, RHSDRE); 14543 if (ReductionOp.isUsable()) { 14544 if (BOK != BO_LT && BOK != BO_GT) { 14545 ReductionOp = 14546 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(), 14547 BO_Assign, LHSDRE, ReductionOp.get()); 14548 } else { 14549 auto *ConditionalOp = new (Context) 14550 ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc, RHSDRE, 14551 Type, VK_LValue, OK_Ordinary); 14552 ReductionOp = 14553 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(), 14554 BO_Assign, LHSDRE, ConditionalOp); 14555 } 14556 if (ReductionOp.isUsable()) 14557 ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get(), 14558 /*DiscardedValue*/ false); 14559 } 14560 if (!ReductionOp.isUsable()) 14561 continue; 14562 } 14563 14564 // OpenMP [2.15.4.6, Restrictions, p.2] 14565 // A list item that appears in an in_reduction clause of a task construct 14566 // must appear in a task_reduction clause of a construct associated with a 14567 // taskgroup region that includes the participating task in its taskgroup 14568 // set. The construct associated with the innermost region that meets this 14569 // condition must specify the same reduction-identifier as the in_reduction 14570 // clause. 14571 if (ClauseKind == OMPC_in_reduction) { 14572 SourceRange ParentSR; 14573 BinaryOperatorKind ParentBOK; 14574 const Expr *ParentReductionOp; 14575 Expr *ParentBOKTD, *ParentReductionOpTD; 14576 DSAStackTy::DSAVarData ParentBOKDSA = 14577 Stack->getTopMostTaskgroupReductionData(D, ParentSR, ParentBOK, 14578 ParentBOKTD); 14579 DSAStackTy::DSAVarData ParentReductionOpDSA = 14580 Stack->getTopMostTaskgroupReductionData( 14581 D, ParentSR, ParentReductionOp, ParentReductionOpTD); 14582 bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown; 14583 bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown; 14584 if (!IsParentBOK && !IsParentReductionOp) { 14585 S.Diag(ELoc, diag::err_omp_in_reduction_not_task_reduction); 14586 continue; 14587 } 14588 if ((DeclareReductionRef.isUnset() && IsParentReductionOp) || 14589 (DeclareReductionRef.isUsable() && IsParentBOK) || BOK != ParentBOK || 14590 IsParentReductionOp) { 14591 bool EmitError = true; 14592 if (IsParentReductionOp && DeclareReductionRef.isUsable()) { 14593 llvm::FoldingSetNodeID RedId, ParentRedId; 14594 ParentReductionOp->Profile(ParentRedId, Context, /*Canonical=*/true); 14595 DeclareReductionRef.get()->Profile(RedId, Context, 14596 /*Canonical=*/true); 14597 EmitError = RedId != ParentRedId; 14598 } 14599 if (EmitError) { 14600 S.Diag(ReductionId.getBeginLoc(), 14601 diag::err_omp_reduction_identifier_mismatch) 14602 << ReductionIdRange << RefExpr->getSourceRange(); 14603 S.Diag(ParentSR.getBegin(), 14604 diag::note_omp_previous_reduction_identifier) 14605 << ParentSR 14606 << (IsParentBOK ? ParentBOKDSA.RefExpr 14607 : ParentReductionOpDSA.RefExpr) 14608 ->getSourceRange(); 14609 continue; 14610 } 14611 } 14612 TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD; 14613 assert(TaskgroupDescriptor && "Taskgroup descriptor must be defined."); 14614 } 14615 14616 DeclRefExpr *Ref = nullptr; 14617 Expr *VarsExpr = RefExpr->IgnoreParens(); 14618 if (!VD && !S.CurContext->isDependentContext()) { 14619 if (ASE || OASE) { 14620 TransformExprToCaptures RebuildToCapture(S, D); 14621 VarsExpr = 14622 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get(); 14623 Ref = RebuildToCapture.getCapturedExpr(); 14624 } else { 14625 VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false); 14626 } 14627 if (!S.isOpenMPCapturedDecl(D)) { 14628 RD.ExprCaptures.emplace_back(Ref->getDecl()); 14629 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) { 14630 ExprResult RefRes = S.DefaultLvalueConversion(Ref); 14631 if (!RefRes.isUsable()) 14632 continue; 14633 ExprResult PostUpdateRes = 14634 S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr, 14635 RefRes.get()); 14636 if (!PostUpdateRes.isUsable()) 14637 continue; 14638 if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) || 14639 Stack->getCurrentDirective() == OMPD_taskgroup) { 14640 S.Diag(RefExpr->getExprLoc(), 14641 diag::err_omp_reduction_non_addressable_expression) 14642 << RefExpr->getSourceRange(); 14643 continue; 14644 } 14645 RD.ExprPostUpdates.emplace_back( 14646 S.IgnoredValueConversions(PostUpdateRes.get()).get()); 14647 } 14648 } 14649 } 14650 // All reduction items are still marked as reduction (to do not increase 14651 // code base size). 14652 Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref); 14653 if (CurrDir == OMPD_taskgroup) { 14654 if (DeclareReductionRef.isUsable()) 14655 Stack->addTaskgroupReductionData(D, ReductionIdRange, 14656 DeclareReductionRef.get()); 14657 else 14658 Stack->addTaskgroupReductionData(D, ReductionIdRange, BOK); 14659 } 14660 RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get(), 14661 TaskgroupDescriptor); 14662 } 14663 return RD.Vars.empty(); 14664 } 14665 14666 OMPClause *Sema::ActOnOpenMPReductionClause( 14667 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 14668 SourceLocation ColonLoc, SourceLocation EndLoc, 14669 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 14670 ArrayRef<Expr *> UnresolvedReductions) { 14671 ReductionData RD(VarList.size()); 14672 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList, 14673 StartLoc, LParenLoc, ColonLoc, EndLoc, 14674 ReductionIdScopeSpec, ReductionId, 14675 UnresolvedReductions, RD)) 14676 return nullptr; 14677 14678 return OMPReductionClause::Create( 14679 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 14680 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 14681 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, 14682 buildPreInits(Context, RD.ExprCaptures), 14683 buildPostUpdate(*this, RD.ExprPostUpdates)); 14684 } 14685 14686 OMPClause *Sema::ActOnOpenMPTaskReductionClause( 14687 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 14688 SourceLocation ColonLoc, SourceLocation EndLoc, 14689 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 14690 ArrayRef<Expr *> UnresolvedReductions) { 14691 ReductionData RD(VarList.size()); 14692 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction, VarList, 14693 StartLoc, LParenLoc, ColonLoc, EndLoc, 14694 ReductionIdScopeSpec, ReductionId, 14695 UnresolvedReductions, RD)) 14696 return nullptr; 14697 14698 return OMPTaskReductionClause::Create( 14699 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 14700 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 14701 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, 14702 buildPreInits(Context, RD.ExprCaptures), 14703 buildPostUpdate(*this, RD.ExprPostUpdates)); 14704 } 14705 14706 OMPClause *Sema::ActOnOpenMPInReductionClause( 14707 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 14708 SourceLocation ColonLoc, SourceLocation EndLoc, 14709 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 14710 ArrayRef<Expr *> UnresolvedReductions) { 14711 ReductionData RD(VarList.size()); 14712 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_in_reduction, VarList, 14713 StartLoc, LParenLoc, ColonLoc, EndLoc, 14714 ReductionIdScopeSpec, ReductionId, 14715 UnresolvedReductions, RD)) 14716 return nullptr; 14717 14718 return OMPInReductionClause::Create( 14719 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 14720 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 14721 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.TaskgroupDescriptors, 14722 buildPreInits(Context, RD.ExprCaptures), 14723 buildPostUpdate(*this, RD.ExprPostUpdates)); 14724 } 14725 14726 bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind, 14727 SourceLocation LinLoc) { 14728 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) || 14729 LinKind == OMPC_LINEAR_unknown) { 14730 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus; 14731 return true; 14732 } 14733 return false; 14734 } 14735 14736 bool Sema::CheckOpenMPLinearDecl(const ValueDecl *D, SourceLocation ELoc, 14737 OpenMPLinearClauseKind LinKind, QualType Type, 14738 bool IsDeclareSimd) { 14739 const auto *VD = dyn_cast_or_null<VarDecl>(D); 14740 // A variable must not have an incomplete type or a reference type. 14741 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type)) 14742 return true; 14743 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) && 14744 !Type->isReferenceType()) { 14745 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference) 14746 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind); 14747 return true; 14748 } 14749 Type = Type.getNonReferenceType(); 14750 14751 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions] 14752 // A variable that is privatized must not have a const-qualified type 14753 // unless it is of class type with a mutable member. This restriction does 14754 // not apply to the firstprivate clause, nor to the linear clause on 14755 // declarative directives (like declare simd). 14756 if (!IsDeclareSimd && 14757 rejectConstNotMutableType(*this, D, Type, OMPC_linear, ELoc)) 14758 return true; 14759 14760 // A list item must be of integral or pointer type. 14761 Type = Type.getUnqualifiedType().getCanonicalType(); 14762 const auto *Ty = Type.getTypePtrOrNull(); 14763 if (!Ty || (LinKind != OMPC_LINEAR_ref && !Ty->isDependentType() && 14764 !Ty->isIntegralType(Context) && !Ty->isPointerType())) { 14765 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type; 14766 if (D) { 14767 bool IsDecl = 14768 !VD || 14769 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 14770 Diag(D->getLocation(), 14771 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 14772 << D; 14773 } 14774 return true; 14775 } 14776 return false; 14777 } 14778 14779 OMPClause *Sema::ActOnOpenMPLinearClause( 14780 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc, 14781 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind, 14782 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 14783 SmallVector<Expr *, 8> Vars; 14784 SmallVector<Expr *, 8> Privates; 14785 SmallVector<Expr *, 8> Inits; 14786 SmallVector<Decl *, 4> ExprCaptures; 14787 SmallVector<Expr *, 4> ExprPostUpdates; 14788 if (CheckOpenMPLinearModifier(LinKind, LinLoc)) 14789 LinKind = OMPC_LINEAR_val; 14790 for (Expr *RefExpr : VarList) { 14791 assert(RefExpr && "NULL expr in OpenMP linear clause."); 14792 SourceLocation ELoc; 14793 SourceRange ERange; 14794 Expr *SimpleRefExpr = RefExpr; 14795 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 14796 if (Res.second) { 14797 // It will be analyzed later. 14798 Vars.push_back(RefExpr); 14799 Privates.push_back(nullptr); 14800 Inits.push_back(nullptr); 14801 } 14802 ValueDecl *D = Res.first; 14803 if (!D) 14804 continue; 14805 14806 QualType Type = D->getType(); 14807 auto *VD = dyn_cast<VarDecl>(D); 14808 14809 // OpenMP [2.14.3.7, linear clause] 14810 // A list-item cannot appear in more than one linear clause. 14811 // A list-item that appears in a linear clause cannot appear in any 14812 // other data-sharing attribute clause. 14813 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 14814 if (DVar.RefExpr) { 14815 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 14816 << getOpenMPClauseName(OMPC_linear); 14817 reportOriginalDsa(*this, DSAStack, D, DVar); 14818 continue; 14819 } 14820 14821 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type)) 14822 continue; 14823 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType(); 14824 14825 // Build private copy of original var. 14826 VarDecl *Private = 14827 buildVarDecl(*this, ELoc, Type, D->getName(), 14828 D->hasAttrs() ? &D->getAttrs() : nullptr, 14829 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 14830 DeclRefExpr *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc); 14831 // Build var to save initial value. 14832 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start"); 14833 Expr *InitExpr; 14834 DeclRefExpr *Ref = nullptr; 14835 if (!VD && !CurContext->isDependentContext()) { 14836 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 14837 if (!isOpenMPCapturedDecl(D)) { 14838 ExprCaptures.push_back(Ref->getDecl()); 14839 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) { 14840 ExprResult RefRes = DefaultLvalueConversion(Ref); 14841 if (!RefRes.isUsable()) 14842 continue; 14843 ExprResult PostUpdateRes = 14844 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, 14845 SimpleRefExpr, RefRes.get()); 14846 if (!PostUpdateRes.isUsable()) 14847 continue; 14848 ExprPostUpdates.push_back( 14849 IgnoredValueConversions(PostUpdateRes.get()).get()); 14850 } 14851 } 14852 } 14853 if (LinKind == OMPC_LINEAR_uval) 14854 InitExpr = VD ? VD->getInit() : SimpleRefExpr; 14855 else 14856 InitExpr = VD ? SimpleRefExpr : Ref; 14857 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(), 14858 /*DirectInit=*/false); 14859 DeclRefExpr *InitRef = buildDeclRefExpr(*this, Init, Type, ELoc); 14860 14861 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref); 14862 Vars.push_back((VD || CurContext->isDependentContext()) 14863 ? RefExpr->IgnoreParens() 14864 : Ref); 14865 Privates.push_back(PrivateRef); 14866 Inits.push_back(InitRef); 14867 } 14868 14869 if (Vars.empty()) 14870 return nullptr; 14871 14872 Expr *StepExpr = Step; 14873 Expr *CalcStepExpr = nullptr; 14874 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && 14875 !Step->isInstantiationDependent() && 14876 !Step->containsUnexpandedParameterPack()) { 14877 SourceLocation StepLoc = Step->getBeginLoc(); 14878 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step); 14879 if (Val.isInvalid()) 14880 return nullptr; 14881 StepExpr = Val.get(); 14882 14883 // Build var to save the step value. 14884 VarDecl *SaveVar = 14885 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step"); 14886 ExprResult SaveRef = 14887 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc); 14888 ExprResult CalcStep = 14889 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr); 14890 CalcStep = ActOnFinishFullExpr(CalcStep.get(), /*DiscardedValue*/ false); 14891 14892 // Warn about zero linear step (it would be probably better specified as 14893 // making corresponding variables 'const'). 14894 llvm::APSInt Result; 14895 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context); 14896 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive()) 14897 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0] 14898 << (Vars.size() > 1); 14899 if (!IsConstant && CalcStep.isUsable()) { 14900 // Calculate the step beforehand instead of doing this on each iteration. 14901 // (This is not used if the number of iterations may be kfold-ed). 14902 CalcStepExpr = CalcStep.get(); 14903 } 14904 } 14905 14906 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc, 14907 ColonLoc, EndLoc, Vars, Privates, Inits, 14908 StepExpr, CalcStepExpr, 14909 buildPreInits(Context, ExprCaptures), 14910 buildPostUpdate(*this, ExprPostUpdates)); 14911 } 14912 14913 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, 14914 Expr *NumIterations, Sema &SemaRef, 14915 Scope *S, DSAStackTy *Stack) { 14916 // Walk the vars and build update/final expressions for the CodeGen. 14917 SmallVector<Expr *, 8> Updates; 14918 SmallVector<Expr *, 8> Finals; 14919 SmallVector<Expr *, 8> UsedExprs; 14920 Expr *Step = Clause.getStep(); 14921 Expr *CalcStep = Clause.getCalcStep(); 14922 // OpenMP [2.14.3.7, linear clause] 14923 // If linear-step is not specified it is assumed to be 1. 14924 if (!Step) 14925 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(); 14926 else if (CalcStep) 14927 Step = cast<BinaryOperator>(CalcStep)->getLHS(); 14928 bool HasErrors = false; 14929 auto CurInit = Clause.inits().begin(); 14930 auto CurPrivate = Clause.privates().begin(); 14931 OpenMPLinearClauseKind LinKind = Clause.getModifier(); 14932 for (Expr *RefExpr : Clause.varlists()) { 14933 SourceLocation ELoc; 14934 SourceRange ERange; 14935 Expr *SimpleRefExpr = RefExpr; 14936 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange); 14937 ValueDecl *D = Res.first; 14938 if (Res.second || !D) { 14939 Updates.push_back(nullptr); 14940 Finals.push_back(nullptr); 14941 HasErrors = true; 14942 continue; 14943 } 14944 auto &&Info = Stack->isLoopControlVariable(D); 14945 // OpenMP [2.15.11, distribute simd Construct] 14946 // A list item may not appear in a linear clause, unless it is the loop 14947 // iteration variable. 14948 if (isOpenMPDistributeDirective(Stack->getCurrentDirective()) && 14949 isOpenMPSimdDirective(Stack->getCurrentDirective()) && !Info.first) { 14950 SemaRef.Diag(ELoc, 14951 diag::err_omp_linear_distribute_var_non_loop_iteration); 14952 Updates.push_back(nullptr); 14953 Finals.push_back(nullptr); 14954 HasErrors = true; 14955 continue; 14956 } 14957 Expr *InitExpr = *CurInit; 14958 14959 // Build privatized reference to the current linear var. 14960 auto *DE = cast<DeclRefExpr>(SimpleRefExpr); 14961 Expr *CapturedRef; 14962 if (LinKind == OMPC_LINEAR_uval) 14963 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit(); 14964 else 14965 CapturedRef = 14966 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()), 14967 DE->getType().getUnqualifiedType(), DE->getExprLoc(), 14968 /*RefersToCapture=*/true); 14969 14970 // Build update: Var = InitExpr + IV * Step 14971 ExprResult Update; 14972 if (!Info.first) 14973 Update = buildCounterUpdate( 14974 SemaRef, S, RefExpr->getExprLoc(), *CurPrivate, InitExpr, IV, Step, 14975 /*Subtract=*/false, /*IsNonRectangularLB=*/false); 14976 else 14977 Update = *CurPrivate; 14978 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getBeginLoc(), 14979 /*DiscardedValue*/ false); 14980 14981 // Build final: Var = InitExpr + NumIterations * Step 14982 ExprResult Final; 14983 if (!Info.first) 14984 Final = 14985 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef, 14986 InitExpr, NumIterations, Step, /*Subtract=*/false, 14987 /*IsNonRectangularLB=*/false); 14988 else 14989 Final = *CurPrivate; 14990 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getBeginLoc(), 14991 /*DiscardedValue*/ false); 14992 14993 if (!Update.isUsable() || !Final.isUsable()) { 14994 Updates.push_back(nullptr); 14995 Finals.push_back(nullptr); 14996 UsedExprs.push_back(nullptr); 14997 HasErrors = true; 14998 } else { 14999 Updates.push_back(Update.get()); 15000 Finals.push_back(Final.get()); 15001 if (!Info.first) 15002 UsedExprs.push_back(SimpleRefExpr); 15003 } 15004 ++CurInit; 15005 ++CurPrivate; 15006 } 15007 if (Expr *S = Clause.getStep()) 15008 UsedExprs.push_back(S); 15009 // Fill the remaining part with the nullptr. 15010 UsedExprs.append(Clause.varlist_size() + 1 - UsedExprs.size(), nullptr); 15011 Clause.setUpdates(Updates); 15012 Clause.setFinals(Finals); 15013 Clause.setUsedExprs(UsedExprs); 15014 return HasErrors; 15015 } 15016 15017 OMPClause *Sema::ActOnOpenMPAlignedClause( 15018 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc, 15019 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 15020 SmallVector<Expr *, 8> Vars; 15021 for (Expr *RefExpr : VarList) { 15022 assert(RefExpr && "NULL expr in OpenMP linear clause."); 15023 SourceLocation ELoc; 15024 SourceRange ERange; 15025 Expr *SimpleRefExpr = RefExpr; 15026 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 15027 if (Res.second) { 15028 // It will be analyzed later. 15029 Vars.push_back(RefExpr); 15030 } 15031 ValueDecl *D = Res.first; 15032 if (!D) 15033 continue; 15034 15035 QualType QType = D->getType(); 15036 auto *VD = dyn_cast<VarDecl>(D); 15037 15038 // OpenMP [2.8.1, simd construct, Restrictions] 15039 // The type of list items appearing in the aligned clause must be 15040 // array, pointer, reference to array, or reference to pointer. 15041 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType(); 15042 const Type *Ty = QType.getTypePtrOrNull(); 15043 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) { 15044 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr) 15045 << QType << getLangOpts().CPlusPlus << ERange; 15046 bool IsDecl = 15047 !VD || 15048 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 15049 Diag(D->getLocation(), 15050 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 15051 << D; 15052 continue; 15053 } 15054 15055 // OpenMP [2.8.1, simd construct, Restrictions] 15056 // A list-item cannot appear in more than one aligned clause. 15057 if (const Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) { 15058 Diag(ELoc, diag::err_omp_used_in_clause_twice) 15059 << 0 << getOpenMPClauseName(OMPC_aligned) << ERange; 15060 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa) 15061 << getOpenMPClauseName(OMPC_aligned); 15062 continue; 15063 } 15064 15065 DeclRefExpr *Ref = nullptr; 15066 if (!VD && isOpenMPCapturedDecl(D)) 15067 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 15068 Vars.push_back(DefaultFunctionArrayConversion( 15069 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref) 15070 .get()); 15071 } 15072 15073 // OpenMP [2.8.1, simd construct, Description] 15074 // The parameter of the aligned clause, alignment, must be a constant 15075 // positive integer expression. 15076 // If no optional parameter is specified, implementation-defined default 15077 // alignments for SIMD instructions on the target platforms are assumed. 15078 if (Alignment != nullptr) { 15079 ExprResult AlignResult = 15080 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned); 15081 if (AlignResult.isInvalid()) 15082 return nullptr; 15083 Alignment = AlignResult.get(); 15084 } 15085 if (Vars.empty()) 15086 return nullptr; 15087 15088 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc, 15089 EndLoc, Vars, Alignment); 15090 } 15091 15092 OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList, 15093 SourceLocation StartLoc, 15094 SourceLocation LParenLoc, 15095 SourceLocation EndLoc) { 15096 SmallVector<Expr *, 8> Vars; 15097 SmallVector<Expr *, 8> SrcExprs; 15098 SmallVector<Expr *, 8> DstExprs; 15099 SmallVector<Expr *, 8> AssignmentOps; 15100 for (Expr *RefExpr : VarList) { 15101 assert(RefExpr && "NULL expr in OpenMP copyin clause."); 15102 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 15103 // It will be analyzed later. 15104 Vars.push_back(RefExpr); 15105 SrcExprs.push_back(nullptr); 15106 DstExprs.push_back(nullptr); 15107 AssignmentOps.push_back(nullptr); 15108 continue; 15109 } 15110 15111 SourceLocation ELoc = RefExpr->getExprLoc(); 15112 // OpenMP [2.1, C/C++] 15113 // A list item is a variable name. 15114 // OpenMP [2.14.4.1, Restrictions, p.1] 15115 // A list item that appears in a copyin clause must be threadprivate. 15116 auto *DE = dyn_cast<DeclRefExpr>(RefExpr); 15117 if (!DE || !isa<VarDecl>(DE->getDecl())) { 15118 Diag(ELoc, diag::err_omp_expected_var_name_member_expr) 15119 << 0 << RefExpr->getSourceRange(); 15120 continue; 15121 } 15122 15123 Decl *D = DE->getDecl(); 15124 auto *VD = cast<VarDecl>(D); 15125 15126 QualType Type = VD->getType(); 15127 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 15128 // It will be analyzed later. 15129 Vars.push_back(DE); 15130 SrcExprs.push_back(nullptr); 15131 DstExprs.push_back(nullptr); 15132 AssignmentOps.push_back(nullptr); 15133 continue; 15134 } 15135 15136 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1] 15137 // A list item that appears in a copyin clause must be threadprivate. 15138 if (!DSAStack->isThreadPrivate(VD)) { 15139 Diag(ELoc, diag::err_omp_required_access) 15140 << getOpenMPClauseName(OMPC_copyin) 15141 << getOpenMPDirectiveName(OMPD_threadprivate); 15142 continue; 15143 } 15144 15145 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 15146 // A variable of class type (or array thereof) that appears in a 15147 // copyin clause requires an accessible, unambiguous copy assignment 15148 // operator for the class type. 15149 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType(); 15150 VarDecl *SrcVD = 15151 buildVarDecl(*this, DE->getBeginLoc(), ElemType.getUnqualifiedType(), 15152 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr); 15153 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr( 15154 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc()); 15155 VarDecl *DstVD = 15156 buildVarDecl(*this, DE->getBeginLoc(), ElemType, ".copyin.dst", 15157 VD->hasAttrs() ? &VD->getAttrs() : nullptr); 15158 DeclRefExpr *PseudoDstExpr = 15159 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc()); 15160 // For arrays generate assignment operation for single element and replace 15161 // it by the original array element in CodeGen. 15162 ExprResult AssignmentOp = 15163 BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, PseudoDstExpr, 15164 PseudoSrcExpr); 15165 if (AssignmentOp.isInvalid()) 15166 continue; 15167 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(), 15168 /*DiscardedValue*/ false); 15169 if (AssignmentOp.isInvalid()) 15170 continue; 15171 15172 DSAStack->addDSA(VD, DE, OMPC_copyin); 15173 Vars.push_back(DE); 15174 SrcExprs.push_back(PseudoSrcExpr); 15175 DstExprs.push_back(PseudoDstExpr); 15176 AssignmentOps.push_back(AssignmentOp.get()); 15177 } 15178 15179 if (Vars.empty()) 15180 return nullptr; 15181 15182 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 15183 SrcExprs, DstExprs, AssignmentOps); 15184 } 15185 15186 OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList, 15187 SourceLocation StartLoc, 15188 SourceLocation LParenLoc, 15189 SourceLocation EndLoc) { 15190 SmallVector<Expr *, 8> Vars; 15191 SmallVector<Expr *, 8> SrcExprs; 15192 SmallVector<Expr *, 8> DstExprs; 15193 SmallVector<Expr *, 8> AssignmentOps; 15194 for (Expr *RefExpr : VarList) { 15195 assert(RefExpr && "NULL expr in OpenMP linear clause."); 15196 SourceLocation ELoc; 15197 SourceRange ERange; 15198 Expr *SimpleRefExpr = RefExpr; 15199 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 15200 if (Res.second) { 15201 // It will be analyzed later. 15202 Vars.push_back(RefExpr); 15203 SrcExprs.push_back(nullptr); 15204 DstExprs.push_back(nullptr); 15205 AssignmentOps.push_back(nullptr); 15206 } 15207 ValueDecl *D = Res.first; 15208 if (!D) 15209 continue; 15210 15211 QualType Type = D->getType(); 15212 auto *VD = dyn_cast<VarDecl>(D); 15213 15214 // OpenMP [2.14.4.2, Restrictions, p.2] 15215 // A list item that appears in a copyprivate clause may not appear in a 15216 // private or firstprivate clause on the single construct. 15217 if (!VD || !DSAStack->isThreadPrivate(VD)) { 15218 DSAStackTy::DSAVarData DVar = 15219 DSAStack->getTopDSA(D, /*FromParent=*/false); 15220 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate && 15221 DVar.RefExpr) { 15222 Diag(ELoc, diag::err_omp_wrong_dsa) 15223 << getOpenMPClauseName(DVar.CKind) 15224 << getOpenMPClauseName(OMPC_copyprivate); 15225 reportOriginalDsa(*this, DSAStack, D, DVar); 15226 continue; 15227 } 15228 15229 // OpenMP [2.11.4.2, Restrictions, p.1] 15230 // All list items that appear in a copyprivate clause must be either 15231 // threadprivate or private in the enclosing context. 15232 if (DVar.CKind == OMPC_unknown) { 15233 DVar = DSAStack->getImplicitDSA(D, false); 15234 if (DVar.CKind == OMPC_shared) { 15235 Diag(ELoc, diag::err_omp_required_access) 15236 << getOpenMPClauseName(OMPC_copyprivate) 15237 << "threadprivate or private in the enclosing context"; 15238 reportOriginalDsa(*this, DSAStack, D, DVar); 15239 continue; 15240 } 15241 } 15242 } 15243 15244 // Variably modified types are not supported. 15245 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) { 15246 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 15247 << getOpenMPClauseName(OMPC_copyprivate) << Type 15248 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 15249 bool IsDecl = 15250 !VD || 15251 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 15252 Diag(D->getLocation(), 15253 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 15254 << D; 15255 continue; 15256 } 15257 15258 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 15259 // A variable of class type (or array thereof) that appears in a 15260 // copyin clause requires an accessible, unambiguous copy assignment 15261 // operator for the class type. 15262 Type = Context.getBaseElementType(Type.getNonReferenceType()) 15263 .getUnqualifiedType(); 15264 VarDecl *SrcVD = 15265 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.src", 15266 D->hasAttrs() ? &D->getAttrs() : nullptr); 15267 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc); 15268 VarDecl *DstVD = 15269 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.dst", 15270 D->hasAttrs() ? &D->getAttrs() : nullptr); 15271 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc); 15272 ExprResult AssignmentOp = BuildBinOp( 15273 DSAStack->getCurScope(), ELoc, BO_Assign, PseudoDstExpr, PseudoSrcExpr); 15274 if (AssignmentOp.isInvalid()) 15275 continue; 15276 AssignmentOp = 15277 ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false); 15278 if (AssignmentOp.isInvalid()) 15279 continue; 15280 15281 // No need to mark vars as copyprivate, they are already threadprivate or 15282 // implicitly private. 15283 assert(VD || isOpenMPCapturedDecl(D)); 15284 Vars.push_back( 15285 VD ? RefExpr->IgnoreParens() 15286 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false)); 15287 SrcExprs.push_back(PseudoSrcExpr); 15288 DstExprs.push_back(PseudoDstExpr); 15289 AssignmentOps.push_back(AssignmentOp.get()); 15290 } 15291 15292 if (Vars.empty()) 15293 return nullptr; 15294 15295 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 15296 Vars, SrcExprs, DstExprs, AssignmentOps); 15297 } 15298 15299 OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList, 15300 SourceLocation StartLoc, 15301 SourceLocation LParenLoc, 15302 SourceLocation EndLoc) { 15303 if (VarList.empty()) 15304 return nullptr; 15305 15306 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList); 15307 } 15308 15309 /// Tries to find omp_depend_t. type. 15310 static bool findOMPDependT(Sema &S, SourceLocation Loc, DSAStackTy *Stack, 15311 bool Diagnose = true) { 15312 QualType OMPDependT = Stack->getOMPDependT(); 15313 if (!OMPDependT.isNull()) 15314 return true; 15315 IdentifierInfo *II = &S.PP.getIdentifierTable().get("omp_depend_t"); 15316 ParsedType PT = S.getTypeName(*II, Loc, S.getCurScope()); 15317 if (!PT.getAsOpaquePtr() || PT.get().isNull()) { 15318 if (Diagnose) 15319 S.Diag(Loc, diag::err_omp_implied_type_not_found) << "omp_depend_t"; 15320 return false; 15321 } 15322 Stack->setOMPDependT(PT.get()); 15323 return true; 15324 } 15325 15326 OMPClause *Sema::ActOnOpenMPDepobjClause(Expr *Depobj, SourceLocation StartLoc, 15327 SourceLocation LParenLoc, 15328 SourceLocation EndLoc) { 15329 if (!Depobj) 15330 return nullptr; 15331 15332 bool OMPDependTFound = findOMPDependT(*this, StartLoc, DSAStack); 15333 15334 // OpenMP 5.0, 2.17.10.1 depobj Construct 15335 // depobj is an lvalue expression of type omp_depend_t. 15336 if (!Depobj->isTypeDependent() && !Depobj->isValueDependent() && 15337 !Depobj->isInstantiationDependent() && 15338 !Depobj->containsUnexpandedParameterPack() && 15339 (OMPDependTFound && 15340 !Context.typesAreCompatible(DSAStack->getOMPDependT(), Depobj->getType(), 15341 /*CompareUnqualified=*/true))) { 15342 Diag(Depobj->getExprLoc(), diag::err_omp_expected_omp_depend_t_lvalue) 15343 << 0 << Depobj->getType() << Depobj->getSourceRange(); 15344 } 15345 15346 if (!Depobj->isLValue()) { 15347 Diag(Depobj->getExprLoc(), diag::err_omp_expected_omp_depend_t_lvalue) 15348 << 1 << Depobj->getSourceRange(); 15349 } 15350 15351 return OMPDepobjClause::Create(Context, StartLoc, LParenLoc, EndLoc, Depobj); 15352 } 15353 15354 OMPClause * 15355 Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind, 15356 SourceLocation DepLoc, SourceLocation ColonLoc, 15357 ArrayRef<Expr *> VarList, SourceLocation StartLoc, 15358 SourceLocation LParenLoc, SourceLocation EndLoc) { 15359 if (DSAStack->getCurrentDirective() == OMPD_ordered && 15360 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) { 15361 Diag(DepLoc, diag::err_omp_unexpected_clause_value) 15362 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend); 15363 return nullptr; 15364 } 15365 if ((DSAStack->getCurrentDirective() != OMPD_ordered || 15366 DSAStack->getCurrentDirective() == OMPD_depobj) && 15367 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source || 15368 DepKind == OMPC_DEPEND_sink || 15369 ((LangOpts.OpenMP < 50 || 15370 DSAStack->getCurrentDirective() == OMPD_depobj) && 15371 DepKind == OMPC_DEPEND_depobj))) { 15372 SmallVector<unsigned, 3> Except; 15373 Except.push_back(OMPC_DEPEND_source); 15374 Except.push_back(OMPC_DEPEND_sink); 15375 if (LangOpts.OpenMP < 50 || DSAStack->getCurrentDirective() == OMPD_depobj) 15376 Except.push_back(OMPC_DEPEND_depobj); 15377 Diag(DepLoc, diag::err_omp_unexpected_clause_value) 15378 << getListOfPossibleValues(OMPC_depend, /*First=*/0, 15379 /*Last=*/OMPC_DEPEND_unknown, Except) 15380 << getOpenMPClauseName(OMPC_depend); 15381 return nullptr; 15382 } 15383 SmallVector<Expr *, 8> Vars; 15384 DSAStackTy::OperatorOffsetTy OpsOffs; 15385 llvm::APSInt DepCounter(/*BitWidth=*/32); 15386 llvm::APSInt TotalDepCount(/*BitWidth=*/32); 15387 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) { 15388 if (const Expr *OrderedCountExpr = 15389 DSAStack->getParentOrderedRegionParam().first) { 15390 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context); 15391 TotalDepCount.setIsUnsigned(/*Val=*/true); 15392 } 15393 } 15394 for (Expr *RefExpr : VarList) { 15395 assert(RefExpr && "NULL expr in OpenMP shared clause."); 15396 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 15397 // It will be analyzed later. 15398 Vars.push_back(RefExpr); 15399 continue; 15400 } 15401 15402 SourceLocation ELoc = RefExpr->getExprLoc(); 15403 Expr *SimpleExpr = RefExpr->IgnoreParenCasts(); 15404 if (DepKind == OMPC_DEPEND_sink) { 15405 if (DSAStack->getParentOrderedRegionParam().first && 15406 DepCounter >= TotalDepCount) { 15407 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr); 15408 continue; 15409 } 15410 ++DepCounter; 15411 // OpenMP [2.13.9, Summary] 15412 // depend(dependence-type : vec), where dependence-type is: 15413 // 'sink' and where vec is the iteration vector, which has the form: 15414 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn] 15415 // where n is the value specified by the ordered clause in the loop 15416 // directive, xi denotes the loop iteration variable of the i-th nested 15417 // loop associated with the loop directive, and di is a constant 15418 // non-negative integer. 15419 if (CurContext->isDependentContext()) { 15420 // It will be analyzed later. 15421 Vars.push_back(RefExpr); 15422 continue; 15423 } 15424 SimpleExpr = SimpleExpr->IgnoreImplicit(); 15425 OverloadedOperatorKind OOK = OO_None; 15426 SourceLocation OOLoc; 15427 Expr *LHS = SimpleExpr; 15428 Expr *RHS = nullptr; 15429 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) { 15430 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode()); 15431 OOLoc = BO->getOperatorLoc(); 15432 LHS = BO->getLHS()->IgnoreParenImpCasts(); 15433 RHS = BO->getRHS()->IgnoreParenImpCasts(); 15434 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) { 15435 OOK = OCE->getOperator(); 15436 OOLoc = OCE->getOperatorLoc(); 15437 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); 15438 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts(); 15439 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) { 15440 OOK = MCE->getMethodDecl() 15441 ->getNameInfo() 15442 .getName() 15443 .getCXXOverloadedOperator(); 15444 OOLoc = MCE->getCallee()->getExprLoc(); 15445 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts(); 15446 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); 15447 } 15448 SourceLocation ELoc; 15449 SourceRange ERange; 15450 auto Res = getPrivateItem(*this, LHS, ELoc, ERange); 15451 if (Res.second) { 15452 // It will be analyzed later. 15453 Vars.push_back(RefExpr); 15454 } 15455 ValueDecl *D = Res.first; 15456 if (!D) 15457 continue; 15458 15459 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) { 15460 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus); 15461 continue; 15462 } 15463 if (RHS) { 15464 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause( 15465 RHS, OMPC_depend, /*StrictlyPositive=*/false); 15466 if (RHSRes.isInvalid()) 15467 continue; 15468 } 15469 if (!CurContext->isDependentContext() && 15470 DSAStack->getParentOrderedRegionParam().first && 15471 DepCounter != DSAStack->isParentLoopControlVariable(D).first) { 15472 const ValueDecl *VD = 15473 DSAStack->getParentLoopControlVariable(DepCounter.getZExtValue()); 15474 if (VD) 15475 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) 15476 << 1 << VD; 15477 else 15478 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) << 0; 15479 continue; 15480 } 15481 OpsOffs.emplace_back(RHS, OOK); 15482 } else { 15483 bool OMPDependTFound = LangOpts.OpenMP >= 50; 15484 if (OMPDependTFound) 15485 OMPDependTFound = findOMPDependT(*this, StartLoc, DSAStack, 15486 DepKind == OMPC_DEPEND_depobj); 15487 if (DepKind == OMPC_DEPEND_depobj) { 15488 // OpenMP 5.0, 2.17.11 depend Clause, Restrictions, C/C++ 15489 // List items used in depend clauses with the depobj dependence type 15490 // must be expressions of the omp_depend_t type. 15491 if (!RefExpr->isValueDependent() && !RefExpr->isTypeDependent() && 15492 !RefExpr->isInstantiationDependent() && 15493 !RefExpr->containsUnexpandedParameterPack() && 15494 (OMPDependTFound && 15495 !Context.hasSameUnqualifiedType(DSAStack->getOMPDependT(), 15496 RefExpr->getType()))) { 15497 Diag(ELoc, diag::err_omp_expected_omp_depend_t_lvalue) 15498 << 0 << RefExpr->getType() << RefExpr->getSourceRange(); 15499 continue; 15500 } 15501 if (!RefExpr->isLValue()) { 15502 Diag(ELoc, diag::err_omp_expected_omp_depend_t_lvalue) 15503 << 1 << RefExpr->getType() << RefExpr->getSourceRange(); 15504 continue; 15505 } 15506 } else { 15507 // OpenMP 5.0 [2.17.11, Restrictions] 15508 // List items used in depend clauses cannot be zero-length array 15509 // sections. 15510 QualType ExprTy = RefExpr->getType().getNonReferenceType(); 15511 const auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr); 15512 if (OASE) { 15513 QualType BaseType = 15514 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 15515 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 15516 ExprTy = ATy->getElementType(); 15517 else 15518 ExprTy = BaseType->getPointeeType(); 15519 ExprTy = ExprTy.getNonReferenceType(); 15520 const Expr *Length = OASE->getLength(); 15521 Expr::EvalResult Result; 15522 if (Length && !Length->isValueDependent() && 15523 Length->EvaluateAsInt(Result, Context) && 15524 Result.Val.getInt().isNullValue()) { 15525 Diag(ELoc, 15526 diag::err_omp_depend_zero_length_array_section_not_allowed) 15527 << SimpleExpr->getSourceRange(); 15528 continue; 15529 } 15530 } 15531 15532 // OpenMP 5.0, 2.17.11 depend Clause, Restrictions, C/C++ 15533 // List items used in depend clauses with the in, out, inout or 15534 // mutexinoutset dependence types cannot be expressions of the 15535 // omp_depend_t type. 15536 if (!RefExpr->isValueDependent() && !RefExpr->isTypeDependent() && 15537 !RefExpr->isInstantiationDependent() && 15538 !RefExpr->containsUnexpandedParameterPack() && 15539 (OMPDependTFound && 15540 DSAStack->getOMPDependT().getTypePtr() == ExprTy.getTypePtr())) { 15541 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 15542 << 1 << RefExpr->getSourceRange(); 15543 continue; 15544 } 15545 15546 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr); 15547 if (!RefExpr->IgnoreParenImpCasts()->isLValue() || 15548 (ASE && 15549 !ASE->getBase() 15550 ->getType() 15551 .getNonReferenceType() 15552 ->isPointerType() && 15553 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) { 15554 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 15555 << (LangOpts.OpenMP >= 50 ? 1 : 0) << RefExpr->getSourceRange(); 15556 continue; 15557 } 15558 15559 ExprResult Res; 15560 { 15561 Sema::TentativeAnalysisScope Trap(*this); 15562 Res = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, 15563 RefExpr->IgnoreParenImpCasts()); 15564 } 15565 if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr)) { 15566 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 15567 << (LangOpts.OpenMP >= 50 ? 1 : 0) << RefExpr->getSourceRange(); 15568 continue; 15569 } 15570 } 15571 } 15572 Vars.push_back(RefExpr->IgnoreParenImpCasts()); 15573 } 15574 15575 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink && 15576 TotalDepCount > VarList.size() && 15577 DSAStack->getParentOrderedRegionParam().first && 15578 DSAStack->getParentLoopControlVariable(VarList.size() + 1)) { 15579 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration) 15580 << 1 << DSAStack->getParentLoopControlVariable(VarList.size() + 1); 15581 } 15582 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink && 15583 Vars.empty()) 15584 return nullptr; 15585 15586 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, 15587 DepKind, DepLoc, ColonLoc, Vars, 15588 TotalDepCount.getZExtValue()); 15589 if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) && 15590 DSAStack->isParentOrderedRegion()) 15591 DSAStack->addDoacrossDependClause(C, OpsOffs); 15592 return C; 15593 } 15594 15595 OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc, 15596 SourceLocation LParenLoc, 15597 SourceLocation EndLoc) { 15598 Expr *ValExpr = Device; 15599 Stmt *HelperValStmt = nullptr; 15600 15601 // OpenMP [2.9.1, Restrictions] 15602 // The device expression must evaluate to a non-negative integer value. 15603 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_device, 15604 /*StrictlyPositive=*/false)) 15605 return nullptr; 15606 15607 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 15608 OpenMPDirectiveKind CaptureRegion = 15609 getOpenMPCaptureRegionForClause(DKind, OMPC_device, LangOpts.OpenMP); 15610 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 15611 ValExpr = MakeFullExpr(ValExpr).get(); 15612 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 15613 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 15614 HelperValStmt = buildPreInits(Context, Captures); 15615 } 15616 15617 return new (Context) OMPDeviceClause(ValExpr, HelperValStmt, CaptureRegion, 15618 StartLoc, LParenLoc, EndLoc); 15619 } 15620 15621 static bool checkTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef, 15622 DSAStackTy *Stack, QualType QTy, 15623 bool FullCheck = true) { 15624 NamedDecl *ND; 15625 if (QTy->isIncompleteType(&ND)) { 15626 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR; 15627 return false; 15628 } 15629 if (FullCheck && !SemaRef.CurContext->isDependentContext() && 15630 !QTy.isTriviallyCopyableType(SemaRef.Context)) 15631 SemaRef.Diag(SL, diag::warn_omp_non_trivial_type_mapped) << QTy << SR; 15632 return true; 15633 } 15634 15635 /// Return true if it can be proven that the provided array expression 15636 /// (array section or array subscript) does NOT specify the whole size of the 15637 /// array whose base type is \a BaseQTy. 15638 static bool checkArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef, 15639 const Expr *E, 15640 QualType BaseQTy) { 15641 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E); 15642 15643 // If this is an array subscript, it refers to the whole size if the size of 15644 // the dimension is constant and equals 1. Also, an array section assumes the 15645 // format of an array subscript if no colon is used. 15646 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) { 15647 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) 15648 return ATy->getSize().getSExtValue() != 1; 15649 // Size can't be evaluated statically. 15650 return false; 15651 } 15652 15653 assert(OASE && "Expecting array section if not an array subscript."); 15654 const Expr *LowerBound = OASE->getLowerBound(); 15655 const Expr *Length = OASE->getLength(); 15656 15657 // If there is a lower bound that does not evaluates to zero, we are not 15658 // covering the whole dimension. 15659 if (LowerBound) { 15660 Expr::EvalResult Result; 15661 if (!LowerBound->EvaluateAsInt(Result, SemaRef.getASTContext())) 15662 return false; // Can't get the integer value as a constant. 15663 15664 llvm::APSInt ConstLowerBound = Result.Val.getInt(); 15665 if (ConstLowerBound.getSExtValue()) 15666 return true; 15667 } 15668 15669 // If we don't have a length we covering the whole dimension. 15670 if (!Length) 15671 return false; 15672 15673 // If the base is a pointer, we don't have a way to get the size of the 15674 // pointee. 15675 if (BaseQTy->isPointerType()) 15676 return false; 15677 15678 // We can only check if the length is the same as the size of the dimension 15679 // if we have a constant array. 15680 const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()); 15681 if (!CATy) 15682 return false; 15683 15684 Expr::EvalResult Result; 15685 if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext())) 15686 return false; // Can't get the integer value as a constant. 15687 15688 llvm::APSInt ConstLength = Result.Val.getInt(); 15689 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue(); 15690 } 15691 15692 // Return true if it can be proven that the provided array expression (array 15693 // section or array subscript) does NOT specify a single element of the array 15694 // whose base type is \a BaseQTy. 15695 static bool checkArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef, 15696 const Expr *E, 15697 QualType BaseQTy) { 15698 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E); 15699 15700 // An array subscript always refer to a single element. Also, an array section 15701 // assumes the format of an array subscript if no colon is used. 15702 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) 15703 return false; 15704 15705 assert(OASE && "Expecting array section if not an array subscript."); 15706 const Expr *Length = OASE->getLength(); 15707 15708 // If we don't have a length we have to check if the array has unitary size 15709 // for this dimension. Also, we should always expect a length if the base type 15710 // is pointer. 15711 if (!Length) { 15712 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) 15713 return ATy->getSize().getSExtValue() != 1; 15714 // We cannot assume anything. 15715 return false; 15716 } 15717 15718 // Check if the length evaluates to 1. 15719 Expr::EvalResult Result; 15720 if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext())) 15721 return false; // Can't get the integer value as a constant. 15722 15723 llvm::APSInt ConstLength = Result.Val.getInt(); 15724 return ConstLength.getSExtValue() != 1; 15725 } 15726 15727 // The base of elements of list in a map clause have to be either: 15728 // - a reference to variable or field. 15729 // - a member expression. 15730 // - an array expression. 15731 // 15732 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the 15733 // reference to 'r'. 15734 // 15735 // If we have: 15736 // 15737 // struct SS { 15738 // Bla S; 15739 // foo() { 15740 // #pragma omp target map (S.Arr[:12]); 15741 // } 15742 // } 15743 // 15744 // We want to retrieve the member expression 'this->S'; 15745 15746 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2] 15747 // If a list item is an array section, it must specify contiguous storage. 15748 // 15749 // For this restriction it is sufficient that we make sure only references 15750 // to variables or fields and array expressions, and that no array sections 15751 // exist except in the rightmost expression (unless they cover the whole 15752 // dimension of the array). E.g. these would be invalid: 15753 // 15754 // r.ArrS[3:5].Arr[6:7] 15755 // 15756 // r.ArrS[3:5].x 15757 // 15758 // but these would be valid: 15759 // r.ArrS[3].Arr[6:7] 15760 // 15761 // r.ArrS[3].x 15762 namespace { 15763 class MapBaseChecker final : public StmtVisitor<MapBaseChecker, bool> { 15764 Sema &SemaRef; 15765 OpenMPClauseKind CKind = OMPC_unknown; 15766 OMPClauseMappableExprCommon::MappableExprComponentList &Components; 15767 bool NoDiagnose = false; 15768 const Expr *RelevantExpr = nullptr; 15769 bool AllowUnitySizeArraySection = true; 15770 bool AllowWholeSizeArraySection = true; 15771 SourceLocation ELoc; 15772 SourceRange ERange; 15773 15774 void emitErrorMsg() { 15775 // If nothing else worked, this is not a valid map clause expression. 15776 if (SemaRef.getLangOpts().OpenMP < 50) { 15777 SemaRef.Diag(ELoc, 15778 diag::err_omp_expected_named_var_member_or_array_expression) 15779 << ERange; 15780 } else { 15781 SemaRef.Diag(ELoc, diag::err_omp_non_lvalue_in_map_or_motion_clauses) 15782 << getOpenMPClauseName(CKind) << ERange; 15783 } 15784 } 15785 15786 public: 15787 bool VisitDeclRefExpr(DeclRefExpr *DRE) { 15788 if (!isa<VarDecl>(DRE->getDecl())) { 15789 emitErrorMsg(); 15790 return false; 15791 } 15792 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 15793 RelevantExpr = DRE; 15794 // Record the component. 15795 Components.emplace_back(DRE, DRE->getDecl()); 15796 return true; 15797 } 15798 15799 bool VisitMemberExpr(MemberExpr *ME) { 15800 Expr *E = ME; 15801 Expr *BaseE = ME->getBase()->IgnoreParenCasts(); 15802 15803 if (isa<CXXThisExpr>(BaseE)) { 15804 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 15805 // We found a base expression: this->Val. 15806 RelevantExpr = ME; 15807 } else { 15808 E = BaseE; 15809 } 15810 15811 if (!isa<FieldDecl>(ME->getMemberDecl())) { 15812 if (!NoDiagnose) { 15813 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field) 15814 << ME->getSourceRange(); 15815 return false; 15816 } 15817 if (RelevantExpr) 15818 return false; 15819 return Visit(E); 15820 } 15821 15822 auto *FD = cast<FieldDecl>(ME->getMemberDecl()); 15823 15824 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3] 15825 // A bit-field cannot appear in a map clause. 15826 // 15827 if (FD->isBitField()) { 15828 if (!NoDiagnose) { 15829 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause) 15830 << ME->getSourceRange() << getOpenMPClauseName(CKind); 15831 return false; 15832 } 15833 if (RelevantExpr) 15834 return false; 15835 return Visit(E); 15836 } 15837 15838 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 15839 // If the type of a list item is a reference to a type T then the type 15840 // will be considered to be T for all purposes of this clause. 15841 QualType CurType = BaseE->getType().getNonReferenceType(); 15842 15843 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2] 15844 // A list item cannot be a variable that is a member of a structure with 15845 // a union type. 15846 // 15847 if (CurType->isUnionType()) { 15848 if (!NoDiagnose) { 15849 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed) 15850 << ME->getSourceRange(); 15851 return false; 15852 } 15853 return RelevantExpr || Visit(E); 15854 } 15855 15856 // If we got a member expression, we should not expect any array section 15857 // before that: 15858 // 15859 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7] 15860 // If a list item is an element of a structure, only the rightmost symbol 15861 // of the variable reference can be an array section. 15862 // 15863 AllowUnitySizeArraySection = false; 15864 AllowWholeSizeArraySection = false; 15865 15866 // Record the component. 15867 Components.emplace_back(ME, FD); 15868 return RelevantExpr || Visit(E); 15869 } 15870 15871 bool VisitArraySubscriptExpr(ArraySubscriptExpr *AE) { 15872 Expr *E = AE->getBase()->IgnoreParenImpCasts(); 15873 15874 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) { 15875 if (!NoDiagnose) { 15876 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name) 15877 << 0 << AE->getSourceRange(); 15878 return false; 15879 } 15880 return RelevantExpr || Visit(E); 15881 } 15882 15883 // If we got an array subscript that express the whole dimension we 15884 // can have any array expressions before. If it only expressing part of 15885 // the dimension, we can only have unitary-size array expressions. 15886 if (checkArrayExpressionDoesNotReferToWholeSize(SemaRef, AE, 15887 E->getType())) 15888 AllowWholeSizeArraySection = false; 15889 15890 if (const auto *TE = dyn_cast<CXXThisExpr>(E->IgnoreParenCasts())) { 15891 Expr::EvalResult Result; 15892 if (!AE->getIdx()->isValueDependent() && 15893 AE->getIdx()->EvaluateAsInt(Result, SemaRef.getASTContext()) && 15894 !Result.Val.getInt().isNullValue()) { 15895 SemaRef.Diag(AE->getIdx()->getExprLoc(), 15896 diag::err_omp_invalid_map_this_expr); 15897 SemaRef.Diag(AE->getIdx()->getExprLoc(), 15898 diag::note_omp_invalid_subscript_on_this_ptr_map); 15899 } 15900 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 15901 RelevantExpr = TE; 15902 } 15903 15904 // Record the component - we don't have any declaration associated. 15905 Components.emplace_back(AE, nullptr); 15906 15907 return RelevantExpr || Visit(E); 15908 } 15909 15910 bool VisitOMPArraySectionExpr(OMPArraySectionExpr *OASE) { 15911 assert(!NoDiagnose && "Array sections cannot be implicitly mapped."); 15912 Expr *E = OASE->getBase()->IgnoreParenImpCasts(); 15913 QualType CurType = 15914 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType(); 15915 15916 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 15917 // If the type of a list item is a reference to a type T then the type 15918 // will be considered to be T for all purposes of this clause. 15919 if (CurType->isReferenceType()) 15920 CurType = CurType->getPointeeType(); 15921 15922 bool IsPointer = CurType->isAnyPointerType(); 15923 15924 if (!IsPointer && !CurType->isArrayType()) { 15925 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name) 15926 << 0 << OASE->getSourceRange(); 15927 return false; 15928 } 15929 15930 bool NotWhole = 15931 checkArrayExpressionDoesNotReferToWholeSize(SemaRef, OASE, CurType); 15932 bool NotUnity = 15933 checkArrayExpressionDoesNotReferToUnitySize(SemaRef, OASE, CurType); 15934 15935 if (AllowWholeSizeArraySection) { 15936 // Any array section is currently allowed. Allowing a whole size array 15937 // section implies allowing a unity array section as well. 15938 // 15939 // If this array section refers to the whole dimension we can still 15940 // accept other array sections before this one, except if the base is a 15941 // pointer. Otherwise, only unitary sections are accepted. 15942 if (NotWhole || IsPointer) 15943 AllowWholeSizeArraySection = false; 15944 } else if (AllowUnitySizeArraySection && NotUnity) { 15945 // A unity or whole array section is not allowed and that is not 15946 // compatible with the properties of the current array section. 15947 SemaRef.Diag( 15948 ELoc, diag::err_array_section_does_not_specify_contiguous_storage) 15949 << OASE->getSourceRange(); 15950 return false; 15951 } 15952 15953 if (const auto *TE = dyn_cast<CXXThisExpr>(E)) { 15954 Expr::EvalResult ResultR; 15955 Expr::EvalResult ResultL; 15956 if (!OASE->getLength()->isValueDependent() && 15957 OASE->getLength()->EvaluateAsInt(ResultR, SemaRef.getASTContext()) && 15958 !ResultR.Val.getInt().isOneValue()) { 15959 SemaRef.Diag(OASE->getLength()->getExprLoc(), 15960 diag::err_omp_invalid_map_this_expr); 15961 SemaRef.Diag(OASE->getLength()->getExprLoc(), 15962 diag::note_omp_invalid_length_on_this_ptr_mapping); 15963 } 15964 if (OASE->getLowerBound() && !OASE->getLowerBound()->isValueDependent() && 15965 OASE->getLowerBound()->EvaluateAsInt(ResultL, 15966 SemaRef.getASTContext()) && 15967 !ResultL.Val.getInt().isNullValue()) { 15968 SemaRef.Diag(OASE->getLowerBound()->getExprLoc(), 15969 diag::err_omp_invalid_map_this_expr); 15970 SemaRef.Diag(OASE->getLowerBound()->getExprLoc(), 15971 diag::note_omp_invalid_lower_bound_on_this_ptr_mapping); 15972 } 15973 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 15974 RelevantExpr = TE; 15975 } 15976 15977 // Record the component - we don't have any declaration associated. 15978 Components.emplace_back(OASE, nullptr); 15979 return RelevantExpr || Visit(E); 15980 } 15981 bool VisitUnaryOperator(UnaryOperator *UO) { 15982 if (SemaRef.getLangOpts().OpenMP < 50 || !UO->isLValue() || 15983 UO->getOpcode() != UO_Deref) { 15984 emitErrorMsg(); 15985 return false; 15986 } 15987 if (!RelevantExpr) { 15988 // Record the component if haven't found base decl. 15989 Components.emplace_back(UO, nullptr); 15990 } 15991 return RelevantExpr || Visit(UO->getSubExpr()->IgnoreParenImpCasts()); 15992 } 15993 bool VisitBinaryOperator(BinaryOperator *BO) { 15994 if (SemaRef.getLangOpts().OpenMP < 50 || !BO->getType()->isPointerType()) { 15995 emitErrorMsg(); 15996 return false; 15997 } 15998 15999 // Pointer arithmetic is the only thing we expect to happen here so after we 16000 // make sure the binary operator is a pointer type, the we only thing need 16001 // to to is to visit the subtree that has the same type as root (so that we 16002 // know the other subtree is just an offset) 16003 Expr *LE = BO->getLHS()->IgnoreParenImpCasts(); 16004 Expr *RE = BO->getRHS()->IgnoreParenImpCasts(); 16005 Components.emplace_back(BO, nullptr); 16006 assert((LE->getType().getTypePtr() == BO->getType().getTypePtr() || 16007 RE->getType().getTypePtr() == BO->getType().getTypePtr()) && 16008 "Either LHS or RHS have base decl inside"); 16009 if (BO->getType().getTypePtr() == LE->getType().getTypePtr()) 16010 return RelevantExpr || Visit(LE); 16011 return RelevantExpr || Visit(RE); 16012 } 16013 bool VisitCXXThisExpr(CXXThisExpr *CTE) { 16014 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 16015 RelevantExpr = CTE; 16016 Components.emplace_back(CTE, nullptr); 16017 return true; 16018 } 16019 bool VisitStmt(Stmt *) { 16020 emitErrorMsg(); 16021 return false; 16022 } 16023 const Expr *getFoundBase() const { 16024 return RelevantExpr; 16025 } 16026 explicit MapBaseChecker( 16027 Sema &SemaRef, OpenMPClauseKind CKind, 16028 OMPClauseMappableExprCommon::MappableExprComponentList &Components, 16029 bool NoDiagnose, SourceLocation &ELoc, SourceRange &ERange) 16030 : SemaRef(SemaRef), CKind(CKind), Components(Components), 16031 NoDiagnose(NoDiagnose), ELoc(ELoc), ERange(ERange) {} 16032 }; 16033 } // namespace 16034 16035 /// Return the expression of the base of the mappable expression or null if it 16036 /// cannot be determined and do all the necessary checks to see if the expression 16037 /// is valid as a standalone mappable expression. In the process, record all the 16038 /// components of the expression. 16039 static const Expr *checkMapClauseExpressionBase( 16040 Sema &SemaRef, Expr *E, 16041 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents, 16042 OpenMPClauseKind CKind, bool NoDiagnose) { 16043 SourceLocation ELoc = E->getExprLoc(); 16044 SourceRange ERange = E->getSourceRange(); 16045 MapBaseChecker Checker(SemaRef, CKind, CurComponents, NoDiagnose, ELoc, 16046 ERange); 16047 if (Checker.Visit(E->IgnoreParens())) 16048 return Checker.getFoundBase(); 16049 return nullptr; 16050 } 16051 16052 // Return true if expression E associated with value VD has conflicts with other 16053 // map information. 16054 static bool checkMapConflicts( 16055 Sema &SemaRef, DSAStackTy *DSAS, const ValueDecl *VD, const Expr *E, 16056 bool CurrentRegionOnly, 16057 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents, 16058 OpenMPClauseKind CKind) { 16059 assert(VD && E); 16060 SourceLocation ELoc = E->getExprLoc(); 16061 SourceRange ERange = E->getSourceRange(); 16062 16063 // In order to easily check the conflicts we need to match each component of 16064 // the expression under test with the components of the expressions that are 16065 // already in the stack. 16066 16067 assert(!CurComponents.empty() && "Map clause expression with no components!"); 16068 assert(CurComponents.back().getAssociatedDeclaration() == VD && 16069 "Map clause expression with unexpected base!"); 16070 16071 // Variables to help detecting enclosing problems in data environment nests. 16072 bool IsEnclosedByDataEnvironmentExpr = false; 16073 const Expr *EnclosingExpr = nullptr; 16074 16075 bool FoundError = DSAS->checkMappableExprComponentListsForDecl( 16076 VD, CurrentRegionOnly, 16077 [&IsEnclosedByDataEnvironmentExpr, &SemaRef, VD, CurrentRegionOnly, ELoc, 16078 ERange, CKind, &EnclosingExpr, 16079 CurComponents](OMPClauseMappableExprCommon::MappableExprComponentListRef 16080 StackComponents, 16081 OpenMPClauseKind) { 16082 assert(!StackComponents.empty() && 16083 "Map clause expression with no components!"); 16084 assert(StackComponents.back().getAssociatedDeclaration() == VD && 16085 "Map clause expression with unexpected base!"); 16086 (void)VD; 16087 16088 // The whole expression in the stack. 16089 const Expr *RE = StackComponents.front().getAssociatedExpression(); 16090 16091 // Expressions must start from the same base. Here we detect at which 16092 // point both expressions diverge from each other and see if we can 16093 // detect if the memory referred to both expressions is contiguous and 16094 // do not overlap. 16095 auto CI = CurComponents.rbegin(); 16096 auto CE = CurComponents.rend(); 16097 auto SI = StackComponents.rbegin(); 16098 auto SE = StackComponents.rend(); 16099 for (; CI != CE && SI != SE; ++CI, ++SI) { 16100 16101 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3] 16102 // At most one list item can be an array item derived from a given 16103 // variable in map clauses of the same construct. 16104 if (CurrentRegionOnly && 16105 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) || 16106 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) && 16107 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) || 16108 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) { 16109 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(), 16110 diag::err_omp_multiple_array_items_in_map_clause) 16111 << CI->getAssociatedExpression()->getSourceRange(); 16112 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(), 16113 diag::note_used_here) 16114 << SI->getAssociatedExpression()->getSourceRange(); 16115 return true; 16116 } 16117 16118 // Do both expressions have the same kind? 16119 if (CI->getAssociatedExpression()->getStmtClass() != 16120 SI->getAssociatedExpression()->getStmtClass()) 16121 break; 16122 16123 // Are we dealing with different variables/fields? 16124 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration()) 16125 break; 16126 } 16127 // Check if the extra components of the expressions in the enclosing 16128 // data environment are redundant for the current base declaration. 16129 // If they are, the maps completely overlap, which is legal. 16130 for (; SI != SE; ++SI) { 16131 QualType Type; 16132 if (const auto *ASE = 16133 dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) { 16134 Type = ASE->getBase()->IgnoreParenImpCasts()->getType(); 16135 } else if (const auto *OASE = dyn_cast<OMPArraySectionExpr>( 16136 SI->getAssociatedExpression())) { 16137 const Expr *E = OASE->getBase()->IgnoreParenImpCasts(); 16138 Type = 16139 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType(); 16140 } 16141 if (Type.isNull() || Type->isAnyPointerType() || 16142 checkArrayExpressionDoesNotReferToWholeSize( 16143 SemaRef, SI->getAssociatedExpression(), Type)) 16144 break; 16145 } 16146 16147 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4] 16148 // List items of map clauses in the same construct must not share 16149 // original storage. 16150 // 16151 // If the expressions are exactly the same or one is a subset of the 16152 // other, it means they are sharing storage. 16153 if (CI == CE && SI == SE) { 16154 if (CurrentRegionOnly) { 16155 if (CKind == OMPC_map) { 16156 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange; 16157 } else { 16158 assert(CKind == OMPC_to || CKind == OMPC_from); 16159 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update) 16160 << ERange; 16161 } 16162 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 16163 << RE->getSourceRange(); 16164 return true; 16165 } 16166 // If we find the same expression in the enclosing data environment, 16167 // that is legal. 16168 IsEnclosedByDataEnvironmentExpr = true; 16169 return false; 16170 } 16171 16172 QualType DerivedType = 16173 std::prev(CI)->getAssociatedDeclaration()->getType(); 16174 SourceLocation DerivedLoc = 16175 std::prev(CI)->getAssociatedExpression()->getExprLoc(); 16176 16177 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 16178 // If the type of a list item is a reference to a type T then the type 16179 // will be considered to be T for all purposes of this clause. 16180 DerivedType = DerivedType.getNonReferenceType(); 16181 16182 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1] 16183 // A variable for which the type is pointer and an array section 16184 // derived from that variable must not appear as list items of map 16185 // clauses of the same construct. 16186 // 16187 // Also, cover one of the cases in: 16188 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5] 16189 // If any part of the original storage of a list item has corresponding 16190 // storage in the device data environment, all of the original storage 16191 // must have corresponding storage in the device data environment. 16192 // 16193 if (DerivedType->isAnyPointerType()) { 16194 if (CI == CE || SI == SE) { 16195 SemaRef.Diag( 16196 DerivedLoc, 16197 diag::err_omp_pointer_mapped_along_with_derived_section) 16198 << DerivedLoc; 16199 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 16200 << RE->getSourceRange(); 16201 return true; 16202 } 16203 if (CI->getAssociatedExpression()->getStmtClass() != 16204 SI->getAssociatedExpression()->getStmtClass() || 16205 CI->getAssociatedDeclaration()->getCanonicalDecl() == 16206 SI->getAssociatedDeclaration()->getCanonicalDecl()) { 16207 assert(CI != CE && SI != SE); 16208 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_dereferenced) 16209 << DerivedLoc; 16210 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 16211 << RE->getSourceRange(); 16212 return true; 16213 } 16214 } 16215 16216 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4] 16217 // List items of map clauses in the same construct must not share 16218 // original storage. 16219 // 16220 // An expression is a subset of the other. 16221 if (CurrentRegionOnly && (CI == CE || SI == SE)) { 16222 if (CKind == OMPC_map) { 16223 if (CI != CE || SI != SE) { 16224 // Allow constructs like this: map(s, s.ptr[0:1]), where s.ptr is 16225 // a pointer. 16226 auto Begin = 16227 CI != CE ? CurComponents.begin() : StackComponents.begin(); 16228 auto End = CI != CE ? CurComponents.end() : StackComponents.end(); 16229 auto It = Begin; 16230 while (It != End && !It->getAssociatedDeclaration()) 16231 std::advance(It, 1); 16232 assert(It != End && 16233 "Expected at least one component with the declaration."); 16234 if (It != Begin && It->getAssociatedDeclaration() 16235 ->getType() 16236 .getCanonicalType() 16237 ->isAnyPointerType()) { 16238 IsEnclosedByDataEnvironmentExpr = false; 16239 EnclosingExpr = nullptr; 16240 return false; 16241 } 16242 } 16243 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange; 16244 } else { 16245 assert(CKind == OMPC_to || CKind == OMPC_from); 16246 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update) 16247 << ERange; 16248 } 16249 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 16250 << RE->getSourceRange(); 16251 return true; 16252 } 16253 16254 // The current expression uses the same base as other expression in the 16255 // data environment but does not contain it completely. 16256 if (!CurrentRegionOnly && SI != SE) 16257 EnclosingExpr = RE; 16258 16259 // The current expression is a subset of the expression in the data 16260 // environment. 16261 IsEnclosedByDataEnvironmentExpr |= 16262 (!CurrentRegionOnly && CI != CE && SI == SE); 16263 16264 return false; 16265 }); 16266 16267 if (CurrentRegionOnly) 16268 return FoundError; 16269 16270 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5] 16271 // If any part of the original storage of a list item has corresponding 16272 // storage in the device data environment, all of the original storage must 16273 // have corresponding storage in the device data environment. 16274 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6] 16275 // If a list item is an element of a structure, and a different element of 16276 // the structure has a corresponding list item in the device data environment 16277 // prior to a task encountering the construct associated with the map clause, 16278 // then the list item must also have a corresponding list item in the device 16279 // data environment prior to the task encountering the construct. 16280 // 16281 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) { 16282 SemaRef.Diag(ELoc, 16283 diag::err_omp_original_storage_is_shared_and_does_not_contain) 16284 << ERange; 16285 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here) 16286 << EnclosingExpr->getSourceRange(); 16287 return true; 16288 } 16289 16290 return FoundError; 16291 } 16292 16293 // Look up the user-defined mapper given the mapper name and mapped type, and 16294 // build a reference to it. 16295 static ExprResult buildUserDefinedMapperRef(Sema &SemaRef, Scope *S, 16296 CXXScopeSpec &MapperIdScopeSpec, 16297 const DeclarationNameInfo &MapperId, 16298 QualType Type, 16299 Expr *UnresolvedMapper) { 16300 if (MapperIdScopeSpec.isInvalid()) 16301 return ExprError(); 16302 // Get the actual type for the array type. 16303 if (Type->isArrayType()) { 16304 assert(Type->getAsArrayTypeUnsafe() && "Expect to get a valid array type"); 16305 Type = Type->getAsArrayTypeUnsafe()->getElementType().getCanonicalType(); 16306 } 16307 // Find all user-defined mappers with the given MapperId. 16308 SmallVector<UnresolvedSet<8>, 4> Lookups; 16309 LookupResult Lookup(SemaRef, MapperId, Sema::LookupOMPMapperName); 16310 Lookup.suppressDiagnostics(); 16311 if (S) { 16312 while (S && SemaRef.LookupParsedName(Lookup, S, &MapperIdScopeSpec)) { 16313 NamedDecl *D = Lookup.getRepresentativeDecl(); 16314 while (S && !S->isDeclScope(D)) 16315 S = S->getParent(); 16316 if (S) 16317 S = S->getParent(); 16318 Lookups.emplace_back(); 16319 Lookups.back().append(Lookup.begin(), Lookup.end()); 16320 Lookup.clear(); 16321 } 16322 } else if (auto *ULE = cast_or_null<UnresolvedLookupExpr>(UnresolvedMapper)) { 16323 // Extract the user-defined mappers with the given MapperId. 16324 Lookups.push_back(UnresolvedSet<8>()); 16325 for (NamedDecl *D : ULE->decls()) { 16326 auto *DMD = cast<OMPDeclareMapperDecl>(D); 16327 assert(DMD && "Expect valid OMPDeclareMapperDecl during instantiation."); 16328 Lookups.back().addDecl(DMD); 16329 } 16330 } 16331 // Defer the lookup for dependent types. The results will be passed through 16332 // UnresolvedMapper on instantiation. 16333 if (SemaRef.CurContext->isDependentContext() || Type->isDependentType() || 16334 Type->isInstantiationDependentType() || 16335 Type->containsUnexpandedParameterPack() || 16336 filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) { 16337 return !D->isInvalidDecl() && 16338 (D->getType()->isDependentType() || 16339 D->getType()->isInstantiationDependentType() || 16340 D->getType()->containsUnexpandedParameterPack()); 16341 })) { 16342 UnresolvedSet<8> URS; 16343 for (const UnresolvedSet<8> &Set : Lookups) { 16344 if (Set.empty()) 16345 continue; 16346 URS.append(Set.begin(), Set.end()); 16347 } 16348 return UnresolvedLookupExpr::Create( 16349 SemaRef.Context, /*NamingClass=*/nullptr, 16350 MapperIdScopeSpec.getWithLocInContext(SemaRef.Context), MapperId, 16351 /*ADL=*/false, /*Overloaded=*/true, URS.begin(), URS.end()); 16352 } 16353 SourceLocation Loc = MapperId.getLoc(); 16354 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions 16355 // The type must be of struct, union or class type in C and C++ 16356 if (!Type->isStructureOrClassType() && !Type->isUnionType() && 16357 (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default")) { 16358 SemaRef.Diag(Loc, diag::err_omp_mapper_wrong_type); 16359 return ExprError(); 16360 } 16361 // Perform argument dependent lookup. 16362 if (SemaRef.getLangOpts().CPlusPlus && !MapperIdScopeSpec.isSet()) 16363 argumentDependentLookup(SemaRef, MapperId, Loc, Type, Lookups); 16364 // Return the first user-defined mapper with the desired type. 16365 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 16366 Lookups, [&SemaRef, Type](ValueDecl *D) -> ValueDecl * { 16367 if (!D->isInvalidDecl() && 16368 SemaRef.Context.hasSameType(D->getType(), Type)) 16369 return D; 16370 return nullptr; 16371 })) 16372 return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc); 16373 // Find the first user-defined mapper with a type derived from the desired 16374 // type. 16375 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 16376 Lookups, [&SemaRef, Type, Loc](ValueDecl *D) -> ValueDecl * { 16377 if (!D->isInvalidDecl() && 16378 SemaRef.IsDerivedFrom(Loc, Type, D->getType()) && 16379 !Type.isMoreQualifiedThan(D->getType())) 16380 return D; 16381 return nullptr; 16382 })) { 16383 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, 16384 /*DetectVirtual=*/false); 16385 if (SemaRef.IsDerivedFrom(Loc, Type, VD->getType(), Paths)) { 16386 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType( 16387 VD->getType().getUnqualifiedType()))) { 16388 if (SemaRef.CheckBaseClassAccess( 16389 Loc, VD->getType(), Type, Paths.front(), 16390 /*DiagID=*/0) != Sema::AR_inaccessible) { 16391 return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc); 16392 } 16393 } 16394 } 16395 } 16396 // Report error if a mapper is specified, but cannot be found. 16397 if (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default") { 16398 SemaRef.Diag(Loc, diag::err_omp_invalid_mapper) 16399 << Type << MapperId.getName(); 16400 return ExprError(); 16401 } 16402 return ExprEmpty(); 16403 } 16404 16405 namespace { 16406 // Utility struct that gathers all the related lists associated with a mappable 16407 // expression. 16408 struct MappableVarListInfo { 16409 // The list of expressions. 16410 ArrayRef<Expr *> VarList; 16411 // The list of processed expressions. 16412 SmallVector<Expr *, 16> ProcessedVarList; 16413 // The mappble components for each expression. 16414 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents; 16415 // The base declaration of the variable. 16416 SmallVector<ValueDecl *, 16> VarBaseDeclarations; 16417 // The reference to the user-defined mapper associated with every expression. 16418 SmallVector<Expr *, 16> UDMapperList; 16419 16420 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) { 16421 // We have a list of components and base declarations for each entry in the 16422 // variable list. 16423 VarComponents.reserve(VarList.size()); 16424 VarBaseDeclarations.reserve(VarList.size()); 16425 } 16426 }; 16427 } 16428 16429 // Check the validity of the provided variable list for the provided clause kind 16430 // \a CKind. In the check process the valid expressions, mappable expression 16431 // components, variables, and user-defined mappers are extracted and used to 16432 // fill \a ProcessedVarList, \a VarComponents, \a VarBaseDeclarations, and \a 16433 // UDMapperList in MVLI. \a MapType, \a IsMapTypeImplicit, \a MapperIdScopeSpec, 16434 // and \a MapperId are expected to be valid if the clause kind is 'map'. 16435 static void checkMappableExpressionList( 16436 Sema &SemaRef, DSAStackTy *DSAS, OpenMPClauseKind CKind, 16437 MappableVarListInfo &MVLI, SourceLocation StartLoc, 16438 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo MapperId, 16439 ArrayRef<Expr *> UnresolvedMappers, 16440 OpenMPMapClauseKind MapType = OMPC_MAP_unknown, 16441 bool IsMapTypeImplicit = false) { 16442 // We only expect mappable expressions in 'to', 'from', and 'map' clauses. 16443 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) && 16444 "Unexpected clause kind with mappable expressions!"); 16445 16446 // If the identifier of user-defined mapper is not specified, it is "default". 16447 // We do not change the actual name in this clause to distinguish whether a 16448 // mapper is specified explicitly, i.e., it is not explicitly specified when 16449 // MapperId.getName() is empty. 16450 if (!MapperId.getName() || MapperId.getName().isEmpty()) { 16451 auto &DeclNames = SemaRef.getASTContext().DeclarationNames; 16452 MapperId.setName(DeclNames.getIdentifier( 16453 &SemaRef.getASTContext().Idents.get("default"))); 16454 } 16455 16456 // Iterators to find the current unresolved mapper expression. 16457 auto UMIt = UnresolvedMappers.begin(), UMEnd = UnresolvedMappers.end(); 16458 bool UpdateUMIt = false; 16459 Expr *UnresolvedMapper = nullptr; 16460 16461 // Keep track of the mappable components and base declarations in this clause. 16462 // Each entry in the list is going to have a list of components associated. We 16463 // record each set of the components so that we can build the clause later on. 16464 // In the end we should have the same amount of declarations and component 16465 // lists. 16466 16467 for (Expr *RE : MVLI.VarList) { 16468 assert(RE && "Null expr in omp to/from/map clause"); 16469 SourceLocation ELoc = RE->getExprLoc(); 16470 16471 // Find the current unresolved mapper expression. 16472 if (UpdateUMIt && UMIt != UMEnd) { 16473 UMIt++; 16474 assert( 16475 UMIt != UMEnd && 16476 "Expect the size of UnresolvedMappers to match with that of VarList"); 16477 } 16478 UpdateUMIt = true; 16479 if (UMIt != UMEnd) 16480 UnresolvedMapper = *UMIt; 16481 16482 const Expr *VE = RE->IgnoreParenLValueCasts(); 16483 16484 if (VE->isValueDependent() || VE->isTypeDependent() || 16485 VE->isInstantiationDependent() || 16486 VE->containsUnexpandedParameterPack()) { 16487 // Try to find the associated user-defined mapper. 16488 ExprResult ER = buildUserDefinedMapperRef( 16489 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId, 16490 VE->getType().getCanonicalType(), UnresolvedMapper); 16491 if (ER.isInvalid()) 16492 continue; 16493 MVLI.UDMapperList.push_back(ER.get()); 16494 // We can only analyze this information once the missing information is 16495 // resolved. 16496 MVLI.ProcessedVarList.push_back(RE); 16497 continue; 16498 } 16499 16500 Expr *SimpleExpr = RE->IgnoreParenCasts(); 16501 16502 if (!RE->isLValue()) { 16503 if (SemaRef.getLangOpts().OpenMP < 50) { 16504 SemaRef.Diag( 16505 ELoc, diag::err_omp_expected_named_var_member_or_array_expression) 16506 << RE->getSourceRange(); 16507 } else { 16508 SemaRef.Diag(ELoc, diag::err_omp_non_lvalue_in_map_or_motion_clauses) 16509 << getOpenMPClauseName(CKind) << RE->getSourceRange(); 16510 } 16511 continue; 16512 } 16513 16514 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents; 16515 ValueDecl *CurDeclaration = nullptr; 16516 16517 // Obtain the array or member expression bases if required. Also, fill the 16518 // components array with all the components identified in the process. 16519 const Expr *BE = checkMapClauseExpressionBase( 16520 SemaRef, SimpleExpr, CurComponents, CKind, /*NoDiagnose=*/false); 16521 if (!BE) 16522 continue; 16523 16524 assert(!CurComponents.empty() && 16525 "Invalid mappable expression information."); 16526 16527 if (const auto *TE = dyn_cast<CXXThisExpr>(BE)) { 16528 // Add store "this" pointer to class in DSAStackTy for future checking 16529 DSAS->addMappedClassesQualTypes(TE->getType()); 16530 // Try to find the associated user-defined mapper. 16531 ExprResult ER = buildUserDefinedMapperRef( 16532 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId, 16533 VE->getType().getCanonicalType(), UnresolvedMapper); 16534 if (ER.isInvalid()) 16535 continue; 16536 MVLI.UDMapperList.push_back(ER.get()); 16537 // Skip restriction checking for variable or field declarations 16538 MVLI.ProcessedVarList.push_back(RE); 16539 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 16540 MVLI.VarComponents.back().append(CurComponents.begin(), 16541 CurComponents.end()); 16542 MVLI.VarBaseDeclarations.push_back(nullptr); 16543 continue; 16544 } 16545 16546 // For the following checks, we rely on the base declaration which is 16547 // expected to be associated with the last component. The declaration is 16548 // expected to be a variable or a field (if 'this' is being mapped). 16549 CurDeclaration = CurComponents.back().getAssociatedDeclaration(); 16550 assert(CurDeclaration && "Null decl on map clause."); 16551 assert( 16552 CurDeclaration->isCanonicalDecl() && 16553 "Expecting components to have associated only canonical declarations."); 16554 16555 auto *VD = dyn_cast<VarDecl>(CurDeclaration); 16556 const auto *FD = dyn_cast<FieldDecl>(CurDeclaration); 16557 16558 assert((VD || FD) && "Only variables or fields are expected here!"); 16559 (void)FD; 16560 16561 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10] 16562 // threadprivate variables cannot appear in a map clause. 16563 // OpenMP 4.5 [2.10.5, target update Construct] 16564 // threadprivate variables cannot appear in a from clause. 16565 if (VD && DSAS->isThreadPrivate(VD)) { 16566 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false); 16567 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause) 16568 << getOpenMPClauseName(CKind); 16569 reportOriginalDsa(SemaRef, DSAS, VD, DVar); 16570 continue; 16571 } 16572 16573 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9] 16574 // A list item cannot appear in both a map clause and a data-sharing 16575 // attribute clause on the same construct. 16576 16577 // Check conflicts with other map clause expressions. We check the conflicts 16578 // with the current construct separately from the enclosing data 16579 // environment, because the restrictions are different. We only have to 16580 // check conflicts across regions for the map clauses. 16581 if (checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr, 16582 /*CurrentRegionOnly=*/true, CurComponents, CKind)) 16583 break; 16584 if (CKind == OMPC_map && 16585 checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr, 16586 /*CurrentRegionOnly=*/false, CurComponents, CKind)) 16587 break; 16588 16589 // OpenMP 4.5 [2.10.5, target update Construct] 16590 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 16591 // If the type of a list item is a reference to a type T then the type will 16592 // be considered to be T for all purposes of this clause. 16593 auto I = llvm::find_if( 16594 CurComponents, 16595 [](const OMPClauseMappableExprCommon::MappableComponent &MC) { 16596 return MC.getAssociatedDeclaration(); 16597 }); 16598 assert(I != CurComponents.end() && "Null decl on map clause."); 16599 QualType Type; 16600 auto *ASE = dyn_cast<ArraySubscriptExpr>(VE->IgnoreParens()); 16601 auto *OASE = dyn_cast<OMPArraySectionExpr>(VE->IgnoreParens()); 16602 if (ASE) { 16603 Type = ASE->getType().getNonReferenceType(); 16604 } else if (OASE) { 16605 QualType BaseType = 16606 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 16607 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 16608 Type = ATy->getElementType(); 16609 else 16610 Type = BaseType->getPointeeType(); 16611 Type = Type.getNonReferenceType(); 16612 } else { 16613 Type = VE->getType(); 16614 } 16615 16616 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4] 16617 // A list item in a to or from clause must have a mappable type. 16618 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9] 16619 // A list item must have a mappable type. 16620 if (!checkTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef, 16621 DSAS, Type)) 16622 continue; 16623 16624 Type = I->getAssociatedDeclaration()->getType().getNonReferenceType(); 16625 16626 if (CKind == OMPC_map) { 16627 // target enter data 16628 // OpenMP [2.10.2, Restrictions, p. 99] 16629 // A map-type must be specified in all map clauses and must be either 16630 // to or alloc. 16631 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective(); 16632 if (DKind == OMPD_target_enter_data && 16633 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) { 16634 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 16635 << (IsMapTypeImplicit ? 1 : 0) 16636 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 16637 << getOpenMPDirectiveName(DKind); 16638 continue; 16639 } 16640 16641 // target exit_data 16642 // OpenMP [2.10.3, Restrictions, p. 102] 16643 // A map-type must be specified in all map clauses and must be either 16644 // from, release, or delete. 16645 if (DKind == OMPD_target_exit_data && 16646 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release || 16647 MapType == OMPC_MAP_delete)) { 16648 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 16649 << (IsMapTypeImplicit ? 1 : 0) 16650 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 16651 << getOpenMPDirectiveName(DKind); 16652 continue; 16653 } 16654 16655 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 16656 // A list item cannot appear in both a map clause and a data-sharing 16657 // attribute clause on the same construct 16658 // 16659 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7] 16660 // A list item cannot appear in both a map clause and a data-sharing 16661 // attribute clause on the same construct unless the construct is a 16662 // combined construct. 16663 if (VD && ((SemaRef.LangOpts.OpenMP <= 45 && 16664 isOpenMPTargetExecutionDirective(DKind)) || 16665 DKind == OMPD_target)) { 16666 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false); 16667 if (isOpenMPPrivate(DVar.CKind)) { 16668 SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 16669 << getOpenMPClauseName(DVar.CKind) 16670 << getOpenMPClauseName(OMPC_map) 16671 << getOpenMPDirectiveName(DSAS->getCurrentDirective()); 16672 reportOriginalDsa(SemaRef, DSAS, CurDeclaration, DVar); 16673 continue; 16674 } 16675 } 16676 } 16677 16678 // Try to find the associated user-defined mapper. 16679 ExprResult ER = buildUserDefinedMapperRef( 16680 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId, 16681 Type.getCanonicalType(), UnresolvedMapper); 16682 if (ER.isInvalid()) 16683 continue; 16684 MVLI.UDMapperList.push_back(ER.get()); 16685 16686 // Save the current expression. 16687 MVLI.ProcessedVarList.push_back(RE); 16688 16689 // Store the components in the stack so that they can be used to check 16690 // against other clauses later on. 16691 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents, 16692 /*WhereFoundClauseKind=*/OMPC_map); 16693 16694 // Save the components and declaration to create the clause. For purposes of 16695 // the clause creation, any component list that has has base 'this' uses 16696 // null as base declaration. 16697 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 16698 MVLI.VarComponents.back().append(CurComponents.begin(), 16699 CurComponents.end()); 16700 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr 16701 : CurDeclaration); 16702 } 16703 } 16704 16705 OMPClause *Sema::ActOnOpenMPMapClause( 16706 ArrayRef<OpenMPMapModifierKind> MapTypeModifiers, 16707 ArrayRef<SourceLocation> MapTypeModifiersLoc, 16708 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId, 16709 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, SourceLocation MapLoc, 16710 SourceLocation ColonLoc, ArrayRef<Expr *> VarList, 16711 const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) { 16712 OpenMPMapModifierKind Modifiers[] = {OMPC_MAP_MODIFIER_unknown, 16713 OMPC_MAP_MODIFIER_unknown, 16714 OMPC_MAP_MODIFIER_unknown}; 16715 SourceLocation ModifiersLoc[OMPMapClause::NumberOfModifiers]; 16716 16717 // Process map-type-modifiers, flag errors for duplicate modifiers. 16718 unsigned Count = 0; 16719 for (unsigned I = 0, E = MapTypeModifiers.size(); I < E; ++I) { 16720 if (MapTypeModifiers[I] != OMPC_MAP_MODIFIER_unknown && 16721 llvm::find(Modifiers, MapTypeModifiers[I]) != std::end(Modifiers)) { 16722 Diag(MapTypeModifiersLoc[I], diag::err_omp_duplicate_map_type_modifier); 16723 continue; 16724 } 16725 assert(Count < OMPMapClause::NumberOfModifiers && 16726 "Modifiers exceed the allowed number of map type modifiers"); 16727 Modifiers[Count] = MapTypeModifiers[I]; 16728 ModifiersLoc[Count] = MapTypeModifiersLoc[I]; 16729 ++Count; 16730 } 16731 16732 MappableVarListInfo MVLI(VarList); 16733 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, Locs.StartLoc, 16734 MapperIdScopeSpec, MapperId, UnresolvedMappers, 16735 MapType, IsMapTypeImplicit); 16736 16737 // We need to produce a map clause even if we don't have variables so that 16738 // other diagnostics related with non-existing map clauses are accurate. 16739 return OMPMapClause::Create(Context, Locs, MVLI.ProcessedVarList, 16740 MVLI.VarBaseDeclarations, MVLI.VarComponents, 16741 MVLI.UDMapperList, Modifiers, ModifiersLoc, 16742 MapperIdScopeSpec.getWithLocInContext(Context), 16743 MapperId, MapType, IsMapTypeImplicit, MapLoc); 16744 } 16745 16746 QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc, 16747 TypeResult ParsedType) { 16748 assert(ParsedType.isUsable()); 16749 16750 QualType ReductionType = GetTypeFromParser(ParsedType.get()); 16751 if (ReductionType.isNull()) 16752 return QualType(); 16753 16754 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++ 16755 // A type name in a declare reduction directive cannot be a function type, an 16756 // array type, a reference type, or a type qualified with const, volatile or 16757 // restrict. 16758 if (ReductionType.hasQualifiers()) { 16759 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0; 16760 return QualType(); 16761 } 16762 16763 if (ReductionType->isFunctionType()) { 16764 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1; 16765 return QualType(); 16766 } 16767 if (ReductionType->isReferenceType()) { 16768 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2; 16769 return QualType(); 16770 } 16771 if (ReductionType->isArrayType()) { 16772 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3; 16773 return QualType(); 16774 } 16775 return ReductionType; 16776 } 16777 16778 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart( 16779 Scope *S, DeclContext *DC, DeclarationName Name, 16780 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes, 16781 AccessSpecifier AS, Decl *PrevDeclInScope) { 16782 SmallVector<Decl *, 8> Decls; 16783 Decls.reserve(ReductionTypes.size()); 16784 16785 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName, 16786 forRedeclarationInCurContext()); 16787 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions 16788 // A reduction-identifier may not be re-declared in the current scope for the 16789 // same type or for a type that is compatible according to the base language 16790 // rules. 16791 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes; 16792 OMPDeclareReductionDecl *PrevDRD = nullptr; 16793 bool InCompoundScope = true; 16794 if (S != nullptr) { 16795 // Find previous declaration with the same name not referenced in other 16796 // declarations. 16797 FunctionScopeInfo *ParentFn = getEnclosingFunction(); 16798 InCompoundScope = 16799 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty(); 16800 LookupName(Lookup, S); 16801 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false, 16802 /*AllowInlineNamespace=*/false); 16803 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious; 16804 LookupResult::Filter Filter = Lookup.makeFilter(); 16805 while (Filter.hasNext()) { 16806 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next()); 16807 if (InCompoundScope) { 16808 auto I = UsedAsPrevious.find(PrevDecl); 16809 if (I == UsedAsPrevious.end()) 16810 UsedAsPrevious[PrevDecl] = false; 16811 if (OMPDeclareReductionDecl *D = PrevDecl->getPrevDeclInScope()) 16812 UsedAsPrevious[D] = true; 16813 } 16814 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] = 16815 PrevDecl->getLocation(); 16816 } 16817 Filter.done(); 16818 if (InCompoundScope) { 16819 for (const auto &PrevData : UsedAsPrevious) { 16820 if (!PrevData.second) { 16821 PrevDRD = PrevData.first; 16822 break; 16823 } 16824 } 16825 } 16826 } else if (PrevDeclInScope != nullptr) { 16827 auto *PrevDRDInScope = PrevDRD = 16828 cast<OMPDeclareReductionDecl>(PrevDeclInScope); 16829 do { 16830 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] = 16831 PrevDRDInScope->getLocation(); 16832 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope(); 16833 } while (PrevDRDInScope != nullptr); 16834 } 16835 for (const auto &TyData : ReductionTypes) { 16836 const auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType()); 16837 bool Invalid = false; 16838 if (I != PreviousRedeclTypes.end()) { 16839 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition) 16840 << TyData.first; 16841 Diag(I->second, diag::note_previous_definition); 16842 Invalid = true; 16843 } 16844 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second; 16845 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second, 16846 Name, TyData.first, PrevDRD); 16847 DC->addDecl(DRD); 16848 DRD->setAccess(AS); 16849 Decls.push_back(DRD); 16850 if (Invalid) 16851 DRD->setInvalidDecl(); 16852 else 16853 PrevDRD = DRD; 16854 } 16855 16856 return DeclGroupPtrTy::make( 16857 DeclGroupRef::Create(Context, Decls.begin(), Decls.size())); 16858 } 16859 16860 void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) { 16861 auto *DRD = cast<OMPDeclareReductionDecl>(D); 16862 16863 // Enter new function scope. 16864 PushFunctionScope(); 16865 setFunctionHasBranchProtectedScope(); 16866 getCurFunction()->setHasOMPDeclareReductionCombiner(); 16867 16868 if (S != nullptr) 16869 PushDeclContext(S, DRD); 16870 else 16871 CurContext = DRD; 16872 16873 PushExpressionEvaluationContext( 16874 ExpressionEvaluationContext::PotentiallyEvaluated); 16875 16876 QualType ReductionType = DRD->getType(); 16877 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will 16878 // be replaced by '*omp_parm' during codegen. This required because 'omp_in' 16879 // uses semantics of argument handles by value, but it should be passed by 16880 // reference. C lang does not support references, so pass all parameters as 16881 // pointers. 16882 // Create 'T omp_in;' variable. 16883 VarDecl *OmpInParm = 16884 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in"); 16885 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will 16886 // be replaced by '*omp_parm' during codegen. This required because 'omp_out' 16887 // uses semantics of argument handles by value, but it should be passed by 16888 // reference. C lang does not support references, so pass all parameters as 16889 // pointers. 16890 // Create 'T omp_out;' variable. 16891 VarDecl *OmpOutParm = 16892 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out"); 16893 if (S != nullptr) { 16894 PushOnScopeChains(OmpInParm, S); 16895 PushOnScopeChains(OmpOutParm, S); 16896 } else { 16897 DRD->addDecl(OmpInParm); 16898 DRD->addDecl(OmpOutParm); 16899 } 16900 Expr *InE = 16901 ::buildDeclRefExpr(*this, OmpInParm, ReductionType, D->getLocation()); 16902 Expr *OutE = 16903 ::buildDeclRefExpr(*this, OmpOutParm, ReductionType, D->getLocation()); 16904 DRD->setCombinerData(InE, OutE); 16905 } 16906 16907 void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) { 16908 auto *DRD = cast<OMPDeclareReductionDecl>(D); 16909 DiscardCleanupsInEvaluationContext(); 16910 PopExpressionEvaluationContext(); 16911 16912 PopDeclContext(); 16913 PopFunctionScopeInfo(); 16914 16915 if (Combiner != nullptr) 16916 DRD->setCombiner(Combiner); 16917 else 16918 DRD->setInvalidDecl(); 16919 } 16920 16921 VarDecl *Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) { 16922 auto *DRD = cast<OMPDeclareReductionDecl>(D); 16923 16924 // Enter new function scope. 16925 PushFunctionScope(); 16926 setFunctionHasBranchProtectedScope(); 16927 16928 if (S != nullptr) 16929 PushDeclContext(S, DRD); 16930 else 16931 CurContext = DRD; 16932 16933 PushExpressionEvaluationContext( 16934 ExpressionEvaluationContext::PotentiallyEvaluated); 16935 16936 QualType ReductionType = DRD->getType(); 16937 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will 16938 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv' 16939 // uses semantics of argument handles by value, but it should be passed by 16940 // reference. C lang does not support references, so pass all parameters as 16941 // pointers. 16942 // Create 'T omp_priv;' variable. 16943 VarDecl *OmpPrivParm = 16944 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv"); 16945 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will 16946 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig' 16947 // uses semantics of argument handles by value, but it should be passed by 16948 // reference. C lang does not support references, so pass all parameters as 16949 // pointers. 16950 // Create 'T omp_orig;' variable. 16951 VarDecl *OmpOrigParm = 16952 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig"); 16953 if (S != nullptr) { 16954 PushOnScopeChains(OmpPrivParm, S); 16955 PushOnScopeChains(OmpOrigParm, S); 16956 } else { 16957 DRD->addDecl(OmpPrivParm); 16958 DRD->addDecl(OmpOrigParm); 16959 } 16960 Expr *OrigE = 16961 ::buildDeclRefExpr(*this, OmpOrigParm, ReductionType, D->getLocation()); 16962 Expr *PrivE = 16963 ::buildDeclRefExpr(*this, OmpPrivParm, ReductionType, D->getLocation()); 16964 DRD->setInitializerData(OrigE, PrivE); 16965 return OmpPrivParm; 16966 } 16967 16968 void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, Expr *Initializer, 16969 VarDecl *OmpPrivParm) { 16970 auto *DRD = cast<OMPDeclareReductionDecl>(D); 16971 DiscardCleanupsInEvaluationContext(); 16972 PopExpressionEvaluationContext(); 16973 16974 PopDeclContext(); 16975 PopFunctionScopeInfo(); 16976 16977 if (Initializer != nullptr) { 16978 DRD->setInitializer(Initializer, OMPDeclareReductionDecl::CallInit); 16979 } else if (OmpPrivParm->hasInit()) { 16980 DRD->setInitializer(OmpPrivParm->getInit(), 16981 OmpPrivParm->isDirectInit() 16982 ? OMPDeclareReductionDecl::DirectInit 16983 : OMPDeclareReductionDecl::CopyInit); 16984 } else { 16985 DRD->setInvalidDecl(); 16986 } 16987 } 16988 16989 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd( 16990 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) { 16991 for (Decl *D : DeclReductions.get()) { 16992 if (IsValid) { 16993 if (S) 16994 PushOnScopeChains(cast<OMPDeclareReductionDecl>(D), S, 16995 /*AddToContext=*/false); 16996 } else { 16997 D->setInvalidDecl(); 16998 } 16999 } 17000 return DeclReductions; 17001 } 17002 17003 TypeResult Sema::ActOnOpenMPDeclareMapperVarDecl(Scope *S, Declarator &D) { 17004 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); 17005 QualType T = TInfo->getType(); 17006 if (D.isInvalidType()) 17007 return true; 17008 17009 if (getLangOpts().CPlusPlus) { 17010 // Check that there are no default arguments (C++ only). 17011 CheckExtraCXXDefaultArguments(D); 17012 } 17013 17014 return CreateParsedType(T, TInfo); 17015 } 17016 17017 QualType Sema::ActOnOpenMPDeclareMapperType(SourceLocation TyLoc, 17018 TypeResult ParsedType) { 17019 assert(ParsedType.isUsable() && "Expect usable parsed mapper type"); 17020 17021 QualType MapperType = GetTypeFromParser(ParsedType.get()); 17022 assert(!MapperType.isNull() && "Expect valid mapper type"); 17023 17024 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions 17025 // The type must be of struct, union or class type in C and C++ 17026 if (!MapperType->isStructureOrClassType() && !MapperType->isUnionType()) { 17027 Diag(TyLoc, diag::err_omp_mapper_wrong_type); 17028 return QualType(); 17029 } 17030 return MapperType; 17031 } 17032 17033 OMPDeclareMapperDecl *Sema::ActOnOpenMPDeclareMapperDirectiveStart( 17034 Scope *S, DeclContext *DC, DeclarationName Name, QualType MapperType, 17035 SourceLocation StartLoc, DeclarationName VN, AccessSpecifier AS, 17036 Decl *PrevDeclInScope) { 17037 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPMapperName, 17038 forRedeclarationInCurContext()); 17039 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions 17040 // A mapper-identifier may not be redeclared in the current scope for the 17041 // same type or for a type that is compatible according to the base language 17042 // rules. 17043 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes; 17044 OMPDeclareMapperDecl *PrevDMD = nullptr; 17045 bool InCompoundScope = true; 17046 if (S != nullptr) { 17047 // Find previous declaration with the same name not referenced in other 17048 // declarations. 17049 FunctionScopeInfo *ParentFn = getEnclosingFunction(); 17050 InCompoundScope = 17051 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty(); 17052 LookupName(Lookup, S); 17053 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false, 17054 /*AllowInlineNamespace=*/false); 17055 llvm::DenseMap<OMPDeclareMapperDecl *, bool> UsedAsPrevious; 17056 LookupResult::Filter Filter = Lookup.makeFilter(); 17057 while (Filter.hasNext()) { 17058 auto *PrevDecl = cast<OMPDeclareMapperDecl>(Filter.next()); 17059 if (InCompoundScope) { 17060 auto I = UsedAsPrevious.find(PrevDecl); 17061 if (I == UsedAsPrevious.end()) 17062 UsedAsPrevious[PrevDecl] = false; 17063 if (OMPDeclareMapperDecl *D = PrevDecl->getPrevDeclInScope()) 17064 UsedAsPrevious[D] = true; 17065 } 17066 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] = 17067 PrevDecl->getLocation(); 17068 } 17069 Filter.done(); 17070 if (InCompoundScope) { 17071 for (const auto &PrevData : UsedAsPrevious) { 17072 if (!PrevData.second) { 17073 PrevDMD = PrevData.first; 17074 break; 17075 } 17076 } 17077 } 17078 } else if (PrevDeclInScope) { 17079 auto *PrevDMDInScope = PrevDMD = 17080 cast<OMPDeclareMapperDecl>(PrevDeclInScope); 17081 do { 17082 PreviousRedeclTypes[PrevDMDInScope->getType().getCanonicalType()] = 17083 PrevDMDInScope->getLocation(); 17084 PrevDMDInScope = PrevDMDInScope->getPrevDeclInScope(); 17085 } while (PrevDMDInScope != nullptr); 17086 } 17087 const auto I = PreviousRedeclTypes.find(MapperType.getCanonicalType()); 17088 bool Invalid = false; 17089 if (I != PreviousRedeclTypes.end()) { 17090 Diag(StartLoc, diag::err_omp_declare_mapper_redefinition) 17091 << MapperType << Name; 17092 Diag(I->second, diag::note_previous_definition); 17093 Invalid = true; 17094 } 17095 auto *DMD = OMPDeclareMapperDecl::Create(Context, DC, StartLoc, Name, 17096 MapperType, VN, PrevDMD); 17097 DC->addDecl(DMD); 17098 DMD->setAccess(AS); 17099 if (Invalid) 17100 DMD->setInvalidDecl(); 17101 17102 // Enter new function scope. 17103 PushFunctionScope(); 17104 setFunctionHasBranchProtectedScope(); 17105 17106 CurContext = DMD; 17107 17108 return DMD; 17109 } 17110 17111 void Sema::ActOnOpenMPDeclareMapperDirectiveVarDecl(OMPDeclareMapperDecl *DMD, 17112 Scope *S, 17113 QualType MapperType, 17114 SourceLocation StartLoc, 17115 DeclarationName VN) { 17116 VarDecl *VD = buildVarDecl(*this, StartLoc, MapperType, VN.getAsString()); 17117 if (S) 17118 PushOnScopeChains(VD, S); 17119 else 17120 DMD->addDecl(VD); 17121 Expr *MapperVarRefExpr = buildDeclRefExpr(*this, VD, MapperType, StartLoc); 17122 DMD->setMapperVarRef(MapperVarRefExpr); 17123 } 17124 17125 Sema::DeclGroupPtrTy 17126 Sema::ActOnOpenMPDeclareMapperDirectiveEnd(OMPDeclareMapperDecl *D, Scope *S, 17127 ArrayRef<OMPClause *> ClauseList) { 17128 PopDeclContext(); 17129 PopFunctionScopeInfo(); 17130 17131 if (D) { 17132 if (S) 17133 PushOnScopeChains(D, S, /*AddToContext=*/false); 17134 D->CreateClauses(Context, ClauseList); 17135 } 17136 17137 return DeclGroupPtrTy::make(DeclGroupRef(D)); 17138 } 17139 17140 OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams, 17141 SourceLocation StartLoc, 17142 SourceLocation LParenLoc, 17143 SourceLocation EndLoc) { 17144 Expr *ValExpr = NumTeams; 17145 Stmt *HelperValStmt = nullptr; 17146 17147 // OpenMP [teams Constrcut, Restrictions] 17148 // The num_teams expression must evaluate to a positive integer value. 17149 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams, 17150 /*StrictlyPositive=*/true)) 17151 return nullptr; 17152 17153 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 17154 OpenMPDirectiveKind CaptureRegion = 17155 getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams, LangOpts.OpenMP); 17156 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 17157 ValExpr = MakeFullExpr(ValExpr).get(); 17158 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 17159 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 17160 HelperValStmt = buildPreInits(Context, Captures); 17161 } 17162 17163 return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion, 17164 StartLoc, LParenLoc, EndLoc); 17165 } 17166 17167 OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit, 17168 SourceLocation StartLoc, 17169 SourceLocation LParenLoc, 17170 SourceLocation EndLoc) { 17171 Expr *ValExpr = ThreadLimit; 17172 Stmt *HelperValStmt = nullptr; 17173 17174 // OpenMP [teams Constrcut, Restrictions] 17175 // The thread_limit expression must evaluate to a positive integer value. 17176 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit, 17177 /*StrictlyPositive=*/true)) 17178 return nullptr; 17179 17180 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 17181 OpenMPDirectiveKind CaptureRegion = getOpenMPCaptureRegionForClause( 17182 DKind, OMPC_thread_limit, LangOpts.OpenMP); 17183 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 17184 ValExpr = MakeFullExpr(ValExpr).get(); 17185 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 17186 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 17187 HelperValStmt = buildPreInits(Context, Captures); 17188 } 17189 17190 return new (Context) OMPThreadLimitClause( 17191 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc); 17192 } 17193 17194 OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority, 17195 SourceLocation StartLoc, 17196 SourceLocation LParenLoc, 17197 SourceLocation EndLoc) { 17198 Expr *ValExpr = Priority; 17199 Stmt *HelperValStmt = nullptr; 17200 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 17201 17202 // OpenMP [2.9.1, task Constrcut] 17203 // The priority-value is a non-negative numerical scalar expression. 17204 if (!isNonNegativeIntegerValue( 17205 ValExpr, *this, OMPC_priority, 17206 /*StrictlyPositive=*/false, /*BuildCapture=*/true, 17207 DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt)) 17208 return nullptr; 17209 17210 return new (Context) OMPPriorityClause(ValExpr, HelperValStmt, CaptureRegion, 17211 StartLoc, LParenLoc, EndLoc); 17212 } 17213 17214 OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize, 17215 SourceLocation StartLoc, 17216 SourceLocation LParenLoc, 17217 SourceLocation EndLoc) { 17218 Expr *ValExpr = Grainsize; 17219 Stmt *HelperValStmt = nullptr; 17220 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 17221 17222 // OpenMP [2.9.2, taskloop Constrcut] 17223 // The parameter of the grainsize clause must be a positive integer 17224 // expression. 17225 if (!isNonNegativeIntegerValue( 17226 ValExpr, *this, OMPC_grainsize, 17227 /*StrictlyPositive=*/true, /*BuildCapture=*/true, 17228 DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt)) 17229 return nullptr; 17230 17231 return new (Context) OMPGrainsizeClause(ValExpr, HelperValStmt, CaptureRegion, 17232 StartLoc, LParenLoc, EndLoc); 17233 } 17234 17235 OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks, 17236 SourceLocation StartLoc, 17237 SourceLocation LParenLoc, 17238 SourceLocation EndLoc) { 17239 Expr *ValExpr = NumTasks; 17240 Stmt *HelperValStmt = nullptr; 17241 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 17242 17243 // OpenMP [2.9.2, taskloop Constrcut] 17244 // The parameter of the num_tasks clause must be a positive integer 17245 // expression. 17246 if (!isNonNegativeIntegerValue( 17247 ValExpr, *this, OMPC_num_tasks, 17248 /*StrictlyPositive=*/true, /*BuildCapture=*/true, 17249 DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt)) 17250 return nullptr; 17251 17252 return new (Context) OMPNumTasksClause(ValExpr, HelperValStmt, CaptureRegion, 17253 StartLoc, LParenLoc, EndLoc); 17254 } 17255 17256 OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc, 17257 SourceLocation LParenLoc, 17258 SourceLocation EndLoc) { 17259 // OpenMP [2.13.2, critical construct, Description] 17260 // ... where hint-expression is an integer constant expression that evaluates 17261 // to a valid lock hint. 17262 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint); 17263 if (HintExpr.isInvalid()) 17264 return nullptr; 17265 return new (Context) 17266 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc); 17267 } 17268 17269 OMPClause *Sema::ActOnOpenMPDistScheduleClause( 17270 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, 17271 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc, 17272 SourceLocation EndLoc) { 17273 if (Kind == OMPC_DIST_SCHEDULE_unknown) { 17274 std::string Values; 17275 Values += "'"; 17276 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0); 17277 Values += "'"; 17278 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 17279 << Values << getOpenMPClauseName(OMPC_dist_schedule); 17280 return nullptr; 17281 } 17282 Expr *ValExpr = ChunkSize; 17283 Stmt *HelperValStmt = nullptr; 17284 if (ChunkSize) { 17285 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && 17286 !ChunkSize->isInstantiationDependent() && 17287 !ChunkSize->containsUnexpandedParameterPack()) { 17288 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc(); 17289 ExprResult Val = 17290 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); 17291 if (Val.isInvalid()) 17292 return nullptr; 17293 17294 ValExpr = Val.get(); 17295 17296 // OpenMP [2.7.1, Restrictions] 17297 // chunk_size must be a loop invariant integer expression with a positive 17298 // value. 17299 llvm::APSInt Result; 17300 if (ValExpr->isIntegerConstantExpr(Result, Context)) { 17301 if (Result.isSigned() && !Result.isStrictlyPositive()) { 17302 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) 17303 << "dist_schedule" << ChunkSize->getSourceRange(); 17304 return nullptr; 17305 } 17306 } else if (getOpenMPCaptureRegionForClause( 17307 DSAStack->getCurrentDirective(), OMPC_dist_schedule, 17308 LangOpts.OpenMP) != OMPD_unknown && 17309 !CurContext->isDependentContext()) { 17310 ValExpr = MakeFullExpr(ValExpr).get(); 17311 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 17312 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 17313 HelperValStmt = buildPreInits(Context, Captures); 17314 } 17315 } 17316 } 17317 17318 return new (Context) 17319 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, 17320 Kind, ValExpr, HelperValStmt); 17321 } 17322 17323 OMPClause *Sema::ActOnOpenMPDefaultmapClause( 17324 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind, 17325 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc, 17326 SourceLocation KindLoc, SourceLocation EndLoc) { 17327 if (getLangOpts().OpenMP < 50) { 17328 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || 17329 Kind != OMPC_DEFAULTMAP_scalar) { 17330 std::string Value; 17331 SourceLocation Loc; 17332 Value += "'"; 17333 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) { 17334 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap, 17335 OMPC_DEFAULTMAP_MODIFIER_tofrom); 17336 Loc = MLoc; 17337 } else { 17338 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap, 17339 OMPC_DEFAULTMAP_scalar); 17340 Loc = KindLoc; 17341 } 17342 Value += "'"; 17343 Diag(Loc, diag::err_omp_unexpected_clause_value) 17344 << Value << getOpenMPClauseName(OMPC_defaultmap); 17345 return nullptr; 17346 } 17347 } else { 17348 bool isDefaultmapModifier = (M != OMPC_DEFAULTMAP_MODIFIER_unknown); 17349 bool isDefaultmapKind = (Kind != OMPC_DEFAULTMAP_unknown); 17350 if (!isDefaultmapKind || !isDefaultmapModifier) { 17351 std::string ModifierValue = "'alloc', 'from', 'to', 'tofrom', " 17352 "'firstprivate', 'none', 'default'"; 17353 std::string KindValue = "'scalar', 'aggregate', 'pointer'"; 17354 if (!isDefaultmapKind && isDefaultmapModifier) { 17355 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 17356 << KindValue << getOpenMPClauseName(OMPC_defaultmap); 17357 } else if (isDefaultmapKind && !isDefaultmapModifier) { 17358 Diag(MLoc, diag::err_omp_unexpected_clause_value) 17359 << ModifierValue << getOpenMPClauseName(OMPC_defaultmap); 17360 } else { 17361 Diag(MLoc, diag::err_omp_unexpected_clause_value) 17362 << ModifierValue << getOpenMPClauseName(OMPC_defaultmap); 17363 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 17364 << KindValue << getOpenMPClauseName(OMPC_defaultmap); 17365 } 17366 return nullptr; 17367 } 17368 17369 // OpenMP [5.0, 2.12.5, Restrictions, p. 174] 17370 // At most one defaultmap clause for each category can appear on the 17371 // directive. 17372 if (DSAStack->checkDefaultmapCategory(Kind)) { 17373 Diag(StartLoc, diag::err_omp_one_defaultmap_each_category); 17374 return nullptr; 17375 } 17376 } 17377 DSAStack->setDefaultDMAAttr(M, Kind, StartLoc); 17378 17379 return new (Context) 17380 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M); 17381 } 17382 17383 bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) { 17384 DeclContext *CurLexicalContext = getCurLexicalContext(); 17385 if (!CurLexicalContext->isFileContext() && 17386 !CurLexicalContext->isExternCContext() && 17387 !CurLexicalContext->isExternCXXContext() && 17388 !isa<CXXRecordDecl>(CurLexicalContext) && 17389 !isa<ClassTemplateDecl>(CurLexicalContext) && 17390 !isa<ClassTemplatePartialSpecializationDecl>(CurLexicalContext) && 17391 !isa<ClassTemplateSpecializationDecl>(CurLexicalContext)) { 17392 Diag(Loc, diag::err_omp_region_not_file_context); 17393 return false; 17394 } 17395 ++DeclareTargetNestingLevel; 17396 return true; 17397 } 17398 17399 void Sema::ActOnFinishOpenMPDeclareTargetDirective() { 17400 assert(DeclareTargetNestingLevel > 0 && 17401 "Unexpected ActOnFinishOpenMPDeclareTargetDirective"); 17402 --DeclareTargetNestingLevel; 17403 } 17404 17405 NamedDecl * 17406 Sema::lookupOpenMPDeclareTargetName(Scope *CurScope, CXXScopeSpec &ScopeSpec, 17407 const DeclarationNameInfo &Id, 17408 NamedDeclSetType &SameDirectiveDecls) { 17409 LookupResult Lookup(*this, Id, LookupOrdinaryName); 17410 LookupParsedName(Lookup, CurScope, &ScopeSpec, true); 17411 17412 if (Lookup.isAmbiguous()) 17413 return nullptr; 17414 Lookup.suppressDiagnostics(); 17415 17416 if (!Lookup.isSingleResult()) { 17417 VarOrFuncDeclFilterCCC CCC(*this); 17418 if (TypoCorrection Corrected = 17419 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, CCC, 17420 CTK_ErrorRecovery)) { 17421 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest) 17422 << Id.getName()); 17423 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl()); 17424 return nullptr; 17425 } 17426 17427 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName(); 17428 return nullptr; 17429 } 17430 17431 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>(); 17432 if (!isa<VarDecl>(ND) && !isa<FunctionDecl>(ND) && 17433 !isa<FunctionTemplateDecl>(ND)) { 17434 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName(); 17435 return nullptr; 17436 } 17437 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl()))) 17438 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName(); 17439 return ND; 17440 } 17441 17442 void Sema::ActOnOpenMPDeclareTargetName( 17443 NamedDecl *ND, SourceLocation Loc, OMPDeclareTargetDeclAttr::MapTypeTy MT, 17444 OMPDeclareTargetDeclAttr::DevTypeTy DT) { 17445 assert((isa<VarDecl>(ND) || isa<FunctionDecl>(ND) || 17446 isa<FunctionTemplateDecl>(ND)) && 17447 "Expected variable, function or function template."); 17448 17449 // Diagnose marking after use as it may lead to incorrect diagnosis and 17450 // codegen. 17451 if (LangOpts.OpenMP >= 50 && 17452 (ND->isUsed(/*CheckUsedAttr=*/false) || ND->isReferenced())) 17453 Diag(Loc, diag::warn_omp_declare_target_after_first_use); 17454 17455 Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy = 17456 OMPDeclareTargetDeclAttr::getDeviceType(cast<ValueDecl>(ND)); 17457 if (DevTy.hasValue() && *DevTy != DT) { 17458 Diag(Loc, diag::err_omp_device_type_mismatch) 17459 << OMPDeclareTargetDeclAttr::ConvertDevTypeTyToStr(DT) 17460 << OMPDeclareTargetDeclAttr::ConvertDevTypeTyToStr(*DevTy); 17461 return; 17462 } 17463 Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 17464 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(cast<ValueDecl>(ND)); 17465 if (!Res) { 17466 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT, DT, 17467 SourceRange(Loc, Loc)); 17468 ND->addAttr(A); 17469 if (ASTMutationListener *ML = Context.getASTMutationListener()) 17470 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A); 17471 checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Loc); 17472 } else if (*Res != MT) { 17473 Diag(Loc, diag::err_omp_declare_target_to_and_link) << ND; 17474 } 17475 } 17476 17477 static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR, 17478 Sema &SemaRef, Decl *D) { 17479 if (!D || !isa<VarDecl>(D)) 17480 return; 17481 auto *VD = cast<VarDecl>(D); 17482 Optional<OMPDeclareTargetDeclAttr::MapTypeTy> MapTy = 17483 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); 17484 if (SemaRef.LangOpts.OpenMP >= 50 && 17485 (SemaRef.getCurLambda(/*IgnoreNonLambdaCapturingScope=*/true) || 17486 SemaRef.getCurBlock() || SemaRef.getCurCapturedRegion()) && 17487 VD->hasGlobalStorage()) { 17488 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> MapTy = 17489 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); 17490 if (!MapTy || *MapTy != OMPDeclareTargetDeclAttr::MT_To) { 17491 // OpenMP 5.0, 2.12.7 declare target Directive, Restrictions 17492 // If a lambda declaration and definition appears between a 17493 // declare target directive and the matching end declare target 17494 // directive, all variables that are captured by the lambda 17495 // expression must also appear in a to clause. 17496 SemaRef.Diag(VD->getLocation(), 17497 diag::err_omp_lambda_capture_in_declare_target_not_to); 17498 SemaRef.Diag(SL, diag::note_var_explicitly_captured_here) 17499 << VD << 0 << SR; 17500 return; 17501 } 17502 } 17503 if (MapTy.hasValue()) 17504 return; 17505 SemaRef.Diag(VD->getLocation(), diag::warn_omp_not_in_target_context); 17506 SemaRef.Diag(SL, diag::note_used_here) << SR; 17507 } 17508 17509 static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR, 17510 Sema &SemaRef, DSAStackTy *Stack, 17511 ValueDecl *VD) { 17512 return OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD) || 17513 checkTypeMappable(SL, SR, SemaRef, Stack, VD->getType(), 17514 /*FullCheck=*/false); 17515 } 17516 17517 void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D, 17518 SourceLocation IdLoc) { 17519 if (!D || D->isInvalidDecl()) 17520 return; 17521 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange(); 17522 SourceLocation SL = E ? E->getBeginLoc() : D->getLocation(); 17523 if (auto *VD = dyn_cast<VarDecl>(D)) { 17524 // Only global variables can be marked as declare target. 17525 if (!VD->isFileVarDecl() && !VD->isStaticLocal() && 17526 !VD->isStaticDataMember()) 17527 return; 17528 // 2.10.6: threadprivate variable cannot appear in a declare target 17529 // directive. 17530 if (DSAStack->isThreadPrivate(VD)) { 17531 Diag(SL, diag::err_omp_threadprivate_in_target); 17532 reportOriginalDsa(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false)); 17533 return; 17534 } 17535 } 17536 if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(D)) 17537 D = FTD->getTemplatedDecl(); 17538 if (auto *FD = dyn_cast<FunctionDecl>(D)) { 17539 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 17540 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(FD); 17541 if (IdLoc.isValid() && Res && *Res == OMPDeclareTargetDeclAttr::MT_Link) { 17542 Diag(IdLoc, diag::err_omp_function_in_link_clause); 17543 Diag(FD->getLocation(), diag::note_defined_here) << FD; 17544 return; 17545 } 17546 // Mark the function as must be emitted for the device. 17547 Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy = 17548 OMPDeclareTargetDeclAttr::getDeviceType(FD); 17549 if (LangOpts.OpenMPIsDevice && Res.hasValue() && IdLoc.isValid() && 17550 *DevTy != OMPDeclareTargetDeclAttr::DT_Host) 17551 checkOpenMPDeviceFunction(IdLoc, FD, /*CheckForDelayedContext=*/false); 17552 if (!LangOpts.OpenMPIsDevice && Res.hasValue() && IdLoc.isValid() && 17553 *DevTy != OMPDeclareTargetDeclAttr::DT_NoHost) 17554 checkOpenMPHostFunction(IdLoc, FD, /*CheckCaller=*/false); 17555 } 17556 if (auto *VD = dyn_cast<ValueDecl>(D)) { 17557 // Problem if any with var declared with incomplete type will be reported 17558 // as normal, so no need to check it here. 17559 if ((E || !VD->getType()->isIncompleteType()) && 17560 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) 17561 return; 17562 if (!E && !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) { 17563 // Checking declaration inside declare target region. 17564 if (isa<VarDecl>(D) || isa<FunctionDecl>(D) || 17565 isa<FunctionTemplateDecl>(D)) { 17566 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit( 17567 Context, OMPDeclareTargetDeclAttr::MT_To, 17568 OMPDeclareTargetDeclAttr::DT_Any, SourceRange(IdLoc, IdLoc)); 17569 D->addAttr(A); 17570 if (ASTMutationListener *ML = Context.getASTMutationListener()) 17571 ML->DeclarationMarkedOpenMPDeclareTarget(D, A); 17572 } 17573 return; 17574 } 17575 } 17576 if (!E) 17577 return; 17578 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D); 17579 } 17580 17581 OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList, 17582 CXXScopeSpec &MapperIdScopeSpec, 17583 DeclarationNameInfo &MapperId, 17584 const OMPVarListLocTy &Locs, 17585 ArrayRef<Expr *> UnresolvedMappers) { 17586 MappableVarListInfo MVLI(VarList); 17587 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, Locs.StartLoc, 17588 MapperIdScopeSpec, MapperId, UnresolvedMappers); 17589 if (MVLI.ProcessedVarList.empty()) 17590 return nullptr; 17591 17592 return OMPToClause::Create( 17593 Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, 17594 MVLI.VarComponents, MVLI.UDMapperList, 17595 MapperIdScopeSpec.getWithLocInContext(Context), MapperId); 17596 } 17597 17598 OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList, 17599 CXXScopeSpec &MapperIdScopeSpec, 17600 DeclarationNameInfo &MapperId, 17601 const OMPVarListLocTy &Locs, 17602 ArrayRef<Expr *> UnresolvedMappers) { 17603 MappableVarListInfo MVLI(VarList); 17604 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, Locs.StartLoc, 17605 MapperIdScopeSpec, MapperId, UnresolvedMappers); 17606 if (MVLI.ProcessedVarList.empty()) 17607 return nullptr; 17608 17609 return OMPFromClause::Create( 17610 Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, 17611 MVLI.VarComponents, MVLI.UDMapperList, 17612 MapperIdScopeSpec.getWithLocInContext(Context), MapperId); 17613 } 17614 17615 OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList, 17616 const OMPVarListLocTy &Locs) { 17617 MappableVarListInfo MVLI(VarList); 17618 SmallVector<Expr *, 8> PrivateCopies; 17619 SmallVector<Expr *, 8> Inits; 17620 17621 for (Expr *RefExpr : VarList) { 17622 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause."); 17623 SourceLocation ELoc; 17624 SourceRange ERange; 17625 Expr *SimpleRefExpr = RefExpr; 17626 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 17627 if (Res.second) { 17628 // It will be analyzed later. 17629 MVLI.ProcessedVarList.push_back(RefExpr); 17630 PrivateCopies.push_back(nullptr); 17631 Inits.push_back(nullptr); 17632 } 17633 ValueDecl *D = Res.first; 17634 if (!D) 17635 continue; 17636 17637 QualType Type = D->getType(); 17638 Type = Type.getNonReferenceType().getUnqualifiedType(); 17639 17640 auto *VD = dyn_cast<VarDecl>(D); 17641 17642 // Item should be a pointer or reference to pointer. 17643 if (!Type->isPointerType()) { 17644 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer) 17645 << 0 << RefExpr->getSourceRange(); 17646 continue; 17647 } 17648 17649 // Build the private variable and the expression that refers to it. 17650 auto VDPrivate = 17651 buildVarDecl(*this, ELoc, Type, D->getName(), 17652 D->hasAttrs() ? &D->getAttrs() : nullptr, 17653 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 17654 if (VDPrivate->isInvalidDecl()) 17655 continue; 17656 17657 CurContext->addDecl(VDPrivate); 17658 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 17659 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc); 17660 17661 // Add temporary variable to initialize the private copy of the pointer. 17662 VarDecl *VDInit = 17663 buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp"); 17664 DeclRefExpr *VDInitRefExpr = buildDeclRefExpr( 17665 *this, VDInit, RefExpr->getType(), RefExpr->getExprLoc()); 17666 AddInitializerToDecl(VDPrivate, 17667 DefaultLvalueConversion(VDInitRefExpr).get(), 17668 /*DirectInit=*/false); 17669 17670 // If required, build a capture to implement the privatization initialized 17671 // with the current list item value. 17672 DeclRefExpr *Ref = nullptr; 17673 if (!VD) 17674 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 17675 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref); 17676 PrivateCopies.push_back(VDPrivateRefExpr); 17677 Inits.push_back(VDInitRefExpr); 17678 17679 // We need to add a data sharing attribute for this variable to make sure it 17680 // is correctly captured. A variable that shows up in a use_device_ptr has 17681 // similar properties of a first private variable. 17682 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 17683 17684 // Create a mappable component for the list item. List items in this clause 17685 // only need a component. 17686 MVLI.VarBaseDeclarations.push_back(D); 17687 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 17688 MVLI.VarComponents.back().push_back( 17689 OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D)); 17690 } 17691 17692 if (MVLI.ProcessedVarList.empty()) 17693 return nullptr; 17694 17695 return OMPUseDevicePtrClause::Create( 17696 Context, Locs, MVLI.ProcessedVarList, PrivateCopies, Inits, 17697 MVLI.VarBaseDeclarations, MVLI.VarComponents); 17698 } 17699 17700 OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList, 17701 const OMPVarListLocTy &Locs) { 17702 MappableVarListInfo MVLI(VarList); 17703 for (Expr *RefExpr : VarList) { 17704 assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause."); 17705 SourceLocation ELoc; 17706 SourceRange ERange; 17707 Expr *SimpleRefExpr = RefExpr; 17708 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 17709 if (Res.second) { 17710 // It will be analyzed later. 17711 MVLI.ProcessedVarList.push_back(RefExpr); 17712 } 17713 ValueDecl *D = Res.first; 17714 if (!D) 17715 continue; 17716 17717 QualType Type = D->getType(); 17718 // item should be a pointer or array or reference to pointer or array 17719 if (!Type.getNonReferenceType()->isPointerType() && 17720 !Type.getNonReferenceType()->isArrayType()) { 17721 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr) 17722 << 0 << RefExpr->getSourceRange(); 17723 continue; 17724 } 17725 17726 // Check if the declaration in the clause does not show up in any data 17727 // sharing attribute. 17728 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 17729 if (isOpenMPPrivate(DVar.CKind)) { 17730 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 17731 << getOpenMPClauseName(DVar.CKind) 17732 << getOpenMPClauseName(OMPC_is_device_ptr) 17733 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 17734 reportOriginalDsa(*this, DSAStack, D, DVar); 17735 continue; 17736 } 17737 17738 const Expr *ConflictExpr; 17739 if (DSAStack->checkMappableExprComponentListsForDecl( 17740 D, /*CurrentRegionOnly=*/true, 17741 [&ConflictExpr]( 17742 OMPClauseMappableExprCommon::MappableExprComponentListRef R, 17743 OpenMPClauseKind) -> bool { 17744 ConflictExpr = R.front().getAssociatedExpression(); 17745 return true; 17746 })) { 17747 Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange(); 17748 Diag(ConflictExpr->getExprLoc(), diag::note_used_here) 17749 << ConflictExpr->getSourceRange(); 17750 continue; 17751 } 17752 17753 // Store the components in the stack so that they can be used to check 17754 // against other clauses later on. 17755 OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D); 17756 DSAStack->addMappableExpressionComponents( 17757 D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr); 17758 17759 // Record the expression we've just processed. 17760 MVLI.ProcessedVarList.push_back(SimpleRefExpr); 17761 17762 // Create a mappable component for the list item. List items in this clause 17763 // only need a component. We use a null declaration to signal fields in 17764 // 'this'. 17765 assert((isa<DeclRefExpr>(SimpleRefExpr) || 17766 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) && 17767 "Unexpected device pointer expression!"); 17768 MVLI.VarBaseDeclarations.push_back( 17769 isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr); 17770 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 17771 MVLI.VarComponents.back().push_back(MC); 17772 } 17773 17774 if (MVLI.ProcessedVarList.empty()) 17775 return nullptr; 17776 17777 return OMPIsDevicePtrClause::Create(Context, Locs, MVLI.ProcessedVarList, 17778 MVLI.VarBaseDeclarations, 17779 MVLI.VarComponents); 17780 } 17781 17782 OMPClause *Sema::ActOnOpenMPAllocateClause( 17783 Expr *Allocator, ArrayRef<Expr *> VarList, SourceLocation StartLoc, 17784 SourceLocation ColonLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 17785 if (Allocator) { 17786 // OpenMP [2.11.4 allocate Clause, Description] 17787 // allocator is an expression of omp_allocator_handle_t type. 17788 if (!findOMPAllocatorHandleT(*this, Allocator->getExprLoc(), DSAStack)) 17789 return nullptr; 17790 17791 ExprResult AllocatorRes = DefaultLvalueConversion(Allocator); 17792 if (AllocatorRes.isInvalid()) 17793 return nullptr; 17794 AllocatorRes = PerformImplicitConversion(AllocatorRes.get(), 17795 DSAStack->getOMPAllocatorHandleT(), 17796 Sema::AA_Initializing, 17797 /*AllowExplicit=*/true); 17798 if (AllocatorRes.isInvalid()) 17799 return nullptr; 17800 Allocator = AllocatorRes.get(); 17801 } else { 17802 // OpenMP 5.0, 2.11.4 allocate Clause, Restrictions. 17803 // allocate clauses that appear on a target construct or on constructs in a 17804 // target region must specify an allocator expression unless a requires 17805 // directive with the dynamic_allocators clause is present in the same 17806 // compilation unit. 17807 if (LangOpts.OpenMPIsDevice && 17808 !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>()) 17809 targetDiag(StartLoc, diag::err_expected_allocator_expression); 17810 } 17811 // Analyze and build list of variables. 17812 SmallVector<Expr *, 8> Vars; 17813 for (Expr *RefExpr : VarList) { 17814 assert(RefExpr && "NULL expr in OpenMP private clause."); 17815 SourceLocation ELoc; 17816 SourceRange ERange; 17817 Expr *SimpleRefExpr = RefExpr; 17818 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 17819 if (Res.second) { 17820 // It will be analyzed later. 17821 Vars.push_back(RefExpr); 17822 } 17823 ValueDecl *D = Res.first; 17824 if (!D) 17825 continue; 17826 17827 auto *VD = dyn_cast<VarDecl>(D); 17828 DeclRefExpr *Ref = nullptr; 17829 if (!VD && !CurContext->isDependentContext()) 17830 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 17831 Vars.push_back((VD || CurContext->isDependentContext()) 17832 ? RefExpr->IgnoreParens() 17833 : Ref); 17834 } 17835 17836 if (Vars.empty()) 17837 return nullptr; 17838 17839 if (Allocator) 17840 DSAStack->addInnerAllocatorExpr(Allocator); 17841 return OMPAllocateClause::Create(Context, StartLoc, LParenLoc, Allocator, 17842 ColonLoc, EndLoc, Vars); 17843 } 17844 17845 OMPClause *Sema::ActOnOpenMPNontemporalClause(ArrayRef<Expr *> VarList, 17846 SourceLocation StartLoc, 17847 SourceLocation LParenLoc, 17848 SourceLocation EndLoc) { 17849 SmallVector<Expr *, 8> Vars; 17850 for (Expr *RefExpr : VarList) { 17851 assert(RefExpr && "NULL expr in OpenMP nontemporal clause."); 17852 SourceLocation ELoc; 17853 SourceRange ERange; 17854 Expr *SimpleRefExpr = RefExpr; 17855 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 17856 if (Res.second) 17857 // It will be analyzed later. 17858 Vars.push_back(RefExpr); 17859 ValueDecl *D = Res.first; 17860 if (!D) 17861 continue; 17862 17863 // OpenMP 5.0, 2.9.3.1 simd Construct, Restrictions. 17864 // A list-item cannot appear in more than one nontemporal clause. 17865 if (const Expr *PrevRef = 17866 DSAStack->addUniqueNontemporal(D, SimpleRefExpr)) { 17867 Diag(ELoc, diag::err_omp_used_in_clause_twice) 17868 << 0 << getOpenMPClauseName(OMPC_nontemporal) << ERange; 17869 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa) 17870 << getOpenMPClauseName(OMPC_nontemporal); 17871 continue; 17872 } 17873 17874 Vars.push_back(RefExpr); 17875 } 17876 17877 if (Vars.empty()) 17878 return nullptr; 17879 17880 return OMPNontemporalClause::Create(Context, StartLoc, LParenLoc, EndLoc, 17881 Vars); 17882 } 17883