1 //===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 /// \file 9 /// This file implements semantic analysis for OpenMP directives and 10 /// clauses. 11 /// 12 //===----------------------------------------------------------------------===// 13 14 #include "TreeTransform.h" 15 #include "clang/AST/ASTContext.h" 16 #include "clang/AST/ASTMutationListener.h" 17 #include "clang/AST/CXXInheritance.h" 18 #include "clang/AST/Decl.h" 19 #include "clang/AST/DeclCXX.h" 20 #include "clang/AST/DeclOpenMP.h" 21 #include "clang/AST/OpenMPClause.h" 22 #include "clang/AST/StmtCXX.h" 23 #include "clang/AST/StmtOpenMP.h" 24 #include "clang/AST/StmtVisitor.h" 25 #include "clang/AST/TypeOrdering.h" 26 #include "clang/Basic/DiagnosticSema.h" 27 #include "clang/Basic/OpenMPKinds.h" 28 #include "clang/Basic/PartialDiagnostic.h" 29 #include "clang/Basic/TargetInfo.h" 30 #include "clang/Sema/Initialization.h" 31 #include "clang/Sema/Lookup.h" 32 #include "clang/Sema/Scope.h" 33 #include "clang/Sema/ScopeInfo.h" 34 #include "clang/Sema/SemaInternal.h" 35 #include "llvm/ADT/IndexedMap.h" 36 #include "llvm/ADT/PointerEmbeddedInt.h" 37 #include "llvm/ADT/STLExtras.h" 38 #include "llvm/ADT/StringExtras.h" 39 #include "llvm/Frontend/OpenMP/OMPConstants.h" 40 #include <set> 41 42 using namespace clang; 43 using namespace llvm::omp; 44 45 //===----------------------------------------------------------------------===// 46 // Stack of data-sharing attributes for variables 47 //===----------------------------------------------------------------------===// 48 49 static const Expr *checkMapClauseExpressionBase( 50 Sema &SemaRef, Expr *E, 51 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents, 52 OpenMPClauseKind CKind, OpenMPDirectiveKind DKind, bool NoDiagnose); 53 54 namespace { 55 /// Default data sharing attributes, which can be applied to directive. 56 enum DefaultDataSharingAttributes { 57 DSA_unspecified = 0, /// Data sharing attribute not specified. 58 DSA_none = 1 << 0, /// Default data sharing attribute 'none'. 59 DSA_shared = 1 << 1, /// Default data sharing attribute 'shared'. 60 DSA_firstprivate = 1 << 2, /// Default data sharing attribute 'firstprivate'. 61 }; 62 63 /// Stack for tracking declarations used in OpenMP directives and 64 /// clauses and their data-sharing attributes. 65 class DSAStackTy { 66 public: 67 struct DSAVarData { 68 OpenMPDirectiveKind DKind = OMPD_unknown; 69 OpenMPClauseKind CKind = OMPC_unknown; 70 unsigned Modifier = 0; 71 const Expr *RefExpr = nullptr; 72 DeclRefExpr *PrivateCopy = nullptr; 73 SourceLocation ImplicitDSALoc; 74 bool AppliedToPointee = false; 75 DSAVarData() = default; 76 DSAVarData(OpenMPDirectiveKind DKind, OpenMPClauseKind CKind, 77 const Expr *RefExpr, DeclRefExpr *PrivateCopy, 78 SourceLocation ImplicitDSALoc, unsigned Modifier, 79 bool AppliedToPointee) 80 : DKind(DKind), CKind(CKind), Modifier(Modifier), RefExpr(RefExpr), 81 PrivateCopy(PrivateCopy), ImplicitDSALoc(ImplicitDSALoc), 82 AppliedToPointee(AppliedToPointee) {} 83 }; 84 using OperatorOffsetTy = 85 llvm::SmallVector<std::pair<Expr *, OverloadedOperatorKind>, 4>; 86 using DoacrossDependMapTy = 87 llvm::DenseMap<OMPDependClause *, OperatorOffsetTy>; 88 /// Kind of the declaration used in the uses_allocators clauses. 89 enum class UsesAllocatorsDeclKind { 90 /// Predefined allocator 91 PredefinedAllocator, 92 /// User-defined allocator 93 UserDefinedAllocator, 94 /// The declaration that represent allocator trait 95 AllocatorTrait, 96 }; 97 98 private: 99 struct DSAInfo { 100 OpenMPClauseKind Attributes = OMPC_unknown; 101 unsigned Modifier = 0; 102 /// Pointer to a reference expression and a flag which shows that the 103 /// variable is marked as lastprivate(true) or not (false). 104 llvm::PointerIntPair<const Expr *, 1, bool> RefExpr; 105 DeclRefExpr *PrivateCopy = nullptr; 106 /// true if the attribute is applied to the pointee, not the variable 107 /// itself. 108 bool AppliedToPointee = false; 109 }; 110 using DeclSAMapTy = llvm::SmallDenseMap<const ValueDecl *, DSAInfo, 8>; 111 using UsedRefMapTy = llvm::SmallDenseMap<const ValueDecl *, const Expr *, 8>; 112 using LCDeclInfo = std::pair<unsigned, VarDecl *>; 113 using LoopControlVariablesMapTy = 114 llvm::SmallDenseMap<const ValueDecl *, LCDeclInfo, 8>; 115 /// Struct that associates a component with the clause kind where they are 116 /// found. 117 struct MappedExprComponentTy { 118 OMPClauseMappableExprCommon::MappableExprComponentLists Components; 119 OpenMPClauseKind Kind = OMPC_unknown; 120 }; 121 using MappedExprComponentsTy = 122 llvm::DenseMap<const ValueDecl *, MappedExprComponentTy>; 123 using CriticalsWithHintsTy = 124 llvm::StringMap<std::pair<const OMPCriticalDirective *, llvm::APSInt>>; 125 struct ReductionData { 126 using BOKPtrType = llvm::PointerEmbeddedInt<BinaryOperatorKind, 16>; 127 SourceRange ReductionRange; 128 llvm::PointerUnion<const Expr *, BOKPtrType> ReductionOp; 129 ReductionData() = default; 130 void set(BinaryOperatorKind BO, SourceRange RR) { 131 ReductionRange = RR; 132 ReductionOp = BO; 133 } 134 void set(const Expr *RefExpr, SourceRange RR) { 135 ReductionRange = RR; 136 ReductionOp = RefExpr; 137 } 138 }; 139 using DeclReductionMapTy = 140 llvm::SmallDenseMap<const ValueDecl *, ReductionData, 4>; 141 struct DefaultmapInfo { 142 OpenMPDefaultmapClauseModifier ImplicitBehavior = 143 OMPC_DEFAULTMAP_MODIFIER_unknown; 144 SourceLocation SLoc; 145 DefaultmapInfo() = default; 146 DefaultmapInfo(OpenMPDefaultmapClauseModifier M, SourceLocation Loc) 147 : ImplicitBehavior(M), SLoc(Loc) {} 148 }; 149 150 struct SharingMapTy { 151 DeclSAMapTy SharingMap; 152 DeclReductionMapTy ReductionMap; 153 UsedRefMapTy AlignedMap; 154 UsedRefMapTy NontemporalMap; 155 MappedExprComponentsTy MappedExprComponents; 156 LoopControlVariablesMapTy LCVMap; 157 DefaultDataSharingAttributes DefaultAttr = DSA_unspecified; 158 SourceLocation DefaultAttrLoc; 159 DefaultmapInfo DefaultmapMap[OMPC_DEFAULTMAP_unknown]; 160 OpenMPDirectiveKind Directive = OMPD_unknown; 161 DeclarationNameInfo DirectiveName; 162 Scope *CurScope = nullptr; 163 DeclContext *Context = nullptr; 164 SourceLocation ConstructLoc; 165 /// Set of 'depend' clauses with 'sink|source' dependence kind. Required to 166 /// get the data (loop counters etc.) about enclosing loop-based construct. 167 /// This data is required during codegen. 168 DoacrossDependMapTy DoacrossDepends; 169 /// First argument (Expr *) contains optional argument of the 170 /// 'ordered' clause, the second one is true if the regions has 'ordered' 171 /// clause, false otherwise. 172 llvm::Optional<std::pair<const Expr *, OMPOrderedClause *>> OrderedRegion; 173 unsigned AssociatedLoops = 1; 174 bool HasMutipleLoops = false; 175 const Decl *PossiblyLoopCounter = nullptr; 176 bool NowaitRegion = false; 177 bool CancelRegion = false; 178 bool LoopStart = false; 179 bool BodyComplete = false; 180 SourceLocation PrevScanLocation; 181 SourceLocation PrevOrderedLocation; 182 SourceLocation InnerTeamsRegionLoc; 183 /// Reference to the taskgroup task_reduction reference expression. 184 Expr *TaskgroupReductionRef = nullptr; 185 llvm::DenseSet<QualType> MappedClassesQualTypes; 186 SmallVector<Expr *, 4> InnerUsedAllocators; 187 llvm::DenseSet<CanonicalDeclPtr<Decl>> ImplicitTaskFirstprivates; 188 /// List of globals marked as declare target link in this target region 189 /// (isOpenMPTargetExecutionDirective(Directive) == true). 190 llvm::SmallVector<DeclRefExpr *, 4> DeclareTargetLinkVarDecls; 191 /// List of decls used in inclusive/exclusive clauses of the scan directive. 192 llvm::DenseSet<CanonicalDeclPtr<Decl>> UsedInScanDirective; 193 llvm::DenseMap<CanonicalDeclPtr<const Decl>, UsesAllocatorsDeclKind> 194 UsesAllocatorsDecls; 195 Expr *DeclareMapperVar = nullptr; 196 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name, 197 Scope *CurScope, SourceLocation Loc) 198 : Directive(DKind), DirectiveName(Name), CurScope(CurScope), 199 ConstructLoc(Loc) {} 200 SharingMapTy() = default; 201 }; 202 203 using StackTy = SmallVector<SharingMapTy, 4>; 204 205 /// Stack of used declaration and their data-sharing attributes. 206 DeclSAMapTy Threadprivates; 207 const FunctionScopeInfo *CurrentNonCapturingFunctionScope = nullptr; 208 SmallVector<std::pair<StackTy, const FunctionScopeInfo *>, 4> Stack; 209 /// true, if check for DSA must be from parent directive, false, if 210 /// from current directive. 211 OpenMPClauseKind ClauseKindMode = OMPC_unknown; 212 Sema &SemaRef; 213 bool ForceCapturing = false; 214 /// true if all the variables in the target executable directives must be 215 /// captured by reference. 216 bool ForceCaptureByReferenceInTargetExecutable = false; 217 CriticalsWithHintsTy Criticals; 218 unsigned IgnoredStackElements = 0; 219 220 /// Iterators over the stack iterate in order from innermost to outermost 221 /// directive. 222 using const_iterator = StackTy::const_reverse_iterator; 223 const_iterator begin() const { 224 return Stack.empty() ? const_iterator() 225 : Stack.back().first.rbegin() + IgnoredStackElements; 226 } 227 const_iterator end() const { 228 return Stack.empty() ? const_iterator() : Stack.back().first.rend(); 229 } 230 using iterator = StackTy::reverse_iterator; 231 iterator begin() { 232 return Stack.empty() ? iterator() 233 : Stack.back().first.rbegin() + IgnoredStackElements; 234 } 235 iterator end() { 236 return Stack.empty() ? iterator() : Stack.back().first.rend(); 237 } 238 239 // Convenience operations to get at the elements of the stack. 240 241 bool isStackEmpty() const { 242 return Stack.empty() || 243 Stack.back().second != CurrentNonCapturingFunctionScope || 244 Stack.back().first.size() <= IgnoredStackElements; 245 } 246 size_t getStackSize() const { 247 return isStackEmpty() ? 0 248 : Stack.back().first.size() - IgnoredStackElements; 249 } 250 251 SharingMapTy *getTopOfStackOrNull() { 252 size_t Size = getStackSize(); 253 if (Size == 0) 254 return nullptr; 255 return &Stack.back().first[Size - 1]; 256 } 257 const SharingMapTy *getTopOfStackOrNull() const { 258 return const_cast<DSAStackTy&>(*this).getTopOfStackOrNull(); 259 } 260 SharingMapTy &getTopOfStack() { 261 assert(!isStackEmpty() && "no current directive"); 262 return *getTopOfStackOrNull(); 263 } 264 const SharingMapTy &getTopOfStack() const { 265 return const_cast<DSAStackTy&>(*this).getTopOfStack(); 266 } 267 268 SharingMapTy *getSecondOnStackOrNull() { 269 size_t Size = getStackSize(); 270 if (Size <= 1) 271 return nullptr; 272 return &Stack.back().first[Size - 2]; 273 } 274 const SharingMapTy *getSecondOnStackOrNull() const { 275 return const_cast<DSAStackTy&>(*this).getSecondOnStackOrNull(); 276 } 277 278 /// Get the stack element at a certain level (previously returned by 279 /// \c getNestingLevel). 280 /// 281 /// Note that nesting levels count from outermost to innermost, and this is 282 /// the reverse of our iteration order where new inner levels are pushed at 283 /// the front of the stack. 284 SharingMapTy &getStackElemAtLevel(unsigned Level) { 285 assert(Level < getStackSize() && "no such stack element"); 286 return Stack.back().first[Level]; 287 } 288 const SharingMapTy &getStackElemAtLevel(unsigned Level) const { 289 return const_cast<DSAStackTy&>(*this).getStackElemAtLevel(Level); 290 } 291 292 DSAVarData getDSA(const_iterator &Iter, ValueDecl *D) const; 293 294 /// Checks if the variable is a local for OpenMP region. 295 bool isOpenMPLocal(VarDecl *D, const_iterator Iter) const; 296 297 /// Vector of previously declared requires directives 298 SmallVector<const OMPRequiresDecl *, 2> RequiresDecls; 299 /// omp_allocator_handle_t type. 300 QualType OMPAllocatorHandleT; 301 /// omp_depend_t type. 302 QualType OMPDependT; 303 /// omp_event_handle_t type. 304 QualType OMPEventHandleT; 305 /// omp_alloctrait_t type. 306 QualType OMPAlloctraitT; 307 /// Expression for the predefined allocators. 308 Expr *OMPPredefinedAllocators[OMPAllocateDeclAttr::OMPUserDefinedMemAlloc] = { 309 nullptr}; 310 /// Vector of previously encountered target directives 311 SmallVector<SourceLocation, 2> TargetLocations; 312 SourceLocation AtomicLocation; 313 314 public: 315 explicit DSAStackTy(Sema &S) : SemaRef(S) {} 316 317 /// Sets omp_allocator_handle_t type. 318 void setOMPAllocatorHandleT(QualType Ty) { OMPAllocatorHandleT = Ty; } 319 /// Gets omp_allocator_handle_t type. 320 QualType getOMPAllocatorHandleT() const { return OMPAllocatorHandleT; } 321 /// Sets omp_alloctrait_t type. 322 void setOMPAlloctraitT(QualType Ty) { OMPAlloctraitT = Ty; } 323 /// Gets omp_alloctrait_t type. 324 QualType getOMPAlloctraitT() const { return OMPAlloctraitT; } 325 /// Sets the given default allocator. 326 void setAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, 327 Expr *Allocator) { 328 OMPPredefinedAllocators[AllocatorKind] = Allocator; 329 } 330 /// Returns the specified default allocator. 331 Expr *getAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind) const { 332 return OMPPredefinedAllocators[AllocatorKind]; 333 } 334 /// Sets omp_depend_t type. 335 void setOMPDependT(QualType Ty) { OMPDependT = Ty; } 336 /// Gets omp_depend_t type. 337 QualType getOMPDependT() const { return OMPDependT; } 338 339 /// Sets omp_event_handle_t type. 340 void setOMPEventHandleT(QualType Ty) { OMPEventHandleT = Ty; } 341 /// Gets omp_event_handle_t type. 342 QualType getOMPEventHandleT() const { return OMPEventHandleT; } 343 344 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; } 345 OpenMPClauseKind getClauseParsingMode() const { 346 assert(isClauseParsingMode() && "Must be in clause parsing mode."); 347 return ClauseKindMode; 348 } 349 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; } 350 351 bool isBodyComplete() const { 352 const SharingMapTy *Top = getTopOfStackOrNull(); 353 return Top && Top->BodyComplete; 354 } 355 void setBodyComplete() { 356 getTopOfStack().BodyComplete = true; 357 } 358 359 bool isForceVarCapturing() const { return ForceCapturing; } 360 void setForceVarCapturing(bool V) { ForceCapturing = V; } 361 362 void setForceCaptureByReferenceInTargetExecutable(bool V) { 363 ForceCaptureByReferenceInTargetExecutable = V; 364 } 365 bool isForceCaptureByReferenceInTargetExecutable() const { 366 return ForceCaptureByReferenceInTargetExecutable; 367 } 368 369 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName, 370 Scope *CurScope, SourceLocation Loc) { 371 assert(!IgnoredStackElements && 372 "cannot change stack while ignoring elements"); 373 if (Stack.empty() || 374 Stack.back().second != CurrentNonCapturingFunctionScope) 375 Stack.emplace_back(StackTy(), CurrentNonCapturingFunctionScope); 376 Stack.back().first.emplace_back(DKind, DirName, CurScope, Loc); 377 Stack.back().first.back().DefaultAttrLoc = Loc; 378 } 379 380 void pop() { 381 assert(!IgnoredStackElements && 382 "cannot change stack while ignoring elements"); 383 assert(!Stack.back().first.empty() && 384 "Data-sharing attributes stack is empty!"); 385 Stack.back().first.pop_back(); 386 } 387 388 /// RAII object to temporarily leave the scope of a directive when we want to 389 /// logically operate in its parent. 390 class ParentDirectiveScope { 391 DSAStackTy &Self; 392 bool Active; 393 public: 394 ParentDirectiveScope(DSAStackTy &Self, bool Activate) 395 : Self(Self), Active(false) { 396 if (Activate) 397 enable(); 398 } 399 ~ParentDirectiveScope() { disable(); } 400 void disable() { 401 if (Active) { 402 --Self.IgnoredStackElements; 403 Active = false; 404 } 405 } 406 void enable() { 407 if (!Active) { 408 ++Self.IgnoredStackElements; 409 Active = true; 410 } 411 } 412 }; 413 414 /// Marks that we're started loop parsing. 415 void loopInit() { 416 assert(isOpenMPLoopDirective(getCurrentDirective()) && 417 "Expected loop-based directive."); 418 getTopOfStack().LoopStart = true; 419 } 420 /// Start capturing of the variables in the loop context. 421 void loopStart() { 422 assert(isOpenMPLoopDirective(getCurrentDirective()) && 423 "Expected loop-based directive."); 424 getTopOfStack().LoopStart = false; 425 } 426 /// true, if variables are captured, false otherwise. 427 bool isLoopStarted() const { 428 assert(isOpenMPLoopDirective(getCurrentDirective()) && 429 "Expected loop-based directive."); 430 return !getTopOfStack().LoopStart; 431 } 432 /// Marks (or clears) declaration as possibly loop counter. 433 void resetPossibleLoopCounter(const Decl *D = nullptr) { 434 getTopOfStack().PossiblyLoopCounter = 435 D ? D->getCanonicalDecl() : D; 436 } 437 /// Gets the possible loop counter decl. 438 const Decl *getPossiblyLoopCunter() const { 439 return getTopOfStack().PossiblyLoopCounter; 440 } 441 /// Start new OpenMP region stack in new non-capturing function. 442 void pushFunction() { 443 assert(!IgnoredStackElements && 444 "cannot change stack while ignoring elements"); 445 const FunctionScopeInfo *CurFnScope = SemaRef.getCurFunction(); 446 assert(!isa<CapturingScopeInfo>(CurFnScope)); 447 CurrentNonCapturingFunctionScope = CurFnScope; 448 } 449 /// Pop region stack for non-capturing function. 450 void popFunction(const FunctionScopeInfo *OldFSI) { 451 assert(!IgnoredStackElements && 452 "cannot change stack while ignoring elements"); 453 if (!Stack.empty() && Stack.back().second == OldFSI) { 454 assert(Stack.back().first.empty()); 455 Stack.pop_back(); 456 } 457 CurrentNonCapturingFunctionScope = nullptr; 458 for (const FunctionScopeInfo *FSI : llvm::reverse(SemaRef.FunctionScopes)) { 459 if (!isa<CapturingScopeInfo>(FSI)) { 460 CurrentNonCapturingFunctionScope = FSI; 461 break; 462 } 463 } 464 } 465 466 void addCriticalWithHint(const OMPCriticalDirective *D, llvm::APSInt Hint) { 467 Criticals.try_emplace(D->getDirectiveName().getAsString(), D, Hint); 468 } 469 const std::pair<const OMPCriticalDirective *, llvm::APSInt> 470 getCriticalWithHint(const DeclarationNameInfo &Name) const { 471 auto I = Criticals.find(Name.getAsString()); 472 if (I != Criticals.end()) 473 return I->second; 474 return std::make_pair(nullptr, llvm::APSInt()); 475 } 476 /// If 'aligned' declaration for given variable \a D was not seen yet, 477 /// add it and return NULL; otherwise return previous occurrence's expression 478 /// for diagnostics. 479 const Expr *addUniqueAligned(const ValueDecl *D, const Expr *NewDE); 480 /// If 'nontemporal' declaration for given variable \a D was not seen yet, 481 /// add it and return NULL; otherwise return previous occurrence's expression 482 /// for diagnostics. 483 const Expr *addUniqueNontemporal(const ValueDecl *D, const Expr *NewDE); 484 485 /// Register specified variable as loop control variable. 486 void addLoopControlVariable(const ValueDecl *D, VarDecl *Capture); 487 /// Check if the specified variable is a loop control variable for 488 /// current region. 489 /// \return The index of the loop control variable in the list of associated 490 /// for-loops (from outer to inner). 491 const LCDeclInfo isLoopControlVariable(const ValueDecl *D) const; 492 /// Check if the specified variable is a loop control variable for 493 /// parent region. 494 /// \return The index of the loop control variable in the list of associated 495 /// for-loops (from outer to inner). 496 const LCDeclInfo isParentLoopControlVariable(const ValueDecl *D) const; 497 /// Check if the specified variable is a loop control variable for 498 /// current region. 499 /// \return The index of the loop control variable in the list of associated 500 /// for-loops (from outer to inner). 501 const LCDeclInfo isLoopControlVariable(const ValueDecl *D, 502 unsigned Level) const; 503 /// Get the loop control variable for the I-th loop (or nullptr) in 504 /// parent directive. 505 const ValueDecl *getParentLoopControlVariable(unsigned I) const; 506 507 /// Marks the specified decl \p D as used in scan directive. 508 void markDeclAsUsedInScanDirective(ValueDecl *D) { 509 if (SharingMapTy *Stack = getSecondOnStackOrNull()) 510 Stack->UsedInScanDirective.insert(D); 511 } 512 513 /// Checks if the specified declaration was used in the inner scan directive. 514 bool isUsedInScanDirective(ValueDecl *D) const { 515 if (const SharingMapTy *Stack = getTopOfStackOrNull()) 516 return Stack->UsedInScanDirective.count(D) > 0; 517 return false; 518 } 519 520 /// Adds explicit data sharing attribute to the specified declaration. 521 void addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A, 522 DeclRefExpr *PrivateCopy = nullptr, unsigned Modifier = 0, 523 bool AppliedToPointee = false); 524 525 /// Adds additional information for the reduction items with the reduction id 526 /// represented as an operator. 527 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 528 BinaryOperatorKind BOK); 529 /// Adds additional information for the reduction items with the reduction id 530 /// represented as reduction identifier. 531 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 532 const Expr *ReductionRef); 533 /// Returns the location and reduction operation from the innermost parent 534 /// region for the given \p D. 535 const DSAVarData 536 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR, 537 BinaryOperatorKind &BOK, 538 Expr *&TaskgroupDescriptor) const; 539 /// Returns the location and reduction operation from the innermost parent 540 /// region for the given \p D. 541 const DSAVarData 542 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR, 543 const Expr *&ReductionRef, 544 Expr *&TaskgroupDescriptor) const; 545 /// Return reduction reference expression for the current taskgroup or 546 /// parallel/worksharing directives with task reductions. 547 Expr *getTaskgroupReductionRef() const { 548 assert((getTopOfStack().Directive == OMPD_taskgroup || 549 ((isOpenMPParallelDirective(getTopOfStack().Directive) || 550 isOpenMPWorksharingDirective(getTopOfStack().Directive)) && 551 !isOpenMPSimdDirective(getTopOfStack().Directive))) && 552 "taskgroup reference expression requested for non taskgroup or " 553 "parallel/worksharing directive."); 554 return getTopOfStack().TaskgroupReductionRef; 555 } 556 /// Checks if the given \p VD declaration is actually a taskgroup reduction 557 /// descriptor variable at the \p Level of OpenMP regions. 558 bool isTaskgroupReductionRef(const ValueDecl *VD, unsigned Level) const { 559 return getStackElemAtLevel(Level).TaskgroupReductionRef && 560 cast<DeclRefExpr>(getStackElemAtLevel(Level).TaskgroupReductionRef) 561 ->getDecl() == VD; 562 } 563 564 /// Returns data sharing attributes from top of the stack for the 565 /// specified declaration. 566 const DSAVarData getTopDSA(ValueDecl *D, bool FromParent); 567 /// Returns data-sharing attributes for the specified declaration. 568 const DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent) const; 569 /// Returns data-sharing attributes for the specified declaration. 570 const DSAVarData getImplicitDSA(ValueDecl *D, unsigned Level) const; 571 /// Checks if the specified variables has data-sharing attributes which 572 /// match specified \a CPred predicate in any directive which matches \a DPred 573 /// predicate. 574 const DSAVarData 575 hasDSA(ValueDecl *D, 576 const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred, 577 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 578 bool FromParent) const; 579 /// Checks if the specified variables has data-sharing attributes which 580 /// match specified \a CPred predicate in any innermost directive which 581 /// matches \a DPred predicate. 582 const DSAVarData 583 hasInnermostDSA(ValueDecl *D, 584 const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred, 585 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 586 bool FromParent) const; 587 /// Checks if the specified variables has explicit data-sharing 588 /// attributes which match specified \a CPred predicate at the specified 589 /// OpenMP region. 590 bool 591 hasExplicitDSA(const ValueDecl *D, 592 const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred, 593 unsigned Level, bool NotLastprivate = false) const; 594 595 /// Returns true if the directive at level \Level matches in the 596 /// specified \a DPred predicate. 597 bool hasExplicitDirective( 598 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 599 unsigned Level) const; 600 601 /// Finds a directive which matches specified \a DPred predicate. 602 bool hasDirective( 603 const llvm::function_ref<bool( 604 OpenMPDirectiveKind, const DeclarationNameInfo &, SourceLocation)> 605 DPred, 606 bool FromParent) const; 607 608 /// Returns currently analyzed directive. 609 OpenMPDirectiveKind getCurrentDirective() const { 610 const SharingMapTy *Top = getTopOfStackOrNull(); 611 return Top ? Top->Directive : OMPD_unknown; 612 } 613 /// Returns directive kind at specified level. 614 OpenMPDirectiveKind getDirective(unsigned Level) const { 615 assert(!isStackEmpty() && "No directive at specified level."); 616 return getStackElemAtLevel(Level).Directive; 617 } 618 /// Returns the capture region at the specified level. 619 OpenMPDirectiveKind getCaptureRegion(unsigned Level, 620 unsigned OpenMPCaptureLevel) const { 621 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 622 getOpenMPCaptureRegions(CaptureRegions, getDirective(Level)); 623 return CaptureRegions[OpenMPCaptureLevel]; 624 } 625 /// Returns parent directive. 626 OpenMPDirectiveKind getParentDirective() const { 627 const SharingMapTy *Parent = getSecondOnStackOrNull(); 628 return Parent ? Parent->Directive : OMPD_unknown; 629 } 630 631 /// Add requires decl to internal vector 632 void addRequiresDecl(OMPRequiresDecl *RD) { 633 RequiresDecls.push_back(RD); 634 } 635 636 /// Checks if the defined 'requires' directive has specified type of clause. 637 template <typename ClauseType> 638 bool hasRequiresDeclWithClause() const { 639 return llvm::any_of(RequiresDecls, [](const OMPRequiresDecl *D) { 640 return llvm::any_of(D->clauselists(), [](const OMPClause *C) { 641 return isa<ClauseType>(C); 642 }); 643 }); 644 } 645 646 /// Checks for a duplicate clause amongst previously declared requires 647 /// directives 648 bool hasDuplicateRequiresClause(ArrayRef<OMPClause *> ClauseList) const { 649 bool IsDuplicate = false; 650 for (OMPClause *CNew : ClauseList) { 651 for (const OMPRequiresDecl *D : RequiresDecls) { 652 for (const OMPClause *CPrev : D->clauselists()) { 653 if (CNew->getClauseKind() == CPrev->getClauseKind()) { 654 SemaRef.Diag(CNew->getBeginLoc(), 655 diag::err_omp_requires_clause_redeclaration) 656 << getOpenMPClauseName(CNew->getClauseKind()); 657 SemaRef.Diag(CPrev->getBeginLoc(), 658 diag::note_omp_requires_previous_clause) 659 << getOpenMPClauseName(CPrev->getClauseKind()); 660 IsDuplicate = true; 661 } 662 } 663 } 664 } 665 return IsDuplicate; 666 } 667 668 /// Add location of previously encountered target to internal vector 669 void addTargetDirLocation(SourceLocation LocStart) { 670 TargetLocations.push_back(LocStart); 671 } 672 673 /// Add location for the first encountered atomicc directive. 674 void addAtomicDirectiveLoc(SourceLocation Loc) { 675 if (AtomicLocation.isInvalid()) 676 AtomicLocation = Loc; 677 } 678 679 /// Returns the location of the first encountered atomic directive in the 680 /// module. 681 SourceLocation getAtomicDirectiveLoc() const { 682 return AtomicLocation; 683 } 684 685 // Return previously encountered target region locations. 686 ArrayRef<SourceLocation> getEncounteredTargetLocs() const { 687 return TargetLocations; 688 } 689 690 /// Set default data sharing attribute to none. 691 void setDefaultDSANone(SourceLocation Loc) { 692 getTopOfStack().DefaultAttr = DSA_none; 693 getTopOfStack().DefaultAttrLoc = Loc; 694 } 695 /// Set default data sharing attribute to shared. 696 void setDefaultDSAShared(SourceLocation Loc) { 697 getTopOfStack().DefaultAttr = DSA_shared; 698 getTopOfStack().DefaultAttrLoc = Loc; 699 } 700 /// Set default data sharing attribute to firstprivate. 701 void setDefaultDSAFirstPrivate(SourceLocation Loc) { 702 getTopOfStack().DefaultAttr = DSA_firstprivate; 703 getTopOfStack().DefaultAttrLoc = Loc; 704 } 705 /// Set default data mapping attribute to Modifier:Kind 706 void setDefaultDMAAttr(OpenMPDefaultmapClauseModifier M, 707 OpenMPDefaultmapClauseKind Kind, 708 SourceLocation Loc) { 709 DefaultmapInfo &DMI = getTopOfStack().DefaultmapMap[Kind]; 710 DMI.ImplicitBehavior = M; 711 DMI.SLoc = Loc; 712 } 713 /// Check whether the implicit-behavior has been set in defaultmap 714 bool checkDefaultmapCategory(OpenMPDefaultmapClauseKind VariableCategory) { 715 if (VariableCategory == OMPC_DEFAULTMAP_unknown) 716 return getTopOfStack() 717 .DefaultmapMap[OMPC_DEFAULTMAP_aggregate] 718 .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown || 719 getTopOfStack() 720 .DefaultmapMap[OMPC_DEFAULTMAP_scalar] 721 .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown || 722 getTopOfStack() 723 .DefaultmapMap[OMPC_DEFAULTMAP_pointer] 724 .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown; 725 return getTopOfStack().DefaultmapMap[VariableCategory].ImplicitBehavior != 726 OMPC_DEFAULTMAP_MODIFIER_unknown; 727 } 728 729 DefaultDataSharingAttributes getDefaultDSA(unsigned Level) const { 730 return getStackSize() <= Level ? DSA_unspecified 731 : getStackElemAtLevel(Level).DefaultAttr; 732 } 733 DefaultDataSharingAttributes getDefaultDSA() const { 734 return isStackEmpty() ? DSA_unspecified 735 : getTopOfStack().DefaultAttr; 736 } 737 SourceLocation getDefaultDSALocation() const { 738 return isStackEmpty() ? SourceLocation() 739 : getTopOfStack().DefaultAttrLoc; 740 } 741 OpenMPDefaultmapClauseModifier 742 getDefaultmapModifier(OpenMPDefaultmapClauseKind Kind) const { 743 return isStackEmpty() 744 ? OMPC_DEFAULTMAP_MODIFIER_unknown 745 : getTopOfStack().DefaultmapMap[Kind].ImplicitBehavior; 746 } 747 OpenMPDefaultmapClauseModifier 748 getDefaultmapModifierAtLevel(unsigned Level, 749 OpenMPDefaultmapClauseKind Kind) const { 750 return getStackElemAtLevel(Level).DefaultmapMap[Kind].ImplicitBehavior; 751 } 752 bool isDefaultmapCapturedByRef(unsigned Level, 753 OpenMPDefaultmapClauseKind Kind) const { 754 OpenMPDefaultmapClauseModifier M = 755 getDefaultmapModifierAtLevel(Level, Kind); 756 if (Kind == OMPC_DEFAULTMAP_scalar || Kind == OMPC_DEFAULTMAP_pointer) { 757 return (M == OMPC_DEFAULTMAP_MODIFIER_alloc) || 758 (M == OMPC_DEFAULTMAP_MODIFIER_to) || 759 (M == OMPC_DEFAULTMAP_MODIFIER_from) || 760 (M == OMPC_DEFAULTMAP_MODIFIER_tofrom); 761 } 762 return true; 763 } 764 static bool mustBeFirstprivateBase(OpenMPDefaultmapClauseModifier M, 765 OpenMPDefaultmapClauseKind Kind) { 766 switch (Kind) { 767 case OMPC_DEFAULTMAP_scalar: 768 case OMPC_DEFAULTMAP_pointer: 769 return (M == OMPC_DEFAULTMAP_MODIFIER_unknown) || 770 (M == OMPC_DEFAULTMAP_MODIFIER_firstprivate) || 771 (M == OMPC_DEFAULTMAP_MODIFIER_default); 772 case OMPC_DEFAULTMAP_aggregate: 773 return M == OMPC_DEFAULTMAP_MODIFIER_firstprivate; 774 default: 775 break; 776 } 777 llvm_unreachable("Unexpected OpenMPDefaultmapClauseKind enum"); 778 } 779 bool mustBeFirstprivateAtLevel(unsigned Level, 780 OpenMPDefaultmapClauseKind Kind) const { 781 OpenMPDefaultmapClauseModifier M = 782 getDefaultmapModifierAtLevel(Level, Kind); 783 return mustBeFirstprivateBase(M, Kind); 784 } 785 bool mustBeFirstprivate(OpenMPDefaultmapClauseKind Kind) const { 786 OpenMPDefaultmapClauseModifier M = getDefaultmapModifier(Kind); 787 return mustBeFirstprivateBase(M, Kind); 788 } 789 790 /// Checks if the specified variable is a threadprivate. 791 bool isThreadPrivate(VarDecl *D) { 792 const DSAVarData DVar = getTopDSA(D, false); 793 return isOpenMPThreadPrivate(DVar.CKind); 794 } 795 796 /// Marks current region as ordered (it has an 'ordered' clause). 797 void setOrderedRegion(bool IsOrdered, const Expr *Param, 798 OMPOrderedClause *Clause) { 799 if (IsOrdered) 800 getTopOfStack().OrderedRegion.emplace(Param, Clause); 801 else 802 getTopOfStack().OrderedRegion.reset(); 803 } 804 /// Returns true, if region is ordered (has associated 'ordered' clause), 805 /// false - otherwise. 806 bool isOrderedRegion() const { 807 if (const SharingMapTy *Top = getTopOfStackOrNull()) 808 return Top->OrderedRegion.hasValue(); 809 return false; 810 } 811 /// Returns optional parameter for the ordered region. 812 std::pair<const Expr *, OMPOrderedClause *> getOrderedRegionParam() const { 813 if (const SharingMapTy *Top = getTopOfStackOrNull()) 814 if (Top->OrderedRegion.hasValue()) 815 return Top->OrderedRegion.getValue(); 816 return std::make_pair(nullptr, nullptr); 817 } 818 /// Returns true, if parent region is ordered (has associated 819 /// 'ordered' clause), false - otherwise. 820 bool isParentOrderedRegion() const { 821 if (const SharingMapTy *Parent = getSecondOnStackOrNull()) 822 return Parent->OrderedRegion.hasValue(); 823 return false; 824 } 825 /// Returns optional parameter for the ordered region. 826 std::pair<const Expr *, OMPOrderedClause *> 827 getParentOrderedRegionParam() const { 828 if (const SharingMapTy *Parent = getSecondOnStackOrNull()) 829 if (Parent->OrderedRegion.hasValue()) 830 return Parent->OrderedRegion.getValue(); 831 return std::make_pair(nullptr, nullptr); 832 } 833 /// Marks current region as nowait (it has a 'nowait' clause). 834 void setNowaitRegion(bool IsNowait = true) { 835 getTopOfStack().NowaitRegion = IsNowait; 836 } 837 /// Returns true, if parent region is nowait (has associated 838 /// 'nowait' clause), false - otherwise. 839 bool isParentNowaitRegion() const { 840 if (const SharingMapTy *Parent = getSecondOnStackOrNull()) 841 return Parent->NowaitRegion; 842 return false; 843 } 844 /// Marks parent region as cancel region. 845 void setParentCancelRegion(bool Cancel = true) { 846 if (SharingMapTy *Parent = getSecondOnStackOrNull()) 847 Parent->CancelRegion |= Cancel; 848 } 849 /// Return true if current region has inner cancel construct. 850 bool isCancelRegion() const { 851 const SharingMapTy *Top = getTopOfStackOrNull(); 852 return Top ? Top->CancelRegion : false; 853 } 854 855 /// Mark that parent region already has scan directive. 856 void setParentHasScanDirective(SourceLocation Loc) { 857 if (SharingMapTy *Parent = getSecondOnStackOrNull()) 858 Parent->PrevScanLocation = Loc; 859 } 860 /// Return true if current region has inner cancel construct. 861 bool doesParentHasScanDirective() const { 862 const SharingMapTy *Top = getSecondOnStackOrNull(); 863 return Top ? Top->PrevScanLocation.isValid() : false; 864 } 865 /// Return true if current region has inner cancel construct. 866 SourceLocation getParentScanDirectiveLoc() const { 867 const SharingMapTy *Top = getSecondOnStackOrNull(); 868 return Top ? Top->PrevScanLocation : SourceLocation(); 869 } 870 /// Mark that parent region already has ordered directive. 871 void setParentHasOrderedDirective(SourceLocation Loc) { 872 if (SharingMapTy *Parent = getSecondOnStackOrNull()) 873 Parent->PrevOrderedLocation = Loc; 874 } 875 /// Return true if current region has inner ordered construct. 876 bool doesParentHasOrderedDirective() const { 877 const SharingMapTy *Top = getSecondOnStackOrNull(); 878 return Top ? Top->PrevOrderedLocation.isValid() : false; 879 } 880 /// Returns the location of the previously specified ordered directive. 881 SourceLocation getParentOrderedDirectiveLoc() const { 882 const SharingMapTy *Top = getSecondOnStackOrNull(); 883 return Top ? Top->PrevOrderedLocation : SourceLocation(); 884 } 885 886 /// Set collapse value for the region. 887 void setAssociatedLoops(unsigned Val) { 888 getTopOfStack().AssociatedLoops = Val; 889 if (Val > 1) 890 getTopOfStack().HasMutipleLoops = true; 891 } 892 /// Return collapse value for region. 893 unsigned getAssociatedLoops() const { 894 const SharingMapTy *Top = getTopOfStackOrNull(); 895 return Top ? Top->AssociatedLoops : 0; 896 } 897 /// Returns true if the construct is associated with multiple loops. 898 bool hasMutipleLoops() const { 899 const SharingMapTy *Top = getTopOfStackOrNull(); 900 return Top ? Top->HasMutipleLoops : false; 901 } 902 903 /// Marks current target region as one with closely nested teams 904 /// region. 905 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) { 906 if (SharingMapTy *Parent = getSecondOnStackOrNull()) 907 Parent->InnerTeamsRegionLoc = TeamsRegionLoc; 908 } 909 /// Returns true, if current region has closely nested teams region. 910 bool hasInnerTeamsRegion() const { 911 return getInnerTeamsRegionLoc().isValid(); 912 } 913 /// Returns location of the nested teams region (if any). 914 SourceLocation getInnerTeamsRegionLoc() const { 915 const SharingMapTy *Top = getTopOfStackOrNull(); 916 return Top ? Top->InnerTeamsRegionLoc : SourceLocation(); 917 } 918 919 Scope *getCurScope() const { 920 const SharingMapTy *Top = getTopOfStackOrNull(); 921 return Top ? Top->CurScope : nullptr; 922 } 923 void setContext(DeclContext *DC) { getTopOfStack().Context = DC; } 924 SourceLocation getConstructLoc() const { 925 const SharingMapTy *Top = getTopOfStackOrNull(); 926 return Top ? Top->ConstructLoc : SourceLocation(); 927 } 928 929 /// Do the check specified in \a Check to all component lists and return true 930 /// if any issue is found. 931 bool checkMappableExprComponentListsForDecl( 932 const ValueDecl *VD, bool CurrentRegionOnly, 933 const llvm::function_ref< 934 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef, 935 OpenMPClauseKind)> 936 Check) const { 937 if (isStackEmpty()) 938 return false; 939 auto SI = begin(); 940 auto SE = end(); 941 942 if (SI == SE) 943 return false; 944 945 if (CurrentRegionOnly) 946 SE = std::next(SI); 947 else 948 std::advance(SI, 1); 949 950 for (; SI != SE; ++SI) { 951 auto MI = SI->MappedExprComponents.find(VD); 952 if (MI != SI->MappedExprComponents.end()) 953 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L : 954 MI->second.Components) 955 if (Check(L, MI->second.Kind)) 956 return true; 957 } 958 return false; 959 } 960 961 /// Do the check specified in \a Check to all component lists at a given level 962 /// and return true if any issue is found. 963 bool checkMappableExprComponentListsForDeclAtLevel( 964 const ValueDecl *VD, unsigned Level, 965 const llvm::function_ref< 966 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef, 967 OpenMPClauseKind)> 968 Check) const { 969 if (getStackSize() <= Level) 970 return false; 971 972 const SharingMapTy &StackElem = getStackElemAtLevel(Level); 973 auto MI = StackElem.MappedExprComponents.find(VD); 974 if (MI != StackElem.MappedExprComponents.end()) 975 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L : 976 MI->second.Components) 977 if (Check(L, MI->second.Kind)) 978 return true; 979 return false; 980 } 981 982 /// Create a new mappable expression component list associated with a given 983 /// declaration and initialize it with the provided list of components. 984 void addMappableExpressionComponents( 985 const ValueDecl *VD, 986 OMPClauseMappableExprCommon::MappableExprComponentListRef Components, 987 OpenMPClauseKind WhereFoundClauseKind) { 988 MappedExprComponentTy &MEC = getTopOfStack().MappedExprComponents[VD]; 989 // Create new entry and append the new components there. 990 MEC.Components.resize(MEC.Components.size() + 1); 991 MEC.Components.back().append(Components.begin(), Components.end()); 992 MEC.Kind = WhereFoundClauseKind; 993 } 994 995 unsigned getNestingLevel() const { 996 assert(!isStackEmpty()); 997 return getStackSize() - 1; 998 } 999 void addDoacrossDependClause(OMPDependClause *C, 1000 const OperatorOffsetTy &OpsOffs) { 1001 SharingMapTy *Parent = getSecondOnStackOrNull(); 1002 assert(Parent && isOpenMPWorksharingDirective(Parent->Directive)); 1003 Parent->DoacrossDepends.try_emplace(C, OpsOffs); 1004 } 1005 llvm::iterator_range<DoacrossDependMapTy::const_iterator> 1006 getDoacrossDependClauses() const { 1007 const SharingMapTy &StackElem = getTopOfStack(); 1008 if (isOpenMPWorksharingDirective(StackElem.Directive)) { 1009 const DoacrossDependMapTy &Ref = StackElem.DoacrossDepends; 1010 return llvm::make_range(Ref.begin(), Ref.end()); 1011 } 1012 return llvm::make_range(StackElem.DoacrossDepends.end(), 1013 StackElem.DoacrossDepends.end()); 1014 } 1015 1016 // Store types of classes which have been explicitly mapped 1017 void addMappedClassesQualTypes(QualType QT) { 1018 SharingMapTy &StackElem = getTopOfStack(); 1019 StackElem.MappedClassesQualTypes.insert(QT); 1020 } 1021 1022 // Return set of mapped classes types 1023 bool isClassPreviouslyMapped(QualType QT) const { 1024 const SharingMapTy &StackElem = getTopOfStack(); 1025 return StackElem.MappedClassesQualTypes.count(QT) != 0; 1026 } 1027 1028 /// Adds global declare target to the parent target region. 1029 void addToParentTargetRegionLinkGlobals(DeclRefExpr *E) { 1030 assert(*OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration( 1031 E->getDecl()) == OMPDeclareTargetDeclAttr::MT_Link && 1032 "Expected declare target link global."); 1033 for (auto &Elem : *this) { 1034 if (isOpenMPTargetExecutionDirective(Elem.Directive)) { 1035 Elem.DeclareTargetLinkVarDecls.push_back(E); 1036 return; 1037 } 1038 } 1039 } 1040 1041 /// Returns the list of globals with declare target link if current directive 1042 /// is target. 1043 ArrayRef<DeclRefExpr *> getLinkGlobals() const { 1044 assert(isOpenMPTargetExecutionDirective(getCurrentDirective()) && 1045 "Expected target executable directive."); 1046 return getTopOfStack().DeclareTargetLinkVarDecls; 1047 } 1048 1049 /// Adds list of allocators expressions. 1050 void addInnerAllocatorExpr(Expr *E) { 1051 getTopOfStack().InnerUsedAllocators.push_back(E); 1052 } 1053 /// Return list of used allocators. 1054 ArrayRef<Expr *> getInnerAllocators() const { 1055 return getTopOfStack().InnerUsedAllocators; 1056 } 1057 /// Marks the declaration as implicitly firstprivate nin the task-based 1058 /// regions. 1059 void addImplicitTaskFirstprivate(unsigned Level, Decl *D) { 1060 getStackElemAtLevel(Level).ImplicitTaskFirstprivates.insert(D); 1061 } 1062 /// Checks if the decl is implicitly firstprivate in the task-based region. 1063 bool isImplicitTaskFirstprivate(Decl *D) const { 1064 return getTopOfStack().ImplicitTaskFirstprivates.count(D) > 0; 1065 } 1066 1067 /// Marks decl as used in uses_allocators clause as the allocator. 1068 void addUsesAllocatorsDecl(const Decl *D, UsesAllocatorsDeclKind Kind) { 1069 getTopOfStack().UsesAllocatorsDecls.try_emplace(D, Kind); 1070 } 1071 /// Checks if specified decl is used in uses allocator clause as the 1072 /// allocator. 1073 Optional<UsesAllocatorsDeclKind> isUsesAllocatorsDecl(unsigned Level, 1074 const Decl *D) const { 1075 const SharingMapTy &StackElem = getTopOfStack(); 1076 auto I = StackElem.UsesAllocatorsDecls.find(D); 1077 if (I == StackElem.UsesAllocatorsDecls.end()) 1078 return None; 1079 return I->getSecond(); 1080 } 1081 Optional<UsesAllocatorsDeclKind> isUsesAllocatorsDecl(const Decl *D) const { 1082 const SharingMapTy &StackElem = getTopOfStack(); 1083 auto I = StackElem.UsesAllocatorsDecls.find(D); 1084 if (I == StackElem.UsesAllocatorsDecls.end()) 1085 return None; 1086 return I->getSecond(); 1087 } 1088 1089 void addDeclareMapperVarRef(Expr *Ref) { 1090 SharingMapTy &StackElem = getTopOfStack(); 1091 StackElem.DeclareMapperVar = Ref; 1092 } 1093 const Expr *getDeclareMapperVarRef() const { 1094 const SharingMapTy *Top = getTopOfStackOrNull(); 1095 return Top ? Top->DeclareMapperVar : nullptr; 1096 } 1097 }; 1098 1099 bool isImplicitTaskingRegion(OpenMPDirectiveKind DKind) { 1100 return isOpenMPParallelDirective(DKind) || isOpenMPTeamsDirective(DKind); 1101 } 1102 1103 bool isImplicitOrExplicitTaskingRegion(OpenMPDirectiveKind DKind) { 1104 return isImplicitTaskingRegion(DKind) || isOpenMPTaskingDirective(DKind) || 1105 DKind == OMPD_unknown; 1106 } 1107 1108 } // namespace 1109 1110 static const Expr *getExprAsWritten(const Expr *E) { 1111 if (const auto *FE = dyn_cast<FullExpr>(E)) 1112 E = FE->getSubExpr(); 1113 1114 if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E)) 1115 E = MTE->getSubExpr(); 1116 1117 while (const auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E)) 1118 E = Binder->getSubExpr(); 1119 1120 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E)) 1121 E = ICE->getSubExprAsWritten(); 1122 return E->IgnoreParens(); 1123 } 1124 1125 static Expr *getExprAsWritten(Expr *E) { 1126 return const_cast<Expr *>(getExprAsWritten(const_cast<const Expr *>(E))); 1127 } 1128 1129 static const ValueDecl *getCanonicalDecl(const ValueDecl *D) { 1130 if (const auto *CED = dyn_cast<OMPCapturedExprDecl>(D)) 1131 if (const auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 1132 D = ME->getMemberDecl(); 1133 const auto *VD = dyn_cast<VarDecl>(D); 1134 const auto *FD = dyn_cast<FieldDecl>(D); 1135 if (VD != nullptr) { 1136 VD = VD->getCanonicalDecl(); 1137 D = VD; 1138 } else { 1139 assert(FD); 1140 FD = FD->getCanonicalDecl(); 1141 D = FD; 1142 } 1143 return D; 1144 } 1145 1146 static ValueDecl *getCanonicalDecl(ValueDecl *D) { 1147 return const_cast<ValueDecl *>( 1148 getCanonicalDecl(const_cast<const ValueDecl *>(D))); 1149 } 1150 1151 DSAStackTy::DSAVarData DSAStackTy::getDSA(const_iterator &Iter, 1152 ValueDecl *D) const { 1153 D = getCanonicalDecl(D); 1154 auto *VD = dyn_cast<VarDecl>(D); 1155 const auto *FD = dyn_cast<FieldDecl>(D); 1156 DSAVarData DVar; 1157 if (Iter == end()) { 1158 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1159 // in a region but not in construct] 1160 // File-scope or namespace-scope variables referenced in called routines 1161 // in the region are shared unless they appear in a threadprivate 1162 // directive. 1163 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(VD)) 1164 DVar.CKind = OMPC_shared; 1165 1166 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced 1167 // in a region but not in construct] 1168 // Variables with static storage duration that are declared in called 1169 // routines in the region are shared. 1170 if (VD && VD->hasGlobalStorage()) 1171 DVar.CKind = OMPC_shared; 1172 1173 // Non-static data members are shared by default. 1174 if (FD) 1175 DVar.CKind = OMPC_shared; 1176 1177 return DVar; 1178 } 1179 1180 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1181 // in a Construct, C/C++, predetermined, p.1] 1182 // Variables with automatic storage duration that are declared in a scope 1183 // inside the construct are private. 1184 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() && 1185 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) { 1186 DVar.CKind = OMPC_private; 1187 return DVar; 1188 } 1189 1190 DVar.DKind = Iter->Directive; 1191 // Explicitly specified attributes and local variables with predetermined 1192 // attributes. 1193 if (Iter->SharingMap.count(D)) { 1194 const DSAInfo &Data = Iter->SharingMap.lookup(D); 1195 DVar.RefExpr = Data.RefExpr.getPointer(); 1196 DVar.PrivateCopy = Data.PrivateCopy; 1197 DVar.CKind = Data.Attributes; 1198 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 1199 DVar.Modifier = Data.Modifier; 1200 DVar.AppliedToPointee = Data.AppliedToPointee; 1201 return DVar; 1202 } 1203 1204 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1205 // in a Construct, C/C++, implicitly determined, p.1] 1206 // In a parallel or task construct, the data-sharing attributes of these 1207 // variables are determined by the default clause, if present. 1208 switch (Iter->DefaultAttr) { 1209 case DSA_shared: 1210 DVar.CKind = OMPC_shared; 1211 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 1212 return DVar; 1213 case DSA_none: 1214 return DVar; 1215 case DSA_firstprivate: 1216 if (VD->getStorageDuration() == SD_Static && 1217 VD->getDeclContext()->isFileContext()) { 1218 DVar.CKind = OMPC_unknown; 1219 } else { 1220 DVar.CKind = OMPC_firstprivate; 1221 } 1222 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 1223 return DVar; 1224 case DSA_unspecified: 1225 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1226 // in a Construct, implicitly determined, p.2] 1227 // In a parallel construct, if no default clause is present, these 1228 // variables are shared. 1229 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 1230 if ((isOpenMPParallelDirective(DVar.DKind) && 1231 !isOpenMPTaskLoopDirective(DVar.DKind)) || 1232 isOpenMPTeamsDirective(DVar.DKind)) { 1233 DVar.CKind = OMPC_shared; 1234 return DVar; 1235 } 1236 1237 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1238 // in a Construct, implicitly determined, p.4] 1239 // In a task construct, if no default clause is present, a variable that in 1240 // the enclosing context is determined to be shared by all implicit tasks 1241 // bound to the current team is shared. 1242 if (isOpenMPTaskingDirective(DVar.DKind)) { 1243 DSAVarData DVarTemp; 1244 const_iterator I = Iter, E = end(); 1245 do { 1246 ++I; 1247 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables 1248 // Referenced in a Construct, implicitly determined, p.6] 1249 // In a task construct, if no default clause is present, a variable 1250 // whose data-sharing attribute is not determined by the rules above is 1251 // firstprivate. 1252 DVarTemp = getDSA(I, D); 1253 if (DVarTemp.CKind != OMPC_shared) { 1254 DVar.RefExpr = nullptr; 1255 DVar.CKind = OMPC_firstprivate; 1256 return DVar; 1257 } 1258 } while (I != E && !isImplicitTaskingRegion(I->Directive)); 1259 DVar.CKind = 1260 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared; 1261 return DVar; 1262 } 1263 } 1264 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1265 // in a Construct, implicitly determined, p.3] 1266 // For constructs other than task, if no default clause is present, these 1267 // variables inherit their data-sharing attributes from the enclosing 1268 // context. 1269 return getDSA(++Iter, D); 1270 } 1271 1272 const Expr *DSAStackTy::addUniqueAligned(const ValueDecl *D, 1273 const Expr *NewDE) { 1274 assert(!isStackEmpty() && "Data sharing attributes stack is empty"); 1275 D = getCanonicalDecl(D); 1276 SharingMapTy &StackElem = getTopOfStack(); 1277 auto It = StackElem.AlignedMap.find(D); 1278 if (It == StackElem.AlignedMap.end()) { 1279 assert(NewDE && "Unexpected nullptr expr to be added into aligned map"); 1280 StackElem.AlignedMap[D] = NewDE; 1281 return nullptr; 1282 } 1283 assert(It->second && "Unexpected nullptr expr in the aligned map"); 1284 return It->second; 1285 } 1286 1287 const Expr *DSAStackTy::addUniqueNontemporal(const ValueDecl *D, 1288 const Expr *NewDE) { 1289 assert(!isStackEmpty() && "Data sharing attributes stack is empty"); 1290 D = getCanonicalDecl(D); 1291 SharingMapTy &StackElem = getTopOfStack(); 1292 auto It = StackElem.NontemporalMap.find(D); 1293 if (It == StackElem.NontemporalMap.end()) { 1294 assert(NewDE && "Unexpected nullptr expr to be added into aligned map"); 1295 StackElem.NontemporalMap[D] = NewDE; 1296 return nullptr; 1297 } 1298 assert(It->second && "Unexpected nullptr expr in the aligned map"); 1299 return It->second; 1300 } 1301 1302 void DSAStackTy::addLoopControlVariable(const ValueDecl *D, VarDecl *Capture) { 1303 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1304 D = getCanonicalDecl(D); 1305 SharingMapTy &StackElem = getTopOfStack(); 1306 StackElem.LCVMap.try_emplace( 1307 D, LCDeclInfo(StackElem.LCVMap.size() + 1, Capture)); 1308 } 1309 1310 const DSAStackTy::LCDeclInfo 1311 DSAStackTy::isLoopControlVariable(const ValueDecl *D) const { 1312 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1313 D = getCanonicalDecl(D); 1314 const SharingMapTy &StackElem = getTopOfStack(); 1315 auto It = StackElem.LCVMap.find(D); 1316 if (It != StackElem.LCVMap.end()) 1317 return It->second; 1318 return {0, nullptr}; 1319 } 1320 1321 const DSAStackTy::LCDeclInfo 1322 DSAStackTy::isLoopControlVariable(const ValueDecl *D, unsigned Level) const { 1323 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1324 D = getCanonicalDecl(D); 1325 for (unsigned I = Level + 1; I > 0; --I) { 1326 const SharingMapTy &StackElem = getStackElemAtLevel(I - 1); 1327 auto It = StackElem.LCVMap.find(D); 1328 if (It != StackElem.LCVMap.end()) 1329 return It->second; 1330 } 1331 return {0, nullptr}; 1332 } 1333 1334 const DSAStackTy::LCDeclInfo 1335 DSAStackTy::isParentLoopControlVariable(const ValueDecl *D) const { 1336 const SharingMapTy *Parent = getSecondOnStackOrNull(); 1337 assert(Parent && "Data-sharing attributes stack is empty"); 1338 D = getCanonicalDecl(D); 1339 auto It = Parent->LCVMap.find(D); 1340 if (It != Parent->LCVMap.end()) 1341 return It->second; 1342 return {0, nullptr}; 1343 } 1344 1345 const ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) const { 1346 const SharingMapTy *Parent = getSecondOnStackOrNull(); 1347 assert(Parent && "Data-sharing attributes stack is empty"); 1348 if (Parent->LCVMap.size() < I) 1349 return nullptr; 1350 for (const auto &Pair : Parent->LCVMap) 1351 if (Pair.second.first == I) 1352 return Pair.first; 1353 return nullptr; 1354 } 1355 1356 void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A, 1357 DeclRefExpr *PrivateCopy, unsigned Modifier, 1358 bool AppliedToPointee) { 1359 D = getCanonicalDecl(D); 1360 if (A == OMPC_threadprivate) { 1361 DSAInfo &Data = Threadprivates[D]; 1362 Data.Attributes = A; 1363 Data.RefExpr.setPointer(E); 1364 Data.PrivateCopy = nullptr; 1365 Data.Modifier = Modifier; 1366 } else { 1367 DSAInfo &Data = getTopOfStack().SharingMap[D]; 1368 assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) || 1369 (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) || 1370 (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) || 1371 (isLoopControlVariable(D).first && A == OMPC_private)); 1372 Data.Modifier = Modifier; 1373 if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) { 1374 Data.RefExpr.setInt(/*IntVal=*/true); 1375 return; 1376 } 1377 const bool IsLastprivate = 1378 A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate; 1379 Data.Attributes = A; 1380 Data.RefExpr.setPointerAndInt(E, IsLastprivate); 1381 Data.PrivateCopy = PrivateCopy; 1382 Data.AppliedToPointee = AppliedToPointee; 1383 if (PrivateCopy) { 1384 DSAInfo &Data = getTopOfStack().SharingMap[PrivateCopy->getDecl()]; 1385 Data.Modifier = Modifier; 1386 Data.Attributes = A; 1387 Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate); 1388 Data.PrivateCopy = nullptr; 1389 Data.AppliedToPointee = AppliedToPointee; 1390 } 1391 } 1392 } 1393 1394 /// Build a variable declaration for OpenMP loop iteration variable. 1395 static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type, 1396 StringRef Name, const AttrVec *Attrs = nullptr, 1397 DeclRefExpr *OrigRef = nullptr) { 1398 DeclContext *DC = SemaRef.CurContext; 1399 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name); 1400 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc); 1401 auto *Decl = 1402 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None); 1403 if (Attrs) { 1404 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end()); 1405 I != E; ++I) 1406 Decl->addAttr(*I); 1407 } 1408 Decl->setImplicit(); 1409 if (OrigRef) { 1410 Decl->addAttr( 1411 OMPReferencedVarAttr::CreateImplicit(SemaRef.Context, OrigRef)); 1412 } 1413 return Decl; 1414 } 1415 1416 static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty, 1417 SourceLocation Loc, 1418 bool RefersToCapture = false) { 1419 D->setReferenced(); 1420 D->markUsed(S.Context); 1421 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(), 1422 SourceLocation(), D, RefersToCapture, Loc, Ty, 1423 VK_LValue); 1424 } 1425 1426 void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 1427 BinaryOperatorKind BOK) { 1428 D = getCanonicalDecl(D); 1429 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1430 assert( 1431 getTopOfStack().SharingMap[D].Attributes == OMPC_reduction && 1432 "Additional reduction info may be specified only for reduction items."); 1433 ReductionData &ReductionData = getTopOfStack().ReductionMap[D]; 1434 assert(ReductionData.ReductionRange.isInvalid() && 1435 (getTopOfStack().Directive == OMPD_taskgroup || 1436 ((isOpenMPParallelDirective(getTopOfStack().Directive) || 1437 isOpenMPWorksharingDirective(getTopOfStack().Directive)) && 1438 !isOpenMPSimdDirective(getTopOfStack().Directive))) && 1439 "Additional reduction info may be specified only once for reduction " 1440 "items."); 1441 ReductionData.set(BOK, SR); 1442 Expr *&TaskgroupReductionRef = 1443 getTopOfStack().TaskgroupReductionRef; 1444 if (!TaskgroupReductionRef) { 1445 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(), 1446 SemaRef.Context.VoidPtrTy, ".task_red."); 1447 TaskgroupReductionRef = 1448 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin()); 1449 } 1450 } 1451 1452 void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 1453 const Expr *ReductionRef) { 1454 D = getCanonicalDecl(D); 1455 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1456 assert( 1457 getTopOfStack().SharingMap[D].Attributes == OMPC_reduction && 1458 "Additional reduction info may be specified only for reduction items."); 1459 ReductionData &ReductionData = getTopOfStack().ReductionMap[D]; 1460 assert(ReductionData.ReductionRange.isInvalid() && 1461 (getTopOfStack().Directive == OMPD_taskgroup || 1462 ((isOpenMPParallelDirective(getTopOfStack().Directive) || 1463 isOpenMPWorksharingDirective(getTopOfStack().Directive)) && 1464 !isOpenMPSimdDirective(getTopOfStack().Directive))) && 1465 "Additional reduction info may be specified only once for reduction " 1466 "items."); 1467 ReductionData.set(ReductionRef, SR); 1468 Expr *&TaskgroupReductionRef = 1469 getTopOfStack().TaskgroupReductionRef; 1470 if (!TaskgroupReductionRef) { 1471 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(), 1472 SemaRef.Context.VoidPtrTy, ".task_red."); 1473 TaskgroupReductionRef = 1474 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin()); 1475 } 1476 } 1477 1478 const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData( 1479 const ValueDecl *D, SourceRange &SR, BinaryOperatorKind &BOK, 1480 Expr *&TaskgroupDescriptor) const { 1481 D = getCanonicalDecl(D); 1482 assert(!isStackEmpty() && "Data-sharing attributes stack is empty."); 1483 for (const_iterator I = begin() + 1, E = end(); I != E; ++I) { 1484 const DSAInfo &Data = I->SharingMap.lookup(D); 1485 if (Data.Attributes != OMPC_reduction || 1486 Data.Modifier != OMPC_REDUCTION_task) 1487 continue; 1488 const ReductionData &ReductionData = I->ReductionMap.lookup(D); 1489 if (!ReductionData.ReductionOp || 1490 ReductionData.ReductionOp.is<const Expr *>()) 1491 return DSAVarData(); 1492 SR = ReductionData.ReductionRange; 1493 BOK = ReductionData.ReductionOp.get<ReductionData::BOKPtrType>(); 1494 assert(I->TaskgroupReductionRef && "taskgroup reduction reference " 1495 "expression for the descriptor is not " 1496 "set."); 1497 TaskgroupDescriptor = I->TaskgroupReductionRef; 1498 return DSAVarData(I->Directive, OMPC_reduction, Data.RefExpr.getPointer(), 1499 Data.PrivateCopy, I->DefaultAttrLoc, OMPC_REDUCTION_task, 1500 /*AppliedToPointee=*/false); 1501 } 1502 return DSAVarData(); 1503 } 1504 1505 const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData( 1506 const ValueDecl *D, SourceRange &SR, const Expr *&ReductionRef, 1507 Expr *&TaskgroupDescriptor) const { 1508 D = getCanonicalDecl(D); 1509 assert(!isStackEmpty() && "Data-sharing attributes stack is empty."); 1510 for (const_iterator I = begin() + 1, E = end(); I != E; ++I) { 1511 const DSAInfo &Data = I->SharingMap.lookup(D); 1512 if (Data.Attributes != OMPC_reduction || 1513 Data.Modifier != OMPC_REDUCTION_task) 1514 continue; 1515 const ReductionData &ReductionData = I->ReductionMap.lookup(D); 1516 if (!ReductionData.ReductionOp || 1517 !ReductionData.ReductionOp.is<const Expr *>()) 1518 return DSAVarData(); 1519 SR = ReductionData.ReductionRange; 1520 ReductionRef = ReductionData.ReductionOp.get<const Expr *>(); 1521 assert(I->TaskgroupReductionRef && "taskgroup reduction reference " 1522 "expression for the descriptor is not " 1523 "set."); 1524 TaskgroupDescriptor = I->TaskgroupReductionRef; 1525 return DSAVarData(I->Directive, OMPC_reduction, Data.RefExpr.getPointer(), 1526 Data.PrivateCopy, I->DefaultAttrLoc, OMPC_REDUCTION_task, 1527 /*AppliedToPointee=*/false); 1528 } 1529 return DSAVarData(); 1530 } 1531 1532 bool DSAStackTy::isOpenMPLocal(VarDecl *D, const_iterator I) const { 1533 D = D->getCanonicalDecl(); 1534 for (const_iterator E = end(); I != E; ++I) { 1535 if (isImplicitOrExplicitTaskingRegion(I->Directive) || 1536 isOpenMPTargetExecutionDirective(I->Directive)) { 1537 if (I->CurScope) { 1538 Scope *TopScope = I->CurScope->getParent(); 1539 Scope *CurScope = getCurScope(); 1540 while (CurScope && CurScope != TopScope && !CurScope->isDeclScope(D)) 1541 CurScope = CurScope->getParent(); 1542 return CurScope != TopScope; 1543 } 1544 for (DeclContext *DC = D->getDeclContext(); DC; DC = DC->getParent()) 1545 if (I->Context == DC) 1546 return true; 1547 return false; 1548 } 1549 } 1550 return false; 1551 } 1552 1553 static bool isConstNotMutableType(Sema &SemaRef, QualType Type, 1554 bool AcceptIfMutable = true, 1555 bool *IsClassType = nullptr) { 1556 ASTContext &Context = SemaRef.getASTContext(); 1557 Type = Type.getNonReferenceType().getCanonicalType(); 1558 bool IsConstant = Type.isConstant(Context); 1559 Type = Context.getBaseElementType(Type); 1560 const CXXRecordDecl *RD = AcceptIfMutable && SemaRef.getLangOpts().CPlusPlus 1561 ? Type->getAsCXXRecordDecl() 1562 : nullptr; 1563 if (const auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD)) 1564 if (const ClassTemplateDecl *CTD = CTSD->getSpecializedTemplate()) 1565 RD = CTD->getTemplatedDecl(); 1566 if (IsClassType) 1567 *IsClassType = RD; 1568 return IsConstant && !(SemaRef.getLangOpts().CPlusPlus && RD && 1569 RD->hasDefinition() && RD->hasMutableFields()); 1570 } 1571 1572 static bool rejectConstNotMutableType(Sema &SemaRef, const ValueDecl *D, 1573 QualType Type, OpenMPClauseKind CKind, 1574 SourceLocation ELoc, 1575 bool AcceptIfMutable = true, 1576 bool ListItemNotVar = false) { 1577 ASTContext &Context = SemaRef.getASTContext(); 1578 bool IsClassType; 1579 if (isConstNotMutableType(SemaRef, Type, AcceptIfMutable, &IsClassType)) { 1580 unsigned Diag = ListItemNotVar 1581 ? diag::err_omp_const_list_item 1582 : IsClassType ? diag::err_omp_const_not_mutable_variable 1583 : diag::err_omp_const_variable; 1584 SemaRef.Diag(ELoc, Diag) << getOpenMPClauseName(CKind); 1585 if (!ListItemNotVar && D) { 1586 const VarDecl *VD = dyn_cast<VarDecl>(D); 1587 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 1588 VarDecl::DeclarationOnly; 1589 SemaRef.Diag(D->getLocation(), 1590 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1591 << D; 1592 } 1593 return true; 1594 } 1595 return false; 1596 } 1597 1598 const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, 1599 bool FromParent) { 1600 D = getCanonicalDecl(D); 1601 DSAVarData DVar; 1602 1603 auto *VD = dyn_cast<VarDecl>(D); 1604 auto TI = Threadprivates.find(D); 1605 if (TI != Threadprivates.end()) { 1606 DVar.RefExpr = TI->getSecond().RefExpr.getPointer(); 1607 DVar.CKind = OMPC_threadprivate; 1608 DVar.Modifier = TI->getSecond().Modifier; 1609 return DVar; 1610 } 1611 if (VD && VD->hasAttr<OMPThreadPrivateDeclAttr>()) { 1612 DVar.RefExpr = buildDeclRefExpr( 1613 SemaRef, VD, D->getType().getNonReferenceType(), 1614 VD->getAttr<OMPThreadPrivateDeclAttr>()->getLocation()); 1615 DVar.CKind = OMPC_threadprivate; 1616 addDSA(D, DVar.RefExpr, OMPC_threadprivate); 1617 return DVar; 1618 } 1619 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1620 // in a Construct, C/C++, predetermined, p.1] 1621 // Variables appearing in threadprivate directives are threadprivate. 1622 if ((VD && VD->getTLSKind() != VarDecl::TLS_None && 1623 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() && 1624 SemaRef.getLangOpts().OpenMPUseTLS && 1625 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) || 1626 (VD && VD->getStorageClass() == SC_Register && 1627 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) { 1628 DVar.RefExpr = buildDeclRefExpr( 1629 SemaRef, VD, D->getType().getNonReferenceType(), D->getLocation()); 1630 DVar.CKind = OMPC_threadprivate; 1631 addDSA(D, DVar.RefExpr, OMPC_threadprivate); 1632 return DVar; 1633 } 1634 if (SemaRef.getLangOpts().OpenMPCUDAMode && VD && 1635 VD->isLocalVarDeclOrParm() && !isStackEmpty() && 1636 !isLoopControlVariable(D).first) { 1637 const_iterator IterTarget = 1638 std::find_if(begin(), end(), [](const SharingMapTy &Data) { 1639 return isOpenMPTargetExecutionDirective(Data.Directive); 1640 }); 1641 if (IterTarget != end()) { 1642 const_iterator ParentIterTarget = IterTarget + 1; 1643 for (const_iterator Iter = begin(); 1644 Iter != ParentIterTarget; ++Iter) { 1645 if (isOpenMPLocal(VD, Iter)) { 1646 DVar.RefExpr = 1647 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(), 1648 D->getLocation()); 1649 DVar.CKind = OMPC_threadprivate; 1650 return DVar; 1651 } 1652 } 1653 if (!isClauseParsingMode() || IterTarget != begin()) { 1654 auto DSAIter = IterTarget->SharingMap.find(D); 1655 if (DSAIter != IterTarget->SharingMap.end() && 1656 isOpenMPPrivate(DSAIter->getSecond().Attributes)) { 1657 DVar.RefExpr = DSAIter->getSecond().RefExpr.getPointer(); 1658 DVar.CKind = OMPC_threadprivate; 1659 return DVar; 1660 } 1661 const_iterator End = end(); 1662 if (!SemaRef.isOpenMPCapturedByRef( 1663 D, std::distance(ParentIterTarget, End), 1664 /*OpenMPCaptureLevel=*/0)) { 1665 DVar.RefExpr = 1666 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(), 1667 IterTarget->ConstructLoc); 1668 DVar.CKind = OMPC_threadprivate; 1669 return DVar; 1670 } 1671 } 1672 } 1673 } 1674 1675 if (isStackEmpty()) 1676 // Not in OpenMP execution region and top scope was already checked. 1677 return DVar; 1678 1679 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1680 // in a Construct, C/C++, predetermined, p.4] 1681 // Static data members are shared. 1682 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1683 // in a Construct, C/C++, predetermined, p.7] 1684 // Variables with static storage duration that are declared in a scope 1685 // inside the construct are shared. 1686 if (VD && VD->isStaticDataMember()) { 1687 // Check for explicitly specified attributes. 1688 const_iterator I = begin(); 1689 const_iterator EndI = end(); 1690 if (FromParent && I != EndI) 1691 ++I; 1692 if (I != EndI) { 1693 auto It = I->SharingMap.find(D); 1694 if (It != I->SharingMap.end()) { 1695 const DSAInfo &Data = It->getSecond(); 1696 DVar.RefExpr = Data.RefExpr.getPointer(); 1697 DVar.PrivateCopy = Data.PrivateCopy; 1698 DVar.CKind = Data.Attributes; 1699 DVar.ImplicitDSALoc = I->DefaultAttrLoc; 1700 DVar.DKind = I->Directive; 1701 DVar.Modifier = Data.Modifier; 1702 DVar.AppliedToPointee = Data.AppliedToPointee; 1703 return DVar; 1704 } 1705 } 1706 1707 DVar.CKind = OMPC_shared; 1708 return DVar; 1709 } 1710 1711 auto &&MatchesAlways = [](OpenMPDirectiveKind) { return true; }; 1712 // The predetermined shared attribute for const-qualified types having no 1713 // mutable members was removed after OpenMP 3.1. 1714 if (SemaRef.LangOpts.OpenMP <= 31) { 1715 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1716 // in a Construct, C/C++, predetermined, p.6] 1717 // Variables with const qualified type having no mutable member are 1718 // shared. 1719 if (isConstNotMutableType(SemaRef, D->getType())) { 1720 // Variables with const-qualified type having no mutable member may be 1721 // listed in a firstprivate clause, even if they are static data members. 1722 DSAVarData DVarTemp = hasInnermostDSA( 1723 D, 1724 [](OpenMPClauseKind C, bool) { 1725 return C == OMPC_firstprivate || C == OMPC_shared; 1726 }, 1727 MatchesAlways, FromParent); 1728 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr) 1729 return DVarTemp; 1730 1731 DVar.CKind = OMPC_shared; 1732 return DVar; 1733 } 1734 } 1735 1736 // Explicitly specified attributes and local variables with predetermined 1737 // attributes. 1738 const_iterator I = begin(); 1739 const_iterator EndI = end(); 1740 if (FromParent && I != EndI) 1741 ++I; 1742 if (I == EndI) 1743 return DVar; 1744 auto It = I->SharingMap.find(D); 1745 if (It != I->SharingMap.end()) { 1746 const DSAInfo &Data = It->getSecond(); 1747 DVar.RefExpr = Data.RefExpr.getPointer(); 1748 DVar.PrivateCopy = Data.PrivateCopy; 1749 DVar.CKind = Data.Attributes; 1750 DVar.ImplicitDSALoc = I->DefaultAttrLoc; 1751 DVar.DKind = I->Directive; 1752 DVar.Modifier = Data.Modifier; 1753 DVar.AppliedToPointee = Data.AppliedToPointee; 1754 } 1755 1756 return DVar; 1757 } 1758 1759 const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D, 1760 bool FromParent) const { 1761 if (isStackEmpty()) { 1762 const_iterator I; 1763 return getDSA(I, D); 1764 } 1765 D = getCanonicalDecl(D); 1766 const_iterator StartI = begin(); 1767 const_iterator EndI = end(); 1768 if (FromParent && StartI != EndI) 1769 ++StartI; 1770 return getDSA(StartI, D); 1771 } 1772 1773 const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D, 1774 unsigned Level) const { 1775 if (getStackSize() <= Level) 1776 return DSAVarData(); 1777 D = getCanonicalDecl(D); 1778 const_iterator StartI = std::next(begin(), getStackSize() - 1 - Level); 1779 return getDSA(StartI, D); 1780 } 1781 1782 const DSAStackTy::DSAVarData 1783 DSAStackTy::hasDSA(ValueDecl *D, 1784 const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred, 1785 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1786 bool FromParent) const { 1787 if (isStackEmpty()) 1788 return {}; 1789 D = getCanonicalDecl(D); 1790 const_iterator I = begin(); 1791 const_iterator EndI = end(); 1792 if (FromParent && I != EndI) 1793 ++I; 1794 for (; I != EndI; ++I) { 1795 if (!DPred(I->Directive) && 1796 !isImplicitOrExplicitTaskingRegion(I->Directive)) 1797 continue; 1798 const_iterator NewI = I; 1799 DSAVarData DVar = getDSA(NewI, D); 1800 if (I == NewI && CPred(DVar.CKind, DVar.AppliedToPointee)) 1801 return DVar; 1802 } 1803 return {}; 1804 } 1805 1806 const DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA( 1807 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred, 1808 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1809 bool FromParent) const { 1810 if (isStackEmpty()) 1811 return {}; 1812 D = getCanonicalDecl(D); 1813 const_iterator StartI = begin(); 1814 const_iterator EndI = end(); 1815 if (FromParent && StartI != EndI) 1816 ++StartI; 1817 if (StartI == EndI || !DPred(StartI->Directive)) 1818 return {}; 1819 const_iterator NewI = StartI; 1820 DSAVarData DVar = getDSA(NewI, D); 1821 return (NewI == StartI && CPred(DVar.CKind, DVar.AppliedToPointee)) 1822 ? DVar 1823 : DSAVarData(); 1824 } 1825 1826 bool DSAStackTy::hasExplicitDSA( 1827 const ValueDecl *D, 1828 const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred, 1829 unsigned Level, bool NotLastprivate) const { 1830 if (getStackSize() <= Level) 1831 return false; 1832 D = getCanonicalDecl(D); 1833 const SharingMapTy &StackElem = getStackElemAtLevel(Level); 1834 auto I = StackElem.SharingMap.find(D); 1835 if (I != StackElem.SharingMap.end() && I->getSecond().RefExpr.getPointer() && 1836 CPred(I->getSecond().Attributes, I->getSecond().AppliedToPointee) && 1837 (!NotLastprivate || !I->getSecond().RefExpr.getInt())) 1838 return true; 1839 // Check predetermined rules for the loop control variables. 1840 auto LI = StackElem.LCVMap.find(D); 1841 if (LI != StackElem.LCVMap.end()) 1842 return CPred(OMPC_private, /*AppliedToPointee=*/false); 1843 return false; 1844 } 1845 1846 bool DSAStackTy::hasExplicitDirective( 1847 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1848 unsigned Level) const { 1849 if (getStackSize() <= Level) 1850 return false; 1851 const SharingMapTy &StackElem = getStackElemAtLevel(Level); 1852 return DPred(StackElem.Directive); 1853 } 1854 1855 bool DSAStackTy::hasDirective( 1856 const llvm::function_ref<bool(OpenMPDirectiveKind, 1857 const DeclarationNameInfo &, SourceLocation)> 1858 DPred, 1859 bool FromParent) const { 1860 // We look only in the enclosing region. 1861 size_t Skip = FromParent ? 2 : 1; 1862 for (const_iterator I = begin() + std::min(Skip, getStackSize()), E = end(); 1863 I != E; ++I) { 1864 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc)) 1865 return true; 1866 } 1867 return false; 1868 } 1869 1870 void Sema::InitDataSharingAttributesStack() { 1871 VarDataSharingAttributesStack = new DSAStackTy(*this); 1872 } 1873 1874 #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack) 1875 1876 void Sema::pushOpenMPFunctionRegion() { 1877 DSAStack->pushFunction(); 1878 } 1879 1880 void Sema::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) { 1881 DSAStack->popFunction(OldFSI); 1882 } 1883 1884 static bool isOpenMPDeviceDelayedContext(Sema &S) { 1885 assert(S.LangOpts.OpenMP && S.LangOpts.OpenMPIsDevice && 1886 "Expected OpenMP device compilation."); 1887 return !S.isInOpenMPTargetExecutionDirective(); 1888 } 1889 1890 namespace { 1891 /// Status of the function emission on the host/device. 1892 enum class FunctionEmissionStatus { 1893 Emitted, 1894 Discarded, 1895 Unknown, 1896 }; 1897 } // anonymous namespace 1898 1899 Sema::SemaDiagnosticBuilder Sema::diagIfOpenMPDeviceCode(SourceLocation Loc, 1900 unsigned DiagID, 1901 FunctionDecl *FD) { 1902 assert(LangOpts.OpenMP && LangOpts.OpenMPIsDevice && 1903 "Expected OpenMP device compilation."); 1904 1905 SemaDiagnosticBuilder::Kind Kind = SemaDiagnosticBuilder::K_Nop; 1906 if (FD) { 1907 FunctionEmissionStatus FES = getEmissionStatus(FD); 1908 switch (FES) { 1909 case FunctionEmissionStatus::Emitted: 1910 Kind = SemaDiagnosticBuilder::K_Immediate; 1911 break; 1912 case FunctionEmissionStatus::Unknown: 1913 // TODO: We should always delay diagnostics here in case a target 1914 // region is in a function we do not emit. However, as the 1915 // current diagnostics are associated with the function containing 1916 // the target region and we do not emit that one, we would miss out 1917 // on diagnostics for the target region itself. We need to anchor 1918 // the diagnostics with the new generated function *or* ensure we 1919 // emit diagnostics associated with the surrounding function. 1920 Kind = isOpenMPDeviceDelayedContext(*this) 1921 ? SemaDiagnosticBuilder::K_Deferred 1922 : SemaDiagnosticBuilder::K_Immediate; 1923 break; 1924 case FunctionEmissionStatus::TemplateDiscarded: 1925 case FunctionEmissionStatus::OMPDiscarded: 1926 Kind = SemaDiagnosticBuilder::K_Nop; 1927 break; 1928 case FunctionEmissionStatus::CUDADiscarded: 1929 llvm_unreachable("CUDADiscarded unexpected in OpenMP device compilation"); 1930 break; 1931 } 1932 } 1933 1934 return SemaDiagnosticBuilder(Kind, Loc, DiagID, FD, *this); 1935 } 1936 1937 Sema::SemaDiagnosticBuilder Sema::diagIfOpenMPHostCode(SourceLocation Loc, 1938 unsigned DiagID, 1939 FunctionDecl *FD) { 1940 assert(LangOpts.OpenMP && !LangOpts.OpenMPIsDevice && 1941 "Expected OpenMP host compilation."); 1942 1943 SemaDiagnosticBuilder::Kind Kind = SemaDiagnosticBuilder::K_Nop; 1944 if (FD) { 1945 FunctionEmissionStatus FES = getEmissionStatus(FD); 1946 switch (FES) { 1947 case FunctionEmissionStatus::Emitted: 1948 Kind = SemaDiagnosticBuilder::K_Immediate; 1949 break; 1950 case FunctionEmissionStatus::Unknown: 1951 Kind = SemaDiagnosticBuilder::K_Deferred; 1952 break; 1953 case FunctionEmissionStatus::TemplateDiscarded: 1954 case FunctionEmissionStatus::OMPDiscarded: 1955 case FunctionEmissionStatus::CUDADiscarded: 1956 Kind = SemaDiagnosticBuilder::K_Nop; 1957 break; 1958 } 1959 } 1960 1961 return SemaDiagnosticBuilder(Kind, Loc, DiagID, FD, *this); 1962 } 1963 1964 static OpenMPDefaultmapClauseKind 1965 getVariableCategoryFromDecl(const LangOptions &LO, const ValueDecl *VD) { 1966 if (LO.OpenMP <= 45) { 1967 if (VD->getType().getNonReferenceType()->isScalarType()) 1968 return OMPC_DEFAULTMAP_scalar; 1969 return OMPC_DEFAULTMAP_aggregate; 1970 } 1971 if (VD->getType().getNonReferenceType()->isAnyPointerType()) 1972 return OMPC_DEFAULTMAP_pointer; 1973 if (VD->getType().getNonReferenceType()->isScalarType()) 1974 return OMPC_DEFAULTMAP_scalar; 1975 return OMPC_DEFAULTMAP_aggregate; 1976 } 1977 1978 bool Sema::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level, 1979 unsigned OpenMPCaptureLevel) const { 1980 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1981 1982 ASTContext &Ctx = getASTContext(); 1983 bool IsByRef = true; 1984 1985 // Find the directive that is associated with the provided scope. 1986 D = cast<ValueDecl>(D->getCanonicalDecl()); 1987 QualType Ty = D->getType(); 1988 1989 bool IsVariableUsedInMapClause = false; 1990 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) { 1991 // This table summarizes how a given variable should be passed to the device 1992 // given its type and the clauses where it appears. This table is based on 1993 // the description in OpenMP 4.5 [2.10.4, target Construct] and 1994 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses]. 1995 // 1996 // ========================================================================= 1997 // | type | defaultmap | pvt | first | is_device_ptr | map | res. | 1998 // | |(tofrom:scalar)| | pvt | | | | 1999 // ========================================================================= 2000 // | scl | | | | - | | bycopy| 2001 // | scl | | - | x | - | - | bycopy| 2002 // | scl | | x | - | - | - | null | 2003 // | scl | x | | | - | | byref | 2004 // | scl | x | - | x | - | - | bycopy| 2005 // | scl | x | x | - | - | - | null | 2006 // | scl | | - | - | - | x | byref | 2007 // | scl | x | - | - | - | x | byref | 2008 // 2009 // | agg | n.a. | | | - | | byref | 2010 // | agg | n.a. | - | x | - | - | byref | 2011 // | agg | n.a. | x | - | - | - | null | 2012 // | agg | n.a. | - | - | - | x | byref | 2013 // | agg | n.a. | - | - | - | x[] | byref | 2014 // 2015 // | ptr | n.a. | | | - | | bycopy| 2016 // | ptr | n.a. | - | x | - | - | bycopy| 2017 // | ptr | n.a. | x | - | - | - | null | 2018 // | ptr | n.a. | - | - | - | x | byref | 2019 // | ptr | n.a. | - | - | - | x[] | bycopy| 2020 // | ptr | n.a. | - | - | x | | bycopy| 2021 // | ptr | n.a. | - | - | x | x | bycopy| 2022 // | ptr | n.a. | - | - | x | x[] | bycopy| 2023 // ========================================================================= 2024 // Legend: 2025 // scl - scalar 2026 // ptr - pointer 2027 // agg - aggregate 2028 // x - applies 2029 // - - invalid in this combination 2030 // [] - mapped with an array section 2031 // byref - should be mapped by reference 2032 // byval - should be mapped by value 2033 // null - initialize a local variable to null on the device 2034 // 2035 // Observations: 2036 // - All scalar declarations that show up in a map clause have to be passed 2037 // by reference, because they may have been mapped in the enclosing data 2038 // environment. 2039 // - If the scalar value does not fit the size of uintptr, it has to be 2040 // passed by reference, regardless the result in the table above. 2041 // - For pointers mapped by value that have either an implicit map or an 2042 // array section, the runtime library may pass the NULL value to the 2043 // device instead of the value passed to it by the compiler. 2044 2045 if (Ty->isReferenceType()) 2046 Ty = Ty->castAs<ReferenceType>()->getPointeeType(); 2047 2048 // Locate map clauses and see if the variable being captured is referred to 2049 // in any of those clauses. Here we only care about variables, not fields, 2050 // because fields are part of aggregates. 2051 bool IsVariableAssociatedWithSection = false; 2052 2053 DSAStack->checkMappableExprComponentListsForDeclAtLevel( 2054 D, Level, 2055 [&IsVariableUsedInMapClause, &IsVariableAssociatedWithSection, D]( 2056 OMPClauseMappableExprCommon::MappableExprComponentListRef 2057 MapExprComponents, 2058 OpenMPClauseKind WhereFoundClauseKind) { 2059 // Only the map clause information influences how a variable is 2060 // captured. E.g. is_device_ptr does not require changing the default 2061 // behavior. 2062 if (WhereFoundClauseKind != OMPC_map) 2063 return false; 2064 2065 auto EI = MapExprComponents.rbegin(); 2066 auto EE = MapExprComponents.rend(); 2067 2068 assert(EI != EE && "Invalid map expression!"); 2069 2070 if (isa<DeclRefExpr>(EI->getAssociatedExpression())) 2071 IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D; 2072 2073 ++EI; 2074 if (EI == EE) 2075 return false; 2076 2077 if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) || 2078 isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) || 2079 isa<MemberExpr>(EI->getAssociatedExpression()) || 2080 isa<OMPArrayShapingExpr>(EI->getAssociatedExpression())) { 2081 IsVariableAssociatedWithSection = true; 2082 // There is nothing more we need to know about this variable. 2083 return true; 2084 } 2085 2086 // Keep looking for more map info. 2087 return false; 2088 }); 2089 2090 if (IsVariableUsedInMapClause) { 2091 // If variable is identified in a map clause it is always captured by 2092 // reference except if it is a pointer that is dereferenced somehow. 2093 IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection); 2094 } else { 2095 // By default, all the data that has a scalar type is mapped by copy 2096 // (except for reduction variables). 2097 // Defaultmap scalar is mutual exclusive to defaultmap pointer 2098 IsByRef = (DSAStack->isForceCaptureByReferenceInTargetExecutable() && 2099 !Ty->isAnyPointerType()) || 2100 !Ty->isScalarType() || 2101 DSAStack->isDefaultmapCapturedByRef( 2102 Level, getVariableCategoryFromDecl(LangOpts, D)) || 2103 DSAStack->hasExplicitDSA( 2104 D, 2105 [](OpenMPClauseKind K, bool AppliedToPointee) { 2106 return K == OMPC_reduction && !AppliedToPointee; 2107 }, 2108 Level); 2109 } 2110 } 2111 2112 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) { 2113 IsByRef = 2114 ((IsVariableUsedInMapClause && 2115 DSAStack->getCaptureRegion(Level, OpenMPCaptureLevel) == 2116 OMPD_target) || 2117 !(DSAStack->hasExplicitDSA( 2118 D, 2119 [](OpenMPClauseKind K, bool AppliedToPointee) -> bool { 2120 return K == OMPC_firstprivate || 2121 (K == OMPC_reduction && AppliedToPointee); 2122 }, 2123 Level, /*NotLastprivate=*/true) || 2124 DSAStack->isUsesAllocatorsDecl(Level, D))) && 2125 // If the variable is artificial and must be captured by value - try to 2126 // capture by value. 2127 !(isa<OMPCapturedExprDecl>(D) && !D->hasAttr<OMPCaptureNoInitAttr>() && 2128 !cast<OMPCapturedExprDecl>(D)->getInit()->isGLValue()) && 2129 // If the variable is implicitly firstprivate and scalar - capture by 2130 // copy 2131 !(DSAStack->getDefaultDSA() == DSA_firstprivate && 2132 !DSAStack->hasExplicitDSA( 2133 D, [](OpenMPClauseKind K, bool) { return K != OMPC_unknown; }, 2134 Level) && 2135 !DSAStack->isLoopControlVariable(D, Level).first); 2136 } 2137 2138 // When passing data by copy, we need to make sure it fits the uintptr size 2139 // and alignment, because the runtime library only deals with uintptr types. 2140 // If it does not fit the uintptr size, we need to pass the data by reference 2141 // instead. 2142 if (!IsByRef && 2143 (Ctx.getTypeSizeInChars(Ty) > 2144 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) || 2145 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) { 2146 IsByRef = true; 2147 } 2148 2149 return IsByRef; 2150 } 2151 2152 unsigned Sema::getOpenMPNestingLevel() const { 2153 assert(getLangOpts().OpenMP); 2154 return DSAStack->getNestingLevel(); 2155 } 2156 2157 bool Sema::isInOpenMPTargetExecutionDirective() const { 2158 return (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) && 2159 !DSAStack->isClauseParsingMode()) || 2160 DSAStack->hasDirective( 2161 [](OpenMPDirectiveKind K, const DeclarationNameInfo &, 2162 SourceLocation) -> bool { 2163 return isOpenMPTargetExecutionDirective(K); 2164 }, 2165 false); 2166 } 2167 2168 VarDecl *Sema::isOpenMPCapturedDecl(ValueDecl *D, bool CheckScopeInfo, 2169 unsigned StopAt) { 2170 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 2171 D = getCanonicalDecl(D); 2172 2173 auto *VD = dyn_cast<VarDecl>(D); 2174 // Do not capture constexpr variables. 2175 if (VD && VD->isConstexpr()) 2176 return nullptr; 2177 2178 // If we want to determine whether the variable should be captured from the 2179 // perspective of the current capturing scope, and we've already left all the 2180 // capturing scopes of the top directive on the stack, check from the 2181 // perspective of its parent directive (if any) instead. 2182 DSAStackTy::ParentDirectiveScope InParentDirectiveRAII( 2183 *DSAStack, CheckScopeInfo && DSAStack->isBodyComplete()); 2184 2185 // If we are attempting to capture a global variable in a directive with 2186 // 'target' we return true so that this global is also mapped to the device. 2187 // 2188 if (VD && !VD->hasLocalStorage() && 2189 (getCurCapturedRegion() || getCurBlock() || getCurLambda())) { 2190 if (isInOpenMPDeclareTargetContext()) { 2191 // Try to mark variable as declare target if it is used in capturing 2192 // regions. 2193 if (LangOpts.OpenMP <= 45 && 2194 !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) 2195 checkDeclIsAllowedInOpenMPTarget(nullptr, VD); 2196 return nullptr; 2197 } 2198 if (isInOpenMPTargetExecutionDirective()) { 2199 // If the declaration is enclosed in a 'declare target' directive, 2200 // then it should not be captured. 2201 // 2202 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) 2203 return nullptr; 2204 CapturedRegionScopeInfo *CSI = nullptr; 2205 for (FunctionScopeInfo *FSI : llvm::drop_begin( 2206 llvm::reverse(FunctionScopes), 2207 CheckScopeInfo ? (FunctionScopes.size() - (StopAt + 1)) : 0)) { 2208 if (!isa<CapturingScopeInfo>(FSI)) 2209 return nullptr; 2210 if (auto *RSI = dyn_cast<CapturedRegionScopeInfo>(FSI)) 2211 if (RSI->CapRegionKind == CR_OpenMP) { 2212 CSI = RSI; 2213 break; 2214 } 2215 } 2216 assert(CSI && "Failed to find CapturedRegionScopeInfo"); 2217 SmallVector<OpenMPDirectiveKind, 4> Regions; 2218 getOpenMPCaptureRegions(Regions, 2219 DSAStack->getDirective(CSI->OpenMPLevel)); 2220 if (Regions[CSI->OpenMPCaptureLevel] != OMPD_task) 2221 return VD; 2222 } 2223 } 2224 2225 if (CheckScopeInfo) { 2226 bool OpenMPFound = false; 2227 for (unsigned I = StopAt + 1; I > 0; --I) { 2228 FunctionScopeInfo *FSI = FunctionScopes[I - 1]; 2229 if(!isa<CapturingScopeInfo>(FSI)) 2230 return nullptr; 2231 if (auto *RSI = dyn_cast<CapturedRegionScopeInfo>(FSI)) 2232 if (RSI->CapRegionKind == CR_OpenMP) { 2233 OpenMPFound = true; 2234 break; 2235 } 2236 } 2237 if (!OpenMPFound) 2238 return nullptr; 2239 } 2240 2241 if (DSAStack->getCurrentDirective() != OMPD_unknown && 2242 (!DSAStack->isClauseParsingMode() || 2243 DSAStack->getParentDirective() != OMPD_unknown)) { 2244 auto &&Info = DSAStack->isLoopControlVariable(D); 2245 if (Info.first || 2246 (VD && VD->hasLocalStorage() && 2247 isImplicitOrExplicitTaskingRegion(DSAStack->getCurrentDirective())) || 2248 (VD && DSAStack->isForceVarCapturing())) 2249 return VD ? VD : Info.second; 2250 DSAStackTy::DSAVarData DVarTop = 2251 DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode()); 2252 if (DVarTop.CKind != OMPC_unknown && isOpenMPPrivate(DVarTop.CKind) && 2253 (!VD || VD->hasLocalStorage() || !DVarTop.AppliedToPointee)) 2254 return VD ? VD : cast<VarDecl>(DVarTop.PrivateCopy->getDecl()); 2255 // Threadprivate variables must not be captured. 2256 if (isOpenMPThreadPrivate(DVarTop.CKind)) 2257 return nullptr; 2258 // The variable is not private or it is the variable in the directive with 2259 // default(none) clause and not used in any clause. 2260 DSAStackTy::DSAVarData DVarPrivate = DSAStack->hasDSA( 2261 D, 2262 [](OpenMPClauseKind C, bool AppliedToPointee) { 2263 return isOpenMPPrivate(C) && !AppliedToPointee; 2264 }, 2265 [](OpenMPDirectiveKind) { return true; }, 2266 DSAStack->isClauseParsingMode()); 2267 // Global shared must not be captured. 2268 if (VD && !VD->hasLocalStorage() && DVarPrivate.CKind == OMPC_unknown && 2269 ((DSAStack->getDefaultDSA() != DSA_none && 2270 DSAStack->getDefaultDSA() != DSA_firstprivate) || 2271 DVarTop.CKind == OMPC_shared)) 2272 return nullptr; 2273 if (DVarPrivate.CKind != OMPC_unknown || 2274 (VD && (DSAStack->getDefaultDSA() == DSA_none || 2275 DSAStack->getDefaultDSA() == DSA_firstprivate))) 2276 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl()); 2277 } 2278 return nullptr; 2279 } 2280 2281 void Sema::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex, 2282 unsigned Level) const { 2283 FunctionScopesIndex -= getOpenMPCaptureLevels(DSAStack->getDirective(Level)); 2284 } 2285 2286 void Sema::startOpenMPLoop() { 2287 assert(LangOpts.OpenMP && "OpenMP must be enabled."); 2288 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) 2289 DSAStack->loopInit(); 2290 } 2291 2292 void Sema::startOpenMPCXXRangeFor() { 2293 assert(LangOpts.OpenMP && "OpenMP must be enabled."); 2294 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { 2295 DSAStack->resetPossibleLoopCounter(); 2296 DSAStack->loopStart(); 2297 } 2298 } 2299 2300 OpenMPClauseKind Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level, 2301 unsigned CapLevel) const { 2302 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 2303 if (DSAStack->hasExplicitDirective( 2304 [](OpenMPDirectiveKind K) { return isOpenMPTaskingDirective(K); }, 2305 Level)) { 2306 bool IsTriviallyCopyable = 2307 D->getType().getNonReferenceType().isTriviallyCopyableType(Context) && 2308 !D->getType() 2309 .getNonReferenceType() 2310 .getCanonicalType() 2311 ->getAsCXXRecordDecl(); 2312 OpenMPDirectiveKind DKind = DSAStack->getDirective(Level); 2313 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 2314 getOpenMPCaptureRegions(CaptureRegions, DKind); 2315 if (isOpenMPTaskingDirective(CaptureRegions[CapLevel]) && 2316 (IsTriviallyCopyable || 2317 !isOpenMPTaskLoopDirective(CaptureRegions[CapLevel]))) { 2318 if (DSAStack->hasExplicitDSA( 2319 D, 2320 [](OpenMPClauseKind K, bool) { return K == OMPC_firstprivate; }, 2321 Level, /*NotLastprivate=*/true)) 2322 return OMPC_firstprivate; 2323 DSAStackTy::DSAVarData DVar = DSAStack->getImplicitDSA(D, Level); 2324 if (DVar.CKind != OMPC_shared && 2325 !DSAStack->isLoopControlVariable(D, Level).first && !DVar.RefExpr) { 2326 DSAStack->addImplicitTaskFirstprivate(Level, D); 2327 return OMPC_firstprivate; 2328 } 2329 } 2330 } 2331 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { 2332 if (DSAStack->getAssociatedLoops() > 0 && 2333 !DSAStack->isLoopStarted()) { 2334 DSAStack->resetPossibleLoopCounter(D); 2335 DSAStack->loopStart(); 2336 return OMPC_private; 2337 } 2338 if ((DSAStack->getPossiblyLoopCunter() == D->getCanonicalDecl() || 2339 DSAStack->isLoopControlVariable(D).first) && 2340 !DSAStack->hasExplicitDSA( 2341 D, [](OpenMPClauseKind K, bool) { return K != OMPC_private; }, 2342 Level) && 2343 !isOpenMPSimdDirective(DSAStack->getCurrentDirective())) 2344 return OMPC_private; 2345 } 2346 if (const auto *VD = dyn_cast<VarDecl>(D)) { 2347 if (DSAStack->isThreadPrivate(const_cast<VarDecl *>(VD)) && 2348 DSAStack->isForceVarCapturing() && 2349 !DSAStack->hasExplicitDSA( 2350 D, [](OpenMPClauseKind K, bool) { return K == OMPC_copyin; }, 2351 Level)) 2352 return OMPC_private; 2353 } 2354 // User-defined allocators are private since they must be defined in the 2355 // context of target region. 2356 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level) && 2357 DSAStack->isUsesAllocatorsDecl(Level, D).getValueOr( 2358 DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait) == 2359 DSAStackTy::UsesAllocatorsDeclKind::UserDefinedAllocator) 2360 return OMPC_private; 2361 return (DSAStack->hasExplicitDSA( 2362 D, [](OpenMPClauseKind K, bool) { return K == OMPC_private; }, 2363 Level) || 2364 (DSAStack->isClauseParsingMode() && 2365 DSAStack->getClauseParsingMode() == OMPC_private) || 2366 // Consider taskgroup reduction descriptor variable a private 2367 // to avoid possible capture in the region. 2368 (DSAStack->hasExplicitDirective( 2369 [](OpenMPDirectiveKind K) { 2370 return K == OMPD_taskgroup || 2371 ((isOpenMPParallelDirective(K) || 2372 isOpenMPWorksharingDirective(K)) && 2373 !isOpenMPSimdDirective(K)); 2374 }, 2375 Level) && 2376 DSAStack->isTaskgroupReductionRef(D, Level))) 2377 ? OMPC_private 2378 : OMPC_unknown; 2379 } 2380 2381 void Sema::setOpenMPCaptureKind(FieldDecl *FD, const ValueDecl *D, 2382 unsigned Level) { 2383 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 2384 D = getCanonicalDecl(D); 2385 OpenMPClauseKind OMPC = OMPC_unknown; 2386 for (unsigned I = DSAStack->getNestingLevel() + 1; I > Level; --I) { 2387 const unsigned NewLevel = I - 1; 2388 if (DSAStack->hasExplicitDSA( 2389 D, 2390 [&OMPC](const OpenMPClauseKind K, bool AppliedToPointee) { 2391 if (isOpenMPPrivate(K) && !AppliedToPointee) { 2392 OMPC = K; 2393 return true; 2394 } 2395 return false; 2396 }, 2397 NewLevel)) 2398 break; 2399 if (DSAStack->checkMappableExprComponentListsForDeclAtLevel( 2400 D, NewLevel, 2401 [](OMPClauseMappableExprCommon::MappableExprComponentListRef, 2402 OpenMPClauseKind) { return true; })) { 2403 OMPC = OMPC_map; 2404 break; 2405 } 2406 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, 2407 NewLevel)) { 2408 OMPC = OMPC_map; 2409 if (DSAStack->mustBeFirstprivateAtLevel( 2410 NewLevel, getVariableCategoryFromDecl(LangOpts, D))) 2411 OMPC = OMPC_firstprivate; 2412 break; 2413 } 2414 } 2415 if (OMPC != OMPC_unknown) 2416 FD->addAttr(OMPCaptureKindAttr::CreateImplicit(Context, unsigned(OMPC))); 2417 } 2418 2419 bool Sema::isOpenMPTargetCapturedDecl(const ValueDecl *D, unsigned Level, 2420 unsigned CaptureLevel) const { 2421 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 2422 // Return true if the current level is no longer enclosed in a target region. 2423 2424 SmallVector<OpenMPDirectiveKind, 4> Regions; 2425 getOpenMPCaptureRegions(Regions, DSAStack->getDirective(Level)); 2426 const auto *VD = dyn_cast<VarDecl>(D); 2427 return VD && !VD->hasLocalStorage() && 2428 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, 2429 Level) && 2430 Regions[CaptureLevel] != OMPD_task; 2431 } 2432 2433 bool Sema::isOpenMPGlobalCapturedDecl(ValueDecl *D, unsigned Level, 2434 unsigned CaptureLevel) const { 2435 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 2436 // Return true if the current level is no longer enclosed in a target region. 2437 2438 if (const auto *VD = dyn_cast<VarDecl>(D)) { 2439 if (!VD->hasLocalStorage()) { 2440 if (isInOpenMPTargetExecutionDirective()) 2441 return true; 2442 DSAStackTy::DSAVarData TopDVar = 2443 DSAStack->getTopDSA(D, /*FromParent=*/false); 2444 unsigned NumLevels = 2445 getOpenMPCaptureLevels(DSAStack->getDirective(Level)); 2446 if (Level == 0) 2447 return (NumLevels == CaptureLevel + 1) && TopDVar.CKind != OMPC_shared; 2448 do { 2449 --Level; 2450 DSAStackTy::DSAVarData DVar = DSAStack->getImplicitDSA(D, Level); 2451 if (DVar.CKind != OMPC_shared) 2452 return true; 2453 } while (Level > 0); 2454 } 2455 } 2456 return true; 2457 } 2458 2459 void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; } 2460 2461 void Sema::ActOnOpenMPBeginDeclareVariant(SourceLocation Loc, 2462 OMPTraitInfo &TI) { 2463 OMPDeclareVariantScopes.push_back(OMPDeclareVariantScope(TI)); 2464 } 2465 2466 void Sema::ActOnOpenMPEndDeclareVariant() { 2467 assert(isInOpenMPDeclareVariantScope() && 2468 "Not in OpenMP declare variant scope!"); 2469 2470 OMPDeclareVariantScopes.pop_back(); 2471 } 2472 2473 void Sema::finalizeOpenMPDelayedAnalysis(const FunctionDecl *Caller, 2474 const FunctionDecl *Callee, 2475 SourceLocation Loc) { 2476 assert(LangOpts.OpenMP && "Expected OpenMP compilation mode."); 2477 Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy = 2478 OMPDeclareTargetDeclAttr::getDeviceType(Caller->getMostRecentDecl()); 2479 // Ignore host functions during device analyzis. 2480 if (LangOpts.OpenMPIsDevice && DevTy && 2481 *DevTy == OMPDeclareTargetDeclAttr::DT_Host) 2482 return; 2483 // Ignore nohost functions during host analyzis. 2484 if (!LangOpts.OpenMPIsDevice && DevTy && 2485 *DevTy == OMPDeclareTargetDeclAttr::DT_NoHost) 2486 return; 2487 const FunctionDecl *FD = Callee->getMostRecentDecl(); 2488 DevTy = OMPDeclareTargetDeclAttr::getDeviceType(FD); 2489 if (LangOpts.OpenMPIsDevice && DevTy && 2490 *DevTy == OMPDeclareTargetDeclAttr::DT_Host) { 2491 // Diagnose host function called during device codegen. 2492 StringRef HostDevTy = 2493 getOpenMPSimpleClauseTypeName(OMPC_device_type, OMPC_DEVICE_TYPE_host); 2494 Diag(Loc, diag::err_omp_wrong_device_function_call) << HostDevTy << 0; 2495 Diag(*OMPDeclareTargetDeclAttr::getLocation(FD), 2496 diag::note_omp_marked_device_type_here) 2497 << HostDevTy; 2498 return; 2499 } 2500 if (!LangOpts.OpenMPIsDevice && DevTy && 2501 *DevTy == OMPDeclareTargetDeclAttr::DT_NoHost) { 2502 // Diagnose nohost function called during host codegen. 2503 StringRef NoHostDevTy = getOpenMPSimpleClauseTypeName( 2504 OMPC_device_type, OMPC_DEVICE_TYPE_nohost); 2505 Diag(Loc, diag::err_omp_wrong_device_function_call) << NoHostDevTy << 1; 2506 Diag(*OMPDeclareTargetDeclAttr::getLocation(FD), 2507 diag::note_omp_marked_device_type_here) 2508 << NoHostDevTy; 2509 } 2510 } 2511 2512 void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind, 2513 const DeclarationNameInfo &DirName, 2514 Scope *CurScope, SourceLocation Loc) { 2515 DSAStack->push(DKind, DirName, CurScope, Loc); 2516 PushExpressionEvaluationContext( 2517 ExpressionEvaluationContext::PotentiallyEvaluated); 2518 } 2519 2520 void Sema::StartOpenMPClause(OpenMPClauseKind K) { 2521 DSAStack->setClauseParsingMode(K); 2522 } 2523 2524 void Sema::EndOpenMPClause() { 2525 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown); 2526 } 2527 2528 static std::pair<ValueDecl *, bool> 2529 getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc, 2530 SourceRange &ERange, bool AllowArraySection = false); 2531 2532 /// Check consistency of the reduction clauses. 2533 static void checkReductionClauses(Sema &S, DSAStackTy *Stack, 2534 ArrayRef<OMPClause *> Clauses) { 2535 bool InscanFound = false; 2536 SourceLocation InscanLoc; 2537 // OpenMP 5.0, 2.19.5.4 reduction Clause, Restrictions. 2538 // A reduction clause without the inscan reduction-modifier may not appear on 2539 // a construct on which a reduction clause with the inscan reduction-modifier 2540 // appears. 2541 for (OMPClause *C : Clauses) { 2542 if (C->getClauseKind() != OMPC_reduction) 2543 continue; 2544 auto *RC = cast<OMPReductionClause>(C); 2545 if (RC->getModifier() == OMPC_REDUCTION_inscan) { 2546 InscanFound = true; 2547 InscanLoc = RC->getModifierLoc(); 2548 continue; 2549 } 2550 if (RC->getModifier() == OMPC_REDUCTION_task) { 2551 // OpenMP 5.0, 2.19.5.4 reduction Clause. 2552 // A reduction clause with the task reduction-modifier may only appear on 2553 // a parallel construct, a worksharing construct or a combined or 2554 // composite construct for which any of the aforementioned constructs is a 2555 // constituent construct and simd or loop are not constituent constructs. 2556 OpenMPDirectiveKind CurDir = Stack->getCurrentDirective(); 2557 if (!(isOpenMPParallelDirective(CurDir) || 2558 isOpenMPWorksharingDirective(CurDir)) || 2559 isOpenMPSimdDirective(CurDir)) 2560 S.Diag(RC->getModifierLoc(), 2561 diag::err_omp_reduction_task_not_parallel_or_worksharing); 2562 continue; 2563 } 2564 } 2565 if (InscanFound) { 2566 for (OMPClause *C : Clauses) { 2567 if (C->getClauseKind() != OMPC_reduction) 2568 continue; 2569 auto *RC = cast<OMPReductionClause>(C); 2570 if (RC->getModifier() != OMPC_REDUCTION_inscan) { 2571 S.Diag(RC->getModifier() == OMPC_REDUCTION_unknown 2572 ? RC->getBeginLoc() 2573 : RC->getModifierLoc(), 2574 diag::err_omp_inscan_reduction_expected); 2575 S.Diag(InscanLoc, diag::note_omp_previous_inscan_reduction); 2576 continue; 2577 } 2578 for (Expr *Ref : RC->varlists()) { 2579 assert(Ref && "NULL expr in OpenMP nontemporal clause."); 2580 SourceLocation ELoc; 2581 SourceRange ERange; 2582 Expr *SimpleRefExpr = Ref; 2583 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange, 2584 /*AllowArraySection=*/true); 2585 ValueDecl *D = Res.first; 2586 if (!D) 2587 continue; 2588 if (!Stack->isUsedInScanDirective(getCanonicalDecl(D))) { 2589 S.Diag(Ref->getExprLoc(), 2590 diag::err_omp_reduction_not_inclusive_exclusive) 2591 << Ref->getSourceRange(); 2592 } 2593 } 2594 } 2595 } 2596 } 2597 2598 static void checkAllocateClauses(Sema &S, DSAStackTy *Stack, 2599 ArrayRef<OMPClause *> Clauses); 2600 static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr, 2601 bool WithInit); 2602 2603 static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack, 2604 const ValueDecl *D, 2605 const DSAStackTy::DSAVarData &DVar, 2606 bool IsLoopIterVar = false); 2607 2608 void Sema::EndOpenMPDSABlock(Stmt *CurDirective) { 2609 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1] 2610 // A variable of class type (or array thereof) that appears in a lastprivate 2611 // clause requires an accessible, unambiguous default constructor for the 2612 // class type, unless the list item is also specified in a firstprivate 2613 // clause. 2614 if (const auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) { 2615 for (OMPClause *C : D->clauses()) { 2616 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) { 2617 SmallVector<Expr *, 8> PrivateCopies; 2618 for (Expr *DE : Clause->varlists()) { 2619 if (DE->isValueDependent() || DE->isTypeDependent()) { 2620 PrivateCopies.push_back(nullptr); 2621 continue; 2622 } 2623 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens()); 2624 auto *VD = cast<VarDecl>(DRE->getDecl()); 2625 QualType Type = VD->getType().getNonReferenceType(); 2626 const DSAStackTy::DSAVarData DVar = 2627 DSAStack->getTopDSA(VD, /*FromParent=*/false); 2628 if (DVar.CKind == OMPC_lastprivate) { 2629 // Generate helper private variable and initialize it with the 2630 // default value. The address of the original variable is replaced 2631 // by the address of the new private variable in CodeGen. This new 2632 // variable is not added to IdResolver, so the code in the OpenMP 2633 // region uses original variable for proper diagnostics. 2634 VarDecl *VDPrivate = buildVarDecl( 2635 *this, DE->getExprLoc(), Type.getUnqualifiedType(), 2636 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr, DRE); 2637 ActOnUninitializedDecl(VDPrivate); 2638 if (VDPrivate->isInvalidDecl()) { 2639 PrivateCopies.push_back(nullptr); 2640 continue; 2641 } 2642 PrivateCopies.push_back(buildDeclRefExpr( 2643 *this, VDPrivate, DE->getType(), DE->getExprLoc())); 2644 } else { 2645 // The variable is also a firstprivate, so initialization sequence 2646 // for private copy is generated already. 2647 PrivateCopies.push_back(nullptr); 2648 } 2649 } 2650 Clause->setPrivateCopies(PrivateCopies); 2651 continue; 2652 } 2653 // Finalize nontemporal clause by handling private copies, if any. 2654 if (auto *Clause = dyn_cast<OMPNontemporalClause>(C)) { 2655 SmallVector<Expr *, 8> PrivateRefs; 2656 for (Expr *RefExpr : Clause->varlists()) { 2657 assert(RefExpr && "NULL expr in OpenMP nontemporal clause."); 2658 SourceLocation ELoc; 2659 SourceRange ERange; 2660 Expr *SimpleRefExpr = RefExpr; 2661 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 2662 if (Res.second) 2663 // It will be analyzed later. 2664 PrivateRefs.push_back(RefExpr); 2665 ValueDecl *D = Res.first; 2666 if (!D) 2667 continue; 2668 2669 const DSAStackTy::DSAVarData DVar = 2670 DSAStack->getTopDSA(D, /*FromParent=*/false); 2671 PrivateRefs.push_back(DVar.PrivateCopy ? DVar.PrivateCopy 2672 : SimpleRefExpr); 2673 } 2674 Clause->setPrivateRefs(PrivateRefs); 2675 continue; 2676 } 2677 if (auto *Clause = dyn_cast<OMPUsesAllocatorsClause>(C)) { 2678 for (unsigned I = 0, E = Clause->getNumberOfAllocators(); I < E; ++I) { 2679 OMPUsesAllocatorsClause::Data D = Clause->getAllocatorData(I); 2680 auto *DRE = dyn_cast<DeclRefExpr>(D.Allocator->IgnoreParenImpCasts()); 2681 if (!DRE) 2682 continue; 2683 ValueDecl *VD = DRE->getDecl(); 2684 if (!VD || !isa<VarDecl>(VD)) 2685 continue; 2686 DSAStackTy::DSAVarData DVar = 2687 DSAStack->getTopDSA(VD, /*FromParent=*/false); 2688 // OpenMP [2.12.5, target Construct] 2689 // Memory allocators that appear in a uses_allocators clause cannot 2690 // appear in other data-sharing attribute clauses or data-mapping 2691 // attribute clauses in the same construct. 2692 Expr *MapExpr = nullptr; 2693 if (DVar.RefExpr || 2694 DSAStack->checkMappableExprComponentListsForDecl( 2695 VD, /*CurrentRegionOnly=*/true, 2696 [VD, &MapExpr]( 2697 OMPClauseMappableExprCommon::MappableExprComponentListRef 2698 MapExprComponents, 2699 OpenMPClauseKind C) { 2700 auto MI = MapExprComponents.rbegin(); 2701 auto ME = MapExprComponents.rend(); 2702 if (MI != ME && 2703 MI->getAssociatedDeclaration()->getCanonicalDecl() == 2704 VD->getCanonicalDecl()) { 2705 MapExpr = MI->getAssociatedExpression(); 2706 return true; 2707 } 2708 return false; 2709 })) { 2710 Diag(D.Allocator->getExprLoc(), 2711 diag::err_omp_allocator_used_in_clauses) 2712 << D.Allocator->getSourceRange(); 2713 if (DVar.RefExpr) 2714 reportOriginalDsa(*this, DSAStack, VD, DVar); 2715 else 2716 Diag(MapExpr->getExprLoc(), diag::note_used_here) 2717 << MapExpr->getSourceRange(); 2718 } 2719 } 2720 continue; 2721 } 2722 } 2723 // Check allocate clauses. 2724 if (!CurContext->isDependentContext()) 2725 checkAllocateClauses(*this, DSAStack, D->clauses()); 2726 checkReductionClauses(*this, DSAStack, D->clauses()); 2727 } 2728 2729 DSAStack->pop(); 2730 DiscardCleanupsInEvaluationContext(); 2731 PopExpressionEvaluationContext(); 2732 } 2733 2734 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, 2735 Expr *NumIterations, Sema &SemaRef, 2736 Scope *S, DSAStackTy *Stack); 2737 2738 namespace { 2739 2740 class VarDeclFilterCCC final : public CorrectionCandidateCallback { 2741 private: 2742 Sema &SemaRef; 2743 2744 public: 2745 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {} 2746 bool ValidateCandidate(const TypoCorrection &Candidate) override { 2747 NamedDecl *ND = Candidate.getCorrectionDecl(); 2748 if (const auto *VD = dyn_cast_or_null<VarDecl>(ND)) { 2749 return VD->hasGlobalStorage() && 2750 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), 2751 SemaRef.getCurScope()); 2752 } 2753 return false; 2754 } 2755 2756 std::unique_ptr<CorrectionCandidateCallback> clone() override { 2757 return std::make_unique<VarDeclFilterCCC>(*this); 2758 } 2759 2760 }; 2761 2762 class VarOrFuncDeclFilterCCC final : public CorrectionCandidateCallback { 2763 private: 2764 Sema &SemaRef; 2765 2766 public: 2767 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {} 2768 bool ValidateCandidate(const TypoCorrection &Candidate) override { 2769 NamedDecl *ND = Candidate.getCorrectionDecl(); 2770 if (ND && ((isa<VarDecl>(ND) && ND->getKind() == Decl::Var) || 2771 isa<FunctionDecl>(ND))) { 2772 return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), 2773 SemaRef.getCurScope()); 2774 } 2775 return false; 2776 } 2777 2778 std::unique_ptr<CorrectionCandidateCallback> clone() override { 2779 return std::make_unique<VarOrFuncDeclFilterCCC>(*this); 2780 } 2781 }; 2782 2783 } // namespace 2784 2785 ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope, 2786 CXXScopeSpec &ScopeSpec, 2787 const DeclarationNameInfo &Id, 2788 OpenMPDirectiveKind Kind) { 2789 LookupResult Lookup(*this, Id, LookupOrdinaryName); 2790 LookupParsedName(Lookup, CurScope, &ScopeSpec, true); 2791 2792 if (Lookup.isAmbiguous()) 2793 return ExprError(); 2794 2795 VarDecl *VD; 2796 if (!Lookup.isSingleResult()) { 2797 VarDeclFilterCCC CCC(*this); 2798 if (TypoCorrection Corrected = 2799 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, CCC, 2800 CTK_ErrorRecovery)) { 2801 diagnoseTypo(Corrected, 2802 PDiag(Lookup.empty() 2803 ? diag::err_undeclared_var_use_suggest 2804 : diag::err_omp_expected_var_arg_suggest) 2805 << Id.getName()); 2806 VD = Corrected.getCorrectionDeclAs<VarDecl>(); 2807 } else { 2808 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use 2809 : diag::err_omp_expected_var_arg) 2810 << Id.getName(); 2811 return ExprError(); 2812 } 2813 } else if (!(VD = Lookup.getAsSingle<VarDecl>())) { 2814 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName(); 2815 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at); 2816 return ExprError(); 2817 } 2818 Lookup.suppressDiagnostics(); 2819 2820 // OpenMP [2.9.2, Syntax, C/C++] 2821 // Variables must be file-scope, namespace-scope, or static block-scope. 2822 if (Kind == OMPD_threadprivate && !VD->hasGlobalStorage()) { 2823 Diag(Id.getLoc(), diag::err_omp_global_var_arg) 2824 << getOpenMPDirectiveName(Kind) << !VD->isStaticLocal(); 2825 bool IsDecl = 2826 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2827 Diag(VD->getLocation(), 2828 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2829 << VD; 2830 return ExprError(); 2831 } 2832 2833 VarDecl *CanonicalVD = VD->getCanonicalDecl(); 2834 NamedDecl *ND = CanonicalVD; 2835 // OpenMP [2.9.2, Restrictions, C/C++, p.2] 2836 // A threadprivate directive for file-scope variables must appear outside 2837 // any definition or declaration. 2838 if (CanonicalVD->getDeclContext()->isTranslationUnit() && 2839 !getCurLexicalContext()->isTranslationUnit()) { 2840 Diag(Id.getLoc(), diag::err_omp_var_scope) 2841 << getOpenMPDirectiveName(Kind) << VD; 2842 bool IsDecl = 2843 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2844 Diag(VD->getLocation(), 2845 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2846 << VD; 2847 return ExprError(); 2848 } 2849 // OpenMP [2.9.2, Restrictions, C/C++, p.3] 2850 // A threadprivate directive for static class member variables must appear 2851 // in the class definition, in the same scope in which the member 2852 // variables are declared. 2853 if (CanonicalVD->isStaticDataMember() && 2854 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) { 2855 Diag(Id.getLoc(), diag::err_omp_var_scope) 2856 << getOpenMPDirectiveName(Kind) << VD; 2857 bool IsDecl = 2858 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2859 Diag(VD->getLocation(), 2860 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2861 << VD; 2862 return ExprError(); 2863 } 2864 // OpenMP [2.9.2, Restrictions, C/C++, p.4] 2865 // A threadprivate directive for namespace-scope variables must appear 2866 // outside any definition or declaration other than the namespace 2867 // definition itself. 2868 if (CanonicalVD->getDeclContext()->isNamespace() && 2869 (!getCurLexicalContext()->isFileContext() || 2870 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) { 2871 Diag(Id.getLoc(), diag::err_omp_var_scope) 2872 << getOpenMPDirectiveName(Kind) << VD; 2873 bool IsDecl = 2874 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2875 Diag(VD->getLocation(), 2876 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2877 << VD; 2878 return ExprError(); 2879 } 2880 // OpenMP [2.9.2, Restrictions, C/C++, p.6] 2881 // A threadprivate directive for static block-scope variables must appear 2882 // in the scope of the variable and not in a nested scope. 2883 if (CanonicalVD->isLocalVarDecl() && CurScope && 2884 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) { 2885 Diag(Id.getLoc(), diag::err_omp_var_scope) 2886 << getOpenMPDirectiveName(Kind) << VD; 2887 bool IsDecl = 2888 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2889 Diag(VD->getLocation(), 2890 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2891 << VD; 2892 return ExprError(); 2893 } 2894 2895 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6] 2896 // A threadprivate directive must lexically precede all references to any 2897 // of the variables in its list. 2898 if (Kind == OMPD_threadprivate && VD->isUsed() && 2899 !DSAStack->isThreadPrivate(VD)) { 2900 Diag(Id.getLoc(), diag::err_omp_var_used) 2901 << getOpenMPDirectiveName(Kind) << VD; 2902 return ExprError(); 2903 } 2904 2905 QualType ExprType = VD->getType().getNonReferenceType(); 2906 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(), 2907 SourceLocation(), VD, 2908 /*RefersToEnclosingVariableOrCapture=*/false, 2909 Id.getLoc(), ExprType, VK_LValue); 2910 } 2911 2912 Sema::DeclGroupPtrTy 2913 Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc, 2914 ArrayRef<Expr *> VarList) { 2915 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) { 2916 CurContext->addDecl(D); 2917 return DeclGroupPtrTy::make(DeclGroupRef(D)); 2918 } 2919 return nullptr; 2920 } 2921 2922 namespace { 2923 class LocalVarRefChecker final 2924 : public ConstStmtVisitor<LocalVarRefChecker, bool> { 2925 Sema &SemaRef; 2926 2927 public: 2928 bool VisitDeclRefExpr(const DeclRefExpr *E) { 2929 if (const auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 2930 if (VD->hasLocalStorage()) { 2931 SemaRef.Diag(E->getBeginLoc(), 2932 diag::err_omp_local_var_in_threadprivate_init) 2933 << E->getSourceRange(); 2934 SemaRef.Diag(VD->getLocation(), diag::note_defined_here) 2935 << VD << VD->getSourceRange(); 2936 return true; 2937 } 2938 } 2939 return false; 2940 } 2941 bool VisitStmt(const Stmt *S) { 2942 for (const Stmt *Child : S->children()) { 2943 if (Child && Visit(Child)) 2944 return true; 2945 } 2946 return false; 2947 } 2948 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {} 2949 }; 2950 } // namespace 2951 2952 OMPThreadPrivateDecl * 2953 Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) { 2954 SmallVector<Expr *, 8> Vars; 2955 for (Expr *RefExpr : VarList) { 2956 auto *DE = cast<DeclRefExpr>(RefExpr); 2957 auto *VD = cast<VarDecl>(DE->getDecl()); 2958 SourceLocation ILoc = DE->getExprLoc(); 2959 2960 // Mark variable as used. 2961 VD->setReferenced(); 2962 VD->markUsed(Context); 2963 2964 QualType QType = VD->getType(); 2965 if (QType->isDependentType() || QType->isInstantiationDependentType()) { 2966 // It will be analyzed later. 2967 Vars.push_back(DE); 2968 continue; 2969 } 2970 2971 // OpenMP [2.9.2, Restrictions, C/C++, p.10] 2972 // A threadprivate variable must not have an incomplete type. 2973 if (RequireCompleteType(ILoc, VD->getType(), 2974 diag::err_omp_threadprivate_incomplete_type)) { 2975 continue; 2976 } 2977 2978 // OpenMP [2.9.2, Restrictions, C/C++, p.10] 2979 // A threadprivate variable must not have a reference type. 2980 if (VD->getType()->isReferenceType()) { 2981 Diag(ILoc, diag::err_omp_ref_type_arg) 2982 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType(); 2983 bool IsDecl = 2984 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2985 Diag(VD->getLocation(), 2986 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2987 << VD; 2988 continue; 2989 } 2990 2991 // Check if this is a TLS variable. If TLS is not being supported, produce 2992 // the corresponding diagnostic. 2993 if ((VD->getTLSKind() != VarDecl::TLS_None && 2994 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() && 2995 getLangOpts().OpenMPUseTLS && 2996 getASTContext().getTargetInfo().isTLSSupported())) || 2997 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() && 2998 !VD->isLocalVarDecl())) { 2999 Diag(ILoc, diag::err_omp_var_thread_local) 3000 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1); 3001 bool IsDecl = 3002 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 3003 Diag(VD->getLocation(), 3004 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 3005 << VD; 3006 continue; 3007 } 3008 3009 // Check if initial value of threadprivate variable reference variable with 3010 // local storage (it is not supported by runtime). 3011 if (const Expr *Init = VD->getAnyInitializer()) { 3012 LocalVarRefChecker Checker(*this); 3013 if (Checker.Visit(Init)) 3014 continue; 3015 } 3016 3017 Vars.push_back(RefExpr); 3018 DSAStack->addDSA(VD, DE, OMPC_threadprivate); 3019 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit( 3020 Context, SourceRange(Loc, Loc))); 3021 if (ASTMutationListener *ML = Context.getASTMutationListener()) 3022 ML->DeclarationMarkedOpenMPThreadPrivate(VD); 3023 } 3024 OMPThreadPrivateDecl *D = nullptr; 3025 if (!Vars.empty()) { 3026 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc, 3027 Vars); 3028 D->setAccess(AS_public); 3029 } 3030 return D; 3031 } 3032 3033 static OMPAllocateDeclAttr::AllocatorTypeTy 3034 getAllocatorKind(Sema &S, DSAStackTy *Stack, Expr *Allocator) { 3035 if (!Allocator) 3036 return OMPAllocateDeclAttr::OMPNullMemAlloc; 3037 if (Allocator->isTypeDependent() || Allocator->isValueDependent() || 3038 Allocator->isInstantiationDependent() || 3039 Allocator->containsUnexpandedParameterPack()) 3040 return OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; 3041 auto AllocatorKindRes = OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; 3042 const Expr *AE = Allocator->IgnoreParenImpCasts(); 3043 for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) { 3044 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I); 3045 const Expr *DefAllocator = Stack->getAllocator(AllocatorKind); 3046 llvm::FoldingSetNodeID AEId, DAEId; 3047 AE->Profile(AEId, S.getASTContext(), /*Canonical=*/true); 3048 DefAllocator->Profile(DAEId, S.getASTContext(), /*Canonical=*/true); 3049 if (AEId == DAEId) { 3050 AllocatorKindRes = AllocatorKind; 3051 break; 3052 } 3053 } 3054 return AllocatorKindRes; 3055 } 3056 3057 static bool checkPreviousOMPAllocateAttribute( 3058 Sema &S, DSAStackTy *Stack, Expr *RefExpr, VarDecl *VD, 3059 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, Expr *Allocator) { 3060 if (!VD->hasAttr<OMPAllocateDeclAttr>()) 3061 return false; 3062 const auto *A = VD->getAttr<OMPAllocateDeclAttr>(); 3063 Expr *PrevAllocator = A->getAllocator(); 3064 OMPAllocateDeclAttr::AllocatorTypeTy PrevAllocatorKind = 3065 getAllocatorKind(S, Stack, PrevAllocator); 3066 bool AllocatorsMatch = AllocatorKind == PrevAllocatorKind; 3067 if (AllocatorsMatch && 3068 AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc && 3069 Allocator && PrevAllocator) { 3070 const Expr *AE = Allocator->IgnoreParenImpCasts(); 3071 const Expr *PAE = PrevAllocator->IgnoreParenImpCasts(); 3072 llvm::FoldingSetNodeID AEId, PAEId; 3073 AE->Profile(AEId, S.Context, /*Canonical=*/true); 3074 PAE->Profile(PAEId, S.Context, /*Canonical=*/true); 3075 AllocatorsMatch = AEId == PAEId; 3076 } 3077 if (!AllocatorsMatch) { 3078 SmallString<256> AllocatorBuffer; 3079 llvm::raw_svector_ostream AllocatorStream(AllocatorBuffer); 3080 if (Allocator) 3081 Allocator->printPretty(AllocatorStream, nullptr, S.getPrintingPolicy()); 3082 SmallString<256> PrevAllocatorBuffer; 3083 llvm::raw_svector_ostream PrevAllocatorStream(PrevAllocatorBuffer); 3084 if (PrevAllocator) 3085 PrevAllocator->printPretty(PrevAllocatorStream, nullptr, 3086 S.getPrintingPolicy()); 3087 3088 SourceLocation AllocatorLoc = 3089 Allocator ? Allocator->getExprLoc() : RefExpr->getExprLoc(); 3090 SourceRange AllocatorRange = 3091 Allocator ? Allocator->getSourceRange() : RefExpr->getSourceRange(); 3092 SourceLocation PrevAllocatorLoc = 3093 PrevAllocator ? PrevAllocator->getExprLoc() : A->getLocation(); 3094 SourceRange PrevAllocatorRange = 3095 PrevAllocator ? PrevAllocator->getSourceRange() : A->getRange(); 3096 S.Diag(AllocatorLoc, diag::warn_omp_used_different_allocator) 3097 << (Allocator ? 1 : 0) << AllocatorStream.str() 3098 << (PrevAllocator ? 1 : 0) << PrevAllocatorStream.str() 3099 << AllocatorRange; 3100 S.Diag(PrevAllocatorLoc, diag::note_omp_previous_allocator) 3101 << PrevAllocatorRange; 3102 return true; 3103 } 3104 return false; 3105 } 3106 3107 static void 3108 applyOMPAllocateAttribute(Sema &S, VarDecl *VD, 3109 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, 3110 Expr *Allocator, SourceRange SR) { 3111 if (VD->hasAttr<OMPAllocateDeclAttr>()) 3112 return; 3113 if (Allocator && 3114 (Allocator->isTypeDependent() || Allocator->isValueDependent() || 3115 Allocator->isInstantiationDependent() || 3116 Allocator->containsUnexpandedParameterPack())) 3117 return; 3118 auto *A = OMPAllocateDeclAttr::CreateImplicit(S.Context, AllocatorKind, 3119 Allocator, SR); 3120 VD->addAttr(A); 3121 if (ASTMutationListener *ML = S.Context.getASTMutationListener()) 3122 ML->DeclarationMarkedOpenMPAllocate(VD, A); 3123 } 3124 3125 Sema::DeclGroupPtrTy Sema::ActOnOpenMPAllocateDirective( 3126 SourceLocation Loc, ArrayRef<Expr *> VarList, 3127 ArrayRef<OMPClause *> Clauses, DeclContext *Owner) { 3128 assert(Clauses.size() <= 1 && "Expected at most one clause."); 3129 Expr *Allocator = nullptr; 3130 if (Clauses.empty()) { 3131 // OpenMP 5.0, 2.11.3 allocate Directive, Restrictions. 3132 // allocate directives that appear in a target region must specify an 3133 // allocator clause unless a requires directive with the dynamic_allocators 3134 // clause is present in the same compilation unit. 3135 if (LangOpts.OpenMPIsDevice && 3136 !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>()) 3137 targetDiag(Loc, diag::err_expected_allocator_clause); 3138 } else { 3139 Allocator = cast<OMPAllocatorClause>(Clauses.back())->getAllocator(); 3140 } 3141 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind = 3142 getAllocatorKind(*this, DSAStack, Allocator); 3143 SmallVector<Expr *, 8> Vars; 3144 for (Expr *RefExpr : VarList) { 3145 auto *DE = cast<DeclRefExpr>(RefExpr); 3146 auto *VD = cast<VarDecl>(DE->getDecl()); 3147 3148 // Check if this is a TLS variable or global register. 3149 if (VD->getTLSKind() != VarDecl::TLS_None || 3150 VD->hasAttr<OMPThreadPrivateDeclAttr>() || 3151 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() && 3152 !VD->isLocalVarDecl())) 3153 continue; 3154 3155 // If the used several times in the allocate directive, the same allocator 3156 // must be used. 3157 if (checkPreviousOMPAllocateAttribute(*this, DSAStack, RefExpr, VD, 3158 AllocatorKind, Allocator)) 3159 continue; 3160 3161 // OpenMP, 2.11.3 allocate Directive, Restrictions, C / C++ 3162 // If a list item has a static storage type, the allocator expression in the 3163 // allocator clause must be a constant expression that evaluates to one of 3164 // the predefined memory allocator values. 3165 if (Allocator && VD->hasGlobalStorage()) { 3166 if (AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc) { 3167 Diag(Allocator->getExprLoc(), 3168 diag::err_omp_expected_predefined_allocator) 3169 << Allocator->getSourceRange(); 3170 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 3171 VarDecl::DeclarationOnly; 3172 Diag(VD->getLocation(), 3173 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 3174 << VD; 3175 continue; 3176 } 3177 } 3178 3179 Vars.push_back(RefExpr); 3180 applyOMPAllocateAttribute(*this, VD, AllocatorKind, Allocator, 3181 DE->getSourceRange()); 3182 } 3183 if (Vars.empty()) 3184 return nullptr; 3185 if (!Owner) 3186 Owner = getCurLexicalContext(); 3187 auto *D = OMPAllocateDecl::Create(Context, Owner, Loc, Vars, Clauses); 3188 D->setAccess(AS_public); 3189 Owner->addDecl(D); 3190 return DeclGroupPtrTy::make(DeclGroupRef(D)); 3191 } 3192 3193 Sema::DeclGroupPtrTy 3194 Sema::ActOnOpenMPRequiresDirective(SourceLocation Loc, 3195 ArrayRef<OMPClause *> ClauseList) { 3196 OMPRequiresDecl *D = nullptr; 3197 if (!CurContext->isFileContext()) { 3198 Diag(Loc, diag::err_omp_invalid_scope) << "requires"; 3199 } else { 3200 D = CheckOMPRequiresDecl(Loc, ClauseList); 3201 if (D) { 3202 CurContext->addDecl(D); 3203 DSAStack->addRequiresDecl(D); 3204 } 3205 } 3206 return DeclGroupPtrTy::make(DeclGroupRef(D)); 3207 } 3208 3209 void Sema::ActOnOpenMPAssumesDirective(SourceLocation Loc, 3210 OpenMPDirectiveKind DKind, 3211 ArrayRef<StringRef> Assumptions, 3212 bool SkippedClauses) { 3213 if (!SkippedClauses && Assumptions.empty()) 3214 Diag(Loc, diag::err_omp_no_clause_for_directive) 3215 << llvm::omp::getAllAssumeClauseOptions() 3216 << llvm::omp::getOpenMPDirectiveName(DKind); 3217 3218 auto *AA = AssumptionAttr::Create(Context, llvm::join(Assumptions, ","), Loc); 3219 if (DKind == llvm::omp::Directive::OMPD_begin_assumes) { 3220 OMPAssumeScoped.push_back(AA); 3221 return; 3222 } 3223 3224 // Global assumes without assumption clauses are ignored. 3225 if (Assumptions.empty()) 3226 return; 3227 3228 assert(DKind == llvm::omp::Directive::OMPD_assumes && 3229 "Unexpected omp assumption directive!"); 3230 OMPAssumeGlobal.push_back(AA); 3231 3232 // The OMPAssumeGlobal scope above will take care of new declarations but 3233 // we also want to apply the assumption to existing ones, e.g., to 3234 // declarations in included headers. To this end, we traverse all existing 3235 // declaration contexts and annotate function declarations here. 3236 SmallVector<DeclContext *, 8> DeclContexts; 3237 auto *Ctx = CurContext; 3238 while (Ctx->getLexicalParent()) 3239 Ctx = Ctx->getLexicalParent(); 3240 DeclContexts.push_back(Ctx); 3241 while (!DeclContexts.empty()) { 3242 DeclContext *DC = DeclContexts.pop_back_val(); 3243 for (auto *SubDC : DC->decls()) { 3244 if (SubDC->isInvalidDecl()) 3245 continue; 3246 if (auto *CTD = dyn_cast<ClassTemplateDecl>(SubDC)) { 3247 DeclContexts.push_back(CTD->getTemplatedDecl()); 3248 for (auto *S : CTD->specializations()) 3249 DeclContexts.push_back(S); 3250 continue; 3251 } 3252 if (auto *DC = dyn_cast<DeclContext>(SubDC)) 3253 DeclContexts.push_back(DC); 3254 if (auto *F = dyn_cast<FunctionDecl>(SubDC)) { 3255 F->addAttr(AA); 3256 continue; 3257 } 3258 } 3259 } 3260 } 3261 3262 void Sema::ActOnOpenMPEndAssumesDirective() { 3263 assert(isInOpenMPAssumeScope() && "Not in OpenMP assumes scope!"); 3264 OMPAssumeScoped.pop_back(); 3265 } 3266 3267 OMPRequiresDecl *Sema::CheckOMPRequiresDecl(SourceLocation Loc, 3268 ArrayRef<OMPClause *> ClauseList) { 3269 /// For target specific clauses, the requires directive cannot be 3270 /// specified after the handling of any of the target regions in the 3271 /// current compilation unit. 3272 ArrayRef<SourceLocation> TargetLocations = 3273 DSAStack->getEncounteredTargetLocs(); 3274 SourceLocation AtomicLoc = DSAStack->getAtomicDirectiveLoc(); 3275 if (!TargetLocations.empty() || !AtomicLoc.isInvalid()) { 3276 for (const OMPClause *CNew : ClauseList) { 3277 // Check if any of the requires clauses affect target regions. 3278 if (isa<OMPUnifiedSharedMemoryClause>(CNew) || 3279 isa<OMPUnifiedAddressClause>(CNew) || 3280 isa<OMPReverseOffloadClause>(CNew) || 3281 isa<OMPDynamicAllocatorsClause>(CNew)) { 3282 Diag(Loc, diag::err_omp_directive_before_requires) 3283 << "target" << getOpenMPClauseName(CNew->getClauseKind()); 3284 for (SourceLocation TargetLoc : TargetLocations) { 3285 Diag(TargetLoc, diag::note_omp_requires_encountered_directive) 3286 << "target"; 3287 } 3288 } else if (!AtomicLoc.isInvalid() && 3289 isa<OMPAtomicDefaultMemOrderClause>(CNew)) { 3290 Diag(Loc, diag::err_omp_directive_before_requires) 3291 << "atomic" << getOpenMPClauseName(CNew->getClauseKind()); 3292 Diag(AtomicLoc, diag::note_omp_requires_encountered_directive) 3293 << "atomic"; 3294 } 3295 } 3296 } 3297 3298 if (!DSAStack->hasDuplicateRequiresClause(ClauseList)) 3299 return OMPRequiresDecl::Create(Context, getCurLexicalContext(), Loc, 3300 ClauseList); 3301 return nullptr; 3302 } 3303 3304 static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack, 3305 const ValueDecl *D, 3306 const DSAStackTy::DSAVarData &DVar, 3307 bool IsLoopIterVar) { 3308 if (DVar.RefExpr) { 3309 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa) 3310 << getOpenMPClauseName(DVar.CKind); 3311 return; 3312 } 3313 enum { 3314 PDSA_StaticMemberShared, 3315 PDSA_StaticLocalVarShared, 3316 PDSA_LoopIterVarPrivate, 3317 PDSA_LoopIterVarLinear, 3318 PDSA_LoopIterVarLastprivate, 3319 PDSA_ConstVarShared, 3320 PDSA_GlobalVarShared, 3321 PDSA_TaskVarFirstprivate, 3322 PDSA_LocalVarPrivate, 3323 PDSA_Implicit 3324 } Reason = PDSA_Implicit; 3325 bool ReportHint = false; 3326 auto ReportLoc = D->getLocation(); 3327 auto *VD = dyn_cast<VarDecl>(D); 3328 if (IsLoopIterVar) { 3329 if (DVar.CKind == OMPC_private) 3330 Reason = PDSA_LoopIterVarPrivate; 3331 else if (DVar.CKind == OMPC_lastprivate) 3332 Reason = PDSA_LoopIterVarLastprivate; 3333 else 3334 Reason = PDSA_LoopIterVarLinear; 3335 } else if (isOpenMPTaskingDirective(DVar.DKind) && 3336 DVar.CKind == OMPC_firstprivate) { 3337 Reason = PDSA_TaskVarFirstprivate; 3338 ReportLoc = DVar.ImplicitDSALoc; 3339 } else if (VD && VD->isStaticLocal()) 3340 Reason = PDSA_StaticLocalVarShared; 3341 else if (VD && VD->isStaticDataMember()) 3342 Reason = PDSA_StaticMemberShared; 3343 else if (VD && VD->isFileVarDecl()) 3344 Reason = PDSA_GlobalVarShared; 3345 else if (D->getType().isConstant(SemaRef.getASTContext())) 3346 Reason = PDSA_ConstVarShared; 3347 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) { 3348 ReportHint = true; 3349 Reason = PDSA_LocalVarPrivate; 3350 } 3351 if (Reason != PDSA_Implicit) { 3352 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa) 3353 << Reason << ReportHint 3354 << getOpenMPDirectiveName(Stack->getCurrentDirective()); 3355 } else if (DVar.ImplicitDSALoc.isValid()) { 3356 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa) 3357 << getOpenMPClauseName(DVar.CKind); 3358 } 3359 } 3360 3361 static OpenMPMapClauseKind 3362 getMapClauseKindFromModifier(OpenMPDefaultmapClauseModifier M, 3363 bool IsAggregateOrDeclareTarget) { 3364 OpenMPMapClauseKind Kind = OMPC_MAP_unknown; 3365 switch (M) { 3366 case OMPC_DEFAULTMAP_MODIFIER_alloc: 3367 Kind = OMPC_MAP_alloc; 3368 break; 3369 case OMPC_DEFAULTMAP_MODIFIER_to: 3370 Kind = OMPC_MAP_to; 3371 break; 3372 case OMPC_DEFAULTMAP_MODIFIER_from: 3373 Kind = OMPC_MAP_from; 3374 break; 3375 case OMPC_DEFAULTMAP_MODIFIER_tofrom: 3376 Kind = OMPC_MAP_tofrom; 3377 break; 3378 case OMPC_DEFAULTMAP_MODIFIER_present: 3379 // OpenMP 5.1 [2.21.7.3] defaultmap clause, Description] 3380 // If implicit-behavior is present, each variable referenced in the 3381 // construct in the category specified by variable-category is treated as if 3382 // it had been listed in a map clause with the map-type of alloc and 3383 // map-type-modifier of present. 3384 Kind = OMPC_MAP_alloc; 3385 break; 3386 case OMPC_DEFAULTMAP_MODIFIER_firstprivate: 3387 case OMPC_DEFAULTMAP_MODIFIER_last: 3388 llvm_unreachable("Unexpected defaultmap implicit behavior"); 3389 case OMPC_DEFAULTMAP_MODIFIER_none: 3390 case OMPC_DEFAULTMAP_MODIFIER_default: 3391 case OMPC_DEFAULTMAP_MODIFIER_unknown: 3392 // IsAggregateOrDeclareTarget could be true if: 3393 // 1. the implicit behavior for aggregate is tofrom 3394 // 2. it's a declare target link 3395 if (IsAggregateOrDeclareTarget) { 3396 Kind = OMPC_MAP_tofrom; 3397 break; 3398 } 3399 llvm_unreachable("Unexpected defaultmap implicit behavior"); 3400 } 3401 assert(Kind != OMPC_MAP_unknown && "Expect map kind to be known"); 3402 return Kind; 3403 } 3404 3405 namespace { 3406 class DSAAttrChecker final : public StmtVisitor<DSAAttrChecker, void> { 3407 DSAStackTy *Stack; 3408 Sema &SemaRef; 3409 bool ErrorFound = false; 3410 bool TryCaptureCXXThisMembers = false; 3411 CapturedStmt *CS = nullptr; 3412 const static unsigned DefaultmapKindNum = OMPC_DEFAULTMAP_pointer + 1; 3413 llvm::SmallVector<Expr *, 4> ImplicitFirstprivate; 3414 llvm::SmallVector<Expr *, 4> ImplicitMap[DefaultmapKindNum][OMPC_MAP_delete]; 3415 llvm::SmallVector<OpenMPMapModifierKind, NumberOfOMPMapClauseModifiers> 3416 ImplicitMapModifier[DefaultmapKindNum]; 3417 Sema::VarsWithInheritedDSAType VarsWithInheritedDSA; 3418 llvm::SmallDenseSet<const ValueDecl *, 4> ImplicitDeclarations; 3419 3420 void VisitSubCaptures(OMPExecutableDirective *S) { 3421 // Check implicitly captured variables. 3422 if (!S->hasAssociatedStmt() || !S->getAssociatedStmt()) 3423 return; 3424 if (S->getDirectiveKind() == OMPD_atomic || 3425 S->getDirectiveKind() == OMPD_critical || 3426 S->getDirectiveKind() == OMPD_section || 3427 S->getDirectiveKind() == OMPD_master || 3428 isOpenMPLoopTransformationDirective(S->getDirectiveKind())) { 3429 Visit(S->getAssociatedStmt()); 3430 return; 3431 } 3432 visitSubCaptures(S->getInnermostCapturedStmt()); 3433 // Try to capture inner this->member references to generate correct mappings 3434 // and diagnostics. 3435 if (TryCaptureCXXThisMembers || 3436 (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) && 3437 llvm::any_of(S->getInnermostCapturedStmt()->captures(), 3438 [](const CapturedStmt::Capture &C) { 3439 return C.capturesThis(); 3440 }))) { 3441 bool SavedTryCaptureCXXThisMembers = TryCaptureCXXThisMembers; 3442 TryCaptureCXXThisMembers = true; 3443 Visit(S->getInnermostCapturedStmt()->getCapturedStmt()); 3444 TryCaptureCXXThisMembers = SavedTryCaptureCXXThisMembers; 3445 } 3446 // In tasks firstprivates are not captured anymore, need to analyze them 3447 // explicitly. 3448 if (isOpenMPTaskingDirective(S->getDirectiveKind()) && 3449 !isOpenMPTaskLoopDirective(S->getDirectiveKind())) { 3450 for (OMPClause *C : S->clauses()) 3451 if (auto *FC = dyn_cast<OMPFirstprivateClause>(C)) { 3452 for (Expr *Ref : FC->varlists()) 3453 Visit(Ref); 3454 } 3455 } 3456 } 3457 3458 public: 3459 void VisitDeclRefExpr(DeclRefExpr *E) { 3460 if (TryCaptureCXXThisMembers || E->isTypeDependent() || 3461 E->isValueDependent() || E->containsUnexpandedParameterPack() || 3462 E->isInstantiationDependent()) 3463 return; 3464 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 3465 // Check the datasharing rules for the expressions in the clauses. 3466 if (!CS) { 3467 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD)) 3468 if (!CED->hasAttr<OMPCaptureNoInitAttr>()) { 3469 Visit(CED->getInit()); 3470 return; 3471 } 3472 } else if (VD->isImplicit() || isa<OMPCapturedExprDecl>(VD)) 3473 // Do not analyze internal variables and do not enclose them into 3474 // implicit clauses. 3475 return; 3476 VD = VD->getCanonicalDecl(); 3477 // Skip internally declared variables. 3478 if (VD->hasLocalStorage() && CS && !CS->capturesVariable(VD) && 3479 !Stack->isImplicitTaskFirstprivate(VD)) 3480 return; 3481 // Skip allocators in uses_allocators clauses. 3482 if (Stack->isUsesAllocatorsDecl(VD).hasValue()) 3483 return; 3484 3485 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false); 3486 // Check if the variable has explicit DSA set and stop analysis if it so. 3487 if (DVar.RefExpr || !ImplicitDeclarations.insert(VD).second) 3488 return; 3489 3490 // Skip internally declared static variables. 3491 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 3492 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); 3493 if (VD->hasGlobalStorage() && CS && !CS->capturesVariable(VD) && 3494 (Stack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() || 3495 !Res || *Res != OMPDeclareTargetDeclAttr::MT_Link) && 3496 !Stack->isImplicitTaskFirstprivate(VD)) 3497 return; 3498 3499 SourceLocation ELoc = E->getExprLoc(); 3500 OpenMPDirectiveKind DKind = Stack->getCurrentDirective(); 3501 // The default(none) clause requires that each variable that is referenced 3502 // in the construct, and does not have a predetermined data-sharing 3503 // attribute, must have its data-sharing attribute explicitly determined 3504 // by being listed in a data-sharing attribute clause. 3505 if (DVar.CKind == OMPC_unknown && 3506 (Stack->getDefaultDSA() == DSA_none || 3507 Stack->getDefaultDSA() == DSA_firstprivate) && 3508 isImplicitOrExplicitTaskingRegion(DKind) && 3509 VarsWithInheritedDSA.count(VD) == 0) { 3510 bool InheritedDSA = Stack->getDefaultDSA() == DSA_none; 3511 if (!InheritedDSA && Stack->getDefaultDSA() == DSA_firstprivate) { 3512 DSAStackTy::DSAVarData DVar = 3513 Stack->getImplicitDSA(VD, /*FromParent=*/false); 3514 InheritedDSA = DVar.CKind == OMPC_unknown; 3515 } 3516 if (InheritedDSA) 3517 VarsWithInheritedDSA[VD] = E; 3518 return; 3519 } 3520 3521 // OpenMP 5.0 [2.19.7.2, defaultmap clause, Description] 3522 // If implicit-behavior is none, each variable referenced in the 3523 // construct that does not have a predetermined data-sharing attribute 3524 // and does not appear in a to or link clause on a declare target 3525 // directive must be listed in a data-mapping attribute clause, a 3526 // data-haring attribute clause (including a data-sharing attribute 3527 // clause on a combined construct where target. is one of the 3528 // constituent constructs), or an is_device_ptr clause. 3529 OpenMPDefaultmapClauseKind ClauseKind = 3530 getVariableCategoryFromDecl(SemaRef.getLangOpts(), VD); 3531 if (SemaRef.getLangOpts().OpenMP >= 50) { 3532 bool IsModifierNone = Stack->getDefaultmapModifier(ClauseKind) == 3533 OMPC_DEFAULTMAP_MODIFIER_none; 3534 if (DVar.CKind == OMPC_unknown && IsModifierNone && 3535 VarsWithInheritedDSA.count(VD) == 0 && !Res) { 3536 // Only check for data-mapping attribute and is_device_ptr here 3537 // since we have already make sure that the declaration does not 3538 // have a data-sharing attribute above 3539 if (!Stack->checkMappableExprComponentListsForDecl( 3540 VD, /*CurrentRegionOnly=*/true, 3541 [VD](OMPClauseMappableExprCommon::MappableExprComponentListRef 3542 MapExprComponents, 3543 OpenMPClauseKind) { 3544 auto MI = MapExprComponents.rbegin(); 3545 auto ME = MapExprComponents.rend(); 3546 return MI != ME && MI->getAssociatedDeclaration() == VD; 3547 })) { 3548 VarsWithInheritedDSA[VD] = E; 3549 return; 3550 } 3551 } 3552 } 3553 if (SemaRef.getLangOpts().OpenMP > 50) { 3554 bool IsModifierPresent = Stack->getDefaultmapModifier(ClauseKind) == 3555 OMPC_DEFAULTMAP_MODIFIER_present; 3556 if (IsModifierPresent) { 3557 if (llvm::find(ImplicitMapModifier[ClauseKind], 3558 OMPC_MAP_MODIFIER_present) == 3559 std::end(ImplicitMapModifier[ClauseKind])) { 3560 ImplicitMapModifier[ClauseKind].push_back( 3561 OMPC_MAP_MODIFIER_present); 3562 } 3563 } 3564 } 3565 3566 if (isOpenMPTargetExecutionDirective(DKind) && 3567 !Stack->isLoopControlVariable(VD).first) { 3568 if (!Stack->checkMappableExprComponentListsForDecl( 3569 VD, /*CurrentRegionOnly=*/true, 3570 [this](OMPClauseMappableExprCommon::MappableExprComponentListRef 3571 StackComponents, 3572 OpenMPClauseKind) { 3573 if (SemaRef.LangOpts.OpenMP >= 50) 3574 return !StackComponents.empty(); 3575 // Variable is used if it has been marked as an array, array 3576 // section, array shaping or the variable iself. 3577 return StackComponents.size() == 1 || 3578 std::all_of( 3579 std::next(StackComponents.rbegin()), 3580 StackComponents.rend(), 3581 [](const OMPClauseMappableExprCommon:: 3582 MappableComponent &MC) { 3583 return MC.getAssociatedDeclaration() == 3584 nullptr && 3585 (isa<OMPArraySectionExpr>( 3586 MC.getAssociatedExpression()) || 3587 isa<OMPArrayShapingExpr>( 3588 MC.getAssociatedExpression()) || 3589 isa<ArraySubscriptExpr>( 3590 MC.getAssociatedExpression())); 3591 }); 3592 })) { 3593 bool IsFirstprivate = false; 3594 // By default lambdas are captured as firstprivates. 3595 if (const auto *RD = 3596 VD->getType().getNonReferenceType()->getAsCXXRecordDecl()) 3597 IsFirstprivate = RD->isLambda(); 3598 IsFirstprivate = 3599 IsFirstprivate || (Stack->mustBeFirstprivate(ClauseKind) && !Res); 3600 if (IsFirstprivate) { 3601 ImplicitFirstprivate.emplace_back(E); 3602 } else { 3603 OpenMPDefaultmapClauseModifier M = 3604 Stack->getDefaultmapModifier(ClauseKind); 3605 OpenMPMapClauseKind Kind = getMapClauseKindFromModifier( 3606 M, ClauseKind == OMPC_DEFAULTMAP_aggregate || Res); 3607 ImplicitMap[ClauseKind][Kind].emplace_back(E); 3608 } 3609 return; 3610 } 3611 } 3612 3613 // OpenMP [2.9.3.6, Restrictions, p.2] 3614 // A list item that appears in a reduction clause of the innermost 3615 // enclosing worksharing or parallel construct may not be accessed in an 3616 // explicit task. 3617 DVar = Stack->hasInnermostDSA( 3618 VD, 3619 [](OpenMPClauseKind C, bool AppliedToPointee) { 3620 return C == OMPC_reduction && !AppliedToPointee; 3621 }, 3622 [](OpenMPDirectiveKind K) { 3623 return isOpenMPParallelDirective(K) || 3624 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K); 3625 }, 3626 /*FromParent=*/true); 3627 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) { 3628 ErrorFound = true; 3629 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); 3630 reportOriginalDsa(SemaRef, Stack, VD, DVar); 3631 return; 3632 } 3633 3634 // Define implicit data-sharing attributes for task. 3635 DVar = Stack->getImplicitDSA(VD, /*FromParent=*/false); 3636 if (((isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared) || 3637 (Stack->getDefaultDSA() == DSA_firstprivate && 3638 DVar.CKind == OMPC_firstprivate && !DVar.RefExpr)) && 3639 !Stack->isLoopControlVariable(VD).first) { 3640 ImplicitFirstprivate.push_back(E); 3641 return; 3642 } 3643 3644 // Store implicitly used globals with declare target link for parent 3645 // target. 3646 if (!isOpenMPTargetExecutionDirective(DKind) && Res && 3647 *Res == OMPDeclareTargetDeclAttr::MT_Link) { 3648 Stack->addToParentTargetRegionLinkGlobals(E); 3649 return; 3650 } 3651 } 3652 } 3653 void VisitMemberExpr(MemberExpr *E) { 3654 if (E->isTypeDependent() || E->isValueDependent() || 3655 E->containsUnexpandedParameterPack() || E->isInstantiationDependent()) 3656 return; 3657 auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); 3658 OpenMPDirectiveKind DKind = Stack->getCurrentDirective(); 3659 if (auto *TE = dyn_cast<CXXThisExpr>(E->getBase()->IgnoreParenCasts())) { 3660 if (!FD) 3661 return; 3662 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(FD, /*FromParent=*/false); 3663 // Check if the variable has explicit DSA set and stop analysis if it 3664 // so. 3665 if (DVar.RefExpr || !ImplicitDeclarations.insert(FD).second) 3666 return; 3667 3668 if (isOpenMPTargetExecutionDirective(DKind) && 3669 !Stack->isLoopControlVariable(FD).first && 3670 !Stack->checkMappableExprComponentListsForDecl( 3671 FD, /*CurrentRegionOnly=*/true, 3672 [](OMPClauseMappableExprCommon::MappableExprComponentListRef 3673 StackComponents, 3674 OpenMPClauseKind) { 3675 return isa<CXXThisExpr>( 3676 cast<MemberExpr>( 3677 StackComponents.back().getAssociatedExpression()) 3678 ->getBase() 3679 ->IgnoreParens()); 3680 })) { 3681 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3] 3682 // A bit-field cannot appear in a map clause. 3683 // 3684 if (FD->isBitField()) 3685 return; 3686 3687 // Check to see if the member expression is referencing a class that 3688 // has already been explicitly mapped 3689 if (Stack->isClassPreviouslyMapped(TE->getType())) 3690 return; 3691 3692 OpenMPDefaultmapClauseModifier Modifier = 3693 Stack->getDefaultmapModifier(OMPC_DEFAULTMAP_aggregate); 3694 OpenMPDefaultmapClauseKind ClauseKind = 3695 getVariableCategoryFromDecl(SemaRef.getLangOpts(), FD); 3696 OpenMPMapClauseKind Kind = getMapClauseKindFromModifier( 3697 Modifier, /*IsAggregateOrDeclareTarget*/ true); 3698 ImplicitMap[ClauseKind][Kind].emplace_back(E); 3699 return; 3700 } 3701 3702 SourceLocation ELoc = E->getExprLoc(); 3703 // OpenMP [2.9.3.6, Restrictions, p.2] 3704 // A list item that appears in a reduction clause of the innermost 3705 // enclosing worksharing or parallel construct may not be accessed in 3706 // an explicit task. 3707 DVar = Stack->hasInnermostDSA( 3708 FD, 3709 [](OpenMPClauseKind C, bool AppliedToPointee) { 3710 return C == OMPC_reduction && !AppliedToPointee; 3711 }, 3712 [](OpenMPDirectiveKind K) { 3713 return isOpenMPParallelDirective(K) || 3714 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K); 3715 }, 3716 /*FromParent=*/true); 3717 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) { 3718 ErrorFound = true; 3719 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); 3720 reportOriginalDsa(SemaRef, Stack, FD, DVar); 3721 return; 3722 } 3723 3724 // Define implicit data-sharing attributes for task. 3725 DVar = Stack->getImplicitDSA(FD, /*FromParent=*/false); 3726 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared && 3727 !Stack->isLoopControlVariable(FD).first) { 3728 // Check if there is a captured expression for the current field in the 3729 // region. Do not mark it as firstprivate unless there is no captured 3730 // expression. 3731 // TODO: try to make it firstprivate. 3732 if (DVar.CKind != OMPC_unknown) 3733 ImplicitFirstprivate.push_back(E); 3734 } 3735 return; 3736 } 3737 if (isOpenMPTargetExecutionDirective(DKind)) { 3738 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents; 3739 if (!checkMapClauseExpressionBase(SemaRef, E, CurComponents, OMPC_map, 3740 Stack->getCurrentDirective(), 3741 /*NoDiagnose=*/true)) 3742 return; 3743 const auto *VD = cast<ValueDecl>( 3744 CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl()); 3745 if (!Stack->checkMappableExprComponentListsForDecl( 3746 VD, /*CurrentRegionOnly=*/true, 3747 [&CurComponents]( 3748 OMPClauseMappableExprCommon::MappableExprComponentListRef 3749 StackComponents, 3750 OpenMPClauseKind) { 3751 auto CCI = CurComponents.rbegin(); 3752 auto CCE = CurComponents.rend(); 3753 for (const auto &SC : llvm::reverse(StackComponents)) { 3754 // Do both expressions have the same kind? 3755 if (CCI->getAssociatedExpression()->getStmtClass() != 3756 SC.getAssociatedExpression()->getStmtClass()) 3757 if (!((isa<OMPArraySectionExpr>( 3758 SC.getAssociatedExpression()) || 3759 isa<OMPArrayShapingExpr>( 3760 SC.getAssociatedExpression())) && 3761 isa<ArraySubscriptExpr>( 3762 CCI->getAssociatedExpression()))) 3763 return false; 3764 3765 const Decl *CCD = CCI->getAssociatedDeclaration(); 3766 const Decl *SCD = SC.getAssociatedDeclaration(); 3767 CCD = CCD ? CCD->getCanonicalDecl() : nullptr; 3768 SCD = SCD ? SCD->getCanonicalDecl() : nullptr; 3769 if (SCD != CCD) 3770 return false; 3771 std::advance(CCI, 1); 3772 if (CCI == CCE) 3773 break; 3774 } 3775 return true; 3776 })) { 3777 Visit(E->getBase()); 3778 } 3779 } else if (!TryCaptureCXXThisMembers) { 3780 Visit(E->getBase()); 3781 } 3782 } 3783 void VisitOMPExecutableDirective(OMPExecutableDirective *S) { 3784 for (OMPClause *C : S->clauses()) { 3785 // Skip analysis of arguments of implicitly defined firstprivate clause 3786 // for task|target directives. 3787 // Skip analysis of arguments of implicitly defined map clause for target 3788 // directives. 3789 if (C && !((isa<OMPFirstprivateClause>(C) || isa<OMPMapClause>(C)) && 3790 C->isImplicit() && 3791 !isOpenMPTaskingDirective(Stack->getCurrentDirective()))) { 3792 for (Stmt *CC : C->children()) { 3793 if (CC) 3794 Visit(CC); 3795 } 3796 } 3797 } 3798 // Check implicitly captured variables. 3799 VisitSubCaptures(S); 3800 } 3801 3802 void VisitOMPTileDirective(OMPTileDirective *S) { 3803 // #pragma omp tile does not introduce data sharing. 3804 VisitStmt(S); 3805 } 3806 3807 void VisitStmt(Stmt *S) { 3808 for (Stmt *C : S->children()) { 3809 if (C) { 3810 // Check implicitly captured variables in the task-based directives to 3811 // check if they must be firstprivatized. 3812 Visit(C); 3813 } 3814 } 3815 } 3816 3817 void visitSubCaptures(CapturedStmt *S) { 3818 for (const CapturedStmt::Capture &Cap : S->captures()) { 3819 if (!Cap.capturesVariable() && !Cap.capturesVariableByCopy()) 3820 continue; 3821 VarDecl *VD = Cap.getCapturedVar(); 3822 // Do not try to map the variable if it or its sub-component was mapped 3823 // already. 3824 if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) && 3825 Stack->checkMappableExprComponentListsForDecl( 3826 VD, /*CurrentRegionOnly=*/true, 3827 [](OMPClauseMappableExprCommon::MappableExprComponentListRef, 3828 OpenMPClauseKind) { return true; })) 3829 continue; 3830 DeclRefExpr *DRE = buildDeclRefExpr( 3831 SemaRef, VD, VD->getType().getNonLValueExprType(SemaRef.Context), 3832 Cap.getLocation(), /*RefersToCapture=*/true); 3833 Visit(DRE); 3834 } 3835 } 3836 bool isErrorFound() const { return ErrorFound; } 3837 ArrayRef<Expr *> getImplicitFirstprivate() const { 3838 return ImplicitFirstprivate; 3839 } 3840 ArrayRef<Expr *> getImplicitMap(OpenMPDefaultmapClauseKind DK, 3841 OpenMPMapClauseKind MK) const { 3842 return ImplicitMap[DK][MK]; 3843 } 3844 ArrayRef<OpenMPMapModifierKind> 3845 getImplicitMapModifier(OpenMPDefaultmapClauseKind Kind) const { 3846 return ImplicitMapModifier[Kind]; 3847 } 3848 const Sema::VarsWithInheritedDSAType &getVarsWithInheritedDSA() const { 3849 return VarsWithInheritedDSA; 3850 } 3851 3852 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS) 3853 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) { 3854 // Process declare target link variables for the target directives. 3855 if (isOpenMPTargetExecutionDirective(S->getCurrentDirective())) { 3856 for (DeclRefExpr *E : Stack->getLinkGlobals()) 3857 Visit(E); 3858 } 3859 } 3860 }; 3861 } // namespace 3862 3863 void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) { 3864 switch (DKind) { 3865 case OMPD_parallel: 3866 case OMPD_parallel_for: 3867 case OMPD_parallel_for_simd: 3868 case OMPD_parallel_sections: 3869 case OMPD_parallel_master: 3870 case OMPD_teams: 3871 case OMPD_teams_distribute: 3872 case OMPD_teams_distribute_simd: { 3873 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3874 QualType KmpInt32PtrTy = 3875 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3876 Sema::CapturedParamNameType Params[] = { 3877 std::make_pair(".global_tid.", KmpInt32PtrTy), 3878 std::make_pair(".bound_tid.", KmpInt32PtrTy), 3879 std::make_pair(StringRef(), QualType()) // __context with shared vars 3880 }; 3881 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3882 Params); 3883 break; 3884 } 3885 case OMPD_target_teams: 3886 case OMPD_target_parallel: 3887 case OMPD_target_parallel_for: 3888 case OMPD_target_parallel_for_simd: 3889 case OMPD_target_teams_distribute: 3890 case OMPD_target_teams_distribute_simd: { 3891 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3892 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 3893 QualType KmpInt32PtrTy = 3894 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3895 QualType Args[] = {VoidPtrTy}; 3896 FunctionProtoType::ExtProtoInfo EPI; 3897 EPI.Variadic = true; 3898 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 3899 Sema::CapturedParamNameType Params[] = { 3900 std::make_pair(".global_tid.", KmpInt32Ty), 3901 std::make_pair(".part_id.", KmpInt32PtrTy), 3902 std::make_pair(".privates.", VoidPtrTy), 3903 std::make_pair( 3904 ".copy_fn.", 3905 Context.getPointerType(CopyFnType).withConst().withRestrict()), 3906 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 3907 std::make_pair(StringRef(), QualType()) // __context with shared vars 3908 }; 3909 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3910 Params, /*OpenMPCaptureLevel=*/0); 3911 // Mark this captured region as inlined, because we don't use outlined 3912 // function directly. 3913 getCurCapturedRegion()->TheCapturedDecl->addAttr( 3914 AlwaysInlineAttr::CreateImplicit( 3915 Context, {}, AttributeCommonInfo::AS_Keyword, 3916 AlwaysInlineAttr::Keyword_forceinline)); 3917 Sema::CapturedParamNameType ParamsTarget[] = { 3918 std::make_pair(StringRef(), QualType()) // __context with shared vars 3919 }; 3920 // Start a captured region for 'target' with no implicit parameters. 3921 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3922 ParamsTarget, /*OpenMPCaptureLevel=*/1); 3923 Sema::CapturedParamNameType ParamsTeamsOrParallel[] = { 3924 std::make_pair(".global_tid.", KmpInt32PtrTy), 3925 std::make_pair(".bound_tid.", KmpInt32PtrTy), 3926 std::make_pair(StringRef(), QualType()) // __context with shared vars 3927 }; 3928 // Start a captured region for 'teams' or 'parallel'. Both regions have 3929 // the same implicit parameters. 3930 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3931 ParamsTeamsOrParallel, /*OpenMPCaptureLevel=*/2); 3932 break; 3933 } 3934 case OMPD_target: 3935 case OMPD_target_simd: { 3936 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3937 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 3938 QualType KmpInt32PtrTy = 3939 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3940 QualType Args[] = {VoidPtrTy}; 3941 FunctionProtoType::ExtProtoInfo EPI; 3942 EPI.Variadic = true; 3943 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 3944 Sema::CapturedParamNameType Params[] = { 3945 std::make_pair(".global_tid.", KmpInt32Ty), 3946 std::make_pair(".part_id.", KmpInt32PtrTy), 3947 std::make_pair(".privates.", VoidPtrTy), 3948 std::make_pair( 3949 ".copy_fn.", 3950 Context.getPointerType(CopyFnType).withConst().withRestrict()), 3951 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 3952 std::make_pair(StringRef(), QualType()) // __context with shared vars 3953 }; 3954 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3955 Params, /*OpenMPCaptureLevel=*/0); 3956 // Mark this captured region as inlined, because we don't use outlined 3957 // function directly. 3958 getCurCapturedRegion()->TheCapturedDecl->addAttr( 3959 AlwaysInlineAttr::CreateImplicit( 3960 Context, {}, AttributeCommonInfo::AS_Keyword, 3961 AlwaysInlineAttr::Keyword_forceinline)); 3962 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3963 std::make_pair(StringRef(), QualType()), 3964 /*OpenMPCaptureLevel=*/1); 3965 break; 3966 } 3967 case OMPD_atomic: 3968 case OMPD_critical: 3969 case OMPD_section: 3970 case OMPD_master: 3971 case OMPD_tile: 3972 break; 3973 case OMPD_simd: 3974 case OMPD_for: 3975 case OMPD_for_simd: 3976 case OMPD_sections: 3977 case OMPD_single: 3978 case OMPD_taskgroup: 3979 case OMPD_distribute: 3980 case OMPD_distribute_simd: 3981 case OMPD_ordered: 3982 case OMPD_target_data: { 3983 Sema::CapturedParamNameType Params[] = { 3984 std::make_pair(StringRef(), QualType()) // __context with shared vars 3985 }; 3986 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3987 Params); 3988 break; 3989 } 3990 case OMPD_task: { 3991 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3992 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 3993 QualType KmpInt32PtrTy = 3994 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3995 QualType Args[] = {VoidPtrTy}; 3996 FunctionProtoType::ExtProtoInfo EPI; 3997 EPI.Variadic = true; 3998 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 3999 Sema::CapturedParamNameType Params[] = { 4000 std::make_pair(".global_tid.", KmpInt32Ty), 4001 std::make_pair(".part_id.", KmpInt32PtrTy), 4002 std::make_pair(".privates.", VoidPtrTy), 4003 std::make_pair( 4004 ".copy_fn.", 4005 Context.getPointerType(CopyFnType).withConst().withRestrict()), 4006 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 4007 std::make_pair(StringRef(), QualType()) // __context with shared vars 4008 }; 4009 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4010 Params); 4011 // Mark this captured region as inlined, because we don't use outlined 4012 // function directly. 4013 getCurCapturedRegion()->TheCapturedDecl->addAttr( 4014 AlwaysInlineAttr::CreateImplicit( 4015 Context, {}, AttributeCommonInfo::AS_Keyword, 4016 AlwaysInlineAttr::Keyword_forceinline)); 4017 break; 4018 } 4019 case OMPD_taskloop: 4020 case OMPD_taskloop_simd: 4021 case OMPD_master_taskloop: 4022 case OMPD_master_taskloop_simd: { 4023 QualType KmpInt32Ty = 4024 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1) 4025 .withConst(); 4026 QualType KmpUInt64Ty = 4027 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0) 4028 .withConst(); 4029 QualType KmpInt64Ty = 4030 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1) 4031 .withConst(); 4032 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 4033 QualType KmpInt32PtrTy = 4034 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4035 QualType Args[] = {VoidPtrTy}; 4036 FunctionProtoType::ExtProtoInfo EPI; 4037 EPI.Variadic = true; 4038 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 4039 Sema::CapturedParamNameType Params[] = { 4040 std::make_pair(".global_tid.", KmpInt32Ty), 4041 std::make_pair(".part_id.", KmpInt32PtrTy), 4042 std::make_pair(".privates.", VoidPtrTy), 4043 std::make_pair( 4044 ".copy_fn.", 4045 Context.getPointerType(CopyFnType).withConst().withRestrict()), 4046 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 4047 std::make_pair(".lb.", KmpUInt64Ty), 4048 std::make_pair(".ub.", KmpUInt64Ty), 4049 std::make_pair(".st.", KmpInt64Ty), 4050 std::make_pair(".liter.", KmpInt32Ty), 4051 std::make_pair(".reductions.", VoidPtrTy), 4052 std::make_pair(StringRef(), QualType()) // __context with shared vars 4053 }; 4054 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4055 Params); 4056 // Mark this captured region as inlined, because we don't use outlined 4057 // function directly. 4058 getCurCapturedRegion()->TheCapturedDecl->addAttr( 4059 AlwaysInlineAttr::CreateImplicit( 4060 Context, {}, AttributeCommonInfo::AS_Keyword, 4061 AlwaysInlineAttr::Keyword_forceinline)); 4062 break; 4063 } 4064 case OMPD_parallel_master_taskloop: 4065 case OMPD_parallel_master_taskloop_simd: { 4066 QualType KmpInt32Ty = 4067 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1) 4068 .withConst(); 4069 QualType KmpUInt64Ty = 4070 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0) 4071 .withConst(); 4072 QualType KmpInt64Ty = 4073 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1) 4074 .withConst(); 4075 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 4076 QualType KmpInt32PtrTy = 4077 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4078 Sema::CapturedParamNameType ParamsParallel[] = { 4079 std::make_pair(".global_tid.", KmpInt32PtrTy), 4080 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4081 std::make_pair(StringRef(), QualType()) // __context with shared vars 4082 }; 4083 // Start a captured region for 'parallel'. 4084 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4085 ParamsParallel, /*OpenMPCaptureLevel=*/0); 4086 QualType Args[] = {VoidPtrTy}; 4087 FunctionProtoType::ExtProtoInfo EPI; 4088 EPI.Variadic = true; 4089 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 4090 Sema::CapturedParamNameType Params[] = { 4091 std::make_pair(".global_tid.", KmpInt32Ty), 4092 std::make_pair(".part_id.", KmpInt32PtrTy), 4093 std::make_pair(".privates.", VoidPtrTy), 4094 std::make_pair( 4095 ".copy_fn.", 4096 Context.getPointerType(CopyFnType).withConst().withRestrict()), 4097 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 4098 std::make_pair(".lb.", KmpUInt64Ty), 4099 std::make_pair(".ub.", KmpUInt64Ty), 4100 std::make_pair(".st.", KmpInt64Ty), 4101 std::make_pair(".liter.", KmpInt32Ty), 4102 std::make_pair(".reductions.", VoidPtrTy), 4103 std::make_pair(StringRef(), QualType()) // __context with shared vars 4104 }; 4105 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4106 Params, /*OpenMPCaptureLevel=*/1); 4107 // Mark this captured region as inlined, because we don't use outlined 4108 // function directly. 4109 getCurCapturedRegion()->TheCapturedDecl->addAttr( 4110 AlwaysInlineAttr::CreateImplicit( 4111 Context, {}, AttributeCommonInfo::AS_Keyword, 4112 AlwaysInlineAttr::Keyword_forceinline)); 4113 break; 4114 } 4115 case OMPD_distribute_parallel_for_simd: 4116 case OMPD_distribute_parallel_for: { 4117 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 4118 QualType KmpInt32PtrTy = 4119 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4120 Sema::CapturedParamNameType Params[] = { 4121 std::make_pair(".global_tid.", KmpInt32PtrTy), 4122 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4123 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 4124 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 4125 std::make_pair(StringRef(), QualType()) // __context with shared vars 4126 }; 4127 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4128 Params); 4129 break; 4130 } 4131 case OMPD_target_teams_distribute_parallel_for: 4132 case OMPD_target_teams_distribute_parallel_for_simd: { 4133 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 4134 QualType KmpInt32PtrTy = 4135 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4136 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 4137 4138 QualType Args[] = {VoidPtrTy}; 4139 FunctionProtoType::ExtProtoInfo EPI; 4140 EPI.Variadic = true; 4141 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 4142 Sema::CapturedParamNameType Params[] = { 4143 std::make_pair(".global_tid.", KmpInt32Ty), 4144 std::make_pair(".part_id.", KmpInt32PtrTy), 4145 std::make_pair(".privates.", VoidPtrTy), 4146 std::make_pair( 4147 ".copy_fn.", 4148 Context.getPointerType(CopyFnType).withConst().withRestrict()), 4149 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 4150 std::make_pair(StringRef(), QualType()) // __context with shared vars 4151 }; 4152 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4153 Params, /*OpenMPCaptureLevel=*/0); 4154 // Mark this captured region as inlined, because we don't use outlined 4155 // function directly. 4156 getCurCapturedRegion()->TheCapturedDecl->addAttr( 4157 AlwaysInlineAttr::CreateImplicit( 4158 Context, {}, AttributeCommonInfo::AS_Keyword, 4159 AlwaysInlineAttr::Keyword_forceinline)); 4160 Sema::CapturedParamNameType ParamsTarget[] = { 4161 std::make_pair(StringRef(), QualType()) // __context with shared vars 4162 }; 4163 // Start a captured region for 'target' with no implicit parameters. 4164 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4165 ParamsTarget, /*OpenMPCaptureLevel=*/1); 4166 4167 Sema::CapturedParamNameType ParamsTeams[] = { 4168 std::make_pair(".global_tid.", KmpInt32PtrTy), 4169 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4170 std::make_pair(StringRef(), QualType()) // __context with shared vars 4171 }; 4172 // Start a captured region for 'target' with no implicit parameters. 4173 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4174 ParamsTeams, /*OpenMPCaptureLevel=*/2); 4175 4176 Sema::CapturedParamNameType ParamsParallel[] = { 4177 std::make_pair(".global_tid.", KmpInt32PtrTy), 4178 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4179 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 4180 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 4181 std::make_pair(StringRef(), QualType()) // __context with shared vars 4182 }; 4183 // Start a captured region for 'teams' or 'parallel'. Both regions have 4184 // the same implicit parameters. 4185 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4186 ParamsParallel, /*OpenMPCaptureLevel=*/3); 4187 break; 4188 } 4189 4190 case OMPD_teams_distribute_parallel_for: 4191 case OMPD_teams_distribute_parallel_for_simd: { 4192 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 4193 QualType KmpInt32PtrTy = 4194 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4195 4196 Sema::CapturedParamNameType ParamsTeams[] = { 4197 std::make_pair(".global_tid.", KmpInt32PtrTy), 4198 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4199 std::make_pair(StringRef(), QualType()) // __context with shared vars 4200 }; 4201 // Start a captured region for 'target' with no implicit parameters. 4202 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4203 ParamsTeams, /*OpenMPCaptureLevel=*/0); 4204 4205 Sema::CapturedParamNameType ParamsParallel[] = { 4206 std::make_pair(".global_tid.", KmpInt32PtrTy), 4207 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4208 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 4209 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 4210 std::make_pair(StringRef(), QualType()) // __context with shared vars 4211 }; 4212 // Start a captured region for 'teams' or 'parallel'. Both regions have 4213 // the same implicit parameters. 4214 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4215 ParamsParallel, /*OpenMPCaptureLevel=*/1); 4216 break; 4217 } 4218 case OMPD_target_update: 4219 case OMPD_target_enter_data: 4220 case OMPD_target_exit_data: { 4221 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 4222 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 4223 QualType KmpInt32PtrTy = 4224 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4225 QualType Args[] = {VoidPtrTy}; 4226 FunctionProtoType::ExtProtoInfo EPI; 4227 EPI.Variadic = true; 4228 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 4229 Sema::CapturedParamNameType Params[] = { 4230 std::make_pair(".global_tid.", KmpInt32Ty), 4231 std::make_pair(".part_id.", KmpInt32PtrTy), 4232 std::make_pair(".privates.", VoidPtrTy), 4233 std::make_pair( 4234 ".copy_fn.", 4235 Context.getPointerType(CopyFnType).withConst().withRestrict()), 4236 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 4237 std::make_pair(StringRef(), QualType()) // __context with shared vars 4238 }; 4239 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4240 Params); 4241 // Mark this captured region as inlined, because we don't use outlined 4242 // function directly. 4243 getCurCapturedRegion()->TheCapturedDecl->addAttr( 4244 AlwaysInlineAttr::CreateImplicit( 4245 Context, {}, AttributeCommonInfo::AS_Keyword, 4246 AlwaysInlineAttr::Keyword_forceinline)); 4247 break; 4248 } 4249 case OMPD_threadprivate: 4250 case OMPD_allocate: 4251 case OMPD_taskyield: 4252 case OMPD_barrier: 4253 case OMPD_taskwait: 4254 case OMPD_cancellation_point: 4255 case OMPD_cancel: 4256 case OMPD_flush: 4257 case OMPD_depobj: 4258 case OMPD_scan: 4259 case OMPD_declare_reduction: 4260 case OMPD_declare_mapper: 4261 case OMPD_declare_simd: 4262 case OMPD_declare_target: 4263 case OMPD_end_declare_target: 4264 case OMPD_requires: 4265 case OMPD_declare_variant: 4266 case OMPD_begin_declare_variant: 4267 case OMPD_end_declare_variant: 4268 llvm_unreachable("OpenMP Directive is not allowed"); 4269 case OMPD_unknown: 4270 default: 4271 llvm_unreachable("Unknown OpenMP directive"); 4272 } 4273 DSAStack->setContext(CurContext); 4274 } 4275 4276 int Sema::getNumberOfConstructScopes(unsigned Level) const { 4277 return getOpenMPCaptureLevels(DSAStack->getDirective(Level)); 4278 } 4279 4280 int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) { 4281 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 4282 getOpenMPCaptureRegions(CaptureRegions, DKind); 4283 return CaptureRegions.size(); 4284 } 4285 4286 static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id, 4287 Expr *CaptureExpr, bool WithInit, 4288 bool AsExpression) { 4289 assert(CaptureExpr); 4290 ASTContext &C = S.getASTContext(); 4291 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts(); 4292 QualType Ty = Init->getType(); 4293 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) { 4294 if (S.getLangOpts().CPlusPlus) { 4295 Ty = C.getLValueReferenceType(Ty); 4296 } else { 4297 Ty = C.getPointerType(Ty); 4298 ExprResult Res = 4299 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init); 4300 if (!Res.isUsable()) 4301 return nullptr; 4302 Init = Res.get(); 4303 } 4304 WithInit = true; 4305 } 4306 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty, 4307 CaptureExpr->getBeginLoc()); 4308 if (!WithInit) 4309 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C)); 4310 S.CurContext->addHiddenDecl(CED); 4311 Sema::TentativeAnalysisScope Trap(S); 4312 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false); 4313 return CED; 4314 } 4315 4316 static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr, 4317 bool WithInit) { 4318 OMPCapturedExprDecl *CD; 4319 if (VarDecl *VD = S.isOpenMPCapturedDecl(D)) 4320 CD = cast<OMPCapturedExprDecl>(VD); 4321 else 4322 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit, 4323 /*AsExpression=*/false); 4324 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(), 4325 CaptureExpr->getExprLoc()); 4326 } 4327 4328 static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) { 4329 CaptureExpr = S.DefaultLvalueConversion(CaptureExpr).get(); 4330 if (!Ref) { 4331 OMPCapturedExprDecl *CD = buildCaptureDecl( 4332 S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr, 4333 /*WithInit=*/true, /*AsExpression=*/true); 4334 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(), 4335 CaptureExpr->getExprLoc()); 4336 } 4337 ExprResult Res = Ref; 4338 if (!S.getLangOpts().CPlusPlus && 4339 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() && 4340 Ref->getType()->isPointerType()) { 4341 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref); 4342 if (!Res.isUsable()) 4343 return ExprError(); 4344 } 4345 return S.DefaultLvalueConversion(Res.get()); 4346 } 4347 4348 namespace { 4349 // OpenMP directives parsed in this section are represented as a 4350 // CapturedStatement with an associated statement. If a syntax error 4351 // is detected during the parsing of the associated statement, the 4352 // compiler must abort processing and close the CapturedStatement. 4353 // 4354 // Combined directives such as 'target parallel' have more than one 4355 // nested CapturedStatements. This RAII ensures that we unwind out 4356 // of all the nested CapturedStatements when an error is found. 4357 class CaptureRegionUnwinderRAII { 4358 private: 4359 Sema &S; 4360 bool &ErrorFound; 4361 OpenMPDirectiveKind DKind = OMPD_unknown; 4362 4363 public: 4364 CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound, 4365 OpenMPDirectiveKind DKind) 4366 : S(S), ErrorFound(ErrorFound), DKind(DKind) {} 4367 ~CaptureRegionUnwinderRAII() { 4368 if (ErrorFound) { 4369 int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind); 4370 while (--ThisCaptureLevel >= 0) 4371 S.ActOnCapturedRegionError(); 4372 } 4373 } 4374 }; 4375 } // namespace 4376 4377 void Sema::tryCaptureOpenMPLambdas(ValueDecl *V) { 4378 // Capture variables captured by reference in lambdas for target-based 4379 // directives. 4380 if (!CurContext->isDependentContext() && 4381 (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) || 4382 isOpenMPTargetDataManagementDirective( 4383 DSAStack->getCurrentDirective()))) { 4384 QualType Type = V->getType(); 4385 if (const auto *RD = Type.getCanonicalType() 4386 .getNonReferenceType() 4387 ->getAsCXXRecordDecl()) { 4388 bool SavedForceCaptureByReferenceInTargetExecutable = 4389 DSAStack->isForceCaptureByReferenceInTargetExecutable(); 4390 DSAStack->setForceCaptureByReferenceInTargetExecutable( 4391 /*V=*/true); 4392 if (RD->isLambda()) { 4393 llvm::DenseMap<const VarDecl *, FieldDecl *> Captures; 4394 FieldDecl *ThisCapture; 4395 RD->getCaptureFields(Captures, ThisCapture); 4396 for (const LambdaCapture &LC : RD->captures()) { 4397 if (LC.getCaptureKind() == LCK_ByRef) { 4398 VarDecl *VD = LC.getCapturedVar(); 4399 DeclContext *VDC = VD->getDeclContext(); 4400 if (!VDC->Encloses(CurContext)) 4401 continue; 4402 MarkVariableReferenced(LC.getLocation(), VD); 4403 } else if (LC.getCaptureKind() == LCK_This) { 4404 QualType ThisTy = getCurrentThisType(); 4405 if (!ThisTy.isNull() && 4406 Context.typesAreCompatible(ThisTy, ThisCapture->getType())) 4407 CheckCXXThisCapture(LC.getLocation()); 4408 } 4409 } 4410 } 4411 DSAStack->setForceCaptureByReferenceInTargetExecutable( 4412 SavedForceCaptureByReferenceInTargetExecutable); 4413 } 4414 } 4415 } 4416 4417 static bool checkOrderedOrderSpecified(Sema &S, 4418 const ArrayRef<OMPClause *> Clauses) { 4419 const OMPOrderedClause *Ordered = nullptr; 4420 const OMPOrderClause *Order = nullptr; 4421 4422 for (const OMPClause *Clause : Clauses) { 4423 if (Clause->getClauseKind() == OMPC_ordered) 4424 Ordered = cast<OMPOrderedClause>(Clause); 4425 else if (Clause->getClauseKind() == OMPC_order) { 4426 Order = cast<OMPOrderClause>(Clause); 4427 if (Order->getKind() != OMPC_ORDER_concurrent) 4428 Order = nullptr; 4429 } 4430 if (Ordered && Order) 4431 break; 4432 } 4433 4434 if (Ordered && Order) { 4435 S.Diag(Order->getKindKwLoc(), 4436 diag::err_omp_simple_clause_incompatible_with_ordered) 4437 << getOpenMPClauseName(OMPC_order) 4438 << getOpenMPSimpleClauseTypeName(OMPC_order, OMPC_ORDER_concurrent) 4439 << SourceRange(Order->getBeginLoc(), Order->getEndLoc()); 4440 S.Diag(Ordered->getBeginLoc(), diag::note_omp_ordered_param) 4441 << 0 << SourceRange(Ordered->getBeginLoc(), Ordered->getEndLoc()); 4442 return true; 4443 } 4444 return false; 4445 } 4446 4447 StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S, 4448 ArrayRef<OMPClause *> Clauses) { 4449 if (DSAStack->getCurrentDirective() == OMPD_atomic || 4450 DSAStack->getCurrentDirective() == OMPD_critical || 4451 DSAStack->getCurrentDirective() == OMPD_section || 4452 DSAStack->getCurrentDirective() == OMPD_master) 4453 return S; 4454 4455 bool ErrorFound = false; 4456 CaptureRegionUnwinderRAII CaptureRegionUnwinder( 4457 *this, ErrorFound, DSAStack->getCurrentDirective()); 4458 if (!S.isUsable()) { 4459 ErrorFound = true; 4460 return StmtError(); 4461 } 4462 4463 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 4464 getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective()); 4465 OMPOrderedClause *OC = nullptr; 4466 OMPScheduleClause *SC = nullptr; 4467 SmallVector<const OMPLinearClause *, 4> LCs; 4468 SmallVector<const OMPClauseWithPreInit *, 4> PICs; 4469 // This is required for proper codegen. 4470 for (OMPClause *Clause : Clauses) { 4471 if (!LangOpts.OpenMPSimd && 4472 isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) && 4473 Clause->getClauseKind() == OMPC_in_reduction) { 4474 // Capture taskgroup task_reduction descriptors inside the tasking regions 4475 // with the corresponding in_reduction items. 4476 auto *IRC = cast<OMPInReductionClause>(Clause); 4477 for (Expr *E : IRC->taskgroup_descriptors()) 4478 if (E) 4479 MarkDeclarationsReferencedInExpr(E); 4480 } 4481 if (isOpenMPPrivate(Clause->getClauseKind()) || 4482 Clause->getClauseKind() == OMPC_copyprivate || 4483 (getLangOpts().OpenMPUseTLS && 4484 getASTContext().getTargetInfo().isTLSSupported() && 4485 Clause->getClauseKind() == OMPC_copyin)) { 4486 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin); 4487 // Mark all variables in private list clauses as used in inner region. 4488 for (Stmt *VarRef : Clause->children()) { 4489 if (auto *E = cast_or_null<Expr>(VarRef)) { 4490 MarkDeclarationsReferencedInExpr(E); 4491 } 4492 } 4493 DSAStack->setForceVarCapturing(/*V=*/false); 4494 } else if (isOpenMPLoopTransformationDirective( 4495 DSAStack->getCurrentDirective())) { 4496 assert(CaptureRegions.empty() && 4497 "No captured regions in loop transformation directives."); 4498 } else if (CaptureRegions.size() > 1 || 4499 CaptureRegions.back() != OMPD_unknown) { 4500 if (auto *C = OMPClauseWithPreInit::get(Clause)) 4501 PICs.push_back(C); 4502 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) { 4503 if (Expr *E = C->getPostUpdateExpr()) 4504 MarkDeclarationsReferencedInExpr(E); 4505 } 4506 } 4507 if (Clause->getClauseKind() == OMPC_schedule) 4508 SC = cast<OMPScheduleClause>(Clause); 4509 else if (Clause->getClauseKind() == OMPC_ordered) 4510 OC = cast<OMPOrderedClause>(Clause); 4511 else if (Clause->getClauseKind() == OMPC_linear) 4512 LCs.push_back(cast<OMPLinearClause>(Clause)); 4513 } 4514 // Capture allocator expressions if used. 4515 for (Expr *E : DSAStack->getInnerAllocators()) 4516 MarkDeclarationsReferencedInExpr(E); 4517 // OpenMP, 2.7.1 Loop Construct, Restrictions 4518 // The nonmonotonic modifier cannot be specified if an ordered clause is 4519 // specified. 4520 if (SC && 4521 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic || 4522 SC->getSecondScheduleModifier() == 4523 OMPC_SCHEDULE_MODIFIER_nonmonotonic) && 4524 OC) { 4525 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic 4526 ? SC->getFirstScheduleModifierLoc() 4527 : SC->getSecondScheduleModifierLoc(), 4528 diag::err_omp_simple_clause_incompatible_with_ordered) 4529 << getOpenMPClauseName(OMPC_schedule) 4530 << getOpenMPSimpleClauseTypeName(OMPC_schedule, 4531 OMPC_SCHEDULE_MODIFIER_nonmonotonic) 4532 << SourceRange(OC->getBeginLoc(), OC->getEndLoc()); 4533 ErrorFound = true; 4534 } 4535 // OpenMP 5.0, 2.9.2 Worksharing-Loop Construct, Restrictions. 4536 // If an order(concurrent) clause is present, an ordered clause may not appear 4537 // on the same directive. 4538 if (checkOrderedOrderSpecified(*this, Clauses)) 4539 ErrorFound = true; 4540 if (!LCs.empty() && OC && OC->getNumForLoops()) { 4541 for (const OMPLinearClause *C : LCs) { 4542 Diag(C->getBeginLoc(), diag::err_omp_linear_ordered) 4543 << SourceRange(OC->getBeginLoc(), OC->getEndLoc()); 4544 } 4545 ErrorFound = true; 4546 } 4547 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) && 4548 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC && 4549 OC->getNumForLoops()) { 4550 Diag(OC->getBeginLoc(), diag::err_omp_ordered_simd) 4551 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 4552 ErrorFound = true; 4553 } 4554 if (ErrorFound) { 4555 return StmtError(); 4556 } 4557 StmtResult SR = S; 4558 unsigned CompletedRegions = 0; 4559 for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(CaptureRegions)) { 4560 // Mark all variables in private list clauses as used in inner region. 4561 // Required for proper codegen of combined directives. 4562 // TODO: add processing for other clauses. 4563 if (ThisCaptureRegion != OMPD_unknown) { 4564 for (const clang::OMPClauseWithPreInit *C : PICs) { 4565 OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion(); 4566 // Find the particular capture region for the clause if the 4567 // directive is a combined one with multiple capture regions. 4568 // If the directive is not a combined one, the capture region 4569 // associated with the clause is OMPD_unknown and is generated 4570 // only once. 4571 if (CaptureRegion == ThisCaptureRegion || 4572 CaptureRegion == OMPD_unknown) { 4573 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) { 4574 for (Decl *D : DS->decls()) 4575 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D)); 4576 } 4577 } 4578 } 4579 } 4580 if (ThisCaptureRegion == OMPD_target) { 4581 // Capture allocator traits in the target region. They are used implicitly 4582 // and, thus, are not captured by default. 4583 for (OMPClause *C : Clauses) { 4584 if (const auto *UAC = dyn_cast<OMPUsesAllocatorsClause>(C)) { 4585 for (unsigned I = 0, End = UAC->getNumberOfAllocators(); I < End; 4586 ++I) { 4587 OMPUsesAllocatorsClause::Data D = UAC->getAllocatorData(I); 4588 if (Expr *E = D.AllocatorTraits) 4589 MarkDeclarationsReferencedInExpr(E); 4590 } 4591 continue; 4592 } 4593 } 4594 } 4595 if (++CompletedRegions == CaptureRegions.size()) 4596 DSAStack->setBodyComplete(); 4597 SR = ActOnCapturedRegionEnd(SR.get()); 4598 } 4599 return SR; 4600 } 4601 4602 static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion, 4603 OpenMPDirectiveKind CancelRegion, 4604 SourceLocation StartLoc) { 4605 // CancelRegion is only needed for cancel and cancellation_point. 4606 if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point) 4607 return false; 4608 4609 if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for || 4610 CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup) 4611 return false; 4612 4613 SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region) 4614 << getOpenMPDirectiveName(CancelRegion); 4615 return true; 4616 } 4617 4618 static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack, 4619 OpenMPDirectiveKind CurrentRegion, 4620 const DeclarationNameInfo &CurrentName, 4621 OpenMPDirectiveKind CancelRegion, 4622 SourceLocation StartLoc) { 4623 if (Stack->getCurScope()) { 4624 OpenMPDirectiveKind ParentRegion = Stack->getParentDirective(); 4625 OpenMPDirectiveKind OffendingRegion = ParentRegion; 4626 bool NestingProhibited = false; 4627 bool CloseNesting = true; 4628 bool OrphanSeen = false; 4629 enum { 4630 NoRecommend, 4631 ShouldBeInParallelRegion, 4632 ShouldBeInOrderedRegion, 4633 ShouldBeInTargetRegion, 4634 ShouldBeInTeamsRegion, 4635 ShouldBeInLoopSimdRegion, 4636 } Recommend = NoRecommend; 4637 if (isOpenMPSimdDirective(ParentRegion) && 4638 ((SemaRef.LangOpts.OpenMP <= 45 && CurrentRegion != OMPD_ordered) || 4639 (SemaRef.LangOpts.OpenMP >= 50 && CurrentRegion != OMPD_ordered && 4640 CurrentRegion != OMPD_simd && CurrentRegion != OMPD_atomic && 4641 CurrentRegion != OMPD_scan))) { 4642 // OpenMP [2.16, Nesting of Regions] 4643 // OpenMP constructs may not be nested inside a simd region. 4644 // OpenMP [2.8.1,simd Construct, Restrictions] 4645 // An ordered construct with the simd clause is the only OpenMP 4646 // construct that can appear in the simd region. 4647 // Allowing a SIMD construct nested in another SIMD construct is an 4648 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning 4649 // message. 4650 // OpenMP 5.0 [2.9.3.1, simd Construct, Restrictions] 4651 // The only OpenMP constructs that can be encountered during execution of 4652 // a simd region are the atomic construct, the loop construct, the simd 4653 // construct and the ordered construct with the simd clause. 4654 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd) 4655 ? diag::err_omp_prohibited_region_simd 4656 : diag::warn_omp_nesting_simd) 4657 << (SemaRef.LangOpts.OpenMP >= 50 ? 1 : 0); 4658 return CurrentRegion != OMPD_simd; 4659 } 4660 if (ParentRegion == OMPD_atomic) { 4661 // OpenMP [2.16, Nesting of Regions] 4662 // OpenMP constructs may not be nested inside an atomic region. 4663 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic); 4664 return true; 4665 } 4666 if (CurrentRegion == OMPD_section) { 4667 // OpenMP [2.7.2, sections Construct, Restrictions] 4668 // Orphaned section directives are prohibited. That is, the section 4669 // directives must appear within the sections construct and must not be 4670 // encountered elsewhere in the sections region. 4671 if (ParentRegion != OMPD_sections && 4672 ParentRegion != OMPD_parallel_sections) { 4673 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive) 4674 << (ParentRegion != OMPD_unknown) 4675 << getOpenMPDirectiveName(ParentRegion); 4676 return true; 4677 } 4678 return false; 4679 } 4680 // Allow some constructs (except teams and cancellation constructs) to be 4681 // orphaned (they could be used in functions, called from OpenMP regions 4682 // with the required preconditions). 4683 if (ParentRegion == OMPD_unknown && 4684 !isOpenMPNestingTeamsDirective(CurrentRegion) && 4685 CurrentRegion != OMPD_cancellation_point && 4686 CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_scan) 4687 return false; 4688 if (CurrentRegion == OMPD_cancellation_point || 4689 CurrentRegion == OMPD_cancel) { 4690 // OpenMP [2.16, Nesting of Regions] 4691 // A cancellation point construct for which construct-type-clause is 4692 // taskgroup must be nested inside a task construct. A cancellation 4693 // point construct for which construct-type-clause is not taskgroup must 4694 // be closely nested inside an OpenMP construct that matches the type 4695 // specified in construct-type-clause. 4696 // A cancel construct for which construct-type-clause is taskgroup must be 4697 // nested inside a task construct. A cancel construct for which 4698 // construct-type-clause is not taskgroup must be closely nested inside an 4699 // OpenMP construct that matches the type specified in 4700 // construct-type-clause. 4701 NestingProhibited = 4702 !((CancelRegion == OMPD_parallel && 4703 (ParentRegion == OMPD_parallel || 4704 ParentRegion == OMPD_target_parallel)) || 4705 (CancelRegion == OMPD_for && 4706 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for || 4707 ParentRegion == OMPD_target_parallel_for || 4708 ParentRegion == OMPD_distribute_parallel_for || 4709 ParentRegion == OMPD_teams_distribute_parallel_for || 4710 ParentRegion == OMPD_target_teams_distribute_parallel_for)) || 4711 (CancelRegion == OMPD_taskgroup && 4712 (ParentRegion == OMPD_task || 4713 (SemaRef.getLangOpts().OpenMP >= 50 && 4714 (ParentRegion == OMPD_taskloop || 4715 ParentRegion == OMPD_master_taskloop || 4716 ParentRegion == OMPD_parallel_master_taskloop)))) || 4717 (CancelRegion == OMPD_sections && 4718 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections || 4719 ParentRegion == OMPD_parallel_sections))); 4720 OrphanSeen = ParentRegion == OMPD_unknown; 4721 } else if (CurrentRegion == OMPD_master) { 4722 // OpenMP [2.16, Nesting of Regions] 4723 // A master region may not be closely nested inside a worksharing, 4724 // atomic, or explicit task region. 4725 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 4726 isOpenMPTaskingDirective(ParentRegion); 4727 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) { 4728 // OpenMP [2.16, Nesting of Regions] 4729 // A critical region may not be nested (closely or otherwise) inside a 4730 // critical region with the same name. Note that this restriction is not 4731 // sufficient to prevent deadlock. 4732 SourceLocation PreviousCriticalLoc; 4733 bool DeadLock = Stack->hasDirective( 4734 [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K, 4735 const DeclarationNameInfo &DNI, 4736 SourceLocation Loc) { 4737 if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) { 4738 PreviousCriticalLoc = Loc; 4739 return true; 4740 } 4741 return false; 4742 }, 4743 false /* skip top directive */); 4744 if (DeadLock) { 4745 SemaRef.Diag(StartLoc, 4746 diag::err_omp_prohibited_region_critical_same_name) 4747 << CurrentName.getName(); 4748 if (PreviousCriticalLoc.isValid()) 4749 SemaRef.Diag(PreviousCriticalLoc, 4750 diag::note_omp_previous_critical_region); 4751 return true; 4752 } 4753 } else if (CurrentRegion == OMPD_barrier) { 4754 // OpenMP [2.16, Nesting of Regions] 4755 // A barrier region may not be closely nested inside a worksharing, 4756 // explicit task, critical, ordered, atomic, or master region. 4757 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 4758 isOpenMPTaskingDirective(ParentRegion) || 4759 ParentRegion == OMPD_master || 4760 ParentRegion == OMPD_parallel_master || 4761 ParentRegion == OMPD_critical || 4762 ParentRegion == OMPD_ordered; 4763 } else if (isOpenMPWorksharingDirective(CurrentRegion) && 4764 !isOpenMPParallelDirective(CurrentRegion) && 4765 !isOpenMPTeamsDirective(CurrentRegion)) { 4766 // OpenMP [2.16, Nesting of Regions] 4767 // A worksharing region may not be closely nested inside a worksharing, 4768 // explicit task, critical, ordered, atomic, or master region. 4769 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 4770 isOpenMPTaskingDirective(ParentRegion) || 4771 ParentRegion == OMPD_master || 4772 ParentRegion == OMPD_parallel_master || 4773 ParentRegion == OMPD_critical || 4774 ParentRegion == OMPD_ordered; 4775 Recommend = ShouldBeInParallelRegion; 4776 } else if (CurrentRegion == OMPD_ordered) { 4777 // OpenMP [2.16, Nesting of Regions] 4778 // An ordered region may not be closely nested inside a critical, 4779 // atomic, or explicit task region. 4780 // An ordered region must be closely nested inside a loop region (or 4781 // parallel loop region) with an ordered clause. 4782 // OpenMP [2.8.1,simd Construct, Restrictions] 4783 // An ordered construct with the simd clause is the only OpenMP construct 4784 // that can appear in the simd region. 4785 NestingProhibited = ParentRegion == OMPD_critical || 4786 isOpenMPTaskingDirective(ParentRegion) || 4787 !(isOpenMPSimdDirective(ParentRegion) || 4788 Stack->isParentOrderedRegion()); 4789 Recommend = ShouldBeInOrderedRegion; 4790 } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) { 4791 // OpenMP [2.16, Nesting of Regions] 4792 // If specified, a teams construct must be contained within a target 4793 // construct. 4794 NestingProhibited = 4795 (SemaRef.LangOpts.OpenMP <= 45 && ParentRegion != OMPD_target) || 4796 (SemaRef.LangOpts.OpenMP >= 50 && ParentRegion != OMPD_unknown && 4797 ParentRegion != OMPD_target); 4798 OrphanSeen = ParentRegion == OMPD_unknown; 4799 Recommend = ShouldBeInTargetRegion; 4800 } else if (CurrentRegion == OMPD_scan) { 4801 // OpenMP [2.16, Nesting of Regions] 4802 // If specified, a teams construct must be contained within a target 4803 // construct. 4804 NestingProhibited = 4805 SemaRef.LangOpts.OpenMP < 50 || 4806 (ParentRegion != OMPD_simd && ParentRegion != OMPD_for && 4807 ParentRegion != OMPD_for_simd && ParentRegion != OMPD_parallel_for && 4808 ParentRegion != OMPD_parallel_for_simd); 4809 OrphanSeen = ParentRegion == OMPD_unknown; 4810 Recommend = ShouldBeInLoopSimdRegion; 4811 } 4812 if (!NestingProhibited && 4813 !isOpenMPTargetExecutionDirective(CurrentRegion) && 4814 !isOpenMPTargetDataManagementDirective(CurrentRegion) && 4815 (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) { 4816 // OpenMP [2.16, Nesting of Regions] 4817 // distribute, parallel, parallel sections, parallel workshare, and the 4818 // parallel loop and parallel loop SIMD constructs are the only OpenMP 4819 // constructs that can be closely nested in the teams region. 4820 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) && 4821 !isOpenMPDistributeDirective(CurrentRegion); 4822 Recommend = ShouldBeInParallelRegion; 4823 } 4824 if (!NestingProhibited && 4825 isOpenMPNestingDistributeDirective(CurrentRegion)) { 4826 // OpenMP 4.5 [2.17 Nesting of Regions] 4827 // The region associated with the distribute construct must be strictly 4828 // nested inside a teams region 4829 NestingProhibited = 4830 (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams); 4831 Recommend = ShouldBeInTeamsRegion; 4832 } 4833 if (!NestingProhibited && 4834 (isOpenMPTargetExecutionDirective(CurrentRegion) || 4835 isOpenMPTargetDataManagementDirective(CurrentRegion))) { 4836 // OpenMP 4.5 [2.17 Nesting of Regions] 4837 // If a target, target update, target data, target enter data, or 4838 // target exit data construct is encountered during execution of a 4839 // target region, the behavior is unspecified. 4840 NestingProhibited = Stack->hasDirective( 4841 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &, 4842 SourceLocation) { 4843 if (isOpenMPTargetExecutionDirective(K)) { 4844 OffendingRegion = K; 4845 return true; 4846 } 4847 return false; 4848 }, 4849 false /* don't skip top directive */); 4850 CloseNesting = false; 4851 } 4852 if (NestingProhibited) { 4853 if (OrphanSeen) { 4854 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive) 4855 << getOpenMPDirectiveName(CurrentRegion) << Recommend; 4856 } else { 4857 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region) 4858 << CloseNesting << getOpenMPDirectiveName(OffendingRegion) 4859 << Recommend << getOpenMPDirectiveName(CurrentRegion); 4860 } 4861 return true; 4862 } 4863 } 4864 return false; 4865 } 4866 4867 struct Kind2Unsigned { 4868 using argument_type = OpenMPDirectiveKind; 4869 unsigned operator()(argument_type DK) { return unsigned(DK); } 4870 }; 4871 static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind, 4872 ArrayRef<OMPClause *> Clauses, 4873 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) { 4874 bool ErrorFound = false; 4875 unsigned NamedModifiersNumber = 0; 4876 llvm::IndexedMap<const OMPIfClause *, Kind2Unsigned> FoundNameModifiers; 4877 FoundNameModifiers.resize(llvm::omp::Directive_enumSize + 1); 4878 SmallVector<SourceLocation, 4> NameModifierLoc; 4879 for (const OMPClause *C : Clauses) { 4880 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) { 4881 // At most one if clause without a directive-name-modifier can appear on 4882 // the directive. 4883 OpenMPDirectiveKind CurNM = IC->getNameModifier(); 4884 if (FoundNameModifiers[CurNM]) { 4885 S.Diag(C->getBeginLoc(), diag::err_omp_more_one_clause) 4886 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if) 4887 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM); 4888 ErrorFound = true; 4889 } else if (CurNM != OMPD_unknown) { 4890 NameModifierLoc.push_back(IC->getNameModifierLoc()); 4891 ++NamedModifiersNumber; 4892 } 4893 FoundNameModifiers[CurNM] = IC; 4894 if (CurNM == OMPD_unknown) 4895 continue; 4896 // Check if the specified name modifier is allowed for the current 4897 // directive. 4898 // At most one if clause with the particular directive-name-modifier can 4899 // appear on the directive. 4900 bool MatchFound = false; 4901 for (auto NM : AllowedNameModifiers) { 4902 if (CurNM == NM) { 4903 MatchFound = true; 4904 break; 4905 } 4906 } 4907 if (!MatchFound) { 4908 S.Diag(IC->getNameModifierLoc(), 4909 diag::err_omp_wrong_if_directive_name_modifier) 4910 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind); 4911 ErrorFound = true; 4912 } 4913 } 4914 } 4915 // If any if clause on the directive includes a directive-name-modifier then 4916 // all if clauses on the directive must include a directive-name-modifier. 4917 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) { 4918 if (NamedModifiersNumber == AllowedNameModifiers.size()) { 4919 S.Diag(FoundNameModifiers[OMPD_unknown]->getBeginLoc(), 4920 diag::err_omp_no_more_if_clause); 4921 } else { 4922 std::string Values; 4923 std::string Sep(", "); 4924 unsigned AllowedCnt = 0; 4925 unsigned TotalAllowedNum = 4926 AllowedNameModifiers.size() - NamedModifiersNumber; 4927 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End; 4928 ++Cnt) { 4929 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt]; 4930 if (!FoundNameModifiers[NM]) { 4931 Values += "'"; 4932 Values += getOpenMPDirectiveName(NM); 4933 Values += "'"; 4934 if (AllowedCnt + 2 == TotalAllowedNum) 4935 Values += " or "; 4936 else if (AllowedCnt + 1 != TotalAllowedNum) 4937 Values += Sep; 4938 ++AllowedCnt; 4939 } 4940 } 4941 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getBeginLoc(), 4942 diag::err_omp_unnamed_if_clause) 4943 << (TotalAllowedNum > 1) << Values; 4944 } 4945 for (SourceLocation Loc : NameModifierLoc) { 4946 S.Diag(Loc, diag::note_omp_previous_named_if_clause); 4947 } 4948 ErrorFound = true; 4949 } 4950 return ErrorFound; 4951 } 4952 4953 static std::pair<ValueDecl *, bool> getPrivateItem(Sema &S, Expr *&RefExpr, 4954 SourceLocation &ELoc, 4955 SourceRange &ERange, 4956 bool AllowArraySection) { 4957 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() || 4958 RefExpr->containsUnexpandedParameterPack()) 4959 return std::make_pair(nullptr, true); 4960 4961 // OpenMP [3.1, C/C++] 4962 // A list item is a variable name. 4963 // OpenMP [2.9.3.3, Restrictions, p.1] 4964 // A variable that is part of another variable (as an array or 4965 // structure element) cannot appear in a private clause. 4966 RefExpr = RefExpr->IgnoreParens(); 4967 enum { 4968 NoArrayExpr = -1, 4969 ArraySubscript = 0, 4970 OMPArraySection = 1 4971 } IsArrayExpr = NoArrayExpr; 4972 if (AllowArraySection) { 4973 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) { 4974 Expr *Base = ASE->getBase()->IgnoreParenImpCasts(); 4975 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 4976 Base = TempASE->getBase()->IgnoreParenImpCasts(); 4977 RefExpr = Base; 4978 IsArrayExpr = ArraySubscript; 4979 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) { 4980 Expr *Base = OASE->getBase()->IgnoreParenImpCasts(); 4981 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) 4982 Base = TempOASE->getBase()->IgnoreParenImpCasts(); 4983 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 4984 Base = TempASE->getBase()->IgnoreParenImpCasts(); 4985 RefExpr = Base; 4986 IsArrayExpr = OMPArraySection; 4987 } 4988 } 4989 ELoc = RefExpr->getExprLoc(); 4990 ERange = RefExpr->getSourceRange(); 4991 RefExpr = RefExpr->IgnoreParenImpCasts(); 4992 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr); 4993 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr); 4994 if ((!DE || !isa<VarDecl>(DE->getDecl())) && 4995 (S.getCurrentThisType().isNull() || !ME || 4996 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) || 4997 !isa<FieldDecl>(ME->getMemberDecl()))) { 4998 if (IsArrayExpr != NoArrayExpr) { 4999 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr 5000 << ERange; 5001 } else { 5002 S.Diag(ELoc, 5003 AllowArraySection 5004 ? diag::err_omp_expected_var_name_member_expr_or_array_item 5005 : diag::err_omp_expected_var_name_member_expr) 5006 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange; 5007 } 5008 return std::make_pair(nullptr, false); 5009 } 5010 return std::make_pair( 5011 getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false); 5012 } 5013 5014 namespace { 5015 /// Checks if the allocator is used in uses_allocators clause to be allowed in 5016 /// target regions. 5017 class AllocatorChecker final : public ConstStmtVisitor<AllocatorChecker, bool> { 5018 DSAStackTy *S = nullptr; 5019 5020 public: 5021 bool VisitDeclRefExpr(const DeclRefExpr *E) { 5022 return S->isUsesAllocatorsDecl(E->getDecl()) 5023 .getValueOr( 5024 DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait) == 5025 DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait; 5026 } 5027 bool VisitStmt(const Stmt *S) { 5028 for (const Stmt *Child : S->children()) { 5029 if (Child && Visit(Child)) 5030 return true; 5031 } 5032 return false; 5033 } 5034 explicit AllocatorChecker(DSAStackTy *S) : S(S) {} 5035 }; 5036 } // namespace 5037 5038 static void checkAllocateClauses(Sema &S, DSAStackTy *Stack, 5039 ArrayRef<OMPClause *> Clauses) { 5040 assert(!S.CurContext->isDependentContext() && 5041 "Expected non-dependent context."); 5042 auto AllocateRange = 5043 llvm::make_filter_range(Clauses, OMPAllocateClause::classof); 5044 llvm::DenseMap<CanonicalDeclPtr<Decl>, CanonicalDeclPtr<VarDecl>> 5045 DeclToCopy; 5046 auto PrivateRange = llvm::make_filter_range(Clauses, [](const OMPClause *C) { 5047 return isOpenMPPrivate(C->getClauseKind()); 5048 }); 5049 for (OMPClause *Cl : PrivateRange) { 5050 MutableArrayRef<Expr *>::iterator I, It, Et; 5051 if (Cl->getClauseKind() == OMPC_private) { 5052 auto *PC = cast<OMPPrivateClause>(Cl); 5053 I = PC->private_copies().begin(); 5054 It = PC->varlist_begin(); 5055 Et = PC->varlist_end(); 5056 } else if (Cl->getClauseKind() == OMPC_firstprivate) { 5057 auto *PC = cast<OMPFirstprivateClause>(Cl); 5058 I = PC->private_copies().begin(); 5059 It = PC->varlist_begin(); 5060 Et = PC->varlist_end(); 5061 } else if (Cl->getClauseKind() == OMPC_lastprivate) { 5062 auto *PC = cast<OMPLastprivateClause>(Cl); 5063 I = PC->private_copies().begin(); 5064 It = PC->varlist_begin(); 5065 Et = PC->varlist_end(); 5066 } else if (Cl->getClauseKind() == OMPC_linear) { 5067 auto *PC = cast<OMPLinearClause>(Cl); 5068 I = PC->privates().begin(); 5069 It = PC->varlist_begin(); 5070 Et = PC->varlist_end(); 5071 } else if (Cl->getClauseKind() == OMPC_reduction) { 5072 auto *PC = cast<OMPReductionClause>(Cl); 5073 I = PC->privates().begin(); 5074 It = PC->varlist_begin(); 5075 Et = PC->varlist_end(); 5076 } else if (Cl->getClauseKind() == OMPC_task_reduction) { 5077 auto *PC = cast<OMPTaskReductionClause>(Cl); 5078 I = PC->privates().begin(); 5079 It = PC->varlist_begin(); 5080 Et = PC->varlist_end(); 5081 } else if (Cl->getClauseKind() == OMPC_in_reduction) { 5082 auto *PC = cast<OMPInReductionClause>(Cl); 5083 I = PC->privates().begin(); 5084 It = PC->varlist_begin(); 5085 Et = PC->varlist_end(); 5086 } else { 5087 llvm_unreachable("Expected private clause."); 5088 } 5089 for (Expr *E : llvm::make_range(It, Et)) { 5090 if (!*I) { 5091 ++I; 5092 continue; 5093 } 5094 SourceLocation ELoc; 5095 SourceRange ERange; 5096 Expr *SimpleRefExpr = E; 5097 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange, 5098 /*AllowArraySection=*/true); 5099 DeclToCopy.try_emplace(Res.first, 5100 cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl())); 5101 ++I; 5102 } 5103 } 5104 for (OMPClause *C : AllocateRange) { 5105 auto *AC = cast<OMPAllocateClause>(C); 5106 if (S.getLangOpts().OpenMP >= 50 && 5107 !Stack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>() && 5108 isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) && 5109 AC->getAllocator()) { 5110 Expr *Allocator = AC->getAllocator(); 5111 // OpenMP, 2.12.5 target Construct 5112 // Memory allocators that do not appear in a uses_allocators clause cannot 5113 // appear as an allocator in an allocate clause or be used in the target 5114 // region unless a requires directive with the dynamic_allocators clause 5115 // is present in the same compilation unit. 5116 AllocatorChecker Checker(Stack); 5117 if (Checker.Visit(Allocator)) 5118 S.Diag(Allocator->getExprLoc(), 5119 diag::err_omp_allocator_not_in_uses_allocators) 5120 << Allocator->getSourceRange(); 5121 } 5122 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind = 5123 getAllocatorKind(S, Stack, AC->getAllocator()); 5124 // OpenMP, 2.11.4 allocate Clause, Restrictions. 5125 // For task, taskloop or target directives, allocation requests to memory 5126 // allocators with the trait access set to thread result in unspecified 5127 // behavior. 5128 if (AllocatorKind == OMPAllocateDeclAttr::OMPThreadMemAlloc && 5129 (isOpenMPTaskingDirective(Stack->getCurrentDirective()) || 5130 isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()))) { 5131 S.Diag(AC->getAllocator()->getExprLoc(), 5132 diag::warn_omp_allocate_thread_on_task_target_directive) 5133 << getOpenMPDirectiveName(Stack->getCurrentDirective()); 5134 } 5135 for (Expr *E : AC->varlists()) { 5136 SourceLocation ELoc; 5137 SourceRange ERange; 5138 Expr *SimpleRefExpr = E; 5139 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange); 5140 ValueDecl *VD = Res.first; 5141 DSAStackTy::DSAVarData Data = Stack->getTopDSA(VD, /*FromParent=*/false); 5142 if (!isOpenMPPrivate(Data.CKind)) { 5143 S.Diag(E->getExprLoc(), 5144 diag::err_omp_expected_private_copy_for_allocate); 5145 continue; 5146 } 5147 VarDecl *PrivateVD = DeclToCopy[VD]; 5148 if (checkPreviousOMPAllocateAttribute(S, Stack, E, PrivateVD, 5149 AllocatorKind, AC->getAllocator())) 5150 continue; 5151 applyOMPAllocateAttribute(S, PrivateVD, AllocatorKind, AC->getAllocator(), 5152 E->getSourceRange()); 5153 } 5154 } 5155 } 5156 5157 namespace { 5158 /// Rewrite statements and expressions for Sema \p Actions CurContext. 5159 /// 5160 /// Used to wrap already parsed statements/expressions into a new CapturedStmt 5161 /// context. DeclRefExpr used inside the new context are changed to refer to the 5162 /// captured variable instead. 5163 class CaptureVars : public TreeTransform<CaptureVars> { 5164 using BaseTransform = TreeTransform<CaptureVars>; 5165 5166 public: 5167 CaptureVars(Sema &Actions) : BaseTransform(Actions) {} 5168 5169 bool AlwaysRebuild() { return true; } 5170 }; 5171 } // namespace 5172 5173 static VarDecl *precomputeExpr(Sema &Actions, 5174 SmallVectorImpl<Stmt *> &BodyStmts, Expr *E, 5175 StringRef Name) { 5176 Expr *NewE = AssertSuccess(CaptureVars(Actions).TransformExpr(E)); 5177 VarDecl *NewVar = buildVarDecl(Actions, {}, NewE->getType(), Name, nullptr, 5178 dyn_cast<DeclRefExpr>(E->IgnoreImplicit())); 5179 auto *NewDeclStmt = cast<DeclStmt>(AssertSuccess( 5180 Actions.ActOnDeclStmt(Actions.ConvertDeclToDeclGroup(NewVar), {}, {}))); 5181 Actions.AddInitializerToDecl(NewDeclStmt->getSingleDecl(), NewE, false); 5182 BodyStmts.push_back(NewDeclStmt); 5183 return NewVar; 5184 } 5185 5186 /// Create a closure that computes the number of iterations of a loop. 5187 /// 5188 /// \param Actions The Sema object. 5189 /// \param LogicalTy Type for the logical iteration number. 5190 /// \param Rel Comparison operator of the loop condition. 5191 /// \param StartExpr Value of the loop counter at the first iteration. 5192 /// \param StopExpr Expression the loop counter is compared against in the loop 5193 /// condition. \param StepExpr Amount of increment after each iteration. 5194 /// 5195 /// \return Closure (CapturedStmt) of the distance calculation. 5196 static CapturedStmt *buildDistanceFunc(Sema &Actions, QualType LogicalTy, 5197 BinaryOperator::Opcode Rel, 5198 Expr *StartExpr, Expr *StopExpr, 5199 Expr *StepExpr) { 5200 ASTContext &Ctx = Actions.getASTContext(); 5201 TypeSourceInfo *LogicalTSI = Ctx.getTrivialTypeSourceInfo(LogicalTy); 5202 5203 // Captured regions currently don't support return values, we use an 5204 // out-parameter instead. All inputs are implicit captures. 5205 // TODO: Instead of capturing each DeclRefExpr occurring in 5206 // StartExpr/StopExpr/Step, these could also be passed as a value capture. 5207 QualType ResultTy = Ctx.getLValueReferenceType(LogicalTy); 5208 Sema::CapturedParamNameType Params[] = {{"Distance", ResultTy}, 5209 {StringRef(), QualType()}}; 5210 Actions.ActOnCapturedRegionStart({}, nullptr, CR_Default, Params); 5211 5212 Stmt *Body; 5213 { 5214 Sema::CompoundScopeRAII CompoundScope(Actions); 5215 CapturedDecl *CS = cast<CapturedDecl>(Actions.CurContext); 5216 5217 // Get the LValue expression for the result. 5218 ImplicitParamDecl *DistParam = CS->getParam(0); 5219 DeclRefExpr *DistRef = Actions.BuildDeclRefExpr( 5220 DistParam, LogicalTy, VK_LValue, {}, nullptr, nullptr, {}, nullptr); 5221 5222 SmallVector<Stmt *, 4> BodyStmts; 5223 5224 // Capture all referenced variable references. 5225 // TODO: Instead of computing NewStart/NewStop/NewStep inside the 5226 // CapturedStmt, we could compute them before and capture the result, to be 5227 // used jointly with the LoopVar function. 5228 VarDecl *NewStart = precomputeExpr(Actions, BodyStmts, StartExpr, ".start"); 5229 VarDecl *NewStop = precomputeExpr(Actions, BodyStmts, StopExpr, ".stop"); 5230 VarDecl *NewStep = precomputeExpr(Actions, BodyStmts, StepExpr, ".step"); 5231 auto BuildVarRef = [&](VarDecl *VD) { 5232 return buildDeclRefExpr(Actions, VD, VD->getType(), {}); 5233 }; 5234 5235 IntegerLiteral *Zero = IntegerLiteral::Create( 5236 Ctx, llvm::APInt(Ctx.getIntWidth(LogicalTy), 0), LogicalTy, {}); 5237 Expr *Dist; 5238 if (Rel == BO_NE) { 5239 // When using a != comparison, the increment can be +1 or -1. This can be 5240 // dynamic at runtime, so we need to check for the direction. 5241 Expr *IsNegStep = AssertSuccess( 5242 Actions.BuildBinOp(nullptr, {}, BO_LT, BuildVarRef(NewStep), Zero)); 5243 5244 // Positive increment. 5245 Expr *ForwardRange = AssertSuccess(Actions.BuildBinOp( 5246 nullptr, {}, BO_Sub, BuildVarRef(NewStop), BuildVarRef(NewStart))); 5247 ForwardRange = AssertSuccess( 5248 Actions.BuildCStyleCastExpr({}, LogicalTSI, {}, ForwardRange)); 5249 Expr *ForwardDist = AssertSuccess(Actions.BuildBinOp( 5250 nullptr, {}, BO_Div, ForwardRange, BuildVarRef(NewStep))); 5251 5252 // Negative increment. 5253 Expr *BackwardRange = AssertSuccess(Actions.BuildBinOp( 5254 nullptr, {}, BO_Sub, BuildVarRef(NewStart), BuildVarRef(NewStop))); 5255 BackwardRange = AssertSuccess( 5256 Actions.BuildCStyleCastExpr({}, LogicalTSI, {}, BackwardRange)); 5257 Expr *NegIncAmount = AssertSuccess( 5258 Actions.BuildUnaryOp(nullptr, {}, UO_Minus, BuildVarRef(NewStep))); 5259 Expr *BackwardDist = AssertSuccess( 5260 Actions.BuildBinOp(nullptr, {}, BO_Div, BackwardRange, NegIncAmount)); 5261 5262 // Use the appropriate case. 5263 Dist = AssertSuccess(Actions.ActOnConditionalOp( 5264 {}, {}, IsNegStep, BackwardDist, ForwardDist)); 5265 } else { 5266 assert((Rel == BO_LT || Rel == BO_LE || Rel == BO_GE || Rel == BO_GT) && 5267 "Expected one of these relational operators"); 5268 5269 // We can derive the direction from any other comparison operator. It is 5270 // non well-formed OpenMP if Step increments/decrements in the other 5271 // directions. Whether at least the first iteration passes the loop 5272 // condition. 5273 Expr *HasAnyIteration = AssertSuccess(Actions.BuildBinOp( 5274 nullptr, {}, Rel, BuildVarRef(NewStart), BuildVarRef(NewStop))); 5275 5276 // Compute the range between first and last counter value. 5277 Expr *Range; 5278 if (Rel == BO_GE || Rel == BO_GT) 5279 Range = AssertSuccess(Actions.BuildBinOp( 5280 nullptr, {}, BO_Sub, BuildVarRef(NewStart), BuildVarRef(NewStop))); 5281 else 5282 Range = AssertSuccess(Actions.BuildBinOp( 5283 nullptr, {}, BO_Sub, BuildVarRef(NewStop), BuildVarRef(NewStart))); 5284 5285 // Ensure unsigned range space. 5286 Range = 5287 AssertSuccess(Actions.BuildCStyleCastExpr({}, LogicalTSI, {}, Range)); 5288 5289 if (Rel == BO_LE || Rel == BO_GE) { 5290 // Add one to the range if the relational operator is inclusive. 5291 Range = 5292 AssertSuccess(Actions.BuildUnaryOp(nullptr, {}, UO_PreInc, Range)); 5293 } 5294 5295 // Divide by the absolute step amount. 5296 Expr *Divisor = BuildVarRef(NewStep); 5297 if (Rel == BO_GE || Rel == BO_GT) 5298 Divisor = 5299 AssertSuccess(Actions.BuildUnaryOp(nullptr, {}, UO_Minus, Divisor)); 5300 Dist = AssertSuccess( 5301 Actions.BuildBinOp(nullptr, {}, BO_Div, Range, Divisor)); 5302 5303 // If there is not at least one iteration, the range contains garbage. Fix 5304 // to zero in this case. 5305 Dist = AssertSuccess( 5306 Actions.ActOnConditionalOp({}, {}, HasAnyIteration, Dist, Zero)); 5307 } 5308 5309 // Assign the result to the out-parameter. 5310 Stmt *ResultAssign = AssertSuccess(Actions.BuildBinOp( 5311 Actions.getCurScope(), {}, BO_Assign, DistRef, Dist)); 5312 BodyStmts.push_back(ResultAssign); 5313 5314 Body = AssertSuccess(Actions.ActOnCompoundStmt({}, {}, BodyStmts, false)); 5315 } 5316 5317 return cast<CapturedStmt>( 5318 AssertSuccess(Actions.ActOnCapturedRegionEnd(Body))); 5319 } 5320 5321 /// Create a closure that computes the loop variable from the logical iteration 5322 /// number. 5323 /// 5324 /// \param Actions The Sema object. 5325 /// \param LoopVarTy Type for the loop variable used for result value. 5326 /// \param LogicalTy Type for the logical iteration number. 5327 /// \param StartExpr Value of the loop counter at the first iteration. 5328 /// \param Step Amount of increment after each iteration. 5329 /// \param Deref Whether the loop variable is a dereference of the loop 5330 /// counter variable. 5331 /// 5332 /// \return Closure (CapturedStmt) of the loop value calculation. 5333 static CapturedStmt *buildLoopVarFunc(Sema &Actions, QualType LoopVarTy, 5334 QualType LogicalTy, 5335 DeclRefExpr *StartExpr, Expr *Step, 5336 bool Deref) { 5337 ASTContext &Ctx = Actions.getASTContext(); 5338 5339 // Pass the result as an out-parameter. Passing as return value would require 5340 // the OpenMPIRBuilder to know additional C/C++ semantics, such as how to 5341 // invoke a copy constructor. 5342 QualType TargetParamTy = Ctx.getLValueReferenceType(LoopVarTy); 5343 Sema::CapturedParamNameType Params[] = {{"LoopVar", TargetParamTy}, 5344 {"Logical", LogicalTy}, 5345 {StringRef(), QualType()}}; 5346 Actions.ActOnCapturedRegionStart({}, nullptr, CR_Default, Params); 5347 5348 // Capture the initial iterator which represents the LoopVar value at the 5349 // zero's logical iteration. Since the original ForStmt/CXXForRangeStmt update 5350 // it in every iteration, capture it by value before it is modified. 5351 VarDecl *StartVar = cast<VarDecl>(StartExpr->getDecl()); 5352 bool Invalid = Actions.tryCaptureVariable(StartVar, {}, 5353 Sema::TryCapture_ExplicitByVal, {}); 5354 (void)Invalid; 5355 assert(!Invalid && "Expecting capture-by-value to work."); 5356 5357 Expr *Body; 5358 { 5359 Sema::CompoundScopeRAII CompoundScope(Actions); 5360 auto *CS = cast<CapturedDecl>(Actions.CurContext); 5361 5362 ImplicitParamDecl *TargetParam = CS->getParam(0); 5363 DeclRefExpr *TargetRef = Actions.BuildDeclRefExpr( 5364 TargetParam, LoopVarTy, VK_LValue, {}, nullptr, nullptr, {}, nullptr); 5365 ImplicitParamDecl *IndvarParam = CS->getParam(1); 5366 DeclRefExpr *LogicalRef = Actions.BuildDeclRefExpr( 5367 IndvarParam, LogicalTy, VK_LValue, {}, nullptr, nullptr, {}, nullptr); 5368 5369 // Capture the Start expression. 5370 CaptureVars Recap(Actions); 5371 Expr *NewStart = AssertSuccess(Recap.TransformExpr(StartExpr)); 5372 Expr *NewStep = AssertSuccess(Recap.TransformExpr(Step)); 5373 5374 Expr *Skip = AssertSuccess( 5375 Actions.BuildBinOp(nullptr, {}, BO_Mul, NewStep, LogicalRef)); 5376 // TODO: Explicitly cast to the iterator's difference_type instead of 5377 // relying on implicit conversion. 5378 Expr *Advanced = 5379 AssertSuccess(Actions.BuildBinOp(nullptr, {}, BO_Add, NewStart, Skip)); 5380 5381 if (Deref) { 5382 // For range-based for-loops convert the loop counter value to a concrete 5383 // loop variable value by dereferencing the iterator. 5384 Advanced = 5385 AssertSuccess(Actions.BuildUnaryOp(nullptr, {}, UO_Deref, Advanced)); 5386 } 5387 5388 // Assign the result to the output parameter. 5389 Body = AssertSuccess(Actions.BuildBinOp(Actions.getCurScope(), {}, 5390 BO_Assign, TargetRef, Advanced)); 5391 } 5392 return cast<CapturedStmt>( 5393 AssertSuccess(Actions.ActOnCapturedRegionEnd(Body))); 5394 } 5395 5396 StmtResult Sema::ActOnOpenMPCanonicalLoop(Stmt *AStmt) { 5397 ASTContext &Ctx = getASTContext(); 5398 5399 // Extract the common elements of ForStmt and CXXForRangeStmt: 5400 // Loop variable, repeat condition, increment 5401 Expr *Cond, *Inc; 5402 VarDecl *LIVDecl, *LUVDecl; 5403 if (auto *For = dyn_cast<ForStmt>(AStmt)) { 5404 Stmt *Init = For->getInit(); 5405 if (auto *LCVarDeclStmt = dyn_cast<DeclStmt>(Init)) { 5406 // For statement declares loop variable. 5407 LIVDecl = cast<VarDecl>(LCVarDeclStmt->getSingleDecl()); 5408 } else if (auto *LCAssign = dyn_cast<BinaryOperator>(Init)) { 5409 // For statement reuses variable. 5410 assert(LCAssign->getOpcode() == BO_Assign && 5411 "init part must be a loop variable assignment"); 5412 auto *CounterRef = cast<DeclRefExpr>(LCAssign->getLHS()); 5413 LIVDecl = cast<VarDecl>(CounterRef->getDecl()); 5414 } else 5415 llvm_unreachable("Cannot determine loop variable"); 5416 LUVDecl = LIVDecl; 5417 5418 Cond = For->getCond(); 5419 Inc = For->getInc(); 5420 } else if (auto *RangeFor = dyn_cast<CXXForRangeStmt>(AStmt)) { 5421 DeclStmt *BeginStmt = RangeFor->getBeginStmt(); 5422 LIVDecl = cast<VarDecl>(BeginStmt->getSingleDecl()); 5423 LUVDecl = RangeFor->getLoopVariable(); 5424 5425 Cond = RangeFor->getCond(); 5426 Inc = RangeFor->getInc(); 5427 } else 5428 llvm_unreachable("unhandled kind of loop"); 5429 5430 QualType CounterTy = LIVDecl->getType(); 5431 QualType LVTy = LUVDecl->getType(); 5432 5433 // Analyze the loop condition. 5434 Expr *LHS, *RHS; 5435 BinaryOperator::Opcode CondRel; 5436 Cond = Cond->IgnoreImplicit(); 5437 if (auto *CondBinExpr = dyn_cast<BinaryOperator>(Cond)) { 5438 LHS = CondBinExpr->getLHS(); 5439 RHS = CondBinExpr->getRHS(); 5440 CondRel = CondBinExpr->getOpcode(); 5441 } else if (auto *CondCXXOp = dyn_cast<CXXOperatorCallExpr>(Cond)) { 5442 assert(CondCXXOp->getNumArgs() == 2 && "Comparison should have 2 operands"); 5443 LHS = CondCXXOp->getArg(0); 5444 RHS = CondCXXOp->getArg(1); 5445 switch (CondCXXOp->getOperator()) { 5446 case OO_ExclaimEqual: 5447 CondRel = BO_NE; 5448 break; 5449 case OO_Less: 5450 CondRel = BO_LT; 5451 break; 5452 case OO_LessEqual: 5453 CondRel = BO_LE; 5454 break; 5455 case OO_Greater: 5456 CondRel = BO_GT; 5457 break; 5458 case OO_GreaterEqual: 5459 CondRel = BO_GE; 5460 break; 5461 default: 5462 llvm_unreachable("unexpected iterator operator"); 5463 } 5464 } else 5465 llvm_unreachable("unexpected loop condition"); 5466 5467 // Normalize such that the loop counter is on the LHS. 5468 if (!isa<DeclRefExpr>(LHS->IgnoreImplicit()) || 5469 cast<DeclRefExpr>(LHS->IgnoreImplicit())->getDecl() != LIVDecl) { 5470 std::swap(LHS, RHS); 5471 CondRel = BinaryOperator::reverseComparisonOp(CondRel); 5472 } 5473 auto *CounterRef = cast<DeclRefExpr>(LHS->IgnoreImplicit()); 5474 5475 // Decide the bit width for the logical iteration counter. By default use the 5476 // unsigned ptrdiff_t integer size (for iterators and pointers). 5477 // TODO: For iterators, use iterator::difference_type, 5478 // std::iterator_traits<>::difference_type or decltype(it - end). 5479 QualType LogicalTy = Ctx.getUnsignedPointerDiffType(); 5480 if (CounterTy->isIntegerType()) { 5481 unsigned BitWidth = Ctx.getIntWidth(CounterTy); 5482 LogicalTy = Ctx.getIntTypeForBitwidth(BitWidth, false); 5483 } 5484 5485 // Analyze the loop increment. 5486 Expr *Step; 5487 if (auto *IncUn = dyn_cast<UnaryOperator>(Inc)) { 5488 int Direction; 5489 switch (IncUn->getOpcode()) { 5490 case UO_PreInc: 5491 case UO_PostInc: 5492 Direction = 1; 5493 break; 5494 case UO_PreDec: 5495 case UO_PostDec: 5496 Direction = -1; 5497 break; 5498 default: 5499 llvm_unreachable("unhandled unary increment operator"); 5500 } 5501 Step = IntegerLiteral::Create( 5502 Ctx, llvm::APInt(Ctx.getIntWidth(LogicalTy), Direction), LogicalTy, {}); 5503 } else if (auto *IncBin = dyn_cast<BinaryOperator>(Inc)) { 5504 if (IncBin->getOpcode() == BO_AddAssign) { 5505 Step = IncBin->getRHS(); 5506 } else if (IncBin->getOpcode() == BO_SubAssign) { 5507 Step = 5508 AssertSuccess(BuildUnaryOp(nullptr, {}, UO_Minus, IncBin->getRHS())); 5509 } else 5510 llvm_unreachable("unhandled binary increment operator"); 5511 } else if (auto *CondCXXOp = dyn_cast<CXXOperatorCallExpr>(Inc)) { 5512 switch (CondCXXOp->getOperator()) { 5513 case OO_PlusPlus: 5514 Step = IntegerLiteral::Create( 5515 Ctx, llvm::APInt(Ctx.getIntWidth(LogicalTy), 1), LogicalTy, {}); 5516 break; 5517 case OO_MinusMinus: 5518 Step = IntegerLiteral::Create( 5519 Ctx, llvm::APInt(Ctx.getIntWidth(LogicalTy), -1), LogicalTy, {}); 5520 break; 5521 case OO_PlusEqual: 5522 Step = CondCXXOp->getArg(1); 5523 break; 5524 case OO_MinusEqual: 5525 Step = AssertSuccess( 5526 BuildUnaryOp(nullptr, {}, UO_Minus, CondCXXOp->getArg(1))); 5527 break; 5528 default: 5529 llvm_unreachable("unhandled overloaded increment operator"); 5530 } 5531 } else 5532 llvm_unreachable("unknown increment expression"); 5533 5534 CapturedStmt *DistanceFunc = 5535 buildDistanceFunc(*this, LogicalTy, CondRel, LHS, RHS, Step); 5536 CapturedStmt *LoopVarFunc = buildLoopVarFunc( 5537 *this, LVTy, LogicalTy, CounterRef, Step, isa<CXXForRangeStmt>(AStmt)); 5538 DeclRefExpr *LVRef = BuildDeclRefExpr(LUVDecl, LUVDecl->getType(), VK_LValue, 5539 {}, nullptr, nullptr, {}, nullptr); 5540 return OMPCanonicalLoop::create(getASTContext(), AStmt, DistanceFunc, 5541 LoopVarFunc, LVRef); 5542 } 5543 5544 static ExprResult buildUserDefinedMapperRef(Sema &SemaRef, Scope *S, 5545 CXXScopeSpec &MapperIdScopeSpec, 5546 const DeclarationNameInfo &MapperId, 5547 QualType Type, 5548 Expr *UnresolvedMapper); 5549 5550 /// Perform DFS through the structure/class data members trying to find 5551 /// member(s) with user-defined 'default' mapper and generate implicit map 5552 /// clauses for such members with the found 'default' mapper. 5553 static void 5554 processImplicitMapsWithDefaultMappers(Sema &S, DSAStackTy *Stack, 5555 SmallVectorImpl<OMPClause *> &Clauses) { 5556 // Check for the deault mapper for data members. 5557 if (S.getLangOpts().OpenMP < 50) 5558 return; 5559 SmallVector<OMPClause *, 4> ImplicitMaps; 5560 DeclarationNameInfo DefaultMapperId; 5561 DefaultMapperId.setName(S.Context.DeclarationNames.getIdentifier( 5562 &S.Context.Idents.get("default"))); 5563 for (int Cnt = 0, EndCnt = Clauses.size(); Cnt < EndCnt; ++Cnt) { 5564 auto *C = dyn_cast<OMPMapClause>(Clauses[Cnt]); 5565 if (!C) 5566 continue; 5567 SmallVector<Expr *, 4> SubExprs; 5568 auto *MI = C->mapperlist_begin(); 5569 for (auto I = C->varlist_begin(), End = C->varlist_end(); I != End; 5570 ++I, ++MI) { 5571 // Expression is mapped using mapper - skip it. 5572 if (*MI) 5573 continue; 5574 Expr *E = *I; 5575 // Expression is dependent - skip it, build the mapper when it gets 5576 // instantiated. 5577 if (E->isTypeDependent() || E->isValueDependent() || 5578 E->containsUnexpandedParameterPack()) 5579 continue; 5580 // Array section - need to check for the mapping of the array section 5581 // element. 5582 QualType CanonType = E->getType().getCanonicalType(); 5583 if (CanonType->isSpecificBuiltinType(BuiltinType::OMPArraySection)) { 5584 const auto *OASE = cast<OMPArraySectionExpr>(E->IgnoreParenImpCasts()); 5585 QualType BaseType = 5586 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 5587 QualType ElemType; 5588 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 5589 ElemType = ATy->getElementType(); 5590 else 5591 ElemType = BaseType->getPointeeType(); 5592 CanonType = ElemType; 5593 } 5594 5595 // DFS over data members in structures/classes. 5596 SmallVector<std::pair<QualType, FieldDecl *>, 4> Types( 5597 1, {CanonType, nullptr}); 5598 llvm::DenseMap<const Type *, Expr *> Visited; 5599 SmallVector<std::pair<FieldDecl *, unsigned>, 4> ParentChain( 5600 1, {nullptr, 1}); 5601 while (!Types.empty()) { 5602 QualType BaseType; 5603 FieldDecl *CurFD; 5604 std::tie(BaseType, CurFD) = Types.pop_back_val(); 5605 while (ParentChain.back().second == 0) 5606 ParentChain.pop_back(); 5607 --ParentChain.back().second; 5608 if (BaseType.isNull()) 5609 continue; 5610 // Only structs/classes are allowed to have mappers. 5611 const RecordDecl *RD = BaseType.getCanonicalType()->getAsRecordDecl(); 5612 if (!RD) 5613 continue; 5614 auto It = Visited.find(BaseType.getTypePtr()); 5615 if (It == Visited.end()) { 5616 // Try to find the associated user-defined mapper. 5617 CXXScopeSpec MapperIdScopeSpec; 5618 ExprResult ER = buildUserDefinedMapperRef( 5619 S, Stack->getCurScope(), MapperIdScopeSpec, DefaultMapperId, 5620 BaseType, /*UnresolvedMapper=*/nullptr); 5621 if (ER.isInvalid()) 5622 continue; 5623 It = Visited.try_emplace(BaseType.getTypePtr(), ER.get()).first; 5624 } 5625 // Found default mapper. 5626 if (It->second) { 5627 auto *OE = new (S.Context) OpaqueValueExpr(E->getExprLoc(), CanonType, 5628 VK_LValue, OK_Ordinary, E); 5629 OE->setIsUnique(/*V=*/true); 5630 Expr *BaseExpr = OE; 5631 for (const auto &P : ParentChain) { 5632 if (P.first) { 5633 BaseExpr = S.BuildMemberExpr( 5634 BaseExpr, /*IsArrow=*/false, E->getExprLoc(), 5635 NestedNameSpecifierLoc(), SourceLocation(), P.first, 5636 DeclAccessPair::make(P.first, P.first->getAccess()), 5637 /*HadMultipleCandidates=*/false, DeclarationNameInfo(), 5638 P.first->getType(), VK_LValue, OK_Ordinary); 5639 BaseExpr = S.DefaultLvalueConversion(BaseExpr).get(); 5640 } 5641 } 5642 if (CurFD) 5643 BaseExpr = S.BuildMemberExpr( 5644 BaseExpr, /*IsArrow=*/false, E->getExprLoc(), 5645 NestedNameSpecifierLoc(), SourceLocation(), CurFD, 5646 DeclAccessPair::make(CurFD, CurFD->getAccess()), 5647 /*HadMultipleCandidates=*/false, DeclarationNameInfo(), 5648 CurFD->getType(), VK_LValue, OK_Ordinary); 5649 SubExprs.push_back(BaseExpr); 5650 continue; 5651 } 5652 // Check for the "default" mapper for data memebers. 5653 bool FirstIter = true; 5654 for (FieldDecl *FD : RD->fields()) { 5655 if (!FD) 5656 continue; 5657 QualType FieldTy = FD->getType(); 5658 if (FieldTy.isNull() || 5659 !(FieldTy->isStructureOrClassType() || FieldTy->isUnionType())) 5660 continue; 5661 if (FirstIter) { 5662 FirstIter = false; 5663 ParentChain.emplace_back(CurFD, 1); 5664 } else { 5665 ++ParentChain.back().second; 5666 } 5667 Types.emplace_back(FieldTy, FD); 5668 } 5669 } 5670 } 5671 if (SubExprs.empty()) 5672 continue; 5673 CXXScopeSpec MapperIdScopeSpec; 5674 DeclarationNameInfo MapperId; 5675 if (OMPClause *NewClause = S.ActOnOpenMPMapClause( 5676 C->getMapTypeModifiers(), C->getMapTypeModifiersLoc(), 5677 MapperIdScopeSpec, MapperId, C->getMapType(), 5678 /*IsMapTypeImplicit=*/true, SourceLocation(), SourceLocation(), 5679 SubExprs, OMPVarListLocTy())) 5680 Clauses.push_back(NewClause); 5681 } 5682 } 5683 5684 StmtResult Sema::ActOnOpenMPExecutableDirective( 5685 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName, 5686 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses, 5687 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { 5688 StmtResult Res = StmtError(); 5689 // First check CancelRegion which is then used in checkNestingOfRegions. 5690 if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) || 5691 checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion, 5692 StartLoc)) 5693 return StmtError(); 5694 5695 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit; 5696 VarsWithInheritedDSAType VarsWithInheritedDSA; 5697 bool ErrorFound = false; 5698 ClausesWithImplicit.append(Clauses.begin(), Clauses.end()); 5699 if (AStmt && !CurContext->isDependentContext() && Kind != OMPD_atomic && 5700 Kind != OMPD_critical && Kind != OMPD_section && Kind != OMPD_master && 5701 !isOpenMPLoopTransformationDirective(Kind)) { 5702 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5703 5704 // Check default data sharing attributes for referenced variables. 5705 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt)); 5706 int ThisCaptureLevel = getOpenMPCaptureLevels(Kind); 5707 Stmt *S = AStmt; 5708 while (--ThisCaptureLevel >= 0) 5709 S = cast<CapturedStmt>(S)->getCapturedStmt(); 5710 DSAChecker.Visit(S); 5711 if (!isOpenMPTargetDataManagementDirective(Kind) && 5712 !isOpenMPTaskingDirective(Kind)) { 5713 // Visit subcaptures to generate implicit clauses for captured vars. 5714 auto *CS = cast<CapturedStmt>(AStmt); 5715 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 5716 getOpenMPCaptureRegions(CaptureRegions, Kind); 5717 // Ignore outer tasking regions for target directives. 5718 if (CaptureRegions.size() > 1 && CaptureRegions.front() == OMPD_task) 5719 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 5720 DSAChecker.visitSubCaptures(CS); 5721 } 5722 if (DSAChecker.isErrorFound()) 5723 return StmtError(); 5724 // Generate list of implicitly defined firstprivate variables. 5725 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA(); 5726 5727 SmallVector<Expr *, 4> ImplicitFirstprivates( 5728 DSAChecker.getImplicitFirstprivate().begin(), 5729 DSAChecker.getImplicitFirstprivate().end()); 5730 const unsigned DefaultmapKindNum = OMPC_DEFAULTMAP_pointer + 1; 5731 SmallVector<Expr *, 4> ImplicitMaps[DefaultmapKindNum][OMPC_MAP_delete]; 5732 SmallVector<OpenMPMapModifierKind, NumberOfOMPMapClauseModifiers> 5733 ImplicitMapModifiers[DefaultmapKindNum]; 5734 SmallVector<SourceLocation, NumberOfOMPMapClauseModifiers> 5735 ImplicitMapModifiersLoc[DefaultmapKindNum]; 5736 // Get the original location of present modifier from Defaultmap clause. 5737 SourceLocation PresentModifierLocs[DefaultmapKindNum]; 5738 for (OMPClause *C : Clauses) { 5739 if (auto *DMC = dyn_cast<OMPDefaultmapClause>(C)) 5740 if (DMC->getDefaultmapModifier() == OMPC_DEFAULTMAP_MODIFIER_present) 5741 PresentModifierLocs[DMC->getDefaultmapKind()] = 5742 DMC->getDefaultmapModifierLoc(); 5743 } 5744 for (unsigned VC = 0; VC < DefaultmapKindNum; ++VC) { 5745 auto Kind = static_cast<OpenMPDefaultmapClauseKind>(VC); 5746 for (unsigned I = 0; I < OMPC_MAP_delete; ++I) { 5747 ArrayRef<Expr *> ImplicitMap = DSAChecker.getImplicitMap( 5748 Kind, static_cast<OpenMPMapClauseKind>(I)); 5749 ImplicitMaps[VC][I].append(ImplicitMap.begin(), ImplicitMap.end()); 5750 } 5751 ArrayRef<OpenMPMapModifierKind> ImplicitModifier = 5752 DSAChecker.getImplicitMapModifier(Kind); 5753 ImplicitMapModifiers[VC].append(ImplicitModifier.begin(), 5754 ImplicitModifier.end()); 5755 std::fill_n(std::back_inserter(ImplicitMapModifiersLoc[VC]), 5756 ImplicitModifier.size(), PresentModifierLocs[VC]); 5757 } 5758 // Mark taskgroup task_reduction descriptors as implicitly firstprivate. 5759 for (OMPClause *C : Clauses) { 5760 if (auto *IRC = dyn_cast<OMPInReductionClause>(C)) { 5761 for (Expr *E : IRC->taskgroup_descriptors()) 5762 if (E) 5763 ImplicitFirstprivates.emplace_back(E); 5764 } 5765 // OpenMP 5.0, 2.10.1 task Construct 5766 // [detach clause]... The event-handle will be considered as if it was 5767 // specified on a firstprivate clause. 5768 if (auto *DC = dyn_cast<OMPDetachClause>(C)) 5769 ImplicitFirstprivates.push_back(DC->getEventHandler()); 5770 } 5771 if (!ImplicitFirstprivates.empty()) { 5772 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause( 5773 ImplicitFirstprivates, SourceLocation(), SourceLocation(), 5774 SourceLocation())) { 5775 ClausesWithImplicit.push_back(Implicit); 5776 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() != 5777 ImplicitFirstprivates.size(); 5778 } else { 5779 ErrorFound = true; 5780 } 5781 } 5782 for (unsigned I = 0, E = DefaultmapKindNum; I < E; ++I) { 5783 int ClauseKindCnt = -1; 5784 for (ArrayRef<Expr *> ImplicitMap : ImplicitMaps[I]) { 5785 ++ClauseKindCnt; 5786 if (ImplicitMap.empty()) 5787 continue; 5788 CXXScopeSpec MapperIdScopeSpec; 5789 DeclarationNameInfo MapperId; 5790 auto Kind = static_cast<OpenMPMapClauseKind>(ClauseKindCnt); 5791 if (OMPClause *Implicit = ActOnOpenMPMapClause( 5792 ImplicitMapModifiers[I], ImplicitMapModifiersLoc[I], 5793 MapperIdScopeSpec, MapperId, Kind, /*IsMapTypeImplicit=*/true, 5794 SourceLocation(), SourceLocation(), ImplicitMap, 5795 OMPVarListLocTy())) { 5796 ClausesWithImplicit.emplace_back(Implicit); 5797 ErrorFound |= cast<OMPMapClause>(Implicit)->varlist_size() != 5798 ImplicitMap.size(); 5799 } else { 5800 ErrorFound = true; 5801 } 5802 } 5803 } 5804 // Build expressions for implicit maps of data members with 'default' 5805 // mappers. 5806 if (LangOpts.OpenMP >= 50) 5807 processImplicitMapsWithDefaultMappers(*this, DSAStack, 5808 ClausesWithImplicit); 5809 } 5810 5811 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers; 5812 switch (Kind) { 5813 case OMPD_parallel: 5814 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc, 5815 EndLoc); 5816 AllowedNameModifiers.push_back(OMPD_parallel); 5817 break; 5818 case OMPD_simd: 5819 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, 5820 VarsWithInheritedDSA); 5821 if (LangOpts.OpenMP >= 50) 5822 AllowedNameModifiers.push_back(OMPD_simd); 5823 break; 5824 case OMPD_tile: 5825 Res = 5826 ActOnOpenMPTileDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 5827 break; 5828 case OMPD_for: 5829 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, 5830 VarsWithInheritedDSA); 5831 break; 5832 case OMPD_for_simd: 5833 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 5834 EndLoc, VarsWithInheritedDSA); 5835 if (LangOpts.OpenMP >= 50) 5836 AllowedNameModifiers.push_back(OMPD_simd); 5837 break; 5838 case OMPD_sections: 5839 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc, 5840 EndLoc); 5841 break; 5842 case OMPD_section: 5843 assert(ClausesWithImplicit.empty() && 5844 "No clauses are allowed for 'omp section' directive"); 5845 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc); 5846 break; 5847 case OMPD_single: 5848 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc, 5849 EndLoc); 5850 break; 5851 case OMPD_master: 5852 assert(ClausesWithImplicit.empty() && 5853 "No clauses are allowed for 'omp master' directive"); 5854 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc); 5855 break; 5856 case OMPD_critical: 5857 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt, 5858 StartLoc, EndLoc); 5859 break; 5860 case OMPD_parallel_for: 5861 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc, 5862 EndLoc, VarsWithInheritedDSA); 5863 AllowedNameModifiers.push_back(OMPD_parallel); 5864 break; 5865 case OMPD_parallel_for_simd: 5866 Res = ActOnOpenMPParallelForSimdDirective( 5867 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5868 AllowedNameModifiers.push_back(OMPD_parallel); 5869 if (LangOpts.OpenMP >= 50) 5870 AllowedNameModifiers.push_back(OMPD_simd); 5871 break; 5872 case OMPD_parallel_master: 5873 Res = ActOnOpenMPParallelMasterDirective(ClausesWithImplicit, AStmt, 5874 StartLoc, EndLoc); 5875 AllowedNameModifiers.push_back(OMPD_parallel); 5876 break; 5877 case OMPD_parallel_sections: 5878 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt, 5879 StartLoc, EndLoc); 5880 AllowedNameModifiers.push_back(OMPD_parallel); 5881 break; 5882 case OMPD_task: 5883 Res = 5884 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 5885 AllowedNameModifiers.push_back(OMPD_task); 5886 break; 5887 case OMPD_taskyield: 5888 assert(ClausesWithImplicit.empty() && 5889 "No clauses are allowed for 'omp taskyield' directive"); 5890 assert(AStmt == nullptr && 5891 "No associated statement allowed for 'omp taskyield' directive"); 5892 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc); 5893 break; 5894 case OMPD_barrier: 5895 assert(ClausesWithImplicit.empty() && 5896 "No clauses are allowed for 'omp barrier' directive"); 5897 assert(AStmt == nullptr && 5898 "No associated statement allowed for 'omp barrier' directive"); 5899 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc); 5900 break; 5901 case OMPD_taskwait: 5902 assert(ClausesWithImplicit.empty() && 5903 "No clauses are allowed for 'omp taskwait' directive"); 5904 assert(AStmt == nullptr && 5905 "No associated statement allowed for 'omp taskwait' directive"); 5906 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc); 5907 break; 5908 case OMPD_taskgroup: 5909 Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc, 5910 EndLoc); 5911 break; 5912 case OMPD_flush: 5913 assert(AStmt == nullptr && 5914 "No associated statement allowed for 'omp flush' directive"); 5915 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc); 5916 break; 5917 case OMPD_depobj: 5918 assert(AStmt == nullptr && 5919 "No associated statement allowed for 'omp depobj' directive"); 5920 Res = ActOnOpenMPDepobjDirective(ClausesWithImplicit, StartLoc, EndLoc); 5921 break; 5922 case OMPD_scan: 5923 assert(AStmt == nullptr && 5924 "No associated statement allowed for 'omp scan' directive"); 5925 Res = ActOnOpenMPScanDirective(ClausesWithImplicit, StartLoc, EndLoc); 5926 break; 5927 case OMPD_ordered: 5928 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc, 5929 EndLoc); 5930 break; 5931 case OMPD_atomic: 5932 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc, 5933 EndLoc); 5934 break; 5935 case OMPD_teams: 5936 Res = 5937 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 5938 break; 5939 case OMPD_target: 5940 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc, 5941 EndLoc); 5942 AllowedNameModifiers.push_back(OMPD_target); 5943 break; 5944 case OMPD_target_parallel: 5945 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt, 5946 StartLoc, EndLoc); 5947 AllowedNameModifiers.push_back(OMPD_target); 5948 AllowedNameModifiers.push_back(OMPD_parallel); 5949 break; 5950 case OMPD_target_parallel_for: 5951 Res = ActOnOpenMPTargetParallelForDirective( 5952 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5953 AllowedNameModifiers.push_back(OMPD_target); 5954 AllowedNameModifiers.push_back(OMPD_parallel); 5955 break; 5956 case OMPD_cancellation_point: 5957 assert(ClausesWithImplicit.empty() && 5958 "No clauses are allowed for 'omp cancellation point' directive"); 5959 assert(AStmt == nullptr && "No associated statement allowed for 'omp " 5960 "cancellation point' directive"); 5961 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion); 5962 break; 5963 case OMPD_cancel: 5964 assert(AStmt == nullptr && 5965 "No associated statement allowed for 'omp cancel' directive"); 5966 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc, 5967 CancelRegion); 5968 AllowedNameModifiers.push_back(OMPD_cancel); 5969 break; 5970 case OMPD_target_data: 5971 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc, 5972 EndLoc); 5973 AllowedNameModifiers.push_back(OMPD_target_data); 5974 break; 5975 case OMPD_target_enter_data: 5976 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc, 5977 EndLoc, AStmt); 5978 AllowedNameModifiers.push_back(OMPD_target_enter_data); 5979 break; 5980 case OMPD_target_exit_data: 5981 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc, 5982 EndLoc, AStmt); 5983 AllowedNameModifiers.push_back(OMPD_target_exit_data); 5984 break; 5985 case OMPD_taskloop: 5986 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc, 5987 EndLoc, VarsWithInheritedDSA); 5988 AllowedNameModifiers.push_back(OMPD_taskloop); 5989 break; 5990 case OMPD_taskloop_simd: 5991 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 5992 EndLoc, VarsWithInheritedDSA); 5993 AllowedNameModifiers.push_back(OMPD_taskloop); 5994 if (LangOpts.OpenMP >= 50) 5995 AllowedNameModifiers.push_back(OMPD_simd); 5996 break; 5997 case OMPD_master_taskloop: 5998 Res = ActOnOpenMPMasterTaskLoopDirective( 5999 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6000 AllowedNameModifiers.push_back(OMPD_taskloop); 6001 break; 6002 case OMPD_master_taskloop_simd: 6003 Res = ActOnOpenMPMasterTaskLoopSimdDirective( 6004 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6005 AllowedNameModifiers.push_back(OMPD_taskloop); 6006 if (LangOpts.OpenMP >= 50) 6007 AllowedNameModifiers.push_back(OMPD_simd); 6008 break; 6009 case OMPD_parallel_master_taskloop: 6010 Res = ActOnOpenMPParallelMasterTaskLoopDirective( 6011 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6012 AllowedNameModifiers.push_back(OMPD_taskloop); 6013 AllowedNameModifiers.push_back(OMPD_parallel); 6014 break; 6015 case OMPD_parallel_master_taskloop_simd: 6016 Res = ActOnOpenMPParallelMasterTaskLoopSimdDirective( 6017 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6018 AllowedNameModifiers.push_back(OMPD_taskloop); 6019 AllowedNameModifiers.push_back(OMPD_parallel); 6020 if (LangOpts.OpenMP >= 50) 6021 AllowedNameModifiers.push_back(OMPD_simd); 6022 break; 6023 case OMPD_distribute: 6024 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc, 6025 EndLoc, VarsWithInheritedDSA); 6026 break; 6027 case OMPD_target_update: 6028 Res = ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc, 6029 EndLoc, AStmt); 6030 AllowedNameModifiers.push_back(OMPD_target_update); 6031 break; 6032 case OMPD_distribute_parallel_for: 6033 Res = ActOnOpenMPDistributeParallelForDirective( 6034 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6035 AllowedNameModifiers.push_back(OMPD_parallel); 6036 break; 6037 case OMPD_distribute_parallel_for_simd: 6038 Res = ActOnOpenMPDistributeParallelForSimdDirective( 6039 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6040 AllowedNameModifiers.push_back(OMPD_parallel); 6041 if (LangOpts.OpenMP >= 50) 6042 AllowedNameModifiers.push_back(OMPD_simd); 6043 break; 6044 case OMPD_distribute_simd: 6045 Res = ActOnOpenMPDistributeSimdDirective( 6046 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6047 if (LangOpts.OpenMP >= 50) 6048 AllowedNameModifiers.push_back(OMPD_simd); 6049 break; 6050 case OMPD_target_parallel_for_simd: 6051 Res = ActOnOpenMPTargetParallelForSimdDirective( 6052 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6053 AllowedNameModifiers.push_back(OMPD_target); 6054 AllowedNameModifiers.push_back(OMPD_parallel); 6055 if (LangOpts.OpenMP >= 50) 6056 AllowedNameModifiers.push_back(OMPD_simd); 6057 break; 6058 case OMPD_target_simd: 6059 Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 6060 EndLoc, VarsWithInheritedDSA); 6061 AllowedNameModifiers.push_back(OMPD_target); 6062 if (LangOpts.OpenMP >= 50) 6063 AllowedNameModifiers.push_back(OMPD_simd); 6064 break; 6065 case OMPD_teams_distribute: 6066 Res = ActOnOpenMPTeamsDistributeDirective( 6067 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6068 break; 6069 case OMPD_teams_distribute_simd: 6070 Res = ActOnOpenMPTeamsDistributeSimdDirective( 6071 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6072 if (LangOpts.OpenMP >= 50) 6073 AllowedNameModifiers.push_back(OMPD_simd); 6074 break; 6075 case OMPD_teams_distribute_parallel_for_simd: 6076 Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective( 6077 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6078 AllowedNameModifiers.push_back(OMPD_parallel); 6079 if (LangOpts.OpenMP >= 50) 6080 AllowedNameModifiers.push_back(OMPD_simd); 6081 break; 6082 case OMPD_teams_distribute_parallel_for: 6083 Res = ActOnOpenMPTeamsDistributeParallelForDirective( 6084 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6085 AllowedNameModifiers.push_back(OMPD_parallel); 6086 break; 6087 case OMPD_target_teams: 6088 Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, 6089 EndLoc); 6090 AllowedNameModifiers.push_back(OMPD_target); 6091 break; 6092 case OMPD_target_teams_distribute: 6093 Res = ActOnOpenMPTargetTeamsDistributeDirective( 6094 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6095 AllowedNameModifiers.push_back(OMPD_target); 6096 break; 6097 case OMPD_target_teams_distribute_parallel_for: 6098 Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective( 6099 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6100 AllowedNameModifiers.push_back(OMPD_target); 6101 AllowedNameModifiers.push_back(OMPD_parallel); 6102 break; 6103 case OMPD_target_teams_distribute_parallel_for_simd: 6104 Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective( 6105 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6106 AllowedNameModifiers.push_back(OMPD_target); 6107 AllowedNameModifiers.push_back(OMPD_parallel); 6108 if (LangOpts.OpenMP >= 50) 6109 AllowedNameModifiers.push_back(OMPD_simd); 6110 break; 6111 case OMPD_target_teams_distribute_simd: 6112 Res = ActOnOpenMPTargetTeamsDistributeSimdDirective( 6113 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6114 AllowedNameModifiers.push_back(OMPD_target); 6115 if (LangOpts.OpenMP >= 50) 6116 AllowedNameModifiers.push_back(OMPD_simd); 6117 break; 6118 case OMPD_declare_target: 6119 case OMPD_end_declare_target: 6120 case OMPD_threadprivate: 6121 case OMPD_allocate: 6122 case OMPD_declare_reduction: 6123 case OMPD_declare_mapper: 6124 case OMPD_declare_simd: 6125 case OMPD_requires: 6126 case OMPD_declare_variant: 6127 case OMPD_begin_declare_variant: 6128 case OMPD_end_declare_variant: 6129 llvm_unreachable("OpenMP Directive is not allowed"); 6130 case OMPD_unknown: 6131 default: 6132 llvm_unreachable("Unknown OpenMP directive"); 6133 } 6134 6135 ErrorFound = Res.isInvalid() || ErrorFound; 6136 6137 // Check variables in the clauses if default(none) or 6138 // default(firstprivate) was specified. 6139 if (DSAStack->getDefaultDSA() == DSA_none || 6140 DSAStack->getDefaultDSA() == DSA_firstprivate) { 6141 DSAAttrChecker DSAChecker(DSAStack, *this, nullptr); 6142 for (OMPClause *C : Clauses) { 6143 switch (C->getClauseKind()) { 6144 case OMPC_num_threads: 6145 case OMPC_dist_schedule: 6146 // Do not analyse if no parent teams directive. 6147 if (isOpenMPTeamsDirective(Kind)) 6148 break; 6149 continue; 6150 case OMPC_if: 6151 if (isOpenMPTeamsDirective(Kind) && 6152 cast<OMPIfClause>(C)->getNameModifier() != OMPD_target) 6153 break; 6154 if (isOpenMPParallelDirective(Kind) && 6155 isOpenMPTaskLoopDirective(Kind) && 6156 cast<OMPIfClause>(C)->getNameModifier() != OMPD_parallel) 6157 break; 6158 continue; 6159 case OMPC_schedule: 6160 case OMPC_detach: 6161 break; 6162 case OMPC_grainsize: 6163 case OMPC_num_tasks: 6164 case OMPC_final: 6165 case OMPC_priority: 6166 // Do not analyze if no parent parallel directive. 6167 if (isOpenMPParallelDirective(Kind)) 6168 break; 6169 continue; 6170 case OMPC_ordered: 6171 case OMPC_device: 6172 case OMPC_num_teams: 6173 case OMPC_thread_limit: 6174 case OMPC_hint: 6175 case OMPC_collapse: 6176 case OMPC_safelen: 6177 case OMPC_simdlen: 6178 case OMPC_sizes: 6179 case OMPC_default: 6180 case OMPC_proc_bind: 6181 case OMPC_private: 6182 case OMPC_firstprivate: 6183 case OMPC_lastprivate: 6184 case OMPC_shared: 6185 case OMPC_reduction: 6186 case OMPC_task_reduction: 6187 case OMPC_in_reduction: 6188 case OMPC_linear: 6189 case OMPC_aligned: 6190 case OMPC_copyin: 6191 case OMPC_copyprivate: 6192 case OMPC_nowait: 6193 case OMPC_untied: 6194 case OMPC_mergeable: 6195 case OMPC_allocate: 6196 case OMPC_read: 6197 case OMPC_write: 6198 case OMPC_update: 6199 case OMPC_capture: 6200 case OMPC_seq_cst: 6201 case OMPC_acq_rel: 6202 case OMPC_acquire: 6203 case OMPC_release: 6204 case OMPC_relaxed: 6205 case OMPC_depend: 6206 case OMPC_threads: 6207 case OMPC_simd: 6208 case OMPC_map: 6209 case OMPC_nogroup: 6210 case OMPC_defaultmap: 6211 case OMPC_to: 6212 case OMPC_from: 6213 case OMPC_use_device_ptr: 6214 case OMPC_use_device_addr: 6215 case OMPC_is_device_ptr: 6216 case OMPC_nontemporal: 6217 case OMPC_order: 6218 case OMPC_destroy: 6219 case OMPC_inclusive: 6220 case OMPC_exclusive: 6221 case OMPC_uses_allocators: 6222 case OMPC_affinity: 6223 continue; 6224 case OMPC_allocator: 6225 case OMPC_flush: 6226 case OMPC_depobj: 6227 case OMPC_threadprivate: 6228 case OMPC_uniform: 6229 case OMPC_unknown: 6230 case OMPC_unified_address: 6231 case OMPC_unified_shared_memory: 6232 case OMPC_reverse_offload: 6233 case OMPC_dynamic_allocators: 6234 case OMPC_atomic_default_mem_order: 6235 case OMPC_device_type: 6236 case OMPC_match: 6237 default: 6238 llvm_unreachable("Unexpected clause"); 6239 } 6240 for (Stmt *CC : C->children()) { 6241 if (CC) 6242 DSAChecker.Visit(CC); 6243 } 6244 } 6245 for (const auto &P : DSAChecker.getVarsWithInheritedDSA()) 6246 VarsWithInheritedDSA[P.getFirst()] = P.getSecond(); 6247 } 6248 for (const auto &P : VarsWithInheritedDSA) { 6249 if (P.getFirst()->isImplicit() || isa<OMPCapturedExprDecl>(P.getFirst())) 6250 continue; 6251 ErrorFound = true; 6252 if (DSAStack->getDefaultDSA() == DSA_none || 6253 DSAStack->getDefaultDSA() == DSA_firstprivate) { 6254 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable) 6255 << P.first << P.second->getSourceRange(); 6256 Diag(DSAStack->getDefaultDSALocation(), diag::note_omp_default_dsa_none); 6257 } else if (getLangOpts().OpenMP >= 50) { 6258 Diag(P.second->getExprLoc(), 6259 diag::err_omp_defaultmap_no_attr_for_variable) 6260 << P.first << P.second->getSourceRange(); 6261 Diag(DSAStack->getDefaultDSALocation(), 6262 diag::note_omp_defaultmap_attr_none); 6263 } 6264 } 6265 6266 if (!AllowedNameModifiers.empty()) 6267 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) || 6268 ErrorFound; 6269 6270 if (ErrorFound) 6271 return StmtError(); 6272 6273 if (!CurContext->isDependentContext() && 6274 isOpenMPTargetExecutionDirective(Kind) && 6275 !(DSAStack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() || 6276 DSAStack->hasRequiresDeclWithClause<OMPUnifiedAddressClause>() || 6277 DSAStack->hasRequiresDeclWithClause<OMPReverseOffloadClause>() || 6278 DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())) { 6279 // Register target to DSA Stack. 6280 DSAStack->addTargetDirLocation(StartLoc); 6281 } 6282 6283 return Res; 6284 } 6285 6286 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective( 6287 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen, 6288 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds, 6289 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears, 6290 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) { 6291 assert(Aligneds.size() == Alignments.size()); 6292 assert(Linears.size() == LinModifiers.size()); 6293 assert(Linears.size() == Steps.size()); 6294 if (!DG || DG.get().isNull()) 6295 return DeclGroupPtrTy(); 6296 6297 const int SimdId = 0; 6298 if (!DG.get().isSingleDecl()) { 6299 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd_variant) 6300 << SimdId; 6301 return DG; 6302 } 6303 Decl *ADecl = DG.get().getSingleDecl(); 6304 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl)) 6305 ADecl = FTD->getTemplatedDecl(); 6306 6307 auto *FD = dyn_cast<FunctionDecl>(ADecl); 6308 if (!FD) { 6309 Diag(ADecl->getLocation(), diag::err_omp_function_expected) << SimdId; 6310 return DeclGroupPtrTy(); 6311 } 6312 6313 // OpenMP [2.8.2, declare simd construct, Description] 6314 // The parameter of the simdlen clause must be a constant positive integer 6315 // expression. 6316 ExprResult SL; 6317 if (Simdlen) 6318 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen); 6319 // OpenMP [2.8.2, declare simd construct, Description] 6320 // The special this pointer can be used as if was one of the arguments to the 6321 // function in any of the linear, aligned, or uniform clauses. 6322 // The uniform clause declares one or more arguments to have an invariant 6323 // value for all concurrent invocations of the function in the execution of a 6324 // single SIMD loop. 6325 llvm::DenseMap<const Decl *, const Expr *> UniformedArgs; 6326 const Expr *UniformedLinearThis = nullptr; 6327 for (const Expr *E : Uniforms) { 6328 E = E->IgnoreParenImpCasts(); 6329 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 6330 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) 6331 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 6332 FD->getParamDecl(PVD->getFunctionScopeIndex()) 6333 ->getCanonicalDecl() == PVD->getCanonicalDecl()) { 6334 UniformedArgs.try_emplace(PVD->getCanonicalDecl(), E); 6335 continue; 6336 } 6337 if (isa<CXXThisExpr>(E)) { 6338 UniformedLinearThis = E; 6339 continue; 6340 } 6341 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 6342 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 6343 } 6344 // OpenMP [2.8.2, declare simd construct, Description] 6345 // The aligned clause declares that the object to which each list item points 6346 // is aligned to the number of bytes expressed in the optional parameter of 6347 // the aligned clause. 6348 // The special this pointer can be used as if was one of the arguments to the 6349 // function in any of the linear, aligned, or uniform clauses. 6350 // The type of list items appearing in the aligned clause must be array, 6351 // pointer, reference to array, or reference to pointer. 6352 llvm::DenseMap<const Decl *, const Expr *> AlignedArgs; 6353 const Expr *AlignedThis = nullptr; 6354 for (const Expr *E : Aligneds) { 6355 E = E->IgnoreParenImpCasts(); 6356 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 6357 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 6358 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 6359 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 6360 FD->getParamDecl(PVD->getFunctionScopeIndex()) 6361 ->getCanonicalDecl() == CanonPVD) { 6362 // OpenMP [2.8.1, simd construct, Restrictions] 6363 // A list-item cannot appear in more than one aligned clause. 6364 if (AlignedArgs.count(CanonPVD) > 0) { 6365 Diag(E->getExprLoc(), diag::err_omp_used_in_clause_twice) 6366 << 1 << getOpenMPClauseName(OMPC_aligned) 6367 << E->getSourceRange(); 6368 Diag(AlignedArgs[CanonPVD]->getExprLoc(), 6369 diag::note_omp_explicit_dsa) 6370 << getOpenMPClauseName(OMPC_aligned); 6371 continue; 6372 } 6373 AlignedArgs[CanonPVD] = E; 6374 QualType QTy = PVD->getType() 6375 .getNonReferenceType() 6376 .getUnqualifiedType() 6377 .getCanonicalType(); 6378 const Type *Ty = QTy.getTypePtrOrNull(); 6379 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) { 6380 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr) 6381 << QTy << getLangOpts().CPlusPlus << E->getSourceRange(); 6382 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD; 6383 } 6384 continue; 6385 } 6386 } 6387 if (isa<CXXThisExpr>(E)) { 6388 if (AlignedThis) { 6389 Diag(E->getExprLoc(), diag::err_omp_used_in_clause_twice) 6390 << 2 << getOpenMPClauseName(OMPC_aligned) << E->getSourceRange(); 6391 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa) 6392 << getOpenMPClauseName(OMPC_aligned); 6393 } 6394 AlignedThis = E; 6395 continue; 6396 } 6397 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 6398 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 6399 } 6400 // The optional parameter of the aligned clause, alignment, must be a constant 6401 // positive integer expression. If no optional parameter is specified, 6402 // implementation-defined default alignments for SIMD instructions on the 6403 // target platforms are assumed. 6404 SmallVector<const Expr *, 4> NewAligns; 6405 for (Expr *E : Alignments) { 6406 ExprResult Align; 6407 if (E) 6408 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned); 6409 NewAligns.push_back(Align.get()); 6410 } 6411 // OpenMP [2.8.2, declare simd construct, Description] 6412 // The linear clause declares one or more list items to be private to a SIMD 6413 // lane and to have a linear relationship with respect to the iteration space 6414 // of a loop. 6415 // The special this pointer can be used as if was one of the arguments to the 6416 // function in any of the linear, aligned, or uniform clauses. 6417 // When a linear-step expression is specified in a linear clause it must be 6418 // either a constant integer expression or an integer-typed parameter that is 6419 // specified in a uniform clause on the directive. 6420 llvm::DenseMap<const Decl *, const Expr *> LinearArgs; 6421 const bool IsUniformedThis = UniformedLinearThis != nullptr; 6422 auto MI = LinModifiers.begin(); 6423 for (const Expr *E : Linears) { 6424 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI); 6425 ++MI; 6426 E = E->IgnoreParenImpCasts(); 6427 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 6428 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 6429 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 6430 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 6431 FD->getParamDecl(PVD->getFunctionScopeIndex()) 6432 ->getCanonicalDecl() == CanonPVD) { 6433 // OpenMP [2.15.3.7, linear Clause, Restrictions] 6434 // A list-item cannot appear in more than one linear clause. 6435 if (LinearArgs.count(CanonPVD) > 0) { 6436 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 6437 << getOpenMPClauseName(OMPC_linear) 6438 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange(); 6439 Diag(LinearArgs[CanonPVD]->getExprLoc(), 6440 diag::note_omp_explicit_dsa) 6441 << getOpenMPClauseName(OMPC_linear); 6442 continue; 6443 } 6444 // Each argument can appear in at most one uniform or linear clause. 6445 if (UniformedArgs.count(CanonPVD) > 0) { 6446 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 6447 << getOpenMPClauseName(OMPC_linear) 6448 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange(); 6449 Diag(UniformedArgs[CanonPVD]->getExprLoc(), 6450 diag::note_omp_explicit_dsa) 6451 << getOpenMPClauseName(OMPC_uniform); 6452 continue; 6453 } 6454 LinearArgs[CanonPVD] = E; 6455 if (E->isValueDependent() || E->isTypeDependent() || 6456 E->isInstantiationDependent() || 6457 E->containsUnexpandedParameterPack()) 6458 continue; 6459 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind, 6460 PVD->getOriginalType(), 6461 /*IsDeclareSimd=*/true); 6462 continue; 6463 } 6464 } 6465 if (isa<CXXThisExpr>(E)) { 6466 if (UniformedLinearThis) { 6467 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 6468 << getOpenMPClauseName(OMPC_linear) 6469 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear) 6470 << E->getSourceRange(); 6471 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa) 6472 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform 6473 : OMPC_linear); 6474 continue; 6475 } 6476 UniformedLinearThis = E; 6477 if (E->isValueDependent() || E->isTypeDependent() || 6478 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 6479 continue; 6480 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind, 6481 E->getType(), /*IsDeclareSimd=*/true); 6482 continue; 6483 } 6484 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 6485 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 6486 } 6487 Expr *Step = nullptr; 6488 Expr *NewStep = nullptr; 6489 SmallVector<Expr *, 4> NewSteps; 6490 for (Expr *E : Steps) { 6491 // Skip the same step expression, it was checked already. 6492 if (Step == E || !E) { 6493 NewSteps.push_back(E ? NewStep : nullptr); 6494 continue; 6495 } 6496 Step = E; 6497 if (const auto *DRE = dyn_cast<DeclRefExpr>(Step)) 6498 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 6499 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 6500 if (UniformedArgs.count(CanonPVD) == 0) { 6501 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param) 6502 << Step->getSourceRange(); 6503 } else if (E->isValueDependent() || E->isTypeDependent() || 6504 E->isInstantiationDependent() || 6505 E->containsUnexpandedParameterPack() || 6506 CanonPVD->getType()->hasIntegerRepresentation()) { 6507 NewSteps.push_back(Step); 6508 } else { 6509 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param) 6510 << Step->getSourceRange(); 6511 } 6512 continue; 6513 } 6514 NewStep = Step; 6515 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && 6516 !Step->isInstantiationDependent() && 6517 !Step->containsUnexpandedParameterPack()) { 6518 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step) 6519 .get(); 6520 if (NewStep) 6521 NewStep = 6522 VerifyIntegerConstantExpression(NewStep, /*FIXME*/ AllowFold).get(); 6523 } 6524 NewSteps.push_back(NewStep); 6525 } 6526 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit( 6527 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()), 6528 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(), 6529 const_cast<Expr **>(NewAligns.data()), NewAligns.size(), 6530 const_cast<Expr **>(Linears.data()), Linears.size(), 6531 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(), 6532 NewSteps.data(), NewSteps.size(), SR); 6533 ADecl->addAttr(NewAttr); 6534 return DG; 6535 } 6536 6537 static void setPrototype(Sema &S, FunctionDecl *FD, FunctionDecl *FDWithProto, 6538 QualType NewType) { 6539 assert(NewType->isFunctionProtoType() && 6540 "Expected function type with prototype."); 6541 assert(FD->getType()->isFunctionNoProtoType() && 6542 "Expected function with type with no prototype."); 6543 assert(FDWithProto->getType()->isFunctionProtoType() && 6544 "Expected function with prototype."); 6545 // Synthesize parameters with the same types. 6546 FD->setType(NewType); 6547 SmallVector<ParmVarDecl *, 16> Params; 6548 for (const ParmVarDecl *P : FDWithProto->parameters()) { 6549 auto *Param = ParmVarDecl::Create(S.getASTContext(), FD, SourceLocation(), 6550 SourceLocation(), nullptr, P->getType(), 6551 /*TInfo=*/nullptr, SC_None, nullptr); 6552 Param->setScopeInfo(0, Params.size()); 6553 Param->setImplicit(); 6554 Params.push_back(Param); 6555 } 6556 6557 FD->setParams(Params); 6558 } 6559 6560 void Sema::ActOnFinishedFunctionDefinitionInOpenMPAssumeScope(Decl *D) { 6561 if (D->isInvalidDecl()) 6562 return; 6563 FunctionDecl *FD = nullptr; 6564 if (auto *UTemplDecl = dyn_cast<FunctionTemplateDecl>(D)) 6565 FD = UTemplDecl->getTemplatedDecl(); 6566 else 6567 FD = cast<FunctionDecl>(D); 6568 assert(FD && "Expected a function declaration!"); 6569 6570 // If we are intantiating templates we do *not* apply scoped assumptions but 6571 // only global ones. We apply scoped assumption to the template definition 6572 // though. 6573 if (!inTemplateInstantiation()) { 6574 for (AssumptionAttr *AA : OMPAssumeScoped) 6575 FD->addAttr(AA); 6576 } 6577 for (AssumptionAttr *AA : OMPAssumeGlobal) 6578 FD->addAttr(AA); 6579 } 6580 6581 Sema::OMPDeclareVariantScope::OMPDeclareVariantScope(OMPTraitInfo &TI) 6582 : TI(&TI), NameSuffix(TI.getMangledName()) {} 6583 6584 void Sema::ActOnStartOfFunctionDefinitionInOpenMPDeclareVariantScope( 6585 Scope *S, Declarator &D, MultiTemplateParamsArg TemplateParamLists, 6586 SmallVectorImpl<FunctionDecl *> &Bases) { 6587 if (!D.getIdentifier()) 6588 return; 6589 6590 OMPDeclareVariantScope &DVScope = OMPDeclareVariantScopes.back(); 6591 6592 // Template specialization is an extension, check if we do it. 6593 bool IsTemplated = !TemplateParamLists.empty(); 6594 if (IsTemplated & 6595 !DVScope.TI->isExtensionActive( 6596 llvm::omp::TraitProperty::implementation_extension_allow_templates)) 6597 return; 6598 6599 IdentifierInfo *BaseII = D.getIdentifier(); 6600 LookupResult Lookup(*this, DeclarationName(BaseII), D.getIdentifierLoc(), 6601 LookupOrdinaryName); 6602 LookupParsedName(Lookup, S, &D.getCXXScopeSpec()); 6603 6604 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); 6605 QualType FType = TInfo->getType(); 6606 6607 bool IsConstexpr = 6608 D.getDeclSpec().getConstexprSpecifier() == ConstexprSpecKind::Constexpr; 6609 bool IsConsteval = 6610 D.getDeclSpec().getConstexprSpecifier() == ConstexprSpecKind::Consteval; 6611 6612 for (auto *Candidate : Lookup) { 6613 auto *CandidateDecl = Candidate->getUnderlyingDecl(); 6614 FunctionDecl *UDecl = nullptr; 6615 if (IsTemplated && isa<FunctionTemplateDecl>(CandidateDecl)) 6616 UDecl = cast<FunctionTemplateDecl>(CandidateDecl)->getTemplatedDecl(); 6617 else if (!IsTemplated) 6618 UDecl = dyn_cast<FunctionDecl>(CandidateDecl); 6619 if (!UDecl) 6620 continue; 6621 6622 // Don't specialize constexpr/consteval functions with 6623 // non-constexpr/consteval functions. 6624 if (UDecl->isConstexpr() && !IsConstexpr) 6625 continue; 6626 if (UDecl->isConsteval() && !IsConsteval) 6627 continue; 6628 6629 QualType UDeclTy = UDecl->getType(); 6630 if (!UDeclTy->isDependentType()) { 6631 QualType NewType = Context.mergeFunctionTypes( 6632 FType, UDeclTy, /* OfBlockPointer */ false, 6633 /* Unqualified */ false, /* AllowCXX */ true); 6634 if (NewType.isNull()) 6635 continue; 6636 } 6637 6638 // Found a base! 6639 Bases.push_back(UDecl); 6640 } 6641 6642 bool UseImplicitBase = !DVScope.TI->isExtensionActive( 6643 llvm::omp::TraitProperty::implementation_extension_disable_implicit_base); 6644 // If no base was found we create a declaration that we use as base. 6645 if (Bases.empty() && UseImplicitBase) { 6646 D.setFunctionDefinitionKind(FunctionDefinitionKind::Declaration); 6647 Decl *BaseD = HandleDeclarator(S, D, TemplateParamLists); 6648 BaseD->setImplicit(true); 6649 if (auto *BaseTemplD = dyn_cast<FunctionTemplateDecl>(BaseD)) 6650 Bases.push_back(BaseTemplD->getTemplatedDecl()); 6651 else 6652 Bases.push_back(cast<FunctionDecl>(BaseD)); 6653 } 6654 6655 std::string MangledName; 6656 MangledName += D.getIdentifier()->getName(); 6657 MangledName += getOpenMPVariantManglingSeparatorStr(); 6658 MangledName += DVScope.NameSuffix; 6659 IdentifierInfo &VariantII = Context.Idents.get(MangledName); 6660 6661 VariantII.setMangledOpenMPVariantName(true); 6662 D.SetIdentifier(&VariantII, D.getBeginLoc()); 6663 } 6664 6665 void Sema::ActOnFinishedFunctionDefinitionInOpenMPDeclareVariantScope( 6666 Decl *D, SmallVectorImpl<FunctionDecl *> &Bases) { 6667 // Do not mark function as is used to prevent its emission if this is the 6668 // only place where it is used. 6669 EnterExpressionEvaluationContext Unevaluated( 6670 *this, Sema::ExpressionEvaluationContext::Unevaluated); 6671 6672 FunctionDecl *FD = nullptr; 6673 if (auto *UTemplDecl = dyn_cast<FunctionTemplateDecl>(D)) 6674 FD = UTemplDecl->getTemplatedDecl(); 6675 else 6676 FD = cast<FunctionDecl>(D); 6677 auto *VariantFuncRef = DeclRefExpr::Create( 6678 Context, NestedNameSpecifierLoc(), SourceLocation(), FD, 6679 /* RefersToEnclosingVariableOrCapture */ false, 6680 /* NameLoc */ FD->getLocation(), FD->getType(), ExprValueKind::VK_RValue); 6681 6682 OMPDeclareVariantScope &DVScope = OMPDeclareVariantScopes.back(); 6683 auto *OMPDeclareVariantA = OMPDeclareVariantAttr::CreateImplicit( 6684 Context, VariantFuncRef, DVScope.TI); 6685 for (FunctionDecl *BaseFD : Bases) 6686 BaseFD->addAttr(OMPDeclareVariantA); 6687 } 6688 6689 ExprResult Sema::ActOnOpenMPCall(ExprResult Call, Scope *Scope, 6690 SourceLocation LParenLoc, 6691 MultiExprArg ArgExprs, 6692 SourceLocation RParenLoc, Expr *ExecConfig) { 6693 // The common case is a regular call we do not want to specialize at all. Try 6694 // to make that case fast by bailing early. 6695 CallExpr *CE = dyn_cast<CallExpr>(Call.get()); 6696 if (!CE) 6697 return Call; 6698 6699 FunctionDecl *CalleeFnDecl = CE->getDirectCallee(); 6700 if (!CalleeFnDecl) 6701 return Call; 6702 6703 if (!CalleeFnDecl->hasAttr<OMPDeclareVariantAttr>()) 6704 return Call; 6705 6706 ASTContext &Context = getASTContext(); 6707 std::function<void(StringRef)> DiagUnknownTrait = [this, 6708 CE](StringRef ISATrait) { 6709 // TODO Track the selector locations in a way that is accessible here to 6710 // improve the diagnostic location. 6711 Diag(CE->getBeginLoc(), diag::warn_unknown_declare_variant_isa_trait) 6712 << ISATrait; 6713 }; 6714 TargetOMPContext OMPCtx(Context, std::move(DiagUnknownTrait), 6715 getCurFunctionDecl()); 6716 6717 QualType CalleeFnType = CalleeFnDecl->getType(); 6718 6719 SmallVector<Expr *, 4> Exprs; 6720 SmallVector<VariantMatchInfo, 4> VMIs; 6721 while (CalleeFnDecl) { 6722 for (OMPDeclareVariantAttr *A : 6723 CalleeFnDecl->specific_attrs<OMPDeclareVariantAttr>()) { 6724 Expr *VariantRef = A->getVariantFuncRef(); 6725 6726 VariantMatchInfo VMI; 6727 OMPTraitInfo &TI = A->getTraitInfo(); 6728 TI.getAsVariantMatchInfo(Context, VMI); 6729 if (!isVariantApplicableInContext(VMI, OMPCtx, 6730 /* DeviceSetOnly */ false)) 6731 continue; 6732 6733 VMIs.push_back(VMI); 6734 Exprs.push_back(VariantRef); 6735 } 6736 6737 CalleeFnDecl = CalleeFnDecl->getPreviousDecl(); 6738 } 6739 6740 ExprResult NewCall; 6741 do { 6742 int BestIdx = getBestVariantMatchForContext(VMIs, OMPCtx); 6743 if (BestIdx < 0) 6744 return Call; 6745 Expr *BestExpr = cast<DeclRefExpr>(Exprs[BestIdx]); 6746 Decl *BestDecl = cast<DeclRefExpr>(BestExpr)->getDecl(); 6747 6748 { 6749 // Try to build a (member) call expression for the current best applicable 6750 // variant expression. We allow this to fail in which case we continue 6751 // with the next best variant expression. The fail case is part of the 6752 // implementation defined behavior in the OpenMP standard when it talks 6753 // about what differences in the function prototypes: "Any differences 6754 // that the specific OpenMP context requires in the prototype of the 6755 // variant from the base function prototype are implementation defined." 6756 // This wording is there to allow the specialized variant to have a 6757 // different type than the base function. This is intended and OK but if 6758 // we cannot create a call the difference is not in the "implementation 6759 // defined range" we allow. 6760 Sema::TentativeAnalysisScope Trap(*this); 6761 6762 if (auto *SpecializedMethod = dyn_cast<CXXMethodDecl>(BestDecl)) { 6763 auto *MemberCall = dyn_cast<CXXMemberCallExpr>(CE); 6764 BestExpr = MemberExpr::CreateImplicit( 6765 Context, MemberCall->getImplicitObjectArgument(), 6766 /* IsArrow */ false, SpecializedMethod, Context.BoundMemberTy, 6767 MemberCall->getValueKind(), MemberCall->getObjectKind()); 6768 } 6769 NewCall = BuildCallExpr(Scope, BestExpr, LParenLoc, ArgExprs, RParenLoc, 6770 ExecConfig); 6771 if (NewCall.isUsable()) { 6772 if (CallExpr *NCE = dyn_cast<CallExpr>(NewCall.get())) { 6773 FunctionDecl *NewCalleeFnDecl = NCE->getDirectCallee(); 6774 QualType NewType = Context.mergeFunctionTypes( 6775 CalleeFnType, NewCalleeFnDecl->getType(), 6776 /* OfBlockPointer */ false, 6777 /* Unqualified */ false, /* AllowCXX */ true); 6778 if (!NewType.isNull()) 6779 break; 6780 // Don't use the call if the function type was not compatible. 6781 NewCall = nullptr; 6782 } 6783 } 6784 } 6785 6786 VMIs.erase(VMIs.begin() + BestIdx); 6787 Exprs.erase(Exprs.begin() + BestIdx); 6788 } while (!VMIs.empty()); 6789 6790 if (!NewCall.isUsable()) 6791 return Call; 6792 return PseudoObjectExpr::Create(Context, CE, {NewCall.get()}, 0); 6793 } 6794 6795 Optional<std::pair<FunctionDecl *, Expr *>> 6796 Sema::checkOpenMPDeclareVariantFunction(Sema::DeclGroupPtrTy DG, 6797 Expr *VariantRef, OMPTraitInfo &TI, 6798 SourceRange SR) { 6799 if (!DG || DG.get().isNull()) 6800 return None; 6801 6802 const int VariantId = 1; 6803 // Must be applied only to single decl. 6804 if (!DG.get().isSingleDecl()) { 6805 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd_variant) 6806 << VariantId << SR; 6807 return None; 6808 } 6809 Decl *ADecl = DG.get().getSingleDecl(); 6810 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl)) 6811 ADecl = FTD->getTemplatedDecl(); 6812 6813 // Decl must be a function. 6814 auto *FD = dyn_cast<FunctionDecl>(ADecl); 6815 if (!FD) { 6816 Diag(ADecl->getLocation(), diag::err_omp_function_expected) 6817 << VariantId << SR; 6818 return None; 6819 } 6820 6821 auto &&HasMultiVersionAttributes = [](const FunctionDecl *FD) { 6822 return FD->hasAttrs() && 6823 (FD->hasAttr<CPUDispatchAttr>() || FD->hasAttr<CPUSpecificAttr>() || 6824 FD->hasAttr<TargetAttr>()); 6825 }; 6826 // OpenMP is not compatible with CPU-specific attributes. 6827 if (HasMultiVersionAttributes(FD)) { 6828 Diag(FD->getLocation(), diag::err_omp_declare_variant_incompat_attributes) 6829 << SR; 6830 return None; 6831 } 6832 6833 // Allow #pragma omp declare variant only if the function is not used. 6834 if (FD->isUsed(false)) 6835 Diag(SR.getBegin(), diag::warn_omp_declare_variant_after_used) 6836 << FD->getLocation(); 6837 6838 // Check if the function was emitted already. 6839 const FunctionDecl *Definition; 6840 if (!FD->isThisDeclarationADefinition() && FD->isDefined(Definition) && 6841 (LangOpts.EmitAllDecls || Context.DeclMustBeEmitted(Definition))) 6842 Diag(SR.getBegin(), diag::warn_omp_declare_variant_after_emitted) 6843 << FD->getLocation(); 6844 6845 // The VariantRef must point to function. 6846 if (!VariantRef) { 6847 Diag(SR.getBegin(), diag::err_omp_function_expected) << VariantId; 6848 return None; 6849 } 6850 6851 auto ShouldDelayChecks = [](Expr *&E, bool) { 6852 return E && (E->isTypeDependent() || E->isValueDependent() || 6853 E->containsUnexpandedParameterPack() || 6854 E->isInstantiationDependent()); 6855 }; 6856 // Do not check templates, wait until instantiation. 6857 if (FD->isDependentContext() || ShouldDelayChecks(VariantRef, false) || 6858 TI.anyScoreOrCondition(ShouldDelayChecks)) 6859 return std::make_pair(FD, VariantRef); 6860 6861 // Deal with non-constant score and user condition expressions. 6862 auto HandleNonConstantScoresAndConditions = [this](Expr *&E, 6863 bool IsScore) -> bool { 6864 if (!E || E->isIntegerConstantExpr(Context)) 6865 return false; 6866 6867 if (IsScore) { 6868 // We warn on non-constant scores and pretend they were not present. 6869 Diag(E->getExprLoc(), diag::warn_omp_declare_variant_score_not_constant) 6870 << E; 6871 E = nullptr; 6872 } else { 6873 // We could replace a non-constant user condition with "false" but we 6874 // will soon need to handle these anyway for the dynamic version of 6875 // OpenMP context selectors. 6876 Diag(E->getExprLoc(), 6877 diag::err_omp_declare_variant_user_condition_not_constant) 6878 << E; 6879 } 6880 return true; 6881 }; 6882 if (TI.anyScoreOrCondition(HandleNonConstantScoresAndConditions)) 6883 return None; 6884 6885 // Convert VariantRef expression to the type of the original function to 6886 // resolve possible conflicts. 6887 ExprResult VariantRefCast = VariantRef; 6888 if (LangOpts.CPlusPlus) { 6889 QualType FnPtrType; 6890 auto *Method = dyn_cast<CXXMethodDecl>(FD); 6891 if (Method && !Method->isStatic()) { 6892 const Type *ClassType = 6893 Context.getTypeDeclType(Method->getParent()).getTypePtr(); 6894 FnPtrType = Context.getMemberPointerType(FD->getType(), ClassType); 6895 ExprResult ER; 6896 { 6897 // Build adrr_of unary op to correctly handle type checks for member 6898 // functions. 6899 Sema::TentativeAnalysisScope Trap(*this); 6900 ER = CreateBuiltinUnaryOp(VariantRef->getBeginLoc(), UO_AddrOf, 6901 VariantRef); 6902 } 6903 if (!ER.isUsable()) { 6904 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected) 6905 << VariantId << VariantRef->getSourceRange(); 6906 return None; 6907 } 6908 VariantRef = ER.get(); 6909 } else { 6910 FnPtrType = Context.getPointerType(FD->getType()); 6911 } 6912 QualType VarianPtrType = Context.getPointerType(VariantRef->getType()); 6913 if (VarianPtrType.getUnqualifiedType() != FnPtrType.getUnqualifiedType()) { 6914 ImplicitConversionSequence ICS = TryImplicitConversion( 6915 VariantRef, FnPtrType.getUnqualifiedType(), 6916 /*SuppressUserConversions=*/false, AllowedExplicit::None, 6917 /*InOverloadResolution=*/false, 6918 /*CStyle=*/false, 6919 /*AllowObjCWritebackConversion=*/false); 6920 if (ICS.isFailure()) { 6921 Diag(VariantRef->getExprLoc(), 6922 diag::err_omp_declare_variant_incompat_types) 6923 << VariantRef->getType() 6924 << ((Method && !Method->isStatic()) ? FnPtrType : FD->getType()) 6925 << VariantRef->getSourceRange(); 6926 return None; 6927 } 6928 VariantRefCast = PerformImplicitConversion( 6929 VariantRef, FnPtrType.getUnqualifiedType(), AA_Converting); 6930 if (!VariantRefCast.isUsable()) 6931 return None; 6932 } 6933 // Drop previously built artificial addr_of unary op for member functions. 6934 if (Method && !Method->isStatic()) { 6935 Expr *PossibleAddrOfVariantRef = VariantRefCast.get(); 6936 if (auto *UO = dyn_cast<UnaryOperator>( 6937 PossibleAddrOfVariantRef->IgnoreImplicit())) 6938 VariantRefCast = UO->getSubExpr(); 6939 } 6940 } 6941 6942 ExprResult ER = CheckPlaceholderExpr(VariantRefCast.get()); 6943 if (!ER.isUsable() || 6944 !ER.get()->IgnoreParenImpCasts()->getType()->isFunctionType()) { 6945 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected) 6946 << VariantId << VariantRef->getSourceRange(); 6947 return None; 6948 } 6949 6950 // The VariantRef must point to function. 6951 auto *DRE = dyn_cast<DeclRefExpr>(ER.get()->IgnoreParenImpCasts()); 6952 if (!DRE) { 6953 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected) 6954 << VariantId << VariantRef->getSourceRange(); 6955 return None; 6956 } 6957 auto *NewFD = dyn_cast_or_null<FunctionDecl>(DRE->getDecl()); 6958 if (!NewFD) { 6959 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected) 6960 << VariantId << VariantRef->getSourceRange(); 6961 return None; 6962 } 6963 6964 // Check if function types are compatible in C. 6965 if (!LangOpts.CPlusPlus) { 6966 QualType NewType = 6967 Context.mergeFunctionTypes(FD->getType(), NewFD->getType()); 6968 if (NewType.isNull()) { 6969 Diag(VariantRef->getExprLoc(), 6970 diag::err_omp_declare_variant_incompat_types) 6971 << NewFD->getType() << FD->getType() << VariantRef->getSourceRange(); 6972 return None; 6973 } 6974 if (NewType->isFunctionProtoType()) { 6975 if (FD->getType()->isFunctionNoProtoType()) 6976 setPrototype(*this, FD, NewFD, NewType); 6977 else if (NewFD->getType()->isFunctionNoProtoType()) 6978 setPrototype(*this, NewFD, FD, NewType); 6979 } 6980 } 6981 6982 // Check if variant function is not marked with declare variant directive. 6983 if (NewFD->hasAttrs() && NewFD->hasAttr<OMPDeclareVariantAttr>()) { 6984 Diag(VariantRef->getExprLoc(), 6985 diag::warn_omp_declare_variant_marked_as_declare_variant) 6986 << VariantRef->getSourceRange(); 6987 SourceRange SR = 6988 NewFD->specific_attr_begin<OMPDeclareVariantAttr>()->getRange(); 6989 Diag(SR.getBegin(), diag::note_omp_marked_declare_variant_here) << SR; 6990 return None; 6991 } 6992 6993 enum DoesntSupport { 6994 VirtFuncs = 1, 6995 Constructors = 3, 6996 Destructors = 4, 6997 DeletedFuncs = 5, 6998 DefaultedFuncs = 6, 6999 ConstexprFuncs = 7, 7000 ConstevalFuncs = 8, 7001 }; 7002 if (const auto *CXXFD = dyn_cast<CXXMethodDecl>(FD)) { 7003 if (CXXFD->isVirtual()) { 7004 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 7005 << VirtFuncs; 7006 return None; 7007 } 7008 7009 if (isa<CXXConstructorDecl>(FD)) { 7010 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 7011 << Constructors; 7012 return None; 7013 } 7014 7015 if (isa<CXXDestructorDecl>(FD)) { 7016 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 7017 << Destructors; 7018 return None; 7019 } 7020 } 7021 7022 if (FD->isDeleted()) { 7023 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 7024 << DeletedFuncs; 7025 return None; 7026 } 7027 7028 if (FD->isDefaulted()) { 7029 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 7030 << DefaultedFuncs; 7031 return None; 7032 } 7033 7034 if (FD->isConstexpr()) { 7035 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 7036 << (NewFD->isConsteval() ? ConstevalFuncs : ConstexprFuncs); 7037 return None; 7038 } 7039 7040 // Check general compatibility. 7041 if (areMultiversionVariantFunctionsCompatible( 7042 FD, NewFD, PartialDiagnostic::NullDiagnostic(), 7043 PartialDiagnosticAt(SourceLocation(), 7044 PartialDiagnostic::NullDiagnostic()), 7045 PartialDiagnosticAt( 7046 VariantRef->getExprLoc(), 7047 PDiag(diag::err_omp_declare_variant_doesnt_support)), 7048 PartialDiagnosticAt(VariantRef->getExprLoc(), 7049 PDiag(diag::err_omp_declare_variant_diff) 7050 << FD->getLocation()), 7051 /*TemplatesSupported=*/true, /*ConstexprSupported=*/false, 7052 /*CLinkageMayDiffer=*/true)) 7053 return None; 7054 return std::make_pair(FD, cast<Expr>(DRE)); 7055 } 7056 7057 void Sema::ActOnOpenMPDeclareVariantDirective(FunctionDecl *FD, 7058 Expr *VariantRef, 7059 OMPTraitInfo &TI, 7060 SourceRange SR) { 7061 auto *NewAttr = 7062 OMPDeclareVariantAttr::CreateImplicit(Context, VariantRef, &TI, SR); 7063 FD->addAttr(NewAttr); 7064 } 7065 7066 StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses, 7067 Stmt *AStmt, 7068 SourceLocation StartLoc, 7069 SourceLocation EndLoc) { 7070 if (!AStmt) 7071 return StmtError(); 7072 7073 auto *CS = cast<CapturedStmt>(AStmt); 7074 // 1.2.2 OpenMP Language Terminology 7075 // Structured block - An executable statement with a single entry at the 7076 // top and a single exit at the bottom. 7077 // The point of exit cannot be a branch out of the structured block. 7078 // longjmp() and throw() must not violate the entry/exit criteria. 7079 CS->getCapturedDecl()->setNothrow(); 7080 7081 setFunctionHasBranchProtectedScope(); 7082 7083 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 7084 DSAStack->getTaskgroupReductionRef(), 7085 DSAStack->isCancelRegion()); 7086 } 7087 7088 namespace { 7089 /// Iteration space of a single for loop. 7090 struct LoopIterationSpace final { 7091 /// True if the condition operator is the strict compare operator (<, > or 7092 /// !=). 7093 bool IsStrictCompare = false; 7094 /// Condition of the loop. 7095 Expr *PreCond = nullptr; 7096 /// This expression calculates the number of iterations in the loop. 7097 /// It is always possible to calculate it before starting the loop. 7098 Expr *NumIterations = nullptr; 7099 /// The loop counter variable. 7100 Expr *CounterVar = nullptr; 7101 /// Private loop counter variable. 7102 Expr *PrivateCounterVar = nullptr; 7103 /// This is initializer for the initial value of #CounterVar. 7104 Expr *CounterInit = nullptr; 7105 /// This is step for the #CounterVar used to generate its update: 7106 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration. 7107 Expr *CounterStep = nullptr; 7108 /// Should step be subtracted? 7109 bool Subtract = false; 7110 /// Source range of the loop init. 7111 SourceRange InitSrcRange; 7112 /// Source range of the loop condition. 7113 SourceRange CondSrcRange; 7114 /// Source range of the loop increment. 7115 SourceRange IncSrcRange; 7116 /// Minimum value that can have the loop control variable. Used to support 7117 /// non-rectangular loops. Applied only for LCV with the non-iterator types, 7118 /// since only such variables can be used in non-loop invariant expressions. 7119 Expr *MinValue = nullptr; 7120 /// Maximum value that can have the loop control variable. Used to support 7121 /// non-rectangular loops. Applied only for LCV with the non-iterator type, 7122 /// since only such variables can be used in non-loop invariant expressions. 7123 Expr *MaxValue = nullptr; 7124 /// true, if the lower bound depends on the outer loop control var. 7125 bool IsNonRectangularLB = false; 7126 /// true, if the upper bound depends on the outer loop control var. 7127 bool IsNonRectangularUB = false; 7128 /// Index of the loop this loop depends on and forms non-rectangular loop 7129 /// nest. 7130 unsigned LoopDependentIdx = 0; 7131 /// Final condition for the non-rectangular loop nest support. It is used to 7132 /// check that the number of iterations for this particular counter must be 7133 /// finished. 7134 Expr *FinalCondition = nullptr; 7135 }; 7136 7137 /// Helper class for checking canonical form of the OpenMP loops and 7138 /// extracting iteration space of each loop in the loop nest, that will be used 7139 /// for IR generation. 7140 class OpenMPIterationSpaceChecker { 7141 /// Reference to Sema. 7142 Sema &SemaRef; 7143 /// Does the loop associated directive support non-rectangular loops? 7144 bool SupportsNonRectangular; 7145 /// Data-sharing stack. 7146 DSAStackTy &Stack; 7147 /// A location for diagnostics (when there is no some better location). 7148 SourceLocation DefaultLoc; 7149 /// A location for diagnostics (when increment is not compatible). 7150 SourceLocation ConditionLoc; 7151 /// A source location for referring to loop init later. 7152 SourceRange InitSrcRange; 7153 /// A source location for referring to condition later. 7154 SourceRange ConditionSrcRange; 7155 /// A source location for referring to increment later. 7156 SourceRange IncrementSrcRange; 7157 /// Loop variable. 7158 ValueDecl *LCDecl = nullptr; 7159 /// Reference to loop variable. 7160 Expr *LCRef = nullptr; 7161 /// Lower bound (initializer for the var). 7162 Expr *LB = nullptr; 7163 /// Upper bound. 7164 Expr *UB = nullptr; 7165 /// Loop step (increment). 7166 Expr *Step = nullptr; 7167 /// This flag is true when condition is one of: 7168 /// Var < UB 7169 /// Var <= UB 7170 /// UB > Var 7171 /// UB >= Var 7172 /// This will have no value when the condition is != 7173 llvm::Optional<bool> TestIsLessOp; 7174 /// This flag is true when condition is strict ( < or > ). 7175 bool TestIsStrictOp = false; 7176 /// This flag is true when step is subtracted on each iteration. 7177 bool SubtractStep = false; 7178 /// The outer loop counter this loop depends on (if any). 7179 const ValueDecl *DepDecl = nullptr; 7180 /// Contains number of loop (starts from 1) on which loop counter init 7181 /// expression of this loop depends on. 7182 Optional<unsigned> InitDependOnLC; 7183 /// Contains number of loop (starts from 1) on which loop counter condition 7184 /// expression of this loop depends on. 7185 Optional<unsigned> CondDependOnLC; 7186 /// Checks if the provide statement depends on the loop counter. 7187 Optional<unsigned> doesDependOnLoopCounter(const Stmt *S, bool IsInitializer); 7188 /// Original condition required for checking of the exit condition for 7189 /// non-rectangular loop. 7190 Expr *Condition = nullptr; 7191 7192 public: 7193 OpenMPIterationSpaceChecker(Sema &SemaRef, bool SupportsNonRectangular, 7194 DSAStackTy &Stack, SourceLocation DefaultLoc) 7195 : SemaRef(SemaRef), SupportsNonRectangular(SupportsNonRectangular), 7196 Stack(Stack), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {} 7197 /// Check init-expr for canonical loop form and save loop counter 7198 /// variable - #Var and its initialization value - #LB. 7199 bool checkAndSetInit(Stmt *S, bool EmitDiags = true); 7200 /// Check test-expr for canonical form, save upper-bound (#UB), flags 7201 /// for less/greater and for strict/non-strict comparison. 7202 bool checkAndSetCond(Expr *S); 7203 /// Check incr-expr for canonical loop form and return true if it 7204 /// does not conform, otherwise save loop step (#Step). 7205 bool checkAndSetInc(Expr *S); 7206 /// Return the loop counter variable. 7207 ValueDecl *getLoopDecl() const { return LCDecl; } 7208 /// Return the reference expression to loop counter variable. 7209 Expr *getLoopDeclRefExpr() const { return LCRef; } 7210 /// Source range of the loop init. 7211 SourceRange getInitSrcRange() const { return InitSrcRange; } 7212 /// Source range of the loop condition. 7213 SourceRange getConditionSrcRange() const { return ConditionSrcRange; } 7214 /// Source range of the loop increment. 7215 SourceRange getIncrementSrcRange() const { return IncrementSrcRange; } 7216 /// True if the step should be subtracted. 7217 bool shouldSubtractStep() const { return SubtractStep; } 7218 /// True, if the compare operator is strict (<, > or !=). 7219 bool isStrictTestOp() const { return TestIsStrictOp; } 7220 /// Build the expression to calculate the number of iterations. 7221 Expr *buildNumIterations( 7222 Scope *S, ArrayRef<LoopIterationSpace> ResultIterSpaces, bool LimitedType, 7223 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 7224 /// Build the precondition expression for the loops. 7225 Expr * 7226 buildPreCond(Scope *S, Expr *Cond, 7227 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 7228 /// Build reference expression to the counter be used for codegen. 7229 DeclRefExpr * 7230 buildCounterVar(llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 7231 DSAStackTy &DSA) const; 7232 /// Build reference expression to the private counter be used for 7233 /// codegen. 7234 Expr *buildPrivateCounterVar() const; 7235 /// Build initialization of the counter be used for codegen. 7236 Expr *buildCounterInit() const; 7237 /// Build step of the counter be used for codegen. 7238 Expr *buildCounterStep() const; 7239 /// Build loop data with counter value for depend clauses in ordered 7240 /// directives. 7241 Expr * 7242 buildOrderedLoopData(Scope *S, Expr *Counter, 7243 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 7244 SourceLocation Loc, Expr *Inc = nullptr, 7245 OverloadedOperatorKind OOK = OO_Amp); 7246 /// Builds the minimum value for the loop counter. 7247 std::pair<Expr *, Expr *> buildMinMaxValues( 7248 Scope *S, llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 7249 /// Builds final condition for the non-rectangular loops. 7250 Expr *buildFinalCondition(Scope *S) const; 7251 /// Return true if any expression is dependent. 7252 bool dependent() const; 7253 /// Returns true if the initializer forms non-rectangular loop. 7254 bool doesInitDependOnLC() const { return InitDependOnLC.hasValue(); } 7255 /// Returns true if the condition forms non-rectangular loop. 7256 bool doesCondDependOnLC() const { return CondDependOnLC.hasValue(); } 7257 /// Returns index of the loop we depend on (starting from 1), or 0 otherwise. 7258 unsigned getLoopDependentIdx() const { 7259 return InitDependOnLC.getValueOr(CondDependOnLC.getValueOr(0)); 7260 } 7261 7262 private: 7263 /// Check the right-hand side of an assignment in the increment 7264 /// expression. 7265 bool checkAndSetIncRHS(Expr *RHS); 7266 /// Helper to set loop counter variable and its initializer. 7267 bool setLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB, 7268 bool EmitDiags); 7269 /// Helper to set upper bound. 7270 bool setUB(Expr *NewUB, llvm::Optional<bool> LessOp, bool StrictOp, 7271 SourceRange SR, SourceLocation SL); 7272 /// Helper to set loop increment. 7273 bool setStep(Expr *NewStep, bool Subtract); 7274 }; 7275 7276 bool OpenMPIterationSpaceChecker::dependent() const { 7277 if (!LCDecl) { 7278 assert(!LB && !UB && !Step); 7279 return false; 7280 } 7281 return LCDecl->getType()->isDependentType() || 7282 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) || 7283 (Step && Step->isValueDependent()); 7284 } 7285 7286 bool OpenMPIterationSpaceChecker::setLCDeclAndLB(ValueDecl *NewLCDecl, 7287 Expr *NewLCRefExpr, 7288 Expr *NewLB, bool EmitDiags) { 7289 // State consistency checking to ensure correct usage. 7290 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr && 7291 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp); 7292 if (!NewLCDecl || !NewLB) 7293 return true; 7294 LCDecl = getCanonicalDecl(NewLCDecl); 7295 LCRef = NewLCRefExpr; 7296 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB)) 7297 if (const CXXConstructorDecl *Ctor = CE->getConstructor()) 7298 if ((Ctor->isCopyOrMoveConstructor() || 7299 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && 7300 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) 7301 NewLB = CE->getArg(0)->IgnoreParenImpCasts(); 7302 LB = NewLB; 7303 if (EmitDiags) 7304 InitDependOnLC = doesDependOnLoopCounter(LB, /*IsInitializer=*/true); 7305 return false; 7306 } 7307 7308 bool OpenMPIterationSpaceChecker::setUB(Expr *NewUB, 7309 llvm::Optional<bool> LessOp, 7310 bool StrictOp, SourceRange SR, 7311 SourceLocation SL) { 7312 // State consistency checking to ensure correct usage. 7313 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr && 7314 Step == nullptr && !TestIsLessOp && !TestIsStrictOp); 7315 if (!NewUB) 7316 return true; 7317 UB = NewUB; 7318 if (LessOp) 7319 TestIsLessOp = LessOp; 7320 TestIsStrictOp = StrictOp; 7321 ConditionSrcRange = SR; 7322 ConditionLoc = SL; 7323 CondDependOnLC = doesDependOnLoopCounter(UB, /*IsInitializer=*/false); 7324 return false; 7325 } 7326 7327 bool OpenMPIterationSpaceChecker::setStep(Expr *NewStep, bool Subtract) { 7328 // State consistency checking to ensure correct usage. 7329 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr); 7330 if (!NewStep) 7331 return true; 7332 if (!NewStep->isValueDependent()) { 7333 // Check that the step is integer expression. 7334 SourceLocation StepLoc = NewStep->getBeginLoc(); 7335 ExprResult Val = SemaRef.PerformOpenMPImplicitIntegerConversion( 7336 StepLoc, getExprAsWritten(NewStep)); 7337 if (Val.isInvalid()) 7338 return true; 7339 NewStep = Val.get(); 7340 7341 // OpenMP [2.6, Canonical Loop Form, Restrictions] 7342 // If test-expr is of form var relational-op b and relational-op is < or 7343 // <= then incr-expr must cause var to increase on each iteration of the 7344 // loop. If test-expr is of form var relational-op b and relational-op is 7345 // > or >= then incr-expr must cause var to decrease on each iteration of 7346 // the loop. 7347 // If test-expr is of form b relational-op var and relational-op is < or 7348 // <= then incr-expr must cause var to decrease on each iteration of the 7349 // loop. If test-expr is of form b relational-op var and relational-op is 7350 // > or >= then incr-expr must cause var to increase on each iteration of 7351 // the loop. 7352 Optional<llvm::APSInt> Result = 7353 NewStep->getIntegerConstantExpr(SemaRef.Context); 7354 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation(); 7355 bool IsConstNeg = 7356 Result && Result->isSigned() && (Subtract != Result->isNegative()); 7357 bool IsConstPos = 7358 Result && Result->isSigned() && (Subtract == Result->isNegative()); 7359 bool IsConstZero = Result && !Result->getBoolValue(); 7360 7361 // != with increment is treated as <; != with decrement is treated as > 7362 if (!TestIsLessOp.hasValue()) 7363 TestIsLessOp = IsConstPos || (IsUnsigned && !Subtract); 7364 if (UB && (IsConstZero || 7365 (TestIsLessOp.getValue() ? 7366 (IsConstNeg || (IsUnsigned && Subtract)) : 7367 (IsConstPos || (IsUnsigned && !Subtract))))) { 7368 SemaRef.Diag(NewStep->getExprLoc(), 7369 diag::err_omp_loop_incr_not_compatible) 7370 << LCDecl << TestIsLessOp.getValue() << NewStep->getSourceRange(); 7371 SemaRef.Diag(ConditionLoc, 7372 diag::note_omp_loop_cond_requres_compatible_incr) 7373 << TestIsLessOp.getValue() << ConditionSrcRange; 7374 return true; 7375 } 7376 if (TestIsLessOp.getValue() == Subtract) { 7377 NewStep = 7378 SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep) 7379 .get(); 7380 Subtract = !Subtract; 7381 } 7382 } 7383 7384 Step = NewStep; 7385 SubtractStep = Subtract; 7386 return false; 7387 } 7388 7389 namespace { 7390 /// Checker for the non-rectangular loops. Checks if the initializer or 7391 /// condition expression references loop counter variable. 7392 class LoopCounterRefChecker final 7393 : public ConstStmtVisitor<LoopCounterRefChecker, bool> { 7394 Sema &SemaRef; 7395 DSAStackTy &Stack; 7396 const ValueDecl *CurLCDecl = nullptr; 7397 const ValueDecl *DepDecl = nullptr; 7398 const ValueDecl *PrevDepDecl = nullptr; 7399 bool IsInitializer = true; 7400 bool SupportsNonRectangular; 7401 unsigned BaseLoopId = 0; 7402 bool checkDecl(const Expr *E, const ValueDecl *VD) { 7403 if (getCanonicalDecl(VD) == getCanonicalDecl(CurLCDecl)) { 7404 SemaRef.Diag(E->getExprLoc(), diag::err_omp_stmt_depends_on_loop_counter) 7405 << (IsInitializer ? 0 : 1); 7406 return false; 7407 } 7408 const auto &&Data = Stack.isLoopControlVariable(VD); 7409 // OpenMP, 2.9.1 Canonical Loop Form, Restrictions. 7410 // The type of the loop iterator on which we depend may not have a random 7411 // access iterator type. 7412 if (Data.first && VD->getType()->isRecordType()) { 7413 SmallString<128> Name; 7414 llvm::raw_svector_ostream OS(Name); 7415 VD->getNameForDiagnostic(OS, SemaRef.getPrintingPolicy(), 7416 /*Qualified=*/true); 7417 SemaRef.Diag(E->getExprLoc(), 7418 diag::err_omp_wrong_dependency_iterator_type) 7419 << OS.str(); 7420 SemaRef.Diag(VD->getLocation(), diag::note_previous_decl) << VD; 7421 return false; 7422 } 7423 if (Data.first && !SupportsNonRectangular) { 7424 SemaRef.Diag(E->getExprLoc(), diag::err_omp_invariant_dependency); 7425 return false; 7426 } 7427 if (Data.first && 7428 (DepDecl || (PrevDepDecl && 7429 getCanonicalDecl(VD) != getCanonicalDecl(PrevDepDecl)))) { 7430 if (!DepDecl && PrevDepDecl) 7431 DepDecl = PrevDepDecl; 7432 SmallString<128> Name; 7433 llvm::raw_svector_ostream OS(Name); 7434 DepDecl->getNameForDiagnostic(OS, SemaRef.getPrintingPolicy(), 7435 /*Qualified=*/true); 7436 SemaRef.Diag(E->getExprLoc(), 7437 diag::err_omp_invariant_or_linear_dependency) 7438 << OS.str(); 7439 return false; 7440 } 7441 if (Data.first) { 7442 DepDecl = VD; 7443 BaseLoopId = Data.first; 7444 } 7445 return Data.first; 7446 } 7447 7448 public: 7449 bool VisitDeclRefExpr(const DeclRefExpr *E) { 7450 const ValueDecl *VD = E->getDecl(); 7451 if (isa<VarDecl>(VD)) 7452 return checkDecl(E, VD); 7453 return false; 7454 } 7455 bool VisitMemberExpr(const MemberExpr *E) { 7456 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) { 7457 const ValueDecl *VD = E->getMemberDecl(); 7458 if (isa<VarDecl>(VD) || isa<FieldDecl>(VD)) 7459 return checkDecl(E, VD); 7460 } 7461 return false; 7462 } 7463 bool VisitStmt(const Stmt *S) { 7464 bool Res = false; 7465 for (const Stmt *Child : S->children()) 7466 Res = (Child && Visit(Child)) || Res; 7467 return Res; 7468 } 7469 explicit LoopCounterRefChecker(Sema &SemaRef, DSAStackTy &Stack, 7470 const ValueDecl *CurLCDecl, bool IsInitializer, 7471 const ValueDecl *PrevDepDecl = nullptr, 7472 bool SupportsNonRectangular = true) 7473 : SemaRef(SemaRef), Stack(Stack), CurLCDecl(CurLCDecl), 7474 PrevDepDecl(PrevDepDecl), IsInitializer(IsInitializer), 7475 SupportsNonRectangular(SupportsNonRectangular) {} 7476 unsigned getBaseLoopId() const { 7477 assert(CurLCDecl && "Expected loop dependency."); 7478 return BaseLoopId; 7479 } 7480 const ValueDecl *getDepDecl() const { 7481 assert(CurLCDecl && "Expected loop dependency."); 7482 return DepDecl; 7483 } 7484 }; 7485 } // namespace 7486 7487 Optional<unsigned> 7488 OpenMPIterationSpaceChecker::doesDependOnLoopCounter(const Stmt *S, 7489 bool IsInitializer) { 7490 // Check for the non-rectangular loops. 7491 LoopCounterRefChecker LoopStmtChecker(SemaRef, Stack, LCDecl, IsInitializer, 7492 DepDecl, SupportsNonRectangular); 7493 if (LoopStmtChecker.Visit(S)) { 7494 DepDecl = LoopStmtChecker.getDepDecl(); 7495 return LoopStmtChecker.getBaseLoopId(); 7496 } 7497 return llvm::None; 7498 } 7499 7500 bool OpenMPIterationSpaceChecker::checkAndSetInit(Stmt *S, bool EmitDiags) { 7501 // Check init-expr for canonical loop form and save loop counter 7502 // variable - #Var and its initialization value - #LB. 7503 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following: 7504 // var = lb 7505 // integer-type var = lb 7506 // random-access-iterator-type var = lb 7507 // pointer-type var = lb 7508 // 7509 if (!S) { 7510 if (EmitDiags) { 7511 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init); 7512 } 7513 return true; 7514 } 7515 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S)) 7516 if (!ExprTemp->cleanupsHaveSideEffects()) 7517 S = ExprTemp->getSubExpr(); 7518 7519 InitSrcRange = S->getSourceRange(); 7520 if (Expr *E = dyn_cast<Expr>(S)) 7521 S = E->IgnoreParens(); 7522 if (auto *BO = dyn_cast<BinaryOperator>(S)) { 7523 if (BO->getOpcode() == BO_Assign) { 7524 Expr *LHS = BO->getLHS()->IgnoreParens(); 7525 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) { 7526 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) 7527 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 7528 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 7529 EmitDiags); 7530 return setLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS(), EmitDiags); 7531 } 7532 if (auto *ME = dyn_cast<MemberExpr>(LHS)) { 7533 if (ME->isArrow() && 7534 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 7535 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 7536 EmitDiags); 7537 } 7538 } 7539 } else if (auto *DS = dyn_cast<DeclStmt>(S)) { 7540 if (DS->isSingleDecl()) { 7541 if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) { 7542 if (Var->hasInit() && !Var->getType()->isReferenceType()) { 7543 // Accept non-canonical init form here but emit ext. warning. 7544 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags) 7545 SemaRef.Diag(S->getBeginLoc(), 7546 diag::ext_omp_loop_not_canonical_init) 7547 << S->getSourceRange(); 7548 return setLCDeclAndLB( 7549 Var, 7550 buildDeclRefExpr(SemaRef, Var, 7551 Var->getType().getNonReferenceType(), 7552 DS->getBeginLoc()), 7553 Var->getInit(), EmitDiags); 7554 } 7555 } 7556 } 7557 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 7558 if (CE->getOperator() == OO_Equal) { 7559 Expr *LHS = CE->getArg(0); 7560 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) { 7561 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) 7562 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 7563 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 7564 EmitDiags); 7565 return setLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1), EmitDiags); 7566 } 7567 if (auto *ME = dyn_cast<MemberExpr>(LHS)) { 7568 if (ME->isArrow() && 7569 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 7570 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 7571 EmitDiags); 7572 } 7573 } 7574 } 7575 7576 if (dependent() || SemaRef.CurContext->isDependentContext()) 7577 return false; 7578 if (EmitDiags) { 7579 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_init) 7580 << S->getSourceRange(); 7581 } 7582 return true; 7583 } 7584 7585 /// Ignore parenthesizes, implicit casts, copy constructor and return the 7586 /// variable (which may be the loop variable) if possible. 7587 static const ValueDecl *getInitLCDecl(const Expr *E) { 7588 if (!E) 7589 return nullptr; 7590 E = getExprAsWritten(E); 7591 if (const auto *CE = dyn_cast_or_null<CXXConstructExpr>(E)) 7592 if (const CXXConstructorDecl *Ctor = CE->getConstructor()) 7593 if ((Ctor->isCopyOrMoveConstructor() || 7594 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && 7595 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) 7596 E = CE->getArg(0)->IgnoreParenImpCasts(); 7597 if (const auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) { 7598 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) 7599 return getCanonicalDecl(VD); 7600 } 7601 if (const auto *ME = dyn_cast_or_null<MemberExpr>(E)) 7602 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 7603 return getCanonicalDecl(ME->getMemberDecl()); 7604 return nullptr; 7605 } 7606 7607 bool OpenMPIterationSpaceChecker::checkAndSetCond(Expr *S) { 7608 // Check test-expr for canonical form, save upper-bound UB, flags for 7609 // less/greater and for strict/non-strict comparison. 7610 // OpenMP [2.9] Canonical loop form. Test-expr may be one of the following: 7611 // var relational-op b 7612 // b relational-op var 7613 // 7614 bool IneqCondIsCanonical = SemaRef.getLangOpts().OpenMP >= 50; 7615 if (!S) { 7616 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) 7617 << (IneqCondIsCanonical ? 1 : 0) << LCDecl; 7618 return true; 7619 } 7620 Condition = S; 7621 S = getExprAsWritten(S); 7622 SourceLocation CondLoc = S->getBeginLoc(); 7623 if (auto *BO = dyn_cast<BinaryOperator>(S)) { 7624 if (BO->isRelationalOp()) { 7625 if (getInitLCDecl(BO->getLHS()) == LCDecl) 7626 return setUB(BO->getRHS(), 7627 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE), 7628 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT), 7629 BO->getSourceRange(), BO->getOperatorLoc()); 7630 if (getInitLCDecl(BO->getRHS()) == LCDecl) 7631 return setUB(BO->getLHS(), 7632 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE), 7633 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT), 7634 BO->getSourceRange(), BO->getOperatorLoc()); 7635 } else if (IneqCondIsCanonical && BO->getOpcode() == BO_NE) 7636 return setUB( 7637 getInitLCDecl(BO->getLHS()) == LCDecl ? BO->getRHS() : BO->getLHS(), 7638 /*LessOp=*/llvm::None, 7639 /*StrictOp=*/true, BO->getSourceRange(), BO->getOperatorLoc()); 7640 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 7641 if (CE->getNumArgs() == 2) { 7642 auto Op = CE->getOperator(); 7643 switch (Op) { 7644 case OO_Greater: 7645 case OO_GreaterEqual: 7646 case OO_Less: 7647 case OO_LessEqual: 7648 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 7649 return setUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual, 7650 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(), 7651 CE->getOperatorLoc()); 7652 if (getInitLCDecl(CE->getArg(1)) == LCDecl) 7653 return setUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual, 7654 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(), 7655 CE->getOperatorLoc()); 7656 break; 7657 case OO_ExclaimEqual: 7658 if (IneqCondIsCanonical) 7659 return setUB(getInitLCDecl(CE->getArg(0)) == LCDecl ? CE->getArg(1) 7660 : CE->getArg(0), 7661 /*LessOp=*/llvm::None, 7662 /*StrictOp=*/true, CE->getSourceRange(), 7663 CE->getOperatorLoc()); 7664 break; 7665 default: 7666 break; 7667 } 7668 } 7669 } 7670 if (dependent() || SemaRef.CurContext->isDependentContext()) 7671 return false; 7672 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond) 7673 << (IneqCondIsCanonical ? 1 : 0) << S->getSourceRange() << LCDecl; 7674 return true; 7675 } 7676 7677 bool OpenMPIterationSpaceChecker::checkAndSetIncRHS(Expr *RHS) { 7678 // RHS of canonical loop form increment can be: 7679 // var + incr 7680 // incr + var 7681 // var - incr 7682 // 7683 RHS = RHS->IgnoreParenImpCasts(); 7684 if (auto *BO = dyn_cast<BinaryOperator>(RHS)) { 7685 if (BO->isAdditiveOp()) { 7686 bool IsAdd = BO->getOpcode() == BO_Add; 7687 if (getInitLCDecl(BO->getLHS()) == LCDecl) 7688 return setStep(BO->getRHS(), !IsAdd); 7689 if (IsAdd && getInitLCDecl(BO->getRHS()) == LCDecl) 7690 return setStep(BO->getLHS(), /*Subtract=*/false); 7691 } 7692 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) { 7693 bool IsAdd = CE->getOperator() == OO_Plus; 7694 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) { 7695 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 7696 return setStep(CE->getArg(1), !IsAdd); 7697 if (IsAdd && getInitLCDecl(CE->getArg(1)) == LCDecl) 7698 return setStep(CE->getArg(0), /*Subtract=*/false); 7699 } 7700 } 7701 if (dependent() || SemaRef.CurContext->isDependentContext()) 7702 return false; 7703 SemaRef.Diag(RHS->getBeginLoc(), diag::err_omp_loop_not_canonical_incr) 7704 << RHS->getSourceRange() << LCDecl; 7705 return true; 7706 } 7707 7708 bool OpenMPIterationSpaceChecker::checkAndSetInc(Expr *S) { 7709 // Check incr-expr for canonical loop form and return true if it 7710 // does not conform. 7711 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following: 7712 // ++var 7713 // var++ 7714 // --var 7715 // var-- 7716 // var += incr 7717 // var -= incr 7718 // var = var + incr 7719 // var = incr + var 7720 // var = var - incr 7721 // 7722 if (!S) { 7723 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl; 7724 return true; 7725 } 7726 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S)) 7727 if (!ExprTemp->cleanupsHaveSideEffects()) 7728 S = ExprTemp->getSubExpr(); 7729 7730 IncrementSrcRange = S->getSourceRange(); 7731 S = S->IgnoreParens(); 7732 if (auto *UO = dyn_cast<UnaryOperator>(S)) { 7733 if (UO->isIncrementDecrementOp() && 7734 getInitLCDecl(UO->getSubExpr()) == LCDecl) 7735 return setStep(SemaRef 7736 .ActOnIntegerConstant(UO->getBeginLoc(), 7737 (UO->isDecrementOp() ? -1 : 1)) 7738 .get(), 7739 /*Subtract=*/false); 7740 } else if (auto *BO = dyn_cast<BinaryOperator>(S)) { 7741 switch (BO->getOpcode()) { 7742 case BO_AddAssign: 7743 case BO_SubAssign: 7744 if (getInitLCDecl(BO->getLHS()) == LCDecl) 7745 return setStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign); 7746 break; 7747 case BO_Assign: 7748 if (getInitLCDecl(BO->getLHS()) == LCDecl) 7749 return checkAndSetIncRHS(BO->getRHS()); 7750 break; 7751 default: 7752 break; 7753 } 7754 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 7755 switch (CE->getOperator()) { 7756 case OO_PlusPlus: 7757 case OO_MinusMinus: 7758 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 7759 return setStep(SemaRef 7760 .ActOnIntegerConstant( 7761 CE->getBeginLoc(), 7762 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)) 7763 .get(), 7764 /*Subtract=*/false); 7765 break; 7766 case OO_PlusEqual: 7767 case OO_MinusEqual: 7768 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 7769 return setStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual); 7770 break; 7771 case OO_Equal: 7772 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 7773 return checkAndSetIncRHS(CE->getArg(1)); 7774 break; 7775 default: 7776 break; 7777 } 7778 } 7779 if (dependent() || SemaRef.CurContext->isDependentContext()) 7780 return false; 7781 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_incr) 7782 << S->getSourceRange() << LCDecl; 7783 return true; 7784 } 7785 7786 static ExprResult 7787 tryBuildCapture(Sema &SemaRef, Expr *Capture, 7788 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 7789 if (SemaRef.CurContext->isDependentContext() || Capture->containsErrors()) 7790 return Capture; 7791 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects)) 7792 return SemaRef.PerformImplicitConversion( 7793 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting, 7794 /*AllowExplicit=*/true); 7795 auto I = Captures.find(Capture); 7796 if (I != Captures.end()) 7797 return buildCapture(SemaRef, Capture, I->second); 7798 DeclRefExpr *Ref = nullptr; 7799 ExprResult Res = buildCapture(SemaRef, Capture, Ref); 7800 Captures[Capture] = Ref; 7801 return Res; 7802 } 7803 7804 /// Calculate number of iterations, transforming to unsigned, if number of 7805 /// iterations may be larger than the original type. 7806 static Expr * 7807 calculateNumIters(Sema &SemaRef, Scope *S, SourceLocation DefaultLoc, 7808 Expr *Lower, Expr *Upper, Expr *Step, QualType LCTy, 7809 bool TestIsStrictOp, bool RoundToStep, 7810 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 7811 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures); 7812 if (!NewStep.isUsable()) 7813 return nullptr; 7814 llvm::APSInt LRes, SRes; 7815 bool IsLowerConst = false, IsStepConst = false; 7816 if (Optional<llvm::APSInt> Res = Lower->getIntegerConstantExpr(SemaRef.Context)) { 7817 LRes = *Res; 7818 IsLowerConst = true; 7819 } 7820 if (Optional<llvm::APSInt> Res = Step->getIntegerConstantExpr(SemaRef.Context)) { 7821 SRes = *Res; 7822 IsStepConst = true; 7823 } 7824 bool NoNeedToConvert = IsLowerConst && !RoundToStep && 7825 ((!TestIsStrictOp && LRes.isNonNegative()) || 7826 (TestIsStrictOp && LRes.isStrictlyPositive())); 7827 bool NeedToReorganize = false; 7828 // Check if any subexpressions in Lower -Step [+ 1] lead to overflow. 7829 if (!NoNeedToConvert && IsLowerConst && 7830 (TestIsStrictOp || (RoundToStep && IsStepConst))) { 7831 NoNeedToConvert = true; 7832 if (RoundToStep) { 7833 unsigned BW = LRes.getBitWidth() > SRes.getBitWidth() 7834 ? LRes.getBitWidth() 7835 : SRes.getBitWidth(); 7836 LRes = LRes.extend(BW + 1); 7837 LRes.setIsSigned(true); 7838 SRes = SRes.extend(BW + 1); 7839 SRes.setIsSigned(true); 7840 LRes -= SRes; 7841 NoNeedToConvert = LRes.trunc(BW).extend(BW + 1) == LRes; 7842 LRes = LRes.trunc(BW); 7843 } 7844 if (TestIsStrictOp) { 7845 unsigned BW = LRes.getBitWidth(); 7846 LRes = LRes.extend(BW + 1); 7847 LRes.setIsSigned(true); 7848 ++LRes; 7849 NoNeedToConvert = 7850 NoNeedToConvert && LRes.trunc(BW).extend(BW + 1) == LRes; 7851 // truncate to the original bitwidth. 7852 LRes = LRes.trunc(BW); 7853 } 7854 NeedToReorganize = NoNeedToConvert; 7855 } 7856 llvm::APSInt URes; 7857 bool IsUpperConst = false; 7858 if (Optional<llvm::APSInt> Res = Upper->getIntegerConstantExpr(SemaRef.Context)) { 7859 URes = *Res; 7860 IsUpperConst = true; 7861 } 7862 if (NoNeedToConvert && IsLowerConst && IsUpperConst && 7863 (!RoundToStep || IsStepConst)) { 7864 unsigned BW = LRes.getBitWidth() > URes.getBitWidth() ? LRes.getBitWidth() 7865 : URes.getBitWidth(); 7866 LRes = LRes.extend(BW + 1); 7867 LRes.setIsSigned(true); 7868 URes = URes.extend(BW + 1); 7869 URes.setIsSigned(true); 7870 URes -= LRes; 7871 NoNeedToConvert = URes.trunc(BW).extend(BW + 1) == URes; 7872 NeedToReorganize = NoNeedToConvert; 7873 } 7874 // If the boundaries are not constant or (Lower - Step [+ 1]) is not constant 7875 // or less than zero (Upper - (Lower - Step [+ 1]) may overflow) - promote to 7876 // unsigned. 7877 if ((!NoNeedToConvert || (LRes.isNegative() && !IsUpperConst)) && 7878 !LCTy->isDependentType() && LCTy->isIntegerType()) { 7879 QualType LowerTy = Lower->getType(); 7880 QualType UpperTy = Upper->getType(); 7881 uint64_t LowerSize = SemaRef.Context.getTypeSize(LowerTy); 7882 uint64_t UpperSize = SemaRef.Context.getTypeSize(UpperTy); 7883 if ((LowerSize <= UpperSize && UpperTy->hasSignedIntegerRepresentation()) || 7884 (LowerSize > UpperSize && LowerTy->hasSignedIntegerRepresentation())) { 7885 QualType CastType = SemaRef.Context.getIntTypeForBitwidth( 7886 LowerSize > UpperSize ? LowerSize : UpperSize, /*Signed=*/0); 7887 Upper = 7888 SemaRef 7889 .PerformImplicitConversion( 7890 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Upper).get(), 7891 CastType, Sema::AA_Converting) 7892 .get(); 7893 Lower = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Lower).get(); 7894 NewStep = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, NewStep.get()); 7895 } 7896 } 7897 if (!Lower || !Upper || NewStep.isInvalid()) 7898 return nullptr; 7899 7900 ExprResult Diff; 7901 // If need to reorganize, then calculate the form as Upper - (Lower - Step [+ 7902 // 1]). 7903 if (NeedToReorganize) { 7904 Diff = Lower; 7905 7906 if (RoundToStep) { 7907 // Lower - Step 7908 Diff = 7909 SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Diff.get(), NewStep.get()); 7910 if (!Diff.isUsable()) 7911 return nullptr; 7912 } 7913 7914 // Lower - Step [+ 1] 7915 if (TestIsStrictOp) 7916 Diff = SemaRef.BuildBinOp( 7917 S, DefaultLoc, BO_Add, Diff.get(), 7918 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 7919 if (!Diff.isUsable()) 7920 return nullptr; 7921 7922 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 7923 if (!Diff.isUsable()) 7924 return nullptr; 7925 7926 // Upper - (Lower - Step [+ 1]). 7927 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Diff.get()); 7928 if (!Diff.isUsable()) 7929 return nullptr; 7930 } else { 7931 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower); 7932 7933 if (!Diff.isUsable() && LCTy->getAsCXXRecordDecl()) { 7934 // BuildBinOp already emitted error, this one is to point user to upper 7935 // and lower bound, and to tell what is passed to 'operator-'. 7936 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx) 7937 << Upper->getSourceRange() << Lower->getSourceRange(); 7938 return nullptr; 7939 } 7940 7941 if (!Diff.isUsable()) 7942 return nullptr; 7943 7944 // Upper - Lower [- 1] 7945 if (TestIsStrictOp) 7946 Diff = SemaRef.BuildBinOp( 7947 S, DefaultLoc, BO_Sub, Diff.get(), 7948 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 7949 if (!Diff.isUsable()) 7950 return nullptr; 7951 7952 if (RoundToStep) { 7953 // Upper - Lower [- 1] + Step 7954 Diff = 7955 SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get()); 7956 if (!Diff.isUsable()) 7957 return nullptr; 7958 } 7959 } 7960 7961 // Parentheses (for dumping/debugging purposes only). 7962 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 7963 if (!Diff.isUsable()) 7964 return nullptr; 7965 7966 // (Upper - Lower [- 1] + Step) / Step or (Upper - Lower) / Step 7967 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get()); 7968 if (!Diff.isUsable()) 7969 return nullptr; 7970 7971 return Diff.get(); 7972 } 7973 7974 /// Build the expression to calculate the number of iterations. 7975 Expr *OpenMPIterationSpaceChecker::buildNumIterations( 7976 Scope *S, ArrayRef<LoopIterationSpace> ResultIterSpaces, bool LimitedType, 7977 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 7978 QualType VarType = LCDecl->getType().getNonReferenceType(); 7979 if (!VarType->isIntegerType() && !VarType->isPointerType() && 7980 !SemaRef.getLangOpts().CPlusPlus) 7981 return nullptr; 7982 Expr *LBVal = LB; 7983 Expr *UBVal = UB; 7984 // LB = TestIsLessOp.getValue() ? min(LB(MinVal), LB(MaxVal)) : 7985 // max(LB(MinVal), LB(MaxVal)) 7986 if (InitDependOnLC) { 7987 const LoopIterationSpace &IS = ResultIterSpaces[*InitDependOnLC - 1]; 7988 if (!IS.MinValue || !IS.MaxValue) 7989 return nullptr; 7990 // OuterVar = Min 7991 ExprResult MinValue = 7992 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MinValue); 7993 if (!MinValue.isUsable()) 7994 return nullptr; 7995 7996 ExprResult LBMinVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign, 7997 IS.CounterVar, MinValue.get()); 7998 if (!LBMinVal.isUsable()) 7999 return nullptr; 8000 // OuterVar = Min, LBVal 8001 LBMinVal = 8002 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, LBMinVal.get(), LBVal); 8003 if (!LBMinVal.isUsable()) 8004 return nullptr; 8005 // (OuterVar = Min, LBVal) 8006 LBMinVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, LBMinVal.get()); 8007 if (!LBMinVal.isUsable()) 8008 return nullptr; 8009 8010 // OuterVar = Max 8011 ExprResult MaxValue = 8012 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MaxValue); 8013 if (!MaxValue.isUsable()) 8014 return nullptr; 8015 8016 ExprResult LBMaxVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign, 8017 IS.CounterVar, MaxValue.get()); 8018 if (!LBMaxVal.isUsable()) 8019 return nullptr; 8020 // OuterVar = Max, LBVal 8021 LBMaxVal = 8022 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, LBMaxVal.get(), LBVal); 8023 if (!LBMaxVal.isUsable()) 8024 return nullptr; 8025 // (OuterVar = Max, LBVal) 8026 LBMaxVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, LBMaxVal.get()); 8027 if (!LBMaxVal.isUsable()) 8028 return nullptr; 8029 8030 Expr *LBMin = tryBuildCapture(SemaRef, LBMinVal.get(), Captures).get(); 8031 Expr *LBMax = tryBuildCapture(SemaRef, LBMaxVal.get(), Captures).get(); 8032 if (!LBMin || !LBMax) 8033 return nullptr; 8034 // LB(MinVal) < LB(MaxVal) 8035 ExprResult MinLessMaxRes = 8036 SemaRef.BuildBinOp(S, DefaultLoc, BO_LT, LBMin, LBMax); 8037 if (!MinLessMaxRes.isUsable()) 8038 return nullptr; 8039 Expr *MinLessMax = 8040 tryBuildCapture(SemaRef, MinLessMaxRes.get(), Captures).get(); 8041 if (!MinLessMax) 8042 return nullptr; 8043 if (TestIsLessOp.getValue()) { 8044 // LB(MinVal) < LB(MaxVal) ? LB(MinVal) : LB(MaxVal) - min(LB(MinVal), 8045 // LB(MaxVal)) 8046 ExprResult MinLB = SemaRef.ActOnConditionalOp(DefaultLoc, DefaultLoc, 8047 MinLessMax, LBMin, LBMax); 8048 if (!MinLB.isUsable()) 8049 return nullptr; 8050 LBVal = MinLB.get(); 8051 } else { 8052 // LB(MinVal) < LB(MaxVal) ? LB(MaxVal) : LB(MinVal) - max(LB(MinVal), 8053 // LB(MaxVal)) 8054 ExprResult MaxLB = SemaRef.ActOnConditionalOp(DefaultLoc, DefaultLoc, 8055 MinLessMax, LBMax, LBMin); 8056 if (!MaxLB.isUsable()) 8057 return nullptr; 8058 LBVal = MaxLB.get(); 8059 } 8060 } 8061 // UB = TestIsLessOp.getValue() ? max(UB(MinVal), UB(MaxVal)) : 8062 // min(UB(MinVal), UB(MaxVal)) 8063 if (CondDependOnLC) { 8064 const LoopIterationSpace &IS = ResultIterSpaces[*CondDependOnLC - 1]; 8065 if (!IS.MinValue || !IS.MaxValue) 8066 return nullptr; 8067 // OuterVar = Min 8068 ExprResult MinValue = 8069 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MinValue); 8070 if (!MinValue.isUsable()) 8071 return nullptr; 8072 8073 ExprResult UBMinVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign, 8074 IS.CounterVar, MinValue.get()); 8075 if (!UBMinVal.isUsable()) 8076 return nullptr; 8077 // OuterVar = Min, UBVal 8078 UBMinVal = 8079 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, UBMinVal.get(), UBVal); 8080 if (!UBMinVal.isUsable()) 8081 return nullptr; 8082 // (OuterVar = Min, UBVal) 8083 UBMinVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, UBMinVal.get()); 8084 if (!UBMinVal.isUsable()) 8085 return nullptr; 8086 8087 // OuterVar = Max 8088 ExprResult MaxValue = 8089 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MaxValue); 8090 if (!MaxValue.isUsable()) 8091 return nullptr; 8092 8093 ExprResult UBMaxVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign, 8094 IS.CounterVar, MaxValue.get()); 8095 if (!UBMaxVal.isUsable()) 8096 return nullptr; 8097 // OuterVar = Max, UBVal 8098 UBMaxVal = 8099 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, UBMaxVal.get(), UBVal); 8100 if (!UBMaxVal.isUsable()) 8101 return nullptr; 8102 // (OuterVar = Max, UBVal) 8103 UBMaxVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, UBMaxVal.get()); 8104 if (!UBMaxVal.isUsable()) 8105 return nullptr; 8106 8107 Expr *UBMin = tryBuildCapture(SemaRef, UBMinVal.get(), Captures).get(); 8108 Expr *UBMax = tryBuildCapture(SemaRef, UBMaxVal.get(), Captures).get(); 8109 if (!UBMin || !UBMax) 8110 return nullptr; 8111 // UB(MinVal) > UB(MaxVal) 8112 ExprResult MinGreaterMaxRes = 8113 SemaRef.BuildBinOp(S, DefaultLoc, BO_GT, UBMin, UBMax); 8114 if (!MinGreaterMaxRes.isUsable()) 8115 return nullptr; 8116 Expr *MinGreaterMax = 8117 tryBuildCapture(SemaRef, MinGreaterMaxRes.get(), Captures).get(); 8118 if (!MinGreaterMax) 8119 return nullptr; 8120 if (TestIsLessOp.getValue()) { 8121 // UB(MinVal) > UB(MaxVal) ? UB(MinVal) : UB(MaxVal) - max(UB(MinVal), 8122 // UB(MaxVal)) 8123 ExprResult MaxUB = SemaRef.ActOnConditionalOp( 8124 DefaultLoc, DefaultLoc, MinGreaterMax, UBMin, UBMax); 8125 if (!MaxUB.isUsable()) 8126 return nullptr; 8127 UBVal = MaxUB.get(); 8128 } else { 8129 // UB(MinVal) > UB(MaxVal) ? UB(MaxVal) : UB(MinVal) - min(UB(MinVal), 8130 // UB(MaxVal)) 8131 ExprResult MinUB = SemaRef.ActOnConditionalOp( 8132 DefaultLoc, DefaultLoc, MinGreaterMax, UBMax, UBMin); 8133 if (!MinUB.isUsable()) 8134 return nullptr; 8135 UBVal = MinUB.get(); 8136 } 8137 } 8138 Expr *UBExpr = TestIsLessOp.getValue() ? UBVal : LBVal; 8139 Expr *LBExpr = TestIsLessOp.getValue() ? LBVal : UBVal; 8140 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get(); 8141 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get(); 8142 if (!Upper || !Lower) 8143 return nullptr; 8144 8145 ExprResult Diff = calculateNumIters(SemaRef, S, DefaultLoc, Lower, Upper, 8146 Step, VarType, TestIsStrictOp, 8147 /*RoundToStep=*/true, Captures); 8148 if (!Diff.isUsable()) 8149 return nullptr; 8150 8151 // OpenMP runtime requires 32-bit or 64-bit loop variables. 8152 QualType Type = Diff.get()->getType(); 8153 ASTContext &C = SemaRef.Context; 8154 bool UseVarType = VarType->hasIntegerRepresentation() && 8155 C.getTypeSize(Type) > C.getTypeSize(VarType); 8156 if (!Type->isIntegerType() || UseVarType) { 8157 unsigned NewSize = 8158 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type); 8159 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation() 8160 : Type->hasSignedIntegerRepresentation(); 8161 Type = C.getIntTypeForBitwidth(NewSize, IsSigned); 8162 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) { 8163 Diff = SemaRef.PerformImplicitConversion( 8164 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true); 8165 if (!Diff.isUsable()) 8166 return nullptr; 8167 } 8168 } 8169 if (LimitedType) { 8170 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32; 8171 if (NewSize != C.getTypeSize(Type)) { 8172 if (NewSize < C.getTypeSize(Type)) { 8173 assert(NewSize == 64 && "incorrect loop var size"); 8174 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var) 8175 << InitSrcRange << ConditionSrcRange; 8176 } 8177 QualType NewType = C.getIntTypeForBitwidth( 8178 NewSize, Type->hasSignedIntegerRepresentation() || 8179 C.getTypeSize(Type) < NewSize); 8180 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) { 8181 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType, 8182 Sema::AA_Converting, true); 8183 if (!Diff.isUsable()) 8184 return nullptr; 8185 } 8186 } 8187 } 8188 8189 return Diff.get(); 8190 } 8191 8192 std::pair<Expr *, Expr *> OpenMPIterationSpaceChecker::buildMinMaxValues( 8193 Scope *S, llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 8194 // Do not build for iterators, they cannot be used in non-rectangular loop 8195 // nests. 8196 if (LCDecl->getType()->isRecordType()) 8197 return std::make_pair(nullptr, nullptr); 8198 // If we subtract, the min is in the condition, otherwise the min is in the 8199 // init value. 8200 Expr *MinExpr = nullptr; 8201 Expr *MaxExpr = nullptr; 8202 Expr *LBExpr = TestIsLessOp.getValue() ? LB : UB; 8203 Expr *UBExpr = TestIsLessOp.getValue() ? UB : LB; 8204 bool LBNonRect = TestIsLessOp.getValue() ? InitDependOnLC.hasValue() 8205 : CondDependOnLC.hasValue(); 8206 bool UBNonRect = TestIsLessOp.getValue() ? CondDependOnLC.hasValue() 8207 : InitDependOnLC.hasValue(); 8208 Expr *Lower = 8209 LBNonRect ? LBExpr : tryBuildCapture(SemaRef, LBExpr, Captures).get(); 8210 Expr *Upper = 8211 UBNonRect ? UBExpr : tryBuildCapture(SemaRef, UBExpr, Captures).get(); 8212 if (!Upper || !Lower) 8213 return std::make_pair(nullptr, nullptr); 8214 8215 if (TestIsLessOp.getValue()) 8216 MinExpr = Lower; 8217 else 8218 MaxExpr = Upper; 8219 8220 // Build minimum/maximum value based on number of iterations. 8221 QualType VarType = LCDecl->getType().getNonReferenceType(); 8222 8223 ExprResult Diff = calculateNumIters(SemaRef, S, DefaultLoc, Lower, Upper, 8224 Step, VarType, TestIsStrictOp, 8225 /*RoundToStep=*/false, Captures); 8226 if (!Diff.isUsable()) 8227 return std::make_pair(nullptr, nullptr); 8228 8229 // ((Upper - Lower [- 1]) / Step) * Step 8230 // Parentheses (for dumping/debugging purposes only). 8231 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 8232 if (!Diff.isUsable()) 8233 return std::make_pair(nullptr, nullptr); 8234 8235 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures); 8236 if (!NewStep.isUsable()) 8237 return std::make_pair(nullptr, nullptr); 8238 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Mul, Diff.get(), NewStep.get()); 8239 if (!Diff.isUsable()) 8240 return std::make_pair(nullptr, nullptr); 8241 8242 // Parentheses (for dumping/debugging purposes only). 8243 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 8244 if (!Diff.isUsable()) 8245 return std::make_pair(nullptr, nullptr); 8246 8247 // Convert to the ptrdiff_t, if original type is pointer. 8248 if (VarType->isAnyPointerType() && 8249 !SemaRef.Context.hasSameType( 8250 Diff.get()->getType(), 8251 SemaRef.Context.getUnsignedPointerDiffType())) { 8252 Diff = SemaRef.PerformImplicitConversion( 8253 Diff.get(), SemaRef.Context.getUnsignedPointerDiffType(), 8254 Sema::AA_Converting, /*AllowExplicit=*/true); 8255 } 8256 if (!Diff.isUsable()) 8257 return std::make_pair(nullptr, nullptr); 8258 8259 if (TestIsLessOp.getValue()) { 8260 // MinExpr = Lower; 8261 // MaxExpr = Lower + (((Upper - Lower [- 1]) / Step) * Step) 8262 Diff = SemaRef.BuildBinOp( 8263 S, DefaultLoc, BO_Add, 8264 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Lower).get(), 8265 Diff.get()); 8266 if (!Diff.isUsable()) 8267 return std::make_pair(nullptr, nullptr); 8268 } else { 8269 // MaxExpr = Upper; 8270 // MinExpr = Upper - (((Upper - Lower [- 1]) / Step) * Step) 8271 Diff = SemaRef.BuildBinOp( 8272 S, DefaultLoc, BO_Sub, 8273 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Upper).get(), 8274 Diff.get()); 8275 if (!Diff.isUsable()) 8276 return std::make_pair(nullptr, nullptr); 8277 } 8278 8279 // Convert to the original type. 8280 if (SemaRef.Context.hasSameType(Diff.get()->getType(), VarType)) 8281 Diff = SemaRef.PerformImplicitConversion(Diff.get(), VarType, 8282 Sema::AA_Converting, 8283 /*AllowExplicit=*/true); 8284 if (!Diff.isUsable()) 8285 return std::make_pair(nullptr, nullptr); 8286 8287 Sema::TentativeAnalysisScope Trap(SemaRef); 8288 Diff = SemaRef.ActOnFinishFullExpr(Diff.get(), /*DiscardedValue=*/false); 8289 if (!Diff.isUsable()) 8290 return std::make_pair(nullptr, nullptr); 8291 8292 if (TestIsLessOp.getValue()) 8293 MaxExpr = Diff.get(); 8294 else 8295 MinExpr = Diff.get(); 8296 8297 return std::make_pair(MinExpr, MaxExpr); 8298 } 8299 8300 Expr *OpenMPIterationSpaceChecker::buildFinalCondition(Scope *S) const { 8301 if (InitDependOnLC || CondDependOnLC) 8302 return Condition; 8303 return nullptr; 8304 } 8305 8306 Expr *OpenMPIterationSpaceChecker::buildPreCond( 8307 Scope *S, Expr *Cond, 8308 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 8309 // Do not build a precondition when the condition/initialization is dependent 8310 // to prevent pessimistic early loop exit. 8311 // TODO: this can be improved by calculating min/max values but not sure that 8312 // it will be very effective. 8313 if (CondDependOnLC || InitDependOnLC) 8314 return SemaRef.PerformImplicitConversion( 8315 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(), 8316 SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting, 8317 /*AllowExplicit=*/true).get(); 8318 8319 // Try to build LB <op> UB, where <op> is <, >, <=, or >=. 8320 Sema::TentativeAnalysisScope Trap(SemaRef); 8321 8322 ExprResult NewLB = tryBuildCapture(SemaRef, LB, Captures); 8323 ExprResult NewUB = tryBuildCapture(SemaRef, UB, Captures); 8324 if (!NewLB.isUsable() || !NewUB.isUsable()) 8325 return nullptr; 8326 8327 ExprResult CondExpr = 8328 SemaRef.BuildBinOp(S, DefaultLoc, 8329 TestIsLessOp.getValue() ? 8330 (TestIsStrictOp ? BO_LT : BO_LE) : 8331 (TestIsStrictOp ? BO_GT : BO_GE), 8332 NewLB.get(), NewUB.get()); 8333 if (CondExpr.isUsable()) { 8334 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(), 8335 SemaRef.Context.BoolTy)) 8336 CondExpr = SemaRef.PerformImplicitConversion( 8337 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting, 8338 /*AllowExplicit=*/true); 8339 } 8340 8341 // Otherwise use original loop condition and evaluate it in runtime. 8342 return CondExpr.isUsable() ? CondExpr.get() : Cond; 8343 } 8344 8345 /// Build reference expression to the counter be used for codegen. 8346 DeclRefExpr *OpenMPIterationSpaceChecker::buildCounterVar( 8347 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 8348 DSAStackTy &DSA) const { 8349 auto *VD = dyn_cast<VarDecl>(LCDecl); 8350 if (!VD) { 8351 VD = SemaRef.isOpenMPCapturedDecl(LCDecl); 8352 DeclRefExpr *Ref = buildDeclRefExpr( 8353 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc); 8354 const DSAStackTy::DSAVarData Data = 8355 DSA.getTopDSA(LCDecl, /*FromParent=*/false); 8356 // If the loop control decl is explicitly marked as private, do not mark it 8357 // as captured again. 8358 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr) 8359 Captures.insert(std::make_pair(LCRef, Ref)); 8360 return Ref; 8361 } 8362 return cast<DeclRefExpr>(LCRef); 8363 } 8364 8365 Expr *OpenMPIterationSpaceChecker::buildPrivateCounterVar() const { 8366 if (LCDecl && !LCDecl->isInvalidDecl()) { 8367 QualType Type = LCDecl->getType().getNonReferenceType(); 8368 VarDecl *PrivateVar = buildVarDecl( 8369 SemaRef, DefaultLoc, Type, LCDecl->getName(), 8370 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr, 8371 isa<VarDecl>(LCDecl) 8372 ? buildDeclRefExpr(SemaRef, cast<VarDecl>(LCDecl), Type, DefaultLoc) 8373 : nullptr); 8374 if (PrivateVar->isInvalidDecl()) 8375 return nullptr; 8376 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc); 8377 } 8378 return nullptr; 8379 } 8380 8381 /// Build initialization of the counter to be used for codegen. 8382 Expr *OpenMPIterationSpaceChecker::buildCounterInit() const { return LB; } 8383 8384 /// Build step of the counter be used for codegen. 8385 Expr *OpenMPIterationSpaceChecker::buildCounterStep() const { return Step; } 8386 8387 Expr *OpenMPIterationSpaceChecker::buildOrderedLoopData( 8388 Scope *S, Expr *Counter, 8389 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, SourceLocation Loc, 8390 Expr *Inc, OverloadedOperatorKind OOK) { 8391 Expr *Cnt = SemaRef.DefaultLvalueConversion(Counter).get(); 8392 if (!Cnt) 8393 return nullptr; 8394 if (Inc) { 8395 assert((OOK == OO_Plus || OOK == OO_Minus) && 8396 "Expected only + or - operations for depend clauses."); 8397 BinaryOperatorKind BOK = (OOK == OO_Plus) ? BO_Add : BO_Sub; 8398 Cnt = SemaRef.BuildBinOp(S, Loc, BOK, Cnt, Inc).get(); 8399 if (!Cnt) 8400 return nullptr; 8401 } 8402 QualType VarType = LCDecl->getType().getNonReferenceType(); 8403 if (!VarType->isIntegerType() && !VarType->isPointerType() && 8404 !SemaRef.getLangOpts().CPlusPlus) 8405 return nullptr; 8406 // Upper - Lower 8407 Expr *Upper = TestIsLessOp.getValue() 8408 ? Cnt 8409 : tryBuildCapture(SemaRef, LB, Captures).get(); 8410 Expr *Lower = TestIsLessOp.getValue() 8411 ? tryBuildCapture(SemaRef, LB, Captures).get() 8412 : Cnt; 8413 if (!Upper || !Lower) 8414 return nullptr; 8415 8416 ExprResult Diff = calculateNumIters( 8417 SemaRef, S, DefaultLoc, Lower, Upper, Step, VarType, 8418 /*TestIsStrictOp=*/false, /*RoundToStep=*/false, Captures); 8419 if (!Diff.isUsable()) 8420 return nullptr; 8421 8422 return Diff.get(); 8423 } 8424 } // namespace 8425 8426 void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) { 8427 assert(getLangOpts().OpenMP && "OpenMP is not active."); 8428 assert(Init && "Expected loop in canonical form."); 8429 unsigned AssociatedLoops = DSAStack->getAssociatedLoops(); 8430 if (AssociatedLoops > 0 && 8431 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { 8432 DSAStack->loopStart(); 8433 OpenMPIterationSpaceChecker ISC(*this, /*SupportsNonRectangular=*/true, 8434 *DSAStack, ForLoc); 8435 if (!ISC.checkAndSetInit(Init, /*EmitDiags=*/false)) { 8436 if (ValueDecl *D = ISC.getLoopDecl()) { 8437 auto *VD = dyn_cast<VarDecl>(D); 8438 DeclRefExpr *PrivateRef = nullptr; 8439 if (!VD) { 8440 if (VarDecl *Private = isOpenMPCapturedDecl(D)) { 8441 VD = Private; 8442 } else { 8443 PrivateRef = buildCapture(*this, D, ISC.getLoopDeclRefExpr(), 8444 /*WithInit=*/false); 8445 VD = cast<VarDecl>(PrivateRef->getDecl()); 8446 } 8447 } 8448 DSAStack->addLoopControlVariable(D, VD); 8449 const Decl *LD = DSAStack->getPossiblyLoopCunter(); 8450 if (LD != D->getCanonicalDecl()) { 8451 DSAStack->resetPossibleLoopCounter(); 8452 if (auto *Var = dyn_cast_or_null<VarDecl>(LD)) 8453 MarkDeclarationsReferencedInExpr( 8454 buildDeclRefExpr(*this, const_cast<VarDecl *>(Var), 8455 Var->getType().getNonLValueExprType(Context), 8456 ForLoc, /*RefersToCapture=*/true)); 8457 } 8458 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 8459 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables 8460 // Referenced in a Construct, C/C++]. The loop iteration variable in the 8461 // associated for-loop of a simd construct with just one associated 8462 // for-loop may be listed in a linear clause with a constant-linear-step 8463 // that is the increment of the associated for-loop. The loop iteration 8464 // variable(s) in the associated for-loop(s) of a for or parallel for 8465 // construct may be listed in a private or lastprivate clause. 8466 DSAStackTy::DSAVarData DVar = 8467 DSAStack->getTopDSA(D, /*FromParent=*/false); 8468 // If LoopVarRefExpr is nullptr it means the corresponding loop variable 8469 // is declared in the loop and it is predetermined as a private. 8470 Expr *LoopDeclRefExpr = ISC.getLoopDeclRefExpr(); 8471 OpenMPClauseKind PredeterminedCKind = 8472 isOpenMPSimdDirective(DKind) 8473 ? (DSAStack->hasMutipleLoops() ? OMPC_lastprivate : OMPC_linear) 8474 : OMPC_private; 8475 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && 8476 DVar.CKind != PredeterminedCKind && DVar.RefExpr && 8477 (LangOpts.OpenMP <= 45 || (DVar.CKind != OMPC_lastprivate && 8478 DVar.CKind != OMPC_private))) || 8479 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop || 8480 DKind == OMPD_master_taskloop || 8481 DKind == OMPD_parallel_master_taskloop || 8482 isOpenMPDistributeDirective(DKind)) && 8483 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && 8484 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) && 8485 (DVar.CKind != OMPC_private || DVar.RefExpr)) { 8486 Diag(Init->getBeginLoc(), diag::err_omp_loop_var_dsa) 8487 << getOpenMPClauseName(DVar.CKind) 8488 << getOpenMPDirectiveName(DKind) 8489 << getOpenMPClauseName(PredeterminedCKind); 8490 if (DVar.RefExpr == nullptr) 8491 DVar.CKind = PredeterminedCKind; 8492 reportOriginalDsa(*this, DSAStack, D, DVar, 8493 /*IsLoopIterVar=*/true); 8494 } else if (LoopDeclRefExpr) { 8495 // Make the loop iteration variable private (for worksharing 8496 // constructs), linear (for simd directives with the only one 8497 // associated loop) or lastprivate (for simd directives with several 8498 // collapsed or ordered loops). 8499 if (DVar.CKind == OMPC_unknown) 8500 DSAStack->addDSA(D, LoopDeclRefExpr, PredeterminedCKind, 8501 PrivateRef); 8502 } 8503 } 8504 } 8505 DSAStack->setAssociatedLoops(AssociatedLoops - 1); 8506 } 8507 } 8508 8509 /// Called on a for stmt to check and extract its iteration space 8510 /// for further processing (such as collapsing). 8511 static bool checkOpenMPIterationSpace( 8512 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA, 8513 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount, 8514 unsigned TotalNestedLoopCount, Expr *CollapseLoopCountExpr, 8515 Expr *OrderedLoopCountExpr, 8516 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA, 8517 llvm::MutableArrayRef<LoopIterationSpace> ResultIterSpaces, 8518 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 8519 bool SupportsNonRectangular = !isOpenMPLoopTransformationDirective(DKind); 8520 // OpenMP [2.9.1, Canonical Loop Form] 8521 // for (init-expr; test-expr; incr-expr) structured-block 8522 // for (range-decl: range-expr) structured-block 8523 if (auto *CanonLoop = dyn_cast_or_null<OMPCanonicalLoop>(S)) 8524 S = CanonLoop->getLoopStmt(); 8525 auto *For = dyn_cast_or_null<ForStmt>(S); 8526 auto *CXXFor = dyn_cast_or_null<CXXForRangeStmt>(S); 8527 // Ranged for is supported only in OpenMP 5.0. 8528 if (!For && (SemaRef.LangOpts.OpenMP <= 45 || !CXXFor)) { 8529 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_not_for) 8530 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr) 8531 << getOpenMPDirectiveName(DKind) << TotalNestedLoopCount 8532 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount; 8533 if (TotalNestedLoopCount > 1) { 8534 if (CollapseLoopCountExpr && OrderedLoopCountExpr) 8535 SemaRef.Diag(DSA.getConstructLoc(), 8536 diag::note_omp_collapse_ordered_expr) 8537 << 2 << CollapseLoopCountExpr->getSourceRange() 8538 << OrderedLoopCountExpr->getSourceRange(); 8539 else if (CollapseLoopCountExpr) 8540 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), 8541 diag::note_omp_collapse_ordered_expr) 8542 << 0 << CollapseLoopCountExpr->getSourceRange(); 8543 else 8544 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), 8545 diag::note_omp_collapse_ordered_expr) 8546 << 1 << OrderedLoopCountExpr->getSourceRange(); 8547 } 8548 return true; 8549 } 8550 assert(((For && For->getBody()) || (CXXFor && CXXFor->getBody())) && 8551 "No loop body."); 8552 8553 OpenMPIterationSpaceChecker ISC(SemaRef, SupportsNonRectangular, DSA, 8554 For ? For->getForLoc() : CXXFor->getForLoc()); 8555 8556 // Check init. 8557 Stmt *Init = For ? For->getInit() : CXXFor->getBeginStmt(); 8558 if (ISC.checkAndSetInit(Init)) 8559 return true; 8560 8561 bool HasErrors = false; 8562 8563 // Check loop variable's type. 8564 if (ValueDecl *LCDecl = ISC.getLoopDecl()) { 8565 // OpenMP [2.6, Canonical Loop Form] 8566 // Var is one of the following: 8567 // A variable of signed or unsigned integer type. 8568 // For C++, a variable of a random access iterator type. 8569 // For C, a variable of a pointer type. 8570 QualType VarType = LCDecl->getType().getNonReferenceType(); 8571 if (!VarType->isDependentType() && !VarType->isIntegerType() && 8572 !VarType->isPointerType() && 8573 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) { 8574 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_variable_type) 8575 << SemaRef.getLangOpts().CPlusPlus; 8576 HasErrors = true; 8577 } 8578 8579 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in 8580 // a Construct 8581 // The loop iteration variable(s) in the associated for-loop(s) of a for or 8582 // parallel for construct is (are) private. 8583 // The loop iteration variable in the associated for-loop of a simd 8584 // construct with just one associated for-loop is linear with a 8585 // constant-linear-step that is the increment of the associated for-loop. 8586 // Exclude loop var from the list of variables with implicitly defined data 8587 // sharing attributes. 8588 VarsWithImplicitDSA.erase(LCDecl); 8589 8590 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars"); 8591 8592 // Check test-expr. 8593 HasErrors |= ISC.checkAndSetCond(For ? For->getCond() : CXXFor->getCond()); 8594 8595 // Check incr-expr. 8596 HasErrors |= ISC.checkAndSetInc(For ? For->getInc() : CXXFor->getInc()); 8597 } 8598 8599 if (ISC.dependent() || SemaRef.CurContext->isDependentContext() || HasErrors) 8600 return HasErrors; 8601 8602 // Build the loop's iteration space representation. 8603 ResultIterSpaces[CurrentNestedLoopCount].PreCond = ISC.buildPreCond( 8604 DSA.getCurScope(), For ? For->getCond() : CXXFor->getCond(), Captures); 8605 ResultIterSpaces[CurrentNestedLoopCount].NumIterations = 8606 ISC.buildNumIterations(DSA.getCurScope(), ResultIterSpaces, 8607 (isOpenMPWorksharingDirective(DKind) || 8608 isOpenMPTaskLoopDirective(DKind) || 8609 isOpenMPDistributeDirective(DKind) || 8610 isOpenMPLoopTransformationDirective(DKind)), 8611 Captures); 8612 ResultIterSpaces[CurrentNestedLoopCount].CounterVar = 8613 ISC.buildCounterVar(Captures, DSA); 8614 ResultIterSpaces[CurrentNestedLoopCount].PrivateCounterVar = 8615 ISC.buildPrivateCounterVar(); 8616 ResultIterSpaces[CurrentNestedLoopCount].CounterInit = ISC.buildCounterInit(); 8617 ResultIterSpaces[CurrentNestedLoopCount].CounterStep = ISC.buildCounterStep(); 8618 ResultIterSpaces[CurrentNestedLoopCount].InitSrcRange = ISC.getInitSrcRange(); 8619 ResultIterSpaces[CurrentNestedLoopCount].CondSrcRange = 8620 ISC.getConditionSrcRange(); 8621 ResultIterSpaces[CurrentNestedLoopCount].IncSrcRange = 8622 ISC.getIncrementSrcRange(); 8623 ResultIterSpaces[CurrentNestedLoopCount].Subtract = ISC.shouldSubtractStep(); 8624 ResultIterSpaces[CurrentNestedLoopCount].IsStrictCompare = 8625 ISC.isStrictTestOp(); 8626 std::tie(ResultIterSpaces[CurrentNestedLoopCount].MinValue, 8627 ResultIterSpaces[CurrentNestedLoopCount].MaxValue) = 8628 ISC.buildMinMaxValues(DSA.getCurScope(), Captures); 8629 ResultIterSpaces[CurrentNestedLoopCount].FinalCondition = 8630 ISC.buildFinalCondition(DSA.getCurScope()); 8631 ResultIterSpaces[CurrentNestedLoopCount].IsNonRectangularLB = 8632 ISC.doesInitDependOnLC(); 8633 ResultIterSpaces[CurrentNestedLoopCount].IsNonRectangularUB = 8634 ISC.doesCondDependOnLC(); 8635 ResultIterSpaces[CurrentNestedLoopCount].LoopDependentIdx = 8636 ISC.getLoopDependentIdx(); 8637 8638 HasErrors |= 8639 (ResultIterSpaces[CurrentNestedLoopCount].PreCond == nullptr || 8640 ResultIterSpaces[CurrentNestedLoopCount].NumIterations == nullptr || 8641 ResultIterSpaces[CurrentNestedLoopCount].CounterVar == nullptr || 8642 ResultIterSpaces[CurrentNestedLoopCount].PrivateCounterVar == nullptr || 8643 ResultIterSpaces[CurrentNestedLoopCount].CounterInit == nullptr || 8644 ResultIterSpaces[CurrentNestedLoopCount].CounterStep == nullptr); 8645 if (!HasErrors && DSA.isOrderedRegion()) { 8646 if (DSA.getOrderedRegionParam().second->getNumForLoops()) { 8647 if (CurrentNestedLoopCount < 8648 DSA.getOrderedRegionParam().second->getLoopNumIterations().size()) { 8649 DSA.getOrderedRegionParam().second->setLoopNumIterations( 8650 CurrentNestedLoopCount, 8651 ResultIterSpaces[CurrentNestedLoopCount].NumIterations); 8652 DSA.getOrderedRegionParam().second->setLoopCounter( 8653 CurrentNestedLoopCount, 8654 ResultIterSpaces[CurrentNestedLoopCount].CounterVar); 8655 } 8656 } 8657 for (auto &Pair : DSA.getDoacrossDependClauses()) { 8658 if (CurrentNestedLoopCount >= Pair.first->getNumLoops()) { 8659 // Erroneous case - clause has some problems. 8660 continue; 8661 } 8662 if (Pair.first->getDependencyKind() == OMPC_DEPEND_sink && 8663 Pair.second.size() <= CurrentNestedLoopCount) { 8664 // Erroneous case - clause has some problems. 8665 Pair.first->setLoopData(CurrentNestedLoopCount, nullptr); 8666 continue; 8667 } 8668 Expr *CntValue; 8669 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source) 8670 CntValue = ISC.buildOrderedLoopData( 8671 DSA.getCurScope(), 8672 ResultIterSpaces[CurrentNestedLoopCount].CounterVar, Captures, 8673 Pair.first->getDependencyLoc()); 8674 else 8675 CntValue = ISC.buildOrderedLoopData( 8676 DSA.getCurScope(), 8677 ResultIterSpaces[CurrentNestedLoopCount].CounterVar, Captures, 8678 Pair.first->getDependencyLoc(), 8679 Pair.second[CurrentNestedLoopCount].first, 8680 Pair.second[CurrentNestedLoopCount].second); 8681 Pair.first->setLoopData(CurrentNestedLoopCount, CntValue); 8682 } 8683 } 8684 8685 return HasErrors; 8686 } 8687 8688 /// Build 'VarRef = Start. 8689 static ExprResult 8690 buildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef, 8691 ExprResult Start, bool IsNonRectangularLB, 8692 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 8693 // Build 'VarRef = Start. 8694 ExprResult NewStart = IsNonRectangularLB 8695 ? Start.get() 8696 : tryBuildCapture(SemaRef, Start.get(), Captures); 8697 if (!NewStart.isUsable()) 8698 return ExprError(); 8699 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(), 8700 VarRef.get()->getType())) { 8701 NewStart = SemaRef.PerformImplicitConversion( 8702 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting, 8703 /*AllowExplicit=*/true); 8704 if (!NewStart.isUsable()) 8705 return ExprError(); 8706 } 8707 8708 ExprResult Init = 8709 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); 8710 return Init; 8711 } 8712 8713 /// Build 'VarRef = Start + Iter * Step'. 8714 static ExprResult buildCounterUpdate( 8715 Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef, 8716 ExprResult Start, ExprResult Iter, ExprResult Step, bool Subtract, 8717 bool IsNonRectangularLB, 8718 llvm::MapVector<const Expr *, DeclRefExpr *> *Captures = nullptr) { 8719 // Add parentheses (for debugging purposes only). 8720 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get()); 8721 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() || 8722 !Step.isUsable()) 8723 return ExprError(); 8724 8725 ExprResult NewStep = Step; 8726 if (Captures) 8727 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures); 8728 if (NewStep.isInvalid()) 8729 return ExprError(); 8730 ExprResult Update = 8731 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get()); 8732 if (!Update.isUsable()) 8733 return ExprError(); 8734 8735 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or 8736 // 'VarRef = Start (+|-) Iter * Step'. 8737 if (!Start.isUsable()) 8738 return ExprError(); 8739 ExprResult NewStart = SemaRef.ActOnParenExpr(Loc, Loc, Start.get()); 8740 if (!NewStart.isUsable()) 8741 return ExprError(); 8742 if (Captures && !IsNonRectangularLB) 8743 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures); 8744 if (NewStart.isInvalid()) 8745 return ExprError(); 8746 8747 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'. 8748 ExprResult SavedUpdate = Update; 8749 ExprResult UpdateVal; 8750 if (VarRef.get()->getType()->isOverloadableType() || 8751 NewStart.get()->getType()->isOverloadableType() || 8752 Update.get()->getType()->isOverloadableType()) { 8753 Sema::TentativeAnalysisScope Trap(SemaRef); 8754 8755 Update = 8756 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); 8757 if (Update.isUsable()) { 8758 UpdateVal = 8759 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign, 8760 VarRef.get(), SavedUpdate.get()); 8761 if (UpdateVal.isUsable()) { 8762 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(), 8763 UpdateVal.get()); 8764 } 8765 } 8766 } 8767 8768 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'. 8769 if (!Update.isUsable() || !UpdateVal.isUsable()) { 8770 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add, 8771 NewStart.get(), SavedUpdate.get()); 8772 if (!Update.isUsable()) 8773 return ExprError(); 8774 8775 if (!SemaRef.Context.hasSameType(Update.get()->getType(), 8776 VarRef.get()->getType())) { 8777 Update = SemaRef.PerformImplicitConversion( 8778 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true); 8779 if (!Update.isUsable()) 8780 return ExprError(); 8781 } 8782 8783 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get()); 8784 } 8785 return Update; 8786 } 8787 8788 /// Convert integer expression \a E to make it have at least \a Bits 8789 /// bits. 8790 static ExprResult widenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) { 8791 if (E == nullptr) 8792 return ExprError(); 8793 ASTContext &C = SemaRef.Context; 8794 QualType OldType = E->getType(); 8795 unsigned HasBits = C.getTypeSize(OldType); 8796 if (HasBits >= Bits) 8797 return ExprResult(E); 8798 // OK to convert to signed, because new type has more bits than old. 8799 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true); 8800 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting, 8801 true); 8802 } 8803 8804 /// Check if the given expression \a E is a constant integer that fits 8805 /// into \a Bits bits. 8806 static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) { 8807 if (E == nullptr) 8808 return false; 8809 if (Optional<llvm::APSInt> Result = 8810 E->getIntegerConstantExpr(SemaRef.Context)) 8811 return Signed ? Result->isSignedIntN(Bits) : Result->isIntN(Bits); 8812 return false; 8813 } 8814 8815 /// Build preinits statement for the given declarations. 8816 static Stmt *buildPreInits(ASTContext &Context, 8817 MutableArrayRef<Decl *> PreInits) { 8818 if (!PreInits.empty()) { 8819 return new (Context) DeclStmt( 8820 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()), 8821 SourceLocation(), SourceLocation()); 8822 } 8823 return nullptr; 8824 } 8825 8826 /// Build preinits statement for the given declarations. 8827 static Stmt * 8828 buildPreInits(ASTContext &Context, 8829 const llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 8830 if (!Captures.empty()) { 8831 SmallVector<Decl *, 16> PreInits; 8832 for (const auto &Pair : Captures) 8833 PreInits.push_back(Pair.second->getDecl()); 8834 return buildPreInits(Context, PreInits); 8835 } 8836 return nullptr; 8837 } 8838 8839 /// Build postupdate expression for the given list of postupdates expressions. 8840 static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) { 8841 Expr *PostUpdate = nullptr; 8842 if (!PostUpdates.empty()) { 8843 for (Expr *E : PostUpdates) { 8844 Expr *ConvE = S.BuildCStyleCastExpr( 8845 E->getExprLoc(), 8846 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy), 8847 E->getExprLoc(), E) 8848 .get(); 8849 PostUpdate = PostUpdate 8850 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma, 8851 PostUpdate, ConvE) 8852 .get() 8853 : ConvE; 8854 } 8855 } 8856 return PostUpdate; 8857 } 8858 8859 /// Called on a for stmt to check itself and nested loops (if any). 8860 /// \return Returns 0 if one of the collapsed stmts is not canonical for loop, 8861 /// number of collapsed loops otherwise. 8862 static unsigned 8863 checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr, 8864 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef, 8865 DSAStackTy &DSA, 8866 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA, 8867 OMPLoopBasedDirective::HelperExprs &Built) { 8868 unsigned NestedLoopCount = 1; 8869 bool SupportsNonPerfectlyNested = (SemaRef.LangOpts.OpenMP >= 50) && 8870 !isOpenMPLoopTransformationDirective(DKind); 8871 8872 if (CollapseLoopCountExpr) { 8873 // Found 'collapse' clause - calculate collapse number. 8874 Expr::EvalResult Result; 8875 if (!CollapseLoopCountExpr->isValueDependent() && 8876 CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) { 8877 NestedLoopCount = Result.Val.getInt().getLimitedValue(); 8878 } else { 8879 Built.clear(/*Size=*/1); 8880 return 1; 8881 } 8882 } 8883 unsigned OrderedLoopCount = 1; 8884 if (OrderedLoopCountExpr) { 8885 // Found 'ordered' clause - calculate collapse number. 8886 Expr::EvalResult EVResult; 8887 if (!OrderedLoopCountExpr->isValueDependent() && 8888 OrderedLoopCountExpr->EvaluateAsInt(EVResult, 8889 SemaRef.getASTContext())) { 8890 llvm::APSInt Result = EVResult.Val.getInt(); 8891 if (Result.getLimitedValue() < NestedLoopCount) { 8892 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), 8893 diag::err_omp_wrong_ordered_loop_count) 8894 << OrderedLoopCountExpr->getSourceRange(); 8895 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), 8896 diag::note_collapse_loop_count) 8897 << CollapseLoopCountExpr->getSourceRange(); 8898 } 8899 OrderedLoopCount = Result.getLimitedValue(); 8900 } else { 8901 Built.clear(/*Size=*/1); 8902 return 1; 8903 } 8904 } 8905 // This is helper routine for loop directives (e.g., 'for', 'simd', 8906 // 'for simd', etc.). 8907 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 8908 unsigned NumLoops = std::max(OrderedLoopCount, NestedLoopCount); 8909 SmallVector<LoopIterationSpace, 4> IterSpaces(NumLoops); 8910 if (!OMPLoopBasedDirective::doForAllLoops( 8911 AStmt->IgnoreContainers(!isOpenMPLoopTransformationDirective(DKind)), 8912 SupportsNonPerfectlyNested, NumLoops, 8913 [DKind, &SemaRef, &DSA, NumLoops, NestedLoopCount, 8914 CollapseLoopCountExpr, OrderedLoopCountExpr, &VarsWithImplicitDSA, 8915 &IterSpaces, &Captures](unsigned Cnt, Stmt *CurStmt) { 8916 if (checkOpenMPIterationSpace( 8917 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount, 8918 NumLoops, CollapseLoopCountExpr, OrderedLoopCountExpr, 8919 VarsWithImplicitDSA, IterSpaces, Captures)) 8920 return true; 8921 if (Cnt > 0 && Cnt >= NestedLoopCount && 8922 IterSpaces[Cnt].CounterVar) { 8923 // Handle initialization of captured loop iterator variables. 8924 auto *DRE = cast<DeclRefExpr>(IterSpaces[Cnt].CounterVar); 8925 if (isa<OMPCapturedExprDecl>(DRE->getDecl())) { 8926 Captures[DRE] = DRE; 8927 } 8928 } 8929 return false; 8930 })) 8931 return 0; 8932 8933 Built.clear(/* size */ NestedLoopCount); 8934 8935 if (SemaRef.CurContext->isDependentContext()) 8936 return NestedLoopCount; 8937 8938 // An example of what is generated for the following code: 8939 // 8940 // #pragma omp simd collapse(2) ordered(2) 8941 // for (i = 0; i < NI; ++i) 8942 // for (k = 0; k < NK; ++k) 8943 // for (j = J0; j < NJ; j+=2) { 8944 // <loop body> 8945 // } 8946 // 8947 // We generate the code below. 8948 // Note: the loop body may be outlined in CodeGen. 8949 // Note: some counters may be C++ classes, operator- is used to find number of 8950 // iterations and operator+= to calculate counter value. 8951 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32 8952 // or i64 is currently supported). 8953 // 8954 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2)) 8955 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) { 8956 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2); 8957 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2; 8958 // // similar updates for vars in clauses (e.g. 'linear') 8959 // <loop body (using local i and j)> 8960 // } 8961 // i = NI; // assign final values of counters 8962 // j = NJ; 8963 // 8964 8965 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are 8966 // the iteration counts of the collapsed for loops. 8967 // Precondition tests if there is at least one iteration (all conditions are 8968 // true). 8969 auto PreCond = ExprResult(IterSpaces[0].PreCond); 8970 Expr *N0 = IterSpaces[0].NumIterations; 8971 ExprResult LastIteration32 = 8972 widenIterationCount(/*Bits=*/32, 8973 SemaRef 8974 .PerformImplicitConversion( 8975 N0->IgnoreImpCasts(), N0->getType(), 8976 Sema::AA_Converting, /*AllowExplicit=*/true) 8977 .get(), 8978 SemaRef); 8979 ExprResult LastIteration64 = widenIterationCount( 8980 /*Bits=*/64, 8981 SemaRef 8982 .PerformImplicitConversion(N0->IgnoreImpCasts(), N0->getType(), 8983 Sema::AA_Converting, 8984 /*AllowExplicit=*/true) 8985 .get(), 8986 SemaRef); 8987 8988 if (!LastIteration32.isUsable() || !LastIteration64.isUsable()) 8989 return NestedLoopCount; 8990 8991 ASTContext &C = SemaRef.Context; 8992 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32; 8993 8994 Scope *CurScope = DSA.getCurScope(); 8995 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) { 8996 if (PreCond.isUsable()) { 8997 PreCond = 8998 SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd, 8999 PreCond.get(), IterSpaces[Cnt].PreCond); 9000 } 9001 Expr *N = IterSpaces[Cnt].NumIterations; 9002 SourceLocation Loc = N->getExprLoc(); 9003 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32; 9004 if (LastIteration32.isUsable()) 9005 LastIteration32 = SemaRef.BuildBinOp( 9006 CurScope, Loc, BO_Mul, LastIteration32.get(), 9007 SemaRef 9008 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), 9009 Sema::AA_Converting, 9010 /*AllowExplicit=*/true) 9011 .get()); 9012 if (LastIteration64.isUsable()) 9013 LastIteration64 = SemaRef.BuildBinOp( 9014 CurScope, Loc, BO_Mul, LastIteration64.get(), 9015 SemaRef 9016 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), 9017 Sema::AA_Converting, 9018 /*AllowExplicit=*/true) 9019 .get()); 9020 } 9021 9022 // Choose either the 32-bit or 64-bit version. 9023 ExprResult LastIteration = LastIteration64; 9024 if (SemaRef.getLangOpts().OpenMPOptimisticCollapse || 9025 (LastIteration32.isUsable() && 9026 C.getTypeSize(LastIteration32.get()->getType()) == 32 && 9027 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 || 9028 fitsInto( 9029 /*Bits=*/32, 9030 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(), 9031 LastIteration64.get(), SemaRef)))) 9032 LastIteration = LastIteration32; 9033 QualType VType = LastIteration.get()->getType(); 9034 QualType RealVType = VType; 9035 QualType StrideVType = VType; 9036 if (isOpenMPTaskLoopDirective(DKind)) { 9037 VType = 9038 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0); 9039 StrideVType = 9040 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1); 9041 } 9042 9043 if (!LastIteration.isUsable()) 9044 return 0; 9045 9046 // Save the number of iterations. 9047 ExprResult NumIterations = LastIteration; 9048 { 9049 LastIteration = SemaRef.BuildBinOp( 9050 CurScope, LastIteration.get()->getExprLoc(), BO_Sub, 9051 LastIteration.get(), 9052 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 9053 if (!LastIteration.isUsable()) 9054 return 0; 9055 } 9056 9057 // Calculate the last iteration number beforehand instead of doing this on 9058 // each iteration. Do not do this if the number of iterations may be kfold-ed. 9059 bool IsConstant = LastIteration.get()->isIntegerConstantExpr(SemaRef.Context); 9060 ExprResult CalcLastIteration; 9061 if (!IsConstant) { 9062 ExprResult SaveRef = 9063 tryBuildCapture(SemaRef, LastIteration.get(), Captures); 9064 LastIteration = SaveRef; 9065 9066 // Prepare SaveRef + 1. 9067 NumIterations = SemaRef.BuildBinOp( 9068 CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(), 9069 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 9070 if (!NumIterations.isUsable()) 9071 return 0; 9072 } 9073 9074 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin(); 9075 9076 // Build variables passed into runtime, necessary for worksharing directives. 9077 ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB; 9078 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || 9079 isOpenMPDistributeDirective(DKind) || 9080 isOpenMPLoopTransformationDirective(DKind)) { 9081 // Lower bound variable, initialized with zero. 9082 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb"); 9083 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc); 9084 SemaRef.AddInitializerToDecl(LBDecl, 9085 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 9086 /*DirectInit*/ false); 9087 9088 // Upper bound variable, initialized with last iteration number. 9089 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub"); 9090 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc); 9091 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(), 9092 /*DirectInit*/ false); 9093 9094 // A 32-bit variable-flag where runtime returns 1 for the last iteration. 9095 // This will be used to implement clause 'lastprivate'. 9096 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true); 9097 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last"); 9098 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc); 9099 SemaRef.AddInitializerToDecl(ILDecl, 9100 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 9101 /*DirectInit*/ false); 9102 9103 // Stride variable returned by runtime (we initialize it to 1 by default). 9104 VarDecl *STDecl = 9105 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride"); 9106 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc); 9107 SemaRef.AddInitializerToDecl(STDecl, 9108 SemaRef.ActOnIntegerConstant(InitLoc, 1).get(), 9109 /*DirectInit*/ false); 9110 9111 // Build expression: UB = min(UB, LastIteration) 9112 // It is necessary for CodeGen of directives with static scheduling. 9113 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT, 9114 UB.get(), LastIteration.get()); 9115 ExprResult CondOp = SemaRef.ActOnConditionalOp( 9116 LastIteration.get()->getExprLoc(), InitLoc, IsUBGreater.get(), 9117 LastIteration.get(), UB.get()); 9118 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(), 9119 CondOp.get()); 9120 EUB = SemaRef.ActOnFinishFullExpr(EUB.get(), /*DiscardedValue*/ false); 9121 9122 // If we have a combined directive that combines 'distribute', 'for' or 9123 // 'simd' we need to be able to access the bounds of the schedule of the 9124 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained 9125 // by scheduling 'distribute' have to be passed to the schedule of 'for'. 9126 if (isOpenMPLoopBoundSharingDirective(DKind)) { 9127 // Lower bound variable, initialized with zero. 9128 VarDecl *CombLBDecl = 9129 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb"); 9130 CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc); 9131 SemaRef.AddInitializerToDecl( 9132 CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 9133 /*DirectInit*/ false); 9134 9135 // Upper bound variable, initialized with last iteration number. 9136 VarDecl *CombUBDecl = 9137 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub"); 9138 CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc); 9139 SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(), 9140 /*DirectInit*/ false); 9141 9142 ExprResult CombIsUBGreater = SemaRef.BuildBinOp( 9143 CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get()); 9144 ExprResult CombCondOp = 9145 SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(), 9146 LastIteration.get(), CombUB.get()); 9147 CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(), 9148 CombCondOp.get()); 9149 CombEUB = 9150 SemaRef.ActOnFinishFullExpr(CombEUB.get(), /*DiscardedValue*/ false); 9151 9152 const CapturedDecl *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl(); 9153 // We expect to have at least 2 more parameters than the 'parallel' 9154 // directive does - the lower and upper bounds of the previous schedule. 9155 assert(CD->getNumParams() >= 4 && 9156 "Unexpected number of parameters in loop combined directive"); 9157 9158 // Set the proper type for the bounds given what we learned from the 9159 // enclosed loops. 9160 ImplicitParamDecl *PrevLBDecl = CD->getParam(/*PrevLB=*/2); 9161 ImplicitParamDecl *PrevUBDecl = CD->getParam(/*PrevUB=*/3); 9162 9163 // Previous lower and upper bounds are obtained from the region 9164 // parameters. 9165 PrevLB = 9166 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc); 9167 PrevUB = 9168 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc); 9169 } 9170 } 9171 9172 // Build the iteration variable and its initialization before loop. 9173 ExprResult IV; 9174 ExprResult Init, CombInit; 9175 { 9176 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv"); 9177 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc); 9178 Expr *RHS = (isOpenMPWorksharingDirective(DKind) || 9179 isOpenMPTaskLoopDirective(DKind) || 9180 isOpenMPDistributeDirective(DKind) || 9181 isOpenMPLoopTransformationDirective(DKind)) 9182 ? LB.get() 9183 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get(); 9184 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS); 9185 Init = SemaRef.ActOnFinishFullExpr(Init.get(), /*DiscardedValue*/ false); 9186 9187 if (isOpenMPLoopBoundSharingDirective(DKind)) { 9188 Expr *CombRHS = 9189 (isOpenMPWorksharingDirective(DKind) || 9190 isOpenMPTaskLoopDirective(DKind) || 9191 isOpenMPDistributeDirective(DKind)) 9192 ? CombLB.get() 9193 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get(); 9194 CombInit = 9195 SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS); 9196 CombInit = 9197 SemaRef.ActOnFinishFullExpr(CombInit.get(), /*DiscardedValue*/ false); 9198 } 9199 } 9200 9201 bool UseStrictCompare = 9202 RealVType->hasUnsignedIntegerRepresentation() && 9203 llvm::all_of(IterSpaces, [](const LoopIterationSpace &LIS) { 9204 return LIS.IsStrictCompare; 9205 }); 9206 // Loop condition (IV < NumIterations) or (IV <= UB or IV < UB + 1 (for 9207 // unsigned IV)) for worksharing loops. 9208 SourceLocation CondLoc = AStmt->getBeginLoc(); 9209 Expr *BoundUB = UB.get(); 9210 if (UseStrictCompare) { 9211 BoundUB = 9212 SemaRef 9213 .BuildBinOp(CurScope, CondLoc, BO_Add, BoundUB, 9214 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()) 9215 .get(); 9216 BoundUB = 9217 SemaRef.ActOnFinishFullExpr(BoundUB, /*DiscardedValue*/ false).get(); 9218 } 9219 ExprResult Cond = 9220 (isOpenMPWorksharingDirective(DKind) || 9221 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind) || 9222 isOpenMPLoopTransformationDirective(DKind)) 9223 ? SemaRef.BuildBinOp(CurScope, CondLoc, 9224 UseStrictCompare ? BO_LT : BO_LE, IV.get(), 9225 BoundUB) 9226 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(), 9227 NumIterations.get()); 9228 ExprResult CombDistCond; 9229 if (isOpenMPLoopBoundSharingDirective(DKind)) { 9230 CombDistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(), 9231 NumIterations.get()); 9232 } 9233 9234 ExprResult CombCond; 9235 if (isOpenMPLoopBoundSharingDirective(DKind)) { 9236 Expr *BoundCombUB = CombUB.get(); 9237 if (UseStrictCompare) { 9238 BoundCombUB = 9239 SemaRef 9240 .BuildBinOp( 9241 CurScope, CondLoc, BO_Add, BoundCombUB, 9242 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()) 9243 .get(); 9244 BoundCombUB = 9245 SemaRef.ActOnFinishFullExpr(BoundCombUB, /*DiscardedValue*/ false) 9246 .get(); 9247 } 9248 CombCond = 9249 SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, 9250 IV.get(), BoundCombUB); 9251 } 9252 // Loop increment (IV = IV + 1) 9253 SourceLocation IncLoc = AStmt->getBeginLoc(); 9254 ExprResult Inc = 9255 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(), 9256 SemaRef.ActOnIntegerConstant(IncLoc, 1).get()); 9257 if (!Inc.isUsable()) 9258 return 0; 9259 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get()); 9260 Inc = SemaRef.ActOnFinishFullExpr(Inc.get(), /*DiscardedValue*/ false); 9261 if (!Inc.isUsable()) 9262 return 0; 9263 9264 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST). 9265 // Used for directives with static scheduling. 9266 // In combined construct, add combined version that use CombLB and CombUB 9267 // base variables for the update 9268 ExprResult NextLB, NextUB, CombNextLB, CombNextUB; 9269 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || 9270 isOpenMPDistributeDirective(DKind) || 9271 isOpenMPLoopTransformationDirective(DKind)) { 9272 // LB + ST 9273 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get()); 9274 if (!NextLB.isUsable()) 9275 return 0; 9276 // LB = LB + ST 9277 NextLB = 9278 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get()); 9279 NextLB = 9280 SemaRef.ActOnFinishFullExpr(NextLB.get(), /*DiscardedValue*/ false); 9281 if (!NextLB.isUsable()) 9282 return 0; 9283 // UB + ST 9284 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get()); 9285 if (!NextUB.isUsable()) 9286 return 0; 9287 // UB = UB + ST 9288 NextUB = 9289 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get()); 9290 NextUB = 9291 SemaRef.ActOnFinishFullExpr(NextUB.get(), /*DiscardedValue*/ false); 9292 if (!NextUB.isUsable()) 9293 return 0; 9294 if (isOpenMPLoopBoundSharingDirective(DKind)) { 9295 CombNextLB = 9296 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get()); 9297 if (!NextLB.isUsable()) 9298 return 0; 9299 // LB = LB + ST 9300 CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(), 9301 CombNextLB.get()); 9302 CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get(), 9303 /*DiscardedValue*/ false); 9304 if (!CombNextLB.isUsable()) 9305 return 0; 9306 // UB + ST 9307 CombNextUB = 9308 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get()); 9309 if (!CombNextUB.isUsable()) 9310 return 0; 9311 // UB = UB + ST 9312 CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(), 9313 CombNextUB.get()); 9314 CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get(), 9315 /*DiscardedValue*/ false); 9316 if (!CombNextUB.isUsable()) 9317 return 0; 9318 } 9319 } 9320 9321 // Create increment expression for distribute loop when combined in a same 9322 // directive with for as IV = IV + ST; ensure upper bound expression based 9323 // on PrevUB instead of NumIterations - used to implement 'for' when found 9324 // in combination with 'distribute', like in 'distribute parallel for' 9325 SourceLocation DistIncLoc = AStmt->getBeginLoc(); 9326 ExprResult DistCond, DistInc, PrevEUB, ParForInDistCond; 9327 if (isOpenMPLoopBoundSharingDirective(DKind)) { 9328 DistCond = SemaRef.BuildBinOp( 9329 CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, IV.get(), BoundUB); 9330 assert(DistCond.isUsable() && "distribute cond expr was not built"); 9331 9332 DistInc = 9333 SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get()); 9334 assert(DistInc.isUsable() && "distribute inc expr was not built"); 9335 DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(), 9336 DistInc.get()); 9337 DistInc = 9338 SemaRef.ActOnFinishFullExpr(DistInc.get(), /*DiscardedValue*/ false); 9339 assert(DistInc.isUsable() && "distribute inc expr was not built"); 9340 9341 // Build expression: UB = min(UB, prevUB) for #for in composite or combined 9342 // construct 9343 SourceLocation DistEUBLoc = AStmt->getBeginLoc(); 9344 ExprResult IsUBGreater = 9345 SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, UB.get(), PrevUB.get()); 9346 ExprResult CondOp = SemaRef.ActOnConditionalOp( 9347 DistEUBLoc, DistEUBLoc, IsUBGreater.get(), PrevUB.get(), UB.get()); 9348 PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(), 9349 CondOp.get()); 9350 PrevEUB = 9351 SemaRef.ActOnFinishFullExpr(PrevEUB.get(), /*DiscardedValue*/ false); 9352 9353 // Build IV <= PrevUB or IV < PrevUB + 1 for unsigned IV to be used in 9354 // parallel for is in combination with a distribute directive with 9355 // schedule(static, 1) 9356 Expr *BoundPrevUB = PrevUB.get(); 9357 if (UseStrictCompare) { 9358 BoundPrevUB = 9359 SemaRef 9360 .BuildBinOp( 9361 CurScope, CondLoc, BO_Add, BoundPrevUB, 9362 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()) 9363 .get(); 9364 BoundPrevUB = 9365 SemaRef.ActOnFinishFullExpr(BoundPrevUB, /*DiscardedValue*/ false) 9366 .get(); 9367 } 9368 ParForInDistCond = 9369 SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, 9370 IV.get(), BoundPrevUB); 9371 } 9372 9373 // Build updates and final values of the loop counters. 9374 bool HasErrors = false; 9375 Built.Counters.resize(NestedLoopCount); 9376 Built.Inits.resize(NestedLoopCount); 9377 Built.Updates.resize(NestedLoopCount); 9378 Built.Finals.resize(NestedLoopCount); 9379 Built.DependentCounters.resize(NestedLoopCount); 9380 Built.DependentInits.resize(NestedLoopCount); 9381 Built.FinalsConditions.resize(NestedLoopCount); 9382 { 9383 // We implement the following algorithm for obtaining the 9384 // original loop iteration variable values based on the 9385 // value of the collapsed loop iteration variable IV. 9386 // 9387 // Let n+1 be the number of collapsed loops in the nest. 9388 // Iteration variables (I0, I1, .... In) 9389 // Iteration counts (N0, N1, ... Nn) 9390 // 9391 // Acc = IV; 9392 // 9393 // To compute Ik for loop k, 0 <= k <= n, generate: 9394 // Prod = N(k+1) * N(k+2) * ... * Nn; 9395 // Ik = Acc / Prod; 9396 // Acc -= Ik * Prod; 9397 // 9398 ExprResult Acc = IV; 9399 for (unsigned int Cnt = 0; Cnt < NestedLoopCount; ++Cnt) { 9400 LoopIterationSpace &IS = IterSpaces[Cnt]; 9401 SourceLocation UpdLoc = IS.IncSrcRange.getBegin(); 9402 ExprResult Iter; 9403 9404 // Compute prod 9405 ExprResult Prod = 9406 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(); 9407 for (unsigned int K = Cnt+1; K < NestedLoopCount; ++K) 9408 Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Prod.get(), 9409 IterSpaces[K].NumIterations); 9410 9411 // Iter = Acc / Prod 9412 // If there is at least one more inner loop to avoid 9413 // multiplication by 1. 9414 if (Cnt + 1 < NestedLoopCount) 9415 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, 9416 Acc.get(), Prod.get()); 9417 else 9418 Iter = Acc; 9419 if (!Iter.isUsable()) { 9420 HasErrors = true; 9421 break; 9422 } 9423 9424 // Update Acc: 9425 // Acc -= Iter * Prod 9426 // Check if there is at least one more inner loop to avoid 9427 // multiplication by 1. 9428 if (Cnt + 1 < NestedLoopCount) 9429 Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, 9430 Iter.get(), Prod.get()); 9431 else 9432 Prod = Iter; 9433 Acc = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Sub, 9434 Acc.get(), Prod.get()); 9435 9436 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step 9437 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()); 9438 DeclRefExpr *CounterVar = buildDeclRefExpr( 9439 SemaRef, VD, IS.CounterVar->getType(), IS.CounterVar->getExprLoc(), 9440 /*RefersToCapture=*/true); 9441 ExprResult Init = 9442 buildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar, 9443 IS.CounterInit, IS.IsNonRectangularLB, Captures); 9444 if (!Init.isUsable()) { 9445 HasErrors = true; 9446 break; 9447 } 9448 ExprResult Update = buildCounterUpdate( 9449 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter, 9450 IS.CounterStep, IS.Subtract, IS.IsNonRectangularLB, &Captures); 9451 if (!Update.isUsable()) { 9452 HasErrors = true; 9453 break; 9454 } 9455 9456 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step 9457 ExprResult Final = 9458 buildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar, 9459 IS.CounterInit, IS.NumIterations, IS.CounterStep, 9460 IS.Subtract, IS.IsNonRectangularLB, &Captures); 9461 if (!Final.isUsable()) { 9462 HasErrors = true; 9463 break; 9464 } 9465 9466 if (!Update.isUsable() || !Final.isUsable()) { 9467 HasErrors = true; 9468 break; 9469 } 9470 // Save results 9471 Built.Counters[Cnt] = IS.CounterVar; 9472 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar; 9473 Built.Inits[Cnt] = Init.get(); 9474 Built.Updates[Cnt] = Update.get(); 9475 Built.Finals[Cnt] = Final.get(); 9476 Built.DependentCounters[Cnt] = nullptr; 9477 Built.DependentInits[Cnt] = nullptr; 9478 Built.FinalsConditions[Cnt] = nullptr; 9479 if (IS.IsNonRectangularLB || IS.IsNonRectangularUB) { 9480 Built.DependentCounters[Cnt] = 9481 Built.Counters[NestedLoopCount - 1 - IS.LoopDependentIdx]; 9482 Built.DependentInits[Cnt] = 9483 Built.Inits[NestedLoopCount - 1 - IS.LoopDependentIdx]; 9484 Built.FinalsConditions[Cnt] = IS.FinalCondition; 9485 } 9486 } 9487 } 9488 9489 if (HasErrors) 9490 return 0; 9491 9492 // Save results 9493 Built.IterationVarRef = IV.get(); 9494 Built.LastIteration = LastIteration.get(); 9495 Built.NumIterations = NumIterations.get(); 9496 Built.CalcLastIteration = SemaRef 9497 .ActOnFinishFullExpr(CalcLastIteration.get(), 9498 /*DiscardedValue=*/false) 9499 .get(); 9500 Built.PreCond = PreCond.get(); 9501 Built.PreInits = buildPreInits(C, Captures); 9502 Built.Cond = Cond.get(); 9503 Built.Init = Init.get(); 9504 Built.Inc = Inc.get(); 9505 Built.LB = LB.get(); 9506 Built.UB = UB.get(); 9507 Built.IL = IL.get(); 9508 Built.ST = ST.get(); 9509 Built.EUB = EUB.get(); 9510 Built.NLB = NextLB.get(); 9511 Built.NUB = NextUB.get(); 9512 Built.PrevLB = PrevLB.get(); 9513 Built.PrevUB = PrevUB.get(); 9514 Built.DistInc = DistInc.get(); 9515 Built.PrevEUB = PrevEUB.get(); 9516 Built.DistCombinedFields.LB = CombLB.get(); 9517 Built.DistCombinedFields.UB = CombUB.get(); 9518 Built.DistCombinedFields.EUB = CombEUB.get(); 9519 Built.DistCombinedFields.Init = CombInit.get(); 9520 Built.DistCombinedFields.Cond = CombCond.get(); 9521 Built.DistCombinedFields.NLB = CombNextLB.get(); 9522 Built.DistCombinedFields.NUB = CombNextUB.get(); 9523 Built.DistCombinedFields.DistCond = CombDistCond.get(); 9524 Built.DistCombinedFields.ParForInDistCond = ParForInDistCond.get(); 9525 9526 return NestedLoopCount; 9527 } 9528 9529 static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) { 9530 auto CollapseClauses = 9531 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses); 9532 if (CollapseClauses.begin() != CollapseClauses.end()) 9533 return (*CollapseClauses.begin())->getNumForLoops(); 9534 return nullptr; 9535 } 9536 9537 static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) { 9538 auto OrderedClauses = 9539 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses); 9540 if (OrderedClauses.begin() != OrderedClauses.end()) 9541 return (*OrderedClauses.begin())->getNumForLoops(); 9542 return nullptr; 9543 } 9544 9545 static bool checkSimdlenSafelenSpecified(Sema &S, 9546 const ArrayRef<OMPClause *> Clauses) { 9547 const OMPSafelenClause *Safelen = nullptr; 9548 const OMPSimdlenClause *Simdlen = nullptr; 9549 9550 for (const OMPClause *Clause : Clauses) { 9551 if (Clause->getClauseKind() == OMPC_safelen) 9552 Safelen = cast<OMPSafelenClause>(Clause); 9553 else if (Clause->getClauseKind() == OMPC_simdlen) 9554 Simdlen = cast<OMPSimdlenClause>(Clause); 9555 if (Safelen && Simdlen) 9556 break; 9557 } 9558 9559 if (Simdlen && Safelen) { 9560 const Expr *SimdlenLength = Simdlen->getSimdlen(); 9561 const Expr *SafelenLength = Safelen->getSafelen(); 9562 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() || 9563 SimdlenLength->isInstantiationDependent() || 9564 SimdlenLength->containsUnexpandedParameterPack()) 9565 return false; 9566 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() || 9567 SafelenLength->isInstantiationDependent() || 9568 SafelenLength->containsUnexpandedParameterPack()) 9569 return false; 9570 Expr::EvalResult SimdlenResult, SafelenResult; 9571 SimdlenLength->EvaluateAsInt(SimdlenResult, S.Context); 9572 SafelenLength->EvaluateAsInt(SafelenResult, S.Context); 9573 llvm::APSInt SimdlenRes = SimdlenResult.Val.getInt(); 9574 llvm::APSInt SafelenRes = SafelenResult.Val.getInt(); 9575 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions] 9576 // If both simdlen and safelen clauses are specified, the value of the 9577 // simdlen parameter must be less than or equal to the value of the safelen 9578 // parameter. 9579 if (SimdlenRes > SafelenRes) { 9580 S.Diag(SimdlenLength->getExprLoc(), 9581 diag::err_omp_wrong_simdlen_safelen_values) 9582 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange(); 9583 return true; 9584 } 9585 } 9586 return false; 9587 } 9588 9589 StmtResult 9590 Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt, 9591 SourceLocation StartLoc, SourceLocation EndLoc, 9592 VarsWithInheritedDSAType &VarsWithImplicitDSA) { 9593 if (!AStmt) 9594 return StmtError(); 9595 9596 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9597 OMPLoopBasedDirective::HelperExprs B; 9598 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 9599 // define the nested loops number. 9600 unsigned NestedLoopCount = checkOpenMPLoop( 9601 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), 9602 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 9603 if (NestedLoopCount == 0) 9604 return StmtError(); 9605 9606 assert((CurContext->isDependentContext() || B.builtAll()) && 9607 "omp simd loop exprs were not built"); 9608 9609 if (!CurContext->isDependentContext()) { 9610 // Finalize the clauses that need pre-built expressions for CodeGen. 9611 for (OMPClause *C : Clauses) { 9612 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 9613 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 9614 B.NumIterations, *this, CurScope, 9615 DSAStack)) 9616 return StmtError(); 9617 } 9618 } 9619 9620 if (checkSimdlenSafelenSpecified(*this, Clauses)) 9621 return StmtError(); 9622 9623 setFunctionHasBranchProtectedScope(); 9624 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 9625 Clauses, AStmt, B); 9626 } 9627 9628 StmtResult 9629 Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt, 9630 SourceLocation StartLoc, SourceLocation EndLoc, 9631 VarsWithInheritedDSAType &VarsWithImplicitDSA) { 9632 if (!AStmt) 9633 return StmtError(); 9634 9635 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9636 OMPLoopBasedDirective::HelperExprs B; 9637 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 9638 // define the nested loops number. 9639 unsigned NestedLoopCount = checkOpenMPLoop( 9640 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), 9641 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 9642 if (NestedLoopCount == 0) 9643 return StmtError(); 9644 9645 assert((CurContext->isDependentContext() || B.builtAll()) && 9646 "omp for loop exprs were not built"); 9647 9648 if (!CurContext->isDependentContext()) { 9649 // Finalize the clauses that need pre-built expressions for CodeGen. 9650 for (OMPClause *C : Clauses) { 9651 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 9652 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 9653 B.NumIterations, *this, CurScope, 9654 DSAStack)) 9655 return StmtError(); 9656 } 9657 } 9658 9659 setFunctionHasBranchProtectedScope(); 9660 return OMPForDirective::Create( 9661 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 9662 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 9663 } 9664 9665 StmtResult Sema::ActOnOpenMPForSimdDirective( 9666 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 9667 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 9668 if (!AStmt) 9669 return StmtError(); 9670 9671 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9672 OMPLoopBasedDirective::HelperExprs B; 9673 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 9674 // define the nested loops number. 9675 unsigned NestedLoopCount = 9676 checkOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses), 9677 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 9678 VarsWithImplicitDSA, B); 9679 if (NestedLoopCount == 0) 9680 return StmtError(); 9681 9682 assert((CurContext->isDependentContext() || B.builtAll()) && 9683 "omp for simd loop exprs were not built"); 9684 9685 if (!CurContext->isDependentContext()) { 9686 // Finalize the clauses that need pre-built expressions for CodeGen. 9687 for (OMPClause *C : Clauses) { 9688 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 9689 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 9690 B.NumIterations, *this, CurScope, 9691 DSAStack)) 9692 return StmtError(); 9693 } 9694 } 9695 9696 if (checkSimdlenSafelenSpecified(*this, Clauses)) 9697 return StmtError(); 9698 9699 setFunctionHasBranchProtectedScope(); 9700 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 9701 Clauses, AStmt, B); 9702 } 9703 9704 StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses, 9705 Stmt *AStmt, 9706 SourceLocation StartLoc, 9707 SourceLocation EndLoc) { 9708 if (!AStmt) 9709 return StmtError(); 9710 9711 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9712 auto BaseStmt = AStmt; 9713 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 9714 BaseStmt = CS->getCapturedStmt(); 9715 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 9716 auto S = C->children(); 9717 if (S.begin() == S.end()) 9718 return StmtError(); 9719 // All associated statements must be '#pragma omp section' except for 9720 // the first one. 9721 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) { 9722 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 9723 if (SectionStmt) 9724 Diag(SectionStmt->getBeginLoc(), 9725 diag::err_omp_sections_substmt_not_section); 9726 return StmtError(); 9727 } 9728 cast<OMPSectionDirective>(SectionStmt) 9729 ->setHasCancel(DSAStack->isCancelRegion()); 9730 } 9731 } else { 9732 Diag(AStmt->getBeginLoc(), diag::err_omp_sections_not_compound_stmt); 9733 return StmtError(); 9734 } 9735 9736 setFunctionHasBranchProtectedScope(); 9737 9738 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 9739 DSAStack->getTaskgroupReductionRef(), 9740 DSAStack->isCancelRegion()); 9741 } 9742 9743 StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt, 9744 SourceLocation StartLoc, 9745 SourceLocation EndLoc) { 9746 if (!AStmt) 9747 return StmtError(); 9748 9749 setFunctionHasBranchProtectedScope(); 9750 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion()); 9751 9752 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt, 9753 DSAStack->isCancelRegion()); 9754 } 9755 9756 StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses, 9757 Stmt *AStmt, 9758 SourceLocation StartLoc, 9759 SourceLocation EndLoc) { 9760 if (!AStmt) 9761 return StmtError(); 9762 9763 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9764 9765 setFunctionHasBranchProtectedScope(); 9766 9767 // OpenMP [2.7.3, single Construct, Restrictions] 9768 // The copyprivate clause must not be used with the nowait clause. 9769 const OMPClause *Nowait = nullptr; 9770 const OMPClause *Copyprivate = nullptr; 9771 for (const OMPClause *Clause : Clauses) { 9772 if (Clause->getClauseKind() == OMPC_nowait) 9773 Nowait = Clause; 9774 else if (Clause->getClauseKind() == OMPC_copyprivate) 9775 Copyprivate = Clause; 9776 if (Copyprivate && Nowait) { 9777 Diag(Copyprivate->getBeginLoc(), 9778 diag::err_omp_single_copyprivate_with_nowait); 9779 Diag(Nowait->getBeginLoc(), diag::note_omp_nowait_clause_here); 9780 return StmtError(); 9781 } 9782 } 9783 9784 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 9785 } 9786 9787 StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt, 9788 SourceLocation StartLoc, 9789 SourceLocation EndLoc) { 9790 if (!AStmt) 9791 return StmtError(); 9792 9793 setFunctionHasBranchProtectedScope(); 9794 9795 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt); 9796 } 9797 9798 StmtResult Sema::ActOnOpenMPCriticalDirective( 9799 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses, 9800 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { 9801 if (!AStmt) 9802 return StmtError(); 9803 9804 bool ErrorFound = false; 9805 llvm::APSInt Hint; 9806 SourceLocation HintLoc; 9807 bool DependentHint = false; 9808 for (const OMPClause *C : Clauses) { 9809 if (C->getClauseKind() == OMPC_hint) { 9810 if (!DirName.getName()) { 9811 Diag(C->getBeginLoc(), diag::err_omp_hint_clause_no_name); 9812 ErrorFound = true; 9813 } 9814 Expr *E = cast<OMPHintClause>(C)->getHint(); 9815 if (E->isTypeDependent() || E->isValueDependent() || 9816 E->isInstantiationDependent()) { 9817 DependentHint = true; 9818 } else { 9819 Hint = E->EvaluateKnownConstInt(Context); 9820 HintLoc = C->getBeginLoc(); 9821 } 9822 } 9823 } 9824 if (ErrorFound) 9825 return StmtError(); 9826 const auto Pair = DSAStack->getCriticalWithHint(DirName); 9827 if (Pair.first && DirName.getName() && !DependentHint) { 9828 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) { 9829 Diag(StartLoc, diag::err_omp_critical_with_hint); 9830 if (HintLoc.isValid()) 9831 Diag(HintLoc, diag::note_omp_critical_hint_here) 9832 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false); 9833 else 9834 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0; 9835 if (const auto *C = Pair.first->getSingleClause<OMPHintClause>()) { 9836 Diag(C->getBeginLoc(), diag::note_omp_critical_hint_here) 9837 << 1 9838 << C->getHint()->EvaluateKnownConstInt(Context).toString( 9839 /*Radix=*/10, /*Signed=*/false); 9840 } else { 9841 Diag(Pair.first->getBeginLoc(), diag::note_omp_critical_no_hint) << 1; 9842 } 9843 } 9844 } 9845 9846 setFunctionHasBranchProtectedScope(); 9847 9848 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc, 9849 Clauses, AStmt); 9850 if (!Pair.first && DirName.getName() && !DependentHint) 9851 DSAStack->addCriticalWithHint(Dir, Hint); 9852 return Dir; 9853 } 9854 9855 StmtResult Sema::ActOnOpenMPParallelForDirective( 9856 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 9857 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 9858 if (!AStmt) 9859 return StmtError(); 9860 9861 auto *CS = cast<CapturedStmt>(AStmt); 9862 // 1.2.2 OpenMP Language Terminology 9863 // Structured block - An executable statement with a single entry at the 9864 // top and a single exit at the bottom. 9865 // The point of exit cannot be a branch out of the structured block. 9866 // longjmp() and throw() must not violate the entry/exit criteria. 9867 CS->getCapturedDecl()->setNothrow(); 9868 9869 OMPLoopBasedDirective::HelperExprs B; 9870 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 9871 // define the nested loops number. 9872 unsigned NestedLoopCount = 9873 checkOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses), 9874 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 9875 VarsWithImplicitDSA, B); 9876 if (NestedLoopCount == 0) 9877 return StmtError(); 9878 9879 assert((CurContext->isDependentContext() || B.builtAll()) && 9880 "omp parallel for loop exprs were not built"); 9881 9882 if (!CurContext->isDependentContext()) { 9883 // Finalize the clauses that need pre-built expressions for CodeGen. 9884 for (OMPClause *C : Clauses) { 9885 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 9886 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 9887 B.NumIterations, *this, CurScope, 9888 DSAStack)) 9889 return StmtError(); 9890 } 9891 } 9892 9893 setFunctionHasBranchProtectedScope(); 9894 return OMPParallelForDirective::Create( 9895 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 9896 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 9897 } 9898 9899 StmtResult Sema::ActOnOpenMPParallelForSimdDirective( 9900 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 9901 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 9902 if (!AStmt) 9903 return StmtError(); 9904 9905 auto *CS = cast<CapturedStmt>(AStmt); 9906 // 1.2.2 OpenMP Language Terminology 9907 // Structured block - An executable statement with a single entry at the 9908 // top and a single exit at the bottom. 9909 // The point of exit cannot be a branch out of the structured block. 9910 // longjmp() and throw() must not violate the entry/exit criteria. 9911 CS->getCapturedDecl()->setNothrow(); 9912 9913 OMPLoopBasedDirective::HelperExprs B; 9914 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 9915 // define the nested loops number. 9916 unsigned NestedLoopCount = 9917 checkOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses), 9918 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 9919 VarsWithImplicitDSA, B); 9920 if (NestedLoopCount == 0) 9921 return StmtError(); 9922 9923 if (!CurContext->isDependentContext()) { 9924 // Finalize the clauses that need pre-built expressions for CodeGen. 9925 for (OMPClause *C : Clauses) { 9926 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 9927 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 9928 B.NumIterations, *this, CurScope, 9929 DSAStack)) 9930 return StmtError(); 9931 } 9932 } 9933 9934 if (checkSimdlenSafelenSpecified(*this, Clauses)) 9935 return StmtError(); 9936 9937 setFunctionHasBranchProtectedScope(); 9938 return OMPParallelForSimdDirective::Create( 9939 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 9940 } 9941 9942 StmtResult 9943 Sema::ActOnOpenMPParallelMasterDirective(ArrayRef<OMPClause *> Clauses, 9944 Stmt *AStmt, SourceLocation StartLoc, 9945 SourceLocation EndLoc) { 9946 if (!AStmt) 9947 return StmtError(); 9948 9949 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9950 auto *CS = cast<CapturedStmt>(AStmt); 9951 // 1.2.2 OpenMP Language Terminology 9952 // Structured block - An executable statement with a single entry at the 9953 // top and a single exit at the bottom. 9954 // The point of exit cannot be a branch out of the structured block. 9955 // longjmp() and throw() must not violate the entry/exit criteria. 9956 CS->getCapturedDecl()->setNothrow(); 9957 9958 setFunctionHasBranchProtectedScope(); 9959 9960 return OMPParallelMasterDirective::Create( 9961 Context, StartLoc, EndLoc, Clauses, AStmt, 9962 DSAStack->getTaskgroupReductionRef()); 9963 } 9964 9965 StmtResult 9966 Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses, 9967 Stmt *AStmt, SourceLocation StartLoc, 9968 SourceLocation EndLoc) { 9969 if (!AStmt) 9970 return StmtError(); 9971 9972 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9973 auto BaseStmt = AStmt; 9974 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 9975 BaseStmt = CS->getCapturedStmt(); 9976 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 9977 auto S = C->children(); 9978 if (S.begin() == S.end()) 9979 return StmtError(); 9980 // All associated statements must be '#pragma omp section' except for 9981 // the first one. 9982 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) { 9983 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 9984 if (SectionStmt) 9985 Diag(SectionStmt->getBeginLoc(), 9986 diag::err_omp_parallel_sections_substmt_not_section); 9987 return StmtError(); 9988 } 9989 cast<OMPSectionDirective>(SectionStmt) 9990 ->setHasCancel(DSAStack->isCancelRegion()); 9991 } 9992 } else { 9993 Diag(AStmt->getBeginLoc(), 9994 diag::err_omp_parallel_sections_not_compound_stmt); 9995 return StmtError(); 9996 } 9997 9998 setFunctionHasBranchProtectedScope(); 9999 10000 return OMPParallelSectionsDirective::Create( 10001 Context, StartLoc, EndLoc, Clauses, AStmt, 10002 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 10003 } 10004 10005 /// detach and mergeable clauses are mutially exclusive, check for it. 10006 static bool checkDetachMergeableClauses(Sema &S, 10007 ArrayRef<OMPClause *> Clauses) { 10008 const OMPClause *PrevClause = nullptr; 10009 bool ErrorFound = false; 10010 for (const OMPClause *C : Clauses) { 10011 if (C->getClauseKind() == OMPC_detach || 10012 C->getClauseKind() == OMPC_mergeable) { 10013 if (!PrevClause) { 10014 PrevClause = C; 10015 } else if (PrevClause->getClauseKind() != C->getClauseKind()) { 10016 S.Diag(C->getBeginLoc(), diag::err_omp_clauses_mutually_exclusive) 10017 << getOpenMPClauseName(C->getClauseKind()) 10018 << getOpenMPClauseName(PrevClause->getClauseKind()); 10019 S.Diag(PrevClause->getBeginLoc(), diag::note_omp_previous_clause) 10020 << getOpenMPClauseName(PrevClause->getClauseKind()); 10021 ErrorFound = true; 10022 } 10023 } 10024 } 10025 return ErrorFound; 10026 } 10027 10028 StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses, 10029 Stmt *AStmt, SourceLocation StartLoc, 10030 SourceLocation EndLoc) { 10031 if (!AStmt) 10032 return StmtError(); 10033 10034 // OpenMP 5.0, 2.10.1 task Construct 10035 // If a detach clause appears on the directive, then a mergeable clause cannot 10036 // appear on the same directive. 10037 if (checkDetachMergeableClauses(*this, Clauses)) 10038 return StmtError(); 10039 10040 auto *CS = cast<CapturedStmt>(AStmt); 10041 // 1.2.2 OpenMP Language Terminology 10042 // Structured block - An executable statement with a single entry at the 10043 // top and a single exit at the bottom. 10044 // The point of exit cannot be a branch out of the structured block. 10045 // longjmp() and throw() must not violate the entry/exit criteria. 10046 CS->getCapturedDecl()->setNothrow(); 10047 10048 setFunctionHasBranchProtectedScope(); 10049 10050 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 10051 DSAStack->isCancelRegion()); 10052 } 10053 10054 StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc, 10055 SourceLocation EndLoc) { 10056 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc); 10057 } 10058 10059 StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc, 10060 SourceLocation EndLoc) { 10061 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc); 10062 } 10063 10064 StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc, 10065 SourceLocation EndLoc) { 10066 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc); 10067 } 10068 10069 StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses, 10070 Stmt *AStmt, 10071 SourceLocation StartLoc, 10072 SourceLocation EndLoc) { 10073 if (!AStmt) 10074 return StmtError(); 10075 10076 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10077 10078 setFunctionHasBranchProtectedScope(); 10079 10080 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses, 10081 AStmt, 10082 DSAStack->getTaskgroupReductionRef()); 10083 } 10084 10085 StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses, 10086 SourceLocation StartLoc, 10087 SourceLocation EndLoc) { 10088 OMPFlushClause *FC = nullptr; 10089 OMPClause *OrderClause = nullptr; 10090 for (OMPClause *C : Clauses) { 10091 if (C->getClauseKind() == OMPC_flush) 10092 FC = cast<OMPFlushClause>(C); 10093 else 10094 OrderClause = C; 10095 } 10096 OpenMPClauseKind MemOrderKind = OMPC_unknown; 10097 SourceLocation MemOrderLoc; 10098 for (const OMPClause *C : Clauses) { 10099 if (C->getClauseKind() == OMPC_acq_rel || 10100 C->getClauseKind() == OMPC_acquire || 10101 C->getClauseKind() == OMPC_release) { 10102 if (MemOrderKind != OMPC_unknown) { 10103 Diag(C->getBeginLoc(), diag::err_omp_several_mem_order_clauses) 10104 << getOpenMPDirectiveName(OMPD_flush) << 1 10105 << SourceRange(C->getBeginLoc(), C->getEndLoc()); 10106 Diag(MemOrderLoc, diag::note_omp_previous_mem_order_clause) 10107 << getOpenMPClauseName(MemOrderKind); 10108 } else { 10109 MemOrderKind = C->getClauseKind(); 10110 MemOrderLoc = C->getBeginLoc(); 10111 } 10112 } 10113 } 10114 if (FC && OrderClause) { 10115 Diag(FC->getLParenLoc(), diag::err_omp_flush_order_clause_and_list) 10116 << getOpenMPClauseName(OrderClause->getClauseKind()); 10117 Diag(OrderClause->getBeginLoc(), diag::note_omp_flush_order_clause_here) 10118 << getOpenMPClauseName(OrderClause->getClauseKind()); 10119 return StmtError(); 10120 } 10121 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses); 10122 } 10123 10124 StmtResult Sema::ActOnOpenMPDepobjDirective(ArrayRef<OMPClause *> Clauses, 10125 SourceLocation StartLoc, 10126 SourceLocation EndLoc) { 10127 if (Clauses.empty()) { 10128 Diag(StartLoc, diag::err_omp_depobj_expected); 10129 return StmtError(); 10130 } else if (Clauses[0]->getClauseKind() != OMPC_depobj) { 10131 Diag(Clauses[0]->getBeginLoc(), diag::err_omp_depobj_expected); 10132 return StmtError(); 10133 } 10134 // Only depobj expression and another single clause is allowed. 10135 if (Clauses.size() > 2) { 10136 Diag(Clauses[2]->getBeginLoc(), 10137 diag::err_omp_depobj_single_clause_expected); 10138 return StmtError(); 10139 } else if (Clauses.size() < 1) { 10140 Diag(Clauses[0]->getEndLoc(), diag::err_omp_depobj_single_clause_expected); 10141 return StmtError(); 10142 } 10143 return OMPDepobjDirective::Create(Context, StartLoc, EndLoc, Clauses); 10144 } 10145 10146 StmtResult Sema::ActOnOpenMPScanDirective(ArrayRef<OMPClause *> Clauses, 10147 SourceLocation StartLoc, 10148 SourceLocation EndLoc) { 10149 // Check that exactly one clause is specified. 10150 if (Clauses.size() != 1) { 10151 Diag(Clauses.empty() ? EndLoc : Clauses[1]->getBeginLoc(), 10152 diag::err_omp_scan_single_clause_expected); 10153 return StmtError(); 10154 } 10155 // Check that scan directive is used in the scopeof the OpenMP loop body. 10156 if (Scope *S = DSAStack->getCurScope()) { 10157 Scope *ParentS = S->getParent(); 10158 if (!ParentS || ParentS->getParent() != ParentS->getBreakParent() || 10159 !ParentS->getBreakParent()->isOpenMPLoopScope()) 10160 return StmtError(Diag(StartLoc, diag::err_omp_orphaned_device_directive) 10161 << getOpenMPDirectiveName(OMPD_scan) << 5); 10162 } 10163 // Check that only one instance of scan directives is used in the same outer 10164 // region. 10165 if (DSAStack->doesParentHasScanDirective()) { 10166 Diag(StartLoc, diag::err_omp_several_directives_in_region) << "scan"; 10167 Diag(DSAStack->getParentScanDirectiveLoc(), 10168 diag::note_omp_previous_directive) 10169 << "scan"; 10170 return StmtError(); 10171 } 10172 DSAStack->setParentHasScanDirective(StartLoc); 10173 return OMPScanDirective::Create(Context, StartLoc, EndLoc, Clauses); 10174 } 10175 10176 StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses, 10177 Stmt *AStmt, 10178 SourceLocation StartLoc, 10179 SourceLocation EndLoc) { 10180 const OMPClause *DependFound = nullptr; 10181 const OMPClause *DependSourceClause = nullptr; 10182 const OMPClause *DependSinkClause = nullptr; 10183 bool ErrorFound = false; 10184 const OMPThreadsClause *TC = nullptr; 10185 const OMPSIMDClause *SC = nullptr; 10186 for (const OMPClause *C : Clauses) { 10187 if (auto *DC = dyn_cast<OMPDependClause>(C)) { 10188 DependFound = C; 10189 if (DC->getDependencyKind() == OMPC_DEPEND_source) { 10190 if (DependSourceClause) { 10191 Diag(C->getBeginLoc(), diag::err_omp_more_one_clause) 10192 << getOpenMPDirectiveName(OMPD_ordered) 10193 << getOpenMPClauseName(OMPC_depend) << 2; 10194 ErrorFound = true; 10195 } else { 10196 DependSourceClause = C; 10197 } 10198 if (DependSinkClause) { 10199 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed) 10200 << 0; 10201 ErrorFound = true; 10202 } 10203 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) { 10204 if (DependSourceClause) { 10205 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed) 10206 << 1; 10207 ErrorFound = true; 10208 } 10209 DependSinkClause = C; 10210 } 10211 } else if (C->getClauseKind() == OMPC_threads) { 10212 TC = cast<OMPThreadsClause>(C); 10213 } else if (C->getClauseKind() == OMPC_simd) { 10214 SC = cast<OMPSIMDClause>(C); 10215 } 10216 } 10217 if (!ErrorFound && !SC && 10218 isOpenMPSimdDirective(DSAStack->getParentDirective())) { 10219 // OpenMP [2.8.1,simd Construct, Restrictions] 10220 // An ordered construct with the simd clause is the only OpenMP construct 10221 // that can appear in the simd region. 10222 Diag(StartLoc, diag::err_omp_prohibited_region_simd) 10223 << (LangOpts.OpenMP >= 50 ? 1 : 0); 10224 ErrorFound = true; 10225 } else if (DependFound && (TC || SC)) { 10226 Diag(DependFound->getBeginLoc(), diag::err_omp_depend_clause_thread_simd) 10227 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind()); 10228 ErrorFound = true; 10229 } else if (DependFound && !DSAStack->getParentOrderedRegionParam().first) { 10230 Diag(DependFound->getBeginLoc(), 10231 diag::err_omp_ordered_directive_without_param); 10232 ErrorFound = true; 10233 } else if (TC || Clauses.empty()) { 10234 if (const Expr *Param = DSAStack->getParentOrderedRegionParam().first) { 10235 SourceLocation ErrLoc = TC ? TC->getBeginLoc() : StartLoc; 10236 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param) 10237 << (TC != nullptr); 10238 Diag(Param->getBeginLoc(), diag::note_omp_ordered_param) << 1; 10239 ErrorFound = true; 10240 } 10241 } 10242 if ((!AStmt && !DependFound) || ErrorFound) 10243 return StmtError(); 10244 10245 // OpenMP 5.0, 2.17.9, ordered Construct, Restrictions. 10246 // During execution of an iteration of a worksharing-loop or a loop nest 10247 // within a worksharing-loop, simd, or worksharing-loop SIMD region, a thread 10248 // must not execute more than one ordered region corresponding to an ordered 10249 // construct without a depend clause. 10250 if (!DependFound) { 10251 if (DSAStack->doesParentHasOrderedDirective()) { 10252 Diag(StartLoc, diag::err_omp_several_directives_in_region) << "ordered"; 10253 Diag(DSAStack->getParentOrderedDirectiveLoc(), 10254 diag::note_omp_previous_directive) 10255 << "ordered"; 10256 return StmtError(); 10257 } 10258 DSAStack->setParentHasOrderedDirective(StartLoc); 10259 } 10260 10261 if (AStmt) { 10262 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10263 10264 setFunctionHasBranchProtectedScope(); 10265 } 10266 10267 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 10268 } 10269 10270 namespace { 10271 /// Helper class for checking expression in 'omp atomic [update]' 10272 /// construct. 10273 class OpenMPAtomicUpdateChecker { 10274 /// Error results for atomic update expressions. 10275 enum ExprAnalysisErrorCode { 10276 /// A statement is not an expression statement. 10277 NotAnExpression, 10278 /// Expression is not builtin binary or unary operation. 10279 NotABinaryOrUnaryExpression, 10280 /// Unary operation is not post-/pre- increment/decrement operation. 10281 NotAnUnaryIncDecExpression, 10282 /// An expression is not of scalar type. 10283 NotAScalarType, 10284 /// A binary operation is not an assignment operation. 10285 NotAnAssignmentOp, 10286 /// RHS part of the binary operation is not a binary expression. 10287 NotABinaryExpression, 10288 /// RHS part is not additive/multiplicative/shift/biwise binary 10289 /// expression. 10290 NotABinaryOperator, 10291 /// RHS binary operation does not have reference to the updated LHS 10292 /// part. 10293 NotAnUpdateExpression, 10294 /// No errors is found. 10295 NoError 10296 }; 10297 /// Reference to Sema. 10298 Sema &SemaRef; 10299 /// A location for note diagnostics (when error is found). 10300 SourceLocation NoteLoc; 10301 /// 'x' lvalue part of the source atomic expression. 10302 Expr *X; 10303 /// 'expr' rvalue part of the source atomic expression. 10304 Expr *E; 10305 /// Helper expression of the form 10306 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or 10307 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. 10308 Expr *UpdateExpr; 10309 /// Is 'x' a LHS in a RHS part of full update expression. It is 10310 /// important for non-associative operations. 10311 bool IsXLHSInRHSPart; 10312 BinaryOperatorKind Op; 10313 SourceLocation OpLoc; 10314 /// true if the source expression is a postfix unary operation, false 10315 /// if it is a prefix unary operation. 10316 bool IsPostfixUpdate; 10317 10318 public: 10319 OpenMPAtomicUpdateChecker(Sema &SemaRef) 10320 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr), 10321 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {} 10322 /// Check specified statement that it is suitable for 'atomic update' 10323 /// constructs and extract 'x', 'expr' and Operation from the original 10324 /// expression. If DiagId and NoteId == 0, then only check is performed 10325 /// without error notification. 10326 /// \param DiagId Diagnostic which should be emitted if error is found. 10327 /// \param NoteId Diagnostic note for the main error message. 10328 /// \return true if statement is not an update expression, false otherwise. 10329 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0); 10330 /// Return the 'x' lvalue part of the source atomic expression. 10331 Expr *getX() const { return X; } 10332 /// Return the 'expr' rvalue part of the source atomic expression. 10333 Expr *getExpr() const { return E; } 10334 /// Return the update expression used in calculation of the updated 10335 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or 10336 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. 10337 Expr *getUpdateExpr() const { return UpdateExpr; } 10338 /// Return true if 'x' is LHS in RHS part of full update expression, 10339 /// false otherwise. 10340 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; } 10341 10342 /// true if the source expression is a postfix unary operation, false 10343 /// if it is a prefix unary operation. 10344 bool isPostfixUpdate() const { return IsPostfixUpdate; } 10345 10346 private: 10347 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0, 10348 unsigned NoteId = 0); 10349 }; 10350 } // namespace 10351 10352 bool OpenMPAtomicUpdateChecker::checkBinaryOperation( 10353 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) { 10354 ExprAnalysisErrorCode ErrorFound = NoError; 10355 SourceLocation ErrorLoc, NoteLoc; 10356 SourceRange ErrorRange, NoteRange; 10357 // Allowed constructs are: 10358 // x = x binop expr; 10359 // x = expr binop x; 10360 if (AtomicBinOp->getOpcode() == BO_Assign) { 10361 X = AtomicBinOp->getLHS(); 10362 if (const auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>( 10363 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) { 10364 if (AtomicInnerBinOp->isMultiplicativeOp() || 10365 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() || 10366 AtomicInnerBinOp->isBitwiseOp()) { 10367 Op = AtomicInnerBinOp->getOpcode(); 10368 OpLoc = AtomicInnerBinOp->getOperatorLoc(); 10369 Expr *LHS = AtomicInnerBinOp->getLHS(); 10370 Expr *RHS = AtomicInnerBinOp->getRHS(); 10371 llvm::FoldingSetNodeID XId, LHSId, RHSId; 10372 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(), 10373 /*Canonical=*/true); 10374 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(), 10375 /*Canonical=*/true); 10376 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(), 10377 /*Canonical=*/true); 10378 if (XId == LHSId) { 10379 E = RHS; 10380 IsXLHSInRHSPart = true; 10381 } else if (XId == RHSId) { 10382 E = LHS; 10383 IsXLHSInRHSPart = false; 10384 } else { 10385 ErrorLoc = AtomicInnerBinOp->getExprLoc(); 10386 ErrorRange = AtomicInnerBinOp->getSourceRange(); 10387 NoteLoc = X->getExprLoc(); 10388 NoteRange = X->getSourceRange(); 10389 ErrorFound = NotAnUpdateExpression; 10390 } 10391 } else { 10392 ErrorLoc = AtomicInnerBinOp->getExprLoc(); 10393 ErrorRange = AtomicInnerBinOp->getSourceRange(); 10394 NoteLoc = AtomicInnerBinOp->getOperatorLoc(); 10395 NoteRange = SourceRange(NoteLoc, NoteLoc); 10396 ErrorFound = NotABinaryOperator; 10397 } 10398 } else { 10399 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc(); 10400 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange(); 10401 ErrorFound = NotABinaryExpression; 10402 } 10403 } else { 10404 ErrorLoc = AtomicBinOp->getExprLoc(); 10405 ErrorRange = AtomicBinOp->getSourceRange(); 10406 NoteLoc = AtomicBinOp->getOperatorLoc(); 10407 NoteRange = SourceRange(NoteLoc, NoteLoc); 10408 ErrorFound = NotAnAssignmentOp; 10409 } 10410 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { 10411 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; 10412 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; 10413 return true; 10414 } 10415 if (SemaRef.CurContext->isDependentContext()) 10416 E = X = UpdateExpr = nullptr; 10417 return ErrorFound != NoError; 10418 } 10419 10420 bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId, 10421 unsigned NoteId) { 10422 ExprAnalysisErrorCode ErrorFound = NoError; 10423 SourceLocation ErrorLoc, NoteLoc; 10424 SourceRange ErrorRange, NoteRange; 10425 // Allowed constructs are: 10426 // x++; 10427 // x--; 10428 // ++x; 10429 // --x; 10430 // x binop= expr; 10431 // x = x binop expr; 10432 // x = expr binop x; 10433 if (auto *AtomicBody = dyn_cast<Expr>(S)) { 10434 AtomicBody = AtomicBody->IgnoreParenImpCasts(); 10435 if (AtomicBody->getType()->isScalarType() || 10436 AtomicBody->isInstantiationDependent()) { 10437 if (const auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>( 10438 AtomicBody->IgnoreParenImpCasts())) { 10439 // Check for Compound Assignment Operation 10440 Op = BinaryOperator::getOpForCompoundAssignment( 10441 AtomicCompAssignOp->getOpcode()); 10442 OpLoc = AtomicCompAssignOp->getOperatorLoc(); 10443 E = AtomicCompAssignOp->getRHS(); 10444 X = AtomicCompAssignOp->getLHS()->IgnoreParens(); 10445 IsXLHSInRHSPart = true; 10446 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>( 10447 AtomicBody->IgnoreParenImpCasts())) { 10448 // Check for Binary Operation 10449 if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId)) 10450 return true; 10451 } else if (const auto *AtomicUnaryOp = dyn_cast<UnaryOperator>( 10452 AtomicBody->IgnoreParenImpCasts())) { 10453 // Check for Unary Operation 10454 if (AtomicUnaryOp->isIncrementDecrementOp()) { 10455 IsPostfixUpdate = AtomicUnaryOp->isPostfix(); 10456 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub; 10457 OpLoc = AtomicUnaryOp->getOperatorLoc(); 10458 X = AtomicUnaryOp->getSubExpr()->IgnoreParens(); 10459 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get(); 10460 IsXLHSInRHSPart = true; 10461 } else { 10462 ErrorFound = NotAnUnaryIncDecExpression; 10463 ErrorLoc = AtomicUnaryOp->getExprLoc(); 10464 ErrorRange = AtomicUnaryOp->getSourceRange(); 10465 NoteLoc = AtomicUnaryOp->getOperatorLoc(); 10466 NoteRange = SourceRange(NoteLoc, NoteLoc); 10467 } 10468 } else if (!AtomicBody->isInstantiationDependent()) { 10469 ErrorFound = NotABinaryOrUnaryExpression; 10470 NoteLoc = ErrorLoc = AtomicBody->getExprLoc(); 10471 NoteRange = ErrorRange = AtomicBody->getSourceRange(); 10472 } 10473 } else { 10474 ErrorFound = NotAScalarType; 10475 NoteLoc = ErrorLoc = AtomicBody->getBeginLoc(); 10476 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 10477 } 10478 } else { 10479 ErrorFound = NotAnExpression; 10480 NoteLoc = ErrorLoc = S->getBeginLoc(); 10481 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 10482 } 10483 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { 10484 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; 10485 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; 10486 return true; 10487 } 10488 if (SemaRef.CurContext->isDependentContext()) 10489 E = X = UpdateExpr = nullptr; 10490 if (ErrorFound == NoError && E && X) { 10491 // Build an update expression of form 'OpaqueValueExpr(x) binop 10492 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop 10493 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression. 10494 auto *OVEX = new (SemaRef.getASTContext()) 10495 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue); 10496 auto *OVEExpr = new (SemaRef.getASTContext()) 10497 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue); 10498 ExprResult Update = 10499 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr, 10500 IsXLHSInRHSPart ? OVEExpr : OVEX); 10501 if (Update.isInvalid()) 10502 return true; 10503 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(), 10504 Sema::AA_Casting); 10505 if (Update.isInvalid()) 10506 return true; 10507 UpdateExpr = Update.get(); 10508 } 10509 return ErrorFound != NoError; 10510 } 10511 10512 StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses, 10513 Stmt *AStmt, 10514 SourceLocation StartLoc, 10515 SourceLocation EndLoc) { 10516 // Register location of the first atomic directive. 10517 DSAStack->addAtomicDirectiveLoc(StartLoc); 10518 if (!AStmt) 10519 return StmtError(); 10520 10521 // 1.2.2 OpenMP Language Terminology 10522 // Structured block - An executable statement with a single entry at the 10523 // top and a single exit at the bottom. 10524 // The point of exit cannot be a branch out of the structured block. 10525 // longjmp() and throw() must not violate the entry/exit criteria. 10526 OpenMPClauseKind AtomicKind = OMPC_unknown; 10527 SourceLocation AtomicKindLoc; 10528 OpenMPClauseKind MemOrderKind = OMPC_unknown; 10529 SourceLocation MemOrderLoc; 10530 for (const OMPClause *C : Clauses) { 10531 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write || 10532 C->getClauseKind() == OMPC_update || 10533 C->getClauseKind() == OMPC_capture) { 10534 if (AtomicKind != OMPC_unknown) { 10535 Diag(C->getBeginLoc(), diag::err_omp_atomic_several_clauses) 10536 << SourceRange(C->getBeginLoc(), C->getEndLoc()); 10537 Diag(AtomicKindLoc, diag::note_omp_previous_mem_order_clause) 10538 << getOpenMPClauseName(AtomicKind); 10539 } else { 10540 AtomicKind = C->getClauseKind(); 10541 AtomicKindLoc = C->getBeginLoc(); 10542 } 10543 } 10544 if (C->getClauseKind() == OMPC_seq_cst || 10545 C->getClauseKind() == OMPC_acq_rel || 10546 C->getClauseKind() == OMPC_acquire || 10547 C->getClauseKind() == OMPC_release || 10548 C->getClauseKind() == OMPC_relaxed) { 10549 if (MemOrderKind != OMPC_unknown) { 10550 Diag(C->getBeginLoc(), diag::err_omp_several_mem_order_clauses) 10551 << getOpenMPDirectiveName(OMPD_atomic) << 0 10552 << SourceRange(C->getBeginLoc(), C->getEndLoc()); 10553 Diag(MemOrderLoc, diag::note_omp_previous_mem_order_clause) 10554 << getOpenMPClauseName(MemOrderKind); 10555 } else { 10556 MemOrderKind = C->getClauseKind(); 10557 MemOrderLoc = C->getBeginLoc(); 10558 } 10559 } 10560 } 10561 // OpenMP 5.0, 2.17.7 atomic Construct, Restrictions 10562 // If atomic-clause is read then memory-order-clause must not be acq_rel or 10563 // release. 10564 // If atomic-clause is write then memory-order-clause must not be acq_rel or 10565 // acquire. 10566 // If atomic-clause is update or not present then memory-order-clause must not 10567 // be acq_rel or acquire. 10568 if ((AtomicKind == OMPC_read && 10569 (MemOrderKind == OMPC_acq_rel || MemOrderKind == OMPC_release)) || 10570 ((AtomicKind == OMPC_write || AtomicKind == OMPC_update || 10571 AtomicKind == OMPC_unknown) && 10572 (MemOrderKind == OMPC_acq_rel || MemOrderKind == OMPC_acquire))) { 10573 SourceLocation Loc = AtomicKindLoc; 10574 if (AtomicKind == OMPC_unknown) 10575 Loc = StartLoc; 10576 Diag(Loc, diag::err_omp_atomic_incompatible_mem_order_clause) 10577 << getOpenMPClauseName(AtomicKind) 10578 << (AtomicKind == OMPC_unknown ? 1 : 0) 10579 << getOpenMPClauseName(MemOrderKind); 10580 Diag(MemOrderLoc, diag::note_omp_previous_mem_order_clause) 10581 << getOpenMPClauseName(MemOrderKind); 10582 } 10583 10584 Stmt *Body = AStmt; 10585 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body)) 10586 Body = EWC->getSubExpr(); 10587 10588 Expr *X = nullptr; 10589 Expr *V = nullptr; 10590 Expr *E = nullptr; 10591 Expr *UE = nullptr; 10592 bool IsXLHSInRHSPart = false; 10593 bool IsPostfixUpdate = false; 10594 // OpenMP [2.12.6, atomic Construct] 10595 // In the next expressions: 10596 // * x and v (as applicable) are both l-value expressions with scalar type. 10597 // * During the execution of an atomic region, multiple syntactic 10598 // occurrences of x must designate the same storage location. 10599 // * Neither of v and expr (as applicable) may access the storage location 10600 // designated by x. 10601 // * Neither of x and expr (as applicable) may access the storage location 10602 // designated by v. 10603 // * expr is an expression with scalar type. 10604 // * binop is one of +, *, -, /, &, ^, |, <<, or >>. 10605 // * binop, binop=, ++, and -- are not overloaded operators. 10606 // * The expression x binop expr must be numerically equivalent to x binop 10607 // (expr). This requirement is satisfied if the operators in expr have 10608 // precedence greater than binop, or by using parentheses around expr or 10609 // subexpressions of expr. 10610 // * The expression expr binop x must be numerically equivalent to (expr) 10611 // binop x. This requirement is satisfied if the operators in expr have 10612 // precedence equal to or greater than binop, or by using parentheses around 10613 // expr or subexpressions of expr. 10614 // * For forms that allow multiple occurrences of x, the number of times 10615 // that x is evaluated is unspecified. 10616 if (AtomicKind == OMPC_read) { 10617 enum { 10618 NotAnExpression, 10619 NotAnAssignmentOp, 10620 NotAScalarType, 10621 NotAnLValue, 10622 NoError 10623 } ErrorFound = NoError; 10624 SourceLocation ErrorLoc, NoteLoc; 10625 SourceRange ErrorRange, NoteRange; 10626 // If clause is read: 10627 // v = x; 10628 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 10629 const auto *AtomicBinOp = 10630 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 10631 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 10632 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); 10633 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts(); 10634 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && 10635 (V->isInstantiationDependent() || V->getType()->isScalarType())) { 10636 if (!X->isLValue() || !V->isLValue()) { 10637 const Expr *NotLValueExpr = X->isLValue() ? V : X; 10638 ErrorFound = NotAnLValue; 10639 ErrorLoc = AtomicBinOp->getExprLoc(); 10640 ErrorRange = AtomicBinOp->getSourceRange(); 10641 NoteLoc = NotLValueExpr->getExprLoc(); 10642 NoteRange = NotLValueExpr->getSourceRange(); 10643 } 10644 } else if (!X->isInstantiationDependent() || 10645 !V->isInstantiationDependent()) { 10646 const Expr *NotScalarExpr = 10647 (X->isInstantiationDependent() || X->getType()->isScalarType()) 10648 ? V 10649 : X; 10650 ErrorFound = NotAScalarType; 10651 ErrorLoc = AtomicBinOp->getExprLoc(); 10652 ErrorRange = AtomicBinOp->getSourceRange(); 10653 NoteLoc = NotScalarExpr->getExprLoc(); 10654 NoteRange = NotScalarExpr->getSourceRange(); 10655 } 10656 } else if (!AtomicBody->isInstantiationDependent()) { 10657 ErrorFound = NotAnAssignmentOp; 10658 ErrorLoc = AtomicBody->getExprLoc(); 10659 ErrorRange = AtomicBody->getSourceRange(); 10660 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 10661 : AtomicBody->getExprLoc(); 10662 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 10663 : AtomicBody->getSourceRange(); 10664 } 10665 } else { 10666 ErrorFound = NotAnExpression; 10667 NoteLoc = ErrorLoc = Body->getBeginLoc(); 10668 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 10669 } 10670 if (ErrorFound != NoError) { 10671 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement) 10672 << ErrorRange; 10673 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound 10674 << NoteRange; 10675 return StmtError(); 10676 } 10677 if (CurContext->isDependentContext()) 10678 V = X = nullptr; 10679 } else if (AtomicKind == OMPC_write) { 10680 enum { 10681 NotAnExpression, 10682 NotAnAssignmentOp, 10683 NotAScalarType, 10684 NotAnLValue, 10685 NoError 10686 } ErrorFound = NoError; 10687 SourceLocation ErrorLoc, NoteLoc; 10688 SourceRange ErrorRange, NoteRange; 10689 // If clause is write: 10690 // x = expr; 10691 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 10692 const auto *AtomicBinOp = 10693 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 10694 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 10695 X = AtomicBinOp->getLHS(); 10696 E = AtomicBinOp->getRHS(); 10697 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && 10698 (E->isInstantiationDependent() || E->getType()->isScalarType())) { 10699 if (!X->isLValue()) { 10700 ErrorFound = NotAnLValue; 10701 ErrorLoc = AtomicBinOp->getExprLoc(); 10702 ErrorRange = AtomicBinOp->getSourceRange(); 10703 NoteLoc = X->getExprLoc(); 10704 NoteRange = X->getSourceRange(); 10705 } 10706 } else if (!X->isInstantiationDependent() || 10707 !E->isInstantiationDependent()) { 10708 const Expr *NotScalarExpr = 10709 (X->isInstantiationDependent() || X->getType()->isScalarType()) 10710 ? E 10711 : X; 10712 ErrorFound = NotAScalarType; 10713 ErrorLoc = AtomicBinOp->getExprLoc(); 10714 ErrorRange = AtomicBinOp->getSourceRange(); 10715 NoteLoc = NotScalarExpr->getExprLoc(); 10716 NoteRange = NotScalarExpr->getSourceRange(); 10717 } 10718 } else if (!AtomicBody->isInstantiationDependent()) { 10719 ErrorFound = NotAnAssignmentOp; 10720 ErrorLoc = AtomicBody->getExprLoc(); 10721 ErrorRange = AtomicBody->getSourceRange(); 10722 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 10723 : AtomicBody->getExprLoc(); 10724 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 10725 : AtomicBody->getSourceRange(); 10726 } 10727 } else { 10728 ErrorFound = NotAnExpression; 10729 NoteLoc = ErrorLoc = Body->getBeginLoc(); 10730 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 10731 } 10732 if (ErrorFound != NoError) { 10733 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement) 10734 << ErrorRange; 10735 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound 10736 << NoteRange; 10737 return StmtError(); 10738 } 10739 if (CurContext->isDependentContext()) 10740 E = X = nullptr; 10741 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) { 10742 // If clause is update: 10743 // x++; 10744 // x--; 10745 // ++x; 10746 // --x; 10747 // x binop= expr; 10748 // x = x binop expr; 10749 // x = expr binop x; 10750 OpenMPAtomicUpdateChecker Checker(*this); 10751 if (Checker.checkStatement( 10752 Body, (AtomicKind == OMPC_update) 10753 ? diag::err_omp_atomic_update_not_expression_statement 10754 : diag::err_omp_atomic_not_expression_statement, 10755 diag::note_omp_atomic_update)) 10756 return StmtError(); 10757 if (!CurContext->isDependentContext()) { 10758 E = Checker.getExpr(); 10759 X = Checker.getX(); 10760 UE = Checker.getUpdateExpr(); 10761 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 10762 } 10763 } else if (AtomicKind == OMPC_capture) { 10764 enum { 10765 NotAnAssignmentOp, 10766 NotACompoundStatement, 10767 NotTwoSubstatements, 10768 NotASpecificExpression, 10769 NoError 10770 } ErrorFound = NoError; 10771 SourceLocation ErrorLoc, NoteLoc; 10772 SourceRange ErrorRange, NoteRange; 10773 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 10774 // If clause is a capture: 10775 // v = x++; 10776 // v = x--; 10777 // v = ++x; 10778 // v = --x; 10779 // v = x binop= expr; 10780 // v = x = x binop expr; 10781 // v = x = expr binop x; 10782 const auto *AtomicBinOp = 10783 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 10784 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 10785 V = AtomicBinOp->getLHS(); 10786 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); 10787 OpenMPAtomicUpdateChecker Checker(*this); 10788 if (Checker.checkStatement( 10789 Body, diag::err_omp_atomic_capture_not_expression_statement, 10790 diag::note_omp_atomic_update)) 10791 return StmtError(); 10792 E = Checker.getExpr(); 10793 X = Checker.getX(); 10794 UE = Checker.getUpdateExpr(); 10795 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 10796 IsPostfixUpdate = Checker.isPostfixUpdate(); 10797 } else if (!AtomicBody->isInstantiationDependent()) { 10798 ErrorLoc = AtomicBody->getExprLoc(); 10799 ErrorRange = AtomicBody->getSourceRange(); 10800 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 10801 : AtomicBody->getExprLoc(); 10802 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 10803 : AtomicBody->getSourceRange(); 10804 ErrorFound = NotAnAssignmentOp; 10805 } 10806 if (ErrorFound != NoError) { 10807 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement) 10808 << ErrorRange; 10809 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; 10810 return StmtError(); 10811 } 10812 if (CurContext->isDependentContext()) 10813 UE = V = E = X = nullptr; 10814 } else { 10815 // If clause is a capture: 10816 // { v = x; x = expr; } 10817 // { v = x; x++; } 10818 // { v = x; x--; } 10819 // { v = x; ++x; } 10820 // { v = x; --x; } 10821 // { v = x; x binop= expr; } 10822 // { v = x; x = x binop expr; } 10823 // { v = x; x = expr binop x; } 10824 // { x++; v = x; } 10825 // { x--; v = x; } 10826 // { ++x; v = x; } 10827 // { --x; v = x; } 10828 // { x binop= expr; v = x; } 10829 // { x = x binop expr; v = x; } 10830 // { x = expr binop x; v = x; } 10831 if (auto *CS = dyn_cast<CompoundStmt>(Body)) { 10832 // Check that this is { expr1; expr2; } 10833 if (CS->size() == 2) { 10834 Stmt *First = CS->body_front(); 10835 Stmt *Second = CS->body_back(); 10836 if (auto *EWC = dyn_cast<ExprWithCleanups>(First)) 10837 First = EWC->getSubExpr()->IgnoreParenImpCasts(); 10838 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second)) 10839 Second = EWC->getSubExpr()->IgnoreParenImpCasts(); 10840 // Need to find what subexpression is 'v' and what is 'x'. 10841 OpenMPAtomicUpdateChecker Checker(*this); 10842 bool IsUpdateExprFound = !Checker.checkStatement(Second); 10843 BinaryOperator *BinOp = nullptr; 10844 if (IsUpdateExprFound) { 10845 BinOp = dyn_cast<BinaryOperator>(First); 10846 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; 10847 } 10848 if (IsUpdateExprFound && !CurContext->isDependentContext()) { 10849 // { v = x; x++; } 10850 // { v = x; x--; } 10851 // { v = x; ++x; } 10852 // { v = x; --x; } 10853 // { v = x; x binop= expr; } 10854 // { v = x; x = x binop expr; } 10855 // { v = x; x = expr binop x; } 10856 // Check that the first expression has form v = x. 10857 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); 10858 llvm::FoldingSetNodeID XId, PossibleXId; 10859 Checker.getX()->Profile(XId, Context, /*Canonical=*/true); 10860 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); 10861 IsUpdateExprFound = XId == PossibleXId; 10862 if (IsUpdateExprFound) { 10863 V = BinOp->getLHS(); 10864 X = Checker.getX(); 10865 E = Checker.getExpr(); 10866 UE = Checker.getUpdateExpr(); 10867 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 10868 IsPostfixUpdate = true; 10869 } 10870 } 10871 if (!IsUpdateExprFound) { 10872 IsUpdateExprFound = !Checker.checkStatement(First); 10873 BinOp = nullptr; 10874 if (IsUpdateExprFound) { 10875 BinOp = dyn_cast<BinaryOperator>(Second); 10876 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; 10877 } 10878 if (IsUpdateExprFound && !CurContext->isDependentContext()) { 10879 // { x++; v = x; } 10880 // { x--; v = x; } 10881 // { ++x; v = x; } 10882 // { --x; v = x; } 10883 // { x binop= expr; v = x; } 10884 // { x = x binop expr; v = x; } 10885 // { x = expr binop x; v = x; } 10886 // Check that the second expression has form v = x. 10887 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); 10888 llvm::FoldingSetNodeID XId, PossibleXId; 10889 Checker.getX()->Profile(XId, Context, /*Canonical=*/true); 10890 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); 10891 IsUpdateExprFound = XId == PossibleXId; 10892 if (IsUpdateExprFound) { 10893 V = BinOp->getLHS(); 10894 X = Checker.getX(); 10895 E = Checker.getExpr(); 10896 UE = Checker.getUpdateExpr(); 10897 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 10898 IsPostfixUpdate = false; 10899 } 10900 } 10901 } 10902 if (!IsUpdateExprFound) { 10903 // { v = x; x = expr; } 10904 auto *FirstExpr = dyn_cast<Expr>(First); 10905 auto *SecondExpr = dyn_cast<Expr>(Second); 10906 if (!FirstExpr || !SecondExpr || 10907 !(FirstExpr->isInstantiationDependent() || 10908 SecondExpr->isInstantiationDependent())) { 10909 auto *FirstBinOp = dyn_cast<BinaryOperator>(First); 10910 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) { 10911 ErrorFound = NotAnAssignmentOp; 10912 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc() 10913 : First->getBeginLoc(); 10914 NoteRange = ErrorRange = FirstBinOp 10915 ? FirstBinOp->getSourceRange() 10916 : SourceRange(ErrorLoc, ErrorLoc); 10917 } else { 10918 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second); 10919 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) { 10920 ErrorFound = NotAnAssignmentOp; 10921 NoteLoc = ErrorLoc = SecondBinOp 10922 ? SecondBinOp->getOperatorLoc() 10923 : Second->getBeginLoc(); 10924 NoteRange = ErrorRange = 10925 SecondBinOp ? SecondBinOp->getSourceRange() 10926 : SourceRange(ErrorLoc, ErrorLoc); 10927 } else { 10928 Expr *PossibleXRHSInFirst = 10929 FirstBinOp->getRHS()->IgnoreParenImpCasts(); 10930 Expr *PossibleXLHSInSecond = 10931 SecondBinOp->getLHS()->IgnoreParenImpCasts(); 10932 llvm::FoldingSetNodeID X1Id, X2Id; 10933 PossibleXRHSInFirst->Profile(X1Id, Context, 10934 /*Canonical=*/true); 10935 PossibleXLHSInSecond->Profile(X2Id, Context, 10936 /*Canonical=*/true); 10937 IsUpdateExprFound = X1Id == X2Id; 10938 if (IsUpdateExprFound) { 10939 V = FirstBinOp->getLHS(); 10940 X = SecondBinOp->getLHS(); 10941 E = SecondBinOp->getRHS(); 10942 UE = nullptr; 10943 IsXLHSInRHSPart = false; 10944 IsPostfixUpdate = true; 10945 } else { 10946 ErrorFound = NotASpecificExpression; 10947 ErrorLoc = FirstBinOp->getExprLoc(); 10948 ErrorRange = FirstBinOp->getSourceRange(); 10949 NoteLoc = SecondBinOp->getLHS()->getExprLoc(); 10950 NoteRange = SecondBinOp->getRHS()->getSourceRange(); 10951 } 10952 } 10953 } 10954 } 10955 } 10956 } else { 10957 NoteLoc = ErrorLoc = Body->getBeginLoc(); 10958 NoteRange = ErrorRange = 10959 SourceRange(Body->getBeginLoc(), Body->getBeginLoc()); 10960 ErrorFound = NotTwoSubstatements; 10961 } 10962 } else { 10963 NoteLoc = ErrorLoc = Body->getBeginLoc(); 10964 NoteRange = ErrorRange = 10965 SourceRange(Body->getBeginLoc(), Body->getBeginLoc()); 10966 ErrorFound = NotACompoundStatement; 10967 } 10968 if (ErrorFound != NoError) { 10969 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement) 10970 << ErrorRange; 10971 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; 10972 return StmtError(); 10973 } 10974 if (CurContext->isDependentContext()) 10975 UE = V = E = X = nullptr; 10976 } 10977 } 10978 10979 setFunctionHasBranchProtectedScope(); 10980 10981 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 10982 X, V, E, UE, IsXLHSInRHSPart, 10983 IsPostfixUpdate); 10984 } 10985 10986 StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses, 10987 Stmt *AStmt, 10988 SourceLocation StartLoc, 10989 SourceLocation EndLoc) { 10990 if (!AStmt) 10991 return StmtError(); 10992 10993 auto *CS = cast<CapturedStmt>(AStmt); 10994 // 1.2.2 OpenMP Language Terminology 10995 // Structured block - An executable statement with a single entry at the 10996 // top and a single exit at the bottom. 10997 // The point of exit cannot be a branch out of the structured block. 10998 // longjmp() and throw() must not violate the entry/exit criteria. 10999 CS->getCapturedDecl()->setNothrow(); 11000 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target); 11001 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11002 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11003 // 1.2.2 OpenMP Language Terminology 11004 // Structured block - An executable statement with a single entry at the 11005 // top and a single exit at the bottom. 11006 // The point of exit cannot be a branch out of the structured block. 11007 // longjmp() and throw() must not violate the entry/exit criteria. 11008 CS->getCapturedDecl()->setNothrow(); 11009 } 11010 11011 // OpenMP [2.16, Nesting of Regions] 11012 // If specified, a teams construct must be contained within a target 11013 // construct. That target construct must contain no statements or directives 11014 // outside of the teams construct. 11015 if (DSAStack->hasInnerTeamsRegion()) { 11016 const Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true); 11017 bool OMPTeamsFound = true; 11018 if (const auto *CS = dyn_cast<CompoundStmt>(S)) { 11019 auto I = CS->body_begin(); 11020 while (I != CS->body_end()) { 11021 const auto *OED = dyn_cast<OMPExecutableDirective>(*I); 11022 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind()) || 11023 OMPTeamsFound) { 11024 11025 OMPTeamsFound = false; 11026 break; 11027 } 11028 ++I; 11029 } 11030 assert(I != CS->body_end() && "Not found statement"); 11031 S = *I; 11032 } else { 11033 const auto *OED = dyn_cast<OMPExecutableDirective>(S); 11034 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind()); 11035 } 11036 if (!OMPTeamsFound) { 11037 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams); 11038 Diag(DSAStack->getInnerTeamsRegionLoc(), 11039 diag::note_omp_nested_teams_construct_here); 11040 Diag(S->getBeginLoc(), diag::note_omp_nested_statement_here) 11041 << isa<OMPExecutableDirective>(S); 11042 return StmtError(); 11043 } 11044 } 11045 11046 setFunctionHasBranchProtectedScope(); 11047 11048 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 11049 } 11050 11051 StmtResult 11052 Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses, 11053 Stmt *AStmt, SourceLocation StartLoc, 11054 SourceLocation EndLoc) { 11055 if (!AStmt) 11056 return StmtError(); 11057 11058 auto *CS = cast<CapturedStmt>(AStmt); 11059 // 1.2.2 OpenMP Language Terminology 11060 // Structured block - An executable statement with a single entry at the 11061 // top and a single exit at the bottom. 11062 // The point of exit cannot be a branch out of the structured block. 11063 // longjmp() and throw() must not violate the entry/exit criteria. 11064 CS->getCapturedDecl()->setNothrow(); 11065 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel); 11066 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11067 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11068 // 1.2.2 OpenMP Language Terminology 11069 // Structured block - An executable statement with a single entry at the 11070 // top and a single exit at the bottom. 11071 // The point of exit cannot be a branch out of the structured block. 11072 // longjmp() and throw() must not violate the entry/exit criteria. 11073 CS->getCapturedDecl()->setNothrow(); 11074 } 11075 11076 setFunctionHasBranchProtectedScope(); 11077 11078 return OMPTargetParallelDirective::Create( 11079 Context, StartLoc, EndLoc, Clauses, AStmt, 11080 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 11081 } 11082 11083 StmtResult Sema::ActOnOpenMPTargetParallelForDirective( 11084 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11085 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11086 if (!AStmt) 11087 return StmtError(); 11088 11089 auto *CS = cast<CapturedStmt>(AStmt); 11090 // 1.2.2 OpenMP Language Terminology 11091 // Structured block - An executable statement with a single entry at the 11092 // top and a single exit at the bottom. 11093 // The point of exit cannot be a branch out of the structured block. 11094 // longjmp() and throw() must not violate the entry/exit criteria. 11095 CS->getCapturedDecl()->setNothrow(); 11096 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for); 11097 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11098 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11099 // 1.2.2 OpenMP Language Terminology 11100 // Structured block - An executable statement with a single entry at the 11101 // top and a single exit at the bottom. 11102 // The point of exit cannot be a branch out of the structured block. 11103 // longjmp() and throw() must not violate the entry/exit criteria. 11104 CS->getCapturedDecl()->setNothrow(); 11105 } 11106 11107 OMPLoopBasedDirective::HelperExprs B; 11108 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11109 // define the nested loops number. 11110 unsigned NestedLoopCount = 11111 checkOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses), 11112 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 11113 VarsWithImplicitDSA, B); 11114 if (NestedLoopCount == 0) 11115 return StmtError(); 11116 11117 assert((CurContext->isDependentContext() || B.builtAll()) && 11118 "omp target parallel for loop exprs were not built"); 11119 11120 if (!CurContext->isDependentContext()) { 11121 // Finalize the clauses that need pre-built expressions for CodeGen. 11122 for (OMPClause *C : Clauses) { 11123 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11124 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11125 B.NumIterations, *this, CurScope, 11126 DSAStack)) 11127 return StmtError(); 11128 } 11129 } 11130 11131 setFunctionHasBranchProtectedScope(); 11132 return OMPTargetParallelForDirective::Create( 11133 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 11134 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 11135 } 11136 11137 /// Check for existence of a map clause in the list of clauses. 11138 static bool hasClauses(ArrayRef<OMPClause *> Clauses, 11139 const OpenMPClauseKind K) { 11140 return llvm::any_of( 11141 Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; }); 11142 } 11143 11144 template <typename... Params> 11145 static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K, 11146 const Params... ClauseTypes) { 11147 return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...); 11148 } 11149 11150 StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses, 11151 Stmt *AStmt, 11152 SourceLocation StartLoc, 11153 SourceLocation EndLoc) { 11154 if (!AStmt) 11155 return StmtError(); 11156 11157 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11158 11159 // OpenMP [2.12.2, target data Construct, Restrictions] 11160 // At least one map, use_device_addr or use_device_ptr clause must appear on 11161 // the directive. 11162 if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr) && 11163 (LangOpts.OpenMP < 50 || !hasClauses(Clauses, OMPC_use_device_addr))) { 11164 StringRef Expected; 11165 if (LangOpts.OpenMP < 50) 11166 Expected = "'map' or 'use_device_ptr'"; 11167 else 11168 Expected = "'map', 'use_device_ptr', or 'use_device_addr'"; 11169 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 11170 << Expected << getOpenMPDirectiveName(OMPD_target_data); 11171 return StmtError(); 11172 } 11173 11174 setFunctionHasBranchProtectedScope(); 11175 11176 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 11177 AStmt); 11178 } 11179 11180 StmtResult 11181 Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses, 11182 SourceLocation StartLoc, 11183 SourceLocation EndLoc, Stmt *AStmt) { 11184 if (!AStmt) 11185 return StmtError(); 11186 11187 auto *CS = cast<CapturedStmt>(AStmt); 11188 // 1.2.2 OpenMP Language Terminology 11189 // Structured block - An executable statement with a single entry at the 11190 // top and a single exit at the bottom. 11191 // The point of exit cannot be a branch out of the structured block. 11192 // longjmp() and throw() must not violate the entry/exit criteria. 11193 CS->getCapturedDecl()->setNothrow(); 11194 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_enter_data); 11195 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11196 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11197 // 1.2.2 OpenMP Language Terminology 11198 // Structured block - An executable statement with a single entry at the 11199 // top and a single exit at the bottom. 11200 // The point of exit cannot be a branch out of the structured block. 11201 // longjmp() and throw() must not violate the entry/exit criteria. 11202 CS->getCapturedDecl()->setNothrow(); 11203 } 11204 11205 // OpenMP [2.10.2, Restrictions, p. 99] 11206 // At least one map clause must appear on the directive. 11207 if (!hasClauses(Clauses, OMPC_map)) { 11208 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 11209 << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data); 11210 return StmtError(); 11211 } 11212 11213 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 11214 AStmt); 11215 } 11216 11217 StmtResult 11218 Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses, 11219 SourceLocation StartLoc, 11220 SourceLocation EndLoc, Stmt *AStmt) { 11221 if (!AStmt) 11222 return StmtError(); 11223 11224 auto *CS = cast<CapturedStmt>(AStmt); 11225 // 1.2.2 OpenMP Language Terminology 11226 // Structured block - An executable statement with a single entry at the 11227 // top and a single exit at the bottom. 11228 // The point of exit cannot be a branch out of the structured block. 11229 // longjmp() and throw() must not violate the entry/exit criteria. 11230 CS->getCapturedDecl()->setNothrow(); 11231 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_exit_data); 11232 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11233 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11234 // 1.2.2 OpenMP Language Terminology 11235 // Structured block - An executable statement with a single entry at the 11236 // top and a single exit at the bottom. 11237 // The point of exit cannot be a branch out of the structured block. 11238 // longjmp() and throw() must not violate the entry/exit criteria. 11239 CS->getCapturedDecl()->setNothrow(); 11240 } 11241 11242 // OpenMP [2.10.3, Restrictions, p. 102] 11243 // At least one map clause must appear on the directive. 11244 if (!hasClauses(Clauses, OMPC_map)) { 11245 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 11246 << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data); 11247 return StmtError(); 11248 } 11249 11250 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 11251 AStmt); 11252 } 11253 11254 StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses, 11255 SourceLocation StartLoc, 11256 SourceLocation EndLoc, 11257 Stmt *AStmt) { 11258 if (!AStmt) 11259 return StmtError(); 11260 11261 auto *CS = cast<CapturedStmt>(AStmt); 11262 // 1.2.2 OpenMP Language Terminology 11263 // Structured block - An executable statement with a single entry at the 11264 // top and a single exit at the bottom. 11265 // The point of exit cannot be a branch out of the structured block. 11266 // longjmp() and throw() must not violate the entry/exit criteria. 11267 CS->getCapturedDecl()->setNothrow(); 11268 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_update); 11269 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11270 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11271 // 1.2.2 OpenMP Language Terminology 11272 // Structured block - An executable statement with a single entry at the 11273 // top and a single exit at the bottom. 11274 // The point of exit cannot be a branch out of the structured block. 11275 // longjmp() and throw() must not violate the entry/exit criteria. 11276 CS->getCapturedDecl()->setNothrow(); 11277 } 11278 11279 if (!hasClauses(Clauses, OMPC_to, OMPC_from)) { 11280 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required); 11281 return StmtError(); 11282 } 11283 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses, 11284 AStmt); 11285 } 11286 11287 StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses, 11288 Stmt *AStmt, SourceLocation StartLoc, 11289 SourceLocation EndLoc) { 11290 if (!AStmt) 11291 return StmtError(); 11292 11293 auto *CS = cast<CapturedStmt>(AStmt); 11294 // 1.2.2 OpenMP Language Terminology 11295 // Structured block - An executable statement with a single entry at the 11296 // top and a single exit at the bottom. 11297 // The point of exit cannot be a branch out of the structured block. 11298 // longjmp() and throw() must not violate the entry/exit criteria. 11299 CS->getCapturedDecl()->setNothrow(); 11300 11301 setFunctionHasBranchProtectedScope(); 11302 11303 DSAStack->setParentTeamsRegionLoc(StartLoc); 11304 11305 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 11306 } 11307 11308 StmtResult 11309 Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc, 11310 SourceLocation EndLoc, 11311 OpenMPDirectiveKind CancelRegion) { 11312 if (DSAStack->isParentNowaitRegion()) { 11313 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0; 11314 return StmtError(); 11315 } 11316 if (DSAStack->isParentOrderedRegion()) { 11317 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0; 11318 return StmtError(); 11319 } 11320 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc, 11321 CancelRegion); 11322 } 11323 11324 StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses, 11325 SourceLocation StartLoc, 11326 SourceLocation EndLoc, 11327 OpenMPDirectiveKind CancelRegion) { 11328 if (DSAStack->isParentNowaitRegion()) { 11329 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1; 11330 return StmtError(); 11331 } 11332 if (DSAStack->isParentOrderedRegion()) { 11333 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1; 11334 return StmtError(); 11335 } 11336 DSAStack->setParentCancelRegion(/*Cancel=*/true); 11337 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses, 11338 CancelRegion); 11339 } 11340 11341 static bool checkGrainsizeNumTasksClauses(Sema &S, 11342 ArrayRef<OMPClause *> Clauses) { 11343 const OMPClause *PrevClause = nullptr; 11344 bool ErrorFound = false; 11345 for (const OMPClause *C : Clauses) { 11346 if (C->getClauseKind() == OMPC_grainsize || 11347 C->getClauseKind() == OMPC_num_tasks) { 11348 if (!PrevClause) 11349 PrevClause = C; 11350 else if (PrevClause->getClauseKind() != C->getClauseKind()) { 11351 S.Diag(C->getBeginLoc(), diag::err_omp_clauses_mutually_exclusive) 11352 << getOpenMPClauseName(C->getClauseKind()) 11353 << getOpenMPClauseName(PrevClause->getClauseKind()); 11354 S.Diag(PrevClause->getBeginLoc(), diag::note_omp_previous_clause) 11355 << getOpenMPClauseName(PrevClause->getClauseKind()); 11356 ErrorFound = true; 11357 } 11358 } 11359 } 11360 return ErrorFound; 11361 } 11362 11363 static bool checkReductionClauseWithNogroup(Sema &S, 11364 ArrayRef<OMPClause *> Clauses) { 11365 const OMPClause *ReductionClause = nullptr; 11366 const OMPClause *NogroupClause = nullptr; 11367 for (const OMPClause *C : Clauses) { 11368 if (C->getClauseKind() == OMPC_reduction) { 11369 ReductionClause = C; 11370 if (NogroupClause) 11371 break; 11372 continue; 11373 } 11374 if (C->getClauseKind() == OMPC_nogroup) { 11375 NogroupClause = C; 11376 if (ReductionClause) 11377 break; 11378 continue; 11379 } 11380 } 11381 if (ReductionClause && NogroupClause) { 11382 S.Diag(ReductionClause->getBeginLoc(), diag::err_omp_reduction_with_nogroup) 11383 << SourceRange(NogroupClause->getBeginLoc(), 11384 NogroupClause->getEndLoc()); 11385 return true; 11386 } 11387 return false; 11388 } 11389 11390 StmtResult Sema::ActOnOpenMPTaskLoopDirective( 11391 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11392 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11393 if (!AStmt) 11394 return StmtError(); 11395 11396 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11397 OMPLoopBasedDirective::HelperExprs B; 11398 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11399 // define the nested loops number. 11400 unsigned NestedLoopCount = 11401 checkOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses), 11402 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 11403 VarsWithImplicitDSA, B); 11404 if (NestedLoopCount == 0) 11405 return StmtError(); 11406 11407 assert((CurContext->isDependentContext() || B.builtAll()) && 11408 "omp for loop exprs were not built"); 11409 11410 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11411 // The grainsize clause and num_tasks clause are mutually exclusive and may 11412 // not appear on the same taskloop directive. 11413 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 11414 return StmtError(); 11415 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11416 // If a reduction clause is present on the taskloop directive, the nogroup 11417 // clause must not be specified. 11418 if (checkReductionClauseWithNogroup(*this, Clauses)) 11419 return StmtError(); 11420 11421 setFunctionHasBranchProtectedScope(); 11422 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc, 11423 NestedLoopCount, Clauses, AStmt, B, 11424 DSAStack->isCancelRegion()); 11425 } 11426 11427 StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective( 11428 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11429 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11430 if (!AStmt) 11431 return StmtError(); 11432 11433 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11434 OMPLoopBasedDirective::HelperExprs B; 11435 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11436 // define the nested loops number. 11437 unsigned NestedLoopCount = 11438 checkOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses), 11439 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 11440 VarsWithImplicitDSA, B); 11441 if (NestedLoopCount == 0) 11442 return StmtError(); 11443 11444 assert((CurContext->isDependentContext() || B.builtAll()) && 11445 "omp for loop exprs were not built"); 11446 11447 if (!CurContext->isDependentContext()) { 11448 // Finalize the clauses that need pre-built expressions for CodeGen. 11449 for (OMPClause *C : Clauses) { 11450 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11451 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11452 B.NumIterations, *this, CurScope, 11453 DSAStack)) 11454 return StmtError(); 11455 } 11456 } 11457 11458 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11459 // The grainsize clause and num_tasks clause are mutually exclusive and may 11460 // not appear on the same taskloop directive. 11461 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 11462 return StmtError(); 11463 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11464 // If a reduction clause is present on the taskloop directive, the nogroup 11465 // clause must not be specified. 11466 if (checkReductionClauseWithNogroup(*this, Clauses)) 11467 return StmtError(); 11468 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11469 return StmtError(); 11470 11471 setFunctionHasBranchProtectedScope(); 11472 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc, 11473 NestedLoopCount, Clauses, AStmt, B); 11474 } 11475 11476 StmtResult Sema::ActOnOpenMPMasterTaskLoopDirective( 11477 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11478 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11479 if (!AStmt) 11480 return StmtError(); 11481 11482 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11483 OMPLoopBasedDirective::HelperExprs B; 11484 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11485 // define the nested loops number. 11486 unsigned NestedLoopCount = 11487 checkOpenMPLoop(OMPD_master_taskloop, getCollapseNumberExpr(Clauses), 11488 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 11489 VarsWithImplicitDSA, B); 11490 if (NestedLoopCount == 0) 11491 return StmtError(); 11492 11493 assert((CurContext->isDependentContext() || B.builtAll()) && 11494 "omp for loop exprs were not built"); 11495 11496 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11497 // The grainsize clause and num_tasks clause are mutually exclusive and may 11498 // not appear on the same taskloop directive. 11499 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 11500 return StmtError(); 11501 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11502 // If a reduction clause is present on the taskloop directive, the nogroup 11503 // clause must not be specified. 11504 if (checkReductionClauseWithNogroup(*this, Clauses)) 11505 return StmtError(); 11506 11507 setFunctionHasBranchProtectedScope(); 11508 return OMPMasterTaskLoopDirective::Create(Context, StartLoc, EndLoc, 11509 NestedLoopCount, Clauses, AStmt, B, 11510 DSAStack->isCancelRegion()); 11511 } 11512 11513 StmtResult Sema::ActOnOpenMPMasterTaskLoopSimdDirective( 11514 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11515 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11516 if (!AStmt) 11517 return StmtError(); 11518 11519 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11520 OMPLoopBasedDirective::HelperExprs B; 11521 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11522 // define the nested loops number. 11523 unsigned NestedLoopCount = 11524 checkOpenMPLoop(OMPD_master_taskloop_simd, getCollapseNumberExpr(Clauses), 11525 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 11526 VarsWithImplicitDSA, B); 11527 if (NestedLoopCount == 0) 11528 return StmtError(); 11529 11530 assert((CurContext->isDependentContext() || B.builtAll()) && 11531 "omp for loop exprs were not built"); 11532 11533 if (!CurContext->isDependentContext()) { 11534 // Finalize the clauses that need pre-built expressions for CodeGen. 11535 for (OMPClause *C : Clauses) { 11536 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11537 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11538 B.NumIterations, *this, CurScope, 11539 DSAStack)) 11540 return StmtError(); 11541 } 11542 } 11543 11544 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11545 // The grainsize clause and num_tasks clause are mutually exclusive and may 11546 // not appear on the same taskloop directive. 11547 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 11548 return StmtError(); 11549 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11550 // If a reduction clause is present on the taskloop directive, the nogroup 11551 // clause must not be specified. 11552 if (checkReductionClauseWithNogroup(*this, Clauses)) 11553 return StmtError(); 11554 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11555 return StmtError(); 11556 11557 setFunctionHasBranchProtectedScope(); 11558 return OMPMasterTaskLoopSimdDirective::Create( 11559 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 11560 } 11561 11562 StmtResult Sema::ActOnOpenMPParallelMasterTaskLoopDirective( 11563 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11564 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11565 if (!AStmt) 11566 return StmtError(); 11567 11568 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11569 auto *CS = cast<CapturedStmt>(AStmt); 11570 // 1.2.2 OpenMP Language Terminology 11571 // Structured block - An executable statement with a single entry at the 11572 // top and a single exit at the bottom. 11573 // The point of exit cannot be a branch out of the structured block. 11574 // longjmp() and throw() must not violate the entry/exit criteria. 11575 CS->getCapturedDecl()->setNothrow(); 11576 for (int ThisCaptureLevel = 11577 getOpenMPCaptureLevels(OMPD_parallel_master_taskloop); 11578 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11579 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11580 // 1.2.2 OpenMP Language Terminology 11581 // Structured block - An executable statement with a single entry at the 11582 // top and a single exit at the bottom. 11583 // The point of exit cannot be a branch out of the structured block. 11584 // longjmp() and throw() must not violate the entry/exit criteria. 11585 CS->getCapturedDecl()->setNothrow(); 11586 } 11587 11588 OMPLoopBasedDirective::HelperExprs B; 11589 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11590 // define the nested loops number. 11591 unsigned NestedLoopCount = checkOpenMPLoop( 11592 OMPD_parallel_master_taskloop, getCollapseNumberExpr(Clauses), 11593 /*OrderedLoopCountExpr=*/nullptr, CS, *this, *DSAStack, 11594 VarsWithImplicitDSA, B); 11595 if (NestedLoopCount == 0) 11596 return StmtError(); 11597 11598 assert((CurContext->isDependentContext() || B.builtAll()) && 11599 "omp for loop exprs were not built"); 11600 11601 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11602 // The grainsize clause and num_tasks clause are mutually exclusive and may 11603 // not appear on the same taskloop directive. 11604 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 11605 return StmtError(); 11606 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11607 // If a reduction clause is present on the taskloop directive, the nogroup 11608 // clause must not be specified. 11609 if (checkReductionClauseWithNogroup(*this, Clauses)) 11610 return StmtError(); 11611 11612 setFunctionHasBranchProtectedScope(); 11613 return OMPParallelMasterTaskLoopDirective::Create( 11614 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 11615 DSAStack->isCancelRegion()); 11616 } 11617 11618 StmtResult Sema::ActOnOpenMPParallelMasterTaskLoopSimdDirective( 11619 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11620 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11621 if (!AStmt) 11622 return StmtError(); 11623 11624 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11625 auto *CS = cast<CapturedStmt>(AStmt); 11626 // 1.2.2 OpenMP Language Terminology 11627 // Structured block - An executable statement with a single entry at the 11628 // top and a single exit at the bottom. 11629 // The point of exit cannot be a branch out of the structured block. 11630 // longjmp() and throw() must not violate the entry/exit criteria. 11631 CS->getCapturedDecl()->setNothrow(); 11632 for (int ThisCaptureLevel = 11633 getOpenMPCaptureLevels(OMPD_parallel_master_taskloop_simd); 11634 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11635 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11636 // 1.2.2 OpenMP Language Terminology 11637 // Structured block - An executable statement with a single entry at the 11638 // top and a single exit at the bottom. 11639 // The point of exit cannot be a branch out of the structured block. 11640 // longjmp() and throw() must not violate the entry/exit criteria. 11641 CS->getCapturedDecl()->setNothrow(); 11642 } 11643 11644 OMPLoopBasedDirective::HelperExprs B; 11645 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11646 // define the nested loops number. 11647 unsigned NestedLoopCount = checkOpenMPLoop( 11648 OMPD_parallel_master_taskloop_simd, getCollapseNumberExpr(Clauses), 11649 /*OrderedLoopCountExpr=*/nullptr, CS, *this, *DSAStack, 11650 VarsWithImplicitDSA, B); 11651 if (NestedLoopCount == 0) 11652 return StmtError(); 11653 11654 assert((CurContext->isDependentContext() || B.builtAll()) && 11655 "omp for loop exprs were not built"); 11656 11657 if (!CurContext->isDependentContext()) { 11658 // Finalize the clauses that need pre-built expressions for CodeGen. 11659 for (OMPClause *C : Clauses) { 11660 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11661 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11662 B.NumIterations, *this, CurScope, 11663 DSAStack)) 11664 return StmtError(); 11665 } 11666 } 11667 11668 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11669 // The grainsize clause and num_tasks clause are mutually exclusive and may 11670 // not appear on the same taskloop directive. 11671 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 11672 return StmtError(); 11673 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11674 // If a reduction clause is present on the taskloop directive, the nogroup 11675 // clause must not be specified. 11676 if (checkReductionClauseWithNogroup(*this, Clauses)) 11677 return StmtError(); 11678 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11679 return StmtError(); 11680 11681 setFunctionHasBranchProtectedScope(); 11682 return OMPParallelMasterTaskLoopSimdDirective::Create( 11683 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 11684 } 11685 11686 StmtResult Sema::ActOnOpenMPDistributeDirective( 11687 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11688 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11689 if (!AStmt) 11690 return StmtError(); 11691 11692 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11693 OMPLoopBasedDirective::HelperExprs B; 11694 // In presence of clause 'collapse' with number of loops, it will 11695 // define the nested loops number. 11696 unsigned NestedLoopCount = 11697 checkOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses), 11698 nullptr /*ordered not a clause on distribute*/, AStmt, 11699 *this, *DSAStack, VarsWithImplicitDSA, B); 11700 if (NestedLoopCount == 0) 11701 return StmtError(); 11702 11703 assert((CurContext->isDependentContext() || B.builtAll()) && 11704 "omp for loop exprs were not built"); 11705 11706 setFunctionHasBranchProtectedScope(); 11707 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc, 11708 NestedLoopCount, Clauses, AStmt, B); 11709 } 11710 11711 StmtResult Sema::ActOnOpenMPDistributeParallelForDirective( 11712 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11713 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11714 if (!AStmt) 11715 return StmtError(); 11716 11717 auto *CS = cast<CapturedStmt>(AStmt); 11718 // 1.2.2 OpenMP Language Terminology 11719 // Structured block - An executable statement with a single entry at the 11720 // top and a single exit at the bottom. 11721 // The point of exit cannot be a branch out of the structured block. 11722 // longjmp() and throw() must not violate the entry/exit criteria. 11723 CS->getCapturedDecl()->setNothrow(); 11724 for (int ThisCaptureLevel = 11725 getOpenMPCaptureLevels(OMPD_distribute_parallel_for); 11726 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11727 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11728 // 1.2.2 OpenMP Language Terminology 11729 // Structured block - An executable statement with a single entry at the 11730 // top and a single exit at the bottom. 11731 // The point of exit cannot be a branch out of the structured block. 11732 // longjmp() and throw() must not violate the entry/exit criteria. 11733 CS->getCapturedDecl()->setNothrow(); 11734 } 11735 11736 OMPLoopBasedDirective::HelperExprs B; 11737 // In presence of clause 'collapse' with number of loops, it will 11738 // define the nested loops number. 11739 unsigned NestedLoopCount = checkOpenMPLoop( 11740 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses), 11741 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 11742 VarsWithImplicitDSA, B); 11743 if (NestedLoopCount == 0) 11744 return StmtError(); 11745 11746 assert((CurContext->isDependentContext() || B.builtAll()) && 11747 "omp for loop exprs were not built"); 11748 11749 setFunctionHasBranchProtectedScope(); 11750 return OMPDistributeParallelForDirective::Create( 11751 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 11752 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 11753 } 11754 11755 StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective( 11756 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11757 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11758 if (!AStmt) 11759 return StmtError(); 11760 11761 auto *CS = cast<CapturedStmt>(AStmt); 11762 // 1.2.2 OpenMP Language Terminology 11763 // Structured block - An executable statement with a single entry at the 11764 // top and a single exit at the bottom. 11765 // The point of exit cannot be a branch out of the structured block. 11766 // longjmp() and throw() must not violate the entry/exit criteria. 11767 CS->getCapturedDecl()->setNothrow(); 11768 for (int ThisCaptureLevel = 11769 getOpenMPCaptureLevels(OMPD_distribute_parallel_for_simd); 11770 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11771 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11772 // 1.2.2 OpenMP Language Terminology 11773 // Structured block - An executable statement with a single entry at the 11774 // top and a single exit at the bottom. 11775 // The point of exit cannot be a branch out of the structured block. 11776 // longjmp() and throw() must not violate the entry/exit criteria. 11777 CS->getCapturedDecl()->setNothrow(); 11778 } 11779 11780 OMPLoopBasedDirective::HelperExprs B; 11781 // In presence of clause 'collapse' with number of loops, it will 11782 // define the nested loops number. 11783 unsigned NestedLoopCount = checkOpenMPLoop( 11784 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses), 11785 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 11786 VarsWithImplicitDSA, B); 11787 if (NestedLoopCount == 0) 11788 return StmtError(); 11789 11790 assert((CurContext->isDependentContext() || B.builtAll()) && 11791 "omp for loop exprs were not built"); 11792 11793 if (!CurContext->isDependentContext()) { 11794 // Finalize the clauses that need pre-built expressions for CodeGen. 11795 for (OMPClause *C : Clauses) { 11796 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11797 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11798 B.NumIterations, *this, CurScope, 11799 DSAStack)) 11800 return StmtError(); 11801 } 11802 } 11803 11804 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11805 return StmtError(); 11806 11807 setFunctionHasBranchProtectedScope(); 11808 return OMPDistributeParallelForSimdDirective::Create( 11809 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 11810 } 11811 11812 StmtResult Sema::ActOnOpenMPDistributeSimdDirective( 11813 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11814 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11815 if (!AStmt) 11816 return StmtError(); 11817 11818 auto *CS = cast<CapturedStmt>(AStmt); 11819 // 1.2.2 OpenMP Language Terminology 11820 // Structured block - An executable statement with a single entry at the 11821 // top and a single exit at the bottom. 11822 // The point of exit cannot be a branch out of the structured block. 11823 // longjmp() and throw() must not violate the entry/exit criteria. 11824 CS->getCapturedDecl()->setNothrow(); 11825 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_distribute_simd); 11826 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11827 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11828 // 1.2.2 OpenMP Language Terminology 11829 // Structured block - An executable statement with a single entry at the 11830 // top and a single exit at the bottom. 11831 // The point of exit cannot be a branch out of the structured block. 11832 // longjmp() and throw() must not violate the entry/exit criteria. 11833 CS->getCapturedDecl()->setNothrow(); 11834 } 11835 11836 OMPLoopBasedDirective::HelperExprs B; 11837 // In presence of clause 'collapse' with number of loops, it will 11838 // define the nested loops number. 11839 unsigned NestedLoopCount = 11840 checkOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses), 11841 nullptr /*ordered not a clause on distribute*/, CS, *this, 11842 *DSAStack, VarsWithImplicitDSA, B); 11843 if (NestedLoopCount == 0) 11844 return StmtError(); 11845 11846 assert((CurContext->isDependentContext() || B.builtAll()) && 11847 "omp for loop exprs were not built"); 11848 11849 if (!CurContext->isDependentContext()) { 11850 // Finalize the clauses that need pre-built expressions for CodeGen. 11851 for (OMPClause *C : Clauses) { 11852 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11853 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11854 B.NumIterations, *this, CurScope, 11855 DSAStack)) 11856 return StmtError(); 11857 } 11858 } 11859 11860 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11861 return StmtError(); 11862 11863 setFunctionHasBranchProtectedScope(); 11864 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc, 11865 NestedLoopCount, Clauses, AStmt, B); 11866 } 11867 11868 StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective( 11869 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11870 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11871 if (!AStmt) 11872 return StmtError(); 11873 11874 auto *CS = cast<CapturedStmt>(AStmt); 11875 // 1.2.2 OpenMP Language Terminology 11876 // Structured block - An executable statement with a single entry at the 11877 // top and a single exit at the bottom. 11878 // The point of exit cannot be a branch out of the structured block. 11879 // longjmp() and throw() must not violate the entry/exit criteria. 11880 CS->getCapturedDecl()->setNothrow(); 11881 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for); 11882 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11883 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11884 // 1.2.2 OpenMP Language Terminology 11885 // Structured block - An executable statement with a single entry at the 11886 // top and a single exit at the bottom. 11887 // The point of exit cannot be a branch out of the structured block. 11888 // longjmp() and throw() must not violate the entry/exit criteria. 11889 CS->getCapturedDecl()->setNothrow(); 11890 } 11891 11892 OMPLoopBasedDirective::HelperExprs B; 11893 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11894 // define the nested loops number. 11895 unsigned NestedLoopCount = checkOpenMPLoop( 11896 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses), 11897 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 11898 VarsWithImplicitDSA, B); 11899 if (NestedLoopCount == 0) 11900 return StmtError(); 11901 11902 assert((CurContext->isDependentContext() || B.builtAll()) && 11903 "omp target parallel for simd loop exprs were not built"); 11904 11905 if (!CurContext->isDependentContext()) { 11906 // Finalize the clauses that need pre-built expressions for CodeGen. 11907 for (OMPClause *C : Clauses) { 11908 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11909 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11910 B.NumIterations, *this, CurScope, 11911 DSAStack)) 11912 return StmtError(); 11913 } 11914 } 11915 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11916 return StmtError(); 11917 11918 setFunctionHasBranchProtectedScope(); 11919 return OMPTargetParallelForSimdDirective::Create( 11920 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 11921 } 11922 11923 StmtResult Sema::ActOnOpenMPTargetSimdDirective( 11924 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11925 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11926 if (!AStmt) 11927 return StmtError(); 11928 11929 auto *CS = cast<CapturedStmt>(AStmt); 11930 // 1.2.2 OpenMP Language Terminology 11931 // Structured block - An executable statement with a single entry at the 11932 // top and a single exit at the bottom. 11933 // The point of exit cannot be a branch out of the structured block. 11934 // longjmp() and throw() must not violate the entry/exit criteria. 11935 CS->getCapturedDecl()->setNothrow(); 11936 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_simd); 11937 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11938 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11939 // 1.2.2 OpenMP Language Terminology 11940 // Structured block - An executable statement with a single entry at the 11941 // top and a single exit at the bottom. 11942 // The point of exit cannot be a branch out of the structured block. 11943 // longjmp() and throw() must not violate the entry/exit criteria. 11944 CS->getCapturedDecl()->setNothrow(); 11945 } 11946 11947 OMPLoopBasedDirective::HelperExprs B; 11948 // In presence of clause 'collapse' with number of loops, it will define the 11949 // nested loops number. 11950 unsigned NestedLoopCount = 11951 checkOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses), 11952 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 11953 VarsWithImplicitDSA, B); 11954 if (NestedLoopCount == 0) 11955 return StmtError(); 11956 11957 assert((CurContext->isDependentContext() || B.builtAll()) && 11958 "omp target simd loop exprs were not built"); 11959 11960 if (!CurContext->isDependentContext()) { 11961 // Finalize the clauses that need pre-built expressions for CodeGen. 11962 for (OMPClause *C : Clauses) { 11963 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11964 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11965 B.NumIterations, *this, CurScope, 11966 DSAStack)) 11967 return StmtError(); 11968 } 11969 } 11970 11971 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11972 return StmtError(); 11973 11974 setFunctionHasBranchProtectedScope(); 11975 return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc, 11976 NestedLoopCount, Clauses, AStmt, B); 11977 } 11978 11979 StmtResult Sema::ActOnOpenMPTeamsDistributeDirective( 11980 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11981 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11982 if (!AStmt) 11983 return StmtError(); 11984 11985 auto *CS = cast<CapturedStmt>(AStmt); 11986 // 1.2.2 OpenMP Language Terminology 11987 // Structured block - An executable statement with a single entry at the 11988 // top and a single exit at the bottom. 11989 // The point of exit cannot be a branch out of the structured block. 11990 // longjmp() and throw() must not violate the entry/exit criteria. 11991 CS->getCapturedDecl()->setNothrow(); 11992 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_teams_distribute); 11993 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11994 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11995 // 1.2.2 OpenMP Language Terminology 11996 // Structured block - An executable statement with a single entry at the 11997 // top and a single exit at the bottom. 11998 // The point of exit cannot be a branch out of the structured block. 11999 // longjmp() and throw() must not violate the entry/exit criteria. 12000 CS->getCapturedDecl()->setNothrow(); 12001 } 12002 12003 OMPLoopBasedDirective::HelperExprs B; 12004 // In presence of clause 'collapse' with number of loops, it will 12005 // define the nested loops number. 12006 unsigned NestedLoopCount = 12007 checkOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses), 12008 nullptr /*ordered not a clause on distribute*/, CS, *this, 12009 *DSAStack, VarsWithImplicitDSA, B); 12010 if (NestedLoopCount == 0) 12011 return StmtError(); 12012 12013 assert((CurContext->isDependentContext() || B.builtAll()) && 12014 "omp teams distribute loop exprs were not built"); 12015 12016 setFunctionHasBranchProtectedScope(); 12017 12018 DSAStack->setParentTeamsRegionLoc(StartLoc); 12019 12020 return OMPTeamsDistributeDirective::Create( 12021 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12022 } 12023 12024 StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective( 12025 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12026 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12027 if (!AStmt) 12028 return StmtError(); 12029 12030 auto *CS = cast<CapturedStmt>(AStmt); 12031 // 1.2.2 OpenMP Language Terminology 12032 // Structured block - An executable statement with a single entry at the 12033 // top and a single exit at the bottom. 12034 // The point of exit cannot be a branch out of the structured block. 12035 // longjmp() and throw() must not violate the entry/exit criteria. 12036 CS->getCapturedDecl()->setNothrow(); 12037 for (int ThisCaptureLevel = 12038 getOpenMPCaptureLevels(OMPD_teams_distribute_simd); 12039 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12040 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12041 // 1.2.2 OpenMP Language Terminology 12042 // Structured block - An executable statement with a single entry at the 12043 // top and a single exit at the bottom. 12044 // The point of exit cannot be a branch out of the structured block. 12045 // longjmp() and throw() must not violate the entry/exit criteria. 12046 CS->getCapturedDecl()->setNothrow(); 12047 } 12048 12049 OMPLoopBasedDirective::HelperExprs B; 12050 // In presence of clause 'collapse' with number of loops, it will 12051 // define the nested loops number. 12052 unsigned NestedLoopCount = checkOpenMPLoop( 12053 OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses), 12054 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 12055 VarsWithImplicitDSA, B); 12056 12057 if (NestedLoopCount == 0) 12058 return StmtError(); 12059 12060 assert((CurContext->isDependentContext() || B.builtAll()) && 12061 "omp teams distribute simd loop exprs were not built"); 12062 12063 if (!CurContext->isDependentContext()) { 12064 // Finalize the clauses that need pre-built expressions for CodeGen. 12065 for (OMPClause *C : Clauses) { 12066 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12067 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12068 B.NumIterations, *this, CurScope, 12069 DSAStack)) 12070 return StmtError(); 12071 } 12072 } 12073 12074 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12075 return StmtError(); 12076 12077 setFunctionHasBranchProtectedScope(); 12078 12079 DSAStack->setParentTeamsRegionLoc(StartLoc); 12080 12081 return OMPTeamsDistributeSimdDirective::Create( 12082 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12083 } 12084 12085 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective( 12086 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12087 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12088 if (!AStmt) 12089 return StmtError(); 12090 12091 auto *CS = cast<CapturedStmt>(AStmt); 12092 // 1.2.2 OpenMP Language Terminology 12093 // Structured block - An executable statement with a single entry at the 12094 // top and a single exit at the bottom. 12095 // The point of exit cannot be a branch out of the structured block. 12096 // longjmp() and throw() must not violate the entry/exit criteria. 12097 CS->getCapturedDecl()->setNothrow(); 12098 12099 for (int ThisCaptureLevel = 12100 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for_simd); 12101 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12102 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12103 // 1.2.2 OpenMP Language Terminology 12104 // Structured block - An executable statement with a single entry at the 12105 // top and a single exit at the bottom. 12106 // The point of exit cannot be a branch out of the structured block. 12107 // longjmp() and throw() must not violate the entry/exit criteria. 12108 CS->getCapturedDecl()->setNothrow(); 12109 } 12110 12111 OMPLoopBasedDirective::HelperExprs B; 12112 // In presence of clause 'collapse' with number of loops, it will 12113 // define the nested loops number. 12114 unsigned NestedLoopCount = checkOpenMPLoop( 12115 OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses), 12116 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 12117 VarsWithImplicitDSA, B); 12118 12119 if (NestedLoopCount == 0) 12120 return StmtError(); 12121 12122 assert((CurContext->isDependentContext() || B.builtAll()) && 12123 "omp for loop exprs were not built"); 12124 12125 if (!CurContext->isDependentContext()) { 12126 // Finalize the clauses that need pre-built expressions for CodeGen. 12127 for (OMPClause *C : Clauses) { 12128 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12129 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12130 B.NumIterations, *this, CurScope, 12131 DSAStack)) 12132 return StmtError(); 12133 } 12134 } 12135 12136 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12137 return StmtError(); 12138 12139 setFunctionHasBranchProtectedScope(); 12140 12141 DSAStack->setParentTeamsRegionLoc(StartLoc); 12142 12143 return OMPTeamsDistributeParallelForSimdDirective::Create( 12144 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12145 } 12146 12147 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective( 12148 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12149 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12150 if (!AStmt) 12151 return StmtError(); 12152 12153 auto *CS = cast<CapturedStmt>(AStmt); 12154 // 1.2.2 OpenMP Language Terminology 12155 // Structured block - An executable statement with a single entry at the 12156 // top and a single exit at the bottom. 12157 // The point of exit cannot be a branch out of the structured block. 12158 // longjmp() and throw() must not violate the entry/exit criteria. 12159 CS->getCapturedDecl()->setNothrow(); 12160 12161 for (int ThisCaptureLevel = 12162 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for); 12163 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12164 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12165 // 1.2.2 OpenMP Language Terminology 12166 // Structured block - An executable statement with a single entry at the 12167 // top and a single exit at the bottom. 12168 // The point of exit cannot be a branch out of the structured block. 12169 // longjmp() and throw() must not violate the entry/exit criteria. 12170 CS->getCapturedDecl()->setNothrow(); 12171 } 12172 12173 OMPLoopBasedDirective::HelperExprs B; 12174 // In presence of clause 'collapse' with number of loops, it will 12175 // define the nested loops number. 12176 unsigned NestedLoopCount = checkOpenMPLoop( 12177 OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses), 12178 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 12179 VarsWithImplicitDSA, B); 12180 12181 if (NestedLoopCount == 0) 12182 return StmtError(); 12183 12184 assert((CurContext->isDependentContext() || B.builtAll()) && 12185 "omp for loop exprs were not built"); 12186 12187 setFunctionHasBranchProtectedScope(); 12188 12189 DSAStack->setParentTeamsRegionLoc(StartLoc); 12190 12191 return OMPTeamsDistributeParallelForDirective::Create( 12192 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 12193 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 12194 } 12195 12196 StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses, 12197 Stmt *AStmt, 12198 SourceLocation StartLoc, 12199 SourceLocation EndLoc) { 12200 if (!AStmt) 12201 return StmtError(); 12202 12203 auto *CS = cast<CapturedStmt>(AStmt); 12204 // 1.2.2 OpenMP Language Terminology 12205 // Structured block - An executable statement with a single entry at the 12206 // top and a single exit at the bottom. 12207 // The point of exit cannot be a branch out of the structured block. 12208 // longjmp() and throw() must not violate the entry/exit criteria. 12209 CS->getCapturedDecl()->setNothrow(); 12210 12211 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_teams); 12212 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12213 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12214 // 1.2.2 OpenMP Language Terminology 12215 // Structured block - An executable statement with a single entry at the 12216 // top and a single exit at the bottom. 12217 // The point of exit cannot be a branch out of the structured block. 12218 // longjmp() and throw() must not violate the entry/exit criteria. 12219 CS->getCapturedDecl()->setNothrow(); 12220 } 12221 setFunctionHasBranchProtectedScope(); 12222 12223 return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, 12224 AStmt); 12225 } 12226 12227 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective( 12228 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12229 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12230 if (!AStmt) 12231 return StmtError(); 12232 12233 auto *CS = cast<CapturedStmt>(AStmt); 12234 // 1.2.2 OpenMP Language Terminology 12235 // Structured block - An executable statement with a single entry at the 12236 // top and a single exit at the bottom. 12237 // The point of exit cannot be a branch out of the structured block. 12238 // longjmp() and throw() must not violate the entry/exit criteria. 12239 CS->getCapturedDecl()->setNothrow(); 12240 for (int ThisCaptureLevel = 12241 getOpenMPCaptureLevels(OMPD_target_teams_distribute); 12242 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12243 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12244 // 1.2.2 OpenMP Language Terminology 12245 // Structured block - An executable statement with a single entry at the 12246 // top and a single exit at the bottom. 12247 // The point of exit cannot be a branch out of the structured block. 12248 // longjmp() and throw() must not violate the entry/exit criteria. 12249 CS->getCapturedDecl()->setNothrow(); 12250 } 12251 12252 OMPLoopBasedDirective::HelperExprs B; 12253 // In presence of clause 'collapse' with number of loops, it will 12254 // define the nested loops number. 12255 unsigned NestedLoopCount = checkOpenMPLoop( 12256 OMPD_target_teams_distribute, getCollapseNumberExpr(Clauses), 12257 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 12258 VarsWithImplicitDSA, B); 12259 if (NestedLoopCount == 0) 12260 return StmtError(); 12261 12262 assert((CurContext->isDependentContext() || B.builtAll()) && 12263 "omp target teams distribute loop exprs were not built"); 12264 12265 setFunctionHasBranchProtectedScope(); 12266 return OMPTargetTeamsDistributeDirective::Create( 12267 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12268 } 12269 12270 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective( 12271 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12272 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12273 if (!AStmt) 12274 return StmtError(); 12275 12276 auto *CS = cast<CapturedStmt>(AStmt); 12277 // 1.2.2 OpenMP Language Terminology 12278 // Structured block - An executable statement with a single entry at the 12279 // top and a single exit at the bottom. 12280 // The point of exit cannot be a branch out of the structured block. 12281 // longjmp() and throw() must not violate the entry/exit criteria. 12282 CS->getCapturedDecl()->setNothrow(); 12283 for (int ThisCaptureLevel = 12284 getOpenMPCaptureLevels(OMPD_target_teams_distribute_parallel_for); 12285 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12286 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12287 // 1.2.2 OpenMP Language Terminology 12288 // Structured block - An executable statement with a single entry at the 12289 // top and a single exit at the bottom. 12290 // The point of exit cannot be a branch out of the structured block. 12291 // longjmp() and throw() must not violate the entry/exit criteria. 12292 CS->getCapturedDecl()->setNothrow(); 12293 } 12294 12295 OMPLoopBasedDirective::HelperExprs B; 12296 // In presence of clause 'collapse' with number of loops, it will 12297 // define the nested loops number. 12298 unsigned NestedLoopCount = checkOpenMPLoop( 12299 OMPD_target_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses), 12300 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 12301 VarsWithImplicitDSA, B); 12302 if (NestedLoopCount == 0) 12303 return StmtError(); 12304 12305 assert((CurContext->isDependentContext() || B.builtAll()) && 12306 "omp target teams distribute parallel for loop exprs were not built"); 12307 12308 if (!CurContext->isDependentContext()) { 12309 // Finalize the clauses that need pre-built expressions for CodeGen. 12310 for (OMPClause *C : Clauses) { 12311 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12312 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12313 B.NumIterations, *this, CurScope, 12314 DSAStack)) 12315 return StmtError(); 12316 } 12317 } 12318 12319 setFunctionHasBranchProtectedScope(); 12320 return OMPTargetTeamsDistributeParallelForDirective::Create( 12321 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 12322 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 12323 } 12324 12325 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective( 12326 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12327 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12328 if (!AStmt) 12329 return StmtError(); 12330 12331 auto *CS = cast<CapturedStmt>(AStmt); 12332 // 1.2.2 OpenMP Language Terminology 12333 // Structured block - An executable statement with a single entry at the 12334 // top and a single exit at the bottom. 12335 // The point of exit cannot be a branch out of the structured block. 12336 // longjmp() and throw() must not violate the entry/exit criteria. 12337 CS->getCapturedDecl()->setNothrow(); 12338 for (int ThisCaptureLevel = getOpenMPCaptureLevels( 12339 OMPD_target_teams_distribute_parallel_for_simd); 12340 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12341 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12342 // 1.2.2 OpenMP Language Terminology 12343 // Structured block - An executable statement with a single entry at the 12344 // top and a single exit at the bottom. 12345 // The point of exit cannot be a branch out of the structured block. 12346 // longjmp() and throw() must not violate the entry/exit criteria. 12347 CS->getCapturedDecl()->setNothrow(); 12348 } 12349 12350 OMPLoopBasedDirective::HelperExprs B; 12351 // In presence of clause 'collapse' with number of loops, it will 12352 // define the nested loops number. 12353 unsigned NestedLoopCount = 12354 checkOpenMPLoop(OMPD_target_teams_distribute_parallel_for_simd, 12355 getCollapseNumberExpr(Clauses), 12356 nullptr /*ordered not a clause on distribute*/, CS, *this, 12357 *DSAStack, VarsWithImplicitDSA, B); 12358 if (NestedLoopCount == 0) 12359 return StmtError(); 12360 12361 assert((CurContext->isDependentContext() || B.builtAll()) && 12362 "omp target teams distribute parallel for simd loop exprs were not " 12363 "built"); 12364 12365 if (!CurContext->isDependentContext()) { 12366 // Finalize the clauses that need pre-built expressions for CodeGen. 12367 for (OMPClause *C : Clauses) { 12368 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12369 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12370 B.NumIterations, *this, CurScope, 12371 DSAStack)) 12372 return StmtError(); 12373 } 12374 } 12375 12376 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12377 return StmtError(); 12378 12379 setFunctionHasBranchProtectedScope(); 12380 return OMPTargetTeamsDistributeParallelForSimdDirective::Create( 12381 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12382 } 12383 12384 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective( 12385 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12386 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12387 if (!AStmt) 12388 return StmtError(); 12389 12390 auto *CS = cast<CapturedStmt>(AStmt); 12391 // 1.2.2 OpenMP Language Terminology 12392 // Structured block - An executable statement with a single entry at the 12393 // top and a single exit at the bottom. 12394 // The point of exit cannot be a branch out of the structured block. 12395 // longjmp() and throw() must not violate the entry/exit criteria. 12396 CS->getCapturedDecl()->setNothrow(); 12397 for (int ThisCaptureLevel = 12398 getOpenMPCaptureLevels(OMPD_target_teams_distribute_simd); 12399 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12400 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12401 // 1.2.2 OpenMP Language Terminology 12402 // Structured block - An executable statement with a single entry at the 12403 // top and a single exit at the bottom. 12404 // The point of exit cannot be a branch out of the structured block. 12405 // longjmp() and throw() must not violate the entry/exit criteria. 12406 CS->getCapturedDecl()->setNothrow(); 12407 } 12408 12409 OMPLoopBasedDirective::HelperExprs B; 12410 // In presence of clause 'collapse' with number of loops, it will 12411 // define the nested loops number. 12412 unsigned NestedLoopCount = checkOpenMPLoop( 12413 OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses), 12414 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 12415 VarsWithImplicitDSA, B); 12416 if (NestedLoopCount == 0) 12417 return StmtError(); 12418 12419 assert((CurContext->isDependentContext() || B.builtAll()) && 12420 "omp target teams distribute simd loop exprs were not built"); 12421 12422 if (!CurContext->isDependentContext()) { 12423 // Finalize the clauses that need pre-built expressions for CodeGen. 12424 for (OMPClause *C : Clauses) { 12425 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12426 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12427 B.NumIterations, *this, CurScope, 12428 DSAStack)) 12429 return StmtError(); 12430 } 12431 } 12432 12433 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12434 return StmtError(); 12435 12436 setFunctionHasBranchProtectedScope(); 12437 return OMPTargetTeamsDistributeSimdDirective::Create( 12438 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12439 } 12440 12441 StmtResult Sema::ActOnOpenMPTileDirective(ArrayRef<OMPClause *> Clauses, 12442 Stmt *AStmt, SourceLocation StartLoc, 12443 SourceLocation EndLoc) { 12444 auto SizesClauses = 12445 OMPExecutableDirective::getClausesOfKind<OMPSizesClause>(Clauses); 12446 if (SizesClauses.empty()) { 12447 // A missing 'sizes' clause is already reported by the parser. 12448 return StmtError(); 12449 } 12450 const OMPSizesClause *SizesClause = *SizesClauses.begin(); 12451 unsigned NumLoops = SizesClause->getNumSizes(); 12452 12453 // Empty statement should only be possible if there already was an error. 12454 if (!AStmt) 12455 return StmtError(); 12456 12457 // Verify and diagnose loop nest. 12458 SmallVector<OMPLoopBasedDirective::HelperExprs, 4> LoopHelpers(NumLoops); 12459 Stmt *Body = nullptr; 12460 SmallVector<Stmt *, 4> OriginalInits; 12461 if (!OMPLoopBasedDirective::doForAllLoops( 12462 AStmt->IgnoreContainers(), /*TryImperfectlyNestedLoops=*/false, 12463 NumLoops, 12464 [this, &LoopHelpers, &Body, &OriginalInits](unsigned Cnt, 12465 Stmt *CurStmt) { 12466 VarsWithInheritedDSAType TmpDSA; 12467 unsigned SingleNumLoops = 12468 checkOpenMPLoop(OMPD_tile, nullptr, nullptr, CurStmt, *this, 12469 *DSAStack, TmpDSA, LoopHelpers[Cnt]); 12470 if (SingleNumLoops == 0) 12471 return true; 12472 assert(SingleNumLoops == 1 && "Expect single loop iteration space"); 12473 if (auto *For = dyn_cast<ForStmt>(CurStmt)) { 12474 OriginalInits.push_back(For->getInit()); 12475 Body = For->getBody(); 12476 } else { 12477 assert(isa<CXXForRangeStmt>(CurStmt) && 12478 "Expected canonical for or range-based for loops."); 12479 auto *CXXFor = cast<CXXForRangeStmt>(CurStmt); 12480 OriginalInits.push_back(CXXFor->getBeginStmt()); 12481 Body = CXXFor->getBody(); 12482 } 12483 return false; 12484 })) 12485 return StmtError(); 12486 12487 // Delay tiling to when template is completely instantiated. 12488 if (CurContext->isDependentContext()) 12489 return OMPTileDirective::Create(Context, StartLoc, EndLoc, Clauses, 12490 NumLoops, AStmt, nullptr, nullptr); 12491 12492 // Collection of generated variable declaration. 12493 SmallVector<Decl *, 4> PreInits; 12494 12495 // Create iteration variables for the generated loops. 12496 SmallVector<VarDecl *, 4> FloorIndVars; 12497 SmallVector<VarDecl *, 4> TileIndVars; 12498 FloorIndVars.resize(NumLoops); 12499 TileIndVars.resize(NumLoops); 12500 for (unsigned I = 0; I < NumLoops; ++I) { 12501 OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers[I]; 12502 if (auto *PI = cast_or_null<DeclStmt>(LoopHelper.PreInits)) 12503 PreInits.append(PI->decl_begin(), PI->decl_end()); 12504 assert(LoopHelper.Counters.size() == 1 && 12505 "Expect single-dimensional loop iteration space"); 12506 auto *OrigCntVar = cast<DeclRefExpr>(LoopHelper.Counters.front()); 12507 std::string OrigVarName = OrigCntVar->getNameInfo().getAsString(); 12508 DeclRefExpr *IterVarRef = cast<DeclRefExpr>(LoopHelper.IterationVarRef); 12509 QualType CntTy = IterVarRef->getType(); 12510 12511 // Iteration variable for the floor (i.e. outer) loop. 12512 { 12513 std::string FloorCntName = 12514 (Twine(".floor_") + llvm::utostr(I) + ".iv." + OrigVarName).str(); 12515 VarDecl *FloorCntDecl = 12516 buildVarDecl(*this, {}, CntTy, FloorCntName, nullptr, OrigCntVar); 12517 FloorIndVars[I] = FloorCntDecl; 12518 } 12519 12520 // Iteration variable for the tile (i.e. inner) loop. 12521 { 12522 std::string TileCntName = 12523 (Twine(".tile_") + llvm::utostr(I) + ".iv." + OrigVarName).str(); 12524 12525 // Reuse the iteration variable created by checkOpenMPLoop. It is also 12526 // used by the expressions to derive the original iteration variable's 12527 // value from the logical iteration number. 12528 auto *TileCntDecl = cast<VarDecl>(IterVarRef->getDecl()); 12529 TileCntDecl->setDeclName(&PP.getIdentifierTable().get(TileCntName)); 12530 TileIndVars[I] = TileCntDecl; 12531 } 12532 if (auto *PI = dyn_cast_or_null<DeclStmt>(OriginalInits[I])) 12533 PreInits.append(PI->decl_begin(), PI->decl_end()); 12534 // Gather declarations for the data members used as counters. 12535 for (Expr *CounterRef : LoopHelper.Counters) { 12536 auto *CounterDecl = cast<DeclRefExpr>(CounterRef)->getDecl(); 12537 if (isa<OMPCapturedExprDecl>(CounterDecl)) 12538 PreInits.push_back(CounterDecl); 12539 } 12540 } 12541 12542 // Once the original iteration values are set, append the innermost body. 12543 Stmt *Inner = Body; 12544 12545 // Create tile loops from the inside to the outside. 12546 for (int I = NumLoops - 1; I >= 0; --I) { 12547 OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers[I]; 12548 Expr *NumIterations = LoopHelper.NumIterations; 12549 auto *OrigCntVar = cast<DeclRefExpr>(LoopHelper.Counters[0]); 12550 QualType CntTy = OrigCntVar->getType(); 12551 Expr *DimTileSize = SizesClause->getSizesRefs()[I]; 12552 Scope *CurScope = getCurScope(); 12553 12554 // Commonly used variables. 12555 DeclRefExpr *TileIV = buildDeclRefExpr(*this, TileIndVars[I], CntTy, 12556 OrigCntVar->getExprLoc()); 12557 DeclRefExpr *FloorIV = buildDeclRefExpr(*this, FloorIndVars[I], CntTy, 12558 OrigCntVar->getExprLoc()); 12559 12560 // For init-statement: auto .tile.iv = .floor.iv 12561 AddInitializerToDecl(TileIndVars[I], DefaultLvalueConversion(FloorIV).get(), 12562 /*DirectInit=*/false); 12563 Decl *CounterDecl = TileIndVars[I]; 12564 StmtResult InitStmt = new (Context) 12565 DeclStmt(DeclGroupRef::Create(Context, &CounterDecl, 1), 12566 OrigCntVar->getBeginLoc(), OrigCntVar->getEndLoc()); 12567 if (!InitStmt.isUsable()) 12568 return StmtError(); 12569 12570 // For cond-expression: .tile.iv < min(.floor.iv + DimTileSize, 12571 // NumIterations) 12572 ExprResult EndOfTile = BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), 12573 BO_Add, FloorIV, DimTileSize); 12574 if (!EndOfTile.isUsable()) 12575 return StmtError(); 12576 ExprResult IsPartialTile = 12577 BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), BO_LT, 12578 NumIterations, EndOfTile.get()); 12579 if (!IsPartialTile.isUsable()) 12580 return StmtError(); 12581 ExprResult MinTileAndIterSpace = ActOnConditionalOp( 12582 LoopHelper.Cond->getBeginLoc(), LoopHelper.Cond->getEndLoc(), 12583 IsPartialTile.get(), NumIterations, EndOfTile.get()); 12584 if (!MinTileAndIterSpace.isUsable()) 12585 return StmtError(); 12586 ExprResult CondExpr = BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), 12587 BO_LT, TileIV, MinTileAndIterSpace.get()); 12588 if (!CondExpr.isUsable()) 12589 return StmtError(); 12590 12591 // For incr-statement: ++.tile.iv 12592 ExprResult IncrStmt = 12593 BuildUnaryOp(CurScope, LoopHelper.Inc->getExprLoc(), UO_PreInc, TileIV); 12594 if (!IncrStmt.isUsable()) 12595 return StmtError(); 12596 12597 // Statements to set the original iteration variable's value from the 12598 // logical iteration number. 12599 // Generated for loop is: 12600 // Original_for_init; 12601 // for (auto .tile.iv = .floor.iv; .tile.iv < min(.floor.iv + DimTileSize, 12602 // NumIterations); ++.tile.iv) { 12603 // Original_Body; 12604 // Original_counter_update; 12605 // } 12606 // FIXME: If the innermost body is an loop itself, inserting these 12607 // statements stops it being recognized as a perfectly nested loop (e.g. 12608 // for applying tiling again). If this is the case, sink the expressions 12609 // further into the inner loop. 12610 SmallVector<Stmt *, 4> BodyParts; 12611 BodyParts.append(LoopHelper.Updates.begin(), LoopHelper.Updates.end()); 12612 BodyParts.push_back(Inner); 12613 Inner = CompoundStmt::Create(Context, BodyParts, Inner->getBeginLoc(), 12614 Inner->getEndLoc()); 12615 Inner = new (Context) 12616 ForStmt(Context, InitStmt.get(), CondExpr.get(), nullptr, 12617 IncrStmt.get(), Inner, LoopHelper.Init->getBeginLoc(), 12618 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc()); 12619 } 12620 12621 // Create floor loops from the inside to the outside. 12622 for (int I = NumLoops - 1; I >= 0; --I) { 12623 auto &LoopHelper = LoopHelpers[I]; 12624 Expr *NumIterations = LoopHelper.NumIterations; 12625 DeclRefExpr *OrigCntVar = cast<DeclRefExpr>(LoopHelper.Counters[0]); 12626 QualType CntTy = OrigCntVar->getType(); 12627 Expr *DimTileSize = SizesClause->getSizesRefs()[I]; 12628 Scope *CurScope = getCurScope(); 12629 12630 // Commonly used variables. 12631 DeclRefExpr *FloorIV = buildDeclRefExpr(*this, FloorIndVars[I], CntTy, 12632 OrigCntVar->getExprLoc()); 12633 12634 // For init-statement: auto .floor.iv = 0 12635 AddInitializerToDecl( 12636 FloorIndVars[I], 12637 ActOnIntegerConstant(LoopHelper.Init->getExprLoc(), 0).get(), 12638 /*DirectInit=*/false); 12639 Decl *CounterDecl = FloorIndVars[I]; 12640 StmtResult InitStmt = new (Context) 12641 DeclStmt(DeclGroupRef::Create(Context, &CounterDecl, 1), 12642 OrigCntVar->getBeginLoc(), OrigCntVar->getEndLoc()); 12643 if (!InitStmt.isUsable()) 12644 return StmtError(); 12645 12646 // For cond-expression: .floor.iv < NumIterations 12647 ExprResult CondExpr = BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), 12648 BO_LT, FloorIV, NumIterations); 12649 if (!CondExpr.isUsable()) 12650 return StmtError(); 12651 12652 // For incr-statement: .floor.iv += DimTileSize 12653 ExprResult IncrStmt = BuildBinOp(CurScope, LoopHelper.Inc->getExprLoc(), 12654 BO_AddAssign, FloorIV, DimTileSize); 12655 if (!IncrStmt.isUsable()) 12656 return StmtError(); 12657 12658 Inner = new (Context) 12659 ForStmt(Context, InitStmt.get(), CondExpr.get(), nullptr, 12660 IncrStmt.get(), Inner, LoopHelper.Init->getBeginLoc(), 12661 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc()); 12662 } 12663 12664 return OMPTileDirective::Create(Context, StartLoc, EndLoc, Clauses, NumLoops, 12665 AStmt, Inner, 12666 buildPreInits(Context, PreInits)); 12667 } 12668 12669 OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr, 12670 SourceLocation StartLoc, 12671 SourceLocation LParenLoc, 12672 SourceLocation EndLoc) { 12673 OMPClause *Res = nullptr; 12674 switch (Kind) { 12675 case OMPC_final: 12676 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc); 12677 break; 12678 case OMPC_num_threads: 12679 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc); 12680 break; 12681 case OMPC_safelen: 12682 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc); 12683 break; 12684 case OMPC_simdlen: 12685 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc); 12686 break; 12687 case OMPC_allocator: 12688 Res = ActOnOpenMPAllocatorClause(Expr, StartLoc, LParenLoc, EndLoc); 12689 break; 12690 case OMPC_collapse: 12691 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc); 12692 break; 12693 case OMPC_ordered: 12694 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr); 12695 break; 12696 case OMPC_num_teams: 12697 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc); 12698 break; 12699 case OMPC_thread_limit: 12700 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc); 12701 break; 12702 case OMPC_priority: 12703 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc); 12704 break; 12705 case OMPC_grainsize: 12706 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc); 12707 break; 12708 case OMPC_num_tasks: 12709 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc); 12710 break; 12711 case OMPC_hint: 12712 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc); 12713 break; 12714 case OMPC_depobj: 12715 Res = ActOnOpenMPDepobjClause(Expr, StartLoc, LParenLoc, EndLoc); 12716 break; 12717 case OMPC_detach: 12718 Res = ActOnOpenMPDetachClause(Expr, StartLoc, LParenLoc, EndLoc); 12719 break; 12720 case OMPC_device: 12721 case OMPC_if: 12722 case OMPC_default: 12723 case OMPC_proc_bind: 12724 case OMPC_schedule: 12725 case OMPC_private: 12726 case OMPC_firstprivate: 12727 case OMPC_lastprivate: 12728 case OMPC_shared: 12729 case OMPC_reduction: 12730 case OMPC_task_reduction: 12731 case OMPC_in_reduction: 12732 case OMPC_linear: 12733 case OMPC_aligned: 12734 case OMPC_copyin: 12735 case OMPC_copyprivate: 12736 case OMPC_nowait: 12737 case OMPC_untied: 12738 case OMPC_mergeable: 12739 case OMPC_threadprivate: 12740 case OMPC_sizes: 12741 case OMPC_allocate: 12742 case OMPC_flush: 12743 case OMPC_read: 12744 case OMPC_write: 12745 case OMPC_update: 12746 case OMPC_capture: 12747 case OMPC_seq_cst: 12748 case OMPC_acq_rel: 12749 case OMPC_acquire: 12750 case OMPC_release: 12751 case OMPC_relaxed: 12752 case OMPC_depend: 12753 case OMPC_threads: 12754 case OMPC_simd: 12755 case OMPC_map: 12756 case OMPC_nogroup: 12757 case OMPC_dist_schedule: 12758 case OMPC_defaultmap: 12759 case OMPC_unknown: 12760 case OMPC_uniform: 12761 case OMPC_to: 12762 case OMPC_from: 12763 case OMPC_use_device_ptr: 12764 case OMPC_use_device_addr: 12765 case OMPC_is_device_ptr: 12766 case OMPC_unified_address: 12767 case OMPC_unified_shared_memory: 12768 case OMPC_reverse_offload: 12769 case OMPC_dynamic_allocators: 12770 case OMPC_atomic_default_mem_order: 12771 case OMPC_device_type: 12772 case OMPC_match: 12773 case OMPC_nontemporal: 12774 case OMPC_order: 12775 case OMPC_destroy: 12776 case OMPC_inclusive: 12777 case OMPC_exclusive: 12778 case OMPC_uses_allocators: 12779 case OMPC_affinity: 12780 default: 12781 llvm_unreachable("Clause is not allowed."); 12782 } 12783 return Res; 12784 } 12785 12786 // An OpenMP directive such as 'target parallel' has two captured regions: 12787 // for the 'target' and 'parallel' respectively. This function returns 12788 // the region in which to capture expressions associated with a clause. 12789 // A return value of OMPD_unknown signifies that the expression should not 12790 // be captured. 12791 static OpenMPDirectiveKind getOpenMPCaptureRegionForClause( 12792 OpenMPDirectiveKind DKind, OpenMPClauseKind CKind, unsigned OpenMPVersion, 12793 OpenMPDirectiveKind NameModifier = OMPD_unknown) { 12794 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 12795 switch (CKind) { 12796 case OMPC_if: 12797 switch (DKind) { 12798 case OMPD_target_parallel_for_simd: 12799 if (OpenMPVersion >= 50 && 12800 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) { 12801 CaptureRegion = OMPD_parallel; 12802 break; 12803 } 12804 LLVM_FALLTHROUGH; 12805 case OMPD_target_parallel: 12806 case OMPD_target_parallel_for: 12807 // If this clause applies to the nested 'parallel' region, capture within 12808 // the 'target' region, otherwise do not capture. 12809 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel) 12810 CaptureRegion = OMPD_target; 12811 break; 12812 case OMPD_target_teams_distribute_parallel_for_simd: 12813 if (OpenMPVersion >= 50 && 12814 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) { 12815 CaptureRegion = OMPD_parallel; 12816 break; 12817 } 12818 LLVM_FALLTHROUGH; 12819 case OMPD_target_teams_distribute_parallel_for: 12820 // If this clause applies to the nested 'parallel' region, capture within 12821 // the 'teams' region, otherwise do not capture. 12822 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel) 12823 CaptureRegion = OMPD_teams; 12824 break; 12825 case OMPD_teams_distribute_parallel_for_simd: 12826 if (OpenMPVersion >= 50 && 12827 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) { 12828 CaptureRegion = OMPD_parallel; 12829 break; 12830 } 12831 LLVM_FALLTHROUGH; 12832 case OMPD_teams_distribute_parallel_for: 12833 CaptureRegion = OMPD_teams; 12834 break; 12835 case OMPD_target_update: 12836 case OMPD_target_enter_data: 12837 case OMPD_target_exit_data: 12838 CaptureRegion = OMPD_task; 12839 break; 12840 case OMPD_parallel_master_taskloop: 12841 if (NameModifier == OMPD_unknown || NameModifier == OMPD_taskloop) 12842 CaptureRegion = OMPD_parallel; 12843 break; 12844 case OMPD_parallel_master_taskloop_simd: 12845 if ((OpenMPVersion <= 45 && NameModifier == OMPD_unknown) || 12846 NameModifier == OMPD_taskloop) { 12847 CaptureRegion = OMPD_parallel; 12848 break; 12849 } 12850 if (OpenMPVersion <= 45) 12851 break; 12852 if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd) 12853 CaptureRegion = OMPD_taskloop; 12854 break; 12855 case OMPD_parallel_for_simd: 12856 if (OpenMPVersion <= 45) 12857 break; 12858 if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd) 12859 CaptureRegion = OMPD_parallel; 12860 break; 12861 case OMPD_taskloop_simd: 12862 case OMPD_master_taskloop_simd: 12863 if (OpenMPVersion <= 45) 12864 break; 12865 if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd) 12866 CaptureRegion = OMPD_taskloop; 12867 break; 12868 case OMPD_distribute_parallel_for_simd: 12869 if (OpenMPVersion <= 45) 12870 break; 12871 if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd) 12872 CaptureRegion = OMPD_parallel; 12873 break; 12874 case OMPD_target_simd: 12875 if (OpenMPVersion >= 50 && 12876 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) 12877 CaptureRegion = OMPD_target; 12878 break; 12879 case OMPD_teams_distribute_simd: 12880 case OMPD_target_teams_distribute_simd: 12881 if (OpenMPVersion >= 50 && 12882 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) 12883 CaptureRegion = OMPD_teams; 12884 break; 12885 case OMPD_cancel: 12886 case OMPD_parallel: 12887 case OMPD_parallel_master: 12888 case OMPD_parallel_sections: 12889 case OMPD_parallel_for: 12890 case OMPD_target: 12891 case OMPD_target_teams: 12892 case OMPD_target_teams_distribute: 12893 case OMPD_distribute_parallel_for: 12894 case OMPD_task: 12895 case OMPD_taskloop: 12896 case OMPD_master_taskloop: 12897 case OMPD_target_data: 12898 case OMPD_simd: 12899 case OMPD_for_simd: 12900 case OMPD_distribute_simd: 12901 // Do not capture if-clause expressions. 12902 break; 12903 case OMPD_threadprivate: 12904 case OMPD_allocate: 12905 case OMPD_taskyield: 12906 case OMPD_barrier: 12907 case OMPD_taskwait: 12908 case OMPD_cancellation_point: 12909 case OMPD_flush: 12910 case OMPD_depobj: 12911 case OMPD_scan: 12912 case OMPD_declare_reduction: 12913 case OMPD_declare_mapper: 12914 case OMPD_declare_simd: 12915 case OMPD_declare_variant: 12916 case OMPD_begin_declare_variant: 12917 case OMPD_end_declare_variant: 12918 case OMPD_declare_target: 12919 case OMPD_end_declare_target: 12920 case OMPD_teams: 12921 case OMPD_tile: 12922 case OMPD_for: 12923 case OMPD_sections: 12924 case OMPD_section: 12925 case OMPD_single: 12926 case OMPD_master: 12927 case OMPD_critical: 12928 case OMPD_taskgroup: 12929 case OMPD_distribute: 12930 case OMPD_ordered: 12931 case OMPD_atomic: 12932 case OMPD_teams_distribute: 12933 case OMPD_requires: 12934 llvm_unreachable("Unexpected OpenMP directive with if-clause"); 12935 case OMPD_unknown: 12936 default: 12937 llvm_unreachable("Unknown OpenMP directive"); 12938 } 12939 break; 12940 case OMPC_num_threads: 12941 switch (DKind) { 12942 case OMPD_target_parallel: 12943 case OMPD_target_parallel_for: 12944 case OMPD_target_parallel_for_simd: 12945 CaptureRegion = OMPD_target; 12946 break; 12947 case OMPD_teams_distribute_parallel_for: 12948 case OMPD_teams_distribute_parallel_for_simd: 12949 case OMPD_target_teams_distribute_parallel_for: 12950 case OMPD_target_teams_distribute_parallel_for_simd: 12951 CaptureRegion = OMPD_teams; 12952 break; 12953 case OMPD_parallel: 12954 case OMPD_parallel_master: 12955 case OMPD_parallel_sections: 12956 case OMPD_parallel_for: 12957 case OMPD_parallel_for_simd: 12958 case OMPD_distribute_parallel_for: 12959 case OMPD_distribute_parallel_for_simd: 12960 case OMPD_parallel_master_taskloop: 12961 case OMPD_parallel_master_taskloop_simd: 12962 // Do not capture num_threads-clause expressions. 12963 break; 12964 case OMPD_target_data: 12965 case OMPD_target_enter_data: 12966 case OMPD_target_exit_data: 12967 case OMPD_target_update: 12968 case OMPD_target: 12969 case OMPD_target_simd: 12970 case OMPD_target_teams: 12971 case OMPD_target_teams_distribute: 12972 case OMPD_target_teams_distribute_simd: 12973 case OMPD_cancel: 12974 case OMPD_task: 12975 case OMPD_taskloop: 12976 case OMPD_taskloop_simd: 12977 case OMPD_master_taskloop: 12978 case OMPD_master_taskloop_simd: 12979 case OMPD_threadprivate: 12980 case OMPD_allocate: 12981 case OMPD_taskyield: 12982 case OMPD_barrier: 12983 case OMPD_taskwait: 12984 case OMPD_cancellation_point: 12985 case OMPD_flush: 12986 case OMPD_depobj: 12987 case OMPD_scan: 12988 case OMPD_declare_reduction: 12989 case OMPD_declare_mapper: 12990 case OMPD_declare_simd: 12991 case OMPD_declare_variant: 12992 case OMPD_begin_declare_variant: 12993 case OMPD_end_declare_variant: 12994 case OMPD_declare_target: 12995 case OMPD_end_declare_target: 12996 case OMPD_teams: 12997 case OMPD_simd: 12998 case OMPD_tile: 12999 case OMPD_for: 13000 case OMPD_for_simd: 13001 case OMPD_sections: 13002 case OMPD_section: 13003 case OMPD_single: 13004 case OMPD_master: 13005 case OMPD_critical: 13006 case OMPD_taskgroup: 13007 case OMPD_distribute: 13008 case OMPD_ordered: 13009 case OMPD_atomic: 13010 case OMPD_distribute_simd: 13011 case OMPD_teams_distribute: 13012 case OMPD_teams_distribute_simd: 13013 case OMPD_requires: 13014 llvm_unreachable("Unexpected OpenMP directive with num_threads-clause"); 13015 case OMPD_unknown: 13016 default: 13017 llvm_unreachable("Unknown OpenMP directive"); 13018 } 13019 break; 13020 case OMPC_num_teams: 13021 switch (DKind) { 13022 case OMPD_target_teams: 13023 case OMPD_target_teams_distribute: 13024 case OMPD_target_teams_distribute_simd: 13025 case OMPD_target_teams_distribute_parallel_for: 13026 case OMPD_target_teams_distribute_parallel_for_simd: 13027 CaptureRegion = OMPD_target; 13028 break; 13029 case OMPD_teams_distribute_parallel_for: 13030 case OMPD_teams_distribute_parallel_for_simd: 13031 case OMPD_teams: 13032 case OMPD_teams_distribute: 13033 case OMPD_teams_distribute_simd: 13034 // Do not capture num_teams-clause expressions. 13035 break; 13036 case OMPD_distribute_parallel_for: 13037 case OMPD_distribute_parallel_for_simd: 13038 case OMPD_task: 13039 case OMPD_taskloop: 13040 case OMPD_taskloop_simd: 13041 case OMPD_master_taskloop: 13042 case OMPD_master_taskloop_simd: 13043 case OMPD_parallel_master_taskloop: 13044 case OMPD_parallel_master_taskloop_simd: 13045 case OMPD_target_data: 13046 case OMPD_target_enter_data: 13047 case OMPD_target_exit_data: 13048 case OMPD_target_update: 13049 case OMPD_cancel: 13050 case OMPD_parallel: 13051 case OMPD_parallel_master: 13052 case OMPD_parallel_sections: 13053 case OMPD_parallel_for: 13054 case OMPD_parallel_for_simd: 13055 case OMPD_target: 13056 case OMPD_target_simd: 13057 case OMPD_target_parallel: 13058 case OMPD_target_parallel_for: 13059 case OMPD_target_parallel_for_simd: 13060 case OMPD_threadprivate: 13061 case OMPD_allocate: 13062 case OMPD_taskyield: 13063 case OMPD_barrier: 13064 case OMPD_taskwait: 13065 case OMPD_cancellation_point: 13066 case OMPD_flush: 13067 case OMPD_depobj: 13068 case OMPD_scan: 13069 case OMPD_declare_reduction: 13070 case OMPD_declare_mapper: 13071 case OMPD_declare_simd: 13072 case OMPD_declare_variant: 13073 case OMPD_begin_declare_variant: 13074 case OMPD_end_declare_variant: 13075 case OMPD_declare_target: 13076 case OMPD_end_declare_target: 13077 case OMPD_simd: 13078 case OMPD_tile: 13079 case OMPD_for: 13080 case OMPD_for_simd: 13081 case OMPD_sections: 13082 case OMPD_section: 13083 case OMPD_single: 13084 case OMPD_master: 13085 case OMPD_critical: 13086 case OMPD_taskgroup: 13087 case OMPD_distribute: 13088 case OMPD_ordered: 13089 case OMPD_atomic: 13090 case OMPD_distribute_simd: 13091 case OMPD_requires: 13092 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause"); 13093 case OMPD_unknown: 13094 default: 13095 llvm_unreachable("Unknown OpenMP directive"); 13096 } 13097 break; 13098 case OMPC_thread_limit: 13099 switch (DKind) { 13100 case OMPD_target_teams: 13101 case OMPD_target_teams_distribute: 13102 case OMPD_target_teams_distribute_simd: 13103 case OMPD_target_teams_distribute_parallel_for: 13104 case OMPD_target_teams_distribute_parallel_for_simd: 13105 CaptureRegion = OMPD_target; 13106 break; 13107 case OMPD_teams_distribute_parallel_for: 13108 case OMPD_teams_distribute_parallel_for_simd: 13109 case OMPD_teams: 13110 case OMPD_teams_distribute: 13111 case OMPD_teams_distribute_simd: 13112 // Do not capture thread_limit-clause expressions. 13113 break; 13114 case OMPD_distribute_parallel_for: 13115 case OMPD_distribute_parallel_for_simd: 13116 case OMPD_task: 13117 case OMPD_taskloop: 13118 case OMPD_taskloop_simd: 13119 case OMPD_master_taskloop: 13120 case OMPD_master_taskloop_simd: 13121 case OMPD_parallel_master_taskloop: 13122 case OMPD_parallel_master_taskloop_simd: 13123 case OMPD_target_data: 13124 case OMPD_target_enter_data: 13125 case OMPD_target_exit_data: 13126 case OMPD_target_update: 13127 case OMPD_cancel: 13128 case OMPD_parallel: 13129 case OMPD_parallel_master: 13130 case OMPD_parallel_sections: 13131 case OMPD_parallel_for: 13132 case OMPD_parallel_for_simd: 13133 case OMPD_target: 13134 case OMPD_target_simd: 13135 case OMPD_target_parallel: 13136 case OMPD_target_parallel_for: 13137 case OMPD_target_parallel_for_simd: 13138 case OMPD_threadprivate: 13139 case OMPD_allocate: 13140 case OMPD_taskyield: 13141 case OMPD_barrier: 13142 case OMPD_taskwait: 13143 case OMPD_cancellation_point: 13144 case OMPD_flush: 13145 case OMPD_depobj: 13146 case OMPD_scan: 13147 case OMPD_declare_reduction: 13148 case OMPD_declare_mapper: 13149 case OMPD_declare_simd: 13150 case OMPD_declare_variant: 13151 case OMPD_begin_declare_variant: 13152 case OMPD_end_declare_variant: 13153 case OMPD_declare_target: 13154 case OMPD_end_declare_target: 13155 case OMPD_simd: 13156 case OMPD_tile: 13157 case OMPD_for: 13158 case OMPD_for_simd: 13159 case OMPD_sections: 13160 case OMPD_section: 13161 case OMPD_single: 13162 case OMPD_master: 13163 case OMPD_critical: 13164 case OMPD_taskgroup: 13165 case OMPD_distribute: 13166 case OMPD_ordered: 13167 case OMPD_atomic: 13168 case OMPD_distribute_simd: 13169 case OMPD_requires: 13170 llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause"); 13171 case OMPD_unknown: 13172 default: 13173 llvm_unreachable("Unknown OpenMP directive"); 13174 } 13175 break; 13176 case OMPC_schedule: 13177 switch (DKind) { 13178 case OMPD_parallel_for: 13179 case OMPD_parallel_for_simd: 13180 case OMPD_distribute_parallel_for: 13181 case OMPD_distribute_parallel_for_simd: 13182 case OMPD_teams_distribute_parallel_for: 13183 case OMPD_teams_distribute_parallel_for_simd: 13184 case OMPD_target_parallel_for: 13185 case OMPD_target_parallel_for_simd: 13186 case OMPD_target_teams_distribute_parallel_for: 13187 case OMPD_target_teams_distribute_parallel_for_simd: 13188 CaptureRegion = OMPD_parallel; 13189 break; 13190 case OMPD_for: 13191 case OMPD_for_simd: 13192 // Do not capture schedule-clause expressions. 13193 break; 13194 case OMPD_task: 13195 case OMPD_taskloop: 13196 case OMPD_taskloop_simd: 13197 case OMPD_master_taskloop: 13198 case OMPD_master_taskloop_simd: 13199 case OMPD_parallel_master_taskloop: 13200 case OMPD_parallel_master_taskloop_simd: 13201 case OMPD_target_data: 13202 case OMPD_target_enter_data: 13203 case OMPD_target_exit_data: 13204 case OMPD_target_update: 13205 case OMPD_teams: 13206 case OMPD_teams_distribute: 13207 case OMPD_teams_distribute_simd: 13208 case OMPD_target_teams_distribute: 13209 case OMPD_target_teams_distribute_simd: 13210 case OMPD_target: 13211 case OMPD_target_simd: 13212 case OMPD_target_parallel: 13213 case OMPD_cancel: 13214 case OMPD_parallel: 13215 case OMPD_parallel_master: 13216 case OMPD_parallel_sections: 13217 case OMPD_threadprivate: 13218 case OMPD_allocate: 13219 case OMPD_taskyield: 13220 case OMPD_barrier: 13221 case OMPD_taskwait: 13222 case OMPD_cancellation_point: 13223 case OMPD_flush: 13224 case OMPD_depobj: 13225 case OMPD_scan: 13226 case OMPD_declare_reduction: 13227 case OMPD_declare_mapper: 13228 case OMPD_declare_simd: 13229 case OMPD_declare_variant: 13230 case OMPD_begin_declare_variant: 13231 case OMPD_end_declare_variant: 13232 case OMPD_declare_target: 13233 case OMPD_end_declare_target: 13234 case OMPD_simd: 13235 case OMPD_tile: 13236 case OMPD_sections: 13237 case OMPD_section: 13238 case OMPD_single: 13239 case OMPD_master: 13240 case OMPD_critical: 13241 case OMPD_taskgroup: 13242 case OMPD_distribute: 13243 case OMPD_ordered: 13244 case OMPD_atomic: 13245 case OMPD_distribute_simd: 13246 case OMPD_target_teams: 13247 case OMPD_requires: 13248 llvm_unreachable("Unexpected OpenMP directive with schedule clause"); 13249 case OMPD_unknown: 13250 default: 13251 llvm_unreachable("Unknown OpenMP directive"); 13252 } 13253 break; 13254 case OMPC_dist_schedule: 13255 switch (DKind) { 13256 case OMPD_teams_distribute_parallel_for: 13257 case OMPD_teams_distribute_parallel_for_simd: 13258 case OMPD_teams_distribute: 13259 case OMPD_teams_distribute_simd: 13260 case OMPD_target_teams_distribute_parallel_for: 13261 case OMPD_target_teams_distribute_parallel_for_simd: 13262 case OMPD_target_teams_distribute: 13263 case OMPD_target_teams_distribute_simd: 13264 CaptureRegion = OMPD_teams; 13265 break; 13266 case OMPD_distribute_parallel_for: 13267 case OMPD_distribute_parallel_for_simd: 13268 case OMPD_distribute: 13269 case OMPD_distribute_simd: 13270 // Do not capture dist_schedule-clause expressions. 13271 break; 13272 case OMPD_parallel_for: 13273 case OMPD_parallel_for_simd: 13274 case OMPD_target_parallel_for_simd: 13275 case OMPD_target_parallel_for: 13276 case OMPD_task: 13277 case OMPD_taskloop: 13278 case OMPD_taskloop_simd: 13279 case OMPD_master_taskloop: 13280 case OMPD_master_taskloop_simd: 13281 case OMPD_parallel_master_taskloop: 13282 case OMPD_parallel_master_taskloop_simd: 13283 case OMPD_target_data: 13284 case OMPD_target_enter_data: 13285 case OMPD_target_exit_data: 13286 case OMPD_target_update: 13287 case OMPD_teams: 13288 case OMPD_target: 13289 case OMPD_target_simd: 13290 case OMPD_target_parallel: 13291 case OMPD_cancel: 13292 case OMPD_parallel: 13293 case OMPD_parallel_master: 13294 case OMPD_parallel_sections: 13295 case OMPD_threadprivate: 13296 case OMPD_allocate: 13297 case OMPD_taskyield: 13298 case OMPD_barrier: 13299 case OMPD_taskwait: 13300 case OMPD_cancellation_point: 13301 case OMPD_flush: 13302 case OMPD_depobj: 13303 case OMPD_scan: 13304 case OMPD_declare_reduction: 13305 case OMPD_declare_mapper: 13306 case OMPD_declare_simd: 13307 case OMPD_declare_variant: 13308 case OMPD_begin_declare_variant: 13309 case OMPD_end_declare_variant: 13310 case OMPD_declare_target: 13311 case OMPD_end_declare_target: 13312 case OMPD_simd: 13313 case OMPD_tile: 13314 case OMPD_for: 13315 case OMPD_for_simd: 13316 case OMPD_sections: 13317 case OMPD_section: 13318 case OMPD_single: 13319 case OMPD_master: 13320 case OMPD_critical: 13321 case OMPD_taskgroup: 13322 case OMPD_ordered: 13323 case OMPD_atomic: 13324 case OMPD_target_teams: 13325 case OMPD_requires: 13326 llvm_unreachable("Unexpected OpenMP directive with dist_schedule clause"); 13327 case OMPD_unknown: 13328 default: 13329 llvm_unreachable("Unknown OpenMP directive"); 13330 } 13331 break; 13332 case OMPC_device: 13333 switch (DKind) { 13334 case OMPD_target_update: 13335 case OMPD_target_enter_data: 13336 case OMPD_target_exit_data: 13337 case OMPD_target: 13338 case OMPD_target_simd: 13339 case OMPD_target_teams: 13340 case OMPD_target_parallel: 13341 case OMPD_target_teams_distribute: 13342 case OMPD_target_teams_distribute_simd: 13343 case OMPD_target_parallel_for: 13344 case OMPD_target_parallel_for_simd: 13345 case OMPD_target_teams_distribute_parallel_for: 13346 case OMPD_target_teams_distribute_parallel_for_simd: 13347 CaptureRegion = OMPD_task; 13348 break; 13349 case OMPD_target_data: 13350 // Do not capture device-clause expressions. 13351 break; 13352 case OMPD_teams_distribute_parallel_for: 13353 case OMPD_teams_distribute_parallel_for_simd: 13354 case OMPD_teams: 13355 case OMPD_teams_distribute: 13356 case OMPD_teams_distribute_simd: 13357 case OMPD_distribute_parallel_for: 13358 case OMPD_distribute_parallel_for_simd: 13359 case OMPD_task: 13360 case OMPD_taskloop: 13361 case OMPD_taskloop_simd: 13362 case OMPD_master_taskloop: 13363 case OMPD_master_taskloop_simd: 13364 case OMPD_parallel_master_taskloop: 13365 case OMPD_parallel_master_taskloop_simd: 13366 case OMPD_cancel: 13367 case OMPD_parallel: 13368 case OMPD_parallel_master: 13369 case OMPD_parallel_sections: 13370 case OMPD_parallel_for: 13371 case OMPD_parallel_for_simd: 13372 case OMPD_threadprivate: 13373 case OMPD_allocate: 13374 case OMPD_taskyield: 13375 case OMPD_barrier: 13376 case OMPD_taskwait: 13377 case OMPD_cancellation_point: 13378 case OMPD_flush: 13379 case OMPD_depobj: 13380 case OMPD_scan: 13381 case OMPD_declare_reduction: 13382 case OMPD_declare_mapper: 13383 case OMPD_declare_simd: 13384 case OMPD_declare_variant: 13385 case OMPD_begin_declare_variant: 13386 case OMPD_end_declare_variant: 13387 case OMPD_declare_target: 13388 case OMPD_end_declare_target: 13389 case OMPD_simd: 13390 case OMPD_tile: 13391 case OMPD_for: 13392 case OMPD_for_simd: 13393 case OMPD_sections: 13394 case OMPD_section: 13395 case OMPD_single: 13396 case OMPD_master: 13397 case OMPD_critical: 13398 case OMPD_taskgroup: 13399 case OMPD_distribute: 13400 case OMPD_ordered: 13401 case OMPD_atomic: 13402 case OMPD_distribute_simd: 13403 case OMPD_requires: 13404 llvm_unreachable("Unexpected OpenMP directive with device-clause"); 13405 case OMPD_unknown: 13406 default: 13407 llvm_unreachable("Unknown OpenMP directive"); 13408 } 13409 break; 13410 case OMPC_grainsize: 13411 case OMPC_num_tasks: 13412 case OMPC_final: 13413 case OMPC_priority: 13414 switch (DKind) { 13415 case OMPD_task: 13416 case OMPD_taskloop: 13417 case OMPD_taskloop_simd: 13418 case OMPD_master_taskloop: 13419 case OMPD_master_taskloop_simd: 13420 break; 13421 case OMPD_parallel_master_taskloop: 13422 case OMPD_parallel_master_taskloop_simd: 13423 CaptureRegion = OMPD_parallel; 13424 break; 13425 case OMPD_target_update: 13426 case OMPD_target_enter_data: 13427 case OMPD_target_exit_data: 13428 case OMPD_target: 13429 case OMPD_target_simd: 13430 case OMPD_target_teams: 13431 case OMPD_target_parallel: 13432 case OMPD_target_teams_distribute: 13433 case OMPD_target_teams_distribute_simd: 13434 case OMPD_target_parallel_for: 13435 case OMPD_target_parallel_for_simd: 13436 case OMPD_target_teams_distribute_parallel_for: 13437 case OMPD_target_teams_distribute_parallel_for_simd: 13438 case OMPD_target_data: 13439 case OMPD_teams_distribute_parallel_for: 13440 case OMPD_teams_distribute_parallel_for_simd: 13441 case OMPD_teams: 13442 case OMPD_teams_distribute: 13443 case OMPD_teams_distribute_simd: 13444 case OMPD_distribute_parallel_for: 13445 case OMPD_distribute_parallel_for_simd: 13446 case OMPD_cancel: 13447 case OMPD_parallel: 13448 case OMPD_parallel_master: 13449 case OMPD_parallel_sections: 13450 case OMPD_parallel_for: 13451 case OMPD_parallel_for_simd: 13452 case OMPD_threadprivate: 13453 case OMPD_allocate: 13454 case OMPD_taskyield: 13455 case OMPD_barrier: 13456 case OMPD_taskwait: 13457 case OMPD_cancellation_point: 13458 case OMPD_flush: 13459 case OMPD_depobj: 13460 case OMPD_scan: 13461 case OMPD_declare_reduction: 13462 case OMPD_declare_mapper: 13463 case OMPD_declare_simd: 13464 case OMPD_declare_variant: 13465 case OMPD_begin_declare_variant: 13466 case OMPD_end_declare_variant: 13467 case OMPD_declare_target: 13468 case OMPD_end_declare_target: 13469 case OMPD_simd: 13470 case OMPD_tile: 13471 case OMPD_for: 13472 case OMPD_for_simd: 13473 case OMPD_sections: 13474 case OMPD_section: 13475 case OMPD_single: 13476 case OMPD_master: 13477 case OMPD_critical: 13478 case OMPD_taskgroup: 13479 case OMPD_distribute: 13480 case OMPD_ordered: 13481 case OMPD_atomic: 13482 case OMPD_distribute_simd: 13483 case OMPD_requires: 13484 llvm_unreachable("Unexpected OpenMP directive with grainsize-clause"); 13485 case OMPD_unknown: 13486 default: 13487 llvm_unreachable("Unknown OpenMP directive"); 13488 } 13489 break; 13490 case OMPC_firstprivate: 13491 case OMPC_lastprivate: 13492 case OMPC_reduction: 13493 case OMPC_task_reduction: 13494 case OMPC_in_reduction: 13495 case OMPC_linear: 13496 case OMPC_default: 13497 case OMPC_proc_bind: 13498 case OMPC_safelen: 13499 case OMPC_simdlen: 13500 case OMPC_sizes: 13501 case OMPC_allocator: 13502 case OMPC_collapse: 13503 case OMPC_private: 13504 case OMPC_shared: 13505 case OMPC_aligned: 13506 case OMPC_copyin: 13507 case OMPC_copyprivate: 13508 case OMPC_ordered: 13509 case OMPC_nowait: 13510 case OMPC_untied: 13511 case OMPC_mergeable: 13512 case OMPC_threadprivate: 13513 case OMPC_allocate: 13514 case OMPC_flush: 13515 case OMPC_depobj: 13516 case OMPC_read: 13517 case OMPC_write: 13518 case OMPC_update: 13519 case OMPC_capture: 13520 case OMPC_seq_cst: 13521 case OMPC_acq_rel: 13522 case OMPC_acquire: 13523 case OMPC_release: 13524 case OMPC_relaxed: 13525 case OMPC_depend: 13526 case OMPC_threads: 13527 case OMPC_simd: 13528 case OMPC_map: 13529 case OMPC_nogroup: 13530 case OMPC_hint: 13531 case OMPC_defaultmap: 13532 case OMPC_unknown: 13533 case OMPC_uniform: 13534 case OMPC_to: 13535 case OMPC_from: 13536 case OMPC_use_device_ptr: 13537 case OMPC_use_device_addr: 13538 case OMPC_is_device_ptr: 13539 case OMPC_unified_address: 13540 case OMPC_unified_shared_memory: 13541 case OMPC_reverse_offload: 13542 case OMPC_dynamic_allocators: 13543 case OMPC_atomic_default_mem_order: 13544 case OMPC_device_type: 13545 case OMPC_match: 13546 case OMPC_nontemporal: 13547 case OMPC_order: 13548 case OMPC_destroy: 13549 case OMPC_detach: 13550 case OMPC_inclusive: 13551 case OMPC_exclusive: 13552 case OMPC_uses_allocators: 13553 case OMPC_affinity: 13554 default: 13555 llvm_unreachable("Unexpected OpenMP clause."); 13556 } 13557 return CaptureRegion; 13558 } 13559 13560 OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier, 13561 Expr *Condition, SourceLocation StartLoc, 13562 SourceLocation LParenLoc, 13563 SourceLocation NameModifierLoc, 13564 SourceLocation ColonLoc, 13565 SourceLocation EndLoc) { 13566 Expr *ValExpr = Condition; 13567 Stmt *HelperValStmt = nullptr; 13568 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 13569 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 13570 !Condition->isInstantiationDependent() && 13571 !Condition->containsUnexpandedParameterPack()) { 13572 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 13573 if (Val.isInvalid()) 13574 return nullptr; 13575 13576 ValExpr = Val.get(); 13577 13578 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 13579 CaptureRegion = getOpenMPCaptureRegionForClause( 13580 DKind, OMPC_if, LangOpts.OpenMP, NameModifier); 13581 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 13582 ValExpr = MakeFullExpr(ValExpr).get(); 13583 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 13584 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 13585 HelperValStmt = buildPreInits(Context, Captures); 13586 } 13587 } 13588 13589 return new (Context) 13590 OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc, 13591 LParenLoc, NameModifierLoc, ColonLoc, EndLoc); 13592 } 13593 13594 OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition, 13595 SourceLocation StartLoc, 13596 SourceLocation LParenLoc, 13597 SourceLocation EndLoc) { 13598 Expr *ValExpr = Condition; 13599 Stmt *HelperValStmt = nullptr; 13600 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 13601 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 13602 !Condition->isInstantiationDependent() && 13603 !Condition->containsUnexpandedParameterPack()) { 13604 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 13605 if (Val.isInvalid()) 13606 return nullptr; 13607 13608 ValExpr = MakeFullExpr(Val.get()).get(); 13609 13610 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 13611 CaptureRegion = 13612 getOpenMPCaptureRegionForClause(DKind, OMPC_final, LangOpts.OpenMP); 13613 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 13614 ValExpr = MakeFullExpr(ValExpr).get(); 13615 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 13616 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 13617 HelperValStmt = buildPreInits(Context, Captures); 13618 } 13619 } 13620 13621 return new (Context) OMPFinalClause(ValExpr, HelperValStmt, CaptureRegion, 13622 StartLoc, LParenLoc, EndLoc); 13623 } 13624 13625 ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc, 13626 Expr *Op) { 13627 if (!Op) 13628 return ExprError(); 13629 13630 class IntConvertDiagnoser : public ICEConvertDiagnoser { 13631 public: 13632 IntConvertDiagnoser() 13633 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {} 13634 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, 13635 QualType T) override { 13636 return S.Diag(Loc, diag::err_omp_not_integral) << T; 13637 } 13638 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc, 13639 QualType T) override { 13640 return S.Diag(Loc, diag::err_omp_incomplete_type) << T; 13641 } 13642 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc, 13643 QualType T, 13644 QualType ConvTy) override { 13645 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy; 13646 } 13647 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv, 13648 QualType ConvTy) override { 13649 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 13650 << ConvTy->isEnumeralType() << ConvTy; 13651 } 13652 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc, 13653 QualType T) override { 13654 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T; 13655 } 13656 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv, 13657 QualType ConvTy) override { 13658 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 13659 << ConvTy->isEnumeralType() << ConvTy; 13660 } 13661 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType, 13662 QualType) override { 13663 llvm_unreachable("conversion functions are permitted"); 13664 } 13665 } ConvertDiagnoser; 13666 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser); 13667 } 13668 13669 static bool 13670 isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef, OpenMPClauseKind CKind, 13671 bool StrictlyPositive, bool BuildCapture = false, 13672 OpenMPDirectiveKind DKind = OMPD_unknown, 13673 OpenMPDirectiveKind *CaptureRegion = nullptr, 13674 Stmt **HelperValStmt = nullptr) { 13675 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() && 13676 !ValExpr->isInstantiationDependent()) { 13677 SourceLocation Loc = ValExpr->getExprLoc(); 13678 ExprResult Value = 13679 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr); 13680 if (Value.isInvalid()) 13681 return false; 13682 13683 ValExpr = Value.get(); 13684 // The expression must evaluate to a non-negative integer value. 13685 if (Optional<llvm::APSInt> Result = 13686 ValExpr->getIntegerConstantExpr(SemaRef.Context)) { 13687 if (Result->isSigned() && 13688 !((!StrictlyPositive && Result->isNonNegative()) || 13689 (StrictlyPositive && Result->isStrictlyPositive()))) { 13690 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause) 13691 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) 13692 << ValExpr->getSourceRange(); 13693 return false; 13694 } 13695 } 13696 if (!BuildCapture) 13697 return true; 13698 *CaptureRegion = 13699 getOpenMPCaptureRegionForClause(DKind, CKind, SemaRef.LangOpts.OpenMP); 13700 if (*CaptureRegion != OMPD_unknown && 13701 !SemaRef.CurContext->isDependentContext()) { 13702 ValExpr = SemaRef.MakeFullExpr(ValExpr).get(); 13703 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 13704 ValExpr = tryBuildCapture(SemaRef, ValExpr, Captures).get(); 13705 *HelperValStmt = buildPreInits(SemaRef.Context, Captures); 13706 } 13707 } 13708 return true; 13709 } 13710 13711 OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads, 13712 SourceLocation StartLoc, 13713 SourceLocation LParenLoc, 13714 SourceLocation EndLoc) { 13715 Expr *ValExpr = NumThreads; 13716 Stmt *HelperValStmt = nullptr; 13717 13718 // OpenMP [2.5, Restrictions] 13719 // The num_threads expression must evaluate to a positive integer value. 13720 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads, 13721 /*StrictlyPositive=*/true)) 13722 return nullptr; 13723 13724 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 13725 OpenMPDirectiveKind CaptureRegion = 13726 getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads, LangOpts.OpenMP); 13727 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 13728 ValExpr = MakeFullExpr(ValExpr).get(); 13729 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 13730 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 13731 HelperValStmt = buildPreInits(Context, Captures); 13732 } 13733 13734 return new (Context) OMPNumThreadsClause( 13735 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc); 13736 } 13737 13738 ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E, 13739 OpenMPClauseKind CKind, 13740 bool StrictlyPositive) { 13741 if (!E) 13742 return ExprError(); 13743 if (E->isValueDependent() || E->isTypeDependent() || 13744 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 13745 return E; 13746 llvm::APSInt Result; 13747 ExprResult ICE = 13748 VerifyIntegerConstantExpression(E, &Result, /*FIXME*/ AllowFold); 13749 if (ICE.isInvalid()) 13750 return ExprError(); 13751 if ((StrictlyPositive && !Result.isStrictlyPositive()) || 13752 (!StrictlyPositive && !Result.isNonNegative())) { 13753 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause) 13754 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) 13755 << E->getSourceRange(); 13756 return ExprError(); 13757 } 13758 if (CKind == OMPC_aligned && !Result.isPowerOf2()) { 13759 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two) 13760 << E->getSourceRange(); 13761 return ExprError(); 13762 } 13763 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1) 13764 DSAStack->setAssociatedLoops(Result.getExtValue()); 13765 else if (CKind == OMPC_ordered) 13766 DSAStack->setAssociatedLoops(Result.getExtValue()); 13767 return ICE; 13768 } 13769 13770 OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc, 13771 SourceLocation LParenLoc, 13772 SourceLocation EndLoc) { 13773 // OpenMP [2.8.1, simd construct, Description] 13774 // The parameter of the safelen clause must be a constant 13775 // positive integer expression. 13776 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen); 13777 if (Safelen.isInvalid()) 13778 return nullptr; 13779 return new (Context) 13780 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc); 13781 } 13782 13783 OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc, 13784 SourceLocation LParenLoc, 13785 SourceLocation EndLoc) { 13786 // OpenMP [2.8.1, simd construct, Description] 13787 // The parameter of the simdlen clause must be a constant 13788 // positive integer expression. 13789 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen); 13790 if (Simdlen.isInvalid()) 13791 return nullptr; 13792 return new (Context) 13793 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc); 13794 } 13795 13796 /// Tries to find omp_allocator_handle_t type. 13797 static bool findOMPAllocatorHandleT(Sema &S, SourceLocation Loc, 13798 DSAStackTy *Stack) { 13799 QualType OMPAllocatorHandleT = Stack->getOMPAllocatorHandleT(); 13800 if (!OMPAllocatorHandleT.isNull()) 13801 return true; 13802 // Build the predefined allocator expressions. 13803 bool ErrorFound = false; 13804 for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) { 13805 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I); 13806 StringRef Allocator = 13807 OMPAllocateDeclAttr::ConvertAllocatorTypeTyToStr(AllocatorKind); 13808 DeclarationName AllocatorName = &S.getASTContext().Idents.get(Allocator); 13809 auto *VD = dyn_cast_or_null<ValueDecl>( 13810 S.LookupSingleName(S.TUScope, AllocatorName, Loc, Sema::LookupAnyName)); 13811 if (!VD) { 13812 ErrorFound = true; 13813 break; 13814 } 13815 QualType AllocatorType = 13816 VD->getType().getNonLValueExprType(S.getASTContext()); 13817 ExprResult Res = S.BuildDeclRefExpr(VD, AllocatorType, VK_LValue, Loc); 13818 if (!Res.isUsable()) { 13819 ErrorFound = true; 13820 break; 13821 } 13822 if (OMPAllocatorHandleT.isNull()) 13823 OMPAllocatorHandleT = AllocatorType; 13824 if (!S.getASTContext().hasSameType(OMPAllocatorHandleT, AllocatorType)) { 13825 ErrorFound = true; 13826 break; 13827 } 13828 Stack->setAllocator(AllocatorKind, Res.get()); 13829 } 13830 if (ErrorFound) { 13831 S.Diag(Loc, diag::err_omp_implied_type_not_found) 13832 << "omp_allocator_handle_t"; 13833 return false; 13834 } 13835 OMPAllocatorHandleT.addConst(); 13836 Stack->setOMPAllocatorHandleT(OMPAllocatorHandleT); 13837 return true; 13838 } 13839 13840 OMPClause *Sema::ActOnOpenMPAllocatorClause(Expr *A, SourceLocation StartLoc, 13841 SourceLocation LParenLoc, 13842 SourceLocation EndLoc) { 13843 // OpenMP [2.11.3, allocate Directive, Description] 13844 // allocator is an expression of omp_allocator_handle_t type. 13845 if (!findOMPAllocatorHandleT(*this, A->getExprLoc(), DSAStack)) 13846 return nullptr; 13847 13848 ExprResult Allocator = DefaultLvalueConversion(A); 13849 if (Allocator.isInvalid()) 13850 return nullptr; 13851 Allocator = PerformImplicitConversion(Allocator.get(), 13852 DSAStack->getOMPAllocatorHandleT(), 13853 Sema::AA_Initializing, 13854 /*AllowExplicit=*/true); 13855 if (Allocator.isInvalid()) 13856 return nullptr; 13857 return new (Context) 13858 OMPAllocatorClause(Allocator.get(), StartLoc, LParenLoc, EndLoc); 13859 } 13860 13861 OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops, 13862 SourceLocation StartLoc, 13863 SourceLocation LParenLoc, 13864 SourceLocation EndLoc) { 13865 // OpenMP [2.7.1, loop construct, Description] 13866 // OpenMP [2.8.1, simd construct, Description] 13867 // OpenMP [2.9.6, distribute construct, Description] 13868 // The parameter of the collapse clause must be a constant 13869 // positive integer expression. 13870 ExprResult NumForLoopsResult = 13871 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse); 13872 if (NumForLoopsResult.isInvalid()) 13873 return nullptr; 13874 return new (Context) 13875 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc); 13876 } 13877 13878 OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc, 13879 SourceLocation EndLoc, 13880 SourceLocation LParenLoc, 13881 Expr *NumForLoops) { 13882 // OpenMP [2.7.1, loop construct, Description] 13883 // OpenMP [2.8.1, simd construct, Description] 13884 // OpenMP [2.9.6, distribute construct, Description] 13885 // The parameter of the ordered clause must be a constant 13886 // positive integer expression if any. 13887 if (NumForLoops && LParenLoc.isValid()) { 13888 ExprResult NumForLoopsResult = 13889 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered); 13890 if (NumForLoopsResult.isInvalid()) 13891 return nullptr; 13892 NumForLoops = NumForLoopsResult.get(); 13893 } else { 13894 NumForLoops = nullptr; 13895 } 13896 auto *Clause = OMPOrderedClause::Create( 13897 Context, NumForLoops, NumForLoops ? DSAStack->getAssociatedLoops() : 0, 13898 StartLoc, LParenLoc, EndLoc); 13899 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops, Clause); 13900 return Clause; 13901 } 13902 13903 OMPClause *Sema::ActOnOpenMPSimpleClause( 13904 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc, 13905 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 13906 OMPClause *Res = nullptr; 13907 switch (Kind) { 13908 case OMPC_default: 13909 Res = ActOnOpenMPDefaultClause(static_cast<DefaultKind>(Argument), 13910 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 13911 break; 13912 case OMPC_proc_bind: 13913 Res = ActOnOpenMPProcBindClause(static_cast<ProcBindKind>(Argument), 13914 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 13915 break; 13916 case OMPC_atomic_default_mem_order: 13917 Res = ActOnOpenMPAtomicDefaultMemOrderClause( 13918 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Argument), 13919 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 13920 break; 13921 case OMPC_order: 13922 Res = ActOnOpenMPOrderClause(static_cast<OpenMPOrderClauseKind>(Argument), 13923 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 13924 break; 13925 case OMPC_update: 13926 Res = ActOnOpenMPUpdateClause(static_cast<OpenMPDependClauseKind>(Argument), 13927 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 13928 break; 13929 case OMPC_if: 13930 case OMPC_final: 13931 case OMPC_num_threads: 13932 case OMPC_safelen: 13933 case OMPC_simdlen: 13934 case OMPC_sizes: 13935 case OMPC_allocator: 13936 case OMPC_collapse: 13937 case OMPC_schedule: 13938 case OMPC_private: 13939 case OMPC_firstprivate: 13940 case OMPC_lastprivate: 13941 case OMPC_shared: 13942 case OMPC_reduction: 13943 case OMPC_task_reduction: 13944 case OMPC_in_reduction: 13945 case OMPC_linear: 13946 case OMPC_aligned: 13947 case OMPC_copyin: 13948 case OMPC_copyprivate: 13949 case OMPC_ordered: 13950 case OMPC_nowait: 13951 case OMPC_untied: 13952 case OMPC_mergeable: 13953 case OMPC_threadprivate: 13954 case OMPC_allocate: 13955 case OMPC_flush: 13956 case OMPC_depobj: 13957 case OMPC_read: 13958 case OMPC_write: 13959 case OMPC_capture: 13960 case OMPC_seq_cst: 13961 case OMPC_acq_rel: 13962 case OMPC_acquire: 13963 case OMPC_release: 13964 case OMPC_relaxed: 13965 case OMPC_depend: 13966 case OMPC_device: 13967 case OMPC_threads: 13968 case OMPC_simd: 13969 case OMPC_map: 13970 case OMPC_num_teams: 13971 case OMPC_thread_limit: 13972 case OMPC_priority: 13973 case OMPC_grainsize: 13974 case OMPC_nogroup: 13975 case OMPC_num_tasks: 13976 case OMPC_hint: 13977 case OMPC_dist_schedule: 13978 case OMPC_defaultmap: 13979 case OMPC_unknown: 13980 case OMPC_uniform: 13981 case OMPC_to: 13982 case OMPC_from: 13983 case OMPC_use_device_ptr: 13984 case OMPC_use_device_addr: 13985 case OMPC_is_device_ptr: 13986 case OMPC_unified_address: 13987 case OMPC_unified_shared_memory: 13988 case OMPC_reverse_offload: 13989 case OMPC_dynamic_allocators: 13990 case OMPC_device_type: 13991 case OMPC_match: 13992 case OMPC_nontemporal: 13993 case OMPC_destroy: 13994 case OMPC_detach: 13995 case OMPC_inclusive: 13996 case OMPC_exclusive: 13997 case OMPC_uses_allocators: 13998 case OMPC_affinity: 13999 default: 14000 llvm_unreachable("Clause is not allowed."); 14001 } 14002 return Res; 14003 } 14004 14005 static std::string 14006 getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last, 14007 ArrayRef<unsigned> Exclude = llvm::None) { 14008 SmallString<256> Buffer; 14009 llvm::raw_svector_ostream Out(Buffer); 14010 unsigned Skipped = Exclude.size(); 14011 auto S = Exclude.begin(), E = Exclude.end(); 14012 for (unsigned I = First; I < Last; ++I) { 14013 if (std::find(S, E, I) != E) { 14014 --Skipped; 14015 continue; 14016 } 14017 Out << "'" << getOpenMPSimpleClauseTypeName(K, I) << "'"; 14018 if (I + Skipped + 2 == Last) 14019 Out << " or "; 14020 else if (I + Skipped + 1 != Last) 14021 Out << ", "; 14022 } 14023 return std::string(Out.str()); 14024 } 14025 14026 OMPClause *Sema::ActOnOpenMPDefaultClause(DefaultKind Kind, 14027 SourceLocation KindKwLoc, 14028 SourceLocation StartLoc, 14029 SourceLocation LParenLoc, 14030 SourceLocation EndLoc) { 14031 if (Kind == OMP_DEFAULT_unknown) { 14032 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 14033 << getListOfPossibleValues(OMPC_default, /*First=*/0, 14034 /*Last=*/unsigned(OMP_DEFAULT_unknown)) 14035 << getOpenMPClauseName(OMPC_default); 14036 return nullptr; 14037 } 14038 14039 switch (Kind) { 14040 case OMP_DEFAULT_none: 14041 DSAStack->setDefaultDSANone(KindKwLoc); 14042 break; 14043 case OMP_DEFAULT_shared: 14044 DSAStack->setDefaultDSAShared(KindKwLoc); 14045 break; 14046 case OMP_DEFAULT_firstprivate: 14047 DSAStack->setDefaultDSAFirstPrivate(KindKwLoc); 14048 break; 14049 default: 14050 llvm_unreachable("DSA unexpected in OpenMP default clause"); 14051 } 14052 14053 return new (Context) 14054 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 14055 } 14056 14057 OMPClause *Sema::ActOnOpenMPProcBindClause(ProcBindKind Kind, 14058 SourceLocation KindKwLoc, 14059 SourceLocation StartLoc, 14060 SourceLocation LParenLoc, 14061 SourceLocation EndLoc) { 14062 if (Kind == OMP_PROC_BIND_unknown) { 14063 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 14064 << getListOfPossibleValues(OMPC_proc_bind, 14065 /*First=*/unsigned(OMP_PROC_BIND_master), 14066 /*Last=*/5) 14067 << getOpenMPClauseName(OMPC_proc_bind); 14068 return nullptr; 14069 } 14070 return new (Context) 14071 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 14072 } 14073 14074 OMPClause *Sema::ActOnOpenMPAtomicDefaultMemOrderClause( 14075 OpenMPAtomicDefaultMemOrderClauseKind Kind, SourceLocation KindKwLoc, 14076 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 14077 if (Kind == OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) { 14078 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 14079 << getListOfPossibleValues( 14080 OMPC_atomic_default_mem_order, /*First=*/0, 14081 /*Last=*/OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) 14082 << getOpenMPClauseName(OMPC_atomic_default_mem_order); 14083 return nullptr; 14084 } 14085 return new (Context) OMPAtomicDefaultMemOrderClause(Kind, KindKwLoc, StartLoc, 14086 LParenLoc, EndLoc); 14087 } 14088 14089 OMPClause *Sema::ActOnOpenMPOrderClause(OpenMPOrderClauseKind Kind, 14090 SourceLocation KindKwLoc, 14091 SourceLocation StartLoc, 14092 SourceLocation LParenLoc, 14093 SourceLocation EndLoc) { 14094 if (Kind == OMPC_ORDER_unknown) { 14095 static_assert(OMPC_ORDER_unknown > 0, 14096 "OMPC_ORDER_unknown not greater than 0"); 14097 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 14098 << getListOfPossibleValues(OMPC_order, /*First=*/0, 14099 /*Last=*/OMPC_ORDER_unknown) 14100 << getOpenMPClauseName(OMPC_order); 14101 return nullptr; 14102 } 14103 return new (Context) 14104 OMPOrderClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 14105 } 14106 14107 OMPClause *Sema::ActOnOpenMPUpdateClause(OpenMPDependClauseKind Kind, 14108 SourceLocation KindKwLoc, 14109 SourceLocation StartLoc, 14110 SourceLocation LParenLoc, 14111 SourceLocation EndLoc) { 14112 if (Kind == OMPC_DEPEND_unknown || Kind == OMPC_DEPEND_source || 14113 Kind == OMPC_DEPEND_sink || Kind == OMPC_DEPEND_depobj) { 14114 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink, 14115 OMPC_DEPEND_depobj}; 14116 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 14117 << getListOfPossibleValues(OMPC_depend, /*First=*/0, 14118 /*Last=*/OMPC_DEPEND_unknown, Except) 14119 << getOpenMPClauseName(OMPC_update); 14120 return nullptr; 14121 } 14122 return OMPUpdateClause::Create(Context, StartLoc, LParenLoc, KindKwLoc, Kind, 14123 EndLoc); 14124 } 14125 14126 OMPClause *Sema::ActOnOpenMPSizesClause(ArrayRef<Expr *> SizeExprs, 14127 SourceLocation StartLoc, 14128 SourceLocation LParenLoc, 14129 SourceLocation EndLoc) { 14130 for (Expr *SizeExpr : SizeExprs) { 14131 ExprResult NumForLoopsResult = VerifyPositiveIntegerConstantInClause( 14132 SizeExpr, OMPC_sizes, /*StrictlyPositive=*/true); 14133 if (!NumForLoopsResult.isUsable()) 14134 return nullptr; 14135 } 14136 14137 DSAStack->setAssociatedLoops(SizeExprs.size()); 14138 return OMPSizesClause::Create(Context, StartLoc, LParenLoc, EndLoc, 14139 SizeExprs); 14140 } 14141 14142 OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause( 14143 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr, 14144 SourceLocation StartLoc, SourceLocation LParenLoc, 14145 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc, 14146 SourceLocation EndLoc) { 14147 OMPClause *Res = nullptr; 14148 switch (Kind) { 14149 case OMPC_schedule: 14150 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements }; 14151 assert(Argument.size() == NumberOfElements && 14152 ArgumentLoc.size() == NumberOfElements); 14153 Res = ActOnOpenMPScheduleClause( 14154 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]), 14155 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]), 14156 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr, 14157 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2], 14158 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc); 14159 break; 14160 case OMPC_if: 14161 assert(Argument.size() == 1 && ArgumentLoc.size() == 1); 14162 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()), 14163 Expr, StartLoc, LParenLoc, ArgumentLoc.back(), 14164 DelimLoc, EndLoc); 14165 break; 14166 case OMPC_dist_schedule: 14167 Res = ActOnOpenMPDistScheduleClause( 14168 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr, 14169 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc); 14170 break; 14171 case OMPC_defaultmap: 14172 enum { Modifier, DefaultmapKind }; 14173 Res = ActOnOpenMPDefaultmapClause( 14174 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]), 14175 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]), 14176 StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind], 14177 EndLoc); 14178 break; 14179 case OMPC_device: 14180 assert(Argument.size() == 1 && ArgumentLoc.size() == 1); 14181 Res = ActOnOpenMPDeviceClause( 14182 static_cast<OpenMPDeviceClauseModifier>(Argument.back()), Expr, 14183 StartLoc, LParenLoc, ArgumentLoc.back(), EndLoc); 14184 break; 14185 case OMPC_final: 14186 case OMPC_num_threads: 14187 case OMPC_safelen: 14188 case OMPC_simdlen: 14189 case OMPC_sizes: 14190 case OMPC_allocator: 14191 case OMPC_collapse: 14192 case OMPC_default: 14193 case OMPC_proc_bind: 14194 case OMPC_private: 14195 case OMPC_firstprivate: 14196 case OMPC_lastprivate: 14197 case OMPC_shared: 14198 case OMPC_reduction: 14199 case OMPC_task_reduction: 14200 case OMPC_in_reduction: 14201 case OMPC_linear: 14202 case OMPC_aligned: 14203 case OMPC_copyin: 14204 case OMPC_copyprivate: 14205 case OMPC_ordered: 14206 case OMPC_nowait: 14207 case OMPC_untied: 14208 case OMPC_mergeable: 14209 case OMPC_threadprivate: 14210 case OMPC_allocate: 14211 case OMPC_flush: 14212 case OMPC_depobj: 14213 case OMPC_read: 14214 case OMPC_write: 14215 case OMPC_update: 14216 case OMPC_capture: 14217 case OMPC_seq_cst: 14218 case OMPC_acq_rel: 14219 case OMPC_acquire: 14220 case OMPC_release: 14221 case OMPC_relaxed: 14222 case OMPC_depend: 14223 case OMPC_threads: 14224 case OMPC_simd: 14225 case OMPC_map: 14226 case OMPC_num_teams: 14227 case OMPC_thread_limit: 14228 case OMPC_priority: 14229 case OMPC_grainsize: 14230 case OMPC_nogroup: 14231 case OMPC_num_tasks: 14232 case OMPC_hint: 14233 case OMPC_unknown: 14234 case OMPC_uniform: 14235 case OMPC_to: 14236 case OMPC_from: 14237 case OMPC_use_device_ptr: 14238 case OMPC_use_device_addr: 14239 case OMPC_is_device_ptr: 14240 case OMPC_unified_address: 14241 case OMPC_unified_shared_memory: 14242 case OMPC_reverse_offload: 14243 case OMPC_dynamic_allocators: 14244 case OMPC_atomic_default_mem_order: 14245 case OMPC_device_type: 14246 case OMPC_match: 14247 case OMPC_nontemporal: 14248 case OMPC_order: 14249 case OMPC_destroy: 14250 case OMPC_detach: 14251 case OMPC_inclusive: 14252 case OMPC_exclusive: 14253 case OMPC_uses_allocators: 14254 case OMPC_affinity: 14255 default: 14256 llvm_unreachable("Clause is not allowed."); 14257 } 14258 return Res; 14259 } 14260 14261 static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1, 14262 OpenMPScheduleClauseModifier M2, 14263 SourceLocation M1Loc, SourceLocation M2Loc) { 14264 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) { 14265 SmallVector<unsigned, 2> Excluded; 14266 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown) 14267 Excluded.push_back(M2); 14268 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) 14269 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic); 14270 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic) 14271 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic); 14272 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value) 14273 << getListOfPossibleValues(OMPC_schedule, 14274 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1, 14275 /*Last=*/OMPC_SCHEDULE_MODIFIER_last, 14276 Excluded) 14277 << getOpenMPClauseName(OMPC_schedule); 14278 return true; 14279 } 14280 return false; 14281 } 14282 14283 OMPClause *Sema::ActOnOpenMPScheduleClause( 14284 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2, 14285 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, 14286 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc, 14287 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) { 14288 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) || 14289 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc)) 14290 return nullptr; 14291 // OpenMP, 2.7.1, Loop Construct, Restrictions 14292 // Either the monotonic modifier or the nonmonotonic modifier can be specified 14293 // but not both. 14294 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) || 14295 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic && 14296 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) || 14297 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic && 14298 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) { 14299 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier) 14300 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2) 14301 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1); 14302 return nullptr; 14303 } 14304 if (Kind == OMPC_SCHEDULE_unknown) { 14305 std::string Values; 14306 if (M1Loc.isInvalid() && M2Loc.isInvalid()) { 14307 unsigned Exclude[] = {OMPC_SCHEDULE_unknown}; 14308 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0, 14309 /*Last=*/OMPC_SCHEDULE_MODIFIER_last, 14310 Exclude); 14311 } else { 14312 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0, 14313 /*Last=*/OMPC_SCHEDULE_unknown); 14314 } 14315 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 14316 << Values << getOpenMPClauseName(OMPC_schedule); 14317 return nullptr; 14318 } 14319 // OpenMP, 2.7.1, Loop Construct, Restrictions 14320 // The nonmonotonic modifier can only be specified with schedule(dynamic) or 14321 // schedule(guided). 14322 // OpenMP 5.0 does not have this restriction. 14323 if (LangOpts.OpenMP < 50 && 14324 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic || 14325 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) && 14326 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) { 14327 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc, 14328 diag::err_omp_schedule_nonmonotonic_static); 14329 return nullptr; 14330 } 14331 Expr *ValExpr = ChunkSize; 14332 Stmt *HelperValStmt = nullptr; 14333 if (ChunkSize) { 14334 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && 14335 !ChunkSize->isInstantiationDependent() && 14336 !ChunkSize->containsUnexpandedParameterPack()) { 14337 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc(); 14338 ExprResult Val = 14339 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); 14340 if (Val.isInvalid()) 14341 return nullptr; 14342 14343 ValExpr = Val.get(); 14344 14345 // OpenMP [2.7.1, Restrictions] 14346 // chunk_size must be a loop invariant integer expression with a positive 14347 // value. 14348 if (Optional<llvm::APSInt> Result = 14349 ValExpr->getIntegerConstantExpr(Context)) { 14350 if (Result->isSigned() && !Result->isStrictlyPositive()) { 14351 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) 14352 << "schedule" << 1 << ChunkSize->getSourceRange(); 14353 return nullptr; 14354 } 14355 } else if (getOpenMPCaptureRegionForClause( 14356 DSAStack->getCurrentDirective(), OMPC_schedule, 14357 LangOpts.OpenMP) != OMPD_unknown && 14358 !CurContext->isDependentContext()) { 14359 ValExpr = MakeFullExpr(ValExpr).get(); 14360 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 14361 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 14362 HelperValStmt = buildPreInits(Context, Captures); 14363 } 14364 } 14365 } 14366 14367 return new (Context) 14368 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind, 14369 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc); 14370 } 14371 14372 OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind, 14373 SourceLocation StartLoc, 14374 SourceLocation EndLoc) { 14375 OMPClause *Res = nullptr; 14376 switch (Kind) { 14377 case OMPC_ordered: 14378 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc); 14379 break; 14380 case OMPC_nowait: 14381 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc); 14382 break; 14383 case OMPC_untied: 14384 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc); 14385 break; 14386 case OMPC_mergeable: 14387 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc); 14388 break; 14389 case OMPC_read: 14390 Res = ActOnOpenMPReadClause(StartLoc, EndLoc); 14391 break; 14392 case OMPC_write: 14393 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc); 14394 break; 14395 case OMPC_update: 14396 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc); 14397 break; 14398 case OMPC_capture: 14399 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc); 14400 break; 14401 case OMPC_seq_cst: 14402 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc); 14403 break; 14404 case OMPC_acq_rel: 14405 Res = ActOnOpenMPAcqRelClause(StartLoc, EndLoc); 14406 break; 14407 case OMPC_acquire: 14408 Res = ActOnOpenMPAcquireClause(StartLoc, EndLoc); 14409 break; 14410 case OMPC_release: 14411 Res = ActOnOpenMPReleaseClause(StartLoc, EndLoc); 14412 break; 14413 case OMPC_relaxed: 14414 Res = ActOnOpenMPRelaxedClause(StartLoc, EndLoc); 14415 break; 14416 case OMPC_threads: 14417 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc); 14418 break; 14419 case OMPC_simd: 14420 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc); 14421 break; 14422 case OMPC_nogroup: 14423 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc); 14424 break; 14425 case OMPC_unified_address: 14426 Res = ActOnOpenMPUnifiedAddressClause(StartLoc, EndLoc); 14427 break; 14428 case OMPC_unified_shared_memory: 14429 Res = ActOnOpenMPUnifiedSharedMemoryClause(StartLoc, EndLoc); 14430 break; 14431 case OMPC_reverse_offload: 14432 Res = ActOnOpenMPReverseOffloadClause(StartLoc, EndLoc); 14433 break; 14434 case OMPC_dynamic_allocators: 14435 Res = ActOnOpenMPDynamicAllocatorsClause(StartLoc, EndLoc); 14436 break; 14437 case OMPC_destroy: 14438 Res = ActOnOpenMPDestroyClause(StartLoc, EndLoc); 14439 break; 14440 case OMPC_if: 14441 case OMPC_final: 14442 case OMPC_num_threads: 14443 case OMPC_safelen: 14444 case OMPC_simdlen: 14445 case OMPC_sizes: 14446 case OMPC_allocator: 14447 case OMPC_collapse: 14448 case OMPC_schedule: 14449 case OMPC_private: 14450 case OMPC_firstprivate: 14451 case OMPC_lastprivate: 14452 case OMPC_shared: 14453 case OMPC_reduction: 14454 case OMPC_task_reduction: 14455 case OMPC_in_reduction: 14456 case OMPC_linear: 14457 case OMPC_aligned: 14458 case OMPC_copyin: 14459 case OMPC_copyprivate: 14460 case OMPC_default: 14461 case OMPC_proc_bind: 14462 case OMPC_threadprivate: 14463 case OMPC_allocate: 14464 case OMPC_flush: 14465 case OMPC_depobj: 14466 case OMPC_depend: 14467 case OMPC_device: 14468 case OMPC_map: 14469 case OMPC_num_teams: 14470 case OMPC_thread_limit: 14471 case OMPC_priority: 14472 case OMPC_grainsize: 14473 case OMPC_num_tasks: 14474 case OMPC_hint: 14475 case OMPC_dist_schedule: 14476 case OMPC_defaultmap: 14477 case OMPC_unknown: 14478 case OMPC_uniform: 14479 case OMPC_to: 14480 case OMPC_from: 14481 case OMPC_use_device_ptr: 14482 case OMPC_use_device_addr: 14483 case OMPC_is_device_ptr: 14484 case OMPC_atomic_default_mem_order: 14485 case OMPC_device_type: 14486 case OMPC_match: 14487 case OMPC_nontemporal: 14488 case OMPC_order: 14489 case OMPC_detach: 14490 case OMPC_inclusive: 14491 case OMPC_exclusive: 14492 case OMPC_uses_allocators: 14493 case OMPC_affinity: 14494 default: 14495 llvm_unreachable("Clause is not allowed."); 14496 } 14497 return Res; 14498 } 14499 14500 OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc, 14501 SourceLocation EndLoc) { 14502 DSAStack->setNowaitRegion(); 14503 return new (Context) OMPNowaitClause(StartLoc, EndLoc); 14504 } 14505 14506 OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc, 14507 SourceLocation EndLoc) { 14508 return new (Context) OMPUntiedClause(StartLoc, EndLoc); 14509 } 14510 14511 OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc, 14512 SourceLocation EndLoc) { 14513 return new (Context) OMPMergeableClause(StartLoc, EndLoc); 14514 } 14515 14516 OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc, 14517 SourceLocation EndLoc) { 14518 return new (Context) OMPReadClause(StartLoc, EndLoc); 14519 } 14520 14521 OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc, 14522 SourceLocation EndLoc) { 14523 return new (Context) OMPWriteClause(StartLoc, EndLoc); 14524 } 14525 14526 OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc, 14527 SourceLocation EndLoc) { 14528 return OMPUpdateClause::Create(Context, StartLoc, EndLoc); 14529 } 14530 14531 OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc, 14532 SourceLocation EndLoc) { 14533 return new (Context) OMPCaptureClause(StartLoc, EndLoc); 14534 } 14535 14536 OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc, 14537 SourceLocation EndLoc) { 14538 return new (Context) OMPSeqCstClause(StartLoc, EndLoc); 14539 } 14540 14541 OMPClause *Sema::ActOnOpenMPAcqRelClause(SourceLocation StartLoc, 14542 SourceLocation EndLoc) { 14543 return new (Context) OMPAcqRelClause(StartLoc, EndLoc); 14544 } 14545 14546 OMPClause *Sema::ActOnOpenMPAcquireClause(SourceLocation StartLoc, 14547 SourceLocation EndLoc) { 14548 return new (Context) OMPAcquireClause(StartLoc, EndLoc); 14549 } 14550 14551 OMPClause *Sema::ActOnOpenMPReleaseClause(SourceLocation StartLoc, 14552 SourceLocation EndLoc) { 14553 return new (Context) OMPReleaseClause(StartLoc, EndLoc); 14554 } 14555 14556 OMPClause *Sema::ActOnOpenMPRelaxedClause(SourceLocation StartLoc, 14557 SourceLocation EndLoc) { 14558 return new (Context) OMPRelaxedClause(StartLoc, EndLoc); 14559 } 14560 14561 OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc, 14562 SourceLocation EndLoc) { 14563 return new (Context) OMPThreadsClause(StartLoc, EndLoc); 14564 } 14565 14566 OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc, 14567 SourceLocation EndLoc) { 14568 return new (Context) OMPSIMDClause(StartLoc, EndLoc); 14569 } 14570 14571 OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc, 14572 SourceLocation EndLoc) { 14573 return new (Context) OMPNogroupClause(StartLoc, EndLoc); 14574 } 14575 14576 OMPClause *Sema::ActOnOpenMPUnifiedAddressClause(SourceLocation StartLoc, 14577 SourceLocation EndLoc) { 14578 return new (Context) OMPUnifiedAddressClause(StartLoc, EndLoc); 14579 } 14580 14581 OMPClause *Sema::ActOnOpenMPUnifiedSharedMemoryClause(SourceLocation StartLoc, 14582 SourceLocation EndLoc) { 14583 return new (Context) OMPUnifiedSharedMemoryClause(StartLoc, EndLoc); 14584 } 14585 14586 OMPClause *Sema::ActOnOpenMPReverseOffloadClause(SourceLocation StartLoc, 14587 SourceLocation EndLoc) { 14588 return new (Context) OMPReverseOffloadClause(StartLoc, EndLoc); 14589 } 14590 14591 OMPClause *Sema::ActOnOpenMPDynamicAllocatorsClause(SourceLocation StartLoc, 14592 SourceLocation EndLoc) { 14593 return new (Context) OMPDynamicAllocatorsClause(StartLoc, EndLoc); 14594 } 14595 14596 OMPClause *Sema::ActOnOpenMPDestroyClause(SourceLocation StartLoc, 14597 SourceLocation EndLoc) { 14598 return new (Context) OMPDestroyClause(StartLoc, EndLoc); 14599 } 14600 14601 OMPClause *Sema::ActOnOpenMPVarListClause( 14602 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *DepModOrTailExpr, 14603 const OMPVarListLocTy &Locs, SourceLocation ColonLoc, 14604 CXXScopeSpec &ReductionOrMapperIdScopeSpec, 14605 DeclarationNameInfo &ReductionOrMapperId, int ExtraModifier, 14606 ArrayRef<OpenMPMapModifierKind> MapTypeModifiers, 14607 ArrayRef<SourceLocation> MapTypeModifiersLoc, bool IsMapTypeImplicit, 14608 SourceLocation ExtraModifierLoc, 14609 ArrayRef<OpenMPMotionModifierKind> MotionModifiers, 14610 ArrayRef<SourceLocation> MotionModifiersLoc) { 14611 SourceLocation StartLoc = Locs.StartLoc; 14612 SourceLocation LParenLoc = Locs.LParenLoc; 14613 SourceLocation EndLoc = Locs.EndLoc; 14614 OMPClause *Res = nullptr; 14615 switch (Kind) { 14616 case OMPC_private: 14617 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc); 14618 break; 14619 case OMPC_firstprivate: 14620 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 14621 break; 14622 case OMPC_lastprivate: 14623 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_LASTPRIVATE_unknown && 14624 "Unexpected lastprivate modifier."); 14625 Res = ActOnOpenMPLastprivateClause( 14626 VarList, static_cast<OpenMPLastprivateModifier>(ExtraModifier), 14627 ExtraModifierLoc, ColonLoc, StartLoc, LParenLoc, EndLoc); 14628 break; 14629 case OMPC_shared: 14630 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc); 14631 break; 14632 case OMPC_reduction: 14633 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_REDUCTION_unknown && 14634 "Unexpected lastprivate modifier."); 14635 Res = ActOnOpenMPReductionClause( 14636 VarList, static_cast<OpenMPReductionClauseModifier>(ExtraModifier), 14637 StartLoc, LParenLoc, ExtraModifierLoc, ColonLoc, EndLoc, 14638 ReductionOrMapperIdScopeSpec, ReductionOrMapperId); 14639 break; 14640 case OMPC_task_reduction: 14641 Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 14642 EndLoc, ReductionOrMapperIdScopeSpec, 14643 ReductionOrMapperId); 14644 break; 14645 case OMPC_in_reduction: 14646 Res = ActOnOpenMPInReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 14647 EndLoc, ReductionOrMapperIdScopeSpec, 14648 ReductionOrMapperId); 14649 break; 14650 case OMPC_linear: 14651 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_LINEAR_unknown && 14652 "Unexpected linear modifier."); 14653 Res = ActOnOpenMPLinearClause( 14654 VarList, DepModOrTailExpr, StartLoc, LParenLoc, 14655 static_cast<OpenMPLinearClauseKind>(ExtraModifier), ExtraModifierLoc, 14656 ColonLoc, EndLoc); 14657 break; 14658 case OMPC_aligned: 14659 Res = ActOnOpenMPAlignedClause(VarList, DepModOrTailExpr, StartLoc, 14660 LParenLoc, ColonLoc, EndLoc); 14661 break; 14662 case OMPC_copyin: 14663 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc); 14664 break; 14665 case OMPC_copyprivate: 14666 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 14667 break; 14668 case OMPC_flush: 14669 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc); 14670 break; 14671 case OMPC_depend: 14672 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_DEPEND_unknown && 14673 "Unexpected depend modifier."); 14674 Res = ActOnOpenMPDependClause( 14675 DepModOrTailExpr, static_cast<OpenMPDependClauseKind>(ExtraModifier), 14676 ExtraModifierLoc, ColonLoc, VarList, StartLoc, LParenLoc, EndLoc); 14677 break; 14678 case OMPC_map: 14679 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_MAP_unknown && 14680 "Unexpected map modifier."); 14681 Res = ActOnOpenMPMapClause( 14682 MapTypeModifiers, MapTypeModifiersLoc, ReductionOrMapperIdScopeSpec, 14683 ReductionOrMapperId, static_cast<OpenMPMapClauseKind>(ExtraModifier), 14684 IsMapTypeImplicit, ExtraModifierLoc, ColonLoc, VarList, Locs); 14685 break; 14686 case OMPC_to: 14687 Res = ActOnOpenMPToClause(MotionModifiers, MotionModifiersLoc, 14688 ReductionOrMapperIdScopeSpec, ReductionOrMapperId, 14689 ColonLoc, VarList, Locs); 14690 break; 14691 case OMPC_from: 14692 Res = ActOnOpenMPFromClause(MotionModifiers, MotionModifiersLoc, 14693 ReductionOrMapperIdScopeSpec, 14694 ReductionOrMapperId, ColonLoc, VarList, Locs); 14695 break; 14696 case OMPC_use_device_ptr: 14697 Res = ActOnOpenMPUseDevicePtrClause(VarList, Locs); 14698 break; 14699 case OMPC_use_device_addr: 14700 Res = ActOnOpenMPUseDeviceAddrClause(VarList, Locs); 14701 break; 14702 case OMPC_is_device_ptr: 14703 Res = ActOnOpenMPIsDevicePtrClause(VarList, Locs); 14704 break; 14705 case OMPC_allocate: 14706 Res = ActOnOpenMPAllocateClause(DepModOrTailExpr, VarList, StartLoc, 14707 LParenLoc, ColonLoc, EndLoc); 14708 break; 14709 case OMPC_nontemporal: 14710 Res = ActOnOpenMPNontemporalClause(VarList, StartLoc, LParenLoc, EndLoc); 14711 break; 14712 case OMPC_inclusive: 14713 Res = ActOnOpenMPInclusiveClause(VarList, StartLoc, LParenLoc, EndLoc); 14714 break; 14715 case OMPC_exclusive: 14716 Res = ActOnOpenMPExclusiveClause(VarList, StartLoc, LParenLoc, EndLoc); 14717 break; 14718 case OMPC_affinity: 14719 Res = ActOnOpenMPAffinityClause(StartLoc, LParenLoc, ColonLoc, EndLoc, 14720 DepModOrTailExpr, VarList); 14721 break; 14722 case OMPC_if: 14723 case OMPC_depobj: 14724 case OMPC_final: 14725 case OMPC_num_threads: 14726 case OMPC_safelen: 14727 case OMPC_simdlen: 14728 case OMPC_sizes: 14729 case OMPC_allocator: 14730 case OMPC_collapse: 14731 case OMPC_default: 14732 case OMPC_proc_bind: 14733 case OMPC_schedule: 14734 case OMPC_ordered: 14735 case OMPC_nowait: 14736 case OMPC_untied: 14737 case OMPC_mergeable: 14738 case OMPC_threadprivate: 14739 case OMPC_read: 14740 case OMPC_write: 14741 case OMPC_update: 14742 case OMPC_capture: 14743 case OMPC_seq_cst: 14744 case OMPC_acq_rel: 14745 case OMPC_acquire: 14746 case OMPC_release: 14747 case OMPC_relaxed: 14748 case OMPC_device: 14749 case OMPC_threads: 14750 case OMPC_simd: 14751 case OMPC_num_teams: 14752 case OMPC_thread_limit: 14753 case OMPC_priority: 14754 case OMPC_grainsize: 14755 case OMPC_nogroup: 14756 case OMPC_num_tasks: 14757 case OMPC_hint: 14758 case OMPC_dist_schedule: 14759 case OMPC_defaultmap: 14760 case OMPC_unknown: 14761 case OMPC_uniform: 14762 case OMPC_unified_address: 14763 case OMPC_unified_shared_memory: 14764 case OMPC_reverse_offload: 14765 case OMPC_dynamic_allocators: 14766 case OMPC_atomic_default_mem_order: 14767 case OMPC_device_type: 14768 case OMPC_match: 14769 case OMPC_order: 14770 case OMPC_destroy: 14771 case OMPC_detach: 14772 case OMPC_uses_allocators: 14773 default: 14774 llvm_unreachable("Clause is not allowed."); 14775 } 14776 return Res; 14777 } 14778 14779 ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK, 14780 ExprObjectKind OK, SourceLocation Loc) { 14781 ExprResult Res = BuildDeclRefExpr( 14782 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc); 14783 if (!Res.isUsable()) 14784 return ExprError(); 14785 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) { 14786 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get()); 14787 if (!Res.isUsable()) 14788 return ExprError(); 14789 } 14790 if (VK != VK_LValue && Res.get()->isGLValue()) { 14791 Res = DefaultLvalueConversion(Res.get()); 14792 if (!Res.isUsable()) 14793 return ExprError(); 14794 } 14795 return Res; 14796 } 14797 14798 OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList, 14799 SourceLocation StartLoc, 14800 SourceLocation LParenLoc, 14801 SourceLocation EndLoc) { 14802 SmallVector<Expr *, 8> Vars; 14803 SmallVector<Expr *, 8> PrivateCopies; 14804 for (Expr *RefExpr : VarList) { 14805 assert(RefExpr && "NULL expr in OpenMP private clause."); 14806 SourceLocation ELoc; 14807 SourceRange ERange; 14808 Expr *SimpleRefExpr = RefExpr; 14809 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 14810 if (Res.second) { 14811 // It will be analyzed later. 14812 Vars.push_back(RefExpr); 14813 PrivateCopies.push_back(nullptr); 14814 } 14815 ValueDecl *D = Res.first; 14816 if (!D) 14817 continue; 14818 14819 QualType Type = D->getType(); 14820 auto *VD = dyn_cast<VarDecl>(D); 14821 14822 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 14823 // A variable that appears in a private clause must not have an incomplete 14824 // type or a reference type. 14825 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type)) 14826 continue; 14827 Type = Type.getNonReferenceType(); 14828 14829 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions] 14830 // A variable that is privatized must not have a const-qualified type 14831 // unless it is of class type with a mutable member. This restriction does 14832 // not apply to the firstprivate clause. 14833 // 14834 // OpenMP 3.1 [2.9.3.3, private clause, Restrictions] 14835 // A variable that appears in a private clause must not have a 14836 // const-qualified type unless it is of class type with a mutable member. 14837 if (rejectConstNotMutableType(*this, D, Type, OMPC_private, ELoc)) 14838 continue; 14839 14840 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 14841 // in a Construct] 14842 // Variables with the predetermined data-sharing attributes may not be 14843 // listed in data-sharing attributes clauses, except for the cases 14844 // listed below. For these exceptions only, listing a predetermined 14845 // variable in a data-sharing attribute clause is allowed and overrides 14846 // the variable's predetermined data-sharing attributes. 14847 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 14848 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) { 14849 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 14850 << getOpenMPClauseName(OMPC_private); 14851 reportOriginalDsa(*this, DSAStack, D, DVar); 14852 continue; 14853 } 14854 14855 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 14856 // Variably modified types are not supported for tasks. 14857 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && 14858 isOpenMPTaskingDirective(CurrDir)) { 14859 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 14860 << getOpenMPClauseName(OMPC_private) << Type 14861 << getOpenMPDirectiveName(CurrDir); 14862 bool IsDecl = 14863 !VD || 14864 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 14865 Diag(D->getLocation(), 14866 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 14867 << D; 14868 continue; 14869 } 14870 14871 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 14872 // A list item cannot appear in both a map clause and a data-sharing 14873 // attribute clause on the same construct 14874 // 14875 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7] 14876 // A list item cannot appear in both a map clause and a data-sharing 14877 // attribute clause on the same construct unless the construct is a 14878 // combined construct. 14879 if ((LangOpts.OpenMP <= 45 && isOpenMPTargetExecutionDirective(CurrDir)) || 14880 CurrDir == OMPD_target) { 14881 OpenMPClauseKind ConflictKind; 14882 if (DSAStack->checkMappableExprComponentListsForDecl( 14883 VD, /*CurrentRegionOnly=*/true, 14884 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef, 14885 OpenMPClauseKind WhereFoundClauseKind) -> bool { 14886 ConflictKind = WhereFoundClauseKind; 14887 return true; 14888 })) { 14889 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 14890 << getOpenMPClauseName(OMPC_private) 14891 << getOpenMPClauseName(ConflictKind) 14892 << getOpenMPDirectiveName(CurrDir); 14893 reportOriginalDsa(*this, DSAStack, D, DVar); 14894 continue; 14895 } 14896 } 14897 14898 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1] 14899 // A variable of class type (or array thereof) that appears in a private 14900 // clause requires an accessible, unambiguous default constructor for the 14901 // class type. 14902 // Generate helper private variable and initialize it with the default 14903 // value. The address of the original variable is replaced by the address of 14904 // the new private variable in CodeGen. This new variable is not added to 14905 // IdResolver, so the code in the OpenMP region uses original variable for 14906 // proper diagnostics. 14907 Type = Type.getUnqualifiedType(); 14908 VarDecl *VDPrivate = 14909 buildVarDecl(*this, ELoc, Type, D->getName(), 14910 D->hasAttrs() ? &D->getAttrs() : nullptr, 14911 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 14912 ActOnUninitializedDecl(VDPrivate); 14913 if (VDPrivate->isInvalidDecl()) 14914 continue; 14915 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 14916 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc); 14917 14918 DeclRefExpr *Ref = nullptr; 14919 if (!VD && !CurContext->isDependentContext()) 14920 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 14921 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref); 14922 Vars.push_back((VD || CurContext->isDependentContext()) 14923 ? RefExpr->IgnoreParens() 14924 : Ref); 14925 PrivateCopies.push_back(VDPrivateRefExpr); 14926 } 14927 14928 if (Vars.empty()) 14929 return nullptr; 14930 14931 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 14932 PrivateCopies); 14933 } 14934 14935 namespace { 14936 class DiagsUninitializedSeveretyRAII { 14937 private: 14938 DiagnosticsEngine &Diags; 14939 SourceLocation SavedLoc; 14940 bool IsIgnored = false; 14941 14942 public: 14943 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc, 14944 bool IsIgnored) 14945 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) { 14946 if (!IsIgnored) { 14947 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init, 14948 /*Map*/ diag::Severity::Ignored, Loc); 14949 } 14950 } 14951 ~DiagsUninitializedSeveretyRAII() { 14952 if (!IsIgnored) 14953 Diags.popMappings(SavedLoc); 14954 } 14955 }; 14956 } 14957 14958 OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList, 14959 SourceLocation StartLoc, 14960 SourceLocation LParenLoc, 14961 SourceLocation EndLoc) { 14962 SmallVector<Expr *, 8> Vars; 14963 SmallVector<Expr *, 8> PrivateCopies; 14964 SmallVector<Expr *, 8> Inits; 14965 SmallVector<Decl *, 4> ExprCaptures; 14966 bool IsImplicitClause = 14967 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid(); 14968 SourceLocation ImplicitClauseLoc = DSAStack->getConstructLoc(); 14969 14970 for (Expr *RefExpr : VarList) { 14971 assert(RefExpr && "NULL expr in OpenMP firstprivate clause."); 14972 SourceLocation ELoc; 14973 SourceRange ERange; 14974 Expr *SimpleRefExpr = RefExpr; 14975 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 14976 if (Res.second) { 14977 // It will be analyzed later. 14978 Vars.push_back(RefExpr); 14979 PrivateCopies.push_back(nullptr); 14980 Inits.push_back(nullptr); 14981 } 14982 ValueDecl *D = Res.first; 14983 if (!D) 14984 continue; 14985 14986 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc; 14987 QualType Type = D->getType(); 14988 auto *VD = dyn_cast<VarDecl>(D); 14989 14990 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 14991 // A variable that appears in a private clause must not have an incomplete 14992 // type or a reference type. 14993 if (RequireCompleteType(ELoc, Type, 14994 diag::err_omp_firstprivate_incomplete_type)) 14995 continue; 14996 Type = Type.getNonReferenceType(); 14997 14998 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1] 14999 // A variable of class type (or array thereof) that appears in a private 15000 // clause requires an accessible, unambiguous copy constructor for the 15001 // class type. 15002 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType(); 15003 15004 // If an implicit firstprivate variable found it was checked already. 15005 DSAStackTy::DSAVarData TopDVar; 15006 if (!IsImplicitClause) { 15007 DSAStackTy::DSAVarData DVar = 15008 DSAStack->getTopDSA(D, /*FromParent=*/false); 15009 TopDVar = DVar; 15010 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 15011 bool IsConstant = ElemType.isConstant(Context); 15012 // OpenMP [2.4.13, Data-sharing Attribute Clauses] 15013 // A list item that specifies a given variable may not appear in more 15014 // than one clause on the same directive, except that a variable may be 15015 // specified in both firstprivate and lastprivate clauses. 15016 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3] 15017 // A list item may appear in a firstprivate or lastprivate clause but not 15018 // both. 15019 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate && 15020 (isOpenMPDistributeDirective(CurrDir) || 15021 DVar.CKind != OMPC_lastprivate) && 15022 DVar.RefExpr) { 15023 Diag(ELoc, diag::err_omp_wrong_dsa) 15024 << getOpenMPClauseName(DVar.CKind) 15025 << getOpenMPClauseName(OMPC_firstprivate); 15026 reportOriginalDsa(*this, DSAStack, D, DVar); 15027 continue; 15028 } 15029 15030 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 15031 // in a Construct] 15032 // Variables with the predetermined data-sharing attributes may not be 15033 // listed in data-sharing attributes clauses, except for the cases 15034 // listed below. For these exceptions only, listing a predetermined 15035 // variable in a data-sharing attribute clause is allowed and overrides 15036 // the variable's predetermined data-sharing attributes. 15037 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 15038 // in a Construct, C/C++, p.2] 15039 // Variables with const-qualified type having no mutable member may be 15040 // listed in a firstprivate clause, even if they are static data members. 15041 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr && 15042 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) { 15043 Diag(ELoc, diag::err_omp_wrong_dsa) 15044 << getOpenMPClauseName(DVar.CKind) 15045 << getOpenMPClauseName(OMPC_firstprivate); 15046 reportOriginalDsa(*this, DSAStack, D, DVar); 15047 continue; 15048 } 15049 15050 // OpenMP [2.9.3.4, Restrictions, p.2] 15051 // A list item that is private within a parallel region must not appear 15052 // in a firstprivate clause on a worksharing construct if any of the 15053 // worksharing regions arising from the worksharing construct ever bind 15054 // to any of the parallel regions arising from the parallel construct. 15055 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3] 15056 // A list item that is private within a teams region must not appear in a 15057 // firstprivate clause on a distribute construct if any of the distribute 15058 // regions arising from the distribute construct ever bind to any of the 15059 // teams regions arising from the teams construct. 15060 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3] 15061 // A list item that appears in a reduction clause of a teams construct 15062 // must not appear in a firstprivate clause on a distribute construct if 15063 // any of the distribute regions arising from the distribute construct 15064 // ever bind to any of the teams regions arising from the teams construct. 15065 if ((isOpenMPWorksharingDirective(CurrDir) || 15066 isOpenMPDistributeDirective(CurrDir)) && 15067 !isOpenMPParallelDirective(CurrDir) && 15068 !isOpenMPTeamsDirective(CurrDir)) { 15069 DVar = DSAStack->getImplicitDSA(D, true); 15070 if (DVar.CKind != OMPC_shared && 15071 (isOpenMPParallelDirective(DVar.DKind) || 15072 isOpenMPTeamsDirective(DVar.DKind) || 15073 DVar.DKind == OMPD_unknown)) { 15074 Diag(ELoc, diag::err_omp_required_access) 15075 << getOpenMPClauseName(OMPC_firstprivate) 15076 << getOpenMPClauseName(OMPC_shared); 15077 reportOriginalDsa(*this, DSAStack, D, DVar); 15078 continue; 15079 } 15080 } 15081 // OpenMP [2.9.3.4, Restrictions, p.3] 15082 // A list item that appears in a reduction clause of a parallel construct 15083 // must not appear in a firstprivate clause on a worksharing or task 15084 // construct if any of the worksharing or task regions arising from the 15085 // worksharing or task construct ever bind to any of the parallel regions 15086 // arising from the parallel construct. 15087 // OpenMP [2.9.3.4, Restrictions, p.4] 15088 // A list item that appears in a reduction clause in worksharing 15089 // construct must not appear in a firstprivate clause in a task construct 15090 // encountered during execution of any of the worksharing regions arising 15091 // from the worksharing construct. 15092 if (isOpenMPTaskingDirective(CurrDir)) { 15093 DVar = DSAStack->hasInnermostDSA( 15094 D, 15095 [](OpenMPClauseKind C, bool AppliedToPointee) { 15096 return C == OMPC_reduction && !AppliedToPointee; 15097 }, 15098 [](OpenMPDirectiveKind K) { 15099 return isOpenMPParallelDirective(K) || 15100 isOpenMPWorksharingDirective(K) || 15101 isOpenMPTeamsDirective(K); 15102 }, 15103 /*FromParent=*/true); 15104 if (DVar.CKind == OMPC_reduction && 15105 (isOpenMPParallelDirective(DVar.DKind) || 15106 isOpenMPWorksharingDirective(DVar.DKind) || 15107 isOpenMPTeamsDirective(DVar.DKind))) { 15108 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate) 15109 << getOpenMPDirectiveName(DVar.DKind); 15110 reportOriginalDsa(*this, DSAStack, D, DVar); 15111 continue; 15112 } 15113 } 15114 15115 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 15116 // A list item cannot appear in both a map clause and a data-sharing 15117 // attribute clause on the same construct 15118 // 15119 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7] 15120 // A list item cannot appear in both a map clause and a data-sharing 15121 // attribute clause on the same construct unless the construct is a 15122 // combined construct. 15123 if ((LangOpts.OpenMP <= 45 && 15124 isOpenMPTargetExecutionDirective(CurrDir)) || 15125 CurrDir == OMPD_target) { 15126 OpenMPClauseKind ConflictKind; 15127 if (DSAStack->checkMappableExprComponentListsForDecl( 15128 VD, /*CurrentRegionOnly=*/true, 15129 [&ConflictKind]( 15130 OMPClauseMappableExprCommon::MappableExprComponentListRef, 15131 OpenMPClauseKind WhereFoundClauseKind) { 15132 ConflictKind = WhereFoundClauseKind; 15133 return true; 15134 })) { 15135 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 15136 << getOpenMPClauseName(OMPC_firstprivate) 15137 << getOpenMPClauseName(ConflictKind) 15138 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 15139 reportOriginalDsa(*this, DSAStack, D, DVar); 15140 continue; 15141 } 15142 } 15143 } 15144 15145 // Variably modified types are not supported for tasks. 15146 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && 15147 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) { 15148 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 15149 << getOpenMPClauseName(OMPC_firstprivate) << Type 15150 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 15151 bool IsDecl = 15152 !VD || 15153 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 15154 Diag(D->getLocation(), 15155 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 15156 << D; 15157 continue; 15158 } 15159 15160 Type = Type.getUnqualifiedType(); 15161 VarDecl *VDPrivate = 15162 buildVarDecl(*this, ELoc, Type, D->getName(), 15163 D->hasAttrs() ? &D->getAttrs() : nullptr, 15164 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 15165 // Generate helper private variable and initialize it with the value of the 15166 // original variable. The address of the original variable is replaced by 15167 // the address of the new private variable in the CodeGen. This new variable 15168 // is not added to IdResolver, so the code in the OpenMP region uses 15169 // original variable for proper diagnostics and variable capturing. 15170 Expr *VDInitRefExpr = nullptr; 15171 // For arrays generate initializer for single element and replace it by the 15172 // original array element in CodeGen. 15173 if (Type->isArrayType()) { 15174 VarDecl *VDInit = 15175 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName()); 15176 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc); 15177 Expr *Init = DefaultLvalueConversion(VDInitRefExpr).get(); 15178 ElemType = ElemType.getUnqualifiedType(); 15179 VarDecl *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, 15180 ".firstprivate.temp"); 15181 InitializedEntity Entity = 15182 InitializedEntity::InitializeVariable(VDInitTemp); 15183 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc); 15184 15185 InitializationSequence InitSeq(*this, Entity, Kind, Init); 15186 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init); 15187 if (Result.isInvalid()) 15188 VDPrivate->setInvalidDecl(); 15189 else 15190 VDPrivate->setInit(Result.getAs<Expr>()); 15191 // Remove temp variable declaration. 15192 Context.Deallocate(VDInitTemp); 15193 } else { 15194 VarDecl *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type, 15195 ".firstprivate.temp"); 15196 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(), 15197 RefExpr->getExprLoc()); 15198 AddInitializerToDecl(VDPrivate, 15199 DefaultLvalueConversion(VDInitRefExpr).get(), 15200 /*DirectInit=*/false); 15201 } 15202 if (VDPrivate->isInvalidDecl()) { 15203 if (IsImplicitClause) { 15204 Diag(RefExpr->getExprLoc(), 15205 diag::note_omp_task_predetermined_firstprivate_here); 15206 } 15207 continue; 15208 } 15209 CurContext->addDecl(VDPrivate); 15210 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 15211 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), 15212 RefExpr->getExprLoc()); 15213 DeclRefExpr *Ref = nullptr; 15214 if (!VD && !CurContext->isDependentContext()) { 15215 if (TopDVar.CKind == OMPC_lastprivate) { 15216 Ref = TopDVar.PrivateCopy; 15217 } else { 15218 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 15219 if (!isOpenMPCapturedDecl(D)) 15220 ExprCaptures.push_back(Ref->getDecl()); 15221 } 15222 } 15223 if (!IsImplicitClause) 15224 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 15225 Vars.push_back((VD || CurContext->isDependentContext()) 15226 ? RefExpr->IgnoreParens() 15227 : Ref); 15228 PrivateCopies.push_back(VDPrivateRefExpr); 15229 Inits.push_back(VDInitRefExpr); 15230 } 15231 15232 if (Vars.empty()) 15233 return nullptr; 15234 15235 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 15236 Vars, PrivateCopies, Inits, 15237 buildPreInits(Context, ExprCaptures)); 15238 } 15239 15240 OMPClause *Sema::ActOnOpenMPLastprivateClause( 15241 ArrayRef<Expr *> VarList, OpenMPLastprivateModifier LPKind, 15242 SourceLocation LPKindLoc, SourceLocation ColonLoc, SourceLocation StartLoc, 15243 SourceLocation LParenLoc, SourceLocation EndLoc) { 15244 if (LPKind == OMPC_LASTPRIVATE_unknown && LPKindLoc.isValid()) { 15245 assert(ColonLoc.isValid() && "Colon location must be valid."); 15246 Diag(LPKindLoc, diag::err_omp_unexpected_clause_value) 15247 << getListOfPossibleValues(OMPC_lastprivate, /*First=*/0, 15248 /*Last=*/OMPC_LASTPRIVATE_unknown) 15249 << getOpenMPClauseName(OMPC_lastprivate); 15250 return nullptr; 15251 } 15252 15253 SmallVector<Expr *, 8> Vars; 15254 SmallVector<Expr *, 8> SrcExprs; 15255 SmallVector<Expr *, 8> DstExprs; 15256 SmallVector<Expr *, 8> AssignmentOps; 15257 SmallVector<Decl *, 4> ExprCaptures; 15258 SmallVector<Expr *, 4> ExprPostUpdates; 15259 for (Expr *RefExpr : VarList) { 15260 assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); 15261 SourceLocation ELoc; 15262 SourceRange ERange; 15263 Expr *SimpleRefExpr = RefExpr; 15264 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 15265 if (Res.second) { 15266 // It will be analyzed later. 15267 Vars.push_back(RefExpr); 15268 SrcExprs.push_back(nullptr); 15269 DstExprs.push_back(nullptr); 15270 AssignmentOps.push_back(nullptr); 15271 } 15272 ValueDecl *D = Res.first; 15273 if (!D) 15274 continue; 15275 15276 QualType Type = D->getType(); 15277 auto *VD = dyn_cast<VarDecl>(D); 15278 15279 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2] 15280 // A variable that appears in a lastprivate clause must not have an 15281 // incomplete type or a reference type. 15282 if (RequireCompleteType(ELoc, Type, 15283 diag::err_omp_lastprivate_incomplete_type)) 15284 continue; 15285 Type = Type.getNonReferenceType(); 15286 15287 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions] 15288 // A variable that is privatized must not have a const-qualified type 15289 // unless it is of class type with a mutable member. This restriction does 15290 // not apply to the firstprivate clause. 15291 // 15292 // OpenMP 3.1 [2.9.3.5, lastprivate clause, Restrictions] 15293 // A variable that appears in a lastprivate clause must not have a 15294 // const-qualified type unless it is of class type with a mutable member. 15295 if (rejectConstNotMutableType(*this, D, Type, OMPC_lastprivate, ELoc)) 15296 continue; 15297 15298 // OpenMP 5.0 [2.19.4.5 lastprivate Clause, Restrictions] 15299 // A list item that appears in a lastprivate clause with the conditional 15300 // modifier must be a scalar variable. 15301 if (LPKind == OMPC_LASTPRIVATE_conditional && !Type->isScalarType()) { 15302 Diag(ELoc, diag::err_omp_lastprivate_conditional_non_scalar); 15303 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 15304 VarDecl::DeclarationOnly; 15305 Diag(D->getLocation(), 15306 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 15307 << D; 15308 continue; 15309 } 15310 15311 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 15312 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 15313 // in a Construct] 15314 // Variables with the predetermined data-sharing attributes may not be 15315 // listed in data-sharing attributes clauses, except for the cases 15316 // listed below. 15317 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3] 15318 // A list item may appear in a firstprivate or lastprivate clause but not 15319 // both. 15320 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 15321 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate && 15322 (isOpenMPDistributeDirective(CurrDir) || 15323 DVar.CKind != OMPC_firstprivate) && 15324 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) { 15325 Diag(ELoc, diag::err_omp_wrong_dsa) 15326 << getOpenMPClauseName(DVar.CKind) 15327 << getOpenMPClauseName(OMPC_lastprivate); 15328 reportOriginalDsa(*this, DSAStack, D, DVar); 15329 continue; 15330 } 15331 15332 // OpenMP [2.14.3.5, Restrictions, p.2] 15333 // A list item that is private within a parallel region, or that appears in 15334 // the reduction clause of a parallel construct, must not appear in a 15335 // lastprivate clause on a worksharing construct if any of the corresponding 15336 // worksharing regions ever binds to any of the corresponding parallel 15337 // regions. 15338 DSAStackTy::DSAVarData TopDVar = DVar; 15339 if (isOpenMPWorksharingDirective(CurrDir) && 15340 !isOpenMPParallelDirective(CurrDir) && 15341 !isOpenMPTeamsDirective(CurrDir)) { 15342 DVar = DSAStack->getImplicitDSA(D, true); 15343 if (DVar.CKind != OMPC_shared) { 15344 Diag(ELoc, diag::err_omp_required_access) 15345 << getOpenMPClauseName(OMPC_lastprivate) 15346 << getOpenMPClauseName(OMPC_shared); 15347 reportOriginalDsa(*this, DSAStack, D, DVar); 15348 continue; 15349 } 15350 } 15351 15352 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2] 15353 // A variable of class type (or array thereof) that appears in a 15354 // lastprivate clause requires an accessible, unambiguous default 15355 // constructor for the class type, unless the list item is also specified 15356 // in a firstprivate clause. 15357 // A variable of class type (or array thereof) that appears in a 15358 // lastprivate clause requires an accessible, unambiguous copy assignment 15359 // operator for the class type. 15360 Type = Context.getBaseElementType(Type).getNonReferenceType(); 15361 VarDecl *SrcVD = buildVarDecl(*this, ERange.getBegin(), 15362 Type.getUnqualifiedType(), ".lastprivate.src", 15363 D->hasAttrs() ? &D->getAttrs() : nullptr); 15364 DeclRefExpr *PseudoSrcExpr = 15365 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc); 15366 VarDecl *DstVD = 15367 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst", 15368 D->hasAttrs() ? &D->getAttrs() : nullptr); 15369 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc); 15370 // For arrays generate assignment operation for single element and replace 15371 // it by the original array element in CodeGen. 15372 ExprResult AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign, 15373 PseudoDstExpr, PseudoSrcExpr); 15374 if (AssignmentOp.isInvalid()) 15375 continue; 15376 AssignmentOp = 15377 ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false); 15378 if (AssignmentOp.isInvalid()) 15379 continue; 15380 15381 DeclRefExpr *Ref = nullptr; 15382 if (!VD && !CurContext->isDependentContext()) { 15383 if (TopDVar.CKind == OMPC_firstprivate) { 15384 Ref = TopDVar.PrivateCopy; 15385 } else { 15386 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 15387 if (!isOpenMPCapturedDecl(D)) 15388 ExprCaptures.push_back(Ref->getDecl()); 15389 } 15390 if ((TopDVar.CKind == OMPC_firstprivate && !TopDVar.PrivateCopy) || 15391 (!isOpenMPCapturedDecl(D) && 15392 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) { 15393 ExprResult RefRes = DefaultLvalueConversion(Ref); 15394 if (!RefRes.isUsable()) 15395 continue; 15396 ExprResult PostUpdateRes = 15397 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr, 15398 RefRes.get()); 15399 if (!PostUpdateRes.isUsable()) 15400 continue; 15401 ExprPostUpdates.push_back( 15402 IgnoredValueConversions(PostUpdateRes.get()).get()); 15403 } 15404 } 15405 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref); 15406 Vars.push_back((VD || CurContext->isDependentContext()) 15407 ? RefExpr->IgnoreParens() 15408 : Ref); 15409 SrcExprs.push_back(PseudoSrcExpr); 15410 DstExprs.push_back(PseudoDstExpr); 15411 AssignmentOps.push_back(AssignmentOp.get()); 15412 } 15413 15414 if (Vars.empty()) 15415 return nullptr; 15416 15417 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 15418 Vars, SrcExprs, DstExprs, AssignmentOps, 15419 LPKind, LPKindLoc, ColonLoc, 15420 buildPreInits(Context, ExprCaptures), 15421 buildPostUpdate(*this, ExprPostUpdates)); 15422 } 15423 15424 OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList, 15425 SourceLocation StartLoc, 15426 SourceLocation LParenLoc, 15427 SourceLocation EndLoc) { 15428 SmallVector<Expr *, 8> Vars; 15429 for (Expr *RefExpr : VarList) { 15430 assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); 15431 SourceLocation ELoc; 15432 SourceRange ERange; 15433 Expr *SimpleRefExpr = RefExpr; 15434 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 15435 if (Res.second) { 15436 // It will be analyzed later. 15437 Vars.push_back(RefExpr); 15438 } 15439 ValueDecl *D = Res.first; 15440 if (!D) 15441 continue; 15442 15443 auto *VD = dyn_cast<VarDecl>(D); 15444 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 15445 // in a Construct] 15446 // Variables with the predetermined data-sharing attributes may not be 15447 // listed in data-sharing attributes clauses, except for the cases 15448 // listed below. For these exceptions only, listing a predetermined 15449 // variable in a data-sharing attribute clause is allowed and overrides 15450 // the variable's predetermined data-sharing attributes. 15451 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 15452 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared && 15453 DVar.RefExpr) { 15454 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 15455 << getOpenMPClauseName(OMPC_shared); 15456 reportOriginalDsa(*this, DSAStack, D, DVar); 15457 continue; 15458 } 15459 15460 DeclRefExpr *Ref = nullptr; 15461 if (!VD && isOpenMPCapturedDecl(D) && !CurContext->isDependentContext()) 15462 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 15463 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref); 15464 Vars.push_back((VD || !Ref || CurContext->isDependentContext()) 15465 ? RefExpr->IgnoreParens() 15466 : Ref); 15467 } 15468 15469 if (Vars.empty()) 15470 return nullptr; 15471 15472 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 15473 } 15474 15475 namespace { 15476 class DSARefChecker : public StmtVisitor<DSARefChecker, bool> { 15477 DSAStackTy *Stack; 15478 15479 public: 15480 bool VisitDeclRefExpr(DeclRefExpr *E) { 15481 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 15482 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false); 15483 if (DVar.CKind == OMPC_shared && !DVar.RefExpr) 15484 return false; 15485 if (DVar.CKind != OMPC_unknown) 15486 return true; 15487 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA( 15488 VD, 15489 [](OpenMPClauseKind C, bool AppliedToPointee) { 15490 return isOpenMPPrivate(C) && !AppliedToPointee; 15491 }, 15492 [](OpenMPDirectiveKind) { return true; }, 15493 /*FromParent=*/true); 15494 return DVarPrivate.CKind != OMPC_unknown; 15495 } 15496 return false; 15497 } 15498 bool VisitStmt(Stmt *S) { 15499 for (Stmt *Child : S->children()) { 15500 if (Child && Visit(Child)) 15501 return true; 15502 } 15503 return false; 15504 } 15505 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {} 15506 }; 15507 } // namespace 15508 15509 namespace { 15510 // Transform MemberExpression for specified FieldDecl of current class to 15511 // DeclRefExpr to specified OMPCapturedExprDecl. 15512 class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> { 15513 typedef TreeTransform<TransformExprToCaptures> BaseTransform; 15514 ValueDecl *Field = nullptr; 15515 DeclRefExpr *CapturedExpr = nullptr; 15516 15517 public: 15518 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl) 15519 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {} 15520 15521 ExprResult TransformMemberExpr(MemberExpr *E) { 15522 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) && 15523 E->getMemberDecl() == Field) { 15524 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false); 15525 return CapturedExpr; 15526 } 15527 return BaseTransform::TransformMemberExpr(E); 15528 } 15529 DeclRefExpr *getCapturedExpr() { return CapturedExpr; } 15530 }; 15531 } // namespace 15532 15533 template <typename T, typename U> 15534 static T filterLookupForUDReductionAndMapper( 15535 SmallVectorImpl<U> &Lookups, const llvm::function_ref<T(ValueDecl *)> Gen) { 15536 for (U &Set : Lookups) { 15537 for (auto *D : Set) { 15538 if (T Res = Gen(cast<ValueDecl>(D))) 15539 return Res; 15540 } 15541 } 15542 return T(); 15543 } 15544 15545 static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D) { 15546 assert(!LookupResult::isVisible(SemaRef, D) && "not in slow case"); 15547 15548 for (auto RD : D->redecls()) { 15549 // Don't bother with extra checks if we already know this one isn't visible. 15550 if (RD == D) 15551 continue; 15552 15553 auto ND = cast<NamedDecl>(RD); 15554 if (LookupResult::isVisible(SemaRef, ND)) 15555 return ND; 15556 } 15557 15558 return nullptr; 15559 } 15560 15561 static void 15562 argumentDependentLookup(Sema &SemaRef, const DeclarationNameInfo &Id, 15563 SourceLocation Loc, QualType Ty, 15564 SmallVectorImpl<UnresolvedSet<8>> &Lookups) { 15565 // Find all of the associated namespaces and classes based on the 15566 // arguments we have. 15567 Sema::AssociatedNamespaceSet AssociatedNamespaces; 15568 Sema::AssociatedClassSet AssociatedClasses; 15569 OpaqueValueExpr OVE(Loc, Ty, VK_LValue); 15570 SemaRef.FindAssociatedClassesAndNamespaces(Loc, &OVE, AssociatedNamespaces, 15571 AssociatedClasses); 15572 15573 // C++ [basic.lookup.argdep]p3: 15574 // Let X be the lookup set produced by unqualified lookup (3.4.1) 15575 // and let Y be the lookup set produced by argument dependent 15576 // lookup (defined as follows). If X contains [...] then Y is 15577 // empty. Otherwise Y is the set of declarations found in the 15578 // namespaces associated with the argument types as described 15579 // below. The set of declarations found by the lookup of the name 15580 // is the union of X and Y. 15581 // 15582 // Here, we compute Y and add its members to the overloaded 15583 // candidate set. 15584 for (auto *NS : AssociatedNamespaces) { 15585 // When considering an associated namespace, the lookup is the 15586 // same as the lookup performed when the associated namespace is 15587 // used as a qualifier (3.4.3.2) except that: 15588 // 15589 // -- Any using-directives in the associated namespace are 15590 // ignored. 15591 // 15592 // -- Any namespace-scope friend functions declared in 15593 // associated classes are visible within their respective 15594 // namespaces even if they are not visible during an ordinary 15595 // lookup (11.4). 15596 DeclContext::lookup_result R = NS->lookup(Id.getName()); 15597 for (auto *D : R) { 15598 auto *Underlying = D; 15599 if (auto *USD = dyn_cast<UsingShadowDecl>(D)) 15600 Underlying = USD->getTargetDecl(); 15601 15602 if (!isa<OMPDeclareReductionDecl>(Underlying) && 15603 !isa<OMPDeclareMapperDecl>(Underlying)) 15604 continue; 15605 15606 if (!SemaRef.isVisible(D)) { 15607 D = findAcceptableDecl(SemaRef, D); 15608 if (!D) 15609 continue; 15610 if (auto *USD = dyn_cast<UsingShadowDecl>(D)) 15611 Underlying = USD->getTargetDecl(); 15612 } 15613 Lookups.emplace_back(); 15614 Lookups.back().addDecl(Underlying); 15615 } 15616 } 15617 } 15618 15619 static ExprResult 15620 buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range, 15621 Scope *S, CXXScopeSpec &ReductionIdScopeSpec, 15622 const DeclarationNameInfo &ReductionId, QualType Ty, 15623 CXXCastPath &BasePath, Expr *UnresolvedReduction) { 15624 if (ReductionIdScopeSpec.isInvalid()) 15625 return ExprError(); 15626 SmallVector<UnresolvedSet<8>, 4> Lookups; 15627 if (S) { 15628 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName); 15629 Lookup.suppressDiagnostics(); 15630 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) { 15631 NamedDecl *D = Lookup.getRepresentativeDecl(); 15632 do { 15633 S = S->getParent(); 15634 } while (S && !S->isDeclScope(D)); 15635 if (S) 15636 S = S->getParent(); 15637 Lookups.emplace_back(); 15638 Lookups.back().append(Lookup.begin(), Lookup.end()); 15639 Lookup.clear(); 15640 } 15641 } else if (auto *ULE = 15642 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) { 15643 Lookups.push_back(UnresolvedSet<8>()); 15644 Decl *PrevD = nullptr; 15645 for (NamedDecl *D : ULE->decls()) { 15646 if (D == PrevD) 15647 Lookups.push_back(UnresolvedSet<8>()); 15648 else if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(D)) 15649 Lookups.back().addDecl(DRD); 15650 PrevD = D; 15651 } 15652 } 15653 if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() || 15654 Ty->isInstantiationDependentType() || 15655 Ty->containsUnexpandedParameterPack() || 15656 filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) { 15657 return !D->isInvalidDecl() && 15658 (D->getType()->isDependentType() || 15659 D->getType()->isInstantiationDependentType() || 15660 D->getType()->containsUnexpandedParameterPack()); 15661 })) { 15662 UnresolvedSet<8> ResSet; 15663 for (const UnresolvedSet<8> &Set : Lookups) { 15664 if (Set.empty()) 15665 continue; 15666 ResSet.append(Set.begin(), Set.end()); 15667 // The last item marks the end of all declarations at the specified scope. 15668 ResSet.addDecl(Set[Set.size() - 1]); 15669 } 15670 return UnresolvedLookupExpr::Create( 15671 SemaRef.Context, /*NamingClass=*/nullptr, 15672 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId, 15673 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end()); 15674 } 15675 // Lookup inside the classes. 15676 // C++ [over.match.oper]p3: 15677 // For a unary operator @ with an operand of a type whose 15678 // cv-unqualified version is T1, and for a binary operator @ with 15679 // a left operand of a type whose cv-unqualified version is T1 and 15680 // a right operand of a type whose cv-unqualified version is T2, 15681 // three sets of candidate functions, designated member 15682 // candidates, non-member candidates and built-in candidates, are 15683 // constructed as follows: 15684 // -- If T1 is a complete class type or a class currently being 15685 // defined, the set of member candidates is the result of the 15686 // qualified lookup of T1::operator@ (13.3.1.1.1); otherwise, 15687 // the set of member candidates is empty. 15688 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName); 15689 Lookup.suppressDiagnostics(); 15690 if (const auto *TyRec = Ty->getAs<RecordType>()) { 15691 // Complete the type if it can be completed. 15692 // If the type is neither complete nor being defined, bail out now. 15693 if (SemaRef.isCompleteType(Loc, Ty) || TyRec->isBeingDefined() || 15694 TyRec->getDecl()->getDefinition()) { 15695 Lookup.clear(); 15696 SemaRef.LookupQualifiedName(Lookup, TyRec->getDecl()); 15697 if (Lookup.empty()) { 15698 Lookups.emplace_back(); 15699 Lookups.back().append(Lookup.begin(), Lookup.end()); 15700 } 15701 } 15702 } 15703 // Perform ADL. 15704 if (SemaRef.getLangOpts().CPlusPlus) 15705 argumentDependentLookup(SemaRef, ReductionId, Loc, Ty, Lookups); 15706 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 15707 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * { 15708 if (!D->isInvalidDecl() && 15709 SemaRef.Context.hasSameType(D->getType(), Ty)) 15710 return D; 15711 return nullptr; 15712 })) 15713 return SemaRef.BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(), 15714 VK_LValue, Loc); 15715 if (SemaRef.getLangOpts().CPlusPlus) { 15716 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 15717 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * { 15718 if (!D->isInvalidDecl() && 15719 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) && 15720 !Ty.isMoreQualifiedThan(D->getType())) 15721 return D; 15722 return nullptr; 15723 })) { 15724 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, 15725 /*DetectVirtual=*/false); 15726 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) { 15727 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType( 15728 VD->getType().getUnqualifiedType()))) { 15729 if (SemaRef.CheckBaseClassAccess( 15730 Loc, VD->getType(), Ty, Paths.front(), 15731 /*DiagID=*/0) != Sema::AR_inaccessible) { 15732 SemaRef.BuildBasePathArray(Paths, BasePath); 15733 return SemaRef.BuildDeclRefExpr( 15734 VD, VD->getType().getNonReferenceType(), VK_LValue, Loc); 15735 } 15736 } 15737 } 15738 } 15739 } 15740 if (ReductionIdScopeSpec.isSet()) { 15741 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) 15742 << Ty << Range; 15743 return ExprError(); 15744 } 15745 return ExprEmpty(); 15746 } 15747 15748 namespace { 15749 /// Data for the reduction-based clauses. 15750 struct ReductionData { 15751 /// List of original reduction items. 15752 SmallVector<Expr *, 8> Vars; 15753 /// List of private copies of the reduction items. 15754 SmallVector<Expr *, 8> Privates; 15755 /// LHS expressions for the reduction_op expressions. 15756 SmallVector<Expr *, 8> LHSs; 15757 /// RHS expressions for the reduction_op expressions. 15758 SmallVector<Expr *, 8> RHSs; 15759 /// Reduction operation expression. 15760 SmallVector<Expr *, 8> ReductionOps; 15761 /// inscan copy operation expressions. 15762 SmallVector<Expr *, 8> InscanCopyOps; 15763 /// inscan copy temp array expressions for prefix sums. 15764 SmallVector<Expr *, 8> InscanCopyArrayTemps; 15765 /// inscan copy temp array element expressions for prefix sums. 15766 SmallVector<Expr *, 8> InscanCopyArrayElems; 15767 /// Taskgroup descriptors for the corresponding reduction items in 15768 /// in_reduction clauses. 15769 SmallVector<Expr *, 8> TaskgroupDescriptors; 15770 /// List of captures for clause. 15771 SmallVector<Decl *, 4> ExprCaptures; 15772 /// List of postupdate expressions. 15773 SmallVector<Expr *, 4> ExprPostUpdates; 15774 /// Reduction modifier. 15775 unsigned RedModifier = 0; 15776 ReductionData() = delete; 15777 /// Reserves required memory for the reduction data. 15778 ReductionData(unsigned Size, unsigned Modifier = 0) : RedModifier(Modifier) { 15779 Vars.reserve(Size); 15780 Privates.reserve(Size); 15781 LHSs.reserve(Size); 15782 RHSs.reserve(Size); 15783 ReductionOps.reserve(Size); 15784 if (RedModifier == OMPC_REDUCTION_inscan) { 15785 InscanCopyOps.reserve(Size); 15786 InscanCopyArrayTemps.reserve(Size); 15787 InscanCopyArrayElems.reserve(Size); 15788 } 15789 TaskgroupDescriptors.reserve(Size); 15790 ExprCaptures.reserve(Size); 15791 ExprPostUpdates.reserve(Size); 15792 } 15793 /// Stores reduction item and reduction operation only (required for dependent 15794 /// reduction item). 15795 void push(Expr *Item, Expr *ReductionOp) { 15796 Vars.emplace_back(Item); 15797 Privates.emplace_back(nullptr); 15798 LHSs.emplace_back(nullptr); 15799 RHSs.emplace_back(nullptr); 15800 ReductionOps.emplace_back(ReductionOp); 15801 TaskgroupDescriptors.emplace_back(nullptr); 15802 if (RedModifier == OMPC_REDUCTION_inscan) { 15803 InscanCopyOps.push_back(nullptr); 15804 InscanCopyArrayTemps.push_back(nullptr); 15805 InscanCopyArrayElems.push_back(nullptr); 15806 } 15807 } 15808 /// Stores reduction data. 15809 void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp, 15810 Expr *TaskgroupDescriptor, Expr *CopyOp, Expr *CopyArrayTemp, 15811 Expr *CopyArrayElem) { 15812 Vars.emplace_back(Item); 15813 Privates.emplace_back(Private); 15814 LHSs.emplace_back(LHS); 15815 RHSs.emplace_back(RHS); 15816 ReductionOps.emplace_back(ReductionOp); 15817 TaskgroupDescriptors.emplace_back(TaskgroupDescriptor); 15818 if (RedModifier == OMPC_REDUCTION_inscan) { 15819 InscanCopyOps.push_back(CopyOp); 15820 InscanCopyArrayTemps.push_back(CopyArrayTemp); 15821 InscanCopyArrayElems.push_back(CopyArrayElem); 15822 } else { 15823 assert(CopyOp == nullptr && CopyArrayTemp == nullptr && 15824 CopyArrayElem == nullptr && 15825 "Copy operation must be used for inscan reductions only."); 15826 } 15827 } 15828 }; 15829 } // namespace 15830 15831 static bool checkOMPArraySectionConstantForReduction( 15832 ASTContext &Context, const OMPArraySectionExpr *OASE, bool &SingleElement, 15833 SmallVectorImpl<llvm::APSInt> &ArraySizes) { 15834 const Expr *Length = OASE->getLength(); 15835 if (Length == nullptr) { 15836 // For array sections of the form [1:] or [:], we would need to analyze 15837 // the lower bound... 15838 if (OASE->getColonLocFirst().isValid()) 15839 return false; 15840 15841 // This is an array subscript which has implicit length 1! 15842 SingleElement = true; 15843 ArraySizes.push_back(llvm::APSInt::get(1)); 15844 } else { 15845 Expr::EvalResult Result; 15846 if (!Length->EvaluateAsInt(Result, Context)) 15847 return false; 15848 15849 llvm::APSInt ConstantLengthValue = Result.Val.getInt(); 15850 SingleElement = (ConstantLengthValue.getSExtValue() == 1); 15851 ArraySizes.push_back(ConstantLengthValue); 15852 } 15853 15854 // Get the base of this array section and walk up from there. 15855 const Expr *Base = OASE->getBase()->IgnoreParenImpCasts(); 15856 15857 // We require length = 1 for all array sections except the right-most to 15858 // guarantee that the memory region is contiguous and has no holes in it. 15859 while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) { 15860 Length = TempOASE->getLength(); 15861 if (Length == nullptr) { 15862 // For array sections of the form [1:] or [:], we would need to analyze 15863 // the lower bound... 15864 if (OASE->getColonLocFirst().isValid()) 15865 return false; 15866 15867 // This is an array subscript which has implicit length 1! 15868 ArraySizes.push_back(llvm::APSInt::get(1)); 15869 } else { 15870 Expr::EvalResult Result; 15871 if (!Length->EvaluateAsInt(Result, Context)) 15872 return false; 15873 15874 llvm::APSInt ConstantLengthValue = Result.Val.getInt(); 15875 if (ConstantLengthValue.getSExtValue() != 1) 15876 return false; 15877 15878 ArraySizes.push_back(ConstantLengthValue); 15879 } 15880 Base = TempOASE->getBase()->IgnoreParenImpCasts(); 15881 } 15882 15883 // If we have a single element, we don't need to add the implicit lengths. 15884 if (!SingleElement) { 15885 while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) { 15886 // Has implicit length 1! 15887 ArraySizes.push_back(llvm::APSInt::get(1)); 15888 Base = TempASE->getBase()->IgnoreParenImpCasts(); 15889 } 15890 } 15891 15892 // This array section can be privatized as a single value or as a constant 15893 // sized array. 15894 return true; 15895 } 15896 15897 static bool actOnOMPReductionKindClause( 15898 Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind, 15899 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 15900 SourceLocation ColonLoc, SourceLocation EndLoc, 15901 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 15902 ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) { 15903 DeclarationName DN = ReductionId.getName(); 15904 OverloadedOperatorKind OOK = DN.getCXXOverloadedOperator(); 15905 BinaryOperatorKind BOK = BO_Comma; 15906 15907 ASTContext &Context = S.Context; 15908 // OpenMP [2.14.3.6, reduction clause] 15909 // C 15910 // reduction-identifier is either an identifier or one of the following 15911 // operators: +, -, *, &, |, ^, && and || 15912 // C++ 15913 // reduction-identifier is either an id-expression or one of the following 15914 // operators: +, -, *, &, |, ^, && and || 15915 switch (OOK) { 15916 case OO_Plus: 15917 case OO_Minus: 15918 BOK = BO_Add; 15919 break; 15920 case OO_Star: 15921 BOK = BO_Mul; 15922 break; 15923 case OO_Amp: 15924 BOK = BO_And; 15925 break; 15926 case OO_Pipe: 15927 BOK = BO_Or; 15928 break; 15929 case OO_Caret: 15930 BOK = BO_Xor; 15931 break; 15932 case OO_AmpAmp: 15933 BOK = BO_LAnd; 15934 break; 15935 case OO_PipePipe: 15936 BOK = BO_LOr; 15937 break; 15938 case OO_New: 15939 case OO_Delete: 15940 case OO_Array_New: 15941 case OO_Array_Delete: 15942 case OO_Slash: 15943 case OO_Percent: 15944 case OO_Tilde: 15945 case OO_Exclaim: 15946 case OO_Equal: 15947 case OO_Less: 15948 case OO_Greater: 15949 case OO_LessEqual: 15950 case OO_GreaterEqual: 15951 case OO_PlusEqual: 15952 case OO_MinusEqual: 15953 case OO_StarEqual: 15954 case OO_SlashEqual: 15955 case OO_PercentEqual: 15956 case OO_CaretEqual: 15957 case OO_AmpEqual: 15958 case OO_PipeEqual: 15959 case OO_LessLess: 15960 case OO_GreaterGreater: 15961 case OO_LessLessEqual: 15962 case OO_GreaterGreaterEqual: 15963 case OO_EqualEqual: 15964 case OO_ExclaimEqual: 15965 case OO_Spaceship: 15966 case OO_PlusPlus: 15967 case OO_MinusMinus: 15968 case OO_Comma: 15969 case OO_ArrowStar: 15970 case OO_Arrow: 15971 case OO_Call: 15972 case OO_Subscript: 15973 case OO_Conditional: 15974 case OO_Coawait: 15975 case NUM_OVERLOADED_OPERATORS: 15976 llvm_unreachable("Unexpected reduction identifier"); 15977 case OO_None: 15978 if (IdentifierInfo *II = DN.getAsIdentifierInfo()) { 15979 if (II->isStr("max")) 15980 BOK = BO_GT; 15981 else if (II->isStr("min")) 15982 BOK = BO_LT; 15983 } 15984 break; 15985 } 15986 SourceRange ReductionIdRange; 15987 if (ReductionIdScopeSpec.isValid()) 15988 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc()); 15989 else 15990 ReductionIdRange.setBegin(ReductionId.getBeginLoc()); 15991 ReductionIdRange.setEnd(ReductionId.getEndLoc()); 15992 15993 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end(); 15994 bool FirstIter = true; 15995 for (Expr *RefExpr : VarList) { 15996 assert(RefExpr && "nullptr expr in OpenMP reduction clause."); 15997 // OpenMP [2.1, C/C++] 15998 // A list item is a variable or array section, subject to the restrictions 15999 // specified in Section 2.4 on page 42 and in each of the sections 16000 // describing clauses and directives for which a list appears. 16001 // OpenMP [2.14.3.3, Restrictions, p.1] 16002 // A variable that is part of another variable (as an array or 16003 // structure element) cannot appear in a private clause. 16004 if (!FirstIter && IR != ER) 16005 ++IR; 16006 FirstIter = false; 16007 SourceLocation ELoc; 16008 SourceRange ERange; 16009 Expr *SimpleRefExpr = RefExpr; 16010 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange, 16011 /*AllowArraySection=*/true); 16012 if (Res.second) { 16013 // Try to find 'declare reduction' corresponding construct before using 16014 // builtin/overloaded operators. 16015 QualType Type = Context.DependentTy; 16016 CXXCastPath BasePath; 16017 ExprResult DeclareReductionRef = buildDeclareReductionRef( 16018 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec, 16019 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR); 16020 Expr *ReductionOp = nullptr; 16021 if (S.CurContext->isDependentContext() && 16022 (DeclareReductionRef.isUnset() || 16023 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) 16024 ReductionOp = DeclareReductionRef.get(); 16025 // It will be analyzed later. 16026 RD.push(RefExpr, ReductionOp); 16027 } 16028 ValueDecl *D = Res.first; 16029 if (!D) 16030 continue; 16031 16032 Expr *TaskgroupDescriptor = nullptr; 16033 QualType Type; 16034 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens()); 16035 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens()); 16036 if (ASE) { 16037 Type = ASE->getType().getNonReferenceType(); 16038 } else if (OASE) { 16039 QualType BaseType = 16040 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 16041 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 16042 Type = ATy->getElementType(); 16043 else 16044 Type = BaseType->getPointeeType(); 16045 Type = Type.getNonReferenceType(); 16046 } else { 16047 Type = Context.getBaseElementType(D->getType().getNonReferenceType()); 16048 } 16049 auto *VD = dyn_cast<VarDecl>(D); 16050 16051 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 16052 // A variable that appears in a private clause must not have an incomplete 16053 // type or a reference type. 16054 if (S.RequireCompleteType(ELoc, D->getType(), 16055 diag::err_omp_reduction_incomplete_type)) 16056 continue; 16057 // OpenMP [2.14.3.6, reduction clause, Restrictions] 16058 // A list item that appears in a reduction clause must not be 16059 // const-qualified. 16060 if (rejectConstNotMutableType(S, D, Type, ClauseKind, ELoc, 16061 /*AcceptIfMutable*/ false, ASE || OASE)) 16062 continue; 16063 16064 OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective(); 16065 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4] 16066 // If a list-item is a reference type then it must bind to the same object 16067 // for all threads of the team. 16068 if (!ASE && !OASE) { 16069 if (VD) { 16070 VarDecl *VDDef = VD->getDefinition(); 16071 if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) { 16072 DSARefChecker Check(Stack); 16073 if (Check.Visit(VDDef->getInit())) { 16074 S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg) 16075 << getOpenMPClauseName(ClauseKind) << ERange; 16076 S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef; 16077 continue; 16078 } 16079 } 16080 } 16081 16082 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 16083 // in a Construct] 16084 // Variables with the predetermined data-sharing attributes may not be 16085 // listed in data-sharing attributes clauses, except for the cases 16086 // listed below. For these exceptions only, listing a predetermined 16087 // variable in a data-sharing attribute clause is allowed and overrides 16088 // the variable's predetermined data-sharing attributes. 16089 // OpenMP [2.14.3.6, Restrictions, p.3] 16090 // Any number of reduction clauses can be specified on the directive, 16091 // but a list item can appear only once in the reduction clauses for that 16092 // directive. 16093 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false); 16094 if (DVar.CKind == OMPC_reduction) { 16095 S.Diag(ELoc, diag::err_omp_once_referenced) 16096 << getOpenMPClauseName(ClauseKind); 16097 if (DVar.RefExpr) 16098 S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced); 16099 continue; 16100 } 16101 if (DVar.CKind != OMPC_unknown) { 16102 S.Diag(ELoc, diag::err_omp_wrong_dsa) 16103 << getOpenMPClauseName(DVar.CKind) 16104 << getOpenMPClauseName(OMPC_reduction); 16105 reportOriginalDsa(S, Stack, D, DVar); 16106 continue; 16107 } 16108 16109 // OpenMP [2.14.3.6, Restrictions, p.1] 16110 // A list item that appears in a reduction clause of a worksharing 16111 // construct must be shared in the parallel regions to which any of the 16112 // worksharing regions arising from the worksharing construct bind. 16113 if (isOpenMPWorksharingDirective(CurrDir) && 16114 !isOpenMPParallelDirective(CurrDir) && 16115 !isOpenMPTeamsDirective(CurrDir)) { 16116 DVar = Stack->getImplicitDSA(D, true); 16117 if (DVar.CKind != OMPC_shared) { 16118 S.Diag(ELoc, diag::err_omp_required_access) 16119 << getOpenMPClauseName(OMPC_reduction) 16120 << getOpenMPClauseName(OMPC_shared); 16121 reportOriginalDsa(S, Stack, D, DVar); 16122 continue; 16123 } 16124 } 16125 } else { 16126 // Threadprivates cannot be shared between threads, so dignose if the base 16127 // is a threadprivate variable. 16128 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false); 16129 if (DVar.CKind == OMPC_threadprivate) { 16130 S.Diag(ELoc, diag::err_omp_wrong_dsa) 16131 << getOpenMPClauseName(DVar.CKind) 16132 << getOpenMPClauseName(OMPC_reduction); 16133 reportOriginalDsa(S, Stack, D, DVar); 16134 continue; 16135 } 16136 } 16137 16138 // Try to find 'declare reduction' corresponding construct before using 16139 // builtin/overloaded operators. 16140 CXXCastPath BasePath; 16141 ExprResult DeclareReductionRef = buildDeclareReductionRef( 16142 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec, 16143 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR); 16144 if (DeclareReductionRef.isInvalid()) 16145 continue; 16146 if (S.CurContext->isDependentContext() && 16147 (DeclareReductionRef.isUnset() || 16148 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) { 16149 RD.push(RefExpr, DeclareReductionRef.get()); 16150 continue; 16151 } 16152 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) { 16153 // Not allowed reduction identifier is found. 16154 S.Diag(ReductionId.getBeginLoc(), 16155 diag::err_omp_unknown_reduction_identifier) 16156 << Type << ReductionIdRange; 16157 continue; 16158 } 16159 16160 // OpenMP [2.14.3.6, reduction clause, Restrictions] 16161 // The type of a list item that appears in a reduction clause must be valid 16162 // for the reduction-identifier. For a max or min reduction in C, the type 16163 // of the list item must be an allowed arithmetic data type: char, int, 16164 // float, double, or _Bool, possibly modified with long, short, signed, or 16165 // unsigned. For a max or min reduction in C++, the type of the list item 16166 // must be an allowed arithmetic data type: char, wchar_t, int, float, 16167 // double, or bool, possibly modified with long, short, signed, or unsigned. 16168 if (DeclareReductionRef.isUnset()) { 16169 if ((BOK == BO_GT || BOK == BO_LT) && 16170 !(Type->isScalarType() || 16171 (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) { 16172 S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg) 16173 << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus; 16174 if (!ASE && !OASE) { 16175 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 16176 VarDecl::DeclarationOnly; 16177 S.Diag(D->getLocation(), 16178 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 16179 << D; 16180 } 16181 continue; 16182 } 16183 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) && 16184 !S.getLangOpts().CPlusPlus && Type->isFloatingType()) { 16185 S.Diag(ELoc, diag::err_omp_clause_floating_type_arg) 16186 << getOpenMPClauseName(ClauseKind); 16187 if (!ASE && !OASE) { 16188 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 16189 VarDecl::DeclarationOnly; 16190 S.Diag(D->getLocation(), 16191 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 16192 << D; 16193 } 16194 continue; 16195 } 16196 } 16197 16198 Type = Type.getNonLValueExprType(Context).getUnqualifiedType(); 16199 VarDecl *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs", 16200 D->hasAttrs() ? &D->getAttrs() : nullptr); 16201 VarDecl *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(), 16202 D->hasAttrs() ? &D->getAttrs() : nullptr); 16203 QualType PrivateTy = Type; 16204 16205 // Try if we can determine constant lengths for all array sections and avoid 16206 // the VLA. 16207 bool ConstantLengthOASE = false; 16208 if (OASE) { 16209 bool SingleElement; 16210 llvm::SmallVector<llvm::APSInt, 4> ArraySizes; 16211 ConstantLengthOASE = checkOMPArraySectionConstantForReduction( 16212 Context, OASE, SingleElement, ArraySizes); 16213 16214 // If we don't have a single element, we must emit a constant array type. 16215 if (ConstantLengthOASE && !SingleElement) { 16216 for (llvm::APSInt &Size : ArraySizes) 16217 PrivateTy = Context.getConstantArrayType(PrivateTy, Size, nullptr, 16218 ArrayType::Normal, 16219 /*IndexTypeQuals=*/0); 16220 } 16221 } 16222 16223 if ((OASE && !ConstantLengthOASE) || 16224 (!OASE && !ASE && 16225 D->getType().getNonReferenceType()->isVariablyModifiedType())) { 16226 if (!Context.getTargetInfo().isVLASupported()) { 16227 if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective())) { 16228 S.Diag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE; 16229 S.Diag(ELoc, diag::note_vla_unsupported); 16230 continue; 16231 } else { 16232 S.targetDiag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE; 16233 S.targetDiag(ELoc, diag::note_vla_unsupported); 16234 } 16235 } 16236 // For arrays/array sections only: 16237 // Create pseudo array type for private copy. The size for this array will 16238 // be generated during codegen. 16239 // For array subscripts or single variables Private Ty is the same as Type 16240 // (type of the variable or single array element). 16241 PrivateTy = Context.getVariableArrayType( 16242 Type, 16243 new (Context) OpaqueValueExpr(ELoc, Context.getSizeType(), VK_RValue), 16244 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange()); 16245 } else if (!ASE && !OASE && 16246 Context.getAsArrayType(D->getType().getNonReferenceType())) { 16247 PrivateTy = D->getType().getNonReferenceType(); 16248 } 16249 // Private copy. 16250 VarDecl *PrivateVD = 16251 buildVarDecl(S, ELoc, PrivateTy, D->getName(), 16252 D->hasAttrs() ? &D->getAttrs() : nullptr, 16253 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 16254 // Add initializer for private variable. 16255 Expr *Init = nullptr; 16256 DeclRefExpr *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc); 16257 DeclRefExpr *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc); 16258 if (DeclareReductionRef.isUsable()) { 16259 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>(); 16260 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl()); 16261 if (DRD->getInitializer()) { 16262 S.ActOnUninitializedDecl(PrivateVD); 16263 Init = DRDRef; 16264 RHSVD->setInit(DRDRef); 16265 RHSVD->setInitStyle(VarDecl::CallInit); 16266 } 16267 } else { 16268 switch (BOK) { 16269 case BO_Add: 16270 case BO_Xor: 16271 case BO_Or: 16272 case BO_LOr: 16273 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'. 16274 if (Type->isScalarType() || Type->isAnyComplexType()) 16275 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get(); 16276 break; 16277 case BO_Mul: 16278 case BO_LAnd: 16279 if (Type->isScalarType() || Type->isAnyComplexType()) { 16280 // '*' and '&&' reduction ops - initializer is '1'. 16281 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get(); 16282 } 16283 break; 16284 case BO_And: { 16285 // '&' reduction op - initializer is '~0'. 16286 QualType OrigType = Type; 16287 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) 16288 Type = ComplexTy->getElementType(); 16289 if (Type->isRealFloatingType()) { 16290 llvm::APFloat InitValue = llvm::APFloat::getAllOnesValue( 16291 Context.getFloatTypeSemantics(Type), 16292 Context.getTypeSize(Type)); 16293 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, 16294 Type, ELoc); 16295 } else if (Type->isScalarType()) { 16296 uint64_t Size = Context.getTypeSize(Type); 16297 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0); 16298 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size); 16299 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); 16300 } 16301 if (Init && OrigType->isAnyComplexType()) { 16302 // Init = 0xFFFF + 0xFFFFi; 16303 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType); 16304 Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get(); 16305 } 16306 Type = OrigType; 16307 break; 16308 } 16309 case BO_LT: 16310 case BO_GT: { 16311 // 'min' reduction op - initializer is 'Largest representable number in 16312 // the reduction list item type'. 16313 // 'max' reduction op - initializer is 'Least representable number in 16314 // the reduction list item type'. 16315 if (Type->isIntegerType() || Type->isPointerType()) { 16316 bool IsSigned = Type->hasSignedIntegerRepresentation(); 16317 uint64_t Size = Context.getTypeSize(Type); 16318 QualType IntTy = 16319 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned); 16320 llvm::APInt InitValue = 16321 (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size) 16322 : llvm::APInt::getMinValue(Size) 16323 : IsSigned ? llvm::APInt::getSignedMaxValue(Size) 16324 : llvm::APInt::getMaxValue(Size); 16325 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); 16326 if (Type->isPointerType()) { 16327 // Cast to pointer type. 16328 ExprResult CastExpr = S.BuildCStyleCastExpr( 16329 ELoc, Context.getTrivialTypeSourceInfo(Type, ELoc), ELoc, Init); 16330 if (CastExpr.isInvalid()) 16331 continue; 16332 Init = CastExpr.get(); 16333 } 16334 } else if (Type->isRealFloatingType()) { 16335 llvm::APFloat InitValue = llvm::APFloat::getLargest( 16336 Context.getFloatTypeSemantics(Type), BOK != BO_LT); 16337 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, 16338 Type, ELoc); 16339 } 16340 break; 16341 } 16342 case BO_PtrMemD: 16343 case BO_PtrMemI: 16344 case BO_MulAssign: 16345 case BO_Div: 16346 case BO_Rem: 16347 case BO_Sub: 16348 case BO_Shl: 16349 case BO_Shr: 16350 case BO_LE: 16351 case BO_GE: 16352 case BO_EQ: 16353 case BO_NE: 16354 case BO_Cmp: 16355 case BO_AndAssign: 16356 case BO_XorAssign: 16357 case BO_OrAssign: 16358 case BO_Assign: 16359 case BO_AddAssign: 16360 case BO_SubAssign: 16361 case BO_DivAssign: 16362 case BO_RemAssign: 16363 case BO_ShlAssign: 16364 case BO_ShrAssign: 16365 case BO_Comma: 16366 llvm_unreachable("Unexpected reduction operation"); 16367 } 16368 } 16369 if (Init && DeclareReductionRef.isUnset()) { 16370 S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false); 16371 // Store initializer for single element in private copy. Will be used 16372 // during codegen. 16373 PrivateVD->setInit(RHSVD->getInit()); 16374 PrivateVD->setInitStyle(RHSVD->getInitStyle()); 16375 } else if (!Init) { 16376 S.ActOnUninitializedDecl(RHSVD); 16377 // Store initializer for single element in private copy. Will be used 16378 // during codegen. 16379 PrivateVD->setInit(RHSVD->getInit()); 16380 PrivateVD->setInitStyle(RHSVD->getInitStyle()); 16381 } 16382 if (RHSVD->isInvalidDecl()) 16383 continue; 16384 if (!RHSVD->hasInit() && 16385 (DeclareReductionRef.isUnset() || !S.LangOpts.CPlusPlus)) { 16386 S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible) 16387 << Type << ReductionIdRange; 16388 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 16389 VarDecl::DeclarationOnly; 16390 S.Diag(D->getLocation(), 16391 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 16392 << D; 16393 continue; 16394 } 16395 DeclRefExpr *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc); 16396 ExprResult ReductionOp; 16397 if (DeclareReductionRef.isUsable()) { 16398 QualType RedTy = DeclareReductionRef.get()->getType(); 16399 QualType PtrRedTy = Context.getPointerType(RedTy); 16400 ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE); 16401 ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE); 16402 if (!BasePath.empty()) { 16403 LHS = S.DefaultLvalueConversion(LHS.get()); 16404 RHS = S.DefaultLvalueConversion(RHS.get()); 16405 LHS = ImplicitCastExpr::Create( 16406 Context, PtrRedTy, CK_UncheckedDerivedToBase, LHS.get(), &BasePath, 16407 LHS.get()->getValueKind(), FPOptionsOverride()); 16408 RHS = ImplicitCastExpr::Create( 16409 Context, PtrRedTy, CK_UncheckedDerivedToBase, RHS.get(), &BasePath, 16410 RHS.get()->getValueKind(), FPOptionsOverride()); 16411 } 16412 FunctionProtoType::ExtProtoInfo EPI; 16413 QualType Params[] = {PtrRedTy, PtrRedTy}; 16414 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI); 16415 auto *OVE = new (Context) OpaqueValueExpr( 16416 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary, 16417 S.DefaultLvalueConversion(DeclareReductionRef.get()).get()); 16418 Expr *Args[] = {LHS.get(), RHS.get()}; 16419 ReductionOp = 16420 CallExpr::Create(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc, 16421 S.CurFPFeatureOverrides()); 16422 } else { 16423 ReductionOp = S.BuildBinOp( 16424 Stack->getCurScope(), ReductionId.getBeginLoc(), BOK, LHSDRE, RHSDRE); 16425 if (ReductionOp.isUsable()) { 16426 if (BOK != BO_LT && BOK != BO_GT) { 16427 ReductionOp = 16428 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(), 16429 BO_Assign, LHSDRE, ReductionOp.get()); 16430 } else { 16431 auto *ConditionalOp = new (Context) 16432 ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc, RHSDRE, 16433 Type, VK_LValue, OK_Ordinary); 16434 ReductionOp = 16435 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(), 16436 BO_Assign, LHSDRE, ConditionalOp); 16437 } 16438 if (ReductionOp.isUsable()) 16439 ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get(), 16440 /*DiscardedValue*/ false); 16441 } 16442 if (!ReductionOp.isUsable()) 16443 continue; 16444 } 16445 16446 // Add copy operations for inscan reductions. 16447 // LHS = RHS; 16448 ExprResult CopyOpRes, TempArrayRes, TempArrayElem; 16449 if (ClauseKind == OMPC_reduction && 16450 RD.RedModifier == OMPC_REDUCTION_inscan) { 16451 ExprResult RHS = S.DefaultLvalueConversion(RHSDRE); 16452 CopyOpRes = S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, LHSDRE, 16453 RHS.get()); 16454 if (!CopyOpRes.isUsable()) 16455 continue; 16456 CopyOpRes = 16457 S.ActOnFinishFullExpr(CopyOpRes.get(), /*DiscardedValue=*/true); 16458 if (!CopyOpRes.isUsable()) 16459 continue; 16460 // For simd directive and simd-based directives in simd mode no need to 16461 // construct temp array, need just a single temp element. 16462 if (Stack->getCurrentDirective() == OMPD_simd || 16463 (S.getLangOpts().OpenMPSimd && 16464 isOpenMPSimdDirective(Stack->getCurrentDirective()))) { 16465 VarDecl *TempArrayVD = 16466 buildVarDecl(S, ELoc, PrivateTy, D->getName(), 16467 D->hasAttrs() ? &D->getAttrs() : nullptr); 16468 // Add a constructor to the temp decl. 16469 S.ActOnUninitializedDecl(TempArrayVD); 16470 TempArrayRes = buildDeclRefExpr(S, TempArrayVD, PrivateTy, ELoc); 16471 } else { 16472 // Build temp array for prefix sum. 16473 auto *Dim = new (S.Context) 16474 OpaqueValueExpr(ELoc, S.Context.getSizeType(), VK_RValue); 16475 QualType ArrayTy = 16476 S.Context.getVariableArrayType(PrivateTy, Dim, ArrayType::Normal, 16477 /*IndexTypeQuals=*/0, {ELoc, ELoc}); 16478 VarDecl *TempArrayVD = 16479 buildVarDecl(S, ELoc, ArrayTy, D->getName(), 16480 D->hasAttrs() ? &D->getAttrs() : nullptr); 16481 // Add a constructor to the temp decl. 16482 S.ActOnUninitializedDecl(TempArrayVD); 16483 TempArrayRes = buildDeclRefExpr(S, TempArrayVD, ArrayTy, ELoc); 16484 TempArrayElem = 16485 S.DefaultFunctionArrayLvalueConversion(TempArrayRes.get()); 16486 auto *Idx = new (S.Context) 16487 OpaqueValueExpr(ELoc, S.Context.getSizeType(), VK_RValue); 16488 TempArrayElem = S.CreateBuiltinArraySubscriptExpr(TempArrayElem.get(), 16489 ELoc, Idx, ELoc); 16490 } 16491 } 16492 16493 // OpenMP [2.15.4.6, Restrictions, p.2] 16494 // A list item that appears in an in_reduction clause of a task construct 16495 // must appear in a task_reduction clause of a construct associated with a 16496 // taskgroup region that includes the participating task in its taskgroup 16497 // set. The construct associated with the innermost region that meets this 16498 // condition must specify the same reduction-identifier as the in_reduction 16499 // clause. 16500 if (ClauseKind == OMPC_in_reduction) { 16501 SourceRange ParentSR; 16502 BinaryOperatorKind ParentBOK; 16503 const Expr *ParentReductionOp = nullptr; 16504 Expr *ParentBOKTD = nullptr, *ParentReductionOpTD = nullptr; 16505 DSAStackTy::DSAVarData ParentBOKDSA = 16506 Stack->getTopMostTaskgroupReductionData(D, ParentSR, ParentBOK, 16507 ParentBOKTD); 16508 DSAStackTy::DSAVarData ParentReductionOpDSA = 16509 Stack->getTopMostTaskgroupReductionData( 16510 D, ParentSR, ParentReductionOp, ParentReductionOpTD); 16511 bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown; 16512 bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown; 16513 if ((DeclareReductionRef.isUnset() && IsParentReductionOp) || 16514 (DeclareReductionRef.isUsable() && IsParentBOK) || 16515 (IsParentBOK && BOK != ParentBOK) || IsParentReductionOp) { 16516 bool EmitError = true; 16517 if (IsParentReductionOp && DeclareReductionRef.isUsable()) { 16518 llvm::FoldingSetNodeID RedId, ParentRedId; 16519 ParentReductionOp->Profile(ParentRedId, Context, /*Canonical=*/true); 16520 DeclareReductionRef.get()->Profile(RedId, Context, 16521 /*Canonical=*/true); 16522 EmitError = RedId != ParentRedId; 16523 } 16524 if (EmitError) { 16525 S.Diag(ReductionId.getBeginLoc(), 16526 diag::err_omp_reduction_identifier_mismatch) 16527 << ReductionIdRange << RefExpr->getSourceRange(); 16528 S.Diag(ParentSR.getBegin(), 16529 diag::note_omp_previous_reduction_identifier) 16530 << ParentSR 16531 << (IsParentBOK ? ParentBOKDSA.RefExpr 16532 : ParentReductionOpDSA.RefExpr) 16533 ->getSourceRange(); 16534 continue; 16535 } 16536 } 16537 TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD; 16538 } 16539 16540 DeclRefExpr *Ref = nullptr; 16541 Expr *VarsExpr = RefExpr->IgnoreParens(); 16542 if (!VD && !S.CurContext->isDependentContext()) { 16543 if (ASE || OASE) { 16544 TransformExprToCaptures RebuildToCapture(S, D); 16545 VarsExpr = 16546 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get(); 16547 Ref = RebuildToCapture.getCapturedExpr(); 16548 } else { 16549 VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false); 16550 } 16551 if (!S.isOpenMPCapturedDecl(D)) { 16552 RD.ExprCaptures.emplace_back(Ref->getDecl()); 16553 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) { 16554 ExprResult RefRes = S.DefaultLvalueConversion(Ref); 16555 if (!RefRes.isUsable()) 16556 continue; 16557 ExprResult PostUpdateRes = 16558 S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr, 16559 RefRes.get()); 16560 if (!PostUpdateRes.isUsable()) 16561 continue; 16562 if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) || 16563 Stack->getCurrentDirective() == OMPD_taskgroup) { 16564 S.Diag(RefExpr->getExprLoc(), 16565 diag::err_omp_reduction_non_addressable_expression) 16566 << RefExpr->getSourceRange(); 16567 continue; 16568 } 16569 RD.ExprPostUpdates.emplace_back( 16570 S.IgnoredValueConversions(PostUpdateRes.get()).get()); 16571 } 16572 } 16573 } 16574 // All reduction items are still marked as reduction (to do not increase 16575 // code base size). 16576 unsigned Modifier = RD.RedModifier; 16577 // Consider task_reductions as reductions with task modifier. Required for 16578 // correct analysis of in_reduction clauses. 16579 if (CurrDir == OMPD_taskgroup && ClauseKind == OMPC_task_reduction) 16580 Modifier = OMPC_REDUCTION_task; 16581 Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref, Modifier, 16582 ASE || OASE); 16583 if (Modifier == OMPC_REDUCTION_task && 16584 (CurrDir == OMPD_taskgroup || 16585 ((isOpenMPParallelDirective(CurrDir) || 16586 isOpenMPWorksharingDirective(CurrDir)) && 16587 !isOpenMPSimdDirective(CurrDir)))) { 16588 if (DeclareReductionRef.isUsable()) 16589 Stack->addTaskgroupReductionData(D, ReductionIdRange, 16590 DeclareReductionRef.get()); 16591 else 16592 Stack->addTaskgroupReductionData(D, ReductionIdRange, BOK); 16593 } 16594 RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get(), 16595 TaskgroupDescriptor, CopyOpRes.get(), TempArrayRes.get(), 16596 TempArrayElem.get()); 16597 } 16598 return RD.Vars.empty(); 16599 } 16600 16601 OMPClause *Sema::ActOnOpenMPReductionClause( 16602 ArrayRef<Expr *> VarList, OpenMPReductionClauseModifier Modifier, 16603 SourceLocation StartLoc, SourceLocation LParenLoc, 16604 SourceLocation ModifierLoc, SourceLocation ColonLoc, SourceLocation EndLoc, 16605 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 16606 ArrayRef<Expr *> UnresolvedReductions) { 16607 if (ModifierLoc.isValid() && Modifier == OMPC_REDUCTION_unknown) { 16608 Diag(LParenLoc, diag::err_omp_unexpected_clause_value) 16609 << getListOfPossibleValues(OMPC_reduction, /*First=*/0, 16610 /*Last=*/OMPC_REDUCTION_unknown) 16611 << getOpenMPClauseName(OMPC_reduction); 16612 return nullptr; 16613 } 16614 // OpenMP 5.0, 2.19.5.4 reduction Clause, Restrictions 16615 // A reduction clause with the inscan reduction-modifier may only appear on a 16616 // worksharing-loop construct, a worksharing-loop SIMD construct, a simd 16617 // construct, a parallel worksharing-loop construct or a parallel 16618 // worksharing-loop SIMD construct. 16619 if (Modifier == OMPC_REDUCTION_inscan && 16620 (DSAStack->getCurrentDirective() != OMPD_for && 16621 DSAStack->getCurrentDirective() != OMPD_for_simd && 16622 DSAStack->getCurrentDirective() != OMPD_simd && 16623 DSAStack->getCurrentDirective() != OMPD_parallel_for && 16624 DSAStack->getCurrentDirective() != OMPD_parallel_for_simd)) { 16625 Diag(ModifierLoc, diag::err_omp_wrong_inscan_reduction); 16626 return nullptr; 16627 } 16628 16629 ReductionData RD(VarList.size(), Modifier); 16630 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList, 16631 StartLoc, LParenLoc, ColonLoc, EndLoc, 16632 ReductionIdScopeSpec, ReductionId, 16633 UnresolvedReductions, RD)) 16634 return nullptr; 16635 16636 return OMPReductionClause::Create( 16637 Context, StartLoc, LParenLoc, ModifierLoc, ColonLoc, EndLoc, Modifier, 16638 RD.Vars, ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 16639 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.InscanCopyOps, 16640 RD.InscanCopyArrayTemps, RD.InscanCopyArrayElems, 16641 buildPreInits(Context, RD.ExprCaptures), 16642 buildPostUpdate(*this, RD.ExprPostUpdates)); 16643 } 16644 16645 OMPClause *Sema::ActOnOpenMPTaskReductionClause( 16646 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 16647 SourceLocation ColonLoc, SourceLocation EndLoc, 16648 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 16649 ArrayRef<Expr *> UnresolvedReductions) { 16650 ReductionData RD(VarList.size()); 16651 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction, VarList, 16652 StartLoc, LParenLoc, ColonLoc, EndLoc, 16653 ReductionIdScopeSpec, ReductionId, 16654 UnresolvedReductions, RD)) 16655 return nullptr; 16656 16657 return OMPTaskReductionClause::Create( 16658 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 16659 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 16660 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, 16661 buildPreInits(Context, RD.ExprCaptures), 16662 buildPostUpdate(*this, RD.ExprPostUpdates)); 16663 } 16664 16665 OMPClause *Sema::ActOnOpenMPInReductionClause( 16666 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 16667 SourceLocation ColonLoc, SourceLocation EndLoc, 16668 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 16669 ArrayRef<Expr *> UnresolvedReductions) { 16670 ReductionData RD(VarList.size()); 16671 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_in_reduction, VarList, 16672 StartLoc, LParenLoc, ColonLoc, EndLoc, 16673 ReductionIdScopeSpec, ReductionId, 16674 UnresolvedReductions, RD)) 16675 return nullptr; 16676 16677 return OMPInReductionClause::Create( 16678 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 16679 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 16680 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.TaskgroupDescriptors, 16681 buildPreInits(Context, RD.ExprCaptures), 16682 buildPostUpdate(*this, RD.ExprPostUpdates)); 16683 } 16684 16685 bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind, 16686 SourceLocation LinLoc) { 16687 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) || 16688 LinKind == OMPC_LINEAR_unknown) { 16689 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus; 16690 return true; 16691 } 16692 return false; 16693 } 16694 16695 bool Sema::CheckOpenMPLinearDecl(const ValueDecl *D, SourceLocation ELoc, 16696 OpenMPLinearClauseKind LinKind, QualType Type, 16697 bool IsDeclareSimd) { 16698 const auto *VD = dyn_cast_or_null<VarDecl>(D); 16699 // A variable must not have an incomplete type or a reference type. 16700 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type)) 16701 return true; 16702 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) && 16703 !Type->isReferenceType()) { 16704 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference) 16705 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind); 16706 return true; 16707 } 16708 Type = Type.getNonReferenceType(); 16709 16710 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions] 16711 // A variable that is privatized must not have a const-qualified type 16712 // unless it is of class type with a mutable member. This restriction does 16713 // not apply to the firstprivate clause, nor to the linear clause on 16714 // declarative directives (like declare simd). 16715 if (!IsDeclareSimd && 16716 rejectConstNotMutableType(*this, D, Type, OMPC_linear, ELoc)) 16717 return true; 16718 16719 // A list item must be of integral or pointer type. 16720 Type = Type.getUnqualifiedType().getCanonicalType(); 16721 const auto *Ty = Type.getTypePtrOrNull(); 16722 if (!Ty || (LinKind != OMPC_LINEAR_ref && !Ty->isDependentType() && 16723 !Ty->isIntegralType(Context) && !Ty->isPointerType())) { 16724 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type; 16725 if (D) { 16726 bool IsDecl = 16727 !VD || 16728 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 16729 Diag(D->getLocation(), 16730 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 16731 << D; 16732 } 16733 return true; 16734 } 16735 return false; 16736 } 16737 16738 OMPClause *Sema::ActOnOpenMPLinearClause( 16739 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc, 16740 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind, 16741 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 16742 SmallVector<Expr *, 8> Vars; 16743 SmallVector<Expr *, 8> Privates; 16744 SmallVector<Expr *, 8> Inits; 16745 SmallVector<Decl *, 4> ExprCaptures; 16746 SmallVector<Expr *, 4> ExprPostUpdates; 16747 if (CheckOpenMPLinearModifier(LinKind, LinLoc)) 16748 LinKind = OMPC_LINEAR_val; 16749 for (Expr *RefExpr : VarList) { 16750 assert(RefExpr && "NULL expr in OpenMP linear clause."); 16751 SourceLocation ELoc; 16752 SourceRange ERange; 16753 Expr *SimpleRefExpr = RefExpr; 16754 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 16755 if (Res.second) { 16756 // It will be analyzed later. 16757 Vars.push_back(RefExpr); 16758 Privates.push_back(nullptr); 16759 Inits.push_back(nullptr); 16760 } 16761 ValueDecl *D = Res.first; 16762 if (!D) 16763 continue; 16764 16765 QualType Type = D->getType(); 16766 auto *VD = dyn_cast<VarDecl>(D); 16767 16768 // OpenMP [2.14.3.7, linear clause] 16769 // A list-item cannot appear in more than one linear clause. 16770 // A list-item that appears in a linear clause cannot appear in any 16771 // other data-sharing attribute clause. 16772 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 16773 if (DVar.RefExpr) { 16774 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 16775 << getOpenMPClauseName(OMPC_linear); 16776 reportOriginalDsa(*this, DSAStack, D, DVar); 16777 continue; 16778 } 16779 16780 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type)) 16781 continue; 16782 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType(); 16783 16784 // Build private copy of original var. 16785 VarDecl *Private = 16786 buildVarDecl(*this, ELoc, Type, D->getName(), 16787 D->hasAttrs() ? &D->getAttrs() : nullptr, 16788 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 16789 DeclRefExpr *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc); 16790 // Build var to save initial value. 16791 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start"); 16792 Expr *InitExpr; 16793 DeclRefExpr *Ref = nullptr; 16794 if (!VD && !CurContext->isDependentContext()) { 16795 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 16796 if (!isOpenMPCapturedDecl(D)) { 16797 ExprCaptures.push_back(Ref->getDecl()); 16798 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) { 16799 ExprResult RefRes = DefaultLvalueConversion(Ref); 16800 if (!RefRes.isUsable()) 16801 continue; 16802 ExprResult PostUpdateRes = 16803 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, 16804 SimpleRefExpr, RefRes.get()); 16805 if (!PostUpdateRes.isUsable()) 16806 continue; 16807 ExprPostUpdates.push_back( 16808 IgnoredValueConversions(PostUpdateRes.get()).get()); 16809 } 16810 } 16811 } 16812 if (LinKind == OMPC_LINEAR_uval) 16813 InitExpr = VD ? VD->getInit() : SimpleRefExpr; 16814 else 16815 InitExpr = VD ? SimpleRefExpr : Ref; 16816 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(), 16817 /*DirectInit=*/false); 16818 DeclRefExpr *InitRef = buildDeclRefExpr(*this, Init, Type, ELoc); 16819 16820 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref); 16821 Vars.push_back((VD || CurContext->isDependentContext()) 16822 ? RefExpr->IgnoreParens() 16823 : Ref); 16824 Privates.push_back(PrivateRef); 16825 Inits.push_back(InitRef); 16826 } 16827 16828 if (Vars.empty()) 16829 return nullptr; 16830 16831 Expr *StepExpr = Step; 16832 Expr *CalcStepExpr = nullptr; 16833 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && 16834 !Step->isInstantiationDependent() && 16835 !Step->containsUnexpandedParameterPack()) { 16836 SourceLocation StepLoc = Step->getBeginLoc(); 16837 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step); 16838 if (Val.isInvalid()) 16839 return nullptr; 16840 StepExpr = Val.get(); 16841 16842 // Build var to save the step value. 16843 VarDecl *SaveVar = 16844 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step"); 16845 ExprResult SaveRef = 16846 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc); 16847 ExprResult CalcStep = 16848 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr); 16849 CalcStep = ActOnFinishFullExpr(CalcStep.get(), /*DiscardedValue*/ false); 16850 16851 // Warn about zero linear step (it would be probably better specified as 16852 // making corresponding variables 'const'). 16853 if (Optional<llvm::APSInt> Result = 16854 StepExpr->getIntegerConstantExpr(Context)) { 16855 if (!Result->isNegative() && !Result->isStrictlyPositive()) 16856 Diag(StepLoc, diag::warn_omp_linear_step_zero) 16857 << Vars[0] << (Vars.size() > 1); 16858 } else if (CalcStep.isUsable()) { 16859 // Calculate the step beforehand instead of doing this on each iteration. 16860 // (This is not used if the number of iterations may be kfold-ed). 16861 CalcStepExpr = CalcStep.get(); 16862 } 16863 } 16864 16865 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc, 16866 ColonLoc, EndLoc, Vars, Privates, Inits, 16867 StepExpr, CalcStepExpr, 16868 buildPreInits(Context, ExprCaptures), 16869 buildPostUpdate(*this, ExprPostUpdates)); 16870 } 16871 16872 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, 16873 Expr *NumIterations, Sema &SemaRef, 16874 Scope *S, DSAStackTy *Stack) { 16875 // Walk the vars and build update/final expressions for the CodeGen. 16876 SmallVector<Expr *, 8> Updates; 16877 SmallVector<Expr *, 8> Finals; 16878 SmallVector<Expr *, 8> UsedExprs; 16879 Expr *Step = Clause.getStep(); 16880 Expr *CalcStep = Clause.getCalcStep(); 16881 // OpenMP [2.14.3.7, linear clause] 16882 // If linear-step is not specified it is assumed to be 1. 16883 if (!Step) 16884 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(); 16885 else if (CalcStep) 16886 Step = cast<BinaryOperator>(CalcStep)->getLHS(); 16887 bool HasErrors = false; 16888 auto CurInit = Clause.inits().begin(); 16889 auto CurPrivate = Clause.privates().begin(); 16890 OpenMPLinearClauseKind LinKind = Clause.getModifier(); 16891 for (Expr *RefExpr : Clause.varlists()) { 16892 SourceLocation ELoc; 16893 SourceRange ERange; 16894 Expr *SimpleRefExpr = RefExpr; 16895 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange); 16896 ValueDecl *D = Res.first; 16897 if (Res.second || !D) { 16898 Updates.push_back(nullptr); 16899 Finals.push_back(nullptr); 16900 HasErrors = true; 16901 continue; 16902 } 16903 auto &&Info = Stack->isLoopControlVariable(D); 16904 // OpenMP [2.15.11, distribute simd Construct] 16905 // A list item may not appear in a linear clause, unless it is the loop 16906 // iteration variable. 16907 if (isOpenMPDistributeDirective(Stack->getCurrentDirective()) && 16908 isOpenMPSimdDirective(Stack->getCurrentDirective()) && !Info.first) { 16909 SemaRef.Diag(ELoc, 16910 diag::err_omp_linear_distribute_var_non_loop_iteration); 16911 Updates.push_back(nullptr); 16912 Finals.push_back(nullptr); 16913 HasErrors = true; 16914 continue; 16915 } 16916 Expr *InitExpr = *CurInit; 16917 16918 // Build privatized reference to the current linear var. 16919 auto *DE = cast<DeclRefExpr>(SimpleRefExpr); 16920 Expr *CapturedRef; 16921 if (LinKind == OMPC_LINEAR_uval) 16922 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit(); 16923 else 16924 CapturedRef = 16925 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()), 16926 DE->getType().getUnqualifiedType(), DE->getExprLoc(), 16927 /*RefersToCapture=*/true); 16928 16929 // Build update: Var = InitExpr + IV * Step 16930 ExprResult Update; 16931 if (!Info.first) 16932 Update = buildCounterUpdate( 16933 SemaRef, S, RefExpr->getExprLoc(), *CurPrivate, InitExpr, IV, Step, 16934 /*Subtract=*/false, /*IsNonRectangularLB=*/false); 16935 else 16936 Update = *CurPrivate; 16937 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getBeginLoc(), 16938 /*DiscardedValue*/ false); 16939 16940 // Build final: Var = InitExpr + NumIterations * Step 16941 ExprResult Final; 16942 if (!Info.first) 16943 Final = 16944 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef, 16945 InitExpr, NumIterations, Step, /*Subtract=*/false, 16946 /*IsNonRectangularLB=*/false); 16947 else 16948 Final = *CurPrivate; 16949 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getBeginLoc(), 16950 /*DiscardedValue*/ false); 16951 16952 if (!Update.isUsable() || !Final.isUsable()) { 16953 Updates.push_back(nullptr); 16954 Finals.push_back(nullptr); 16955 UsedExprs.push_back(nullptr); 16956 HasErrors = true; 16957 } else { 16958 Updates.push_back(Update.get()); 16959 Finals.push_back(Final.get()); 16960 if (!Info.first) 16961 UsedExprs.push_back(SimpleRefExpr); 16962 } 16963 ++CurInit; 16964 ++CurPrivate; 16965 } 16966 if (Expr *S = Clause.getStep()) 16967 UsedExprs.push_back(S); 16968 // Fill the remaining part with the nullptr. 16969 UsedExprs.append(Clause.varlist_size() + 1 - UsedExprs.size(), nullptr); 16970 Clause.setUpdates(Updates); 16971 Clause.setFinals(Finals); 16972 Clause.setUsedExprs(UsedExprs); 16973 return HasErrors; 16974 } 16975 16976 OMPClause *Sema::ActOnOpenMPAlignedClause( 16977 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc, 16978 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 16979 SmallVector<Expr *, 8> Vars; 16980 for (Expr *RefExpr : VarList) { 16981 assert(RefExpr && "NULL expr in OpenMP linear clause."); 16982 SourceLocation ELoc; 16983 SourceRange ERange; 16984 Expr *SimpleRefExpr = RefExpr; 16985 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 16986 if (Res.second) { 16987 // It will be analyzed later. 16988 Vars.push_back(RefExpr); 16989 } 16990 ValueDecl *D = Res.first; 16991 if (!D) 16992 continue; 16993 16994 QualType QType = D->getType(); 16995 auto *VD = dyn_cast<VarDecl>(D); 16996 16997 // OpenMP [2.8.1, simd construct, Restrictions] 16998 // The type of list items appearing in the aligned clause must be 16999 // array, pointer, reference to array, or reference to pointer. 17000 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType(); 17001 const Type *Ty = QType.getTypePtrOrNull(); 17002 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) { 17003 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr) 17004 << QType << getLangOpts().CPlusPlus << ERange; 17005 bool IsDecl = 17006 !VD || 17007 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 17008 Diag(D->getLocation(), 17009 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 17010 << D; 17011 continue; 17012 } 17013 17014 // OpenMP [2.8.1, simd construct, Restrictions] 17015 // A list-item cannot appear in more than one aligned clause. 17016 if (const Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) { 17017 Diag(ELoc, diag::err_omp_used_in_clause_twice) 17018 << 0 << getOpenMPClauseName(OMPC_aligned) << ERange; 17019 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa) 17020 << getOpenMPClauseName(OMPC_aligned); 17021 continue; 17022 } 17023 17024 DeclRefExpr *Ref = nullptr; 17025 if (!VD && isOpenMPCapturedDecl(D)) 17026 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 17027 Vars.push_back(DefaultFunctionArrayConversion( 17028 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref) 17029 .get()); 17030 } 17031 17032 // OpenMP [2.8.1, simd construct, Description] 17033 // The parameter of the aligned clause, alignment, must be a constant 17034 // positive integer expression. 17035 // If no optional parameter is specified, implementation-defined default 17036 // alignments for SIMD instructions on the target platforms are assumed. 17037 if (Alignment != nullptr) { 17038 ExprResult AlignResult = 17039 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned); 17040 if (AlignResult.isInvalid()) 17041 return nullptr; 17042 Alignment = AlignResult.get(); 17043 } 17044 if (Vars.empty()) 17045 return nullptr; 17046 17047 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc, 17048 EndLoc, Vars, Alignment); 17049 } 17050 17051 OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList, 17052 SourceLocation StartLoc, 17053 SourceLocation LParenLoc, 17054 SourceLocation EndLoc) { 17055 SmallVector<Expr *, 8> Vars; 17056 SmallVector<Expr *, 8> SrcExprs; 17057 SmallVector<Expr *, 8> DstExprs; 17058 SmallVector<Expr *, 8> AssignmentOps; 17059 for (Expr *RefExpr : VarList) { 17060 assert(RefExpr && "NULL expr in OpenMP copyin clause."); 17061 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 17062 // It will be analyzed later. 17063 Vars.push_back(RefExpr); 17064 SrcExprs.push_back(nullptr); 17065 DstExprs.push_back(nullptr); 17066 AssignmentOps.push_back(nullptr); 17067 continue; 17068 } 17069 17070 SourceLocation ELoc = RefExpr->getExprLoc(); 17071 // OpenMP [2.1, C/C++] 17072 // A list item is a variable name. 17073 // OpenMP [2.14.4.1, Restrictions, p.1] 17074 // A list item that appears in a copyin clause must be threadprivate. 17075 auto *DE = dyn_cast<DeclRefExpr>(RefExpr); 17076 if (!DE || !isa<VarDecl>(DE->getDecl())) { 17077 Diag(ELoc, diag::err_omp_expected_var_name_member_expr) 17078 << 0 << RefExpr->getSourceRange(); 17079 continue; 17080 } 17081 17082 Decl *D = DE->getDecl(); 17083 auto *VD = cast<VarDecl>(D); 17084 17085 QualType Type = VD->getType(); 17086 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 17087 // It will be analyzed later. 17088 Vars.push_back(DE); 17089 SrcExprs.push_back(nullptr); 17090 DstExprs.push_back(nullptr); 17091 AssignmentOps.push_back(nullptr); 17092 continue; 17093 } 17094 17095 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1] 17096 // A list item that appears in a copyin clause must be threadprivate. 17097 if (!DSAStack->isThreadPrivate(VD)) { 17098 Diag(ELoc, diag::err_omp_required_access) 17099 << getOpenMPClauseName(OMPC_copyin) 17100 << getOpenMPDirectiveName(OMPD_threadprivate); 17101 continue; 17102 } 17103 17104 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 17105 // A variable of class type (or array thereof) that appears in a 17106 // copyin clause requires an accessible, unambiguous copy assignment 17107 // operator for the class type. 17108 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType(); 17109 VarDecl *SrcVD = 17110 buildVarDecl(*this, DE->getBeginLoc(), ElemType.getUnqualifiedType(), 17111 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr); 17112 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr( 17113 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc()); 17114 VarDecl *DstVD = 17115 buildVarDecl(*this, DE->getBeginLoc(), ElemType, ".copyin.dst", 17116 VD->hasAttrs() ? &VD->getAttrs() : nullptr); 17117 DeclRefExpr *PseudoDstExpr = 17118 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc()); 17119 // For arrays generate assignment operation for single element and replace 17120 // it by the original array element in CodeGen. 17121 ExprResult AssignmentOp = 17122 BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, PseudoDstExpr, 17123 PseudoSrcExpr); 17124 if (AssignmentOp.isInvalid()) 17125 continue; 17126 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(), 17127 /*DiscardedValue*/ false); 17128 if (AssignmentOp.isInvalid()) 17129 continue; 17130 17131 DSAStack->addDSA(VD, DE, OMPC_copyin); 17132 Vars.push_back(DE); 17133 SrcExprs.push_back(PseudoSrcExpr); 17134 DstExprs.push_back(PseudoDstExpr); 17135 AssignmentOps.push_back(AssignmentOp.get()); 17136 } 17137 17138 if (Vars.empty()) 17139 return nullptr; 17140 17141 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 17142 SrcExprs, DstExprs, AssignmentOps); 17143 } 17144 17145 OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList, 17146 SourceLocation StartLoc, 17147 SourceLocation LParenLoc, 17148 SourceLocation EndLoc) { 17149 SmallVector<Expr *, 8> Vars; 17150 SmallVector<Expr *, 8> SrcExprs; 17151 SmallVector<Expr *, 8> DstExprs; 17152 SmallVector<Expr *, 8> AssignmentOps; 17153 for (Expr *RefExpr : VarList) { 17154 assert(RefExpr && "NULL expr in OpenMP linear clause."); 17155 SourceLocation ELoc; 17156 SourceRange ERange; 17157 Expr *SimpleRefExpr = RefExpr; 17158 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 17159 if (Res.second) { 17160 // It will be analyzed later. 17161 Vars.push_back(RefExpr); 17162 SrcExprs.push_back(nullptr); 17163 DstExprs.push_back(nullptr); 17164 AssignmentOps.push_back(nullptr); 17165 } 17166 ValueDecl *D = Res.first; 17167 if (!D) 17168 continue; 17169 17170 QualType Type = D->getType(); 17171 auto *VD = dyn_cast<VarDecl>(D); 17172 17173 // OpenMP [2.14.4.2, Restrictions, p.2] 17174 // A list item that appears in a copyprivate clause may not appear in a 17175 // private or firstprivate clause on the single construct. 17176 if (!VD || !DSAStack->isThreadPrivate(VD)) { 17177 DSAStackTy::DSAVarData DVar = 17178 DSAStack->getTopDSA(D, /*FromParent=*/false); 17179 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate && 17180 DVar.RefExpr) { 17181 Diag(ELoc, diag::err_omp_wrong_dsa) 17182 << getOpenMPClauseName(DVar.CKind) 17183 << getOpenMPClauseName(OMPC_copyprivate); 17184 reportOriginalDsa(*this, DSAStack, D, DVar); 17185 continue; 17186 } 17187 17188 // OpenMP [2.11.4.2, Restrictions, p.1] 17189 // All list items that appear in a copyprivate clause must be either 17190 // threadprivate or private in the enclosing context. 17191 if (DVar.CKind == OMPC_unknown) { 17192 DVar = DSAStack->getImplicitDSA(D, false); 17193 if (DVar.CKind == OMPC_shared) { 17194 Diag(ELoc, diag::err_omp_required_access) 17195 << getOpenMPClauseName(OMPC_copyprivate) 17196 << "threadprivate or private in the enclosing context"; 17197 reportOriginalDsa(*this, DSAStack, D, DVar); 17198 continue; 17199 } 17200 } 17201 } 17202 17203 // Variably modified types are not supported. 17204 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) { 17205 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 17206 << getOpenMPClauseName(OMPC_copyprivate) << Type 17207 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 17208 bool IsDecl = 17209 !VD || 17210 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 17211 Diag(D->getLocation(), 17212 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 17213 << D; 17214 continue; 17215 } 17216 17217 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 17218 // A variable of class type (or array thereof) that appears in a 17219 // copyin clause requires an accessible, unambiguous copy assignment 17220 // operator for the class type. 17221 Type = Context.getBaseElementType(Type.getNonReferenceType()) 17222 .getUnqualifiedType(); 17223 VarDecl *SrcVD = 17224 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.src", 17225 D->hasAttrs() ? &D->getAttrs() : nullptr); 17226 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc); 17227 VarDecl *DstVD = 17228 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.dst", 17229 D->hasAttrs() ? &D->getAttrs() : nullptr); 17230 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc); 17231 ExprResult AssignmentOp = BuildBinOp( 17232 DSAStack->getCurScope(), ELoc, BO_Assign, PseudoDstExpr, PseudoSrcExpr); 17233 if (AssignmentOp.isInvalid()) 17234 continue; 17235 AssignmentOp = 17236 ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false); 17237 if (AssignmentOp.isInvalid()) 17238 continue; 17239 17240 // No need to mark vars as copyprivate, they are already threadprivate or 17241 // implicitly private. 17242 assert(VD || isOpenMPCapturedDecl(D)); 17243 Vars.push_back( 17244 VD ? RefExpr->IgnoreParens() 17245 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false)); 17246 SrcExprs.push_back(PseudoSrcExpr); 17247 DstExprs.push_back(PseudoDstExpr); 17248 AssignmentOps.push_back(AssignmentOp.get()); 17249 } 17250 17251 if (Vars.empty()) 17252 return nullptr; 17253 17254 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 17255 Vars, SrcExprs, DstExprs, AssignmentOps); 17256 } 17257 17258 OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList, 17259 SourceLocation StartLoc, 17260 SourceLocation LParenLoc, 17261 SourceLocation EndLoc) { 17262 if (VarList.empty()) 17263 return nullptr; 17264 17265 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList); 17266 } 17267 17268 /// Tries to find omp_depend_t. type. 17269 static bool findOMPDependT(Sema &S, SourceLocation Loc, DSAStackTy *Stack, 17270 bool Diagnose = true) { 17271 QualType OMPDependT = Stack->getOMPDependT(); 17272 if (!OMPDependT.isNull()) 17273 return true; 17274 IdentifierInfo *II = &S.PP.getIdentifierTable().get("omp_depend_t"); 17275 ParsedType PT = S.getTypeName(*II, Loc, S.getCurScope()); 17276 if (!PT.getAsOpaquePtr() || PT.get().isNull()) { 17277 if (Diagnose) 17278 S.Diag(Loc, diag::err_omp_implied_type_not_found) << "omp_depend_t"; 17279 return false; 17280 } 17281 Stack->setOMPDependT(PT.get()); 17282 return true; 17283 } 17284 17285 OMPClause *Sema::ActOnOpenMPDepobjClause(Expr *Depobj, SourceLocation StartLoc, 17286 SourceLocation LParenLoc, 17287 SourceLocation EndLoc) { 17288 if (!Depobj) 17289 return nullptr; 17290 17291 bool OMPDependTFound = findOMPDependT(*this, StartLoc, DSAStack); 17292 17293 // OpenMP 5.0, 2.17.10.1 depobj Construct 17294 // depobj is an lvalue expression of type omp_depend_t. 17295 if (!Depobj->isTypeDependent() && !Depobj->isValueDependent() && 17296 !Depobj->isInstantiationDependent() && 17297 !Depobj->containsUnexpandedParameterPack() && 17298 (OMPDependTFound && 17299 !Context.typesAreCompatible(DSAStack->getOMPDependT(), Depobj->getType(), 17300 /*CompareUnqualified=*/true))) { 17301 Diag(Depobj->getExprLoc(), diag::err_omp_expected_omp_depend_t_lvalue) 17302 << 0 << Depobj->getType() << Depobj->getSourceRange(); 17303 } 17304 17305 if (!Depobj->isLValue()) { 17306 Diag(Depobj->getExprLoc(), diag::err_omp_expected_omp_depend_t_lvalue) 17307 << 1 << Depobj->getSourceRange(); 17308 } 17309 17310 return OMPDepobjClause::Create(Context, StartLoc, LParenLoc, EndLoc, Depobj); 17311 } 17312 17313 OMPClause * 17314 Sema::ActOnOpenMPDependClause(Expr *DepModifier, OpenMPDependClauseKind DepKind, 17315 SourceLocation DepLoc, SourceLocation ColonLoc, 17316 ArrayRef<Expr *> VarList, SourceLocation StartLoc, 17317 SourceLocation LParenLoc, SourceLocation EndLoc) { 17318 if (DSAStack->getCurrentDirective() == OMPD_ordered && 17319 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) { 17320 Diag(DepLoc, diag::err_omp_unexpected_clause_value) 17321 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend); 17322 return nullptr; 17323 } 17324 if ((DSAStack->getCurrentDirective() != OMPD_ordered || 17325 DSAStack->getCurrentDirective() == OMPD_depobj) && 17326 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source || 17327 DepKind == OMPC_DEPEND_sink || 17328 ((LangOpts.OpenMP < 50 || 17329 DSAStack->getCurrentDirective() == OMPD_depobj) && 17330 DepKind == OMPC_DEPEND_depobj))) { 17331 SmallVector<unsigned, 3> Except; 17332 Except.push_back(OMPC_DEPEND_source); 17333 Except.push_back(OMPC_DEPEND_sink); 17334 if (LangOpts.OpenMP < 50 || DSAStack->getCurrentDirective() == OMPD_depobj) 17335 Except.push_back(OMPC_DEPEND_depobj); 17336 std::string Expected = (LangOpts.OpenMP >= 50 && !DepModifier) 17337 ? "depend modifier(iterator) or " 17338 : ""; 17339 Diag(DepLoc, diag::err_omp_unexpected_clause_value) 17340 << Expected + getListOfPossibleValues(OMPC_depend, /*First=*/0, 17341 /*Last=*/OMPC_DEPEND_unknown, 17342 Except) 17343 << getOpenMPClauseName(OMPC_depend); 17344 return nullptr; 17345 } 17346 if (DepModifier && 17347 (DepKind == OMPC_DEPEND_source || DepKind == OMPC_DEPEND_sink)) { 17348 Diag(DepModifier->getExprLoc(), 17349 diag::err_omp_depend_sink_source_with_modifier); 17350 return nullptr; 17351 } 17352 if (DepModifier && 17353 !DepModifier->getType()->isSpecificBuiltinType(BuiltinType::OMPIterator)) 17354 Diag(DepModifier->getExprLoc(), diag::err_omp_depend_modifier_not_iterator); 17355 17356 SmallVector<Expr *, 8> Vars; 17357 DSAStackTy::OperatorOffsetTy OpsOffs; 17358 llvm::APSInt DepCounter(/*BitWidth=*/32); 17359 llvm::APSInt TotalDepCount(/*BitWidth=*/32); 17360 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) { 17361 if (const Expr *OrderedCountExpr = 17362 DSAStack->getParentOrderedRegionParam().first) { 17363 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context); 17364 TotalDepCount.setIsUnsigned(/*Val=*/true); 17365 } 17366 } 17367 for (Expr *RefExpr : VarList) { 17368 assert(RefExpr && "NULL expr in OpenMP shared clause."); 17369 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 17370 // It will be analyzed later. 17371 Vars.push_back(RefExpr); 17372 continue; 17373 } 17374 17375 SourceLocation ELoc = RefExpr->getExprLoc(); 17376 Expr *SimpleExpr = RefExpr->IgnoreParenCasts(); 17377 if (DepKind == OMPC_DEPEND_sink) { 17378 if (DSAStack->getParentOrderedRegionParam().first && 17379 DepCounter >= TotalDepCount) { 17380 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr); 17381 continue; 17382 } 17383 ++DepCounter; 17384 // OpenMP [2.13.9, Summary] 17385 // depend(dependence-type : vec), where dependence-type is: 17386 // 'sink' and where vec is the iteration vector, which has the form: 17387 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn] 17388 // where n is the value specified by the ordered clause in the loop 17389 // directive, xi denotes the loop iteration variable of the i-th nested 17390 // loop associated with the loop directive, and di is a constant 17391 // non-negative integer. 17392 if (CurContext->isDependentContext()) { 17393 // It will be analyzed later. 17394 Vars.push_back(RefExpr); 17395 continue; 17396 } 17397 SimpleExpr = SimpleExpr->IgnoreImplicit(); 17398 OverloadedOperatorKind OOK = OO_None; 17399 SourceLocation OOLoc; 17400 Expr *LHS = SimpleExpr; 17401 Expr *RHS = nullptr; 17402 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) { 17403 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode()); 17404 OOLoc = BO->getOperatorLoc(); 17405 LHS = BO->getLHS()->IgnoreParenImpCasts(); 17406 RHS = BO->getRHS()->IgnoreParenImpCasts(); 17407 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) { 17408 OOK = OCE->getOperator(); 17409 OOLoc = OCE->getOperatorLoc(); 17410 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); 17411 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts(); 17412 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) { 17413 OOK = MCE->getMethodDecl() 17414 ->getNameInfo() 17415 .getName() 17416 .getCXXOverloadedOperator(); 17417 OOLoc = MCE->getCallee()->getExprLoc(); 17418 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts(); 17419 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); 17420 } 17421 SourceLocation ELoc; 17422 SourceRange ERange; 17423 auto Res = getPrivateItem(*this, LHS, ELoc, ERange); 17424 if (Res.second) { 17425 // It will be analyzed later. 17426 Vars.push_back(RefExpr); 17427 } 17428 ValueDecl *D = Res.first; 17429 if (!D) 17430 continue; 17431 17432 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) { 17433 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus); 17434 continue; 17435 } 17436 if (RHS) { 17437 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause( 17438 RHS, OMPC_depend, /*StrictlyPositive=*/false); 17439 if (RHSRes.isInvalid()) 17440 continue; 17441 } 17442 if (!CurContext->isDependentContext() && 17443 DSAStack->getParentOrderedRegionParam().first && 17444 DepCounter != DSAStack->isParentLoopControlVariable(D).first) { 17445 const ValueDecl *VD = 17446 DSAStack->getParentLoopControlVariable(DepCounter.getZExtValue()); 17447 if (VD) 17448 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) 17449 << 1 << VD; 17450 else 17451 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) << 0; 17452 continue; 17453 } 17454 OpsOffs.emplace_back(RHS, OOK); 17455 } else { 17456 bool OMPDependTFound = LangOpts.OpenMP >= 50; 17457 if (OMPDependTFound) 17458 OMPDependTFound = findOMPDependT(*this, StartLoc, DSAStack, 17459 DepKind == OMPC_DEPEND_depobj); 17460 if (DepKind == OMPC_DEPEND_depobj) { 17461 // OpenMP 5.0, 2.17.11 depend Clause, Restrictions, C/C++ 17462 // List items used in depend clauses with the depobj dependence type 17463 // must be expressions of the omp_depend_t type. 17464 if (!RefExpr->isValueDependent() && !RefExpr->isTypeDependent() && 17465 !RefExpr->isInstantiationDependent() && 17466 !RefExpr->containsUnexpandedParameterPack() && 17467 (OMPDependTFound && 17468 !Context.hasSameUnqualifiedType(DSAStack->getOMPDependT(), 17469 RefExpr->getType()))) { 17470 Diag(ELoc, diag::err_omp_expected_omp_depend_t_lvalue) 17471 << 0 << RefExpr->getType() << RefExpr->getSourceRange(); 17472 continue; 17473 } 17474 if (!RefExpr->isLValue()) { 17475 Diag(ELoc, diag::err_omp_expected_omp_depend_t_lvalue) 17476 << 1 << RefExpr->getType() << RefExpr->getSourceRange(); 17477 continue; 17478 } 17479 } else { 17480 // OpenMP 5.0 [2.17.11, Restrictions] 17481 // List items used in depend clauses cannot be zero-length array 17482 // sections. 17483 QualType ExprTy = RefExpr->getType().getNonReferenceType(); 17484 const auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr); 17485 if (OASE) { 17486 QualType BaseType = 17487 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 17488 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 17489 ExprTy = ATy->getElementType(); 17490 else 17491 ExprTy = BaseType->getPointeeType(); 17492 ExprTy = ExprTy.getNonReferenceType(); 17493 const Expr *Length = OASE->getLength(); 17494 Expr::EvalResult Result; 17495 if (Length && !Length->isValueDependent() && 17496 Length->EvaluateAsInt(Result, Context) && 17497 Result.Val.getInt().isNullValue()) { 17498 Diag(ELoc, 17499 diag::err_omp_depend_zero_length_array_section_not_allowed) 17500 << SimpleExpr->getSourceRange(); 17501 continue; 17502 } 17503 } 17504 17505 // OpenMP 5.0, 2.17.11 depend Clause, Restrictions, C/C++ 17506 // List items used in depend clauses with the in, out, inout or 17507 // mutexinoutset dependence types cannot be expressions of the 17508 // omp_depend_t type. 17509 if (!RefExpr->isValueDependent() && !RefExpr->isTypeDependent() && 17510 !RefExpr->isInstantiationDependent() && 17511 !RefExpr->containsUnexpandedParameterPack() && 17512 (OMPDependTFound && 17513 DSAStack->getOMPDependT().getTypePtr() == ExprTy.getTypePtr())) { 17514 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 17515 << (LangOpts.OpenMP >= 50 ? 1 : 0) << 1 17516 << RefExpr->getSourceRange(); 17517 continue; 17518 } 17519 17520 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr); 17521 if (!RefExpr->IgnoreParenImpCasts()->isLValue() || 17522 (ASE && !ASE->getBase()->isTypeDependent() && 17523 !ASE->getBase() 17524 ->getType() 17525 .getNonReferenceType() 17526 ->isPointerType() && 17527 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) { 17528 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 17529 << (LangOpts.OpenMP >= 50 ? 1 : 0) 17530 << (LangOpts.OpenMP >= 50 ? 1 : 0) << RefExpr->getSourceRange(); 17531 continue; 17532 } 17533 17534 ExprResult Res; 17535 { 17536 Sema::TentativeAnalysisScope Trap(*this); 17537 Res = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, 17538 RefExpr->IgnoreParenImpCasts()); 17539 } 17540 if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr) && 17541 !isa<OMPArrayShapingExpr>(SimpleExpr)) { 17542 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 17543 << (LangOpts.OpenMP >= 50 ? 1 : 0) 17544 << (LangOpts.OpenMP >= 50 ? 1 : 0) << RefExpr->getSourceRange(); 17545 continue; 17546 } 17547 } 17548 } 17549 Vars.push_back(RefExpr->IgnoreParenImpCasts()); 17550 } 17551 17552 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink && 17553 TotalDepCount > VarList.size() && 17554 DSAStack->getParentOrderedRegionParam().first && 17555 DSAStack->getParentLoopControlVariable(VarList.size() + 1)) { 17556 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration) 17557 << 1 << DSAStack->getParentLoopControlVariable(VarList.size() + 1); 17558 } 17559 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink && 17560 Vars.empty()) 17561 return nullptr; 17562 17563 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, 17564 DepModifier, DepKind, DepLoc, ColonLoc, 17565 Vars, TotalDepCount.getZExtValue()); 17566 if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) && 17567 DSAStack->isParentOrderedRegion()) 17568 DSAStack->addDoacrossDependClause(C, OpsOffs); 17569 return C; 17570 } 17571 17572 OMPClause *Sema::ActOnOpenMPDeviceClause(OpenMPDeviceClauseModifier Modifier, 17573 Expr *Device, SourceLocation StartLoc, 17574 SourceLocation LParenLoc, 17575 SourceLocation ModifierLoc, 17576 SourceLocation EndLoc) { 17577 assert((ModifierLoc.isInvalid() || LangOpts.OpenMP >= 50) && 17578 "Unexpected device modifier in OpenMP < 50."); 17579 17580 bool ErrorFound = false; 17581 if (ModifierLoc.isValid() && Modifier == OMPC_DEVICE_unknown) { 17582 std::string Values = 17583 getListOfPossibleValues(OMPC_device, /*First=*/0, OMPC_DEVICE_unknown); 17584 Diag(ModifierLoc, diag::err_omp_unexpected_clause_value) 17585 << Values << getOpenMPClauseName(OMPC_device); 17586 ErrorFound = true; 17587 } 17588 17589 Expr *ValExpr = Device; 17590 Stmt *HelperValStmt = nullptr; 17591 17592 // OpenMP [2.9.1, Restrictions] 17593 // The device expression must evaluate to a non-negative integer value. 17594 ErrorFound = !isNonNegativeIntegerValue(ValExpr, *this, OMPC_device, 17595 /*StrictlyPositive=*/false) || 17596 ErrorFound; 17597 if (ErrorFound) 17598 return nullptr; 17599 17600 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 17601 OpenMPDirectiveKind CaptureRegion = 17602 getOpenMPCaptureRegionForClause(DKind, OMPC_device, LangOpts.OpenMP); 17603 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 17604 ValExpr = MakeFullExpr(ValExpr).get(); 17605 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 17606 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 17607 HelperValStmt = buildPreInits(Context, Captures); 17608 } 17609 17610 return new (Context) 17611 OMPDeviceClause(Modifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc, 17612 LParenLoc, ModifierLoc, EndLoc); 17613 } 17614 17615 static bool checkTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef, 17616 DSAStackTy *Stack, QualType QTy, 17617 bool FullCheck = true) { 17618 NamedDecl *ND; 17619 if (QTy->isIncompleteType(&ND)) { 17620 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR; 17621 return false; 17622 } 17623 if (FullCheck && !SemaRef.CurContext->isDependentContext() && 17624 !QTy.isTriviallyCopyableType(SemaRef.Context)) 17625 SemaRef.Diag(SL, diag::warn_omp_non_trivial_type_mapped) << QTy << SR; 17626 return true; 17627 } 17628 17629 /// Return true if it can be proven that the provided array expression 17630 /// (array section or array subscript) does NOT specify the whole size of the 17631 /// array whose base type is \a BaseQTy. 17632 static bool checkArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef, 17633 const Expr *E, 17634 QualType BaseQTy) { 17635 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E); 17636 17637 // If this is an array subscript, it refers to the whole size if the size of 17638 // the dimension is constant and equals 1. Also, an array section assumes the 17639 // format of an array subscript if no colon is used. 17640 if (isa<ArraySubscriptExpr>(E) || 17641 (OASE && OASE->getColonLocFirst().isInvalid())) { 17642 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) 17643 return ATy->getSize().getSExtValue() != 1; 17644 // Size can't be evaluated statically. 17645 return false; 17646 } 17647 17648 assert(OASE && "Expecting array section if not an array subscript."); 17649 const Expr *LowerBound = OASE->getLowerBound(); 17650 const Expr *Length = OASE->getLength(); 17651 17652 // If there is a lower bound that does not evaluates to zero, we are not 17653 // covering the whole dimension. 17654 if (LowerBound) { 17655 Expr::EvalResult Result; 17656 if (!LowerBound->EvaluateAsInt(Result, SemaRef.getASTContext())) 17657 return false; // Can't get the integer value as a constant. 17658 17659 llvm::APSInt ConstLowerBound = Result.Val.getInt(); 17660 if (ConstLowerBound.getSExtValue()) 17661 return true; 17662 } 17663 17664 // If we don't have a length we covering the whole dimension. 17665 if (!Length) 17666 return false; 17667 17668 // If the base is a pointer, we don't have a way to get the size of the 17669 // pointee. 17670 if (BaseQTy->isPointerType()) 17671 return false; 17672 17673 // We can only check if the length is the same as the size of the dimension 17674 // if we have a constant array. 17675 const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()); 17676 if (!CATy) 17677 return false; 17678 17679 Expr::EvalResult Result; 17680 if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext())) 17681 return false; // Can't get the integer value as a constant. 17682 17683 llvm::APSInt ConstLength = Result.Val.getInt(); 17684 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue(); 17685 } 17686 17687 // Return true if it can be proven that the provided array expression (array 17688 // section or array subscript) does NOT specify a single element of the array 17689 // whose base type is \a BaseQTy. 17690 static bool checkArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef, 17691 const Expr *E, 17692 QualType BaseQTy) { 17693 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E); 17694 17695 // An array subscript always refer to a single element. Also, an array section 17696 // assumes the format of an array subscript if no colon is used. 17697 if (isa<ArraySubscriptExpr>(E) || 17698 (OASE && OASE->getColonLocFirst().isInvalid())) 17699 return false; 17700 17701 assert(OASE && "Expecting array section if not an array subscript."); 17702 const Expr *Length = OASE->getLength(); 17703 17704 // If we don't have a length we have to check if the array has unitary size 17705 // for this dimension. Also, we should always expect a length if the base type 17706 // is pointer. 17707 if (!Length) { 17708 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) 17709 return ATy->getSize().getSExtValue() != 1; 17710 // We cannot assume anything. 17711 return false; 17712 } 17713 17714 // Check if the length evaluates to 1. 17715 Expr::EvalResult Result; 17716 if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext())) 17717 return false; // Can't get the integer value as a constant. 17718 17719 llvm::APSInt ConstLength = Result.Val.getInt(); 17720 return ConstLength.getSExtValue() != 1; 17721 } 17722 17723 // The base of elements of list in a map clause have to be either: 17724 // - a reference to variable or field. 17725 // - a member expression. 17726 // - an array expression. 17727 // 17728 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the 17729 // reference to 'r'. 17730 // 17731 // If we have: 17732 // 17733 // struct SS { 17734 // Bla S; 17735 // foo() { 17736 // #pragma omp target map (S.Arr[:12]); 17737 // } 17738 // } 17739 // 17740 // We want to retrieve the member expression 'this->S'; 17741 17742 // OpenMP 5.0 [2.19.7.1, map Clause, Restrictions, p.2] 17743 // If a list item is an array section, it must specify contiguous storage. 17744 // 17745 // For this restriction it is sufficient that we make sure only references 17746 // to variables or fields and array expressions, and that no array sections 17747 // exist except in the rightmost expression (unless they cover the whole 17748 // dimension of the array). E.g. these would be invalid: 17749 // 17750 // r.ArrS[3:5].Arr[6:7] 17751 // 17752 // r.ArrS[3:5].x 17753 // 17754 // but these would be valid: 17755 // r.ArrS[3].Arr[6:7] 17756 // 17757 // r.ArrS[3].x 17758 namespace { 17759 class MapBaseChecker final : public StmtVisitor<MapBaseChecker, bool> { 17760 Sema &SemaRef; 17761 OpenMPClauseKind CKind = OMPC_unknown; 17762 OpenMPDirectiveKind DKind = OMPD_unknown; 17763 OMPClauseMappableExprCommon::MappableExprComponentList &Components; 17764 bool IsNonContiguous = false; 17765 bool NoDiagnose = false; 17766 const Expr *RelevantExpr = nullptr; 17767 bool AllowUnitySizeArraySection = true; 17768 bool AllowWholeSizeArraySection = true; 17769 bool AllowAnotherPtr = true; 17770 SourceLocation ELoc; 17771 SourceRange ERange; 17772 17773 void emitErrorMsg() { 17774 // If nothing else worked, this is not a valid map clause expression. 17775 if (SemaRef.getLangOpts().OpenMP < 50) { 17776 SemaRef.Diag(ELoc, 17777 diag::err_omp_expected_named_var_member_or_array_expression) 17778 << ERange; 17779 } else { 17780 SemaRef.Diag(ELoc, diag::err_omp_non_lvalue_in_map_or_motion_clauses) 17781 << getOpenMPClauseName(CKind) << ERange; 17782 } 17783 } 17784 17785 public: 17786 bool VisitDeclRefExpr(DeclRefExpr *DRE) { 17787 if (!isa<VarDecl>(DRE->getDecl())) { 17788 emitErrorMsg(); 17789 return false; 17790 } 17791 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 17792 RelevantExpr = DRE; 17793 // Record the component. 17794 Components.emplace_back(DRE, DRE->getDecl(), IsNonContiguous); 17795 return true; 17796 } 17797 17798 bool VisitMemberExpr(MemberExpr *ME) { 17799 Expr *E = ME; 17800 Expr *BaseE = ME->getBase()->IgnoreParenCasts(); 17801 17802 if (isa<CXXThisExpr>(BaseE)) { 17803 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 17804 // We found a base expression: this->Val. 17805 RelevantExpr = ME; 17806 } else { 17807 E = BaseE; 17808 } 17809 17810 if (!isa<FieldDecl>(ME->getMemberDecl())) { 17811 if (!NoDiagnose) { 17812 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field) 17813 << ME->getSourceRange(); 17814 return false; 17815 } 17816 if (RelevantExpr) 17817 return false; 17818 return Visit(E); 17819 } 17820 17821 auto *FD = cast<FieldDecl>(ME->getMemberDecl()); 17822 17823 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3] 17824 // A bit-field cannot appear in a map clause. 17825 // 17826 if (FD->isBitField()) { 17827 if (!NoDiagnose) { 17828 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause) 17829 << ME->getSourceRange() << getOpenMPClauseName(CKind); 17830 return false; 17831 } 17832 if (RelevantExpr) 17833 return false; 17834 return Visit(E); 17835 } 17836 17837 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 17838 // If the type of a list item is a reference to a type T then the type 17839 // will be considered to be T for all purposes of this clause. 17840 QualType CurType = BaseE->getType().getNonReferenceType(); 17841 17842 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2] 17843 // A list item cannot be a variable that is a member of a structure with 17844 // a union type. 17845 // 17846 if (CurType->isUnionType()) { 17847 if (!NoDiagnose) { 17848 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed) 17849 << ME->getSourceRange(); 17850 return false; 17851 } 17852 return RelevantExpr || Visit(E); 17853 } 17854 17855 // If we got a member expression, we should not expect any array section 17856 // before that: 17857 // 17858 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7] 17859 // If a list item is an element of a structure, only the rightmost symbol 17860 // of the variable reference can be an array section. 17861 // 17862 AllowUnitySizeArraySection = false; 17863 AllowWholeSizeArraySection = false; 17864 17865 // Record the component. 17866 Components.emplace_back(ME, FD, IsNonContiguous); 17867 return RelevantExpr || Visit(E); 17868 } 17869 17870 bool VisitArraySubscriptExpr(ArraySubscriptExpr *AE) { 17871 Expr *E = AE->getBase()->IgnoreParenImpCasts(); 17872 17873 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) { 17874 if (!NoDiagnose) { 17875 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name) 17876 << 0 << AE->getSourceRange(); 17877 return false; 17878 } 17879 return RelevantExpr || Visit(E); 17880 } 17881 17882 // If we got an array subscript that express the whole dimension we 17883 // can have any array expressions before. If it only expressing part of 17884 // the dimension, we can only have unitary-size array expressions. 17885 if (checkArrayExpressionDoesNotReferToWholeSize(SemaRef, AE, 17886 E->getType())) 17887 AllowWholeSizeArraySection = false; 17888 17889 if (const auto *TE = dyn_cast<CXXThisExpr>(E->IgnoreParenCasts())) { 17890 Expr::EvalResult Result; 17891 if (!AE->getIdx()->isValueDependent() && 17892 AE->getIdx()->EvaluateAsInt(Result, SemaRef.getASTContext()) && 17893 !Result.Val.getInt().isNullValue()) { 17894 SemaRef.Diag(AE->getIdx()->getExprLoc(), 17895 diag::err_omp_invalid_map_this_expr); 17896 SemaRef.Diag(AE->getIdx()->getExprLoc(), 17897 diag::note_omp_invalid_subscript_on_this_ptr_map); 17898 } 17899 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 17900 RelevantExpr = TE; 17901 } 17902 17903 // Record the component - we don't have any declaration associated. 17904 Components.emplace_back(AE, nullptr, IsNonContiguous); 17905 17906 return RelevantExpr || Visit(E); 17907 } 17908 17909 bool VisitOMPArraySectionExpr(OMPArraySectionExpr *OASE) { 17910 assert(!NoDiagnose && "Array sections cannot be implicitly mapped."); 17911 Expr *E = OASE->getBase()->IgnoreParenImpCasts(); 17912 QualType CurType = 17913 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType(); 17914 17915 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 17916 // If the type of a list item is a reference to a type T then the type 17917 // will be considered to be T for all purposes of this clause. 17918 if (CurType->isReferenceType()) 17919 CurType = CurType->getPointeeType(); 17920 17921 bool IsPointer = CurType->isAnyPointerType(); 17922 17923 if (!IsPointer && !CurType->isArrayType()) { 17924 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name) 17925 << 0 << OASE->getSourceRange(); 17926 return false; 17927 } 17928 17929 bool NotWhole = 17930 checkArrayExpressionDoesNotReferToWholeSize(SemaRef, OASE, CurType); 17931 bool NotUnity = 17932 checkArrayExpressionDoesNotReferToUnitySize(SemaRef, OASE, CurType); 17933 17934 if (AllowWholeSizeArraySection) { 17935 // Any array section is currently allowed. Allowing a whole size array 17936 // section implies allowing a unity array section as well. 17937 // 17938 // If this array section refers to the whole dimension we can still 17939 // accept other array sections before this one, except if the base is a 17940 // pointer. Otherwise, only unitary sections are accepted. 17941 if (NotWhole || IsPointer) 17942 AllowWholeSizeArraySection = false; 17943 } else if (DKind == OMPD_target_update && 17944 SemaRef.getLangOpts().OpenMP >= 50) { 17945 if (IsPointer && !AllowAnotherPtr) 17946 SemaRef.Diag(ELoc, diag::err_omp_section_length_undefined) 17947 << /*array of unknown bound */ 1; 17948 else 17949 IsNonContiguous = true; 17950 } else if (AllowUnitySizeArraySection && NotUnity) { 17951 // A unity or whole array section is not allowed and that is not 17952 // compatible with the properties of the current array section. 17953 SemaRef.Diag( 17954 ELoc, diag::err_array_section_does_not_specify_contiguous_storage) 17955 << OASE->getSourceRange(); 17956 return false; 17957 } 17958 17959 if (IsPointer) 17960 AllowAnotherPtr = false; 17961 17962 if (const auto *TE = dyn_cast<CXXThisExpr>(E)) { 17963 Expr::EvalResult ResultR; 17964 Expr::EvalResult ResultL; 17965 if (!OASE->getLength()->isValueDependent() && 17966 OASE->getLength()->EvaluateAsInt(ResultR, SemaRef.getASTContext()) && 17967 !ResultR.Val.getInt().isOneValue()) { 17968 SemaRef.Diag(OASE->getLength()->getExprLoc(), 17969 diag::err_omp_invalid_map_this_expr); 17970 SemaRef.Diag(OASE->getLength()->getExprLoc(), 17971 diag::note_omp_invalid_length_on_this_ptr_mapping); 17972 } 17973 if (OASE->getLowerBound() && !OASE->getLowerBound()->isValueDependent() && 17974 OASE->getLowerBound()->EvaluateAsInt(ResultL, 17975 SemaRef.getASTContext()) && 17976 !ResultL.Val.getInt().isNullValue()) { 17977 SemaRef.Diag(OASE->getLowerBound()->getExprLoc(), 17978 diag::err_omp_invalid_map_this_expr); 17979 SemaRef.Diag(OASE->getLowerBound()->getExprLoc(), 17980 diag::note_omp_invalid_lower_bound_on_this_ptr_mapping); 17981 } 17982 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 17983 RelevantExpr = TE; 17984 } 17985 17986 // Record the component - we don't have any declaration associated. 17987 Components.emplace_back(OASE, nullptr, /*IsNonContiguous=*/false); 17988 return RelevantExpr || Visit(E); 17989 } 17990 bool VisitOMPArrayShapingExpr(OMPArrayShapingExpr *E) { 17991 Expr *Base = E->getBase(); 17992 17993 // Record the component - we don't have any declaration associated. 17994 Components.emplace_back(E, nullptr, IsNonContiguous); 17995 17996 return Visit(Base->IgnoreParenImpCasts()); 17997 } 17998 17999 bool VisitUnaryOperator(UnaryOperator *UO) { 18000 if (SemaRef.getLangOpts().OpenMP < 50 || !UO->isLValue() || 18001 UO->getOpcode() != UO_Deref) { 18002 emitErrorMsg(); 18003 return false; 18004 } 18005 if (!RelevantExpr) { 18006 // Record the component if haven't found base decl. 18007 Components.emplace_back(UO, nullptr, /*IsNonContiguous=*/false); 18008 } 18009 return RelevantExpr || Visit(UO->getSubExpr()->IgnoreParenImpCasts()); 18010 } 18011 bool VisitBinaryOperator(BinaryOperator *BO) { 18012 if (SemaRef.getLangOpts().OpenMP < 50 || !BO->getType()->isPointerType()) { 18013 emitErrorMsg(); 18014 return false; 18015 } 18016 18017 // Pointer arithmetic is the only thing we expect to happen here so after we 18018 // make sure the binary operator is a pointer type, the we only thing need 18019 // to to is to visit the subtree that has the same type as root (so that we 18020 // know the other subtree is just an offset) 18021 Expr *LE = BO->getLHS()->IgnoreParenImpCasts(); 18022 Expr *RE = BO->getRHS()->IgnoreParenImpCasts(); 18023 Components.emplace_back(BO, nullptr, false); 18024 assert((LE->getType().getTypePtr() == BO->getType().getTypePtr() || 18025 RE->getType().getTypePtr() == BO->getType().getTypePtr()) && 18026 "Either LHS or RHS have base decl inside"); 18027 if (BO->getType().getTypePtr() == LE->getType().getTypePtr()) 18028 return RelevantExpr || Visit(LE); 18029 return RelevantExpr || Visit(RE); 18030 } 18031 bool VisitCXXThisExpr(CXXThisExpr *CTE) { 18032 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 18033 RelevantExpr = CTE; 18034 Components.emplace_back(CTE, nullptr, IsNonContiguous); 18035 return true; 18036 } 18037 bool VisitCXXOperatorCallExpr(CXXOperatorCallExpr *COCE) { 18038 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 18039 Components.emplace_back(COCE, nullptr, IsNonContiguous); 18040 return true; 18041 } 18042 bool VisitOpaqueValueExpr(OpaqueValueExpr *E) { 18043 Expr *Source = E->getSourceExpr(); 18044 if (!Source) { 18045 emitErrorMsg(); 18046 return false; 18047 } 18048 return Visit(Source); 18049 } 18050 bool VisitStmt(Stmt *) { 18051 emitErrorMsg(); 18052 return false; 18053 } 18054 const Expr *getFoundBase() const { 18055 return RelevantExpr; 18056 } 18057 explicit MapBaseChecker( 18058 Sema &SemaRef, OpenMPClauseKind CKind, OpenMPDirectiveKind DKind, 18059 OMPClauseMappableExprCommon::MappableExprComponentList &Components, 18060 bool NoDiagnose, SourceLocation &ELoc, SourceRange &ERange) 18061 : SemaRef(SemaRef), CKind(CKind), DKind(DKind), Components(Components), 18062 NoDiagnose(NoDiagnose), ELoc(ELoc), ERange(ERange) {} 18063 }; 18064 } // namespace 18065 18066 /// Return the expression of the base of the mappable expression or null if it 18067 /// cannot be determined and do all the necessary checks to see if the expression 18068 /// is valid as a standalone mappable expression. In the process, record all the 18069 /// components of the expression. 18070 static const Expr *checkMapClauseExpressionBase( 18071 Sema &SemaRef, Expr *E, 18072 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents, 18073 OpenMPClauseKind CKind, OpenMPDirectiveKind DKind, bool NoDiagnose) { 18074 SourceLocation ELoc = E->getExprLoc(); 18075 SourceRange ERange = E->getSourceRange(); 18076 MapBaseChecker Checker(SemaRef, CKind, DKind, CurComponents, NoDiagnose, ELoc, 18077 ERange); 18078 if (Checker.Visit(E->IgnoreParens())) { 18079 // Check if the highest dimension array section has length specified 18080 if (SemaRef.getLangOpts().OpenMP >= 50 && !CurComponents.empty() && 18081 (CKind == OMPC_to || CKind == OMPC_from)) { 18082 auto CI = CurComponents.rbegin(); 18083 auto CE = CurComponents.rend(); 18084 for (; CI != CE; ++CI) { 18085 const auto *OASE = 18086 dyn_cast<OMPArraySectionExpr>(CI->getAssociatedExpression()); 18087 if (!OASE) 18088 continue; 18089 if (OASE && OASE->getLength()) 18090 break; 18091 SemaRef.Diag(ELoc, diag::err_array_section_does_not_specify_length) 18092 << ERange; 18093 } 18094 } 18095 return Checker.getFoundBase(); 18096 } 18097 return nullptr; 18098 } 18099 18100 // Return true if expression E associated with value VD has conflicts with other 18101 // map information. 18102 static bool checkMapConflicts( 18103 Sema &SemaRef, DSAStackTy *DSAS, const ValueDecl *VD, const Expr *E, 18104 bool CurrentRegionOnly, 18105 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents, 18106 OpenMPClauseKind CKind) { 18107 assert(VD && E); 18108 SourceLocation ELoc = E->getExprLoc(); 18109 SourceRange ERange = E->getSourceRange(); 18110 18111 // In order to easily check the conflicts we need to match each component of 18112 // the expression under test with the components of the expressions that are 18113 // already in the stack. 18114 18115 assert(!CurComponents.empty() && "Map clause expression with no components!"); 18116 assert(CurComponents.back().getAssociatedDeclaration() == VD && 18117 "Map clause expression with unexpected base!"); 18118 18119 // Variables to help detecting enclosing problems in data environment nests. 18120 bool IsEnclosedByDataEnvironmentExpr = false; 18121 const Expr *EnclosingExpr = nullptr; 18122 18123 bool FoundError = DSAS->checkMappableExprComponentListsForDecl( 18124 VD, CurrentRegionOnly, 18125 [&IsEnclosedByDataEnvironmentExpr, &SemaRef, VD, CurrentRegionOnly, ELoc, 18126 ERange, CKind, &EnclosingExpr, 18127 CurComponents](OMPClauseMappableExprCommon::MappableExprComponentListRef 18128 StackComponents, 18129 OpenMPClauseKind Kind) { 18130 if (CKind == Kind && SemaRef.LangOpts.OpenMP >= 50) 18131 return false; 18132 assert(!StackComponents.empty() && 18133 "Map clause expression with no components!"); 18134 assert(StackComponents.back().getAssociatedDeclaration() == VD && 18135 "Map clause expression with unexpected base!"); 18136 (void)VD; 18137 18138 // The whole expression in the stack. 18139 const Expr *RE = StackComponents.front().getAssociatedExpression(); 18140 18141 // Expressions must start from the same base. Here we detect at which 18142 // point both expressions diverge from each other and see if we can 18143 // detect if the memory referred to both expressions is contiguous and 18144 // do not overlap. 18145 auto CI = CurComponents.rbegin(); 18146 auto CE = CurComponents.rend(); 18147 auto SI = StackComponents.rbegin(); 18148 auto SE = StackComponents.rend(); 18149 for (; CI != CE && SI != SE; ++CI, ++SI) { 18150 18151 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3] 18152 // At most one list item can be an array item derived from a given 18153 // variable in map clauses of the same construct. 18154 if (CurrentRegionOnly && 18155 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) || 18156 isa<OMPArraySectionExpr>(CI->getAssociatedExpression()) || 18157 isa<OMPArrayShapingExpr>(CI->getAssociatedExpression())) && 18158 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) || 18159 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()) || 18160 isa<OMPArrayShapingExpr>(SI->getAssociatedExpression()))) { 18161 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(), 18162 diag::err_omp_multiple_array_items_in_map_clause) 18163 << CI->getAssociatedExpression()->getSourceRange(); 18164 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(), 18165 diag::note_used_here) 18166 << SI->getAssociatedExpression()->getSourceRange(); 18167 return true; 18168 } 18169 18170 // Do both expressions have the same kind? 18171 if (CI->getAssociatedExpression()->getStmtClass() != 18172 SI->getAssociatedExpression()->getStmtClass()) 18173 break; 18174 18175 // Are we dealing with different variables/fields? 18176 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration()) 18177 break; 18178 } 18179 // Check if the extra components of the expressions in the enclosing 18180 // data environment are redundant for the current base declaration. 18181 // If they are, the maps completely overlap, which is legal. 18182 for (; SI != SE; ++SI) { 18183 QualType Type; 18184 if (const auto *ASE = 18185 dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) { 18186 Type = ASE->getBase()->IgnoreParenImpCasts()->getType(); 18187 } else if (const auto *OASE = dyn_cast<OMPArraySectionExpr>( 18188 SI->getAssociatedExpression())) { 18189 const Expr *E = OASE->getBase()->IgnoreParenImpCasts(); 18190 Type = 18191 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType(); 18192 } else if (const auto *OASE = dyn_cast<OMPArrayShapingExpr>( 18193 SI->getAssociatedExpression())) { 18194 Type = OASE->getBase()->getType()->getPointeeType(); 18195 } 18196 if (Type.isNull() || Type->isAnyPointerType() || 18197 checkArrayExpressionDoesNotReferToWholeSize( 18198 SemaRef, SI->getAssociatedExpression(), Type)) 18199 break; 18200 } 18201 18202 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4] 18203 // List items of map clauses in the same construct must not share 18204 // original storage. 18205 // 18206 // If the expressions are exactly the same or one is a subset of the 18207 // other, it means they are sharing storage. 18208 if (CI == CE && SI == SE) { 18209 if (CurrentRegionOnly) { 18210 if (CKind == OMPC_map) { 18211 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange; 18212 } else { 18213 assert(CKind == OMPC_to || CKind == OMPC_from); 18214 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update) 18215 << ERange; 18216 } 18217 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 18218 << RE->getSourceRange(); 18219 return true; 18220 } 18221 // If we find the same expression in the enclosing data environment, 18222 // that is legal. 18223 IsEnclosedByDataEnvironmentExpr = true; 18224 return false; 18225 } 18226 18227 QualType DerivedType = 18228 std::prev(CI)->getAssociatedDeclaration()->getType(); 18229 SourceLocation DerivedLoc = 18230 std::prev(CI)->getAssociatedExpression()->getExprLoc(); 18231 18232 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 18233 // If the type of a list item is a reference to a type T then the type 18234 // will be considered to be T for all purposes of this clause. 18235 DerivedType = DerivedType.getNonReferenceType(); 18236 18237 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1] 18238 // A variable for which the type is pointer and an array section 18239 // derived from that variable must not appear as list items of map 18240 // clauses of the same construct. 18241 // 18242 // Also, cover one of the cases in: 18243 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5] 18244 // If any part of the original storage of a list item has corresponding 18245 // storage in the device data environment, all of the original storage 18246 // must have corresponding storage in the device data environment. 18247 // 18248 if (DerivedType->isAnyPointerType()) { 18249 if (CI == CE || SI == SE) { 18250 SemaRef.Diag( 18251 DerivedLoc, 18252 diag::err_omp_pointer_mapped_along_with_derived_section) 18253 << DerivedLoc; 18254 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 18255 << RE->getSourceRange(); 18256 return true; 18257 } 18258 if (CI->getAssociatedExpression()->getStmtClass() != 18259 SI->getAssociatedExpression()->getStmtClass() || 18260 CI->getAssociatedDeclaration()->getCanonicalDecl() == 18261 SI->getAssociatedDeclaration()->getCanonicalDecl()) { 18262 assert(CI != CE && SI != SE); 18263 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_dereferenced) 18264 << DerivedLoc; 18265 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 18266 << RE->getSourceRange(); 18267 return true; 18268 } 18269 } 18270 18271 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4] 18272 // List items of map clauses in the same construct must not share 18273 // original storage. 18274 // 18275 // An expression is a subset of the other. 18276 if (CurrentRegionOnly && (CI == CE || SI == SE)) { 18277 if (CKind == OMPC_map) { 18278 if (CI != CE || SI != SE) { 18279 // Allow constructs like this: map(s, s.ptr[0:1]), where s.ptr is 18280 // a pointer. 18281 auto Begin = 18282 CI != CE ? CurComponents.begin() : StackComponents.begin(); 18283 auto End = CI != CE ? CurComponents.end() : StackComponents.end(); 18284 auto It = Begin; 18285 while (It != End && !It->getAssociatedDeclaration()) 18286 std::advance(It, 1); 18287 assert(It != End && 18288 "Expected at least one component with the declaration."); 18289 if (It != Begin && It->getAssociatedDeclaration() 18290 ->getType() 18291 .getCanonicalType() 18292 ->isAnyPointerType()) { 18293 IsEnclosedByDataEnvironmentExpr = false; 18294 EnclosingExpr = nullptr; 18295 return false; 18296 } 18297 } 18298 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange; 18299 } else { 18300 assert(CKind == OMPC_to || CKind == OMPC_from); 18301 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update) 18302 << ERange; 18303 } 18304 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 18305 << RE->getSourceRange(); 18306 return true; 18307 } 18308 18309 // The current expression uses the same base as other expression in the 18310 // data environment but does not contain it completely. 18311 if (!CurrentRegionOnly && SI != SE) 18312 EnclosingExpr = RE; 18313 18314 // The current expression is a subset of the expression in the data 18315 // environment. 18316 IsEnclosedByDataEnvironmentExpr |= 18317 (!CurrentRegionOnly && CI != CE && SI == SE); 18318 18319 return false; 18320 }); 18321 18322 if (CurrentRegionOnly) 18323 return FoundError; 18324 18325 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5] 18326 // If any part of the original storage of a list item has corresponding 18327 // storage in the device data environment, all of the original storage must 18328 // have corresponding storage in the device data environment. 18329 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6] 18330 // If a list item is an element of a structure, and a different element of 18331 // the structure has a corresponding list item in the device data environment 18332 // prior to a task encountering the construct associated with the map clause, 18333 // then the list item must also have a corresponding list item in the device 18334 // data environment prior to the task encountering the construct. 18335 // 18336 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) { 18337 SemaRef.Diag(ELoc, 18338 diag::err_omp_original_storage_is_shared_and_does_not_contain) 18339 << ERange; 18340 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here) 18341 << EnclosingExpr->getSourceRange(); 18342 return true; 18343 } 18344 18345 return FoundError; 18346 } 18347 18348 // Look up the user-defined mapper given the mapper name and mapped type, and 18349 // build a reference to it. 18350 static ExprResult buildUserDefinedMapperRef(Sema &SemaRef, Scope *S, 18351 CXXScopeSpec &MapperIdScopeSpec, 18352 const DeclarationNameInfo &MapperId, 18353 QualType Type, 18354 Expr *UnresolvedMapper) { 18355 if (MapperIdScopeSpec.isInvalid()) 18356 return ExprError(); 18357 // Get the actual type for the array type. 18358 if (Type->isArrayType()) { 18359 assert(Type->getAsArrayTypeUnsafe() && "Expect to get a valid array type"); 18360 Type = Type->getAsArrayTypeUnsafe()->getElementType().getCanonicalType(); 18361 } 18362 // Find all user-defined mappers with the given MapperId. 18363 SmallVector<UnresolvedSet<8>, 4> Lookups; 18364 LookupResult Lookup(SemaRef, MapperId, Sema::LookupOMPMapperName); 18365 Lookup.suppressDiagnostics(); 18366 if (S) { 18367 while (S && SemaRef.LookupParsedName(Lookup, S, &MapperIdScopeSpec)) { 18368 NamedDecl *D = Lookup.getRepresentativeDecl(); 18369 while (S && !S->isDeclScope(D)) 18370 S = S->getParent(); 18371 if (S) 18372 S = S->getParent(); 18373 Lookups.emplace_back(); 18374 Lookups.back().append(Lookup.begin(), Lookup.end()); 18375 Lookup.clear(); 18376 } 18377 } else if (auto *ULE = cast_or_null<UnresolvedLookupExpr>(UnresolvedMapper)) { 18378 // Extract the user-defined mappers with the given MapperId. 18379 Lookups.push_back(UnresolvedSet<8>()); 18380 for (NamedDecl *D : ULE->decls()) { 18381 auto *DMD = cast<OMPDeclareMapperDecl>(D); 18382 assert(DMD && "Expect valid OMPDeclareMapperDecl during instantiation."); 18383 Lookups.back().addDecl(DMD); 18384 } 18385 } 18386 // Defer the lookup for dependent types. The results will be passed through 18387 // UnresolvedMapper on instantiation. 18388 if (SemaRef.CurContext->isDependentContext() || Type->isDependentType() || 18389 Type->isInstantiationDependentType() || 18390 Type->containsUnexpandedParameterPack() || 18391 filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) { 18392 return !D->isInvalidDecl() && 18393 (D->getType()->isDependentType() || 18394 D->getType()->isInstantiationDependentType() || 18395 D->getType()->containsUnexpandedParameterPack()); 18396 })) { 18397 UnresolvedSet<8> URS; 18398 for (const UnresolvedSet<8> &Set : Lookups) { 18399 if (Set.empty()) 18400 continue; 18401 URS.append(Set.begin(), Set.end()); 18402 } 18403 return UnresolvedLookupExpr::Create( 18404 SemaRef.Context, /*NamingClass=*/nullptr, 18405 MapperIdScopeSpec.getWithLocInContext(SemaRef.Context), MapperId, 18406 /*ADL=*/false, /*Overloaded=*/true, URS.begin(), URS.end()); 18407 } 18408 SourceLocation Loc = MapperId.getLoc(); 18409 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions 18410 // The type must be of struct, union or class type in C and C++ 18411 if (!Type->isStructureOrClassType() && !Type->isUnionType() && 18412 (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default")) { 18413 SemaRef.Diag(Loc, diag::err_omp_mapper_wrong_type); 18414 return ExprError(); 18415 } 18416 // Perform argument dependent lookup. 18417 if (SemaRef.getLangOpts().CPlusPlus && !MapperIdScopeSpec.isSet()) 18418 argumentDependentLookup(SemaRef, MapperId, Loc, Type, Lookups); 18419 // Return the first user-defined mapper with the desired type. 18420 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 18421 Lookups, [&SemaRef, Type](ValueDecl *D) -> ValueDecl * { 18422 if (!D->isInvalidDecl() && 18423 SemaRef.Context.hasSameType(D->getType(), Type)) 18424 return D; 18425 return nullptr; 18426 })) 18427 return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc); 18428 // Find the first user-defined mapper with a type derived from the desired 18429 // type. 18430 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 18431 Lookups, [&SemaRef, Type, Loc](ValueDecl *D) -> ValueDecl * { 18432 if (!D->isInvalidDecl() && 18433 SemaRef.IsDerivedFrom(Loc, Type, D->getType()) && 18434 !Type.isMoreQualifiedThan(D->getType())) 18435 return D; 18436 return nullptr; 18437 })) { 18438 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, 18439 /*DetectVirtual=*/false); 18440 if (SemaRef.IsDerivedFrom(Loc, Type, VD->getType(), Paths)) { 18441 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType( 18442 VD->getType().getUnqualifiedType()))) { 18443 if (SemaRef.CheckBaseClassAccess( 18444 Loc, VD->getType(), Type, Paths.front(), 18445 /*DiagID=*/0) != Sema::AR_inaccessible) { 18446 return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc); 18447 } 18448 } 18449 } 18450 } 18451 // Report error if a mapper is specified, but cannot be found. 18452 if (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default") { 18453 SemaRef.Diag(Loc, diag::err_omp_invalid_mapper) 18454 << Type << MapperId.getName(); 18455 return ExprError(); 18456 } 18457 return ExprEmpty(); 18458 } 18459 18460 namespace { 18461 // Utility struct that gathers all the related lists associated with a mappable 18462 // expression. 18463 struct MappableVarListInfo { 18464 // The list of expressions. 18465 ArrayRef<Expr *> VarList; 18466 // The list of processed expressions. 18467 SmallVector<Expr *, 16> ProcessedVarList; 18468 // The mappble components for each expression. 18469 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents; 18470 // The base declaration of the variable. 18471 SmallVector<ValueDecl *, 16> VarBaseDeclarations; 18472 // The reference to the user-defined mapper associated with every expression. 18473 SmallVector<Expr *, 16> UDMapperList; 18474 18475 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) { 18476 // We have a list of components and base declarations for each entry in the 18477 // variable list. 18478 VarComponents.reserve(VarList.size()); 18479 VarBaseDeclarations.reserve(VarList.size()); 18480 } 18481 }; 18482 } 18483 18484 // Check the validity of the provided variable list for the provided clause kind 18485 // \a CKind. In the check process the valid expressions, mappable expression 18486 // components, variables, and user-defined mappers are extracted and used to 18487 // fill \a ProcessedVarList, \a VarComponents, \a VarBaseDeclarations, and \a 18488 // UDMapperList in MVLI. \a MapType, \a IsMapTypeImplicit, \a MapperIdScopeSpec, 18489 // and \a MapperId are expected to be valid if the clause kind is 'map'. 18490 static void checkMappableExpressionList( 18491 Sema &SemaRef, DSAStackTy *DSAS, OpenMPClauseKind CKind, 18492 MappableVarListInfo &MVLI, SourceLocation StartLoc, 18493 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo MapperId, 18494 ArrayRef<Expr *> UnresolvedMappers, 18495 OpenMPMapClauseKind MapType = OMPC_MAP_unknown, 18496 bool IsMapTypeImplicit = false) { 18497 // We only expect mappable expressions in 'to', 'from', and 'map' clauses. 18498 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) && 18499 "Unexpected clause kind with mappable expressions!"); 18500 18501 // If the identifier of user-defined mapper is not specified, it is "default". 18502 // We do not change the actual name in this clause to distinguish whether a 18503 // mapper is specified explicitly, i.e., it is not explicitly specified when 18504 // MapperId.getName() is empty. 18505 if (!MapperId.getName() || MapperId.getName().isEmpty()) { 18506 auto &DeclNames = SemaRef.getASTContext().DeclarationNames; 18507 MapperId.setName(DeclNames.getIdentifier( 18508 &SemaRef.getASTContext().Idents.get("default"))); 18509 MapperId.setLoc(StartLoc); 18510 } 18511 18512 // Iterators to find the current unresolved mapper expression. 18513 auto UMIt = UnresolvedMappers.begin(), UMEnd = UnresolvedMappers.end(); 18514 bool UpdateUMIt = false; 18515 Expr *UnresolvedMapper = nullptr; 18516 18517 // Keep track of the mappable components and base declarations in this clause. 18518 // Each entry in the list is going to have a list of components associated. We 18519 // record each set of the components so that we can build the clause later on. 18520 // In the end we should have the same amount of declarations and component 18521 // lists. 18522 18523 for (Expr *RE : MVLI.VarList) { 18524 assert(RE && "Null expr in omp to/from/map clause"); 18525 SourceLocation ELoc = RE->getExprLoc(); 18526 18527 // Find the current unresolved mapper expression. 18528 if (UpdateUMIt && UMIt != UMEnd) { 18529 UMIt++; 18530 assert( 18531 UMIt != UMEnd && 18532 "Expect the size of UnresolvedMappers to match with that of VarList"); 18533 } 18534 UpdateUMIt = true; 18535 if (UMIt != UMEnd) 18536 UnresolvedMapper = *UMIt; 18537 18538 const Expr *VE = RE->IgnoreParenLValueCasts(); 18539 18540 if (VE->isValueDependent() || VE->isTypeDependent() || 18541 VE->isInstantiationDependent() || 18542 VE->containsUnexpandedParameterPack()) { 18543 // Try to find the associated user-defined mapper. 18544 ExprResult ER = buildUserDefinedMapperRef( 18545 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId, 18546 VE->getType().getCanonicalType(), UnresolvedMapper); 18547 if (ER.isInvalid()) 18548 continue; 18549 MVLI.UDMapperList.push_back(ER.get()); 18550 // We can only analyze this information once the missing information is 18551 // resolved. 18552 MVLI.ProcessedVarList.push_back(RE); 18553 continue; 18554 } 18555 18556 Expr *SimpleExpr = RE->IgnoreParenCasts(); 18557 18558 if (!RE->isLValue()) { 18559 if (SemaRef.getLangOpts().OpenMP < 50) { 18560 SemaRef.Diag( 18561 ELoc, diag::err_omp_expected_named_var_member_or_array_expression) 18562 << RE->getSourceRange(); 18563 } else { 18564 SemaRef.Diag(ELoc, diag::err_omp_non_lvalue_in_map_or_motion_clauses) 18565 << getOpenMPClauseName(CKind) << RE->getSourceRange(); 18566 } 18567 continue; 18568 } 18569 18570 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents; 18571 ValueDecl *CurDeclaration = nullptr; 18572 18573 // Obtain the array or member expression bases if required. Also, fill the 18574 // components array with all the components identified in the process. 18575 const Expr *BE = checkMapClauseExpressionBase( 18576 SemaRef, SimpleExpr, CurComponents, CKind, DSAS->getCurrentDirective(), 18577 /*NoDiagnose=*/false); 18578 if (!BE) 18579 continue; 18580 18581 assert(!CurComponents.empty() && 18582 "Invalid mappable expression information."); 18583 18584 if (const auto *TE = dyn_cast<CXXThisExpr>(BE)) { 18585 // Add store "this" pointer to class in DSAStackTy for future checking 18586 DSAS->addMappedClassesQualTypes(TE->getType()); 18587 // Try to find the associated user-defined mapper. 18588 ExprResult ER = buildUserDefinedMapperRef( 18589 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId, 18590 VE->getType().getCanonicalType(), UnresolvedMapper); 18591 if (ER.isInvalid()) 18592 continue; 18593 MVLI.UDMapperList.push_back(ER.get()); 18594 // Skip restriction checking for variable or field declarations 18595 MVLI.ProcessedVarList.push_back(RE); 18596 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 18597 MVLI.VarComponents.back().append(CurComponents.begin(), 18598 CurComponents.end()); 18599 MVLI.VarBaseDeclarations.push_back(nullptr); 18600 continue; 18601 } 18602 18603 // For the following checks, we rely on the base declaration which is 18604 // expected to be associated with the last component. The declaration is 18605 // expected to be a variable or a field (if 'this' is being mapped). 18606 CurDeclaration = CurComponents.back().getAssociatedDeclaration(); 18607 assert(CurDeclaration && "Null decl on map clause."); 18608 assert( 18609 CurDeclaration->isCanonicalDecl() && 18610 "Expecting components to have associated only canonical declarations."); 18611 18612 auto *VD = dyn_cast<VarDecl>(CurDeclaration); 18613 const auto *FD = dyn_cast<FieldDecl>(CurDeclaration); 18614 18615 assert((VD || FD) && "Only variables or fields are expected here!"); 18616 (void)FD; 18617 18618 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10] 18619 // threadprivate variables cannot appear in a map clause. 18620 // OpenMP 4.5 [2.10.5, target update Construct] 18621 // threadprivate variables cannot appear in a from clause. 18622 if (VD && DSAS->isThreadPrivate(VD)) { 18623 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false); 18624 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause) 18625 << getOpenMPClauseName(CKind); 18626 reportOriginalDsa(SemaRef, DSAS, VD, DVar); 18627 continue; 18628 } 18629 18630 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9] 18631 // A list item cannot appear in both a map clause and a data-sharing 18632 // attribute clause on the same construct. 18633 18634 // Check conflicts with other map clause expressions. We check the conflicts 18635 // with the current construct separately from the enclosing data 18636 // environment, because the restrictions are different. We only have to 18637 // check conflicts across regions for the map clauses. 18638 if (checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr, 18639 /*CurrentRegionOnly=*/true, CurComponents, CKind)) 18640 break; 18641 if (CKind == OMPC_map && 18642 (SemaRef.getLangOpts().OpenMP <= 45 || StartLoc.isValid()) && 18643 checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr, 18644 /*CurrentRegionOnly=*/false, CurComponents, CKind)) 18645 break; 18646 18647 // OpenMP 4.5 [2.10.5, target update Construct] 18648 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 18649 // If the type of a list item is a reference to a type T then the type will 18650 // be considered to be T for all purposes of this clause. 18651 auto I = llvm::find_if( 18652 CurComponents, 18653 [](const OMPClauseMappableExprCommon::MappableComponent &MC) { 18654 return MC.getAssociatedDeclaration(); 18655 }); 18656 assert(I != CurComponents.end() && "Null decl on map clause."); 18657 (void)I; 18658 QualType Type; 18659 auto *ASE = dyn_cast<ArraySubscriptExpr>(VE->IgnoreParens()); 18660 auto *OASE = dyn_cast<OMPArraySectionExpr>(VE->IgnoreParens()); 18661 auto *OAShE = dyn_cast<OMPArrayShapingExpr>(VE->IgnoreParens()); 18662 if (ASE) { 18663 Type = ASE->getType().getNonReferenceType(); 18664 } else if (OASE) { 18665 QualType BaseType = 18666 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 18667 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 18668 Type = ATy->getElementType(); 18669 else 18670 Type = BaseType->getPointeeType(); 18671 Type = Type.getNonReferenceType(); 18672 } else if (OAShE) { 18673 Type = OAShE->getBase()->getType()->getPointeeType(); 18674 } else { 18675 Type = VE->getType(); 18676 } 18677 18678 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4] 18679 // A list item in a to or from clause must have a mappable type. 18680 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9] 18681 // A list item must have a mappable type. 18682 if (!checkTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef, 18683 DSAS, Type)) 18684 continue; 18685 18686 if (CKind == OMPC_map) { 18687 // target enter data 18688 // OpenMP [2.10.2, Restrictions, p. 99] 18689 // A map-type must be specified in all map clauses and must be either 18690 // to or alloc. 18691 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective(); 18692 if (DKind == OMPD_target_enter_data && 18693 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) { 18694 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 18695 << (IsMapTypeImplicit ? 1 : 0) 18696 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 18697 << getOpenMPDirectiveName(DKind); 18698 continue; 18699 } 18700 18701 // target exit_data 18702 // OpenMP [2.10.3, Restrictions, p. 102] 18703 // A map-type must be specified in all map clauses and must be either 18704 // from, release, or delete. 18705 if (DKind == OMPD_target_exit_data && 18706 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release || 18707 MapType == OMPC_MAP_delete)) { 18708 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 18709 << (IsMapTypeImplicit ? 1 : 0) 18710 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 18711 << getOpenMPDirectiveName(DKind); 18712 continue; 18713 } 18714 18715 // target, target data 18716 // OpenMP 5.0 [2.12.2, Restrictions, p. 163] 18717 // OpenMP 5.0 [2.12.5, Restrictions, p. 174] 18718 // A map-type in a map clause must be to, from, tofrom or alloc 18719 if ((DKind == OMPD_target_data || 18720 isOpenMPTargetExecutionDirective(DKind)) && 18721 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_from || 18722 MapType == OMPC_MAP_tofrom || MapType == OMPC_MAP_alloc)) { 18723 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 18724 << (IsMapTypeImplicit ? 1 : 0) 18725 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 18726 << getOpenMPDirectiveName(DKind); 18727 continue; 18728 } 18729 18730 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 18731 // A list item cannot appear in both a map clause and a data-sharing 18732 // attribute clause on the same construct 18733 // 18734 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7] 18735 // A list item cannot appear in both a map clause and a data-sharing 18736 // attribute clause on the same construct unless the construct is a 18737 // combined construct. 18738 if (VD && ((SemaRef.LangOpts.OpenMP <= 45 && 18739 isOpenMPTargetExecutionDirective(DKind)) || 18740 DKind == OMPD_target)) { 18741 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false); 18742 if (isOpenMPPrivate(DVar.CKind)) { 18743 SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 18744 << getOpenMPClauseName(DVar.CKind) 18745 << getOpenMPClauseName(OMPC_map) 18746 << getOpenMPDirectiveName(DSAS->getCurrentDirective()); 18747 reportOriginalDsa(SemaRef, DSAS, CurDeclaration, DVar); 18748 continue; 18749 } 18750 } 18751 } 18752 18753 // Try to find the associated user-defined mapper. 18754 ExprResult ER = buildUserDefinedMapperRef( 18755 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId, 18756 Type.getCanonicalType(), UnresolvedMapper); 18757 if (ER.isInvalid()) 18758 continue; 18759 MVLI.UDMapperList.push_back(ER.get()); 18760 18761 // Save the current expression. 18762 MVLI.ProcessedVarList.push_back(RE); 18763 18764 // Store the components in the stack so that they can be used to check 18765 // against other clauses later on. 18766 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents, 18767 /*WhereFoundClauseKind=*/OMPC_map); 18768 18769 // Save the components and declaration to create the clause. For purposes of 18770 // the clause creation, any component list that has has base 'this' uses 18771 // null as base declaration. 18772 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 18773 MVLI.VarComponents.back().append(CurComponents.begin(), 18774 CurComponents.end()); 18775 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr 18776 : CurDeclaration); 18777 } 18778 } 18779 18780 OMPClause *Sema::ActOnOpenMPMapClause( 18781 ArrayRef<OpenMPMapModifierKind> MapTypeModifiers, 18782 ArrayRef<SourceLocation> MapTypeModifiersLoc, 18783 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId, 18784 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, SourceLocation MapLoc, 18785 SourceLocation ColonLoc, ArrayRef<Expr *> VarList, 18786 const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) { 18787 OpenMPMapModifierKind Modifiers[] = { 18788 OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown, 18789 OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown}; 18790 SourceLocation ModifiersLoc[NumberOfOMPMapClauseModifiers]; 18791 18792 // Process map-type-modifiers, flag errors for duplicate modifiers. 18793 unsigned Count = 0; 18794 for (unsigned I = 0, E = MapTypeModifiers.size(); I < E; ++I) { 18795 if (MapTypeModifiers[I] != OMPC_MAP_MODIFIER_unknown && 18796 llvm::find(Modifiers, MapTypeModifiers[I]) != std::end(Modifiers)) { 18797 Diag(MapTypeModifiersLoc[I], diag::err_omp_duplicate_map_type_modifier); 18798 continue; 18799 } 18800 assert(Count < NumberOfOMPMapClauseModifiers && 18801 "Modifiers exceed the allowed number of map type modifiers"); 18802 Modifiers[Count] = MapTypeModifiers[I]; 18803 ModifiersLoc[Count] = MapTypeModifiersLoc[I]; 18804 ++Count; 18805 } 18806 18807 MappableVarListInfo MVLI(VarList); 18808 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, Locs.StartLoc, 18809 MapperIdScopeSpec, MapperId, UnresolvedMappers, 18810 MapType, IsMapTypeImplicit); 18811 18812 // We need to produce a map clause even if we don't have variables so that 18813 // other diagnostics related with non-existing map clauses are accurate. 18814 return OMPMapClause::Create(Context, Locs, MVLI.ProcessedVarList, 18815 MVLI.VarBaseDeclarations, MVLI.VarComponents, 18816 MVLI.UDMapperList, Modifiers, ModifiersLoc, 18817 MapperIdScopeSpec.getWithLocInContext(Context), 18818 MapperId, MapType, IsMapTypeImplicit, MapLoc); 18819 } 18820 18821 QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc, 18822 TypeResult ParsedType) { 18823 assert(ParsedType.isUsable()); 18824 18825 QualType ReductionType = GetTypeFromParser(ParsedType.get()); 18826 if (ReductionType.isNull()) 18827 return QualType(); 18828 18829 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++ 18830 // A type name in a declare reduction directive cannot be a function type, an 18831 // array type, a reference type, or a type qualified with const, volatile or 18832 // restrict. 18833 if (ReductionType.hasQualifiers()) { 18834 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0; 18835 return QualType(); 18836 } 18837 18838 if (ReductionType->isFunctionType()) { 18839 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1; 18840 return QualType(); 18841 } 18842 if (ReductionType->isReferenceType()) { 18843 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2; 18844 return QualType(); 18845 } 18846 if (ReductionType->isArrayType()) { 18847 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3; 18848 return QualType(); 18849 } 18850 return ReductionType; 18851 } 18852 18853 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart( 18854 Scope *S, DeclContext *DC, DeclarationName Name, 18855 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes, 18856 AccessSpecifier AS, Decl *PrevDeclInScope) { 18857 SmallVector<Decl *, 8> Decls; 18858 Decls.reserve(ReductionTypes.size()); 18859 18860 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName, 18861 forRedeclarationInCurContext()); 18862 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions 18863 // A reduction-identifier may not be re-declared in the current scope for the 18864 // same type or for a type that is compatible according to the base language 18865 // rules. 18866 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes; 18867 OMPDeclareReductionDecl *PrevDRD = nullptr; 18868 bool InCompoundScope = true; 18869 if (S != nullptr) { 18870 // Find previous declaration with the same name not referenced in other 18871 // declarations. 18872 FunctionScopeInfo *ParentFn = getEnclosingFunction(); 18873 InCompoundScope = 18874 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty(); 18875 LookupName(Lookup, S); 18876 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false, 18877 /*AllowInlineNamespace=*/false); 18878 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious; 18879 LookupResult::Filter Filter = Lookup.makeFilter(); 18880 while (Filter.hasNext()) { 18881 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next()); 18882 if (InCompoundScope) { 18883 auto I = UsedAsPrevious.find(PrevDecl); 18884 if (I == UsedAsPrevious.end()) 18885 UsedAsPrevious[PrevDecl] = false; 18886 if (OMPDeclareReductionDecl *D = PrevDecl->getPrevDeclInScope()) 18887 UsedAsPrevious[D] = true; 18888 } 18889 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] = 18890 PrevDecl->getLocation(); 18891 } 18892 Filter.done(); 18893 if (InCompoundScope) { 18894 for (const auto &PrevData : UsedAsPrevious) { 18895 if (!PrevData.second) { 18896 PrevDRD = PrevData.first; 18897 break; 18898 } 18899 } 18900 } 18901 } else if (PrevDeclInScope != nullptr) { 18902 auto *PrevDRDInScope = PrevDRD = 18903 cast<OMPDeclareReductionDecl>(PrevDeclInScope); 18904 do { 18905 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] = 18906 PrevDRDInScope->getLocation(); 18907 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope(); 18908 } while (PrevDRDInScope != nullptr); 18909 } 18910 for (const auto &TyData : ReductionTypes) { 18911 const auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType()); 18912 bool Invalid = false; 18913 if (I != PreviousRedeclTypes.end()) { 18914 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition) 18915 << TyData.first; 18916 Diag(I->second, diag::note_previous_definition); 18917 Invalid = true; 18918 } 18919 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second; 18920 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second, 18921 Name, TyData.first, PrevDRD); 18922 DC->addDecl(DRD); 18923 DRD->setAccess(AS); 18924 Decls.push_back(DRD); 18925 if (Invalid) 18926 DRD->setInvalidDecl(); 18927 else 18928 PrevDRD = DRD; 18929 } 18930 18931 return DeclGroupPtrTy::make( 18932 DeclGroupRef::Create(Context, Decls.begin(), Decls.size())); 18933 } 18934 18935 void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) { 18936 auto *DRD = cast<OMPDeclareReductionDecl>(D); 18937 18938 // Enter new function scope. 18939 PushFunctionScope(); 18940 setFunctionHasBranchProtectedScope(); 18941 getCurFunction()->setHasOMPDeclareReductionCombiner(); 18942 18943 if (S != nullptr) 18944 PushDeclContext(S, DRD); 18945 else 18946 CurContext = DRD; 18947 18948 PushExpressionEvaluationContext( 18949 ExpressionEvaluationContext::PotentiallyEvaluated); 18950 18951 QualType ReductionType = DRD->getType(); 18952 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will 18953 // be replaced by '*omp_parm' during codegen. This required because 'omp_in' 18954 // uses semantics of argument handles by value, but it should be passed by 18955 // reference. C lang does not support references, so pass all parameters as 18956 // pointers. 18957 // Create 'T omp_in;' variable. 18958 VarDecl *OmpInParm = 18959 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in"); 18960 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will 18961 // be replaced by '*omp_parm' during codegen. This required because 'omp_out' 18962 // uses semantics of argument handles by value, but it should be passed by 18963 // reference. C lang does not support references, so pass all parameters as 18964 // pointers. 18965 // Create 'T omp_out;' variable. 18966 VarDecl *OmpOutParm = 18967 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out"); 18968 if (S != nullptr) { 18969 PushOnScopeChains(OmpInParm, S); 18970 PushOnScopeChains(OmpOutParm, S); 18971 } else { 18972 DRD->addDecl(OmpInParm); 18973 DRD->addDecl(OmpOutParm); 18974 } 18975 Expr *InE = 18976 ::buildDeclRefExpr(*this, OmpInParm, ReductionType, D->getLocation()); 18977 Expr *OutE = 18978 ::buildDeclRefExpr(*this, OmpOutParm, ReductionType, D->getLocation()); 18979 DRD->setCombinerData(InE, OutE); 18980 } 18981 18982 void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) { 18983 auto *DRD = cast<OMPDeclareReductionDecl>(D); 18984 DiscardCleanupsInEvaluationContext(); 18985 PopExpressionEvaluationContext(); 18986 18987 PopDeclContext(); 18988 PopFunctionScopeInfo(); 18989 18990 if (Combiner != nullptr) 18991 DRD->setCombiner(Combiner); 18992 else 18993 DRD->setInvalidDecl(); 18994 } 18995 18996 VarDecl *Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) { 18997 auto *DRD = cast<OMPDeclareReductionDecl>(D); 18998 18999 // Enter new function scope. 19000 PushFunctionScope(); 19001 setFunctionHasBranchProtectedScope(); 19002 19003 if (S != nullptr) 19004 PushDeclContext(S, DRD); 19005 else 19006 CurContext = DRD; 19007 19008 PushExpressionEvaluationContext( 19009 ExpressionEvaluationContext::PotentiallyEvaluated); 19010 19011 QualType ReductionType = DRD->getType(); 19012 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will 19013 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv' 19014 // uses semantics of argument handles by value, but it should be passed by 19015 // reference. C lang does not support references, so pass all parameters as 19016 // pointers. 19017 // Create 'T omp_priv;' variable. 19018 VarDecl *OmpPrivParm = 19019 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv"); 19020 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will 19021 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig' 19022 // uses semantics of argument handles by value, but it should be passed by 19023 // reference. C lang does not support references, so pass all parameters as 19024 // pointers. 19025 // Create 'T omp_orig;' variable. 19026 VarDecl *OmpOrigParm = 19027 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig"); 19028 if (S != nullptr) { 19029 PushOnScopeChains(OmpPrivParm, S); 19030 PushOnScopeChains(OmpOrigParm, S); 19031 } else { 19032 DRD->addDecl(OmpPrivParm); 19033 DRD->addDecl(OmpOrigParm); 19034 } 19035 Expr *OrigE = 19036 ::buildDeclRefExpr(*this, OmpOrigParm, ReductionType, D->getLocation()); 19037 Expr *PrivE = 19038 ::buildDeclRefExpr(*this, OmpPrivParm, ReductionType, D->getLocation()); 19039 DRD->setInitializerData(OrigE, PrivE); 19040 return OmpPrivParm; 19041 } 19042 19043 void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, Expr *Initializer, 19044 VarDecl *OmpPrivParm) { 19045 auto *DRD = cast<OMPDeclareReductionDecl>(D); 19046 DiscardCleanupsInEvaluationContext(); 19047 PopExpressionEvaluationContext(); 19048 19049 PopDeclContext(); 19050 PopFunctionScopeInfo(); 19051 19052 if (Initializer != nullptr) { 19053 DRD->setInitializer(Initializer, OMPDeclareReductionDecl::CallInit); 19054 } else if (OmpPrivParm->hasInit()) { 19055 DRD->setInitializer(OmpPrivParm->getInit(), 19056 OmpPrivParm->isDirectInit() 19057 ? OMPDeclareReductionDecl::DirectInit 19058 : OMPDeclareReductionDecl::CopyInit); 19059 } else { 19060 DRD->setInvalidDecl(); 19061 } 19062 } 19063 19064 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd( 19065 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) { 19066 for (Decl *D : DeclReductions.get()) { 19067 if (IsValid) { 19068 if (S) 19069 PushOnScopeChains(cast<OMPDeclareReductionDecl>(D), S, 19070 /*AddToContext=*/false); 19071 } else { 19072 D->setInvalidDecl(); 19073 } 19074 } 19075 return DeclReductions; 19076 } 19077 19078 TypeResult Sema::ActOnOpenMPDeclareMapperVarDecl(Scope *S, Declarator &D) { 19079 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); 19080 QualType T = TInfo->getType(); 19081 if (D.isInvalidType()) 19082 return true; 19083 19084 if (getLangOpts().CPlusPlus) { 19085 // Check that there are no default arguments (C++ only). 19086 CheckExtraCXXDefaultArguments(D); 19087 } 19088 19089 return CreateParsedType(T, TInfo); 19090 } 19091 19092 QualType Sema::ActOnOpenMPDeclareMapperType(SourceLocation TyLoc, 19093 TypeResult ParsedType) { 19094 assert(ParsedType.isUsable() && "Expect usable parsed mapper type"); 19095 19096 QualType MapperType = GetTypeFromParser(ParsedType.get()); 19097 assert(!MapperType.isNull() && "Expect valid mapper type"); 19098 19099 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions 19100 // The type must be of struct, union or class type in C and C++ 19101 if (!MapperType->isStructureOrClassType() && !MapperType->isUnionType()) { 19102 Diag(TyLoc, diag::err_omp_mapper_wrong_type); 19103 return QualType(); 19104 } 19105 return MapperType; 19106 } 19107 19108 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareMapperDirective( 19109 Scope *S, DeclContext *DC, DeclarationName Name, QualType MapperType, 19110 SourceLocation StartLoc, DeclarationName VN, AccessSpecifier AS, 19111 Expr *MapperVarRef, ArrayRef<OMPClause *> Clauses, Decl *PrevDeclInScope) { 19112 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPMapperName, 19113 forRedeclarationInCurContext()); 19114 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions 19115 // A mapper-identifier may not be redeclared in the current scope for the 19116 // same type or for a type that is compatible according to the base language 19117 // rules. 19118 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes; 19119 OMPDeclareMapperDecl *PrevDMD = nullptr; 19120 bool InCompoundScope = true; 19121 if (S != nullptr) { 19122 // Find previous declaration with the same name not referenced in other 19123 // declarations. 19124 FunctionScopeInfo *ParentFn = getEnclosingFunction(); 19125 InCompoundScope = 19126 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty(); 19127 LookupName(Lookup, S); 19128 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false, 19129 /*AllowInlineNamespace=*/false); 19130 llvm::DenseMap<OMPDeclareMapperDecl *, bool> UsedAsPrevious; 19131 LookupResult::Filter Filter = Lookup.makeFilter(); 19132 while (Filter.hasNext()) { 19133 auto *PrevDecl = cast<OMPDeclareMapperDecl>(Filter.next()); 19134 if (InCompoundScope) { 19135 auto I = UsedAsPrevious.find(PrevDecl); 19136 if (I == UsedAsPrevious.end()) 19137 UsedAsPrevious[PrevDecl] = false; 19138 if (OMPDeclareMapperDecl *D = PrevDecl->getPrevDeclInScope()) 19139 UsedAsPrevious[D] = true; 19140 } 19141 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] = 19142 PrevDecl->getLocation(); 19143 } 19144 Filter.done(); 19145 if (InCompoundScope) { 19146 for (const auto &PrevData : UsedAsPrevious) { 19147 if (!PrevData.second) { 19148 PrevDMD = PrevData.first; 19149 break; 19150 } 19151 } 19152 } 19153 } else if (PrevDeclInScope) { 19154 auto *PrevDMDInScope = PrevDMD = 19155 cast<OMPDeclareMapperDecl>(PrevDeclInScope); 19156 do { 19157 PreviousRedeclTypes[PrevDMDInScope->getType().getCanonicalType()] = 19158 PrevDMDInScope->getLocation(); 19159 PrevDMDInScope = PrevDMDInScope->getPrevDeclInScope(); 19160 } while (PrevDMDInScope != nullptr); 19161 } 19162 const auto I = PreviousRedeclTypes.find(MapperType.getCanonicalType()); 19163 bool Invalid = false; 19164 if (I != PreviousRedeclTypes.end()) { 19165 Diag(StartLoc, diag::err_omp_declare_mapper_redefinition) 19166 << MapperType << Name; 19167 Diag(I->second, diag::note_previous_definition); 19168 Invalid = true; 19169 } 19170 // Build expressions for implicit maps of data members with 'default' 19171 // mappers. 19172 SmallVector<OMPClause *, 4> ClausesWithImplicit(Clauses.begin(), 19173 Clauses.end()); 19174 if (LangOpts.OpenMP >= 50) 19175 processImplicitMapsWithDefaultMappers(*this, DSAStack, ClausesWithImplicit); 19176 auto *DMD = 19177 OMPDeclareMapperDecl::Create(Context, DC, StartLoc, Name, MapperType, VN, 19178 ClausesWithImplicit, PrevDMD); 19179 if (S) 19180 PushOnScopeChains(DMD, S); 19181 else 19182 DC->addDecl(DMD); 19183 DMD->setAccess(AS); 19184 if (Invalid) 19185 DMD->setInvalidDecl(); 19186 19187 auto *VD = cast<DeclRefExpr>(MapperVarRef)->getDecl(); 19188 VD->setDeclContext(DMD); 19189 VD->setLexicalDeclContext(DMD); 19190 DMD->addDecl(VD); 19191 DMD->setMapperVarRef(MapperVarRef); 19192 19193 return DeclGroupPtrTy::make(DeclGroupRef(DMD)); 19194 } 19195 19196 ExprResult 19197 Sema::ActOnOpenMPDeclareMapperDirectiveVarDecl(Scope *S, QualType MapperType, 19198 SourceLocation StartLoc, 19199 DeclarationName VN) { 19200 TypeSourceInfo *TInfo = 19201 Context.getTrivialTypeSourceInfo(MapperType, StartLoc); 19202 auto *VD = VarDecl::Create(Context, Context.getTranslationUnitDecl(), 19203 StartLoc, StartLoc, VN.getAsIdentifierInfo(), 19204 MapperType, TInfo, SC_None); 19205 if (S) 19206 PushOnScopeChains(VD, S, /*AddToContext=*/false); 19207 Expr *E = buildDeclRefExpr(*this, VD, MapperType, StartLoc); 19208 DSAStack->addDeclareMapperVarRef(E); 19209 return E; 19210 } 19211 19212 bool Sema::isOpenMPDeclareMapperVarDeclAllowed(const VarDecl *VD) const { 19213 assert(LangOpts.OpenMP && "Expected OpenMP mode."); 19214 const Expr *Ref = DSAStack->getDeclareMapperVarRef(); 19215 if (const auto *DRE = cast_or_null<DeclRefExpr>(Ref)) 19216 return VD->getCanonicalDecl() == DRE->getDecl()->getCanonicalDecl(); 19217 return true; 19218 } 19219 19220 const ValueDecl *Sema::getOpenMPDeclareMapperVarName() const { 19221 assert(LangOpts.OpenMP && "Expected OpenMP mode."); 19222 return cast<DeclRefExpr>(DSAStack->getDeclareMapperVarRef())->getDecl(); 19223 } 19224 19225 OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams, 19226 SourceLocation StartLoc, 19227 SourceLocation LParenLoc, 19228 SourceLocation EndLoc) { 19229 Expr *ValExpr = NumTeams; 19230 Stmt *HelperValStmt = nullptr; 19231 19232 // OpenMP [teams Constrcut, Restrictions] 19233 // The num_teams expression must evaluate to a positive integer value. 19234 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams, 19235 /*StrictlyPositive=*/true)) 19236 return nullptr; 19237 19238 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 19239 OpenMPDirectiveKind CaptureRegion = 19240 getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams, LangOpts.OpenMP); 19241 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 19242 ValExpr = MakeFullExpr(ValExpr).get(); 19243 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 19244 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 19245 HelperValStmt = buildPreInits(Context, Captures); 19246 } 19247 19248 return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion, 19249 StartLoc, LParenLoc, EndLoc); 19250 } 19251 19252 OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit, 19253 SourceLocation StartLoc, 19254 SourceLocation LParenLoc, 19255 SourceLocation EndLoc) { 19256 Expr *ValExpr = ThreadLimit; 19257 Stmt *HelperValStmt = nullptr; 19258 19259 // OpenMP [teams Constrcut, Restrictions] 19260 // The thread_limit expression must evaluate to a positive integer value. 19261 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit, 19262 /*StrictlyPositive=*/true)) 19263 return nullptr; 19264 19265 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 19266 OpenMPDirectiveKind CaptureRegion = getOpenMPCaptureRegionForClause( 19267 DKind, OMPC_thread_limit, LangOpts.OpenMP); 19268 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 19269 ValExpr = MakeFullExpr(ValExpr).get(); 19270 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 19271 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 19272 HelperValStmt = buildPreInits(Context, Captures); 19273 } 19274 19275 return new (Context) OMPThreadLimitClause( 19276 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc); 19277 } 19278 19279 OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority, 19280 SourceLocation StartLoc, 19281 SourceLocation LParenLoc, 19282 SourceLocation EndLoc) { 19283 Expr *ValExpr = Priority; 19284 Stmt *HelperValStmt = nullptr; 19285 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 19286 19287 // OpenMP [2.9.1, task Constrcut] 19288 // The priority-value is a non-negative numerical scalar expression. 19289 if (!isNonNegativeIntegerValue( 19290 ValExpr, *this, OMPC_priority, 19291 /*StrictlyPositive=*/false, /*BuildCapture=*/true, 19292 DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt)) 19293 return nullptr; 19294 19295 return new (Context) OMPPriorityClause(ValExpr, HelperValStmt, CaptureRegion, 19296 StartLoc, LParenLoc, EndLoc); 19297 } 19298 19299 OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize, 19300 SourceLocation StartLoc, 19301 SourceLocation LParenLoc, 19302 SourceLocation EndLoc) { 19303 Expr *ValExpr = Grainsize; 19304 Stmt *HelperValStmt = nullptr; 19305 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 19306 19307 // OpenMP [2.9.2, taskloop Constrcut] 19308 // The parameter of the grainsize clause must be a positive integer 19309 // expression. 19310 if (!isNonNegativeIntegerValue( 19311 ValExpr, *this, OMPC_grainsize, 19312 /*StrictlyPositive=*/true, /*BuildCapture=*/true, 19313 DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt)) 19314 return nullptr; 19315 19316 return new (Context) OMPGrainsizeClause(ValExpr, HelperValStmt, CaptureRegion, 19317 StartLoc, LParenLoc, EndLoc); 19318 } 19319 19320 OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks, 19321 SourceLocation StartLoc, 19322 SourceLocation LParenLoc, 19323 SourceLocation EndLoc) { 19324 Expr *ValExpr = NumTasks; 19325 Stmt *HelperValStmt = nullptr; 19326 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 19327 19328 // OpenMP [2.9.2, taskloop Constrcut] 19329 // The parameter of the num_tasks clause must be a positive integer 19330 // expression. 19331 if (!isNonNegativeIntegerValue( 19332 ValExpr, *this, OMPC_num_tasks, 19333 /*StrictlyPositive=*/true, /*BuildCapture=*/true, 19334 DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt)) 19335 return nullptr; 19336 19337 return new (Context) OMPNumTasksClause(ValExpr, HelperValStmt, CaptureRegion, 19338 StartLoc, LParenLoc, EndLoc); 19339 } 19340 19341 OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc, 19342 SourceLocation LParenLoc, 19343 SourceLocation EndLoc) { 19344 // OpenMP [2.13.2, critical construct, Description] 19345 // ... where hint-expression is an integer constant expression that evaluates 19346 // to a valid lock hint. 19347 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint); 19348 if (HintExpr.isInvalid()) 19349 return nullptr; 19350 return new (Context) 19351 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc); 19352 } 19353 19354 /// Tries to find omp_event_handle_t type. 19355 static bool findOMPEventHandleT(Sema &S, SourceLocation Loc, 19356 DSAStackTy *Stack) { 19357 QualType OMPEventHandleT = Stack->getOMPEventHandleT(); 19358 if (!OMPEventHandleT.isNull()) 19359 return true; 19360 IdentifierInfo *II = &S.PP.getIdentifierTable().get("omp_event_handle_t"); 19361 ParsedType PT = S.getTypeName(*II, Loc, S.getCurScope()); 19362 if (!PT.getAsOpaquePtr() || PT.get().isNull()) { 19363 S.Diag(Loc, diag::err_omp_implied_type_not_found) << "omp_event_handle_t"; 19364 return false; 19365 } 19366 Stack->setOMPEventHandleT(PT.get()); 19367 return true; 19368 } 19369 19370 OMPClause *Sema::ActOnOpenMPDetachClause(Expr *Evt, SourceLocation StartLoc, 19371 SourceLocation LParenLoc, 19372 SourceLocation EndLoc) { 19373 if (!Evt->isValueDependent() && !Evt->isTypeDependent() && 19374 !Evt->isInstantiationDependent() && 19375 !Evt->containsUnexpandedParameterPack()) { 19376 if (!findOMPEventHandleT(*this, Evt->getExprLoc(), DSAStack)) 19377 return nullptr; 19378 // OpenMP 5.0, 2.10.1 task Construct. 19379 // event-handle is a variable of the omp_event_handle_t type. 19380 auto *Ref = dyn_cast<DeclRefExpr>(Evt->IgnoreParenImpCasts()); 19381 if (!Ref) { 19382 Diag(Evt->getExprLoc(), diag::err_omp_var_expected) 19383 << "omp_event_handle_t" << 0 << Evt->getSourceRange(); 19384 return nullptr; 19385 } 19386 auto *VD = dyn_cast_or_null<VarDecl>(Ref->getDecl()); 19387 if (!VD) { 19388 Diag(Evt->getExprLoc(), diag::err_omp_var_expected) 19389 << "omp_event_handle_t" << 0 << Evt->getSourceRange(); 19390 return nullptr; 19391 } 19392 if (!Context.hasSameUnqualifiedType(DSAStack->getOMPEventHandleT(), 19393 VD->getType()) || 19394 VD->getType().isConstant(Context)) { 19395 Diag(Evt->getExprLoc(), diag::err_omp_var_expected) 19396 << "omp_event_handle_t" << 1 << VD->getType() 19397 << Evt->getSourceRange(); 19398 return nullptr; 19399 } 19400 // OpenMP 5.0, 2.10.1 task Construct 19401 // [detach clause]... The event-handle will be considered as if it was 19402 // specified on a firstprivate clause. 19403 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, /*FromParent=*/false); 19404 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate && 19405 DVar.RefExpr) { 19406 Diag(Evt->getExprLoc(), diag::err_omp_wrong_dsa) 19407 << getOpenMPClauseName(DVar.CKind) 19408 << getOpenMPClauseName(OMPC_firstprivate); 19409 reportOriginalDsa(*this, DSAStack, VD, DVar); 19410 return nullptr; 19411 } 19412 } 19413 19414 return new (Context) OMPDetachClause(Evt, StartLoc, LParenLoc, EndLoc); 19415 } 19416 19417 OMPClause *Sema::ActOnOpenMPDistScheduleClause( 19418 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, 19419 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc, 19420 SourceLocation EndLoc) { 19421 if (Kind == OMPC_DIST_SCHEDULE_unknown) { 19422 std::string Values; 19423 Values += "'"; 19424 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0); 19425 Values += "'"; 19426 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 19427 << Values << getOpenMPClauseName(OMPC_dist_schedule); 19428 return nullptr; 19429 } 19430 Expr *ValExpr = ChunkSize; 19431 Stmt *HelperValStmt = nullptr; 19432 if (ChunkSize) { 19433 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && 19434 !ChunkSize->isInstantiationDependent() && 19435 !ChunkSize->containsUnexpandedParameterPack()) { 19436 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc(); 19437 ExprResult Val = 19438 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); 19439 if (Val.isInvalid()) 19440 return nullptr; 19441 19442 ValExpr = Val.get(); 19443 19444 // OpenMP [2.7.1, Restrictions] 19445 // chunk_size must be a loop invariant integer expression with a positive 19446 // value. 19447 if (Optional<llvm::APSInt> Result = 19448 ValExpr->getIntegerConstantExpr(Context)) { 19449 if (Result->isSigned() && !Result->isStrictlyPositive()) { 19450 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) 19451 << "dist_schedule" << ChunkSize->getSourceRange(); 19452 return nullptr; 19453 } 19454 } else if (getOpenMPCaptureRegionForClause( 19455 DSAStack->getCurrentDirective(), OMPC_dist_schedule, 19456 LangOpts.OpenMP) != OMPD_unknown && 19457 !CurContext->isDependentContext()) { 19458 ValExpr = MakeFullExpr(ValExpr).get(); 19459 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 19460 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 19461 HelperValStmt = buildPreInits(Context, Captures); 19462 } 19463 } 19464 } 19465 19466 return new (Context) 19467 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, 19468 Kind, ValExpr, HelperValStmt); 19469 } 19470 19471 OMPClause *Sema::ActOnOpenMPDefaultmapClause( 19472 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind, 19473 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc, 19474 SourceLocation KindLoc, SourceLocation EndLoc) { 19475 if (getLangOpts().OpenMP < 50) { 19476 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || 19477 Kind != OMPC_DEFAULTMAP_scalar) { 19478 std::string Value; 19479 SourceLocation Loc; 19480 Value += "'"; 19481 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) { 19482 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap, 19483 OMPC_DEFAULTMAP_MODIFIER_tofrom); 19484 Loc = MLoc; 19485 } else { 19486 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap, 19487 OMPC_DEFAULTMAP_scalar); 19488 Loc = KindLoc; 19489 } 19490 Value += "'"; 19491 Diag(Loc, diag::err_omp_unexpected_clause_value) 19492 << Value << getOpenMPClauseName(OMPC_defaultmap); 19493 return nullptr; 19494 } 19495 } else { 19496 bool isDefaultmapModifier = (M != OMPC_DEFAULTMAP_MODIFIER_unknown); 19497 bool isDefaultmapKind = (Kind != OMPC_DEFAULTMAP_unknown) || 19498 (LangOpts.OpenMP >= 50 && KindLoc.isInvalid()); 19499 if (!isDefaultmapKind || !isDefaultmapModifier) { 19500 StringRef KindValue = "'scalar', 'aggregate', 'pointer'"; 19501 if (LangOpts.OpenMP == 50) { 19502 StringRef ModifierValue = "'alloc', 'from', 'to', 'tofrom', " 19503 "'firstprivate', 'none', 'default'"; 19504 if (!isDefaultmapKind && isDefaultmapModifier) { 19505 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 19506 << KindValue << getOpenMPClauseName(OMPC_defaultmap); 19507 } else if (isDefaultmapKind && !isDefaultmapModifier) { 19508 Diag(MLoc, diag::err_omp_unexpected_clause_value) 19509 << ModifierValue << getOpenMPClauseName(OMPC_defaultmap); 19510 } else { 19511 Diag(MLoc, diag::err_omp_unexpected_clause_value) 19512 << ModifierValue << getOpenMPClauseName(OMPC_defaultmap); 19513 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 19514 << KindValue << getOpenMPClauseName(OMPC_defaultmap); 19515 } 19516 } else { 19517 StringRef ModifierValue = 19518 "'alloc', 'from', 'to', 'tofrom', " 19519 "'firstprivate', 'none', 'default', 'present'"; 19520 if (!isDefaultmapKind && isDefaultmapModifier) { 19521 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 19522 << KindValue << getOpenMPClauseName(OMPC_defaultmap); 19523 } else if (isDefaultmapKind && !isDefaultmapModifier) { 19524 Diag(MLoc, diag::err_omp_unexpected_clause_value) 19525 << ModifierValue << getOpenMPClauseName(OMPC_defaultmap); 19526 } else { 19527 Diag(MLoc, diag::err_omp_unexpected_clause_value) 19528 << ModifierValue << getOpenMPClauseName(OMPC_defaultmap); 19529 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 19530 << KindValue << getOpenMPClauseName(OMPC_defaultmap); 19531 } 19532 } 19533 return nullptr; 19534 } 19535 19536 // OpenMP [5.0, 2.12.5, Restrictions, p. 174] 19537 // At most one defaultmap clause for each category can appear on the 19538 // directive. 19539 if (DSAStack->checkDefaultmapCategory(Kind)) { 19540 Diag(StartLoc, diag::err_omp_one_defaultmap_each_category); 19541 return nullptr; 19542 } 19543 } 19544 if (Kind == OMPC_DEFAULTMAP_unknown) { 19545 // Variable category is not specified - mark all categories. 19546 DSAStack->setDefaultDMAAttr(M, OMPC_DEFAULTMAP_aggregate, StartLoc); 19547 DSAStack->setDefaultDMAAttr(M, OMPC_DEFAULTMAP_scalar, StartLoc); 19548 DSAStack->setDefaultDMAAttr(M, OMPC_DEFAULTMAP_pointer, StartLoc); 19549 } else { 19550 DSAStack->setDefaultDMAAttr(M, Kind, StartLoc); 19551 } 19552 19553 return new (Context) 19554 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M); 19555 } 19556 19557 bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) { 19558 DeclContext *CurLexicalContext = getCurLexicalContext(); 19559 if (!CurLexicalContext->isFileContext() && 19560 !CurLexicalContext->isExternCContext() && 19561 !CurLexicalContext->isExternCXXContext() && 19562 !isa<CXXRecordDecl>(CurLexicalContext) && 19563 !isa<ClassTemplateDecl>(CurLexicalContext) && 19564 !isa<ClassTemplatePartialSpecializationDecl>(CurLexicalContext) && 19565 !isa<ClassTemplateSpecializationDecl>(CurLexicalContext)) { 19566 Diag(Loc, diag::err_omp_region_not_file_context); 19567 return false; 19568 } 19569 DeclareTargetNesting.push_back(Loc); 19570 return true; 19571 } 19572 19573 void Sema::ActOnFinishOpenMPDeclareTargetDirective() { 19574 assert(!DeclareTargetNesting.empty() && 19575 "Unexpected ActOnFinishOpenMPDeclareTargetDirective"); 19576 DeclareTargetNesting.pop_back(); 19577 } 19578 19579 NamedDecl * 19580 Sema::lookupOpenMPDeclareTargetName(Scope *CurScope, CXXScopeSpec &ScopeSpec, 19581 const DeclarationNameInfo &Id, 19582 NamedDeclSetType &SameDirectiveDecls) { 19583 LookupResult Lookup(*this, Id, LookupOrdinaryName); 19584 LookupParsedName(Lookup, CurScope, &ScopeSpec, true); 19585 19586 if (Lookup.isAmbiguous()) 19587 return nullptr; 19588 Lookup.suppressDiagnostics(); 19589 19590 if (!Lookup.isSingleResult()) { 19591 VarOrFuncDeclFilterCCC CCC(*this); 19592 if (TypoCorrection Corrected = 19593 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, CCC, 19594 CTK_ErrorRecovery)) { 19595 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest) 19596 << Id.getName()); 19597 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl()); 19598 return nullptr; 19599 } 19600 19601 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName(); 19602 return nullptr; 19603 } 19604 19605 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>(); 19606 if (!isa<VarDecl>(ND) && !isa<FunctionDecl>(ND) && 19607 !isa<FunctionTemplateDecl>(ND)) { 19608 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName(); 19609 return nullptr; 19610 } 19611 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl()))) 19612 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName(); 19613 return ND; 19614 } 19615 19616 void Sema::ActOnOpenMPDeclareTargetName( 19617 NamedDecl *ND, SourceLocation Loc, OMPDeclareTargetDeclAttr::MapTypeTy MT, 19618 OMPDeclareTargetDeclAttr::DevTypeTy DT) { 19619 assert((isa<VarDecl>(ND) || isa<FunctionDecl>(ND) || 19620 isa<FunctionTemplateDecl>(ND)) && 19621 "Expected variable, function or function template."); 19622 19623 // Diagnose marking after use as it may lead to incorrect diagnosis and 19624 // codegen. 19625 if (LangOpts.OpenMP >= 50 && 19626 (ND->isUsed(/*CheckUsedAttr=*/false) || ND->isReferenced())) 19627 Diag(Loc, diag::warn_omp_declare_target_after_first_use); 19628 19629 auto *VD = cast<ValueDecl>(ND); 19630 Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy = 19631 OMPDeclareTargetDeclAttr::getDeviceType(VD); 19632 Optional<SourceLocation> AttrLoc = OMPDeclareTargetDeclAttr::getLocation(VD); 19633 if (DevTy.hasValue() && *DevTy != DT && 19634 (DeclareTargetNesting.empty() || 19635 *AttrLoc != DeclareTargetNesting.back())) { 19636 Diag(Loc, diag::err_omp_device_type_mismatch) 19637 << OMPDeclareTargetDeclAttr::ConvertDevTypeTyToStr(DT) 19638 << OMPDeclareTargetDeclAttr::ConvertDevTypeTyToStr(*DevTy); 19639 return; 19640 } 19641 Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 19642 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); 19643 if (!Res || (!DeclareTargetNesting.empty() && 19644 *AttrLoc == DeclareTargetNesting.back())) { 19645 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit( 19646 Context, MT, DT, DeclareTargetNesting.size() + 1, 19647 SourceRange(Loc, Loc)); 19648 ND->addAttr(A); 19649 if (ASTMutationListener *ML = Context.getASTMutationListener()) 19650 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A); 19651 checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Loc); 19652 } else if (*Res != MT) { 19653 Diag(Loc, diag::err_omp_declare_target_to_and_link) << ND; 19654 } 19655 } 19656 19657 static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR, 19658 Sema &SemaRef, Decl *D) { 19659 if (!D || !isa<VarDecl>(D)) 19660 return; 19661 auto *VD = cast<VarDecl>(D); 19662 Optional<OMPDeclareTargetDeclAttr::MapTypeTy> MapTy = 19663 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); 19664 if (SemaRef.LangOpts.OpenMP >= 50 && 19665 (SemaRef.getCurLambda(/*IgnoreNonLambdaCapturingScope=*/true) || 19666 SemaRef.getCurBlock() || SemaRef.getCurCapturedRegion()) && 19667 VD->hasGlobalStorage()) { 19668 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> MapTy = 19669 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); 19670 if (!MapTy || *MapTy != OMPDeclareTargetDeclAttr::MT_To) { 19671 // OpenMP 5.0, 2.12.7 declare target Directive, Restrictions 19672 // If a lambda declaration and definition appears between a 19673 // declare target directive and the matching end declare target 19674 // directive, all variables that are captured by the lambda 19675 // expression must also appear in a to clause. 19676 SemaRef.Diag(VD->getLocation(), 19677 diag::err_omp_lambda_capture_in_declare_target_not_to); 19678 SemaRef.Diag(SL, diag::note_var_explicitly_captured_here) 19679 << VD << 0 << SR; 19680 return; 19681 } 19682 } 19683 if (MapTy.hasValue()) 19684 return; 19685 SemaRef.Diag(VD->getLocation(), diag::warn_omp_not_in_target_context); 19686 SemaRef.Diag(SL, diag::note_used_here) << SR; 19687 } 19688 19689 static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR, 19690 Sema &SemaRef, DSAStackTy *Stack, 19691 ValueDecl *VD) { 19692 return OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD) || 19693 checkTypeMappable(SL, SR, SemaRef, Stack, VD->getType(), 19694 /*FullCheck=*/false); 19695 } 19696 19697 void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D, 19698 SourceLocation IdLoc) { 19699 if (!D || D->isInvalidDecl()) 19700 return; 19701 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange(); 19702 SourceLocation SL = E ? E->getBeginLoc() : D->getLocation(); 19703 if (auto *VD = dyn_cast<VarDecl>(D)) { 19704 // Only global variables can be marked as declare target. 19705 if (!VD->isFileVarDecl() && !VD->isStaticLocal() && 19706 !VD->isStaticDataMember()) 19707 return; 19708 // 2.10.6: threadprivate variable cannot appear in a declare target 19709 // directive. 19710 if (DSAStack->isThreadPrivate(VD)) { 19711 Diag(SL, diag::err_omp_threadprivate_in_target); 19712 reportOriginalDsa(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false)); 19713 return; 19714 } 19715 } 19716 if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(D)) 19717 D = FTD->getTemplatedDecl(); 19718 if (auto *FD = dyn_cast<FunctionDecl>(D)) { 19719 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 19720 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(FD); 19721 if (IdLoc.isValid() && Res && *Res == OMPDeclareTargetDeclAttr::MT_Link) { 19722 Diag(IdLoc, diag::err_omp_function_in_link_clause); 19723 Diag(FD->getLocation(), diag::note_defined_here) << FD; 19724 return; 19725 } 19726 } 19727 if (auto *VD = dyn_cast<ValueDecl>(D)) { 19728 // Problem if any with var declared with incomplete type will be reported 19729 // as normal, so no need to check it here. 19730 if ((E || !VD->getType()->isIncompleteType()) && 19731 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) 19732 return; 19733 if (!E && !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) { 19734 // Checking declaration inside declare target region. 19735 if (isa<VarDecl>(D) || isa<FunctionDecl>(D) || 19736 isa<FunctionTemplateDecl>(D)) { 19737 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit( 19738 Context, OMPDeclareTargetDeclAttr::MT_To, 19739 OMPDeclareTargetDeclAttr::DT_Any, DeclareTargetNesting.size(), 19740 SourceRange(DeclareTargetNesting.back(), 19741 DeclareTargetNesting.back())); 19742 D->addAttr(A); 19743 if (ASTMutationListener *ML = Context.getASTMutationListener()) 19744 ML->DeclarationMarkedOpenMPDeclareTarget(D, A); 19745 } 19746 return; 19747 } 19748 } 19749 if (!E) 19750 return; 19751 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D); 19752 } 19753 19754 OMPClause *Sema::ActOnOpenMPToClause( 19755 ArrayRef<OpenMPMotionModifierKind> MotionModifiers, 19756 ArrayRef<SourceLocation> MotionModifiersLoc, 19757 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId, 19758 SourceLocation ColonLoc, ArrayRef<Expr *> VarList, 19759 const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) { 19760 OpenMPMotionModifierKind Modifiers[] = {OMPC_MOTION_MODIFIER_unknown, 19761 OMPC_MOTION_MODIFIER_unknown}; 19762 SourceLocation ModifiersLoc[NumberOfOMPMotionModifiers]; 19763 19764 // Process motion-modifiers, flag errors for duplicate modifiers. 19765 unsigned Count = 0; 19766 for (unsigned I = 0, E = MotionModifiers.size(); I < E; ++I) { 19767 if (MotionModifiers[I] != OMPC_MOTION_MODIFIER_unknown && 19768 llvm::find(Modifiers, MotionModifiers[I]) != std::end(Modifiers)) { 19769 Diag(MotionModifiersLoc[I], diag::err_omp_duplicate_motion_modifier); 19770 continue; 19771 } 19772 assert(Count < NumberOfOMPMotionModifiers && 19773 "Modifiers exceed the allowed number of motion modifiers"); 19774 Modifiers[Count] = MotionModifiers[I]; 19775 ModifiersLoc[Count] = MotionModifiersLoc[I]; 19776 ++Count; 19777 } 19778 19779 MappableVarListInfo MVLI(VarList); 19780 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, Locs.StartLoc, 19781 MapperIdScopeSpec, MapperId, UnresolvedMappers); 19782 if (MVLI.ProcessedVarList.empty()) 19783 return nullptr; 19784 19785 return OMPToClause::Create( 19786 Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, 19787 MVLI.VarComponents, MVLI.UDMapperList, Modifiers, ModifiersLoc, 19788 MapperIdScopeSpec.getWithLocInContext(Context), MapperId); 19789 } 19790 19791 OMPClause *Sema::ActOnOpenMPFromClause( 19792 ArrayRef<OpenMPMotionModifierKind> MotionModifiers, 19793 ArrayRef<SourceLocation> MotionModifiersLoc, 19794 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId, 19795 SourceLocation ColonLoc, ArrayRef<Expr *> VarList, 19796 const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) { 19797 OpenMPMotionModifierKind Modifiers[] = {OMPC_MOTION_MODIFIER_unknown, 19798 OMPC_MOTION_MODIFIER_unknown}; 19799 SourceLocation ModifiersLoc[NumberOfOMPMotionModifiers]; 19800 19801 // Process motion-modifiers, flag errors for duplicate modifiers. 19802 unsigned Count = 0; 19803 for (unsigned I = 0, E = MotionModifiers.size(); I < E; ++I) { 19804 if (MotionModifiers[I] != OMPC_MOTION_MODIFIER_unknown && 19805 llvm::find(Modifiers, MotionModifiers[I]) != std::end(Modifiers)) { 19806 Diag(MotionModifiersLoc[I], diag::err_omp_duplicate_motion_modifier); 19807 continue; 19808 } 19809 assert(Count < NumberOfOMPMotionModifiers && 19810 "Modifiers exceed the allowed number of motion modifiers"); 19811 Modifiers[Count] = MotionModifiers[I]; 19812 ModifiersLoc[Count] = MotionModifiersLoc[I]; 19813 ++Count; 19814 } 19815 19816 MappableVarListInfo MVLI(VarList); 19817 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, Locs.StartLoc, 19818 MapperIdScopeSpec, MapperId, UnresolvedMappers); 19819 if (MVLI.ProcessedVarList.empty()) 19820 return nullptr; 19821 19822 return OMPFromClause::Create( 19823 Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, 19824 MVLI.VarComponents, MVLI.UDMapperList, Modifiers, ModifiersLoc, 19825 MapperIdScopeSpec.getWithLocInContext(Context), MapperId); 19826 } 19827 19828 OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList, 19829 const OMPVarListLocTy &Locs) { 19830 MappableVarListInfo MVLI(VarList); 19831 SmallVector<Expr *, 8> PrivateCopies; 19832 SmallVector<Expr *, 8> Inits; 19833 19834 for (Expr *RefExpr : VarList) { 19835 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause."); 19836 SourceLocation ELoc; 19837 SourceRange ERange; 19838 Expr *SimpleRefExpr = RefExpr; 19839 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 19840 if (Res.second) { 19841 // It will be analyzed later. 19842 MVLI.ProcessedVarList.push_back(RefExpr); 19843 PrivateCopies.push_back(nullptr); 19844 Inits.push_back(nullptr); 19845 } 19846 ValueDecl *D = Res.first; 19847 if (!D) 19848 continue; 19849 19850 QualType Type = D->getType(); 19851 Type = Type.getNonReferenceType().getUnqualifiedType(); 19852 19853 auto *VD = dyn_cast<VarDecl>(D); 19854 19855 // Item should be a pointer or reference to pointer. 19856 if (!Type->isPointerType()) { 19857 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer) 19858 << 0 << RefExpr->getSourceRange(); 19859 continue; 19860 } 19861 19862 // Build the private variable and the expression that refers to it. 19863 auto VDPrivate = 19864 buildVarDecl(*this, ELoc, Type, D->getName(), 19865 D->hasAttrs() ? &D->getAttrs() : nullptr, 19866 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 19867 if (VDPrivate->isInvalidDecl()) 19868 continue; 19869 19870 CurContext->addDecl(VDPrivate); 19871 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 19872 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc); 19873 19874 // Add temporary variable to initialize the private copy of the pointer. 19875 VarDecl *VDInit = 19876 buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp"); 19877 DeclRefExpr *VDInitRefExpr = buildDeclRefExpr( 19878 *this, VDInit, RefExpr->getType(), RefExpr->getExprLoc()); 19879 AddInitializerToDecl(VDPrivate, 19880 DefaultLvalueConversion(VDInitRefExpr).get(), 19881 /*DirectInit=*/false); 19882 19883 // If required, build a capture to implement the privatization initialized 19884 // with the current list item value. 19885 DeclRefExpr *Ref = nullptr; 19886 if (!VD) 19887 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 19888 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref); 19889 PrivateCopies.push_back(VDPrivateRefExpr); 19890 Inits.push_back(VDInitRefExpr); 19891 19892 // We need to add a data sharing attribute for this variable to make sure it 19893 // is correctly captured. A variable that shows up in a use_device_ptr has 19894 // similar properties of a first private variable. 19895 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 19896 19897 // Create a mappable component for the list item. List items in this clause 19898 // only need a component. 19899 MVLI.VarBaseDeclarations.push_back(D); 19900 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 19901 MVLI.VarComponents.back().emplace_back(SimpleRefExpr, D, 19902 /*IsNonContiguous=*/false); 19903 } 19904 19905 if (MVLI.ProcessedVarList.empty()) 19906 return nullptr; 19907 19908 return OMPUseDevicePtrClause::Create( 19909 Context, Locs, MVLI.ProcessedVarList, PrivateCopies, Inits, 19910 MVLI.VarBaseDeclarations, MVLI.VarComponents); 19911 } 19912 19913 OMPClause *Sema::ActOnOpenMPUseDeviceAddrClause(ArrayRef<Expr *> VarList, 19914 const OMPVarListLocTy &Locs) { 19915 MappableVarListInfo MVLI(VarList); 19916 19917 for (Expr *RefExpr : VarList) { 19918 assert(RefExpr && "NULL expr in OpenMP use_device_addr clause."); 19919 SourceLocation ELoc; 19920 SourceRange ERange; 19921 Expr *SimpleRefExpr = RefExpr; 19922 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, 19923 /*AllowArraySection=*/true); 19924 if (Res.second) { 19925 // It will be analyzed later. 19926 MVLI.ProcessedVarList.push_back(RefExpr); 19927 } 19928 ValueDecl *D = Res.first; 19929 if (!D) 19930 continue; 19931 auto *VD = dyn_cast<VarDecl>(D); 19932 19933 // If required, build a capture to implement the privatization initialized 19934 // with the current list item value. 19935 DeclRefExpr *Ref = nullptr; 19936 if (!VD) 19937 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 19938 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref); 19939 19940 // We need to add a data sharing attribute for this variable to make sure it 19941 // is correctly captured. A variable that shows up in a use_device_addr has 19942 // similar properties of a first private variable. 19943 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 19944 19945 // Create a mappable component for the list item. List items in this clause 19946 // only need a component. 19947 MVLI.VarBaseDeclarations.push_back(D); 19948 MVLI.VarComponents.emplace_back(); 19949 Expr *Component = SimpleRefExpr; 19950 if (VD && (isa<OMPArraySectionExpr>(RefExpr->IgnoreParenImpCasts()) || 19951 isa<ArraySubscriptExpr>(RefExpr->IgnoreParenImpCasts()))) 19952 Component = DefaultFunctionArrayLvalueConversion(SimpleRefExpr).get(); 19953 MVLI.VarComponents.back().emplace_back(Component, D, 19954 /*IsNonContiguous=*/false); 19955 } 19956 19957 if (MVLI.ProcessedVarList.empty()) 19958 return nullptr; 19959 19960 return OMPUseDeviceAddrClause::Create(Context, Locs, MVLI.ProcessedVarList, 19961 MVLI.VarBaseDeclarations, 19962 MVLI.VarComponents); 19963 } 19964 19965 OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList, 19966 const OMPVarListLocTy &Locs) { 19967 MappableVarListInfo MVLI(VarList); 19968 for (Expr *RefExpr : VarList) { 19969 assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause."); 19970 SourceLocation ELoc; 19971 SourceRange ERange; 19972 Expr *SimpleRefExpr = RefExpr; 19973 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 19974 if (Res.second) { 19975 // It will be analyzed later. 19976 MVLI.ProcessedVarList.push_back(RefExpr); 19977 } 19978 ValueDecl *D = Res.first; 19979 if (!D) 19980 continue; 19981 19982 QualType Type = D->getType(); 19983 // item should be a pointer or array or reference to pointer or array 19984 if (!Type.getNonReferenceType()->isPointerType() && 19985 !Type.getNonReferenceType()->isArrayType()) { 19986 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr) 19987 << 0 << RefExpr->getSourceRange(); 19988 continue; 19989 } 19990 19991 // Check if the declaration in the clause does not show up in any data 19992 // sharing attribute. 19993 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 19994 if (isOpenMPPrivate(DVar.CKind)) { 19995 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 19996 << getOpenMPClauseName(DVar.CKind) 19997 << getOpenMPClauseName(OMPC_is_device_ptr) 19998 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 19999 reportOriginalDsa(*this, DSAStack, D, DVar); 20000 continue; 20001 } 20002 20003 const Expr *ConflictExpr; 20004 if (DSAStack->checkMappableExprComponentListsForDecl( 20005 D, /*CurrentRegionOnly=*/true, 20006 [&ConflictExpr]( 20007 OMPClauseMappableExprCommon::MappableExprComponentListRef R, 20008 OpenMPClauseKind) -> bool { 20009 ConflictExpr = R.front().getAssociatedExpression(); 20010 return true; 20011 })) { 20012 Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange(); 20013 Diag(ConflictExpr->getExprLoc(), diag::note_used_here) 20014 << ConflictExpr->getSourceRange(); 20015 continue; 20016 } 20017 20018 // Store the components in the stack so that they can be used to check 20019 // against other clauses later on. 20020 OMPClauseMappableExprCommon::MappableComponent MC( 20021 SimpleRefExpr, D, /*IsNonContiguous=*/false); 20022 DSAStack->addMappableExpressionComponents( 20023 D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr); 20024 20025 // Record the expression we've just processed. 20026 MVLI.ProcessedVarList.push_back(SimpleRefExpr); 20027 20028 // Create a mappable component for the list item. List items in this clause 20029 // only need a component. We use a null declaration to signal fields in 20030 // 'this'. 20031 assert((isa<DeclRefExpr>(SimpleRefExpr) || 20032 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) && 20033 "Unexpected device pointer expression!"); 20034 MVLI.VarBaseDeclarations.push_back( 20035 isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr); 20036 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 20037 MVLI.VarComponents.back().push_back(MC); 20038 } 20039 20040 if (MVLI.ProcessedVarList.empty()) 20041 return nullptr; 20042 20043 return OMPIsDevicePtrClause::Create(Context, Locs, MVLI.ProcessedVarList, 20044 MVLI.VarBaseDeclarations, 20045 MVLI.VarComponents); 20046 } 20047 20048 OMPClause *Sema::ActOnOpenMPAllocateClause( 20049 Expr *Allocator, ArrayRef<Expr *> VarList, SourceLocation StartLoc, 20050 SourceLocation ColonLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 20051 if (Allocator) { 20052 // OpenMP [2.11.4 allocate Clause, Description] 20053 // allocator is an expression of omp_allocator_handle_t type. 20054 if (!findOMPAllocatorHandleT(*this, Allocator->getExprLoc(), DSAStack)) 20055 return nullptr; 20056 20057 ExprResult AllocatorRes = DefaultLvalueConversion(Allocator); 20058 if (AllocatorRes.isInvalid()) 20059 return nullptr; 20060 AllocatorRes = PerformImplicitConversion(AllocatorRes.get(), 20061 DSAStack->getOMPAllocatorHandleT(), 20062 Sema::AA_Initializing, 20063 /*AllowExplicit=*/true); 20064 if (AllocatorRes.isInvalid()) 20065 return nullptr; 20066 Allocator = AllocatorRes.get(); 20067 } else { 20068 // OpenMP 5.0, 2.11.4 allocate Clause, Restrictions. 20069 // allocate clauses that appear on a target construct or on constructs in a 20070 // target region must specify an allocator expression unless a requires 20071 // directive with the dynamic_allocators clause is present in the same 20072 // compilation unit. 20073 if (LangOpts.OpenMPIsDevice && 20074 !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>()) 20075 targetDiag(StartLoc, diag::err_expected_allocator_expression); 20076 } 20077 // Analyze and build list of variables. 20078 SmallVector<Expr *, 8> Vars; 20079 for (Expr *RefExpr : VarList) { 20080 assert(RefExpr && "NULL expr in OpenMP private clause."); 20081 SourceLocation ELoc; 20082 SourceRange ERange; 20083 Expr *SimpleRefExpr = RefExpr; 20084 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 20085 if (Res.second) { 20086 // It will be analyzed later. 20087 Vars.push_back(RefExpr); 20088 } 20089 ValueDecl *D = Res.first; 20090 if (!D) 20091 continue; 20092 20093 auto *VD = dyn_cast<VarDecl>(D); 20094 DeclRefExpr *Ref = nullptr; 20095 if (!VD && !CurContext->isDependentContext()) 20096 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 20097 Vars.push_back((VD || CurContext->isDependentContext()) 20098 ? RefExpr->IgnoreParens() 20099 : Ref); 20100 } 20101 20102 if (Vars.empty()) 20103 return nullptr; 20104 20105 if (Allocator) 20106 DSAStack->addInnerAllocatorExpr(Allocator); 20107 return OMPAllocateClause::Create(Context, StartLoc, LParenLoc, Allocator, 20108 ColonLoc, EndLoc, Vars); 20109 } 20110 20111 OMPClause *Sema::ActOnOpenMPNontemporalClause(ArrayRef<Expr *> VarList, 20112 SourceLocation StartLoc, 20113 SourceLocation LParenLoc, 20114 SourceLocation EndLoc) { 20115 SmallVector<Expr *, 8> Vars; 20116 for (Expr *RefExpr : VarList) { 20117 assert(RefExpr && "NULL expr in OpenMP nontemporal clause."); 20118 SourceLocation ELoc; 20119 SourceRange ERange; 20120 Expr *SimpleRefExpr = RefExpr; 20121 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 20122 if (Res.second) 20123 // It will be analyzed later. 20124 Vars.push_back(RefExpr); 20125 ValueDecl *D = Res.first; 20126 if (!D) 20127 continue; 20128 20129 // OpenMP 5.0, 2.9.3.1 simd Construct, Restrictions. 20130 // A list-item cannot appear in more than one nontemporal clause. 20131 if (const Expr *PrevRef = 20132 DSAStack->addUniqueNontemporal(D, SimpleRefExpr)) { 20133 Diag(ELoc, diag::err_omp_used_in_clause_twice) 20134 << 0 << getOpenMPClauseName(OMPC_nontemporal) << ERange; 20135 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa) 20136 << getOpenMPClauseName(OMPC_nontemporal); 20137 continue; 20138 } 20139 20140 Vars.push_back(RefExpr); 20141 } 20142 20143 if (Vars.empty()) 20144 return nullptr; 20145 20146 return OMPNontemporalClause::Create(Context, StartLoc, LParenLoc, EndLoc, 20147 Vars); 20148 } 20149 20150 OMPClause *Sema::ActOnOpenMPInclusiveClause(ArrayRef<Expr *> VarList, 20151 SourceLocation StartLoc, 20152 SourceLocation LParenLoc, 20153 SourceLocation EndLoc) { 20154 SmallVector<Expr *, 8> Vars; 20155 for (Expr *RefExpr : VarList) { 20156 assert(RefExpr && "NULL expr in OpenMP nontemporal clause."); 20157 SourceLocation ELoc; 20158 SourceRange ERange; 20159 Expr *SimpleRefExpr = RefExpr; 20160 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, 20161 /*AllowArraySection=*/true); 20162 if (Res.second) 20163 // It will be analyzed later. 20164 Vars.push_back(RefExpr); 20165 ValueDecl *D = Res.first; 20166 if (!D) 20167 continue; 20168 20169 const DSAStackTy::DSAVarData DVar = 20170 DSAStack->getTopDSA(D, /*FromParent=*/true); 20171 // OpenMP 5.0, 2.9.6, scan Directive, Restrictions. 20172 // A list item that appears in the inclusive or exclusive clause must appear 20173 // in a reduction clause with the inscan modifier on the enclosing 20174 // worksharing-loop, worksharing-loop SIMD, or simd construct. 20175 if (DVar.CKind != OMPC_reduction || 20176 DVar.Modifier != OMPC_REDUCTION_inscan) 20177 Diag(ELoc, diag::err_omp_inclusive_exclusive_not_reduction) 20178 << RefExpr->getSourceRange(); 20179 20180 if (DSAStack->getParentDirective() != OMPD_unknown) 20181 DSAStack->markDeclAsUsedInScanDirective(D); 20182 Vars.push_back(RefExpr); 20183 } 20184 20185 if (Vars.empty()) 20186 return nullptr; 20187 20188 return OMPInclusiveClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 20189 } 20190 20191 OMPClause *Sema::ActOnOpenMPExclusiveClause(ArrayRef<Expr *> VarList, 20192 SourceLocation StartLoc, 20193 SourceLocation LParenLoc, 20194 SourceLocation EndLoc) { 20195 SmallVector<Expr *, 8> Vars; 20196 for (Expr *RefExpr : VarList) { 20197 assert(RefExpr && "NULL expr in OpenMP nontemporal clause."); 20198 SourceLocation ELoc; 20199 SourceRange ERange; 20200 Expr *SimpleRefExpr = RefExpr; 20201 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, 20202 /*AllowArraySection=*/true); 20203 if (Res.second) 20204 // It will be analyzed later. 20205 Vars.push_back(RefExpr); 20206 ValueDecl *D = Res.first; 20207 if (!D) 20208 continue; 20209 20210 OpenMPDirectiveKind ParentDirective = DSAStack->getParentDirective(); 20211 DSAStackTy::DSAVarData DVar; 20212 if (ParentDirective != OMPD_unknown) 20213 DVar = DSAStack->getTopDSA(D, /*FromParent=*/true); 20214 // OpenMP 5.0, 2.9.6, scan Directive, Restrictions. 20215 // A list item that appears in the inclusive or exclusive clause must appear 20216 // in a reduction clause with the inscan modifier on the enclosing 20217 // worksharing-loop, worksharing-loop SIMD, or simd construct. 20218 if (ParentDirective == OMPD_unknown || DVar.CKind != OMPC_reduction || 20219 DVar.Modifier != OMPC_REDUCTION_inscan) { 20220 Diag(ELoc, diag::err_omp_inclusive_exclusive_not_reduction) 20221 << RefExpr->getSourceRange(); 20222 } else { 20223 DSAStack->markDeclAsUsedInScanDirective(D); 20224 } 20225 Vars.push_back(RefExpr); 20226 } 20227 20228 if (Vars.empty()) 20229 return nullptr; 20230 20231 return OMPExclusiveClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 20232 } 20233 20234 /// Tries to find omp_alloctrait_t type. 20235 static bool findOMPAlloctraitT(Sema &S, SourceLocation Loc, DSAStackTy *Stack) { 20236 QualType OMPAlloctraitT = Stack->getOMPAlloctraitT(); 20237 if (!OMPAlloctraitT.isNull()) 20238 return true; 20239 IdentifierInfo &II = S.PP.getIdentifierTable().get("omp_alloctrait_t"); 20240 ParsedType PT = S.getTypeName(II, Loc, S.getCurScope()); 20241 if (!PT.getAsOpaquePtr() || PT.get().isNull()) { 20242 S.Diag(Loc, diag::err_omp_implied_type_not_found) << "omp_alloctrait_t"; 20243 return false; 20244 } 20245 Stack->setOMPAlloctraitT(PT.get()); 20246 return true; 20247 } 20248 20249 OMPClause *Sema::ActOnOpenMPUsesAllocatorClause( 20250 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc, 20251 ArrayRef<UsesAllocatorsData> Data) { 20252 // OpenMP [2.12.5, target Construct] 20253 // allocator is an identifier of omp_allocator_handle_t type. 20254 if (!findOMPAllocatorHandleT(*this, StartLoc, DSAStack)) 20255 return nullptr; 20256 // OpenMP [2.12.5, target Construct] 20257 // allocator-traits-array is an identifier of const omp_alloctrait_t * type. 20258 if (llvm::any_of( 20259 Data, 20260 [](const UsesAllocatorsData &D) { return D.AllocatorTraits; }) && 20261 !findOMPAlloctraitT(*this, StartLoc, DSAStack)) 20262 return nullptr; 20263 llvm::SmallPtrSet<CanonicalDeclPtr<Decl>, 4> PredefinedAllocators; 20264 for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) { 20265 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I); 20266 StringRef Allocator = 20267 OMPAllocateDeclAttr::ConvertAllocatorTypeTyToStr(AllocatorKind); 20268 DeclarationName AllocatorName = &Context.Idents.get(Allocator); 20269 PredefinedAllocators.insert(LookupSingleName( 20270 TUScope, AllocatorName, StartLoc, Sema::LookupAnyName)); 20271 } 20272 20273 SmallVector<OMPUsesAllocatorsClause::Data, 4> NewData; 20274 for (const UsesAllocatorsData &D : Data) { 20275 Expr *AllocatorExpr = nullptr; 20276 // Check allocator expression. 20277 if (D.Allocator->isTypeDependent()) { 20278 AllocatorExpr = D.Allocator; 20279 } else { 20280 // Traits were specified - need to assign new allocator to the specified 20281 // allocator, so it must be an lvalue. 20282 AllocatorExpr = D.Allocator->IgnoreParenImpCasts(); 20283 auto *DRE = dyn_cast<DeclRefExpr>(AllocatorExpr); 20284 bool IsPredefinedAllocator = false; 20285 if (DRE) 20286 IsPredefinedAllocator = PredefinedAllocators.count(DRE->getDecl()); 20287 if (!DRE || 20288 !(Context.hasSameUnqualifiedType( 20289 AllocatorExpr->getType(), DSAStack->getOMPAllocatorHandleT()) || 20290 Context.typesAreCompatible(AllocatorExpr->getType(), 20291 DSAStack->getOMPAllocatorHandleT(), 20292 /*CompareUnqualified=*/true)) || 20293 (!IsPredefinedAllocator && 20294 (AllocatorExpr->getType().isConstant(Context) || 20295 !AllocatorExpr->isLValue()))) { 20296 Diag(D.Allocator->getExprLoc(), diag::err_omp_var_expected) 20297 << "omp_allocator_handle_t" << (DRE ? 1 : 0) 20298 << AllocatorExpr->getType() << D.Allocator->getSourceRange(); 20299 continue; 20300 } 20301 // OpenMP [2.12.5, target Construct] 20302 // Predefined allocators appearing in a uses_allocators clause cannot have 20303 // traits specified. 20304 if (IsPredefinedAllocator && D.AllocatorTraits) { 20305 Diag(D.AllocatorTraits->getExprLoc(), 20306 diag::err_omp_predefined_allocator_with_traits) 20307 << D.AllocatorTraits->getSourceRange(); 20308 Diag(D.Allocator->getExprLoc(), diag::note_omp_predefined_allocator) 20309 << cast<NamedDecl>(DRE->getDecl())->getName() 20310 << D.Allocator->getSourceRange(); 20311 continue; 20312 } 20313 // OpenMP [2.12.5, target Construct] 20314 // Non-predefined allocators appearing in a uses_allocators clause must 20315 // have traits specified. 20316 if (!IsPredefinedAllocator && !D.AllocatorTraits) { 20317 Diag(D.Allocator->getExprLoc(), 20318 diag::err_omp_nonpredefined_allocator_without_traits); 20319 continue; 20320 } 20321 // No allocator traits - just convert it to rvalue. 20322 if (!D.AllocatorTraits) 20323 AllocatorExpr = DefaultLvalueConversion(AllocatorExpr).get(); 20324 DSAStack->addUsesAllocatorsDecl( 20325 DRE->getDecl(), 20326 IsPredefinedAllocator 20327 ? DSAStackTy::UsesAllocatorsDeclKind::PredefinedAllocator 20328 : DSAStackTy::UsesAllocatorsDeclKind::UserDefinedAllocator); 20329 } 20330 Expr *AllocatorTraitsExpr = nullptr; 20331 if (D.AllocatorTraits) { 20332 if (D.AllocatorTraits->isTypeDependent()) { 20333 AllocatorTraitsExpr = D.AllocatorTraits; 20334 } else { 20335 // OpenMP [2.12.5, target Construct] 20336 // Arrays that contain allocator traits that appear in a uses_allocators 20337 // clause must be constant arrays, have constant values and be defined 20338 // in the same scope as the construct in which the clause appears. 20339 AllocatorTraitsExpr = D.AllocatorTraits->IgnoreParenImpCasts(); 20340 // Check that traits expr is a constant array. 20341 QualType TraitTy; 20342 if (const ArrayType *Ty = 20343 AllocatorTraitsExpr->getType()->getAsArrayTypeUnsafe()) 20344 if (const auto *ConstArrayTy = dyn_cast<ConstantArrayType>(Ty)) 20345 TraitTy = ConstArrayTy->getElementType(); 20346 if (TraitTy.isNull() || 20347 !(Context.hasSameUnqualifiedType(TraitTy, 20348 DSAStack->getOMPAlloctraitT()) || 20349 Context.typesAreCompatible(TraitTy, DSAStack->getOMPAlloctraitT(), 20350 /*CompareUnqualified=*/true))) { 20351 Diag(D.AllocatorTraits->getExprLoc(), 20352 diag::err_omp_expected_array_alloctraits) 20353 << AllocatorTraitsExpr->getType(); 20354 continue; 20355 } 20356 // Do not map by default allocator traits if it is a standalone 20357 // variable. 20358 if (auto *DRE = dyn_cast<DeclRefExpr>(AllocatorTraitsExpr)) 20359 DSAStack->addUsesAllocatorsDecl( 20360 DRE->getDecl(), 20361 DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait); 20362 } 20363 } 20364 OMPUsesAllocatorsClause::Data &NewD = NewData.emplace_back(); 20365 NewD.Allocator = AllocatorExpr; 20366 NewD.AllocatorTraits = AllocatorTraitsExpr; 20367 NewD.LParenLoc = D.LParenLoc; 20368 NewD.RParenLoc = D.RParenLoc; 20369 } 20370 return OMPUsesAllocatorsClause::Create(Context, StartLoc, LParenLoc, EndLoc, 20371 NewData); 20372 } 20373 20374 OMPClause *Sema::ActOnOpenMPAffinityClause( 20375 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc, 20376 SourceLocation EndLoc, Expr *Modifier, ArrayRef<Expr *> Locators) { 20377 SmallVector<Expr *, 8> Vars; 20378 for (Expr *RefExpr : Locators) { 20379 assert(RefExpr && "NULL expr in OpenMP shared clause."); 20380 if (isa<DependentScopeDeclRefExpr>(RefExpr) || RefExpr->isTypeDependent()) { 20381 // It will be analyzed later. 20382 Vars.push_back(RefExpr); 20383 continue; 20384 } 20385 20386 SourceLocation ELoc = RefExpr->getExprLoc(); 20387 Expr *SimpleExpr = RefExpr->IgnoreParenImpCasts(); 20388 20389 if (!SimpleExpr->isLValue()) { 20390 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 20391 << 1 << 0 << RefExpr->getSourceRange(); 20392 continue; 20393 } 20394 20395 ExprResult Res; 20396 { 20397 Sema::TentativeAnalysisScope Trap(*this); 20398 Res = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, SimpleExpr); 20399 } 20400 if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr) && 20401 !isa<OMPArrayShapingExpr>(SimpleExpr)) { 20402 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 20403 << 1 << 0 << RefExpr->getSourceRange(); 20404 continue; 20405 } 20406 Vars.push_back(SimpleExpr); 20407 } 20408 20409 return OMPAffinityClause::Create(Context, StartLoc, LParenLoc, ColonLoc, 20410 EndLoc, Modifier, Vars); 20411 } 20412