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/OMPAssume.h" 40 #include "llvm/Frontend/OpenMP/OMPConstants.h" 41 #include <set> 42 43 using namespace clang; 44 using namespace llvm::omp; 45 46 //===----------------------------------------------------------------------===// 47 // Stack of data-sharing attributes for variables 48 //===----------------------------------------------------------------------===// 49 50 static const Expr *checkMapClauseExpressionBase( 51 Sema &SemaRef, Expr *E, 52 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents, 53 OpenMPClauseKind CKind, OpenMPDirectiveKind DKind, bool NoDiagnose); 54 55 namespace { 56 /// Default data sharing attributes, which can be applied to directive. 57 enum DefaultDataSharingAttributes { 58 DSA_unspecified = 0, /// Data sharing attribute not specified. 59 DSA_none = 1 << 0, /// Default data sharing attribute 'none'. 60 DSA_shared = 1 << 1, /// Default data sharing attribute 'shared'. 61 DSA_firstprivate = 1 << 2, /// Default data sharing attribute 'firstprivate'. 62 }; 63 64 /// Stack for tracking declarations used in OpenMP directives and 65 /// clauses and their data-sharing attributes. 66 class DSAStackTy { 67 public: 68 struct DSAVarData { 69 OpenMPDirectiveKind DKind = OMPD_unknown; 70 OpenMPClauseKind CKind = OMPC_unknown; 71 unsigned Modifier = 0; 72 const Expr *RefExpr = nullptr; 73 DeclRefExpr *PrivateCopy = nullptr; 74 SourceLocation ImplicitDSALoc; 75 bool AppliedToPointee = false; 76 DSAVarData() = default; 77 DSAVarData(OpenMPDirectiveKind DKind, OpenMPClauseKind CKind, 78 const Expr *RefExpr, DeclRefExpr *PrivateCopy, 79 SourceLocation ImplicitDSALoc, unsigned Modifier, 80 bool AppliedToPointee) 81 : DKind(DKind), CKind(CKind), Modifier(Modifier), RefExpr(RefExpr), 82 PrivateCopy(PrivateCopy), ImplicitDSALoc(ImplicitDSALoc), 83 AppliedToPointee(AppliedToPointee) {} 84 }; 85 using OperatorOffsetTy = 86 llvm::SmallVector<std::pair<Expr *, OverloadedOperatorKind>, 4>; 87 using DoacrossDependMapTy = 88 llvm::DenseMap<OMPDependClause *, OperatorOffsetTy>; 89 /// Kind of the declaration used in the uses_allocators clauses. 90 enum class UsesAllocatorsDeclKind { 91 /// Predefined allocator 92 PredefinedAllocator, 93 /// User-defined allocator 94 UserDefinedAllocator, 95 /// The declaration that represent allocator trait 96 AllocatorTrait, 97 }; 98 99 private: 100 struct DSAInfo { 101 OpenMPClauseKind Attributes = OMPC_unknown; 102 unsigned Modifier = 0; 103 /// Pointer to a reference expression and a flag which shows that the 104 /// variable is marked as lastprivate(true) or not (false). 105 llvm::PointerIntPair<const Expr *, 1, bool> RefExpr; 106 DeclRefExpr *PrivateCopy = nullptr; 107 /// true if the attribute is applied to the pointee, not the variable 108 /// itself. 109 bool AppliedToPointee = false; 110 }; 111 using DeclSAMapTy = llvm::SmallDenseMap<const ValueDecl *, DSAInfo, 8>; 112 using UsedRefMapTy = llvm::SmallDenseMap<const ValueDecl *, const Expr *, 8>; 113 using LCDeclInfo = std::pair<unsigned, VarDecl *>; 114 using LoopControlVariablesMapTy = 115 llvm::SmallDenseMap<const ValueDecl *, LCDeclInfo, 8>; 116 /// Struct that associates a component with the clause kind where they are 117 /// found. 118 struct MappedExprComponentTy { 119 OMPClauseMappableExprCommon::MappableExprComponentLists Components; 120 OpenMPClauseKind Kind = OMPC_unknown; 121 }; 122 using MappedExprComponentsTy = 123 llvm::DenseMap<const ValueDecl *, MappedExprComponentTy>; 124 using CriticalsWithHintsTy = 125 llvm::StringMap<std::pair<const OMPCriticalDirective *, llvm::APSInt>>; 126 struct ReductionData { 127 using BOKPtrType = llvm::PointerEmbeddedInt<BinaryOperatorKind, 16>; 128 SourceRange ReductionRange; 129 llvm::PointerUnion<const Expr *, BOKPtrType> ReductionOp; 130 ReductionData() = default; 131 void set(BinaryOperatorKind BO, SourceRange RR) { 132 ReductionRange = RR; 133 ReductionOp = BO; 134 } 135 void set(const Expr *RefExpr, SourceRange RR) { 136 ReductionRange = RR; 137 ReductionOp = RefExpr; 138 } 139 }; 140 using DeclReductionMapTy = 141 llvm::SmallDenseMap<const ValueDecl *, ReductionData, 4>; 142 struct DefaultmapInfo { 143 OpenMPDefaultmapClauseModifier ImplicitBehavior = 144 OMPC_DEFAULTMAP_MODIFIER_unknown; 145 SourceLocation SLoc; 146 DefaultmapInfo() = default; 147 DefaultmapInfo(OpenMPDefaultmapClauseModifier M, SourceLocation Loc) 148 : ImplicitBehavior(M), SLoc(Loc) {} 149 }; 150 151 struct SharingMapTy { 152 DeclSAMapTy SharingMap; 153 DeclReductionMapTy ReductionMap; 154 UsedRefMapTy AlignedMap; 155 UsedRefMapTy NontemporalMap; 156 MappedExprComponentsTy MappedExprComponents; 157 LoopControlVariablesMapTy LCVMap; 158 DefaultDataSharingAttributes DefaultAttr = DSA_unspecified; 159 SourceLocation DefaultAttrLoc; 160 DefaultmapInfo DefaultmapMap[OMPC_DEFAULTMAP_unknown]; 161 OpenMPDirectiveKind Directive = OMPD_unknown; 162 DeclarationNameInfo DirectiveName; 163 Scope *CurScope = nullptr; 164 DeclContext *Context = nullptr; 165 SourceLocation ConstructLoc; 166 /// Set of 'depend' clauses with 'sink|source' dependence kind. Required to 167 /// get the data (loop counters etc.) about enclosing loop-based construct. 168 /// This data is required during codegen. 169 DoacrossDependMapTy DoacrossDepends; 170 /// First argument (Expr *) contains optional argument of the 171 /// 'ordered' clause, the second one is true if the regions has 'ordered' 172 /// clause, false otherwise. 173 llvm::Optional<std::pair<const Expr *, OMPOrderedClause *>> OrderedRegion; 174 unsigned AssociatedLoops = 1; 175 bool HasMutipleLoops = false; 176 const Decl *PossiblyLoopCounter = nullptr; 177 bool NowaitRegion = false; 178 bool CancelRegion = false; 179 bool LoopStart = false; 180 bool BodyComplete = false; 181 SourceLocation PrevScanLocation; 182 SourceLocation PrevOrderedLocation; 183 SourceLocation InnerTeamsRegionLoc; 184 /// Reference to the taskgroup task_reduction reference expression. 185 Expr *TaskgroupReductionRef = nullptr; 186 llvm::DenseSet<QualType> MappedClassesQualTypes; 187 SmallVector<Expr *, 4> InnerUsedAllocators; 188 llvm::DenseSet<CanonicalDeclPtr<Decl>> ImplicitTaskFirstprivates; 189 /// List of globals marked as declare target link in this target region 190 /// (isOpenMPTargetExecutionDirective(Directive) == true). 191 llvm::SmallVector<DeclRefExpr *, 4> DeclareTargetLinkVarDecls; 192 /// List of decls used in inclusive/exclusive clauses of the scan directive. 193 llvm::DenseSet<CanonicalDeclPtr<Decl>> UsedInScanDirective; 194 llvm::DenseMap<CanonicalDeclPtr<const Decl>, UsesAllocatorsDeclKind> 195 UsesAllocatorsDecls; 196 Expr *DeclareMapperVar = nullptr; 197 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name, 198 Scope *CurScope, SourceLocation Loc) 199 : Directive(DKind), DirectiveName(Name), CurScope(CurScope), 200 ConstructLoc(Loc) {} 201 SharingMapTy() = default; 202 }; 203 204 using StackTy = SmallVector<SharingMapTy, 4>; 205 206 /// Stack of used declaration and their data-sharing attributes. 207 DeclSAMapTy Threadprivates; 208 const FunctionScopeInfo *CurrentNonCapturingFunctionScope = nullptr; 209 SmallVector<std::pair<StackTy, const FunctionScopeInfo *>, 4> Stack; 210 /// true, if check for DSA must be from parent directive, false, if 211 /// from current directive. 212 OpenMPClauseKind ClauseKindMode = OMPC_unknown; 213 Sema &SemaRef; 214 bool ForceCapturing = false; 215 /// true if all the variables in the target executable directives must be 216 /// captured by reference. 217 bool ForceCaptureByReferenceInTargetExecutable = false; 218 CriticalsWithHintsTy Criticals; 219 unsigned IgnoredStackElements = 0; 220 221 /// Iterators over the stack iterate in order from innermost to outermost 222 /// directive. 223 using const_iterator = StackTy::const_reverse_iterator; 224 const_iterator begin() const { 225 return Stack.empty() ? const_iterator() 226 : Stack.back().first.rbegin() + IgnoredStackElements; 227 } 228 const_iterator end() const { 229 return Stack.empty() ? const_iterator() : Stack.back().first.rend(); 230 } 231 using iterator = StackTy::reverse_iterator; 232 iterator begin() { 233 return Stack.empty() ? iterator() 234 : Stack.back().first.rbegin() + IgnoredStackElements; 235 } 236 iterator end() { 237 return Stack.empty() ? iterator() : Stack.back().first.rend(); 238 } 239 240 // Convenience operations to get at the elements of the stack. 241 242 bool isStackEmpty() const { 243 return Stack.empty() || 244 Stack.back().second != CurrentNonCapturingFunctionScope || 245 Stack.back().first.size() <= IgnoredStackElements; 246 } 247 size_t getStackSize() const { 248 return isStackEmpty() ? 0 249 : Stack.back().first.size() - IgnoredStackElements; 250 } 251 252 SharingMapTy *getTopOfStackOrNull() { 253 size_t Size = getStackSize(); 254 if (Size == 0) 255 return nullptr; 256 return &Stack.back().first[Size - 1]; 257 } 258 const SharingMapTy *getTopOfStackOrNull() const { 259 return const_cast<DSAStackTy &>(*this).getTopOfStackOrNull(); 260 } 261 SharingMapTy &getTopOfStack() { 262 assert(!isStackEmpty() && "no current directive"); 263 return *getTopOfStackOrNull(); 264 } 265 const SharingMapTy &getTopOfStack() const { 266 return const_cast<DSAStackTy &>(*this).getTopOfStack(); 267 } 268 269 SharingMapTy *getSecondOnStackOrNull() { 270 size_t Size = getStackSize(); 271 if (Size <= 1) 272 return nullptr; 273 return &Stack.back().first[Size - 2]; 274 } 275 const SharingMapTy *getSecondOnStackOrNull() const { 276 return const_cast<DSAStackTy &>(*this).getSecondOnStackOrNull(); 277 } 278 279 /// Get the stack element at a certain level (previously returned by 280 /// \c getNestingLevel). 281 /// 282 /// Note that nesting levels count from outermost to innermost, and this is 283 /// the reverse of our iteration order where new inner levels are pushed at 284 /// the front of the stack. 285 SharingMapTy &getStackElemAtLevel(unsigned Level) { 286 assert(Level < getStackSize() && "no such stack element"); 287 return Stack.back().first[Level]; 288 } 289 const SharingMapTy &getStackElemAtLevel(unsigned Level) const { 290 return const_cast<DSAStackTy &>(*this).getStackElemAtLevel(Level); 291 } 292 293 DSAVarData getDSA(const_iterator &Iter, ValueDecl *D) const; 294 295 /// Checks if the variable is a local for OpenMP region. 296 bool isOpenMPLocal(VarDecl *D, const_iterator Iter) const; 297 298 /// Vector of previously declared requires directives 299 SmallVector<const OMPRequiresDecl *, 2> RequiresDecls; 300 /// omp_allocator_handle_t type. 301 QualType OMPAllocatorHandleT; 302 /// omp_depend_t type. 303 QualType OMPDependT; 304 /// omp_event_handle_t type. 305 QualType OMPEventHandleT; 306 /// omp_alloctrait_t type. 307 QualType OMPAlloctraitT; 308 /// Expression for the predefined allocators. 309 Expr *OMPPredefinedAllocators[OMPAllocateDeclAttr::OMPUserDefinedMemAlloc] = { 310 nullptr}; 311 /// Vector of previously encountered target directives 312 SmallVector<SourceLocation, 2> TargetLocations; 313 SourceLocation AtomicLocation; 314 /// Vector of declare variant construct traits. 315 SmallVector<llvm::omp::TraitProperty, 8> ConstructTraits; 316 317 public: 318 explicit DSAStackTy(Sema &S) : SemaRef(S) {} 319 320 /// Sets omp_allocator_handle_t type. 321 void setOMPAllocatorHandleT(QualType Ty) { OMPAllocatorHandleT = Ty; } 322 /// Gets omp_allocator_handle_t type. 323 QualType getOMPAllocatorHandleT() const { return OMPAllocatorHandleT; } 324 /// Sets omp_alloctrait_t type. 325 void setOMPAlloctraitT(QualType Ty) { OMPAlloctraitT = Ty; } 326 /// Gets omp_alloctrait_t type. 327 QualType getOMPAlloctraitT() const { return OMPAlloctraitT; } 328 /// Sets the given default allocator. 329 void setAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, 330 Expr *Allocator) { 331 OMPPredefinedAllocators[AllocatorKind] = Allocator; 332 } 333 /// Returns the specified default allocator. 334 Expr *getAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind) const { 335 return OMPPredefinedAllocators[AllocatorKind]; 336 } 337 /// Sets omp_depend_t type. 338 void setOMPDependT(QualType Ty) { OMPDependT = Ty; } 339 /// Gets omp_depend_t type. 340 QualType getOMPDependT() const { return OMPDependT; } 341 342 /// Sets omp_event_handle_t type. 343 void setOMPEventHandleT(QualType Ty) { OMPEventHandleT = Ty; } 344 /// Gets omp_event_handle_t type. 345 QualType getOMPEventHandleT() const { return OMPEventHandleT; } 346 347 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; } 348 OpenMPClauseKind getClauseParsingMode() const { 349 assert(isClauseParsingMode() && "Must be in clause parsing mode."); 350 return ClauseKindMode; 351 } 352 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; } 353 354 bool isBodyComplete() const { 355 const SharingMapTy *Top = getTopOfStackOrNull(); 356 return Top && Top->BodyComplete; 357 } 358 void setBodyComplete() { getTopOfStack().BodyComplete = true; } 359 360 bool isForceVarCapturing() const { return ForceCapturing; } 361 void setForceVarCapturing(bool V) { ForceCapturing = V; } 362 363 void setForceCaptureByReferenceInTargetExecutable(bool V) { 364 ForceCaptureByReferenceInTargetExecutable = V; 365 } 366 bool isForceCaptureByReferenceInTargetExecutable() const { 367 return ForceCaptureByReferenceInTargetExecutable; 368 } 369 370 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName, 371 Scope *CurScope, SourceLocation Loc) { 372 assert(!IgnoredStackElements && 373 "cannot change stack while ignoring elements"); 374 if (Stack.empty() || 375 Stack.back().second != CurrentNonCapturingFunctionScope) 376 Stack.emplace_back(StackTy(), CurrentNonCapturingFunctionScope); 377 Stack.back().first.emplace_back(DKind, DirName, CurScope, Loc); 378 Stack.back().first.back().DefaultAttrLoc = Loc; 379 } 380 381 void pop() { 382 assert(!IgnoredStackElements && 383 "cannot change stack while ignoring elements"); 384 assert(!Stack.back().first.empty() && 385 "Data-sharing attributes stack is empty!"); 386 Stack.back().first.pop_back(); 387 } 388 389 /// RAII object to temporarily leave the scope of a directive when we want to 390 /// logically operate in its parent. 391 class ParentDirectiveScope { 392 DSAStackTy &Self; 393 bool Active; 394 395 public: 396 ParentDirectiveScope(DSAStackTy &Self, bool Activate) 397 : Self(Self), Active(false) { 398 if (Activate) 399 enable(); 400 } 401 ~ParentDirectiveScope() { disable(); } 402 void disable() { 403 if (Active) { 404 --Self.IgnoredStackElements; 405 Active = false; 406 } 407 } 408 void enable() { 409 if (!Active) { 410 ++Self.IgnoredStackElements; 411 Active = true; 412 } 413 } 414 }; 415 416 /// Marks that we're started loop parsing. 417 void loopInit() { 418 assert(isOpenMPLoopDirective(getCurrentDirective()) && 419 "Expected loop-based directive."); 420 getTopOfStack().LoopStart = true; 421 } 422 /// Start capturing of the variables in the loop context. 423 void loopStart() { 424 assert(isOpenMPLoopDirective(getCurrentDirective()) && 425 "Expected loop-based directive."); 426 getTopOfStack().LoopStart = false; 427 } 428 /// true, if variables are captured, false otherwise. 429 bool isLoopStarted() const { 430 assert(isOpenMPLoopDirective(getCurrentDirective()) && 431 "Expected loop-based directive."); 432 return !getTopOfStack().LoopStart; 433 } 434 /// Marks (or clears) declaration as possibly loop counter. 435 void resetPossibleLoopCounter(const Decl *D = nullptr) { 436 getTopOfStack().PossiblyLoopCounter = D ? D->getCanonicalDecl() : D; 437 } 438 /// Gets the possible loop counter decl. 439 const Decl *getPossiblyLoopCunter() const { 440 return getTopOfStack().PossiblyLoopCounter; 441 } 442 /// Start new OpenMP region stack in new non-capturing function. 443 void pushFunction() { 444 assert(!IgnoredStackElements && 445 "cannot change stack while ignoring elements"); 446 const FunctionScopeInfo *CurFnScope = SemaRef.getCurFunction(); 447 assert(!isa<CapturingScopeInfo>(CurFnScope)); 448 CurrentNonCapturingFunctionScope = CurFnScope; 449 } 450 /// Pop region stack for non-capturing function. 451 void popFunction(const FunctionScopeInfo *OldFSI) { 452 assert(!IgnoredStackElements && 453 "cannot change stack while ignoring elements"); 454 if (!Stack.empty() && Stack.back().second == OldFSI) { 455 assert(Stack.back().first.empty()); 456 Stack.pop_back(); 457 } 458 CurrentNonCapturingFunctionScope = nullptr; 459 for (const FunctionScopeInfo *FSI : llvm::reverse(SemaRef.FunctionScopes)) { 460 if (!isa<CapturingScopeInfo>(FSI)) { 461 CurrentNonCapturingFunctionScope = FSI; 462 break; 463 } 464 } 465 } 466 467 void addCriticalWithHint(const OMPCriticalDirective *D, llvm::APSInt Hint) { 468 Criticals.try_emplace(D->getDirectiveName().getAsString(), D, Hint); 469 } 470 const std::pair<const OMPCriticalDirective *, llvm::APSInt> 471 getCriticalWithHint(const DeclarationNameInfo &Name) const { 472 auto I = Criticals.find(Name.getAsString()); 473 if (I != Criticals.end()) 474 return I->second; 475 return std::make_pair(nullptr, llvm::APSInt()); 476 } 477 /// If 'aligned' declaration for given variable \a D was not seen yet, 478 /// add it and return NULL; otherwise return previous occurrence's expression 479 /// for diagnostics. 480 const Expr *addUniqueAligned(const ValueDecl *D, const Expr *NewDE); 481 /// If 'nontemporal' declaration for given variable \a D was not seen yet, 482 /// add it and return NULL; otherwise return previous occurrence's expression 483 /// for diagnostics. 484 const Expr *addUniqueNontemporal(const ValueDecl *D, const Expr *NewDE); 485 486 /// Register specified variable as loop control variable. 487 void addLoopControlVariable(const ValueDecl *D, VarDecl *Capture); 488 /// Check if the specified variable is a loop control variable for 489 /// current region. 490 /// \return The index of the loop control variable in the list of associated 491 /// for-loops (from outer to inner). 492 const LCDeclInfo isLoopControlVariable(const ValueDecl *D) const; 493 /// Check if the specified variable is a loop control variable for 494 /// parent region. 495 /// \return The index of the loop control variable in the list of associated 496 /// for-loops (from outer to inner). 497 const LCDeclInfo isParentLoopControlVariable(const ValueDecl *D) const; 498 /// Check if the specified variable is a loop control variable for 499 /// current region. 500 /// \return The index of the loop control variable in the list of associated 501 /// for-loops (from outer to inner). 502 const LCDeclInfo isLoopControlVariable(const ValueDecl *D, 503 unsigned Level) const; 504 /// Get the loop control variable for the I-th loop (or nullptr) in 505 /// parent directive. 506 const ValueDecl *getParentLoopControlVariable(unsigned I) const; 507 508 /// Marks the specified decl \p D as used in scan directive. 509 void markDeclAsUsedInScanDirective(ValueDecl *D) { 510 if (SharingMapTy *Stack = getSecondOnStackOrNull()) 511 Stack->UsedInScanDirective.insert(D); 512 } 513 514 /// Checks if the specified declaration was used in the inner scan directive. 515 bool isUsedInScanDirective(ValueDecl *D) const { 516 if (const SharingMapTy *Stack = getTopOfStackOrNull()) 517 return Stack->UsedInScanDirective.contains(D); 518 return false; 519 } 520 521 /// Adds explicit data sharing attribute to the specified declaration. 522 void addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A, 523 DeclRefExpr *PrivateCopy = nullptr, unsigned Modifier = 0, 524 bool AppliedToPointee = false); 525 526 /// Adds additional information for the reduction items with the reduction id 527 /// represented as an operator. 528 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 529 BinaryOperatorKind BOK); 530 /// Adds additional information for the reduction items with the reduction id 531 /// represented as reduction identifier. 532 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 533 const Expr *ReductionRef); 534 /// Returns the location and reduction operation from the innermost parent 535 /// region for the given \p D. 536 const DSAVarData 537 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR, 538 BinaryOperatorKind &BOK, 539 Expr *&TaskgroupDescriptor) const; 540 /// Returns the location and reduction operation from the innermost parent 541 /// region for the given \p D. 542 const DSAVarData 543 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR, 544 const Expr *&ReductionRef, 545 Expr *&TaskgroupDescriptor) const; 546 /// Return reduction reference expression for the current taskgroup or 547 /// parallel/worksharing directives with task reductions. 548 Expr *getTaskgroupReductionRef() const { 549 assert((getTopOfStack().Directive == OMPD_taskgroup || 550 ((isOpenMPParallelDirective(getTopOfStack().Directive) || 551 isOpenMPWorksharingDirective(getTopOfStack().Directive)) && 552 !isOpenMPSimdDirective(getTopOfStack().Directive))) && 553 "taskgroup reference expression requested for non taskgroup or " 554 "parallel/worksharing directive."); 555 return getTopOfStack().TaskgroupReductionRef; 556 } 557 /// Checks if the given \p VD declaration is actually a taskgroup reduction 558 /// descriptor variable at the \p Level of OpenMP regions. 559 bool isTaskgroupReductionRef(const ValueDecl *VD, unsigned Level) const { 560 return getStackElemAtLevel(Level).TaskgroupReductionRef && 561 cast<DeclRefExpr>(getStackElemAtLevel(Level).TaskgroupReductionRef) 562 ->getDecl() == VD; 563 } 564 565 /// Returns data sharing attributes from top of the stack for the 566 /// specified declaration. 567 const DSAVarData getTopDSA(ValueDecl *D, bool FromParent); 568 /// Returns data-sharing attributes for the specified declaration. 569 const DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent) const; 570 /// Returns data-sharing attributes for the specified declaration. 571 const DSAVarData getImplicitDSA(ValueDecl *D, unsigned Level) const; 572 /// Checks if the specified variables has data-sharing attributes which 573 /// match specified \a CPred predicate in any directive which matches \a DPred 574 /// predicate. 575 const DSAVarData 576 hasDSA(ValueDecl *D, 577 const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred, 578 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 579 bool FromParent) const; 580 /// Checks if the specified variables has data-sharing attributes which 581 /// match specified \a CPred predicate in any innermost directive which 582 /// matches \a DPred predicate. 583 const DSAVarData 584 hasInnermostDSA(ValueDecl *D, 585 const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred, 586 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 587 bool FromParent) const; 588 /// Checks if the specified variables has explicit data-sharing 589 /// attributes which match specified \a CPred predicate at the specified 590 /// OpenMP region. 591 bool 592 hasExplicitDSA(const ValueDecl *D, 593 const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred, 594 unsigned Level, bool NotLastprivate = false) const; 595 596 /// Returns true if the directive at level \Level matches in the 597 /// specified \a DPred predicate. 598 bool hasExplicitDirective( 599 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 600 unsigned Level) const; 601 602 /// Finds a directive which matches specified \a DPred predicate. 603 bool hasDirective( 604 const llvm::function_ref<bool( 605 OpenMPDirectiveKind, const DeclarationNameInfo &, SourceLocation)> 606 DPred, 607 bool FromParent) const; 608 609 /// Returns currently analyzed directive. 610 OpenMPDirectiveKind getCurrentDirective() const { 611 const SharingMapTy *Top = getTopOfStackOrNull(); 612 return Top ? Top->Directive : OMPD_unknown; 613 } 614 /// Returns directive kind at specified level. 615 OpenMPDirectiveKind getDirective(unsigned Level) const { 616 assert(!isStackEmpty() && "No directive at specified level."); 617 return getStackElemAtLevel(Level).Directive; 618 } 619 /// Returns the capture region at the specified level. 620 OpenMPDirectiveKind getCaptureRegion(unsigned Level, 621 unsigned OpenMPCaptureLevel) const { 622 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 623 getOpenMPCaptureRegions(CaptureRegions, getDirective(Level)); 624 return CaptureRegions[OpenMPCaptureLevel]; 625 } 626 /// Returns parent directive. 627 OpenMPDirectiveKind getParentDirective() const { 628 const SharingMapTy *Parent = getSecondOnStackOrNull(); 629 return Parent ? Parent->Directive : OMPD_unknown; 630 } 631 632 /// Add requires decl to internal vector 633 void addRequiresDecl(OMPRequiresDecl *RD) { RequiresDecls.push_back(RD); } 634 635 /// Checks if the defined 'requires' directive has specified type of clause. 636 template <typename ClauseType> bool hasRequiresDeclWithClause() const { 637 return llvm::any_of(RequiresDecls, [](const OMPRequiresDecl *D) { 638 return llvm::any_of(D->clauselists(), [](const OMPClause *C) { 639 return isa<ClauseType>(C); 640 }); 641 }); 642 } 643 644 /// Checks for a duplicate clause amongst previously declared requires 645 /// directives 646 bool hasDuplicateRequiresClause(ArrayRef<OMPClause *> ClauseList) const { 647 bool IsDuplicate = false; 648 for (OMPClause *CNew : ClauseList) { 649 for (const OMPRequiresDecl *D : RequiresDecls) { 650 for (const OMPClause *CPrev : D->clauselists()) { 651 if (CNew->getClauseKind() == CPrev->getClauseKind()) { 652 SemaRef.Diag(CNew->getBeginLoc(), 653 diag::err_omp_requires_clause_redeclaration) 654 << getOpenMPClauseName(CNew->getClauseKind()); 655 SemaRef.Diag(CPrev->getBeginLoc(), 656 diag::note_omp_requires_previous_clause) 657 << getOpenMPClauseName(CPrev->getClauseKind()); 658 IsDuplicate = true; 659 } 660 } 661 } 662 } 663 return IsDuplicate; 664 } 665 666 /// Add location of previously encountered target to internal vector 667 void addTargetDirLocation(SourceLocation LocStart) { 668 TargetLocations.push_back(LocStart); 669 } 670 671 /// Add location for the first encountered atomicc directive. 672 void addAtomicDirectiveLoc(SourceLocation Loc) { 673 if (AtomicLocation.isInvalid()) 674 AtomicLocation = Loc; 675 } 676 677 /// Returns the location of the first encountered atomic directive in the 678 /// module. 679 SourceLocation getAtomicDirectiveLoc() const { return AtomicLocation; } 680 681 // Return previously encountered target region locations. 682 ArrayRef<SourceLocation> getEncounteredTargetLocs() const { 683 return TargetLocations; 684 } 685 686 /// Set default data sharing attribute to none. 687 void setDefaultDSANone(SourceLocation Loc) { 688 getTopOfStack().DefaultAttr = DSA_none; 689 getTopOfStack().DefaultAttrLoc = Loc; 690 } 691 /// Set default data sharing attribute to shared. 692 void setDefaultDSAShared(SourceLocation Loc) { 693 getTopOfStack().DefaultAttr = DSA_shared; 694 getTopOfStack().DefaultAttrLoc = Loc; 695 } 696 /// Set default data sharing attribute to firstprivate. 697 void setDefaultDSAFirstPrivate(SourceLocation Loc) { 698 getTopOfStack().DefaultAttr = DSA_firstprivate; 699 getTopOfStack().DefaultAttrLoc = Loc; 700 } 701 /// Set default data mapping attribute to Modifier:Kind 702 void setDefaultDMAAttr(OpenMPDefaultmapClauseModifier M, 703 OpenMPDefaultmapClauseKind Kind, SourceLocation Loc) { 704 DefaultmapInfo &DMI = getTopOfStack().DefaultmapMap[Kind]; 705 DMI.ImplicitBehavior = M; 706 DMI.SLoc = Loc; 707 } 708 /// Check whether the implicit-behavior has been set in defaultmap 709 bool checkDefaultmapCategory(OpenMPDefaultmapClauseKind VariableCategory) { 710 if (VariableCategory == OMPC_DEFAULTMAP_unknown) 711 return getTopOfStack() 712 .DefaultmapMap[OMPC_DEFAULTMAP_aggregate] 713 .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown || 714 getTopOfStack() 715 .DefaultmapMap[OMPC_DEFAULTMAP_scalar] 716 .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown || 717 getTopOfStack() 718 .DefaultmapMap[OMPC_DEFAULTMAP_pointer] 719 .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown; 720 return getTopOfStack().DefaultmapMap[VariableCategory].ImplicitBehavior != 721 OMPC_DEFAULTMAP_MODIFIER_unknown; 722 } 723 724 ArrayRef<llvm::omp::TraitProperty> getConstructTraits() { 725 return ConstructTraits; 726 } 727 void handleConstructTrait(ArrayRef<llvm::omp::TraitProperty> Traits, 728 bool ScopeEntry) { 729 if (ScopeEntry) 730 ConstructTraits.append(Traits.begin(), Traits.end()); 731 else 732 for (llvm::omp::TraitProperty Trait : llvm::reverse(Traits)) { 733 llvm::omp::TraitProperty Top = ConstructTraits.pop_back_val(); 734 assert(Top == Trait && "Something left a trait on the stack!"); 735 (void)Trait; 736 (void)Top; 737 } 738 } 739 740 DefaultDataSharingAttributes getDefaultDSA(unsigned Level) const { 741 return getStackSize() <= Level ? DSA_unspecified 742 : getStackElemAtLevel(Level).DefaultAttr; 743 } 744 DefaultDataSharingAttributes getDefaultDSA() const { 745 return isStackEmpty() ? DSA_unspecified : getTopOfStack().DefaultAttr; 746 } 747 SourceLocation getDefaultDSALocation() const { 748 return isStackEmpty() ? SourceLocation() : getTopOfStack().DefaultAttrLoc; 749 } 750 OpenMPDefaultmapClauseModifier 751 getDefaultmapModifier(OpenMPDefaultmapClauseKind Kind) const { 752 return isStackEmpty() 753 ? OMPC_DEFAULTMAP_MODIFIER_unknown 754 : getTopOfStack().DefaultmapMap[Kind].ImplicitBehavior; 755 } 756 OpenMPDefaultmapClauseModifier 757 getDefaultmapModifierAtLevel(unsigned Level, 758 OpenMPDefaultmapClauseKind Kind) const { 759 return getStackElemAtLevel(Level).DefaultmapMap[Kind].ImplicitBehavior; 760 } 761 bool isDefaultmapCapturedByRef(unsigned Level, 762 OpenMPDefaultmapClauseKind Kind) const { 763 OpenMPDefaultmapClauseModifier M = 764 getDefaultmapModifierAtLevel(Level, Kind); 765 if (Kind == OMPC_DEFAULTMAP_scalar || Kind == OMPC_DEFAULTMAP_pointer) { 766 return (M == OMPC_DEFAULTMAP_MODIFIER_alloc) || 767 (M == OMPC_DEFAULTMAP_MODIFIER_to) || 768 (M == OMPC_DEFAULTMAP_MODIFIER_from) || 769 (M == OMPC_DEFAULTMAP_MODIFIER_tofrom); 770 } 771 return true; 772 } 773 static bool mustBeFirstprivateBase(OpenMPDefaultmapClauseModifier M, 774 OpenMPDefaultmapClauseKind Kind) { 775 switch (Kind) { 776 case OMPC_DEFAULTMAP_scalar: 777 case OMPC_DEFAULTMAP_pointer: 778 return (M == OMPC_DEFAULTMAP_MODIFIER_unknown) || 779 (M == OMPC_DEFAULTMAP_MODIFIER_firstprivate) || 780 (M == OMPC_DEFAULTMAP_MODIFIER_default); 781 case OMPC_DEFAULTMAP_aggregate: 782 return M == OMPC_DEFAULTMAP_MODIFIER_firstprivate; 783 default: 784 break; 785 } 786 llvm_unreachable("Unexpected OpenMPDefaultmapClauseKind enum"); 787 } 788 bool mustBeFirstprivateAtLevel(unsigned Level, 789 OpenMPDefaultmapClauseKind Kind) const { 790 OpenMPDefaultmapClauseModifier M = 791 getDefaultmapModifierAtLevel(Level, Kind); 792 return mustBeFirstprivateBase(M, Kind); 793 } 794 bool mustBeFirstprivate(OpenMPDefaultmapClauseKind Kind) const { 795 OpenMPDefaultmapClauseModifier M = getDefaultmapModifier(Kind); 796 return mustBeFirstprivateBase(M, Kind); 797 } 798 799 /// Checks if the specified variable is a threadprivate. 800 bool isThreadPrivate(VarDecl *D) { 801 const DSAVarData DVar = getTopDSA(D, false); 802 return isOpenMPThreadPrivate(DVar.CKind); 803 } 804 805 /// Marks current region as ordered (it has an 'ordered' clause). 806 void setOrderedRegion(bool IsOrdered, const Expr *Param, 807 OMPOrderedClause *Clause) { 808 if (IsOrdered) 809 getTopOfStack().OrderedRegion.emplace(Param, Clause); 810 else 811 getTopOfStack().OrderedRegion.reset(); 812 } 813 /// Returns true, if region is ordered (has associated 'ordered' clause), 814 /// false - otherwise. 815 bool isOrderedRegion() const { 816 if (const SharingMapTy *Top = getTopOfStackOrNull()) 817 return Top->OrderedRegion.hasValue(); 818 return false; 819 } 820 /// Returns optional parameter for the ordered region. 821 std::pair<const Expr *, OMPOrderedClause *> getOrderedRegionParam() const { 822 if (const SharingMapTy *Top = getTopOfStackOrNull()) 823 if (Top->OrderedRegion.hasValue()) 824 return Top->OrderedRegion.getValue(); 825 return std::make_pair(nullptr, nullptr); 826 } 827 /// Returns true, if parent region is ordered (has associated 828 /// 'ordered' clause), false - otherwise. 829 bool isParentOrderedRegion() const { 830 if (const SharingMapTy *Parent = getSecondOnStackOrNull()) 831 return Parent->OrderedRegion.hasValue(); 832 return false; 833 } 834 /// Returns optional parameter for the ordered region. 835 std::pair<const Expr *, OMPOrderedClause *> 836 getParentOrderedRegionParam() const { 837 if (const SharingMapTy *Parent = getSecondOnStackOrNull()) 838 if (Parent->OrderedRegion.hasValue()) 839 return Parent->OrderedRegion.getValue(); 840 return std::make_pair(nullptr, nullptr); 841 } 842 /// Marks current region as nowait (it has a 'nowait' clause). 843 void setNowaitRegion(bool IsNowait = true) { 844 getTopOfStack().NowaitRegion = IsNowait; 845 } 846 /// Returns true, if parent region is nowait (has associated 847 /// 'nowait' clause), false - otherwise. 848 bool isParentNowaitRegion() const { 849 if (const SharingMapTy *Parent = getSecondOnStackOrNull()) 850 return Parent->NowaitRegion; 851 return false; 852 } 853 /// Marks parent region as cancel region. 854 void setParentCancelRegion(bool Cancel = true) { 855 if (SharingMapTy *Parent = getSecondOnStackOrNull()) 856 Parent->CancelRegion |= Cancel; 857 } 858 /// Return true if current region has inner cancel construct. 859 bool isCancelRegion() const { 860 const SharingMapTy *Top = getTopOfStackOrNull(); 861 return Top ? Top->CancelRegion : false; 862 } 863 864 /// Mark that parent region already has scan directive. 865 void setParentHasScanDirective(SourceLocation Loc) { 866 if (SharingMapTy *Parent = getSecondOnStackOrNull()) 867 Parent->PrevScanLocation = Loc; 868 } 869 /// Return true if current region has inner cancel construct. 870 bool doesParentHasScanDirective() const { 871 const SharingMapTy *Top = getSecondOnStackOrNull(); 872 return Top ? Top->PrevScanLocation.isValid() : false; 873 } 874 /// Return true if current region has inner cancel construct. 875 SourceLocation getParentScanDirectiveLoc() const { 876 const SharingMapTy *Top = getSecondOnStackOrNull(); 877 return Top ? Top->PrevScanLocation : SourceLocation(); 878 } 879 /// Mark that parent region already has ordered directive. 880 void setParentHasOrderedDirective(SourceLocation Loc) { 881 if (SharingMapTy *Parent = getSecondOnStackOrNull()) 882 Parent->PrevOrderedLocation = Loc; 883 } 884 /// Return true if current region has inner ordered construct. 885 bool doesParentHasOrderedDirective() const { 886 const SharingMapTy *Top = getSecondOnStackOrNull(); 887 return Top ? Top->PrevOrderedLocation.isValid() : false; 888 } 889 /// Returns the location of the previously specified ordered directive. 890 SourceLocation getParentOrderedDirectiveLoc() const { 891 const SharingMapTy *Top = getSecondOnStackOrNull(); 892 return Top ? Top->PrevOrderedLocation : SourceLocation(); 893 } 894 895 /// Set collapse value for the region. 896 void setAssociatedLoops(unsigned Val) { 897 getTopOfStack().AssociatedLoops = Val; 898 if (Val > 1) 899 getTopOfStack().HasMutipleLoops = true; 900 } 901 /// Return collapse value for region. 902 unsigned getAssociatedLoops() const { 903 const SharingMapTy *Top = getTopOfStackOrNull(); 904 return Top ? Top->AssociatedLoops : 0; 905 } 906 /// Returns true if the construct is associated with multiple loops. 907 bool hasMutipleLoops() const { 908 const SharingMapTy *Top = getTopOfStackOrNull(); 909 return Top ? Top->HasMutipleLoops : false; 910 } 911 912 /// Marks current target region as one with closely nested teams 913 /// region. 914 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) { 915 if (SharingMapTy *Parent = getSecondOnStackOrNull()) 916 Parent->InnerTeamsRegionLoc = TeamsRegionLoc; 917 } 918 /// Returns true, if current region has closely nested teams region. 919 bool hasInnerTeamsRegion() const { 920 return getInnerTeamsRegionLoc().isValid(); 921 } 922 /// Returns location of the nested teams region (if any). 923 SourceLocation getInnerTeamsRegionLoc() const { 924 const SharingMapTy *Top = getTopOfStackOrNull(); 925 return Top ? Top->InnerTeamsRegionLoc : SourceLocation(); 926 } 927 928 Scope *getCurScope() const { 929 const SharingMapTy *Top = getTopOfStackOrNull(); 930 return Top ? Top->CurScope : nullptr; 931 } 932 void setContext(DeclContext *DC) { getTopOfStack().Context = DC; } 933 SourceLocation getConstructLoc() const { 934 const SharingMapTy *Top = getTopOfStackOrNull(); 935 return Top ? Top->ConstructLoc : SourceLocation(); 936 } 937 938 /// Do the check specified in \a Check to all component lists and return true 939 /// if any issue is found. 940 bool checkMappableExprComponentListsForDecl( 941 const ValueDecl *VD, bool CurrentRegionOnly, 942 const llvm::function_ref< 943 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef, 944 OpenMPClauseKind)> 945 Check) const { 946 if (isStackEmpty()) 947 return false; 948 auto SI = begin(); 949 auto SE = end(); 950 951 if (SI == SE) 952 return false; 953 954 if (CurrentRegionOnly) 955 SE = std::next(SI); 956 else 957 std::advance(SI, 1); 958 959 for (; SI != SE; ++SI) { 960 auto MI = SI->MappedExprComponents.find(VD); 961 if (MI != SI->MappedExprComponents.end()) 962 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L : 963 MI->second.Components) 964 if (Check(L, MI->second.Kind)) 965 return true; 966 } 967 return false; 968 } 969 970 /// Do the check specified in \a Check to all component lists at a given level 971 /// and return true if any issue is found. 972 bool checkMappableExprComponentListsForDeclAtLevel( 973 const ValueDecl *VD, unsigned Level, 974 const llvm::function_ref< 975 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef, 976 OpenMPClauseKind)> 977 Check) const { 978 if (getStackSize() <= Level) 979 return false; 980 981 const SharingMapTy &StackElem = getStackElemAtLevel(Level); 982 auto MI = StackElem.MappedExprComponents.find(VD); 983 if (MI != StackElem.MappedExprComponents.end()) 984 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L : 985 MI->second.Components) 986 if (Check(L, MI->second.Kind)) 987 return true; 988 return false; 989 } 990 991 /// Create a new mappable expression component list associated with a given 992 /// declaration and initialize it with the provided list of components. 993 void addMappableExpressionComponents( 994 const ValueDecl *VD, 995 OMPClauseMappableExprCommon::MappableExprComponentListRef Components, 996 OpenMPClauseKind WhereFoundClauseKind) { 997 MappedExprComponentTy &MEC = getTopOfStack().MappedExprComponents[VD]; 998 // Create new entry and append the new components there. 999 MEC.Components.resize(MEC.Components.size() + 1); 1000 MEC.Components.back().append(Components.begin(), Components.end()); 1001 MEC.Kind = WhereFoundClauseKind; 1002 } 1003 1004 unsigned getNestingLevel() const { 1005 assert(!isStackEmpty()); 1006 return getStackSize() - 1; 1007 } 1008 void addDoacrossDependClause(OMPDependClause *C, 1009 const OperatorOffsetTy &OpsOffs) { 1010 SharingMapTy *Parent = getSecondOnStackOrNull(); 1011 assert(Parent && isOpenMPWorksharingDirective(Parent->Directive)); 1012 Parent->DoacrossDepends.try_emplace(C, OpsOffs); 1013 } 1014 llvm::iterator_range<DoacrossDependMapTy::const_iterator> 1015 getDoacrossDependClauses() const { 1016 const SharingMapTy &StackElem = getTopOfStack(); 1017 if (isOpenMPWorksharingDirective(StackElem.Directive)) { 1018 const DoacrossDependMapTy &Ref = StackElem.DoacrossDepends; 1019 return llvm::make_range(Ref.begin(), Ref.end()); 1020 } 1021 return llvm::make_range(StackElem.DoacrossDepends.end(), 1022 StackElem.DoacrossDepends.end()); 1023 } 1024 1025 // Store types of classes which have been explicitly mapped 1026 void addMappedClassesQualTypes(QualType QT) { 1027 SharingMapTy &StackElem = getTopOfStack(); 1028 StackElem.MappedClassesQualTypes.insert(QT); 1029 } 1030 1031 // Return set of mapped classes types 1032 bool isClassPreviouslyMapped(QualType QT) const { 1033 const SharingMapTy &StackElem = getTopOfStack(); 1034 return StackElem.MappedClassesQualTypes.contains(QT); 1035 } 1036 1037 /// Adds global declare target to the parent target region. 1038 void addToParentTargetRegionLinkGlobals(DeclRefExpr *E) { 1039 assert(*OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration( 1040 E->getDecl()) == OMPDeclareTargetDeclAttr::MT_Link && 1041 "Expected declare target link global."); 1042 for (auto &Elem : *this) { 1043 if (isOpenMPTargetExecutionDirective(Elem.Directive)) { 1044 Elem.DeclareTargetLinkVarDecls.push_back(E); 1045 return; 1046 } 1047 } 1048 } 1049 1050 /// Returns the list of globals with declare target link if current directive 1051 /// is target. 1052 ArrayRef<DeclRefExpr *> getLinkGlobals() const { 1053 assert(isOpenMPTargetExecutionDirective(getCurrentDirective()) && 1054 "Expected target executable directive."); 1055 return getTopOfStack().DeclareTargetLinkVarDecls; 1056 } 1057 1058 /// Adds list of allocators expressions. 1059 void addInnerAllocatorExpr(Expr *E) { 1060 getTopOfStack().InnerUsedAllocators.push_back(E); 1061 } 1062 /// Return list of used allocators. 1063 ArrayRef<Expr *> getInnerAllocators() const { 1064 return getTopOfStack().InnerUsedAllocators; 1065 } 1066 /// Marks the declaration as implicitly firstprivate nin the task-based 1067 /// regions. 1068 void addImplicitTaskFirstprivate(unsigned Level, Decl *D) { 1069 getStackElemAtLevel(Level).ImplicitTaskFirstprivates.insert(D); 1070 } 1071 /// Checks if the decl is implicitly firstprivate in the task-based region. 1072 bool isImplicitTaskFirstprivate(Decl *D) const { 1073 return getTopOfStack().ImplicitTaskFirstprivates.contains(D); 1074 } 1075 1076 /// Marks decl as used in uses_allocators clause as the allocator. 1077 void addUsesAllocatorsDecl(const Decl *D, UsesAllocatorsDeclKind Kind) { 1078 getTopOfStack().UsesAllocatorsDecls.try_emplace(D, Kind); 1079 } 1080 /// Checks if specified decl is used in uses allocator clause as the 1081 /// allocator. 1082 Optional<UsesAllocatorsDeclKind> isUsesAllocatorsDecl(unsigned Level, 1083 const Decl *D) const { 1084 const SharingMapTy &StackElem = getTopOfStack(); 1085 auto I = StackElem.UsesAllocatorsDecls.find(D); 1086 if (I == StackElem.UsesAllocatorsDecls.end()) 1087 return None; 1088 return I->getSecond(); 1089 } 1090 Optional<UsesAllocatorsDeclKind> isUsesAllocatorsDecl(const Decl *D) const { 1091 const SharingMapTy &StackElem = getTopOfStack(); 1092 auto I = StackElem.UsesAllocatorsDecls.find(D); 1093 if (I == StackElem.UsesAllocatorsDecls.end()) 1094 return None; 1095 return I->getSecond(); 1096 } 1097 1098 void addDeclareMapperVarRef(Expr *Ref) { 1099 SharingMapTy &StackElem = getTopOfStack(); 1100 StackElem.DeclareMapperVar = Ref; 1101 } 1102 const Expr *getDeclareMapperVarRef() const { 1103 const SharingMapTy *Top = getTopOfStackOrNull(); 1104 return Top ? Top->DeclareMapperVar : nullptr; 1105 } 1106 }; 1107 1108 bool isImplicitTaskingRegion(OpenMPDirectiveKind DKind) { 1109 return isOpenMPParallelDirective(DKind) || isOpenMPTeamsDirective(DKind); 1110 } 1111 1112 bool isImplicitOrExplicitTaskingRegion(OpenMPDirectiveKind DKind) { 1113 return isImplicitTaskingRegion(DKind) || isOpenMPTaskingDirective(DKind) || 1114 DKind == OMPD_unknown; 1115 } 1116 1117 } // namespace 1118 1119 static const Expr *getExprAsWritten(const Expr *E) { 1120 if (const auto *FE = dyn_cast<FullExpr>(E)) 1121 E = FE->getSubExpr(); 1122 1123 if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E)) 1124 E = MTE->getSubExpr(); 1125 1126 while (const auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E)) 1127 E = Binder->getSubExpr(); 1128 1129 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E)) 1130 E = ICE->getSubExprAsWritten(); 1131 return E->IgnoreParens(); 1132 } 1133 1134 static Expr *getExprAsWritten(Expr *E) { 1135 return const_cast<Expr *>(getExprAsWritten(const_cast<const Expr *>(E))); 1136 } 1137 1138 static const ValueDecl *getCanonicalDecl(const ValueDecl *D) { 1139 if (const auto *CED = dyn_cast<OMPCapturedExprDecl>(D)) 1140 if (const auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 1141 D = ME->getMemberDecl(); 1142 const auto *VD = dyn_cast<VarDecl>(D); 1143 const auto *FD = dyn_cast<FieldDecl>(D); 1144 if (VD != nullptr) { 1145 VD = VD->getCanonicalDecl(); 1146 D = VD; 1147 } else { 1148 assert(FD); 1149 FD = FD->getCanonicalDecl(); 1150 D = FD; 1151 } 1152 return D; 1153 } 1154 1155 static ValueDecl *getCanonicalDecl(ValueDecl *D) { 1156 return const_cast<ValueDecl *>( 1157 getCanonicalDecl(const_cast<const ValueDecl *>(D))); 1158 } 1159 1160 DSAStackTy::DSAVarData DSAStackTy::getDSA(const_iterator &Iter, 1161 ValueDecl *D) const { 1162 D = getCanonicalDecl(D); 1163 auto *VD = dyn_cast<VarDecl>(D); 1164 const auto *FD = dyn_cast<FieldDecl>(D); 1165 DSAVarData DVar; 1166 if (Iter == end()) { 1167 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1168 // in a region but not in construct] 1169 // File-scope or namespace-scope variables referenced in called routines 1170 // in the region are shared unless they appear in a threadprivate 1171 // directive. 1172 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(VD)) 1173 DVar.CKind = OMPC_shared; 1174 1175 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced 1176 // in a region but not in construct] 1177 // Variables with static storage duration that are declared in called 1178 // routines in the region are shared. 1179 if (VD && VD->hasGlobalStorage()) 1180 DVar.CKind = OMPC_shared; 1181 1182 // Non-static data members are shared by default. 1183 if (FD) 1184 DVar.CKind = OMPC_shared; 1185 1186 return DVar; 1187 } 1188 1189 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1190 // in a Construct, C/C++, predetermined, p.1] 1191 // Variables with automatic storage duration that are declared in a scope 1192 // inside the construct are private. 1193 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() && 1194 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) { 1195 DVar.CKind = OMPC_private; 1196 return DVar; 1197 } 1198 1199 DVar.DKind = Iter->Directive; 1200 // Explicitly specified attributes and local variables with predetermined 1201 // attributes. 1202 if (Iter->SharingMap.count(D)) { 1203 const DSAInfo &Data = Iter->SharingMap.lookup(D); 1204 DVar.RefExpr = Data.RefExpr.getPointer(); 1205 DVar.PrivateCopy = Data.PrivateCopy; 1206 DVar.CKind = Data.Attributes; 1207 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 1208 DVar.Modifier = Data.Modifier; 1209 DVar.AppliedToPointee = Data.AppliedToPointee; 1210 return DVar; 1211 } 1212 1213 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1214 // in a Construct, C/C++, implicitly determined, p.1] 1215 // In a parallel or task construct, the data-sharing attributes of these 1216 // variables are determined by the default clause, if present. 1217 switch (Iter->DefaultAttr) { 1218 case DSA_shared: 1219 DVar.CKind = OMPC_shared; 1220 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 1221 return DVar; 1222 case DSA_none: 1223 return DVar; 1224 case DSA_firstprivate: 1225 if (VD->getStorageDuration() == SD_Static && 1226 VD->getDeclContext()->isFileContext()) { 1227 DVar.CKind = OMPC_unknown; 1228 } else { 1229 DVar.CKind = OMPC_firstprivate; 1230 } 1231 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 1232 return DVar; 1233 case DSA_unspecified: 1234 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1235 // in a Construct, implicitly determined, p.2] 1236 // In a parallel construct, if no default clause is present, these 1237 // variables are shared. 1238 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 1239 if ((isOpenMPParallelDirective(DVar.DKind) && 1240 !isOpenMPTaskLoopDirective(DVar.DKind)) || 1241 isOpenMPTeamsDirective(DVar.DKind)) { 1242 DVar.CKind = OMPC_shared; 1243 return DVar; 1244 } 1245 1246 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1247 // in a Construct, implicitly determined, p.4] 1248 // In a task construct, if no default clause is present, a variable that in 1249 // the enclosing context is determined to be shared by all implicit tasks 1250 // bound to the current team is shared. 1251 if (isOpenMPTaskingDirective(DVar.DKind)) { 1252 DSAVarData DVarTemp; 1253 const_iterator I = Iter, E = end(); 1254 do { 1255 ++I; 1256 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables 1257 // Referenced in a Construct, implicitly determined, p.6] 1258 // In a task construct, if no default clause is present, a variable 1259 // whose data-sharing attribute is not determined by the rules above is 1260 // firstprivate. 1261 DVarTemp = getDSA(I, D); 1262 if (DVarTemp.CKind != OMPC_shared) { 1263 DVar.RefExpr = nullptr; 1264 DVar.CKind = OMPC_firstprivate; 1265 return DVar; 1266 } 1267 } while (I != E && !isImplicitTaskingRegion(I->Directive)); 1268 DVar.CKind = 1269 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared; 1270 return DVar; 1271 } 1272 } 1273 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1274 // in a Construct, implicitly determined, p.3] 1275 // For constructs other than task, if no default clause is present, these 1276 // variables inherit their data-sharing attributes from the enclosing 1277 // context. 1278 return getDSA(++Iter, D); 1279 } 1280 1281 const Expr *DSAStackTy::addUniqueAligned(const ValueDecl *D, 1282 const Expr *NewDE) { 1283 assert(!isStackEmpty() && "Data sharing attributes stack is empty"); 1284 D = getCanonicalDecl(D); 1285 SharingMapTy &StackElem = getTopOfStack(); 1286 auto It = StackElem.AlignedMap.find(D); 1287 if (It == StackElem.AlignedMap.end()) { 1288 assert(NewDE && "Unexpected nullptr expr to be added into aligned map"); 1289 StackElem.AlignedMap[D] = NewDE; 1290 return nullptr; 1291 } 1292 assert(It->second && "Unexpected nullptr expr in the aligned map"); 1293 return It->second; 1294 } 1295 1296 const Expr *DSAStackTy::addUniqueNontemporal(const ValueDecl *D, 1297 const Expr *NewDE) { 1298 assert(!isStackEmpty() && "Data sharing attributes stack is empty"); 1299 D = getCanonicalDecl(D); 1300 SharingMapTy &StackElem = getTopOfStack(); 1301 auto It = StackElem.NontemporalMap.find(D); 1302 if (It == StackElem.NontemporalMap.end()) { 1303 assert(NewDE && "Unexpected nullptr expr to be added into aligned map"); 1304 StackElem.NontemporalMap[D] = NewDE; 1305 return nullptr; 1306 } 1307 assert(It->second && "Unexpected nullptr expr in the aligned map"); 1308 return It->second; 1309 } 1310 1311 void DSAStackTy::addLoopControlVariable(const ValueDecl *D, VarDecl *Capture) { 1312 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1313 D = getCanonicalDecl(D); 1314 SharingMapTy &StackElem = getTopOfStack(); 1315 StackElem.LCVMap.try_emplace( 1316 D, LCDeclInfo(StackElem.LCVMap.size() + 1, Capture)); 1317 } 1318 1319 const DSAStackTy::LCDeclInfo 1320 DSAStackTy::isLoopControlVariable(const ValueDecl *D) const { 1321 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1322 D = getCanonicalDecl(D); 1323 const SharingMapTy &StackElem = getTopOfStack(); 1324 auto It = StackElem.LCVMap.find(D); 1325 if (It != StackElem.LCVMap.end()) 1326 return It->second; 1327 return {0, nullptr}; 1328 } 1329 1330 const DSAStackTy::LCDeclInfo 1331 DSAStackTy::isLoopControlVariable(const ValueDecl *D, unsigned Level) const { 1332 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1333 D = getCanonicalDecl(D); 1334 for (unsigned I = Level + 1; I > 0; --I) { 1335 const SharingMapTy &StackElem = getStackElemAtLevel(I - 1); 1336 auto It = StackElem.LCVMap.find(D); 1337 if (It != StackElem.LCVMap.end()) 1338 return It->second; 1339 } 1340 return {0, nullptr}; 1341 } 1342 1343 const DSAStackTy::LCDeclInfo 1344 DSAStackTy::isParentLoopControlVariable(const ValueDecl *D) const { 1345 const SharingMapTy *Parent = getSecondOnStackOrNull(); 1346 assert(Parent && "Data-sharing attributes stack is empty"); 1347 D = getCanonicalDecl(D); 1348 auto It = Parent->LCVMap.find(D); 1349 if (It != Parent->LCVMap.end()) 1350 return It->second; 1351 return {0, nullptr}; 1352 } 1353 1354 const ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) const { 1355 const SharingMapTy *Parent = getSecondOnStackOrNull(); 1356 assert(Parent && "Data-sharing attributes stack is empty"); 1357 if (Parent->LCVMap.size() < I) 1358 return nullptr; 1359 for (const auto &Pair : Parent->LCVMap) 1360 if (Pair.second.first == I) 1361 return Pair.first; 1362 return nullptr; 1363 } 1364 1365 void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A, 1366 DeclRefExpr *PrivateCopy, unsigned Modifier, 1367 bool AppliedToPointee) { 1368 D = getCanonicalDecl(D); 1369 if (A == OMPC_threadprivate) { 1370 DSAInfo &Data = Threadprivates[D]; 1371 Data.Attributes = A; 1372 Data.RefExpr.setPointer(E); 1373 Data.PrivateCopy = nullptr; 1374 Data.Modifier = Modifier; 1375 } else { 1376 DSAInfo &Data = getTopOfStack().SharingMap[D]; 1377 assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) || 1378 (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) || 1379 (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) || 1380 (isLoopControlVariable(D).first && A == OMPC_private)); 1381 Data.Modifier = Modifier; 1382 if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) { 1383 Data.RefExpr.setInt(/*IntVal=*/true); 1384 return; 1385 } 1386 const bool IsLastprivate = 1387 A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate; 1388 Data.Attributes = A; 1389 Data.RefExpr.setPointerAndInt(E, IsLastprivate); 1390 Data.PrivateCopy = PrivateCopy; 1391 Data.AppliedToPointee = AppliedToPointee; 1392 if (PrivateCopy) { 1393 DSAInfo &Data = getTopOfStack().SharingMap[PrivateCopy->getDecl()]; 1394 Data.Modifier = Modifier; 1395 Data.Attributes = A; 1396 Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate); 1397 Data.PrivateCopy = nullptr; 1398 Data.AppliedToPointee = AppliedToPointee; 1399 } 1400 } 1401 } 1402 1403 /// Build a variable declaration for OpenMP loop iteration variable. 1404 static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type, 1405 StringRef Name, const AttrVec *Attrs = nullptr, 1406 DeclRefExpr *OrigRef = nullptr) { 1407 DeclContext *DC = SemaRef.CurContext; 1408 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name); 1409 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc); 1410 auto *Decl = 1411 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None); 1412 if (Attrs) { 1413 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end()); 1414 I != E; ++I) 1415 Decl->addAttr(*I); 1416 } 1417 Decl->setImplicit(); 1418 if (OrigRef) { 1419 Decl->addAttr( 1420 OMPReferencedVarAttr::CreateImplicit(SemaRef.Context, OrigRef)); 1421 } 1422 return Decl; 1423 } 1424 1425 static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty, 1426 SourceLocation Loc, 1427 bool RefersToCapture = false) { 1428 D->setReferenced(); 1429 D->markUsed(S.Context); 1430 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(), 1431 SourceLocation(), D, RefersToCapture, Loc, Ty, 1432 VK_LValue); 1433 } 1434 1435 void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 1436 BinaryOperatorKind BOK) { 1437 D = getCanonicalDecl(D); 1438 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1439 assert( 1440 getTopOfStack().SharingMap[D].Attributes == OMPC_reduction && 1441 "Additional reduction info may be specified only for reduction items."); 1442 ReductionData &ReductionData = getTopOfStack().ReductionMap[D]; 1443 assert(ReductionData.ReductionRange.isInvalid() && 1444 (getTopOfStack().Directive == OMPD_taskgroup || 1445 ((isOpenMPParallelDirective(getTopOfStack().Directive) || 1446 isOpenMPWorksharingDirective(getTopOfStack().Directive)) && 1447 !isOpenMPSimdDirective(getTopOfStack().Directive))) && 1448 "Additional reduction info may be specified only once for reduction " 1449 "items."); 1450 ReductionData.set(BOK, SR); 1451 Expr *&TaskgroupReductionRef = getTopOfStack().TaskgroupReductionRef; 1452 if (!TaskgroupReductionRef) { 1453 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(), 1454 SemaRef.Context.VoidPtrTy, ".task_red."); 1455 TaskgroupReductionRef = 1456 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin()); 1457 } 1458 } 1459 1460 void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 1461 const Expr *ReductionRef) { 1462 D = getCanonicalDecl(D); 1463 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1464 assert( 1465 getTopOfStack().SharingMap[D].Attributes == OMPC_reduction && 1466 "Additional reduction info may be specified only for reduction items."); 1467 ReductionData &ReductionData = getTopOfStack().ReductionMap[D]; 1468 assert(ReductionData.ReductionRange.isInvalid() && 1469 (getTopOfStack().Directive == OMPD_taskgroup || 1470 ((isOpenMPParallelDirective(getTopOfStack().Directive) || 1471 isOpenMPWorksharingDirective(getTopOfStack().Directive)) && 1472 !isOpenMPSimdDirective(getTopOfStack().Directive))) && 1473 "Additional reduction info may be specified only once for reduction " 1474 "items."); 1475 ReductionData.set(ReductionRef, SR); 1476 Expr *&TaskgroupReductionRef = getTopOfStack().TaskgroupReductionRef; 1477 if (!TaskgroupReductionRef) { 1478 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(), 1479 SemaRef.Context.VoidPtrTy, ".task_red."); 1480 TaskgroupReductionRef = 1481 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin()); 1482 } 1483 } 1484 1485 const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData( 1486 const ValueDecl *D, SourceRange &SR, BinaryOperatorKind &BOK, 1487 Expr *&TaskgroupDescriptor) const { 1488 D = getCanonicalDecl(D); 1489 assert(!isStackEmpty() && "Data-sharing attributes stack is empty."); 1490 for (const_iterator I = begin() + 1, E = end(); I != E; ++I) { 1491 const DSAInfo &Data = I->SharingMap.lookup(D); 1492 if (Data.Attributes != OMPC_reduction || 1493 Data.Modifier != OMPC_REDUCTION_task) 1494 continue; 1495 const ReductionData &ReductionData = I->ReductionMap.lookup(D); 1496 if (!ReductionData.ReductionOp || 1497 ReductionData.ReductionOp.is<const Expr *>()) 1498 return DSAVarData(); 1499 SR = ReductionData.ReductionRange; 1500 BOK = ReductionData.ReductionOp.get<ReductionData::BOKPtrType>(); 1501 assert(I->TaskgroupReductionRef && "taskgroup reduction reference " 1502 "expression for the descriptor is not " 1503 "set."); 1504 TaskgroupDescriptor = I->TaskgroupReductionRef; 1505 return DSAVarData(I->Directive, OMPC_reduction, Data.RefExpr.getPointer(), 1506 Data.PrivateCopy, I->DefaultAttrLoc, OMPC_REDUCTION_task, 1507 /*AppliedToPointee=*/false); 1508 } 1509 return DSAVarData(); 1510 } 1511 1512 const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData( 1513 const ValueDecl *D, SourceRange &SR, const Expr *&ReductionRef, 1514 Expr *&TaskgroupDescriptor) const { 1515 D = getCanonicalDecl(D); 1516 assert(!isStackEmpty() && "Data-sharing attributes stack is empty."); 1517 for (const_iterator I = begin() + 1, E = end(); I != E; ++I) { 1518 const DSAInfo &Data = I->SharingMap.lookup(D); 1519 if (Data.Attributes != OMPC_reduction || 1520 Data.Modifier != OMPC_REDUCTION_task) 1521 continue; 1522 const ReductionData &ReductionData = I->ReductionMap.lookup(D); 1523 if (!ReductionData.ReductionOp || 1524 !ReductionData.ReductionOp.is<const Expr *>()) 1525 return DSAVarData(); 1526 SR = ReductionData.ReductionRange; 1527 ReductionRef = ReductionData.ReductionOp.get<const Expr *>(); 1528 assert(I->TaskgroupReductionRef && "taskgroup reduction reference " 1529 "expression for the descriptor is not " 1530 "set."); 1531 TaskgroupDescriptor = I->TaskgroupReductionRef; 1532 return DSAVarData(I->Directive, OMPC_reduction, Data.RefExpr.getPointer(), 1533 Data.PrivateCopy, I->DefaultAttrLoc, OMPC_REDUCTION_task, 1534 /*AppliedToPointee=*/false); 1535 } 1536 return DSAVarData(); 1537 } 1538 1539 bool DSAStackTy::isOpenMPLocal(VarDecl *D, const_iterator I) const { 1540 D = D->getCanonicalDecl(); 1541 for (const_iterator E = end(); I != E; ++I) { 1542 if (isImplicitOrExplicitTaskingRegion(I->Directive) || 1543 isOpenMPTargetExecutionDirective(I->Directive)) { 1544 if (I->CurScope) { 1545 Scope *TopScope = I->CurScope->getParent(); 1546 Scope *CurScope = getCurScope(); 1547 while (CurScope && CurScope != TopScope && !CurScope->isDeclScope(D)) 1548 CurScope = CurScope->getParent(); 1549 return CurScope != TopScope; 1550 } 1551 for (DeclContext *DC = D->getDeclContext(); DC; DC = DC->getParent()) 1552 if (I->Context == DC) 1553 return true; 1554 return false; 1555 } 1556 } 1557 return false; 1558 } 1559 1560 static bool isConstNotMutableType(Sema &SemaRef, QualType Type, 1561 bool AcceptIfMutable = true, 1562 bool *IsClassType = nullptr) { 1563 ASTContext &Context = SemaRef.getASTContext(); 1564 Type = Type.getNonReferenceType().getCanonicalType(); 1565 bool IsConstant = Type.isConstant(Context); 1566 Type = Context.getBaseElementType(Type); 1567 const CXXRecordDecl *RD = AcceptIfMutable && SemaRef.getLangOpts().CPlusPlus 1568 ? Type->getAsCXXRecordDecl() 1569 : nullptr; 1570 if (const auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD)) 1571 if (const ClassTemplateDecl *CTD = CTSD->getSpecializedTemplate()) 1572 RD = CTD->getTemplatedDecl(); 1573 if (IsClassType) 1574 *IsClassType = RD; 1575 return IsConstant && !(SemaRef.getLangOpts().CPlusPlus && RD && 1576 RD->hasDefinition() && RD->hasMutableFields()); 1577 } 1578 1579 static bool rejectConstNotMutableType(Sema &SemaRef, const ValueDecl *D, 1580 QualType Type, OpenMPClauseKind CKind, 1581 SourceLocation ELoc, 1582 bool AcceptIfMutable = true, 1583 bool ListItemNotVar = false) { 1584 ASTContext &Context = SemaRef.getASTContext(); 1585 bool IsClassType; 1586 if (isConstNotMutableType(SemaRef, Type, AcceptIfMutable, &IsClassType)) { 1587 unsigned Diag = ListItemNotVar ? diag::err_omp_const_list_item 1588 : IsClassType ? diag::err_omp_const_not_mutable_variable 1589 : diag::err_omp_const_variable; 1590 SemaRef.Diag(ELoc, Diag) << getOpenMPClauseName(CKind); 1591 if (!ListItemNotVar && D) { 1592 const VarDecl *VD = dyn_cast<VarDecl>(D); 1593 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 1594 VarDecl::DeclarationOnly; 1595 SemaRef.Diag(D->getLocation(), 1596 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1597 << D; 1598 } 1599 return true; 1600 } 1601 return false; 1602 } 1603 1604 const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, 1605 bool FromParent) { 1606 D = getCanonicalDecl(D); 1607 DSAVarData DVar; 1608 1609 auto *VD = dyn_cast<VarDecl>(D); 1610 auto TI = Threadprivates.find(D); 1611 if (TI != Threadprivates.end()) { 1612 DVar.RefExpr = TI->getSecond().RefExpr.getPointer(); 1613 DVar.CKind = OMPC_threadprivate; 1614 DVar.Modifier = TI->getSecond().Modifier; 1615 return DVar; 1616 } 1617 if (VD && VD->hasAttr<OMPThreadPrivateDeclAttr>()) { 1618 DVar.RefExpr = buildDeclRefExpr( 1619 SemaRef, VD, D->getType().getNonReferenceType(), 1620 VD->getAttr<OMPThreadPrivateDeclAttr>()->getLocation()); 1621 DVar.CKind = OMPC_threadprivate; 1622 addDSA(D, DVar.RefExpr, OMPC_threadprivate); 1623 return DVar; 1624 } 1625 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1626 // in a Construct, C/C++, predetermined, p.1] 1627 // Variables appearing in threadprivate directives are threadprivate. 1628 if ((VD && VD->getTLSKind() != VarDecl::TLS_None && 1629 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() && 1630 SemaRef.getLangOpts().OpenMPUseTLS && 1631 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) || 1632 (VD && VD->getStorageClass() == SC_Register && 1633 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) { 1634 DVar.RefExpr = buildDeclRefExpr( 1635 SemaRef, VD, D->getType().getNonReferenceType(), D->getLocation()); 1636 DVar.CKind = OMPC_threadprivate; 1637 addDSA(D, DVar.RefExpr, OMPC_threadprivate); 1638 return DVar; 1639 } 1640 if (SemaRef.getLangOpts().OpenMPCUDAMode && VD && 1641 VD->isLocalVarDeclOrParm() && !isStackEmpty() && 1642 !isLoopControlVariable(D).first) { 1643 const_iterator IterTarget = 1644 std::find_if(begin(), end(), [](const SharingMapTy &Data) { 1645 return isOpenMPTargetExecutionDirective(Data.Directive); 1646 }); 1647 if (IterTarget != end()) { 1648 const_iterator ParentIterTarget = IterTarget + 1; 1649 for (const_iterator Iter = begin(); Iter != ParentIterTarget; ++Iter) { 1650 if (isOpenMPLocal(VD, Iter)) { 1651 DVar.RefExpr = 1652 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(), 1653 D->getLocation()); 1654 DVar.CKind = OMPC_threadprivate; 1655 return DVar; 1656 } 1657 } 1658 if (!isClauseParsingMode() || IterTarget != begin()) { 1659 auto DSAIter = IterTarget->SharingMap.find(D); 1660 if (DSAIter != IterTarget->SharingMap.end() && 1661 isOpenMPPrivate(DSAIter->getSecond().Attributes)) { 1662 DVar.RefExpr = DSAIter->getSecond().RefExpr.getPointer(); 1663 DVar.CKind = OMPC_threadprivate; 1664 return DVar; 1665 } 1666 const_iterator End = end(); 1667 if (!SemaRef.isOpenMPCapturedByRef(D, 1668 std::distance(ParentIterTarget, End), 1669 /*OpenMPCaptureLevel=*/0)) { 1670 DVar.RefExpr = 1671 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(), 1672 IterTarget->ConstructLoc); 1673 DVar.CKind = OMPC_threadprivate; 1674 return DVar; 1675 } 1676 } 1677 } 1678 } 1679 1680 if (isStackEmpty()) 1681 // Not in OpenMP execution region and top scope was already checked. 1682 return DVar; 1683 1684 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1685 // in a Construct, C/C++, predetermined, p.4] 1686 // Static data members are shared. 1687 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1688 // in a Construct, C/C++, predetermined, p.7] 1689 // Variables with static storage duration that are declared in a scope 1690 // inside the construct are shared. 1691 if (VD && VD->isStaticDataMember()) { 1692 // Check for explicitly specified attributes. 1693 const_iterator I = begin(); 1694 const_iterator EndI = end(); 1695 if (FromParent && I != EndI) 1696 ++I; 1697 if (I != EndI) { 1698 auto It = I->SharingMap.find(D); 1699 if (It != I->SharingMap.end()) { 1700 const DSAInfo &Data = It->getSecond(); 1701 DVar.RefExpr = Data.RefExpr.getPointer(); 1702 DVar.PrivateCopy = Data.PrivateCopy; 1703 DVar.CKind = Data.Attributes; 1704 DVar.ImplicitDSALoc = I->DefaultAttrLoc; 1705 DVar.DKind = I->Directive; 1706 DVar.Modifier = Data.Modifier; 1707 DVar.AppliedToPointee = Data.AppliedToPointee; 1708 return DVar; 1709 } 1710 } 1711 1712 DVar.CKind = OMPC_shared; 1713 return DVar; 1714 } 1715 1716 auto &&MatchesAlways = [](OpenMPDirectiveKind) { return true; }; 1717 // The predetermined shared attribute for const-qualified types having no 1718 // mutable members was removed after OpenMP 3.1. 1719 if (SemaRef.LangOpts.OpenMP <= 31) { 1720 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1721 // in a Construct, C/C++, predetermined, p.6] 1722 // Variables with const qualified type having no mutable member are 1723 // shared. 1724 if (isConstNotMutableType(SemaRef, D->getType())) { 1725 // Variables with const-qualified type having no mutable member may be 1726 // listed in a firstprivate clause, even if they are static data members. 1727 DSAVarData DVarTemp = hasInnermostDSA( 1728 D, 1729 [](OpenMPClauseKind C, bool) { 1730 return C == OMPC_firstprivate || C == OMPC_shared; 1731 }, 1732 MatchesAlways, FromParent); 1733 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr) 1734 return DVarTemp; 1735 1736 DVar.CKind = OMPC_shared; 1737 return DVar; 1738 } 1739 } 1740 1741 // Explicitly specified attributes and local variables with predetermined 1742 // attributes. 1743 const_iterator I = begin(); 1744 const_iterator EndI = end(); 1745 if (FromParent && I != EndI) 1746 ++I; 1747 if (I == EndI) 1748 return DVar; 1749 auto It = I->SharingMap.find(D); 1750 if (It != I->SharingMap.end()) { 1751 const DSAInfo &Data = It->getSecond(); 1752 DVar.RefExpr = Data.RefExpr.getPointer(); 1753 DVar.PrivateCopy = Data.PrivateCopy; 1754 DVar.CKind = Data.Attributes; 1755 DVar.ImplicitDSALoc = I->DefaultAttrLoc; 1756 DVar.DKind = I->Directive; 1757 DVar.Modifier = Data.Modifier; 1758 DVar.AppliedToPointee = Data.AppliedToPointee; 1759 } 1760 1761 return DVar; 1762 } 1763 1764 const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D, 1765 bool FromParent) const { 1766 if (isStackEmpty()) { 1767 const_iterator I; 1768 return getDSA(I, D); 1769 } 1770 D = getCanonicalDecl(D); 1771 const_iterator StartI = begin(); 1772 const_iterator EndI = end(); 1773 if (FromParent && StartI != EndI) 1774 ++StartI; 1775 return getDSA(StartI, D); 1776 } 1777 1778 const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D, 1779 unsigned Level) const { 1780 if (getStackSize() <= Level) 1781 return DSAVarData(); 1782 D = getCanonicalDecl(D); 1783 const_iterator StartI = std::next(begin(), getStackSize() - 1 - Level); 1784 return getDSA(StartI, D); 1785 } 1786 1787 const DSAStackTy::DSAVarData 1788 DSAStackTy::hasDSA(ValueDecl *D, 1789 const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred, 1790 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1791 bool FromParent) const { 1792 if (isStackEmpty()) 1793 return {}; 1794 D = getCanonicalDecl(D); 1795 const_iterator I = begin(); 1796 const_iterator EndI = end(); 1797 if (FromParent && I != EndI) 1798 ++I; 1799 for (; I != EndI; ++I) { 1800 if (!DPred(I->Directive) && 1801 !isImplicitOrExplicitTaskingRegion(I->Directive)) 1802 continue; 1803 const_iterator NewI = I; 1804 DSAVarData DVar = getDSA(NewI, D); 1805 if (I == NewI && CPred(DVar.CKind, DVar.AppliedToPointee)) 1806 return DVar; 1807 } 1808 return {}; 1809 } 1810 1811 const DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA( 1812 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred, 1813 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1814 bool FromParent) const { 1815 if (isStackEmpty()) 1816 return {}; 1817 D = getCanonicalDecl(D); 1818 const_iterator StartI = begin(); 1819 const_iterator EndI = end(); 1820 if (FromParent && StartI != EndI) 1821 ++StartI; 1822 if (StartI == EndI || !DPred(StartI->Directive)) 1823 return {}; 1824 const_iterator NewI = StartI; 1825 DSAVarData DVar = getDSA(NewI, D); 1826 return (NewI == StartI && CPred(DVar.CKind, DVar.AppliedToPointee)) 1827 ? DVar 1828 : DSAVarData(); 1829 } 1830 1831 bool DSAStackTy::hasExplicitDSA( 1832 const ValueDecl *D, 1833 const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred, 1834 unsigned Level, bool NotLastprivate) const { 1835 if (getStackSize() <= Level) 1836 return false; 1837 D = getCanonicalDecl(D); 1838 const SharingMapTy &StackElem = getStackElemAtLevel(Level); 1839 auto I = StackElem.SharingMap.find(D); 1840 if (I != StackElem.SharingMap.end() && I->getSecond().RefExpr.getPointer() && 1841 CPred(I->getSecond().Attributes, I->getSecond().AppliedToPointee) && 1842 (!NotLastprivate || !I->getSecond().RefExpr.getInt())) 1843 return true; 1844 // Check predetermined rules for the loop control variables. 1845 auto LI = StackElem.LCVMap.find(D); 1846 if (LI != StackElem.LCVMap.end()) 1847 return CPred(OMPC_private, /*AppliedToPointee=*/false); 1848 return false; 1849 } 1850 1851 bool DSAStackTy::hasExplicitDirective( 1852 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1853 unsigned Level) const { 1854 if (getStackSize() <= Level) 1855 return false; 1856 const SharingMapTy &StackElem = getStackElemAtLevel(Level); 1857 return DPred(StackElem.Directive); 1858 } 1859 1860 bool DSAStackTy::hasDirective( 1861 const llvm::function_ref<bool(OpenMPDirectiveKind, 1862 const DeclarationNameInfo &, SourceLocation)> 1863 DPred, 1864 bool FromParent) const { 1865 // We look only in the enclosing region. 1866 size_t Skip = FromParent ? 2 : 1; 1867 for (const_iterator I = begin() + std::min(Skip, getStackSize()), E = end(); 1868 I != E; ++I) { 1869 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc)) 1870 return true; 1871 } 1872 return false; 1873 } 1874 1875 void Sema::InitDataSharingAttributesStack() { 1876 VarDataSharingAttributesStack = new DSAStackTy(*this); 1877 } 1878 1879 #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack) 1880 1881 void Sema::pushOpenMPFunctionRegion() { DSAStack->pushFunction(); } 1882 1883 void Sema::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) { 1884 DSAStack->popFunction(OldFSI); 1885 } 1886 1887 static bool isOpenMPDeviceDelayedContext(Sema &S) { 1888 assert(S.LangOpts.OpenMP && S.LangOpts.OpenMPIsDevice && 1889 "Expected OpenMP device compilation."); 1890 return !S.isInOpenMPTargetExecutionDirective(); 1891 } 1892 1893 namespace { 1894 /// Status of the function emission on the host/device. 1895 enum class FunctionEmissionStatus { 1896 Emitted, 1897 Discarded, 1898 Unknown, 1899 }; 1900 } // anonymous namespace 1901 1902 Sema::SemaDiagnosticBuilder Sema::diagIfOpenMPDeviceCode(SourceLocation Loc, 1903 unsigned DiagID, 1904 FunctionDecl *FD) { 1905 assert(LangOpts.OpenMP && LangOpts.OpenMPIsDevice && 1906 "Expected OpenMP device compilation."); 1907 1908 SemaDiagnosticBuilder::Kind Kind = SemaDiagnosticBuilder::K_Nop; 1909 if (FD) { 1910 FunctionEmissionStatus FES = getEmissionStatus(FD); 1911 switch (FES) { 1912 case FunctionEmissionStatus::Emitted: 1913 Kind = SemaDiagnosticBuilder::K_Immediate; 1914 break; 1915 case FunctionEmissionStatus::Unknown: 1916 // TODO: We should always delay diagnostics here in case a target 1917 // region is in a function we do not emit. However, as the 1918 // current diagnostics are associated with the function containing 1919 // the target region and we do not emit that one, we would miss out 1920 // on diagnostics for the target region itself. We need to anchor 1921 // the diagnostics with the new generated function *or* ensure we 1922 // emit diagnostics associated with the surrounding function. 1923 Kind = isOpenMPDeviceDelayedContext(*this) 1924 ? SemaDiagnosticBuilder::K_Deferred 1925 : SemaDiagnosticBuilder::K_Immediate; 1926 break; 1927 case FunctionEmissionStatus::TemplateDiscarded: 1928 case FunctionEmissionStatus::OMPDiscarded: 1929 Kind = SemaDiagnosticBuilder::K_Nop; 1930 break; 1931 case FunctionEmissionStatus::CUDADiscarded: 1932 llvm_unreachable("CUDADiscarded unexpected in OpenMP device compilation"); 1933 break; 1934 } 1935 } 1936 1937 return SemaDiagnosticBuilder(Kind, Loc, DiagID, FD, *this); 1938 } 1939 1940 Sema::SemaDiagnosticBuilder Sema::diagIfOpenMPHostCode(SourceLocation Loc, 1941 unsigned DiagID, 1942 FunctionDecl *FD) { 1943 assert(LangOpts.OpenMP && !LangOpts.OpenMPIsDevice && 1944 "Expected OpenMP host compilation."); 1945 1946 SemaDiagnosticBuilder::Kind Kind = SemaDiagnosticBuilder::K_Nop; 1947 if (FD) { 1948 FunctionEmissionStatus FES = getEmissionStatus(FD); 1949 switch (FES) { 1950 case FunctionEmissionStatus::Emitted: 1951 Kind = SemaDiagnosticBuilder::K_Immediate; 1952 break; 1953 case FunctionEmissionStatus::Unknown: 1954 Kind = SemaDiagnosticBuilder::K_Deferred; 1955 break; 1956 case FunctionEmissionStatus::TemplateDiscarded: 1957 case FunctionEmissionStatus::OMPDiscarded: 1958 case FunctionEmissionStatus::CUDADiscarded: 1959 Kind = SemaDiagnosticBuilder::K_Nop; 1960 break; 1961 } 1962 } 1963 1964 return SemaDiagnosticBuilder(Kind, Loc, DiagID, FD, *this); 1965 } 1966 1967 static OpenMPDefaultmapClauseKind 1968 getVariableCategoryFromDecl(const LangOptions &LO, const ValueDecl *VD) { 1969 if (LO.OpenMP <= 45) { 1970 if (VD->getType().getNonReferenceType()->isScalarType()) 1971 return OMPC_DEFAULTMAP_scalar; 1972 return OMPC_DEFAULTMAP_aggregate; 1973 } 1974 if (VD->getType().getNonReferenceType()->isAnyPointerType()) 1975 return OMPC_DEFAULTMAP_pointer; 1976 if (VD->getType().getNonReferenceType()->isScalarType()) 1977 return OMPC_DEFAULTMAP_scalar; 1978 return OMPC_DEFAULTMAP_aggregate; 1979 } 1980 1981 bool Sema::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level, 1982 unsigned OpenMPCaptureLevel) const { 1983 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1984 1985 ASTContext &Ctx = getASTContext(); 1986 bool IsByRef = true; 1987 1988 // Find the directive that is associated with the provided scope. 1989 D = cast<ValueDecl>(D->getCanonicalDecl()); 1990 QualType Ty = D->getType(); 1991 1992 bool IsVariableUsedInMapClause = false; 1993 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) { 1994 // This table summarizes how a given variable should be passed to the device 1995 // given its type and the clauses where it appears. This table is based on 1996 // the description in OpenMP 4.5 [2.10.4, target Construct] and 1997 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses]. 1998 // 1999 // ========================================================================= 2000 // | type | defaultmap | pvt | first | is_device_ptr | map | res. | 2001 // | |(tofrom:scalar)| | pvt | | | | 2002 // ========================================================================= 2003 // | scl | | | | - | | bycopy| 2004 // | scl | | - | x | - | - | bycopy| 2005 // | scl | | x | - | - | - | null | 2006 // | scl | x | | | - | | byref | 2007 // | scl | x | - | x | - | - | bycopy| 2008 // | scl | x | x | - | - | - | null | 2009 // | scl | | - | - | - | x | byref | 2010 // | scl | x | - | - | - | x | byref | 2011 // 2012 // | agg | n.a. | | | - | | byref | 2013 // | agg | n.a. | - | x | - | - | byref | 2014 // | agg | n.a. | x | - | - | - | null | 2015 // | agg | n.a. | - | - | - | x | byref | 2016 // | agg | n.a. | - | - | - | x[] | byref | 2017 // 2018 // | ptr | n.a. | | | - | | bycopy| 2019 // | ptr | n.a. | - | x | - | - | bycopy| 2020 // | ptr | n.a. | x | - | - | - | null | 2021 // | ptr | n.a. | - | - | - | x | byref | 2022 // | ptr | n.a. | - | - | - | x[] | bycopy| 2023 // | ptr | n.a. | - | - | x | | bycopy| 2024 // | ptr | n.a. | - | - | x | x | bycopy| 2025 // | ptr | n.a. | - | - | x | x[] | bycopy| 2026 // ========================================================================= 2027 // Legend: 2028 // scl - scalar 2029 // ptr - pointer 2030 // agg - aggregate 2031 // x - applies 2032 // - - invalid in this combination 2033 // [] - mapped with an array section 2034 // byref - should be mapped by reference 2035 // byval - should be mapped by value 2036 // null - initialize a local variable to null on the device 2037 // 2038 // Observations: 2039 // - All scalar declarations that show up in a map clause have to be passed 2040 // by reference, because they may have been mapped in the enclosing data 2041 // environment. 2042 // - If the scalar value does not fit the size of uintptr, it has to be 2043 // passed by reference, regardless the result in the table above. 2044 // - For pointers mapped by value that have either an implicit map or an 2045 // array section, the runtime library may pass the NULL value to the 2046 // device instead of the value passed to it by the compiler. 2047 2048 if (Ty->isReferenceType()) 2049 Ty = Ty->castAs<ReferenceType>()->getPointeeType(); 2050 2051 // Locate map clauses and see if the variable being captured is referred to 2052 // in any of those clauses. Here we only care about variables, not fields, 2053 // because fields are part of aggregates. 2054 bool IsVariableAssociatedWithSection = false; 2055 2056 DSAStack->checkMappableExprComponentListsForDeclAtLevel( 2057 D, Level, 2058 [&IsVariableUsedInMapClause, &IsVariableAssociatedWithSection, 2059 D](OMPClauseMappableExprCommon::MappableExprComponentListRef 2060 MapExprComponents, 2061 OpenMPClauseKind WhereFoundClauseKind) { 2062 // Only the map clause information influences how a variable is 2063 // captured. E.g. is_device_ptr does not require changing the default 2064 // behavior. 2065 if (WhereFoundClauseKind != OMPC_map) 2066 return false; 2067 2068 auto EI = MapExprComponents.rbegin(); 2069 auto EE = MapExprComponents.rend(); 2070 2071 assert(EI != EE && "Invalid map expression!"); 2072 2073 if (isa<DeclRefExpr>(EI->getAssociatedExpression())) 2074 IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D; 2075 2076 ++EI; 2077 if (EI == EE) 2078 return false; 2079 2080 if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) || 2081 isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) || 2082 isa<MemberExpr>(EI->getAssociatedExpression()) || 2083 isa<OMPArrayShapingExpr>(EI->getAssociatedExpression())) { 2084 IsVariableAssociatedWithSection = true; 2085 // There is nothing more we need to know about this variable. 2086 return true; 2087 } 2088 2089 // Keep looking for more map info. 2090 return false; 2091 }); 2092 2093 if (IsVariableUsedInMapClause) { 2094 // If variable is identified in a map clause it is always captured by 2095 // reference except if it is a pointer that is dereferenced somehow. 2096 IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection); 2097 } else { 2098 // By default, all the data that has a scalar type is mapped by copy 2099 // (except for reduction variables). 2100 // Defaultmap scalar is mutual exclusive to defaultmap pointer 2101 IsByRef = (DSAStack->isForceCaptureByReferenceInTargetExecutable() && 2102 !Ty->isAnyPointerType()) || 2103 !Ty->isScalarType() || 2104 DSAStack->isDefaultmapCapturedByRef( 2105 Level, getVariableCategoryFromDecl(LangOpts, D)) || 2106 DSAStack->hasExplicitDSA( 2107 D, 2108 [](OpenMPClauseKind K, bool AppliedToPointee) { 2109 return K == OMPC_reduction && !AppliedToPointee; 2110 }, 2111 Level); 2112 } 2113 } 2114 2115 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) { 2116 IsByRef = 2117 ((IsVariableUsedInMapClause && 2118 DSAStack->getCaptureRegion(Level, OpenMPCaptureLevel) == 2119 OMPD_target) || 2120 !(DSAStack->hasExplicitDSA( 2121 D, 2122 [](OpenMPClauseKind K, bool AppliedToPointee) -> bool { 2123 return K == OMPC_firstprivate || 2124 (K == OMPC_reduction && AppliedToPointee); 2125 }, 2126 Level, /*NotLastprivate=*/true) || 2127 DSAStack->isUsesAllocatorsDecl(Level, D))) && 2128 // If the variable is artificial and must be captured by value - try to 2129 // capture by value. 2130 !(isa<OMPCapturedExprDecl>(D) && !D->hasAttr<OMPCaptureNoInitAttr>() && 2131 !cast<OMPCapturedExprDecl>(D)->getInit()->isGLValue()) && 2132 // If the variable is implicitly firstprivate and scalar - capture by 2133 // copy 2134 !(DSAStack->getDefaultDSA() == DSA_firstprivate && 2135 !DSAStack->hasExplicitDSA( 2136 D, [](OpenMPClauseKind K, bool) { return K != OMPC_unknown; }, 2137 Level) && 2138 !DSAStack->isLoopControlVariable(D, Level).first); 2139 } 2140 2141 // When passing data by copy, we need to make sure it fits the uintptr size 2142 // and alignment, because the runtime library only deals with uintptr types. 2143 // If it does not fit the uintptr size, we need to pass the data by reference 2144 // instead. 2145 if (!IsByRef && 2146 (Ctx.getTypeSizeInChars(Ty) > 2147 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) || 2148 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) { 2149 IsByRef = true; 2150 } 2151 2152 return IsByRef; 2153 } 2154 2155 unsigned Sema::getOpenMPNestingLevel() const { 2156 assert(getLangOpts().OpenMP); 2157 return DSAStack->getNestingLevel(); 2158 } 2159 2160 bool Sema::isInOpenMPTargetExecutionDirective() const { 2161 return (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) && 2162 !DSAStack->isClauseParsingMode()) || 2163 DSAStack->hasDirective( 2164 [](OpenMPDirectiveKind K, const DeclarationNameInfo &, 2165 SourceLocation) -> bool { 2166 return isOpenMPTargetExecutionDirective(K); 2167 }, 2168 false); 2169 } 2170 2171 VarDecl *Sema::isOpenMPCapturedDecl(ValueDecl *D, bool CheckScopeInfo, 2172 unsigned StopAt) { 2173 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 2174 D = getCanonicalDecl(D); 2175 2176 auto *VD = dyn_cast<VarDecl>(D); 2177 // Do not capture constexpr variables. 2178 if (VD && VD->isConstexpr()) 2179 return nullptr; 2180 2181 // If we want to determine whether the variable should be captured from the 2182 // perspective of the current capturing scope, and we've already left all the 2183 // capturing scopes of the top directive on the stack, check from the 2184 // perspective of its parent directive (if any) instead. 2185 DSAStackTy::ParentDirectiveScope InParentDirectiveRAII( 2186 *DSAStack, CheckScopeInfo && DSAStack->isBodyComplete()); 2187 2188 // If we are attempting to capture a global variable in a directive with 2189 // 'target' we return true so that this global is also mapped to the device. 2190 // 2191 if (VD && !VD->hasLocalStorage() && 2192 (getCurCapturedRegion() || getCurBlock() || getCurLambda())) { 2193 if (isInOpenMPTargetExecutionDirective()) { 2194 DSAStackTy::DSAVarData DVarTop = 2195 DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode()); 2196 if (DVarTop.CKind != OMPC_unknown && DVarTop.RefExpr) 2197 return VD; 2198 // If the declaration is enclosed in a 'declare target' directive, 2199 // then it should not be captured. 2200 // 2201 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) 2202 return nullptr; 2203 CapturedRegionScopeInfo *CSI = nullptr; 2204 for (FunctionScopeInfo *FSI : llvm::drop_begin( 2205 llvm::reverse(FunctionScopes), 2206 CheckScopeInfo ? (FunctionScopes.size() - (StopAt + 1)) : 0)) { 2207 if (!isa<CapturingScopeInfo>(FSI)) 2208 return nullptr; 2209 if (auto *RSI = dyn_cast<CapturedRegionScopeInfo>(FSI)) 2210 if (RSI->CapRegionKind == CR_OpenMP) { 2211 CSI = RSI; 2212 break; 2213 } 2214 } 2215 assert(CSI && "Failed to find CapturedRegionScopeInfo"); 2216 SmallVector<OpenMPDirectiveKind, 4> Regions; 2217 getOpenMPCaptureRegions(Regions, 2218 DSAStack->getDirective(CSI->OpenMPLevel)); 2219 if (Regions[CSI->OpenMPCaptureLevel] != OMPD_task) 2220 return VD; 2221 } 2222 if (isInOpenMPDeclareTargetContext()) { 2223 // Try to mark variable as declare target if it is used in capturing 2224 // regions. 2225 if (LangOpts.OpenMP <= 45 && 2226 !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) 2227 checkDeclIsAllowedInOpenMPTarget(nullptr, VD); 2228 return nullptr; 2229 } 2230 } 2231 2232 if (CheckScopeInfo) { 2233 bool OpenMPFound = false; 2234 for (unsigned I = StopAt + 1; I > 0; --I) { 2235 FunctionScopeInfo *FSI = FunctionScopes[I - 1]; 2236 if (!isa<CapturingScopeInfo>(FSI)) 2237 return nullptr; 2238 if (auto *RSI = dyn_cast<CapturedRegionScopeInfo>(FSI)) 2239 if (RSI->CapRegionKind == CR_OpenMP) { 2240 OpenMPFound = true; 2241 break; 2242 } 2243 } 2244 if (!OpenMPFound) 2245 return nullptr; 2246 } 2247 2248 if (DSAStack->getCurrentDirective() != OMPD_unknown && 2249 (!DSAStack->isClauseParsingMode() || 2250 DSAStack->getParentDirective() != OMPD_unknown)) { 2251 auto &&Info = DSAStack->isLoopControlVariable(D); 2252 if (Info.first || 2253 (VD && VD->hasLocalStorage() && 2254 isImplicitOrExplicitTaskingRegion(DSAStack->getCurrentDirective())) || 2255 (VD && DSAStack->isForceVarCapturing())) 2256 return VD ? VD : Info.second; 2257 DSAStackTy::DSAVarData DVarTop = 2258 DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode()); 2259 if (DVarTop.CKind != OMPC_unknown && isOpenMPPrivate(DVarTop.CKind) && 2260 (!VD || VD->hasLocalStorage() || !DVarTop.AppliedToPointee)) 2261 return VD ? VD : cast<VarDecl>(DVarTop.PrivateCopy->getDecl()); 2262 // Threadprivate variables must not be captured. 2263 if (isOpenMPThreadPrivate(DVarTop.CKind)) 2264 return nullptr; 2265 // The variable is not private or it is the variable in the directive with 2266 // default(none) clause and not used in any clause. 2267 DSAStackTy::DSAVarData DVarPrivate = DSAStack->hasDSA( 2268 D, 2269 [](OpenMPClauseKind C, bool AppliedToPointee) { 2270 return isOpenMPPrivate(C) && !AppliedToPointee; 2271 }, 2272 [](OpenMPDirectiveKind) { return true; }, 2273 DSAStack->isClauseParsingMode()); 2274 // Global shared must not be captured. 2275 if (VD && !VD->hasLocalStorage() && DVarPrivate.CKind == OMPC_unknown && 2276 ((DSAStack->getDefaultDSA() != DSA_none && 2277 DSAStack->getDefaultDSA() != DSA_firstprivate) || 2278 DVarTop.CKind == OMPC_shared)) 2279 return nullptr; 2280 if (DVarPrivate.CKind != OMPC_unknown || 2281 (VD && (DSAStack->getDefaultDSA() == DSA_none || 2282 DSAStack->getDefaultDSA() == DSA_firstprivate))) 2283 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl()); 2284 } 2285 return nullptr; 2286 } 2287 2288 void Sema::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex, 2289 unsigned Level) const { 2290 FunctionScopesIndex -= getOpenMPCaptureLevels(DSAStack->getDirective(Level)); 2291 } 2292 2293 void Sema::startOpenMPLoop() { 2294 assert(LangOpts.OpenMP && "OpenMP must be enabled."); 2295 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) 2296 DSAStack->loopInit(); 2297 } 2298 2299 void Sema::startOpenMPCXXRangeFor() { 2300 assert(LangOpts.OpenMP && "OpenMP must be enabled."); 2301 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { 2302 DSAStack->resetPossibleLoopCounter(); 2303 DSAStack->loopStart(); 2304 } 2305 } 2306 2307 OpenMPClauseKind Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level, 2308 unsigned CapLevel) const { 2309 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 2310 if (DSAStack->hasExplicitDirective(isOpenMPTaskingDirective, Level)) { 2311 bool IsTriviallyCopyable = 2312 D->getType().getNonReferenceType().isTriviallyCopyableType(Context) && 2313 !D->getType() 2314 .getNonReferenceType() 2315 .getCanonicalType() 2316 ->getAsCXXRecordDecl(); 2317 OpenMPDirectiveKind DKind = DSAStack->getDirective(Level); 2318 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 2319 getOpenMPCaptureRegions(CaptureRegions, DKind); 2320 if (isOpenMPTaskingDirective(CaptureRegions[CapLevel]) && 2321 (IsTriviallyCopyable || 2322 !isOpenMPTaskLoopDirective(CaptureRegions[CapLevel]))) { 2323 if (DSAStack->hasExplicitDSA( 2324 D, 2325 [](OpenMPClauseKind K, bool) { return K == OMPC_firstprivate; }, 2326 Level, /*NotLastprivate=*/true)) 2327 return OMPC_firstprivate; 2328 DSAStackTy::DSAVarData DVar = DSAStack->getImplicitDSA(D, Level); 2329 if (DVar.CKind != OMPC_shared && 2330 !DSAStack->isLoopControlVariable(D, Level).first && !DVar.RefExpr) { 2331 DSAStack->addImplicitTaskFirstprivate(Level, D); 2332 return OMPC_firstprivate; 2333 } 2334 } 2335 } 2336 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { 2337 if (DSAStack->getAssociatedLoops() > 0 && !DSAStack->isLoopStarted()) { 2338 DSAStack->resetPossibleLoopCounter(D); 2339 DSAStack->loopStart(); 2340 return OMPC_private; 2341 } 2342 if ((DSAStack->getPossiblyLoopCunter() == D->getCanonicalDecl() || 2343 DSAStack->isLoopControlVariable(D).first) && 2344 !DSAStack->hasExplicitDSA( 2345 D, [](OpenMPClauseKind K, bool) { return K != OMPC_private; }, 2346 Level) && 2347 !isOpenMPSimdDirective(DSAStack->getCurrentDirective())) 2348 return OMPC_private; 2349 } 2350 if (const auto *VD = dyn_cast<VarDecl>(D)) { 2351 if (DSAStack->isThreadPrivate(const_cast<VarDecl *>(VD)) && 2352 DSAStack->isForceVarCapturing() && 2353 !DSAStack->hasExplicitDSA( 2354 D, [](OpenMPClauseKind K, bool) { return K == OMPC_copyin; }, 2355 Level)) 2356 return OMPC_private; 2357 } 2358 // User-defined allocators are private since they must be defined in the 2359 // context of target region. 2360 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level) && 2361 DSAStack->isUsesAllocatorsDecl(Level, D).getValueOr( 2362 DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait) == 2363 DSAStackTy::UsesAllocatorsDeclKind::UserDefinedAllocator) 2364 return OMPC_private; 2365 return (DSAStack->hasExplicitDSA( 2366 D, [](OpenMPClauseKind K, bool) { return K == OMPC_private; }, 2367 Level) || 2368 (DSAStack->isClauseParsingMode() && 2369 DSAStack->getClauseParsingMode() == OMPC_private) || 2370 // Consider taskgroup reduction descriptor variable a private 2371 // to avoid possible capture in the region. 2372 (DSAStack->hasExplicitDirective( 2373 [](OpenMPDirectiveKind K) { 2374 return K == OMPD_taskgroup || 2375 ((isOpenMPParallelDirective(K) || 2376 isOpenMPWorksharingDirective(K)) && 2377 !isOpenMPSimdDirective(K)); 2378 }, 2379 Level) && 2380 DSAStack->isTaskgroupReductionRef(D, Level))) 2381 ? OMPC_private 2382 : OMPC_unknown; 2383 } 2384 2385 void Sema::setOpenMPCaptureKind(FieldDecl *FD, const ValueDecl *D, 2386 unsigned Level) { 2387 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 2388 D = getCanonicalDecl(D); 2389 OpenMPClauseKind OMPC = OMPC_unknown; 2390 for (unsigned I = DSAStack->getNestingLevel() + 1; I > Level; --I) { 2391 const unsigned NewLevel = I - 1; 2392 if (DSAStack->hasExplicitDSA( 2393 D, 2394 [&OMPC](const OpenMPClauseKind K, bool AppliedToPointee) { 2395 if (isOpenMPPrivate(K) && !AppliedToPointee) { 2396 OMPC = K; 2397 return true; 2398 } 2399 return false; 2400 }, 2401 NewLevel)) 2402 break; 2403 if (DSAStack->checkMappableExprComponentListsForDeclAtLevel( 2404 D, NewLevel, 2405 [](OMPClauseMappableExprCommon::MappableExprComponentListRef, 2406 OpenMPClauseKind) { return true; })) { 2407 OMPC = OMPC_map; 2408 break; 2409 } 2410 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, 2411 NewLevel)) { 2412 OMPC = OMPC_map; 2413 if (DSAStack->mustBeFirstprivateAtLevel( 2414 NewLevel, getVariableCategoryFromDecl(LangOpts, D))) 2415 OMPC = OMPC_firstprivate; 2416 break; 2417 } 2418 } 2419 if (OMPC != OMPC_unknown) 2420 FD->addAttr(OMPCaptureKindAttr::CreateImplicit(Context, unsigned(OMPC))); 2421 } 2422 2423 bool Sema::isOpenMPTargetCapturedDecl(const ValueDecl *D, unsigned Level, 2424 unsigned CaptureLevel) const { 2425 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 2426 // Return true if the current level is no longer enclosed in a target region. 2427 2428 SmallVector<OpenMPDirectiveKind, 4> Regions; 2429 getOpenMPCaptureRegions(Regions, DSAStack->getDirective(Level)); 2430 const auto *VD = dyn_cast<VarDecl>(D); 2431 return VD && !VD->hasLocalStorage() && 2432 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, 2433 Level) && 2434 Regions[CaptureLevel] != OMPD_task; 2435 } 2436 2437 bool Sema::isOpenMPGlobalCapturedDecl(ValueDecl *D, unsigned Level, 2438 unsigned CaptureLevel) const { 2439 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 2440 // Return true if the current level is no longer enclosed in a target region. 2441 2442 if (const auto *VD = dyn_cast<VarDecl>(D)) { 2443 if (!VD->hasLocalStorage()) { 2444 if (isInOpenMPTargetExecutionDirective()) 2445 return true; 2446 DSAStackTy::DSAVarData TopDVar = 2447 DSAStack->getTopDSA(D, /*FromParent=*/false); 2448 unsigned NumLevels = 2449 getOpenMPCaptureLevels(DSAStack->getDirective(Level)); 2450 if (Level == 0) 2451 return (NumLevels == CaptureLevel + 1) && TopDVar.CKind != OMPC_shared; 2452 do { 2453 --Level; 2454 DSAStackTy::DSAVarData DVar = DSAStack->getImplicitDSA(D, Level); 2455 if (DVar.CKind != OMPC_shared) 2456 return true; 2457 } while (Level > 0); 2458 } 2459 } 2460 return true; 2461 } 2462 2463 void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; } 2464 2465 void Sema::ActOnOpenMPBeginDeclareVariant(SourceLocation Loc, 2466 OMPTraitInfo &TI) { 2467 OMPDeclareVariantScopes.push_back(OMPDeclareVariantScope(TI)); 2468 } 2469 2470 void Sema::ActOnOpenMPEndDeclareVariant() { 2471 assert(isInOpenMPDeclareVariantScope() && 2472 "Not in OpenMP declare variant scope!"); 2473 2474 OMPDeclareVariantScopes.pop_back(); 2475 } 2476 2477 void Sema::finalizeOpenMPDelayedAnalysis(const FunctionDecl *Caller, 2478 const FunctionDecl *Callee, 2479 SourceLocation Loc) { 2480 assert(LangOpts.OpenMP && "Expected OpenMP compilation mode."); 2481 Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy = 2482 OMPDeclareTargetDeclAttr::getDeviceType(Caller->getMostRecentDecl()); 2483 // Ignore host functions during device analyzis. 2484 if (LangOpts.OpenMPIsDevice && 2485 (!DevTy || *DevTy == OMPDeclareTargetDeclAttr::DT_Host)) 2486 return; 2487 // Ignore nohost functions during host analyzis. 2488 if (!LangOpts.OpenMPIsDevice && DevTy && 2489 *DevTy == OMPDeclareTargetDeclAttr::DT_NoHost) 2490 return; 2491 const FunctionDecl *FD = Callee->getMostRecentDecl(); 2492 DevTy = OMPDeclareTargetDeclAttr::getDeviceType(FD); 2493 if (LangOpts.OpenMPIsDevice && DevTy && 2494 *DevTy == OMPDeclareTargetDeclAttr::DT_Host) { 2495 // Diagnose host function called during device codegen. 2496 StringRef HostDevTy = 2497 getOpenMPSimpleClauseTypeName(OMPC_device_type, OMPC_DEVICE_TYPE_host); 2498 Diag(Loc, diag::err_omp_wrong_device_function_call) << HostDevTy << 0; 2499 Diag(*OMPDeclareTargetDeclAttr::getLocation(FD), 2500 diag::note_omp_marked_device_type_here) 2501 << HostDevTy; 2502 return; 2503 } 2504 if (!LangOpts.OpenMPIsDevice && DevTy && 2505 *DevTy == OMPDeclareTargetDeclAttr::DT_NoHost) { 2506 // Diagnose nohost function called during host codegen. 2507 StringRef NoHostDevTy = getOpenMPSimpleClauseTypeName( 2508 OMPC_device_type, OMPC_DEVICE_TYPE_nohost); 2509 Diag(Loc, diag::err_omp_wrong_device_function_call) << NoHostDevTy << 1; 2510 Diag(*OMPDeclareTargetDeclAttr::getLocation(FD), 2511 diag::note_omp_marked_device_type_here) 2512 << NoHostDevTy; 2513 } 2514 } 2515 2516 void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind, 2517 const DeclarationNameInfo &DirName, 2518 Scope *CurScope, SourceLocation Loc) { 2519 DSAStack->push(DKind, DirName, CurScope, Loc); 2520 PushExpressionEvaluationContext( 2521 ExpressionEvaluationContext::PotentiallyEvaluated); 2522 } 2523 2524 void Sema::StartOpenMPClause(OpenMPClauseKind K) { 2525 DSAStack->setClauseParsingMode(K); 2526 } 2527 2528 void Sema::EndOpenMPClause() { 2529 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown); 2530 CleanupVarDeclMarking(); 2531 } 2532 2533 static std::pair<ValueDecl *, bool> 2534 getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc, 2535 SourceRange &ERange, bool AllowArraySection = false); 2536 2537 /// Check consistency of the reduction clauses. 2538 static void checkReductionClauses(Sema &S, DSAStackTy *Stack, 2539 ArrayRef<OMPClause *> Clauses) { 2540 bool InscanFound = false; 2541 SourceLocation InscanLoc; 2542 // OpenMP 5.0, 2.19.5.4 reduction Clause, Restrictions. 2543 // A reduction clause without the inscan reduction-modifier may not appear on 2544 // a construct on which a reduction clause with the inscan reduction-modifier 2545 // appears. 2546 for (OMPClause *C : Clauses) { 2547 if (C->getClauseKind() != OMPC_reduction) 2548 continue; 2549 auto *RC = cast<OMPReductionClause>(C); 2550 if (RC->getModifier() == OMPC_REDUCTION_inscan) { 2551 InscanFound = true; 2552 InscanLoc = RC->getModifierLoc(); 2553 continue; 2554 } 2555 if (RC->getModifier() == OMPC_REDUCTION_task) { 2556 // OpenMP 5.0, 2.19.5.4 reduction Clause. 2557 // A reduction clause with the task reduction-modifier may only appear on 2558 // a parallel construct, a worksharing construct or a combined or 2559 // composite construct for which any of the aforementioned constructs is a 2560 // constituent construct and simd or loop are not constituent constructs. 2561 OpenMPDirectiveKind CurDir = Stack->getCurrentDirective(); 2562 if (!(isOpenMPParallelDirective(CurDir) || 2563 isOpenMPWorksharingDirective(CurDir)) || 2564 isOpenMPSimdDirective(CurDir)) 2565 S.Diag(RC->getModifierLoc(), 2566 diag::err_omp_reduction_task_not_parallel_or_worksharing); 2567 continue; 2568 } 2569 } 2570 if (InscanFound) { 2571 for (OMPClause *C : Clauses) { 2572 if (C->getClauseKind() != OMPC_reduction) 2573 continue; 2574 auto *RC = cast<OMPReductionClause>(C); 2575 if (RC->getModifier() != OMPC_REDUCTION_inscan) { 2576 S.Diag(RC->getModifier() == OMPC_REDUCTION_unknown 2577 ? RC->getBeginLoc() 2578 : RC->getModifierLoc(), 2579 diag::err_omp_inscan_reduction_expected); 2580 S.Diag(InscanLoc, diag::note_omp_previous_inscan_reduction); 2581 continue; 2582 } 2583 for (Expr *Ref : RC->varlists()) { 2584 assert(Ref && "NULL expr in OpenMP nontemporal clause."); 2585 SourceLocation ELoc; 2586 SourceRange ERange; 2587 Expr *SimpleRefExpr = Ref; 2588 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange, 2589 /*AllowArraySection=*/true); 2590 ValueDecl *D = Res.first; 2591 if (!D) 2592 continue; 2593 if (!Stack->isUsedInScanDirective(getCanonicalDecl(D))) { 2594 S.Diag(Ref->getExprLoc(), 2595 diag::err_omp_reduction_not_inclusive_exclusive) 2596 << Ref->getSourceRange(); 2597 } 2598 } 2599 } 2600 } 2601 } 2602 2603 static void checkAllocateClauses(Sema &S, DSAStackTy *Stack, 2604 ArrayRef<OMPClause *> Clauses); 2605 static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr, 2606 bool WithInit); 2607 2608 static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack, 2609 const ValueDecl *D, 2610 const DSAStackTy::DSAVarData &DVar, 2611 bool IsLoopIterVar = false); 2612 2613 void Sema::EndOpenMPDSABlock(Stmt *CurDirective) { 2614 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1] 2615 // A variable of class type (or array thereof) that appears in a lastprivate 2616 // clause requires an accessible, unambiguous default constructor for the 2617 // class type, unless the list item is also specified in a firstprivate 2618 // clause. 2619 if (const auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) { 2620 for (OMPClause *C : D->clauses()) { 2621 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) { 2622 SmallVector<Expr *, 8> PrivateCopies; 2623 for (Expr *DE : Clause->varlists()) { 2624 if (DE->isValueDependent() || DE->isTypeDependent()) { 2625 PrivateCopies.push_back(nullptr); 2626 continue; 2627 } 2628 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens()); 2629 auto *VD = cast<VarDecl>(DRE->getDecl()); 2630 QualType Type = VD->getType().getNonReferenceType(); 2631 const DSAStackTy::DSAVarData DVar = 2632 DSAStack->getTopDSA(VD, /*FromParent=*/false); 2633 if (DVar.CKind == OMPC_lastprivate) { 2634 // Generate helper private variable and initialize it with the 2635 // default value. The address of the original variable is replaced 2636 // by the address of the new private variable in CodeGen. This new 2637 // variable is not added to IdResolver, so the code in the OpenMP 2638 // region uses original variable for proper diagnostics. 2639 VarDecl *VDPrivate = buildVarDecl( 2640 *this, DE->getExprLoc(), Type.getUnqualifiedType(), 2641 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr, DRE); 2642 ActOnUninitializedDecl(VDPrivate); 2643 if (VDPrivate->isInvalidDecl()) { 2644 PrivateCopies.push_back(nullptr); 2645 continue; 2646 } 2647 PrivateCopies.push_back(buildDeclRefExpr( 2648 *this, VDPrivate, DE->getType(), DE->getExprLoc())); 2649 } else { 2650 // The variable is also a firstprivate, so initialization sequence 2651 // for private copy is generated already. 2652 PrivateCopies.push_back(nullptr); 2653 } 2654 } 2655 Clause->setPrivateCopies(PrivateCopies); 2656 continue; 2657 } 2658 // Finalize nontemporal clause by handling private copies, if any. 2659 if (auto *Clause = dyn_cast<OMPNontemporalClause>(C)) { 2660 SmallVector<Expr *, 8> PrivateRefs; 2661 for (Expr *RefExpr : Clause->varlists()) { 2662 assert(RefExpr && "NULL expr in OpenMP nontemporal clause."); 2663 SourceLocation ELoc; 2664 SourceRange ERange; 2665 Expr *SimpleRefExpr = RefExpr; 2666 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 2667 if (Res.second) 2668 // It will be analyzed later. 2669 PrivateRefs.push_back(RefExpr); 2670 ValueDecl *D = Res.first; 2671 if (!D) 2672 continue; 2673 2674 const DSAStackTy::DSAVarData DVar = 2675 DSAStack->getTopDSA(D, /*FromParent=*/false); 2676 PrivateRefs.push_back(DVar.PrivateCopy ? DVar.PrivateCopy 2677 : SimpleRefExpr); 2678 } 2679 Clause->setPrivateRefs(PrivateRefs); 2680 continue; 2681 } 2682 if (auto *Clause = dyn_cast<OMPUsesAllocatorsClause>(C)) { 2683 for (unsigned I = 0, E = Clause->getNumberOfAllocators(); I < E; ++I) { 2684 OMPUsesAllocatorsClause::Data D = Clause->getAllocatorData(I); 2685 auto *DRE = dyn_cast<DeclRefExpr>(D.Allocator->IgnoreParenImpCasts()); 2686 if (!DRE) 2687 continue; 2688 ValueDecl *VD = DRE->getDecl(); 2689 if (!VD || !isa<VarDecl>(VD)) 2690 continue; 2691 DSAStackTy::DSAVarData DVar = 2692 DSAStack->getTopDSA(VD, /*FromParent=*/false); 2693 // OpenMP [2.12.5, target Construct] 2694 // Memory allocators that appear in a uses_allocators clause cannot 2695 // appear in other data-sharing attribute clauses or data-mapping 2696 // attribute clauses in the same construct. 2697 Expr *MapExpr = nullptr; 2698 if (DVar.RefExpr || 2699 DSAStack->checkMappableExprComponentListsForDecl( 2700 VD, /*CurrentRegionOnly=*/true, 2701 [VD, &MapExpr]( 2702 OMPClauseMappableExprCommon::MappableExprComponentListRef 2703 MapExprComponents, 2704 OpenMPClauseKind C) { 2705 auto MI = MapExprComponents.rbegin(); 2706 auto ME = MapExprComponents.rend(); 2707 if (MI != ME && 2708 MI->getAssociatedDeclaration()->getCanonicalDecl() == 2709 VD->getCanonicalDecl()) { 2710 MapExpr = MI->getAssociatedExpression(); 2711 return true; 2712 } 2713 return false; 2714 })) { 2715 Diag(D.Allocator->getExprLoc(), 2716 diag::err_omp_allocator_used_in_clauses) 2717 << D.Allocator->getSourceRange(); 2718 if (DVar.RefExpr) 2719 reportOriginalDsa(*this, DSAStack, VD, DVar); 2720 else 2721 Diag(MapExpr->getExprLoc(), diag::note_used_here) 2722 << MapExpr->getSourceRange(); 2723 } 2724 } 2725 continue; 2726 } 2727 } 2728 // Check allocate clauses. 2729 if (!CurContext->isDependentContext()) 2730 checkAllocateClauses(*this, DSAStack, D->clauses()); 2731 checkReductionClauses(*this, DSAStack, D->clauses()); 2732 } 2733 2734 DSAStack->pop(); 2735 DiscardCleanupsInEvaluationContext(); 2736 PopExpressionEvaluationContext(); 2737 } 2738 2739 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, 2740 Expr *NumIterations, Sema &SemaRef, 2741 Scope *S, DSAStackTy *Stack); 2742 2743 namespace { 2744 2745 class VarDeclFilterCCC final : public CorrectionCandidateCallback { 2746 private: 2747 Sema &SemaRef; 2748 2749 public: 2750 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {} 2751 bool ValidateCandidate(const TypoCorrection &Candidate) override { 2752 NamedDecl *ND = Candidate.getCorrectionDecl(); 2753 if (const auto *VD = dyn_cast_or_null<VarDecl>(ND)) { 2754 return VD->hasGlobalStorage() && 2755 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), 2756 SemaRef.getCurScope()); 2757 } 2758 return false; 2759 } 2760 2761 std::unique_ptr<CorrectionCandidateCallback> clone() override { 2762 return std::make_unique<VarDeclFilterCCC>(*this); 2763 } 2764 }; 2765 2766 class VarOrFuncDeclFilterCCC final : public CorrectionCandidateCallback { 2767 private: 2768 Sema &SemaRef; 2769 2770 public: 2771 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {} 2772 bool ValidateCandidate(const TypoCorrection &Candidate) override { 2773 NamedDecl *ND = Candidate.getCorrectionDecl(); 2774 if (ND && ((isa<VarDecl>(ND) && ND->getKind() == Decl::Var) || 2775 isa<FunctionDecl>(ND))) { 2776 return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), 2777 SemaRef.getCurScope()); 2778 } 2779 return false; 2780 } 2781 2782 std::unique_ptr<CorrectionCandidateCallback> clone() override { 2783 return std::make_unique<VarOrFuncDeclFilterCCC>(*this); 2784 } 2785 }; 2786 2787 } // namespace 2788 2789 ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope, 2790 CXXScopeSpec &ScopeSpec, 2791 const DeclarationNameInfo &Id, 2792 OpenMPDirectiveKind Kind) { 2793 LookupResult Lookup(*this, Id, LookupOrdinaryName); 2794 LookupParsedName(Lookup, CurScope, &ScopeSpec, true); 2795 2796 if (Lookup.isAmbiguous()) 2797 return ExprError(); 2798 2799 VarDecl *VD; 2800 if (!Lookup.isSingleResult()) { 2801 VarDeclFilterCCC CCC(*this); 2802 if (TypoCorrection Corrected = 2803 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, CCC, 2804 CTK_ErrorRecovery)) { 2805 diagnoseTypo(Corrected, 2806 PDiag(Lookup.empty() 2807 ? diag::err_undeclared_var_use_suggest 2808 : diag::err_omp_expected_var_arg_suggest) 2809 << Id.getName()); 2810 VD = Corrected.getCorrectionDeclAs<VarDecl>(); 2811 } else { 2812 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use 2813 : diag::err_omp_expected_var_arg) 2814 << Id.getName(); 2815 return ExprError(); 2816 } 2817 } else if (!(VD = Lookup.getAsSingle<VarDecl>())) { 2818 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName(); 2819 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at); 2820 return ExprError(); 2821 } 2822 Lookup.suppressDiagnostics(); 2823 2824 // OpenMP [2.9.2, Syntax, C/C++] 2825 // Variables must be file-scope, namespace-scope, or static block-scope. 2826 if (Kind == OMPD_threadprivate && !VD->hasGlobalStorage()) { 2827 Diag(Id.getLoc(), diag::err_omp_global_var_arg) 2828 << getOpenMPDirectiveName(Kind) << !VD->isStaticLocal(); 2829 bool IsDecl = 2830 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2831 Diag(VD->getLocation(), 2832 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2833 << VD; 2834 return ExprError(); 2835 } 2836 2837 VarDecl *CanonicalVD = VD->getCanonicalDecl(); 2838 NamedDecl *ND = CanonicalVD; 2839 // OpenMP [2.9.2, Restrictions, C/C++, p.2] 2840 // A threadprivate directive for file-scope variables must appear outside 2841 // any definition or declaration. 2842 if (CanonicalVD->getDeclContext()->isTranslationUnit() && 2843 !getCurLexicalContext()->isTranslationUnit()) { 2844 Diag(Id.getLoc(), diag::err_omp_var_scope) 2845 << getOpenMPDirectiveName(Kind) << VD; 2846 bool IsDecl = 2847 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2848 Diag(VD->getLocation(), 2849 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2850 << VD; 2851 return ExprError(); 2852 } 2853 // OpenMP [2.9.2, Restrictions, C/C++, p.3] 2854 // A threadprivate directive for static class member variables must appear 2855 // in the class definition, in the same scope in which the member 2856 // variables are declared. 2857 if (CanonicalVD->isStaticDataMember() && 2858 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) { 2859 Diag(Id.getLoc(), diag::err_omp_var_scope) 2860 << getOpenMPDirectiveName(Kind) << VD; 2861 bool IsDecl = 2862 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2863 Diag(VD->getLocation(), 2864 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2865 << VD; 2866 return ExprError(); 2867 } 2868 // OpenMP [2.9.2, Restrictions, C/C++, p.4] 2869 // A threadprivate directive for namespace-scope variables must appear 2870 // outside any definition or declaration other than the namespace 2871 // definition itself. 2872 if (CanonicalVD->getDeclContext()->isNamespace() && 2873 (!getCurLexicalContext()->isFileContext() || 2874 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) { 2875 Diag(Id.getLoc(), diag::err_omp_var_scope) 2876 << getOpenMPDirectiveName(Kind) << VD; 2877 bool IsDecl = 2878 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2879 Diag(VD->getLocation(), 2880 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2881 << VD; 2882 return ExprError(); 2883 } 2884 // OpenMP [2.9.2, Restrictions, C/C++, p.6] 2885 // A threadprivate directive for static block-scope variables must appear 2886 // in the scope of the variable and not in a nested scope. 2887 if (CanonicalVD->isLocalVarDecl() && CurScope && 2888 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) { 2889 Diag(Id.getLoc(), diag::err_omp_var_scope) 2890 << getOpenMPDirectiveName(Kind) << VD; 2891 bool IsDecl = 2892 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2893 Diag(VD->getLocation(), 2894 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2895 << VD; 2896 return ExprError(); 2897 } 2898 2899 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6] 2900 // A threadprivate directive must lexically precede all references to any 2901 // of the variables in its list. 2902 if (Kind == OMPD_threadprivate && VD->isUsed() && 2903 !DSAStack->isThreadPrivate(VD)) { 2904 Diag(Id.getLoc(), diag::err_omp_var_used) 2905 << getOpenMPDirectiveName(Kind) << VD; 2906 return ExprError(); 2907 } 2908 2909 QualType ExprType = VD->getType().getNonReferenceType(); 2910 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(), 2911 SourceLocation(), VD, 2912 /*RefersToEnclosingVariableOrCapture=*/false, 2913 Id.getLoc(), ExprType, VK_LValue); 2914 } 2915 2916 Sema::DeclGroupPtrTy 2917 Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc, 2918 ArrayRef<Expr *> VarList) { 2919 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) { 2920 CurContext->addDecl(D); 2921 return DeclGroupPtrTy::make(DeclGroupRef(D)); 2922 } 2923 return nullptr; 2924 } 2925 2926 namespace { 2927 class LocalVarRefChecker final 2928 : public ConstStmtVisitor<LocalVarRefChecker, bool> { 2929 Sema &SemaRef; 2930 2931 public: 2932 bool VisitDeclRefExpr(const DeclRefExpr *E) { 2933 if (const auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 2934 if (VD->hasLocalStorage()) { 2935 SemaRef.Diag(E->getBeginLoc(), 2936 diag::err_omp_local_var_in_threadprivate_init) 2937 << E->getSourceRange(); 2938 SemaRef.Diag(VD->getLocation(), diag::note_defined_here) 2939 << VD << VD->getSourceRange(); 2940 return true; 2941 } 2942 } 2943 return false; 2944 } 2945 bool VisitStmt(const Stmt *S) { 2946 for (const Stmt *Child : S->children()) { 2947 if (Child && Visit(Child)) 2948 return true; 2949 } 2950 return false; 2951 } 2952 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {} 2953 }; 2954 } // namespace 2955 2956 OMPThreadPrivateDecl * 2957 Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) { 2958 SmallVector<Expr *, 8> Vars; 2959 for (Expr *RefExpr : VarList) { 2960 auto *DE = cast<DeclRefExpr>(RefExpr); 2961 auto *VD = cast<VarDecl>(DE->getDecl()); 2962 SourceLocation ILoc = DE->getExprLoc(); 2963 2964 // Mark variable as used. 2965 VD->setReferenced(); 2966 VD->markUsed(Context); 2967 2968 QualType QType = VD->getType(); 2969 if (QType->isDependentType() || QType->isInstantiationDependentType()) { 2970 // It will be analyzed later. 2971 Vars.push_back(DE); 2972 continue; 2973 } 2974 2975 // OpenMP [2.9.2, Restrictions, C/C++, p.10] 2976 // A threadprivate variable must not have an incomplete type. 2977 if (RequireCompleteType(ILoc, VD->getType(), 2978 diag::err_omp_threadprivate_incomplete_type)) { 2979 continue; 2980 } 2981 2982 // OpenMP [2.9.2, Restrictions, C/C++, p.10] 2983 // A threadprivate variable must not have a reference type. 2984 if (VD->getType()->isReferenceType()) { 2985 Diag(ILoc, diag::err_omp_ref_type_arg) 2986 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType(); 2987 bool IsDecl = 2988 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2989 Diag(VD->getLocation(), 2990 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2991 << VD; 2992 continue; 2993 } 2994 2995 // Check if this is a TLS variable. If TLS is not being supported, produce 2996 // the corresponding diagnostic. 2997 if ((VD->getTLSKind() != VarDecl::TLS_None && 2998 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() && 2999 getLangOpts().OpenMPUseTLS && 3000 getASTContext().getTargetInfo().isTLSSupported())) || 3001 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() && 3002 !VD->isLocalVarDecl())) { 3003 Diag(ILoc, diag::err_omp_var_thread_local) 3004 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1); 3005 bool IsDecl = 3006 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 3007 Diag(VD->getLocation(), 3008 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 3009 << VD; 3010 continue; 3011 } 3012 3013 // Check if initial value of threadprivate variable reference variable with 3014 // local storage (it is not supported by runtime). 3015 if (const Expr *Init = VD->getAnyInitializer()) { 3016 LocalVarRefChecker Checker(*this); 3017 if (Checker.Visit(Init)) 3018 continue; 3019 } 3020 3021 Vars.push_back(RefExpr); 3022 DSAStack->addDSA(VD, DE, OMPC_threadprivate); 3023 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit( 3024 Context, SourceRange(Loc, Loc))); 3025 if (ASTMutationListener *ML = Context.getASTMutationListener()) 3026 ML->DeclarationMarkedOpenMPThreadPrivate(VD); 3027 } 3028 OMPThreadPrivateDecl *D = nullptr; 3029 if (!Vars.empty()) { 3030 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc, 3031 Vars); 3032 D->setAccess(AS_public); 3033 } 3034 return D; 3035 } 3036 3037 static OMPAllocateDeclAttr::AllocatorTypeTy 3038 getAllocatorKind(Sema &S, DSAStackTy *Stack, Expr *Allocator) { 3039 if (!Allocator) 3040 return OMPAllocateDeclAttr::OMPNullMemAlloc; 3041 if (Allocator->isTypeDependent() || Allocator->isValueDependent() || 3042 Allocator->isInstantiationDependent() || 3043 Allocator->containsUnexpandedParameterPack()) 3044 return OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; 3045 auto AllocatorKindRes = OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; 3046 const Expr *AE = Allocator->IgnoreParenImpCasts(); 3047 for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) { 3048 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I); 3049 const Expr *DefAllocator = Stack->getAllocator(AllocatorKind); 3050 llvm::FoldingSetNodeID AEId, DAEId; 3051 AE->Profile(AEId, S.getASTContext(), /*Canonical=*/true); 3052 DefAllocator->Profile(DAEId, S.getASTContext(), /*Canonical=*/true); 3053 if (AEId == DAEId) { 3054 AllocatorKindRes = AllocatorKind; 3055 break; 3056 } 3057 } 3058 return AllocatorKindRes; 3059 } 3060 3061 static bool checkPreviousOMPAllocateAttribute( 3062 Sema &S, DSAStackTy *Stack, Expr *RefExpr, VarDecl *VD, 3063 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, Expr *Allocator) { 3064 if (!VD->hasAttr<OMPAllocateDeclAttr>()) 3065 return false; 3066 const auto *A = VD->getAttr<OMPAllocateDeclAttr>(); 3067 Expr *PrevAllocator = A->getAllocator(); 3068 OMPAllocateDeclAttr::AllocatorTypeTy PrevAllocatorKind = 3069 getAllocatorKind(S, Stack, PrevAllocator); 3070 bool AllocatorsMatch = AllocatorKind == PrevAllocatorKind; 3071 if (AllocatorsMatch && 3072 AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc && 3073 Allocator && PrevAllocator) { 3074 const Expr *AE = Allocator->IgnoreParenImpCasts(); 3075 const Expr *PAE = PrevAllocator->IgnoreParenImpCasts(); 3076 llvm::FoldingSetNodeID AEId, PAEId; 3077 AE->Profile(AEId, S.Context, /*Canonical=*/true); 3078 PAE->Profile(PAEId, S.Context, /*Canonical=*/true); 3079 AllocatorsMatch = AEId == PAEId; 3080 } 3081 if (!AllocatorsMatch) { 3082 SmallString<256> AllocatorBuffer; 3083 llvm::raw_svector_ostream AllocatorStream(AllocatorBuffer); 3084 if (Allocator) 3085 Allocator->printPretty(AllocatorStream, nullptr, S.getPrintingPolicy()); 3086 SmallString<256> PrevAllocatorBuffer; 3087 llvm::raw_svector_ostream PrevAllocatorStream(PrevAllocatorBuffer); 3088 if (PrevAllocator) 3089 PrevAllocator->printPretty(PrevAllocatorStream, nullptr, 3090 S.getPrintingPolicy()); 3091 3092 SourceLocation AllocatorLoc = 3093 Allocator ? Allocator->getExprLoc() : RefExpr->getExprLoc(); 3094 SourceRange AllocatorRange = 3095 Allocator ? Allocator->getSourceRange() : RefExpr->getSourceRange(); 3096 SourceLocation PrevAllocatorLoc = 3097 PrevAllocator ? PrevAllocator->getExprLoc() : A->getLocation(); 3098 SourceRange PrevAllocatorRange = 3099 PrevAllocator ? PrevAllocator->getSourceRange() : A->getRange(); 3100 S.Diag(AllocatorLoc, diag::warn_omp_used_different_allocator) 3101 << (Allocator ? 1 : 0) << AllocatorStream.str() 3102 << (PrevAllocator ? 1 : 0) << PrevAllocatorStream.str() 3103 << AllocatorRange; 3104 S.Diag(PrevAllocatorLoc, diag::note_omp_previous_allocator) 3105 << PrevAllocatorRange; 3106 return true; 3107 } 3108 return false; 3109 } 3110 3111 static void 3112 applyOMPAllocateAttribute(Sema &S, VarDecl *VD, 3113 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, 3114 Expr *Allocator, Expr *Alignment, SourceRange SR) { 3115 if (VD->hasAttr<OMPAllocateDeclAttr>()) 3116 return; 3117 if (Alignment && 3118 (Alignment->isTypeDependent() || Alignment->isValueDependent() || 3119 Alignment->isInstantiationDependent() || 3120 Alignment->containsUnexpandedParameterPack())) 3121 // Apply later when we have a usable value. 3122 return; 3123 if (Allocator && 3124 (Allocator->isTypeDependent() || Allocator->isValueDependent() || 3125 Allocator->isInstantiationDependent() || 3126 Allocator->containsUnexpandedParameterPack())) 3127 return; 3128 auto *A = OMPAllocateDeclAttr::CreateImplicit(S.Context, AllocatorKind, 3129 Allocator, Alignment, SR); 3130 VD->addAttr(A); 3131 if (ASTMutationListener *ML = S.Context.getASTMutationListener()) 3132 ML->DeclarationMarkedOpenMPAllocate(VD, A); 3133 } 3134 3135 Sema::DeclGroupPtrTy 3136 Sema::ActOnOpenMPAllocateDirective(SourceLocation Loc, ArrayRef<Expr *> VarList, 3137 ArrayRef<OMPClause *> Clauses, 3138 DeclContext *Owner) { 3139 assert(Clauses.size() <= 2 && "Expected at most two clauses."); 3140 Expr *Alignment = nullptr; 3141 Expr *Allocator = nullptr; 3142 if (Clauses.empty()) { 3143 // OpenMP 5.0, 2.11.3 allocate Directive, Restrictions. 3144 // allocate directives that appear in a target region must specify an 3145 // allocator clause unless a requires directive with the dynamic_allocators 3146 // clause is present in the same compilation unit. 3147 if (LangOpts.OpenMPIsDevice && 3148 !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>()) 3149 targetDiag(Loc, diag::err_expected_allocator_clause); 3150 } else { 3151 for (const OMPClause *C : Clauses) 3152 if (const auto *AC = dyn_cast<OMPAllocatorClause>(C)) 3153 Allocator = AC->getAllocator(); 3154 else if (const auto *AC = dyn_cast<OMPAlignClause>(C)) 3155 Alignment = AC->getAlignment(); 3156 else 3157 llvm_unreachable("Unexpected clause on allocate directive"); 3158 } 3159 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind = 3160 getAllocatorKind(*this, DSAStack, Allocator); 3161 SmallVector<Expr *, 8> Vars; 3162 for (Expr *RefExpr : VarList) { 3163 auto *DE = cast<DeclRefExpr>(RefExpr); 3164 auto *VD = cast<VarDecl>(DE->getDecl()); 3165 3166 // Check if this is a TLS variable or global register. 3167 if (VD->getTLSKind() != VarDecl::TLS_None || 3168 VD->hasAttr<OMPThreadPrivateDeclAttr>() || 3169 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() && 3170 !VD->isLocalVarDecl())) 3171 continue; 3172 3173 // If the used several times in the allocate directive, the same allocator 3174 // must be used. 3175 if (checkPreviousOMPAllocateAttribute(*this, DSAStack, RefExpr, VD, 3176 AllocatorKind, Allocator)) 3177 continue; 3178 3179 // OpenMP, 2.11.3 allocate Directive, Restrictions, C / C++ 3180 // If a list item has a static storage type, the allocator expression in the 3181 // allocator clause must be a constant expression that evaluates to one of 3182 // the predefined memory allocator values. 3183 if (Allocator && VD->hasGlobalStorage()) { 3184 if (AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc) { 3185 Diag(Allocator->getExprLoc(), 3186 diag::err_omp_expected_predefined_allocator) 3187 << Allocator->getSourceRange(); 3188 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 3189 VarDecl::DeclarationOnly; 3190 Diag(VD->getLocation(), 3191 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 3192 << VD; 3193 continue; 3194 } 3195 } 3196 3197 Vars.push_back(RefExpr); 3198 applyOMPAllocateAttribute(*this, VD, AllocatorKind, Allocator, Alignment, 3199 DE->getSourceRange()); 3200 } 3201 if (Vars.empty()) 3202 return nullptr; 3203 if (!Owner) 3204 Owner = getCurLexicalContext(); 3205 auto *D = OMPAllocateDecl::Create(Context, Owner, Loc, Vars, Clauses); 3206 D->setAccess(AS_public); 3207 Owner->addDecl(D); 3208 return DeclGroupPtrTy::make(DeclGroupRef(D)); 3209 } 3210 3211 Sema::DeclGroupPtrTy 3212 Sema::ActOnOpenMPRequiresDirective(SourceLocation Loc, 3213 ArrayRef<OMPClause *> ClauseList) { 3214 OMPRequiresDecl *D = nullptr; 3215 if (!CurContext->isFileContext()) { 3216 Diag(Loc, diag::err_omp_invalid_scope) << "requires"; 3217 } else { 3218 D = CheckOMPRequiresDecl(Loc, ClauseList); 3219 if (D) { 3220 CurContext->addDecl(D); 3221 DSAStack->addRequiresDecl(D); 3222 } 3223 } 3224 return DeclGroupPtrTy::make(DeclGroupRef(D)); 3225 } 3226 3227 void Sema::ActOnOpenMPAssumesDirective(SourceLocation Loc, 3228 OpenMPDirectiveKind DKind, 3229 ArrayRef<std::string> Assumptions, 3230 bool SkippedClauses) { 3231 if (!SkippedClauses && Assumptions.empty()) 3232 Diag(Loc, diag::err_omp_no_clause_for_directive) 3233 << llvm::omp::getAllAssumeClauseOptions() 3234 << llvm::omp::getOpenMPDirectiveName(DKind); 3235 3236 auto *AA = AssumptionAttr::Create(Context, llvm::join(Assumptions, ","), Loc); 3237 if (DKind == llvm::omp::Directive::OMPD_begin_assumes) { 3238 OMPAssumeScoped.push_back(AA); 3239 return; 3240 } 3241 3242 // Global assumes without assumption clauses are ignored. 3243 if (Assumptions.empty()) 3244 return; 3245 3246 assert(DKind == llvm::omp::Directive::OMPD_assumes && 3247 "Unexpected omp assumption directive!"); 3248 OMPAssumeGlobal.push_back(AA); 3249 3250 // The OMPAssumeGlobal scope above will take care of new declarations but 3251 // we also want to apply the assumption to existing ones, e.g., to 3252 // declarations in included headers. To this end, we traverse all existing 3253 // declaration contexts and annotate function declarations here. 3254 SmallVector<DeclContext *, 8> DeclContexts; 3255 auto *Ctx = CurContext; 3256 while (Ctx->getLexicalParent()) 3257 Ctx = Ctx->getLexicalParent(); 3258 DeclContexts.push_back(Ctx); 3259 while (!DeclContexts.empty()) { 3260 DeclContext *DC = DeclContexts.pop_back_val(); 3261 for (auto *SubDC : DC->decls()) { 3262 if (SubDC->isInvalidDecl()) 3263 continue; 3264 if (auto *CTD = dyn_cast<ClassTemplateDecl>(SubDC)) { 3265 DeclContexts.push_back(CTD->getTemplatedDecl()); 3266 for (auto *S : CTD->specializations()) 3267 DeclContexts.push_back(S); 3268 continue; 3269 } 3270 if (auto *DC = dyn_cast<DeclContext>(SubDC)) 3271 DeclContexts.push_back(DC); 3272 if (auto *F = dyn_cast<FunctionDecl>(SubDC)) { 3273 F->addAttr(AA); 3274 continue; 3275 } 3276 } 3277 } 3278 } 3279 3280 void Sema::ActOnOpenMPEndAssumesDirective() { 3281 assert(isInOpenMPAssumeScope() && "Not in OpenMP assumes scope!"); 3282 OMPAssumeScoped.pop_back(); 3283 } 3284 3285 OMPRequiresDecl *Sema::CheckOMPRequiresDecl(SourceLocation Loc, 3286 ArrayRef<OMPClause *> ClauseList) { 3287 /// For target specific clauses, the requires directive cannot be 3288 /// specified after the handling of any of the target regions in the 3289 /// current compilation unit. 3290 ArrayRef<SourceLocation> TargetLocations = 3291 DSAStack->getEncounteredTargetLocs(); 3292 SourceLocation AtomicLoc = DSAStack->getAtomicDirectiveLoc(); 3293 if (!TargetLocations.empty() || !AtomicLoc.isInvalid()) { 3294 for (const OMPClause *CNew : ClauseList) { 3295 // Check if any of the requires clauses affect target regions. 3296 if (isa<OMPUnifiedSharedMemoryClause>(CNew) || 3297 isa<OMPUnifiedAddressClause>(CNew) || 3298 isa<OMPReverseOffloadClause>(CNew) || 3299 isa<OMPDynamicAllocatorsClause>(CNew)) { 3300 Diag(Loc, diag::err_omp_directive_before_requires) 3301 << "target" << getOpenMPClauseName(CNew->getClauseKind()); 3302 for (SourceLocation TargetLoc : TargetLocations) { 3303 Diag(TargetLoc, diag::note_omp_requires_encountered_directive) 3304 << "target"; 3305 } 3306 } else if (!AtomicLoc.isInvalid() && 3307 isa<OMPAtomicDefaultMemOrderClause>(CNew)) { 3308 Diag(Loc, diag::err_omp_directive_before_requires) 3309 << "atomic" << getOpenMPClauseName(CNew->getClauseKind()); 3310 Diag(AtomicLoc, diag::note_omp_requires_encountered_directive) 3311 << "atomic"; 3312 } 3313 } 3314 } 3315 3316 if (!DSAStack->hasDuplicateRequiresClause(ClauseList)) 3317 return OMPRequiresDecl::Create(Context, getCurLexicalContext(), Loc, 3318 ClauseList); 3319 return nullptr; 3320 } 3321 3322 static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack, 3323 const ValueDecl *D, 3324 const DSAStackTy::DSAVarData &DVar, 3325 bool IsLoopIterVar) { 3326 if (DVar.RefExpr) { 3327 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa) 3328 << getOpenMPClauseName(DVar.CKind); 3329 return; 3330 } 3331 enum { 3332 PDSA_StaticMemberShared, 3333 PDSA_StaticLocalVarShared, 3334 PDSA_LoopIterVarPrivate, 3335 PDSA_LoopIterVarLinear, 3336 PDSA_LoopIterVarLastprivate, 3337 PDSA_ConstVarShared, 3338 PDSA_GlobalVarShared, 3339 PDSA_TaskVarFirstprivate, 3340 PDSA_LocalVarPrivate, 3341 PDSA_Implicit 3342 } Reason = PDSA_Implicit; 3343 bool ReportHint = false; 3344 auto ReportLoc = D->getLocation(); 3345 auto *VD = dyn_cast<VarDecl>(D); 3346 if (IsLoopIterVar) { 3347 if (DVar.CKind == OMPC_private) 3348 Reason = PDSA_LoopIterVarPrivate; 3349 else if (DVar.CKind == OMPC_lastprivate) 3350 Reason = PDSA_LoopIterVarLastprivate; 3351 else 3352 Reason = PDSA_LoopIterVarLinear; 3353 } else if (isOpenMPTaskingDirective(DVar.DKind) && 3354 DVar.CKind == OMPC_firstprivate) { 3355 Reason = PDSA_TaskVarFirstprivate; 3356 ReportLoc = DVar.ImplicitDSALoc; 3357 } else if (VD && VD->isStaticLocal()) 3358 Reason = PDSA_StaticLocalVarShared; 3359 else if (VD && VD->isStaticDataMember()) 3360 Reason = PDSA_StaticMemberShared; 3361 else if (VD && VD->isFileVarDecl()) 3362 Reason = PDSA_GlobalVarShared; 3363 else if (D->getType().isConstant(SemaRef.getASTContext())) 3364 Reason = PDSA_ConstVarShared; 3365 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) { 3366 ReportHint = true; 3367 Reason = PDSA_LocalVarPrivate; 3368 } 3369 if (Reason != PDSA_Implicit) { 3370 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa) 3371 << Reason << ReportHint 3372 << getOpenMPDirectiveName(Stack->getCurrentDirective()); 3373 } else if (DVar.ImplicitDSALoc.isValid()) { 3374 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa) 3375 << getOpenMPClauseName(DVar.CKind); 3376 } 3377 } 3378 3379 static OpenMPMapClauseKind 3380 getMapClauseKindFromModifier(OpenMPDefaultmapClauseModifier M, 3381 bool IsAggregateOrDeclareTarget) { 3382 OpenMPMapClauseKind Kind = OMPC_MAP_unknown; 3383 switch (M) { 3384 case OMPC_DEFAULTMAP_MODIFIER_alloc: 3385 Kind = OMPC_MAP_alloc; 3386 break; 3387 case OMPC_DEFAULTMAP_MODIFIER_to: 3388 Kind = OMPC_MAP_to; 3389 break; 3390 case OMPC_DEFAULTMAP_MODIFIER_from: 3391 Kind = OMPC_MAP_from; 3392 break; 3393 case OMPC_DEFAULTMAP_MODIFIER_tofrom: 3394 Kind = OMPC_MAP_tofrom; 3395 break; 3396 case OMPC_DEFAULTMAP_MODIFIER_present: 3397 // OpenMP 5.1 [2.21.7.3] defaultmap clause, Description] 3398 // If implicit-behavior is present, each variable referenced in the 3399 // construct in the category specified by variable-category is treated as if 3400 // it had been listed in a map clause with the map-type of alloc and 3401 // map-type-modifier of present. 3402 Kind = OMPC_MAP_alloc; 3403 break; 3404 case OMPC_DEFAULTMAP_MODIFIER_firstprivate: 3405 case OMPC_DEFAULTMAP_MODIFIER_last: 3406 llvm_unreachable("Unexpected defaultmap implicit behavior"); 3407 case OMPC_DEFAULTMAP_MODIFIER_none: 3408 case OMPC_DEFAULTMAP_MODIFIER_default: 3409 case OMPC_DEFAULTMAP_MODIFIER_unknown: 3410 // IsAggregateOrDeclareTarget could be true if: 3411 // 1. the implicit behavior for aggregate is tofrom 3412 // 2. it's a declare target link 3413 if (IsAggregateOrDeclareTarget) { 3414 Kind = OMPC_MAP_tofrom; 3415 break; 3416 } 3417 llvm_unreachable("Unexpected defaultmap implicit behavior"); 3418 } 3419 assert(Kind != OMPC_MAP_unknown && "Expect map kind to be known"); 3420 return Kind; 3421 } 3422 3423 namespace { 3424 class DSAAttrChecker final : public StmtVisitor<DSAAttrChecker, void> { 3425 DSAStackTy *Stack; 3426 Sema &SemaRef; 3427 bool ErrorFound = false; 3428 bool TryCaptureCXXThisMembers = false; 3429 CapturedStmt *CS = nullptr; 3430 const static unsigned DefaultmapKindNum = OMPC_DEFAULTMAP_pointer + 1; 3431 llvm::SmallVector<Expr *, 4> ImplicitFirstprivate; 3432 llvm::SmallVector<Expr *, 4> ImplicitMap[DefaultmapKindNum][OMPC_MAP_delete]; 3433 llvm::SmallVector<OpenMPMapModifierKind, NumberOfOMPMapClauseModifiers> 3434 ImplicitMapModifier[DefaultmapKindNum]; 3435 Sema::VarsWithInheritedDSAType VarsWithInheritedDSA; 3436 llvm::SmallDenseSet<const ValueDecl *, 4> ImplicitDeclarations; 3437 3438 void VisitSubCaptures(OMPExecutableDirective *S) { 3439 // Check implicitly captured variables. 3440 if (!S->hasAssociatedStmt() || !S->getAssociatedStmt()) 3441 return; 3442 if (S->getDirectiveKind() == OMPD_atomic || 3443 S->getDirectiveKind() == OMPD_critical || 3444 S->getDirectiveKind() == OMPD_section || 3445 S->getDirectiveKind() == OMPD_master || 3446 S->getDirectiveKind() == OMPD_masked || 3447 isOpenMPLoopTransformationDirective(S->getDirectiveKind())) { 3448 Visit(S->getAssociatedStmt()); 3449 return; 3450 } 3451 visitSubCaptures(S->getInnermostCapturedStmt()); 3452 // Try to capture inner this->member references to generate correct mappings 3453 // and diagnostics. 3454 if (TryCaptureCXXThisMembers || 3455 (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) && 3456 llvm::any_of(S->getInnermostCapturedStmt()->captures(), 3457 [](const CapturedStmt::Capture &C) { 3458 return C.capturesThis(); 3459 }))) { 3460 bool SavedTryCaptureCXXThisMembers = TryCaptureCXXThisMembers; 3461 TryCaptureCXXThisMembers = true; 3462 Visit(S->getInnermostCapturedStmt()->getCapturedStmt()); 3463 TryCaptureCXXThisMembers = SavedTryCaptureCXXThisMembers; 3464 } 3465 // In tasks firstprivates are not captured anymore, need to analyze them 3466 // explicitly. 3467 if (isOpenMPTaskingDirective(S->getDirectiveKind()) && 3468 !isOpenMPTaskLoopDirective(S->getDirectiveKind())) { 3469 for (OMPClause *C : S->clauses()) 3470 if (auto *FC = dyn_cast<OMPFirstprivateClause>(C)) { 3471 for (Expr *Ref : FC->varlists()) 3472 Visit(Ref); 3473 } 3474 } 3475 } 3476 3477 public: 3478 void VisitDeclRefExpr(DeclRefExpr *E) { 3479 if (TryCaptureCXXThisMembers || E->isTypeDependent() || 3480 E->isValueDependent() || E->containsUnexpandedParameterPack() || 3481 E->isInstantiationDependent()) 3482 return; 3483 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 3484 // Check the datasharing rules for the expressions in the clauses. 3485 if (!CS || (isa<OMPCapturedExprDecl>(VD) && !CS->capturesVariable(VD) && 3486 !Stack->getTopDSA(VD, /*FromParent=*/false).RefExpr)) { 3487 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD)) 3488 if (!CED->hasAttr<OMPCaptureNoInitAttr>()) { 3489 Visit(CED->getInit()); 3490 return; 3491 } 3492 } else if (VD->isImplicit() || isa<OMPCapturedExprDecl>(VD)) 3493 // Do not analyze internal variables and do not enclose them into 3494 // implicit clauses. 3495 return; 3496 VD = VD->getCanonicalDecl(); 3497 // Skip internally declared variables. 3498 if (VD->hasLocalStorage() && CS && !CS->capturesVariable(VD) && 3499 !Stack->isImplicitTaskFirstprivate(VD)) 3500 return; 3501 // Skip allocators in uses_allocators clauses. 3502 if (Stack->isUsesAllocatorsDecl(VD).hasValue()) 3503 return; 3504 3505 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false); 3506 // Check if the variable has explicit DSA set and stop analysis if it so. 3507 if (DVar.RefExpr || !ImplicitDeclarations.insert(VD).second) 3508 return; 3509 3510 // Skip internally declared static variables. 3511 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 3512 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); 3513 if (VD->hasGlobalStorage() && CS && !CS->capturesVariable(VD) && 3514 (Stack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() || 3515 !Res || *Res != OMPDeclareTargetDeclAttr::MT_Link) && 3516 !Stack->isImplicitTaskFirstprivate(VD)) 3517 return; 3518 3519 SourceLocation ELoc = E->getExprLoc(); 3520 OpenMPDirectiveKind DKind = Stack->getCurrentDirective(); 3521 // The default(none) clause requires that each variable that is referenced 3522 // in the construct, and does not have a predetermined data-sharing 3523 // attribute, must have its data-sharing attribute explicitly determined 3524 // by being listed in a data-sharing attribute clause. 3525 if (DVar.CKind == OMPC_unknown && 3526 (Stack->getDefaultDSA() == DSA_none || 3527 Stack->getDefaultDSA() == DSA_firstprivate) && 3528 isImplicitOrExplicitTaskingRegion(DKind) && 3529 VarsWithInheritedDSA.count(VD) == 0) { 3530 bool InheritedDSA = Stack->getDefaultDSA() == DSA_none; 3531 if (!InheritedDSA && Stack->getDefaultDSA() == DSA_firstprivate) { 3532 DSAStackTy::DSAVarData DVar = 3533 Stack->getImplicitDSA(VD, /*FromParent=*/false); 3534 InheritedDSA = DVar.CKind == OMPC_unknown; 3535 } 3536 if (InheritedDSA) 3537 VarsWithInheritedDSA[VD] = E; 3538 return; 3539 } 3540 3541 // OpenMP 5.0 [2.19.7.2, defaultmap clause, Description] 3542 // If implicit-behavior is none, each variable referenced in the 3543 // construct that does not have a predetermined data-sharing attribute 3544 // and does not appear in a to or link clause on a declare target 3545 // directive must be listed in a data-mapping attribute clause, a 3546 // data-haring attribute clause (including a data-sharing attribute 3547 // clause on a combined construct where target. is one of the 3548 // constituent constructs), or an is_device_ptr clause. 3549 OpenMPDefaultmapClauseKind ClauseKind = 3550 getVariableCategoryFromDecl(SemaRef.getLangOpts(), VD); 3551 if (SemaRef.getLangOpts().OpenMP >= 50) { 3552 bool IsModifierNone = Stack->getDefaultmapModifier(ClauseKind) == 3553 OMPC_DEFAULTMAP_MODIFIER_none; 3554 if (DVar.CKind == OMPC_unknown && IsModifierNone && 3555 VarsWithInheritedDSA.count(VD) == 0 && !Res) { 3556 // Only check for data-mapping attribute and is_device_ptr here 3557 // since we have already make sure that the declaration does not 3558 // have a data-sharing attribute above 3559 if (!Stack->checkMappableExprComponentListsForDecl( 3560 VD, /*CurrentRegionOnly=*/true, 3561 [VD](OMPClauseMappableExprCommon::MappableExprComponentListRef 3562 MapExprComponents, 3563 OpenMPClauseKind) { 3564 auto MI = MapExprComponents.rbegin(); 3565 auto ME = MapExprComponents.rend(); 3566 return MI != ME && MI->getAssociatedDeclaration() == VD; 3567 })) { 3568 VarsWithInheritedDSA[VD] = E; 3569 return; 3570 } 3571 } 3572 } 3573 if (SemaRef.getLangOpts().OpenMP > 50) { 3574 bool IsModifierPresent = Stack->getDefaultmapModifier(ClauseKind) == 3575 OMPC_DEFAULTMAP_MODIFIER_present; 3576 if (IsModifierPresent) { 3577 if (llvm::find(ImplicitMapModifier[ClauseKind], 3578 OMPC_MAP_MODIFIER_present) == 3579 std::end(ImplicitMapModifier[ClauseKind])) { 3580 ImplicitMapModifier[ClauseKind].push_back( 3581 OMPC_MAP_MODIFIER_present); 3582 } 3583 } 3584 } 3585 3586 if (isOpenMPTargetExecutionDirective(DKind) && 3587 !Stack->isLoopControlVariable(VD).first) { 3588 if (!Stack->checkMappableExprComponentListsForDecl( 3589 VD, /*CurrentRegionOnly=*/true, 3590 [this](OMPClauseMappableExprCommon::MappableExprComponentListRef 3591 StackComponents, 3592 OpenMPClauseKind) { 3593 if (SemaRef.LangOpts.OpenMP >= 50) 3594 return !StackComponents.empty(); 3595 // Variable is used if it has been marked as an array, array 3596 // section, array shaping or the variable iself. 3597 return StackComponents.size() == 1 || 3598 std::all_of( 3599 std::next(StackComponents.rbegin()), 3600 StackComponents.rend(), 3601 [](const OMPClauseMappableExprCommon:: 3602 MappableComponent &MC) { 3603 return MC.getAssociatedDeclaration() == 3604 nullptr && 3605 (isa<OMPArraySectionExpr>( 3606 MC.getAssociatedExpression()) || 3607 isa<OMPArrayShapingExpr>( 3608 MC.getAssociatedExpression()) || 3609 isa<ArraySubscriptExpr>( 3610 MC.getAssociatedExpression())); 3611 }); 3612 })) { 3613 bool IsFirstprivate = false; 3614 // By default lambdas are captured as firstprivates. 3615 if (const auto *RD = 3616 VD->getType().getNonReferenceType()->getAsCXXRecordDecl()) 3617 IsFirstprivate = RD->isLambda(); 3618 IsFirstprivate = 3619 IsFirstprivate || (Stack->mustBeFirstprivate(ClauseKind) && !Res); 3620 if (IsFirstprivate) { 3621 ImplicitFirstprivate.emplace_back(E); 3622 } else { 3623 OpenMPDefaultmapClauseModifier M = 3624 Stack->getDefaultmapModifier(ClauseKind); 3625 OpenMPMapClauseKind Kind = getMapClauseKindFromModifier( 3626 M, ClauseKind == OMPC_DEFAULTMAP_aggregate || Res); 3627 ImplicitMap[ClauseKind][Kind].emplace_back(E); 3628 } 3629 return; 3630 } 3631 } 3632 3633 // OpenMP [2.9.3.6, Restrictions, p.2] 3634 // A list item that appears in a reduction clause of the innermost 3635 // enclosing worksharing or parallel construct may not be accessed in an 3636 // explicit task. 3637 DVar = Stack->hasInnermostDSA( 3638 VD, 3639 [](OpenMPClauseKind C, bool AppliedToPointee) { 3640 return C == OMPC_reduction && !AppliedToPointee; 3641 }, 3642 [](OpenMPDirectiveKind K) { 3643 return isOpenMPParallelDirective(K) || 3644 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K); 3645 }, 3646 /*FromParent=*/true); 3647 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) { 3648 ErrorFound = true; 3649 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); 3650 reportOriginalDsa(SemaRef, Stack, VD, DVar); 3651 return; 3652 } 3653 3654 // Define implicit data-sharing attributes for task. 3655 DVar = Stack->getImplicitDSA(VD, /*FromParent=*/false); 3656 if (((isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared) || 3657 (Stack->getDefaultDSA() == DSA_firstprivate && 3658 DVar.CKind == OMPC_firstprivate && !DVar.RefExpr)) && 3659 !Stack->isLoopControlVariable(VD).first) { 3660 ImplicitFirstprivate.push_back(E); 3661 return; 3662 } 3663 3664 // Store implicitly used globals with declare target link for parent 3665 // target. 3666 if (!isOpenMPTargetExecutionDirective(DKind) && Res && 3667 *Res == OMPDeclareTargetDeclAttr::MT_Link) { 3668 Stack->addToParentTargetRegionLinkGlobals(E); 3669 return; 3670 } 3671 } 3672 } 3673 void VisitMemberExpr(MemberExpr *E) { 3674 if (E->isTypeDependent() || E->isValueDependent() || 3675 E->containsUnexpandedParameterPack() || E->isInstantiationDependent()) 3676 return; 3677 auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); 3678 OpenMPDirectiveKind DKind = Stack->getCurrentDirective(); 3679 if (auto *TE = dyn_cast<CXXThisExpr>(E->getBase()->IgnoreParenCasts())) { 3680 if (!FD) 3681 return; 3682 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(FD, /*FromParent=*/false); 3683 // Check if the variable has explicit DSA set and stop analysis if it 3684 // so. 3685 if (DVar.RefExpr || !ImplicitDeclarations.insert(FD).second) 3686 return; 3687 3688 if (isOpenMPTargetExecutionDirective(DKind) && 3689 !Stack->isLoopControlVariable(FD).first && 3690 !Stack->checkMappableExprComponentListsForDecl( 3691 FD, /*CurrentRegionOnly=*/true, 3692 [](OMPClauseMappableExprCommon::MappableExprComponentListRef 3693 StackComponents, 3694 OpenMPClauseKind) { 3695 return isa<CXXThisExpr>( 3696 cast<MemberExpr>( 3697 StackComponents.back().getAssociatedExpression()) 3698 ->getBase() 3699 ->IgnoreParens()); 3700 })) { 3701 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3] 3702 // A bit-field cannot appear in a map clause. 3703 // 3704 if (FD->isBitField()) 3705 return; 3706 3707 // Check to see if the member expression is referencing a class that 3708 // has already been explicitly mapped 3709 if (Stack->isClassPreviouslyMapped(TE->getType())) 3710 return; 3711 3712 OpenMPDefaultmapClauseModifier Modifier = 3713 Stack->getDefaultmapModifier(OMPC_DEFAULTMAP_aggregate); 3714 OpenMPDefaultmapClauseKind ClauseKind = 3715 getVariableCategoryFromDecl(SemaRef.getLangOpts(), FD); 3716 OpenMPMapClauseKind Kind = getMapClauseKindFromModifier( 3717 Modifier, /*IsAggregateOrDeclareTarget*/ true); 3718 ImplicitMap[ClauseKind][Kind].emplace_back(E); 3719 return; 3720 } 3721 3722 SourceLocation ELoc = E->getExprLoc(); 3723 // OpenMP [2.9.3.6, Restrictions, p.2] 3724 // A list item that appears in a reduction clause of the innermost 3725 // enclosing worksharing or parallel construct may not be accessed in 3726 // an explicit task. 3727 DVar = Stack->hasInnermostDSA( 3728 FD, 3729 [](OpenMPClauseKind C, bool AppliedToPointee) { 3730 return C == OMPC_reduction && !AppliedToPointee; 3731 }, 3732 [](OpenMPDirectiveKind K) { 3733 return isOpenMPParallelDirective(K) || 3734 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K); 3735 }, 3736 /*FromParent=*/true); 3737 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) { 3738 ErrorFound = true; 3739 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); 3740 reportOriginalDsa(SemaRef, Stack, FD, DVar); 3741 return; 3742 } 3743 3744 // Define implicit data-sharing attributes for task. 3745 DVar = Stack->getImplicitDSA(FD, /*FromParent=*/false); 3746 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared && 3747 !Stack->isLoopControlVariable(FD).first) { 3748 // Check if there is a captured expression for the current field in the 3749 // region. Do not mark it as firstprivate unless there is no captured 3750 // expression. 3751 // TODO: try to make it firstprivate. 3752 if (DVar.CKind != OMPC_unknown) 3753 ImplicitFirstprivate.push_back(E); 3754 } 3755 return; 3756 } 3757 if (isOpenMPTargetExecutionDirective(DKind)) { 3758 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents; 3759 if (!checkMapClauseExpressionBase(SemaRef, E, CurComponents, OMPC_map, 3760 Stack->getCurrentDirective(), 3761 /*NoDiagnose=*/true)) 3762 return; 3763 const auto *VD = cast<ValueDecl>( 3764 CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl()); 3765 if (!Stack->checkMappableExprComponentListsForDecl( 3766 VD, /*CurrentRegionOnly=*/true, 3767 [&CurComponents]( 3768 OMPClauseMappableExprCommon::MappableExprComponentListRef 3769 StackComponents, 3770 OpenMPClauseKind) { 3771 auto CCI = CurComponents.rbegin(); 3772 auto CCE = CurComponents.rend(); 3773 for (const auto &SC : llvm::reverse(StackComponents)) { 3774 // Do both expressions have the same kind? 3775 if (CCI->getAssociatedExpression()->getStmtClass() != 3776 SC.getAssociatedExpression()->getStmtClass()) 3777 if (!((isa<OMPArraySectionExpr>( 3778 SC.getAssociatedExpression()) || 3779 isa<OMPArrayShapingExpr>( 3780 SC.getAssociatedExpression())) && 3781 isa<ArraySubscriptExpr>( 3782 CCI->getAssociatedExpression()))) 3783 return false; 3784 3785 const Decl *CCD = CCI->getAssociatedDeclaration(); 3786 const Decl *SCD = SC.getAssociatedDeclaration(); 3787 CCD = CCD ? CCD->getCanonicalDecl() : nullptr; 3788 SCD = SCD ? SCD->getCanonicalDecl() : nullptr; 3789 if (SCD != CCD) 3790 return false; 3791 std::advance(CCI, 1); 3792 if (CCI == CCE) 3793 break; 3794 } 3795 return true; 3796 })) { 3797 Visit(E->getBase()); 3798 } 3799 } else if (!TryCaptureCXXThisMembers) { 3800 Visit(E->getBase()); 3801 } 3802 } 3803 void VisitOMPExecutableDirective(OMPExecutableDirective *S) { 3804 for (OMPClause *C : S->clauses()) { 3805 // Skip analysis of arguments of private clauses for task|target 3806 // directives. 3807 if (isa_and_nonnull<OMPPrivateClause>(C)) 3808 continue; 3809 // Skip analysis of arguments of implicitly defined firstprivate clause 3810 // for task|target directives. 3811 // Skip analysis of arguments of implicitly defined map clause for target 3812 // directives. 3813 if (C && !((isa<OMPFirstprivateClause>(C) || isa<OMPMapClause>(C)) && 3814 C->isImplicit() && 3815 !isOpenMPTaskingDirective(Stack->getCurrentDirective()))) { 3816 for (Stmt *CC : C->children()) { 3817 if (CC) 3818 Visit(CC); 3819 } 3820 } 3821 } 3822 // Check implicitly captured variables. 3823 VisitSubCaptures(S); 3824 } 3825 3826 void VisitOMPLoopTransformationDirective(OMPLoopTransformationDirective *S) { 3827 // Loop transformation directives do not introduce data sharing 3828 VisitStmt(S); 3829 } 3830 3831 void VisitCallExpr(CallExpr *S) { 3832 for (Stmt *C : S->arguments()) { 3833 if (C) { 3834 // Check implicitly captured variables in the task-based directives to 3835 // check if they must be firstprivatized. 3836 Visit(C); 3837 } 3838 } 3839 if (Expr *Callee = S->getCallee()) 3840 if (auto *CE = dyn_cast<MemberExpr>(Callee->IgnoreParenImpCasts())) 3841 Visit(CE->getBase()); 3842 } 3843 void VisitStmt(Stmt *S) { 3844 for (Stmt *C : S->children()) { 3845 if (C) { 3846 // Check implicitly captured variables in the task-based directives to 3847 // check if they must be firstprivatized. 3848 Visit(C); 3849 } 3850 } 3851 } 3852 3853 void visitSubCaptures(CapturedStmt *S) { 3854 for (const CapturedStmt::Capture &Cap : S->captures()) { 3855 if (!Cap.capturesVariable() && !Cap.capturesVariableByCopy()) 3856 continue; 3857 VarDecl *VD = Cap.getCapturedVar(); 3858 // Do not try to map the variable if it or its sub-component was mapped 3859 // already. 3860 if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) && 3861 Stack->checkMappableExprComponentListsForDecl( 3862 VD, /*CurrentRegionOnly=*/true, 3863 [](OMPClauseMappableExprCommon::MappableExprComponentListRef, 3864 OpenMPClauseKind) { return true; })) 3865 continue; 3866 DeclRefExpr *DRE = buildDeclRefExpr( 3867 SemaRef, VD, VD->getType().getNonLValueExprType(SemaRef.Context), 3868 Cap.getLocation(), /*RefersToCapture=*/true); 3869 Visit(DRE); 3870 } 3871 } 3872 bool isErrorFound() const { return ErrorFound; } 3873 ArrayRef<Expr *> getImplicitFirstprivate() const { 3874 return ImplicitFirstprivate; 3875 } 3876 ArrayRef<Expr *> getImplicitMap(OpenMPDefaultmapClauseKind DK, 3877 OpenMPMapClauseKind MK) const { 3878 return ImplicitMap[DK][MK]; 3879 } 3880 ArrayRef<OpenMPMapModifierKind> 3881 getImplicitMapModifier(OpenMPDefaultmapClauseKind Kind) const { 3882 return ImplicitMapModifier[Kind]; 3883 } 3884 const Sema::VarsWithInheritedDSAType &getVarsWithInheritedDSA() const { 3885 return VarsWithInheritedDSA; 3886 } 3887 3888 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS) 3889 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) { 3890 // Process declare target link variables for the target directives. 3891 if (isOpenMPTargetExecutionDirective(S->getCurrentDirective())) { 3892 for (DeclRefExpr *E : Stack->getLinkGlobals()) 3893 Visit(E); 3894 } 3895 } 3896 }; 3897 } // namespace 3898 3899 static void handleDeclareVariantConstructTrait(DSAStackTy *Stack, 3900 OpenMPDirectiveKind DKind, 3901 bool ScopeEntry) { 3902 SmallVector<llvm::omp::TraitProperty, 8> Traits; 3903 if (isOpenMPTargetExecutionDirective(DKind)) 3904 Traits.emplace_back(llvm::omp::TraitProperty::construct_target_target); 3905 if (isOpenMPTeamsDirective(DKind)) 3906 Traits.emplace_back(llvm::omp::TraitProperty::construct_teams_teams); 3907 if (isOpenMPParallelDirective(DKind)) 3908 Traits.emplace_back(llvm::omp::TraitProperty::construct_parallel_parallel); 3909 if (isOpenMPWorksharingDirective(DKind)) 3910 Traits.emplace_back(llvm::omp::TraitProperty::construct_for_for); 3911 if (isOpenMPSimdDirective(DKind)) 3912 Traits.emplace_back(llvm::omp::TraitProperty::construct_simd_simd); 3913 Stack->handleConstructTrait(Traits, ScopeEntry); 3914 } 3915 3916 void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) { 3917 switch (DKind) { 3918 case OMPD_parallel: 3919 case OMPD_parallel_for: 3920 case OMPD_parallel_for_simd: 3921 case OMPD_parallel_sections: 3922 case OMPD_parallel_master: 3923 case OMPD_teams: 3924 case OMPD_teams_distribute: 3925 case OMPD_teams_distribute_simd: { 3926 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3927 QualType KmpInt32PtrTy = 3928 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3929 Sema::CapturedParamNameType Params[] = { 3930 std::make_pair(".global_tid.", KmpInt32PtrTy), 3931 std::make_pair(".bound_tid.", KmpInt32PtrTy), 3932 std::make_pair(StringRef(), QualType()) // __context with shared vars 3933 }; 3934 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3935 Params); 3936 break; 3937 } 3938 case OMPD_target_teams: 3939 case OMPD_target_parallel: 3940 case OMPD_target_parallel_for: 3941 case OMPD_target_parallel_for_simd: 3942 case OMPD_target_teams_distribute: 3943 case OMPD_target_teams_distribute_simd: { 3944 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3945 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 3946 QualType KmpInt32PtrTy = 3947 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3948 QualType Args[] = {VoidPtrTy}; 3949 FunctionProtoType::ExtProtoInfo EPI; 3950 EPI.Variadic = true; 3951 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 3952 Sema::CapturedParamNameType Params[] = { 3953 std::make_pair(".global_tid.", KmpInt32Ty), 3954 std::make_pair(".part_id.", KmpInt32PtrTy), 3955 std::make_pair(".privates.", VoidPtrTy), 3956 std::make_pair( 3957 ".copy_fn.", 3958 Context.getPointerType(CopyFnType).withConst().withRestrict()), 3959 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 3960 std::make_pair(StringRef(), QualType()) // __context with shared vars 3961 }; 3962 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3963 Params, /*OpenMPCaptureLevel=*/0); 3964 // Mark this captured region as inlined, because we don't use outlined 3965 // function directly. 3966 getCurCapturedRegion()->TheCapturedDecl->addAttr( 3967 AlwaysInlineAttr::CreateImplicit( 3968 Context, {}, AttributeCommonInfo::AS_Keyword, 3969 AlwaysInlineAttr::Keyword_forceinline)); 3970 Sema::CapturedParamNameType ParamsTarget[] = { 3971 std::make_pair(StringRef(), QualType()) // __context with shared vars 3972 }; 3973 // Start a captured region for 'target' with no implicit parameters. 3974 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3975 ParamsTarget, /*OpenMPCaptureLevel=*/1); 3976 Sema::CapturedParamNameType ParamsTeamsOrParallel[] = { 3977 std::make_pair(".global_tid.", KmpInt32PtrTy), 3978 std::make_pair(".bound_tid.", KmpInt32PtrTy), 3979 std::make_pair(StringRef(), QualType()) // __context with shared vars 3980 }; 3981 // Start a captured region for 'teams' or 'parallel'. Both regions have 3982 // the same implicit parameters. 3983 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3984 ParamsTeamsOrParallel, /*OpenMPCaptureLevel=*/2); 3985 break; 3986 } 3987 case OMPD_target: 3988 case OMPD_target_simd: { 3989 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3990 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 3991 QualType KmpInt32PtrTy = 3992 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3993 QualType Args[] = {VoidPtrTy}; 3994 FunctionProtoType::ExtProtoInfo EPI; 3995 EPI.Variadic = true; 3996 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 3997 Sema::CapturedParamNameType Params[] = { 3998 std::make_pair(".global_tid.", KmpInt32Ty), 3999 std::make_pair(".part_id.", KmpInt32PtrTy), 4000 std::make_pair(".privates.", VoidPtrTy), 4001 std::make_pair( 4002 ".copy_fn.", 4003 Context.getPointerType(CopyFnType).withConst().withRestrict()), 4004 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 4005 std::make_pair(StringRef(), QualType()) // __context with shared vars 4006 }; 4007 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4008 Params, /*OpenMPCaptureLevel=*/0); 4009 // Mark this captured region as inlined, because we don't use outlined 4010 // function directly. 4011 getCurCapturedRegion()->TheCapturedDecl->addAttr( 4012 AlwaysInlineAttr::CreateImplicit( 4013 Context, {}, AttributeCommonInfo::AS_Keyword, 4014 AlwaysInlineAttr::Keyword_forceinline)); 4015 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4016 std::make_pair(StringRef(), QualType()), 4017 /*OpenMPCaptureLevel=*/1); 4018 break; 4019 } 4020 case OMPD_atomic: 4021 case OMPD_critical: 4022 case OMPD_section: 4023 case OMPD_master: 4024 case OMPD_masked: 4025 case OMPD_tile: 4026 case OMPD_unroll: 4027 break; 4028 case OMPD_loop: 4029 // TODO: 'loop' may require additional parameters depending on the binding. 4030 // Treat similar to OMPD_simd/OMPD_for for now. 4031 case OMPD_simd: 4032 case OMPD_for: 4033 case OMPD_for_simd: 4034 case OMPD_sections: 4035 case OMPD_single: 4036 case OMPD_taskgroup: 4037 case OMPD_distribute: 4038 case OMPD_distribute_simd: 4039 case OMPD_ordered: 4040 case OMPD_target_data: 4041 case OMPD_dispatch: { 4042 Sema::CapturedParamNameType Params[] = { 4043 std::make_pair(StringRef(), QualType()) // __context with shared vars 4044 }; 4045 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4046 Params); 4047 break; 4048 } 4049 case OMPD_task: { 4050 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 4051 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 4052 QualType KmpInt32PtrTy = 4053 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4054 QualType Args[] = {VoidPtrTy}; 4055 FunctionProtoType::ExtProtoInfo EPI; 4056 EPI.Variadic = true; 4057 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 4058 Sema::CapturedParamNameType Params[] = { 4059 std::make_pair(".global_tid.", KmpInt32Ty), 4060 std::make_pair(".part_id.", KmpInt32PtrTy), 4061 std::make_pair(".privates.", VoidPtrTy), 4062 std::make_pair( 4063 ".copy_fn.", 4064 Context.getPointerType(CopyFnType).withConst().withRestrict()), 4065 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 4066 std::make_pair(StringRef(), QualType()) // __context with shared vars 4067 }; 4068 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4069 Params); 4070 // Mark this captured region as inlined, because we don't use outlined 4071 // function directly. 4072 getCurCapturedRegion()->TheCapturedDecl->addAttr( 4073 AlwaysInlineAttr::CreateImplicit( 4074 Context, {}, AttributeCommonInfo::AS_Keyword, 4075 AlwaysInlineAttr::Keyword_forceinline)); 4076 break; 4077 } 4078 case OMPD_taskloop: 4079 case OMPD_taskloop_simd: 4080 case OMPD_master_taskloop: 4081 case OMPD_master_taskloop_simd: { 4082 QualType KmpInt32Ty = 4083 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1) 4084 .withConst(); 4085 QualType KmpUInt64Ty = 4086 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0) 4087 .withConst(); 4088 QualType KmpInt64Ty = 4089 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1) 4090 .withConst(); 4091 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 4092 QualType KmpInt32PtrTy = 4093 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4094 QualType Args[] = {VoidPtrTy}; 4095 FunctionProtoType::ExtProtoInfo EPI; 4096 EPI.Variadic = true; 4097 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 4098 Sema::CapturedParamNameType Params[] = { 4099 std::make_pair(".global_tid.", KmpInt32Ty), 4100 std::make_pair(".part_id.", KmpInt32PtrTy), 4101 std::make_pair(".privates.", VoidPtrTy), 4102 std::make_pair( 4103 ".copy_fn.", 4104 Context.getPointerType(CopyFnType).withConst().withRestrict()), 4105 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 4106 std::make_pair(".lb.", KmpUInt64Ty), 4107 std::make_pair(".ub.", KmpUInt64Ty), 4108 std::make_pair(".st.", KmpInt64Ty), 4109 std::make_pair(".liter.", KmpInt32Ty), 4110 std::make_pair(".reductions.", VoidPtrTy), 4111 std::make_pair(StringRef(), QualType()) // __context with shared vars 4112 }; 4113 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4114 Params); 4115 // Mark this captured region as inlined, because we don't use outlined 4116 // function directly. 4117 getCurCapturedRegion()->TheCapturedDecl->addAttr( 4118 AlwaysInlineAttr::CreateImplicit( 4119 Context, {}, AttributeCommonInfo::AS_Keyword, 4120 AlwaysInlineAttr::Keyword_forceinline)); 4121 break; 4122 } 4123 case OMPD_parallel_master_taskloop: 4124 case OMPD_parallel_master_taskloop_simd: { 4125 QualType KmpInt32Ty = 4126 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1) 4127 .withConst(); 4128 QualType KmpUInt64Ty = 4129 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0) 4130 .withConst(); 4131 QualType KmpInt64Ty = 4132 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1) 4133 .withConst(); 4134 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 4135 QualType KmpInt32PtrTy = 4136 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4137 Sema::CapturedParamNameType ParamsParallel[] = { 4138 std::make_pair(".global_tid.", KmpInt32PtrTy), 4139 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4140 std::make_pair(StringRef(), QualType()) // __context with shared vars 4141 }; 4142 // Start a captured region for 'parallel'. 4143 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4144 ParamsParallel, /*OpenMPCaptureLevel=*/0); 4145 QualType Args[] = {VoidPtrTy}; 4146 FunctionProtoType::ExtProtoInfo EPI; 4147 EPI.Variadic = true; 4148 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 4149 Sema::CapturedParamNameType Params[] = { 4150 std::make_pair(".global_tid.", KmpInt32Ty), 4151 std::make_pair(".part_id.", KmpInt32PtrTy), 4152 std::make_pair(".privates.", VoidPtrTy), 4153 std::make_pair( 4154 ".copy_fn.", 4155 Context.getPointerType(CopyFnType).withConst().withRestrict()), 4156 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 4157 std::make_pair(".lb.", KmpUInt64Ty), 4158 std::make_pair(".ub.", KmpUInt64Ty), 4159 std::make_pair(".st.", KmpInt64Ty), 4160 std::make_pair(".liter.", KmpInt32Ty), 4161 std::make_pair(".reductions.", VoidPtrTy), 4162 std::make_pair(StringRef(), QualType()) // __context with shared vars 4163 }; 4164 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4165 Params, /*OpenMPCaptureLevel=*/1); 4166 // Mark this captured region as inlined, because we don't use outlined 4167 // function directly. 4168 getCurCapturedRegion()->TheCapturedDecl->addAttr( 4169 AlwaysInlineAttr::CreateImplicit( 4170 Context, {}, AttributeCommonInfo::AS_Keyword, 4171 AlwaysInlineAttr::Keyword_forceinline)); 4172 break; 4173 } 4174 case OMPD_distribute_parallel_for_simd: 4175 case OMPD_distribute_parallel_for: { 4176 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 4177 QualType KmpInt32PtrTy = 4178 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4179 Sema::CapturedParamNameType Params[] = { 4180 std::make_pair(".global_tid.", KmpInt32PtrTy), 4181 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4182 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 4183 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 4184 std::make_pair(StringRef(), QualType()) // __context with shared vars 4185 }; 4186 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4187 Params); 4188 break; 4189 } 4190 case OMPD_target_teams_distribute_parallel_for: 4191 case OMPD_target_teams_distribute_parallel_for_simd: { 4192 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 4193 QualType KmpInt32PtrTy = 4194 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4195 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 4196 4197 QualType Args[] = {VoidPtrTy}; 4198 FunctionProtoType::ExtProtoInfo EPI; 4199 EPI.Variadic = true; 4200 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 4201 Sema::CapturedParamNameType Params[] = { 4202 std::make_pair(".global_tid.", KmpInt32Ty), 4203 std::make_pair(".part_id.", KmpInt32PtrTy), 4204 std::make_pair(".privates.", VoidPtrTy), 4205 std::make_pair( 4206 ".copy_fn.", 4207 Context.getPointerType(CopyFnType).withConst().withRestrict()), 4208 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 4209 std::make_pair(StringRef(), QualType()) // __context with shared vars 4210 }; 4211 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4212 Params, /*OpenMPCaptureLevel=*/0); 4213 // Mark this captured region as inlined, because we don't use outlined 4214 // function directly. 4215 getCurCapturedRegion()->TheCapturedDecl->addAttr( 4216 AlwaysInlineAttr::CreateImplicit( 4217 Context, {}, AttributeCommonInfo::AS_Keyword, 4218 AlwaysInlineAttr::Keyword_forceinline)); 4219 Sema::CapturedParamNameType ParamsTarget[] = { 4220 std::make_pair(StringRef(), QualType()) // __context with shared vars 4221 }; 4222 // Start a captured region for 'target' with no implicit parameters. 4223 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4224 ParamsTarget, /*OpenMPCaptureLevel=*/1); 4225 4226 Sema::CapturedParamNameType ParamsTeams[] = { 4227 std::make_pair(".global_tid.", KmpInt32PtrTy), 4228 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4229 std::make_pair(StringRef(), QualType()) // __context with shared vars 4230 }; 4231 // Start a captured region for 'target' with no implicit parameters. 4232 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4233 ParamsTeams, /*OpenMPCaptureLevel=*/2); 4234 4235 Sema::CapturedParamNameType ParamsParallel[] = { 4236 std::make_pair(".global_tid.", KmpInt32PtrTy), 4237 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4238 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 4239 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 4240 std::make_pair(StringRef(), QualType()) // __context with shared vars 4241 }; 4242 // Start a captured region for 'teams' or 'parallel'. Both regions have 4243 // the same implicit parameters. 4244 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4245 ParamsParallel, /*OpenMPCaptureLevel=*/3); 4246 break; 4247 } 4248 4249 case OMPD_teams_distribute_parallel_for: 4250 case OMPD_teams_distribute_parallel_for_simd: { 4251 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 4252 QualType KmpInt32PtrTy = 4253 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4254 4255 Sema::CapturedParamNameType ParamsTeams[] = { 4256 std::make_pair(".global_tid.", KmpInt32PtrTy), 4257 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4258 std::make_pair(StringRef(), QualType()) // __context with shared vars 4259 }; 4260 // Start a captured region for 'target' with no implicit parameters. 4261 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4262 ParamsTeams, /*OpenMPCaptureLevel=*/0); 4263 4264 Sema::CapturedParamNameType ParamsParallel[] = { 4265 std::make_pair(".global_tid.", KmpInt32PtrTy), 4266 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4267 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 4268 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 4269 std::make_pair(StringRef(), QualType()) // __context with shared vars 4270 }; 4271 // Start a captured region for 'teams' or 'parallel'. Both regions have 4272 // the same implicit parameters. 4273 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4274 ParamsParallel, /*OpenMPCaptureLevel=*/1); 4275 break; 4276 } 4277 case OMPD_target_update: 4278 case OMPD_target_enter_data: 4279 case OMPD_target_exit_data: { 4280 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 4281 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 4282 QualType KmpInt32PtrTy = 4283 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4284 QualType Args[] = {VoidPtrTy}; 4285 FunctionProtoType::ExtProtoInfo EPI; 4286 EPI.Variadic = true; 4287 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 4288 Sema::CapturedParamNameType Params[] = { 4289 std::make_pair(".global_tid.", KmpInt32Ty), 4290 std::make_pair(".part_id.", KmpInt32PtrTy), 4291 std::make_pair(".privates.", VoidPtrTy), 4292 std::make_pair( 4293 ".copy_fn.", 4294 Context.getPointerType(CopyFnType).withConst().withRestrict()), 4295 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 4296 std::make_pair(StringRef(), QualType()) // __context with shared vars 4297 }; 4298 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4299 Params); 4300 // Mark this captured region as inlined, because we don't use outlined 4301 // function directly. 4302 getCurCapturedRegion()->TheCapturedDecl->addAttr( 4303 AlwaysInlineAttr::CreateImplicit( 4304 Context, {}, AttributeCommonInfo::AS_Keyword, 4305 AlwaysInlineAttr::Keyword_forceinline)); 4306 break; 4307 } 4308 case OMPD_threadprivate: 4309 case OMPD_allocate: 4310 case OMPD_taskyield: 4311 case OMPD_barrier: 4312 case OMPD_taskwait: 4313 case OMPD_cancellation_point: 4314 case OMPD_cancel: 4315 case OMPD_flush: 4316 case OMPD_depobj: 4317 case OMPD_scan: 4318 case OMPD_declare_reduction: 4319 case OMPD_declare_mapper: 4320 case OMPD_declare_simd: 4321 case OMPD_declare_target: 4322 case OMPD_end_declare_target: 4323 case OMPD_requires: 4324 case OMPD_declare_variant: 4325 case OMPD_begin_declare_variant: 4326 case OMPD_end_declare_variant: 4327 case OMPD_metadirective: 4328 llvm_unreachable("OpenMP Directive is not allowed"); 4329 case OMPD_unknown: 4330 default: 4331 llvm_unreachable("Unknown OpenMP directive"); 4332 } 4333 DSAStack->setContext(CurContext); 4334 handleDeclareVariantConstructTrait(DSAStack, DKind, /* ScopeEntry */ true); 4335 } 4336 4337 int Sema::getNumberOfConstructScopes(unsigned Level) const { 4338 return getOpenMPCaptureLevels(DSAStack->getDirective(Level)); 4339 } 4340 4341 int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) { 4342 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 4343 getOpenMPCaptureRegions(CaptureRegions, DKind); 4344 return CaptureRegions.size(); 4345 } 4346 4347 static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id, 4348 Expr *CaptureExpr, bool WithInit, 4349 bool AsExpression) { 4350 assert(CaptureExpr); 4351 ASTContext &C = S.getASTContext(); 4352 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts(); 4353 QualType Ty = Init->getType(); 4354 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) { 4355 if (S.getLangOpts().CPlusPlus) { 4356 Ty = C.getLValueReferenceType(Ty); 4357 } else { 4358 Ty = C.getPointerType(Ty); 4359 ExprResult Res = 4360 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init); 4361 if (!Res.isUsable()) 4362 return nullptr; 4363 Init = Res.get(); 4364 } 4365 WithInit = true; 4366 } 4367 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty, 4368 CaptureExpr->getBeginLoc()); 4369 if (!WithInit) 4370 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C)); 4371 S.CurContext->addHiddenDecl(CED); 4372 Sema::TentativeAnalysisScope Trap(S); 4373 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false); 4374 return CED; 4375 } 4376 4377 static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr, 4378 bool WithInit) { 4379 OMPCapturedExprDecl *CD; 4380 if (VarDecl *VD = S.isOpenMPCapturedDecl(D)) 4381 CD = cast<OMPCapturedExprDecl>(VD); 4382 else 4383 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit, 4384 /*AsExpression=*/false); 4385 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(), 4386 CaptureExpr->getExprLoc()); 4387 } 4388 4389 static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) { 4390 CaptureExpr = S.DefaultLvalueConversion(CaptureExpr).get(); 4391 if (!Ref) { 4392 OMPCapturedExprDecl *CD = buildCaptureDecl( 4393 S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr, 4394 /*WithInit=*/true, /*AsExpression=*/true); 4395 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(), 4396 CaptureExpr->getExprLoc()); 4397 } 4398 ExprResult Res = Ref; 4399 if (!S.getLangOpts().CPlusPlus && 4400 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() && 4401 Ref->getType()->isPointerType()) { 4402 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref); 4403 if (!Res.isUsable()) 4404 return ExprError(); 4405 } 4406 return S.DefaultLvalueConversion(Res.get()); 4407 } 4408 4409 namespace { 4410 // OpenMP directives parsed in this section are represented as a 4411 // CapturedStatement with an associated statement. If a syntax error 4412 // is detected during the parsing of the associated statement, the 4413 // compiler must abort processing and close the CapturedStatement. 4414 // 4415 // Combined directives such as 'target parallel' have more than one 4416 // nested CapturedStatements. This RAII ensures that we unwind out 4417 // of all the nested CapturedStatements when an error is found. 4418 class CaptureRegionUnwinderRAII { 4419 private: 4420 Sema &S; 4421 bool &ErrorFound; 4422 OpenMPDirectiveKind DKind = OMPD_unknown; 4423 4424 public: 4425 CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound, 4426 OpenMPDirectiveKind DKind) 4427 : S(S), ErrorFound(ErrorFound), DKind(DKind) {} 4428 ~CaptureRegionUnwinderRAII() { 4429 if (ErrorFound) { 4430 int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind); 4431 while (--ThisCaptureLevel >= 0) 4432 S.ActOnCapturedRegionError(); 4433 } 4434 } 4435 }; 4436 } // namespace 4437 4438 void Sema::tryCaptureOpenMPLambdas(ValueDecl *V) { 4439 // Capture variables captured by reference in lambdas for target-based 4440 // directives. 4441 if (!CurContext->isDependentContext() && 4442 (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) || 4443 isOpenMPTargetDataManagementDirective( 4444 DSAStack->getCurrentDirective()))) { 4445 QualType Type = V->getType(); 4446 if (const auto *RD = Type.getCanonicalType() 4447 .getNonReferenceType() 4448 ->getAsCXXRecordDecl()) { 4449 bool SavedForceCaptureByReferenceInTargetExecutable = 4450 DSAStack->isForceCaptureByReferenceInTargetExecutable(); 4451 DSAStack->setForceCaptureByReferenceInTargetExecutable( 4452 /*V=*/true); 4453 if (RD->isLambda()) { 4454 llvm::DenseMap<const VarDecl *, FieldDecl *> Captures; 4455 FieldDecl *ThisCapture; 4456 RD->getCaptureFields(Captures, ThisCapture); 4457 for (const LambdaCapture &LC : RD->captures()) { 4458 if (LC.getCaptureKind() == LCK_ByRef) { 4459 VarDecl *VD = LC.getCapturedVar(); 4460 DeclContext *VDC = VD->getDeclContext(); 4461 if (!VDC->Encloses(CurContext)) 4462 continue; 4463 MarkVariableReferenced(LC.getLocation(), VD); 4464 } else if (LC.getCaptureKind() == LCK_This) { 4465 QualType ThisTy = getCurrentThisType(); 4466 if (!ThisTy.isNull() && 4467 Context.typesAreCompatible(ThisTy, ThisCapture->getType())) 4468 CheckCXXThisCapture(LC.getLocation()); 4469 } 4470 } 4471 } 4472 DSAStack->setForceCaptureByReferenceInTargetExecutable( 4473 SavedForceCaptureByReferenceInTargetExecutable); 4474 } 4475 } 4476 } 4477 4478 static bool checkOrderedOrderSpecified(Sema &S, 4479 const ArrayRef<OMPClause *> Clauses) { 4480 const OMPOrderedClause *Ordered = nullptr; 4481 const OMPOrderClause *Order = nullptr; 4482 4483 for (const OMPClause *Clause : Clauses) { 4484 if (Clause->getClauseKind() == OMPC_ordered) 4485 Ordered = cast<OMPOrderedClause>(Clause); 4486 else if (Clause->getClauseKind() == OMPC_order) { 4487 Order = cast<OMPOrderClause>(Clause); 4488 if (Order->getKind() != OMPC_ORDER_concurrent) 4489 Order = nullptr; 4490 } 4491 if (Ordered && Order) 4492 break; 4493 } 4494 4495 if (Ordered && Order) { 4496 S.Diag(Order->getKindKwLoc(), 4497 diag::err_omp_simple_clause_incompatible_with_ordered) 4498 << getOpenMPClauseName(OMPC_order) 4499 << getOpenMPSimpleClauseTypeName(OMPC_order, OMPC_ORDER_concurrent) 4500 << SourceRange(Order->getBeginLoc(), Order->getEndLoc()); 4501 S.Diag(Ordered->getBeginLoc(), diag::note_omp_ordered_param) 4502 << 0 << SourceRange(Ordered->getBeginLoc(), Ordered->getEndLoc()); 4503 return true; 4504 } 4505 return false; 4506 } 4507 4508 StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S, 4509 ArrayRef<OMPClause *> Clauses) { 4510 handleDeclareVariantConstructTrait(DSAStack, DSAStack->getCurrentDirective(), 4511 /* ScopeEntry */ false); 4512 if (DSAStack->getCurrentDirective() == OMPD_atomic || 4513 DSAStack->getCurrentDirective() == OMPD_critical || 4514 DSAStack->getCurrentDirective() == OMPD_section || 4515 DSAStack->getCurrentDirective() == OMPD_master || 4516 DSAStack->getCurrentDirective() == OMPD_masked) 4517 return S; 4518 4519 bool ErrorFound = false; 4520 CaptureRegionUnwinderRAII CaptureRegionUnwinder( 4521 *this, ErrorFound, DSAStack->getCurrentDirective()); 4522 if (!S.isUsable()) { 4523 ErrorFound = true; 4524 return StmtError(); 4525 } 4526 4527 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 4528 getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective()); 4529 OMPOrderedClause *OC = nullptr; 4530 OMPScheduleClause *SC = nullptr; 4531 SmallVector<const OMPLinearClause *, 4> LCs; 4532 SmallVector<const OMPClauseWithPreInit *, 4> PICs; 4533 // This is required for proper codegen. 4534 for (OMPClause *Clause : Clauses) { 4535 if (!LangOpts.OpenMPSimd && 4536 isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) && 4537 Clause->getClauseKind() == OMPC_in_reduction) { 4538 // Capture taskgroup task_reduction descriptors inside the tasking regions 4539 // with the corresponding in_reduction items. 4540 auto *IRC = cast<OMPInReductionClause>(Clause); 4541 for (Expr *E : IRC->taskgroup_descriptors()) 4542 if (E) 4543 MarkDeclarationsReferencedInExpr(E); 4544 } 4545 if (isOpenMPPrivate(Clause->getClauseKind()) || 4546 Clause->getClauseKind() == OMPC_copyprivate || 4547 (getLangOpts().OpenMPUseTLS && 4548 getASTContext().getTargetInfo().isTLSSupported() && 4549 Clause->getClauseKind() == OMPC_copyin)) { 4550 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin); 4551 // Mark all variables in private list clauses as used in inner region. 4552 for (Stmt *VarRef : Clause->children()) { 4553 if (auto *E = cast_or_null<Expr>(VarRef)) { 4554 MarkDeclarationsReferencedInExpr(E); 4555 } 4556 } 4557 DSAStack->setForceVarCapturing(/*V=*/false); 4558 } else if (isOpenMPLoopTransformationDirective( 4559 DSAStack->getCurrentDirective())) { 4560 assert(CaptureRegions.empty() && 4561 "No captured regions in loop transformation directives."); 4562 } else if (CaptureRegions.size() > 1 || 4563 CaptureRegions.back() != OMPD_unknown) { 4564 if (auto *C = OMPClauseWithPreInit::get(Clause)) 4565 PICs.push_back(C); 4566 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) { 4567 if (Expr *E = C->getPostUpdateExpr()) 4568 MarkDeclarationsReferencedInExpr(E); 4569 } 4570 } 4571 if (Clause->getClauseKind() == OMPC_schedule) 4572 SC = cast<OMPScheduleClause>(Clause); 4573 else if (Clause->getClauseKind() == OMPC_ordered) 4574 OC = cast<OMPOrderedClause>(Clause); 4575 else if (Clause->getClauseKind() == OMPC_linear) 4576 LCs.push_back(cast<OMPLinearClause>(Clause)); 4577 } 4578 // Capture allocator expressions if used. 4579 for (Expr *E : DSAStack->getInnerAllocators()) 4580 MarkDeclarationsReferencedInExpr(E); 4581 // OpenMP, 2.7.1 Loop Construct, Restrictions 4582 // The nonmonotonic modifier cannot be specified if an ordered clause is 4583 // specified. 4584 if (SC && 4585 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic || 4586 SC->getSecondScheduleModifier() == 4587 OMPC_SCHEDULE_MODIFIER_nonmonotonic) && 4588 OC) { 4589 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic 4590 ? SC->getFirstScheduleModifierLoc() 4591 : SC->getSecondScheduleModifierLoc(), 4592 diag::err_omp_simple_clause_incompatible_with_ordered) 4593 << getOpenMPClauseName(OMPC_schedule) 4594 << getOpenMPSimpleClauseTypeName(OMPC_schedule, 4595 OMPC_SCHEDULE_MODIFIER_nonmonotonic) 4596 << SourceRange(OC->getBeginLoc(), OC->getEndLoc()); 4597 ErrorFound = true; 4598 } 4599 // OpenMP 5.0, 2.9.2 Worksharing-Loop Construct, Restrictions. 4600 // If an order(concurrent) clause is present, an ordered clause may not appear 4601 // on the same directive. 4602 if (checkOrderedOrderSpecified(*this, Clauses)) 4603 ErrorFound = true; 4604 if (!LCs.empty() && OC && OC->getNumForLoops()) { 4605 for (const OMPLinearClause *C : LCs) { 4606 Diag(C->getBeginLoc(), diag::err_omp_linear_ordered) 4607 << SourceRange(OC->getBeginLoc(), OC->getEndLoc()); 4608 } 4609 ErrorFound = true; 4610 } 4611 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) && 4612 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC && 4613 OC->getNumForLoops()) { 4614 Diag(OC->getBeginLoc(), diag::err_omp_ordered_simd) 4615 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 4616 ErrorFound = true; 4617 } 4618 if (ErrorFound) { 4619 return StmtError(); 4620 } 4621 StmtResult SR = S; 4622 unsigned CompletedRegions = 0; 4623 for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(CaptureRegions)) { 4624 // Mark all variables in private list clauses as used in inner region. 4625 // Required for proper codegen of combined directives. 4626 // TODO: add processing for other clauses. 4627 if (ThisCaptureRegion != OMPD_unknown) { 4628 for (const clang::OMPClauseWithPreInit *C : PICs) { 4629 OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion(); 4630 // Find the particular capture region for the clause if the 4631 // directive is a combined one with multiple capture regions. 4632 // If the directive is not a combined one, the capture region 4633 // associated with the clause is OMPD_unknown and is generated 4634 // only once. 4635 if (CaptureRegion == ThisCaptureRegion || 4636 CaptureRegion == OMPD_unknown) { 4637 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) { 4638 for (Decl *D : DS->decls()) 4639 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D)); 4640 } 4641 } 4642 } 4643 } 4644 if (ThisCaptureRegion == OMPD_target) { 4645 // Capture allocator traits in the target region. They are used implicitly 4646 // and, thus, are not captured by default. 4647 for (OMPClause *C : Clauses) { 4648 if (const auto *UAC = dyn_cast<OMPUsesAllocatorsClause>(C)) { 4649 for (unsigned I = 0, End = UAC->getNumberOfAllocators(); I < End; 4650 ++I) { 4651 OMPUsesAllocatorsClause::Data D = UAC->getAllocatorData(I); 4652 if (Expr *E = D.AllocatorTraits) 4653 MarkDeclarationsReferencedInExpr(E); 4654 } 4655 continue; 4656 } 4657 } 4658 } 4659 if (ThisCaptureRegion == OMPD_parallel) { 4660 // Capture temp arrays for inscan reductions and locals in aligned 4661 // clauses. 4662 for (OMPClause *C : Clauses) { 4663 if (auto *RC = dyn_cast<OMPReductionClause>(C)) { 4664 if (RC->getModifier() != OMPC_REDUCTION_inscan) 4665 continue; 4666 for (Expr *E : RC->copy_array_temps()) 4667 MarkDeclarationsReferencedInExpr(E); 4668 } 4669 if (auto *AC = dyn_cast<OMPAlignedClause>(C)) { 4670 for (Expr *E : AC->varlists()) 4671 MarkDeclarationsReferencedInExpr(E); 4672 } 4673 } 4674 } 4675 if (++CompletedRegions == CaptureRegions.size()) 4676 DSAStack->setBodyComplete(); 4677 SR = ActOnCapturedRegionEnd(SR.get()); 4678 } 4679 return SR; 4680 } 4681 4682 static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion, 4683 OpenMPDirectiveKind CancelRegion, 4684 SourceLocation StartLoc) { 4685 // CancelRegion is only needed for cancel and cancellation_point. 4686 if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point) 4687 return false; 4688 4689 if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for || 4690 CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup) 4691 return false; 4692 4693 SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region) 4694 << getOpenMPDirectiveName(CancelRegion); 4695 return true; 4696 } 4697 4698 static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack, 4699 OpenMPDirectiveKind CurrentRegion, 4700 const DeclarationNameInfo &CurrentName, 4701 OpenMPDirectiveKind CancelRegion, 4702 OpenMPBindClauseKind BindKind, 4703 SourceLocation StartLoc) { 4704 if (Stack->getCurScope()) { 4705 OpenMPDirectiveKind ParentRegion = Stack->getParentDirective(); 4706 OpenMPDirectiveKind OffendingRegion = ParentRegion; 4707 bool NestingProhibited = false; 4708 bool CloseNesting = true; 4709 bool OrphanSeen = false; 4710 enum { 4711 NoRecommend, 4712 ShouldBeInParallelRegion, 4713 ShouldBeInOrderedRegion, 4714 ShouldBeInTargetRegion, 4715 ShouldBeInTeamsRegion, 4716 ShouldBeInLoopSimdRegion, 4717 } Recommend = NoRecommend; 4718 if (isOpenMPSimdDirective(ParentRegion) && 4719 ((SemaRef.LangOpts.OpenMP <= 45 && CurrentRegion != OMPD_ordered) || 4720 (SemaRef.LangOpts.OpenMP >= 50 && CurrentRegion != OMPD_ordered && 4721 CurrentRegion != OMPD_simd && CurrentRegion != OMPD_atomic && 4722 CurrentRegion != OMPD_scan))) { 4723 // OpenMP [2.16, Nesting of Regions] 4724 // OpenMP constructs may not be nested inside a simd region. 4725 // OpenMP [2.8.1,simd Construct, Restrictions] 4726 // An ordered construct with the simd clause is the only OpenMP 4727 // construct that can appear in the simd region. 4728 // Allowing a SIMD construct nested in another SIMD construct is an 4729 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning 4730 // message. 4731 // OpenMP 5.0 [2.9.3.1, simd Construct, Restrictions] 4732 // The only OpenMP constructs that can be encountered during execution of 4733 // a simd region are the atomic construct, the loop construct, the simd 4734 // construct and the ordered construct with the simd clause. 4735 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd) 4736 ? diag::err_omp_prohibited_region_simd 4737 : diag::warn_omp_nesting_simd) 4738 << (SemaRef.LangOpts.OpenMP >= 50 ? 1 : 0); 4739 return CurrentRegion != OMPD_simd; 4740 } 4741 if (ParentRegion == OMPD_atomic) { 4742 // OpenMP [2.16, Nesting of Regions] 4743 // OpenMP constructs may not be nested inside an atomic region. 4744 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic); 4745 return true; 4746 } 4747 if (CurrentRegion == OMPD_section) { 4748 // OpenMP [2.7.2, sections Construct, Restrictions] 4749 // Orphaned section directives are prohibited. That is, the section 4750 // directives must appear within the sections construct and must not be 4751 // encountered elsewhere in the sections region. 4752 if (ParentRegion != OMPD_sections && 4753 ParentRegion != OMPD_parallel_sections) { 4754 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive) 4755 << (ParentRegion != OMPD_unknown) 4756 << getOpenMPDirectiveName(ParentRegion); 4757 return true; 4758 } 4759 return false; 4760 } 4761 // Allow some constructs (except teams and cancellation constructs) to be 4762 // orphaned (they could be used in functions, called from OpenMP regions 4763 // with the required preconditions). 4764 if (ParentRegion == OMPD_unknown && 4765 !isOpenMPNestingTeamsDirective(CurrentRegion) && 4766 CurrentRegion != OMPD_cancellation_point && 4767 CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_scan) 4768 return false; 4769 if (CurrentRegion == OMPD_cancellation_point || 4770 CurrentRegion == OMPD_cancel) { 4771 // OpenMP [2.16, Nesting of Regions] 4772 // A cancellation point construct for which construct-type-clause is 4773 // taskgroup must be nested inside a task construct. A cancellation 4774 // point construct for which construct-type-clause is not taskgroup must 4775 // be closely nested inside an OpenMP construct that matches the type 4776 // specified in construct-type-clause. 4777 // A cancel construct for which construct-type-clause is taskgroup must be 4778 // nested inside a task construct. A cancel construct for which 4779 // construct-type-clause is not taskgroup must be closely nested inside an 4780 // OpenMP construct that matches the type specified in 4781 // construct-type-clause. 4782 NestingProhibited = 4783 !((CancelRegion == OMPD_parallel && 4784 (ParentRegion == OMPD_parallel || 4785 ParentRegion == OMPD_target_parallel)) || 4786 (CancelRegion == OMPD_for && 4787 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for || 4788 ParentRegion == OMPD_target_parallel_for || 4789 ParentRegion == OMPD_distribute_parallel_for || 4790 ParentRegion == OMPD_teams_distribute_parallel_for || 4791 ParentRegion == OMPD_target_teams_distribute_parallel_for)) || 4792 (CancelRegion == OMPD_taskgroup && 4793 (ParentRegion == OMPD_task || 4794 (SemaRef.getLangOpts().OpenMP >= 50 && 4795 (ParentRegion == OMPD_taskloop || 4796 ParentRegion == OMPD_master_taskloop || 4797 ParentRegion == OMPD_parallel_master_taskloop)))) || 4798 (CancelRegion == OMPD_sections && 4799 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections || 4800 ParentRegion == OMPD_parallel_sections))); 4801 OrphanSeen = ParentRegion == OMPD_unknown; 4802 } else if (CurrentRegion == OMPD_master || CurrentRegion == OMPD_masked) { 4803 // OpenMP 5.1 [2.22, Nesting of Regions] 4804 // A masked region may not be closely nested inside a worksharing, loop, 4805 // atomic, task, or taskloop region. 4806 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 4807 isOpenMPGenericLoopDirective(ParentRegion) || 4808 isOpenMPTaskingDirective(ParentRegion); 4809 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) { 4810 // OpenMP [2.16, Nesting of Regions] 4811 // A critical region may not be nested (closely or otherwise) inside a 4812 // critical region with the same name. Note that this restriction is not 4813 // sufficient to prevent deadlock. 4814 SourceLocation PreviousCriticalLoc; 4815 bool DeadLock = Stack->hasDirective( 4816 [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K, 4817 const DeclarationNameInfo &DNI, 4818 SourceLocation Loc) { 4819 if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) { 4820 PreviousCriticalLoc = Loc; 4821 return true; 4822 } 4823 return false; 4824 }, 4825 false /* skip top directive */); 4826 if (DeadLock) { 4827 SemaRef.Diag(StartLoc, 4828 diag::err_omp_prohibited_region_critical_same_name) 4829 << CurrentName.getName(); 4830 if (PreviousCriticalLoc.isValid()) 4831 SemaRef.Diag(PreviousCriticalLoc, 4832 diag::note_omp_previous_critical_region); 4833 return true; 4834 } 4835 } else if (CurrentRegion == OMPD_barrier) { 4836 // OpenMP 5.1 [2.22, Nesting of Regions] 4837 // A barrier region may not be closely nested inside a worksharing, loop, 4838 // task, taskloop, critical, ordered, atomic, or masked region. 4839 NestingProhibited = 4840 isOpenMPWorksharingDirective(ParentRegion) || 4841 isOpenMPGenericLoopDirective(ParentRegion) || 4842 isOpenMPTaskingDirective(ParentRegion) || 4843 ParentRegion == OMPD_master || ParentRegion == OMPD_masked || 4844 ParentRegion == OMPD_parallel_master || 4845 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered; 4846 } else if (isOpenMPWorksharingDirective(CurrentRegion) && 4847 !isOpenMPParallelDirective(CurrentRegion) && 4848 !isOpenMPTeamsDirective(CurrentRegion)) { 4849 // OpenMP 5.1 [2.22, Nesting of Regions] 4850 // A loop region that binds to a parallel region or a worksharing region 4851 // may not be closely nested inside a worksharing, loop, task, taskloop, 4852 // critical, ordered, atomic, or masked region. 4853 NestingProhibited = 4854 isOpenMPWorksharingDirective(ParentRegion) || 4855 isOpenMPGenericLoopDirective(ParentRegion) || 4856 isOpenMPTaskingDirective(ParentRegion) || 4857 ParentRegion == OMPD_master || ParentRegion == OMPD_masked || 4858 ParentRegion == OMPD_parallel_master || 4859 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered; 4860 Recommend = ShouldBeInParallelRegion; 4861 } else if (CurrentRegion == OMPD_ordered) { 4862 // OpenMP [2.16, Nesting of Regions] 4863 // An ordered region may not be closely nested inside a critical, 4864 // atomic, or explicit task region. 4865 // An ordered region must be closely nested inside a loop region (or 4866 // parallel loop region) with an ordered clause. 4867 // OpenMP [2.8.1,simd Construct, Restrictions] 4868 // An ordered construct with the simd clause is the only OpenMP construct 4869 // that can appear in the simd region. 4870 NestingProhibited = ParentRegion == OMPD_critical || 4871 isOpenMPTaskingDirective(ParentRegion) || 4872 !(isOpenMPSimdDirective(ParentRegion) || 4873 Stack->isParentOrderedRegion()); 4874 Recommend = ShouldBeInOrderedRegion; 4875 } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) { 4876 // OpenMP [2.16, Nesting of Regions] 4877 // If specified, a teams construct must be contained within a target 4878 // construct. 4879 NestingProhibited = 4880 (SemaRef.LangOpts.OpenMP <= 45 && ParentRegion != OMPD_target) || 4881 (SemaRef.LangOpts.OpenMP >= 50 && ParentRegion != OMPD_unknown && 4882 ParentRegion != OMPD_target); 4883 OrphanSeen = ParentRegion == OMPD_unknown; 4884 Recommend = ShouldBeInTargetRegion; 4885 } else if (CurrentRegion == OMPD_scan) { 4886 // OpenMP [2.16, Nesting of Regions] 4887 // If specified, a teams construct must be contained within a target 4888 // construct. 4889 NestingProhibited = 4890 SemaRef.LangOpts.OpenMP < 50 || 4891 (ParentRegion != OMPD_simd && ParentRegion != OMPD_for && 4892 ParentRegion != OMPD_for_simd && ParentRegion != OMPD_parallel_for && 4893 ParentRegion != OMPD_parallel_for_simd); 4894 OrphanSeen = ParentRegion == OMPD_unknown; 4895 Recommend = ShouldBeInLoopSimdRegion; 4896 } 4897 if (!NestingProhibited && 4898 !isOpenMPTargetExecutionDirective(CurrentRegion) && 4899 !isOpenMPTargetDataManagementDirective(CurrentRegion) && 4900 (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) { 4901 // OpenMP [5.1, 2.22, Nesting of Regions] 4902 // distribute, distribute simd, distribute parallel worksharing-loop, 4903 // distribute parallel worksharing-loop SIMD, loop, parallel regions, 4904 // including any parallel regions arising from combined constructs, 4905 // omp_get_num_teams() regions, and omp_get_team_num() regions are the 4906 // only OpenMP regions that may be strictly nested inside the teams 4907 // region. 4908 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) && 4909 !isOpenMPDistributeDirective(CurrentRegion) && 4910 CurrentRegion != OMPD_loop; 4911 Recommend = ShouldBeInParallelRegion; 4912 } 4913 if (!NestingProhibited && CurrentRegion == OMPD_loop) { 4914 // OpenMP [5.1, 2.11.7, loop Construct, Restrictions] 4915 // If the bind clause is present on the loop construct and binding is 4916 // teams then the corresponding loop region must be strictly nested inside 4917 // a teams region. 4918 NestingProhibited = BindKind == OMPC_BIND_teams && 4919 ParentRegion != OMPD_teams && 4920 ParentRegion != OMPD_target_teams; 4921 Recommend = ShouldBeInTeamsRegion; 4922 } 4923 if (!NestingProhibited && 4924 isOpenMPNestingDistributeDirective(CurrentRegion)) { 4925 // OpenMP 4.5 [2.17 Nesting of Regions] 4926 // The region associated with the distribute construct must be strictly 4927 // nested inside a teams region 4928 NestingProhibited = 4929 (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams); 4930 Recommend = ShouldBeInTeamsRegion; 4931 } 4932 if (!NestingProhibited && 4933 (isOpenMPTargetExecutionDirective(CurrentRegion) || 4934 isOpenMPTargetDataManagementDirective(CurrentRegion))) { 4935 // OpenMP 4.5 [2.17 Nesting of Regions] 4936 // If a target, target update, target data, target enter data, or 4937 // target exit data construct is encountered during execution of a 4938 // target region, the behavior is unspecified. 4939 NestingProhibited = Stack->hasDirective( 4940 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &, 4941 SourceLocation) { 4942 if (isOpenMPTargetExecutionDirective(K)) { 4943 OffendingRegion = K; 4944 return true; 4945 } 4946 return false; 4947 }, 4948 false /* don't skip top directive */); 4949 CloseNesting = false; 4950 } 4951 if (NestingProhibited) { 4952 if (OrphanSeen) { 4953 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive) 4954 << getOpenMPDirectiveName(CurrentRegion) << Recommend; 4955 } else { 4956 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region) 4957 << CloseNesting << getOpenMPDirectiveName(OffendingRegion) 4958 << Recommend << getOpenMPDirectiveName(CurrentRegion); 4959 } 4960 return true; 4961 } 4962 } 4963 return false; 4964 } 4965 4966 struct Kind2Unsigned { 4967 using argument_type = OpenMPDirectiveKind; 4968 unsigned operator()(argument_type DK) { return unsigned(DK); } 4969 }; 4970 static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind, 4971 ArrayRef<OMPClause *> Clauses, 4972 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) { 4973 bool ErrorFound = false; 4974 unsigned NamedModifiersNumber = 0; 4975 llvm::IndexedMap<const OMPIfClause *, Kind2Unsigned> FoundNameModifiers; 4976 FoundNameModifiers.resize(llvm::omp::Directive_enumSize + 1); 4977 SmallVector<SourceLocation, 4> NameModifierLoc; 4978 for (const OMPClause *C : Clauses) { 4979 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) { 4980 // At most one if clause without a directive-name-modifier can appear on 4981 // the directive. 4982 OpenMPDirectiveKind CurNM = IC->getNameModifier(); 4983 if (FoundNameModifiers[CurNM]) { 4984 S.Diag(C->getBeginLoc(), diag::err_omp_more_one_clause) 4985 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if) 4986 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM); 4987 ErrorFound = true; 4988 } else if (CurNM != OMPD_unknown) { 4989 NameModifierLoc.push_back(IC->getNameModifierLoc()); 4990 ++NamedModifiersNumber; 4991 } 4992 FoundNameModifiers[CurNM] = IC; 4993 if (CurNM == OMPD_unknown) 4994 continue; 4995 // Check if the specified name modifier is allowed for the current 4996 // directive. 4997 // At most one if clause with the particular directive-name-modifier can 4998 // appear on the directive. 4999 if (!llvm::is_contained(AllowedNameModifiers, CurNM)) { 5000 S.Diag(IC->getNameModifierLoc(), 5001 diag::err_omp_wrong_if_directive_name_modifier) 5002 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind); 5003 ErrorFound = true; 5004 } 5005 } 5006 } 5007 // If any if clause on the directive includes a directive-name-modifier then 5008 // all if clauses on the directive must include a directive-name-modifier. 5009 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) { 5010 if (NamedModifiersNumber == AllowedNameModifiers.size()) { 5011 S.Diag(FoundNameModifiers[OMPD_unknown]->getBeginLoc(), 5012 diag::err_omp_no_more_if_clause); 5013 } else { 5014 std::string Values; 5015 std::string Sep(", "); 5016 unsigned AllowedCnt = 0; 5017 unsigned TotalAllowedNum = 5018 AllowedNameModifiers.size() - NamedModifiersNumber; 5019 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End; 5020 ++Cnt) { 5021 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt]; 5022 if (!FoundNameModifiers[NM]) { 5023 Values += "'"; 5024 Values += getOpenMPDirectiveName(NM); 5025 Values += "'"; 5026 if (AllowedCnt + 2 == TotalAllowedNum) 5027 Values += " or "; 5028 else if (AllowedCnt + 1 != TotalAllowedNum) 5029 Values += Sep; 5030 ++AllowedCnt; 5031 } 5032 } 5033 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getBeginLoc(), 5034 diag::err_omp_unnamed_if_clause) 5035 << (TotalAllowedNum > 1) << Values; 5036 } 5037 for (SourceLocation Loc : NameModifierLoc) { 5038 S.Diag(Loc, diag::note_omp_previous_named_if_clause); 5039 } 5040 ErrorFound = true; 5041 } 5042 return ErrorFound; 5043 } 5044 5045 static std::pair<ValueDecl *, bool> getPrivateItem(Sema &S, Expr *&RefExpr, 5046 SourceLocation &ELoc, 5047 SourceRange &ERange, 5048 bool AllowArraySection) { 5049 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() || 5050 RefExpr->containsUnexpandedParameterPack()) 5051 return std::make_pair(nullptr, true); 5052 5053 // OpenMP [3.1, C/C++] 5054 // A list item is a variable name. 5055 // OpenMP [2.9.3.3, Restrictions, p.1] 5056 // A variable that is part of another variable (as an array or 5057 // structure element) cannot appear in a private clause. 5058 RefExpr = RefExpr->IgnoreParens(); 5059 enum { 5060 NoArrayExpr = -1, 5061 ArraySubscript = 0, 5062 OMPArraySection = 1 5063 } IsArrayExpr = NoArrayExpr; 5064 if (AllowArraySection) { 5065 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) { 5066 Expr *Base = ASE->getBase()->IgnoreParenImpCasts(); 5067 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 5068 Base = TempASE->getBase()->IgnoreParenImpCasts(); 5069 RefExpr = Base; 5070 IsArrayExpr = ArraySubscript; 5071 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) { 5072 Expr *Base = OASE->getBase()->IgnoreParenImpCasts(); 5073 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) 5074 Base = TempOASE->getBase()->IgnoreParenImpCasts(); 5075 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 5076 Base = TempASE->getBase()->IgnoreParenImpCasts(); 5077 RefExpr = Base; 5078 IsArrayExpr = OMPArraySection; 5079 } 5080 } 5081 ELoc = RefExpr->getExprLoc(); 5082 ERange = RefExpr->getSourceRange(); 5083 RefExpr = RefExpr->IgnoreParenImpCasts(); 5084 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr); 5085 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr); 5086 if ((!DE || !isa<VarDecl>(DE->getDecl())) && 5087 (S.getCurrentThisType().isNull() || !ME || 5088 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) || 5089 !isa<FieldDecl>(ME->getMemberDecl()))) { 5090 if (IsArrayExpr != NoArrayExpr) { 5091 S.Diag(ELoc, diag::err_omp_expected_base_var_name) 5092 << IsArrayExpr << ERange; 5093 } else { 5094 S.Diag(ELoc, 5095 AllowArraySection 5096 ? diag::err_omp_expected_var_name_member_expr_or_array_item 5097 : diag::err_omp_expected_var_name_member_expr) 5098 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange; 5099 } 5100 return std::make_pair(nullptr, false); 5101 } 5102 return std::make_pair( 5103 getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false); 5104 } 5105 5106 namespace { 5107 /// Checks if the allocator is used in uses_allocators clause to be allowed in 5108 /// target regions. 5109 class AllocatorChecker final : public ConstStmtVisitor<AllocatorChecker, bool> { 5110 DSAStackTy *S = nullptr; 5111 5112 public: 5113 bool VisitDeclRefExpr(const DeclRefExpr *E) { 5114 return S->isUsesAllocatorsDecl(E->getDecl()) 5115 .getValueOr( 5116 DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait) == 5117 DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait; 5118 } 5119 bool VisitStmt(const Stmt *S) { 5120 for (const Stmt *Child : S->children()) { 5121 if (Child && Visit(Child)) 5122 return true; 5123 } 5124 return false; 5125 } 5126 explicit AllocatorChecker(DSAStackTy *S) : S(S) {} 5127 }; 5128 } // namespace 5129 5130 static void checkAllocateClauses(Sema &S, DSAStackTy *Stack, 5131 ArrayRef<OMPClause *> Clauses) { 5132 assert(!S.CurContext->isDependentContext() && 5133 "Expected non-dependent context."); 5134 auto AllocateRange = 5135 llvm::make_filter_range(Clauses, OMPAllocateClause::classof); 5136 llvm::DenseMap<CanonicalDeclPtr<Decl>, CanonicalDeclPtr<VarDecl>> DeclToCopy; 5137 auto PrivateRange = llvm::make_filter_range(Clauses, [](const OMPClause *C) { 5138 return isOpenMPPrivate(C->getClauseKind()); 5139 }); 5140 for (OMPClause *Cl : PrivateRange) { 5141 MutableArrayRef<Expr *>::iterator I, It, Et; 5142 if (Cl->getClauseKind() == OMPC_private) { 5143 auto *PC = cast<OMPPrivateClause>(Cl); 5144 I = PC->private_copies().begin(); 5145 It = PC->varlist_begin(); 5146 Et = PC->varlist_end(); 5147 } else if (Cl->getClauseKind() == OMPC_firstprivate) { 5148 auto *PC = cast<OMPFirstprivateClause>(Cl); 5149 I = PC->private_copies().begin(); 5150 It = PC->varlist_begin(); 5151 Et = PC->varlist_end(); 5152 } else if (Cl->getClauseKind() == OMPC_lastprivate) { 5153 auto *PC = cast<OMPLastprivateClause>(Cl); 5154 I = PC->private_copies().begin(); 5155 It = PC->varlist_begin(); 5156 Et = PC->varlist_end(); 5157 } else if (Cl->getClauseKind() == OMPC_linear) { 5158 auto *PC = cast<OMPLinearClause>(Cl); 5159 I = PC->privates().begin(); 5160 It = PC->varlist_begin(); 5161 Et = PC->varlist_end(); 5162 } else if (Cl->getClauseKind() == OMPC_reduction) { 5163 auto *PC = cast<OMPReductionClause>(Cl); 5164 I = PC->privates().begin(); 5165 It = PC->varlist_begin(); 5166 Et = PC->varlist_end(); 5167 } else if (Cl->getClauseKind() == OMPC_task_reduction) { 5168 auto *PC = cast<OMPTaskReductionClause>(Cl); 5169 I = PC->privates().begin(); 5170 It = PC->varlist_begin(); 5171 Et = PC->varlist_end(); 5172 } else if (Cl->getClauseKind() == OMPC_in_reduction) { 5173 auto *PC = cast<OMPInReductionClause>(Cl); 5174 I = PC->privates().begin(); 5175 It = PC->varlist_begin(); 5176 Et = PC->varlist_end(); 5177 } else { 5178 llvm_unreachable("Expected private clause."); 5179 } 5180 for (Expr *E : llvm::make_range(It, Et)) { 5181 if (!*I) { 5182 ++I; 5183 continue; 5184 } 5185 SourceLocation ELoc; 5186 SourceRange ERange; 5187 Expr *SimpleRefExpr = E; 5188 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange, 5189 /*AllowArraySection=*/true); 5190 DeclToCopy.try_emplace(Res.first, 5191 cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl())); 5192 ++I; 5193 } 5194 } 5195 for (OMPClause *C : AllocateRange) { 5196 auto *AC = cast<OMPAllocateClause>(C); 5197 if (S.getLangOpts().OpenMP >= 50 && 5198 !Stack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>() && 5199 isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) && 5200 AC->getAllocator()) { 5201 Expr *Allocator = AC->getAllocator(); 5202 // OpenMP, 2.12.5 target Construct 5203 // Memory allocators that do not appear in a uses_allocators clause cannot 5204 // appear as an allocator in an allocate clause or be used in the target 5205 // region unless a requires directive with the dynamic_allocators clause 5206 // is present in the same compilation unit. 5207 AllocatorChecker Checker(Stack); 5208 if (Checker.Visit(Allocator)) 5209 S.Diag(Allocator->getExprLoc(), 5210 diag::err_omp_allocator_not_in_uses_allocators) 5211 << Allocator->getSourceRange(); 5212 } 5213 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind = 5214 getAllocatorKind(S, Stack, AC->getAllocator()); 5215 // OpenMP, 2.11.4 allocate Clause, Restrictions. 5216 // For task, taskloop or target directives, allocation requests to memory 5217 // allocators with the trait access set to thread result in unspecified 5218 // behavior. 5219 if (AllocatorKind == OMPAllocateDeclAttr::OMPThreadMemAlloc && 5220 (isOpenMPTaskingDirective(Stack->getCurrentDirective()) || 5221 isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()))) { 5222 S.Diag(AC->getAllocator()->getExprLoc(), 5223 diag::warn_omp_allocate_thread_on_task_target_directive) 5224 << getOpenMPDirectiveName(Stack->getCurrentDirective()); 5225 } 5226 for (Expr *E : AC->varlists()) { 5227 SourceLocation ELoc; 5228 SourceRange ERange; 5229 Expr *SimpleRefExpr = E; 5230 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange); 5231 ValueDecl *VD = Res.first; 5232 DSAStackTy::DSAVarData Data = Stack->getTopDSA(VD, /*FromParent=*/false); 5233 if (!isOpenMPPrivate(Data.CKind)) { 5234 S.Diag(E->getExprLoc(), 5235 diag::err_omp_expected_private_copy_for_allocate); 5236 continue; 5237 } 5238 VarDecl *PrivateVD = DeclToCopy[VD]; 5239 if (checkPreviousOMPAllocateAttribute(S, Stack, E, PrivateVD, 5240 AllocatorKind, AC->getAllocator())) 5241 continue; 5242 // Placeholder until allocate clause supports align modifier. 5243 Expr *Alignment = nullptr; 5244 applyOMPAllocateAttribute(S, PrivateVD, AllocatorKind, AC->getAllocator(), 5245 Alignment, E->getSourceRange()); 5246 } 5247 } 5248 } 5249 5250 namespace { 5251 /// Rewrite statements and expressions for Sema \p Actions CurContext. 5252 /// 5253 /// Used to wrap already parsed statements/expressions into a new CapturedStmt 5254 /// context. DeclRefExpr used inside the new context are changed to refer to the 5255 /// captured variable instead. 5256 class CaptureVars : public TreeTransform<CaptureVars> { 5257 using BaseTransform = TreeTransform<CaptureVars>; 5258 5259 public: 5260 CaptureVars(Sema &Actions) : BaseTransform(Actions) {} 5261 5262 bool AlwaysRebuild() { return true; } 5263 }; 5264 } // namespace 5265 5266 static VarDecl *precomputeExpr(Sema &Actions, 5267 SmallVectorImpl<Stmt *> &BodyStmts, Expr *E, 5268 StringRef Name) { 5269 Expr *NewE = AssertSuccess(CaptureVars(Actions).TransformExpr(E)); 5270 VarDecl *NewVar = buildVarDecl(Actions, {}, NewE->getType(), Name, nullptr, 5271 dyn_cast<DeclRefExpr>(E->IgnoreImplicit())); 5272 auto *NewDeclStmt = cast<DeclStmt>(AssertSuccess( 5273 Actions.ActOnDeclStmt(Actions.ConvertDeclToDeclGroup(NewVar), {}, {}))); 5274 Actions.AddInitializerToDecl(NewDeclStmt->getSingleDecl(), NewE, false); 5275 BodyStmts.push_back(NewDeclStmt); 5276 return NewVar; 5277 } 5278 5279 /// Create a closure that computes the number of iterations of a loop. 5280 /// 5281 /// \param Actions The Sema object. 5282 /// \param LogicalTy Type for the logical iteration number. 5283 /// \param Rel Comparison operator of the loop condition. 5284 /// \param StartExpr Value of the loop counter at the first iteration. 5285 /// \param StopExpr Expression the loop counter is compared against in the loop 5286 /// condition. \param StepExpr Amount of increment after each iteration. 5287 /// 5288 /// \return Closure (CapturedStmt) of the distance calculation. 5289 static CapturedStmt *buildDistanceFunc(Sema &Actions, QualType LogicalTy, 5290 BinaryOperator::Opcode Rel, 5291 Expr *StartExpr, Expr *StopExpr, 5292 Expr *StepExpr) { 5293 ASTContext &Ctx = Actions.getASTContext(); 5294 TypeSourceInfo *LogicalTSI = Ctx.getTrivialTypeSourceInfo(LogicalTy); 5295 5296 // Captured regions currently don't support return values, we use an 5297 // out-parameter instead. All inputs are implicit captures. 5298 // TODO: Instead of capturing each DeclRefExpr occurring in 5299 // StartExpr/StopExpr/Step, these could also be passed as a value capture. 5300 QualType ResultTy = Ctx.getLValueReferenceType(LogicalTy); 5301 Sema::CapturedParamNameType Params[] = {{"Distance", ResultTy}, 5302 {StringRef(), QualType()}}; 5303 Actions.ActOnCapturedRegionStart({}, nullptr, CR_Default, Params); 5304 5305 Stmt *Body; 5306 { 5307 Sema::CompoundScopeRAII CompoundScope(Actions); 5308 CapturedDecl *CS = cast<CapturedDecl>(Actions.CurContext); 5309 5310 // Get the LValue expression for the result. 5311 ImplicitParamDecl *DistParam = CS->getParam(0); 5312 DeclRefExpr *DistRef = Actions.BuildDeclRefExpr( 5313 DistParam, LogicalTy, VK_LValue, {}, nullptr, nullptr, {}, nullptr); 5314 5315 SmallVector<Stmt *, 4> BodyStmts; 5316 5317 // Capture all referenced variable references. 5318 // TODO: Instead of computing NewStart/NewStop/NewStep inside the 5319 // CapturedStmt, we could compute them before and capture the result, to be 5320 // used jointly with the LoopVar function. 5321 VarDecl *NewStart = precomputeExpr(Actions, BodyStmts, StartExpr, ".start"); 5322 VarDecl *NewStop = precomputeExpr(Actions, BodyStmts, StopExpr, ".stop"); 5323 VarDecl *NewStep = precomputeExpr(Actions, BodyStmts, StepExpr, ".step"); 5324 auto BuildVarRef = [&](VarDecl *VD) { 5325 return buildDeclRefExpr(Actions, VD, VD->getType(), {}); 5326 }; 5327 5328 IntegerLiteral *Zero = IntegerLiteral::Create( 5329 Ctx, llvm::APInt(Ctx.getIntWidth(LogicalTy), 0), LogicalTy, {}); 5330 IntegerLiteral *One = IntegerLiteral::Create( 5331 Ctx, llvm::APInt(Ctx.getIntWidth(LogicalTy), 1), LogicalTy, {}); 5332 Expr *Dist; 5333 if (Rel == BO_NE) { 5334 // When using a != comparison, the increment can be +1 or -1. This can be 5335 // dynamic at runtime, so we need to check for the direction. 5336 Expr *IsNegStep = AssertSuccess( 5337 Actions.BuildBinOp(nullptr, {}, BO_LT, BuildVarRef(NewStep), Zero)); 5338 5339 // Positive increment. 5340 Expr *ForwardRange = AssertSuccess(Actions.BuildBinOp( 5341 nullptr, {}, BO_Sub, BuildVarRef(NewStop), BuildVarRef(NewStart))); 5342 ForwardRange = AssertSuccess( 5343 Actions.BuildCStyleCastExpr({}, LogicalTSI, {}, ForwardRange)); 5344 Expr *ForwardDist = AssertSuccess(Actions.BuildBinOp( 5345 nullptr, {}, BO_Div, ForwardRange, BuildVarRef(NewStep))); 5346 5347 // Negative increment. 5348 Expr *BackwardRange = AssertSuccess(Actions.BuildBinOp( 5349 nullptr, {}, BO_Sub, BuildVarRef(NewStart), BuildVarRef(NewStop))); 5350 BackwardRange = AssertSuccess( 5351 Actions.BuildCStyleCastExpr({}, LogicalTSI, {}, BackwardRange)); 5352 Expr *NegIncAmount = AssertSuccess( 5353 Actions.BuildUnaryOp(nullptr, {}, UO_Minus, BuildVarRef(NewStep))); 5354 Expr *BackwardDist = AssertSuccess( 5355 Actions.BuildBinOp(nullptr, {}, BO_Div, BackwardRange, NegIncAmount)); 5356 5357 // Use the appropriate case. 5358 Dist = AssertSuccess(Actions.ActOnConditionalOp( 5359 {}, {}, IsNegStep, BackwardDist, ForwardDist)); 5360 } else { 5361 assert((Rel == BO_LT || Rel == BO_LE || Rel == BO_GE || Rel == BO_GT) && 5362 "Expected one of these relational operators"); 5363 5364 // We can derive the direction from any other comparison operator. It is 5365 // non well-formed OpenMP if Step increments/decrements in the other 5366 // directions. Whether at least the first iteration passes the loop 5367 // condition. 5368 Expr *HasAnyIteration = AssertSuccess(Actions.BuildBinOp( 5369 nullptr, {}, Rel, BuildVarRef(NewStart), BuildVarRef(NewStop))); 5370 5371 // Compute the range between first and last counter value. 5372 Expr *Range; 5373 if (Rel == BO_GE || Rel == BO_GT) 5374 Range = AssertSuccess(Actions.BuildBinOp( 5375 nullptr, {}, BO_Sub, BuildVarRef(NewStart), BuildVarRef(NewStop))); 5376 else 5377 Range = AssertSuccess(Actions.BuildBinOp( 5378 nullptr, {}, BO_Sub, BuildVarRef(NewStop), BuildVarRef(NewStart))); 5379 5380 // Ensure unsigned range space. 5381 Range = 5382 AssertSuccess(Actions.BuildCStyleCastExpr({}, LogicalTSI, {}, Range)); 5383 5384 if (Rel == BO_LE || Rel == BO_GE) { 5385 // Add one to the range if the relational operator is inclusive. 5386 Range = 5387 AssertSuccess(Actions.BuildBinOp(nullptr, {}, BO_Add, Range, One)); 5388 } 5389 5390 // Divide by the absolute step amount. If the range is not a multiple of 5391 // the step size, rounding-up the effective upper bound ensures that the 5392 // last iteration is included. 5393 // Note that the rounding-up may cause an overflow in a temporry that 5394 // could be avoided, but would have occured in a C-style for-loop as well. 5395 Expr *Divisor = BuildVarRef(NewStep); 5396 if (Rel == BO_GE || Rel == BO_GT) 5397 Divisor = 5398 AssertSuccess(Actions.BuildUnaryOp(nullptr, {}, UO_Minus, Divisor)); 5399 Expr *DivisorMinusOne = 5400 AssertSuccess(Actions.BuildBinOp(nullptr, {}, BO_Sub, Divisor, One)); 5401 Expr *RangeRoundUp = AssertSuccess( 5402 Actions.BuildBinOp(nullptr, {}, BO_Add, Range, DivisorMinusOne)); 5403 Dist = AssertSuccess( 5404 Actions.BuildBinOp(nullptr, {}, BO_Div, RangeRoundUp, Divisor)); 5405 5406 // If there is not at least one iteration, the range contains garbage. Fix 5407 // to zero in this case. 5408 Dist = AssertSuccess( 5409 Actions.ActOnConditionalOp({}, {}, HasAnyIteration, Dist, Zero)); 5410 } 5411 5412 // Assign the result to the out-parameter. 5413 Stmt *ResultAssign = AssertSuccess(Actions.BuildBinOp( 5414 Actions.getCurScope(), {}, BO_Assign, DistRef, Dist)); 5415 BodyStmts.push_back(ResultAssign); 5416 5417 Body = AssertSuccess(Actions.ActOnCompoundStmt({}, {}, BodyStmts, false)); 5418 } 5419 5420 return cast<CapturedStmt>( 5421 AssertSuccess(Actions.ActOnCapturedRegionEnd(Body))); 5422 } 5423 5424 /// Create a closure that computes the loop variable from the logical iteration 5425 /// number. 5426 /// 5427 /// \param Actions The Sema object. 5428 /// \param LoopVarTy Type for the loop variable used for result value. 5429 /// \param LogicalTy Type for the logical iteration number. 5430 /// \param StartExpr Value of the loop counter at the first iteration. 5431 /// \param Step Amount of increment after each iteration. 5432 /// \param Deref Whether the loop variable is a dereference of the loop 5433 /// counter variable. 5434 /// 5435 /// \return Closure (CapturedStmt) of the loop value calculation. 5436 static CapturedStmt *buildLoopVarFunc(Sema &Actions, QualType LoopVarTy, 5437 QualType LogicalTy, 5438 DeclRefExpr *StartExpr, Expr *Step, 5439 bool Deref) { 5440 ASTContext &Ctx = Actions.getASTContext(); 5441 5442 // Pass the result as an out-parameter. Passing as return value would require 5443 // the OpenMPIRBuilder to know additional C/C++ semantics, such as how to 5444 // invoke a copy constructor. 5445 QualType TargetParamTy = Ctx.getLValueReferenceType(LoopVarTy); 5446 Sema::CapturedParamNameType Params[] = {{"LoopVar", TargetParamTy}, 5447 {"Logical", LogicalTy}, 5448 {StringRef(), QualType()}}; 5449 Actions.ActOnCapturedRegionStart({}, nullptr, CR_Default, Params); 5450 5451 // Capture the initial iterator which represents the LoopVar value at the 5452 // zero's logical iteration. Since the original ForStmt/CXXForRangeStmt update 5453 // it in every iteration, capture it by value before it is modified. 5454 VarDecl *StartVar = cast<VarDecl>(StartExpr->getDecl()); 5455 bool Invalid = Actions.tryCaptureVariable(StartVar, {}, 5456 Sema::TryCapture_ExplicitByVal, {}); 5457 (void)Invalid; 5458 assert(!Invalid && "Expecting capture-by-value to work."); 5459 5460 Expr *Body; 5461 { 5462 Sema::CompoundScopeRAII CompoundScope(Actions); 5463 auto *CS = cast<CapturedDecl>(Actions.CurContext); 5464 5465 ImplicitParamDecl *TargetParam = CS->getParam(0); 5466 DeclRefExpr *TargetRef = Actions.BuildDeclRefExpr( 5467 TargetParam, LoopVarTy, VK_LValue, {}, nullptr, nullptr, {}, nullptr); 5468 ImplicitParamDecl *IndvarParam = CS->getParam(1); 5469 DeclRefExpr *LogicalRef = Actions.BuildDeclRefExpr( 5470 IndvarParam, LogicalTy, VK_LValue, {}, nullptr, nullptr, {}, nullptr); 5471 5472 // Capture the Start expression. 5473 CaptureVars Recap(Actions); 5474 Expr *NewStart = AssertSuccess(Recap.TransformExpr(StartExpr)); 5475 Expr *NewStep = AssertSuccess(Recap.TransformExpr(Step)); 5476 5477 Expr *Skip = AssertSuccess( 5478 Actions.BuildBinOp(nullptr, {}, BO_Mul, NewStep, LogicalRef)); 5479 // TODO: Explicitly cast to the iterator's difference_type instead of 5480 // relying on implicit conversion. 5481 Expr *Advanced = 5482 AssertSuccess(Actions.BuildBinOp(nullptr, {}, BO_Add, NewStart, Skip)); 5483 5484 if (Deref) { 5485 // For range-based for-loops convert the loop counter value to a concrete 5486 // loop variable value by dereferencing the iterator. 5487 Advanced = 5488 AssertSuccess(Actions.BuildUnaryOp(nullptr, {}, UO_Deref, Advanced)); 5489 } 5490 5491 // Assign the result to the output parameter. 5492 Body = AssertSuccess(Actions.BuildBinOp(Actions.getCurScope(), {}, 5493 BO_Assign, TargetRef, Advanced)); 5494 } 5495 return cast<CapturedStmt>( 5496 AssertSuccess(Actions.ActOnCapturedRegionEnd(Body))); 5497 } 5498 5499 StmtResult Sema::ActOnOpenMPCanonicalLoop(Stmt *AStmt) { 5500 ASTContext &Ctx = getASTContext(); 5501 5502 // Extract the common elements of ForStmt and CXXForRangeStmt: 5503 // Loop variable, repeat condition, increment 5504 Expr *Cond, *Inc; 5505 VarDecl *LIVDecl, *LUVDecl; 5506 if (auto *For = dyn_cast<ForStmt>(AStmt)) { 5507 Stmt *Init = For->getInit(); 5508 if (auto *LCVarDeclStmt = dyn_cast<DeclStmt>(Init)) { 5509 // For statement declares loop variable. 5510 LIVDecl = cast<VarDecl>(LCVarDeclStmt->getSingleDecl()); 5511 } else if (auto *LCAssign = dyn_cast<BinaryOperator>(Init)) { 5512 // For statement reuses variable. 5513 assert(LCAssign->getOpcode() == BO_Assign && 5514 "init part must be a loop variable assignment"); 5515 auto *CounterRef = cast<DeclRefExpr>(LCAssign->getLHS()); 5516 LIVDecl = cast<VarDecl>(CounterRef->getDecl()); 5517 } else 5518 llvm_unreachable("Cannot determine loop variable"); 5519 LUVDecl = LIVDecl; 5520 5521 Cond = For->getCond(); 5522 Inc = For->getInc(); 5523 } else if (auto *RangeFor = dyn_cast<CXXForRangeStmt>(AStmt)) { 5524 DeclStmt *BeginStmt = RangeFor->getBeginStmt(); 5525 LIVDecl = cast<VarDecl>(BeginStmt->getSingleDecl()); 5526 LUVDecl = RangeFor->getLoopVariable(); 5527 5528 Cond = RangeFor->getCond(); 5529 Inc = RangeFor->getInc(); 5530 } else 5531 llvm_unreachable("unhandled kind of loop"); 5532 5533 QualType CounterTy = LIVDecl->getType(); 5534 QualType LVTy = LUVDecl->getType(); 5535 5536 // Analyze the loop condition. 5537 Expr *LHS, *RHS; 5538 BinaryOperator::Opcode CondRel; 5539 Cond = Cond->IgnoreImplicit(); 5540 if (auto *CondBinExpr = dyn_cast<BinaryOperator>(Cond)) { 5541 LHS = CondBinExpr->getLHS(); 5542 RHS = CondBinExpr->getRHS(); 5543 CondRel = CondBinExpr->getOpcode(); 5544 } else if (auto *CondCXXOp = dyn_cast<CXXOperatorCallExpr>(Cond)) { 5545 assert(CondCXXOp->getNumArgs() == 2 && "Comparison should have 2 operands"); 5546 LHS = CondCXXOp->getArg(0); 5547 RHS = CondCXXOp->getArg(1); 5548 switch (CondCXXOp->getOperator()) { 5549 case OO_ExclaimEqual: 5550 CondRel = BO_NE; 5551 break; 5552 case OO_Less: 5553 CondRel = BO_LT; 5554 break; 5555 case OO_LessEqual: 5556 CondRel = BO_LE; 5557 break; 5558 case OO_Greater: 5559 CondRel = BO_GT; 5560 break; 5561 case OO_GreaterEqual: 5562 CondRel = BO_GE; 5563 break; 5564 default: 5565 llvm_unreachable("unexpected iterator operator"); 5566 } 5567 } else 5568 llvm_unreachable("unexpected loop condition"); 5569 5570 // Normalize such that the loop counter is on the LHS. 5571 if (!isa<DeclRefExpr>(LHS->IgnoreImplicit()) || 5572 cast<DeclRefExpr>(LHS->IgnoreImplicit())->getDecl() != LIVDecl) { 5573 std::swap(LHS, RHS); 5574 CondRel = BinaryOperator::reverseComparisonOp(CondRel); 5575 } 5576 auto *CounterRef = cast<DeclRefExpr>(LHS->IgnoreImplicit()); 5577 5578 // Decide the bit width for the logical iteration counter. By default use the 5579 // unsigned ptrdiff_t integer size (for iterators and pointers). 5580 // TODO: For iterators, use iterator::difference_type, 5581 // std::iterator_traits<>::difference_type or decltype(it - end). 5582 QualType LogicalTy = Ctx.getUnsignedPointerDiffType(); 5583 if (CounterTy->isIntegerType()) { 5584 unsigned BitWidth = Ctx.getIntWidth(CounterTy); 5585 LogicalTy = Ctx.getIntTypeForBitwidth(BitWidth, false); 5586 } 5587 5588 // Analyze the loop increment. 5589 Expr *Step; 5590 if (auto *IncUn = dyn_cast<UnaryOperator>(Inc)) { 5591 int Direction; 5592 switch (IncUn->getOpcode()) { 5593 case UO_PreInc: 5594 case UO_PostInc: 5595 Direction = 1; 5596 break; 5597 case UO_PreDec: 5598 case UO_PostDec: 5599 Direction = -1; 5600 break; 5601 default: 5602 llvm_unreachable("unhandled unary increment operator"); 5603 } 5604 Step = IntegerLiteral::Create( 5605 Ctx, llvm::APInt(Ctx.getIntWidth(LogicalTy), Direction), LogicalTy, {}); 5606 } else if (auto *IncBin = dyn_cast<BinaryOperator>(Inc)) { 5607 if (IncBin->getOpcode() == BO_AddAssign) { 5608 Step = IncBin->getRHS(); 5609 } else if (IncBin->getOpcode() == BO_SubAssign) { 5610 Step = 5611 AssertSuccess(BuildUnaryOp(nullptr, {}, UO_Minus, IncBin->getRHS())); 5612 } else 5613 llvm_unreachable("unhandled binary increment operator"); 5614 } else if (auto *CondCXXOp = dyn_cast<CXXOperatorCallExpr>(Inc)) { 5615 switch (CondCXXOp->getOperator()) { 5616 case OO_PlusPlus: 5617 Step = IntegerLiteral::Create( 5618 Ctx, llvm::APInt(Ctx.getIntWidth(LogicalTy), 1), LogicalTy, {}); 5619 break; 5620 case OO_MinusMinus: 5621 Step = IntegerLiteral::Create( 5622 Ctx, llvm::APInt(Ctx.getIntWidth(LogicalTy), -1), LogicalTy, {}); 5623 break; 5624 case OO_PlusEqual: 5625 Step = CondCXXOp->getArg(1); 5626 break; 5627 case OO_MinusEqual: 5628 Step = AssertSuccess( 5629 BuildUnaryOp(nullptr, {}, UO_Minus, CondCXXOp->getArg(1))); 5630 break; 5631 default: 5632 llvm_unreachable("unhandled overloaded increment operator"); 5633 } 5634 } else 5635 llvm_unreachable("unknown increment expression"); 5636 5637 CapturedStmt *DistanceFunc = 5638 buildDistanceFunc(*this, LogicalTy, CondRel, LHS, RHS, Step); 5639 CapturedStmt *LoopVarFunc = buildLoopVarFunc( 5640 *this, LVTy, LogicalTy, CounterRef, Step, isa<CXXForRangeStmt>(AStmt)); 5641 DeclRefExpr *LVRef = BuildDeclRefExpr(LUVDecl, LUVDecl->getType(), VK_LValue, 5642 {}, nullptr, nullptr, {}, nullptr); 5643 return OMPCanonicalLoop::create(getASTContext(), AStmt, DistanceFunc, 5644 LoopVarFunc, LVRef); 5645 } 5646 5647 StmtResult Sema::ActOnOpenMPLoopnest(Stmt *AStmt) { 5648 // Handle a literal loop. 5649 if (isa<ForStmt>(AStmt) || isa<CXXForRangeStmt>(AStmt)) 5650 return ActOnOpenMPCanonicalLoop(AStmt); 5651 5652 // If not a literal loop, it must be the result of a loop transformation. 5653 OMPExecutableDirective *LoopTransform = cast<OMPExecutableDirective>(AStmt); 5654 assert( 5655 isOpenMPLoopTransformationDirective(LoopTransform->getDirectiveKind()) && 5656 "Loop transformation directive expected"); 5657 return LoopTransform; 5658 } 5659 5660 static ExprResult buildUserDefinedMapperRef(Sema &SemaRef, Scope *S, 5661 CXXScopeSpec &MapperIdScopeSpec, 5662 const DeclarationNameInfo &MapperId, 5663 QualType Type, 5664 Expr *UnresolvedMapper); 5665 5666 /// Perform DFS through the structure/class data members trying to find 5667 /// member(s) with user-defined 'default' mapper and generate implicit map 5668 /// clauses for such members with the found 'default' mapper. 5669 static void 5670 processImplicitMapsWithDefaultMappers(Sema &S, DSAStackTy *Stack, 5671 SmallVectorImpl<OMPClause *> &Clauses) { 5672 // Check for the deault mapper for data members. 5673 if (S.getLangOpts().OpenMP < 50) 5674 return; 5675 SmallVector<OMPClause *, 4> ImplicitMaps; 5676 for (int Cnt = 0, EndCnt = Clauses.size(); Cnt < EndCnt; ++Cnt) { 5677 auto *C = dyn_cast<OMPMapClause>(Clauses[Cnt]); 5678 if (!C) 5679 continue; 5680 SmallVector<Expr *, 4> SubExprs; 5681 auto *MI = C->mapperlist_begin(); 5682 for (auto I = C->varlist_begin(), End = C->varlist_end(); I != End; 5683 ++I, ++MI) { 5684 // Expression is mapped using mapper - skip it. 5685 if (*MI) 5686 continue; 5687 Expr *E = *I; 5688 // Expression is dependent - skip it, build the mapper when it gets 5689 // instantiated. 5690 if (E->isTypeDependent() || E->isValueDependent() || 5691 E->containsUnexpandedParameterPack()) 5692 continue; 5693 // Array section - need to check for the mapping of the array section 5694 // element. 5695 QualType CanonType = E->getType().getCanonicalType(); 5696 if (CanonType->isSpecificBuiltinType(BuiltinType::OMPArraySection)) { 5697 const auto *OASE = cast<OMPArraySectionExpr>(E->IgnoreParenImpCasts()); 5698 QualType BaseType = 5699 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 5700 QualType ElemType; 5701 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 5702 ElemType = ATy->getElementType(); 5703 else 5704 ElemType = BaseType->getPointeeType(); 5705 CanonType = ElemType; 5706 } 5707 5708 // DFS over data members in structures/classes. 5709 SmallVector<std::pair<QualType, FieldDecl *>, 4> Types( 5710 1, {CanonType, nullptr}); 5711 llvm::DenseMap<const Type *, Expr *> Visited; 5712 SmallVector<std::pair<FieldDecl *, unsigned>, 4> ParentChain( 5713 1, {nullptr, 1}); 5714 while (!Types.empty()) { 5715 QualType BaseType; 5716 FieldDecl *CurFD; 5717 std::tie(BaseType, CurFD) = Types.pop_back_val(); 5718 while (ParentChain.back().second == 0) 5719 ParentChain.pop_back(); 5720 --ParentChain.back().second; 5721 if (BaseType.isNull()) 5722 continue; 5723 // Only structs/classes are allowed to have mappers. 5724 const RecordDecl *RD = BaseType.getCanonicalType()->getAsRecordDecl(); 5725 if (!RD) 5726 continue; 5727 auto It = Visited.find(BaseType.getTypePtr()); 5728 if (It == Visited.end()) { 5729 // Try to find the associated user-defined mapper. 5730 CXXScopeSpec MapperIdScopeSpec; 5731 DeclarationNameInfo DefaultMapperId; 5732 DefaultMapperId.setName(S.Context.DeclarationNames.getIdentifier( 5733 &S.Context.Idents.get("default"))); 5734 DefaultMapperId.setLoc(E->getExprLoc()); 5735 ExprResult ER = buildUserDefinedMapperRef( 5736 S, Stack->getCurScope(), MapperIdScopeSpec, DefaultMapperId, 5737 BaseType, /*UnresolvedMapper=*/nullptr); 5738 if (ER.isInvalid()) 5739 continue; 5740 It = Visited.try_emplace(BaseType.getTypePtr(), ER.get()).first; 5741 } 5742 // Found default mapper. 5743 if (It->second) { 5744 auto *OE = new (S.Context) OpaqueValueExpr(E->getExprLoc(), CanonType, 5745 VK_LValue, OK_Ordinary, E); 5746 OE->setIsUnique(/*V=*/true); 5747 Expr *BaseExpr = OE; 5748 for (const auto &P : ParentChain) { 5749 if (P.first) { 5750 BaseExpr = S.BuildMemberExpr( 5751 BaseExpr, /*IsArrow=*/false, E->getExprLoc(), 5752 NestedNameSpecifierLoc(), SourceLocation(), P.first, 5753 DeclAccessPair::make(P.first, P.first->getAccess()), 5754 /*HadMultipleCandidates=*/false, DeclarationNameInfo(), 5755 P.first->getType(), VK_LValue, OK_Ordinary); 5756 BaseExpr = S.DefaultLvalueConversion(BaseExpr).get(); 5757 } 5758 } 5759 if (CurFD) 5760 BaseExpr = S.BuildMemberExpr( 5761 BaseExpr, /*IsArrow=*/false, E->getExprLoc(), 5762 NestedNameSpecifierLoc(), SourceLocation(), CurFD, 5763 DeclAccessPair::make(CurFD, CurFD->getAccess()), 5764 /*HadMultipleCandidates=*/false, DeclarationNameInfo(), 5765 CurFD->getType(), VK_LValue, OK_Ordinary); 5766 SubExprs.push_back(BaseExpr); 5767 continue; 5768 } 5769 // Check for the "default" mapper for data members. 5770 bool FirstIter = true; 5771 for (FieldDecl *FD : RD->fields()) { 5772 if (!FD) 5773 continue; 5774 QualType FieldTy = FD->getType(); 5775 if (FieldTy.isNull() || 5776 !(FieldTy->isStructureOrClassType() || FieldTy->isUnionType())) 5777 continue; 5778 if (FirstIter) { 5779 FirstIter = false; 5780 ParentChain.emplace_back(CurFD, 1); 5781 } else { 5782 ++ParentChain.back().second; 5783 } 5784 Types.emplace_back(FieldTy, FD); 5785 } 5786 } 5787 } 5788 if (SubExprs.empty()) 5789 continue; 5790 CXXScopeSpec MapperIdScopeSpec; 5791 DeclarationNameInfo MapperId; 5792 if (OMPClause *NewClause = S.ActOnOpenMPMapClause( 5793 C->getMapTypeModifiers(), C->getMapTypeModifiersLoc(), 5794 MapperIdScopeSpec, MapperId, C->getMapType(), 5795 /*IsMapTypeImplicit=*/true, SourceLocation(), SourceLocation(), 5796 SubExprs, OMPVarListLocTy())) 5797 Clauses.push_back(NewClause); 5798 } 5799 } 5800 5801 StmtResult Sema::ActOnOpenMPExecutableDirective( 5802 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName, 5803 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses, 5804 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { 5805 StmtResult Res = StmtError(); 5806 OpenMPBindClauseKind BindKind = OMPC_BIND_unknown; 5807 if (const OMPBindClause *BC = 5808 OMPExecutableDirective::getSingleClause<OMPBindClause>(Clauses)) 5809 BindKind = BC->getBindKind(); 5810 // First check CancelRegion which is then used in checkNestingOfRegions. 5811 if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) || 5812 checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion, 5813 BindKind, StartLoc)) 5814 return StmtError(); 5815 5816 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit; 5817 VarsWithInheritedDSAType VarsWithInheritedDSA; 5818 bool ErrorFound = false; 5819 ClausesWithImplicit.append(Clauses.begin(), Clauses.end()); 5820 if (AStmt && !CurContext->isDependentContext() && Kind != OMPD_atomic && 5821 Kind != OMPD_critical && Kind != OMPD_section && Kind != OMPD_master && 5822 Kind != OMPD_masked && !isOpenMPLoopTransformationDirective(Kind)) { 5823 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5824 5825 // Check default data sharing attributes for referenced variables. 5826 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt)); 5827 int ThisCaptureLevel = getOpenMPCaptureLevels(Kind); 5828 Stmt *S = AStmt; 5829 while (--ThisCaptureLevel >= 0) 5830 S = cast<CapturedStmt>(S)->getCapturedStmt(); 5831 DSAChecker.Visit(S); 5832 if (!isOpenMPTargetDataManagementDirective(Kind) && 5833 !isOpenMPTaskingDirective(Kind)) { 5834 // Visit subcaptures to generate implicit clauses for captured vars. 5835 auto *CS = cast<CapturedStmt>(AStmt); 5836 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 5837 getOpenMPCaptureRegions(CaptureRegions, Kind); 5838 // Ignore outer tasking regions for target directives. 5839 if (CaptureRegions.size() > 1 && CaptureRegions.front() == OMPD_task) 5840 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 5841 DSAChecker.visitSubCaptures(CS); 5842 } 5843 if (DSAChecker.isErrorFound()) 5844 return StmtError(); 5845 // Generate list of implicitly defined firstprivate variables. 5846 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA(); 5847 5848 SmallVector<Expr *, 4> ImplicitFirstprivates( 5849 DSAChecker.getImplicitFirstprivate().begin(), 5850 DSAChecker.getImplicitFirstprivate().end()); 5851 const unsigned DefaultmapKindNum = OMPC_DEFAULTMAP_pointer + 1; 5852 SmallVector<Expr *, 4> ImplicitMaps[DefaultmapKindNum][OMPC_MAP_delete]; 5853 SmallVector<OpenMPMapModifierKind, NumberOfOMPMapClauseModifiers> 5854 ImplicitMapModifiers[DefaultmapKindNum]; 5855 SmallVector<SourceLocation, NumberOfOMPMapClauseModifiers> 5856 ImplicitMapModifiersLoc[DefaultmapKindNum]; 5857 // Get the original location of present modifier from Defaultmap clause. 5858 SourceLocation PresentModifierLocs[DefaultmapKindNum]; 5859 for (OMPClause *C : Clauses) { 5860 if (auto *DMC = dyn_cast<OMPDefaultmapClause>(C)) 5861 if (DMC->getDefaultmapModifier() == OMPC_DEFAULTMAP_MODIFIER_present) 5862 PresentModifierLocs[DMC->getDefaultmapKind()] = 5863 DMC->getDefaultmapModifierLoc(); 5864 } 5865 for (unsigned VC = 0; VC < DefaultmapKindNum; ++VC) { 5866 auto Kind = static_cast<OpenMPDefaultmapClauseKind>(VC); 5867 for (unsigned I = 0; I < OMPC_MAP_delete; ++I) { 5868 ArrayRef<Expr *> ImplicitMap = DSAChecker.getImplicitMap( 5869 Kind, static_cast<OpenMPMapClauseKind>(I)); 5870 ImplicitMaps[VC][I].append(ImplicitMap.begin(), ImplicitMap.end()); 5871 } 5872 ArrayRef<OpenMPMapModifierKind> ImplicitModifier = 5873 DSAChecker.getImplicitMapModifier(Kind); 5874 ImplicitMapModifiers[VC].append(ImplicitModifier.begin(), 5875 ImplicitModifier.end()); 5876 std::fill_n(std::back_inserter(ImplicitMapModifiersLoc[VC]), 5877 ImplicitModifier.size(), PresentModifierLocs[VC]); 5878 } 5879 // Mark taskgroup task_reduction descriptors as implicitly firstprivate. 5880 for (OMPClause *C : Clauses) { 5881 if (auto *IRC = dyn_cast<OMPInReductionClause>(C)) { 5882 for (Expr *E : IRC->taskgroup_descriptors()) 5883 if (E) 5884 ImplicitFirstprivates.emplace_back(E); 5885 } 5886 // OpenMP 5.0, 2.10.1 task Construct 5887 // [detach clause]... The event-handle will be considered as if it was 5888 // specified on a firstprivate clause. 5889 if (auto *DC = dyn_cast<OMPDetachClause>(C)) 5890 ImplicitFirstprivates.push_back(DC->getEventHandler()); 5891 } 5892 if (!ImplicitFirstprivates.empty()) { 5893 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause( 5894 ImplicitFirstprivates, SourceLocation(), SourceLocation(), 5895 SourceLocation())) { 5896 ClausesWithImplicit.push_back(Implicit); 5897 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() != 5898 ImplicitFirstprivates.size(); 5899 } else { 5900 ErrorFound = true; 5901 } 5902 } 5903 // OpenMP 5.0 [2.19.7] 5904 // If a list item appears in a reduction, lastprivate or linear 5905 // clause on a combined target construct then it is treated as 5906 // if it also appears in a map clause with a map-type of tofrom 5907 if (getLangOpts().OpenMP >= 50 && Kind != OMPD_target && 5908 isOpenMPTargetExecutionDirective(Kind)) { 5909 SmallVector<Expr *, 4> ImplicitExprs; 5910 for (OMPClause *C : Clauses) { 5911 if (auto *RC = dyn_cast<OMPReductionClause>(C)) 5912 for (Expr *E : RC->varlists()) 5913 if (!isa<DeclRefExpr>(E->IgnoreParenImpCasts())) 5914 ImplicitExprs.emplace_back(E); 5915 } 5916 if (!ImplicitExprs.empty()) { 5917 ArrayRef<Expr *> Exprs = ImplicitExprs; 5918 CXXScopeSpec MapperIdScopeSpec; 5919 DeclarationNameInfo MapperId; 5920 if (OMPClause *Implicit = ActOnOpenMPMapClause( 5921 OMPC_MAP_MODIFIER_unknown, SourceLocation(), MapperIdScopeSpec, 5922 MapperId, OMPC_MAP_tofrom, 5923 /*IsMapTypeImplicit=*/true, SourceLocation(), SourceLocation(), 5924 Exprs, OMPVarListLocTy(), /*NoDiagnose=*/true)) 5925 ClausesWithImplicit.emplace_back(Implicit); 5926 } 5927 } 5928 for (unsigned I = 0, E = DefaultmapKindNum; I < E; ++I) { 5929 int ClauseKindCnt = -1; 5930 for (ArrayRef<Expr *> ImplicitMap : ImplicitMaps[I]) { 5931 ++ClauseKindCnt; 5932 if (ImplicitMap.empty()) 5933 continue; 5934 CXXScopeSpec MapperIdScopeSpec; 5935 DeclarationNameInfo MapperId; 5936 auto Kind = static_cast<OpenMPMapClauseKind>(ClauseKindCnt); 5937 if (OMPClause *Implicit = ActOnOpenMPMapClause( 5938 ImplicitMapModifiers[I], ImplicitMapModifiersLoc[I], 5939 MapperIdScopeSpec, MapperId, Kind, /*IsMapTypeImplicit=*/true, 5940 SourceLocation(), SourceLocation(), ImplicitMap, 5941 OMPVarListLocTy())) { 5942 ClausesWithImplicit.emplace_back(Implicit); 5943 ErrorFound |= cast<OMPMapClause>(Implicit)->varlist_size() != 5944 ImplicitMap.size(); 5945 } else { 5946 ErrorFound = true; 5947 } 5948 } 5949 } 5950 // Build expressions for implicit maps of data members with 'default' 5951 // mappers. 5952 if (LangOpts.OpenMP >= 50) 5953 processImplicitMapsWithDefaultMappers(*this, DSAStack, 5954 ClausesWithImplicit); 5955 } 5956 5957 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers; 5958 switch (Kind) { 5959 case OMPD_parallel: 5960 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc, 5961 EndLoc); 5962 AllowedNameModifiers.push_back(OMPD_parallel); 5963 break; 5964 case OMPD_simd: 5965 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, 5966 VarsWithInheritedDSA); 5967 if (LangOpts.OpenMP >= 50) 5968 AllowedNameModifiers.push_back(OMPD_simd); 5969 break; 5970 case OMPD_tile: 5971 Res = 5972 ActOnOpenMPTileDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 5973 break; 5974 case OMPD_unroll: 5975 Res = ActOnOpenMPUnrollDirective(ClausesWithImplicit, AStmt, StartLoc, 5976 EndLoc); 5977 break; 5978 case OMPD_for: 5979 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, 5980 VarsWithInheritedDSA); 5981 break; 5982 case OMPD_for_simd: 5983 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 5984 EndLoc, VarsWithInheritedDSA); 5985 if (LangOpts.OpenMP >= 50) 5986 AllowedNameModifiers.push_back(OMPD_simd); 5987 break; 5988 case OMPD_sections: 5989 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc, 5990 EndLoc); 5991 break; 5992 case OMPD_section: 5993 assert(ClausesWithImplicit.empty() && 5994 "No clauses are allowed for 'omp section' directive"); 5995 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc); 5996 break; 5997 case OMPD_single: 5998 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc, 5999 EndLoc); 6000 break; 6001 case OMPD_master: 6002 assert(ClausesWithImplicit.empty() && 6003 "No clauses are allowed for 'omp master' directive"); 6004 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc); 6005 break; 6006 case OMPD_masked: 6007 Res = ActOnOpenMPMaskedDirective(ClausesWithImplicit, AStmt, StartLoc, 6008 EndLoc); 6009 break; 6010 case OMPD_critical: 6011 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt, 6012 StartLoc, EndLoc); 6013 break; 6014 case OMPD_parallel_for: 6015 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc, 6016 EndLoc, VarsWithInheritedDSA); 6017 AllowedNameModifiers.push_back(OMPD_parallel); 6018 break; 6019 case OMPD_parallel_for_simd: 6020 Res = ActOnOpenMPParallelForSimdDirective( 6021 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6022 AllowedNameModifiers.push_back(OMPD_parallel); 6023 if (LangOpts.OpenMP >= 50) 6024 AllowedNameModifiers.push_back(OMPD_simd); 6025 break; 6026 case OMPD_parallel_master: 6027 Res = ActOnOpenMPParallelMasterDirective(ClausesWithImplicit, AStmt, 6028 StartLoc, EndLoc); 6029 AllowedNameModifiers.push_back(OMPD_parallel); 6030 break; 6031 case OMPD_parallel_sections: 6032 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt, 6033 StartLoc, EndLoc); 6034 AllowedNameModifiers.push_back(OMPD_parallel); 6035 break; 6036 case OMPD_task: 6037 Res = 6038 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 6039 AllowedNameModifiers.push_back(OMPD_task); 6040 break; 6041 case OMPD_taskyield: 6042 assert(ClausesWithImplicit.empty() && 6043 "No clauses are allowed for 'omp taskyield' directive"); 6044 assert(AStmt == nullptr && 6045 "No associated statement allowed for 'omp taskyield' directive"); 6046 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc); 6047 break; 6048 case OMPD_barrier: 6049 assert(ClausesWithImplicit.empty() && 6050 "No clauses are allowed for 'omp barrier' directive"); 6051 assert(AStmt == nullptr && 6052 "No associated statement allowed for 'omp barrier' directive"); 6053 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc); 6054 break; 6055 case OMPD_taskwait: 6056 assert(AStmt == nullptr && 6057 "No associated statement allowed for 'omp taskwait' directive"); 6058 Res = ActOnOpenMPTaskwaitDirective(ClausesWithImplicit, StartLoc, EndLoc); 6059 break; 6060 case OMPD_taskgroup: 6061 Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc, 6062 EndLoc); 6063 break; 6064 case OMPD_flush: 6065 assert(AStmt == nullptr && 6066 "No associated statement allowed for 'omp flush' directive"); 6067 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc); 6068 break; 6069 case OMPD_depobj: 6070 assert(AStmt == nullptr && 6071 "No associated statement allowed for 'omp depobj' directive"); 6072 Res = ActOnOpenMPDepobjDirective(ClausesWithImplicit, StartLoc, EndLoc); 6073 break; 6074 case OMPD_scan: 6075 assert(AStmt == nullptr && 6076 "No associated statement allowed for 'omp scan' directive"); 6077 Res = ActOnOpenMPScanDirective(ClausesWithImplicit, StartLoc, EndLoc); 6078 break; 6079 case OMPD_ordered: 6080 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc, 6081 EndLoc); 6082 break; 6083 case OMPD_atomic: 6084 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc, 6085 EndLoc); 6086 break; 6087 case OMPD_teams: 6088 Res = 6089 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 6090 break; 6091 case OMPD_target: 6092 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc, 6093 EndLoc); 6094 AllowedNameModifiers.push_back(OMPD_target); 6095 break; 6096 case OMPD_target_parallel: 6097 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt, 6098 StartLoc, EndLoc); 6099 AllowedNameModifiers.push_back(OMPD_target); 6100 AllowedNameModifiers.push_back(OMPD_parallel); 6101 break; 6102 case OMPD_target_parallel_for: 6103 Res = ActOnOpenMPTargetParallelForDirective( 6104 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6105 AllowedNameModifiers.push_back(OMPD_target); 6106 AllowedNameModifiers.push_back(OMPD_parallel); 6107 break; 6108 case OMPD_cancellation_point: 6109 assert(ClausesWithImplicit.empty() && 6110 "No clauses are allowed for 'omp cancellation point' directive"); 6111 assert(AStmt == nullptr && "No associated statement allowed for 'omp " 6112 "cancellation point' directive"); 6113 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion); 6114 break; 6115 case OMPD_cancel: 6116 assert(AStmt == nullptr && 6117 "No associated statement allowed for 'omp cancel' directive"); 6118 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc, 6119 CancelRegion); 6120 AllowedNameModifiers.push_back(OMPD_cancel); 6121 break; 6122 case OMPD_target_data: 6123 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc, 6124 EndLoc); 6125 AllowedNameModifiers.push_back(OMPD_target_data); 6126 break; 6127 case OMPD_target_enter_data: 6128 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc, 6129 EndLoc, AStmt); 6130 AllowedNameModifiers.push_back(OMPD_target_enter_data); 6131 break; 6132 case OMPD_target_exit_data: 6133 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc, 6134 EndLoc, AStmt); 6135 AllowedNameModifiers.push_back(OMPD_target_exit_data); 6136 break; 6137 case OMPD_taskloop: 6138 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc, 6139 EndLoc, VarsWithInheritedDSA); 6140 AllowedNameModifiers.push_back(OMPD_taskloop); 6141 break; 6142 case OMPD_taskloop_simd: 6143 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 6144 EndLoc, VarsWithInheritedDSA); 6145 AllowedNameModifiers.push_back(OMPD_taskloop); 6146 if (LangOpts.OpenMP >= 50) 6147 AllowedNameModifiers.push_back(OMPD_simd); 6148 break; 6149 case OMPD_master_taskloop: 6150 Res = ActOnOpenMPMasterTaskLoopDirective( 6151 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6152 AllowedNameModifiers.push_back(OMPD_taskloop); 6153 break; 6154 case OMPD_master_taskloop_simd: 6155 Res = ActOnOpenMPMasterTaskLoopSimdDirective( 6156 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6157 AllowedNameModifiers.push_back(OMPD_taskloop); 6158 if (LangOpts.OpenMP >= 50) 6159 AllowedNameModifiers.push_back(OMPD_simd); 6160 break; 6161 case OMPD_parallel_master_taskloop: 6162 Res = ActOnOpenMPParallelMasterTaskLoopDirective( 6163 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6164 AllowedNameModifiers.push_back(OMPD_taskloop); 6165 AllowedNameModifiers.push_back(OMPD_parallel); 6166 break; 6167 case OMPD_parallel_master_taskloop_simd: 6168 Res = ActOnOpenMPParallelMasterTaskLoopSimdDirective( 6169 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6170 AllowedNameModifiers.push_back(OMPD_taskloop); 6171 AllowedNameModifiers.push_back(OMPD_parallel); 6172 if (LangOpts.OpenMP >= 50) 6173 AllowedNameModifiers.push_back(OMPD_simd); 6174 break; 6175 case OMPD_distribute: 6176 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc, 6177 EndLoc, VarsWithInheritedDSA); 6178 break; 6179 case OMPD_target_update: 6180 Res = ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc, 6181 EndLoc, AStmt); 6182 AllowedNameModifiers.push_back(OMPD_target_update); 6183 break; 6184 case OMPD_distribute_parallel_for: 6185 Res = ActOnOpenMPDistributeParallelForDirective( 6186 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6187 AllowedNameModifiers.push_back(OMPD_parallel); 6188 break; 6189 case OMPD_distribute_parallel_for_simd: 6190 Res = ActOnOpenMPDistributeParallelForSimdDirective( 6191 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6192 AllowedNameModifiers.push_back(OMPD_parallel); 6193 if (LangOpts.OpenMP >= 50) 6194 AllowedNameModifiers.push_back(OMPD_simd); 6195 break; 6196 case OMPD_distribute_simd: 6197 Res = ActOnOpenMPDistributeSimdDirective( 6198 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6199 if (LangOpts.OpenMP >= 50) 6200 AllowedNameModifiers.push_back(OMPD_simd); 6201 break; 6202 case OMPD_target_parallel_for_simd: 6203 Res = ActOnOpenMPTargetParallelForSimdDirective( 6204 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6205 AllowedNameModifiers.push_back(OMPD_target); 6206 AllowedNameModifiers.push_back(OMPD_parallel); 6207 if (LangOpts.OpenMP >= 50) 6208 AllowedNameModifiers.push_back(OMPD_simd); 6209 break; 6210 case OMPD_target_simd: 6211 Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 6212 EndLoc, VarsWithInheritedDSA); 6213 AllowedNameModifiers.push_back(OMPD_target); 6214 if (LangOpts.OpenMP >= 50) 6215 AllowedNameModifiers.push_back(OMPD_simd); 6216 break; 6217 case OMPD_teams_distribute: 6218 Res = ActOnOpenMPTeamsDistributeDirective( 6219 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6220 break; 6221 case OMPD_teams_distribute_simd: 6222 Res = ActOnOpenMPTeamsDistributeSimdDirective( 6223 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6224 if (LangOpts.OpenMP >= 50) 6225 AllowedNameModifiers.push_back(OMPD_simd); 6226 break; 6227 case OMPD_teams_distribute_parallel_for_simd: 6228 Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective( 6229 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6230 AllowedNameModifiers.push_back(OMPD_parallel); 6231 if (LangOpts.OpenMP >= 50) 6232 AllowedNameModifiers.push_back(OMPD_simd); 6233 break; 6234 case OMPD_teams_distribute_parallel_for: 6235 Res = ActOnOpenMPTeamsDistributeParallelForDirective( 6236 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6237 AllowedNameModifiers.push_back(OMPD_parallel); 6238 break; 6239 case OMPD_target_teams: 6240 Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, 6241 EndLoc); 6242 AllowedNameModifiers.push_back(OMPD_target); 6243 break; 6244 case OMPD_target_teams_distribute: 6245 Res = ActOnOpenMPTargetTeamsDistributeDirective( 6246 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6247 AllowedNameModifiers.push_back(OMPD_target); 6248 break; 6249 case OMPD_target_teams_distribute_parallel_for: 6250 Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective( 6251 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6252 AllowedNameModifiers.push_back(OMPD_target); 6253 AllowedNameModifiers.push_back(OMPD_parallel); 6254 break; 6255 case OMPD_target_teams_distribute_parallel_for_simd: 6256 Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective( 6257 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6258 AllowedNameModifiers.push_back(OMPD_target); 6259 AllowedNameModifiers.push_back(OMPD_parallel); 6260 if (LangOpts.OpenMP >= 50) 6261 AllowedNameModifiers.push_back(OMPD_simd); 6262 break; 6263 case OMPD_target_teams_distribute_simd: 6264 Res = ActOnOpenMPTargetTeamsDistributeSimdDirective( 6265 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6266 AllowedNameModifiers.push_back(OMPD_target); 6267 if (LangOpts.OpenMP >= 50) 6268 AllowedNameModifiers.push_back(OMPD_simd); 6269 break; 6270 case OMPD_interop: 6271 assert(AStmt == nullptr && 6272 "No associated statement allowed for 'omp interop' directive"); 6273 Res = ActOnOpenMPInteropDirective(ClausesWithImplicit, StartLoc, EndLoc); 6274 break; 6275 case OMPD_dispatch: 6276 Res = ActOnOpenMPDispatchDirective(ClausesWithImplicit, AStmt, StartLoc, 6277 EndLoc); 6278 break; 6279 case OMPD_loop: 6280 Res = ActOnOpenMPGenericLoopDirective(ClausesWithImplicit, AStmt, StartLoc, 6281 EndLoc, VarsWithInheritedDSA); 6282 break; 6283 case OMPD_declare_target: 6284 case OMPD_end_declare_target: 6285 case OMPD_threadprivate: 6286 case OMPD_allocate: 6287 case OMPD_declare_reduction: 6288 case OMPD_declare_mapper: 6289 case OMPD_declare_simd: 6290 case OMPD_requires: 6291 case OMPD_declare_variant: 6292 case OMPD_begin_declare_variant: 6293 case OMPD_end_declare_variant: 6294 llvm_unreachable("OpenMP Directive is not allowed"); 6295 case OMPD_unknown: 6296 default: 6297 llvm_unreachable("Unknown OpenMP directive"); 6298 } 6299 6300 ErrorFound = Res.isInvalid() || ErrorFound; 6301 6302 // Check variables in the clauses if default(none) or 6303 // default(firstprivate) was specified. 6304 if (DSAStack->getDefaultDSA() == DSA_none || 6305 DSAStack->getDefaultDSA() == DSA_firstprivate) { 6306 DSAAttrChecker DSAChecker(DSAStack, *this, nullptr); 6307 for (OMPClause *C : Clauses) { 6308 switch (C->getClauseKind()) { 6309 case OMPC_num_threads: 6310 case OMPC_dist_schedule: 6311 // Do not analyse if no parent teams directive. 6312 if (isOpenMPTeamsDirective(Kind)) 6313 break; 6314 continue; 6315 case OMPC_if: 6316 if (isOpenMPTeamsDirective(Kind) && 6317 cast<OMPIfClause>(C)->getNameModifier() != OMPD_target) 6318 break; 6319 if (isOpenMPParallelDirective(Kind) && 6320 isOpenMPTaskLoopDirective(Kind) && 6321 cast<OMPIfClause>(C)->getNameModifier() != OMPD_parallel) 6322 break; 6323 continue; 6324 case OMPC_schedule: 6325 case OMPC_detach: 6326 break; 6327 case OMPC_grainsize: 6328 case OMPC_num_tasks: 6329 case OMPC_final: 6330 case OMPC_priority: 6331 case OMPC_novariants: 6332 case OMPC_nocontext: 6333 // Do not analyze if no parent parallel directive. 6334 if (isOpenMPParallelDirective(Kind)) 6335 break; 6336 continue; 6337 case OMPC_ordered: 6338 case OMPC_device: 6339 case OMPC_num_teams: 6340 case OMPC_thread_limit: 6341 case OMPC_hint: 6342 case OMPC_collapse: 6343 case OMPC_safelen: 6344 case OMPC_simdlen: 6345 case OMPC_sizes: 6346 case OMPC_default: 6347 case OMPC_proc_bind: 6348 case OMPC_private: 6349 case OMPC_firstprivate: 6350 case OMPC_lastprivate: 6351 case OMPC_shared: 6352 case OMPC_reduction: 6353 case OMPC_task_reduction: 6354 case OMPC_in_reduction: 6355 case OMPC_linear: 6356 case OMPC_aligned: 6357 case OMPC_copyin: 6358 case OMPC_copyprivate: 6359 case OMPC_nowait: 6360 case OMPC_untied: 6361 case OMPC_mergeable: 6362 case OMPC_allocate: 6363 case OMPC_read: 6364 case OMPC_write: 6365 case OMPC_update: 6366 case OMPC_capture: 6367 case OMPC_compare: 6368 case OMPC_seq_cst: 6369 case OMPC_acq_rel: 6370 case OMPC_acquire: 6371 case OMPC_release: 6372 case OMPC_relaxed: 6373 case OMPC_depend: 6374 case OMPC_threads: 6375 case OMPC_simd: 6376 case OMPC_map: 6377 case OMPC_nogroup: 6378 case OMPC_defaultmap: 6379 case OMPC_to: 6380 case OMPC_from: 6381 case OMPC_use_device_ptr: 6382 case OMPC_use_device_addr: 6383 case OMPC_is_device_ptr: 6384 case OMPC_nontemporal: 6385 case OMPC_order: 6386 case OMPC_destroy: 6387 case OMPC_inclusive: 6388 case OMPC_exclusive: 6389 case OMPC_uses_allocators: 6390 case OMPC_affinity: 6391 case OMPC_bind: 6392 continue; 6393 case OMPC_allocator: 6394 case OMPC_flush: 6395 case OMPC_depobj: 6396 case OMPC_threadprivate: 6397 case OMPC_uniform: 6398 case OMPC_unknown: 6399 case OMPC_unified_address: 6400 case OMPC_unified_shared_memory: 6401 case OMPC_reverse_offload: 6402 case OMPC_dynamic_allocators: 6403 case OMPC_atomic_default_mem_order: 6404 case OMPC_device_type: 6405 case OMPC_match: 6406 case OMPC_when: 6407 default: 6408 llvm_unreachable("Unexpected clause"); 6409 } 6410 for (Stmt *CC : C->children()) { 6411 if (CC) 6412 DSAChecker.Visit(CC); 6413 } 6414 } 6415 for (const auto &P : DSAChecker.getVarsWithInheritedDSA()) 6416 VarsWithInheritedDSA[P.getFirst()] = P.getSecond(); 6417 } 6418 for (const auto &P : VarsWithInheritedDSA) { 6419 if (P.getFirst()->isImplicit() || isa<OMPCapturedExprDecl>(P.getFirst())) 6420 continue; 6421 ErrorFound = true; 6422 if (DSAStack->getDefaultDSA() == DSA_none || 6423 DSAStack->getDefaultDSA() == DSA_firstprivate) { 6424 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable) 6425 << P.first << P.second->getSourceRange(); 6426 Diag(DSAStack->getDefaultDSALocation(), diag::note_omp_default_dsa_none); 6427 } else if (getLangOpts().OpenMP >= 50) { 6428 Diag(P.second->getExprLoc(), 6429 diag::err_omp_defaultmap_no_attr_for_variable) 6430 << P.first << P.second->getSourceRange(); 6431 Diag(DSAStack->getDefaultDSALocation(), 6432 diag::note_omp_defaultmap_attr_none); 6433 } 6434 } 6435 6436 if (!AllowedNameModifiers.empty()) 6437 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) || 6438 ErrorFound; 6439 6440 if (ErrorFound) 6441 return StmtError(); 6442 6443 if (!CurContext->isDependentContext() && 6444 isOpenMPTargetExecutionDirective(Kind) && 6445 !(DSAStack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() || 6446 DSAStack->hasRequiresDeclWithClause<OMPUnifiedAddressClause>() || 6447 DSAStack->hasRequiresDeclWithClause<OMPReverseOffloadClause>() || 6448 DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())) { 6449 // Register target to DSA Stack. 6450 DSAStack->addTargetDirLocation(StartLoc); 6451 } 6452 6453 return Res; 6454 } 6455 6456 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective( 6457 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen, 6458 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds, 6459 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears, 6460 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) { 6461 assert(Aligneds.size() == Alignments.size()); 6462 assert(Linears.size() == LinModifiers.size()); 6463 assert(Linears.size() == Steps.size()); 6464 if (!DG || DG.get().isNull()) 6465 return DeclGroupPtrTy(); 6466 6467 const int SimdId = 0; 6468 if (!DG.get().isSingleDecl()) { 6469 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd_variant) 6470 << SimdId; 6471 return DG; 6472 } 6473 Decl *ADecl = DG.get().getSingleDecl(); 6474 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl)) 6475 ADecl = FTD->getTemplatedDecl(); 6476 6477 auto *FD = dyn_cast<FunctionDecl>(ADecl); 6478 if (!FD) { 6479 Diag(ADecl->getLocation(), diag::err_omp_function_expected) << SimdId; 6480 return DeclGroupPtrTy(); 6481 } 6482 6483 // OpenMP [2.8.2, declare simd construct, Description] 6484 // The parameter of the simdlen clause must be a constant positive integer 6485 // expression. 6486 ExprResult SL; 6487 if (Simdlen) 6488 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen); 6489 // OpenMP [2.8.2, declare simd construct, Description] 6490 // The special this pointer can be used as if was one of the arguments to the 6491 // function in any of the linear, aligned, or uniform clauses. 6492 // The uniform clause declares one or more arguments to have an invariant 6493 // value for all concurrent invocations of the function in the execution of a 6494 // single SIMD loop. 6495 llvm::DenseMap<const Decl *, const Expr *> UniformedArgs; 6496 const Expr *UniformedLinearThis = nullptr; 6497 for (const Expr *E : Uniforms) { 6498 E = E->IgnoreParenImpCasts(); 6499 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 6500 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) 6501 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 6502 FD->getParamDecl(PVD->getFunctionScopeIndex()) 6503 ->getCanonicalDecl() == PVD->getCanonicalDecl()) { 6504 UniformedArgs.try_emplace(PVD->getCanonicalDecl(), E); 6505 continue; 6506 } 6507 if (isa<CXXThisExpr>(E)) { 6508 UniformedLinearThis = E; 6509 continue; 6510 } 6511 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 6512 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 6513 } 6514 // OpenMP [2.8.2, declare simd construct, Description] 6515 // The aligned clause declares that the object to which each list item points 6516 // is aligned to the number of bytes expressed in the optional parameter of 6517 // the aligned clause. 6518 // The special this pointer can be used as if was one of the arguments to the 6519 // function in any of the linear, aligned, or uniform clauses. 6520 // The type of list items appearing in the aligned clause must be array, 6521 // pointer, reference to array, or reference to pointer. 6522 llvm::DenseMap<const Decl *, const Expr *> AlignedArgs; 6523 const Expr *AlignedThis = nullptr; 6524 for (const Expr *E : Aligneds) { 6525 E = E->IgnoreParenImpCasts(); 6526 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 6527 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 6528 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 6529 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 6530 FD->getParamDecl(PVD->getFunctionScopeIndex()) 6531 ->getCanonicalDecl() == CanonPVD) { 6532 // OpenMP [2.8.1, simd construct, Restrictions] 6533 // A list-item cannot appear in more than one aligned clause. 6534 if (AlignedArgs.count(CanonPVD) > 0) { 6535 Diag(E->getExprLoc(), diag::err_omp_used_in_clause_twice) 6536 << 1 << getOpenMPClauseName(OMPC_aligned) 6537 << E->getSourceRange(); 6538 Diag(AlignedArgs[CanonPVD]->getExprLoc(), 6539 diag::note_omp_explicit_dsa) 6540 << getOpenMPClauseName(OMPC_aligned); 6541 continue; 6542 } 6543 AlignedArgs[CanonPVD] = E; 6544 QualType QTy = PVD->getType() 6545 .getNonReferenceType() 6546 .getUnqualifiedType() 6547 .getCanonicalType(); 6548 const Type *Ty = QTy.getTypePtrOrNull(); 6549 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) { 6550 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr) 6551 << QTy << getLangOpts().CPlusPlus << E->getSourceRange(); 6552 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD; 6553 } 6554 continue; 6555 } 6556 } 6557 if (isa<CXXThisExpr>(E)) { 6558 if (AlignedThis) { 6559 Diag(E->getExprLoc(), diag::err_omp_used_in_clause_twice) 6560 << 2 << getOpenMPClauseName(OMPC_aligned) << E->getSourceRange(); 6561 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa) 6562 << getOpenMPClauseName(OMPC_aligned); 6563 } 6564 AlignedThis = E; 6565 continue; 6566 } 6567 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 6568 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 6569 } 6570 // The optional parameter of the aligned clause, alignment, must be a constant 6571 // positive integer expression. If no optional parameter is specified, 6572 // implementation-defined default alignments for SIMD instructions on the 6573 // target platforms are assumed. 6574 SmallVector<const Expr *, 4> NewAligns; 6575 for (Expr *E : Alignments) { 6576 ExprResult Align; 6577 if (E) 6578 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned); 6579 NewAligns.push_back(Align.get()); 6580 } 6581 // OpenMP [2.8.2, declare simd construct, Description] 6582 // The linear clause declares one or more list items to be private to a SIMD 6583 // lane and to have a linear relationship with respect to the iteration space 6584 // of a loop. 6585 // The special this pointer can be used as if was one of the arguments to the 6586 // function in any of the linear, aligned, or uniform clauses. 6587 // When a linear-step expression is specified in a linear clause it must be 6588 // either a constant integer expression or an integer-typed parameter that is 6589 // specified in a uniform clause on the directive. 6590 llvm::DenseMap<const Decl *, const Expr *> LinearArgs; 6591 const bool IsUniformedThis = UniformedLinearThis != nullptr; 6592 auto MI = LinModifiers.begin(); 6593 for (const Expr *E : Linears) { 6594 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI); 6595 ++MI; 6596 E = E->IgnoreParenImpCasts(); 6597 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 6598 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 6599 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 6600 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 6601 FD->getParamDecl(PVD->getFunctionScopeIndex()) 6602 ->getCanonicalDecl() == CanonPVD) { 6603 // OpenMP [2.15.3.7, linear Clause, Restrictions] 6604 // A list-item cannot appear in more than one linear clause. 6605 if (LinearArgs.count(CanonPVD) > 0) { 6606 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 6607 << getOpenMPClauseName(OMPC_linear) 6608 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange(); 6609 Diag(LinearArgs[CanonPVD]->getExprLoc(), 6610 diag::note_omp_explicit_dsa) 6611 << getOpenMPClauseName(OMPC_linear); 6612 continue; 6613 } 6614 // Each argument can appear in at most one uniform or linear clause. 6615 if (UniformedArgs.count(CanonPVD) > 0) { 6616 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 6617 << getOpenMPClauseName(OMPC_linear) 6618 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange(); 6619 Diag(UniformedArgs[CanonPVD]->getExprLoc(), 6620 diag::note_omp_explicit_dsa) 6621 << getOpenMPClauseName(OMPC_uniform); 6622 continue; 6623 } 6624 LinearArgs[CanonPVD] = E; 6625 if (E->isValueDependent() || E->isTypeDependent() || 6626 E->isInstantiationDependent() || 6627 E->containsUnexpandedParameterPack()) 6628 continue; 6629 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind, 6630 PVD->getOriginalType(), 6631 /*IsDeclareSimd=*/true); 6632 continue; 6633 } 6634 } 6635 if (isa<CXXThisExpr>(E)) { 6636 if (UniformedLinearThis) { 6637 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 6638 << getOpenMPClauseName(OMPC_linear) 6639 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear) 6640 << E->getSourceRange(); 6641 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa) 6642 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform 6643 : OMPC_linear); 6644 continue; 6645 } 6646 UniformedLinearThis = E; 6647 if (E->isValueDependent() || E->isTypeDependent() || 6648 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 6649 continue; 6650 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind, 6651 E->getType(), /*IsDeclareSimd=*/true); 6652 continue; 6653 } 6654 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 6655 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 6656 } 6657 Expr *Step = nullptr; 6658 Expr *NewStep = nullptr; 6659 SmallVector<Expr *, 4> NewSteps; 6660 for (Expr *E : Steps) { 6661 // Skip the same step expression, it was checked already. 6662 if (Step == E || !E) { 6663 NewSteps.push_back(E ? NewStep : nullptr); 6664 continue; 6665 } 6666 Step = E; 6667 if (const auto *DRE = dyn_cast<DeclRefExpr>(Step)) 6668 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 6669 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 6670 if (UniformedArgs.count(CanonPVD) == 0) { 6671 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param) 6672 << Step->getSourceRange(); 6673 } else if (E->isValueDependent() || E->isTypeDependent() || 6674 E->isInstantiationDependent() || 6675 E->containsUnexpandedParameterPack() || 6676 CanonPVD->getType()->hasIntegerRepresentation()) { 6677 NewSteps.push_back(Step); 6678 } else { 6679 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param) 6680 << Step->getSourceRange(); 6681 } 6682 continue; 6683 } 6684 NewStep = Step; 6685 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && 6686 !Step->isInstantiationDependent() && 6687 !Step->containsUnexpandedParameterPack()) { 6688 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step) 6689 .get(); 6690 if (NewStep) 6691 NewStep = 6692 VerifyIntegerConstantExpression(NewStep, /*FIXME*/ AllowFold).get(); 6693 } 6694 NewSteps.push_back(NewStep); 6695 } 6696 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit( 6697 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()), 6698 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(), 6699 const_cast<Expr **>(NewAligns.data()), NewAligns.size(), 6700 const_cast<Expr **>(Linears.data()), Linears.size(), 6701 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(), 6702 NewSteps.data(), NewSteps.size(), SR); 6703 ADecl->addAttr(NewAttr); 6704 return DG; 6705 } 6706 6707 static void setPrototype(Sema &S, FunctionDecl *FD, FunctionDecl *FDWithProto, 6708 QualType NewType) { 6709 assert(NewType->isFunctionProtoType() && 6710 "Expected function type with prototype."); 6711 assert(FD->getType()->isFunctionNoProtoType() && 6712 "Expected function with type with no prototype."); 6713 assert(FDWithProto->getType()->isFunctionProtoType() && 6714 "Expected function with prototype."); 6715 // Synthesize parameters with the same types. 6716 FD->setType(NewType); 6717 SmallVector<ParmVarDecl *, 16> Params; 6718 for (const ParmVarDecl *P : FDWithProto->parameters()) { 6719 auto *Param = ParmVarDecl::Create(S.getASTContext(), FD, SourceLocation(), 6720 SourceLocation(), nullptr, P->getType(), 6721 /*TInfo=*/nullptr, SC_None, nullptr); 6722 Param->setScopeInfo(0, Params.size()); 6723 Param->setImplicit(); 6724 Params.push_back(Param); 6725 } 6726 6727 FD->setParams(Params); 6728 } 6729 6730 void Sema::ActOnFinishedFunctionDefinitionInOpenMPAssumeScope(Decl *D) { 6731 if (D->isInvalidDecl()) 6732 return; 6733 FunctionDecl *FD = nullptr; 6734 if (auto *UTemplDecl = dyn_cast<FunctionTemplateDecl>(D)) 6735 FD = UTemplDecl->getTemplatedDecl(); 6736 else 6737 FD = cast<FunctionDecl>(D); 6738 assert(FD && "Expected a function declaration!"); 6739 6740 // If we are instantiating templates we do *not* apply scoped assumptions but 6741 // only global ones. We apply scoped assumption to the template definition 6742 // though. 6743 if (!inTemplateInstantiation()) { 6744 for (AssumptionAttr *AA : OMPAssumeScoped) 6745 FD->addAttr(AA); 6746 } 6747 for (AssumptionAttr *AA : OMPAssumeGlobal) 6748 FD->addAttr(AA); 6749 } 6750 6751 Sema::OMPDeclareVariantScope::OMPDeclareVariantScope(OMPTraitInfo &TI) 6752 : TI(&TI), NameSuffix(TI.getMangledName()) {} 6753 6754 void Sema::ActOnStartOfFunctionDefinitionInOpenMPDeclareVariantScope( 6755 Scope *S, Declarator &D, MultiTemplateParamsArg TemplateParamLists, 6756 SmallVectorImpl<FunctionDecl *> &Bases) { 6757 if (!D.getIdentifier()) 6758 return; 6759 6760 OMPDeclareVariantScope &DVScope = OMPDeclareVariantScopes.back(); 6761 6762 // Template specialization is an extension, check if we do it. 6763 bool IsTemplated = !TemplateParamLists.empty(); 6764 if (IsTemplated & 6765 !DVScope.TI->isExtensionActive( 6766 llvm::omp::TraitProperty::implementation_extension_allow_templates)) 6767 return; 6768 6769 IdentifierInfo *BaseII = D.getIdentifier(); 6770 LookupResult Lookup(*this, DeclarationName(BaseII), D.getIdentifierLoc(), 6771 LookupOrdinaryName); 6772 LookupParsedName(Lookup, S, &D.getCXXScopeSpec()); 6773 6774 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); 6775 QualType FType = TInfo->getType(); 6776 6777 bool IsConstexpr = 6778 D.getDeclSpec().getConstexprSpecifier() == ConstexprSpecKind::Constexpr; 6779 bool IsConsteval = 6780 D.getDeclSpec().getConstexprSpecifier() == ConstexprSpecKind::Consteval; 6781 6782 for (auto *Candidate : Lookup) { 6783 auto *CandidateDecl = Candidate->getUnderlyingDecl(); 6784 FunctionDecl *UDecl = nullptr; 6785 if (IsTemplated && isa<FunctionTemplateDecl>(CandidateDecl)) { 6786 auto *FTD = cast<FunctionTemplateDecl>(CandidateDecl); 6787 if (FTD->getTemplateParameters()->size() == TemplateParamLists.size()) 6788 UDecl = FTD->getTemplatedDecl(); 6789 } else if (!IsTemplated) 6790 UDecl = dyn_cast<FunctionDecl>(CandidateDecl); 6791 if (!UDecl) 6792 continue; 6793 6794 // Don't specialize constexpr/consteval functions with 6795 // non-constexpr/consteval functions. 6796 if (UDecl->isConstexpr() && !IsConstexpr) 6797 continue; 6798 if (UDecl->isConsteval() && !IsConsteval) 6799 continue; 6800 6801 QualType UDeclTy = UDecl->getType(); 6802 if (!UDeclTy->isDependentType()) { 6803 QualType NewType = Context.mergeFunctionTypes( 6804 FType, UDeclTy, /* OfBlockPointer */ false, 6805 /* Unqualified */ false, /* AllowCXX */ true); 6806 if (NewType.isNull()) 6807 continue; 6808 } 6809 6810 // Found a base! 6811 Bases.push_back(UDecl); 6812 } 6813 6814 bool UseImplicitBase = !DVScope.TI->isExtensionActive( 6815 llvm::omp::TraitProperty::implementation_extension_disable_implicit_base); 6816 // If no base was found we create a declaration that we use as base. 6817 if (Bases.empty() && UseImplicitBase) { 6818 D.setFunctionDefinitionKind(FunctionDefinitionKind::Declaration); 6819 Decl *BaseD = HandleDeclarator(S, D, TemplateParamLists); 6820 BaseD->setImplicit(true); 6821 if (auto *BaseTemplD = dyn_cast<FunctionTemplateDecl>(BaseD)) 6822 Bases.push_back(BaseTemplD->getTemplatedDecl()); 6823 else 6824 Bases.push_back(cast<FunctionDecl>(BaseD)); 6825 } 6826 6827 std::string MangledName; 6828 MangledName += D.getIdentifier()->getName(); 6829 MangledName += getOpenMPVariantManglingSeparatorStr(); 6830 MangledName += DVScope.NameSuffix; 6831 IdentifierInfo &VariantII = Context.Idents.get(MangledName); 6832 6833 VariantII.setMangledOpenMPVariantName(true); 6834 D.SetIdentifier(&VariantII, D.getBeginLoc()); 6835 } 6836 6837 void Sema::ActOnFinishedFunctionDefinitionInOpenMPDeclareVariantScope( 6838 Decl *D, SmallVectorImpl<FunctionDecl *> &Bases) { 6839 // Do not mark function as is used to prevent its emission if this is the 6840 // only place where it is used. 6841 EnterExpressionEvaluationContext Unevaluated( 6842 *this, Sema::ExpressionEvaluationContext::Unevaluated); 6843 6844 FunctionDecl *FD = nullptr; 6845 if (auto *UTemplDecl = dyn_cast<FunctionTemplateDecl>(D)) 6846 FD = UTemplDecl->getTemplatedDecl(); 6847 else 6848 FD = cast<FunctionDecl>(D); 6849 auto *VariantFuncRef = DeclRefExpr::Create( 6850 Context, NestedNameSpecifierLoc(), SourceLocation(), FD, 6851 /* RefersToEnclosingVariableOrCapture */ false, 6852 /* NameLoc */ FD->getLocation(), FD->getType(), 6853 ExprValueKind::VK_PRValue); 6854 6855 OMPDeclareVariantScope &DVScope = OMPDeclareVariantScopes.back(); 6856 auto *OMPDeclareVariantA = OMPDeclareVariantAttr::CreateImplicit( 6857 Context, VariantFuncRef, DVScope.TI, 6858 /*NothingArgs=*/nullptr, /*NothingArgsSize=*/0, 6859 /*NeedDevicePtrArgs=*/nullptr, /*NeedDevicePtrArgsSize=*/0, 6860 /*AppendArgs=*/nullptr, /*AppendArgsSize=*/0); 6861 for (FunctionDecl *BaseFD : Bases) 6862 BaseFD->addAttr(OMPDeclareVariantA); 6863 } 6864 6865 ExprResult Sema::ActOnOpenMPCall(ExprResult Call, Scope *Scope, 6866 SourceLocation LParenLoc, 6867 MultiExprArg ArgExprs, 6868 SourceLocation RParenLoc, Expr *ExecConfig) { 6869 // The common case is a regular call we do not want to specialize at all. Try 6870 // to make that case fast by bailing early. 6871 CallExpr *CE = dyn_cast<CallExpr>(Call.get()); 6872 if (!CE) 6873 return Call; 6874 6875 FunctionDecl *CalleeFnDecl = CE->getDirectCallee(); 6876 if (!CalleeFnDecl) 6877 return Call; 6878 6879 if (!CalleeFnDecl->hasAttr<OMPDeclareVariantAttr>()) 6880 return Call; 6881 6882 ASTContext &Context = getASTContext(); 6883 std::function<void(StringRef)> DiagUnknownTrait = [this, 6884 CE](StringRef ISATrait) { 6885 // TODO Track the selector locations in a way that is accessible here to 6886 // improve the diagnostic location. 6887 Diag(CE->getBeginLoc(), diag::warn_unknown_declare_variant_isa_trait) 6888 << ISATrait; 6889 }; 6890 TargetOMPContext OMPCtx(Context, std::move(DiagUnknownTrait), 6891 getCurFunctionDecl(), DSAStack->getConstructTraits()); 6892 6893 QualType CalleeFnType = CalleeFnDecl->getType(); 6894 6895 SmallVector<Expr *, 4> Exprs; 6896 SmallVector<VariantMatchInfo, 4> VMIs; 6897 while (CalleeFnDecl) { 6898 for (OMPDeclareVariantAttr *A : 6899 CalleeFnDecl->specific_attrs<OMPDeclareVariantAttr>()) { 6900 Expr *VariantRef = A->getVariantFuncRef(); 6901 6902 VariantMatchInfo VMI; 6903 OMPTraitInfo &TI = A->getTraitInfo(); 6904 TI.getAsVariantMatchInfo(Context, VMI); 6905 if (!isVariantApplicableInContext(VMI, OMPCtx, 6906 /* DeviceSetOnly */ false)) 6907 continue; 6908 6909 VMIs.push_back(VMI); 6910 Exprs.push_back(VariantRef); 6911 } 6912 6913 CalleeFnDecl = CalleeFnDecl->getPreviousDecl(); 6914 } 6915 6916 ExprResult NewCall; 6917 do { 6918 int BestIdx = getBestVariantMatchForContext(VMIs, OMPCtx); 6919 if (BestIdx < 0) 6920 return Call; 6921 Expr *BestExpr = cast<DeclRefExpr>(Exprs[BestIdx]); 6922 Decl *BestDecl = cast<DeclRefExpr>(BestExpr)->getDecl(); 6923 6924 { 6925 // Try to build a (member) call expression for the current best applicable 6926 // variant expression. We allow this to fail in which case we continue 6927 // with the next best variant expression. The fail case is part of the 6928 // implementation defined behavior in the OpenMP standard when it talks 6929 // about what differences in the function prototypes: "Any differences 6930 // that the specific OpenMP context requires in the prototype of the 6931 // variant from the base function prototype are implementation defined." 6932 // This wording is there to allow the specialized variant to have a 6933 // different type than the base function. This is intended and OK but if 6934 // we cannot create a call the difference is not in the "implementation 6935 // defined range" we allow. 6936 Sema::TentativeAnalysisScope Trap(*this); 6937 6938 if (auto *SpecializedMethod = dyn_cast<CXXMethodDecl>(BestDecl)) { 6939 auto *MemberCall = dyn_cast<CXXMemberCallExpr>(CE); 6940 BestExpr = MemberExpr::CreateImplicit( 6941 Context, MemberCall->getImplicitObjectArgument(), 6942 /* IsArrow */ false, SpecializedMethod, Context.BoundMemberTy, 6943 MemberCall->getValueKind(), MemberCall->getObjectKind()); 6944 } 6945 NewCall = BuildCallExpr(Scope, BestExpr, LParenLoc, ArgExprs, RParenLoc, 6946 ExecConfig); 6947 if (NewCall.isUsable()) { 6948 if (CallExpr *NCE = dyn_cast<CallExpr>(NewCall.get())) { 6949 FunctionDecl *NewCalleeFnDecl = NCE->getDirectCallee(); 6950 QualType NewType = Context.mergeFunctionTypes( 6951 CalleeFnType, NewCalleeFnDecl->getType(), 6952 /* OfBlockPointer */ false, 6953 /* Unqualified */ false, /* AllowCXX */ true); 6954 if (!NewType.isNull()) 6955 break; 6956 // Don't use the call if the function type was not compatible. 6957 NewCall = nullptr; 6958 } 6959 } 6960 } 6961 6962 VMIs.erase(VMIs.begin() + BestIdx); 6963 Exprs.erase(Exprs.begin() + BestIdx); 6964 } while (!VMIs.empty()); 6965 6966 if (!NewCall.isUsable()) 6967 return Call; 6968 return PseudoObjectExpr::Create(Context, CE, {NewCall.get()}, 0); 6969 } 6970 6971 Optional<std::pair<FunctionDecl *, Expr *>> 6972 Sema::checkOpenMPDeclareVariantFunction(Sema::DeclGroupPtrTy DG, 6973 Expr *VariantRef, OMPTraitInfo &TI, 6974 unsigned NumAppendArgs, 6975 SourceRange SR) { 6976 if (!DG || DG.get().isNull()) 6977 return None; 6978 6979 const int VariantId = 1; 6980 // Must be applied only to single decl. 6981 if (!DG.get().isSingleDecl()) { 6982 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd_variant) 6983 << VariantId << SR; 6984 return None; 6985 } 6986 Decl *ADecl = DG.get().getSingleDecl(); 6987 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl)) 6988 ADecl = FTD->getTemplatedDecl(); 6989 6990 // Decl must be a function. 6991 auto *FD = dyn_cast<FunctionDecl>(ADecl); 6992 if (!FD) { 6993 Diag(ADecl->getLocation(), diag::err_omp_function_expected) 6994 << VariantId << SR; 6995 return None; 6996 } 6997 6998 auto &&HasMultiVersionAttributes = [](const FunctionDecl *FD) { 6999 return FD->hasAttrs() && 7000 (FD->hasAttr<CPUDispatchAttr>() || FD->hasAttr<CPUSpecificAttr>() || 7001 FD->hasAttr<TargetAttr>()); 7002 }; 7003 // OpenMP is not compatible with CPU-specific attributes. 7004 if (HasMultiVersionAttributes(FD)) { 7005 Diag(FD->getLocation(), diag::err_omp_declare_variant_incompat_attributes) 7006 << SR; 7007 return None; 7008 } 7009 7010 // Allow #pragma omp declare variant only if the function is not used. 7011 if (FD->isUsed(false)) 7012 Diag(SR.getBegin(), diag::warn_omp_declare_variant_after_used) 7013 << FD->getLocation(); 7014 7015 // Check if the function was emitted already. 7016 const FunctionDecl *Definition; 7017 if (!FD->isThisDeclarationADefinition() && FD->isDefined(Definition) && 7018 (LangOpts.EmitAllDecls || Context.DeclMustBeEmitted(Definition))) 7019 Diag(SR.getBegin(), diag::warn_omp_declare_variant_after_emitted) 7020 << FD->getLocation(); 7021 7022 // The VariantRef must point to function. 7023 if (!VariantRef) { 7024 Diag(SR.getBegin(), diag::err_omp_function_expected) << VariantId; 7025 return None; 7026 } 7027 7028 auto ShouldDelayChecks = [](Expr *&E, bool) { 7029 return E && (E->isTypeDependent() || E->isValueDependent() || 7030 E->containsUnexpandedParameterPack() || 7031 E->isInstantiationDependent()); 7032 }; 7033 // Do not check templates, wait until instantiation. 7034 if (FD->isDependentContext() || ShouldDelayChecks(VariantRef, false) || 7035 TI.anyScoreOrCondition(ShouldDelayChecks)) 7036 return std::make_pair(FD, VariantRef); 7037 7038 // Deal with non-constant score and user condition expressions. 7039 auto HandleNonConstantScoresAndConditions = [this](Expr *&E, 7040 bool IsScore) -> bool { 7041 if (!E || E->isIntegerConstantExpr(Context)) 7042 return false; 7043 7044 if (IsScore) { 7045 // We warn on non-constant scores and pretend they were not present. 7046 Diag(E->getExprLoc(), diag::warn_omp_declare_variant_score_not_constant) 7047 << E; 7048 E = nullptr; 7049 } else { 7050 // We could replace a non-constant user condition with "false" but we 7051 // will soon need to handle these anyway for the dynamic version of 7052 // OpenMP context selectors. 7053 Diag(E->getExprLoc(), 7054 diag::err_omp_declare_variant_user_condition_not_constant) 7055 << E; 7056 } 7057 return true; 7058 }; 7059 if (TI.anyScoreOrCondition(HandleNonConstantScoresAndConditions)) 7060 return None; 7061 7062 QualType AdjustedFnType = FD->getType(); 7063 if (NumAppendArgs) { 7064 const auto *PTy = AdjustedFnType->getAsAdjusted<FunctionProtoType>(); 7065 if (!PTy) { 7066 Diag(FD->getLocation(), diag::err_omp_declare_variant_prototype_required) 7067 << SR; 7068 return None; 7069 } 7070 // Adjust the function type to account for an extra omp_interop_t for each 7071 // specified in the append_args clause. 7072 const TypeDecl *TD = nullptr; 7073 LookupResult Result(*this, &Context.Idents.get("omp_interop_t"), 7074 SR.getBegin(), Sema::LookupOrdinaryName); 7075 if (LookupName(Result, getCurScope())) { 7076 NamedDecl *ND = Result.getFoundDecl(); 7077 TD = dyn_cast_or_null<TypeDecl>(ND); 7078 } 7079 if (!TD) { 7080 Diag(SR.getBegin(), diag::err_omp_interop_type_not_found) << SR; 7081 return None; 7082 } 7083 QualType InteropType = Context.getTypeDeclType(TD); 7084 if (PTy->isVariadic()) { 7085 Diag(FD->getLocation(), diag::err_omp_append_args_with_varargs) << SR; 7086 return None; 7087 } 7088 llvm::SmallVector<QualType, 8> Params; 7089 Params.append(PTy->param_type_begin(), PTy->param_type_end()); 7090 Params.insert(Params.end(), NumAppendArgs, InteropType); 7091 AdjustedFnType = Context.getFunctionType(PTy->getReturnType(), Params, 7092 PTy->getExtProtoInfo()); 7093 } 7094 7095 // Convert VariantRef expression to the type of the original function to 7096 // resolve possible conflicts. 7097 ExprResult VariantRefCast = VariantRef; 7098 if (LangOpts.CPlusPlus) { 7099 QualType FnPtrType; 7100 auto *Method = dyn_cast<CXXMethodDecl>(FD); 7101 if (Method && !Method->isStatic()) { 7102 const Type *ClassType = 7103 Context.getTypeDeclType(Method->getParent()).getTypePtr(); 7104 FnPtrType = Context.getMemberPointerType(AdjustedFnType, ClassType); 7105 ExprResult ER; 7106 { 7107 // Build adrr_of unary op to correctly handle type checks for member 7108 // functions. 7109 Sema::TentativeAnalysisScope Trap(*this); 7110 ER = CreateBuiltinUnaryOp(VariantRef->getBeginLoc(), UO_AddrOf, 7111 VariantRef); 7112 } 7113 if (!ER.isUsable()) { 7114 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected) 7115 << VariantId << VariantRef->getSourceRange(); 7116 return None; 7117 } 7118 VariantRef = ER.get(); 7119 } else { 7120 FnPtrType = Context.getPointerType(AdjustedFnType); 7121 } 7122 QualType VarianPtrType = Context.getPointerType(VariantRef->getType()); 7123 if (VarianPtrType.getUnqualifiedType() != FnPtrType.getUnqualifiedType()) { 7124 ImplicitConversionSequence ICS = TryImplicitConversion( 7125 VariantRef, FnPtrType.getUnqualifiedType(), 7126 /*SuppressUserConversions=*/false, AllowedExplicit::None, 7127 /*InOverloadResolution=*/false, 7128 /*CStyle=*/false, 7129 /*AllowObjCWritebackConversion=*/false); 7130 if (ICS.isFailure()) { 7131 Diag(VariantRef->getExprLoc(), 7132 diag::err_omp_declare_variant_incompat_types) 7133 << VariantRef->getType() 7134 << ((Method && !Method->isStatic()) ? FnPtrType : FD->getType()) 7135 << (NumAppendArgs ? 1 : 0) << VariantRef->getSourceRange(); 7136 return None; 7137 } 7138 VariantRefCast = PerformImplicitConversion( 7139 VariantRef, FnPtrType.getUnqualifiedType(), AA_Converting); 7140 if (!VariantRefCast.isUsable()) 7141 return None; 7142 } 7143 // Drop previously built artificial addr_of unary op for member functions. 7144 if (Method && !Method->isStatic()) { 7145 Expr *PossibleAddrOfVariantRef = VariantRefCast.get(); 7146 if (auto *UO = dyn_cast<UnaryOperator>( 7147 PossibleAddrOfVariantRef->IgnoreImplicit())) 7148 VariantRefCast = UO->getSubExpr(); 7149 } 7150 } 7151 7152 ExprResult ER = CheckPlaceholderExpr(VariantRefCast.get()); 7153 if (!ER.isUsable() || 7154 !ER.get()->IgnoreParenImpCasts()->getType()->isFunctionType()) { 7155 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected) 7156 << VariantId << VariantRef->getSourceRange(); 7157 return None; 7158 } 7159 7160 // The VariantRef must point to function. 7161 auto *DRE = dyn_cast<DeclRefExpr>(ER.get()->IgnoreParenImpCasts()); 7162 if (!DRE) { 7163 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected) 7164 << VariantId << VariantRef->getSourceRange(); 7165 return None; 7166 } 7167 auto *NewFD = dyn_cast_or_null<FunctionDecl>(DRE->getDecl()); 7168 if (!NewFD) { 7169 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected) 7170 << VariantId << VariantRef->getSourceRange(); 7171 return None; 7172 } 7173 7174 // Check if function types are compatible in C. 7175 if (!LangOpts.CPlusPlus) { 7176 QualType NewType = 7177 Context.mergeFunctionTypes(AdjustedFnType, NewFD->getType()); 7178 if (NewType.isNull()) { 7179 Diag(VariantRef->getExprLoc(), 7180 diag::err_omp_declare_variant_incompat_types) 7181 << NewFD->getType() << FD->getType() << (NumAppendArgs ? 1 : 0) 7182 << VariantRef->getSourceRange(); 7183 return None; 7184 } 7185 if (NewType->isFunctionProtoType()) { 7186 if (FD->getType()->isFunctionNoProtoType()) 7187 setPrototype(*this, FD, NewFD, NewType); 7188 else if (NewFD->getType()->isFunctionNoProtoType()) 7189 setPrototype(*this, NewFD, FD, NewType); 7190 } 7191 } 7192 7193 // Check if variant function is not marked with declare variant directive. 7194 if (NewFD->hasAttrs() && NewFD->hasAttr<OMPDeclareVariantAttr>()) { 7195 Diag(VariantRef->getExprLoc(), 7196 diag::warn_omp_declare_variant_marked_as_declare_variant) 7197 << VariantRef->getSourceRange(); 7198 SourceRange SR = 7199 NewFD->specific_attr_begin<OMPDeclareVariantAttr>()->getRange(); 7200 Diag(SR.getBegin(), diag::note_omp_marked_declare_variant_here) << SR; 7201 return None; 7202 } 7203 7204 enum DoesntSupport { 7205 VirtFuncs = 1, 7206 Constructors = 3, 7207 Destructors = 4, 7208 DeletedFuncs = 5, 7209 DefaultedFuncs = 6, 7210 ConstexprFuncs = 7, 7211 ConstevalFuncs = 8, 7212 }; 7213 if (const auto *CXXFD = dyn_cast<CXXMethodDecl>(FD)) { 7214 if (CXXFD->isVirtual()) { 7215 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 7216 << VirtFuncs; 7217 return None; 7218 } 7219 7220 if (isa<CXXConstructorDecl>(FD)) { 7221 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 7222 << Constructors; 7223 return None; 7224 } 7225 7226 if (isa<CXXDestructorDecl>(FD)) { 7227 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 7228 << Destructors; 7229 return None; 7230 } 7231 } 7232 7233 if (FD->isDeleted()) { 7234 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 7235 << DeletedFuncs; 7236 return None; 7237 } 7238 7239 if (FD->isDefaulted()) { 7240 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 7241 << DefaultedFuncs; 7242 return None; 7243 } 7244 7245 if (FD->isConstexpr()) { 7246 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 7247 << (NewFD->isConsteval() ? ConstevalFuncs : ConstexprFuncs); 7248 return None; 7249 } 7250 7251 // Check general compatibility. 7252 if (areMultiversionVariantFunctionsCompatible( 7253 FD, NewFD, PartialDiagnostic::NullDiagnostic(), 7254 PartialDiagnosticAt(SourceLocation(), 7255 PartialDiagnostic::NullDiagnostic()), 7256 PartialDiagnosticAt( 7257 VariantRef->getExprLoc(), 7258 PDiag(diag::err_omp_declare_variant_doesnt_support)), 7259 PartialDiagnosticAt(VariantRef->getExprLoc(), 7260 PDiag(diag::err_omp_declare_variant_diff) 7261 << FD->getLocation()), 7262 /*TemplatesSupported=*/true, /*ConstexprSupported=*/false, 7263 /*CLinkageMayDiffer=*/true)) 7264 return None; 7265 return std::make_pair(FD, cast<Expr>(DRE)); 7266 } 7267 7268 void Sema::ActOnOpenMPDeclareVariantDirective( 7269 FunctionDecl *FD, Expr *VariantRef, OMPTraitInfo &TI, 7270 ArrayRef<Expr *> AdjustArgsNothing, 7271 ArrayRef<Expr *> AdjustArgsNeedDevicePtr, 7272 ArrayRef<OMPDeclareVariantAttr::InteropType> AppendArgs, 7273 SourceLocation AdjustArgsLoc, SourceLocation AppendArgsLoc, 7274 SourceRange SR) { 7275 7276 // OpenMP 5.1 [2.3.5, declare variant directive, Restrictions] 7277 // An adjust_args clause or append_args clause can only be specified if the 7278 // dispatch selector of the construct selector set appears in the match 7279 // clause. 7280 7281 SmallVector<Expr *, 8> AllAdjustArgs; 7282 llvm::append_range(AllAdjustArgs, AdjustArgsNothing); 7283 llvm::append_range(AllAdjustArgs, AdjustArgsNeedDevicePtr); 7284 7285 if (!AllAdjustArgs.empty() || !AppendArgs.empty()) { 7286 VariantMatchInfo VMI; 7287 TI.getAsVariantMatchInfo(Context, VMI); 7288 if (!llvm::is_contained( 7289 VMI.ConstructTraits, 7290 llvm::omp::TraitProperty::construct_dispatch_dispatch)) { 7291 if (!AllAdjustArgs.empty()) 7292 Diag(AdjustArgsLoc, diag::err_omp_clause_requires_dispatch_construct) 7293 << getOpenMPClauseName(OMPC_adjust_args); 7294 if (!AppendArgs.empty()) 7295 Diag(AppendArgsLoc, diag::err_omp_clause_requires_dispatch_construct) 7296 << getOpenMPClauseName(OMPC_append_args); 7297 return; 7298 } 7299 } 7300 7301 // OpenMP 5.1 [2.3.5, declare variant directive, Restrictions] 7302 // Each argument can only appear in a single adjust_args clause for each 7303 // declare variant directive. 7304 llvm::SmallPtrSet<const VarDecl *, 4> AdjustVars; 7305 7306 for (Expr *E : AllAdjustArgs) { 7307 E = E->IgnoreParenImpCasts(); 7308 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) { 7309 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 7310 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 7311 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 7312 FD->getParamDecl(PVD->getFunctionScopeIndex()) 7313 ->getCanonicalDecl() == CanonPVD) { 7314 // It's a parameter of the function, check duplicates. 7315 if (!AdjustVars.insert(CanonPVD).second) { 7316 Diag(DRE->getLocation(), diag::err_omp_adjust_arg_multiple_clauses) 7317 << PVD; 7318 return; 7319 } 7320 continue; 7321 } 7322 } 7323 } 7324 // Anything that is not a function parameter is an error. 7325 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) << FD << 0; 7326 return; 7327 } 7328 7329 auto *NewAttr = OMPDeclareVariantAttr::CreateImplicit( 7330 Context, VariantRef, &TI, const_cast<Expr **>(AdjustArgsNothing.data()), 7331 AdjustArgsNothing.size(), 7332 const_cast<Expr **>(AdjustArgsNeedDevicePtr.data()), 7333 AdjustArgsNeedDevicePtr.size(), 7334 const_cast<OMPDeclareVariantAttr::InteropType *>(AppendArgs.data()), 7335 AppendArgs.size(), SR); 7336 FD->addAttr(NewAttr); 7337 } 7338 7339 StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses, 7340 Stmt *AStmt, 7341 SourceLocation StartLoc, 7342 SourceLocation EndLoc) { 7343 if (!AStmt) 7344 return StmtError(); 7345 7346 auto *CS = cast<CapturedStmt>(AStmt); 7347 // 1.2.2 OpenMP Language Terminology 7348 // Structured block - An executable statement with a single entry at the 7349 // top and a single exit at the bottom. 7350 // The point of exit cannot be a branch out of the structured block. 7351 // longjmp() and throw() must not violate the entry/exit criteria. 7352 CS->getCapturedDecl()->setNothrow(); 7353 7354 setFunctionHasBranchProtectedScope(); 7355 7356 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 7357 DSAStack->getTaskgroupReductionRef(), 7358 DSAStack->isCancelRegion()); 7359 } 7360 7361 namespace { 7362 /// Iteration space of a single for loop. 7363 struct LoopIterationSpace final { 7364 /// True if the condition operator is the strict compare operator (<, > or 7365 /// !=). 7366 bool IsStrictCompare = false; 7367 /// Condition of the loop. 7368 Expr *PreCond = nullptr; 7369 /// This expression calculates the number of iterations in the loop. 7370 /// It is always possible to calculate it before starting the loop. 7371 Expr *NumIterations = nullptr; 7372 /// The loop counter variable. 7373 Expr *CounterVar = nullptr; 7374 /// Private loop counter variable. 7375 Expr *PrivateCounterVar = nullptr; 7376 /// This is initializer for the initial value of #CounterVar. 7377 Expr *CounterInit = nullptr; 7378 /// This is step for the #CounterVar used to generate its update: 7379 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration. 7380 Expr *CounterStep = nullptr; 7381 /// Should step be subtracted? 7382 bool Subtract = false; 7383 /// Source range of the loop init. 7384 SourceRange InitSrcRange; 7385 /// Source range of the loop condition. 7386 SourceRange CondSrcRange; 7387 /// Source range of the loop increment. 7388 SourceRange IncSrcRange; 7389 /// Minimum value that can have the loop control variable. Used to support 7390 /// non-rectangular loops. Applied only for LCV with the non-iterator types, 7391 /// since only such variables can be used in non-loop invariant expressions. 7392 Expr *MinValue = nullptr; 7393 /// Maximum value that can have the loop control variable. Used to support 7394 /// non-rectangular loops. Applied only for LCV with the non-iterator type, 7395 /// since only such variables can be used in non-loop invariant expressions. 7396 Expr *MaxValue = nullptr; 7397 /// true, if the lower bound depends on the outer loop control var. 7398 bool IsNonRectangularLB = false; 7399 /// true, if the upper bound depends on the outer loop control var. 7400 bool IsNonRectangularUB = false; 7401 /// Index of the loop this loop depends on and forms non-rectangular loop 7402 /// nest. 7403 unsigned LoopDependentIdx = 0; 7404 /// Final condition for the non-rectangular loop nest support. It is used to 7405 /// check that the number of iterations for this particular counter must be 7406 /// finished. 7407 Expr *FinalCondition = nullptr; 7408 }; 7409 7410 /// Helper class for checking canonical form of the OpenMP loops and 7411 /// extracting iteration space of each loop in the loop nest, that will be used 7412 /// for IR generation. 7413 class OpenMPIterationSpaceChecker { 7414 /// Reference to Sema. 7415 Sema &SemaRef; 7416 /// Does the loop associated directive support non-rectangular loops? 7417 bool SupportsNonRectangular; 7418 /// Data-sharing stack. 7419 DSAStackTy &Stack; 7420 /// A location for diagnostics (when there is no some better location). 7421 SourceLocation DefaultLoc; 7422 /// A location for diagnostics (when increment is not compatible). 7423 SourceLocation ConditionLoc; 7424 /// A source location for referring to loop init later. 7425 SourceRange InitSrcRange; 7426 /// A source location for referring to condition later. 7427 SourceRange ConditionSrcRange; 7428 /// A source location for referring to increment later. 7429 SourceRange IncrementSrcRange; 7430 /// Loop variable. 7431 ValueDecl *LCDecl = nullptr; 7432 /// Reference to loop variable. 7433 Expr *LCRef = nullptr; 7434 /// Lower bound (initializer for the var). 7435 Expr *LB = nullptr; 7436 /// Upper bound. 7437 Expr *UB = nullptr; 7438 /// Loop step (increment). 7439 Expr *Step = nullptr; 7440 /// This flag is true when condition is one of: 7441 /// Var < UB 7442 /// Var <= UB 7443 /// UB > Var 7444 /// UB >= Var 7445 /// This will have no value when the condition is != 7446 llvm::Optional<bool> TestIsLessOp; 7447 /// This flag is true when condition is strict ( < or > ). 7448 bool TestIsStrictOp = false; 7449 /// This flag is true when step is subtracted on each iteration. 7450 bool SubtractStep = false; 7451 /// The outer loop counter this loop depends on (if any). 7452 const ValueDecl *DepDecl = nullptr; 7453 /// Contains number of loop (starts from 1) on which loop counter init 7454 /// expression of this loop depends on. 7455 Optional<unsigned> InitDependOnLC; 7456 /// Contains number of loop (starts from 1) on which loop counter condition 7457 /// expression of this loop depends on. 7458 Optional<unsigned> CondDependOnLC; 7459 /// Checks if the provide statement depends on the loop counter. 7460 Optional<unsigned> doesDependOnLoopCounter(const Stmt *S, bool IsInitializer); 7461 /// Original condition required for checking of the exit condition for 7462 /// non-rectangular loop. 7463 Expr *Condition = nullptr; 7464 7465 public: 7466 OpenMPIterationSpaceChecker(Sema &SemaRef, bool SupportsNonRectangular, 7467 DSAStackTy &Stack, SourceLocation DefaultLoc) 7468 : SemaRef(SemaRef), SupportsNonRectangular(SupportsNonRectangular), 7469 Stack(Stack), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {} 7470 /// Check init-expr for canonical loop form and save loop counter 7471 /// variable - #Var and its initialization value - #LB. 7472 bool checkAndSetInit(Stmt *S, bool EmitDiags = true); 7473 /// Check test-expr for canonical form, save upper-bound (#UB), flags 7474 /// for less/greater and for strict/non-strict comparison. 7475 bool checkAndSetCond(Expr *S); 7476 /// Check incr-expr for canonical loop form and return true if it 7477 /// does not conform, otherwise save loop step (#Step). 7478 bool checkAndSetInc(Expr *S); 7479 /// Return the loop counter variable. 7480 ValueDecl *getLoopDecl() const { return LCDecl; } 7481 /// Return the reference expression to loop counter variable. 7482 Expr *getLoopDeclRefExpr() const { return LCRef; } 7483 /// Source range of the loop init. 7484 SourceRange getInitSrcRange() const { return InitSrcRange; } 7485 /// Source range of the loop condition. 7486 SourceRange getConditionSrcRange() const { return ConditionSrcRange; } 7487 /// Source range of the loop increment. 7488 SourceRange getIncrementSrcRange() const { return IncrementSrcRange; } 7489 /// True if the step should be subtracted. 7490 bool shouldSubtractStep() const { return SubtractStep; } 7491 /// True, if the compare operator is strict (<, > or !=). 7492 bool isStrictTestOp() const { return TestIsStrictOp; } 7493 /// Build the expression to calculate the number of iterations. 7494 Expr *buildNumIterations( 7495 Scope *S, ArrayRef<LoopIterationSpace> ResultIterSpaces, bool LimitedType, 7496 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 7497 /// Build the precondition expression for the loops. 7498 Expr * 7499 buildPreCond(Scope *S, Expr *Cond, 7500 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 7501 /// Build reference expression to the counter be used for codegen. 7502 DeclRefExpr * 7503 buildCounterVar(llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 7504 DSAStackTy &DSA) const; 7505 /// Build reference expression to the private counter be used for 7506 /// codegen. 7507 Expr *buildPrivateCounterVar() const; 7508 /// Build initialization of the counter be used for codegen. 7509 Expr *buildCounterInit() const; 7510 /// Build step of the counter be used for codegen. 7511 Expr *buildCounterStep() const; 7512 /// Build loop data with counter value for depend clauses in ordered 7513 /// directives. 7514 Expr * 7515 buildOrderedLoopData(Scope *S, Expr *Counter, 7516 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 7517 SourceLocation Loc, Expr *Inc = nullptr, 7518 OverloadedOperatorKind OOK = OO_Amp); 7519 /// Builds the minimum value for the loop counter. 7520 std::pair<Expr *, Expr *> buildMinMaxValues( 7521 Scope *S, llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 7522 /// Builds final condition for the non-rectangular loops. 7523 Expr *buildFinalCondition(Scope *S) const; 7524 /// Return true if any expression is dependent. 7525 bool dependent() const; 7526 /// Returns true if the initializer forms non-rectangular loop. 7527 bool doesInitDependOnLC() const { return InitDependOnLC.hasValue(); } 7528 /// Returns true if the condition forms non-rectangular loop. 7529 bool doesCondDependOnLC() const { return CondDependOnLC.hasValue(); } 7530 /// Returns index of the loop we depend on (starting from 1), or 0 otherwise. 7531 unsigned getLoopDependentIdx() const { 7532 return InitDependOnLC.getValueOr(CondDependOnLC.getValueOr(0)); 7533 } 7534 7535 private: 7536 /// Check the right-hand side of an assignment in the increment 7537 /// expression. 7538 bool checkAndSetIncRHS(Expr *RHS); 7539 /// Helper to set loop counter variable and its initializer. 7540 bool setLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB, 7541 bool EmitDiags); 7542 /// Helper to set upper bound. 7543 bool setUB(Expr *NewUB, llvm::Optional<bool> LessOp, bool StrictOp, 7544 SourceRange SR, SourceLocation SL); 7545 /// Helper to set loop increment. 7546 bool setStep(Expr *NewStep, bool Subtract); 7547 }; 7548 7549 bool OpenMPIterationSpaceChecker::dependent() const { 7550 if (!LCDecl) { 7551 assert(!LB && !UB && !Step); 7552 return false; 7553 } 7554 return LCDecl->getType()->isDependentType() || 7555 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) || 7556 (Step && Step->isValueDependent()); 7557 } 7558 7559 bool OpenMPIterationSpaceChecker::setLCDeclAndLB(ValueDecl *NewLCDecl, 7560 Expr *NewLCRefExpr, 7561 Expr *NewLB, bool EmitDiags) { 7562 // State consistency checking to ensure correct usage. 7563 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr && 7564 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp); 7565 if (!NewLCDecl || !NewLB || NewLB->containsErrors()) 7566 return true; 7567 LCDecl = getCanonicalDecl(NewLCDecl); 7568 LCRef = NewLCRefExpr; 7569 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB)) 7570 if (const CXXConstructorDecl *Ctor = CE->getConstructor()) 7571 if ((Ctor->isCopyOrMoveConstructor() || 7572 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && 7573 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) 7574 NewLB = CE->getArg(0)->IgnoreParenImpCasts(); 7575 LB = NewLB; 7576 if (EmitDiags) 7577 InitDependOnLC = doesDependOnLoopCounter(LB, /*IsInitializer=*/true); 7578 return false; 7579 } 7580 7581 bool OpenMPIterationSpaceChecker::setUB(Expr *NewUB, 7582 llvm::Optional<bool> LessOp, 7583 bool StrictOp, SourceRange SR, 7584 SourceLocation SL) { 7585 // State consistency checking to ensure correct usage. 7586 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr && 7587 Step == nullptr && !TestIsLessOp && !TestIsStrictOp); 7588 if (!NewUB || NewUB->containsErrors()) 7589 return true; 7590 UB = NewUB; 7591 if (LessOp) 7592 TestIsLessOp = LessOp; 7593 TestIsStrictOp = StrictOp; 7594 ConditionSrcRange = SR; 7595 ConditionLoc = SL; 7596 CondDependOnLC = doesDependOnLoopCounter(UB, /*IsInitializer=*/false); 7597 return false; 7598 } 7599 7600 bool OpenMPIterationSpaceChecker::setStep(Expr *NewStep, bool Subtract) { 7601 // State consistency checking to ensure correct usage. 7602 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr); 7603 if (!NewStep || NewStep->containsErrors()) 7604 return true; 7605 if (!NewStep->isValueDependent()) { 7606 // Check that the step is integer expression. 7607 SourceLocation StepLoc = NewStep->getBeginLoc(); 7608 ExprResult Val = SemaRef.PerformOpenMPImplicitIntegerConversion( 7609 StepLoc, getExprAsWritten(NewStep)); 7610 if (Val.isInvalid()) 7611 return true; 7612 NewStep = Val.get(); 7613 7614 // OpenMP [2.6, Canonical Loop Form, Restrictions] 7615 // If test-expr is of form var relational-op b and relational-op is < or 7616 // <= then incr-expr must cause var to increase on each iteration of the 7617 // loop. If test-expr is of form var relational-op b and relational-op is 7618 // > or >= then incr-expr must cause var to decrease on each iteration of 7619 // the loop. 7620 // If test-expr is of form b relational-op var and relational-op is < or 7621 // <= then incr-expr must cause var to decrease on each iteration of the 7622 // loop. If test-expr is of form b relational-op var and relational-op is 7623 // > or >= then incr-expr must cause var to increase on each iteration of 7624 // the loop. 7625 Optional<llvm::APSInt> Result = 7626 NewStep->getIntegerConstantExpr(SemaRef.Context); 7627 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation(); 7628 bool IsConstNeg = 7629 Result && Result->isSigned() && (Subtract != Result->isNegative()); 7630 bool IsConstPos = 7631 Result && Result->isSigned() && (Subtract == Result->isNegative()); 7632 bool IsConstZero = Result && !Result->getBoolValue(); 7633 7634 // != with increment is treated as <; != with decrement is treated as > 7635 if (!TestIsLessOp.hasValue()) 7636 TestIsLessOp = IsConstPos || (IsUnsigned && !Subtract); 7637 if (UB && 7638 (IsConstZero || (TestIsLessOp.getValue() 7639 ? (IsConstNeg || (IsUnsigned && Subtract)) 7640 : (IsConstPos || (IsUnsigned && !Subtract))))) { 7641 SemaRef.Diag(NewStep->getExprLoc(), 7642 diag::err_omp_loop_incr_not_compatible) 7643 << LCDecl << TestIsLessOp.getValue() << NewStep->getSourceRange(); 7644 SemaRef.Diag(ConditionLoc, 7645 diag::note_omp_loop_cond_requres_compatible_incr) 7646 << TestIsLessOp.getValue() << ConditionSrcRange; 7647 return true; 7648 } 7649 if (TestIsLessOp.getValue() == Subtract) { 7650 NewStep = 7651 SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep) 7652 .get(); 7653 Subtract = !Subtract; 7654 } 7655 } 7656 7657 Step = NewStep; 7658 SubtractStep = Subtract; 7659 return false; 7660 } 7661 7662 namespace { 7663 /// Checker for the non-rectangular loops. Checks if the initializer or 7664 /// condition expression references loop counter variable. 7665 class LoopCounterRefChecker final 7666 : public ConstStmtVisitor<LoopCounterRefChecker, bool> { 7667 Sema &SemaRef; 7668 DSAStackTy &Stack; 7669 const ValueDecl *CurLCDecl = nullptr; 7670 const ValueDecl *DepDecl = nullptr; 7671 const ValueDecl *PrevDepDecl = nullptr; 7672 bool IsInitializer = true; 7673 bool SupportsNonRectangular; 7674 unsigned BaseLoopId = 0; 7675 bool checkDecl(const Expr *E, const ValueDecl *VD) { 7676 if (getCanonicalDecl(VD) == getCanonicalDecl(CurLCDecl)) { 7677 SemaRef.Diag(E->getExprLoc(), diag::err_omp_stmt_depends_on_loop_counter) 7678 << (IsInitializer ? 0 : 1); 7679 return false; 7680 } 7681 const auto &&Data = Stack.isLoopControlVariable(VD); 7682 // OpenMP, 2.9.1 Canonical Loop Form, Restrictions. 7683 // The type of the loop iterator on which we depend may not have a random 7684 // access iterator type. 7685 if (Data.first && VD->getType()->isRecordType()) { 7686 SmallString<128> Name; 7687 llvm::raw_svector_ostream OS(Name); 7688 VD->getNameForDiagnostic(OS, SemaRef.getPrintingPolicy(), 7689 /*Qualified=*/true); 7690 SemaRef.Diag(E->getExprLoc(), 7691 diag::err_omp_wrong_dependency_iterator_type) 7692 << OS.str(); 7693 SemaRef.Diag(VD->getLocation(), diag::note_previous_decl) << VD; 7694 return false; 7695 } 7696 if (Data.first && !SupportsNonRectangular) { 7697 SemaRef.Diag(E->getExprLoc(), diag::err_omp_invariant_dependency); 7698 return false; 7699 } 7700 if (Data.first && 7701 (DepDecl || (PrevDepDecl && 7702 getCanonicalDecl(VD) != getCanonicalDecl(PrevDepDecl)))) { 7703 if (!DepDecl && PrevDepDecl) 7704 DepDecl = PrevDepDecl; 7705 SmallString<128> Name; 7706 llvm::raw_svector_ostream OS(Name); 7707 DepDecl->getNameForDiagnostic(OS, SemaRef.getPrintingPolicy(), 7708 /*Qualified=*/true); 7709 SemaRef.Diag(E->getExprLoc(), 7710 diag::err_omp_invariant_or_linear_dependency) 7711 << OS.str(); 7712 return false; 7713 } 7714 if (Data.first) { 7715 DepDecl = VD; 7716 BaseLoopId = Data.first; 7717 } 7718 return Data.first; 7719 } 7720 7721 public: 7722 bool VisitDeclRefExpr(const DeclRefExpr *E) { 7723 const ValueDecl *VD = E->getDecl(); 7724 if (isa<VarDecl>(VD)) 7725 return checkDecl(E, VD); 7726 return false; 7727 } 7728 bool VisitMemberExpr(const MemberExpr *E) { 7729 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) { 7730 const ValueDecl *VD = E->getMemberDecl(); 7731 if (isa<VarDecl>(VD) || isa<FieldDecl>(VD)) 7732 return checkDecl(E, VD); 7733 } 7734 return false; 7735 } 7736 bool VisitStmt(const Stmt *S) { 7737 bool Res = false; 7738 for (const Stmt *Child : S->children()) 7739 Res = (Child && Visit(Child)) || Res; 7740 return Res; 7741 } 7742 explicit LoopCounterRefChecker(Sema &SemaRef, DSAStackTy &Stack, 7743 const ValueDecl *CurLCDecl, bool IsInitializer, 7744 const ValueDecl *PrevDepDecl = nullptr, 7745 bool SupportsNonRectangular = true) 7746 : SemaRef(SemaRef), Stack(Stack), CurLCDecl(CurLCDecl), 7747 PrevDepDecl(PrevDepDecl), IsInitializer(IsInitializer), 7748 SupportsNonRectangular(SupportsNonRectangular) {} 7749 unsigned getBaseLoopId() const { 7750 assert(CurLCDecl && "Expected loop dependency."); 7751 return BaseLoopId; 7752 } 7753 const ValueDecl *getDepDecl() const { 7754 assert(CurLCDecl && "Expected loop dependency."); 7755 return DepDecl; 7756 } 7757 }; 7758 } // namespace 7759 7760 Optional<unsigned> 7761 OpenMPIterationSpaceChecker::doesDependOnLoopCounter(const Stmt *S, 7762 bool IsInitializer) { 7763 // Check for the non-rectangular loops. 7764 LoopCounterRefChecker LoopStmtChecker(SemaRef, Stack, LCDecl, IsInitializer, 7765 DepDecl, SupportsNonRectangular); 7766 if (LoopStmtChecker.Visit(S)) { 7767 DepDecl = LoopStmtChecker.getDepDecl(); 7768 return LoopStmtChecker.getBaseLoopId(); 7769 } 7770 return llvm::None; 7771 } 7772 7773 bool OpenMPIterationSpaceChecker::checkAndSetInit(Stmt *S, bool EmitDiags) { 7774 // Check init-expr for canonical loop form and save loop counter 7775 // variable - #Var and its initialization value - #LB. 7776 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following: 7777 // var = lb 7778 // integer-type var = lb 7779 // random-access-iterator-type var = lb 7780 // pointer-type var = lb 7781 // 7782 if (!S) { 7783 if (EmitDiags) { 7784 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init); 7785 } 7786 return true; 7787 } 7788 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S)) 7789 if (!ExprTemp->cleanupsHaveSideEffects()) 7790 S = ExprTemp->getSubExpr(); 7791 7792 InitSrcRange = S->getSourceRange(); 7793 if (Expr *E = dyn_cast<Expr>(S)) 7794 S = E->IgnoreParens(); 7795 if (auto *BO = dyn_cast<BinaryOperator>(S)) { 7796 if (BO->getOpcode() == BO_Assign) { 7797 Expr *LHS = BO->getLHS()->IgnoreParens(); 7798 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) { 7799 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) 7800 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 7801 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 7802 EmitDiags); 7803 return setLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS(), EmitDiags); 7804 } 7805 if (auto *ME = dyn_cast<MemberExpr>(LHS)) { 7806 if (ME->isArrow() && 7807 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 7808 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 7809 EmitDiags); 7810 } 7811 } 7812 } else if (auto *DS = dyn_cast<DeclStmt>(S)) { 7813 if (DS->isSingleDecl()) { 7814 if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) { 7815 if (Var->hasInit() && !Var->getType()->isReferenceType()) { 7816 // Accept non-canonical init form here but emit ext. warning. 7817 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags) 7818 SemaRef.Diag(S->getBeginLoc(), 7819 diag::ext_omp_loop_not_canonical_init) 7820 << S->getSourceRange(); 7821 return setLCDeclAndLB( 7822 Var, 7823 buildDeclRefExpr(SemaRef, Var, 7824 Var->getType().getNonReferenceType(), 7825 DS->getBeginLoc()), 7826 Var->getInit(), EmitDiags); 7827 } 7828 } 7829 } 7830 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 7831 if (CE->getOperator() == OO_Equal) { 7832 Expr *LHS = CE->getArg(0); 7833 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) { 7834 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) 7835 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 7836 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 7837 EmitDiags); 7838 return setLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1), EmitDiags); 7839 } 7840 if (auto *ME = dyn_cast<MemberExpr>(LHS)) { 7841 if (ME->isArrow() && 7842 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 7843 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 7844 EmitDiags); 7845 } 7846 } 7847 } 7848 7849 if (dependent() || SemaRef.CurContext->isDependentContext()) 7850 return false; 7851 if (EmitDiags) { 7852 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_init) 7853 << S->getSourceRange(); 7854 } 7855 return true; 7856 } 7857 7858 /// Ignore parenthesizes, implicit casts, copy constructor and return the 7859 /// variable (which may be the loop variable) if possible. 7860 static const ValueDecl *getInitLCDecl(const Expr *E) { 7861 if (!E) 7862 return nullptr; 7863 E = getExprAsWritten(E); 7864 if (const auto *CE = dyn_cast_or_null<CXXConstructExpr>(E)) 7865 if (const CXXConstructorDecl *Ctor = CE->getConstructor()) 7866 if ((Ctor->isCopyOrMoveConstructor() || 7867 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && 7868 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) 7869 E = CE->getArg(0)->IgnoreParenImpCasts(); 7870 if (const auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) { 7871 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) 7872 return getCanonicalDecl(VD); 7873 } 7874 if (const auto *ME = dyn_cast_or_null<MemberExpr>(E)) 7875 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 7876 return getCanonicalDecl(ME->getMemberDecl()); 7877 return nullptr; 7878 } 7879 7880 bool OpenMPIterationSpaceChecker::checkAndSetCond(Expr *S) { 7881 // Check test-expr for canonical form, save upper-bound UB, flags for 7882 // less/greater and for strict/non-strict comparison. 7883 // OpenMP [2.9] Canonical loop form. Test-expr may be one of the following: 7884 // var relational-op b 7885 // b relational-op var 7886 // 7887 bool IneqCondIsCanonical = SemaRef.getLangOpts().OpenMP >= 50; 7888 if (!S) { 7889 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) 7890 << (IneqCondIsCanonical ? 1 : 0) << LCDecl; 7891 return true; 7892 } 7893 Condition = S; 7894 S = getExprAsWritten(S); 7895 SourceLocation CondLoc = S->getBeginLoc(); 7896 auto &&CheckAndSetCond = [this, IneqCondIsCanonical]( 7897 BinaryOperatorKind Opcode, const Expr *LHS, 7898 const Expr *RHS, SourceRange SR, 7899 SourceLocation OpLoc) -> llvm::Optional<bool> { 7900 if (BinaryOperator::isRelationalOp(Opcode)) { 7901 if (getInitLCDecl(LHS) == LCDecl) 7902 return setUB(const_cast<Expr *>(RHS), 7903 (Opcode == BO_LT || Opcode == BO_LE), 7904 (Opcode == BO_LT || Opcode == BO_GT), SR, OpLoc); 7905 if (getInitLCDecl(RHS) == LCDecl) 7906 return setUB(const_cast<Expr *>(LHS), 7907 (Opcode == BO_GT || Opcode == BO_GE), 7908 (Opcode == BO_LT || Opcode == BO_GT), SR, OpLoc); 7909 } else if (IneqCondIsCanonical && Opcode == BO_NE) { 7910 return setUB(const_cast<Expr *>(getInitLCDecl(LHS) == LCDecl ? RHS : LHS), 7911 /*LessOp=*/llvm::None, 7912 /*StrictOp=*/true, SR, OpLoc); 7913 } 7914 return llvm::None; 7915 }; 7916 llvm::Optional<bool> Res; 7917 if (auto *RBO = dyn_cast<CXXRewrittenBinaryOperator>(S)) { 7918 CXXRewrittenBinaryOperator::DecomposedForm DF = RBO->getDecomposedForm(); 7919 Res = CheckAndSetCond(DF.Opcode, DF.LHS, DF.RHS, RBO->getSourceRange(), 7920 RBO->getOperatorLoc()); 7921 } else if (auto *BO = dyn_cast<BinaryOperator>(S)) { 7922 Res = CheckAndSetCond(BO->getOpcode(), BO->getLHS(), BO->getRHS(), 7923 BO->getSourceRange(), BO->getOperatorLoc()); 7924 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 7925 if (CE->getNumArgs() == 2) { 7926 Res = CheckAndSetCond( 7927 BinaryOperator::getOverloadedOpcode(CE->getOperator()), CE->getArg(0), 7928 CE->getArg(1), CE->getSourceRange(), CE->getOperatorLoc()); 7929 } 7930 } 7931 if (Res.hasValue()) 7932 return *Res; 7933 if (dependent() || SemaRef.CurContext->isDependentContext()) 7934 return false; 7935 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond) 7936 << (IneqCondIsCanonical ? 1 : 0) << S->getSourceRange() << LCDecl; 7937 return true; 7938 } 7939 7940 bool OpenMPIterationSpaceChecker::checkAndSetIncRHS(Expr *RHS) { 7941 // RHS of canonical loop form increment can be: 7942 // var + incr 7943 // incr + var 7944 // var - incr 7945 // 7946 RHS = RHS->IgnoreParenImpCasts(); 7947 if (auto *BO = dyn_cast<BinaryOperator>(RHS)) { 7948 if (BO->isAdditiveOp()) { 7949 bool IsAdd = BO->getOpcode() == BO_Add; 7950 if (getInitLCDecl(BO->getLHS()) == LCDecl) 7951 return setStep(BO->getRHS(), !IsAdd); 7952 if (IsAdd && getInitLCDecl(BO->getRHS()) == LCDecl) 7953 return setStep(BO->getLHS(), /*Subtract=*/false); 7954 } 7955 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) { 7956 bool IsAdd = CE->getOperator() == OO_Plus; 7957 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) { 7958 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 7959 return setStep(CE->getArg(1), !IsAdd); 7960 if (IsAdd && getInitLCDecl(CE->getArg(1)) == LCDecl) 7961 return setStep(CE->getArg(0), /*Subtract=*/false); 7962 } 7963 } 7964 if (dependent() || SemaRef.CurContext->isDependentContext()) 7965 return false; 7966 SemaRef.Diag(RHS->getBeginLoc(), diag::err_omp_loop_not_canonical_incr) 7967 << RHS->getSourceRange() << LCDecl; 7968 return true; 7969 } 7970 7971 bool OpenMPIterationSpaceChecker::checkAndSetInc(Expr *S) { 7972 // Check incr-expr for canonical loop form and return true if it 7973 // does not conform. 7974 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following: 7975 // ++var 7976 // var++ 7977 // --var 7978 // var-- 7979 // var += incr 7980 // var -= incr 7981 // var = var + incr 7982 // var = incr + var 7983 // var = var - incr 7984 // 7985 if (!S) { 7986 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl; 7987 return true; 7988 } 7989 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S)) 7990 if (!ExprTemp->cleanupsHaveSideEffects()) 7991 S = ExprTemp->getSubExpr(); 7992 7993 IncrementSrcRange = S->getSourceRange(); 7994 S = S->IgnoreParens(); 7995 if (auto *UO = dyn_cast<UnaryOperator>(S)) { 7996 if (UO->isIncrementDecrementOp() && 7997 getInitLCDecl(UO->getSubExpr()) == LCDecl) 7998 return setStep(SemaRef 7999 .ActOnIntegerConstant(UO->getBeginLoc(), 8000 (UO->isDecrementOp() ? -1 : 1)) 8001 .get(), 8002 /*Subtract=*/false); 8003 } else if (auto *BO = dyn_cast<BinaryOperator>(S)) { 8004 switch (BO->getOpcode()) { 8005 case BO_AddAssign: 8006 case BO_SubAssign: 8007 if (getInitLCDecl(BO->getLHS()) == LCDecl) 8008 return setStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign); 8009 break; 8010 case BO_Assign: 8011 if (getInitLCDecl(BO->getLHS()) == LCDecl) 8012 return checkAndSetIncRHS(BO->getRHS()); 8013 break; 8014 default: 8015 break; 8016 } 8017 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 8018 switch (CE->getOperator()) { 8019 case OO_PlusPlus: 8020 case OO_MinusMinus: 8021 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 8022 return setStep(SemaRef 8023 .ActOnIntegerConstant( 8024 CE->getBeginLoc(), 8025 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)) 8026 .get(), 8027 /*Subtract=*/false); 8028 break; 8029 case OO_PlusEqual: 8030 case OO_MinusEqual: 8031 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 8032 return setStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual); 8033 break; 8034 case OO_Equal: 8035 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 8036 return checkAndSetIncRHS(CE->getArg(1)); 8037 break; 8038 default: 8039 break; 8040 } 8041 } 8042 if (dependent() || SemaRef.CurContext->isDependentContext()) 8043 return false; 8044 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_incr) 8045 << S->getSourceRange() << LCDecl; 8046 return true; 8047 } 8048 8049 static ExprResult 8050 tryBuildCapture(Sema &SemaRef, Expr *Capture, 8051 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 8052 if (SemaRef.CurContext->isDependentContext() || Capture->containsErrors()) 8053 return Capture; 8054 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects)) 8055 return SemaRef.PerformImplicitConversion( 8056 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting, 8057 /*AllowExplicit=*/true); 8058 auto I = Captures.find(Capture); 8059 if (I != Captures.end()) 8060 return buildCapture(SemaRef, Capture, I->second); 8061 DeclRefExpr *Ref = nullptr; 8062 ExprResult Res = buildCapture(SemaRef, Capture, Ref); 8063 Captures[Capture] = Ref; 8064 return Res; 8065 } 8066 8067 /// Calculate number of iterations, transforming to unsigned, if number of 8068 /// iterations may be larger than the original type. 8069 static Expr * 8070 calculateNumIters(Sema &SemaRef, Scope *S, SourceLocation DefaultLoc, 8071 Expr *Lower, Expr *Upper, Expr *Step, QualType LCTy, 8072 bool TestIsStrictOp, bool RoundToStep, 8073 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 8074 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures); 8075 if (!NewStep.isUsable()) 8076 return nullptr; 8077 llvm::APSInt LRes, SRes; 8078 bool IsLowerConst = false, IsStepConst = false; 8079 if (Optional<llvm::APSInt> Res = 8080 Lower->getIntegerConstantExpr(SemaRef.Context)) { 8081 LRes = *Res; 8082 IsLowerConst = true; 8083 } 8084 if (Optional<llvm::APSInt> Res = 8085 Step->getIntegerConstantExpr(SemaRef.Context)) { 8086 SRes = *Res; 8087 IsStepConst = true; 8088 } 8089 bool NoNeedToConvert = IsLowerConst && !RoundToStep && 8090 ((!TestIsStrictOp && LRes.isNonNegative()) || 8091 (TestIsStrictOp && LRes.isStrictlyPositive())); 8092 bool NeedToReorganize = false; 8093 // Check if any subexpressions in Lower -Step [+ 1] lead to overflow. 8094 if (!NoNeedToConvert && IsLowerConst && 8095 (TestIsStrictOp || (RoundToStep && IsStepConst))) { 8096 NoNeedToConvert = true; 8097 if (RoundToStep) { 8098 unsigned BW = LRes.getBitWidth() > SRes.getBitWidth() 8099 ? LRes.getBitWidth() 8100 : SRes.getBitWidth(); 8101 LRes = LRes.extend(BW + 1); 8102 LRes.setIsSigned(true); 8103 SRes = SRes.extend(BW + 1); 8104 SRes.setIsSigned(true); 8105 LRes -= SRes; 8106 NoNeedToConvert = LRes.trunc(BW).extend(BW + 1) == LRes; 8107 LRes = LRes.trunc(BW); 8108 } 8109 if (TestIsStrictOp) { 8110 unsigned BW = LRes.getBitWidth(); 8111 LRes = LRes.extend(BW + 1); 8112 LRes.setIsSigned(true); 8113 ++LRes; 8114 NoNeedToConvert = 8115 NoNeedToConvert && LRes.trunc(BW).extend(BW + 1) == LRes; 8116 // truncate to the original bitwidth. 8117 LRes = LRes.trunc(BW); 8118 } 8119 NeedToReorganize = NoNeedToConvert; 8120 } 8121 llvm::APSInt URes; 8122 bool IsUpperConst = false; 8123 if (Optional<llvm::APSInt> Res = 8124 Upper->getIntegerConstantExpr(SemaRef.Context)) { 8125 URes = *Res; 8126 IsUpperConst = true; 8127 } 8128 if (NoNeedToConvert && IsLowerConst && IsUpperConst && 8129 (!RoundToStep || IsStepConst)) { 8130 unsigned BW = LRes.getBitWidth() > URes.getBitWidth() ? LRes.getBitWidth() 8131 : URes.getBitWidth(); 8132 LRes = LRes.extend(BW + 1); 8133 LRes.setIsSigned(true); 8134 URes = URes.extend(BW + 1); 8135 URes.setIsSigned(true); 8136 URes -= LRes; 8137 NoNeedToConvert = URes.trunc(BW).extend(BW + 1) == URes; 8138 NeedToReorganize = NoNeedToConvert; 8139 } 8140 // If the boundaries are not constant or (Lower - Step [+ 1]) is not constant 8141 // or less than zero (Upper - (Lower - Step [+ 1]) may overflow) - promote to 8142 // unsigned. 8143 if ((!NoNeedToConvert || (LRes.isNegative() && !IsUpperConst)) && 8144 !LCTy->isDependentType() && LCTy->isIntegerType()) { 8145 QualType LowerTy = Lower->getType(); 8146 QualType UpperTy = Upper->getType(); 8147 uint64_t LowerSize = SemaRef.Context.getTypeSize(LowerTy); 8148 uint64_t UpperSize = SemaRef.Context.getTypeSize(UpperTy); 8149 if ((LowerSize <= UpperSize && UpperTy->hasSignedIntegerRepresentation()) || 8150 (LowerSize > UpperSize && LowerTy->hasSignedIntegerRepresentation())) { 8151 QualType CastType = SemaRef.Context.getIntTypeForBitwidth( 8152 LowerSize > UpperSize ? LowerSize : UpperSize, /*Signed=*/0); 8153 Upper = 8154 SemaRef 8155 .PerformImplicitConversion( 8156 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Upper).get(), 8157 CastType, Sema::AA_Converting) 8158 .get(); 8159 Lower = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Lower).get(); 8160 NewStep = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, NewStep.get()); 8161 } 8162 } 8163 if (!Lower || !Upper || NewStep.isInvalid()) 8164 return nullptr; 8165 8166 ExprResult Diff; 8167 // If need to reorganize, then calculate the form as Upper - (Lower - Step [+ 8168 // 1]). 8169 if (NeedToReorganize) { 8170 Diff = Lower; 8171 8172 if (RoundToStep) { 8173 // Lower - Step 8174 Diff = 8175 SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Diff.get(), NewStep.get()); 8176 if (!Diff.isUsable()) 8177 return nullptr; 8178 } 8179 8180 // Lower - Step [+ 1] 8181 if (TestIsStrictOp) 8182 Diff = SemaRef.BuildBinOp( 8183 S, DefaultLoc, BO_Add, Diff.get(), 8184 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 8185 if (!Diff.isUsable()) 8186 return nullptr; 8187 8188 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 8189 if (!Diff.isUsable()) 8190 return nullptr; 8191 8192 // Upper - (Lower - Step [+ 1]). 8193 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Diff.get()); 8194 if (!Diff.isUsable()) 8195 return nullptr; 8196 } else { 8197 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower); 8198 8199 if (!Diff.isUsable() && LCTy->getAsCXXRecordDecl()) { 8200 // BuildBinOp already emitted error, this one is to point user to upper 8201 // and lower bound, and to tell what is passed to 'operator-'. 8202 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx) 8203 << Upper->getSourceRange() << Lower->getSourceRange(); 8204 return nullptr; 8205 } 8206 8207 if (!Diff.isUsable()) 8208 return nullptr; 8209 8210 // Upper - Lower [- 1] 8211 if (TestIsStrictOp) 8212 Diff = SemaRef.BuildBinOp( 8213 S, DefaultLoc, BO_Sub, Diff.get(), 8214 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 8215 if (!Diff.isUsable()) 8216 return nullptr; 8217 8218 if (RoundToStep) { 8219 // Upper - Lower [- 1] + Step 8220 Diff = 8221 SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get()); 8222 if (!Diff.isUsable()) 8223 return nullptr; 8224 } 8225 } 8226 8227 // Parentheses (for dumping/debugging purposes only). 8228 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 8229 if (!Diff.isUsable()) 8230 return nullptr; 8231 8232 // (Upper - Lower [- 1] + Step) / Step or (Upper - Lower) / Step 8233 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get()); 8234 if (!Diff.isUsable()) 8235 return nullptr; 8236 8237 return Diff.get(); 8238 } 8239 8240 /// Build the expression to calculate the number of iterations. 8241 Expr *OpenMPIterationSpaceChecker::buildNumIterations( 8242 Scope *S, ArrayRef<LoopIterationSpace> ResultIterSpaces, bool LimitedType, 8243 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 8244 QualType VarType = LCDecl->getType().getNonReferenceType(); 8245 if (!VarType->isIntegerType() && !VarType->isPointerType() && 8246 !SemaRef.getLangOpts().CPlusPlus) 8247 return nullptr; 8248 Expr *LBVal = LB; 8249 Expr *UBVal = UB; 8250 // LB = TestIsLessOp.getValue() ? min(LB(MinVal), LB(MaxVal)) : 8251 // max(LB(MinVal), LB(MaxVal)) 8252 if (InitDependOnLC) { 8253 const LoopIterationSpace &IS = ResultIterSpaces[*InitDependOnLC - 1]; 8254 if (!IS.MinValue || !IS.MaxValue) 8255 return nullptr; 8256 // OuterVar = Min 8257 ExprResult MinValue = 8258 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MinValue); 8259 if (!MinValue.isUsable()) 8260 return nullptr; 8261 8262 ExprResult LBMinVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign, 8263 IS.CounterVar, MinValue.get()); 8264 if (!LBMinVal.isUsable()) 8265 return nullptr; 8266 // OuterVar = Min, LBVal 8267 LBMinVal = 8268 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, LBMinVal.get(), LBVal); 8269 if (!LBMinVal.isUsable()) 8270 return nullptr; 8271 // (OuterVar = Min, LBVal) 8272 LBMinVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, LBMinVal.get()); 8273 if (!LBMinVal.isUsable()) 8274 return nullptr; 8275 8276 // OuterVar = Max 8277 ExprResult MaxValue = 8278 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MaxValue); 8279 if (!MaxValue.isUsable()) 8280 return nullptr; 8281 8282 ExprResult LBMaxVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign, 8283 IS.CounterVar, MaxValue.get()); 8284 if (!LBMaxVal.isUsable()) 8285 return nullptr; 8286 // OuterVar = Max, LBVal 8287 LBMaxVal = 8288 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, LBMaxVal.get(), LBVal); 8289 if (!LBMaxVal.isUsable()) 8290 return nullptr; 8291 // (OuterVar = Max, LBVal) 8292 LBMaxVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, LBMaxVal.get()); 8293 if (!LBMaxVal.isUsable()) 8294 return nullptr; 8295 8296 Expr *LBMin = tryBuildCapture(SemaRef, LBMinVal.get(), Captures).get(); 8297 Expr *LBMax = tryBuildCapture(SemaRef, LBMaxVal.get(), Captures).get(); 8298 if (!LBMin || !LBMax) 8299 return nullptr; 8300 // LB(MinVal) < LB(MaxVal) 8301 ExprResult MinLessMaxRes = 8302 SemaRef.BuildBinOp(S, DefaultLoc, BO_LT, LBMin, LBMax); 8303 if (!MinLessMaxRes.isUsable()) 8304 return nullptr; 8305 Expr *MinLessMax = 8306 tryBuildCapture(SemaRef, MinLessMaxRes.get(), Captures).get(); 8307 if (!MinLessMax) 8308 return nullptr; 8309 if (TestIsLessOp.getValue()) { 8310 // LB(MinVal) < LB(MaxVal) ? LB(MinVal) : LB(MaxVal) - min(LB(MinVal), 8311 // LB(MaxVal)) 8312 ExprResult MinLB = SemaRef.ActOnConditionalOp(DefaultLoc, DefaultLoc, 8313 MinLessMax, LBMin, LBMax); 8314 if (!MinLB.isUsable()) 8315 return nullptr; 8316 LBVal = MinLB.get(); 8317 } else { 8318 // LB(MinVal) < LB(MaxVal) ? LB(MaxVal) : LB(MinVal) - max(LB(MinVal), 8319 // LB(MaxVal)) 8320 ExprResult MaxLB = SemaRef.ActOnConditionalOp(DefaultLoc, DefaultLoc, 8321 MinLessMax, LBMax, LBMin); 8322 if (!MaxLB.isUsable()) 8323 return nullptr; 8324 LBVal = MaxLB.get(); 8325 } 8326 } 8327 // UB = TestIsLessOp.getValue() ? max(UB(MinVal), UB(MaxVal)) : 8328 // min(UB(MinVal), UB(MaxVal)) 8329 if (CondDependOnLC) { 8330 const LoopIterationSpace &IS = ResultIterSpaces[*CondDependOnLC - 1]; 8331 if (!IS.MinValue || !IS.MaxValue) 8332 return nullptr; 8333 // OuterVar = Min 8334 ExprResult MinValue = 8335 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MinValue); 8336 if (!MinValue.isUsable()) 8337 return nullptr; 8338 8339 ExprResult UBMinVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign, 8340 IS.CounterVar, MinValue.get()); 8341 if (!UBMinVal.isUsable()) 8342 return nullptr; 8343 // OuterVar = Min, UBVal 8344 UBMinVal = 8345 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, UBMinVal.get(), UBVal); 8346 if (!UBMinVal.isUsable()) 8347 return nullptr; 8348 // (OuterVar = Min, UBVal) 8349 UBMinVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, UBMinVal.get()); 8350 if (!UBMinVal.isUsable()) 8351 return nullptr; 8352 8353 // OuterVar = Max 8354 ExprResult MaxValue = 8355 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MaxValue); 8356 if (!MaxValue.isUsable()) 8357 return nullptr; 8358 8359 ExprResult UBMaxVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign, 8360 IS.CounterVar, MaxValue.get()); 8361 if (!UBMaxVal.isUsable()) 8362 return nullptr; 8363 // OuterVar = Max, UBVal 8364 UBMaxVal = 8365 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, UBMaxVal.get(), UBVal); 8366 if (!UBMaxVal.isUsable()) 8367 return nullptr; 8368 // (OuterVar = Max, UBVal) 8369 UBMaxVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, UBMaxVal.get()); 8370 if (!UBMaxVal.isUsable()) 8371 return nullptr; 8372 8373 Expr *UBMin = tryBuildCapture(SemaRef, UBMinVal.get(), Captures).get(); 8374 Expr *UBMax = tryBuildCapture(SemaRef, UBMaxVal.get(), Captures).get(); 8375 if (!UBMin || !UBMax) 8376 return nullptr; 8377 // UB(MinVal) > UB(MaxVal) 8378 ExprResult MinGreaterMaxRes = 8379 SemaRef.BuildBinOp(S, DefaultLoc, BO_GT, UBMin, UBMax); 8380 if (!MinGreaterMaxRes.isUsable()) 8381 return nullptr; 8382 Expr *MinGreaterMax = 8383 tryBuildCapture(SemaRef, MinGreaterMaxRes.get(), Captures).get(); 8384 if (!MinGreaterMax) 8385 return nullptr; 8386 if (TestIsLessOp.getValue()) { 8387 // UB(MinVal) > UB(MaxVal) ? UB(MinVal) : UB(MaxVal) - max(UB(MinVal), 8388 // UB(MaxVal)) 8389 ExprResult MaxUB = SemaRef.ActOnConditionalOp( 8390 DefaultLoc, DefaultLoc, MinGreaterMax, UBMin, UBMax); 8391 if (!MaxUB.isUsable()) 8392 return nullptr; 8393 UBVal = MaxUB.get(); 8394 } else { 8395 // UB(MinVal) > UB(MaxVal) ? UB(MaxVal) : UB(MinVal) - min(UB(MinVal), 8396 // UB(MaxVal)) 8397 ExprResult MinUB = SemaRef.ActOnConditionalOp( 8398 DefaultLoc, DefaultLoc, MinGreaterMax, UBMax, UBMin); 8399 if (!MinUB.isUsable()) 8400 return nullptr; 8401 UBVal = MinUB.get(); 8402 } 8403 } 8404 Expr *UBExpr = TestIsLessOp.getValue() ? UBVal : LBVal; 8405 Expr *LBExpr = TestIsLessOp.getValue() ? LBVal : UBVal; 8406 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get(); 8407 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get(); 8408 if (!Upper || !Lower) 8409 return nullptr; 8410 8411 ExprResult Diff = calculateNumIters(SemaRef, S, DefaultLoc, Lower, Upper, 8412 Step, VarType, TestIsStrictOp, 8413 /*RoundToStep=*/true, Captures); 8414 if (!Diff.isUsable()) 8415 return nullptr; 8416 8417 // OpenMP runtime requires 32-bit or 64-bit loop variables. 8418 QualType Type = Diff.get()->getType(); 8419 ASTContext &C = SemaRef.Context; 8420 bool UseVarType = VarType->hasIntegerRepresentation() && 8421 C.getTypeSize(Type) > C.getTypeSize(VarType); 8422 if (!Type->isIntegerType() || UseVarType) { 8423 unsigned NewSize = 8424 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type); 8425 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation() 8426 : Type->hasSignedIntegerRepresentation(); 8427 Type = C.getIntTypeForBitwidth(NewSize, IsSigned); 8428 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) { 8429 Diff = SemaRef.PerformImplicitConversion( 8430 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true); 8431 if (!Diff.isUsable()) 8432 return nullptr; 8433 } 8434 } 8435 if (LimitedType) { 8436 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32; 8437 if (NewSize != C.getTypeSize(Type)) { 8438 if (NewSize < C.getTypeSize(Type)) { 8439 assert(NewSize == 64 && "incorrect loop var size"); 8440 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var) 8441 << InitSrcRange << ConditionSrcRange; 8442 } 8443 QualType NewType = C.getIntTypeForBitwidth( 8444 NewSize, Type->hasSignedIntegerRepresentation() || 8445 C.getTypeSize(Type) < NewSize); 8446 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) { 8447 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType, 8448 Sema::AA_Converting, true); 8449 if (!Diff.isUsable()) 8450 return nullptr; 8451 } 8452 } 8453 } 8454 8455 return Diff.get(); 8456 } 8457 8458 std::pair<Expr *, Expr *> OpenMPIterationSpaceChecker::buildMinMaxValues( 8459 Scope *S, llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 8460 // Do not build for iterators, they cannot be used in non-rectangular loop 8461 // nests. 8462 if (LCDecl->getType()->isRecordType()) 8463 return std::make_pair(nullptr, nullptr); 8464 // If we subtract, the min is in the condition, otherwise the min is in the 8465 // init value. 8466 Expr *MinExpr = nullptr; 8467 Expr *MaxExpr = nullptr; 8468 Expr *LBExpr = TestIsLessOp.getValue() ? LB : UB; 8469 Expr *UBExpr = TestIsLessOp.getValue() ? UB : LB; 8470 bool LBNonRect = TestIsLessOp.getValue() ? InitDependOnLC.hasValue() 8471 : CondDependOnLC.hasValue(); 8472 bool UBNonRect = TestIsLessOp.getValue() ? CondDependOnLC.hasValue() 8473 : InitDependOnLC.hasValue(); 8474 Expr *Lower = 8475 LBNonRect ? LBExpr : tryBuildCapture(SemaRef, LBExpr, Captures).get(); 8476 Expr *Upper = 8477 UBNonRect ? UBExpr : tryBuildCapture(SemaRef, UBExpr, Captures).get(); 8478 if (!Upper || !Lower) 8479 return std::make_pair(nullptr, nullptr); 8480 8481 if (TestIsLessOp.getValue()) 8482 MinExpr = Lower; 8483 else 8484 MaxExpr = Upper; 8485 8486 // Build minimum/maximum value based on number of iterations. 8487 QualType VarType = LCDecl->getType().getNonReferenceType(); 8488 8489 ExprResult Diff = calculateNumIters(SemaRef, S, DefaultLoc, Lower, Upper, 8490 Step, VarType, TestIsStrictOp, 8491 /*RoundToStep=*/false, Captures); 8492 if (!Diff.isUsable()) 8493 return std::make_pair(nullptr, nullptr); 8494 8495 // ((Upper - Lower [- 1]) / Step) * Step 8496 // Parentheses (for dumping/debugging purposes only). 8497 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 8498 if (!Diff.isUsable()) 8499 return std::make_pair(nullptr, nullptr); 8500 8501 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures); 8502 if (!NewStep.isUsable()) 8503 return std::make_pair(nullptr, nullptr); 8504 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Mul, Diff.get(), NewStep.get()); 8505 if (!Diff.isUsable()) 8506 return std::make_pair(nullptr, nullptr); 8507 8508 // Parentheses (for dumping/debugging purposes only). 8509 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 8510 if (!Diff.isUsable()) 8511 return std::make_pair(nullptr, nullptr); 8512 8513 // Convert to the ptrdiff_t, if original type is pointer. 8514 if (VarType->isAnyPointerType() && 8515 !SemaRef.Context.hasSameType( 8516 Diff.get()->getType(), 8517 SemaRef.Context.getUnsignedPointerDiffType())) { 8518 Diff = SemaRef.PerformImplicitConversion( 8519 Diff.get(), SemaRef.Context.getUnsignedPointerDiffType(), 8520 Sema::AA_Converting, /*AllowExplicit=*/true); 8521 } 8522 if (!Diff.isUsable()) 8523 return std::make_pair(nullptr, nullptr); 8524 8525 if (TestIsLessOp.getValue()) { 8526 // MinExpr = Lower; 8527 // MaxExpr = Lower + (((Upper - Lower [- 1]) / Step) * Step) 8528 Diff = SemaRef.BuildBinOp( 8529 S, DefaultLoc, BO_Add, 8530 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Lower).get(), 8531 Diff.get()); 8532 if (!Diff.isUsable()) 8533 return std::make_pair(nullptr, nullptr); 8534 } else { 8535 // MaxExpr = Upper; 8536 // MinExpr = Upper - (((Upper - Lower [- 1]) / Step) * Step) 8537 Diff = SemaRef.BuildBinOp( 8538 S, DefaultLoc, BO_Sub, 8539 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Upper).get(), 8540 Diff.get()); 8541 if (!Diff.isUsable()) 8542 return std::make_pair(nullptr, nullptr); 8543 } 8544 8545 // Convert to the original type. 8546 if (SemaRef.Context.hasSameType(Diff.get()->getType(), VarType)) 8547 Diff = SemaRef.PerformImplicitConversion(Diff.get(), VarType, 8548 Sema::AA_Converting, 8549 /*AllowExplicit=*/true); 8550 if (!Diff.isUsable()) 8551 return std::make_pair(nullptr, nullptr); 8552 8553 Sema::TentativeAnalysisScope Trap(SemaRef); 8554 Diff = SemaRef.ActOnFinishFullExpr(Diff.get(), /*DiscardedValue=*/false); 8555 if (!Diff.isUsable()) 8556 return std::make_pair(nullptr, nullptr); 8557 8558 if (TestIsLessOp.getValue()) 8559 MaxExpr = Diff.get(); 8560 else 8561 MinExpr = Diff.get(); 8562 8563 return std::make_pair(MinExpr, MaxExpr); 8564 } 8565 8566 Expr *OpenMPIterationSpaceChecker::buildFinalCondition(Scope *S) const { 8567 if (InitDependOnLC || CondDependOnLC) 8568 return Condition; 8569 return nullptr; 8570 } 8571 8572 Expr *OpenMPIterationSpaceChecker::buildPreCond( 8573 Scope *S, Expr *Cond, 8574 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 8575 // Do not build a precondition when the condition/initialization is dependent 8576 // to prevent pessimistic early loop exit. 8577 // TODO: this can be improved by calculating min/max values but not sure that 8578 // it will be very effective. 8579 if (CondDependOnLC || InitDependOnLC) 8580 return SemaRef 8581 .PerformImplicitConversion( 8582 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(), 8583 SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting, 8584 /*AllowExplicit=*/true) 8585 .get(); 8586 8587 // Try to build LB <op> UB, where <op> is <, >, <=, or >=. 8588 Sema::TentativeAnalysisScope Trap(SemaRef); 8589 8590 ExprResult NewLB = tryBuildCapture(SemaRef, LB, Captures); 8591 ExprResult NewUB = tryBuildCapture(SemaRef, UB, Captures); 8592 if (!NewLB.isUsable() || !NewUB.isUsable()) 8593 return nullptr; 8594 8595 ExprResult CondExpr = SemaRef.BuildBinOp( 8596 S, DefaultLoc, 8597 TestIsLessOp.getValue() ? (TestIsStrictOp ? BO_LT : BO_LE) 8598 : (TestIsStrictOp ? BO_GT : BO_GE), 8599 NewLB.get(), NewUB.get()); 8600 if (CondExpr.isUsable()) { 8601 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(), 8602 SemaRef.Context.BoolTy)) 8603 CondExpr = SemaRef.PerformImplicitConversion( 8604 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting, 8605 /*AllowExplicit=*/true); 8606 } 8607 8608 // Otherwise use original loop condition and evaluate it in runtime. 8609 return CondExpr.isUsable() ? CondExpr.get() : Cond; 8610 } 8611 8612 /// Build reference expression to the counter be used for codegen. 8613 DeclRefExpr *OpenMPIterationSpaceChecker::buildCounterVar( 8614 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 8615 DSAStackTy &DSA) const { 8616 auto *VD = dyn_cast<VarDecl>(LCDecl); 8617 if (!VD) { 8618 VD = SemaRef.isOpenMPCapturedDecl(LCDecl); 8619 DeclRefExpr *Ref = buildDeclRefExpr( 8620 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc); 8621 const DSAStackTy::DSAVarData Data = 8622 DSA.getTopDSA(LCDecl, /*FromParent=*/false); 8623 // If the loop control decl is explicitly marked as private, do not mark it 8624 // as captured again. 8625 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr) 8626 Captures.insert(std::make_pair(LCRef, Ref)); 8627 return Ref; 8628 } 8629 return cast<DeclRefExpr>(LCRef); 8630 } 8631 8632 Expr *OpenMPIterationSpaceChecker::buildPrivateCounterVar() const { 8633 if (LCDecl && !LCDecl->isInvalidDecl()) { 8634 QualType Type = LCDecl->getType().getNonReferenceType(); 8635 VarDecl *PrivateVar = buildVarDecl( 8636 SemaRef, DefaultLoc, Type, LCDecl->getName(), 8637 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr, 8638 isa<VarDecl>(LCDecl) 8639 ? buildDeclRefExpr(SemaRef, cast<VarDecl>(LCDecl), Type, DefaultLoc) 8640 : nullptr); 8641 if (PrivateVar->isInvalidDecl()) 8642 return nullptr; 8643 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc); 8644 } 8645 return nullptr; 8646 } 8647 8648 /// Build initialization of the counter to be used for codegen. 8649 Expr *OpenMPIterationSpaceChecker::buildCounterInit() const { return LB; } 8650 8651 /// Build step of the counter be used for codegen. 8652 Expr *OpenMPIterationSpaceChecker::buildCounterStep() const { return Step; } 8653 8654 Expr *OpenMPIterationSpaceChecker::buildOrderedLoopData( 8655 Scope *S, Expr *Counter, 8656 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, SourceLocation Loc, 8657 Expr *Inc, OverloadedOperatorKind OOK) { 8658 Expr *Cnt = SemaRef.DefaultLvalueConversion(Counter).get(); 8659 if (!Cnt) 8660 return nullptr; 8661 if (Inc) { 8662 assert((OOK == OO_Plus || OOK == OO_Minus) && 8663 "Expected only + or - operations for depend clauses."); 8664 BinaryOperatorKind BOK = (OOK == OO_Plus) ? BO_Add : BO_Sub; 8665 Cnt = SemaRef.BuildBinOp(S, Loc, BOK, Cnt, Inc).get(); 8666 if (!Cnt) 8667 return nullptr; 8668 } 8669 QualType VarType = LCDecl->getType().getNonReferenceType(); 8670 if (!VarType->isIntegerType() && !VarType->isPointerType() && 8671 !SemaRef.getLangOpts().CPlusPlus) 8672 return nullptr; 8673 // Upper - Lower 8674 Expr *Upper = TestIsLessOp.getValue() 8675 ? Cnt 8676 : tryBuildCapture(SemaRef, LB, Captures).get(); 8677 Expr *Lower = TestIsLessOp.getValue() 8678 ? tryBuildCapture(SemaRef, LB, Captures).get() 8679 : Cnt; 8680 if (!Upper || !Lower) 8681 return nullptr; 8682 8683 ExprResult Diff = calculateNumIters( 8684 SemaRef, S, DefaultLoc, Lower, Upper, Step, VarType, 8685 /*TestIsStrictOp=*/false, /*RoundToStep=*/false, Captures); 8686 if (!Diff.isUsable()) 8687 return nullptr; 8688 8689 return Diff.get(); 8690 } 8691 } // namespace 8692 8693 void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) { 8694 assert(getLangOpts().OpenMP && "OpenMP is not active."); 8695 assert(Init && "Expected loop in canonical form."); 8696 unsigned AssociatedLoops = DSAStack->getAssociatedLoops(); 8697 if (AssociatedLoops > 0 && 8698 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { 8699 DSAStack->loopStart(); 8700 OpenMPIterationSpaceChecker ISC(*this, /*SupportsNonRectangular=*/true, 8701 *DSAStack, ForLoc); 8702 if (!ISC.checkAndSetInit(Init, /*EmitDiags=*/false)) { 8703 if (ValueDecl *D = ISC.getLoopDecl()) { 8704 auto *VD = dyn_cast<VarDecl>(D); 8705 DeclRefExpr *PrivateRef = nullptr; 8706 if (!VD) { 8707 if (VarDecl *Private = isOpenMPCapturedDecl(D)) { 8708 VD = Private; 8709 } else { 8710 PrivateRef = buildCapture(*this, D, ISC.getLoopDeclRefExpr(), 8711 /*WithInit=*/false); 8712 VD = cast<VarDecl>(PrivateRef->getDecl()); 8713 } 8714 } 8715 DSAStack->addLoopControlVariable(D, VD); 8716 const Decl *LD = DSAStack->getPossiblyLoopCunter(); 8717 if (LD != D->getCanonicalDecl()) { 8718 DSAStack->resetPossibleLoopCounter(); 8719 if (auto *Var = dyn_cast_or_null<VarDecl>(LD)) 8720 MarkDeclarationsReferencedInExpr( 8721 buildDeclRefExpr(*this, const_cast<VarDecl *>(Var), 8722 Var->getType().getNonLValueExprType(Context), 8723 ForLoc, /*RefersToCapture=*/true)); 8724 } 8725 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 8726 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables 8727 // Referenced in a Construct, C/C++]. The loop iteration variable in the 8728 // associated for-loop of a simd construct with just one associated 8729 // for-loop may be listed in a linear clause with a constant-linear-step 8730 // that is the increment of the associated for-loop. The loop iteration 8731 // variable(s) in the associated for-loop(s) of a for or parallel for 8732 // construct may be listed in a private or lastprivate clause. 8733 DSAStackTy::DSAVarData DVar = 8734 DSAStack->getTopDSA(D, /*FromParent=*/false); 8735 // If LoopVarRefExpr is nullptr it means the corresponding loop variable 8736 // is declared in the loop and it is predetermined as a private. 8737 Expr *LoopDeclRefExpr = ISC.getLoopDeclRefExpr(); 8738 OpenMPClauseKind PredeterminedCKind = 8739 isOpenMPSimdDirective(DKind) 8740 ? (DSAStack->hasMutipleLoops() ? OMPC_lastprivate : OMPC_linear) 8741 : OMPC_private; 8742 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && 8743 DVar.CKind != PredeterminedCKind && DVar.RefExpr && 8744 (LangOpts.OpenMP <= 45 || (DVar.CKind != OMPC_lastprivate && 8745 DVar.CKind != OMPC_private))) || 8746 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop || 8747 DKind == OMPD_master_taskloop || 8748 DKind == OMPD_parallel_master_taskloop || 8749 isOpenMPDistributeDirective(DKind)) && 8750 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && 8751 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) && 8752 (DVar.CKind != OMPC_private || DVar.RefExpr)) { 8753 Diag(Init->getBeginLoc(), diag::err_omp_loop_var_dsa) 8754 << getOpenMPClauseName(DVar.CKind) 8755 << getOpenMPDirectiveName(DKind) 8756 << getOpenMPClauseName(PredeterminedCKind); 8757 if (DVar.RefExpr == nullptr) 8758 DVar.CKind = PredeterminedCKind; 8759 reportOriginalDsa(*this, DSAStack, D, DVar, 8760 /*IsLoopIterVar=*/true); 8761 } else if (LoopDeclRefExpr) { 8762 // Make the loop iteration variable private (for worksharing 8763 // constructs), linear (for simd directives with the only one 8764 // associated loop) or lastprivate (for simd directives with several 8765 // collapsed or ordered loops). 8766 if (DVar.CKind == OMPC_unknown) 8767 DSAStack->addDSA(D, LoopDeclRefExpr, PredeterminedCKind, 8768 PrivateRef); 8769 } 8770 } 8771 } 8772 DSAStack->setAssociatedLoops(AssociatedLoops - 1); 8773 } 8774 } 8775 8776 /// Called on a for stmt to check and extract its iteration space 8777 /// for further processing (such as collapsing). 8778 static bool checkOpenMPIterationSpace( 8779 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA, 8780 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount, 8781 unsigned TotalNestedLoopCount, Expr *CollapseLoopCountExpr, 8782 Expr *OrderedLoopCountExpr, 8783 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA, 8784 llvm::MutableArrayRef<LoopIterationSpace> ResultIterSpaces, 8785 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 8786 bool SupportsNonRectangular = !isOpenMPLoopTransformationDirective(DKind); 8787 // OpenMP [2.9.1, Canonical Loop Form] 8788 // for (init-expr; test-expr; incr-expr) structured-block 8789 // for (range-decl: range-expr) structured-block 8790 if (auto *CanonLoop = dyn_cast_or_null<OMPCanonicalLoop>(S)) 8791 S = CanonLoop->getLoopStmt(); 8792 auto *For = dyn_cast_or_null<ForStmt>(S); 8793 auto *CXXFor = dyn_cast_or_null<CXXForRangeStmt>(S); 8794 // Ranged for is supported only in OpenMP 5.0. 8795 if (!For && (SemaRef.LangOpts.OpenMP <= 45 || !CXXFor)) { 8796 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_not_for) 8797 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr) 8798 << getOpenMPDirectiveName(DKind) << TotalNestedLoopCount 8799 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount; 8800 if (TotalNestedLoopCount > 1) { 8801 if (CollapseLoopCountExpr && OrderedLoopCountExpr) 8802 SemaRef.Diag(DSA.getConstructLoc(), 8803 diag::note_omp_collapse_ordered_expr) 8804 << 2 << CollapseLoopCountExpr->getSourceRange() 8805 << OrderedLoopCountExpr->getSourceRange(); 8806 else if (CollapseLoopCountExpr) 8807 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), 8808 diag::note_omp_collapse_ordered_expr) 8809 << 0 << CollapseLoopCountExpr->getSourceRange(); 8810 else 8811 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), 8812 diag::note_omp_collapse_ordered_expr) 8813 << 1 << OrderedLoopCountExpr->getSourceRange(); 8814 } 8815 return true; 8816 } 8817 assert(((For && For->getBody()) || (CXXFor && CXXFor->getBody())) && 8818 "No loop body."); 8819 // Postpone analysis in dependent contexts for ranged for loops. 8820 if (CXXFor && SemaRef.CurContext->isDependentContext()) 8821 return false; 8822 8823 OpenMPIterationSpaceChecker ISC(SemaRef, SupportsNonRectangular, DSA, 8824 For ? For->getForLoc() : CXXFor->getForLoc()); 8825 8826 // Check init. 8827 Stmt *Init = For ? For->getInit() : CXXFor->getBeginStmt(); 8828 if (ISC.checkAndSetInit(Init)) 8829 return true; 8830 8831 bool HasErrors = false; 8832 8833 // Check loop variable's type. 8834 if (ValueDecl *LCDecl = ISC.getLoopDecl()) { 8835 // OpenMP [2.6, Canonical Loop Form] 8836 // Var is one of the following: 8837 // A variable of signed or unsigned integer type. 8838 // For C++, a variable of a random access iterator type. 8839 // For C, a variable of a pointer type. 8840 QualType VarType = LCDecl->getType().getNonReferenceType(); 8841 if (!VarType->isDependentType() && !VarType->isIntegerType() && 8842 !VarType->isPointerType() && 8843 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) { 8844 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_variable_type) 8845 << SemaRef.getLangOpts().CPlusPlus; 8846 HasErrors = true; 8847 } 8848 8849 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in 8850 // a Construct 8851 // The loop iteration variable(s) in the associated for-loop(s) of a for or 8852 // parallel for construct is (are) private. 8853 // The loop iteration variable in the associated for-loop of a simd 8854 // construct with just one associated for-loop is linear with a 8855 // constant-linear-step that is the increment of the associated for-loop. 8856 // Exclude loop var from the list of variables with implicitly defined data 8857 // sharing attributes. 8858 VarsWithImplicitDSA.erase(LCDecl); 8859 8860 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars"); 8861 8862 // Check test-expr. 8863 HasErrors |= ISC.checkAndSetCond(For ? For->getCond() : CXXFor->getCond()); 8864 8865 // Check incr-expr. 8866 HasErrors |= ISC.checkAndSetInc(For ? For->getInc() : CXXFor->getInc()); 8867 } 8868 8869 if (ISC.dependent() || SemaRef.CurContext->isDependentContext() || HasErrors) 8870 return HasErrors; 8871 8872 // Build the loop's iteration space representation. 8873 ResultIterSpaces[CurrentNestedLoopCount].PreCond = ISC.buildPreCond( 8874 DSA.getCurScope(), For ? For->getCond() : CXXFor->getCond(), Captures); 8875 ResultIterSpaces[CurrentNestedLoopCount].NumIterations = 8876 ISC.buildNumIterations(DSA.getCurScope(), ResultIterSpaces, 8877 (isOpenMPWorksharingDirective(DKind) || 8878 isOpenMPGenericLoopDirective(DKind) || 8879 isOpenMPTaskLoopDirective(DKind) || 8880 isOpenMPDistributeDirective(DKind) || 8881 isOpenMPLoopTransformationDirective(DKind)), 8882 Captures); 8883 ResultIterSpaces[CurrentNestedLoopCount].CounterVar = 8884 ISC.buildCounterVar(Captures, DSA); 8885 ResultIterSpaces[CurrentNestedLoopCount].PrivateCounterVar = 8886 ISC.buildPrivateCounterVar(); 8887 ResultIterSpaces[CurrentNestedLoopCount].CounterInit = ISC.buildCounterInit(); 8888 ResultIterSpaces[CurrentNestedLoopCount].CounterStep = ISC.buildCounterStep(); 8889 ResultIterSpaces[CurrentNestedLoopCount].InitSrcRange = ISC.getInitSrcRange(); 8890 ResultIterSpaces[CurrentNestedLoopCount].CondSrcRange = 8891 ISC.getConditionSrcRange(); 8892 ResultIterSpaces[CurrentNestedLoopCount].IncSrcRange = 8893 ISC.getIncrementSrcRange(); 8894 ResultIterSpaces[CurrentNestedLoopCount].Subtract = ISC.shouldSubtractStep(); 8895 ResultIterSpaces[CurrentNestedLoopCount].IsStrictCompare = 8896 ISC.isStrictTestOp(); 8897 std::tie(ResultIterSpaces[CurrentNestedLoopCount].MinValue, 8898 ResultIterSpaces[CurrentNestedLoopCount].MaxValue) = 8899 ISC.buildMinMaxValues(DSA.getCurScope(), Captures); 8900 ResultIterSpaces[CurrentNestedLoopCount].FinalCondition = 8901 ISC.buildFinalCondition(DSA.getCurScope()); 8902 ResultIterSpaces[CurrentNestedLoopCount].IsNonRectangularLB = 8903 ISC.doesInitDependOnLC(); 8904 ResultIterSpaces[CurrentNestedLoopCount].IsNonRectangularUB = 8905 ISC.doesCondDependOnLC(); 8906 ResultIterSpaces[CurrentNestedLoopCount].LoopDependentIdx = 8907 ISC.getLoopDependentIdx(); 8908 8909 HasErrors |= 8910 (ResultIterSpaces[CurrentNestedLoopCount].PreCond == nullptr || 8911 ResultIterSpaces[CurrentNestedLoopCount].NumIterations == nullptr || 8912 ResultIterSpaces[CurrentNestedLoopCount].CounterVar == nullptr || 8913 ResultIterSpaces[CurrentNestedLoopCount].PrivateCounterVar == nullptr || 8914 ResultIterSpaces[CurrentNestedLoopCount].CounterInit == nullptr || 8915 ResultIterSpaces[CurrentNestedLoopCount].CounterStep == nullptr); 8916 if (!HasErrors && DSA.isOrderedRegion()) { 8917 if (DSA.getOrderedRegionParam().second->getNumForLoops()) { 8918 if (CurrentNestedLoopCount < 8919 DSA.getOrderedRegionParam().second->getLoopNumIterations().size()) { 8920 DSA.getOrderedRegionParam().second->setLoopNumIterations( 8921 CurrentNestedLoopCount, 8922 ResultIterSpaces[CurrentNestedLoopCount].NumIterations); 8923 DSA.getOrderedRegionParam().second->setLoopCounter( 8924 CurrentNestedLoopCount, 8925 ResultIterSpaces[CurrentNestedLoopCount].CounterVar); 8926 } 8927 } 8928 for (auto &Pair : DSA.getDoacrossDependClauses()) { 8929 if (CurrentNestedLoopCount >= Pair.first->getNumLoops()) { 8930 // Erroneous case - clause has some problems. 8931 continue; 8932 } 8933 if (Pair.first->getDependencyKind() == OMPC_DEPEND_sink && 8934 Pair.second.size() <= CurrentNestedLoopCount) { 8935 // Erroneous case - clause has some problems. 8936 Pair.first->setLoopData(CurrentNestedLoopCount, nullptr); 8937 continue; 8938 } 8939 Expr *CntValue; 8940 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source) 8941 CntValue = ISC.buildOrderedLoopData( 8942 DSA.getCurScope(), 8943 ResultIterSpaces[CurrentNestedLoopCount].CounterVar, Captures, 8944 Pair.first->getDependencyLoc()); 8945 else 8946 CntValue = ISC.buildOrderedLoopData( 8947 DSA.getCurScope(), 8948 ResultIterSpaces[CurrentNestedLoopCount].CounterVar, Captures, 8949 Pair.first->getDependencyLoc(), 8950 Pair.second[CurrentNestedLoopCount].first, 8951 Pair.second[CurrentNestedLoopCount].second); 8952 Pair.first->setLoopData(CurrentNestedLoopCount, CntValue); 8953 } 8954 } 8955 8956 return HasErrors; 8957 } 8958 8959 /// Build 'VarRef = Start. 8960 static ExprResult 8961 buildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef, 8962 ExprResult Start, bool IsNonRectangularLB, 8963 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 8964 // Build 'VarRef = Start. 8965 ExprResult NewStart = IsNonRectangularLB 8966 ? Start.get() 8967 : tryBuildCapture(SemaRef, Start.get(), Captures); 8968 if (!NewStart.isUsable()) 8969 return ExprError(); 8970 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(), 8971 VarRef.get()->getType())) { 8972 NewStart = SemaRef.PerformImplicitConversion( 8973 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting, 8974 /*AllowExplicit=*/true); 8975 if (!NewStart.isUsable()) 8976 return ExprError(); 8977 } 8978 8979 ExprResult Init = 8980 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); 8981 return Init; 8982 } 8983 8984 /// Build 'VarRef = Start + Iter * Step'. 8985 static ExprResult buildCounterUpdate( 8986 Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef, 8987 ExprResult Start, ExprResult Iter, ExprResult Step, bool Subtract, 8988 bool IsNonRectangularLB, 8989 llvm::MapVector<const Expr *, DeclRefExpr *> *Captures = nullptr) { 8990 // Add parentheses (for debugging purposes only). 8991 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get()); 8992 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() || 8993 !Step.isUsable()) 8994 return ExprError(); 8995 8996 ExprResult NewStep = Step; 8997 if (Captures) 8998 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures); 8999 if (NewStep.isInvalid()) 9000 return ExprError(); 9001 ExprResult Update = 9002 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get()); 9003 if (!Update.isUsable()) 9004 return ExprError(); 9005 9006 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or 9007 // 'VarRef = Start (+|-) Iter * Step'. 9008 if (!Start.isUsable()) 9009 return ExprError(); 9010 ExprResult NewStart = SemaRef.ActOnParenExpr(Loc, Loc, Start.get()); 9011 if (!NewStart.isUsable()) 9012 return ExprError(); 9013 if (Captures && !IsNonRectangularLB) 9014 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures); 9015 if (NewStart.isInvalid()) 9016 return ExprError(); 9017 9018 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'. 9019 ExprResult SavedUpdate = Update; 9020 ExprResult UpdateVal; 9021 if (VarRef.get()->getType()->isOverloadableType() || 9022 NewStart.get()->getType()->isOverloadableType() || 9023 Update.get()->getType()->isOverloadableType()) { 9024 Sema::TentativeAnalysisScope Trap(SemaRef); 9025 9026 Update = 9027 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); 9028 if (Update.isUsable()) { 9029 UpdateVal = 9030 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign, 9031 VarRef.get(), SavedUpdate.get()); 9032 if (UpdateVal.isUsable()) { 9033 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(), 9034 UpdateVal.get()); 9035 } 9036 } 9037 } 9038 9039 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'. 9040 if (!Update.isUsable() || !UpdateVal.isUsable()) { 9041 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add, 9042 NewStart.get(), SavedUpdate.get()); 9043 if (!Update.isUsable()) 9044 return ExprError(); 9045 9046 if (!SemaRef.Context.hasSameType(Update.get()->getType(), 9047 VarRef.get()->getType())) { 9048 Update = SemaRef.PerformImplicitConversion( 9049 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true); 9050 if (!Update.isUsable()) 9051 return ExprError(); 9052 } 9053 9054 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get()); 9055 } 9056 return Update; 9057 } 9058 9059 /// Convert integer expression \a E to make it have at least \a Bits 9060 /// bits. 9061 static ExprResult widenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) { 9062 if (E == nullptr) 9063 return ExprError(); 9064 ASTContext &C = SemaRef.Context; 9065 QualType OldType = E->getType(); 9066 unsigned HasBits = C.getTypeSize(OldType); 9067 if (HasBits >= Bits) 9068 return ExprResult(E); 9069 // OK to convert to signed, because new type has more bits than old. 9070 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true); 9071 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting, 9072 true); 9073 } 9074 9075 /// Check if the given expression \a E is a constant integer that fits 9076 /// into \a Bits bits. 9077 static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) { 9078 if (E == nullptr) 9079 return false; 9080 if (Optional<llvm::APSInt> Result = 9081 E->getIntegerConstantExpr(SemaRef.Context)) 9082 return Signed ? Result->isSignedIntN(Bits) : Result->isIntN(Bits); 9083 return false; 9084 } 9085 9086 /// Build preinits statement for the given declarations. 9087 static Stmt *buildPreInits(ASTContext &Context, 9088 MutableArrayRef<Decl *> PreInits) { 9089 if (!PreInits.empty()) { 9090 return new (Context) DeclStmt( 9091 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()), 9092 SourceLocation(), SourceLocation()); 9093 } 9094 return nullptr; 9095 } 9096 9097 /// Build preinits statement for the given declarations. 9098 static Stmt * 9099 buildPreInits(ASTContext &Context, 9100 const llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 9101 if (!Captures.empty()) { 9102 SmallVector<Decl *, 16> PreInits; 9103 for (const auto &Pair : Captures) 9104 PreInits.push_back(Pair.second->getDecl()); 9105 return buildPreInits(Context, PreInits); 9106 } 9107 return nullptr; 9108 } 9109 9110 /// Build postupdate expression for the given list of postupdates expressions. 9111 static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) { 9112 Expr *PostUpdate = nullptr; 9113 if (!PostUpdates.empty()) { 9114 for (Expr *E : PostUpdates) { 9115 Expr *ConvE = S.BuildCStyleCastExpr( 9116 E->getExprLoc(), 9117 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy), 9118 E->getExprLoc(), E) 9119 .get(); 9120 PostUpdate = PostUpdate 9121 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma, 9122 PostUpdate, ConvE) 9123 .get() 9124 : ConvE; 9125 } 9126 } 9127 return PostUpdate; 9128 } 9129 9130 /// Called on a for stmt to check itself and nested loops (if any). 9131 /// \return Returns 0 if one of the collapsed stmts is not canonical for loop, 9132 /// number of collapsed loops otherwise. 9133 static unsigned 9134 checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr, 9135 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef, 9136 DSAStackTy &DSA, 9137 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA, 9138 OMPLoopBasedDirective::HelperExprs &Built) { 9139 unsigned NestedLoopCount = 1; 9140 bool SupportsNonPerfectlyNested = (SemaRef.LangOpts.OpenMP >= 50) && 9141 !isOpenMPLoopTransformationDirective(DKind); 9142 9143 if (CollapseLoopCountExpr) { 9144 // Found 'collapse' clause - calculate collapse number. 9145 Expr::EvalResult Result; 9146 if (!CollapseLoopCountExpr->isValueDependent() && 9147 CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) { 9148 NestedLoopCount = Result.Val.getInt().getLimitedValue(); 9149 } else { 9150 Built.clear(/*Size=*/1); 9151 return 1; 9152 } 9153 } 9154 unsigned OrderedLoopCount = 1; 9155 if (OrderedLoopCountExpr) { 9156 // Found 'ordered' clause - calculate collapse number. 9157 Expr::EvalResult EVResult; 9158 if (!OrderedLoopCountExpr->isValueDependent() && 9159 OrderedLoopCountExpr->EvaluateAsInt(EVResult, 9160 SemaRef.getASTContext())) { 9161 llvm::APSInt Result = EVResult.Val.getInt(); 9162 if (Result.getLimitedValue() < NestedLoopCount) { 9163 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), 9164 diag::err_omp_wrong_ordered_loop_count) 9165 << OrderedLoopCountExpr->getSourceRange(); 9166 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), 9167 diag::note_collapse_loop_count) 9168 << CollapseLoopCountExpr->getSourceRange(); 9169 } 9170 OrderedLoopCount = Result.getLimitedValue(); 9171 } else { 9172 Built.clear(/*Size=*/1); 9173 return 1; 9174 } 9175 } 9176 // This is helper routine for loop directives (e.g., 'for', 'simd', 9177 // 'for simd', etc.). 9178 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 9179 unsigned NumLoops = std::max(OrderedLoopCount, NestedLoopCount); 9180 SmallVector<LoopIterationSpace, 4> IterSpaces(NumLoops); 9181 if (!OMPLoopBasedDirective::doForAllLoops( 9182 AStmt->IgnoreContainers(!isOpenMPLoopTransformationDirective(DKind)), 9183 SupportsNonPerfectlyNested, NumLoops, 9184 [DKind, &SemaRef, &DSA, NumLoops, NestedLoopCount, 9185 CollapseLoopCountExpr, OrderedLoopCountExpr, &VarsWithImplicitDSA, 9186 &IterSpaces, &Captures](unsigned Cnt, Stmt *CurStmt) { 9187 if (checkOpenMPIterationSpace( 9188 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount, 9189 NumLoops, CollapseLoopCountExpr, OrderedLoopCountExpr, 9190 VarsWithImplicitDSA, IterSpaces, Captures)) 9191 return true; 9192 if (Cnt > 0 && Cnt >= NestedLoopCount && 9193 IterSpaces[Cnt].CounterVar) { 9194 // Handle initialization of captured loop iterator variables. 9195 auto *DRE = cast<DeclRefExpr>(IterSpaces[Cnt].CounterVar); 9196 if (isa<OMPCapturedExprDecl>(DRE->getDecl())) { 9197 Captures[DRE] = DRE; 9198 } 9199 } 9200 return false; 9201 }, 9202 [&SemaRef, &Captures](OMPLoopTransformationDirective *Transform) { 9203 Stmt *DependentPreInits = Transform->getPreInits(); 9204 if (!DependentPreInits) 9205 return; 9206 for (Decl *C : cast<DeclStmt>(DependentPreInits)->getDeclGroup()) { 9207 auto *D = cast<VarDecl>(C); 9208 DeclRefExpr *Ref = buildDeclRefExpr(SemaRef, D, D->getType(), 9209 Transform->getBeginLoc()); 9210 Captures[Ref] = Ref; 9211 } 9212 })) 9213 return 0; 9214 9215 Built.clear(/* size */ NestedLoopCount); 9216 9217 if (SemaRef.CurContext->isDependentContext()) 9218 return NestedLoopCount; 9219 9220 // An example of what is generated for the following code: 9221 // 9222 // #pragma omp simd collapse(2) ordered(2) 9223 // for (i = 0; i < NI; ++i) 9224 // for (k = 0; k < NK; ++k) 9225 // for (j = J0; j < NJ; j+=2) { 9226 // <loop body> 9227 // } 9228 // 9229 // We generate the code below. 9230 // Note: the loop body may be outlined in CodeGen. 9231 // Note: some counters may be C++ classes, operator- is used to find number of 9232 // iterations and operator+= to calculate counter value. 9233 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32 9234 // or i64 is currently supported). 9235 // 9236 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2)) 9237 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) { 9238 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2); 9239 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2; 9240 // // similar updates for vars in clauses (e.g. 'linear') 9241 // <loop body (using local i and j)> 9242 // } 9243 // i = NI; // assign final values of counters 9244 // j = NJ; 9245 // 9246 9247 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are 9248 // the iteration counts of the collapsed for loops. 9249 // Precondition tests if there is at least one iteration (all conditions are 9250 // true). 9251 auto PreCond = ExprResult(IterSpaces[0].PreCond); 9252 Expr *N0 = IterSpaces[0].NumIterations; 9253 ExprResult LastIteration32 = 9254 widenIterationCount(/*Bits=*/32, 9255 SemaRef 9256 .PerformImplicitConversion( 9257 N0->IgnoreImpCasts(), N0->getType(), 9258 Sema::AA_Converting, /*AllowExplicit=*/true) 9259 .get(), 9260 SemaRef); 9261 ExprResult LastIteration64 = widenIterationCount( 9262 /*Bits=*/64, 9263 SemaRef 9264 .PerformImplicitConversion(N0->IgnoreImpCasts(), N0->getType(), 9265 Sema::AA_Converting, 9266 /*AllowExplicit=*/true) 9267 .get(), 9268 SemaRef); 9269 9270 if (!LastIteration32.isUsable() || !LastIteration64.isUsable()) 9271 return NestedLoopCount; 9272 9273 ASTContext &C = SemaRef.Context; 9274 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32; 9275 9276 Scope *CurScope = DSA.getCurScope(); 9277 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) { 9278 if (PreCond.isUsable()) { 9279 PreCond = 9280 SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd, 9281 PreCond.get(), IterSpaces[Cnt].PreCond); 9282 } 9283 Expr *N = IterSpaces[Cnt].NumIterations; 9284 SourceLocation Loc = N->getExprLoc(); 9285 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32; 9286 if (LastIteration32.isUsable()) 9287 LastIteration32 = SemaRef.BuildBinOp( 9288 CurScope, Loc, BO_Mul, LastIteration32.get(), 9289 SemaRef 9290 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), 9291 Sema::AA_Converting, 9292 /*AllowExplicit=*/true) 9293 .get()); 9294 if (LastIteration64.isUsable()) 9295 LastIteration64 = SemaRef.BuildBinOp( 9296 CurScope, Loc, BO_Mul, LastIteration64.get(), 9297 SemaRef 9298 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), 9299 Sema::AA_Converting, 9300 /*AllowExplicit=*/true) 9301 .get()); 9302 } 9303 9304 // Choose either the 32-bit or 64-bit version. 9305 ExprResult LastIteration = LastIteration64; 9306 if (SemaRef.getLangOpts().OpenMPOptimisticCollapse || 9307 (LastIteration32.isUsable() && 9308 C.getTypeSize(LastIteration32.get()->getType()) == 32 && 9309 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 || 9310 fitsInto( 9311 /*Bits=*/32, 9312 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(), 9313 LastIteration64.get(), SemaRef)))) 9314 LastIteration = LastIteration32; 9315 QualType VType = LastIteration.get()->getType(); 9316 QualType RealVType = VType; 9317 QualType StrideVType = VType; 9318 if (isOpenMPTaskLoopDirective(DKind)) { 9319 VType = 9320 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0); 9321 StrideVType = 9322 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1); 9323 } 9324 9325 if (!LastIteration.isUsable()) 9326 return 0; 9327 9328 // Save the number of iterations. 9329 ExprResult NumIterations = LastIteration; 9330 { 9331 LastIteration = SemaRef.BuildBinOp( 9332 CurScope, LastIteration.get()->getExprLoc(), BO_Sub, 9333 LastIteration.get(), 9334 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 9335 if (!LastIteration.isUsable()) 9336 return 0; 9337 } 9338 9339 // Calculate the last iteration number beforehand instead of doing this on 9340 // each iteration. Do not do this if the number of iterations may be kfold-ed. 9341 bool IsConstant = LastIteration.get()->isIntegerConstantExpr(SemaRef.Context); 9342 ExprResult CalcLastIteration; 9343 if (!IsConstant) { 9344 ExprResult SaveRef = 9345 tryBuildCapture(SemaRef, LastIteration.get(), Captures); 9346 LastIteration = SaveRef; 9347 9348 // Prepare SaveRef + 1. 9349 NumIterations = SemaRef.BuildBinOp( 9350 CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(), 9351 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 9352 if (!NumIterations.isUsable()) 9353 return 0; 9354 } 9355 9356 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin(); 9357 9358 // Build variables passed into runtime, necessary for worksharing directives. 9359 ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB; 9360 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || 9361 isOpenMPDistributeDirective(DKind) || 9362 isOpenMPGenericLoopDirective(DKind) || 9363 isOpenMPLoopTransformationDirective(DKind)) { 9364 // Lower bound variable, initialized with zero. 9365 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb"); 9366 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc); 9367 SemaRef.AddInitializerToDecl(LBDecl, 9368 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 9369 /*DirectInit*/ false); 9370 9371 // Upper bound variable, initialized with last iteration number. 9372 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub"); 9373 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc); 9374 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(), 9375 /*DirectInit*/ false); 9376 9377 // A 32-bit variable-flag where runtime returns 1 for the last iteration. 9378 // This will be used to implement clause 'lastprivate'. 9379 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true); 9380 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last"); 9381 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc); 9382 SemaRef.AddInitializerToDecl(ILDecl, 9383 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 9384 /*DirectInit*/ false); 9385 9386 // Stride variable returned by runtime (we initialize it to 1 by default). 9387 VarDecl *STDecl = 9388 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride"); 9389 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc); 9390 SemaRef.AddInitializerToDecl(STDecl, 9391 SemaRef.ActOnIntegerConstant(InitLoc, 1).get(), 9392 /*DirectInit*/ false); 9393 9394 // Build expression: UB = min(UB, LastIteration) 9395 // It is necessary for CodeGen of directives with static scheduling. 9396 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT, 9397 UB.get(), LastIteration.get()); 9398 ExprResult CondOp = SemaRef.ActOnConditionalOp( 9399 LastIteration.get()->getExprLoc(), InitLoc, IsUBGreater.get(), 9400 LastIteration.get(), UB.get()); 9401 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(), 9402 CondOp.get()); 9403 EUB = SemaRef.ActOnFinishFullExpr(EUB.get(), /*DiscardedValue*/ false); 9404 9405 // If we have a combined directive that combines 'distribute', 'for' or 9406 // 'simd' we need to be able to access the bounds of the schedule of the 9407 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained 9408 // by scheduling 'distribute' have to be passed to the schedule of 'for'. 9409 if (isOpenMPLoopBoundSharingDirective(DKind)) { 9410 // Lower bound variable, initialized with zero. 9411 VarDecl *CombLBDecl = 9412 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb"); 9413 CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc); 9414 SemaRef.AddInitializerToDecl( 9415 CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 9416 /*DirectInit*/ false); 9417 9418 // Upper bound variable, initialized with last iteration number. 9419 VarDecl *CombUBDecl = 9420 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub"); 9421 CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc); 9422 SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(), 9423 /*DirectInit*/ false); 9424 9425 ExprResult CombIsUBGreater = SemaRef.BuildBinOp( 9426 CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get()); 9427 ExprResult CombCondOp = 9428 SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(), 9429 LastIteration.get(), CombUB.get()); 9430 CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(), 9431 CombCondOp.get()); 9432 CombEUB = 9433 SemaRef.ActOnFinishFullExpr(CombEUB.get(), /*DiscardedValue*/ false); 9434 9435 const CapturedDecl *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl(); 9436 // We expect to have at least 2 more parameters than the 'parallel' 9437 // directive does - the lower and upper bounds of the previous schedule. 9438 assert(CD->getNumParams() >= 4 && 9439 "Unexpected number of parameters in loop combined directive"); 9440 9441 // Set the proper type for the bounds given what we learned from the 9442 // enclosed loops. 9443 ImplicitParamDecl *PrevLBDecl = CD->getParam(/*PrevLB=*/2); 9444 ImplicitParamDecl *PrevUBDecl = CD->getParam(/*PrevUB=*/3); 9445 9446 // Previous lower and upper bounds are obtained from the region 9447 // parameters. 9448 PrevLB = 9449 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc); 9450 PrevUB = 9451 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc); 9452 } 9453 } 9454 9455 // Build the iteration variable and its initialization before loop. 9456 ExprResult IV; 9457 ExprResult Init, CombInit; 9458 { 9459 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv"); 9460 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc); 9461 Expr *RHS = (isOpenMPWorksharingDirective(DKind) || 9462 isOpenMPGenericLoopDirective(DKind) || 9463 isOpenMPTaskLoopDirective(DKind) || 9464 isOpenMPDistributeDirective(DKind) || 9465 isOpenMPLoopTransformationDirective(DKind)) 9466 ? LB.get() 9467 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get(); 9468 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS); 9469 Init = SemaRef.ActOnFinishFullExpr(Init.get(), /*DiscardedValue*/ false); 9470 9471 if (isOpenMPLoopBoundSharingDirective(DKind)) { 9472 Expr *CombRHS = 9473 (isOpenMPWorksharingDirective(DKind) || 9474 isOpenMPGenericLoopDirective(DKind) || 9475 isOpenMPTaskLoopDirective(DKind) || 9476 isOpenMPDistributeDirective(DKind)) 9477 ? CombLB.get() 9478 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get(); 9479 CombInit = 9480 SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS); 9481 CombInit = 9482 SemaRef.ActOnFinishFullExpr(CombInit.get(), /*DiscardedValue*/ false); 9483 } 9484 } 9485 9486 bool UseStrictCompare = 9487 RealVType->hasUnsignedIntegerRepresentation() && 9488 llvm::all_of(IterSpaces, [](const LoopIterationSpace &LIS) { 9489 return LIS.IsStrictCompare; 9490 }); 9491 // Loop condition (IV < NumIterations) or (IV <= UB or IV < UB + 1 (for 9492 // unsigned IV)) for worksharing loops. 9493 SourceLocation CondLoc = AStmt->getBeginLoc(); 9494 Expr *BoundUB = UB.get(); 9495 if (UseStrictCompare) { 9496 BoundUB = 9497 SemaRef 9498 .BuildBinOp(CurScope, CondLoc, BO_Add, BoundUB, 9499 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()) 9500 .get(); 9501 BoundUB = 9502 SemaRef.ActOnFinishFullExpr(BoundUB, /*DiscardedValue*/ false).get(); 9503 } 9504 ExprResult Cond = 9505 (isOpenMPWorksharingDirective(DKind) || 9506 isOpenMPGenericLoopDirective(DKind) || 9507 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind) || 9508 isOpenMPLoopTransformationDirective(DKind)) 9509 ? SemaRef.BuildBinOp(CurScope, CondLoc, 9510 UseStrictCompare ? BO_LT : BO_LE, IV.get(), 9511 BoundUB) 9512 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(), 9513 NumIterations.get()); 9514 ExprResult CombDistCond; 9515 if (isOpenMPLoopBoundSharingDirective(DKind)) { 9516 CombDistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(), 9517 NumIterations.get()); 9518 } 9519 9520 ExprResult CombCond; 9521 if (isOpenMPLoopBoundSharingDirective(DKind)) { 9522 Expr *BoundCombUB = CombUB.get(); 9523 if (UseStrictCompare) { 9524 BoundCombUB = 9525 SemaRef 9526 .BuildBinOp( 9527 CurScope, CondLoc, BO_Add, BoundCombUB, 9528 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()) 9529 .get(); 9530 BoundCombUB = 9531 SemaRef.ActOnFinishFullExpr(BoundCombUB, /*DiscardedValue*/ false) 9532 .get(); 9533 } 9534 CombCond = 9535 SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, 9536 IV.get(), BoundCombUB); 9537 } 9538 // Loop increment (IV = IV + 1) 9539 SourceLocation IncLoc = AStmt->getBeginLoc(); 9540 ExprResult Inc = 9541 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(), 9542 SemaRef.ActOnIntegerConstant(IncLoc, 1).get()); 9543 if (!Inc.isUsable()) 9544 return 0; 9545 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get()); 9546 Inc = SemaRef.ActOnFinishFullExpr(Inc.get(), /*DiscardedValue*/ false); 9547 if (!Inc.isUsable()) 9548 return 0; 9549 9550 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST). 9551 // Used for directives with static scheduling. 9552 // In combined construct, add combined version that use CombLB and CombUB 9553 // base variables for the update 9554 ExprResult NextLB, NextUB, CombNextLB, CombNextUB; 9555 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || 9556 isOpenMPGenericLoopDirective(DKind) || 9557 isOpenMPDistributeDirective(DKind) || 9558 isOpenMPLoopTransformationDirective(DKind)) { 9559 // LB + ST 9560 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get()); 9561 if (!NextLB.isUsable()) 9562 return 0; 9563 // LB = LB + ST 9564 NextLB = 9565 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get()); 9566 NextLB = 9567 SemaRef.ActOnFinishFullExpr(NextLB.get(), /*DiscardedValue*/ false); 9568 if (!NextLB.isUsable()) 9569 return 0; 9570 // UB + ST 9571 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get()); 9572 if (!NextUB.isUsable()) 9573 return 0; 9574 // UB = UB + ST 9575 NextUB = 9576 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get()); 9577 NextUB = 9578 SemaRef.ActOnFinishFullExpr(NextUB.get(), /*DiscardedValue*/ false); 9579 if (!NextUB.isUsable()) 9580 return 0; 9581 if (isOpenMPLoopBoundSharingDirective(DKind)) { 9582 CombNextLB = 9583 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get()); 9584 if (!NextLB.isUsable()) 9585 return 0; 9586 // LB = LB + ST 9587 CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(), 9588 CombNextLB.get()); 9589 CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get(), 9590 /*DiscardedValue*/ false); 9591 if (!CombNextLB.isUsable()) 9592 return 0; 9593 // UB + ST 9594 CombNextUB = 9595 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get()); 9596 if (!CombNextUB.isUsable()) 9597 return 0; 9598 // UB = UB + ST 9599 CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(), 9600 CombNextUB.get()); 9601 CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get(), 9602 /*DiscardedValue*/ false); 9603 if (!CombNextUB.isUsable()) 9604 return 0; 9605 } 9606 } 9607 9608 // Create increment expression for distribute loop when combined in a same 9609 // directive with for as IV = IV + ST; ensure upper bound expression based 9610 // on PrevUB instead of NumIterations - used to implement 'for' when found 9611 // in combination with 'distribute', like in 'distribute parallel for' 9612 SourceLocation DistIncLoc = AStmt->getBeginLoc(); 9613 ExprResult DistCond, DistInc, PrevEUB, ParForInDistCond; 9614 if (isOpenMPLoopBoundSharingDirective(DKind)) { 9615 DistCond = SemaRef.BuildBinOp( 9616 CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, IV.get(), BoundUB); 9617 assert(DistCond.isUsable() && "distribute cond expr was not built"); 9618 9619 DistInc = 9620 SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get()); 9621 assert(DistInc.isUsable() && "distribute inc expr was not built"); 9622 DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(), 9623 DistInc.get()); 9624 DistInc = 9625 SemaRef.ActOnFinishFullExpr(DistInc.get(), /*DiscardedValue*/ false); 9626 assert(DistInc.isUsable() && "distribute inc expr was not built"); 9627 9628 // Build expression: UB = min(UB, prevUB) for #for in composite or combined 9629 // construct 9630 ExprResult NewPrevUB = PrevUB; 9631 SourceLocation DistEUBLoc = AStmt->getBeginLoc(); 9632 if (!SemaRef.Context.hasSameType(UB.get()->getType(), 9633 PrevUB.get()->getType())) { 9634 NewPrevUB = SemaRef.BuildCStyleCastExpr( 9635 DistEUBLoc, 9636 SemaRef.Context.getTrivialTypeSourceInfo(UB.get()->getType()), 9637 DistEUBLoc, NewPrevUB.get()); 9638 if (!NewPrevUB.isUsable()) 9639 return 0; 9640 } 9641 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, 9642 UB.get(), NewPrevUB.get()); 9643 ExprResult CondOp = SemaRef.ActOnConditionalOp( 9644 DistEUBLoc, DistEUBLoc, IsUBGreater.get(), NewPrevUB.get(), UB.get()); 9645 PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(), 9646 CondOp.get()); 9647 PrevEUB = 9648 SemaRef.ActOnFinishFullExpr(PrevEUB.get(), /*DiscardedValue*/ false); 9649 9650 // Build IV <= PrevUB or IV < PrevUB + 1 for unsigned IV to be used in 9651 // parallel for is in combination with a distribute directive with 9652 // schedule(static, 1) 9653 Expr *BoundPrevUB = PrevUB.get(); 9654 if (UseStrictCompare) { 9655 BoundPrevUB = 9656 SemaRef 9657 .BuildBinOp( 9658 CurScope, CondLoc, BO_Add, BoundPrevUB, 9659 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()) 9660 .get(); 9661 BoundPrevUB = 9662 SemaRef.ActOnFinishFullExpr(BoundPrevUB, /*DiscardedValue*/ false) 9663 .get(); 9664 } 9665 ParForInDistCond = 9666 SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, 9667 IV.get(), BoundPrevUB); 9668 } 9669 9670 // Build updates and final values of the loop counters. 9671 bool HasErrors = false; 9672 Built.Counters.resize(NestedLoopCount); 9673 Built.Inits.resize(NestedLoopCount); 9674 Built.Updates.resize(NestedLoopCount); 9675 Built.Finals.resize(NestedLoopCount); 9676 Built.DependentCounters.resize(NestedLoopCount); 9677 Built.DependentInits.resize(NestedLoopCount); 9678 Built.FinalsConditions.resize(NestedLoopCount); 9679 { 9680 // We implement the following algorithm for obtaining the 9681 // original loop iteration variable values based on the 9682 // value of the collapsed loop iteration variable IV. 9683 // 9684 // Let n+1 be the number of collapsed loops in the nest. 9685 // Iteration variables (I0, I1, .... In) 9686 // Iteration counts (N0, N1, ... Nn) 9687 // 9688 // Acc = IV; 9689 // 9690 // To compute Ik for loop k, 0 <= k <= n, generate: 9691 // Prod = N(k+1) * N(k+2) * ... * Nn; 9692 // Ik = Acc / Prod; 9693 // Acc -= Ik * Prod; 9694 // 9695 ExprResult Acc = IV; 9696 for (unsigned int Cnt = 0; Cnt < NestedLoopCount; ++Cnt) { 9697 LoopIterationSpace &IS = IterSpaces[Cnt]; 9698 SourceLocation UpdLoc = IS.IncSrcRange.getBegin(); 9699 ExprResult Iter; 9700 9701 // Compute prod 9702 ExprResult Prod = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(); 9703 for (unsigned int K = Cnt + 1; K < NestedLoopCount; ++K) 9704 Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Prod.get(), 9705 IterSpaces[K].NumIterations); 9706 9707 // Iter = Acc / Prod 9708 // If there is at least one more inner loop to avoid 9709 // multiplication by 1. 9710 if (Cnt + 1 < NestedLoopCount) 9711 Iter = 9712 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, Acc.get(), Prod.get()); 9713 else 9714 Iter = Acc; 9715 if (!Iter.isUsable()) { 9716 HasErrors = true; 9717 break; 9718 } 9719 9720 // Update Acc: 9721 // Acc -= Iter * Prod 9722 // Check if there is at least one more inner loop to avoid 9723 // multiplication by 1. 9724 if (Cnt + 1 < NestedLoopCount) 9725 Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Iter.get(), 9726 Prod.get()); 9727 else 9728 Prod = Iter; 9729 Acc = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Sub, Acc.get(), Prod.get()); 9730 9731 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step 9732 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()); 9733 DeclRefExpr *CounterVar = buildDeclRefExpr( 9734 SemaRef, VD, IS.CounterVar->getType(), IS.CounterVar->getExprLoc(), 9735 /*RefersToCapture=*/true); 9736 ExprResult Init = 9737 buildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar, 9738 IS.CounterInit, IS.IsNonRectangularLB, Captures); 9739 if (!Init.isUsable()) { 9740 HasErrors = true; 9741 break; 9742 } 9743 ExprResult Update = buildCounterUpdate( 9744 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter, 9745 IS.CounterStep, IS.Subtract, IS.IsNonRectangularLB, &Captures); 9746 if (!Update.isUsable()) { 9747 HasErrors = true; 9748 break; 9749 } 9750 9751 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step 9752 ExprResult Final = 9753 buildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar, 9754 IS.CounterInit, IS.NumIterations, IS.CounterStep, 9755 IS.Subtract, IS.IsNonRectangularLB, &Captures); 9756 if (!Final.isUsable()) { 9757 HasErrors = true; 9758 break; 9759 } 9760 9761 if (!Update.isUsable() || !Final.isUsable()) { 9762 HasErrors = true; 9763 break; 9764 } 9765 // Save results 9766 Built.Counters[Cnt] = IS.CounterVar; 9767 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar; 9768 Built.Inits[Cnt] = Init.get(); 9769 Built.Updates[Cnt] = Update.get(); 9770 Built.Finals[Cnt] = Final.get(); 9771 Built.DependentCounters[Cnt] = nullptr; 9772 Built.DependentInits[Cnt] = nullptr; 9773 Built.FinalsConditions[Cnt] = nullptr; 9774 if (IS.IsNonRectangularLB || IS.IsNonRectangularUB) { 9775 Built.DependentCounters[Cnt] = 9776 Built.Counters[NestedLoopCount - 1 - IS.LoopDependentIdx]; 9777 Built.DependentInits[Cnt] = 9778 Built.Inits[NestedLoopCount - 1 - IS.LoopDependentIdx]; 9779 Built.FinalsConditions[Cnt] = IS.FinalCondition; 9780 } 9781 } 9782 } 9783 9784 if (HasErrors) 9785 return 0; 9786 9787 // Save results 9788 Built.IterationVarRef = IV.get(); 9789 Built.LastIteration = LastIteration.get(); 9790 Built.NumIterations = NumIterations.get(); 9791 Built.CalcLastIteration = SemaRef 9792 .ActOnFinishFullExpr(CalcLastIteration.get(), 9793 /*DiscardedValue=*/false) 9794 .get(); 9795 Built.PreCond = PreCond.get(); 9796 Built.PreInits = buildPreInits(C, Captures); 9797 Built.Cond = Cond.get(); 9798 Built.Init = Init.get(); 9799 Built.Inc = Inc.get(); 9800 Built.LB = LB.get(); 9801 Built.UB = UB.get(); 9802 Built.IL = IL.get(); 9803 Built.ST = ST.get(); 9804 Built.EUB = EUB.get(); 9805 Built.NLB = NextLB.get(); 9806 Built.NUB = NextUB.get(); 9807 Built.PrevLB = PrevLB.get(); 9808 Built.PrevUB = PrevUB.get(); 9809 Built.DistInc = DistInc.get(); 9810 Built.PrevEUB = PrevEUB.get(); 9811 Built.DistCombinedFields.LB = CombLB.get(); 9812 Built.DistCombinedFields.UB = CombUB.get(); 9813 Built.DistCombinedFields.EUB = CombEUB.get(); 9814 Built.DistCombinedFields.Init = CombInit.get(); 9815 Built.DistCombinedFields.Cond = CombCond.get(); 9816 Built.DistCombinedFields.NLB = CombNextLB.get(); 9817 Built.DistCombinedFields.NUB = CombNextUB.get(); 9818 Built.DistCombinedFields.DistCond = CombDistCond.get(); 9819 Built.DistCombinedFields.ParForInDistCond = ParForInDistCond.get(); 9820 9821 return NestedLoopCount; 9822 } 9823 9824 static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) { 9825 auto CollapseClauses = 9826 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses); 9827 if (CollapseClauses.begin() != CollapseClauses.end()) 9828 return (*CollapseClauses.begin())->getNumForLoops(); 9829 return nullptr; 9830 } 9831 9832 static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) { 9833 auto OrderedClauses = 9834 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses); 9835 if (OrderedClauses.begin() != OrderedClauses.end()) 9836 return (*OrderedClauses.begin())->getNumForLoops(); 9837 return nullptr; 9838 } 9839 9840 static bool checkSimdlenSafelenSpecified(Sema &S, 9841 const ArrayRef<OMPClause *> Clauses) { 9842 const OMPSafelenClause *Safelen = nullptr; 9843 const OMPSimdlenClause *Simdlen = nullptr; 9844 9845 for (const OMPClause *Clause : Clauses) { 9846 if (Clause->getClauseKind() == OMPC_safelen) 9847 Safelen = cast<OMPSafelenClause>(Clause); 9848 else if (Clause->getClauseKind() == OMPC_simdlen) 9849 Simdlen = cast<OMPSimdlenClause>(Clause); 9850 if (Safelen && Simdlen) 9851 break; 9852 } 9853 9854 if (Simdlen && Safelen) { 9855 const Expr *SimdlenLength = Simdlen->getSimdlen(); 9856 const Expr *SafelenLength = Safelen->getSafelen(); 9857 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() || 9858 SimdlenLength->isInstantiationDependent() || 9859 SimdlenLength->containsUnexpandedParameterPack()) 9860 return false; 9861 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() || 9862 SafelenLength->isInstantiationDependent() || 9863 SafelenLength->containsUnexpandedParameterPack()) 9864 return false; 9865 Expr::EvalResult SimdlenResult, SafelenResult; 9866 SimdlenLength->EvaluateAsInt(SimdlenResult, S.Context); 9867 SafelenLength->EvaluateAsInt(SafelenResult, S.Context); 9868 llvm::APSInt SimdlenRes = SimdlenResult.Val.getInt(); 9869 llvm::APSInt SafelenRes = SafelenResult.Val.getInt(); 9870 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions] 9871 // If both simdlen and safelen clauses are specified, the value of the 9872 // simdlen parameter must be less than or equal to the value of the safelen 9873 // parameter. 9874 if (SimdlenRes > SafelenRes) { 9875 S.Diag(SimdlenLength->getExprLoc(), 9876 diag::err_omp_wrong_simdlen_safelen_values) 9877 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange(); 9878 return true; 9879 } 9880 } 9881 return false; 9882 } 9883 9884 StmtResult 9885 Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt, 9886 SourceLocation StartLoc, SourceLocation EndLoc, 9887 VarsWithInheritedDSAType &VarsWithImplicitDSA) { 9888 if (!AStmt) 9889 return StmtError(); 9890 9891 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9892 OMPLoopBasedDirective::HelperExprs B; 9893 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 9894 // define the nested loops number. 9895 unsigned NestedLoopCount = checkOpenMPLoop( 9896 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), 9897 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 9898 if (NestedLoopCount == 0) 9899 return StmtError(); 9900 9901 assert((CurContext->isDependentContext() || B.builtAll()) && 9902 "omp simd loop exprs were not built"); 9903 9904 if (!CurContext->isDependentContext()) { 9905 // Finalize the clauses that need pre-built expressions for CodeGen. 9906 for (OMPClause *C : Clauses) { 9907 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 9908 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 9909 B.NumIterations, *this, CurScope, 9910 DSAStack)) 9911 return StmtError(); 9912 } 9913 } 9914 9915 if (checkSimdlenSafelenSpecified(*this, Clauses)) 9916 return StmtError(); 9917 9918 setFunctionHasBranchProtectedScope(); 9919 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 9920 Clauses, AStmt, B); 9921 } 9922 9923 StmtResult 9924 Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt, 9925 SourceLocation StartLoc, SourceLocation EndLoc, 9926 VarsWithInheritedDSAType &VarsWithImplicitDSA) { 9927 if (!AStmt) 9928 return StmtError(); 9929 9930 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9931 OMPLoopBasedDirective::HelperExprs B; 9932 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 9933 // define the nested loops number. 9934 unsigned NestedLoopCount = checkOpenMPLoop( 9935 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), 9936 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 9937 if (NestedLoopCount == 0) 9938 return StmtError(); 9939 9940 assert((CurContext->isDependentContext() || B.builtAll()) && 9941 "omp for loop exprs were not built"); 9942 9943 if (!CurContext->isDependentContext()) { 9944 // Finalize the clauses that need pre-built expressions for CodeGen. 9945 for (OMPClause *C : Clauses) { 9946 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 9947 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 9948 B.NumIterations, *this, CurScope, 9949 DSAStack)) 9950 return StmtError(); 9951 } 9952 } 9953 9954 setFunctionHasBranchProtectedScope(); 9955 return OMPForDirective::Create( 9956 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 9957 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 9958 } 9959 9960 StmtResult Sema::ActOnOpenMPForSimdDirective( 9961 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 9962 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 9963 if (!AStmt) 9964 return StmtError(); 9965 9966 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9967 OMPLoopBasedDirective::HelperExprs B; 9968 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 9969 // define the nested loops number. 9970 unsigned NestedLoopCount = 9971 checkOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses), 9972 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 9973 VarsWithImplicitDSA, B); 9974 if (NestedLoopCount == 0) 9975 return StmtError(); 9976 9977 assert((CurContext->isDependentContext() || B.builtAll()) && 9978 "omp for simd loop exprs were not built"); 9979 9980 if (!CurContext->isDependentContext()) { 9981 // Finalize the clauses that need pre-built expressions for CodeGen. 9982 for (OMPClause *C : Clauses) { 9983 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 9984 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 9985 B.NumIterations, *this, CurScope, 9986 DSAStack)) 9987 return StmtError(); 9988 } 9989 } 9990 9991 if (checkSimdlenSafelenSpecified(*this, Clauses)) 9992 return StmtError(); 9993 9994 setFunctionHasBranchProtectedScope(); 9995 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 9996 Clauses, AStmt, B); 9997 } 9998 9999 StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses, 10000 Stmt *AStmt, 10001 SourceLocation StartLoc, 10002 SourceLocation EndLoc) { 10003 if (!AStmt) 10004 return StmtError(); 10005 10006 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10007 auto BaseStmt = AStmt; 10008 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 10009 BaseStmt = CS->getCapturedStmt(); 10010 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 10011 auto S = C->children(); 10012 if (S.begin() == S.end()) 10013 return StmtError(); 10014 // All associated statements must be '#pragma omp section' except for 10015 // the first one. 10016 for (Stmt *SectionStmt : llvm::drop_begin(S)) { 10017 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 10018 if (SectionStmt) 10019 Diag(SectionStmt->getBeginLoc(), 10020 diag::err_omp_sections_substmt_not_section); 10021 return StmtError(); 10022 } 10023 cast<OMPSectionDirective>(SectionStmt) 10024 ->setHasCancel(DSAStack->isCancelRegion()); 10025 } 10026 } else { 10027 Diag(AStmt->getBeginLoc(), diag::err_omp_sections_not_compound_stmt); 10028 return StmtError(); 10029 } 10030 10031 setFunctionHasBranchProtectedScope(); 10032 10033 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 10034 DSAStack->getTaskgroupReductionRef(), 10035 DSAStack->isCancelRegion()); 10036 } 10037 10038 StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt, 10039 SourceLocation StartLoc, 10040 SourceLocation EndLoc) { 10041 if (!AStmt) 10042 return StmtError(); 10043 10044 setFunctionHasBranchProtectedScope(); 10045 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion()); 10046 10047 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt, 10048 DSAStack->isCancelRegion()); 10049 } 10050 10051 static Expr *getDirectCallExpr(Expr *E) { 10052 E = E->IgnoreParenCasts()->IgnoreImplicit(); 10053 if (auto *CE = dyn_cast<CallExpr>(E)) 10054 if (CE->getDirectCallee()) 10055 return E; 10056 return nullptr; 10057 } 10058 10059 StmtResult Sema::ActOnOpenMPDispatchDirective(ArrayRef<OMPClause *> Clauses, 10060 Stmt *AStmt, 10061 SourceLocation StartLoc, 10062 SourceLocation EndLoc) { 10063 if (!AStmt) 10064 return StmtError(); 10065 10066 Stmt *S = cast<CapturedStmt>(AStmt)->getCapturedStmt(); 10067 10068 // 5.1 OpenMP 10069 // expression-stmt : an expression statement with one of the following forms: 10070 // expression = target-call ( [expression-list] ); 10071 // target-call ( [expression-list] ); 10072 10073 SourceLocation TargetCallLoc; 10074 10075 if (!CurContext->isDependentContext()) { 10076 Expr *TargetCall = nullptr; 10077 10078 auto *E = dyn_cast<Expr>(S); 10079 if (!E) { 10080 Diag(S->getBeginLoc(), diag::err_omp_dispatch_statement_call); 10081 return StmtError(); 10082 } 10083 10084 E = E->IgnoreParenCasts()->IgnoreImplicit(); 10085 10086 if (auto *BO = dyn_cast<BinaryOperator>(E)) { 10087 if (BO->getOpcode() == BO_Assign) 10088 TargetCall = getDirectCallExpr(BO->getRHS()); 10089 } else { 10090 if (auto *COCE = dyn_cast<CXXOperatorCallExpr>(E)) 10091 if (COCE->getOperator() == OO_Equal) 10092 TargetCall = getDirectCallExpr(COCE->getArg(1)); 10093 if (!TargetCall) 10094 TargetCall = getDirectCallExpr(E); 10095 } 10096 if (!TargetCall) { 10097 Diag(E->getBeginLoc(), diag::err_omp_dispatch_statement_call); 10098 return StmtError(); 10099 } 10100 TargetCallLoc = TargetCall->getExprLoc(); 10101 } 10102 10103 setFunctionHasBranchProtectedScope(); 10104 10105 return OMPDispatchDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 10106 TargetCallLoc); 10107 } 10108 10109 StmtResult Sema::ActOnOpenMPGenericLoopDirective( 10110 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10111 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10112 if (!AStmt) 10113 return StmtError(); 10114 10115 // OpenMP 5.1 [2.11.7, loop construct] 10116 // A list item may not appear in a lastprivate clause unless it is the 10117 // loop iteration variable of a loop that is associated with the construct. 10118 for (OMPClause *C : Clauses) { 10119 if (auto *LPC = dyn_cast<OMPLastprivateClause>(C)) { 10120 for (Expr *RefExpr : LPC->varlists()) { 10121 SourceLocation ELoc; 10122 SourceRange ERange; 10123 Expr *SimpleRefExpr = RefExpr; 10124 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 10125 if (ValueDecl *D = Res.first) { 10126 auto &&Info = DSAStack->isLoopControlVariable(D); 10127 if (!Info.first) { 10128 Diag(ELoc, diag::err_omp_lastprivate_loop_var_non_loop_iteration); 10129 return StmtError(); 10130 } 10131 } 10132 } 10133 } 10134 } 10135 10136 auto *CS = cast<CapturedStmt>(AStmt); 10137 // 1.2.2 OpenMP Language Terminology 10138 // Structured block - An executable statement with a single entry at the 10139 // top and a single exit at the bottom. 10140 // The point of exit cannot be a branch out of the structured block. 10141 // longjmp() and throw() must not violate the entry/exit criteria. 10142 CS->getCapturedDecl()->setNothrow(); 10143 10144 OMPLoopDirective::HelperExprs B; 10145 // In presence of clause 'collapse', it will define the nested loops number. 10146 unsigned NestedLoopCount = checkOpenMPLoop( 10147 OMPD_loop, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), 10148 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 10149 if (NestedLoopCount == 0) 10150 return StmtError(); 10151 10152 assert((CurContext->isDependentContext() || B.builtAll()) && 10153 "omp loop exprs were not built"); 10154 10155 setFunctionHasBranchProtectedScope(); 10156 return OMPGenericLoopDirective::Create(Context, StartLoc, EndLoc, 10157 NestedLoopCount, Clauses, AStmt, B); 10158 } 10159 10160 StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses, 10161 Stmt *AStmt, 10162 SourceLocation StartLoc, 10163 SourceLocation EndLoc) { 10164 if (!AStmt) 10165 return StmtError(); 10166 10167 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10168 10169 setFunctionHasBranchProtectedScope(); 10170 10171 // OpenMP [2.7.3, single Construct, Restrictions] 10172 // The copyprivate clause must not be used with the nowait clause. 10173 const OMPClause *Nowait = nullptr; 10174 const OMPClause *Copyprivate = nullptr; 10175 for (const OMPClause *Clause : Clauses) { 10176 if (Clause->getClauseKind() == OMPC_nowait) 10177 Nowait = Clause; 10178 else if (Clause->getClauseKind() == OMPC_copyprivate) 10179 Copyprivate = Clause; 10180 if (Copyprivate && Nowait) { 10181 Diag(Copyprivate->getBeginLoc(), 10182 diag::err_omp_single_copyprivate_with_nowait); 10183 Diag(Nowait->getBeginLoc(), diag::note_omp_nowait_clause_here); 10184 return StmtError(); 10185 } 10186 } 10187 10188 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 10189 } 10190 10191 StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt, 10192 SourceLocation StartLoc, 10193 SourceLocation EndLoc) { 10194 if (!AStmt) 10195 return StmtError(); 10196 10197 setFunctionHasBranchProtectedScope(); 10198 10199 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt); 10200 } 10201 10202 StmtResult Sema::ActOnOpenMPMaskedDirective(ArrayRef<OMPClause *> Clauses, 10203 Stmt *AStmt, 10204 SourceLocation StartLoc, 10205 SourceLocation EndLoc) { 10206 if (!AStmt) 10207 return StmtError(); 10208 10209 setFunctionHasBranchProtectedScope(); 10210 10211 return OMPMaskedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 10212 } 10213 10214 StmtResult Sema::ActOnOpenMPCriticalDirective( 10215 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses, 10216 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { 10217 if (!AStmt) 10218 return StmtError(); 10219 10220 bool ErrorFound = false; 10221 llvm::APSInt Hint; 10222 SourceLocation HintLoc; 10223 bool DependentHint = false; 10224 for (const OMPClause *C : Clauses) { 10225 if (C->getClauseKind() == OMPC_hint) { 10226 if (!DirName.getName()) { 10227 Diag(C->getBeginLoc(), diag::err_omp_hint_clause_no_name); 10228 ErrorFound = true; 10229 } 10230 Expr *E = cast<OMPHintClause>(C)->getHint(); 10231 if (E->isTypeDependent() || E->isValueDependent() || 10232 E->isInstantiationDependent()) { 10233 DependentHint = true; 10234 } else { 10235 Hint = E->EvaluateKnownConstInt(Context); 10236 HintLoc = C->getBeginLoc(); 10237 } 10238 } 10239 } 10240 if (ErrorFound) 10241 return StmtError(); 10242 const auto Pair = DSAStack->getCriticalWithHint(DirName); 10243 if (Pair.first && DirName.getName() && !DependentHint) { 10244 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) { 10245 Diag(StartLoc, diag::err_omp_critical_with_hint); 10246 if (HintLoc.isValid()) 10247 Diag(HintLoc, diag::note_omp_critical_hint_here) 10248 << 0 << toString(Hint, /*Radix=*/10, /*Signed=*/false); 10249 else 10250 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0; 10251 if (const auto *C = Pair.first->getSingleClause<OMPHintClause>()) { 10252 Diag(C->getBeginLoc(), diag::note_omp_critical_hint_here) 10253 << 1 10254 << toString(C->getHint()->EvaluateKnownConstInt(Context), 10255 /*Radix=*/10, /*Signed=*/false); 10256 } else { 10257 Diag(Pair.first->getBeginLoc(), diag::note_omp_critical_no_hint) << 1; 10258 } 10259 } 10260 } 10261 10262 setFunctionHasBranchProtectedScope(); 10263 10264 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc, 10265 Clauses, AStmt); 10266 if (!Pair.first && DirName.getName() && !DependentHint) 10267 DSAStack->addCriticalWithHint(Dir, Hint); 10268 return Dir; 10269 } 10270 10271 StmtResult Sema::ActOnOpenMPParallelForDirective( 10272 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10273 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10274 if (!AStmt) 10275 return StmtError(); 10276 10277 auto *CS = cast<CapturedStmt>(AStmt); 10278 // 1.2.2 OpenMP Language Terminology 10279 // Structured block - An executable statement with a single entry at the 10280 // top and a single exit at the bottom. 10281 // The point of exit cannot be a branch out of the structured block. 10282 // longjmp() and throw() must not violate the entry/exit criteria. 10283 CS->getCapturedDecl()->setNothrow(); 10284 10285 OMPLoopBasedDirective::HelperExprs B; 10286 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 10287 // define the nested loops number. 10288 unsigned NestedLoopCount = 10289 checkOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses), 10290 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 10291 VarsWithImplicitDSA, B); 10292 if (NestedLoopCount == 0) 10293 return StmtError(); 10294 10295 assert((CurContext->isDependentContext() || B.builtAll()) && 10296 "omp parallel for loop exprs were not built"); 10297 10298 if (!CurContext->isDependentContext()) { 10299 // Finalize the clauses that need pre-built expressions for CodeGen. 10300 for (OMPClause *C : Clauses) { 10301 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 10302 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 10303 B.NumIterations, *this, CurScope, 10304 DSAStack)) 10305 return StmtError(); 10306 } 10307 } 10308 10309 setFunctionHasBranchProtectedScope(); 10310 return OMPParallelForDirective::Create( 10311 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 10312 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 10313 } 10314 10315 StmtResult Sema::ActOnOpenMPParallelForSimdDirective( 10316 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10317 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10318 if (!AStmt) 10319 return StmtError(); 10320 10321 auto *CS = cast<CapturedStmt>(AStmt); 10322 // 1.2.2 OpenMP Language Terminology 10323 // Structured block - An executable statement with a single entry at the 10324 // top and a single exit at the bottom. 10325 // The point of exit cannot be a branch out of the structured block. 10326 // longjmp() and throw() must not violate the entry/exit criteria. 10327 CS->getCapturedDecl()->setNothrow(); 10328 10329 OMPLoopBasedDirective::HelperExprs B; 10330 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 10331 // define the nested loops number. 10332 unsigned NestedLoopCount = 10333 checkOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses), 10334 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 10335 VarsWithImplicitDSA, B); 10336 if (NestedLoopCount == 0) 10337 return StmtError(); 10338 10339 if (!CurContext->isDependentContext()) { 10340 // Finalize the clauses that need pre-built expressions for CodeGen. 10341 for (OMPClause *C : Clauses) { 10342 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 10343 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 10344 B.NumIterations, *this, CurScope, 10345 DSAStack)) 10346 return StmtError(); 10347 } 10348 } 10349 10350 if (checkSimdlenSafelenSpecified(*this, Clauses)) 10351 return StmtError(); 10352 10353 setFunctionHasBranchProtectedScope(); 10354 return OMPParallelForSimdDirective::Create( 10355 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 10356 } 10357 10358 StmtResult 10359 Sema::ActOnOpenMPParallelMasterDirective(ArrayRef<OMPClause *> Clauses, 10360 Stmt *AStmt, SourceLocation StartLoc, 10361 SourceLocation EndLoc) { 10362 if (!AStmt) 10363 return StmtError(); 10364 10365 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10366 auto *CS = cast<CapturedStmt>(AStmt); 10367 // 1.2.2 OpenMP Language Terminology 10368 // Structured block - An executable statement with a single entry at the 10369 // top and a single exit at the bottom. 10370 // The point of exit cannot be a branch out of the structured block. 10371 // longjmp() and throw() must not violate the entry/exit criteria. 10372 CS->getCapturedDecl()->setNothrow(); 10373 10374 setFunctionHasBranchProtectedScope(); 10375 10376 return OMPParallelMasterDirective::Create( 10377 Context, StartLoc, EndLoc, Clauses, AStmt, 10378 DSAStack->getTaskgroupReductionRef()); 10379 } 10380 10381 StmtResult 10382 Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses, 10383 Stmt *AStmt, SourceLocation StartLoc, 10384 SourceLocation EndLoc) { 10385 if (!AStmt) 10386 return StmtError(); 10387 10388 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10389 auto BaseStmt = AStmt; 10390 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 10391 BaseStmt = CS->getCapturedStmt(); 10392 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 10393 auto S = C->children(); 10394 if (S.begin() == S.end()) 10395 return StmtError(); 10396 // All associated statements must be '#pragma omp section' except for 10397 // the first one. 10398 for (Stmt *SectionStmt : llvm::drop_begin(S)) { 10399 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 10400 if (SectionStmt) 10401 Diag(SectionStmt->getBeginLoc(), 10402 diag::err_omp_parallel_sections_substmt_not_section); 10403 return StmtError(); 10404 } 10405 cast<OMPSectionDirective>(SectionStmt) 10406 ->setHasCancel(DSAStack->isCancelRegion()); 10407 } 10408 } else { 10409 Diag(AStmt->getBeginLoc(), 10410 diag::err_omp_parallel_sections_not_compound_stmt); 10411 return StmtError(); 10412 } 10413 10414 setFunctionHasBranchProtectedScope(); 10415 10416 return OMPParallelSectionsDirective::Create( 10417 Context, StartLoc, EndLoc, Clauses, AStmt, 10418 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 10419 } 10420 10421 /// Find and diagnose mutually exclusive clause kinds. 10422 static bool checkMutuallyExclusiveClauses( 10423 Sema &S, ArrayRef<OMPClause *> Clauses, 10424 ArrayRef<OpenMPClauseKind> MutuallyExclusiveClauses) { 10425 const OMPClause *PrevClause = nullptr; 10426 bool ErrorFound = false; 10427 for (const OMPClause *C : Clauses) { 10428 if (llvm::is_contained(MutuallyExclusiveClauses, C->getClauseKind())) { 10429 if (!PrevClause) { 10430 PrevClause = C; 10431 } else if (PrevClause->getClauseKind() != C->getClauseKind()) { 10432 S.Diag(C->getBeginLoc(), diag::err_omp_clauses_mutually_exclusive) 10433 << getOpenMPClauseName(C->getClauseKind()) 10434 << getOpenMPClauseName(PrevClause->getClauseKind()); 10435 S.Diag(PrevClause->getBeginLoc(), diag::note_omp_previous_clause) 10436 << getOpenMPClauseName(PrevClause->getClauseKind()); 10437 ErrorFound = true; 10438 } 10439 } 10440 } 10441 return ErrorFound; 10442 } 10443 10444 StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses, 10445 Stmt *AStmt, SourceLocation StartLoc, 10446 SourceLocation EndLoc) { 10447 if (!AStmt) 10448 return StmtError(); 10449 10450 // OpenMP 5.0, 2.10.1 task Construct 10451 // If a detach clause appears on the directive, then a mergeable clause cannot 10452 // appear on the same directive. 10453 if (checkMutuallyExclusiveClauses(*this, Clauses, 10454 {OMPC_detach, OMPC_mergeable})) 10455 return StmtError(); 10456 10457 auto *CS = cast<CapturedStmt>(AStmt); 10458 // 1.2.2 OpenMP Language Terminology 10459 // Structured block - An executable statement with a single entry at the 10460 // top and a single exit at the bottom. 10461 // The point of exit cannot be a branch out of the structured block. 10462 // longjmp() and throw() must not violate the entry/exit criteria. 10463 CS->getCapturedDecl()->setNothrow(); 10464 10465 setFunctionHasBranchProtectedScope(); 10466 10467 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 10468 DSAStack->isCancelRegion()); 10469 } 10470 10471 StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc, 10472 SourceLocation EndLoc) { 10473 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc); 10474 } 10475 10476 StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc, 10477 SourceLocation EndLoc) { 10478 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc); 10479 } 10480 10481 StmtResult Sema::ActOnOpenMPTaskwaitDirective(ArrayRef<OMPClause *> Clauses, 10482 SourceLocation StartLoc, 10483 SourceLocation EndLoc) { 10484 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc, Clauses); 10485 } 10486 10487 StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses, 10488 Stmt *AStmt, 10489 SourceLocation StartLoc, 10490 SourceLocation EndLoc) { 10491 if (!AStmt) 10492 return StmtError(); 10493 10494 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10495 10496 setFunctionHasBranchProtectedScope(); 10497 10498 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses, 10499 AStmt, 10500 DSAStack->getTaskgroupReductionRef()); 10501 } 10502 10503 StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses, 10504 SourceLocation StartLoc, 10505 SourceLocation EndLoc) { 10506 OMPFlushClause *FC = nullptr; 10507 OMPClause *OrderClause = nullptr; 10508 for (OMPClause *C : Clauses) { 10509 if (C->getClauseKind() == OMPC_flush) 10510 FC = cast<OMPFlushClause>(C); 10511 else 10512 OrderClause = C; 10513 } 10514 OpenMPClauseKind MemOrderKind = OMPC_unknown; 10515 SourceLocation MemOrderLoc; 10516 for (const OMPClause *C : Clauses) { 10517 if (C->getClauseKind() == OMPC_acq_rel || 10518 C->getClauseKind() == OMPC_acquire || 10519 C->getClauseKind() == OMPC_release) { 10520 if (MemOrderKind != OMPC_unknown) { 10521 Diag(C->getBeginLoc(), diag::err_omp_several_mem_order_clauses) 10522 << getOpenMPDirectiveName(OMPD_flush) << 1 10523 << SourceRange(C->getBeginLoc(), C->getEndLoc()); 10524 Diag(MemOrderLoc, diag::note_omp_previous_mem_order_clause) 10525 << getOpenMPClauseName(MemOrderKind); 10526 } else { 10527 MemOrderKind = C->getClauseKind(); 10528 MemOrderLoc = C->getBeginLoc(); 10529 } 10530 } 10531 } 10532 if (FC && OrderClause) { 10533 Diag(FC->getLParenLoc(), diag::err_omp_flush_order_clause_and_list) 10534 << getOpenMPClauseName(OrderClause->getClauseKind()); 10535 Diag(OrderClause->getBeginLoc(), diag::note_omp_flush_order_clause_here) 10536 << getOpenMPClauseName(OrderClause->getClauseKind()); 10537 return StmtError(); 10538 } 10539 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses); 10540 } 10541 10542 StmtResult Sema::ActOnOpenMPDepobjDirective(ArrayRef<OMPClause *> Clauses, 10543 SourceLocation StartLoc, 10544 SourceLocation EndLoc) { 10545 if (Clauses.empty()) { 10546 Diag(StartLoc, diag::err_omp_depobj_expected); 10547 return StmtError(); 10548 } else if (Clauses[0]->getClauseKind() != OMPC_depobj) { 10549 Diag(Clauses[0]->getBeginLoc(), diag::err_omp_depobj_expected); 10550 return StmtError(); 10551 } 10552 // Only depobj expression and another single clause is allowed. 10553 if (Clauses.size() > 2) { 10554 Diag(Clauses[2]->getBeginLoc(), 10555 diag::err_omp_depobj_single_clause_expected); 10556 return StmtError(); 10557 } else if (Clauses.size() < 1) { 10558 Diag(Clauses[0]->getEndLoc(), diag::err_omp_depobj_single_clause_expected); 10559 return StmtError(); 10560 } 10561 return OMPDepobjDirective::Create(Context, StartLoc, EndLoc, Clauses); 10562 } 10563 10564 StmtResult Sema::ActOnOpenMPScanDirective(ArrayRef<OMPClause *> Clauses, 10565 SourceLocation StartLoc, 10566 SourceLocation EndLoc) { 10567 // Check that exactly one clause is specified. 10568 if (Clauses.size() != 1) { 10569 Diag(Clauses.empty() ? EndLoc : Clauses[1]->getBeginLoc(), 10570 diag::err_omp_scan_single_clause_expected); 10571 return StmtError(); 10572 } 10573 // Check that scan directive is used in the scopeof the OpenMP loop body. 10574 if (Scope *S = DSAStack->getCurScope()) { 10575 Scope *ParentS = S->getParent(); 10576 if (!ParentS || ParentS->getParent() != ParentS->getBreakParent() || 10577 !ParentS->getBreakParent()->isOpenMPLoopScope()) 10578 return StmtError(Diag(StartLoc, diag::err_omp_orphaned_device_directive) 10579 << getOpenMPDirectiveName(OMPD_scan) << 5); 10580 } 10581 // Check that only one instance of scan directives is used in the same outer 10582 // region. 10583 if (DSAStack->doesParentHasScanDirective()) { 10584 Diag(StartLoc, diag::err_omp_several_directives_in_region) << "scan"; 10585 Diag(DSAStack->getParentScanDirectiveLoc(), 10586 diag::note_omp_previous_directive) 10587 << "scan"; 10588 return StmtError(); 10589 } 10590 DSAStack->setParentHasScanDirective(StartLoc); 10591 return OMPScanDirective::Create(Context, StartLoc, EndLoc, Clauses); 10592 } 10593 10594 StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses, 10595 Stmt *AStmt, 10596 SourceLocation StartLoc, 10597 SourceLocation EndLoc) { 10598 const OMPClause *DependFound = nullptr; 10599 const OMPClause *DependSourceClause = nullptr; 10600 const OMPClause *DependSinkClause = nullptr; 10601 bool ErrorFound = false; 10602 const OMPThreadsClause *TC = nullptr; 10603 const OMPSIMDClause *SC = nullptr; 10604 for (const OMPClause *C : Clauses) { 10605 if (auto *DC = dyn_cast<OMPDependClause>(C)) { 10606 DependFound = C; 10607 if (DC->getDependencyKind() == OMPC_DEPEND_source) { 10608 if (DependSourceClause) { 10609 Diag(C->getBeginLoc(), diag::err_omp_more_one_clause) 10610 << getOpenMPDirectiveName(OMPD_ordered) 10611 << getOpenMPClauseName(OMPC_depend) << 2; 10612 ErrorFound = true; 10613 } else { 10614 DependSourceClause = C; 10615 } 10616 if (DependSinkClause) { 10617 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed) 10618 << 0; 10619 ErrorFound = true; 10620 } 10621 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) { 10622 if (DependSourceClause) { 10623 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed) 10624 << 1; 10625 ErrorFound = true; 10626 } 10627 DependSinkClause = C; 10628 } 10629 } else if (C->getClauseKind() == OMPC_threads) { 10630 TC = cast<OMPThreadsClause>(C); 10631 } else if (C->getClauseKind() == OMPC_simd) { 10632 SC = cast<OMPSIMDClause>(C); 10633 } 10634 } 10635 if (!ErrorFound && !SC && 10636 isOpenMPSimdDirective(DSAStack->getParentDirective())) { 10637 // OpenMP [2.8.1,simd Construct, Restrictions] 10638 // An ordered construct with the simd clause is the only OpenMP construct 10639 // that can appear in the simd region. 10640 Diag(StartLoc, diag::err_omp_prohibited_region_simd) 10641 << (LangOpts.OpenMP >= 50 ? 1 : 0); 10642 ErrorFound = true; 10643 } else if (DependFound && (TC || SC)) { 10644 Diag(DependFound->getBeginLoc(), diag::err_omp_depend_clause_thread_simd) 10645 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind()); 10646 ErrorFound = true; 10647 } else if (DependFound && !DSAStack->getParentOrderedRegionParam().first) { 10648 Diag(DependFound->getBeginLoc(), 10649 diag::err_omp_ordered_directive_without_param); 10650 ErrorFound = true; 10651 } else if (TC || Clauses.empty()) { 10652 if (const Expr *Param = DSAStack->getParentOrderedRegionParam().first) { 10653 SourceLocation ErrLoc = TC ? TC->getBeginLoc() : StartLoc; 10654 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param) 10655 << (TC != nullptr); 10656 Diag(Param->getBeginLoc(), diag::note_omp_ordered_param) << 1; 10657 ErrorFound = true; 10658 } 10659 } 10660 if ((!AStmt && !DependFound) || ErrorFound) 10661 return StmtError(); 10662 10663 // OpenMP 5.0, 2.17.9, ordered Construct, Restrictions. 10664 // During execution of an iteration of a worksharing-loop or a loop nest 10665 // within a worksharing-loop, simd, or worksharing-loop SIMD region, a thread 10666 // must not execute more than one ordered region corresponding to an ordered 10667 // construct without a depend clause. 10668 if (!DependFound) { 10669 if (DSAStack->doesParentHasOrderedDirective()) { 10670 Diag(StartLoc, diag::err_omp_several_directives_in_region) << "ordered"; 10671 Diag(DSAStack->getParentOrderedDirectiveLoc(), 10672 diag::note_omp_previous_directive) 10673 << "ordered"; 10674 return StmtError(); 10675 } 10676 DSAStack->setParentHasOrderedDirective(StartLoc); 10677 } 10678 10679 if (AStmt) { 10680 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10681 10682 setFunctionHasBranchProtectedScope(); 10683 } 10684 10685 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 10686 } 10687 10688 namespace { 10689 /// Helper class for checking expression in 'omp atomic [update]' 10690 /// construct. 10691 class OpenMPAtomicUpdateChecker { 10692 /// Error results for atomic update expressions. 10693 enum ExprAnalysisErrorCode { 10694 /// A statement is not an expression statement. 10695 NotAnExpression, 10696 /// Expression is not builtin binary or unary operation. 10697 NotABinaryOrUnaryExpression, 10698 /// Unary operation is not post-/pre- increment/decrement operation. 10699 NotAnUnaryIncDecExpression, 10700 /// An expression is not of scalar type. 10701 NotAScalarType, 10702 /// A binary operation is not an assignment operation. 10703 NotAnAssignmentOp, 10704 /// RHS part of the binary operation is not a binary expression. 10705 NotABinaryExpression, 10706 /// RHS part is not additive/multiplicative/shift/biwise binary 10707 /// expression. 10708 NotABinaryOperator, 10709 /// RHS binary operation does not have reference to the updated LHS 10710 /// part. 10711 NotAnUpdateExpression, 10712 /// No errors is found. 10713 NoError 10714 }; 10715 /// Reference to Sema. 10716 Sema &SemaRef; 10717 /// A location for note diagnostics (when error is found). 10718 SourceLocation NoteLoc; 10719 /// 'x' lvalue part of the source atomic expression. 10720 Expr *X; 10721 /// 'expr' rvalue part of the source atomic expression. 10722 Expr *E; 10723 /// Helper expression of the form 10724 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or 10725 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. 10726 Expr *UpdateExpr; 10727 /// Is 'x' a LHS in a RHS part of full update expression. It is 10728 /// important for non-associative operations. 10729 bool IsXLHSInRHSPart; 10730 BinaryOperatorKind Op; 10731 SourceLocation OpLoc; 10732 /// true if the source expression is a postfix unary operation, false 10733 /// if it is a prefix unary operation. 10734 bool IsPostfixUpdate; 10735 10736 public: 10737 OpenMPAtomicUpdateChecker(Sema &SemaRef) 10738 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr), 10739 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {} 10740 /// Check specified statement that it is suitable for 'atomic update' 10741 /// constructs and extract 'x', 'expr' and Operation from the original 10742 /// expression. If DiagId and NoteId == 0, then only check is performed 10743 /// without error notification. 10744 /// \param DiagId Diagnostic which should be emitted if error is found. 10745 /// \param NoteId Diagnostic note for the main error message. 10746 /// \return true if statement is not an update expression, false otherwise. 10747 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0); 10748 /// Return the 'x' lvalue part of the source atomic expression. 10749 Expr *getX() const { return X; } 10750 /// Return the 'expr' rvalue part of the source atomic expression. 10751 Expr *getExpr() const { return E; } 10752 /// Return the update expression used in calculation of the updated 10753 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or 10754 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. 10755 Expr *getUpdateExpr() const { return UpdateExpr; } 10756 /// Return true if 'x' is LHS in RHS part of full update expression, 10757 /// false otherwise. 10758 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; } 10759 10760 /// true if the source expression is a postfix unary operation, false 10761 /// if it is a prefix unary operation. 10762 bool isPostfixUpdate() const { return IsPostfixUpdate; } 10763 10764 private: 10765 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0, 10766 unsigned NoteId = 0); 10767 }; 10768 10769 bool OpenMPAtomicUpdateChecker::checkBinaryOperation( 10770 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) { 10771 ExprAnalysisErrorCode ErrorFound = NoError; 10772 SourceLocation ErrorLoc, NoteLoc; 10773 SourceRange ErrorRange, NoteRange; 10774 // Allowed constructs are: 10775 // x = x binop expr; 10776 // x = expr binop x; 10777 if (AtomicBinOp->getOpcode() == BO_Assign) { 10778 X = AtomicBinOp->getLHS(); 10779 if (const auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>( 10780 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) { 10781 if (AtomicInnerBinOp->isMultiplicativeOp() || 10782 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() || 10783 AtomicInnerBinOp->isBitwiseOp()) { 10784 Op = AtomicInnerBinOp->getOpcode(); 10785 OpLoc = AtomicInnerBinOp->getOperatorLoc(); 10786 Expr *LHS = AtomicInnerBinOp->getLHS(); 10787 Expr *RHS = AtomicInnerBinOp->getRHS(); 10788 llvm::FoldingSetNodeID XId, LHSId, RHSId; 10789 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(), 10790 /*Canonical=*/true); 10791 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(), 10792 /*Canonical=*/true); 10793 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(), 10794 /*Canonical=*/true); 10795 if (XId == LHSId) { 10796 E = RHS; 10797 IsXLHSInRHSPart = true; 10798 } else if (XId == RHSId) { 10799 E = LHS; 10800 IsXLHSInRHSPart = false; 10801 } else { 10802 ErrorLoc = AtomicInnerBinOp->getExprLoc(); 10803 ErrorRange = AtomicInnerBinOp->getSourceRange(); 10804 NoteLoc = X->getExprLoc(); 10805 NoteRange = X->getSourceRange(); 10806 ErrorFound = NotAnUpdateExpression; 10807 } 10808 } else { 10809 ErrorLoc = AtomicInnerBinOp->getExprLoc(); 10810 ErrorRange = AtomicInnerBinOp->getSourceRange(); 10811 NoteLoc = AtomicInnerBinOp->getOperatorLoc(); 10812 NoteRange = SourceRange(NoteLoc, NoteLoc); 10813 ErrorFound = NotABinaryOperator; 10814 } 10815 } else { 10816 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc(); 10817 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange(); 10818 ErrorFound = NotABinaryExpression; 10819 } 10820 } else { 10821 ErrorLoc = AtomicBinOp->getExprLoc(); 10822 ErrorRange = AtomicBinOp->getSourceRange(); 10823 NoteLoc = AtomicBinOp->getOperatorLoc(); 10824 NoteRange = SourceRange(NoteLoc, NoteLoc); 10825 ErrorFound = NotAnAssignmentOp; 10826 } 10827 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { 10828 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; 10829 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; 10830 return true; 10831 } 10832 if (SemaRef.CurContext->isDependentContext()) 10833 E = X = UpdateExpr = nullptr; 10834 return ErrorFound != NoError; 10835 } 10836 10837 bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId, 10838 unsigned NoteId) { 10839 ExprAnalysisErrorCode ErrorFound = NoError; 10840 SourceLocation ErrorLoc, NoteLoc; 10841 SourceRange ErrorRange, NoteRange; 10842 // Allowed constructs are: 10843 // x++; 10844 // x--; 10845 // ++x; 10846 // --x; 10847 // x binop= expr; 10848 // x = x binop expr; 10849 // x = expr binop x; 10850 if (auto *AtomicBody = dyn_cast<Expr>(S)) { 10851 AtomicBody = AtomicBody->IgnoreParenImpCasts(); 10852 if (AtomicBody->getType()->isScalarType() || 10853 AtomicBody->isInstantiationDependent()) { 10854 if (const auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>( 10855 AtomicBody->IgnoreParenImpCasts())) { 10856 // Check for Compound Assignment Operation 10857 Op = BinaryOperator::getOpForCompoundAssignment( 10858 AtomicCompAssignOp->getOpcode()); 10859 OpLoc = AtomicCompAssignOp->getOperatorLoc(); 10860 E = AtomicCompAssignOp->getRHS(); 10861 X = AtomicCompAssignOp->getLHS()->IgnoreParens(); 10862 IsXLHSInRHSPart = true; 10863 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>( 10864 AtomicBody->IgnoreParenImpCasts())) { 10865 // Check for Binary Operation 10866 if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId)) 10867 return true; 10868 } else if (const auto *AtomicUnaryOp = dyn_cast<UnaryOperator>( 10869 AtomicBody->IgnoreParenImpCasts())) { 10870 // Check for Unary Operation 10871 if (AtomicUnaryOp->isIncrementDecrementOp()) { 10872 IsPostfixUpdate = AtomicUnaryOp->isPostfix(); 10873 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub; 10874 OpLoc = AtomicUnaryOp->getOperatorLoc(); 10875 X = AtomicUnaryOp->getSubExpr()->IgnoreParens(); 10876 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get(); 10877 IsXLHSInRHSPart = true; 10878 } else { 10879 ErrorFound = NotAnUnaryIncDecExpression; 10880 ErrorLoc = AtomicUnaryOp->getExprLoc(); 10881 ErrorRange = AtomicUnaryOp->getSourceRange(); 10882 NoteLoc = AtomicUnaryOp->getOperatorLoc(); 10883 NoteRange = SourceRange(NoteLoc, NoteLoc); 10884 } 10885 } else if (!AtomicBody->isInstantiationDependent()) { 10886 ErrorFound = NotABinaryOrUnaryExpression; 10887 NoteLoc = ErrorLoc = AtomicBody->getExprLoc(); 10888 NoteRange = ErrorRange = AtomicBody->getSourceRange(); 10889 } 10890 } else { 10891 ErrorFound = NotAScalarType; 10892 NoteLoc = ErrorLoc = AtomicBody->getBeginLoc(); 10893 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 10894 } 10895 } else { 10896 ErrorFound = NotAnExpression; 10897 NoteLoc = ErrorLoc = S->getBeginLoc(); 10898 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 10899 } 10900 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { 10901 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; 10902 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; 10903 return true; 10904 } 10905 if (SemaRef.CurContext->isDependentContext()) 10906 E = X = UpdateExpr = nullptr; 10907 if (ErrorFound == NoError && E && X) { 10908 // Build an update expression of form 'OpaqueValueExpr(x) binop 10909 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop 10910 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression. 10911 auto *OVEX = new (SemaRef.getASTContext()) 10912 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_PRValue); 10913 auto *OVEExpr = new (SemaRef.getASTContext()) 10914 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_PRValue); 10915 ExprResult Update = 10916 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr, 10917 IsXLHSInRHSPart ? OVEExpr : OVEX); 10918 if (Update.isInvalid()) 10919 return true; 10920 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(), 10921 Sema::AA_Casting); 10922 if (Update.isInvalid()) 10923 return true; 10924 UpdateExpr = Update.get(); 10925 } 10926 return ErrorFound != NoError; 10927 } 10928 } // namespace 10929 10930 StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses, 10931 Stmt *AStmt, 10932 SourceLocation StartLoc, 10933 SourceLocation EndLoc) { 10934 // Register location of the first atomic directive. 10935 DSAStack->addAtomicDirectiveLoc(StartLoc); 10936 if (!AStmt) 10937 return StmtError(); 10938 10939 // 1.2.2 OpenMP Language Terminology 10940 // Structured block - An executable statement with a single entry at the 10941 // top and a single exit at the bottom. 10942 // The point of exit cannot be a branch out of the structured block. 10943 // longjmp() and throw() must not violate the entry/exit criteria. 10944 OpenMPClauseKind AtomicKind = OMPC_unknown; 10945 SourceLocation AtomicKindLoc; 10946 OpenMPClauseKind MemOrderKind = OMPC_unknown; 10947 SourceLocation MemOrderLoc; 10948 for (const OMPClause *C : Clauses) { 10949 switch (C->getClauseKind()) { 10950 case OMPC_read: 10951 case OMPC_write: 10952 case OMPC_update: 10953 case OMPC_capture: 10954 case OMPC_compare: { 10955 if (AtomicKind != OMPC_unknown) { 10956 Diag(C->getBeginLoc(), diag::err_omp_atomic_several_clauses) 10957 << SourceRange(C->getBeginLoc(), C->getEndLoc()); 10958 Diag(AtomicKindLoc, diag::note_omp_previous_mem_order_clause) 10959 << getOpenMPClauseName(AtomicKind); 10960 } else { 10961 AtomicKind = C->getClauseKind(); 10962 AtomicKindLoc = C->getBeginLoc(); 10963 } 10964 break; 10965 } 10966 case OMPC_seq_cst: 10967 case OMPC_acq_rel: 10968 case OMPC_acquire: 10969 case OMPC_release: 10970 case OMPC_relaxed: { 10971 if (MemOrderKind != OMPC_unknown) { 10972 Diag(C->getBeginLoc(), diag::err_omp_several_mem_order_clauses) 10973 << getOpenMPDirectiveName(OMPD_atomic) << 0 10974 << SourceRange(C->getBeginLoc(), C->getEndLoc()); 10975 Diag(MemOrderLoc, diag::note_omp_previous_mem_order_clause) 10976 << getOpenMPClauseName(MemOrderKind); 10977 } else { 10978 MemOrderKind = C->getClauseKind(); 10979 MemOrderLoc = C->getBeginLoc(); 10980 } 10981 break; 10982 } 10983 // The following clauses are allowed, but we don't need to do anything here. 10984 case OMPC_hint: 10985 break; 10986 default: 10987 llvm_unreachable("unknown clause is encountered"); 10988 } 10989 } 10990 // OpenMP 5.0, 2.17.7 atomic Construct, Restrictions 10991 // If atomic-clause is read then memory-order-clause must not be acq_rel or 10992 // release. 10993 // If atomic-clause is write then memory-order-clause must not be acq_rel or 10994 // acquire. 10995 // If atomic-clause is update or not present then memory-order-clause must not 10996 // be acq_rel or acquire. 10997 if ((AtomicKind == OMPC_read && 10998 (MemOrderKind == OMPC_acq_rel || MemOrderKind == OMPC_release)) || 10999 ((AtomicKind == OMPC_write || AtomicKind == OMPC_update || 11000 AtomicKind == OMPC_unknown) && 11001 (MemOrderKind == OMPC_acq_rel || MemOrderKind == OMPC_acquire))) { 11002 SourceLocation Loc = AtomicKindLoc; 11003 if (AtomicKind == OMPC_unknown) 11004 Loc = StartLoc; 11005 Diag(Loc, diag::err_omp_atomic_incompatible_mem_order_clause) 11006 << getOpenMPClauseName(AtomicKind) 11007 << (AtomicKind == OMPC_unknown ? 1 : 0) 11008 << getOpenMPClauseName(MemOrderKind); 11009 Diag(MemOrderLoc, diag::note_omp_previous_mem_order_clause) 11010 << getOpenMPClauseName(MemOrderKind); 11011 } 11012 11013 Stmt *Body = AStmt; 11014 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body)) 11015 Body = EWC->getSubExpr(); 11016 11017 Expr *X = nullptr; 11018 Expr *V = nullptr; 11019 Expr *E = nullptr; 11020 Expr *UE = nullptr; 11021 bool IsXLHSInRHSPart = false; 11022 bool IsPostfixUpdate = false; 11023 // OpenMP [2.12.6, atomic Construct] 11024 // In the next expressions: 11025 // * x and v (as applicable) are both l-value expressions with scalar type. 11026 // * During the execution of an atomic region, multiple syntactic 11027 // occurrences of x must designate the same storage location. 11028 // * Neither of v and expr (as applicable) may access the storage location 11029 // designated by x. 11030 // * Neither of x and expr (as applicable) may access the storage location 11031 // designated by v. 11032 // * expr is an expression with scalar type. 11033 // * binop is one of +, *, -, /, &, ^, |, <<, or >>. 11034 // * binop, binop=, ++, and -- are not overloaded operators. 11035 // * The expression x binop expr must be numerically equivalent to x binop 11036 // (expr). This requirement is satisfied if the operators in expr have 11037 // precedence greater than binop, or by using parentheses around expr or 11038 // subexpressions of expr. 11039 // * The expression expr binop x must be numerically equivalent to (expr) 11040 // binop x. This requirement is satisfied if the operators in expr have 11041 // precedence equal to or greater than binop, or by using parentheses around 11042 // expr or subexpressions of expr. 11043 // * For forms that allow multiple occurrences of x, the number of times 11044 // that x is evaluated is unspecified. 11045 if (AtomicKind == OMPC_read) { 11046 enum { 11047 NotAnExpression, 11048 NotAnAssignmentOp, 11049 NotAScalarType, 11050 NotAnLValue, 11051 NoError 11052 } ErrorFound = NoError; 11053 SourceLocation ErrorLoc, NoteLoc; 11054 SourceRange ErrorRange, NoteRange; 11055 // If clause is read: 11056 // v = x; 11057 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 11058 const auto *AtomicBinOp = 11059 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 11060 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 11061 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); 11062 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts(); 11063 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && 11064 (V->isInstantiationDependent() || V->getType()->isScalarType())) { 11065 if (!X->isLValue() || !V->isLValue()) { 11066 const Expr *NotLValueExpr = X->isLValue() ? V : X; 11067 ErrorFound = NotAnLValue; 11068 ErrorLoc = AtomicBinOp->getExprLoc(); 11069 ErrorRange = AtomicBinOp->getSourceRange(); 11070 NoteLoc = NotLValueExpr->getExprLoc(); 11071 NoteRange = NotLValueExpr->getSourceRange(); 11072 } 11073 } else if (!X->isInstantiationDependent() || 11074 !V->isInstantiationDependent()) { 11075 const Expr *NotScalarExpr = 11076 (X->isInstantiationDependent() || X->getType()->isScalarType()) 11077 ? V 11078 : X; 11079 ErrorFound = NotAScalarType; 11080 ErrorLoc = AtomicBinOp->getExprLoc(); 11081 ErrorRange = AtomicBinOp->getSourceRange(); 11082 NoteLoc = NotScalarExpr->getExprLoc(); 11083 NoteRange = NotScalarExpr->getSourceRange(); 11084 } 11085 } else if (!AtomicBody->isInstantiationDependent()) { 11086 ErrorFound = NotAnAssignmentOp; 11087 ErrorLoc = AtomicBody->getExprLoc(); 11088 ErrorRange = AtomicBody->getSourceRange(); 11089 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 11090 : AtomicBody->getExprLoc(); 11091 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 11092 : AtomicBody->getSourceRange(); 11093 } 11094 } else { 11095 ErrorFound = NotAnExpression; 11096 NoteLoc = ErrorLoc = Body->getBeginLoc(); 11097 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 11098 } 11099 if (ErrorFound != NoError) { 11100 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement) 11101 << ErrorRange; 11102 Diag(NoteLoc, diag::note_omp_atomic_read_write) 11103 << ErrorFound << NoteRange; 11104 return StmtError(); 11105 } 11106 if (CurContext->isDependentContext()) 11107 V = X = nullptr; 11108 } else if (AtomicKind == OMPC_write) { 11109 enum { 11110 NotAnExpression, 11111 NotAnAssignmentOp, 11112 NotAScalarType, 11113 NotAnLValue, 11114 NoError 11115 } ErrorFound = NoError; 11116 SourceLocation ErrorLoc, NoteLoc; 11117 SourceRange ErrorRange, NoteRange; 11118 // If clause is write: 11119 // x = expr; 11120 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 11121 const auto *AtomicBinOp = 11122 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 11123 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 11124 X = AtomicBinOp->getLHS(); 11125 E = AtomicBinOp->getRHS(); 11126 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && 11127 (E->isInstantiationDependent() || E->getType()->isScalarType())) { 11128 if (!X->isLValue()) { 11129 ErrorFound = NotAnLValue; 11130 ErrorLoc = AtomicBinOp->getExprLoc(); 11131 ErrorRange = AtomicBinOp->getSourceRange(); 11132 NoteLoc = X->getExprLoc(); 11133 NoteRange = X->getSourceRange(); 11134 } 11135 } else if (!X->isInstantiationDependent() || 11136 !E->isInstantiationDependent()) { 11137 const Expr *NotScalarExpr = 11138 (X->isInstantiationDependent() || X->getType()->isScalarType()) 11139 ? E 11140 : X; 11141 ErrorFound = NotAScalarType; 11142 ErrorLoc = AtomicBinOp->getExprLoc(); 11143 ErrorRange = AtomicBinOp->getSourceRange(); 11144 NoteLoc = NotScalarExpr->getExprLoc(); 11145 NoteRange = NotScalarExpr->getSourceRange(); 11146 } 11147 } else if (!AtomicBody->isInstantiationDependent()) { 11148 ErrorFound = NotAnAssignmentOp; 11149 ErrorLoc = AtomicBody->getExprLoc(); 11150 ErrorRange = AtomicBody->getSourceRange(); 11151 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 11152 : AtomicBody->getExprLoc(); 11153 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 11154 : AtomicBody->getSourceRange(); 11155 } 11156 } else { 11157 ErrorFound = NotAnExpression; 11158 NoteLoc = ErrorLoc = Body->getBeginLoc(); 11159 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 11160 } 11161 if (ErrorFound != NoError) { 11162 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement) 11163 << ErrorRange; 11164 Diag(NoteLoc, diag::note_omp_atomic_read_write) 11165 << ErrorFound << NoteRange; 11166 return StmtError(); 11167 } 11168 if (CurContext->isDependentContext()) 11169 E = X = nullptr; 11170 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) { 11171 // If clause is update: 11172 // x++; 11173 // x--; 11174 // ++x; 11175 // --x; 11176 // x binop= expr; 11177 // x = x binop expr; 11178 // x = expr binop x; 11179 OpenMPAtomicUpdateChecker Checker(*this); 11180 if (Checker.checkStatement( 11181 Body, 11182 (AtomicKind == OMPC_update) 11183 ? diag::err_omp_atomic_update_not_expression_statement 11184 : diag::err_omp_atomic_not_expression_statement, 11185 diag::note_omp_atomic_update)) 11186 return StmtError(); 11187 if (!CurContext->isDependentContext()) { 11188 E = Checker.getExpr(); 11189 X = Checker.getX(); 11190 UE = Checker.getUpdateExpr(); 11191 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 11192 } 11193 } else if (AtomicKind == OMPC_capture) { 11194 enum { 11195 NotAnAssignmentOp, 11196 NotACompoundStatement, 11197 NotTwoSubstatements, 11198 NotASpecificExpression, 11199 NoError 11200 } ErrorFound = NoError; 11201 SourceLocation ErrorLoc, NoteLoc; 11202 SourceRange ErrorRange, NoteRange; 11203 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 11204 // If clause is a capture: 11205 // v = x++; 11206 // v = x--; 11207 // v = ++x; 11208 // v = --x; 11209 // v = x binop= expr; 11210 // v = x = x binop expr; 11211 // v = x = expr binop x; 11212 const auto *AtomicBinOp = 11213 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 11214 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 11215 V = AtomicBinOp->getLHS(); 11216 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); 11217 OpenMPAtomicUpdateChecker Checker(*this); 11218 if (Checker.checkStatement( 11219 Body, diag::err_omp_atomic_capture_not_expression_statement, 11220 diag::note_omp_atomic_update)) 11221 return StmtError(); 11222 E = Checker.getExpr(); 11223 X = Checker.getX(); 11224 UE = Checker.getUpdateExpr(); 11225 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 11226 IsPostfixUpdate = Checker.isPostfixUpdate(); 11227 } else if (!AtomicBody->isInstantiationDependent()) { 11228 ErrorLoc = AtomicBody->getExprLoc(); 11229 ErrorRange = AtomicBody->getSourceRange(); 11230 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 11231 : AtomicBody->getExprLoc(); 11232 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 11233 : AtomicBody->getSourceRange(); 11234 ErrorFound = NotAnAssignmentOp; 11235 } 11236 if (ErrorFound != NoError) { 11237 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement) 11238 << ErrorRange; 11239 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; 11240 return StmtError(); 11241 } 11242 if (CurContext->isDependentContext()) 11243 UE = V = E = X = nullptr; 11244 } else { 11245 // If clause is a capture: 11246 // { v = x; x = expr; } 11247 // { v = x; x++; } 11248 // { v = x; x--; } 11249 // { v = x; ++x; } 11250 // { v = x; --x; } 11251 // { v = x; x binop= expr; } 11252 // { v = x; x = x binop expr; } 11253 // { v = x; x = expr binop x; } 11254 // { x++; v = x; } 11255 // { x--; v = x; } 11256 // { ++x; v = x; } 11257 // { --x; v = x; } 11258 // { x binop= expr; v = x; } 11259 // { x = x binop expr; v = x; } 11260 // { x = expr binop x; v = x; } 11261 if (auto *CS = dyn_cast<CompoundStmt>(Body)) { 11262 // Check that this is { expr1; expr2; } 11263 if (CS->size() == 2) { 11264 Stmt *First = CS->body_front(); 11265 Stmt *Second = CS->body_back(); 11266 if (auto *EWC = dyn_cast<ExprWithCleanups>(First)) 11267 First = EWC->getSubExpr()->IgnoreParenImpCasts(); 11268 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second)) 11269 Second = EWC->getSubExpr()->IgnoreParenImpCasts(); 11270 // Need to find what subexpression is 'v' and what is 'x'. 11271 OpenMPAtomicUpdateChecker Checker(*this); 11272 bool IsUpdateExprFound = !Checker.checkStatement(Second); 11273 BinaryOperator *BinOp = nullptr; 11274 if (IsUpdateExprFound) { 11275 BinOp = dyn_cast<BinaryOperator>(First); 11276 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; 11277 } 11278 if (IsUpdateExprFound && !CurContext->isDependentContext()) { 11279 // { v = x; x++; } 11280 // { v = x; x--; } 11281 // { v = x; ++x; } 11282 // { v = x; --x; } 11283 // { v = x; x binop= expr; } 11284 // { v = x; x = x binop expr; } 11285 // { v = x; x = expr binop x; } 11286 // Check that the first expression has form v = x. 11287 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); 11288 llvm::FoldingSetNodeID XId, PossibleXId; 11289 Checker.getX()->Profile(XId, Context, /*Canonical=*/true); 11290 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); 11291 IsUpdateExprFound = XId == PossibleXId; 11292 if (IsUpdateExprFound) { 11293 V = BinOp->getLHS(); 11294 X = Checker.getX(); 11295 E = Checker.getExpr(); 11296 UE = Checker.getUpdateExpr(); 11297 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 11298 IsPostfixUpdate = true; 11299 } 11300 } 11301 if (!IsUpdateExprFound) { 11302 IsUpdateExprFound = !Checker.checkStatement(First); 11303 BinOp = nullptr; 11304 if (IsUpdateExprFound) { 11305 BinOp = dyn_cast<BinaryOperator>(Second); 11306 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; 11307 } 11308 if (IsUpdateExprFound && !CurContext->isDependentContext()) { 11309 // { x++; v = x; } 11310 // { x--; v = x; } 11311 // { ++x; v = x; } 11312 // { --x; v = x; } 11313 // { x binop= expr; v = x; } 11314 // { x = x binop expr; v = x; } 11315 // { x = expr binop x; v = x; } 11316 // Check that the second expression has form v = x. 11317 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); 11318 llvm::FoldingSetNodeID XId, PossibleXId; 11319 Checker.getX()->Profile(XId, Context, /*Canonical=*/true); 11320 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); 11321 IsUpdateExprFound = XId == PossibleXId; 11322 if (IsUpdateExprFound) { 11323 V = BinOp->getLHS(); 11324 X = Checker.getX(); 11325 E = Checker.getExpr(); 11326 UE = Checker.getUpdateExpr(); 11327 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 11328 IsPostfixUpdate = false; 11329 } 11330 } 11331 } 11332 if (!IsUpdateExprFound) { 11333 // { v = x; x = expr; } 11334 auto *FirstExpr = dyn_cast<Expr>(First); 11335 auto *SecondExpr = dyn_cast<Expr>(Second); 11336 if (!FirstExpr || !SecondExpr || 11337 !(FirstExpr->isInstantiationDependent() || 11338 SecondExpr->isInstantiationDependent())) { 11339 auto *FirstBinOp = dyn_cast<BinaryOperator>(First); 11340 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) { 11341 ErrorFound = NotAnAssignmentOp; 11342 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc() 11343 : First->getBeginLoc(); 11344 NoteRange = ErrorRange = FirstBinOp 11345 ? FirstBinOp->getSourceRange() 11346 : SourceRange(ErrorLoc, ErrorLoc); 11347 } else { 11348 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second); 11349 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) { 11350 ErrorFound = NotAnAssignmentOp; 11351 NoteLoc = ErrorLoc = SecondBinOp 11352 ? SecondBinOp->getOperatorLoc() 11353 : Second->getBeginLoc(); 11354 NoteRange = ErrorRange = 11355 SecondBinOp ? SecondBinOp->getSourceRange() 11356 : SourceRange(ErrorLoc, ErrorLoc); 11357 } else { 11358 Expr *PossibleXRHSInFirst = 11359 FirstBinOp->getRHS()->IgnoreParenImpCasts(); 11360 Expr *PossibleXLHSInSecond = 11361 SecondBinOp->getLHS()->IgnoreParenImpCasts(); 11362 llvm::FoldingSetNodeID X1Id, X2Id; 11363 PossibleXRHSInFirst->Profile(X1Id, Context, 11364 /*Canonical=*/true); 11365 PossibleXLHSInSecond->Profile(X2Id, Context, 11366 /*Canonical=*/true); 11367 IsUpdateExprFound = X1Id == X2Id; 11368 if (IsUpdateExprFound) { 11369 V = FirstBinOp->getLHS(); 11370 X = SecondBinOp->getLHS(); 11371 E = SecondBinOp->getRHS(); 11372 UE = nullptr; 11373 IsXLHSInRHSPart = false; 11374 IsPostfixUpdate = true; 11375 } else { 11376 ErrorFound = NotASpecificExpression; 11377 ErrorLoc = FirstBinOp->getExprLoc(); 11378 ErrorRange = FirstBinOp->getSourceRange(); 11379 NoteLoc = SecondBinOp->getLHS()->getExprLoc(); 11380 NoteRange = SecondBinOp->getRHS()->getSourceRange(); 11381 } 11382 } 11383 } 11384 } 11385 } 11386 } else { 11387 NoteLoc = ErrorLoc = Body->getBeginLoc(); 11388 NoteRange = ErrorRange = 11389 SourceRange(Body->getBeginLoc(), Body->getBeginLoc()); 11390 ErrorFound = NotTwoSubstatements; 11391 } 11392 } else { 11393 NoteLoc = ErrorLoc = Body->getBeginLoc(); 11394 NoteRange = ErrorRange = 11395 SourceRange(Body->getBeginLoc(), Body->getBeginLoc()); 11396 ErrorFound = NotACompoundStatement; 11397 } 11398 } 11399 if (ErrorFound != NoError) { 11400 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement) 11401 << ErrorRange; 11402 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; 11403 return StmtError(); 11404 } 11405 if (CurContext->isDependentContext()) 11406 UE = V = E = X = nullptr; 11407 } else if (AtomicKind == OMPC_compare) { 11408 // TODO: For now we emit an error here and in emitOMPAtomicExpr we ignore 11409 // code gen. 11410 unsigned DiagID = Diags.getCustomDiagID( 11411 DiagnosticsEngine::Error, "atomic compare is not supported for now"); 11412 Diag(AtomicKindLoc, DiagID); 11413 } 11414 11415 setFunctionHasBranchProtectedScope(); 11416 11417 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 11418 X, V, E, UE, IsXLHSInRHSPart, 11419 IsPostfixUpdate); 11420 } 11421 11422 StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses, 11423 Stmt *AStmt, 11424 SourceLocation StartLoc, 11425 SourceLocation EndLoc) { 11426 if (!AStmt) 11427 return StmtError(); 11428 11429 auto *CS = cast<CapturedStmt>(AStmt); 11430 // 1.2.2 OpenMP Language Terminology 11431 // Structured block - An executable statement with a single entry at the 11432 // top and a single exit at the bottom. 11433 // The point of exit cannot be a branch out of the structured block. 11434 // longjmp() and throw() must not violate the entry/exit criteria. 11435 CS->getCapturedDecl()->setNothrow(); 11436 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target); 11437 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11438 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11439 // 1.2.2 OpenMP Language Terminology 11440 // Structured block - An executable statement with a single entry at the 11441 // top and a single exit at the bottom. 11442 // The point of exit cannot be a branch out of the structured block. 11443 // longjmp() and throw() must not violate the entry/exit criteria. 11444 CS->getCapturedDecl()->setNothrow(); 11445 } 11446 11447 // OpenMP [2.16, Nesting of Regions] 11448 // If specified, a teams construct must be contained within a target 11449 // construct. That target construct must contain no statements or directives 11450 // outside of the teams construct. 11451 if (DSAStack->hasInnerTeamsRegion()) { 11452 const Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true); 11453 bool OMPTeamsFound = true; 11454 if (const auto *CS = dyn_cast<CompoundStmt>(S)) { 11455 auto I = CS->body_begin(); 11456 while (I != CS->body_end()) { 11457 const auto *OED = dyn_cast<OMPExecutableDirective>(*I); 11458 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind()) || 11459 OMPTeamsFound) { 11460 11461 OMPTeamsFound = false; 11462 break; 11463 } 11464 ++I; 11465 } 11466 assert(I != CS->body_end() && "Not found statement"); 11467 S = *I; 11468 } else { 11469 const auto *OED = dyn_cast<OMPExecutableDirective>(S); 11470 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind()); 11471 } 11472 if (!OMPTeamsFound) { 11473 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams); 11474 Diag(DSAStack->getInnerTeamsRegionLoc(), 11475 diag::note_omp_nested_teams_construct_here); 11476 Diag(S->getBeginLoc(), diag::note_omp_nested_statement_here) 11477 << isa<OMPExecutableDirective>(S); 11478 return StmtError(); 11479 } 11480 } 11481 11482 setFunctionHasBranchProtectedScope(); 11483 11484 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 11485 } 11486 11487 StmtResult 11488 Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses, 11489 Stmt *AStmt, SourceLocation StartLoc, 11490 SourceLocation EndLoc) { 11491 if (!AStmt) 11492 return StmtError(); 11493 11494 auto *CS = cast<CapturedStmt>(AStmt); 11495 // 1.2.2 OpenMP Language Terminology 11496 // Structured block - An executable statement with a single entry at the 11497 // top and a single exit at the bottom. 11498 // The point of exit cannot be a branch out of the structured block. 11499 // longjmp() and throw() must not violate the entry/exit criteria. 11500 CS->getCapturedDecl()->setNothrow(); 11501 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel); 11502 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11503 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11504 // 1.2.2 OpenMP Language Terminology 11505 // Structured block - An executable statement with a single entry at the 11506 // top and a single exit at the bottom. 11507 // The point of exit cannot be a branch out of the structured block. 11508 // longjmp() and throw() must not violate the entry/exit criteria. 11509 CS->getCapturedDecl()->setNothrow(); 11510 } 11511 11512 setFunctionHasBranchProtectedScope(); 11513 11514 return OMPTargetParallelDirective::Create( 11515 Context, StartLoc, EndLoc, Clauses, AStmt, 11516 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 11517 } 11518 11519 StmtResult Sema::ActOnOpenMPTargetParallelForDirective( 11520 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11521 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11522 if (!AStmt) 11523 return StmtError(); 11524 11525 auto *CS = cast<CapturedStmt>(AStmt); 11526 // 1.2.2 OpenMP Language Terminology 11527 // Structured block - An executable statement with a single entry at the 11528 // top and a single exit at the bottom. 11529 // The point of exit cannot be a branch out of the structured block. 11530 // longjmp() and throw() must not violate the entry/exit criteria. 11531 CS->getCapturedDecl()->setNothrow(); 11532 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for); 11533 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11534 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11535 // 1.2.2 OpenMP Language Terminology 11536 // Structured block - An executable statement with a single entry at the 11537 // top and a single exit at the bottom. 11538 // The point of exit cannot be a branch out of the structured block. 11539 // longjmp() and throw() must not violate the entry/exit criteria. 11540 CS->getCapturedDecl()->setNothrow(); 11541 } 11542 11543 OMPLoopBasedDirective::HelperExprs B; 11544 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11545 // define the nested loops number. 11546 unsigned NestedLoopCount = 11547 checkOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses), 11548 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 11549 VarsWithImplicitDSA, B); 11550 if (NestedLoopCount == 0) 11551 return StmtError(); 11552 11553 assert((CurContext->isDependentContext() || B.builtAll()) && 11554 "omp target parallel for loop exprs were not built"); 11555 11556 if (!CurContext->isDependentContext()) { 11557 // Finalize the clauses that need pre-built expressions for CodeGen. 11558 for (OMPClause *C : Clauses) { 11559 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11560 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11561 B.NumIterations, *this, CurScope, 11562 DSAStack)) 11563 return StmtError(); 11564 } 11565 } 11566 11567 setFunctionHasBranchProtectedScope(); 11568 return OMPTargetParallelForDirective::Create( 11569 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 11570 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 11571 } 11572 11573 /// Check for existence of a map clause in the list of clauses. 11574 static bool hasClauses(ArrayRef<OMPClause *> Clauses, 11575 const OpenMPClauseKind K) { 11576 return llvm::any_of( 11577 Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; }); 11578 } 11579 11580 template <typename... Params> 11581 static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K, 11582 const Params... ClauseTypes) { 11583 return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...); 11584 } 11585 11586 StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses, 11587 Stmt *AStmt, 11588 SourceLocation StartLoc, 11589 SourceLocation EndLoc) { 11590 if (!AStmt) 11591 return StmtError(); 11592 11593 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11594 11595 // OpenMP [2.12.2, target data Construct, Restrictions] 11596 // At least one map, use_device_addr or use_device_ptr clause must appear on 11597 // the directive. 11598 if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr) && 11599 (LangOpts.OpenMP < 50 || !hasClauses(Clauses, OMPC_use_device_addr))) { 11600 StringRef Expected; 11601 if (LangOpts.OpenMP < 50) 11602 Expected = "'map' or 'use_device_ptr'"; 11603 else 11604 Expected = "'map', 'use_device_ptr', or 'use_device_addr'"; 11605 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 11606 << Expected << getOpenMPDirectiveName(OMPD_target_data); 11607 return StmtError(); 11608 } 11609 11610 setFunctionHasBranchProtectedScope(); 11611 11612 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 11613 AStmt); 11614 } 11615 11616 StmtResult 11617 Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses, 11618 SourceLocation StartLoc, 11619 SourceLocation EndLoc, Stmt *AStmt) { 11620 if (!AStmt) 11621 return StmtError(); 11622 11623 auto *CS = cast<CapturedStmt>(AStmt); 11624 // 1.2.2 OpenMP Language Terminology 11625 // Structured block - An executable statement with a single entry at the 11626 // top and a single exit at the bottom. 11627 // The point of exit cannot be a branch out of the structured block. 11628 // longjmp() and throw() must not violate the entry/exit criteria. 11629 CS->getCapturedDecl()->setNothrow(); 11630 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_enter_data); 11631 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11632 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11633 // 1.2.2 OpenMP Language Terminology 11634 // Structured block - An executable statement with a single entry at the 11635 // top and a single exit at the bottom. 11636 // The point of exit cannot be a branch out of the structured block. 11637 // longjmp() and throw() must not violate the entry/exit criteria. 11638 CS->getCapturedDecl()->setNothrow(); 11639 } 11640 11641 // OpenMP [2.10.2, Restrictions, p. 99] 11642 // At least one map clause must appear on the directive. 11643 if (!hasClauses(Clauses, OMPC_map)) { 11644 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 11645 << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data); 11646 return StmtError(); 11647 } 11648 11649 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 11650 AStmt); 11651 } 11652 11653 StmtResult 11654 Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses, 11655 SourceLocation StartLoc, 11656 SourceLocation EndLoc, Stmt *AStmt) { 11657 if (!AStmt) 11658 return StmtError(); 11659 11660 auto *CS = cast<CapturedStmt>(AStmt); 11661 // 1.2.2 OpenMP Language Terminology 11662 // Structured block - An executable statement with a single entry at the 11663 // top and a single exit at the bottom. 11664 // The point of exit cannot be a branch out of the structured block. 11665 // longjmp() and throw() must not violate the entry/exit criteria. 11666 CS->getCapturedDecl()->setNothrow(); 11667 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_exit_data); 11668 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11669 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11670 // 1.2.2 OpenMP Language Terminology 11671 // Structured block - An executable statement with a single entry at the 11672 // top and a single exit at the bottom. 11673 // The point of exit cannot be a branch out of the structured block. 11674 // longjmp() and throw() must not violate the entry/exit criteria. 11675 CS->getCapturedDecl()->setNothrow(); 11676 } 11677 11678 // OpenMP [2.10.3, Restrictions, p. 102] 11679 // At least one map clause must appear on the directive. 11680 if (!hasClauses(Clauses, OMPC_map)) { 11681 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 11682 << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data); 11683 return StmtError(); 11684 } 11685 11686 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 11687 AStmt); 11688 } 11689 11690 StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses, 11691 SourceLocation StartLoc, 11692 SourceLocation EndLoc, 11693 Stmt *AStmt) { 11694 if (!AStmt) 11695 return StmtError(); 11696 11697 auto *CS = cast<CapturedStmt>(AStmt); 11698 // 1.2.2 OpenMP Language Terminology 11699 // Structured block - An executable statement with a single entry at the 11700 // top and a single exit at the bottom. 11701 // The point of exit cannot be a branch out of the structured block. 11702 // longjmp() and throw() must not violate the entry/exit criteria. 11703 CS->getCapturedDecl()->setNothrow(); 11704 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_update); 11705 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11706 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11707 // 1.2.2 OpenMP Language Terminology 11708 // Structured block - An executable statement with a single entry at the 11709 // top and a single exit at the bottom. 11710 // The point of exit cannot be a branch out of the structured block. 11711 // longjmp() and throw() must not violate the entry/exit criteria. 11712 CS->getCapturedDecl()->setNothrow(); 11713 } 11714 11715 if (!hasClauses(Clauses, OMPC_to, OMPC_from)) { 11716 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required); 11717 return StmtError(); 11718 } 11719 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses, 11720 AStmt); 11721 } 11722 11723 StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses, 11724 Stmt *AStmt, SourceLocation StartLoc, 11725 SourceLocation EndLoc) { 11726 if (!AStmt) 11727 return StmtError(); 11728 11729 auto *CS = cast<CapturedStmt>(AStmt); 11730 // 1.2.2 OpenMP Language Terminology 11731 // Structured block - An executable statement with a single entry at the 11732 // top and a single exit at the bottom. 11733 // The point of exit cannot be a branch out of the structured block. 11734 // longjmp() and throw() must not violate the entry/exit criteria. 11735 CS->getCapturedDecl()->setNothrow(); 11736 11737 setFunctionHasBranchProtectedScope(); 11738 11739 DSAStack->setParentTeamsRegionLoc(StartLoc); 11740 11741 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 11742 } 11743 11744 StmtResult 11745 Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc, 11746 SourceLocation EndLoc, 11747 OpenMPDirectiveKind CancelRegion) { 11748 if (DSAStack->isParentNowaitRegion()) { 11749 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0; 11750 return StmtError(); 11751 } 11752 if (DSAStack->isParentOrderedRegion()) { 11753 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0; 11754 return StmtError(); 11755 } 11756 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc, 11757 CancelRegion); 11758 } 11759 11760 StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses, 11761 SourceLocation StartLoc, 11762 SourceLocation EndLoc, 11763 OpenMPDirectiveKind CancelRegion) { 11764 if (DSAStack->isParentNowaitRegion()) { 11765 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1; 11766 return StmtError(); 11767 } 11768 if (DSAStack->isParentOrderedRegion()) { 11769 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1; 11770 return StmtError(); 11771 } 11772 DSAStack->setParentCancelRegion(/*Cancel=*/true); 11773 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses, 11774 CancelRegion); 11775 } 11776 11777 static bool checkReductionClauseWithNogroup(Sema &S, 11778 ArrayRef<OMPClause *> Clauses) { 11779 const OMPClause *ReductionClause = nullptr; 11780 const OMPClause *NogroupClause = nullptr; 11781 for (const OMPClause *C : Clauses) { 11782 if (C->getClauseKind() == OMPC_reduction) { 11783 ReductionClause = C; 11784 if (NogroupClause) 11785 break; 11786 continue; 11787 } 11788 if (C->getClauseKind() == OMPC_nogroup) { 11789 NogroupClause = C; 11790 if (ReductionClause) 11791 break; 11792 continue; 11793 } 11794 } 11795 if (ReductionClause && NogroupClause) { 11796 S.Diag(ReductionClause->getBeginLoc(), diag::err_omp_reduction_with_nogroup) 11797 << SourceRange(NogroupClause->getBeginLoc(), 11798 NogroupClause->getEndLoc()); 11799 return true; 11800 } 11801 return false; 11802 } 11803 11804 StmtResult Sema::ActOnOpenMPTaskLoopDirective( 11805 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11806 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11807 if (!AStmt) 11808 return StmtError(); 11809 11810 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11811 OMPLoopBasedDirective::HelperExprs B; 11812 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11813 // define the nested loops number. 11814 unsigned NestedLoopCount = 11815 checkOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses), 11816 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 11817 VarsWithImplicitDSA, B); 11818 if (NestedLoopCount == 0) 11819 return StmtError(); 11820 11821 assert((CurContext->isDependentContext() || B.builtAll()) && 11822 "omp for loop exprs were not built"); 11823 11824 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11825 // The grainsize clause and num_tasks clause are mutually exclusive and may 11826 // not appear on the same taskloop directive. 11827 if (checkMutuallyExclusiveClauses(*this, Clauses, 11828 {OMPC_grainsize, OMPC_num_tasks})) 11829 return StmtError(); 11830 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11831 // If a reduction clause is present on the taskloop directive, the nogroup 11832 // clause must not be specified. 11833 if (checkReductionClauseWithNogroup(*this, Clauses)) 11834 return StmtError(); 11835 11836 setFunctionHasBranchProtectedScope(); 11837 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc, 11838 NestedLoopCount, Clauses, AStmt, B, 11839 DSAStack->isCancelRegion()); 11840 } 11841 11842 StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective( 11843 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11844 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11845 if (!AStmt) 11846 return StmtError(); 11847 11848 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11849 OMPLoopBasedDirective::HelperExprs B; 11850 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11851 // define the nested loops number. 11852 unsigned NestedLoopCount = 11853 checkOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses), 11854 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 11855 VarsWithImplicitDSA, B); 11856 if (NestedLoopCount == 0) 11857 return StmtError(); 11858 11859 assert((CurContext->isDependentContext() || B.builtAll()) && 11860 "omp for loop exprs were not built"); 11861 11862 if (!CurContext->isDependentContext()) { 11863 // Finalize the clauses that need pre-built expressions for CodeGen. 11864 for (OMPClause *C : Clauses) { 11865 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11866 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11867 B.NumIterations, *this, CurScope, 11868 DSAStack)) 11869 return StmtError(); 11870 } 11871 } 11872 11873 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11874 // The grainsize clause and num_tasks clause are mutually exclusive and may 11875 // not appear on the same taskloop directive. 11876 if (checkMutuallyExclusiveClauses(*this, Clauses, 11877 {OMPC_grainsize, OMPC_num_tasks})) 11878 return StmtError(); 11879 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11880 // If a reduction clause is present on the taskloop directive, the nogroup 11881 // clause must not be specified. 11882 if (checkReductionClauseWithNogroup(*this, Clauses)) 11883 return StmtError(); 11884 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11885 return StmtError(); 11886 11887 setFunctionHasBranchProtectedScope(); 11888 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc, 11889 NestedLoopCount, Clauses, AStmt, B); 11890 } 11891 11892 StmtResult Sema::ActOnOpenMPMasterTaskLoopDirective( 11893 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11894 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11895 if (!AStmt) 11896 return StmtError(); 11897 11898 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11899 OMPLoopBasedDirective::HelperExprs B; 11900 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11901 // define the nested loops number. 11902 unsigned NestedLoopCount = 11903 checkOpenMPLoop(OMPD_master_taskloop, getCollapseNumberExpr(Clauses), 11904 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 11905 VarsWithImplicitDSA, B); 11906 if (NestedLoopCount == 0) 11907 return StmtError(); 11908 11909 assert((CurContext->isDependentContext() || B.builtAll()) && 11910 "omp for loop exprs were not built"); 11911 11912 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11913 // The grainsize clause and num_tasks clause are mutually exclusive and may 11914 // not appear on the same taskloop directive. 11915 if (checkMutuallyExclusiveClauses(*this, Clauses, 11916 {OMPC_grainsize, OMPC_num_tasks})) 11917 return StmtError(); 11918 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11919 // If a reduction clause is present on the taskloop directive, the nogroup 11920 // clause must not be specified. 11921 if (checkReductionClauseWithNogroup(*this, Clauses)) 11922 return StmtError(); 11923 11924 setFunctionHasBranchProtectedScope(); 11925 return OMPMasterTaskLoopDirective::Create(Context, StartLoc, EndLoc, 11926 NestedLoopCount, Clauses, AStmt, B, 11927 DSAStack->isCancelRegion()); 11928 } 11929 11930 StmtResult Sema::ActOnOpenMPMasterTaskLoopSimdDirective( 11931 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11932 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11933 if (!AStmt) 11934 return StmtError(); 11935 11936 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11937 OMPLoopBasedDirective::HelperExprs B; 11938 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11939 // define the nested loops number. 11940 unsigned NestedLoopCount = 11941 checkOpenMPLoop(OMPD_master_taskloop_simd, getCollapseNumberExpr(Clauses), 11942 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 11943 VarsWithImplicitDSA, B); 11944 if (NestedLoopCount == 0) 11945 return StmtError(); 11946 11947 assert((CurContext->isDependentContext() || B.builtAll()) && 11948 "omp for loop exprs were not built"); 11949 11950 if (!CurContext->isDependentContext()) { 11951 // Finalize the clauses that need pre-built expressions for CodeGen. 11952 for (OMPClause *C : Clauses) { 11953 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11954 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11955 B.NumIterations, *this, CurScope, 11956 DSAStack)) 11957 return StmtError(); 11958 } 11959 } 11960 11961 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11962 // The grainsize clause and num_tasks clause are mutually exclusive and may 11963 // not appear on the same taskloop directive. 11964 if (checkMutuallyExclusiveClauses(*this, Clauses, 11965 {OMPC_grainsize, OMPC_num_tasks})) 11966 return StmtError(); 11967 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11968 // If a reduction clause is present on the taskloop directive, the nogroup 11969 // clause must not be specified. 11970 if (checkReductionClauseWithNogroup(*this, Clauses)) 11971 return StmtError(); 11972 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11973 return StmtError(); 11974 11975 setFunctionHasBranchProtectedScope(); 11976 return OMPMasterTaskLoopSimdDirective::Create( 11977 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 11978 } 11979 11980 StmtResult Sema::ActOnOpenMPParallelMasterTaskLoopDirective( 11981 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11982 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11983 if (!AStmt) 11984 return StmtError(); 11985 11986 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11987 auto *CS = cast<CapturedStmt>(AStmt); 11988 // 1.2.2 OpenMP Language Terminology 11989 // Structured block - An executable statement with a single entry at the 11990 // top and a single exit at the bottom. 11991 // The point of exit cannot be a branch out of the structured block. 11992 // longjmp() and throw() must not violate the entry/exit criteria. 11993 CS->getCapturedDecl()->setNothrow(); 11994 for (int ThisCaptureLevel = 11995 getOpenMPCaptureLevels(OMPD_parallel_master_taskloop); 11996 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11997 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11998 // 1.2.2 OpenMP Language Terminology 11999 // Structured block - An executable statement with a single entry at the 12000 // top and a single exit at the bottom. 12001 // The point of exit cannot be a branch out of the structured block. 12002 // longjmp() and throw() must not violate the entry/exit criteria. 12003 CS->getCapturedDecl()->setNothrow(); 12004 } 12005 12006 OMPLoopBasedDirective::HelperExprs B; 12007 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 12008 // define the nested loops number. 12009 unsigned NestedLoopCount = checkOpenMPLoop( 12010 OMPD_parallel_master_taskloop, getCollapseNumberExpr(Clauses), 12011 /*OrderedLoopCountExpr=*/nullptr, CS, *this, *DSAStack, 12012 VarsWithImplicitDSA, B); 12013 if (NestedLoopCount == 0) 12014 return StmtError(); 12015 12016 assert((CurContext->isDependentContext() || B.builtAll()) && 12017 "omp for loop exprs were not built"); 12018 12019 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 12020 // The grainsize clause and num_tasks clause are mutually exclusive and may 12021 // not appear on the same taskloop directive. 12022 if (checkMutuallyExclusiveClauses(*this, Clauses, 12023 {OMPC_grainsize, OMPC_num_tasks})) 12024 return StmtError(); 12025 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 12026 // If a reduction clause is present on the taskloop directive, the nogroup 12027 // clause must not be specified. 12028 if (checkReductionClauseWithNogroup(*this, Clauses)) 12029 return StmtError(); 12030 12031 setFunctionHasBranchProtectedScope(); 12032 return OMPParallelMasterTaskLoopDirective::Create( 12033 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 12034 DSAStack->isCancelRegion()); 12035 } 12036 12037 StmtResult Sema::ActOnOpenMPParallelMasterTaskLoopSimdDirective( 12038 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12039 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12040 if (!AStmt) 12041 return StmtError(); 12042 12043 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 12044 auto *CS = cast<CapturedStmt>(AStmt); 12045 // 1.2.2 OpenMP Language Terminology 12046 // Structured block - An executable statement with a single entry at the 12047 // top and a single exit at the bottom. 12048 // The point of exit cannot be a branch out of the structured block. 12049 // longjmp() and throw() must not violate the entry/exit criteria. 12050 CS->getCapturedDecl()->setNothrow(); 12051 for (int ThisCaptureLevel = 12052 getOpenMPCaptureLevels(OMPD_parallel_master_taskloop_simd); 12053 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12054 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12055 // 1.2.2 OpenMP Language Terminology 12056 // Structured block - An executable statement with a single entry at the 12057 // top and a single exit at the bottom. 12058 // The point of exit cannot be a branch out of the structured block. 12059 // longjmp() and throw() must not violate the entry/exit criteria. 12060 CS->getCapturedDecl()->setNothrow(); 12061 } 12062 12063 OMPLoopBasedDirective::HelperExprs B; 12064 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 12065 // define the nested loops number. 12066 unsigned NestedLoopCount = checkOpenMPLoop( 12067 OMPD_parallel_master_taskloop_simd, getCollapseNumberExpr(Clauses), 12068 /*OrderedLoopCountExpr=*/nullptr, CS, *this, *DSAStack, 12069 VarsWithImplicitDSA, B); 12070 if (NestedLoopCount == 0) 12071 return StmtError(); 12072 12073 assert((CurContext->isDependentContext() || B.builtAll()) && 12074 "omp for loop exprs were not built"); 12075 12076 if (!CurContext->isDependentContext()) { 12077 // Finalize the clauses that need pre-built expressions for CodeGen. 12078 for (OMPClause *C : Clauses) { 12079 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12080 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12081 B.NumIterations, *this, CurScope, 12082 DSAStack)) 12083 return StmtError(); 12084 } 12085 } 12086 12087 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 12088 // The grainsize clause and num_tasks clause are mutually exclusive and may 12089 // not appear on the same taskloop directive. 12090 if (checkMutuallyExclusiveClauses(*this, Clauses, 12091 {OMPC_grainsize, OMPC_num_tasks})) 12092 return StmtError(); 12093 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 12094 // If a reduction clause is present on the taskloop directive, the nogroup 12095 // clause must not be specified. 12096 if (checkReductionClauseWithNogroup(*this, Clauses)) 12097 return StmtError(); 12098 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12099 return StmtError(); 12100 12101 setFunctionHasBranchProtectedScope(); 12102 return OMPParallelMasterTaskLoopSimdDirective::Create( 12103 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12104 } 12105 12106 StmtResult Sema::ActOnOpenMPDistributeDirective( 12107 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12108 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12109 if (!AStmt) 12110 return StmtError(); 12111 12112 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 12113 OMPLoopBasedDirective::HelperExprs B; 12114 // In presence of clause 'collapse' with number of loops, it will 12115 // define the nested loops number. 12116 unsigned NestedLoopCount = 12117 checkOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses), 12118 nullptr /*ordered not a clause on distribute*/, AStmt, 12119 *this, *DSAStack, VarsWithImplicitDSA, B); 12120 if (NestedLoopCount == 0) 12121 return StmtError(); 12122 12123 assert((CurContext->isDependentContext() || B.builtAll()) && 12124 "omp for loop exprs were not built"); 12125 12126 setFunctionHasBranchProtectedScope(); 12127 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc, 12128 NestedLoopCount, Clauses, AStmt, B); 12129 } 12130 12131 StmtResult Sema::ActOnOpenMPDistributeParallelForDirective( 12132 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12133 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12134 if (!AStmt) 12135 return StmtError(); 12136 12137 auto *CS = cast<CapturedStmt>(AStmt); 12138 // 1.2.2 OpenMP Language Terminology 12139 // Structured block - An executable statement with a single entry at the 12140 // top and a single exit at the bottom. 12141 // The point of exit cannot be a branch out of the structured block. 12142 // longjmp() and throw() must not violate the entry/exit criteria. 12143 CS->getCapturedDecl()->setNothrow(); 12144 for (int ThisCaptureLevel = 12145 getOpenMPCaptureLevels(OMPD_distribute_parallel_for); 12146 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12147 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12148 // 1.2.2 OpenMP Language Terminology 12149 // Structured block - An executable statement with a single entry at the 12150 // top and a single exit at the bottom. 12151 // The point of exit cannot be a branch out of the structured block. 12152 // longjmp() and throw() must not violate the entry/exit criteria. 12153 CS->getCapturedDecl()->setNothrow(); 12154 } 12155 12156 OMPLoopBasedDirective::HelperExprs B; 12157 // In presence of clause 'collapse' with number of loops, it will 12158 // define the nested loops number. 12159 unsigned NestedLoopCount = checkOpenMPLoop( 12160 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses), 12161 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 12162 VarsWithImplicitDSA, B); 12163 if (NestedLoopCount == 0) 12164 return StmtError(); 12165 12166 assert((CurContext->isDependentContext() || B.builtAll()) && 12167 "omp for loop exprs were not built"); 12168 12169 setFunctionHasBranchProtectedScope(); 12170 return OMPDistributeParallelForDirective::Create( 12171 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 12172 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 12173 } 12174 12175 StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective( 12176 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12177 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12178 if (!AStmt) 12179 return StmtError(); 12180 12181 auto *CS = cast<CapturedStmt>(AStmt); 12182 // 1.2.2 OpenMP Language Terminology 12183 // Structured block - An executable statement with a single entry at the 12184 // top and a single exit at the bottom. 12185 // The point of exit cannot be a branch out of the structured block. 12186 // longjmp() and throw() must not violate the entry/exit criteria. 12187 CS->getCapturedDecl()->setNothrow(); 12188 for (int ThisCaptureLevel = 12189 getOpenMPCaptureLevels(OMPD_distribute_parallel_for_simd); 12190 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12191 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12192 // 1.2.2 OpenMP Language Terminology 12193 // Structured block - An executable statement with a single entry at the 12194 // top and a single exit at the bottom. 12195 // The point of exit cannot be a branch out of the structured block. 12196 // longjmp() and throw() must not violate the entry/exit criteria. 12197 CS->getCapturedDecl()->setNothrow(); 12198 } 12199 12200 OMPLoopBasedDirective::HelperExprs B; 12201 // In presence of clause 'collapse' with number of loops, it will 12202 // define the nested loops number. 12203 unsigned NestedLoopCount = checkOpenMPLoop( 12204 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses), 12205 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 12206 VarsWithImplicitDSA, B); 12207 if (NestedLoopCount == 0) 12208 return StmtError(); 12209 12210 assert((CurContext->isDependentContext() || B.builtAll()) && 12211 "omp for loop exprs were not built"); 12212 12213 if (!CurContext->isDependentContext()) { 12214 // Finalize the clauses that need pre-built expressions for CodeGen. 12215 for (OMPClause *C : Clauses) { 12216 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12217 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12218 B.NumIterations, *this, CurScope, 12219 DSAStack)) 12220 return StmtError(); 12221 } 12222 } 12223 12224 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12225 return StmtError(); 12226 12227 setFunctionHasBranchProtectedScope(); 12228 return OMPDistributeParallelForSimdDirective::Create( 12229 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12230 } 12231 12232 StmtResult Sema::ActOnOpenMPDistributeSimdDirective( 12233 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12234 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12235 if (!AStmt) 12236 return StmtError(); 12237 12238 auto *CS = cast<CapturedStmt>(AStmt); 12239 // 1.2.2 OpenMP Language Terminology 12240 // Structured block - An executable statement with a single entry at the 12241 // top and a single exit at the bottom. 12242 // The point of exit cannot be a branch out of the structured block. 12243 // longjmp() and throw() must not violate the entry/exit criteria. 12244 CS->getCapturedDecl()->setNothrow(); 12245 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_distribute_simd); 12246 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12247 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12248 // 1.2.2 OpenMP Language Terminology 12249 // Structured block - An executable statement with a single entry at the 12250 // top and a single exit at the bottom. 12251 // The point of exit cannot be a branch out of the structured block. 12252 // longjmp() and throw() must not violate the entry/exit criteria. 12253 CS->getCapturedDecl()->setNothrow(); 12254 } 12255 12256 OMPLoopBasedDirective::HelperExprs B; 12257 // In presence of clause 'collapse' with number of loops, it will 12258 // define the nested loops number. 12259 unsigned NestedLoopCount = 12260 checkOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses), 12261 nullptr /*ordered not a clause on distribute*/, CS, *this, 12262 *DSAStack, VarsWithImplicitDSA, B); 12263 if (NestedLoopCount == 0) 12264 return StmtError(); 12265 12266 assert((CurContext->isDependentContext() || B.builtAll()) && 12267 "omp for loop exprs were not built"); 12268 12269 if (!CurContext->isDependentContext()) { 12270 // Finalize the clauses that need pre-built expressions for CodeGen. 12271 for (OMPClause *C : Clauses) { 12272 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12273 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12274 B.NumIterations, *this, CurScope, 12275 DSAStack)) 12276 return StmtError(); 12277 } 12278 } 12279 12280 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12281 return StmtError(); 12282 12283 setFunctionHasBranchProtectedScope(); 12284 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc, 12285 NestedLoopCount, Clauses, AStmt, B); 12286 } 12287 12288 StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective( 12289 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12290 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12291 if (!AStmt) 12292 return StmtError(); 12293 12294 auto *CS = cast<CapturedStmt>(AStmt); 12295 // 1.2.2 OpenMP Language Terminology 12296 // Structured block - An executable statement with a single entry at the 12297 // top and a single exit at the bottom. 12298 // The point of exit cannot be a branch out of the structured block. 12299 // longjmp() and throw() must not violate the entry/exit criteria. 12300 CS->getCapturedDecl()->setNothrow(); 12301 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for); 12302 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12303 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12304 // 1.2.2 OpenMP Language Terminology 12305 // Structured block - An executable statement with a single entry at the 12306 // top and a single exit at the bottom. 12307 // The point of exit cannot be a branch out of the structured block. 12308 // longjmp() and throw() must not violate the entry/exit criteria. 12309 CS->getCapturedDecl()->setNothrow(); 12310 } 12311 12312 OMPLoopBasedDirective::HelperExprs B; 12313 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 12314 // define the nested loops number. 12315 unsigned NestedLoopCount = checkOpenMPLoop( 12316 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses), 12317 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, VarsWithImplicitDSA, 12318 B); 12319 if (NestedLoopCount == 0) 12320 return StmtError(); 12321 12322 assert((CurContext->isDependentContext() || B.builtAll()) && 12323 "omp target parallel for simd loop exprs were not built"); 12324 12325 if (!CurContext->isDependentContext()) { 12326 // Finalize the clauses that need pre-built expressions for CodeGen. 12327 for (OMPClause *C : Clauses) { 12328 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12329 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12330 B.NumIterations, *this, CurScope, 12331 DSAStack)) 12332 return StmtError(); 12333 } 12334 } 12335 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12336 return StmtError(); 12337 12338 setFunctionHasBranchProtectedScope(); 12339 return OMPTargetParallelForSimdDirective::Create( 12340 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12341 } 12342 12343 StmtResult Sema::ActOnOpenMPTargetSimdDirective( 12344 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12345 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12346 if (!AStmt) 12347 return StmtError(); 12348 12349 auto *CS = cast<CapturedStmt>(AStmt); 12350 // 1.2.2 OpenMP Language Terminology 12351 // Structured block - An executable statement with a single entry at the 12352 // top and a single exit at the bottom. 12353 // The point of exit cannot be a branch out of the structured block. 12354 // longjmp() and throw() must not violate the entry/exit criteria. 12355 CS->getCapturedDecl()->setNothrow(); 12356 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_simd); 12357 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12358 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12359 // 1.2.2 OpenMP Language Terminology 12360 // Structured block - An executable statement with a single entry at the 12361 // top and a single exit at the bottom. 12362 // The point of exit cannot be a branch out of the structured block. 12363 // longjmp() and throw() must not violate the entry/exit criteria. 12364 CS->getCapturedDecl()->setNothrow(); 12365 } 12366 12367 OMPLoopBasedDirective::HelperExprs B; 12368 // In presence of clause 'collapse' with number of loops, it will define the 12369 // nested loops number. 12370 unsigned NestedLoopCount = 12371 checkOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses), 12372 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 12373 VarsWithImplicitDSA, B); 12374 if (NestedLoopCount == 0) 12375 return StmtError(); 12376 12377 assert((CurContext->isDependentContext() || B.builtAll()) && 12378 "omp target simd loop exprs were not built"); 12379 12380 if (!CurContext->isDependentContext()) { 12381 // Finalize the clauses that need pre-built expressions for CodeGen. 12382 for (OMPClause *C : Clauses) { 12383 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12384 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12385 B.NumIterations, *this, CurScope, 12386 DSAStack)) 12387 return StmtError(); 12388 } 12389 } 12390 12391 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12392 return StmtError(); 12393 12394 setFunctionHasBranchProtectedScope(); 12395 return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc, 12396 NestedLoopCount, Clauses, AStmt, B); 12397 } 12398 12399 StmtResult Sema::ActOnOpenMPTeamsDistributeDirective( 12400 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12401 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12402 if (!AStmt) 12403 return StmtError(); 12404 12405 auto *CS = cast<CapturedStmt>(AStmt); 12406 // 1.2.2 OpenMP Language Terminology 12407 // Structured block - An executable statement with a single entry at the 12408 // top and a single exit at the bottom. 12409 // The point of exit cannot be a branch out of the structured block. 12410 // longjmp() and throw() must not violate the entry/exit criteria. 12411 CS->getCapturedDecl()->setNothrow(); 12412 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_teams_distribute); 12413 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12414 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12415 // 1.2.2 OpenMP Language Terminology 12416 // Structured block - An executable statement with a single entry at the 12417 // top and a single exit at the bottom. 12418 // The point of exit cannot be a branch out of the structured block. 12419 // longjmp() and throw() must not violate the entry/exit criteria. 12420 CS->getCapturedDecl()->setNothrow(); 12421 } 12422 12423 OMPLoopBasedDirective::HelperExprs B; 12424 // In presence of clause 'collapse' with number of loops, it will 12425 // define the nested loops number. 12426 unsigned NestedLoopCount = 12427 checkOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses), 12428 nullptr /*ordered not a clause on distribute*/, CS, *this, 12429 *DSAStack, VarsWithImplicitDSA, B); 12430 if (NestedLoopCount == 0) 12431 return StmtError(); 12432 12433 assert((CurContext->isDependentContext() || B.builtAll()) && 12434 "omp teams distribute loop exprs were not built"); 12435 12436 setFunctionHasBranchProtectedScope(); 12437 12438 DSAStack->setParentTeamsRegionLoc(StartLoc); 12439 12440 return OMPTeamsDistributeDirective::Create( 12441 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12442 } 12443 12444 StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective( 12445 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12446 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12447 if (!AStmt) 12448 return StmtError(); 12449 12450 auto *CS = cast<CapturedStmt>(AStmt); 12451 // 1.2.2 OpenMP Language Terminology 12452 // Structured block - An executable statement with a single entry at the 12453 // top and a single exit at the bottom. 12454 // The point of exit cannot be a branch out of the structured block. 12455 // longjmp() and throw() must not violate the entry/exit criteria. 12456 CS->getCapturedDecl()->setNothrow(); 12457 for (int ThisCaptureLevel = 12458 getOpenMPCaptureLevels(OMPD_teams_distribute_simd); 12459 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12460 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12461 // 1.2.2 OpenMP Language Terminology 12462 // Structured block - An executable statement with a single entry at the 12463 // top and a single exit at the bottom. 12464 // The point of exit cannot be a branch out of the structured block. 12465 // longjmp() and throw() must not violate the entry/exit criteria. 12466 CS->getCapturedDecl()->setNothrow(); 12467 } 12468 12469 OMPLoopBasedDirective::HelperExprs B; 12470 // In presence of clause 'collapse' with number of loops, it will 12471 // define the nested loops number. 12472 unsigned NestedLoopCount = checkOpenMPLoop( 12473 OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses), 12474 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 12475 VarsWithImplicitDSA, B); 12476 12477 if (NestedLoopCount == 0) 12478 return StmtError(); 12479 12480 assert((CurContext->isDependentContext() || B.builtAll()) && 12481 "omp teams distribute simd loop exprs were not built"); 12482 12483 if (!CurContext->isDependentContext()) { 12484 // Finalize the clauses that need pre-built expressions for CodeGen. 12485 for (OMPClause *C : Clauses) { 12486 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12487 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12488 B.NumIterations, *this, CurScope, 12489 DSAStack)) 12490 return StmtError(); 12491 } 12492 } 12493 12494 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12495 return StmtError(); 12496 12497 setFunctionHasBranchProtectedScope(); 12498 12499 DSAStack->setParentTeamsRegionLoc(StartLoc); 12500 12501 return OMPTeamsDistributeSimdDirective::Create( 12502 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12503 } 12504 12505 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective( 12506 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12507 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12508 if (!AStmt) 12509 return StmtError(); 12510 12511 auto *CS = cast<CapturedStmt>(AStmt); 12512 // 1.2.2 OpenMP Language Terminology 12513 // Structured block - An executable statement with a single entry at the 12514 // top and a single exit at the bottom. 12515 // The point of exit cannot be a branch out of the structured block. 12516 // longjmp() and throw() must not violate the entry/exit criteria. 12517 CS->getCapturedDecl()->setNothrow(); 12518 12519 for (int ThisCaptureLevel = 12520 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for_simd); 12521 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12522 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12523 // 1.2.2 OpenMP Language Terminology 12524 // Structured block - An executable statement with a single entry at the 12525 // top and a single exit at the bottom. 12526 // The point of exit cannot be a branch out of the structured block. 12527 // longjmp() and throw() must not violate the entry/exit criteria. 12528 CS->getCapturedDecl()->setNothrow(); 12529 } 12530 12531 OMPLoopBasedDirective::HelperExprs B; 12532 // In presence of clause 'collapse' with number of loops, it will 12533 // define the nested loops number. 12534 unsigned NestedLoopCount = checkOpenMPLoop( 12535 OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses), 12536 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 12537 VarsWithImplicitDSA, B); 12538 12539 if (NestedLoopCount == 0) 12540 return StmtError(); 12541 12542 assert((CurContext->isDependentContext() || B.builtAll()) && 12543 "omp for loop exprs were not built"); 12544 12545 if (!CurContext->isDependentContext()) { 12546 // Finalize the clauses that need pre-built expressions for CodeGen. 12547 for (OMPClause *C : Clauses) { 12548 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12549 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12550 B.NumIterations, *this, CurScope, 12551 DSAStack)) 12552 return StmtError(); 12553 } 12554 } 12555 12556 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12557 return StmtError(); 12558 12559 setFunctionHasBranchProtectedScope(); 12560 12561 DSAStack->setParentTeamsRegionLoc(StartLoc); 12562 12563 return OMPTeamsDistributeParallelForSimdDirective::Create( 12564 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12565 } 12566 12567 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective( 12568 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12569 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12570 if (!AStmt) 12571 return StmtError(); 12572 12573 auto *CS = cast<CapturedStmt>(AStmt); 12574 // 1.2.2 OpenMP Language Terminology 12575 // Structured block - An executable statement with a single entry at the 12576 // top and a single exit at the bottom. 12577 // The point of exit cannot be a branch out of the structured block. 12578 // longjmp() and throw() must not violate the entry/exit criteria. 12579 CS->getCapturedDecl()->setNothrow(); 12580 12581 for (int ThisCaptureLevel = 12582 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for); 12583 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12584 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12585 // 1.2.2 OpenMP Language Terminology 12586 // Structured block - An executable statement with a single entry at the 12587 // top and a single exit at the bottom. 12588 // The point of exit cannot be a branch out of the structured block. 12589 // longjmp() and throw() must not violate the entry/exit criteria. 12590 CS->getCapturedDecl()->setNothrow(); 12591 } 12592 12593 OMPLoopBasedDirective::HelperExprs B; 12594 // In presence of clause 'collapse' with number of loops, it will 12595 // define the nested loops number. 12596 unsigned NestedLoopCount = checkOpenMPLoop( 12597 OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses), 12598 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 12599 VarsWithImplicitDSA, B); 12600 12601 if (NestedLoopCount == 0) 12602 return StmtError(); 12603 12604 assert((CurContext->isDependentContext() || B.builtAll()) && 12605 "omp for loop exprs were not built"); 12606 12607 setFunctionHasBranchProtectedScope(); 12608 12609 DSAStack->setParentTeamsRegionLoc(StartLoc); 12610 12611 return OMPTeamsDistributeParallelForDirective::Create( 12612 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 12613 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 12614 } 12615 12616 StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses, 12617 Stmt *AStmt, 12618 SourceLocation StartLoc, 12619 SourceLocation EndLoc) { 12620 if (!AStmt) 12621 return StmtError(); 12622 12623 auto *CS = cast<CapturedStmt>(AStmt); 12624 // 1.2.2 OpenMP Language Terminology 12625 // Structured block - An executable statement with a single entry at the 12626 // top and a single exit at the bottom. 12627 // The point of exit cannot be a branch out of the structured block. 12628 // longjmp() and throw() must not violate the entry/exit criteria. 12629 CS->getCapturedDecl()->setNothrow(); 12630 12631 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_teams); 12632 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12633 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12634 // 1.2.2 OpenMP Language Terminology 12635 // Structured block - An executable statement with a single entry at the 12636 // top and a single exit at the bottom. 12637 // The point of exit cannot be a branch out of the structured block. 12638 // longjmp() and throw() must not violate the entry/exit criteria. 12639 CS->getCapturedDecl()->setNothrow(); 12640 } 12641 setFunctionHasBranchProtectedScope(); 12642 12643 return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, 12644 AStmt); 12645 } 12646 12647 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective( 12648 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12649 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12650 if (!AStmt) 12651 return StmtError(); 12652 12653 auto *CS = cast<CapturedStmt>(AStmt); 12654 // 1.2.2 OpenMP Language Terminology 12655 // Structured block - An executable statement with a single entry at the 12656 // top and a single exit at the bottom. 12657 // The point of exit cannot be a branch out of the structured block. 12658 // longjmp() and throw() must not violate the entry/exit criteria. 12659 CS->getCapturedDecl()->setNothrow(); 12660 for (int ThisCaptureLevel = 12661 getOpenMPCaptureLevels(OMPD_target_teams_distribute); 12662 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12663 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12664 // 1.2.2 OpenMP Language Terminology 12665 // Structured block - An executable statement with a single entry at the 12666 // top and a single exit at the bottom. 12667 // The point of exit cannot be a branch out of the structured block. 12668 // longjmp() and throw() must not violate the entry/exit criteria. 12669 CS->getCapturedDecl()->setNothrow(); 12670 } 12671 12672 OMPLoopBasedDirective::HelperExprs B; 12673 // In presence of clause 'collapse' with number of loops, it will 12674 // define the nested loops number. 12675 unsigned NestedLoopCount = checkOpenMPLoop( 12676 OMPD_target_teams_distribute, getCollapseNumberExpr(Clauses), 12677 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 12678 VarsWithImplicitDSA, B); 12679 if (NestedLoopCount == 0) 12680 return StmtError(); 12681 12682 assert((CurContext->isDependentContext() || B.builtAll()) && 12683 "omp target teams distribute loop exprs were not built"); 12684 12685 setFunctionHasBranchProtectedScope(); 12686 return OMPTargetTeamsDistributeDirective::Create( 12687 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12688 } 12689 12690 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective( 12691 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12692 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12693 if (!AStmt) 12694 return StmtError(); 12695 12696 auto *CS = cast<CapturedStmt>(AStmt); 12697 // 1.2.2 OpenMP Language Terminology 12698 // Structured block - An executable statement with a single entry at the 12699 // top and a single exit at the bottom. 12700 // The point of exit cannot be a branch out of the structured block. 12701 // longjmp() and throw() must not violate the entry/exit criteria. 12702 CS->getCapturedDecl()->setNothrow(); 12703 for (int ThisCaptureLevel = 12704 getOpenMPCaptureLevels(OMPD_target_teams_distribute_parallel_for); 12705 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12706 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12707 // 1.2.2 OpenMP Language Terminology 12708 // Structured block - An executable statement with a single entry at the 12709 // top and a single exit at the bottom. 12710 // The point of exit cannot be a branch out of the structured block. 12711 // longjmp() and throw() must not violate the entry/exit criteria. 12712 CS->getCapturedDecl()->setNothrow(); 12713 } 12714 12715 OMPLoopBasedDirective::HelperExprs B; 12716 // In presence of clause 'collapse' with number of loops, it will 12717 // define the nested loops number. 12718 unsigned NestedLoopCount = checkOpenMPLoop( 12719 OMPD_target_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses), 12720 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 12721 VarsWithImplicitDSA, B); 12722 if (NestedLoopCount == 0) 12723 return StmtError(); 12724 12725 assert((CurContext->isDependentContext() || B.builtAll()) && 12726 "omp target teams distribute parallel for loop exprs were not built"); 12727 12728 if (!CurContext->isDependentContext()) { 12729 // Finalize the clauses that need pre-built expressions for CodeGen. 12730 for (OMPClause *C : Clauses) { 12731 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12732 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12733 B.NumIterations, *this, CurScope, 12734 DSAStack)) 12735 return StmtError(); 12736 } 12737 } 12738 12739 setFunctionHasBranchProtectedScope(); 12740 return OMPTargetTeamsDistributeParallelForDirective::Create( 12741 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 12742 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 12743 } 12744 12745 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective( 12746 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12747 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12748 if (!AStmt) 12749 return StmtError(); 12750 12751 auto *CS = cast<CapturedStmt>(AStmt); 12752 // 1.2.2 OpenMP Language Terminology 12753 // Structured block - An executable statement with a single entry at the 12754 // top and a single exit at the bottom. 12755 // The point of exit cannot be a branch out of the structured block. 12756 // longjmp() and throw() must not violate the entry/exit criteria. 12757 CS->getCapturedDecl()->setNothrow(); 12758 for (int ThisCaptureLevel = getOpenMPCaptureLevels( 12759 OMPD_target_teams_distribute_parallel_for_simd); 12760 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12761 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12762 // 1.2.2 OpenMP Language Terminology 12763 // Structured block - An executable statement with a single entry at the 12764 // top and a single exit at the bottom. 12765 // The point of exit cannot be a branch out of the structured block. 12766 // longjmp() and throw() must not violate the entry/exit criteria. 12767 CS->getCapturedDecl()->setNothrow(); 12768 } 12769 12770 OMPLoopBasedDirective::HelperExprs B; 12771 // In presence of clause 'collapse' with number of loops, it will 12772 // define the nested loops number. 12773 unsigned NestedLoopCount = 12774 checkOpenMPLoop(OMPD_target_teams_distribute_parallel_for_simd, 12775 getCollapseNumberExpr(Clauses), 12776 nullptr /*ordered not a clause on distribute*/, CS, *this, 12777 *DSAStack, VarsWithImplicitDSA, B); 12778 if (NestedLoopCount == 0) 12779 return StmtError(); 12780 12781 assert((CurContext->isDependentContext() || B.builtAll()) && 12782 "omp target teams distribute parallel for simd loop exprs were not " 12783 "built"); 12784 12785 if (!CurContext->isDependentContext()) { 12786 // Finalize the clauses that need pre-built expressions for CodeGen. 12787 for (OMPClause *C : Clauses) { 12788 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12789 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12790 B.NumIterations, *this, CurScope, 12791 DSAStack)) 12792 return StmtError(); 12793 } 12794 } 12795 12796 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12797 return StmtError(); 12798 12799 setFunctionHasBranchProtectedScope(); 12800 return OMPTargetTeamsDistributeParallelForSimdDirective::Create( 12801 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12802 } 12803 12804 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective( 12805 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12806 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12807 if (!AStmt) 12808 return StmtError(); 12809 12810 auto *CS = cast<CapturedStmt>(AStmt); 12811 // 1.2.2 OpenMP Language Terminology 12812 // Structured block - An executable statement with a single entry at the 12813 // top and a single exit at the bottom. 12814 // The point of exit cannot be a branch out of the structured block. 12815 // longjmp() and throw() must not violate the entry/exit criteria. 12816 CS->getCapturedDecl()->setNothrow(); 12817 for (int ThisCaptureLevel = 12818 getOpenMPCaptureLevels(OMPD_target_teams_distribute_simd); 12819 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12820 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12821 // 1.2.2 OpenMP Language Terminology 12822 // Structured block - An executable statement with a single entry at the 12823 // top and a single exit at the bottom. 12824 // The point of exit cannot be a branch out of the structured block. 12825 // longjmp() and throw() must not violate the entry/exit criteria. 12826 CS->getCapturedDecl()->setNothrow(); 12827 } 12828 12829 OMPLoopBasedDirective::HelperExprs B; 12830 // In presence of clause 'collapse' with number of loops, it will 12831 // define the nested loops number. 12832 unsigned NestedLoopCount = checkOpenMPLoop( 12833 OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses), 12834 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 12835 VarsWithImplicitDSA, B); 12836 if (NestedLoopCount == 0) 12837 return StmtError(); 12838 12839 assert((CurContext->isDependentContext() || B.builtAll()) && 12840 "omp target teams distribute simd loop exprs were not built"); 12841 12842 if (!CurContext->isDependentContext()) { 12843 // Finalize the clauses that need pre-built expressions for CodeGen. 12844 for (OMPClause *C : Clauses) { 12845 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12846 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12847 B.NumIterations, *this, CurScope, 12848 DSAStack)) 12849 return StmtError(); 12850 } 12851 } 12852 12853 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12854 return StmtError(); 12855 12856 setFunctionHasBranchProtectedScope(); 12857 return OMPTargetTeamsDistributeSimdDirective::Create( 12858 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12859 } 12860 12861 bool Sema::checkTransformableLoopNest( 12862 OpenMPDirectiveKind Kind, Stmt *AStmt, int NumLoops, 12863 SmallVectorImpl<OMPLoopBasedDirective::HelperExprs> &LoopHelpers, 12864 Stmt *&Body, 12865 SmallVectorImpl<SmallVector<llvm::PointerUnion<Stmt *, Decl *>, 0>> 12866 &OriginalInits) { 12867 OriginalInits.emplace_back(); 12868 bool Result = OMPLoopBasedDirective::doForAllLoops( 12869 AStmt->IgnoreContainers(), /*TryImperfectlyNestedLoops=*/false, NumLoops, 12870 [this, &LoopHelpers, &Body, &OriginalInits, Kind](unsigned Cnt, 12871 Stmt *CurStmt) { 12872 VarsWithInheritedDSAType TmpDSA; 12873 unsigned SingleNumLoops = 12874 checkOpenMPLoop(Kind, nullptr, nullptr, CurStmt, *this, *DSAStack, 12875 TmpDSA, LoopHelpers[Cnt]); 12876 if (SingleNumLoops == 0) 12877 return true; 12878 assert(SingleNumLoops == 1 && "Expect single loop iteration space"); 12879 if (auto *For = dyn_cast<ForStmt>(CurStmt)) { 12880 OriginalInits.back().push_back(For->getInit()); 12881 Body = For->getBody(); 12882 } else { 12883 assert(isa<CXXForRangeStmt>(CurStmt) && 12884 "Expected canonical for or range-based for loops."); 12885 auto *CXXFor = cast<CXXForRangeStmt>(CurStmt); 12886 OriginalInits.back().push_back(CXXFor->getBeginStmt()); 12887 Body = CXXFor->getBody(); 12888 } 12889 OriginalInits.emplace_back(); 12890 return false; 12891 }, 12892 [&OriginalInits](OMPLoopBasedDirective *Transform) { 12893 Stmt *DependentPreInits; 12894 if (auto *Dir = dyn_cast<OMPTileDirective>(Transform)) 12895 DependentPreInits = Dir->getPreInits(); 12896 else if (auto *Dir = dyn_cast<OMPUnrollDirective>(Transform)) 12897 DependentPreInits = Dir->getPreInits(); 12898 else 12899 llvm_unreachable("Unhandled loop transformation"); 12900 if (!DependentPreInits) 12901 return; 12902 for (Decl *C : cast<DeclStmt>(DependentPreInits)->getDeclGroup()) 12903 OriginalInits.back().push_back(C); 12904 }); 12905 assert(OriginalInits.back().empty() && "No preinit after innermost loop"); 12906 OriginalInits.pop_back(); 12907 return Result; 12908 } 12909 12910 StmtResult Sema::ActOnOpenMPTileDirective(ArrayRef<OMPClause *> Clauses, 12911 Stmt *AStmt, SourceLocation StartLoc, 12912 SourceLocation EndLoc) { 12913 auto SizesClauses = 12914 OMPExecutableDirective::getClausesOfKind<OMPSizesClause>(Clauses); 12915 if (SizesClauses.empty()) { 12916 // A missing 'sizes' clause is already reported by the parser. 12917 return StmtError(); 12918 } 12919 const OMPSizesClause *SizesClause = *SizesClauses.begin(); 12920 unsigned NumLoops = SizesClause->getNumSizes(); 12921 12922 // Empty statement should only be possible if there already was an error. 12923 if (!AStmt) 12924 return StmtError(); 12925 12926 // Verify and diagnose loop nest. 12927 SmallVector<OMPLoopBasedDirective::HelperExprs, 4> LoopHelpers(NumLoops); 12928 Stmt *Body = nullptr; 12929 SmallVector<SmallVector<llvm::PointerUnion<Stmt *, Decl *>, 0>, 4> 12930 OriginalInits; 12931 if (!checkTransformableLoopNest(OMPD_tile, AStmt, NumLoops, LoopHelpers, Body, 12932 OriginalInits)) 12933 return StmtError(); 12934 12935 // Delay tiling to when template is completely instantiated. 12936 if (CurContext->isDependentContext()) 12937 return OMPTileDirective::Create(Context, StartLoc, EndLoc, Clauses, 12938 NumLoops, AStmt, nullptr, nullptr); 12939 12940 SmallVector<Decl *, 4> PreInits; 12941 12942 // Create iteration variables for the generated loops. 12943 SmallVector<VarDecl *, 4> FloorIndVars; 12944 SmallVector<VarDecl *, 4> TileIndVars; 12945 FloorIndVars.resize(NumLoops); 12946 TileIndVars.resize(NumLoops); 12947 for (unsigned I = 0; I < NumLoops; ++I) { 12948 OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers[I]; 12949 12950 assert(LoopHelper.Counters.size() == 1 && 12951 "Expect single-dimensional loop iteration space"); 12952 auto *OrigCntVar = cast<DeclRefExpr>(LoopHelper.Counters.front()); 12953 std::string OrigVarName = OrigCntVar->getNameInfo().getAsString(); 12954 DeclRefExpr *IterVarRef = cast<DeclRefExpr>(LoopHelper.IterationVarRef); 12955 QualType CntTy = IterVarRef->getType(); 12956 12957 // Iteration variable for the floor (i.e. outer) loop. 12958 { 12959 std::string FloorCntName = 12960 (Twine(".floor_") + llvm::utostr(I) + ".iv." + OrigVarName).str(); 12961 VarDecl *FloorCntDecl = 12962 buildVarDecl(*this, {}, CntTy, FloorCntName, nullptr, OrigCntVar); 12963 FloorIndVars[I] = FloorCntDecl; 12964 } 12965 12966 // Iteration variable for the tile (i.e. inner) loop. 12967 { 12968 std::string TileCntName = 12969 (Twine(".tile_") + llvm::utostr(I) + ".iv." + OrigVarName).str(); 12970 12971 // Reuse the iteration variable created by checkOpenMPLoop. It is also 12972 // used by the expressions to derive the original iteration variable's 12973 // value from the logical iteration number. 12974 auto *TileCntDecl = cast<VarDecl>(IterVarRef->getDecl()); 12975 TileCntDecl->setDeclName(&PP.getIdentifierTable().get(TileCntName)); 12976 TileIndVars[I] = TileCntDecl; 12977 } 12978 for (auto &P : OriginalInits[I]) { 12979 if (auto *D = P.dyn_cast<Decl *>()) 12980 PreInits.push_back(D); 12981 else if (auto *PI = dyn_cast_or_null<DeclStmt>(P.dyn_cast<Stmt *>())) 12982 PreInits.append(PI->decl_begin(), PI->decl_end()); 12983 } 12984 if (auto *PI = cast_or_null<DeclStmt>(LoopHelper.PreInits)) 12985 PreInits.append(PI->decl_begin(), PI->decl_end()); 12986 // Gather declarations for the data members used as counters. 12987 for (Expr *CounterRef : LoopHelper.Counters) { 12988 auto *CounterDecl = cast<DeclRefExpr>(CounterRef)->getDecl(); 12989 if (isa<OMPCapturedExprDecl>(CounterDecl)) 12990 PreInits.push_back(CounterDecl); 12991 } 12992 } 12993 12994 // Once the original iteration values are set, append the innermost body. 12995 Stmt *Inner = Body; 12996 12997 // Create tile loops from the inside to the outside. 12998 for (int I = NumLoops - 1; I >= 0; --I) { 12999 OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers[I]; 13000 Expr *NumIterations = LoopHelper.NumIterations; 13001 auto *OrigCntVar = cast<DeclRefExpr>(LoopHelper.Counters[0]); 13002 QualType CntTy = OrigCntVar->getType(); 13003 Expr *DimTileSize = SizesClause->getSizesRefs()[I]; 13004 Scope *CurScope = getCurScope(); 13005 13006 // Commonly used variables. 13007 DeclRefExpr *TileIV = buildDeclRefExpr(*this, TileIndVars[I], CntTy, 13008 OrigCntVar->getExprLoc()); 13009 DeclRefExpr *FloorIV = buildDeclRefExpr(*this, FloorIndVars[I], CntTy, 13010 OrigCntVar->getExprLoc()); 13011 13012 // For init-statement: auto .tile.iv = .floor.iv 13013 AddInitializerToDecl(TileIndVars[I], DefaultLvalueConversion(FloorIV).get(), 13014 /*DirectInit=*/false); 13015 Decl *CounterDecl = TileIndVars[I]; 13016 StmtResult InitStmt = new (Context) 13017 DeclStmt(DeclGroupRef::Create(Context, &CounterDecl, 1), 13018 OrigCntVar->getBeginLoc(), OrigCntVar->getEndLoc()); 13019 if (!InitStmt.isUsable()) 13020 return StmtError(); 13021 13022 // For cond-expression: .tile.iv < min(.floor.iv + DimTileSize, 13023 // NumIterations) 13024 ExprResult EndOfTile = BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), 13025 BO_Add, FloorIV, DimTileSize); 13026 if (!EndOfTile.isUsable()) 13027 return StmtError(); 13028 ExprResult IsPartialTile = 13029 BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), BO_LT, 13030 NumIterations, EndOfTile.get()); 13031 if (!IsPartialTile.isUsable()) 13032 return StmtError(); 13033 ExprResult MinTileAndIterSpace = ActOnConditionalOp( 13034 LoopHelper.Cond->getBeginLoc(), LoopHelper.Cond->getEndLoc(), 13035 IsPartialTile.get(), NumIterations, EndOfTile.get()); 13036 if (!MinTileAndIterSpace.isUsable()) 13037 return StmtError(); 13038 ExprResult CondExpr = BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), 13039 BO_LT, TileIV, MinTileAndIterSpace.get()); 13040 if (!CondExpr.isUsable()) 13041 return StmtError(); 13042 13043 // For incr-statement: ++.tile.iv 13044 ExprResult IncrStmt = 13045 BuildUnaryOp(CurScope, LoopHelper.Inc->getExprLoc(), UO_PreInc, TileIV); 13046 if (!IncrStmt.isUsable()) 13047 return StmtError(); 13048 13049 // Statements to set the original iteration variable's value from the 13050 // logical iteration number. 13051 // Generated for loop is: 13052 // Original_for_init; 13053 // for (auto .tile.iv = .floor.iv; .tile.iv < min(.floor.iv + DimTileSize, 13054 // NumIterations); ++.tile.iv) { 13055 // Original_Body; 13056 // Original_counter_update; 13057 // } 13058 // FIXME: If the innermost body is an loop itself, inserting these 13059 // statements stops it being recognized as a perfectly nested loop (e.g. 13060 // for applying tiling again). If this is the case, sink the expressions 13061 // further into the inner loop. 13062 SmallVector<Stmt *, 4> BodyParts; 13063 BodyParts.append(LoopHelper.Updates.begin(), LoopHelper.Updates.end()); 13064 BodyParts.push_back(Inner); 13065 Inner = CompoundStmt::Create(Context, BodyParts, Inner->getBeginLoc(), 13066 Inner->getEndLoc()); 13067 Inner = new (Context) 13068 ForStmt(Context, InitStmt.get(), CondExpr.get(), nullptr, 13069 IncrStmt.get(), Inner, LoopHelper.Init->getBeginLoc(), 13070 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc()); 13071 } 13072 13073 // Create floor loops from the inside to the outside. 13074 for (int I = NumLoops - 1; I >= 0; --I) { 13075 auto &LoopHelper = LoopHelpers[I]; 13076 Expr *NumIterations = LoopHelper.NumIterations; 13077 DeclRefExpr *OrigCntVar = cast<DeclRefExpr>(LoopHelper.Counters[0]); 13078 QualType CntTy = OrigCntVar->getType(); 13079 Expr *DimTileSize = SizesClause->getSizesRefs()[I]; 13080 Scope *CurScope = getCurScope(); 13081 13082 // Commonly used variables. 13083 DeclRefExpr *FloorIV = buildDeclRefExpr(*this, FloorIndVars[I], CntTy, 13084 OrigCntVar->getExprLoc()); 13085 13086 // For init-statement: auto .floor.iv = 0 13087 AddInitializerToDecl( 13088 FloorIndVars[I], 13089 ActOnIntegerConstant(LoopHelper.Init->getExprLoc(), 0).get(), 13090 /*DirectInit=*/false); 13091 Decl *CounterDecl = FloorIndVars[I]; 13092 StmtResult InitStmt = new (Context) 13093 DeclStmt(DeclGroupRef::Create(Context, &CounterDecl, 1), 13094 OrigCntVar->getBeginLoc(), OrigCntVar->getEndLoc()); 13095 if (!InitStmt.isUsable()) 13096 return StmtError(); 13097 13098 // For cond-expression: .floor.iv < NumIterations 13099 ExprResult CondExpr = BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), 13100 BO_LT, FloorIV, NumIterations); 13101 if (!CondExpr.isUsable()) 13102 return StmtError(); 13103 13104 // For incr-statement: .floor.iv += DimTileSize 13105 ExprResult IncrStmt = BuildBinOp(CurScope, LoopHelper.Inc->getExprLoc(), 13106 BO_AddAssign, FloorIV, DimTileSize); 13107 if (!IncrStmt.isUsable()) 13108 return StmtError(); 13109 13110 Inner = new (Context) 13111 ForStmt(Context, InitStmt.get(), CondExpr.get(), nullptr, 13112 IncrStmt.get(), Inner, LoopHelper.Init->getBeginLoc(), 13113 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc()); 13114 } 13115 13116 return OMPTileDirective::Create(Context, StartLoc, EndLoc, Clauses, NumLoops, 13117 AStmt, Inner, 13118 buildPreInits(Context, PreInits)); 13119 } 13120 13121 StmtResult Sema::ActOnOpenMPUnrollDirective(ArrayRef<OMPClause *> Clauses, 13122 Stmt *AStmt, 13123 SourceLocation StartLoc, 13124 SourceLocation EndLoc) { 13125 // Empty statement should only be possible if there already was an error. 13126 if (!AStmt) 13127 return StmtError(); 13128 13129 if (checkMutuallyExclusiveClauses(*this, Clauses, {OMPC_partial, OMPC_full})) 13130 return StmtError(); 13131 13132 const OMPFullClause *FullClause = 13133 OMPExecutableDirective::getSingleClause<OMPFullClause>(Clauses); 13134 const OMPPartialClause *PartialClause = 13135 OMPExecutableDirective::getSingleClause<OMPPartialClause>(Clauses); 13136 assert(!(FullClause && PartialClause) && 13137 "mutual exclusivity must have been checked before"); 13138 13139 constexpr unsigned NumLoops = 1; 13140 Stmt *Body = nullptr; 13141 SmallVector<OMPLoopBasedDirective::HelperExprs, NumLoops> LoopHelpers( 13142 NumLoops); 13143 SmallVector<SmallVector<llvm::PointerUnion<Stmt *, Decl *>, 0>, NumLoops + 1> 13144 OriginalInits; 13145 if (!checkTransformableLoopNest(OMPD_unroll, AStmt, NumLoops, LoopHelpers, 13146 Body, OriginalInits)) 13147 return StmtError(); 13148 13149 unsigned NumGeneratedLoops = PartialClause ? 1 : 0; 13150 13151 // Delay unrolling to when template is completely instantiated. 13152 if (CurContext->isDependentContext()) 13153 return OMPUnrollDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 13154 NumGeneratedLoops, nullptr, nullptr); 13155 13156 OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers.front(); 13157 13158 if (FullClause) { 13159 if (!VerifyPositiveIntegerConstantInClause( 13160 LoopHelper.NumIterations, OMPC_full, /*StrictlyPositive=*/false, 13161 /*SuppressExprDiags=*/true) 13162 .isUsable()) { 13163 Diag(AStmt->getBeginLoc(), diag::err_omp_unroll_full_variable_trip_count); 13164 Diag(FullClause->getBeginLoc(), diag::note_omp_directive_here) 13165 << "#pragma omp unroll full"; 13166 return StmtError(); 13167 } 13168 } 13169 13170 // The generated loop may only be passed to other loop-associated directive 13171 // when a partial clause is specified. Without the requirement it is 13172 // sufficient to generate loop unroll metadata at code-generation. 13173 if (NumGeneratedLoops == 0) 13174 return OMPUnrollDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 13175 NumGeneratedLoops, nullptr, nullptr); 13176 13177 // Otherwise, we need to provide a de-sugared/transformed AST that can be 13178 // associated with another loop directive. 13179 // 13180 // The canonical loop analysis return by checkTransformableLoopNest assumes 13181 // the following structure to be the same loop without transformations or 13182 // directives applied: \code OriginalInits; LoopHelper.PreInits; 13183 // LoopHelper.Counters; 13184 // for (; IV < LoopHelper.NumIterations; ++IV) { 13185 // LoopHelper.Updates; 13186 // Body; 13187 // } 13188 // \endcode 13189 // where IV is a variable declared and initialized to 0 in LoopHelper.PreInits 13190 // and referenced by LoopHelper.IterationVarRef. 13191 // 13192 // The unrolling directive transforms this into the following loop: 13193 // \code 13194 // OriginalInits; \ 13195 // LoopHelper.PreInits; > NewPreInits 13196 // LoopHelper.Counters; / 13197 // for (auto UIV = 0; UIV < LoopHelper.NumIterations; UIV+=Factor) { 13198 // #pragma clang loop unroll_count(Factor) 13199 // for (IV = UIV; IV < UIV + Factor && UIV < LoopHelper.NumIterations; ++IV) 13200 // { 13201 // LoopHelper.Updates; 13202 // Body; 13203 // } 13204 // } 13205 // \endcode 13206 // where UIV is a new logical iteration counter. IV must be the same VarDecl 13207 // as the original LoopHelper.IterationVarRef because LoopHelper.Updates 13208 // references it. If the partially unrolled loop is associated with another 13209 // loop directive (like an OMPForDirective), it will use checkOpenMPLoop to 13210 // analyze this loop, i.e. the outer loop must fulfill the constraints of an 13211 // OpenMP canonical loop. The inner loop is not an associable canonical loop 13212 // and only exists to defer its unrolling to LLVM's LoopUnroll instead of 13213 // doing it in the frontend (by adding loop metadata). NewPreInits becomes a 13214 // property of the OMPLoopBasedDirective instead of statements in 13215 // CompoundStatement. This is to allow the loop to become a non-outermost loop 13216 // of a canonical loop nest where these PreInits are emitted before the 13217 // outermost directive. 13218 13219 // Determine the PreInit declarations. 13220 SmallVector<Decl *, 4> PreInits; 13221 assert(OriginalInits.size() == 1 && 13222 "Expecting a single-dimensional loop iteration space"); 13223 for (auto &P : OriginalInits[0]) { 13224 if (auto *D = P.dyn_cast<Decl *>()) 13225 PreInits.push_back(D); 13226 else if (auto *PI = dyn_cast_or_null<DeclStmt>(P.dyn_cast<Stmt *>())) 13227 PreInits.append(PI->decl_begin(), PI->decl_end()); 13228 } 13229 if (auto *PI = cast_or_null<DeclStmt>(LoopHelper.PreInits)) 13230 PreInits.append(PI->decl_begin(), PI->decl_end()); 13231 // Gather declarations for the data members used as counters. 13232 for (Expr *CounterRef : LoopHelper.Counters) { 13233 auto *CounterDecl = cast<DeclRefExpr>(CounterRef)->getDecl(); 13234 if (isa<OMPCapturedExprDecl>(CounterDecl)) 13235 PreInits.push_back(CounterDecl); 13236 } 13237 13238 auto *IterationVarRef = cast<DeclRefExpr>(LoopHelper.IterationVarRef); 13239 QualType IVTy = IterationVarRef->getType(); 13240 assert(LoopHelper.Counters.size() == 1 && 13241 "Expecting a single-dimensional loop iteration space"); 13242 auto *OrigVar = cast<DeclRefExpr>(LoopHelper.Counters.front()); 13243 13244 // Determine the unroll factor. 13245 uint64_t Factor; 13246 SourceLocation FactorLoc; 13247 if (Expr *FactorVal = PartialClause->getFactor()) { 13248 Factor = 13249 FactorVal->getIntegerConstantExpr(Context).getValue().getZExtValue(); 13250 FactorLoc = FactorVal->getExprLoc(); 13251 } else { 13252 // TODO: Use a better profitability model. 13253 Factor = 2; 13254 } 13255 assert(Factor > 0 && "Expected positive unroll factor"); 13256 auto MakeFactorExpr = [this, Factor, IVTy, FactorLoc]() { 13257 return IntegerLiteral::Create( 13258 Context, llvm::APInt(Context.getIntWidth(IVTy), Factor), IVTy, 13259 FactorLoc); 13260 }; 13261 13262 // Iteration variable SourceLocations. 13263 SourceLocation OrigVarLoc = OrigVar->getExprLoc(); 13264 SourceLocation OrigVarLocBegin = OrigVar->getBeginLoc(); 13265 SourceLocation OrigVarLocEnd = OrigVar->getEndLoc(); 13266 13267 // Internal variable names. 13268 std::string OrigVarName = OrigVar->getNameInfo().getAsString(); 13269 std::string OuterIVName = (Twine(".unrolled.iv.") + OrigVarName).str(); 13270 std::string InnerIVName = (Twine(".unroll_inner.iv.") + OrigVarName).str(); 13271 std::string InnerTripCountName = 13272 (Twine(".unroll_inner.tripcount.") + OrigVarName).str(); 13273 13274 // Create the iteration variable for the unrolled loop. 13275 VarDecl *OuterIVDecl = 13276 buildVarDecl(*this, {}, IVTy, OuterIVName, nullptr, OrigVar); 13277 auto MakeOuterRef = [this, OuterIVDecl, IVTy, OrigVarLoc]() { 13278 return buildDeclRefExpr(*this, OuterIVDecl, IVTy, OrigVarLoc); 13279 }; 13280 13281 // Iteration variable for the inner loop: Reuse the iteration variable created 13282 // by checkOpenMPLoop. 13283 auto *InnerIVDecl = cast<VarDecl>(IterationVarRef->getDecl()); 13284 InnerIVDecl->setDeclName(&PP.getIdentifierTable().get(InnerIVName)); 13285 auto MakeInnerRef = [this, InnerIVDecl, IVTy, OrigVarLoc]() { 13286 return buildDeclRefExpr(*this, InnerIVDecl, IVTy, OrigVarLoc); 13287 }; 13288 13289 // Make a copy of the NumIterations expression for each use: By the AST 13290 // constraints, every expression object in a DeclContext must be unique. 13291 CaptureVars CopyTransformer(*this); 13292 auto MakeNumIterations = [&CopyTransformer, &LoopHelper]() -> Expr * { 13293 return AssertSuccess( 13294 CopyTransformer.TransformExpr(LoopHelper.NumIterations)); 13295 }; 13296 13297 // Inner For init-statement: auto .unroll_inner.iv = .unrolled.iv 13298 ExprResult LValueConv = DefaultLvalueConversion(MakeOuterRef()); 13299 AddInitializerToDecl(InnerIVDecl, LValueConv.get(), /*DirectInit=*/false); 13300 StmtResult InnerInit = new (Context) 13301 DeclStmt(DeclGroupRef(InnerIVDecl), OrigVarLocBegin, OrigVarLocEnd); 13302 if (!InnerInit.isUsable()) 13303 return StmtError(); 13304 13305 // Inner For cond-expression: 13306 // \code 13307 // .unroll_inner.iv < .unrolled.iv + Factor && 13308 // .unroll_inner.iv < NumIterations 13309 // \endcode 13310 // This conjunction of two conditions allows ScalarEvolution to derive the 13311 // maximum trip count of the inner loop. 13312 ExprResult EndOfTile = BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), 13313 BO_Add, MakeOuterRef(), MakeFactorExpr()); 13314 if (!EndOfTile.isUsable()) 13315 return StmtError(); 13316 ExprResult InnerCond1 = BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), 13317 BO_LT, MakeInnerRef(), EndOfTile.get()); 13318 if (!InnerCond1.isUsable()) 13319 return StmtError(); 13320 ExprResult InnerCond2 = 13321 BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), BO_LT, MakeInnerRef(), 13322 MakeNumIterations()); 13323 if (!InnerCond2.isUsable()) 13324 return StmtError(); 13325 ExprResult InnerCond = 13326 BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), BO_LAnd, 13327 InnerCond1.get(), InnerCond2.get()); 13328 if (!InnerCond.isUsable()) 13329 return StmtError(); 13330 13331 // Inner For incr-statement: ++.unroll_inner.iv 13332 ExprResult InnerIncr = BuildUnaryOp(CurScope, LoopHelper.Inc->getExprLoc(), 13333 UO_PreInc, MakeInnerRef()); 13334 if (!InnerIncr.isUsable()) 13335 return StmtError(); 13336 13337 // Inner For statement. 13338 SmallVector<Stmt *> InnerBodyStmts; 13339 InnerBodyStmts.append(LoopHelper.Updates.begin(), LoopHelper.Updates.end()); 13340 InnerBodyStmts.push_back(Body); 13341 CompoundStmt *InnerBody = CompoundStmt::Create( 13342 Context, InnerBodyStmts, Body->getBeginLoc(), Body->getEndLoc()); 13343 ForStmt *InnerFor = new (Context) 13344 ForStmt(Context, InnerInit.get(), InnerCond.get(), nullptr, 13345 InnerIncr.get(), InnerBody, LoopHelper.Init->getBeginLoc(), 13346 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc()); 13347 13348 // Unroll metadata for the inner loop. 13349 // This needs to take into account the remainder portion of the unrolled loop, 13350 // hence `unroll(full)` does not apply here, even though the LoopUnroll pass 13351 // supports multiple loop exits. Instead, unroll using a factor equivalent to 13352 // the maximum trip count, which will also generate a remainder loop. Just 13353 // `unroll(enable)` (which could have been useful if the user has not 13354 // specified a concrete factor; even though the outer loop cannot be 13355 // influenced anymore, would avoid more code bloat than necessary) will refuse 13356 // the loop because "Won't unroll; remainder loop could not be generated when 13357 // assuming runtime trip count". Even if it did work, it must not choose a 13358 // larger unroll factor than the maximum loop length, or it would always just 13359 // execute the remainder loop. 13360 LoopHintAttr *UnrollHintAttr = 13361 LoopHintAttr::CreateImplicit(Context, LoopHintAttr::UnrollCount, 13362 LoopHintAttr::Numeric, MakeFactorExpr()); 13363 AttributedStmt *InnerUnrolled = 13364 AttributedStmt::Create(Context, StartLoc, {UnrollHintAttr}, InnerFor); 13365 13366 // Outer For init-statement: auto .unrolled.iv = 0 13367 AddInitializerToDecl( 13368 OuterIVDecl, ActOnIntegerConstant(LoopHelper.Init->getExprLoc(), 0).get(), 13369 /*DirectInit=*/false); 13370 StmtResult OuterInit = new (Context) 13371 DeclStmt(DeclGroupRef(OuterIVDecl), OrigVarLocBegin, OrigVarLocEnd); 13372 if (!OuterInit.isUsable()) 13373 return StmtError(); 13374 13375 // Outer For cond-expression: .unrolled.iv < NumIterations 13376 ExprResult OuterConde = 13377 BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), BO_LT, MakeOuterRef(), 13378 MakeNumIterations()); 13379 if (!OuterConde.isUsable()) 13380 return StmtError(); 13381 13382 // Outer For incr-statement: .unrolled.iv += Factor 13383 ExprResult OuterIncr = 13384 BuildBinOp(CurScope, LoopHelper.Inc->getExprLoc(), BO_AddAssign, 13385 MakeOuterRef(), MakeFactorExpr()); 13386 if (!OuterIncr.isUsable()) 13387 return StmtError(); 13388 13389 // Outer For statement. 13390 ForStmt *OuterFor = new (Context) 13391 ForStmt(Context, OuterInit.get(), OuterConde.get(), nullptr, 13392 OuterIncr.get(), InnerUnrolled, LoopHelper.Init->getBeginLoc(), 13393 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc()); 13394 13395 return OMPUnrollDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 13396 NumGeneratedLoops, OuterFor, 13397 buildPreInits(Context, PreInits)); 13398 } 13399 13400 OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr, 13401 SourceLocation StartLoc, 13402 SourceLocation LParenLoc, 13403 SourceLocation EndLoc) { 13404 OMPClause *Res = nullptr; 13405 switch (Kind) { 13406 case OMPC_final: 13407 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc); 13408 break; 13409 case OMPC_num_threads: 13410 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc); 13411 break; 13412 case OMPC_safelen: 13413 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc); 13414 break; 13415 case OMPC_simdlen: 13416 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc); 13417 break; 13418 case OMPC_allocator: 13419 Res = ActOnOpenMPAllocatorClause(Expr, StartLoc, LParenLoc, EndLoc); 13420 break; 13421 case OMPC_collapse: 13422 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc); 13423 break; 13424 case OMPC_ordered: 13425 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr); 13426 break; 13427 case OMPC_num_teams: 13428 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc); 13429 break; 13430 case OMPC_thread_limit: 13431 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc); 13432 break; 13433 case OMPC_priority: 13434 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc); 13435 break; 13436 case OMPC_grainsize: 13437 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc); 13438 break; 13439 case OMPC_num_tasks: 13440 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc); 13441 break; 13442 case OMPC_hint: 13443 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc); 13444 break; 13445 case OMPC_depobj: 13446 Res = ActOnOpenMPDepobjClause(Expr, StartLoc, LParenLoc, EndLoc); 13447 break; 13448 case OMPC_detach: 13449 Res = ActOnOpenMPDetachClause(Expr, StartLoc, LParenLoc, EndLoc); 13450 break; 13451 case OMPC_novariants: 13452 Res = ActOnOpenMPNovariantsClause(Expr, StartLoc, LParenLoc, EndLoc); 13453 break; 13454 case OMPC_nocontext: 13455 Res = ActOnOpenMPNocontextClause(Expr, StartLoc, LParenLoc, EndLoc); 13456 break; 13457 case OMPC_filter: 13458 Res = ActOnOpenMPFilterClause(Expr, StartLoc, LParenLoc, EndLoc); 13459 break; 13460 case OMPC_partial: 13461 Res = ActOnOpenMPPartialClause(Expr, StartLoc, LParenLoc, EndLoc); 13462 break; 13463 case OMPC_align: 13464 Res = ActOnOpenMPAlignClause(Expr, StartLoc, LParenLoc, EndLoc); 13465 break; 13466 case OMPC_device: 13467 case OMPC_if: 13468 case OMPC_default: 13469 case OMPC_proc_bind: 13470 case OMPC_schedule: 13471 case OMPC_private: 13472 case OMPC_firstprivate: 13473 case OMPC_lastprivate: 13474 case OMPC_shared: 13475 case OMPC_reduction: 13476 case OMPC_task_reduction: 13477 case OMPC_in_reduction: 13478 case OMPC_linear: 13479 case OMPC_aligned: 13480 case OMPC_copyin: 13481 case OMPC_copyprivate: 13482 case OMPC_nowait: 13483 case OMPC_untied: 13484 case OMPC_mergeable: 13485 case OMPC_threadprivate: 13486 case OMPC_sizes: 13487 case OMPC_allocate: 13488 case OMPC_flush: 13489 case OMPC_read: 13490 case OMPC_write: 13491 case OMPC_update: 13492 case OMPC_capture: 13493 case OMPC_compare: 13494 case OMPC_seq_cst: 13495 case OMPC_acq_rel: 13496 case OMPC_acquire: 13497 case OMPC_release: 13498 case OMPC_relaxed: 13499 case OMPC_depend: 13500 case OMPC_threads: 13501 case OMPC_simd: 13502 case OMPC_map: 13503 case OMPC_nogroup: 13504 case OMPC_dist_schedule: 13505 case OMPC_defaultmap: 13506 case OMPC_unknown: 13507 case OMPC_uniform: 13508 case OMPC_to: 13509 case OMPC_from: 13510 case OMPC_use_device_ptr: 13511 case OMPC_use_device_addr: 13512 case OMPC_is_device_ptr: 13513 case OMPC_unified_address: 13514 case OMPC_unified_shared_memory: 13515 case OMPC_reverse_offload: 13516 case OMPC_dynamic_allocators: 13517 case OMPC_atomic_default_mem_order: 13518 case OMPC_device_type: 13519 case OMPC_match: 13520 case OMPC_nontemporal: 13521 case OMPC_order: 13522 case OMPC_destroy: 13523 case OMPC_inclusive: 13524 case OMPC_exclusive: 13525 case OMPC_uses_allocators: 13526 case OMPC_affinity: 13527 case OMPC_when: 13528 case OMPC_bind: 13529 default: 13530 llvm_unreachable("Clause is not allowed."); 13531 } 13532 return Res; 13533 } 13534 13535 // An OpenMP directive such as 'target parallel' has two captured regions: 13536 // for the 'target' and 'parallel' respectively. This function returns 13537 // the region in which to capture expressions associated with a clause. 13538 // A return value of OMPD_unknown signifies that the expression should not 13539 // be captured. 13540 static OpenMPDirectiveKind getOpenMPCaptureRegionForClause( 13541 OpenMPDirectiveKind DKind, OpenMPClauseKind CKind, unsigned OpenMPVersion, 13542 OpenMPDirectiveKind NameModifier = OMPD_unknown) { 13543 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 13544 switch (CKind) { 13545 case OMPC_if: 13546 switch (DKind) { 13547 case OMPD_target_parallel_for_simd: 13548 if (OpenMPVersion >= 50 && 13549 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) { 13550 CaptureRegion = OMPD_parallel; 13551 break; 13552 } 13553 LLVM_FALLTHROUGH; 13554 case OMPD_target_parallel: 13555 case OMPD_target_parallel_for: 13556 // If this clause applies to the nested 'parallel' region, capture within 13557 // the 'target' region, otherwise do not capture. 13558 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel) 13559 CaptureRegion = OMPD_target; 13560 break; 13561 case OMPD_target_teams_distribute_parallel_for_simd: 13562 if (OpenMPVersion >= 50 && 13563 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) { 13564 CaptureRegion = OMPD_parallel; 13565 break; 13566 } 13567 LLVM_FALLTHROUGH; 13568 case OMPD_target_teams_distribute_parallel_for: 13569 // If this clause applies to the nested 'parallel' region, capture within 13570 // the 'teams' region, otherwise do not capture. 13571 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel) 13572 CaptureRegion = OMPD_teams; 13573 break; 13574 case OMPD_teams_distribute_parallel_for_simd: 13575 if (OpenMPVersion >= 50 && 13576 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) { 13577 CaptureRegion = OMPD_parallel; 13578 break; 13579 } 13580 LLVM_FALLTHROUGH; 13581 case OMPD_teams_distribute_parallel_for: 13582 CaptureRegion = OMPD_teams; 13583 break; 13584 case OMPD_target_update: 13585 case OMPD_target_enter_data: 13586 case OMPD_target_exit_data: 13587 CaptureRegion = OMPD_task; 13588 break; 13589 case OMPD_parallel_master_taskloop: 13590 if (NameModifier == OMPD_unknown || NameModifier == OMPD_taskloop) 13591 CaptureRegion = OMPD_parallel; 13592 break; 13593 case OMPD_parallel_master_taskloop_simd: 13594 if ((OpenMPVersion <= 45 && NameModifier == OMPD_unknown) || 13595 NameModifier == OMPD_taskloop) { 13596 CaptureRegion = OMPD_parallel; 13597 break; 13598 } 13599 if (OpenMPVersion <= 45) 13600 break; 13601 if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd) 13602 CaptureRegion = OMPD_taskloop; 13603 break; 13604 case OMPD_parallel_for_simd: 13605 if (OpenMPVersion <= 45) 13606 break; 13607 if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd) 13608 CaptureRegion = OMPD_parallel; 13609 break; 13610 case OMPD_taskloop_simd: 13611 case OMPD_master_taskloop_simd: 13612 if (OpenMPVersion <= 45) 13613 break; 13614 if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd) 13615 CaptureRegion = OMPD_taskloop; 13616 break; 13617 case OMPD_distribute_parallel_for_simd: 13618 if (OpenMPVersion <= 45) 13619 break; 13620 if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd) 13621 CaptureRegion = OMPD_parallel; 13622 break; 13623 case OMPD_target_simd: 13624 if (OpenMPVersion >= 50 && 13625 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) 13626 CaptureRegion = OMPD_target; 13627 break; 13628 case OMPD_teams_distribute_simd: 13629 case OMPD_target_teams_distribute_simd: 13630 if (OpenMPVersion >= 50 && 13631 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) 13632 CaptureRegion = OMPD_teams; 13633 break; 13634 case OMPD_cancel: 13635 case OMPD_parallel: 13636 case OMPD_parallel_master: 13637 case OMPD_parallel_sections: 13638 case OMPD_parallel_for: 13639 case OMPD_target: 13640 case OMPD_target_teams: 13641 case OMPD_target_teams_distribute: 13642 case OMPD_distribute_parallel_for: 13643 case OMPD_task: 13644 case OMPD_taskloop: 13645 case OMPD_master_taskloop: 13646 case OMPD_target_data: 13647 case OMPD_simd: 13648 case OMPD_for_simd: 13649 case OMPD_distribute_simd: 13650 // Do not capture if-clause expressions. 13651 break; 13652 case OMPD_threadprivate: 13653 case OMPD_allocate: 13654 case OMPD_taskyield: 13655 case OMPD_barrier: 13656 case OMPD_taskwait: 13657 case OMPD_cancellation_point: 13658 case OMPD_flush: 13659 case OMPD_depobj: 13660 case OMPD_scan: 13661 case OMPD_declare_reduction: 13662 case OMPD_declare_mapper: 13663 case OMPD_declare_simd: 13664 case OMPD_declare_variant: 13665 case OMPD_begin_declare_variant: 13666 case OMPD_end_declare_variant: 13667 case OMPD_declare_target: 13668 case OMPD_end_declare_target: 13669 case OMPD_loop: 13670 case OMPD_teams: 13671 case OMPD_tile: 13672 case OMPD_unroll: 13673 case OMPD_for: 13674 case OMPD_sections: 13675 case OMPD_section: 13676 case OMPD_single: 13677 case OMPD_master: 13678 case OMPD_masked: 13679 case OMPD_critical: 13680 case OMPD_taskgroup: 13681 case OMPD_distribute: 13682 case OMPD_ordered: 13683 case OMPD_atomic: 13684 case OMPD_teams_distribute: 13685 case OMPD_requires: 13686 case OMPD_metadirective: 13687 llvm_unreachable("Unexpected OpenMP directive with if-clause"); 13688 case OMPD_unknown: 13689 default: 13690 llvm_unreachable("Unknown OpenMP directive"); 13691 } 13692 break; 13693 case OMPC_num_threads: 13694 switch (DKind) { 13695 case OMPD_target_parallel: 13696 case OMPD_target_parallel_for: 13697 case OMPD_target_parallel_for_simd: 13698 CaptureRegion = OMPD_target; 13699 break; 13700 case OMPD_teams_distribute_parallel_for: 13701 case OMPD_teams_distribute_parallel_for_simd: 13702 case OMPD_target_teams_distribute_parallel_for: 13703 case OMPD_target_teams_distribute_parallel_for_simd: 13704 CaptureRegion = OMPD_teams; 13705 break; 13706 case OMPD_parallel: 13707 case OMPD_parallel_master: 13708 case OMPD_parallel_sections: 13709 case OMPD_parallel_for: 13710 case OMPD_parallel_for_simd: 13711 case OMPD_distribute_parallel_for: 13712 case OMPD_distribute_parallel_for_simd: 13713 case OMPD_parallel_master_taskloop: 13714 case OMPD_parallel_master_taskloop_simd: 13715 // Do not capture num_threads-clause expressions. 13716 break; 13717 case OMPD_target_data: 13718 case OMPD_target_enter_data: 13719 case OMPD_target_exit_data: 13720 case OMPD_target_update: 13721 case OMPD_target: 13722 case OMPD_target_simd: 13723 case OMPD_target_teams: 13724 case OMPD_target_teams_distribute: 13725 case OMPD_target_teams_distribute_simd: 13726 case OMPD_cancel: 13727 case OMPD_task: 13728 case OMPD_taskloop: 13729 case OMPD_taskloop_simd: 13730 case OMPD_master_taskloop: 13731 case OMPD_master_taskloop_simd: 13732 case OMPD_threadprivate: 13733 case OMPD_allocate: 13734 case OMPD_taskyield: 13735 case OMPD_barrier: 13736 case OMPD_taskwait: 13737 case OMPD_cancellation_point: 13738 case OMPD_flush: 13739 case OMPD_depobj: 13740 case OMPD_scan: 13741 case OMPD_declare_reduction: 13742 case OMPD_declare_mapper: 13743 case OMPD_declare_simd: 13744 case OMPD_declare_variant: 13745 case OMPD_begin_declare_variant: 13746 case OMPD_end_declare_variant: 13747 case OMPD_declare_target: 13748 case OMPD_end_declare_target: 13749 case OMPD_loop: 13750 case OMPD_teams: 13751 case OMPD_simd: 13752 case OMPD_tile: 13753 case OMPD_unroll: 13754 case OMPD_for: 13755 case OMPD_for_simd: 13756 case OMPD_sections: 13757 case OMPD_section: 13758 case OMPD_single: 13759 case OMPD_master: 13760 case OMPD_masked: 13761 case OMPD_critical: 13762 case OMPD_taskgroup: 13763 case OMPD_distribute: 13764 case OMPD_ordered: 13765 case OMPD_atomic: 13766 case OMPD_distribute_simd: 13767 case OMPD_teams_distribute: 13768 case OMPD_teams_distribute_simd: 13769 case OMPD_requires: 13770 case OMPD_metadirective: 13771 llvm_unreachable("Unexpected OpenMP directive with num_threads-clause"); 13772 case OMPD_unknown: 13773 default: 13774 llvm_unreachable("Unknown OpenMP directive"); 13775 } 13776 break; 13777 case OMPC_num_teams: 13778 switch (DKind) { 13779 case OMPD_target_teams: 13780 case OMPD_target_teams_distribute: 13781 case OMPD_target_teams_distribute_simd: 13782 case OMPD_target_teams_distribute_parallel_for: 13783 case OMPD_target_teams_distribute_parallel_for_simd: 13784 CaptureRegion = OMPD_target; 13785 break; 13786 case OMPD_teams_distribute_parallel_for: 13787 case OMPD_teams_distribute_parallel_for_simd: 13788 case OMPD_teams: 13789 case OMPD_teams_distribute: 13790 case OMPD_teams_distribute_simd: 13791 // Do not capture num_teams-clause expressions. 13792 break; 13793 case OMPD_distribute_parallel_for: 13794 case OMPD_distribute_parallel_for_simd: 13795 case OMPD_task: 13796 case OMPD_taskloop: 13797 case OMPD_taskloop_simd: 13798 case OMPD_master_taskloop: 13799 case OMPD_master_taskloop_simd: 13800 case OMPD_parallel_master_taskloop: 13801 case OMPD_parallel_master_taskloop_simd: 13802 case OMPD_target_data: 13803 case OMPD_target_enter_data: 13804 case OMPD_target_exit_data: 13805 case OMPD_target_update: 13806 case OMPD_cancel: 13807 case OMPD_parallel: 13808 case OMPD_parallel_master: 13809 case OMPD_parallel_sections: 13810 case OMPD_parallel_for: 13811 case OMPD_parallel_for_simd: 13812 case OMPD_target: 13813 case OMPD_target_simd: 13814 case OMPD_target_parallel: 13815 case OMPD_target_parallel_for: 13816 case OMPD_target_parallel_for_simd: 13817 case OMPD_threadprivate: 13818 case OMPD_allocate: 13819 case OMPD_taskyield: 13820 case OMPD_barrier: 13821 case OMPD_taskwait: 13822 case OMPD_cancellation_point: 13823 case OMPD_flush: 13824 case OMPD_depobj: 13825 case OMPD_scan: 13826 case OMPD_declare_reduction: 13827 case OMPD_declare_mapper: 13828 case OMPD_declare_simd: 13829 case OMPD_declare_variant: 13830 case OMPD_begin_declare_variant: 13831 case OMPD_end_declare_variant: 13832 case OMPD_declare_target: 13833 case OMPD_end_declare_target: 13834 case OMPD_loop: 13835 case OMPD_simd: 13836 case OMPD_tile: 13837 case OMPD_unroll: 13838 case OMPD_for: 13839 case OMPD_for_simd: 13840 case OMPD_sections: 13841 case OMPD_section: 13842 case OMPD_single: 13843 case OMPD_master: 13844 case OMPD_masked: 13845 case OMPD_critical: 13846 case OMPD_taskgroup: 13847 case OMPD_distribute: 13848 case OMPD_ordered: 13849 case OMPD_atomic: 13850 case OMPD_distribute_simd: 13851 case OMPD_requires: 13852 case OMPD_metadirective: 13853 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause"); 13854 case OMPD_unknown: 13855 default: 13856 llvm_unreachable("Unknown OpenMP directive"); 13857 } 13858 break; 13859 case OMPC_thread_limit: 13860 switch (DKind) { 13861 case OMPD_target_teams: 13862 case OMPD_target_teams_distribute: 13863 case OMPD_target_teams_distribute_simd: 13864 case OMPD_target_teams_distribute_parallel_for: 13865 case OMPD_target_teams_distribute_parallel_for_simd: 13866 CaptureRegion = OMPD_target; 13867 break; 13868 case OMPD_teams_distribute_parallel_for: 13869 case OMPD_teams_distribute_parallel_for_simd: 13870 case OMPD_teams: 13871 case OMPD_teams_distribute: 13872 case OMPD_teams_distribute_simd: 13873 // Do not capture thread_limit-clause expressions. 13874 break; 13875 case OMPD_distribute_parallel_for: 13876 case OMPD_distribute_parallel_for_simd: 13877 case OMPD_task: 13878 case OMPD_taskloop: 13879 case OMPD_taskloop_simd: 13880 case OMPD_master_taskloop: 13881 case OMPD_master_taskloop_simd: 13882 case OMPD_parallel_master_taskloop: 13883 case OMPD_parallel_master_taskloop_simd: 13884 case OMPD_target_data: 13885 case OMPD_target_enter_data: 13886 case OMPD_target_exit_data: 13887 case OMPD_target_update: 13888 case OMPD_cancel: 13889 case OMPD_parallel: 13890 case OMPD_parallel_master: 13891 case OMPD_parallel_sections: 13892 case OMPD_parallel_for: 13893 case OMPD_parallel_for_simd: 13894 case OMPD_target: 13895 case OMPD_target_simd: 13896 case OMPD_target_parallel: 13897 case OMPD_target_parallel_for: 13898 case OMPD_target_parallel_for_simd: 13899 case OMPD_threadprivate: 13900 case OMPD_allocate: 13901 case OMPD_taskyield: 13902 case OMPD_barrier: 13903 case OMPD_taskwait: 13904 case OMPD_cancellation_point: 13905 case OMPD_flush: 13906 case OMPD_depobj: 13907 case OMPD_scan: 13908 case OMPD_declare_reduction: 13909 case OMPD_declare_mapper: 13910 case OMPD_declare_simd: 13911 case OMPD_declare_variant: 13912 case OMPD_begin_declare_variant: 13913 case OMPD_end_declare_variant: 13914 case OMPD_declare_target: 13915 case OMPD_end_declare_target: 13916 case OMPD_loop: 13917 case OMPD_simd: 13918 case OMPD_tile: 13919 case OMPD_unroll: 13920 case OMPD_for: 13921 case OMPD_for_simd: 13922 case OMPD_sections: 13923 case OMPD_section: 13924 case OMPD_single: 13925 case OMPD_master: 13926 case OMPD_masked: 13927 case OMPD_critical: 13928 case OMPD_taskgroup: 13929 case OMPD_distribute: 13930 case OMPD_ordered: 13931 case OMPD_atomic: 13932 case OMPD_distribute_simd: 13933 case OMPD_requires: 13934 case OMPD_metadirective: 13935 llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause"); 13936 case OMPD_unknown: 13937 default: 13938 llvm_unreachable("Unknown OpenMP directive"); 13939 } 13940 break; 13941 case OMPC_schedule: 13942 switch (DKind) { 13943 case OMPD_parallel_for: 13944 case OMPD_parallel_for_simd: 13945 case OMPD_distribute_parallel_for: 13946 case OMPD_distribute_parallel_for_simd: 13947 case OMPD_teams_distribute_parallel_for: 13948 case OMPD_teams_distribute_parallel_for_simd: 13949 case OMPD_target_parallel_for: 13950 case OMPD_target_parallel_for_simd: 13951 case OMPD_target_teams_distribute_parallel_for: 13952 case OMPD_target_teams_distribute_parallel_for_simd: 13953 CaptureRegion = OMPD_parallel; 13954 break; 13955 case OMPD_for: 13956 case OMPD_for_simd: 13957 // Do not capture schedule-clause expressions. 13958 break; 13959 case OMPD_task: 13960 case OMPD_taskloop: 13961 case OMPD_taskloop_simd: 13962 case OMPD_master_taskloop: 13963 case OMPD_master_taskloop_simd: 13964 case OMPD_parallel_master_taskloop: 13965 case OMPD_parallel_master_taskloop_simd: 13966 case OMPD_target_data: 13967 case OMPD_target_enter_data: 13968 case OMPD_target_exit_data: 13969 case OMPD_target_update: 13970 case OMPD_teams: 13971 case OMPD_teams_distribute: 13972 case OMPD_teams_distribute_simd: 13973 case OMPD_target_teams_distribute: 13974 case OMPD_target_teams_distribute_simd: 13975 case OMPD_target: 13976 case OMPD_target_simd: 13977 case OMPD_target_parallel: 13978 case OMPD_cancel: 13979 case OMPD_parallel: 13980 case OMPD_parallel_master: 13981 case OMPD_parallel_sections: 13982 case OMPD_threadprivate: 13983 case OMPD_allocate: 13984 case OMPD_taskyield: 13985 case OMPD_barrier: 13986 case OMPD_taskwait: 13987 case OMPD_cancellation_point: 13988 case OMPD_flush: 13989 case OMPD_depobj: 13990 case OMPD_scan: 13991 case OMPD_declare_reduction: 13992 case OMPD_declare_mapper: 13993 case OMPD_declare_simd: 13994 case OMPD_declare_variant: 13995 case OMPD_begin_declare_variant: 13996 case OMPD_end_declare_variant: 13997 case OMPD_declare_target: 13998 case OMPD_end_declare_target: 13999 case OMPD_loop: 14000 case OMPD_simd: 14001 case OMPD_tile: 14002 case OMPD_unroll: 14003 case OMPD_sections: 14004 case OMPD_section: 14005 case OMPD_single: 14006 case OMPD_master: 14007 case OMPD_masked: 14008 case OMPD_critical: 14009 case OMPD_taskgroup: 14010 case OMPD_distribute: 14011 case OMPD_ordered: 14012 case OMPD_atomic: 14013 case OMPD_distribute_simd: 14014 case OMPD_target_teams: 14015 case OMPD_requires: 14016 case OMPD_metadirective: 14017 llvm_unreachable("Unexpected OpenMP directive with schedule clause"); 14018 case OMPD_unknown: 14019 default: 14020 llvm_unreachable("Unknown OpenMP directive"); 14021 } 14022 break; 14023 case OMPC_dist_schedule: 14024 switch (DKind) { 14025 case OMPD_teams_distribute_parallel_for: 14026 case OMPD_teams_distribute_parallel_for_simd: 14027 case OMPD_teams_distribute: 14028 case OMPD_teams_distribute_simd: 14029 case OMPD_target_teams_distribute_parallel_for: 14030 case OMPD_target_teams_distribute_parallel_for_simd: 14031 case OMPD_target_teams_distribute: 14032 case OMPD_target_teams_distribute_simd: 14033 CaptureRegion = OMPD_teams; 14034 break; 14035 case OMPD_distribute_parallel_for: 14036 case OMPD_distribute_parallel_for_simd: 14037 case OMPD_distribute: 14038 case OMPD_distribute_simd: 14039 // Do not capture dist_schedule-clause expressions. 14040 break; 14041 case OMPD_parallel_for: 14042 case OMPD_parallel_for_simd: 14043 case OMPD_target_parallel_for_simd: 14044 case OMPD_target_parallel_for: 14045 case OMPD_task: 14046 case OMPD_taskloop: 14047 case OMPD_taskloop_simd: 14048 case OMPD_master_taskloop: 14049 case OMPD_master_taskloop_simd: 14050 case OMPD_parallel_master_taskloop: 14051 case OMPD_parallel_master_taskloop_simd: 14052 case OMPD_target_data: 14053 case OMPD_target_enter_data: 14054 case OMPD_target_exit_data: 14055 case OMPD_target_update: 14056 case OMPD_teams: 14057 case OMPD_target: 14058 case OMPD_target_simd: 14059 case OMPD_target_parallel: 14060 case OMPD_cancel: 14061 case OMPD_parallel: 14062 case OMPD_parallel_master: 14063 case OMPD_parallel_sections: 14064 case OMPD_threadprivate: 14065 case OMPD_allocate: 14066 case OMPD_taskyield: 14067 case OMPD_barrier: 14068 case OMPD_taskwait: 14069 case OMPD_cancellation_point: 14070 case OMPD_flush: 14071 case OMPD_depobj: 14072 case OMPD_scan: 14073 case OMPD_declare_reduction: 14074 case OMPD_declare_mapper: 14075 case OMPD_declare_simd: 14076 case OMPD_declare_variant: 14077 case OMPD_begin_declare_variant: 14078 case OMPD_end_declare_variant: 14079 case OMPD_declare_target: 14080 case OMPD_end_declare_target: 14081 case OMPD_loop: 14082 case OMPD_simd: 14083 case OMPD_tile: 14084 case OMPD_unroll: 14085 case OMPD_for: 14086 case OMPD_for_simd: 14087 case OMPD_sections: 14088 case OMPD_section: 14089 case OMPD_single: 14090 case OMPD_master: 14091 case OMPD_masked: 14092 case OMPD_critical: 14093 case OMPD_taskgroup: 14094 case OMPD_ordered: 14095 case OMPD_atomic: 14096 case OMPD_target_teams: 14097 case OMPD_requires: 14098 case OMPD_metadirective: 14099 llvm_unreachable("Unexpected OpenMP directive with dist_schedule clause"); 14100 case OMPD_unknown: 14101 default: 14102 llvm_unreachable("Unknown OpenMP directive"); 14103 } 14104 break; 14105 case OMPC_device: 14106 switch (DKind) { 14107 case OMPD_target_update: 14108 case OMPD_target_enter_data: 14109 case OMPD_target_exit_data: 14110 case OMPD_target: 14111 case OMPD_target_simd: 14112 case OMPD_target_teams: 14113 case OMPD_target_parallel: 14114 case OMPD_target_teams_distribute: 14115 case OMPD_target_teams_distribute_simd: 14116 case OMPD_target_parallel_for: 14117 case OMPD_target_parallel_for_simd: 14118 case OMPD_target_teams_distribute_parallel_for: 14119 case OMPD_target_teams_distribute_parallel_for_simd: 14120 case OMPD_dispatch: 14121 CaptureRegion = OMPD_task; 14122 break; 14123 case OMPD_target_data: 14124 case OMPD_interop: 14125 // Do not capture device-clause expressions. 14126 break; 14127 case OMPD_teams_distribute_parallel_for: 14128 case OMPD_teams_distribute_parallel_for_simd: 14129 case OMPD_teams: 14130 case OMPD_teams_distribute: 14131 case OMPD_teams_distribute_simd: 14132 case OMPD_distribute_parallel_for: 14133 case OMPD_distribute_parallel_for_simd: 14134 case OMPD_task: 14135 case OMPD_taskloop: 14136 case OMPD_taskloop_simd: 14137 case OMPD_master_taskloop: 14138 case OMPD_master_taskloop_simd: 14139 case OMPD_parallel_master_taskloop: 14140 case OMPD_parallel_master_taskloop_simd: 14141 case OMPD_cancel: 14142 case OMPD_parallel: 14143 case OMPD_parallel_master: 14144 case OMPD_parallel_sections: 14145 case OMPD_parallel_for: 14146 case OMPD_parallel_for_simd: 14147 case OMPD_threadprivate: 14148 case OMPD_allocate: 14149 case OMPD_taskyield: 14150 case OMPD_barrier: 14151 case OMPD_taskwait: 14152 case OMPD_cancellation_point: 14153 case OMPD_flush: 14154 case OMPD_depobj: 14155 case OMPD_scan: 14156 case OMPD_declare_reduction: 14157 case OMPD_declare_mapper: 14158 case OMPD_declare_simd: 14159 case OMPD_declare_variant: 14160 case OMPD_begin_declare_variant: 14161 case OMPD_end_declare_variant: 14162 case OMPD_declare_target: 14163 case OMPD_end_declare_target: 14164 case OMPD_loop: 14165 case OMPD_simd: 14166 case OMPD_tile: 14167 case OMPD_unroll: 14168 case OMPD_for: 14169 case OMPD_for_simd: 14170 case OMPD_sections: 14171 case OMPD_section: 14172 case OMPD_single: 14173 case OMPD_master: 14174 case OMPD_masked: 14175 case OMPD_critical: 14176 case OMPD_taskgroup: 14177 case OMPD_distribute: 14178 case OMPD_ordered: 14179 case OMPD_atomic: 14180 case OMPD_distribute_simd: 14181 case OMPD_requires: 14182 case OMPD_metadirective: 14183 llvm_unreachable("Unexpected OpenMP directive with device-clause"); 14184 case OMPD_unknown: 14185 default: 14186 llvm_unreachable("Unknown OpenMP directive"); 14187 } 14188 break; 14189 case OMPC_grainsize: 14190 case OMPC_num_tasks: 14191 case OMPC_final: 14192 case OMPC_priority: 14193 switch (DKind) { 14194 case OMPD_task: 14195 case OMPD_taskloop: 14196 case OMPD_taskloop_simd: 14197 case OMPD_master_taskloop: 14198 case OMPD_master_taskloop_simd: 14199 break; 14200 case OMPD_parallel_master_taskloop: 14201 case OMPD_parallel_master_taskloop_simd: 14202 CaptureRegion = OMPD_parallel; 14203 break; 14204 case OMPD_target_update: 14205 case OMPD_target_enter_data: 14206 case OMPD_target_exit_data: 14207 case OMPD_target: 14208 case OMPD_target_simd: 14209 case OMPD_target_teams: 14210 case OMPD_target_parallel: 14211 case OMPD_target_teams_distribute: 14212 case OMPD_target_teams_distribute_simd: 14213 case OMPD_target_parallel_for: 14214 case OMPD_target_parallel_for_simd: 14215 case OMPD_target_teams_distribute_parallel_for: 14216 case OMPD_target_teams_distribute_parallel_for_simd: 14217 case OMPD_target_data: 14218 case OMPD_teams_distribute_parallel_for: 14219 case OMPD_teams_distribute_parallel_for_simd: 14220 case OMPD_teams: 14221 case OMPD_teams_distribute: 14222 case OMPD_teams_distribute_simd: 14223 case OMPD_distribute_parallel_for: 14224 case OMPD_distribute_parallel_for_simd: 14225 case OMPD_cancel: 14226 case OMPD_parallel: 14227 case OMPD_parallel_master: 14228 case OMPD_parallel_sections: 14229 case OMPD_parallel_for: 14230 case OMPD_parallel_for_simd: 14231 case OMPD_threadprivate: 14232 case OMPD_allocate: 14233 case OMPD_taskyield: 14234 case OMPD_barrier: 14235 case OMPD_taskwait: 14236 case OMPD_cancellation_point: 14237 case OMPD_flush: 14238 case OMPD_depobj: 14239 case OMPD_scan: 14240 case OMPD_declare_reduction: 14241 case OMPD_declare_mapper: 14242 case OMPD_declare_simd: 14243 case OMPD_declare_variant: 14244 case OMPD_begin_declare_variant: 14245 case OMPD_end_declare_variant: 14246 case OMPD_declare_target: 14247 case OMPD_end_declare_target: 14248 case OMPD_loop: 14249 case OMPD_simd: 14250 case OMPD_tile: 14251 case OMPD_unroll: 14252 case OMPD_for: 14253 case OMPD_for_simd: 14254 case OMPD_sections: 14255 case OMPD_section: 14256 case OMPD_single: 14257 case OMPD_master: 14258 case OMPD_masked: 14259 case OMPD_critical: 14260 case OMPD_taskgroup: 14261 case OMPD_distribute: 14262 case OMPD_ordered: 14263 case OMPD_atomic: 14264 case OMPD_distribute_simd: 14265 case OMPD_requires: 14266 case OMPD_metadirective: 14267 llvm_unreachable("Unexpected OpenMP directive with grainsize-clause"); 14268 case OMPD_unknown: 14269 default: 14270 llvm_unreachable("Unknown OpenMP directive"); 14271 } 14272 break; 14273 case OMPC_novariants: 14274 case OMPC_nocontext: 14275 switch (DKind) { 14276 case OMPD_dispatch: 14277 CaptureRegion = OMPD_task; 14278 break; 14279 default: 14280 llvm_unreachable("Unexpected OpenMP directive"); 14281 } 14282 break; 14283 case OMPC_filter: 14284 // Do not capture filter-clause expressions. 14285 break; 14286 case OMPC_when: 14287 if (DKind == OMPD_metadirective) { 14288 CaptureRegion = OMPD_metadirective; 14289 } else if (DKind == OMPD_unknown) { 14290 llvm_unreachable("Unknown OpenMP directive"); 14291 } else { 14292 llvm_unreachable("Unexpected OpenMP directive with when clause"); 14293 } 14294 break; 14295 case OMPC_firstprivate: 14296 case OMPC_lastprivate: 14297 case OMPC_reduction: 14298 case OMPC_task_reduction: 14299 case OMPC_in_reduction: 14300 case OMPC_linear: 14301 case OMPC_default: 14302 case OMPC_proc_bind: 14303 case OMPC_safelen: 14304 case OMPC_simdlen: 14305 case OMPC_sizes: 14306 case OMPC_allocator: 14307 case OMPC_collapse: 14308 case OMPC_private: 14309 case OMPC_shared: 14310 case OMPC_aligned: 14311 case OMPC_copyin: 14312 case OMPC_copyprivate: 14313 case OMPC_ordered: 14314 case OMPC_nowait: 14315 case OMPC_untied: 14316 case OMPC_mergeable: 14317 case OMPC_threadprivate: 14318 case OMPC_allocate: 14319 case OMPC_flush: 14320 case OMPC_depobj: 14321 case OMPC_read: 14322 case OMPC_write: 14323 case OMPC_update: 14324 case OMPC_capture: 14325 case OMPC_compare: 14326 case OMPC_seq_cst: 14327 case OMPC_acq_rel: 14328 case OMPC_acquire: 14329 case OMPC_release: 14330 case OMPC_relaxed: 14331 case OMPC_depend: 14332 case OMPC_threads: 14333 case OMPC_simd: 14334 case OMPC_map: 14335 case OMPC_nogroup: 14336 case OMPC_hint: 14337 case OMPC_defaultmap: 14338 case OMPC_unknown: 14339 case OMPC_uniform: 14340 case OMPC_to: 14341 case OMPC_from: 14342 case OMPC_use_device_ptr: 14343 case OMPC_use_device_addr: 14344 case OMPC_is_device_ptr: 14345 case OMPC_unified_address: 14346 case OMPC_unified_shared_memory: 14347 case OMPC_reverse_offload: 14348 case OMPC_dynamic_allocators: 14349 case OMPC_atomic_default_mem_order: 14350 case OMPC_device_type: 14351 case OMPC_match: 14352 case OMPC_nontemporal: 14353 case OMPC_order: 14354 case OMPC_destroy: 14355 case OMPC_detach: 14356 case OMPC_inclusive: 14357 case OMPC_exclusive: 14358 case OMPC_uses_allocators: 14359 case OMPC_affinity: 14360 case OMPC_bind: 14361 default: 14362 llvm_unreachable("Unexpected OpenMP clause."); 14363 } 14364 return CaptureRegion; 14365 } 14366 14367 OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier, 14368 Expr *Condition, SourceLocation StartLoc, 14369 SourceLocation LParenLoc, 14370 SourceLocation NameModifierLoc, 14371 SourceLocation ColonLoc, 14372 SourceLocation EndLoc) { 14373 Expr *ValExpr = Condition; 14374 Stmt *HelperValStmt = nullptr; 14375 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 14376 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 14377 !Condition->isInstantiationDependent() && 14378 !Condition->containsUnexpandedParameterPack()) { 14379 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 14380 if (Val.isInvalid()) 14381 return nullptr; 14382 14383 ValExpr = Val.get(); 14384 14385 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 14386 CaptureRegion = getOpenMPCaptureRegionForClause( 14387 DKind, OMPC_if, LangOpts.OpenMP, NameModifier); 14388 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 14389 ValExpr = MakeFullExpr(ValExpr).get(); 14390 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 14391 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 14392 HelperValStmt = buildPreInits(Context, Captures); 14393 } 14394 } 14395 14396 return new (Context) 14397 OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc, 14398 LParenLoc, NameModifierLoc, ColonLoc, EndLoc); 14399 } 14400 14401 OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition, 14402 SourceLocation StartLoc, 14403 SourceLocation LParenLoc, 14404 SourceLocation EndLoc) { 14405 Expr *ValExpr = Condition; 14406 Stmt *HelperValStmt = nullptr; 14407 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 14408 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 14409 !Condition->isInstantiationDependent() && 14410 !Condition->containsUnexpandedParameterPack()) { 14411 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 14412 if (Val.isInvalid()) 14413 return nullptr; 14414 14415 ValExpr = MakeFullExpr(Val.get()).get(); 14416 14417 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 14418 CaptureRegion = 14419 getOpenMPCaptureRegionForClause(DKind, OMPC_final, LangOpts.OpenMP); 14420 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 14421 ValExpr = MakeFullExpr(ValExpr).get(); 14422 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 14423 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 14424 HelperValStmt = buildPreInits(Context, Captures); 14425 } 14426 } 14427 14428 return new (Context) OMPFinalClause(ValExpr, HelperValStmt, CaptureRegion, 14429 StartLoc, LParenLoc, EndLoc); 14430 } 14431 14432 ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc, 14433 Expr *Op) { 14434 if (!Op) 14435 return ExprError(); 14436 14437 class IntConvertDiagnoser : public ICEConvertDiagnoser { 14438 public: 14439 IntConvertDiagnoser() 14440 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {} 14441 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, 14442 QualType T) override { 14443 return S.Diag(Loc, diag::err_omp_not_integral) << T; 14444 } 14445 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc, 14446 QualType T) override { 14447 return S.Diag(Loc, diag::err_omp_incomplete_type) << T; 14448 } 14449 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc, 14450 QualType T, 14451 QualType ConvTy) override { 14452 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy; 14453 } 14454 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv, 14455 QualType ConvTy) override { 14456 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 14457 << ConvTy->isEnumeralType() << ConvTy; 14458 } 14459 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc, 14460 QualType T) override { 14461 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T; 14462 } 14463 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv, 14464 QualType ConvTy) override { 14465 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 14466 << ConvTy->isEnumeralType() << ConvTy; 14467 } 14468 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType, 14469 QualType) override { 14470 llvm_unreachable("conversion functions are permitted"); 14471 } 14472 } ConvertDiagnoser; 14473 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser); 14474 } 14475 14476 static bool 14477 isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef, OpenMPClauseKind CKind, 14478 bool StrictlyPositive, bool BuildCapture = false, 14479 OpenMPDirectiveKind DKind = OMPD_unknown, 14480 OpenMPDirectiveKind *CaptureRegion = nullptr, 14481 Stmt **HelperValStmt = nullptr) { 14482 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() && 14483 !ValExpr->isInstantiationDependent()) { 14484 SourceLocation Loc = ValExpr->getExprLoc(); 14485 ExprResult Value = 14486 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr); 14487 if (Value.isInvalid()) 14488 return false; 14489 14490 ValExpr = Value.get(); 14491 // The expression must evaluate to a non-negative integer value. 14492 if (Optional<llvm::APSInt> Result = 14493 ValExpr->getIntegerConstantExpr(SemaRef.Context)) { 14494 if (Result->isSigned() && 14495 !((!StrictlyPositive && Result->isNonNegative()) || 14496 (StrictlyPositive && Result->isStrictlyPositive()))) { 14497 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause) 14498 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) 14499 << ValExpr->getSourceRange(); 14500 return false; 14501 } 14502 } 14503 if (!BuildCapture) 14504 return true; 14505 *CaptureRegion = 14506 getOpenMPCaptureRegionForClause(DKind, CKind, SemaRef.LangOpts.OpenMP); 14507 if (*CaptureRegion != OMPD_unknown && 14508 !SemaRef.CurContext->isDependentContext()) { 14509 ValExpr = SemaRef.MakeFullExpr(ValExpr).get(); 14510 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 14511 ValExpr = tryBuildCapture(SemaRef, ValExpr, Captures).get(); 14512 *HelperValStmt = buildPreInits(SemaRef.Context, Captures); 14513 } 14514 } 14515 return true; 14516 } 14517 14518 OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads, 14519 SourceLocation StartLoc, 14520 SourceLocation LParenLoc, 14521 SourceLocation EndLoc) { 14522 Expr *ValExpr = NumThreads; 14523 Stmt *HelperValStmt = nullptr; 14524 14525 // OpenMP [2.5, Restrictions] 14526 // The num_threads expression must evaluate to a positive integer value. 14527 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads, 14528 /*StrictlyPositive=*/true)) 14529 return nullptr; 14530 14531 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 14532 OpenMPDirectiveKind CaptureRegion = 14533 getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads, LangOpts.OpenMP); 14534 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 14535 ValExpr = MakeFullExpr(ValExpr).get(); 14536 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 14537 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 14538 HelperValStmt = buildPreInits(Context, Captures); 14539 } 14540 14541 return new (Context) OMPNumThreadsClause( 14542 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc); 14543 } 14544 14545 ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E, 14546 OpenMPClauseKind CKind, 14547 bool StrictlyPositive, 14548 bool SuppressExprDiags) { 14549 if (!E) 14550 return ExprError(); 14551 if (E->isValueDependent() || E->isTypeDependent() || 14552 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 14553 return E; 14554 14555 llvm::APSInt Result; 14556 ExprResult ICE; 14557 if (SuppressExprDiags) { 14558 // Use a custom diagnoser that suppresses 'note' diagnostics about the 14559 // expression. 14560 struct SuppressedDiagnoser : public Sema::VerifyICEDiagnoser { 14561 SuppressedDiagnoser() : VerifyICEDiagnoser(/*Suppress=*/true) {} 14562 Sema::SemaDiagnosticBuilder diagnoseNotICE(Sema &S, 14563 SourceLocation Loc) override { 14564 llvm_unreachable("Diagnostic suppressed"); 14565 } 14566 } Diagnoser; 14567 ICE = VerifyIntegerConstantExpression(E, &Result, Diagnoser, AllowFold); 14568 } else { 14569 ICE = VerifyIntegerConstantExpression(E, &Result, /*FIXME*/ AllowFold); 14570 } 14571 if (ICE.isInvalid()) 14572 return ExprError(); 14573 14574 if ((StrictlyPositive && !Result.isStrictlyPositive()) || 14575 (!StrictlyPositive && !Result.isNonNegative())) { 14576 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause) 14577 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) 14578 << E->getSourceRange(); 14579 return ExprError(); 14580 } 14581 if ((CKind == OMPC_aligned || CKind == OMPC_align) && !Result.isPowerOf2()) { 14582 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two) 14583 << E->getSourceRange(); 14584 return ExprError(); 14585 } 14586 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1) 14587 DSAStack->setAssociatedLoops(Result.getExtValue()); 14588 else if (CKind == OMPC_ordered) 14589 DSAStack->setAssociatedLoops(Result.getExtValue()); 14590 return ICE; 14591 } 14592 14593 OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc, 14594 SourceLocation LParenLoc, 14595 SourceLocation EndLoc) { 14596 // OpenMP [2.8.1, simd construct, Description] 14597 // The parameter of the safelen clause must be a constant 14598 // positive integer expression. 14599 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen); 14600 if (Safelen.isInvalid()) 14601 return nullptr; 14602 return new (Context) 14603 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc); 14604 } 14605 14606 OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc, 14607 SourceLocation LParenLoc, 14608 SourceLocation EndLoc) { 14609 // OpenMP [2.8.1, simd construct, Description] 14610 // The parameter of the simdlen clause must be a constant 14611 // positive integer expression. 14612 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen); 14613 if (Simdlen.isInvalid()) 14614 return nullptr; 14615 return new (Context) 14616 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc); 14617 } 14618 14619 /// Tries to find omp_allocator_handle_t type. 14620 static bool findOMPAllocatorHandleT(Sema &S, SourceLocation Loc, 14621 DSAStackTy *Stack) { 14622 QualType OMPAllocatorHandleT = Stack->getOMPAllocatorHandleT(); 14623 if (!OMPAllocatorHandleT.isNull()) 14624 return true; 14625 // Build the predefined allocator expressions. 14626 bool ErrorFound = false; 14627 for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) { 14628 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I); 14629 StringRef Allocator = 14630 OMPAllocateDeclAttr::ConvertAllocatorTypeTyToStr(AllocatorKind); 14631 DeclarationName AllocatorName = &S.getASTContext().Idents.get(Allocator); 14632 auto *VD = dyn_cast_or_null<ValueDecl>( 14633 S.LookupSingleName(S.TUScope, AllocatorName, Loc, Sema::LookupAnyName)); 14634 if (!VD) { 14635 ErrorFound = true; 14636 break; 14637 } 14638 QualType AllocatorType = 14639 VD->getType().getNonLValueExprType(S.getASTContext()); 14640 ExprResult Res = S.BuildDeclRefExpr(VD, AllocatorType, VK_LValue, Loc); 14641 if (!Res.isUsable()) { 14642 ErrorFound = true; 14643 break; 14644 } 14645 if (OMPAllocatorHandleT.isNull()) 14646 OMPAllocatorHandleT = AllocatorType; 14647 if (!S.getASTContext().hasSameType(OMPAllocatorHandleT, AllocatorType)) { 14648 ErrorFound = true; 14649 break; 14650 } 14651 Stack->setAllocator(AllocatorKind, Res.get()); 14652 } 14653 if (ErrorFound) { 14654 S.Diag(Loc, diag::err_omp_implied_type_not_found) 14655 << "omp_allocator_handle_t"; 14656 return false; 14657 } 14658 OMPAllocatorHandleT.addConst(); 14659 Stack->setOMPAllocatorHandleT(OMPAllocatorHandleT); 14660 return true; 14661 } 14662 14663 OMPClause *Sema::ActOnOpenMPAllocatorClause(Expr *A, SourceLocation StartLoc, 14664 SourceLocation LParenLoc, 14665 SourceLocation EndLoc) { 14666 // OpenMP [2.11.3, allocate Directive, Description] 14667 // allocator is an expression of omp_allocator_handle_t type. 14668 if (!findOMPAllocatorHandleT(*this, A->getExprLoc(), DSAStack)) 14669 return nullptr; 14670 14671 ExprResult Allocator = DefaultLvalueConversion(A); 14672 if (Allocator.isInvalid()) 14673 return nullptr; 14674 Allocator = PerformImplicitConversion(Allocator.get(), 14675 DSAStack->getOMPAllocatorHandleT(), 14676 Sema::AA_Initializing, 14677 /*AllowExplicit=*/true); 14678 if (Allocator.isInvalid()) 14679 return nullptr; 14680 return new (Context) 14681 OMPAllocatorClause(Allocator.get(), StartLoc, LParenLoc, EndLoc); 14682 } 14683 14684 OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops, 14685 SourceLocation StartLoc, 14686 SourceLocation LParenLoc, 14687 SourceLocation EndLoc) { 14688 // OpenMP [2.7.1, loop construct, Description] 14689 // OpenMP [2.8.1, simd construct, Description] 14690 // OpenMP [2.9.6, distribute construct, Description] 14691 // The parameter of the collapse clause must be a constant 14692 // positive integer expression. 14693 ExprResult NumForLoopsResult = 14694 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse); 14695 if (NumForLoopsResult.isInvalid()) 14696 return nullptr; 14697 return new (Context) 14698 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc); 14699 } 14700 14701 OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc, 14702 SourceLocation EndLoc, 14703 SourceLocation LParenLoc, 14704 Expr *NumForLoops) { 14705 // OpenMP [2.7.1, loop construct, Description] 14706 // OpenMP [2.8.1, simd construct, Description] 14707 // OpenMP [2.9.6, distribute construct, Description] 14708 // The parameter of the ordered clause must be a constant 14709 // positive integer expression if any. 14710 if (NumForLoops && LParenLoc.isValid()) { 14711 ExprResult NumForLoopsResult = 14712 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered); 14713 if (NumForLoopsResult.isInvalid()) 14714 return nullptr; 14715 NumForLoops = NumForLoopsResult.get(); 14716 } else { 14717 NumForLoops = nullptr; 14718 } 14719 auto *Clause = OMPOrderedClause::Create( 14720 Context, NumForLoops, NumForLoops ? DSAStack->getAssociatedLoops() : 0, 14721 StartLoc, LParenLoc, EndLoc); 14722 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops, Clause); 14723 return Clause; 14724 } 14725 14726 OMPClause *Sema::ActOnOpenMPSimpleClause( 14727 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc, 14728 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 14729 OMPClause *Res = nullptr; 14730 switch (Kind) { 14731 case OMPC_default: 14732 Res = ActOnOpenMPDefaultClause(static_cast<DefaultKind>(Argument), 14733 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 14734 break; 14735 case OMPC_proc_bind: 14736 Res = ActOnOpenMPProcBindClause(static_cast<ProcBindKind>(Argument), 14737 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 14738 break; 14739 case OMPC_atomic_default_mem_order: 14740 Res = ActOnOpenMPAtomicDefaultMemOrderClause( 14741 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Argument), 14742 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 14743 break; 14744 case OMPC_order: 14745 Res = ActOnOpenMPOrderClause(static_cast<OpenMPOrderClauseKind>(Argument), 14746 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 14747 break; 14748 case OMPC_update: 14749 Res = ActOnOpenMPUpdateClause(static_cast<OpenMPDependClauseKind>(Argument), 14750 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 14751 break; 14752 case OMPC_bind: 14753 Res = ActOnOpenMPBindClause(static_cast<OpenMPBindClauseKind>(Argument), 14754 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 14755 break; 14756 case OMPC_if: 14757 case OMPC_final: 14758 case OMPC_num_threads: 14759 case OMPC_safelen: 14760 case OMPC_simdlen: 14761 case OMPC_sizes: 14762 case OMPC_allocator: 14763 case OMPC_collapse: 14764 case OMPC_schedule: 14765 case OMPC_private: 14766 case OMPC_firstprivate: 14767 case OMPC_lastprivate: 14768 case OMPC_shared: 14769 case OMPC_reduction: 14770 case OMPC_task_reduction: 14771 case OMPC_in_reduction: 14772 case OMPC_linear: 14773 case OMPC_aligned: 14774 case OMPC_copyin: 14775 case OMPC_copyprivate: 14776 case OMPC_ordered: 14777 case OMPC_nowait: 14778 case OMPC_untied: 14779 case OMPC_mergeable: 14780 case OMPC_threadprivate: 14781 case OMPC_allocate: 14782 case OMPC_flush: 14783 case OMPC_depobj: 14784 case OMPC_read: 14785 case OMPC_write: 14786 case OMPC_capture: 14787 case OMPC_compare: 14788 case OMPC_seq_cst: 14789 case OMPC_acq_rel: 14790 case OMPC_acquire: 14791 case OMPC_release: 14792 case OMPC_relaxed: 14793 case OMPC_depend: 14794 case OMPC_device: 14795 case OMPC_threads: 14796 case OMPC_simd: 14797 case OMPC_map: 14798 case OMPC_num_teams: 14799 case OMPC_thread_limit: 14800 case OMPC_priority: 14801 case OMPC_grainsize: 14802 case OMPC_nogroup: 14803 case OMPC_num_tasks: 14804 case OMPC_hint: 14805 case OMPC_dist_schedule: 14806 case OMPC_defaultmap: 14807 case OMPC_unknown: 14808 case OMPC_uniform: 14809 case OMPC_to: 14810 case OMPC_from: 14811 case OMPC_use_device_ptr: 14812 case OMPC_use_device_addr: 14813 case OMPC_is_device_ptr: 14814 case OMPC_unified_address: 14815 case OMPC_unified_shared_memory: 14816 case OMPC_reverse_offload: 14817 case OMPC_dynamic_allocators: 14818 case OMPC_device_type: 14819 case OMPC_match: 14820 case OMPC_nontemporal: 14821 case OMPC_destroy: 14822 case OMPC_novariants: 14823 case OMPC_nocontext: 14824 case OMPC_detach: 14825 case OMPC_inclusive: 14826 case OMPC_exclusive: 14827 case OMPC_uses_allocators: 14828 case OMPC_affinity: 14829 case OMPC_when: 14830 default: 14831 llvm_unreachable("Clause is not allowed."); 14832 } 14833 return Res; 14834 } 14835 14836 static std::string 14837 getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last, 14838 ArrayRef<unsigned> Exclude = llvm::None) { 14839 SmallString<256> Buffer; 14840 llvm::raw_svector_ostream Out(Buffer); 14841 unsigned Skipped = Exclude.size(); 14842 auto S = Exclude.begin(), E = Exclude.end(); 14843 for (unsigned I = First; I < Last; ++I) { 14844 if (std::find(S, E, I) != E) { 14845 --Skipped; 14846 continue; 14847 } 14848 Out << "'" << getOpenMPSimpleClauseTypeName(K, I) << "'"; 14849 if (I + Skipped + 2 == Last) 14850 Out << " or "; 14851 else if (I + Skipped + 1 != Last) 14852 Out << ", "; 14853 } 14854 return std::string(Out.str()); 14855 } 14856 14857 OMPClause *Sema::ActOnOpenMPDefaultClause(DefaultKind Kind, 14858 SourceLocation KindKwLoc, 14859 SourceLocation StartLoc, 14860 SourceLocation LParenLoc, 14861 SourceLocation EndLoc) { 14862 if (Kind == OMP_DEFAULT_unknown) { 14863 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 14864 << getListOfPossibleValues(OMPC_default, /*First=*/0, 14865 /*Last=*/unsigned(OMP_DEFAULT_unknown)) 14866 << getOpenMPClauseName(OMPC_default); 14867 return nullptr; 14868 } 14869 14870 switch (Kind) { 14871 case OMP_DEFAULT_none: 14872 DSAStack->setDefaultDSANone(KindKwLoc); 14873 break; 14874 case OMP_DEFAULT_shared: 14875 DSAStack->setDefaultDSAShared(KindKwLoc); 14876 break; 14877 case OMP_DEFAULT_firstprivate: 14878 DSAStack->setDefaultDSAFirstPrivate(KindKwLoc); 14879 break; 14880 default: 14881 llvm_unreachable("DSA unexpected in OpenMP default clause"); 14882 } 14883 14884 return new (Context) 14885 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 14886 } 14887 14888 OMPClause *Sema::ActOnOpenMPProcBindClause(ProcBindKind Kind, 14889 SourceLocation KindKwLoc, 14890 SourceLocation StartLoc, 14891 SourceLocation LParenLoc, 14892 SourceLocation EndLoc) { 14893 if (Kind == OMP_PROC_BIND_unknown) { 14894 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 14895 << getListOfPossibleValues(OMPC_proc_bind, 14896 /*First=*/unsigned(OMP_PROC_BIND_master), 14897 /*Last=*/ 14898 unsigned(LangOpts.OpenMP > 50 14899 ? OMP_PROC_BIND_primary 14900 : OMP_PROC_BIND_spread) + 14901 1) 14902 << getOpenMPClauseName(OMPC_proc_bind); 14903 return nullptr; 14904 } 14905 if (Kind == OMP_PROC_BIND_primary && LangOpts.OpenMP < 51) 14906 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 14907 << getListOfPossibleValues(OMPC_proc_bind, 14908 /*First=*/unsigned(OMP_PROC_BIND_master), 14909 /*Last=*/ 14910 unsigned(OMP_PROC_BIND_spread) + 1) 14911 << getOpenMPClauseName(OMPC_proc_bind); 14912 return new (Context) 14913 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 14914 } 14915 14916 OMPClause *Sema::ActOnOpenMPAtomicDefaultMemOrderClause( 14917 OpenMPAtomicDefaultMemOrderClauseKind Kind, SourceLocation KindKwLoc, 14918 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 14919 if (Kind == OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) { 14920 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 14921 << getListOfPossibleValues( 14922 OMPC_atomic_default_mem_order, /*First=*/0, 14923 /*Last=*/OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) 14924 << getOpenMPClauseName(OMPC_atomic_default_mem_order); 14925 return nullptr; 14926 } 14927 return new (Context) OMPAtomicDefaultMemOrderClause(Kind, KindKwLoc, StartLoc, 14928 LParenLoc, EndLoc); 14929 } 14930 14931 OMPClause *Sema::ActOnOpenMPOrderClause(OpenMPOrderClauseKind Kind, 14932 SourceLocation KindKwLoc, 14933 SourceLocation StartLoc, 14934 SourceLocation LParenLoc, 14935 SourceLocation EndLoc) { 14936 if (Kind == OMPC_ORDER_unknown) { 14937 static_assert(OMPC_ORDER_unknown > 0, 14938 "OMPC_ORDER_unknown not greater than 0"); 14939 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 14940 << getListOfPossibleValues(OMPC_order, /*First=*/0, 14941 /*Last=*/OMPC_ORDER_unknown) 14942 << getOpenMPClauseName(OMPC_order); 14943 return nullptr; 14944 } 14945 return new (Context) 14946 OMPOrderClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 14947 } 14948 14949 OMPClause *Sema::ActOnOpenMPUpdateClause(OpenMPDependClauseKind Kind, 14950 SourceLocation KindKwLoc, 14951 SourceLocation StartLoc, 14952 SourceLocation LParenLoc, 14953 SourceLocation EndLoc) { 14954 if (Kind == OMPC_DEPEND_unknown || Kind == OMPC_DEPEND_source || 14955 Kind == OMPC_DEPEND_sink || Kind == OMPC_DEPEND_depobj) { 14956 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink, 14957 OMPC_DEPEND_depobj}; 14958 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 14959 << getListOfPossibleValues(OMPC_depend, /*First=*/0, 14960 /*Last=*/OMPC_DEPEND_unknown, Except) 14961 << getOpenMPClauseName(OMPC_update); 14962 return nullptr; 14963 } 14964 return OMPUpdateClause::Create(Context, StartLoc, LParenLoc, KindKwLoc, Kind, 14965 EndLoc); 14966 } 14967 14968 OMPClause *Sema::ActOnOpenMPSizesClause(ArrayRef<Expr *> SizeExprs, 14969 SourceLocation StartLoc, 14970 SourceLocation LParenLoc, 14971 SourceLocation EndLoc) { 14972 for (Expr *SizeExpr : SizeExprs) { 14973 ExprResult NumForLoopsResult = VerifyPositiveIntegerConstantInClause( 14974 SizeExpr, OMPC_sizes, /*StrictlyPositive=*/true); 14975 if (!NumForLoopsResult.isUsable()) 14976 return nullptr; 14977 } 14978 14979 DSAStack->setAssociatedLoops(SizeExprs.size()); 14980 return OMPSizesClause::Create(Context, StartLoc, LParenLoc, EndLoc, 14981 SizeExprs); 14982 } 14983 14984 OMPClause *Sema::ActOnOpenMPFullClause(SourceLocation StartLoc, 14985 SourceLocation EndLoc) { 14986 return OMPFullClause::Create(Context, StartLoc, EndLoc); 14987 } 14988 14989 OMPClause *Sema::ActOnOpenMPPartialClause(Expr *FactorExpr, 14990 SourceLocation StartLoc, 14991 SourceLocation LParenLoc, 14992 SourceLocation EndLoc) { 14993 if (FactorExpr) { 14994 // If an argument is specified, it must be a constant (or an unevaluated 14995 // template expression). 14996 ExprResult FactorResult = VerifyPositiveIntegerConstantInClause( 14997 FactorExpr, OMPC_partial, /*StrictlyPositive=*/true); 14998 if (FactorResult.isInvalid()) 14999 return nullptr; 15000 FactorExpr = FactorResult.get(); 15001 } 15002 15003 return OMPPartialClause::Create(Context, StartLoc, LParenLoc, EndLoc, 15004 FactorExpr); 15005 } 15006 15007 OMPClause *Sema::ActOnOpenMPAlignClause(Expr *A, SourceLocation StartLoc, 15008 SourceLocation LParenLoc, 15009 SourceLocation EndLoc) { 15010 ExprResult AlignVal; 15011 AlignVal = VerifyPositiveIntegerConstantInClause(A, OMPC_align); 15012 if (AlignVal.isInvalid()) 15013 return nullptr; 15014 return OMPAlignClause::Create(Context, AlignVal.get(), StartLoc, LParenLoc, 15015 EndLoc); 15016 } 15017 15018 OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause( 15019 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr, 15020 SourceLocation StartLoc, SourceLocation LParenLoc, 15021 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc, 15022 SourceLocation EndLoc) { 15023 OMPClause *Res = nullptr; 15024 switch (Kind) { 15025 case OMPC_schedule: 15026 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements }; 15027 assert(Argument.size() == NumberOfElements && 15028 ArgumentLoc.size() == NumberOfElements); 15029 Res = ActOnOpenMPScheduleClause( 15030 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]), 15031 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]), 15032 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr, 15033 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2], 15034 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc); 15035 break; 15036 case OMPC_if: 15037 assert(Argument.size() == 1 && ArgumentLoc.size() == 1); 15038 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()), 15039 Expr, StartLoc, LParenLoc, ArgumentLoc.back(), 15040 DelimLoc, EndLoc); 15041 break; 15042 case OMPC_dist_schedule: 15043 Res = ActOnOpenMPDistScheduleClause( 15044 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr, 15045 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc); 15046 break; 15047 case OMPC_defaultmap: 15048 enum { Modifier, DefaultmapKind }; 15049 Res = ActOnOpenMPDefaultmapClause( 15050 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]), 15051 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]), 15052 StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind], 15053 EndLoc); 15054 break; 15055 case OMPC_device: 15056 assert(Argument.size() == 1 && ArgumentLoc.size() == 1); 15057 Res = ActOnOpenMPDeviceClause( 15058 static_cast<OpenMPDeviceClauseModifier>(Argument.back()), Expr, 15059 StartLoc, LParenLoc, ArgumentLoc.back(), EndLoc); 15060 break; 15061 case OMPC_final: 15062 case OMPC_num_threads: 15063 case OMPC_safelen: 15064 case OMPC_simdlen: 15065 case OMPC_sizes: 15066 case OMPC_allocator: 15067 case OMPC_collapse: 15068 case OMPC_default: 15069 case OMPC_proc_bind: 15070 case OMPC_private: 15071 case OMPC_firstprivate: 15072 case OMPC_lastprivate: 15073 case OMPC_shared: 15074 case OMPC_reduction: 15075 case OMPC_task_reduction: 15076 case OMPC_in_reduction: 15077 case OMPC_linear: 15078 case OMPC_aligned: 15079 case OMPC_copyin: 15080 case OMPC_copyprivate: 15081 case OMPC_ordered: 15082 case OMPC_nowait: 15083 case OMPC_untied: 15084 case OMPC_mergeable: 15085 case OMPC_threadprivate: 15086 case OMPC_allocate: 15087 case OMPC_flush: 15088 case OMPC_depobj: 15089 case OMPC_read: 15090 case OMPC_write: 15091 case OMPC_update: 15092 case OMPC_capture: 15093 case OMPC_compare: 15094 case OMPC_seq_cst: 15095 case OMPC_acq_rel: 15096 case OMPC_acquire: 15097 case OMPC_release: 15098 case OMPC_relaxed: 15099 case OMPC_depend: 15100 case OMPC_threads: 15101 case OMPC_simd: 15102 case OMPC_map: 15103 case OMPC_num_teams: 15104 case OMPC_thread_limit: 15105 case OMPC_priority: 15106 case OMPC_grainsize: 15107 case OMPC_nogroup: 15108 case OMPC_num_tasks: 15109 case OMPC_hint: 15110 case OMPC_unknown: 15111 case OMPC_uniform: 15112 case OMPC_to: 15113 case OMPC_from: 15114 case OMPC_use_device_ptr: 15115 case OMPC_use_device_addr: 15116 case OMPC_is_device_ptr: 15117 case OMPC_unified_address: 15118 case OMPC_unified_shared_memory: 15119 case OMPC_reverse_offload: 15120 case OMPC_dynamic_allocators: 15121 case OMPC_atomic_default_mem_order: 15122 case OMPC_device_type: 15123 case OMPC_match: 15124 case OMPC_nontemporal: 15125 case OMPC_order: 15126 case OMPC_destroy: 15127 case OMPC_novariants: 15128 case OMPC_nocontext: 15129 case OMPC_detach: 15130 case OMPC_inclusive: 15131 case OMPC_exclusive: 15132 case OMPC_uses_allocators: 15133 case OMPC_affinity: 15134 case OMPC_when: 15135 case OMPC_bind: 15136 default: 15137 llvm_unreachable("Clause is not allowed."); 15138 } 15139 return Res; 15140 } 15141 15142 static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1, 15143 OpenMPScheduleClauseModifier M2, 15144 SourceLocation M1Loc, SourceLocation M2Loc) { 15145 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) { 15146 SmallVector<unsigned, 2> Excluded; 15147 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown) 15148 Excluded.push_back(M2); 15149 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) 15150 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic); 15151 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic) 15152 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic); 15153 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value) 15154 << getListOfPossibleValues(OMPC_schedule, 15155 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1, 15156 /*Last=*/OMPC_SCHEDULE_MODIFIER_last, 15157 Excluded) 15158 << getOpenMPClauseName(OMPC_schedule); 15159 return true; 15160 } 15161 return false; 15162 } 15163 15164 OMPClause *Sema::ActOnOpenMPScheduleClause( 15165 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2, 15166 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, 15167 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc, 15168 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) { 15169 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) || 15170 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc)) 15171 return nullptr; 15172 // OpenMP, 2.7.1, Loop Construct, Restrictions 15173 // Either the monotonic modifier or the nonmonotonic modifier can be specified 15174 // but not both. 15175 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) || 15176 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic && 15177 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) || 15178 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic && 15179 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) { 15180 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier) 15181 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2) 15182 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1); 15183 return nullptr; 15184 } 15185 if (Kind == OMPC_SCHEDULE_unknown) { 15186 std::string Values; 15187 if (M1Loc.isInvalid() && M2Loc.isInvalid()) { 15188 unsigned Exclude[] = {OMPC_SCHEDULE_unknown}; 15189 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0, 15190 /*Last=*/OMPC_SCHEDULE_MODIFIER_last, 15191 Exclude); 15192 } else { 15193 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0, 15194 /*Last=*/OMPC_SCHEDULE_unknown); 15195 } 15196 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 15197 << Values << getOpenMPClauseName(OMPC_schedule); 15198 return nullptr; 15199 } 15200 // OpenMP, 2.7.1, Loop Construct, Restrictions 15201 // The nonmonotonic modifier can only be specified with schedule(dynamic) or 15202 // schedule(guided). 15203 // OpenMP 5.0 does not have this restriction. 15204 if (LangOpts.OpenMP < 50 && 15205 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic || 15206 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) && 15207 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) { 15208 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc, 15209 diag::err_omp_schedule_nonmonotonic_static); 15210 return nullptr; 15211 } 15212 Expr *ValExpr = ChunkSize; 15213 Stmt *HelperValStmt = nullptr; 15214 if (ChunkSize) { 15215 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && 15216 !ChunkSize->isInstantiationDependent() && 15217 !ChunkSize->containsUnexpandedParameterPack()) { 15218 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc(); 15219 ExprResult Val = 15220 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); 15221 if (Val.isInvalid()) 15222 return nullptr; 15223 15224 ValExpr = Val.get(); 15225 15226 // OpenMP [2.7.1, Restrictions] 15227 // chunk_size must be a loop invariant integer expression with a positive 15228 // value. 15229 if (Optional<llvm::APSInt> Result = 15230 ValExpr->getIntegerConstantExpr(Context)) { 15231 if (Result->isSigned() && !Result->isStrictlyPositive()) { 15232 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) 15233 << "schedule" << 1 << ChunkSize->getSourceRange(); 15234 return nullptr; 15235 } 15236 } else if (getOpenMPCaptureRegionForClause( 15237 DSAStack->getCurrentDirective(), OMPC_schedule, 15238 LangOpts.OpenMP) != OMPD_unknown && 15239 !CurContext->isDependentContext()) { 15240 ValExpr = MakeFullExpr(ValExpr).get(); 15241 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 15242 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 15243 HelperValStmt = buildPreInits(Context, Captures); 15244 } 15245 } 15246 } 15247 15248 return new (Context) 15249 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind, 15250 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc); 15251 } 15252 15253 OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind, 15254 SourceLocation StartLoc, 15255 SourceLocation EndLoc) { 15256 OMPClause *Res = nullptr; 15257 switch (Kind) { 15258 case OMPC_ordered: 15259 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc); 15260 break; 15261 case OMPC_nowait: 15262 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc); 15263 break; 15264 case OMPC_untied: 15265 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc); 15266 break; 15267 case OMPC_mergeable: 15268 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc); 15269 break; 15270 case OMPC_read: 15271 Res = ActOnOpenMPReadClause(StartLoc, EndLoc); 15272 break; 15273 case OMPC_write: 15274 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc); 15275 break; 15276 case OMPC_update: 15277 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc); 15278 break; 15279 case OMPC_capture: 15280 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc); 15281 break; 15282 case OMPC_compare: 15283 Res = ActOnOpenMPCompareClause(StartLoc, EndLoc); 15284 break; 15285 case OMPC_seq_cst: 15286 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc); 15287 break; 15288 case OMPC_acq_rel: 15289 Res = ActOnOpenMPAcqRelClause(StartLoc, EndLoc); 15290 break; 15291 case OMPC_acquire: 15292 Res = ActOnOpenMPAcquireClause(StartLoc, EndLoc); 15293 break; 15294 case OMPC_release: 15295 Res = ActOnOpenMPReleaseClause(StartLoc, EndLoc); 15296 break; 15297 case OMPC_relaxed: 15298 Res = ActOnOpenMPRelaxedClause(StartLoc, EndLoc); 15299 break; 15300 case OMPC_threads: 15301 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc); 15302 break; 15303 case OMPC_simd: 15304 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc); 15305 break; 15306 case OMPC_nogroup: 15307 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc); 15308 break; 15309 case OMPC_unified_address: 15310 Res = ActOnOpenMPUnifiedAddressClause(StartLoc, EndLoc); 15311 break; 15312 case OMPC_unified_shared_memory: 15313 Res = ActOnOpenMPUnifiedSharedMemoryClause(StartLoc, EndLoc); 15314 break; 15315 case OMPC_reverse_offload: 15316 Res = ActOnOpenMPReverseOffloadClause(StartLoc, EndLoc); 15317 break; 15318 case OMPC_dynamic_allocators: 15319 Res = ActOnOpenMPDynamicAllocatorsClause(StartLoc, EndLoc); 15320 break; 15321 case OMPC_destroy: 15322 Res = ActOnOpenMPDestroyClause(/*InteropVar=*/nullptr, StartLoc, 15323 /*LParenLoc=*/SourceLocation(), 15324 /*VarLoc=*/SourceLocation(), EndLoc); 15325 break; 15326 case OMPC_full: 15327 Res = ActOnOpenMPFullClause(StartLoc, EndLoc); 15328 break; 15329 case OMPC_partial: 15330 Res = ActOnOpenMPPartialClause(nullptr, StartLoc, /*LParenLoc=*/{}, EndLoc); 15331 break; 15332 case OMPC_if: 15333 case OMPC_final: 15334 case OMPC_num_threads: 15335 case OMPC_safelen: 15336 case OMPC_simdlen: 15337 case OMPC_sizes: 15338 case OMPC_allocator: 15339 case OMPC_collapse: 15340 case OMPC_schedule: 15341 case OMPC_private: 15342 case OMPC_firstprivate: 15343 case OMPC_lastprivate: 15344 case OMPC_shared: 15345 case OMPC_reduction: 15346 case OMPC_task_reduction: 15347 case OMPC_in_reduction: 15348 case OMPC_linear: 15349 case OMPC_aligned: 15350 case OMPC_copyin: 15351 case OMPC_copyprivate: 15352 case OMPC_default: 15353 case OMPC_proc_bind: 15354 case OMPC_threadprivate: 15355 case OMPC_allocate: 15356 case OMPC_flush: 15357 case OMPC_depobj: 15358 case OMPC_depend: 15359 case OMPC_device: 15360 case OMPC_map: 15361 case OMPC_num_teams: 15362 case OMPC_thread_limit: 15363 case OMPC_priority: 15364 case OMPC_grainsize: 15365 case OMPC_num_tasks: 15366 case OMPC_hint: 15367 case OMPC_dist_schedule: 15368 case OMPC_defaultmap: 15369 case OMPC_unknown: 15370 case OMPC_uniform: 15371 case OMPC_to: 15372 case OMPC_from: 15373 case OMPC_use_device_ptr: 15374 case OMPC_use_device_addr: 15375 case OMPC_is_device_ptr: 15376 case OMPC_atomic_default_mem_order: 15377 case OMPC_device_type: 15378 case OMPC_match: 15379 case OMPC_nontemporal: 15380 case OMPC_order: 15381 case OMPC_novariants: 15382 case OMPC_nocontext: 15383 case OMPC_detach: 15384 case OMPC_inclusive: 15385 case OMPC_exclusive: 15386 case OMPC_uses_allocators: 15387 case OMPC_affinity: 15388 case OMPC_when: 15389 default: 15390 llvm_unreachable("Clause is not allowed."); 15391 } 15392 return Res; 15393 } 15394 15395 OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc, 15396 SourceLocation EndLoc) { 15397 DSAStack->setNowaitRegion(); 15398 return new (Context) OMPNowaitClause(StartLoc, EndLoc); 15399 } 15400 15401 OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc, 15402 SourceLocation EndLoc) { 15403 return new (Context) OMPUntiedClause(StartLoc, EndLoc); 15404 } 15405 15406 OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc, 15407 SourceLocation EndLoc) { 15408 return new (Context) OMPMergeableClause(StartLoc, EndLoc); 15409 } 15410 15411 OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc, 15412 SourceLocation EndLoc) { 15413 return new (Context) OMPReadClause(StartLoc, EndLoc); 15414 } 15415 15416 OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc, 15417 SourceLocation EndLoc) { 15418 return new (Context) OMPWriteClause(StartLoc, EndLoc); 15419 } 15420 15421 OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc, 15422 SourceLocation EndLoc) { 15423 return OMPUpdateClause::Create(Context, StartLoc, EndLoc); 15424 } 15425 15426 OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc, 15427 SourceLocation EndLoc) { 15428 return new (Context) OMPCaptureClause(StartLoc, EndLoc); 15429 } 15430 15431 OMPClause *Sema::ActOnOpenMPCompareClause(SourceLocation StartLoc, 15432 SourceLocation EndLoc) { 15433 return new (Context) OMPCompareClause(StartLoc, EndLoc); 15434 } 15435 15436 OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc, 15437 SourceLocation EndLoc) { 15438 return new (Context) OMPSeqCstClause(StartLoc, EndLoc); 15439 } 15440 15441 OMPClause *Sema::ActOnOpenMPAcqRelClause(SourceLocation StartLoc, 15442 SourceLocation EndLoc) { 15443 return new (Context) OMPAcqRelClause(StartLoc, EndLoc); 15444 } 15445 15446 OMPClause *Sema::ActOnOpenMPAcquireClause(SourceLocation StartLoc, 15447 SourceLocation EndLoc) { 15448 return new (Context) OMPAcquireClause(StartLoc, EndLoc); 15449 } 15450 15451 OMPClause *Sema::ActOnOpenMPReleaseClause(SourceLocation StartLoc, 15452 SourceLocation EndLoc) { 15453 return new (Context) OMPReleaseClause(StartLoc, EndLoc); 15454 } 15455 15456 OMPClause *Sema::ActOnOpenMPRelaxedClause(SourceLocation StartLoc, 15457 SourceLocation EndLoc) { 15458 return new (Context) OMPRelaxedClause(StartLoc, EndLoc); 15459 } 15460 15461 OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc, 15462 SourceLocation EndLoc) { 15463 return new (Context) OMPThreadsClause(StartLoc, EndLoc); 15464 } 15465 15466 OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc, 15467 SourceLocation EndLoc) { 15468 return new (Context) OMPSIMDClause(StartLoc, EndLoc); 15469 } 15470 15471 OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc, 15472 SourceLocation EndLoc) { 15473 return new (Context) OMPNogroupClause(StartLoc, EndLoc); 15474 } 15475 15476 OMPClause *Sema::ActOnOpenMPUnifiedAddressClause(SourceLocation StartLoc, 15477 SourceLocation EndLoc) { 15478 return new (Context) OMPUnifiedAddressClause(StartLoc, EndLoc); 15479 } 15480 15481 OMPClause *Sema::ActOnOpenMPUnifiedSharedMemoryClause(SourceLocation StartLoc, 15482 SourceLocation EndLoc) { 15483 return new (Context) OMPUnifiedSharedMemoryClause(StartLoc, EndLoc); 15484 } 15485 15486 OMPClause *Sema::ActOnOpenMPReverseOffloadClause(SourceLocation StartLoc, 15487 SourceLocation EndLoc) { 15488 return new (Context) OMPReverseOffloadClause(StartLoc, EndLoc); 15489 } 15490 15491 OMPClause *Sema::ActOnOpenMPDynamicAllocatorsClause(SourceLocation StartLoc, 15492 SourceLocation EndLoc) { 15493 return new (Context) OMPDynamicAllocatorsClause(StartLoc, EndLoc); 15494 } 15495 15496 StmtResult Sema::ActOnOpenMPInteropDirective(ArrayRef<OMPClause *> Clauses, 15497 SourceLocation StartLoc, 15498 SourceLocation EndLoc) { 15499 15500 // OpenMP 5.1 [2.15.1, interop Construct, Restrictions] 15501 // At least one action-clause must appear on a directive. 15502 if (!hasClauses(Clauses, OMPC_init, OMPC_use, OMPC_destroy, OMPC_nowait)) { 15503 StringRef Expected = "'init', 'use', 'destroy', or 'nowait'"; 15504 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 15505 << Expected << getOpenMPDirectiveName(OMPD_interop); 15506 return StmtError(); 15507 } 15508 15509 // OpenMP 5.1 [2.15.1, interop Construct, Restrictions] 15510 // A depend clause can only appear on the directive if a targetsync 15511 // interop-type is present or the interop-var was initialized with 15512 // the targetsync interop-type. 15513 15514 // If there is any 'init' clause diagnose if there is no 'init' clause with 15515 // interop-type of 'targetsync'. Cases involving other directives cannot be 15516 // diagnosed. 15517 const OMPDependClause *DependClause = nullptr; 15518 bool HasInitClause = false; 15519 bool IsTargetSync = false; 15520 for (const OMPClause *C : Clauses) { 15521 if (IsTargetSync) 15522 break; 15523 if (const auto *InitClause = dyn_cast<OMPInitClause>(C)) { 15524 HasInitClause = true; 15525 if (InitClause->getIsTargetSync()) 15526 IsTargetSync = true; 15527 } else if (const auto *DC = dyn_cast<OMPDependClause>(C)) { 15528 DependClause = DC; 15529 } 15530 } 15531 if (DependClause && HasInitClause && !IsTargetSync) { 15532 Diag(DependClause->getBeginLoc(), diag::err_omp_interop_bad_depend_clause); 15533 return StmtError(); 15534 } 15535 15536 // OpenMP 5.1 [2.15.1, interop Construct, Restrictions] 15537 // Each interop-var may be specified for at most one action-clause of each 15538 // interop construct. 15539 llvm::SmallPtrSet<const VarDecl *, 4> InteropVars; 15540 for (const OMPClause *C : Clauses) { 15541 OpenMPClauseKind ClauseKind = C->getClauseKind(); 15542 const DeclRefExpr *DRE = nullptr; 15543 SourceLocation VarLoc; 15544 15545 if (ClauseKind == OMPC_init) { 15546 const auto *IC = cast<OMPInitClause>(C); 15547 VarLoc = IC->getVarLoc(); 15548 DRE = dyn_cast_or_null<DeclRefExpr>(IC->getInteropVar()); 15549 } else if (ClauseKind == OMPC_use) { 15550 const auto *UC = cast<OMPUseClause>(C); 15551 VarLoc = UC->getVarLoc(); 15552 DRE = dyn_cast_or_null<DeclRefExpr>(UC->getInteropVar()); 15553 } else if (ClauseKind == OMPC_destroy) { 15554 const auto *DC = cast<OMPDestroyClause>(C); 15555 VarLoc = DC->getVarLoc(); 15556 DRE = dyn_cast_or_null<DeclRefExpr>(DC->getInteropVar()); 15557 } 15558 15559 if (!DRE) 15560 continue; 15561 15562 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) { 15563 if (!InteropVars.insert(VD->getCanonicalDecl()).second) { 15564 Diag(VarLoc, diag::err_omp_interop_var_multiple_actions) << VD; 15565 return StmtError(); 15566 } 15567 } 15568 } 15569 15570 return OMPInteropDirective::Create(Context, StartLoc, EndLoc, Clauses); 15571 } 15572 15573 static bool isValidInteropVariable(Sema &SemaRef, Expr *InteropVarExpr, 15574 SourceLocation VarLoc, 15575 OpenMPClauseKind Kind) { 15576 if (InteropVarExpr->isValueDependent() || InteropVarExpr->isTypeDependent() || 15577 InteropVarExpr->isInstantiationDependent() || 15578 InteropVarExpr->containsUnexpandedParameterPack()) 15579 return true; 15580 15581 const auto *DRE = dyn_cast<DeclRefExpr>(InteropVarExpr); 15582 if (!DRE || !isa<VarDecl>(DRE->getDecl())) { 15583 SemaRef.Diag(VarLoc, diag::err_omp_interop_variable_expected) << 0; 15584 return false; 15585 } 15586 15587 // Interop variable should be of type omp_interop_t. 15588 bool HasError = false; 15589 QualType InteropType; 15590 LookupResult Result(SemaRef, &SemaRef.Context.Idents.get("omp_interop_t"), 15591 VarLoc, Sema::LookupOrdinaryName); 15592 if (SemaRef.LookupName(Result, SemaRef.getCurScope())) { 15593 NamedDecl *ND = Result.getFoundDecl(); 15594 if (const auto *TD = dyn_cast<TypeDecl>(ND)) { 15595 InteropType = QualType(TD->getTypeForDecl(), 0); 15596 } else { 15597 HasError = true; 15598 } 15599 } else { 15600 HasError = true; 15601 } 15602 15603 if (HasError) { 15604 SemaRef.Diag(VarLoc, diag::err_omp_implied_type_not_found) 15605 << "omp_interop_t"; 15606 return false; 15607 } 15608 15609 QualType VarType = InteropVarExpr->getType().getUnqualifiedType(); 15610 if (!SemaRef.Context.hasSameType(InteropType, VarType)) { 15611 SemaRef.Diag(VarLoc, diag::err_omp_interop_variable_wrong_type); 15612 return false; 15613 } 15614 15615 // OpenMP 5.1 [2.15.1, interop Construct, Restrictions] 15616 // The interop-var passed to init or destroy must be non-const. 15617 if ((Kind == OMPC_init || Kind == OMPC_destroy) && 15618 isConstNotMutableType(SemaRef, InteropVarExpr->getType())) { 15619 SemaRef.Diag(VarLoc, diag::err_omp_interop_variable_expected) 15620 << /*non-const*/ 1; 15621 return false; 15622 } 15623 return true; 15624 } 15625 15626 OMPClause * 15627 Sema::ActOnOpenMPInitClause(Expr *InteropVar, ArrayRef<Expr *> PrefExprs, 15628 bool IsTarget, bool IsTargetSync, 15629 SourceLocation StartLoc, SourceLocation LParenLoc, 15630 SourceLocation VarLoc, SourceLocation EndLoc) { 15631 15632 if (!isValidInteropVariable(*this, InteropVar, VarLoc, OMPC_init)) 15633 return nullptr; 15634 15635 // Check prefer_type values. These foreign-runtime-id values are either 15636 // string literals or constant integral expressions. 15637 for (const Expr *E : PrefExprs) { 15638 if (E->isValueDependent() || E->isTypeDependent() || 15639 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 15640 continue; 15641 if (E->isIntegerConstantExpr(Context)) 15642 continue; 15643 if (isa<StringLiteral>(E)) 15644 continue; 15645 Diag(E->getExprLoc(), diag::err_omp_interop_prefer_type); 15646 return nullptr; 15647 } 15648 15649 return OMPInitClause::Create(Context, InteropVar, PrefExprs, IsTarget, 15650 IsTargetSync, StartLoc, LParenLoc, VarLoc, 15651 EndLoc); 15652 } 15653 15654 OMPClause *Sema::ActOnOpenMPUseClause(Expr *InteropVar, SourceLocation StartLoc, 15655 SourceLocation LParenLoc, 15656 SourceLocation VarLoc, 15657 SourceLocation EndLoc) { 15658 15659 if (!isValidInteropVariable(*this, InteropVar, VarLoc, OMPC_use)) 15660 return nullptr; 15661 15662 return new (Context) 15663 OMPUseClause(InteropVar, StartLoc, LParenLoc, VarLoc, EndLoc); 15664 } 15665 15666 OMPClause *Sema::ActOnOpenMPDestroyClause(Expr *InteropVar, 15667 SourceLocation StartLoc, 15668 SourceLocation LParenLoc, 15669 SourceLocation VarLoc, 15670 SourceLocation EndLoc) { 15671 if (InteropVar && 15672 !isValidInteropVariable(*this, InteropVar, VarLoc, OMPC_destroy)) 15673 return nullptr; 15674 15675 return new (Context) 15676 OMPDestroyClause(InteropVar, StartLoc, LParenLoc, VarLoc, EndLoc); 15677 } 15678 15679 OMPClause *Sema::ActOnOpenMPNovariantsClause(Expr *Condition, 15680 SourceLocation StartLoc, 15681 SourceLocation LParenLoc, 15682 SourceLocation EndLoc) { 15683 Expr *ValExpr = Condition; 15684 Stmt *HelperValStmt = nullptr; 15685 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 15686 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 15687 !Condition->isInstantiationDependent() && 15688 !Condition->containsUnexpandedParameterPack()) { 15689 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 15690 if (Val.isInvalid()) 15691 return nullptr; 15692 15693 ValExpr = MakeFullExpr(Val.get()).get(); 15694 15695 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 15696 CaptureRegion = getOpenMPCaptureRegionForClause(DKind, OMPC_novariants, 15697 LangOpts.OpenMP); 15698 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 15699 ValExpr = MakeFullExpr(ValExpr).get(); 15700 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 15701 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 15702 HelperValStmt = buildPreInits(Context, Captures); 15703 } 15704 } 15705 15706 return new (Context) OMPNovariantsClause( 15707 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc); 15708 } 15709 15710 OMPClause *Sema::ActOnOpenMPNocontextClause(Expr *Condition, 15711 SourceLocation StartLoc, 15712 SourceLocation LParenLoc, 15713 SourceLocation EndLoc) { 15714 Expr *ValExpr = Condition; 15715 Stmt *HelperValStmt = nullptr; 15716 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 15717 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 15718 !Condition->isInstantiationDependent() && 15719 !Condition->containsUnexpandedParameterPack()) { 15720 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 15721 if (Val.isInvalid()) 15722 return nullptr; 15723 15724 ValExpr = MakeFullExpr(Val.get()).get(); 15725 15726 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 15727 CaptureRegion = 15728 getOpenMPCaptureRegionForClause(DKind, OMPC_nocontext, LangOpts.OpenMP); 15729 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 15730 ValExpr = MakeFullExpr(ValExpr).get(); 15731 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 15732 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 15733 HelperValStmt = buildPreInits(Context, Captures); 15734 } 15735 } 15736 15737 return new (Context) OMPNocontextClause(ValExpr, HelperValStmt, CaptureRegion, 15738 StartLoc, LParenLoc, EndLoc); 15739 } 15740 15741 OMPClause *Sema::ActOnOpenMPFilterClause(Expr *ThreadID, 15742 SourceLocation StartLoc, 15743 SourceLocation LParenLoc, 15744 SourceLocation EndLoc) { 15745 Expr *ValExpr = ThreadID; 15746 Stmt *HelperValStmt = nullptr; 15747 15748 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 15749 OpenMPDirectiveKind CaptureRegion = 15750 getOpenMPCaptureRegionForClause(DKind, OMPC_filter, LangOpts.OpenMP); 15751 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 15752 ValExpr = MakeFullExpr(ValExpr).get(); 15753 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 15754 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 15755 HelperValStmt = buildPreInits(Context, Captures); 15756 } 15757 15758 return new (Context) OMPFilterClause(ValExpr, HelperValStmt, CaptureRegion, 15759 StartLoc, LParenLoc, EndLoc); 15760 } 15761 15762 OMPClause *Sema::ActOnOpenMPVarListClause( 15763 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *DepModOrTailExpr, 15764 const OMPVarListLocTy &Locs, SourceLocation ColonLoc, 15765 CXXScopeSpec &ReductionOrMapperIdScopeSpec, 15766 DeclarationNameInfo &ReductionOrMapperId, int ExtraModifier, 15767 ArrayRef<OpenMPMapModifierKind> MapTypeModifiers, 15768 ArrayRef<SourceLocation> MapTypeModifiersLoc, bool IsMapTypeImplicit, 15769 SourceLocation ExtraModifierLoc, 15770 ArrayRef<OpenMPMotionModifierKind> MotionModifiers, 15771 ArrayRef<SourceLocation> MotionModifiersLoc) { 15772 SourceLocation StartLoc = Locs.StartLoc; 15773 SourceLocation LParenLoc = Locs.LParenLoc; 15774 SourceLocation EndLoc = Locs.EndLoc; 15775 OMPClause *Res = nullptr; 15776 switch (Kind) { 15777 case OMPC_private: 15778 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc); 15779 break; 15780 case OMPC_firstprivate: 15781 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 15782 break; 15783 case OMPC_lastprivate: 15784 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_LASTPRIVATE_unknown && 15785 "Unexpected lastprivate modifier."); 15786 Res = ActOnOpenMPLastprivateClause( 15787 VarList, static_cast<OpenMPLastprivateModifier>(ExtraModifier), 15788 ExtraModifierLoc, ColonLoc, StartLoc, LParenLoc, EndLoc); 15789 break; 15790 case OMPC_shared: 15791 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc); 15792 break; 15793 case OMPC_reduction: 15794 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_REDUCTION_unknown && 15795 "Unexpected lastprivate modifier."); 15796 Res = ActOnOpenMPReductionClause( 15797 VarList, static_cast<OpenMPReductionClauseModifier>(ExtraModifier), 15798 StartLoc, LParenLoc, ExtraModifierLoc, ColonLoc, EndLoc, 15799 ReductionOrMapperIdScopeSpec, ReductionOrMapperId); 15800 break; 15801 case OMPC_task_reduction: 15802 Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 15803 EndLoc, ReductionOrMapperIdScopeSpec, 15804 ReductionOrMapperId); 15805 break; 15806 case OMPC_in_reduction: 15807 Res = ActOnOpenMPInReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 15808 EndLoc, ReductionOrMapperIdScopeSpec, 15809 ReductionOrMapperId); 15810 break; 15811 case OMPC_linear: 15812 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_LINEAR_unknown && 15813 "Unexpected linear modifier."); 15814 Res = ActOnOpenMPLinearClause( 15815 VarList, DepModOrTailExpr, StartLoc, LParenLoc, 15816 static_cast<OpenMPLinearClauseKind>(ExtraModifier), ExtraModifierLoc, 15817 ColonLoc, EndLoc); 15818 break; 15819 case OMPC_aligned: 15820 Res = ActOnOpenMPAlignedClause(VarList, DepModOrTailExpr, StartLoc, 15821 LParenLoc, ColonLoc, EndLoc); 15822 break; 15823 case OMPC_copyin: 15824 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc); 15825 break; 15826 case OMPC_copyprivate: 15827 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 15828 break; 15829 case OMPC_flush: 15830 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc); 15831 break; 15832 case OMPC_depend: 15833 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_DEPEND_unknown && 15834 "Unexpected depend modifier."); 15835 Res = ActOnOpenMPDependClause( 15836 DepModOrTailExpr, static_cast<OpenMPDependClauseKind>(ExtraModifier), 15837 ExtraModifierLoc, ColonLoc, VarList, StartLoc, LParenLoc, EndLoc); 15838 break; 15839 case OMPC_map: 15840 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_MAP_unknown && 15841 "Unexpected map modifier."); 15842 Res = ActOnOpenMPMapClause( 15843 MapTypeModifiers, MapTypeModifiersLoc, ReductionOrMapperIdScopeSpec, 15844 ReductionOrMapperId, static_cast<OpenMPMapClauseKind>(ExtraModifier), 15845 IsMapTypeImplicit, ExtraModifierLoc, ColonLoc, VarList, Locs); 15846 break; 15847 case OMPC_to: 15848 Res = ActOnOpenMPToClause(MotionModifiers, MotionModifiersLoc, 15849 ReductionOrMapperIdScopeSpec, ReductionOrMapperId, 15850 ColonLoc, VarList, Locs); 15851 break; 15852 case OMPC_from: 15853 Res = ActOnOpenMPFromClause(MotionModifiers, MotionModifiersLoc, 15854 ReductionOrMapperIdScopeSpec, 15855 ReductionOrMapperId, ColonLoc, VarList, Locs); 15856 break; 15857 case OMPC_use_device_ptr: 15858 Res = ActOnOpenMPUseDevicePtrClause(VarList, Locs); 15859 break; 15860 case OMPC_use_device_addr: 15861 Res = ActOnOpenMPUseDeviceAddrClause(VarList, Locs); 15862 break; 15863 case OMPC_is_device_ptr: 15864 Res = ActOnOpenMPIsDevicePtrClause(VarList, Locs); 15865 break; 15866 case OMPC_allocate: 15867 Res = ActOnOpenMPAllocateClause(DepModOrTailExpr, VarList, StartLoc, 15868 LParenLoc, ColonLoc, EndLoc); 15869 break; 15870 case OMPC_nontemporal: 15871 Res = ActOnOpenMPNontemporalClause(VarList, StartLoc, LParenLoc, EndLoc); 15872 break; 15873 case OMPC_inclusive: 15874 Res = ActOnOpenMPInclusiveClause(VarList, StartLoc, LParenLoc, EndLoc); 15875 break; 15876 case OMPC_exclusive: 15877 Res = ActOnOpenMPExclusiveClause(VarList, StartLoc, LParenLoc, EndLoc); 15878 break; 15879 case OMPC_affinity: 15880 Res = ActOnOpenMPAffinityClause(StartLoc, LParenLoc, ColonLoc, EndLoc, 15881 DepModOrTailExpr, VarList); 15882 break; 15883 case OMPC_if: 15884 case OMPC_depobj: 15885 case OMPC_final: 15886 case OMPC_num_threads: 15887 case OMPC_safelen: 15888 case OMPC_simdlen: 15889 case OMPC_sizes: 15890 case OMPC_allocator: 15891 case OMPC_collapse: 15892 case OMPC_default: 15893 case OMPC_proc_bind: 15894 case OMPC_schedule: 15895 case OMPC_ordered: 15896 case OMPC_nowait: 15897 case OMPC_untied: 15898 case OMPC_mergeable: 15899 case OMPC_threadprivate: 15900 case OMPC_read: 15901 case OMPC_write: 15902 case OMPC_update: 15903 case OMPC_capture: 15904 case OMPC_compare: 15905 case OMPC_seq_cst: 15906 case OMPC_acq_rel: 15907 case OMPC_acquire: 15908 case OMPC_release: 15909 case OMPC_relaxed: 15910 case OMPC_device: 15911 case OMPC_threads: 15912 case OMPC_simd: 15913 case OMPC_num_teams: 15914 case OMPC_thread_limit: 15915 case OMPC_priority: 15916 case OMPC_grainsize: 15917 case OMPC_nogroup: 15918 case OMPC_num_tasks: 15919 case OMPC_hint: 15920 case OMPC_dist_schedule: 15921 case OMPC_defaultmap: 15922 case OMPC_unknown: 15923 case OMPC_uniform: 15924 case OMPC_unified_address: 15925 case OMPC_unified_shared_memory: 15926 case OMPC_reverse_offload: 15927 case OMPC_dynamic_allocators: 15928 case OMPC_atomic_default_mem_order: 15929 case OMPC_device_type: 15930 case OMPC_match: 15931 case OMPC_order: 15932 case OMPC_destroy: 15933 case OMPC_novariants: 15934 case OMPC_nocontext: 15935 case OMPC_detach: 15936 case OMPC_uses_allocators: 15937 case OMPC_when: 15938 case OMPC_bind: 15939 default: 15940 llvm_unreachable("Clause is not allowed."); 15941 } 15942 return Res; 15943 } 15944 15945 ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK, 15946 ExprObjectKind OK, SourceLocation Loc) { 15947 ExprResult Res = BuildDeclRefExpr( 15948 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc); 15949 if (!Res.isUsable()) 15950 return ExprError(); 15951 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) { 15952 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get()); 15953 if (!Res.isUsable()) 15954 return ExprError(); 15955 } 15956 if (VK != VK_LValue && Res.get()->isGLValue()) { 15957 Res = DefaultLvalueConversion(Res.get()); 15958 if (!Res.isUsable()) 15959 return ExprError(); 15960 } 15961 return Res; 15962 } 15963 15964 OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList, 15965 SourceLocation StartLoc, 15966 SourceLocation LParenLoc, 15967 SourceLocation EndLoc) { 15968 SmallVector<Expr *, 8> Vars; 15969 SmallVector<Expr *, 8> PrivateCopies; 15970 for (Expr *RefExpr : VarList) { 15971 assert(RefExpr && "NULL expr in OpenMP private clause."); 15972 SourceLocation ELoc; 15973 SourceRange ERange; 15974 Expr *SimpleRefExpr = RefExpr; 15975 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 15976 if (Res.second) { 15977 // It will be analyzed later. 15978 Vars.push_back(RefExpr); 15979 PrivateCopies.push_back(nullptr); 15980 } 15981 ValueDecl *D = Res.first; 15982 if (!D) 15983 continue; 15984 15985 QualType Type = D->getType(); 15986 auto *VD = dyn_cast<VarDecl>(D); 15987 15988 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 15989 // A variable that appears in a private clause must not have an incomplete 15990 // type or a reference type. 15991 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type)) 15992 continue; 15993 Type = Type.getNonReferenceType(); 15994 15995 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions] 15996 // A variable that is privatized must not have a const-qualified type 15997 // unless it is of class type with a mutable member. This restriction does 15998 // not apply to the firstprivate clause. 15999 // 16000 // OpenMP 3.1 [2.9.3.3, private clause, Restrictions] 16001 // A variable that appears in a private clause must not have a 16002 // const-qualified type unless it is of class type with a mutable member. 16003 if (rejectConstNotMutableType(*this, D, Type, OMPC_private, ELoc)) 16004 continue; 16005 16006 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 16007 // in a Construct] 16008 // Variables with the predetermined data-sharing attributes may not be 16009 // listed in data-sharing attributes clauses, except for the cases 16010 // listed below. For these exceptions only, listing a predetermined 16011 // variable in a data-sharing attribute clause is allowed and overrides 16012 // the variable's predetermined data-sharing attributes. 16013 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 16014 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) { 16015 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 16016 << getOpenMPClauseName(OMPC_private); 16017 reportOriginalDsa(*this, DSAStack, D, DVar); 16018 continue; 16019 } 16020 16021 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 16022 // Variably modified types are not supported for tasks. 16023 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && 16024 isOpenMPTaskingDirective(CurrDir)) { 16025 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 16026 << getOpenMPClauseName(OMPC_private) << Type 16027 << getOpenMPDirectiveName(CurrDir); 16028 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 16029 VarDecl::DeclarationOnly; 16030 Diag(D->getLocation(), 16031 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 16032 << D; 16033 continue; 16034 } 16035 16036 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 16037 // A list item cannot appear in both a map clause and a data-sharing 16038 // attribute clause on the same construct 16039 // 16040 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7] 16041 // A list item cannot appear in both a map clause and a data-sharing 16042 // attribute clause on the same construct unless the construct is a 16043 // combined construct. 16044 if ((LangOpts.OpenMP <= 45 && isOpenMPTargetExecutionDirective(CurrDir)) || 16045 CurrDir == OMPD_target) { 16046 OpenMPClauseKind ConflictKind; 16047 if (DSAStack->checkMappableExprComponentListsForDecl( 16048 VD, /*CurrentRegionOnly=*/true, 16049 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef, 16050 OpenMPClauseKind WhereFoundClauseKind) -> bool { 16051 ConflictKind = WhereFoundClauseKind; 16052 return true; 16053 })) { 16054 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 16055 << getOpenMPClauseName(OMPC_private) 16056 << getOpenMPClauseName(ConflictKind) 16057 << getOpenMPDirectiveName(CurrDir); 16058 reportOriginalDsa(*this, DSAStack, D, DVar); 16059 continue; 16060 } 16061 } 16062 16063 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1] 16064 // A variable of class type (or array thereof) that appears in a private 16065 // clause requires an accessible, unambiguous default constructor for the 16066 // class type. 16067 // Generate helper private variable and initialize it with the default 16068 // value. The address of the original variable is replaced by the address of 16069 // the new private variable in CodeGen. This new variable is not added to 16070 // IdResolver, so the code in the OpenMP region uses original variable for 16071 // proper diagnostics. 16072 Type = Type.getUnqualifiedType(); 16073 VarDecl *VDPrivate = 16074 buildVarDecl(*this, ELoc, Type, D->getName(), 16075 D->hasAttrs() ? &D->getAttrs() : nullptr, 16076 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 16077 ActOnUninitializedDecl(VDPrivate); 16078 if (VDPrivate->isInvalidDecl()) 16079 continue; 16080 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 16081 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc); 16082 16083 DeclRefExpr *Ref = nullptr; 16084 if (!VD && !CurContext->isDependentContext()) 16085 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 16086 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref); 16087 Vars.push_back((VD || CurContext->isDependentContext()) 16088 ? RefExpr->IgnoreParens() 16089 : Ref); 16090 PrivateCopies.push_back(VDPrivateRefExpr); 16091 } 16092 16093 if (Vars.empty()) 16094 return nullptr; 16095 16096 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 16097 PrivateCopies); 16098 } 16099 16100 OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList, 16101 SourceLocation StartLoc, 16102 SourceLocation LParenLoc, 16103 SourceLocation EndLoc) { 16104 SmallVector<Expr *, 8> Vars; 16105 SmallVector<Expr *, 8> PrivateCopies; 16106 SmallVector<Expr *, 8> Inits; 16107 SmallVector<Decl *, 4> ExprCaptures; 16108 bool IsImplicitClause = 16109 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid(); 16110 SourceLocation ImplicitClauseLoc = DSAStack->getConstructLoc(); 16111 16112 for (Expr *RefExpr : VarList) { 16113 assert(RefExpr && "NULL expr in OpenMP firstprivate clause."); 16114 SourceLocation ELoc; 16115 SourceRange ERange; 16116 Expr *SimpleRefExpr = RefExpr; 16117 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 16118 if (Res.second) { 16119 // It will be analyzed later. 16120 Vars.push_back(RefExpr); 16121 PrivateCopies.push_back(nullptr); 16122 Inits.push_back(nullptr); 16123 } 16124 ValueDecl *D = Res.first; 16125 if (!D) 16126 continue; 16127 16128 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc; 16129 QualType Type = D->getType(); 16130 auto *VD = dyn_cast<VarDecl>(D); 16131 16132 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 16133 // A variable that appears in a private clause must not have an incomplete 16134 // type or a reference type. 16135 if (RequireCompleteType(ELoc, Type, 16136 diag::err_omp_firstprivate_incomplete_type)) 16137 continue; 16138 Type = Type.getNonReferenceType(); 16139 16140 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1] 16141 // A variable of class type (or array thereof) that appears in a private 16142 // clause requires an accessible, unambiguous copy constructor for the 16143 // class type. 16144 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType(); 16145 16146 // If an implicit firstprivate variable found it was checked already. 16147 DSAStackTy::DSAVarData TopDVar; 16148 if (!IsImplicitClause) { 16149 DSAStackTy::DSAVarData DVar = 16150 DSAStack->getTopDSA(D, /*FromParent=*/false); 16151 TopDVar = DVar; 16152 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 16153 bool IsConstant = ElemType.isConstant(Context); 16154 // OpenMP [2.4.13, Data-sharing Attribute Clauses] 16155 // A list item that specifies a given variable may not appear in more 16156 // than one clause on the same directive, except that a variable may be 16157 // specified in both firstprivate and lastprivate clauses. 16158 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3] 16159 // A list item may appear in a firstprivate or lastprivate clause but not 16160 // both. 16161 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate && 16162 (isOpenMPDistributeDirective(CurrDir) || 16163 DVar.CKind != OMPC_lastprivate) && 16164 DVar.RefExpr) { 16165 Diag(ELoc, diag::err_omp_wrong_dsa) 16166 << getOpenMPClauseName(DVar.CKind) 16167 << getOpenMPClauseName(OMPC_firstprivate); 16168 reportOriginalDsa(*this, DSAStack, D, DVar); 16169 continue; 16170 } 16171 16172 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 16173 // in a Construct] 16174 // Variables with the predetermined data-sharing attributes may not be 16175 // listed in data-sharing attributes clauses, except for the cases 16176 // listed below. For these exceptions only, listing a predetermined 16177 // variable in a data-sharing attribute clause is allowed and overrides 16178 // the variable's predetermined data-sharing attributes. 16179 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 16180 // in a Construct, C/C++, p.2] 16181 // Variables with const-qualified type having no mutable member may be 16182 // listed in a firstprivate clause, even if they are static data members. 16183 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr && 16184 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) { 16185 Diag(ELoc, diag::err_omp_wrong_dsa) 16186 << getOpenMPClauseName(DVar.CKind) 16187 << getOpenMPClauseName(OMPC_firstprivate); 16188 reportOriginalDsa(*this, DSAStack, D, DVar); 16189 continue; 16190 } 16191 16192 // OpenMP [2.9.3.4, Restrictions, p.2] 16193 // A list item that is private within a parallel region must not appear 16194 // in a firstprivate clause on a worksharing construct if any of the 16195 // worksharing regions arising from the worksharing construct ever bind 16196 // to any of the parallel regions arising from the parallel construct. 16197 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3] 16198 // A list item that is private within a teams region must not appear in a 16199 // firstprivate clause on a distribute construct if any of the distribute 16200 // regions arising from the distribute construct ever bind to any of the 16201 // teams regions arising from the teams construct. 16202 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3] 16203 // A list item that appears in a reduction clause of a teams construct 16204 // must not appear in a firstprivate clause on a distribute construct if 16205 // any of the distribute regions arising from the distribute construct 16206 // ever bind to any of the teams regions arising from the teams construct. 16207 if ((isOpenMPWorksharingDirective(CurrDir) || 16208 isOpenMPDistributeDirective(CurrDir)) && 16209 !isOpenMPParallelDirective(CurrDir) && 16210 !isOpenMPTeamsDirective(CurrDir)) { 16211 DVar = DSAStack->getImplicitDSA(D, true); 16212 if (DVar.CKind != OMPC_shared && 16213 (isOpenMPParallelDirective(DVar.DKind) || 16214 isOpenMPTeamsDirective(DVar.DKind) || 16215 DVar.DKind == OMPD_unknown)) { 16216 Diag(ELoc, diag::err_omp_required_access) 16217 << getOpenMPClauseName(OMPC_firstprivate) 16218 << getOpenMPClauseName(OMPC_shared); 16219 reportOriginalDsa(*this, DSAStack, D, DVar); 16220 continue; 16221 } 16222 } 16223 // OpenMP [2.9.3.4, Restrictions, p.3] 16224 // A list item that appears in a reduction clause of a parallel construct 16225 // must not appear in a firstprivate clause on a worksharing or task 16226 // construct if any of the worksharing or task regions arising from the 16227 // worksharing or task construct ever bind to any of the parallel regions 16228 // arising from the parallel construct. 16229 // OpenMP [2.9.3.4, Restrictions, p.4] 16230 // A list item that appears in a reduction clause in worksharing 16231 // construct must not appear in a firstprivate clause in a task construct 16232 // encountered during execution of any of the worksharing regions arising 16233 // from the worksharing construct. 16234 if (isOpenMPTaskingDirective(CurrDir)) { 16235 DVar = DSAStack->hasInnermostDSA( 16236 D, 16237 [](OpenMPClauseKind C, bool AppliedToPointee) { 16238 return C == OMPC_reduction && !AppliedToPointee; 16239 }, 16240 [](OpenMPDirectiveKind K) { 16241 return isOpenMPParallelDirective(K) || 16242 isOpenMPWorksharingDirective(K) || 16243 isOpenMPTeamsDirective(K); 16244 }, 16245 /*FromParent=*/true); 16246 if (DVar.CKind == OMPC_reduction && 16247 (isOpenMPParallelDirective(DVar.DKind) || 16248 isOpenMPWorksharingDirective(DVar.DKind) || 16249 isOpenMPTeamsDirective(DVar.DKind))) { 16250 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate) 16251 << getOpenMPDirectiveName(DVar.DKind); 16252 reportOriginalDsa(*this, DSAStack, D, DVar); 16253 continue; 16254 } 16255 } 16256 16257 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 16258 // A list item cannot appear in both a map clause and a data-sharing 16259 // attribute clause on the same construct 16260 // 16261 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7] 16262 // A list item cannot appear in both a map clause and a data-sharing 16263 // attribute clause on the same construct unless the construct is a 16264 // combined construct. 16265 if ((LangOpts.OpenMP <= 45 && 16266 isOpenMPTargetExecutionDirective(CurrDir)) || 16267 CurrDir == OMPD_target) { 16268 OpenMPClauseKind ConflictKind; 16269 if (DSAStack->checkMappableExprComponentListsForDecl( 16270 VD, /*CurrentRegionOnly=*/true, 16271 [&ConflictKind]( 16272 OMPClauseMappableExprCommon::MappableExprComponentListRef, 16273 OpenMPClauseKind WhereFoundClauseKind) { 16274 ConflictKind = WhereFoundClauseKind; 16275 return true; 16276 })) { 16277 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 16278 << getOpenMPClauseName(OMPC_firstprivate) 16279 << getOpenMPClauseName(ConflictKind) 16280 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 16281 reportOriginalDsa(*this, DSAStack, D, DVar); 16282 continue; 16283 } 16284 } 16285 } 16286 16287 // Variably modified types are not supported for tasks. 16288 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && 16289 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) { 16290 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 16291 << getOpenMPClauseName(OMPC_firstprivate) << Type 16292 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 16293 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 16294 VarDecl::DeclarationOnly; 16295 Diag(D->getLocation(), 16296 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 16297 << D; 16298 continue; 16299 } 16300 16301 Type = Type.getUnqualifiedType(); 16302 VarDecl *VDPrivate = 16303 buildVarDecl(*this, ELoc, Type, D->getName(), 16304 D->hasAttrs() ? &D->getAttrs() : nullptr, 16305 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 16306 // Generate helper private variable and initialize it with the value of the 16307 // original variable. The address of the original variable is replaced by 16308 // the address of the new private variable in the CodeGen. This new variable 16309 // is not added to IdResolver, so the code in the OpenMP region uses 16310 // original variable for proper diagnostics and variable capturing. 16311 Expr *VDInitRefExpr = nullptr; 16312 // For arrays generate initializer for single element and replace it by the 16313 // original array element in CodeGen. 16314 if (Type->isArrayType()) { 16315 VarDecl *VDInit = 16316 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName()); 16317 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc); 16318 Expr *Init = DefaultLvalueConversion(VDInitRefExpr).get(); 16319 ElemType = ElemType.getUnqualifiedType(); 16320 VarDecl *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, 16321 ".firstprivate.temp"); 16322 InitializedEntity Entity = 16323 InitializedEntity::InitializeVariable(VDInitTemp); 16324 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc); 16325 16326 InitializationSequence InitSeq(*this, Entity, Kind, Init); 16327 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init); 16328 if (Result.isInvalid()) 16329 VDPrivate->setInvalidDecl(); 16330 else 16331 VDPrivate->setInit(Result.getAs<Expr>()); 16332 // Remove temp variable declaration. 16333 Context.Deallocate(VDInitTemp); 16334 } else { 16335 VarDecl *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type, 16336 ".firstprivate.temp"); 16337 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(), 16338 RefExpr->getExprLoc()); 16339 AddInitializerToDecl(VDPrivate, 16340 DefaultLvalueConversion(VDInitRefExpr).get(), 16341 /*DirectInit=*/false); 16342 } 16343 if (VDPrivate->isInvalidDecl()) { 16344 if (IsImplicitClause) { 16345 Diag(RefExpr->getExprLoc(), 16346 diag::note_omp_task_predetermined_firstprivate_here); 16347 } 16348 continue; 16349 } 16350 CurContext->addDecl(VDPrivate); 16351 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 16352 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), 16353 RefExpr->getExprLoc()); 16354 DeclRefExpr *Ref = nullptr; 16355 if (!VD && !CurContext->isDependentContext()) { 16356 if (TopDVar.CKind == OMPC_lastprivate) { 16357 Ref = TopDVar.PrivateCopy; 16358 } else { 16359 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 16360 if (!isOpenMPCapturedDecl(D)) 16361 ExprCaptures.push_back(Ref->getDecl()); 16362 } 16363 } 16364 if (!IsImplicitClause) 16365 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 16366 Vars.push_back((VD || CurContext->isDependentContext()) 16367 ? RefExpr->IgnoreParens() 16368 : Ref); 16369 PrivateCopies.push_back(VDPrivateRefExpr); 16370 Inits.push_back(VDInitRefExpr); 16371 } 16372 16373 if (Vars.empty()) 16374 return nullptr; 16375 16376 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 16377 Vars, PrivateCopies, Inits, 16378 buildPreInits(Context, ExprCaptures)); 16379 } 16380 16381 OMPClause *Sema::ActOnOpenMPLastprivateClause( 16382 ArrayRef<Expr *> VarList, OpenMPLastprivateModifier LPKind, 16383 SourceLocation LPKindLoc, SourceLocation ColonLoc, SourceLocation StartLoc, 16384 SourceLocation LParenLoc, SourceLocation EndLoc) { 16385 if (LPKind == OMPC_LASTPRIVATE_unknown && LPKindLoc.isValid()) { 16386 assert(ColonLoc.isValid() && "Colon location must be valid."); 16387 Diag(LPKindLoc, diag::err_omp_unexpected_clause_value) 16388 << getListOfPossibleValues(OMPC_lastprivate, /*First=*/0, 16389 /*Last=*/OMPC_LASTPRIVATE_unknown) 16390 << getOpenMPClauseName(OMPC_lastprivate); 16391 return nullptr; 16392 } 16393 16394 SmallVector<Expr *, 8> Vars; 16395 SmallVector<Expr *, 8> SrcExprs; 16396 SmallVector<Expr *, 8> DstExprs; 16397 SmallVector<Expr *, 8> AssignmentOps; 16398 SmallVector<Decl *, 4> ExprCaptures; 16399 SmallVector<Expr *, 4> ExprPostUpdates; 16400 for (Expr *RefExpr : VarList) { 16401 assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); 16402 SourceLocation ELoc; 16403 SourceRange ERange; 16404 Expr *SimpleRefExpr = RefExpr; 16405 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 16406 if (Res.second) { 16407 // It will be analyzed later. 16408 Vars.push_back(RefExpr); 16409 SrcExprs.push_back(nullptr); 16410 DstExprs.push_back(nullptr); 16411 AssignmentOps.push_back(nullptr); 16412 } 16413 ValueDecl *D = Res.first; 16414 if (!D) 16415 continue; 16416 16417 QualType Type = D->getType(); 16418 auto *VD = dyn_cast<VarDecl>(D); 16419 16420 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2] 16421 // A variable that appears in a lastprivate clause must not have an 16422 // incomplete type or a reference type. 16423 if (RequireCompleteType(ELoc, Type, 16424 diag::err_omp_lastprivate_incomplete_type)) 16425 continue; 16426 Type = Type.getNonReferenceType(); 16427 16428 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions] 16429 // A variable that is privatized must not have a const-qualified type 16430 // unless it is of class type with a mutable member. This restriction does 16431 // not apply to the firstprivate clause. 16432 // 16433 // OpenMP 3.1 [2.9.3.5, lastprivate clause, Restrictions] 16434 // A variable that appears in a lastprivate clause must not have a 16435 // const-qualified type unless it is of class type with a mutable member. 16436 if (rejectConstNotMutableType(*this, D, Type, OMPC_lastprivate, ELoc)) 16437 continue; 16438 16439 // OpenMP 5.0 [2.19.4.5 lastprivate Clause, Restrictions] 16440 // A list item that appears in a lastprivate clause with the conditional 16441 // modifier must be a scalar variable. 16442 if (LPKind == OMPC_LASTPRIVATE_conditional && !Type->isScalarType()) { 16443 Diag(ELoc, diag::err_omp_lastprivate_conditional_non_scalar); 16444 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 16445 VarDecl::DeclarationOnly; 16446 Diag(D->getLocation(), 16447 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 16448 << D; 16449 continue; 16450 } 16451 16452 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 16453 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 16454 // in a Construct] 16455 // Variables with the predetermined data-sharing attributes may not be 16456 // listed in data-sharing attributes clauses, except for the cases 16457 // listed below. 16458 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3] 16459 // A list item may appear in a firstprivate or lastprivate clause but not 16460 // both. 16461 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 16462 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate && 16463 (isOpenMPDistributeDirective(CurrDir) || 16464 DVar.CKind != OMPC_firstprivate) && 16465 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) { 16466 Diag(ELoc, diag::err_omp_wrong_dsa) 16467 << getOpenMPClauseName(DVar.CKind) 16468 << getOpenMPClauseName(OMPC_lastprivate); 16469 reportOriginalDsa(*this, DSAStack, D, DVar); 16470 continue; 16471 } 16472 16473 // OpenMP [2.14.3.5, Restrictions, p.2] 16474 // A list item that is private within a parallel region, or that appears in 16475 // the reduction clause of a parallel construct, must not appear in a 16476 // lastprivate clause on a worksharing construct if any of the corresponding 16477 // worksharing regions ever binds to any of the corresponding parallel 16478 // regions. 16479 DSAStackTy::DSAVarData TopDVar = DVar; 16480 if (isOpenMPWorksharingDirective(CurrDir) && 16481 !isOpenMPParallelDirective(CurrDir) && 16482 !isOpenMPTeamsDirective(CurrDir)) { 16483 DVar = DSAStack->getImplicitDSA(D, true); 16484 if (DVar.CKind != OMPC_shared) { 16485 Diag(ELoc, diag::err_omp_required_access) 16486 << getOpenMPClauseName(OMPC_lastprivate) 16487 << getOpenMPClauseName(OMPC_shared); 16488 reportOriginalDsa(*this, DSAStack, D, DVar); 16489 continue; 16490 } 16491 } 16492 16493 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2] 16494 // A variable of class type (or array thereof) that appears in a 16495 // lastprivate clause requires an accessible, unambiguous default 16496 // constructor for the class type, unless the list item is also specified 16497 // in a firstprivate clause. 16498 // A variable of class type (or array thereof) that appears in a 16499 // lastprivate clause requires an accessible, unambiguous copy assignment 16500 // operator for the class type. 16501 Type = Context.getBaseElementType(Type).getNonReferenceType(); 16502 VarDecl *SrcVD = buildVarDecl(*this, ERange.getBegin(), 16503 Type.getUnqualifiedType(), ".lastprivate.src", 16504 D->hasAttrs() ? &D->getAttrs() : nullptr); 16505 DeclRefExpr *PseudoSrcExpr = 16506 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc); 16507 VarDecl *DstVD = 16508 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst", 16509 D->hasAttrs() ? &D->getAttrs() : nullptr); 16510 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc); 16511 // For arrays generate assignment operation for single element and replace 16512 // it by the original array element in CodeGen. 16513 ExprResult AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign, 16514 PseudoDstExpr, PseudoSrcExpr); 16515 if (AssignmentOp.isInvalid()) 16516 continue; 16517 AssignmentOp = 16518 ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false); 16519 if (AssignmentOp.isInvalid()) 16520 continue; 16521 16522 DeclRefExpr *Ref = nullptr; 16523 if (!VD && !CurContext->isDependentContext()) { 16524 if (TopDVar.CKind == OMPC_firstprivate) { 16525 Ref = TopDVar.PrivateCopy; 16526 } else { 16527 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 16528 if (!isOpenMPCapturedDecl(D)) 16529 ExprCaptures.push_back(Ref->getDecl()); 16530 } 16531 if ((TopDVar.CKind == OMPC_firstprivate && !TopDVar.PrivateCopy) || 16532 (!isOpenMPCapturedDecl(D) && 16533 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) { 16534 ExprResult RefRes = DefaultLvalueConversion(Ref); 16535 if (!RefRes.isUsable()) 16536 continue; 16537 ExprResult PostUpdateRes = 16538 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr, 16539 RefRes.get()); 16540 if (!PostUpdateRes.isUsable()) 16541 continue; 16542 ExprPostUpdates.push_back( 16543 IgnoredValueConversions(PostUpdateRes.get()).get()); 16544 } 16545 } 16546 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref); 16547 Vars.push_back((VD || CurContext->isDependentContext()) 16548 ? RefExpr->IgnoreParens() 16549 : Ref); 16550 SrcExprs.push_back(PseudoSrcExpr); 16551 DstExprs.push_back(PseudoDstExpr); 16552 AssignmentOps.push_back(AssignmentOp.get()); 16553 } 16554 16555 if (Vars.empty()) 16556 return nullptr; 16557 16558 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 16559 Vars, SrcExprs, DstExprs, AssignmentOps, 16560 LPKind, LPKindLoc, ColonLoc, 16561 buildPreInits(Context, ExprCaptures), 16562 buildPostUpdate(*this, ExprPostUpdates)); 16563 } 16564 16565 OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList, 16566 SourceLocation StartLoc, 16567 SourceLocation LParenLoc, 16568 SourceLocation EndLoc) { 16569 SmallVector<Expr *, 8> Vars; 16570 for (Expr *RefExpr : VarList) { 16571 assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); 16572 SourceLocation ELoc; 16573 SourceRange ERange; 16574 Expr *SimpleRefExpr = RefExpr; 16575 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 16576 if (Res.second) { 16577 // It will be analyzed later. 16578 Vars.push_back(RefExpr); 16579 } 16580 ValueDecl *D = Res.first; 16581 if (!D) 16582 continue; 16583 16584 auto *VD = dyn_cast<VarDecl>(D); 16585 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 16586 // in a Construct] 16587 // Variables with the predetermined data-sharing attributes may not be 16588 // listed in data-sharing attributes clauses, except for the cases 16589 // listed below. For these exceptions only, listing a predetermined 16590 // variable in a data-sharing attribute clause is allowed and overrides 16591 // the variable's predetermined data-sharing attributes. 16592 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 16593 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared && 16594 DVar.RefExpr) { 16595 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 16596 << getOpenMPClauseName(OMPC_shared); 16597 reportOriginalDsa(*this, DSAStack, D, DVar); 16598 continue; 16599 } 16600 16601 DeclRefExpr *Ref = nullptr; 16602 if (!VD && isOpenMPCapturedDecl(D) && !CurContext->isDependentContext()) 16603 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 16604 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref); 16605 Vars.push_back((VD || !Ref || CurContext->isDependentContext()) 16606 ? RefExpr->IgnoreParens() 16607 : Ref); 16608 } 16609 16610 if (Vars.empty()) 16611 return nullptr; 16612 16613 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 16614 } 16615 16616 namespace { 16617 class DSARefChecker : public StmtVisitor<DSARefChecker, bool> { 16618 DSAStackTy *Stack; 16619 16620 public: 16621 bool VisitDeclRefExpr(DeclRefExpr *E) { 16622 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 16623 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false); 16624 if (DVar.CKind == OMPC_shared && !DVar.RefExpr) 16625 return false; 16626 if (DVar.CKind != OMPC_unknown) 16627 return true; 16628 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA( 16629 VD, 16630 [](OpenMPClauseKind C, bool AppliedToPointee) { 16631 return isOpenMPPrivate(C) && !AppliedToPointee; 16632 }, 16633 [](OpenMPDirectiveKind) { return true; }, 16634 /*FromParent=*/true); 16635 return DVarPrivate.CKind != OMPC_unknown; 16636 } 16637 return false; 16638 } 16639 bool VisitStmt(Stmt *S) { 16640 for (Stmt *Child : S->children()) { 16641 if (Child && Visit(Child)) 16642 return true; 16643 } 16644 return false; 16645 } 16646 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {} 16647 }; 16648 } // namespace 16649 16650 namespace { 16651 // Transform MemberExpression for specified FieldDecl of current class to 16652 // DeclRefExpr to specified OMPCapturedExprDecl. 16653 class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> { 16654 typedef TreeTransform<TransformExprToCaptures> BaseTransform; 16655 ValueDecl *Field = nullptr; 16656 DeclRefExpr *CapturedExpr = nullptr; 16657 16658 public: 16659 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl) 16660 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {} 16661 16662 ExprResult TransformMemberExpr(MemberExpr *E) { 16663 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) && 16664 E->getMemberDecl() == Field) { 16665 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false); 16666 return CapturedExpr; 16667 } 16668 return BaseTransform::TransformMemberExpr(E); 16669 } 16670 DeclRefExpr *getCapturedExpr() { return CapturedExpr; } 16671 }; 16672 } // namespace 16673 16674 template <typename T, typename U> 16675 static T filterLookupForUDReductionAndMapper( 16676 SmallVectorImpl<U> &Lookups, const llvm::function_ref<T(ValueDecl *)> Gen) { 16677 for (U &Set : Lookups) { 16678 for (auto *D : Set) { 16679 if (T Res = Gen(cast<ValueDecl>(D))) 16680 return Res; 16681 } 16682 } 16683 return T(); 16684 } 16685 16686 static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D) { 16687 assert(!LookupResult::isVisible(SemaRef, D) && "not in slow case"); 16688 16689 for (auto RD : D->redecls()) { 16690 // Don't bother with extra checks if we already know this one isn't visible. 16691 if (RD == D) 16692 continue; 16693 16694 auto ND = cast<NamedDecl>(RD); 16695 if (LookupResult::isVisible(SemaRef, ND)) 16696 return ND; 16697 } 16698 16699 return nullptr; 16700 } 16701 16702 static void 16703 argumentDependentLookup(Sema &SemaRef, const DeclarationNameInfo &Id, 16704 SourceLocation Loc, QualType Ty, 16705 SmallVectorImpl<UnresolvedSet<8>> &Lookups) { 16706 // Find all of the associated namespaces and classes based on the 16707 // arguments we have. 16708 Sema::AssociatedNamespaceSet AssociatedNamespaces; 16709 Sema::AssociatedClassSet AssociatedClasses; 16710 OpaqueValueExpr OVE(Loc, Ty, VK_LValue); 16711 SemaRef.FindAssociatedClassesAndNamespaces(Loc, &OVE, AssociatedNamespaces, 16712 AssociatedClasses); 16713 16714 // C++ [basic.lookup.argdep]p3: 16715 // Let X be the lookup set produced by unqualified lookup (3.4.1) 16716 // and let Y be the lookup set produced by argument dependent 16717 // lookup (defined as follows). If X contains [...] then Y is 16718 // empty. Otherwise Y is the set of declarations found in the 16719 // namespaces associated with the argument types as described 16720 // below. The set of declarations found by the lookup of the name 16721 // is the union of X and Y. 16722 // 16723 // Here, we compute Y and add its members to the overloaded 16724 // candidate set. 16725 for (auto *NS : AssociatedNamespaces) { 16726 // When considering an associated namespace, the lookup is the 16727 // same as the lookup performed when the associated namespace is 16728 // used as a qualifier (3.4.3.2) except that: 16729 // 16730 // -- Any using-directives in the associated namespace are 16731 // ignored. 16732 // 16733 // -- Any namespace-scope friend functions declared in 16734 // associated classes are visible within their respective 16735 // namespaces even if they are not visible during an ordinary 16736 // lookup (11.4). 16737 DeclContext::lookup_result R = NS->lookup(Id.getName()); 16738 for (auto *D : R) { 16739 auto *Underlying = D; 16740 if (auto *USD = dyn_cast<UsingShadowDecl>(D)) 16741 Underlying = USD->getTargetDecl(); 16742 16743 if (!isa<OMPDeclareReductionDecl>(Underlying) && 16744 !isa<OMPDeclareMapperDecl>(Underlying)) 16745 continue; 16746 16747 if (!SemaRef.isVisible(D)) { 16748 D = findAcceptableDecl(SemaRef, D); 16749 if (!D) 16750 continue; 16751 if (auto *USD = dyn_cast<UsingShadowDecl>(D)) 16752 Underlying = USD->getTargetDecl(); 16753 } 16754 Lookups.emplace_back(); 16755 Lookups.back().addDecl(Underlying); 16756 } 16757 } 16758 } 16759 16760 static ExprResult 16761 buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range, 16762 Scope *S, CXXScopeSpec &ReductionIdScopeSpec, 16763 const DeclarationNameInfo &ReductionId, QualType Ty, 16764 CXXCastPath &BasePath, Expr *UnresolvedReduction) { 16765 if (ReductionIdScopeSpec.isInvalid()) 16766 return ExprError(); 16767 SmallVector<UnresolvedSet<8>, 4> Lookups; 16768 if (S) { 16769 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName); 16770 Lookup.suppressDiagnostics(); 16771 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) { 16772 NamedDecl *D = Lookup.getRepresentativeDecl(); 16773 do { 16774 S = S->getParent(); 16775 } while (S && !S->isDeclScope(D)); 16776 if (S) 16777 S = S->getParent(); 16778 Lookups.emplace_back(); 16779 Lookups.back().append(Lookup.begin(), Lookup.end()); 16780 Lookup.clear(); 16781 } 16782 } else if (auto *ULE = 16783 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) { 16784 Lookups.push_back(UnresolvedSet<8>()); 16785 Decl *PrevD = nullptr; 16786 for (NamedDecl *D : ULE->decls()) { 16787 if (D == PrevD) 16788 Lookups.push_back(UnresolvedSet<8>()); 16789 else if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(D)) 16790 Lookups.back().addDecl(DRD); 16791 PrevD = D; 16792 } 16793 } 16794 if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() || 16795 Ty->isInstantiationDependentType() || 16796 Ty->containsUnexpandedParameterPack() || 16797 filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) { 16798 return !D->isInvalidDecl() && 16799 (D->getType()->isDependentType() || 16800 D->getType()->isInstantiationDependentType() || 16801 D->getType()->containsUnexpandedParameterPack()); 16802 })) { 16803 UnresolvedSet<8> ResSet; 16804 for (const UnresolvedSet<8> &Set : Lookups) { 16805 if (Set.empty()) 16806 continue; 16807 ResSet.append(Set.begin(), Set.end()); 16808 // The last item marks the end of all declarations at the specified scope. 16809 ResSet.addDecl(Set[Set.size() - 1]); 16810 } 16811 return UnresolvedLookupExpr::Create( 16812 SemaRef.Context, /*NamingClass=*/nullptr, 16813 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId, 16814 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end()); 16815 } 16816 // Lookup inside the classes. 16817 // C++ [over.match.oper]p3: 16818 // For a unary operator @ with an operand of a type whose 16819 // cv-unqualified version is T1, and for a binary operator @ with 16820 // a left operand of a type whose cv-unqualified version is T1 and 16821 // a right operand of a type whose cv-unqualified version is T2, 16822 // three sets of candidate functions, designated member 16823 // candidates, non-member candidates and built-in candidates, are 16824 // constructed as follows: 16825 // -- If T1 is a complete class type or a class currently being 16826 // defined, the set of member candidates is the result of the 16827 // qualified lookup of T1::operator@ (13.3.1.1.1); otherwise, 16828 // the set of member candidates is empty. 16829 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName); 16830 Lookup.suppressDiagnostics(); 16831 if (const auto *TyRec = Ty->getAs<RecordType>()) { 16832 // Complete the type if it can be completed. 16833 // If the type is neither complete nor being defined, bail out now. 16834 if (SemaRef.isCompleteType(Loc, Ty) || TyRec->isBeingDefined() || 16835 TyRec->getDecl()->getDefinition()) { 16836 Lookup.clear(); 16837 SemaRef.LookupQualifiedName(Lookup, TyRec->getDecl()); 16838 if (Lookup.empty()) { 16839 Lookups.emplace_back(); 16840 Lookups.back().append(Lookup.begin(), Lookup.end()); 16841 } 16842 } 16843 } 16844 // Perform ADL. 16845 if (SemaRef.getLangOpts().CPlusPlus) 16846 argumentDependentLookup(SemaRef, ReductionId, Loc, Ty, Lookups); 16847 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 16848 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * { 16849 if (!D->isInvalidDecl() && 16850 SemaRef.Context.hasSameType(D->getType(), Ty)) 16851 return D; 16852 return nullptr; 16853 })) 16854 return SemaRef.BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(), 16855 VK_LValue, Loc); 16856 if (SemaRef.getLangOpts().CPlusPlus) { 16857 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 16858 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * { 16859 if (!D->isInvalidDecl() && 16860 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) && 16861 !Ty.isMoreQualifiedThan(D->getType())) 16862 return D; 16863 return nullptr; 16864 })) { 16865 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, 16866 /*DetectVirtual=*/false); 16867 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) { 16868 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType( 16869 VD->getType().getUnqualifiedType()))) { 16870 if (SemaRef.CheckBaseClassAccess( 16871 Loc, VD->getType(), Ty, Paths.front(), 16872 /*DiagID=*/0) != Sema::AR_inaccessible) { 16873 SemaRef.BuildBasePathArray(Paths, BasePath); 16874 return SemaRef.BuildDeclRefExpr( 16875 VD, VD->getType().getNonReferenceType(), VK_LValue, Loc); 16876 } 16877 } 16878 } 16879 } 16880 } 16881 if (ReductionIdScopeSpec.isSet()) { 16882 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) 16883 << Ty << Range; 16884 return ExprError(); 16885 } 16886 return ExprEmpty(); 16887 } 16888 16889 namespace { 16890 /// Data for the reduction-based clauses. 16891 struct ReductionData { 16892 /// List of original reduction items. 16893 SmallVector<Expr *, 8> Vars; 16894 /// List of private copies of the reduction items. 16895 SmallVector<Expr *, 8> Privates; 16896 /// LHS expressions for the reduction_op expressions. 16897 SmallVector<Expr *, 8> LHSs; 16898 /// RHS expressions for the reduction_op expressions. 16899 SmallVector<Expr *, 8> RHSs; 16900 /// Reduction operation expression. 16901 SmallVector<Expr *, 8> ReductionOps; 16902 /// inscan copy operation expressions. 16903 SmallVector<Expr *, 8> InscanCopyOps; 16904 /// inscan copy temp array expressions for prefix sums. 16905 SmallVector<Expr *, 8> InscanCopyArrayTemps; 16906 /// inscan copy temp array element expressions for prefix sums. 16907 SmallVector<Expr *, 8> InscanCopyArrayElems; 16908 /// Taskgroup descriptors for the corresponding reduction items in 16909 /// in_reduction clauses. 16910 SmallVector<Expr *, 8> TaskgroupDescriptors; 16911 /// List of captures for clause. 16912 SmallVector<Decl *, 4> ExprCaptures; 16913 /// List of postupdate expressions. 16914 SmallVector<Expr *, 4> ExprPostUpdates; 16915 /// Reduction modifier. 16916 unsigned RedModifier = 0; 16917 ReductionData() = delete; 16918 /// Reserves required memory for the reduction data. 16919 ReductionData(unsigned Size, unsigned Modifier = 0) : RedModifier(Modifier) { 16920 Vars.reserve(Size); 16921 Privates.reserve(Size); 16922 LHSs.reserve(Size); 16923 RHSs.reserve(Size); 16924 ReductionOps.reserve(Size); 16925 if (RedModifier == OMPC_REDUCTION_inscan) { 16926 InscanCopyOps.reserve(Size); 16927 InscanCopyArrayTemps.reserve(Size); 16928 InscanCopyArrayElems.reserve(Size); 16929 } 16930 TaskgroupDescriptors.reserve(Size); 16931 ExprCaptures.reserve(Size); 16932 ExprPostUpdates.reserve(Size); 16933 } 16934 /// Stores reduction item and reduction operation only (required for dependent 16935 /// reduction item). 16936 void push(Expr *Item, Expr *ReductionOp) { 16937 Vars.emplace_back(Item); 16938 Privates.emplace_back(nullptr); 16939 LHSs.emplace_back(nullptr); 16940 RHSs.emplace_back(nullptr); 16941 ReductionOps.emplace_back(ReductionOp); 16942 TaskgroupDescriptors.emplace_back(nullptr); 16943 if (RedModifier == OMPC_REDUCTION_inscan) { 16944 InscanCopyOps.push_back(nullptr); 16945 InscanCopyArrayTemps.push_back(nullptr); 16946 InscanCopyArrayElems.push_back(nullptr); 16947 } 16948 } 16949 /// Stores reduction data. 16950 void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp, 16951 Expr *TaskgroupDescriptor, Expr *CopyOp, Expr *CopyArrayTemp, 16952 Expr *CopyArrayElem) { 16953 Vars.emplace_back(Item); 16954 Privates.emplace_back(Private); 16955 LHSs.emplace_back(LHS); 16956 RHSs.emplace_back(RHS); 16957 ReductionOps.emplace_back(ReductionOp); 16958 TaskgroupDescriptors.emplace_back(TaskgroupDescriptor); 16959 if (RedModifier == OMPC_REDUCTION_inscan) { 16960 InscanCopyOps.push_back(CopyOp); 16961 InscanCopyArrayTemps.push_back(CopyArrayTemp); 16962 InscanCopyArrayElems.push_back(CopyArrayElem); 16963 } else { 16964 assert(CopyOp == nullptr && CopyArrayTemp == nullptr && 16965 CopyArrayElem == nullptr && 16966 "Copy operation must be used for inscan reductions only."); 16967 } 16968 } 16969 }; 16970 } // namespace 16971 16972 static bool checkOMPArraySectionConstantForReduction( 16973 ASTContext &Context, const OMPArraySectionExpr *OASE, bool &SingleElement, 16974 SmallVectorImpl<llvm::APSInt> &ArraySizes) { 16975 const Expr *Length = OASE->getLength(); 16976 if (Length == nullptr) { 16977 // For array sections of the form [1:] or [:], we would need to analyze 16978 // the lower bound... 16979 if (OASE->getColonLocFirst().isValid()) 16980 return false; 16981 16982 // This is an array subscript which has implicit length 1! 16983 SingleElement = true; 16984 ArraySizes.push_back(llvm::APSInt::get(1)); 16985 } else { 16986 Expr::EvalResult Result; 16987 if (!Length->EvaluateAsInt(Result, Context)) 16988 return false; 16989 16990 llvm::APSInt ConstantLengthValue = Result.Val.getInt(); 16991 SingleElement = (ConstantLengthValue.getSExtValue() == 1); 16992 ArraySizes.push_back(ConstantLengthValue); 16993 } 16994 16995 // Get the base of this array section and walk up from there. 16996 const Expr *Base = OASE->getBase()->IgnoreParenImpCasts(); 16997 16998 // We require length = 1 for all array sections except the right-most to 16999 // guarantee that the memory region is contiguous and has no holes in it. 17000 while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) { 17001 Length = TempOASE->getLength(); 17002 if (Length == nullptr) { 17003 // For array sections of the form [1:] or [:], we would need to analyze 17004 // the lower bound... 17005 if (OASE->getColonLocFirst().isValid()) 17006 return false; 17007 17008 // This is an array subscript which has implicit length 1! 17009 ArraySizes.push_back(llvm::APSInt::get(1)); 17010 } else { 17011 Expr::EvalResult Result; 17012 if (!Length->EvaluateAsInt(Result, Context)) 17013 return false; 17014 17015 llvm::APSInt ConstantLengthValue = Result.Val.getInt(); 17016 if (ConstantLengthValue.getSExtValue() != 1) 17017 return false; 17018 17019 ArraySizes.push_back(ConstantLengthValue); 17020 } 17021 Base = TempOASE->getBase()->IgnoreParenImpCasts(); 17022 } 17023 17024 // If we have a single element, we don't need to add the implicit lengths. 17025 if (!SingleElement) { 17026 while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) { 17027 // Has implicit length 1! 17028 ArraySizes.push_back(llvm::APSInt::get(1)); 17029 Base = TempASE->getBase()->IgnoreParenImpCasts(); 17030 } 17031 } 17032 17033 // This array section can be privatized as a single value or as a constant 17034 // sized array. 17035 return true; 17036 } 17037 17038 static BinaryOperatorKind 17039 getRelatedCompoundReductionOp(BinaryOperatorKind BOK) { 17040 if (BOK == BO_Add) 17041 return BO_AddAssign; 17042 if (BOK == BO_Mul) 17043 return BO_MulAssign; 17044 if (BOK == BO_And) 17045 return BO_AndAssign; 17046 if (BOK == BO_Or) 17047 return BO_OrAssign; 17048 if (BOK == BO_Xor) 17049 return BO_XorAssign; 17050 return BOK; 17051 } 17052 17053 static bool actOnOMPReductionKindClause( 17054 Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind, 17055 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 17056 SourceLocation ColonLoc, SourceLocation EndLoc, 17057 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 17058 ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) { 17059 DeclarationName DN = ReductionId.getName(); 17060 OverloadedOperatorKind OOK = DN.getCXXOverloadedOperator(); 17061 BinaryOperatorKind BOK = BO_Comma; 17062 17063 ASTContext &Context = S.Context; 17064 // OpenMP [2.14.3.6, reduction clause] 17065 // C 17066 // reduction-identifier is either an identifier or one of the following 17067 // operators: +, -, *, &, |, ^, && and || 17068 // C++ 17069 // reduction-identifier is either an id-expression or one of the following 17070 // operators: +, -, *, &, |, ^, && and || 17071 switch (OOK) { 17072 case OO_Plus: 17073 case OO_Minus: 17074 BOK = BO_Add; 17075 break; 17076 case OO_Star: 17077 BOK = BO_Mul; 17078 break; 17079 case OO_Amp: 17080 BOK = BO_And; 17081 break; 17082 case OO_Pipe: 17083 BOK = BO_Or; 17084 break; 17085 case OO_Caret: 17086 BOK = BO_Xor; 17087 break; 17088 case OO_AmpAmp: 17089 BOK = BO_LAnd; 17090 break; 17091 case OO_PipePipe: 17092 BOK = BO_LOr; 17093 break; 17094 case OO_New: 17095 case OO_Delete: 17096 case OO_Array_New: 17097 case OO_Array_Delete: 17098 case OO_Slash: 17099 case OO_Percent: 17100 case OO_Tilde: 17101 case OO_Exclaim: 17102 case OO_Equal: 17103 case OO_Less: 17104 case OO_Greater: 17105 case OO_LessEqual: 17106 case OO_GreaterEqual: 17107 case OO_PlusEqual: 17108 case OO_MinusEqual: 17109 case OO_StarEqual: 17110 case OO_SlashEqual: 17111 case OO_PercentEqual: 17112 case OO_CaretEqual: 17113 case OO_AmpEqual: 17114 case OO_PipeEqual: 17115 case OO_LessLess: 17116 case OO_GreaterGreater: 17117 case OO_LessLessEqual: 17118 case OO_GreaterGreaterEqual: 17119 case OO_EqualEqual: 17120 case OO_ExclaimEqual: 17121 case OO_Spaceship: 17122 case OO_PlusPlus: 17123 case OO_MinusMinus: 17124 case OO_Comma: 17125 case OO_ArrowStar: 17126 case OO_Arrow: 17127 case OO_Call: 17128 case OO_Subscript: 17129 case OO_Conditional: 17130 case OO_Coawait: 17131 case NUM_OVERLOADED_OPERATORS: 17132 llvm_unreachable("Unexpected reduction identifier"); 17133 case OO_None: 17134 if (IdentifierInfo *II = DN.getAsIdentifierInfo()) { 17135 if (II->isStr("max")) 17136 BOK = BO_GT; 17137 else if (II->isStr("min")) 17138 BOK = BO_LT; 17139 } 17140 break; 17141 } 17142 SourceRange ReductionIdRange; 17143 if (ReductionIdScopeSpec.isValid()) 17144 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc()); 17145 else 17146 ReductionIdRange.setBegin(ReductionId.getBeginLoc()); 17147 ReductionIdRange.setEnd(ReductionId.getEndLoc()); 17148 17149 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end(); 17150 bool FirstIter = true; 17151 for (Expr *RefExpr : VarList) { 17152 assert(RefExpr && "nullptr expr in OpenMP reduction clause."); 17153 // OpenMP [2.1, C/C++] 17154 // A list item is a variable or array section, subject to the restrictions 17155 // specified in Section 2.4 on page 42 and in each of the sections 17156 // describing clauses and directives for which a list appears. 17157 // OpenMP [2.14.3.3, Restrictions, p.1] 17158 // A variable that is part of another variable (as an array or 17159 // structure element) cannot appear in a private clause. 17160 if (!FirstIter && IR != ER) 17161 ++IR; 17162 FirstIter = false; 17163 SourceLocation ELoc; 17164 SourceRange ERange; 17165 Expr *SimpleRefExpr = RefExpr; 17166 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange, 17167 /*AllowArraySection=*/true); 17168 if (Res.second) { 17169 // Try to find 'declare reduction' corresponding construct before using 17170 // builtin/overloaded operators. 17171 QualType Type = Context.DependentTy; 17172 CXXCastPath BasePath; 17173 ExprResult DeclareReductionRef = buildDeclareReductionRef( 17174 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec, 17175 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR); 17176 Expr *ReductionOp = nullptr; 17177 if (S.CurContext->isDependentContext() && 17178 (DeclareReductionRef.isUnset() || 17179 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) 17180 ReductionOp = DeclareReductionRef.get(); 17181 // It will be analyzed later. 17182 RD.push(RefExpr, ReductionOp); 17183 } 17184 ValueDecl *D = Res.first; 17185 if (!D) 17186 continue; 17187 17188 Expr *TaskgroupDescriptor = nullptr; 17189 QualType Type; 17190 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens()); 17191 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens()); 17192 if (ASE) { 17193 Type = ASE->getType().getNonReferenceType(); 17194 } else if (OASE) { 17195 QualType BaseType = 17196 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 17197 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 17198 Type = ATy->getElementType(); 17199 else 17200 Type = BaseType->getPointeeType(); 17201 Type = Type.getNonReferenceType(); 17202 } else { 17203 Type = Context.getBaseElementType(D->getType().getNonReferenceType()); 17204 } 17205 auto *VD = dyn_cast<VarDecl>(D); 17206 17207 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 17208 // A variable that appears in a private clause must not have an incomplete 17209 // type or a reference type. 17210 if (S.RequireCompleteType(ELoc, D->getType(), 17211 diag::err_omp_reduction_incomplete_type)) 17212 continue; 17213 // OpenMP [2.14.3.6, reduction clause, Restrictions] 17214 // A list item that appears in a reduction clause must not be 17215 // const-qualified. 17216 if (rejectConstNotMutableType(S, D, Type, ClauseKind, ELoc, 17217 /*AcceptIfMutable*/ false, ASE || OASE)) 17218 continue; 17219 17220 OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective(); 17221 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4] 17222 // If a list-item is a reference type then it must bind to the same object 17223 // for all threads of the team. 17224 if (!ASE && !OASE) { 17225 if (VD) { 17226 VarDecl *VDDef = VD->getDefinition(); 17227 if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) { 17228 DSARefChecker Check(Stack); 17229 if (Check.Visit(VDDef->getInit())) { 17230 S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg) 17231 << getOpenMPClauseName(ClauseKind) << ERange; 17232 S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef; 17233 continue; 17234 } 17235 } 17236 } 17237 17238 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 17239 // in a Construct] 17240 // Variables with the predetermined data-sharing attributes may not be 17241 // listed in data-sharing attributes clauses, except for the cases 17242 // listed below. For these exceptions only, listing a predetermined 17243 // variable in a data-sharing attribute clause is allowed and overrides 17244 // the variable's predetermined data-sharing attributes. 17245 // OpenMP [2.14.3.6, Restrictions, p.3] 17246 // Any number of reduction clauses can be specified on the directive, 17247 // but a list item can appear only once in the reduction clauses for that 17248 // directive. 17249 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false); 17250 if (DVar.CKind == OMPC_reduction) { 17251 S.Diag(ELoc, diag::err_omp_once_referenced) 17252 << getOpenMPClauseName(ClauseKind); 17253 if (DVar.RefExpr) 17254 S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced); 17255 continue; 17256 } 17257 if (DVar.CKind != OMPC_unknown) { 17258 S.Diag(ELoc, diag::err_omp_wrong_dsa) 17259 << getOpenMPClauseName(DVar.CKind) 17260 << getOpenMPClauseName(OMPC_reduction); 17261 reportOriginalDsa(S, Stack, D, DVar); 17262 continue; 17263 } 17264 17265 // OpenMP [2.14.3.6, Restrictions, p.1] 17266 // A list item that appears in a reduction clause of a worksharing 17267 // construct must be shared in the parallel regions to which any of the 17268 // worksharing regions arising from the worksharing construct bind. 17269 if (isOpenMPWorksharingDirective(CurrDir) && 17270 !isOpenMPParallelDirective(CurrDir) && 17271 !isOpenMPTeamsDirective(CurrDir)) { 17272 DVar = Stack->getImplicitDSA(D, true); 17273 if (DVar.CKind != OMPC_shared) { 17274 S.Diag(ELoc, diag::err_omp_required_access) 17275 << getOpenMPClauseName(OMPC_reduction) 17276 << getOpenMPClauseName(OMPC_shared); 17277 reportOriginalDsa(S, Stack, D, DVar); 17278 continue; 17279 } 17280 } 17281 } else { 17282 // Threadprivates cannot be shared between threads, so dignose if the base 17283 // is a threadprivate variable. 17284 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false); 17285 if (DVar.CKind == OMPC_threadprivate) { 17286 S.Diag(ELoc, diag::err_omp_wrong_dsa) 17287 << getOpenMPClauseName(DVar.CKind) 17288 << getOpenMPClauseName(OMPC_reduction); 17289 reportOriginalDsa(S, Stack, D, DVar); 17290 continue; 17291 } 17292 } 17293 17294 // Try to find 'declare reduction' corresponding construct before using 17295 // builtin/overloaded operators. 17296 CXXCastPath BasePath; 17297 ExprResult DeclareReductionRef = buildDeclareReductionRef( 17298 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec, 17299 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR); 17300 if (DeclareReductionRef.isInvalid()) 17301 continue; 17302 if (S.CurContext->isDependentContext() && 17303 (DeclareReductionRef.isUnset() || 17304 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) { 17305 RD.push(RefExpr, DeclareReductionRef.get()); 17306 continue; 17307 } 17308 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) { 17309 // Not allowed reduction identifier is found. 17310 S.Diag(ReductionId.getBeginLoc(), 17311 diag::err_omp_unknown_reduction_identifier) 17312 << Type << ReductionIdRange; 17313 continue; 17314 } 17315 17316 // OpenMP [2.14.3.6, reduction clause, Restrictions] 17317 // The type of a list item that appears in a reduction clause must be valid 17318 // for the reduction-identifier. For a max or min reduction in C, the type 17319 // of the list item must be an allowed arithmetic data type: char, int, 17320 // float, double, or _Bool, possibly modified with long, short, signed, or 17321 // unsigned. For a max or min reduction in C++, the type of the list item 17322 // must be an allowed arithmetic data type: char, wchar_t, int, float, 17323 // double, or bool, possibly modified with long, short, signed, or unsigned. 17324 if (DeclareReductionRef.isUnset()) { 17325 if ((BOK == BO_GT || BOK == BO_LT) && 17326 !(Type->isScalarType() || 17327 (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) { 17328 S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg) 17329 << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus; 17330 if (!ASE && !OASE) { 17331 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 17332 VarDecl::DeclarationOnly; 17333 S.Diag(D->getLocation(), 17334 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 17335 << D; 17336 } 17337 continue; 17338 } 17339 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) && 17340 !S.getLangOpts().CPlusPlus && Type->isFloatingType()) { 17341 S.Diag(ELoc, diag::err_omp_clause_floating_type_arg) 17342 << getOpenMPClauseName(ClauseKind); 17343 if (!ASE && !OASE) { 17344 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 17345 VarDecl::DeclarationOnly; 17346 S.Diag(D->getLocation(), 17347 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 17348 << D; 17349 } 17350 continue; 17351 } 17352 } 17353 17354 Type = Type.getNonLValueExprType(Context).getUnqualifiedType(); 17355 VarDecl *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs", 17356 D->hasAttrs() ? &D->getAttrs() : nullptr); 17357 VarDecl *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(), 17358 D->hasAttrs() ? &D->getAttrs() : nullptr); 17359 QualType PrivateTy = Type; 17360 17361 // Try if we can determine constant lengths for all array sections and avoid 17362 // the VLA. 17363 bool ConstantLengthOASE = false; 17364 if (OASE) { 17365 bool SingleElement; 17366 llvm::SmallVector<llvm::APSInt, 4> ArraySizes; 17367 ConstantLengthOASE = checkOMPArraySectionConstantForReduction( 17368 Context, OASE, SingleElement, ArraySizes); 17369 17370 // If we don't have a single element, we must emit a constant array type. 17371 if (ConstantLengthOASE && !SingleElement) { 17372 for (llvm::APSInt &Size : ArraySizes) 17373 PrivateTy = Context.getConstantArrayType(PrivateTy, Size, nullptr, 17374 ArrayType::Normal, 17375 /*IndexTypeQuals=*/0); 17376 } 17377 } 17378 17379 if ((OASE && !ConstantLengthOASE) || 17380 (!OASE && !ASE && 17381 D->getType().getNonReferenceType()->isVariablyModifiedType())) { 17382 if (!Context.getTargetInfo().isVLASupported()) { 17383 if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective())) { 17384 S.Diag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE; 17385 S.Diag(ELoc, diag::note_vla_unsupported); 17386 continue; 17387 } else { 17388 S.targetDiag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE; 17389 S.targetDiag(ELoc, diag::note_vla_unsupported); 17390 } 17391 } 17392 // For arrays/array sections only: 17393 // Create pseudo array type for private copy. The size for this array will 17394 // be generated during codegen. 17395 // For array subscripts or single variables Private Ty is the same as Type 17396 // (type of the variable or single array element). 17397 PrivateTy = Context.getVariableArrayType( 17398 Type, 17399 new (Context) 17400 OpaqueValueExpr(ELoc, Context.getSizeType(), VK_PRValue), 17401 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange()); 17402 } else if (!ASE && !OASE && 17403 Context.getAsArrayType(D->getType().getNonReferenceType())) { 17404 PrivateTy = D->getType().getNonReferenceType(); 17405 } 17406 // Private copy. 17407 VarDecl *PrivateVD = 17408 buildVarDecl(S, ELoc, PrivateTy, D->getName(), 17409 D->hasAttrs() ? &D->getAttrs() : nullptr, 17410 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 17411 // Add initializer for private variable. 17412 Expr *Init = nullptr; 17413 DeclRefExpr *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc); 17414 DeclRefExpr *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc); 17415 if (DeclareReductionRef.isUsable()) { 17416 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>(); 17417 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl()); 17418 if (DRD->getInitializer()) { 17419 Init = DRDRef; 17420 RHSVD->setInit(DRDRef); 17421 RHSVD->setInitStyle(VarDecl::CallInit); 17422 } 17423 } else { 17424 switch (BOK) { 17425 case BO_Add: 17426 case BO_Xor: 17427 case BO_Or: 17428 case BO_LOr: 17429 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'. 17430 if (Type->isScalarType() || Type->isAnyComplexType()) 17431 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get(); 17432 break; 17433 case BO_Mul: 17434 case BO_LAnd: 17435 if (Type->isScalarType() || Type->isAnyComplexType()) { 17436 // '*' and '&&' reduction ops - initializer is '1'. 17437 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get(); 17438 } 17439 break; 17440 case BO_And: { 17441 // '&' reduction op - initializer is '~0'. 17442 QualType OrigType = Type; 17443 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) 17444 Type = ComplexTy->getElementType(); 17445 if (Type->isRealFloatingType()) { 17446 llvm::APFloat InitValue = llvm::APFloat::getAllOnesValue( 17447 Context.getFloatTypeSemantics(Type)); 17448 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, 17449 Type, ELoc); 17450 } else if (Type->isScalarType()) { 17451 uint64_t Size = Context.getTypeSize(Type); 17452 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0); 17453 llvm::APInt InitValue = llvm::APInt::getAllOnes(Size); 17454 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); 17455 } 17456 if (Init && OrigType->isAnyComplexType()) { 17457 // Init = 0xFFFF + 0xFFFFi; 17458 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType); 17459 Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get(); 17460 } 17461 Type = OrigType; 17462 break; 17463 } 17464 case BO_LT: 17465 case BO_GT: { 17466 // 'min' reduction op - initializer is 'Largest representable number in 17467 // the reduction list item type'. 17468 // 'max' reduction op - initializer is 'Least representable number in 17469 // the reduction list item type'. 17470 if (Type->isIntegerType() || Type->isPointerType()) { 17471 bool IsSigned = Type->hasSignedIntegerRepresentation(); 17472 uint64_t Size = Context.getTypeSize(Type); 17473 QualType IntTy = 17474 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned); 17475 llvm::APInt InitValue = 17476 (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size) 17477 : llvm::APInt::getMinValue(Size) 17478 : IsSigned ? llvm::APInt::getSignedMaxValue(Size) 17479 : llvm::APInt::getMaxValue(Size); 17480 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); 17481 if (Type->isPointerType()) { 17482 // Cast to pointer type. 17483 ExprResult CastExpr = S.BuildCStyleCastExpr( 17484 ELoc, Context.getTrivialTypeSourceInfo(Type, ELoc), ELoc, Init); 17485 if (CastExpr.isInvalid()) 17486 continue; 17487 Init = CastExpr.get(); 17488 } 17489 } else if (Type->isRealFloatingType()) { 17490 llvm::APFloat InitValue = llvm::APFloat::getLargest( 17491 Context.getFloatTypeSemantics(Type), BOK != BO_LT); 17492 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, 17493 Type, ELoc); 17494 } 17495 break; 17496 } 17497 case BO_PtrMemD: 17498 case BO_PtrMemI: 17499 case BO_MulAssign: 17500 case BO_Div: 17501 case BO_Rem: 17502 case BO_Sub: 17503 case BO_Shl: 17504 case BO_Shr: 17505 case BO_LE: 17506 case BO_GE: 17507 case BO_EQ: 17508 case BO_NE: 17509 case BO_Cmp: 17510 case BO_AndAssign: 17511 case BO_XorAssign: 17512 case BO_OrAssign: 17513 case BO_Assign: 17514 case BO_AddAssign: 17515 case BO_SubAssign: 17516 case BO_DivAssign: 17517 case BO_RemAssign: 17518 case BO_ShlAssign: 17519 case BO_ShrAssign: 17520 case BO_Comma: 17521 llvm_unreachable("Unexpected reduction operation"); 17522 } 17523 } 17524 if (Init && DeclareReductionRef.isUnset()) { 17525 S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false); 17526 // Store initializer for single element in private copy. Will be used 17527 // during codegen. 17528 PrivateVD->setInit(RHSVD->getInit()); 17529 PrivateVD->setInitStyle(RHSVD->getInitStyle()); 17530 } else if (!Init) { 17531 S.ActOnUninitializedDecl(RHSVD); 17532 // Store initializer for single element in private copy. Will be used 17533 // during codegen. 17534 PrivateVD->setInit(RHSVD->getInit()); 17535 PrivateVD->setInitStyle(RHSVD->getInitStyle()); 17536 } 17537 if (RHSVD->isInvalidDecl()) 17538 continue; 17539 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) { 17540 S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible) 17541 << Type << ReductionIdRange; 17542 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 17543 VarDecl::DeclarationOnly; 17544 S.Diag(D->getLocation(), 17545 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 17546 << D; 17547 continue; 17548 } 17549 DeclRefExpr *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc); 17550 ExprResult ReductionOp; 17551 if (DeclareReductionRef.isUsable()) { 17552 QualType RedTy = DeclareReductionRef.get()->getType(); 17553 QualType PtrRedTy = Context.getPointerType(RedTy); 17554 ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE); 17555 ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE); 17556 if (!BasePath.empty()) { 17557 LHS = S.DefaultLvalueConversion(LHS.get()); 17558 RHS = S.DefaultLvalueConversion(RHS.get()); 17559 LHS = ImplicitCastExpr::Create( 17560 Context, PtrRedTy, CK_UncheckedDerivedToBase, LHS.get(), &BasePath, 17561 LHS.get()->getValueKind(), FPOptionsOverride()); 17562 RHS = ImplicitCastExpr::Create( 17563 Context, PtrRedTy, CK_UncheckedDerivedToBase, RHS.get(), &BasePath, 17564 RHS.get()->getValueKind(), FPOptionsOverride()); 17565 } 17566 FunctionProtoType::ExtProtoInfo EPI; 17567 QualType Params[] = {PtrRedTy, PtrRedTy}; 17568 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI); 17569 auto *OVE = new (Context) OpaqueValueExpr( 17570 ELoc, Context.getPointerType(FnTy), VK_PRValue, OK_Ordinary, 17571 S.DefaultLvalueConversion(DeclareReductionRef.get()).get()); 17572 Expr *Args[] = {LHS.get(), RHS.get()}; 17573 ReductionOp = 17574 CallExpr::Create(Context, OVE, Args, Context.VoidTy, VK_PRValue, ELoc, 17575 S.CurFPFeatureOverrides()); 17576 } else { 17577 BinaryOperatorKind CombBOK = getRelatedCompoundReductionOp(BOK); 17578 if (Type->isRecordType() && CombBOK != BOK) { 17579 Sema::TentativeAnalysisScope Trap(S); 17580 ReductionOp = 17581 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(), 17582 CombBOK, LHSDRE, RHSDRE); 17583 } 17584 if (!ReductionOp.isUsable()) { 17585 ReductionOp = 17586 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(), BOK, 17587 LHSDRE, RHSDRE); 17588 if (ReductionOp.isUsable()) { 17589 if (BOK != BO_LT && BOK != BO_GT) { 17590 ReductionOp = 17591 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(), 17592 BO_Assign, LHSDRE, ReductionOp.get()); 17593 } else { 17594 auto *ConditionalOp = new (Context) 17595 ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc, 17596 RHSDRE, Type, VK_LValue, OK_Ordinary); 17597 ReductionOp = 17598 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(), 17599 BO_Assign, LHSDRE, ConditionalOp); 17600 } 17601 } 17602 } 17603 if (ReductionOp.isUsable()) 17604 ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get(), 17605 /*DiscardedValue*/ false); 17606 if (!ReductionOp.isUsable()) 17607 continue; 17608 } 17609 17610 // Add copy operations for inscan reductions. 17611 // LHS = RHS; 17612 ExprResult CopyOpRes, TempArrayRes, TempArrayElem; 17613 if (ClauseKind == OMPC_reduction && 17614 RD.RedModifier == OMPC_REDUCTION_inscan) { 17615 ExprResult RHS = S.DefaultLvalueConversion(RHSDRE); 17616 CopyOpRes = S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, LHSDRE, 17617 RHS.get()); 17618 if (!CopyOpRes.isUsable()) 17619 continue; 17620 CopyOpRes = 17621 S.ActOnFinishFullExpr(CopyOpRes.get(), /*DiscardedValue=*/true); 17622 if (!CopyOpRes.isUsable()) 17623 continue; 17624 // For simd directive and simd-based directives in simd mode no need to 17625 // construct temp array, need just a single temp element. 17626 if (Stack->getCurrentDirective() == OMPD_simd || 17627 (S.getLangOpts().OpenMPSimd && 17628 isOpenMPSimdDirective(Stack->getCurrentDirective()))) { 17629 VarDecl *TempArrayVD = 17630 buildVarDecl(S, ELoc, PrivateTy, D->getName(), 17631 D->hasAttrs() ? &D->getAttrs() : nullptr); 17632 // Add a constructor to the temp decl. 17633 S.ActOnUninitializedDecl(TempArrayVD); 17634 TempArrayRes = buildDeclRefExpr(S, TempArrayVD, PrivateTy, ELoc); 17635 } else { 17636 // Build temp array for prefix sum. 17637 auto *Dim = new (S.Context) 17638 OpaqueValueExpr(ELoc, S.Context.getSizeType(), VK_PRValue); 17639 QualType ArrayTy = 17640 S.Context.getVariableArrayType(PrivateTy, Dim, ArrayType::Normal, 17641 /*IndexTypeQuals=*/0, {ELoc, ELoc}); 17642 VarDecl *TempArrayVD = 17643 buildVarDecl(S, ELoc, ArrayTy, D->getName(), 17644 D->hasAttrs() ? &D->getAttrs() : nullptr); 17645 // Add a constructor to the temp decl. 17646 S.ActOnUninitializedDecl(TempArrayVD); 17647 TempArrayRes = buildDeclRefExpr(S, TempArrayVD, ArrayTy, ELoc); 17648 TempArrayElem = 17649 S.DefaultFunctionArrayLvalueConversion(TempArrayRes.get()); 17650 auto *Idx = new (S.Context) 17651 OpaqueValueExpr(ELoc, S.Context.getSizeType(), VK_PRValue); 17652 TempArrayElem = S.CreateBuiltinArraySubscriptExpr(TempArrayElem.get(), 17653 ELoc, Idx, ELoc); 17654 } 17655 } 17656 17657 // OpenMP [2.15.4.6, Restrictions, p.2] 17658 // A list item that appears in an in_reduction clause of a task construct 17659 // must appear in a task_reduction clause of a construct associated with a 17660 // taskgroup region that includes the participating task in its taskgroup 17661 // set. The construct associated with the innermost region that meets this 17662 // condition must specify the same reduction-identifier as the in_reduction 17663 // clause. 17664 if (ClauseKind == OMPC_in_reduction) { 17665 SourceRange ParentSR; 17666 BinaryOperatorKind ParentBOK; 17667 const Expr *ParentReductionOp = nullptr; 17668 Expr *ParentBOKTD = nullptr, *ParentReductionOpTD = nullptr; 17669 DSAStackTy::DSAVarData ParentBOKDSA = 17670 Stack->getTopMostTaskgroupReductionData(D, ParentSR, ParentBOK, 17671 ParentBOKTD); 17672 DSAStackTy::DSAVarData ParentReductionOpDSA = 17673 Stack->getTopMostTaskgroupReductionData( 17674 D, ParentSR, ParentReductionOp, ParentReductionOpTD); 17675 bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown; 17676 bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown; 17677 if ((DeclareReductionRef.isUnset() && IsParentReductionOp) || 17678 (DeclareReductionRef.isUsable() && IsParentBOK) || 17679 (IsParentBOK && BOK != ParentBOK) || IsParentReductionOp) { 17680 bool EmitError = true; 17681 if (IsParentReductionOp && DeclareReductionRef.isUsable()) { 17682 llvm::FoldingSetNodeID RedId, ParentRedId; 17683 ParentReductionOp->Profile(ParentRedId, Context, /*Canonical=*/true); 17684 DeclareReductionRef.get()->Profile(RedId, Context, 17685 /*Canonical=*/true); 17686 EmitError = RedId != ParentRedId; 17687 } 17688 if (EmitError) { 17689 S.Diag(ReductionId.getBeginLoc(), 17690 diag::err_omp_reduction_identifier_mismatch) 17691 << ReductionIdRange << RefExpr->getSourceRange(); 17692 S.Diag(ParentSR.getBegin(), 17693 diag::note_omp_previous_reduction_identifier) 17694 << ParentSR 17695 << (IsParentBOK ? ParentBOKDSA.RefExpr 17696 : ParentReductionOpDSA.RefExpr) 17697 ->getSourceRange(); 17698 continue; 17699 } 17700 } 17701 TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD; 17702 } 17703 17704 DeclRefExpr *Ref = nullptr; 17705 Expr *VarsExpr = RefExpr->IgnoreParens(); 17706 if (!VD && !S.CurContext->isDependentContext()) { 17707 if (ASE || OASE) { 17708 TransformExprToCaptures RebuildToCapture(S, D); 17709 VarsExpr = 17710 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get(); 17711 Ref = RebuildToCapture.getCapturedExpr(); 17712 } else { 17713 VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false); 17714 } 17715 if (!S.isOpenMPCapturedDecl(D)) { 17716 RD.ExprCaptures.emplace_back(Ref->getDecl()); 17717 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) { 17718 ExprResult RefRes = S.DefaultLvalueConversion(Ref); 17719 if (!RefRes.isUsable()) 17720 continue; 17721 ExprResult PostUpdateRes = 17722 S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr, 17723 RefRes.get()); 17724 if (!PostUpdateRes.isUsable()) 17725 continue; 17726 if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) || 17727 Stack->getCurrentDirective() == OMPD_taskgroup) { 17728 S.Diag(RefExpr->getExprLoc(), 17729 diag::err_omp_reduction_non_addressable_expression) 17730 << RefExpr->getSourceRange(); 17731 continue; 17732 } 17733 RD.ExprPostUpdates.emplace_back( 17734 S.IgnoredValueConversions(PostUpdateRes.get()).get()); 17735 } 17736 } 17737 } 17738 // All reduction items are still marked as reduction (to do not increase 17739 // code base size). 17740 unsigned Modifier = RD.RedModifier; 17741 // Consider task_reductions as reductions with task modifier. Required for 17742 // correct analysis of in_reduction clauses. 17743 if (CurrDir == OMPD_taskgroup && ClauseKind == OMPC_task_reduction) 17744 Modifier = OMPC_REDUCTION_task; 17745 Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref, Modifier, 17746 ASE || OASE); 17747 if (Modifier == OMPC_REDUCTION_task && 17748 (CurrDir == OMPD_taskgroup || 17749 ((isOpenMPParallelDirective(CurrDir) || 17750 isOpenMPWorksharingDirective(CurrDir)) && 17751 !isOpenMPSimdDirective(CurrDir)))) { 17752 if (DeclareReductionRef.isUsable()) 17753 Stack->addTaskgroupReductionData(D, ReductionIdRange, 17754 DeclareReductionRef.get()); 17755 else 17756 Stack->addTaskgroupReductionData(D, ReductionIdRange, BOK); 17757 } 17758 RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get(), 17759 TaskgroupDescriptor, CopyOpRes.get(), TempArrayRes.get(), 17760 TempArrayElem.get()); 17761 } 17762 return RD.Vars.empty(); 17763 } 17764 17765 OMPClause *Sema::ActOnOpenMPReductionClause( 17766 ArrayRef<Expr *> VarList, OpenMPReductionClauseModifier Modifier, 17767 SourceLocation StartLoc, SourceLocation LParenLoc, 17768 SourceLocation ModifierLoc, SourceLocation ColonLoc, SourceLocation EndLoc, 17769 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 17770 ArrayRef<Expr *> UnresolvedReductions) { 17771 if (ModifierLoc.isValid() && Modifier == OMPC_REDUCTION_unknown) { 17772 Diag(LParenLoc, diag::err_omp_unexpected_clause_value) 17773 << getListOfPossibleValues(OMPC_reduction, /*First=*/0, 17774 /*Last=*/OMPC_REDUCTION_unknown) 17775 << getOpenMPClauseName(OMPC_reduction); 17776 return nullptr; 17777 } 17778 // OpenMP 5.0, 2.19.5.4 reduction Clause, Restrictions 17779 // A reduction clause with the inscan reduction-modifier may only appear on a 17780 // worksharing-loop construct, a worksharing-loop SIMD construct, a simd 17781 // construct, a parallel worksharing-loop construct or a parallel 17782 // worksharing-loop SIMD construct. 17783 if (Modifier == OMPC_REDUCTION_inscan && 17784 (DSAStack->getCurrentDirective() != OMPD_for && 17785 DSAStack->getCurrentDirective() != OMPD_for_simd && 17786 DSAStack->getCurrentDirective() != OMPD_simd && 17787 DSAStack->getCurrentDirective() != OMPD_parallel_for && 17788 DSAStack->getCurrentDirective() != OMPD_parallel_for_simd)) { 17789 Diag(ModifierLoc, diag::err_omp_wrong_inscan_reduction); 17790 return nullptr; 17791 } 17792 17793 ReductionData RD(VarList.size(), Modifier); 17794 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList, 17795 StartLoc, LParenLoc, ColonLoc, EndLoc, 17796 ReductionIdScopeSpec, ReductionId, 17797 UnresolvedReductions, RD)) 17798 return nullptr; 17799 17800 return OMPReductionClause::Create( 17801 Context, StartLoc, LParenLoc, ModifierLoc, ColonLoc, EndLoc, Modifier, 17802 RD.Vars, ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 17803 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.InscanCopyOps, 17804 RD.InscanCopyArrayTemps, RD.InscanCopyArrayElems, 17805 buildPreInits(Context, RD.ExprCaptures), 17806 buildPostUpdate(*this, RD.ExprPostUpdates)); 17807 } 17808 17809 OMPClause *Sema::ActOnOpenMPTaskReductionClause( 17810 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 17811 SourceLocation ColonLoc, SourceLocation EndLoc, 17812 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 17813 ArrayRef<Expr *> UnresolvedReductions) { 17814 ReductionData RD(VarList.size()); 17815 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction, VarList, 17816 StartLoc, LParenLoc, ColonLoc, EndLoc, 17817 ReductionIdScopeSpec, ReductionId, 17818 UnresolvedReductions, RD)) 17819 return nullptr; 17820 17821 return OMPTaskReductionClause::Create( 17822 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 17823 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 17824 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, 17825 buildPreInits(Context, RD.ExprCaptures), 17826 buildPostUpdate(*this, RD.ExprPostUpdates)); 17827 } 17828 17829 OMPClause *Sema::ActOnOpenMPInReductionClause( 17830 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 17831 SourceLocation ColonLoc, SourceLocation EndLoc, 17832 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 17833 ArrayRef<Expr *> UnresolvedReductions) { 17834 ReductionData RD(VarList.size()); 17835 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_in_reduction, VarList, 17836 StartLoc, LParenLoc, ColonLoc, EndLoc, 17837 ReductionIdScopeSpec, ReductionId, 17838 UnresolvedReductions, RD)) 17839 return nullptr; 17840 17841 return OMPInReductionClause::Create( 17842 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 17843 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 17844 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.TaskgroupDescriptors, 17845 buildPreInits(Context, RD.ExprCaptures), 17846 buildPostUpdate(*this, RD.ExprPostUpdates)); 17847 } 17848 17849 bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind, 17850 SourceLocation LinLoc) { 17851 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) || 17852 LinKind == OMPC_LINEAR_unknown) { 17853 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus; 17854 return true; 17855 } 17856 return false; 17857 } 17858 17859 bool Sema::CheckOpenMPLinearDecl(const ValueDecl *D, SourceLocation ELoc, 17860 OpenMPLinearClauseKind LinKind, QualType Type, 17861 bool IsDeclareSimd) { 17862 const auto *VD = dyn_cast_or_null<VarDecl>(D); 17863 // A variable must not have an incomplete type or a reference type. 17864 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type)) 17865 return true; 17866 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) && 17867 !Type->isReferenceType()) { 17868 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference) 17869 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind); 17870 return true; 17871 } 17872 Type = Type.getNonReferenceType(); 17873 17874 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions] 17875 // A variable that is privatized must not have a const-qualified type 17876 // unless it is of class type with a mutable member. This restriction does 17877 // not apply to the firstprivate clause, nor to the linear clause on 17878 // declarative directives (like declare simd). 17879 if (!IsDeclareSimd && 17880 rejectConstNotMutableType(*this, D, Type, OMPC_linear, ELoc)) 17881 return true; 17882 17883 // A list item must be of integral or pointer type. 17884 Type = Type.getUnqualifiedType().getCanonicalType(); 17885 const auto *Ty = Type.getTypePtrOrNull(); 17886 if (!Ty || (LinKind != OMPC_LINEAR_ref && !Ty->isDependentType() && 17887 !Ty->isIntegralType(Context) && !Ty->isPointerType())) { 17888 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type; 17889 if (D) { 17890 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 17891 VarDecl::DeclarationOnly; 17892 Diag(D->getLocation(), 17893 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 17894 << D; 17895 } 17896 return true; 17897 } 17898 return false; 17899 } 17900 17901 OMPClause *Sema::ActOnOpenMPLinearClause( 17902 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc, 17903 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind, 17904 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 17905 SmallVector<Expr *, 8> Vars; 17906 SmallVector<Expr *, 8> Privates; 17907 SmallVector<Expr *, 8> Inits; 17908 SmallVector<Decl *, 4> ExprCaptures; 17909 SmallVector<Expr *, 4> ExprPostUpdates; 17910 if (CheckOpenMPLinearModifier(LinKind, LinLoc)) 17911 LinKind = OMPC_LINEAR_val; 17912 for (Expr *RefExpr : VarList) { 17913 assert(RefExpr && "NULL expr in OpenMP linear clause."); 17914 SourceLocation ELoc; 17915 SourceRange ERange; 17916 Expr *SimpleRefExpr = RefExpr; 17917 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 17918 if (Res.second) { 17919 // It will be analyzed later. 17920 Vars.push_back(RefExpr); 17921 Privates.push_back(nullptr); 17922 Inits.push_back(nullptr); 17923 } 17924 ValueDecl *D = Res.first; 17925 if (!D) 17926 continue; 17927 17928 QualType Type = D->getType(); 17929 auto *VD = dyn_cast<VarDecl>(D); 17930 17931 // OpenMP [2.14.3.7, linear clause] 17932 // A list-item cannot appear in more than one linear clause. 17933 // A list-item that appears in a linear clause cannot appear in any 17934 // other data-sharing attribute clause. 17935 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 17936 if (DVar.RefExpr) { 17937 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 17938 << getOpenMPClauseName(OMPC_linear); 17939 reportOriginalDsa(*this, DSAStack, D, DVar); 17940 continue; 17941 } 17942 17943 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type)) 17944 continue; 17945 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType(); 17946 17947 // Build private copy of original var. 17948 VarDecl *Private = 17949 buildVarDecl(*this, ELoc, Type, D->getName(), 17950 D->hasAttrs() ? &D->getAttrs() : nullptr, 17951 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 17952 DeclRefExpr *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc); 17953 // Build var to save initial value. 17954 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start"); 17955 Expr *InitExpr; 17956 DeclRefExpr *Ref = nullptr; 17957 if (!VD && !CurContext->isDependentContext()) { 17958 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 17959 if (!isOpenMPCapturedDecl(D)) { 17960 ExprCaptures.push_back(Ref->getDecl()); 17961 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) { 17962 ExprResult RefRes = DefaultLvalueConversion(Ref); 17963 if (!RefRes.isUsable()) 17964 continue; 17965 ExprResult PostUpdateRes = 17966 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, 17967 SimpleRefExpr, RefRes.get()); 17968 if (!PostUpdateRes.isUsable()) 17969 continue; 17970 ExprPostUpdates.push_back( 17971 IgnoredValueConversions(PostUpdateRes.get()).get()); 17972 } 17973 } 17974 } 17975 if (LinKind == OMPC_LINEAR_uval) 17976 InitExpr = VD ? VD->getInit() : SimpleRefExpr; 17977 else 17978 InitExpr = VD ? SimpleRefExpr : Ref; 17979 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(), 17980 /*DirectInit=*/false); 17981 DeclRefExpr *InitRef = buildDeclRefExpr(*this, Init, Type, ELoc); 17982 17983 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref); 17984 Vars.push_back((VD || CurContext->isDependentContext()) 17985 ? RefExpr->IgnoreParens() 17986 : Ref); 17987 Privates.push_back(PrivateRef); 17988 Inits.push_back(InitRef); 17989 } 17990 17991 if (Vars.empty()) 17992 return nullptr; 17993 17994 Expr *StepExpr = Step; 17995 Expr *CalcStepExpr = nullptr; 17996 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && 17997 !Step->isInstantiationDependent() && 17998 !Step->containsUnexpandedParameterPack()) { 17999 SourceLocation StepLoc = Step->getBeginLoc(); 18000 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step); 18001 if (Val.isInvalid()) 18002 return nullptr; 18003 StepExpr = Val.get(); 18004 18005 // Build var to save the step value. 18006 VarDecl *SaveVar = 18007 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step"); 18008 ExprResult SaveRef = 18009 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc); 18010 ExprResult CalcStep = 18011 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr); 18012 CalcStep = ActOnFinishFullExpr(CalcStep.get(), /*DiscardedValue*/ false); 18013 18014 // Warn about zero linear step (it would be probably better specified as 18015 // making corresponding variables 'const'). 18016 if (Optional<llvm::APSInt> Result = 18017 StepExpr->getIntegerConstantExpr(Context)) { 18018 if (!Result->isNegative() && !Result->isStrictlyPositive()) 18019 Diag(StepLoc, diag::warn_omp_linear_step_zero) 18020 << Vars[0] << (Vars.size() > 1); 18021 } else if (CalcStep.isUsable()) { 18022 // Calculate the step beforehand instead of doing this on each iteration. 18023 // (This is not used if the number of iterations may be kfold-ed). 18024 CalcStepExpr = CalcStep.get(); 18025 } 18026 } 18027 18028 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc, 18029 ColonLoc, EndLoc, Vars, Privates, Inits, 18030 StepExpr, CalcStepExpr, 18031 buildPreInits(Context, ExprCaptures), 18032 buildPostUpdate(*this, ExprPostUpdates)); 18033 } 18034 18035 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, 18036 Expr *NumIterations, Sema &SemaRef, 18037 Scope *S, DSAStackTy *Stack) { 18038 // Walk the vars and build update/final expressions for the CodeGen. 18039 SmallVector<Expr *, 8> Updates; 18040 SmallVector<Expr *, 8> Finals; 18041 SmallVector<Expr *, 8> UsedExprs; 18042 Expr *Step = Clause.getStep(); 18043 Expr *CalcStep = Clause.getCalcStep(); 18044 // OpenMP [2.14.3.7, linear clause] 18045 // If linear-step is not specified it is assumed to be 1. 18046 if (!Step) 18047 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(); 18048 else if (CalcStep) 18049 Step = cast<BinaryOperator>(CalcStep)->getLHS(); 18050 bool HasErrors = false; 18051 auto CurInit = Clause.inits().begin(); 18052 auto CurPrivate = Clause.privates().begin(); 18053 OpenMPLinearClauseKind LinKind = Clause.getModifier(); 18054 for (Expr *RefExpr : Clause.varlists()) { 18055 SourceLocation ELoc; 18056 SourceRange ERange; 18057 Expr *SimpleRefExpr = RefExpr; 18058 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange); 18059 ValueDecl *D = Res.first; 18060 if (Res.second || !D) { 18061 Updates.push_back(nullptr); 18062 Finals.push_back(nullptr); 18063 HasErrors = true; 18064 continue; 18065 } 18066 auto &&Info = Stack->isLoopControlVariable(D); 18067 // OpenMP [2.15.11, distribute simd Construct] 18068 // A list item may not appear in a linear clause, unless it is the loop 18069 // iteration variable. 18070 if (isOpenMPDistributeDirective(Stack->getCurrentDirective()) && 18071 isOpenMPSimdDirective(Stack->getCurrentDirective()) && !Info.first) { 18072 SemaRef.Diag(ELoc, 18073 diag::err_omp_linear_distribute_var_non_loop_iteration); 18074 Updates.push_back(nullptr); 18075 Finals.push_back(nullptr); 18076 HasErrors = true; 18077 continue; 18078 } 18079 Expr *InitExpr = *CurInit; 18080 18081 // Build privatized reference to the current linear var. 18082 auto *DE = cast<DeclRefExpr>(SimpleRefExpr); 18083 Expr *CapturedRef; 18084 if (LinKind == OMPC_LINEAR_uval) 18085 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit(); 18086 else 18087 CapturedRef = 18088 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()), 18089 DE->getType().getUnqualifiedType(), DE->getExprLoc(), 18090 /*RefersToCapture=*/true); 18091 18092 // Build update: Var = InitExpr + IV * Step 18093 ExprResult Update; 18094 if (!Info.first) 18095 Update = buildCounterUpdate( 18096 SemaRef, S, RefExpr->getExprLoc(), *CurPrivate, InitExpr, IV, Step, 18097 /*Subtract=*/false, /*IsNonRectangularLB=*/false); 18098 else 18099 Update = *CurPrivate; 18100 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getBeginLoc(), 18101 /*DiscardedValue*/ false); 18102 18103 // Build final: Var = PrivCopy; 18104 ExprResult Final; 18105 if (!Info.first) 18106 Final = SemaRef.BuildBinOp( 18107 S, RefExpr->getExprLoc(), BO_Assign, CapturedRef, 18108 SemaRef.DefaultLvalueConversion(*CurPrivate).get()); 18109 else 18110 Final = *CurPrivate; 18111 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getBeginLoc(), 18112 /*DiscardedValue*/ false); 18113 18114 if (!Update.isUsable() || !Final.isUsable()) { 18115 Updates.push_back(nullptr); 18116 Finals.push_back(nullptr); 18117 UsedExprs.push_back(nullptr); 18118 HasErrors = true; 18119 } else { 18120 Updates.push_back(Update.get()); 18121 Finals.push_back(Final.get()); 18122 if (!Info.first) 18123 UsedExprs.push_back(SimpleRefExpr); 18124 } 18125 ++CurInit; 18126 ++CurPrivate; 18127 } 18128 if (Expr *S = Clause.getStep()) 18129 UsedExprs.push_back(S); 18130 // Fill the remaining part with the nullptr. 18131 UsedExprs.append(Clause.varlist_size() + 1 - UsedExprs.size(), nullptr); 18132 Clause.setUpdates(Updates); 18133 Clause.setFinals(Finals); 18134 Clause.setUsedExprs(UsedExprs); 18135 return HasErrors; 18136 } 18137 18138 OMPClause *Sema::ActOnOpenMPAlignedClause( 18139 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc, 18140 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 18141 SmallVector<Expr *, 8> Vars; 18142 for (Expr *RefExpr : VarList) { 18143 assert(RefExpr && "NULL expr in OpenMP linear clause."); 18144 SourceLocation ELoc; 18145 SourceRange ERange; 18146 Expr *SimpleRefExpr = RefExpr; 18147 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 18148 if (Res.second) { 18149 // It will be analyzed later. 18150 Vars.push_back(RefExpr); 18151 } 18152 ValueDecl *D = Res.first; 18153 if (!D) 18154 continue; 18155 18156 QualType QType = D->getType(); 18157 auto *VD = dyn_cast<VarDecl>(D); 18158 18159 // OpenMP [2.8.1, simd construct, Restrictions] 18160 // The type of list items appearing in the aligned clause must be 18161 // array, pointer, reference to array, or reference to pointer. 18162 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType(); 18163 const Type *Ty = QType.getTypePtrOrNull(); 18164 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) { 18165 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr) 18166 << QType << getLangOpts().CPlusPlus << ERange; 18167 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 18168 VarDecl::DeclarationOnly; 18169 Diag(D->getLocation(), 18170 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 18171 << D; 18172 continue; 18173 } 18174 18175 // OpenMP [2.8.1, simd construct, Restrictions] 18176 // A list-item cannot appear in more than one aligned clause. 18177 if (const Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) { 18178 Diag(ELoc, diag::err_omp_used_in_clause_twice) 18179 << 0 << getOpenMPClauseName(OMPC_aligned) << ERange; 18180 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa) 18181 << getOpenMPClauseName(OMPC_aligned); 18182 continue; 18183 } 18184 18185 DeclRefExpr *Ref = nullptr; 18186 if (!VD && isOpenMPCapturedDecl(D)) 18187 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 18188 Vars.push_back(DefaultFunctionArrayConversion( 18189 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref) 18190 .get()); 18191 } 18192 18193 // OpenMP [2.8.1, simd construct, Description] 18194 // The parameter of the aligned clause, alignment, must be a constant 18195 // positive integer expression. 18196 // If no optional parameter is specified, implementation-defined default 18197 // alignments for SIMD instructions on the target platforms are assumed. 18198 if (Alignment != nullptr) { 18199 ExprResult AlignResult = 18200 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned); 18201 if (AlignResult.isInvalid()) 18202 return nullptr; 18203 Alignment = AlignResult.get(); 18204 } 18205 if (Vars.empty()) 18206 return nullptr; 18207 18208 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc, 18209 EndLoc, Vars, Alignment); 18210 } 18211 18212 OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList, 18213 SourceLocation StartLoc, 18214 SourceLocation LParenLoc, 18215 SourceLocation EndLoc) { 18216 SmallVector<Expr *, 8> Vars; 18217 SmallVector<Expr *, 8> SrcExprs; 18218 SmallVector<Expr *, 8> DstExprs; 18219 SmallVector<Expr *, 8> AssignmentOps; 18220 for (Expr *RefExpr : VarList) { 18221 assert(RefExpr && "NULL expr in OpenMP copyin clause."); 18222 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 18223 // It will be analyzed later. 18224 Vars.push_back(RefExpr); 18225 SrcExprs.push_back(nullptr); 18226 DstExprs.push_back(nullptr); 18227 AssignmentOps.push_back(nullptr); 18228 continue; 18229 } 18230 18231 SourceLocation ELoc = RefExpr->getExprLoc(); 18232 // OpenMP [2.1, C/C++] 18233 // A list item is a variable name. 18234 // OpenMP [2.14.4.1, Restrictions, p.1] 18235 // A list item that appears in a copyin clause must be threadprivate. 18236 auto *DE = dyn_cast<DeclRefExpr>(RefExpr); 18237 if (!DE || !isa<VarDecl>(DE->getDecl())) { 18238 Diag(ELoc, diag::err_omp_expected_var_name_member_expr) 18239 << 0 << RefExpr->getSourceRange(); 18240 continue; 18241 } 18242 18243 Decl *D = DE->getDecl(); 18244 auto *VD = cast<VarDecl>(D); 18245 18246 QualType Type = VD->getType(); 18247 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 18248 // It will be analyzed later. 18249 Vars.push_back(DE); 18250 SrcExprs.push_back(nullptr); 18251 DstExprs.push_back(nullptr); 18252 AssignmentOps.push_back(nullptr); 18253 continue; 18254 } 18255 18256 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1] 18257 // A list item that appears in a copyin clause must be threadprivate. 18258 if (!DSAStack->isThreadPrivate(VD)) { 18259 Diag(ELoc, diag::err_omp_required_access) 18260 << getOpenMPClauseName(OMPC_copyin) 18261 << getOpenMPDirectiveName(OMPD_threadprivate); 18262 continue; 18263 } 18264 18265 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 18266 // A variable of class type (or array thereof) that appears in a 18267 // copyin clause requires an accessible, unambiguous copy assignment 18268 // operator for the class type. 18269 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType(); 18270 VarDecl *SrcVD = 18271 buildVarDecl(*this, DE->getBeginLoc(), ElemType.getUnqualifiedType(), 18272 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr); 18273 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr( 18274 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc()); 18275 VarDecl *DstVD = 18276 buildVarDecl(*this, DE->getBeginLoc(), ElemType, ".copyin.dst", 18277 VD->hasAttrs() ? &VD->getAttrs() : nullptr); 18278 DeclRefExpr *PseudoDstExpr = 18279 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc()); 18280 // For arrays generate assignment operation for single element and replace 18281 // it by the original array element in CodeGen. 18282 ExprResult AssignmentOp = 18283 BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, PseudoDstExpr, 18284 PseudoSrcExpr); 18285 if (AssignmentOp.isInvalid()) 18286 continue; 18287 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(), 18288 /*DiscardedValue*/ false); 18289 if (AssignmentOp.isInvalid()) 18290 continue; 18291 18292 DSAStack->addDSA(VD, DE, OMPC_copyin); 18293 Vars.push_back(DE); 18294 SrcExprs.push_back(PseudoSrcExpr); 18295 DstExprs.push_back(PseudoDstExpr); 18296 AssignmentOps.push_back(AssignmentOp.get()); 18297 } 18298 18299 if (Vars.empty()) 18300 return nullptr; 18301 18302 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 18303 SrcExprs, DstExprs, AssignmentOps); 18304 } 18305 18306 OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList, 18307 SourceLocation StartLoc, 18308 SourceLocation LParenLoc, 18309 SourceLocation EndLoc) { 18310 SmallVector<Expr *, 8> Vars; 18311 SmallVector<Expr *, 8> SrcExprs; 18312 SmallVector<Expr *, 8> DstExprs; 18313 SmallVector<Expr *, 8> AssignmentOps; 18314 for (Expr *RefExpr : VarList) { 18315 assert(RefExpr && "NULL expr in OpenMP linear clause."); 18316 SourceLocation ELoc; 18317 SourceRange ERange; 18318 Expr *SimpleRefExpr = RefExpr; 18319 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 18320 if (Res.second) { 18321 // It will be analyzed later. 18322 Vars.push_back(RefExpr); 18323 SrcExprs.push_back(nullptr); 18324 DstExprs.push_back(nullptr); 18325 AssignmentOps.push_back(nullptr); 18326 } 18327 ValueDecl *D = Res.first; 18328 if (!D) 18329 continue; 18330 18331 QualType Type = D->getType(); 18332 auto *VD = dyn_cast<VarDecl>(D); 18333 18334 // OpenMP [2.14.4.2, Restrictions, p.2] 18335 // A list item that appears in a copyprivate clause may not appear in a 18336 // private or firstprivate clause on the single construct. 18337 if (!VD || !DSAStack->isThreadPrivate(VD)) { 18338 DSAStackTy::DSAVarData DVar = 18339 DSAStack->getTopDSA(D, /*FromParent=*/false); 18340 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate && 18341 DVar.RefExpr) { 18342 Diag(ELoc, diag::err_omp_wrong_dsa) 18343 << getOpenMPClauseName(DVar.CKind) 18344 << getOpenMPClauseName(OMPC_copyprivate); 18345 reportOriginalDsa(*this, DSAStack, D, DVar); 18346 continue; 18347 } 18348 18349 // OpenMP [2.11.4.2, Restrictions, p.1] 18350 // All list items that appear in a copyprivate clause must be either 18351 // threadprivate or private in the enclosing context. 18352 if (DVar.CKind == OMPC_unknown) { 18353 DVar = DSAStack->getImplicitDSA(D, false); 18354 if (DVar.CKind == OMPC_shared) { 18355 Diag(ELoc, diag::err_omp_required_access) 18356 << getOpenMPClauseName(OMPC_copyprivate) 18357 << "threadprivate or private in the enclosing context"; 18358 reportOriginalDsa(*this, DSAStack, D, DVar); 18359 continue; 18360 } 18361 } 18362 } 18363 18364 // Variably modified types are not supported. 18365 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) { 18366 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 18367 << getOpenMPClauseName(OMPC_copyprivate) << Type 18368 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 18369 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 18370 VarDecl::DeclarationOnly; 18371 Diag(D->getLocation(), 18372 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 18373 << D; 18374 continue; 18375 } 18376 18377 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 18378 // A variable of class type (or array thereof) that appears in a 18379 // copyin clause requires an accessible, unambiguous copy assignment 18380 // operator for the class type. 18381 Type = Context.getBaseElementType(Type.getNonReferenceType()) 18382 .getUnqualifiedType(); 18383 VarDecl *SrcVD = 18384 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.src", 18385 D->hasAttrs() ? &D->getAttrs() : nullptr); 18386 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc); 18387 VarDecl *DstVD = 18388 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.dst", 18389 D->hasAttrs() ? &D->getAttrs() : nullptr); 18390 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc); 18391 ExprResult AssignmentOp = BuildBinOp( 18392 DSAStack->getCurScope(), ELoc, BO_Assign, PseudoDstExpr, PseudoSrcExpr); 18393 if (AssignmentOp.isInvalid()) 18394 continue; 18395 AssignmentOp = 18396 ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false); 18397 if (AssignmentOp.isInvalid()) 18398 continue; 18399 18400 // No need to mark vars as copyprivate, they are already threadprivate or 18401 // implicitly private. 18402 assert(VD || isOpenMPCapturedDecl(D)); 18403 Vars.push_back( 18404 VD ? RefExpr->IgnoreParens() 18405 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false)); 18406 SrcExprs.push_back(PseudoSrcExpr); 18407 DstExprs.push_back(PseudoDstExpr); 18408 AssignmentOps.push_back(AssignmentOp.get()); 18409 } 18410 18411 if (Vars.empty()) 18412 return nullptr; 18413 18414 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 18415 Vars, SrcExprs, DstExprs, AssignmentOps); 18416 } 18417 18418 OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList, 18419 SourceLocation StartLoc, 18420 SourceLocation LParenLoc, 18421 SourceLocation EndLoc) { 18422 if (VarList.empty()) 18423 return nullptr; 18424 18425 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList); 18426 } 18427 18428 /// Tries to find omp_depend_t. type. 18429 static bool findOMPDependT(Sema &S, SourceLocation Loc, DSAStackTy *Stack, 18430 bool Diagnose = true) { 18431 QualType OMPDependT = Stack->getOMPDependT(); 18432 if (!OMPDependT.isNull()) 18433 return true; 18434 IdentifierInfo *II = &S.PP.getIdentifierTable().get("omp_depend_t"); 18435 ParsedType PT = S.getTypeName(*II, Loc, S.getCurScope()); 18436 if (!PT.getAsOpaquePtr() || PT.get().isNull()) { 18437 if (Diagnose) 18438 S.Diag(Loc, diag::err_omp_implied_type_not_found) << "omp_depend_t"; 18439 return false; 18440 } 18441 Stack->setOMPDependT(PT.get()); 18442 return true; 18443 } 18444 18445 OMPClause *Sema::ActOnOpenMPDepobjClause(Expr *Depobj, SourceLocation StartLoc, 18446 SourceLocation LParenLoc, 18447 SourceLocation EndLoc) { 18448 if (!Depobj) 18449 return nullptr; 18450 18451 bool OMPDependTFound = findOMPDependT(*this, StartLoc, DSAStack); 18452 18453 // OpenMP 5.0, 2.17.10.1 depobj Construct 18454 // depobj is an lvalue expression of type omp_depend_t. 18455 if (!Depobj->isTypeDependent() && !Depobj->isValueDependent() && 18456 !Depobj->isInstantiationDependent() && 18457 !Depobj->containsUnexpandedParameterPack() && 18458 (OMPDependTFound && 18459 !Context.typesAreCompatible(DSAStack->getOMPDependT(), Depobj->getType(), 18460 /*CompareUnqualified=*/true))) { 18461 Diag(Depobj->getExprLoc(), diag::err_omp_expected_omp_depend_t_lvalue) 18462 << 0 << Depobj->getType() << Depobj->getSourceRange(); 18463 } 18464 18465 if (!Depobj->isLValue()) { 18466 Diag(Depobj->getExprLoc(), diag::err_omp_expected_omp_depend_t_lvalue) 18467 << 1 << Depobj->getSourceRange(); 18468 } 18469 18470 return OMPDepobjClause::Create(Context, StartLoc, LParenLoc, EndLoc, Depobj); 18471 } 18472 18473 OMPClause * 18474 Sema::ActOnOpenMPDependClause(Expr *DepModifier, OpenMPDependClauseKind DepKind, 18475 SourceLocation DepLoc, SourceLocation ColonLoc, 18476 ArrayRef<Expr *> VarList, SourceLocation StartLoc, 18477 SourceLocation LParenLoc, SourceLocation EndLoc) { 18478 if (DSAStack->getCurrentDirective() == OMPD_ordered && 18479 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) { 18480 Diag(DepLoc, diag::err_omp_unexpected_clause_value) 18481 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend); 18482 return nullptr; 18483 } 18484 if (DSAStack->getCurrentDirective() == OMPD_taskwait && 18485 DepKind == OMPC_DEPEND_mutexinoutset) { 18486 Diag(DepLoc, diag::err_omp_taskwait_depend_mutexinoutset_not_allowed); 18487 return nullptr; 18488 } 18489 if ((DSAStack->getCurrentDirective() != OMPD_ordered || 18490 DSAStack->getCurrentDirective() == OMPD_depobj) && 18491 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source || 18492 DepKind == OMPC_DEPEND_sink || 18493 ((LangOpts.OpenMP < 50 || 18494 DSAStack->getCurrentDirective() == OMPD_depobj) && 18495 DepKind == OMPC_DEPEND_depobj))) { 18496 SmallVector<unsigned, 3> Except; 18497 Except.push_back(OMPC_DEPEND_source); 18498 Except.push_back(OMPC_DEPEND_sink); 18499 if (LangOpts.OpenMP < 50 || DSAStack->getCurrentDirective() == OMPD_depobj) 18500 Except.push_back(OMPC_DEPEND_depobj); 18501 std::string Expected = (LangOpts.OpenMP >= 50 && !DepModifier) 18502 ? "depend modifier(iterator) or " 18503 : ""; 18504 Diag(DepLoc, diag::err_omp_unexpected_clause_value) 18505 << Expected + getListOfPossibleValues(OMPC_depend, /*First=*/0, 18506 /*Last=*/OMPC_DEPEND_unknown, 18507 Except) 18508 << getOpenMPClauseName(OMPC_depend); 18509 return nullptr; 18510 } 18511 if (DepModifier && 18512 (DepKind == OMPC_DEPEND_source || DepKind == OMPC_DEPEND_sink)) { 18513 Diag(DepModifier->getExprLoc(), 18514 diag::err_omp_depend_sink_source_with_modifier); 18515 return nullptr; 18516 } 18517 if (DepModifier && 18518 !DepModifier->getType()->isSpecificBuiltinType(BuiltinType::OMPIterator)) 18519 Diag(DepModifier->getExprLoc(), diag::err_omp_depend_modifier_not_iterator); 18520 18521 SmallVector<Expr *, 8> Vars; 18522 DSAStackTy::OperatorOffsetTy OpsOffs; 18523 llvm::APSInt DepCounter(/*BitWidth=*/32); 18524 llvm::APSInt TotalDepCount(/*BitWidth=*/32); 18525 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) { 18526 if (const Expr *OrderedCountExpr = 18527 DSAStack->getParentOrderedRegionParam().first) { 18528 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context); 18529 TotalDepCount.setIsUnsigned(/*Val=*/true); 18530 } 18531 } 18532 for (Expr *RefExpr : VarList) { 18533 assert(RefExpr && "NULL expr in OpenMP shared clause."); 18534 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 18535 // It will be analyzed later. 18536 Vars.push_back(RefExpr); 18537 continue; 18538 } 18539 18540 SourceLocation ELoc = RefExpr->getExprLoc(); 18541 Expr *SimpleExpr = RefExpr->IgnoreParenCasts(); 18542 if (DepKind == OMPC_DEPEND_sink) { 18543 if (DSAStack->getParentOrderedRegionParam().first && 18544 DepCounter >= TotalDepCount) { 18545 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr); 18546 continue; 18547 } 18548 ++DepCounter; 18549 // OpenMP [2.13.9, Summary] 18550 // depend(dependence-type : vec), where dependence-type is: 18551 // 'sink' and where vec is the iteration vector, which has the form: 18552 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn] 18553 // where n is the value specified by the ordered clause in the loop 18554 // directive, xi denotes the loop iteration variable of the i-th nested 18555 // loop associated with the loop directive, and di is a constant 18556 // non-negative integer. 18557 if (CurContext->isDependentContext()) { 18558 // It will be analyzed later. 18559 Vars.push_back(RefExpr); 18560 continue; 18561 } 18562 SimpleExpr = SimpleExpr->IgnoreImplicit(); 18563 OverloadedOperatorKind OOK = OO_None; 18564 SourceLocation OOLoc; 18565 Expr *LHS = SimpleExpr; 18566 Expr *RHS = nullptr; 18567 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) { 18568 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode()); 18569 OOLoc = BO->getOperatorLoc(); 18570 LHS = BO->getLHS()->IgnoreParenImpCasts(); 18571 RHS = BO->getRHS()->IgnoreParenImpCasts(); 18572 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) { 18573 OOK = OCE->getOperator(); 18574 OOLoc = OCE->getOperatorLoc(); 18575 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); 18576 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts(); 18577 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) { 18578 OOK = MCE->getMethodDecl() 18579 ->getNameInfo() 18580 .getName() 18581 .getCXXOverloadedOperator(); 18582 OOLoc = MCE->getCallee()->getExprLoc(); 18583 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts(); 18584 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); 18585 } 18586 SourceLocation ELoc; 18587 SourceRange ERange; 18588 auto Res = getPrivateItem(*this, LHS, ELoc, ERange); 18589 if (Res.second) { 18590 // It will be analyzed later. 18591 Vars.push_back(RefExpr); 18592 } 18593 ValueDecl *D = Res.first; 18594 if (!D) 18595 continue; 18596 18597 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) { 18598 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus); 18599 continue; 18600 } 18601 if (RHS) { 18602 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause( 18603 RHS, OMPC_depend, /*StrictlyPositive=*/false); 18604 if (RHSRes.isInvalid()) 18605 continue; 18606 } 18607 if (!CurContext->isDependentContext() && 18608 DSAStack->getParentOrderedRegionParam().first && 18609 DepCounter != DSAStack->isParentLoopControlVariable(D).first) { 18610 const ValueDecl *VD = 18611 DSAStack->getParentLoopControlVariable(DepCounter.getZExtValue()); 18612 if (VD) 18613 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) 18614 << 1 << VD; 18615 else 18616 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) << 0; 18617 continue; 18618 } 18619 OpsOffs.emplace_back(RHS, OOK); 18620 } else { 18621 bool OMPDependTFound = LangOpts.OpenMP >= 50; 18622 if (OMPDependTFound) 18623 OMPDependTFound = findOMPDependT(*this, StartLoc, DSAStack, 18624 DepKind == OMPC_DEPEND_depobj); 18625 if (DepKind == OMPC_DEPEND_depobj) { 18626 // OpenMP 5.0, 2.17.11 depend Clause, Restrictions, C/C++ 18627 // List items used in depend clauses with the depobj dependence type 18628 // must be expressions of the omp_depend_t type. 18629 if (!RefExpr->isValueDependent() && !RefExpr->isTypeDependent() && 18630 !RefExpr->isInstantiationDependent() && 18631 !RefExpr->containsUnexpandedParameterPack() && 18632 (OMPDependTFound && 18633 !Context.hasSameUnqualifiedType(DSAStack->getOMPDependT(), 18634 RefExpr->getType()))) { 18635 Diag(ELoc, diag::err_omp_expected_omp_depend_t_lvalue) 18636 << 0 << RefExpr->getType() << RefExpr->getSourceRange(); 18637 continue; 18638 } 18639 if (!RefExpr->isLValue()) { 18640 Diag(ELoc, diag::err_omp_expected_omp_depend_t_lvalue) 18641 << 1 << RefExpr->getType() << RefExpr->getSourceRange(); 18642 continue; 18643 } 18644 } else { 18645 // OpenMP 5.0 [2.17.11, Restrictions] 18646 // List items used in depend clauses cannot be zero-length array 18647 // sections. 18648 QualType ExprTy = RefExpr->getType().getNonReferenceType(); 18649 const auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr); 18650 if (OASE) { 18651 QualType BaseType = 18652 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 18653 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 18654 ExprTy = ATy->getElementType(); 18655 else 18656 ExprTy = BaseType->getPointeeType(); 18657 ExprTy = ExprTy.getNonReferenceType(); 18658 const Expr *Length = OASE->getLength(); 18659 Expr::EvalResult Result; 18660 if (Length && !Length->isValueDependent() && 18661 Length->EvaluateAsInt(Result, Context) && 18662 Result.Val.getInt().isZero()) { 18663 Diag(ELoc, 18664 diag::err_omp_depend_zero_length_array_section_not_allowed) 18665 << SimpleExpr->getSourceRange(); 18666 continue; 18667 } 18668 } 18669 18670 // OpenMP 5.0, 2.17.11 depend Clause, Restrictions, C/C++ 18671 // List items used in depend clauses with the in, out, inout or 18672 // mutexinoutset dependence types cannot be expressions of the 18673 // omp_depend_t type. 18674 if (!RefExpr->isValueDependent() && !RefExpr->isTypeDependent() && 18675 !RefExpr->isInstantiationDependent() && 18676 !RefExpr->containsUnexpandedParameterPack() && 18677 (!RefExpr->IgnoreParenImpCasts()->isLValue() || 18678 (OMPDependTFound && 18679 DSAStack->getOMPDependT().getTypePtr() == ExprTy.getTypePtr()))) { 18680 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 18681 << (LangOpts.OpenMP >= 50 ? 1 : 0) 18682 << (LangOpts.OpenMP >= 50 ? 1 : 0) << RefExpr->getSourceRange(); 18683 continue; 18684 } 18685 18686 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr); 18687 if (ASE && !ASE->getBase()->isTypeDependent() && 18688 !ASE->getBase()->getType().getNonReferenceType()->isPointerType() && 18689 !ASE->getBase()->getType().getNonReferenceType()->isArrayType()) { 18690 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 18691 << (LangOpts.OpenMP >= 50 ? 1 : 0) 18692 << (LangOpts.OpenMP >= 50 ? 1 : 0) << RefExpr->getSourceRange(); 18693 continue; 18694 } 18695 18696 ExprResult Res; 18697 { 18698 Sema::TentativeAnalysisScope Trap(*this); 18699 Res = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, 18700 RefExpr->IgnoreParenImpCasts()); 18701 } 18702 if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr) && 18703 !isa<OMPArrayShapingExpr>(SimpleExpr)) { 18704 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 18705 << (LangOpts.OpenMP >= 50 ? 1 : 0) 18706 << (LangOpts.OpenMP >= 50 ? 1 : 0) << RefExpr->getSourceRange(); 18707 continue; 18708 } 18709 } 18710 } 18711 Vars.push_back(RefExpr->IgnoreParenImpCasts()); 18712 } 18713 18714 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink && 18715 TotalDepCount > VarList.size() && 18716 DSAStack->getParentOrderedRegionParam().first && 18717 DSAStack->getParentLoopControlVariable(VarList.size() + 1)) { 18718 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration) 18719 << 1 << DSAStack->getParentLoopControlVariable(VarList.size() + 1); 18720 } 18721 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink && 18722 Vars.empty()) 18723 return nullptr; 18724 18725 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, 18726 DepModifier, DepKind, DepLoc, ColonLoc, 18727 Vars, TotalDepCount.getZExtValue()); 18728 if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) && 18729 DSAStack->isParentOrderedRegion()) 18730 DSAStack->addDoacrossDependClause(C, OpsOffs); 18731 return C; 18732 } 18733 18734 OMPClause *Sema::ActOnOpenMPDeviceClause(OpenMPDeviceClauseModifier Modifier, 18735 Expr *Device, SourceLocation StartLoc, 18736 SourceLocation LParenLoc, 18737 SourceLocation ModifierLoc, 18738 SourceLocation EndLoc) { 18739 assert((ModifierLoc.isInvalid() || LangOpts.OpenMP >= 50) && 18740 "Unexpected device modifier in OpenMP < 50."); 18741 18742 bool ErrorFound = false; 18743 if (ModifierLoc.isValid() && Modifier == OMPC_DEVICE_unknown) { 18744 std::string Values = 18745 getListOfPossibleValues(OMPC_device, /*First=*/0, OMPC_DEVICE_unknown); 18746 Diag(ModifierLoc, diag::err_omp_unexpected_clause_value) 18747 << Values << getOpenMPClauseName(OMPC_device); 18748 ErrorFound = true; 18749 } 18750 18751 Expr *ValExpr = Device; 18752 Stmt *HelperValStmt = nullptr; 18753 18754 // OpenMP [2.9.1, Restrictions] 18755 // The device expression must evaluate to a non-negative integer value. 18756 ErrorFound = !isNonNegativeIntegerValue(ValExpr, *this, OMPC_device, 18757 /*StrictlyPositive=*/false) || 18758 ErrorFound; 18759 if (ErrorFound) 18760 return nullptr; 18761 18762 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 18763 OpenMPDirectiveKind CaptureRegion = 18764 getOpenMPCaptureRegionForClause(DKind, OMPC_device, LangOpts.OpenMP); 18765 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 18766 ValExpr = MakeFullExpr(ValExpr).get(); 18767 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 18768 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 18769 HelperValStmt = buildPreInits(Context, Captures); 18770 } 18771 18772 return new (Context) 18773 OMPDeviceClause(Modifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc, 18774 LParenLoc, ModifierLoc, EndLoc); 18775 } 18776 18777 static bool checkTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef, 18778 DSAStackTy *Stack, QualType QTy, 18779 bool FullCheck = true) { 18780 if (SemaRef.RequireCompleteType(SL, QTy, diag::err_incomplete_type)) 18781 return false; 18782 if (FullCheck && !SemaRef.CurContext->isDependentContext() && 18783 !QTy.isTriviallyCopyableType(SemaRef.Context)) 18784 SemaRef.Diag(SL, diag::warn_omp_non_trivial_type_mapped) << QTy << SR; 18785 return true; 18786 } 18787 18788 /// Return true if it can be proven that the provided array expression 18789 /// (array section or array subscript) does NOT specify the whole size of the 18790 /// array whose base type is \a BaseQTy. 18791 static bool checkArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef, 18792 const Expr *E, 18793 QualType BaseQTy) { 18794 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E); 18795 18796 // If this is an array subscript, it refers to the whole size if the size of 18797 // the dimension is constant and equals 1. Also, an array section assumes the 18798 // format of an array subscript if no colon is used. 18799 if (isa<ArraySubscriptExpr>(E) || 18800 (OASE && OASE->getColonLocFirst().isInvalid())) { 18801 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) 18802 return ATy->getSize().getSExtValue() != 1; 18803 // Size can't be evaluated statically. 18804 return false; 18805 } 18806 18807 assert(OASE && "Expecting array section if not an array subscript."); 18808 const Expr *LowerBound = OASE->getLowerBound(); 18809 const Expr *Length = OASE->getLength(); 18810 18811 // If there is a lower bound that does not evaluates to zero, we are not 18812 // covering the whole dimension. 18813 if (LowerBound) { 18814 Expr::EvalResult Result; 18815 if (!LowerBound->EvaluateAsInt(Result, SemaRef.getASTContext())) 18816 return false; // Can't get the integer value as a constant. 18817 18818 llvm::APSInt ConstLowerBound = Result.Val.getInt(); 18819 if (ConstLowerBound.getSExtValue()) 18820 return true; 18821 } 18822 18823 // If we don't have a length we covering the whole dimension. 18824 if (!Length) 18825 return false; 18826 18827 // If the base is a pointer, we don't have a way to get the size of the 18828 // pointee. 18829 if (BaseQTy->isPointerType()) 18830 return false; 18831 18832 // We can only check if the length is the same as the size of the dimension 18833 // if we have a constant array. 18834 const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()); 18835 if (!CATy) 18836 return false; 18837 18838 Expr::EvalResult Result; 18839 if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext())) 18840 return false; // Can't get the integer value as a constant. 18841 18842 llvm::APSInt ConstLength = Result.Val.getInt(); 18843 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue(); 18844 } 18845 18846 // Return true if it can be proven that the provided array expression (array 18847 // section or array subscript) does NOT specify a single element of the array 18848 // whose base type is \a BaseQTy. 18849 static bool checkArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef, 18850 const Expr *E, 18851 QualType BaseQTy) { 18852 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E); 18853 18854 // An array subscript always refer to a single element. Also, an array section 18855 // assumes the format of an array subscript if no colon is used. 18856 if (isa<ArraySubscriptExpr>(E) || 18857 (OASE && OASE->getColonLocFirst().isInvalid())) 18858 return false; 18859 18860 assert(OASE && "Expecting array section if not an array subscript."); 18861 const Expr *Length = OASE->getLength(); 18862 18863 // If we don't have a length we have to check if the array has unitary size 18864 // for this dimension. Also, we should always expect a length if the base type 18865 // is pointer. 18866 if (!Length) { 18867 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) 18868 return ATy->getSize().getSExtValue() != 1; 18869 // We cannot assume anything. 18870 return false; 18871 } 18872 18873 // Check if the length evaluates to 1. 18874 Expr::EvalResult Result; 18875 if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext())) 18876 return false; // Can't get the integer value as a constant. 18877 18878 llvm::APSInt ConstLength = Result.Val.getInt(); 18879 return ConstLength.getSExtValue() != 1; 18880 } 18881 18882 // The base of elements of list in a map clause have to be either: 18883 // - a reference to variable or field. 18884 // - a member expression. 18885 // - an array expression. 18886 // 18887 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the 18888 // reference to 'r'. 18889 // 18890 // If we have: 18891 // 18892 // struct SS { 18893 // Bla S; 18894 // foo() { 18895 // #pragma omp target map (S.Arr[:12]); 18896 // } 18897 // } 18898 // 18899 // We want to retrieve the member expression 'this->S'; 18900 18901 // OpenMP 5.0 [2.19.7.1, map Clause, Restrictions, p.2] 18902 // If a list item is an array section, it must specify contiguous storage. 18903 // 18904 // For this restriction it is sufficient that we make sure only references 18905 // to variables or fields and array expressions, and that no array sections 18906 // exist except in the rightmost expression (unless they cover the whole 18907 // dimension of the array). E.g. these would be invalid: 18908 // 18909 // r.ArrS[3:5].Arr[6:7] 18910 // 18911 // r.ArrS[3:5].x 18912 // 18913 // but these would be valid: 18914 // r.ArrS[3].Arr[6:7] 18915 // 18916 // r.ArrS[3].x 18917 namespace { 18918 class MapBaseChecker final : public StmtVisitor<MapBaseChecker, bool> { 18919 Sema &SemaRef; 18920 OpenMPClauseKind CKind = OMPC_unknown; 18921 OpenMPDirectiveKind DKind = OMPD_unknown; 18922 OMPClauseMappableExprCommon::MappableExprComponentList &Components; 18923 bool IsNonContiguous = false; 18924 bool NoDiagnose = false; 18925 const Expr *RelevantExpr = nullptr; 18926 bool AllowUnitySizeArraySection = true; 18927 bool AllowWholeSizeArraySection = true; 18928 bool AllowAnotherPtr = true; 18929 SourceLocation ELoc; 18930 SourceRange ERange; 18931 18932 void emitErrorMsg() { 18933 // If nothing else worked, this is not a valid map clause expression. 18934 if (SemaRef.getLangOpts().OpenMP < 50) { 18935 SemaRef.Diag(ELoc, 18936 diag::err_omp_expected_named_var_member_or_array_expression) 18937 << ERange; 18938 } else { 18939 SemaRef.Diag(ELoc, diag::err_omp_non_lvalue_in_map_or_motion_clauses) 18940 << getOpenMPClauseName(CKind) << ERange; 18941 } 18942 } 18943 18944 public: 18945 bool VisitDeclRefExpr(DeclRefExpr *DRE) { 18946 if (!isa<VarDecl>(DRE->getDecl())) { 18947 emitErrorMsg(); 18948 return false; 18949 } 18950 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 18951 RelevantExpr = DRE; 18952 // Record the component. 18953 Components.emplace_back(DRE, DRE->getDecl(), IsNonContiguous); 18954 return true; 18955 } 18956 18957 bool VisitMemberExpr(MemberExpr *ME) { 18958 Expr *E = ME; 18959 Expr *BaseE = ME->getBase()->IgnoreParenCasts(); 18960 18961 if (isa<CXXThisExpr>(BaseE)) { 18962 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 18963 // We found a base expression: this->Val. 18964 RelevantExpr = ME; 18965 } else { 18966 E = BaseE; 18967 } 18968 18969 if (!isa<FieldDecl>(ME->getMemberDecl())) { 18970 if (!NoDiagnose) { 18971 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field) 18972 << ME->getSourceRange(); 18973 return false; 18974 } 18975 if (RelevantExpr) 18976 return false; 18977 return Visit(E); 18978 } 18979 18980 auto *FD = cast<FieldDecl>(ME->getMemberDecl()); 18981 18982 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3] 18983 // A bit-field cannot appear in a map clause. 18984 // 18985 if (FD->isBitField()) { 18986 if (!NoDiagnose) { 18987 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause) 18988 << ME->getSourceRange() << getOpenMPClauseName(CKind); 18989 return false; 18990 } 18991 if (RelevantExpr) 18992 return false; 18993 return Visit(E); 18994 } 18995 18996 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 18997 // If the type of a list item is a reference to a type T then the type 18998 // will be considered to be T for all purposes of this clause. 18999 QualType CurType = BaseE->getType().getNonReferenceType(); 19000 19001 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2] 19002 // A list item cannot be a variable that is a member of a structure with 19003 // a union type. 19004 // 19005 if (CurType->isUnionType()) { 19006 if (!NoDiagnose) { 19007 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed) 19008 << ME->getSourceRange(); 19009 return false; 19010 } 19011 return RelevantExpr || Visit(E); 19012 } 19013 19014 // If we got a member expression, we should not expect any array section 19015 // before that: 19016 // 19017 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7] 19018 // If a list item is an element of a structure, only the rightmost symbol 19019 // of the variable reference can be an array section. 19020 // 19021 AllowUnitySizeArraySection = false; 19022 AllowWholeSizeArraySection = false; 19023 19024 // Record the component. 19025 Components.emplace_back(ME, FD, IsNonContiguous); 19026 return RelevantExpr || Visit(E); 19027 } 19028 19029 bool VisitArraySubscriptExpr(ArraySubscriptExpr *AE) { 19030 Expr *E = AE->getBase()->IgnoreParenImpCasts(); 19031 19032 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) { 19033 if (!NoDiagnose) { 19034 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name) 19035 << 0 << AE->getSourceRange(); 19036 return false; 19037 } 19038 return RelevantExpr || Visit(E); 19039 } 19040 19041 // If we got an array subscript that express the whole dimension we 19042 // can have any array expressions before. If it only expressing part of 19043 // the dimension, we can only have unitary-size array expressions. 19044 if (checkArrayExpressionDoesNotReferToWholeSize(SemaRef, AE, E->getType())) 19045 AllowWholeSizeArraySection = false; 19046 19047 if (const auto *TE = dyn_cast<CXXThisExpr>(E->IgnoreParenCasts())) { 19048 Expr::EvalResult Result; 19049 if (!AE->getIdx()->isValueDependent() && 19050 AE->getIdx()->EvaluateAsInt(Result, SemaRef.getASTContext()) && 19051 !Result.Val.getInt().isZero()) { 19052 SemaRef.Diag(AE->getIdx()->getExprLoc(), 19053 diag::err_omp_invalid_map_this_expr); 19054 SemaRef.Diag(AE->getIdx()->getExprLoc(), 19055 diag::note_omp_invalid_subscript_on_this_ptr_map); 19056 } 19057 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 19058 RelevantExpr = TE; 19059 } 19060 19061 // Record the component - we don't have any declaration associated. 19062 Components.emplace_back(AE, nullptr, IsNonContiguous); 19063 19064 return RelevantExpr || Visit(E); 19065 } 19066 19067 bool VisitOMPArraySectionExpr(OMPArraySectionExpr *OASE) { 19068 // After OMP 5.0 Array section in reduction clause will be implicitly 19069 // mapped 19070 assert(!(SemaRef.getLangOpts().OpenMP < 50 && NoDiagnose) && 19071 "Array sections cannot be implicitly mapped."); 19072 Expr *E = OASE->getBase()->IgnoreParenImpCasts(); 19073 QualType CurType = 19074 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType(); 19075 19076 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 19077 // If the type of a list item is a reference to a type T then the type 19078 // will be considered to be T for all purposes of this clause. 19079 if (CurType->isReferenceType()) 19080 CurType = CurType->getPointeeType(); 19081 19082 bool IsPointer = CurType->isAnyPointerType(); 19083 19084 if (!IsPointer && !CurType->isArrayType()) { 19085 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name) 19086 << 0 << OASE->getSourceRange(); 19087 return false; 19088 } 19089 19090 bool NotWhole = 19091 checkArrayExpressionDoesNotReferToWholeSize(SemaRef, OASE, CurType); 19092 bool NotUnity = 19093 checkArrayExpressionDoesNotReferToUnitySize(SemaRef, OASE, CurType); 19094 19095 if (AllowWholeSizeArraySection) { 19096 // Any array section is currently allowed. Allowing a whole size array 19097 // section implies allowing a unity array section as well. 19098 // 19099 // If this array section refers to the whole dimension we can still 19100 // accept other array sections before this one, except if the base is a 19101 // pointer. Otherwise, only unitary sections are accepted. 19102 if (NotWhole || IsPointer) 19103 AllowWholeSizeArraySection = false; 19104 } else if (DKind == OMPD_target_update && 19105 SemaRef.getLangOpts().OpenMP >= 50) { 19106 if (IsPointer && !AllowAnotherPtr) 19107 SemaRef.Diag(ELoc, diag::err_omp_section_length_undefined) 19108 << /*array of unknown bound */ 1; 19109 else 19110 IsNonContiguous = true; 19111 } else if (AllowUnitySizeArraySection && NotUnity) { 19112 // A unity or whole array section is not allowed and that is not 19113 // compatible with the properties of the current array section. 19114 if (NoDiagnose) 19115 return false; 19116 SemaRef.Diag(ELoc, 19117 diag::err_array_section_does_not_specify_contiguous_storage) 19118 << OASE->getSourceRange(); 19119 return false; 19120 } 19121 19122 if (IsPointer) 19123 AllowAnotherPtr = false; 19124 19125 if (const auto *TE = dyn_cast<CXXThisExpr>(E)) { 19126 Expr::EvalResult ResultR; 19127 Expr::EvalResult ResultL; 19128 if (!OASE->getLength()->isValueDependent() && 19129 OASE->getLength()->EvaluateAsInt(ResultR, SemaRef.getASTContext()) && 19130 !ResultR.Val.getInt().isOne()) { 19131 SemaRef.Diag(OASE->getLength()->getExprLoc(), 19132 diag::err_omp_invalid_map_this_expr); 19133 SemaRef.Diag(OASE->getLength()->getExprLoc(), 19134 diag::note_omp_invalid_length_on_this_ptr_mapping); 19135 } 19136 if (OASE->getLowerBound() && !OASE->getLowerBound()->isValueDependent() && 19137 OASE->getLowerBound()->EvaluateAsInt(ResultL, 19138 SemaRef.getASTContext()) && 19139 !ResultL.Val.getInt().isZero()) { 19140 SemaRef.Diag(OASE->getLowerBound()->getExprLoc(), 19141 diag::err_omp_invalid_map_this_expr); 19142 SemaRef.Diag(OASE->getLowerBound()->getExprLoc(), 19143 diag::note_omp_invalid_lower_bound_on_this_ptr_mapping); 19144 } 19145 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 19146 RelevantExpr = TE; 19147 } 19148 19149 // Record the component - we don't have any declaration associated. 19150 Components.emplace_back(OASE, nullptr, /*IsNonContiguous=*/false); 19151 return RelevantExpr || Visit(E); 19152 } 19153 bool VisitOMPArrayShapingExpr(OMPArrayShapingExpr *E) { 19154 Expr *Base = E->getBase(); 19155 19156 // Record the component - we don't have any declaration associated. 19157 Components.emplace_back(E, nullptr, IsNonContiguous); 19158 19159 return Visit(Base->IgnoreParenImpCasts()); 19160 } 19161 19162 bool VisitUnaryOperator(UnaryOperator *UO) { 19163 if (SemaRef.getLangOpts().OpenMP < 50 || !UO->isLValue() || 19164 UO->getOpcode() != UO_Deref) { 19165 emitErrorMsg(); 19166 return false; 19167 } 19168 if (!RelevantExpr) { 19169 // Record the component if haven't found base decl. 19170 Components.emplace_back(UO, nullptr, /*IsNonContiguous=*/false); 19171 } 19172 return RelevantExpr || Visit(UO->getSubExpr()->IgnoreParenImpCasts()); 19173 } 19174 bool VisitBinaryOperator(BinaryOperator *BO) { 19175 if (SemaRef.getLangOpts().OpenMP < 50 || !BO->getType()->isPointerType()) { 19176 emitErrorMsg(); 19177 return false; 19178 } 19179 19180 // Pointer arithmetic is the only thing we expect to happen here so after we 19181 // make sure the binary operator is a pointer type, the we only thing need 19182 // to to is to visit the subtree that has the same type as root (so that we 19183 // know the other subtree is just an offset) 19184 Expr *LE = BO->getLHS()->IgnoreParenImpCasts(); 19185 Expr *RE = BO->getRHS()->IgnoreParenImpCasts(); 19186 Components.emplace_back(BO, nullptr, false); 19187 assert((LE->getType().getTypePtr() == BO->getType().getTypePtr() || 19188 RE->getType().getTypePtr() == BO->getType().getTypePtr()) && 19189 "Either LHS or RHS have base decl inside"); 19190 if (BO->getType().getTypePtr() == LE->getType().getTypePtr()) 19191 return RelevantExpr || Visit(LE); 19192 return RelevantExpr || Visit(RE); 19193 } 19194 bool VisitCXXThisExpr(CXXThisExpr *CTE) { 19195 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 19196 RelevantExpr = CTE; 19197 Components.emplace_back(CTE, nullptr, IsNonContiguous); 19198 return true; 19199 } 19200 bool VisitCXXOperatorCallExpr(CXXOperatorCallExpr *COCE) { 19201 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 19202 Components.emplace_back(COCE, nullptr, IsNonContiguous); 19203 return true; 19204 } 19205 bool VisitOpaqueValueExpr(OpaqueValueExpr *E) { 19206 Expr *Source = E->getSourceExpr(); 19207 if (!Source) { 19208 emitErrorMsg(); 19209 return false; 19210 } 19211 return Visit(Source); 19212 } 19213 bool VisitStmt(Stmt *) { 19214 emitErrorMsg(); 19215 return false; 19216 } 19217 const Expr *getFoundBase() const { return RelevantExpr; } 19218 explicit MapBaseChecker( 19219 Sema &SemaRef, OpenMPClauseKind CKind, OpenMPDirectiveKind DKind, 19220 OMPClauseMappableExprCommon::MappableExprComponentList &Components, 19221 bool NoDiagnose, SourceLocation &ELoc, SourceRange &ERange) 19222 : SemaRef(SemaRef), CKind(CKind), DKind(DKind), Components(Components), 19223 NoDiagnose(NoDiagnose), ELoc(ELoc), ERange(ERange) {} 19224 }; 19225 } // namespace 19226 19227 /// Return the expression of the base of the mappable expression or null if it 19228 /// cannot be determined and do all the necessary checks to see if the 19229 /// expression is valid as a standalone mappable expression. In the process, 19230 /// record all the components of the expression. 19231 static const Expr *checkMapClauseExpressionBase( 19232 Sema &SemaRef, Expr *E, 19233 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents, 19234 OpenMPClauseKind CKind, OpenMPDirectiveKind DKind, bool NoDiagnose) { 19235 SourceLocation ELoc = E->getExprLoc(); 19236 SourceRange ERange = E->getSourceRange(); 19237 MapBaseChecker Checker(SemaRef, CKind, DKind, CurComponents, NoDiagnose, ELoc, 19238 ERange); 19239 if (Checker.Visit(E->IgnoreParens())) { 19240 // Check if the highest dimension array section has length specified 19241 if (SemaRef.getLangOpts().OpenMP >= 50 && !CurComponents.empty() && 19242 (CKind == OMPC_to || CKind == OMPC_from)) { 19243 auto CI = CurComponents.rbegin(); 19244 auto CE = CurComponents.rend(); 19245 for (; CI != CE; ++CI) { 19246 const auto *OASE = 19247 dyn_cast<OMPArraySectionExpr>(CI->getAssociatedExpression()); 19248 if (!OASE) 19249 continue; 19250 if (OASE && OASE->getLength()) 19251 break; 19252 SemaRef.Diag(ELoc, diag::err_array_section_does_not_specify_length) 19253 << ERange; 19254 } 19255 } 19256 return Checker.getFoundBase(); 19257 } 19258 return nullptr; 19259 } 19260 19261 // Return true if expression E associated with value VD has conflicts with other 19262 // map information. 19263 static bool checkMapConflicts( 19264 Sema &SemaRef, DSAStackTy *DSAS, const ValueDecl *VD, const Expr *E, 19265 bool CurrentRegionOnly, 19266 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents, 19267 OpenMPClauseKind CKind) { 19268 assert(VD && E); 19269 SourceLocation ELoc = E->getExprLoc(); 19270 SourceRange ERange = E->getSourceRange(); 19271 19272 // In order to easily check the conflicts we need to match each component of 19273 // the expression under test with the components of the expressions that are 19274 // already in the stack. 19275 19276 assert(!CurComponents.empty() && "Map clause expression with no components!"); 19277 assert(CurComponents.back().getAssociatedDeclaration() == VD && 19278 "Map clause expression with unexpected base!"); 19279 19280 // Variables to help detecting enclosing problems in data environment nests. 19281 bool IsEnclosedByDataEnvironmentExpr = false; 19282 const Expr *EnclosingExpr = nullptr; 19283 19284 bool FoundError = DSAS->checkMappableExprComponentListsForDecl( 19285 VD, CurrentRegionOnly, 19286 [&IsEnclosedByDataEnvironmentExpr, &SemaRef, VD, CurrentRegionOnly, ELoc, 19287 ERange, CKind, &EnclosingExpr, 19288 CurComponents](OMPClauseMappableExprCommon::MappableExprComponentListRef 19289 StackComponents, 19290 OpenMPClauseKind Kind) { 19291 if (CKind == Kind && SemaRef.LangOpts.OpenMP >= 50) 19292 return false; 19293 assert(!StackComponents.empty() && 19294 "Map clause expression with no components!"); 19295 assert(StackComponents.back().getAssociatedDeclaration() == VD && 19296 "Map clause expression with unexpected base!"); 19297 (void)VD; 19298 19299 // The whole expression in the stack. 19300 const Expr *RE = StackComponents.front().getAssociatedExpression(); 19301 19302 // Expressions must start from the same base. Here we detect at which 19303 // point both expressions diverge from each other and see if we can 19304 // detect if the memory referred to both expressions is contiguous and 19305 // do not overlap. 19306 auto CI = CurComponents.rbegin(); 19307 auto CE = CurComponents.rend(); 19308 auto SI = StackComponents.rbegin(); 19309 auto SE = StackComponents.rend(); 19310 for (; CI != CE && SI != SE; ++CI, ++SI) { 19311 19312 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3] 19313 // At most one list item can be an array item derived from a given 19314 // variable in map clauses of the same construct. 19315 if (CurrentRegionOnly && 19316 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) || 19317 isa<OMPArraySectionExpr>(CI->getAssociatedExpression()) || 19318 isa<OMPArrayShapingExpr>(CI->getAssociatedExpression())) && 19319 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) || 19320 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()) || 19321 isa<OMPArrayShapingExpr>(SI->getAssociatedExpression()))) { 19322 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(), 19323 diag::err_omp_multiple_array_items_in_map_clause) 19324 << CI->getAssociatedExpression()->getSourceRange(); 19325 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(), 19326 diag::note_used_here) 19327 << SI->getAssociatedExpression()->getSourceRange(); 19328 return true; 19329 } 19330 19331 // Do both expressions have the same kind? 19332 if (CI->getAssociatedExpression()->getStmtClass() != 19333 SI->getAssociatedExpression()->getStmtClass()) 19334 break; 19335 19336 // Are we dealing with different variables/fields? 19337 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration()) 19338 break; 19339 } 19340 // Check if the extra components of the expressions in the enclosing 19341 // data environment are redundant for the current base declaration. 19342 // If they are, the maps completely overlap, which is legal. 19343 for (; SI != SE; ++SI) { 19344 QualType Type; 19345 if (const auto *ASE = 19346 dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) { 19347 Type = ASE->getBase()->IgnoreParenImpCasts()->getType(); 19348 } else if (const auto *OASE = dyn_cast<OMPArraySectionExpr>( 19349 SI->getAssociatedExpression())) { 19350 const Expr *E = OASE->getBase()->IgnoreParenImpCasts(); 19351 Type = 19352 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType(); 19353 } else if (const auto *OASE = dyn_cast<OMPArrayShapingExpr>( 19354 SI->getAssociatedExpression())) { 19355 Type = OASE->getBase()->getType()->getPointeeType(); 19356 } 19357 if (Type.isNull() || Type->isAnyPointerType() || 19358 checkArrayExpressionDoesNotReferToWholeSize( 19359 SemaRef, SI->getAssociatedExpression(), Type)) 19360 break; 19361 } 19362 19363 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4] 19364 // List items of map clauses in the same construct must not share 19365 // original storage. 19366 // 19367 // If the expressions are exactly the same or one is a subset of the 19368 // other, it means they are sharing storage. 19369 if (CI == CE && SI == SE) { 19370 if (CurrentRegionOnly) { 19371 if (CKind == OMPC_map) { 19372 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange; 19373 } else { 19374 assert(CKind == OMPC_to || CKind == OMPC_from); 19375 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update) 19376 << ERange; 19377 } 19378 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 19379 << RE->getSourceRange(); 19380 return true; 19381 } 19382 // If we find the same expression in the enclosing data environment, 19383 // that is legal. 19384 IsEnclosedByDataEnvironmentExpr = true; 19385 return false; 19386 } 19387 19388 QualType DerivedType = 19389 std::prev(CI)->getAssociatedDeclaration()->getType(); 19390 SourceLocation DerivedLoc = 19391 std::prev(CI)->getAssociatedExpression()->getExprLoc(); 19392 19393 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 19394 // If the type of a list item is a reference to a type T then the type 19395 // will be considered to be T for all purposes of this clause. 19396 DerivedType = DerivedType.getNonReferenceType(); 19397 19398 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1] 19399 // A variable for which the type is pointer and an array section 19400 // derived from that variable must not appear as list items of map 19401 // clauses of the same construct. 19402 // 19403 // Also, cover one of the cases in: 19404 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5] 19405 // If any part of the original storage of a list item has corresponding 19406 // storage in the device data environment, all of the original storage 19407 // must have corresponding storage in the device data environment. 19408 // 19409 if (DerivedType->isAnyPointerType()) { 19410 if (CI == CE || SI == SE) { 19411 SemaRef.Diag( 19412 DerivedLoc, 19413 diag::err_omp_pointer_mapped_along_with_derived_section) 19414 << DerivedLoc; 19415 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 19416 << RE->getSourceRange(); 19417 return true; 19418 } 19419 if (CI->getAssociatedExpression()->getStmtClass() != 19420 SI->getAssociatedExpression()->getStmtClass() || 19421 CI->getAssociatedDeclaration()->getCanonicalDecl() == 19422 SI->getAssociatedDeclaration()->getCanonicalDecl()) { 19423 assert(CI != CE && SI != SE); 19424 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_dereferenced) 19425 << DerivedLoc; 19426 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 19427 << RE->getSourceRange(); 19428 return true; 19429 } 19430 } 19431 19432 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4] 19433 // List items of map clauses in the same construct must not share 19434 // original storage. 19435 // 19436 // An expression is a subset of the other. 19437 if (CurrentRegionOnly && (CI == CE || SI == SE)) { 19438 if (CKind == OMPC_map) { 19439 if (CI != CE || SI != SE) { 19440 // Allow constructs like this: map(s, s.ptr[0:1]), where s.ptr is 19441 // a pointer. 19442 auto Begin = 19443 CI != CE ? CurComponents.begin() : StackComponents.begin(); 19444 auto End = CI != CE ? CurComponents.end() : StackComponents.end(); 19445 auto It = Begin; 19446 while (It != End && !It->getAssociatedDeclaration()) 19447 std::advance(It, 1); 19448 assert(It != End && 19449 "Expected at least one component with the declaration."); 19450 if (It != Begin && It->getAssociatedDeclaration() 19451 ->getType() 19452 .getCanonicalType() 19453 ->isAnyPointerType()) { 19454 IsEnclosedByDataEnvironmentExpr = false; 19455 EnclosingExpr = nullptr; 19456 return false; 19457 } 19458 } 19459 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange; 19460 } else { 19461 assert(CKind == OMPC_to || CKind == OMPC_from); 19462 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update) 19463 << ERange; 19464 } 19465 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 19466 << RE->getSourceRange(); 19467 return true; 19468 } 19469 19470 // The current expression uses the same base as other expression in the 19471 // data environment but does not contain it completely. 19472 if (!CurrentRegionOnly && SI != SE) 19473 EnclosingExpr = RE; 19474 19475 // The current expression is a subset of the expression in the data 19476 // environment. 19477 IsEnclosedByDataEnvironmentExpr |= 19478 (!CurrentRegionOnly && CI != CE && SI == SE); 19479 19480 return false; 19481 }); 19482 19483 if (CurrentRegionOnly) 19484 return FoundError; 19485 19486 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5] 19487 // If any part of the original storage of a list item has corresponding 19488 // storage in the device data environment, all of the original storage must 19489 // have corresponding storage in the device data environment. 19490 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6] 19491 // If a list item is an element of a structure, and a different element of 19492 // the structure has a corresponding list item in the device data environment 19493 // prior to a task encountering the construct associated with the map clause, 19494 // then the list item must also have a corresponding list item in the device 19495 // data environment prior to the task encountering the construct. 19496 // 19497 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) { 19498 SemaRef.Diag(ELoc, 19499 diag::err_omp_original_storage_is_shared_and_does_not_contain) 19500 << ERange; 19501 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here) 19502 << EnclosingExpr->getSourceRange(); 19503 return true; 19504 } 19505 19506 return FoundError; 19507 } 19508 19509 // Look up the user-defined mapper given the mapper name and mapped type, and 19510 // build a reference to it. 19511 static ExprResult buildUserDefinedMapperRef(Sema &SemaRef, Scope *S, 19512 CXXScopeSpec &MapperIdScopeSpec, 19513 const DeclarationNameInfo &MapperId, 19514 QualType Type, 19515 Expr *UnresolvedMapper) { 19516 if (MapperIdScopeSpec.isInvalid()) 19517 return ExprError(); 19518 // Get the actual type for the array type. 19519 if (Type->isArrayType()) { 19520 assert(Type->getAsArrayTypeUnsafe() && "Expect to get a valid array type"); 19521 Type = Type->getAsArrayTypeUnsafe()->getElementType().getCanonicalType(); 19522 } 19523 // Find all user-defined mappers with the given MapperId. 19524 SmallVector<UnresolvedSet<8>, 4> Lookups; 19525 LookupResult Lookup(SemaRef, MapperId, Sema::LookupOMPMapperName); 19526 Lookup.suppressDiagnostics(); 19527 if (S) { 19528 while (S && SemaRef.LookupParsedName(Lookup, S, &MapperIdScopeSpec)) { 19529 NamedDecl *D = Lookup.getRepresentativeDecl(); 19530 while (S && !S->isDeclScope(D)) 19531 S = S->getParent(); 19532 if (S) 19533 S = S->getParent(); 19534 Lookups.emplace_back(); 19535 Lookups.back().append(Lookup.begin(), Lookup.end()); 19536 Lookup.clear(); 19537 } 19538 } else if (auto *ULE = cast_or_null<UnresolvedLookupExpr>(UnresolvedMapper)) { 19539 // Extract the user-defined mappers with the given MapperId. 19540 Lookups.push_back(UnresolvedSet<8>()); 19541 for (NamedDecl *D : ULE->decls()) { 19542 auto *DMD = cast<OMPDeclareMapperDecl>(D); 19543 assert(DMD && "Expect valid OMPDeclareMapperDecl during instantiation."); 19544 Lookups.back().addDecl(DMD); 19545 } 19546 } 19547 // Defer the lookup for dependent types. The results will be passed through 19548 // UnresolvedMapper on instantiation. 19549 if (SemaRef.CurContext->isDependentContext() || Type->isDependentType() || 19550 Type->isInstantiationDependentType() || 19551 Type->containsUnexpandedParameterPack() || 19552 filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) { 19553 return !D->isInvalidDecl() && 19554 (D->getType()->isDependentType() || 19555 D->getType()->isInstantiationDependentType() || 19556 D->getType()->containsUnexpandedParameterPack()); 19557 })) { 19558 UnresolvedSet<8> URS; 19559 for (const UnresolvedSet<8> &Set : Lookups) { 19560 if (Set.empty()) 19561 continue; 19562 URS.append(Set.begin(), Set.end()); 19563 } 19564 return UnresolvedLookupExpr::Create( 19565 SemaRef.Context, /*NamingClass=*/nullptr, 19566 MapperIdScopeSpec.getWithLocInContext(SemaRef.Context), MapperId, 19567 /*ADL=*/false, /*Overloaded=*/true, URS.begin(), URS.end()); 19568 } 19569 SourceLocation Loc = MapperId.getLoc(); 19570 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions 19571 // The type must be of struct, union or class type in C and C++ 19572 if (!Type->isStructureOrClassType() && !Type->isUnionType() && 19573 (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default")) { 19574 SemaRef.Diag(Loc, diag::err_omp_mapper_wrong_type); 19575 return ExprError(); 19576 } 19577 // Perform argument dependent lookup. 19578 if (SemaRef.getLangOpts().CPlusPlus && !MapperIdScopeSpec.isSet()) 19579 argumentDependentLookup(SemaRef, MapperId, Loc, Type, Lookups); 19580 // Return the first user-defined mapper with the desired type. 19581 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 19582 Lookups, [&SemaRef, Type](ValueDecl *D) -> ValueDecl * { 19583 if (!D->isInvalidDecl() && 19584 SemaRef.Context.hasSameType(D->getType(), Type)) 19585 return D; 19586 return nullptr; 19587 })) 19588 return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc); 19589 // Find the first user-defined mapper with a type derived from the desired 19590 // type. 19591 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 19592 Lookups, [&SemaRef, Type, Loc](ValueDecl *D) -> ValueDecl * { 19593 if (!D->isInvalidDecl() && 19594 SemaRef.IsDerivedFrom(Loc, Type, D->getType()) && 19595 !Type.isMoreQualifiedThan(D->getType())) 19596 return D; 19597 return nullptr; 19598 })) { 19599 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, 19600 /*DetectVirtual=*/false); 19601 if (SemaRef.IsDerivedFrom(Loc, Type, VD->getType(), Paths)) { 19602 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType( 19603 VD->getType().getUnqualifiedType()))) { 19604 if (SemaRef.CheckBaseClassAccess( 19605 Loc, VD->getType(), Type, Paths.front(), 19606 /*DiagID=*/0) != Sema::AR_inaccessible) { 19607 return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc); 19608 } 19609 } 19610 } 19611 } 19612 // Report error if a mapper is specified, but cannot be found. 19613 if (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default") { 19614 SemaRef.Diag(Loc, diag::err_omp_invalid_mapper) 19615 << Type << MapperId.getName(); 19616 return ExprError(); 19617 } 19618 return ExprEmpty(); 19619 } 19620 19621 namespace { 19622 // Utility struct that gathers all the related lists associated with a mappable 19623 // expression. 19624 struct MappableVarListInfo { 19625 // The list of expressions. 19626 ArrayRef<Expr *> VarList; 19627 // The list of processed expressions. 19628 SmallVector<Expr *, 16> ProcessedVarList; 19629 // The mappble components for each expression. 19630 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents; 19631 // The base declaration of the variable. 19632 SmallVector<ValueDecl *, 16> VarBaseDeclarations; 19633 // The reference to the user-defined mapper associated with every expression. 19634 SmallVector<Expr *, 16> UDMapperList; 19635 19636 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) { 19637 // We have a list of components and base declarations for each entry in the 19638 // variable list. 19639 VarComponents.reserve(VarList.size()); 19640 VarBaseDeclarations.reserve(VarList.size()); 19641 } 19642 }; 19643 } // namespace 19644 19645 // Check the validity of the provided variable list for the provided clause kind 19646 // \a CKind. In the check process the valid expressions, mappable expression 19647 // components, variables, and user-defined mappers are extracted and used to 19648 // fill \a ProcessedVarList, \a VarComponents, \a VarBaseDeclarations, and \a 19649 // UDMapperList in MVLI. \a MapType, \a IsMapTypeImplicit, \a MapperIdScopeSpec, 19650 // and \a MapperId are expected to be valid if the clause kind is 'map'. 19651 static void checkMappableExpressionList( 19652 Sema &SemaRef, DSAStackTy *DSAS, OpenMPClauseKind CKind, 19653 MappableVarListInfo &MVLI, SourceLocation StartLoc, 19654 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo MapperId, 19655 ArrayRef<Expr *> UnresolvedMappers, 19656 OpenMPMapClauseKind MapType = OMPC_MAP_unknown, 19657 ArrayRef<OpenMPMapModifierKind> Modifiers = None, 19658 bool IsMapTypeImplicit = false, bool NoDiagnose = false) { 19659 // We only expect mappable expressions in 'to', 'from', and 'map' clauses. 19660 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) && 19661 "Unexpected clause kind with mappable expressions!"); 19662 19663 // If the identifier of user-defined mapper is not specified, it is "default". 19664 // We do not change the actual name in this clause to distinguish whether a 19665 // mapper is specified explicitly, i.e., it is not explicitly specified when 19666 // MapperId.getName() is empty. 19667 if (!MapperId.getName() || MapperId.getName().isEmpty()) { 19668 auto &DeclNames = SemaRef.getASTContext().DeclarationNames; 19669 MapperId.setName(DeclNames.getIdentifier( 19670 &SemaRef.getASTContext().Idents.get("default"))); 19671 MapperId.setLoc(StartLoc); 19672 } 19673 19674 // Iterators to find the current unresolved mapper expression. 19675 auto UMIt = UnresolvedMappers.begin(), UMEnd = UnresolvedMappers.end(); 19676 bool UpdateUMIt = false; 19677 Expr *UnresolvedMapper = nullptr; 19678 19679 bool HasHoldModifier = 19680 llvm::is_contained(Modifiers, OMPC_MAP_MODIFIER_ompx_hold); 19681 19682 // Keep track of the mappable components and base declarations in this clause. 19683 // Each entry in the list is going to have a list of components associated. We 19684 // record each set of the components so that we can build the clause later on. 19685 // In the end we should have the same amount of declarations and component 19686 // lists. 19687 19688 for (Expr *RE : MVLI.VarList) { 19689 assert(RE && "Null expr in omp to/from/map clause"); 19690 SourceLocation ELoc = RE->getExprLoc(); 19691 19692 // Find the current unresolved mapper expression. 19693 if (UpdateUMIt && UMIt != UMEnd) { 19694 UMIt++; 19695 assert( 19696 UMIt != UMEnd && 19697 "Expect the size of UnresolvedMappers to match with that of VarList"); 19698 } 19699 UpdateUMIt = true; 19700 if (UMIt != UMEnd) 19701 UnresolvedMapper = *UMIt; 19702 19703 const Expr *VE = RE->IgnoreParenLValueCasts(); 19704 19705 if (VE->isValueDependent() || VE->isTypeDependent() || 19706 VE->isInstantiationDependent() || 19707 VE->containsUnexpandedParameterPack()) { 19708 // Try to find the associated user-defined mapper. 19709 ExprResult ER = buildUserDefinedMapperRef( 19710 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId, 19711 VE->getType().getCanonicalType(), UnresolvedMapper); 19712 if (ER.isInvalid()) 19713 continue; 19714 MVLI.UDMapperList.push_back(ER.get()); 19715 // We can only analyze this information once the missing information is 19716 // resolved. 19717 MVLI.ProcessedVarList.push_back(RE); 19718 continue; 19719 } 19720 19721 Expr *SimpleExpr = RE->IgnoreParenCasts(); 19722 19723 if (!RE->isLValue()) { 19724 if (SemaRef.getLangOpts().OpenMP < 50) { 19725 SemaRef.Diag( 19726 ELoc, diag::err_omp_expected_named_var_member_or_array_expression) 19727 << RE->getSourceRange(); 19728 } else { 19729 SemaRef.Diag(ELoc, diag::err_omp_non_lvalue_in_map_or_motion_clauses) 19730 << getOpenMPClauseName(CKind) << RE->getSourceRange(); 19731 } 19732 continue; 19733 } 19734 19735 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents; 19736 ValueDecl *CurDeclaration = nullptr; 19737 19738 // Obtain the array or member expression bases if required. Also, fill the 19739 // components array with all the components identified in the process. 19740 const Expr *BE = 19741 checkMapClauseExpressionBase(SemaRef, SimpleExpr, CurComponents, CKind, 19742 DSAS->getCurrentDirective(), NoDiagnose); 19743 if (!BE) 19744 continue; 19745 19746 assert(!CurComponents.empty() && 19747 "Invalid mappable expression information."); 19748 19749 if (const auto *TE = dyn_cast<CXXThisExpr>(BE)) { 19750 // Add store "this" pointer to class in DSAStackTy for future checking 19751 DSAS->addMappedClassesQualTypes(TE->getType()); 19752 // Try to find the associated user-defined mapper. 19753 ExprResult ER = buildUserDefinedMapperRef( 19754 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId, 19755 VE->getType().getCanonicalType(), UnresolvedMapper); 19756 if (ER.isInvalid()) 19757 continue; 19758 MVLI.UDMapperList.push_back(ER.get()); 19759 // Skip restriction checking for variable or field declarations 19760 MVLI.ProcessedVarList.push_back(RE); 19761 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 19762 MVLI.VarComponents.back().append(CurComponents.begin(), 19763 CurComponents.end()); 19764 MVLI.VarBaseDeclarations.push_back(nullptr); 19765 continue; 19766 } 19767 19768 // For the following checks, we rely on the base declaration which is 19769 // expected to be associated with the last component. The declaration is 19770 // expected to be a variable or a field (if 'this' is being mapped). 19771 CurDeclaration = CurComponents.back().getAssociatedDeclaration(); 19772 assert(CurDeclaration && "Null decl on map clause."); 19773 assert( 19774 CurDeclaration->isCanonicalDecl() && 19775 "Expecting components to have associated only canonical declarations."); 19776 19777 auto *VD = dyn_cast<VarDecl>(CurDeclaration); 19778 const auto *FD = dyn_cast<FieldDecl>(CurDeclaration); 19779 19780 assert((VD || FD) && "Only variables or fields are expected here!"); 19781 (void)FD; 19782 19783 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10] 19784 // threadprivate variables cannot appear in a map clause. 19785 // OpenMP 4.5 [2.10.5, target update Construct] 19786 // threadprivate variables cannot appear in a from clause. 19787 if (VD && DSAS->isThreadPrivate(VD)) { 19788 if (NoDiagnose) 19789 continue; 19790 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false); 19791 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause) 19792 << getOpenMPClauseName(CKind); 19793 reportOriginalDsa(SemaRef, DSAS, VD, DVar); 19794 continue; 19795 } 19796 19797 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9] 19798 // A list item cannot appear in both a map clause and a data-sharing 19799 // attribute clause on the same construct. 19800 19801 // Check conflicts with other map clause expressions. We check the conflicts 19802 // with the current construct separately from the enclosing data 19803 // environment, because the restrictions are different. We only have to 19804 // check conflicts across regions for the map clauses. 19805 if (checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr, 19806 /*CurrentRegionOnly=*/true, CurComponents, CKind)) 19807 break; 19808 if (CKind == OMPC_map && 19809 (SemaRef.getLangOpts().OpenMP <= 45 || StartLoc.isValid()) && 19810 checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr, 19811 /*CurrentRegionOnly=*/false, CurComponents, CKind)) 19812 break; 19813 19814 // OpenMP 4.5 [2.10.5, target update Construct] 19815 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 19816 // If the type of a list item is a reference to a type T then the type will 19817 // be considered to be T for all purposes of this clause. 19818 auto I = llvm::find_if( 19819 CurComponents, 19820 [](const OMPClauseMappableExprCommon::MappableComponent &MC) { 19821 return MC.getAssociatedDeclaration(); 19822 }); 19823 assert(I != CurComponents.end() && "Null decl on map clause."); 19824 (void)I; 19825 QualType Type; 19826 auto *ASE = dyn_cast<ArraySubscriptExpr>(VE->IgnoreParens()); 19827 auto *OASE = dyn_cast<OMPArraySectionExpr>(VE->IgnoreParens()); 19828 auto *OAShE = dyn_cast<OMPArrayShapingExpr>(VE->IgnoreParens()); 19829 if (ASE) { 19830 Type = ASE->getType().getNonReferenceType(); 19831 } else if (OASE) { 19832 QualType BaseType = 19833 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 19834 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 19835 Type = ATy->getElementType(); 19836 else 19837 Type = BaseType->getPointeeType(); 19838 Type = Type.getNonReferenceType(); 19839 } else if (OAShE) { 19840 Type = OAShE->getBase()->getType()->getPointeeType(); 19841 } else { 19842 Type = VE->getType(); 19843 } 19844 19845 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4] 19846 // A list item in a to or from clause must have a mappable type. 19847 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9] 19848 // A list item must have a mappable type. 19849 if (!checkTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef, 19850 DSAS, Type, /*FullCheck=*/true)) 19851 continue; 19852 19853 if (CKind == OMPC_map) { 19854 // target enter data 19855 // OpenMP [2.10.2, Restrictions, p. 99] 19856 // A map-type must be specified in all map clauses and must be either 19857 // to or alloc. 19858 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective(); 19859 if (DKind == OMPD_target_enter_data && 19860 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) { 19861 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 19862 << (IsMapTypeImplicit ? 1 : 0) 19863 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 19864 << getOpenMPDirectiveName(DKind); 19865 continue; 19866 } 19867 19868 // target exit_data 19869 // OpenMP [2.10.3, Restrictions, p. 102] 19870 // A map-type must be specified in all map clauses and must be either 19871 // from, release, or delete. 19872 if (DKind == OMPD_target_exit_data && 19873 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release || 19874 MapType == OMPC_MAP_delete)) { 19875 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 19876 << (IsMapTypeImplicit ? 1 : 0) 19877 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 19878 << getOpenMPDirectiveName(DKind); 19879 continue; 19880 } 19881 19882 // The 'ompx_hold' modifier is specifically intended to be used on a 19883 // 'target' or 'target data' directive to prevent data from being unmapped 19884 // during the associated statement. It is not permitted on a 'target 19885 // enter data' or 'target exit data' directive, which have no associated 19886 // statement. 19887 if ((DKind == OMPD_target_enter_data || DKind == OMPD_target_exit_data) && 19888 HasHoldModifier) { 19889 SemaRef.Diag(StartLoc, 19890 diag::err_omp_invalid_map_type_modifier_for_directive) 19891 << getOpenMPSimpleClauseTypeName(OMPC_map, 19892 OMPC_MAP_MODIFIER_ompx_hold) 19893 << getOpenMPDirectiveName(DKind); 19894 continue; 19895 } 19896 19897 // target, target data 19898 // OpenMP 5.0 [2.12.2, Restrictions, p. 163] 19899 // OpenMP 5.0 [2.12.5, Restrictions, p. 174] 19900 // A map-type in a map clause must be to, from, tofrom or alloc 19901 if ((DKind == OMPD_target_data || 19902 isOpenMPTargetExecutionDirective(DKind)) && 19903 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_from || 19904 MapType == OMPC_MAP_tofrom || MapType == OMPC_MAP_alloc)) { 19905 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 19906 << (IsMapTypeImplicit ? 1 : 0) 19907 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 19908 << getOpenMPDirectiveName(DKind); 19909 continue; 19910 } 19911 19912 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 19913 // A list item cannot appear in both a map clause and a data-sharing 19914 // attribute clause on the same construct 19915 // 19916 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7] 19917 // A list item cannot appear in both a map clause and a data-sharing 19918 // attribute clause on the same construct unless the construct is a 19919 // combined construct. 19920 if (VD && ((SemaRef.LangOpts.OpenMP <= 45 && 19921 isOpenMPTargetExecutionDirective(DKind)) || 19922 DKind == OMPD_target)) { 19923 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false); 19924 if (isOpenMPPrivate(DVar.CKind)) { 19925 SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 19926 << getOpenMPClauseName(DVar.CKind) 19927 << getOpenMPClauseName(OMPC_map) 19928 << getOpenMPDirectiveName(DSAS->getCurrentDirective()); 19929 reportOriginalDsa(SemaRef, DSAS, CurDeclaration, DVar); 19930 continue; 19931 } 19932 } 19933 } 19934 19935 // Try to find the associated user-defined mapper. 19936 ExprResult ER = buildUserDefinedMapperRef( 19937 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId, 19938 Type.getCanonicalType(), UnresolvedMapper); 19939 if (ER.isInvalid()) 19940 continue; 19941 MVLI.UDMapperList.push_back(ER.get()); 19942 19943 // Save the current expression. 19944 MVLI.ProcessedVarList.push_back(RE); 19945 19946 // Store the components in the stack so that they can be used to check 19947 // against other clauses later on. 19948 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents, 19949 /*WhereFoundClauseKind=*/OMPC_map); 19950 19951 // Save the components and declaration to create the clause. For purposes of 19952 // the clause creation, any component list that has has base 'this' uses 19953 // null as base declaration. 19954 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 19955 MVLI.VarComponents.back().append(CurComponents.begin(), 19956 CurComponents.end()); 19957 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr 19958 : CurDeclaration); 19959 } 19960 } 19961 19962 OMPClause *Sema::ActOnOpenMPMapClause( 19963 ArrayRef<OpenMPMapModifierKind> MapTypeModifiers, 19964 ArrayRef<SourceLocation> MapTypeModifiersLoc, 19965 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId, 19966 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, SourceLocation MapLoc, 19967 SourceLocation ColonLoc, ArrayRef<Expr *> VarList, 19968 const OMPVarListLocTy &Locs, bool NoDiagnose, 19969 ArrayRef<Expr *> UnresolvedMappers) { 19970 OpenMPMapModifierKind Modifiers[] = { 19971 OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown, 19972 OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown, 19973 OMPC_MAP_MODIFIER_unknown}; 19974 SourceLocation ModifiersLoc[NumberOfOMPMapClauseModifiers]; 19975 19976 // Process map-type-modifiers, flag errors for duplicate modifiers. 19977 unsigned Count = 0; 19978 for (unsigned I = 0, E = MapTypeModifiers.size(); I < E; ++I) { 19979 if (MapTypeModifiers[I] != OMPC_MAP_MODIFIER_unknown && 19980 llvm::is_contained(Modifiers, MapTypeModifiers[I])) { 19981 Diag(MapTypeModifiersLoc[I], diag::err_omp_duplicate_map_type_modifier); 19982 continue; 19983 } 19984 assert(Count < NumberOfOMPMapClauseModifiers && 19985 "Modifiers exceed the allowed number of map type modifiers"); 19986 Modifiers[Count] = MapTypeModifiers[I]; 19987 ModifiersLoc[Count] = MapTypeModifiersLoc[I]; 19988 ++Count; 19989 } 19990 19991 MappableVarListInfo MVLI(VarList); 19992 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, Locs.StartLoc, 19993 MapperIdScopeSpec, MapperId, UnresolvedMappers, 19994 MapType, Modifiers, IsMapTypeImplicit, 19995 NoDiagnose); 19996 19997 // We need to produce a map clause even if we don't have variables so that 19998 // other diagnostics related with non-existing map clauses are accurate. 19999 return OMPMapClause::Create(Context, Locs, MVLI.ProcessedVarList, 20000 MVLI.VarBaseDeclarations, MVLI.VarComponents, 20001 MVLI.UDMapperList, Modifiers, ModifiersLoc, 20002 MapperIdScopeSpec.getWithLocInContext(Context), 20003 MapperId, MapType, IsMapTypeImplicit, MapLoc); 20004 } 20005 20006 QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc, 20007 TypeResult ParsedType) { 20008 assert(ParsedType.isUsable()); 20009 20010 QualType ReductionType = GetTypeFromParser(ParsedType.get()); 20011 if (ReductionType.isNull()) 20012 return QualType(); 20013 20014 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++ 20015 // A type name in a declare reduction directive cannot be a function type, an 20016 // array type, a reference type, or a type qualified with const, volatile or 20017 // restrict. 20018 if (ReductionType.hasQualifiers()) { 20019 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0; 20020 return QualType(); 20021 } 20022 20023 if (ReductionType->isFunctionType()) { 20024 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1; 20025 return QualType(); 20026 } 20027 if (ReductionType->isReferenceType()) { 20028 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2; 20029 return QualType(); 20030 } 20031 if (ReductionType->isArrayType()) { 20032 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3; 20033 return QualType(); 20034 } 20035 return ReductionType; 20036 } 20037 20038 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart( 20039 Scope *S, DeclContext *DC, DeclarationName Name, 20040 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes, 20041 AccessSpecifier AS, Decl *PrevDeclInScope) { 20042 SmallVector<Decl *, 8> Decls; 20043 Decls.reserve(ReductionTypes.size()); 20044 20045 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName, 20046 forRedeclarationInCurContext()); 20047 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions 20048 // A reduction-identifier may not be re-declared in the current scope for the 20049 // same type or for a type that is compatible according to the base language 20050 // rules. 20051 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes; 20052 OMPDeclareReductionDecl *PrevDRD = nullptr; 20053 bool InCompoundScope = true; 20054 if (S != nullptr) { 20055 // Find previous declaration with the same name not referenced in other 20056 // declarations. 20057 FunctionScopeInfo *ParentFn = getEnclosingFunction(); 20058 InCompoundScope = 20059 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty(); 20060 LookupName(Lookup, S); 20061 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false, 20062 /*AllowInlineNamespace=*/false); 20063 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious; 20064 LookupResult::Filter Filter = Lookup.makeFilter(); 20065 while (Filter.hasNext()) { 20066 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next()); 20067 if (InCompoundScope) { 20068 auto I = UsedAsPrevious.find(PrevDecl); 20069 if (I == UsedAsPrevious.end()) 20070 UsedAsPrevious[PrevDecl] = false; 20071 if (OMPDeclareReductionDecl *D = PrevDecl->getPrevDeclInScope()) 20072 UsedAsPrevious[D] = true; 20073 } 20074 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] = 20075 PrevDecl->getLocation(); 20076 } 20077 Filter.done(); 20078 if (InCompoundScope) { 20079 for (const auto &PrevData : UsedAsPrevious) { 20080 if (!PrevData.second) { 20081 PrevDRD = PrevData.first; 20082 break; 20083 } 20084 } 20085 } 20086 } else if (PrevDeclInScope != nullptr) { 20087 auto *PrevDRDInScope = PrevDRD = 20088 cast<OMPDeclareReductionDecl>(PrevDeclInScope); 20089 do { 20090 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] = 20091 PrevDRDInScope->getLocation(); 20092 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope(); 20093 } while (PrevDRDInScope != nullptr); 20094 } 20095 for (const auto &TyData : ReductionTypes) { 20096 const auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType()); 20097 bool Invalid = false; 20098 if (I != PreviousRedeclTypes.end()) { 20099 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition) 20100 << TyData.first; 20101 Diag(I->second, diag::note_previous_definition); 20102 Invalid = true; 20103 } 20104 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second; 20105 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second, 20106 Name, TyData.first, PrevDRD); 20107 DC->addDecl(DRD); 20108 DRD->setAccess(AS); 20109 Decls.push_back(DRD); 20110 if (Invalid) 20111 DRD->setInvalidDecl(); 20112 else 20113 PrevDRD = DRD; 20114 } 20115 20116 return DeclGroupPtrTy::make( 20117 DeclGroupRef::Create(Context, Decls.begin(), Decls.size())); 20118 } 20119 20120 void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) { 20121 auto *DRD = cast<OMPDeclareReductionDecl>(D); 20122 20123 // Enter new function scope. 20124 PushFunctionScope(); 20125 setFunctionHasBranchProtectedScope(); 20126 getCurFunction()->setHasOMPDeclareReductionCombiner(); 20127 20128 if (S != nullptr) 20129 PushDeclContext(S, DRD); 20130 else 20131 CurContext = DRD; 20132 20133 PushExpressionEvaluationContext( 20134 ExpressionEvaluationContext::PotentiallyEvaluated); 20135 20136 QualType ReductionType = DRD->getType(); 20137 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will 20138 // be replaced by '*omp_parm' during codegen. This required because 'omp_in' 20139 // uses semantics of argument handles by value, but it should be passed by 20140 // reference. C lang does not support references, so pass all parameters as 20141 // pointers. 20142 // Create 'T omp_in;' variable. 20143 VarDecl *OmpInParm = 20144 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in"); 20145 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will 20146 // be replaced by '*omp_parm' during codegen. This required because 'omp_out' 20147 // uses semantics of argument handles by value, but it should be passed by 20148 // reference. C lang does not support references, so pass all parameters as 20149 // pointers. 20150 // Create 'T omp_out;' variable. 20151 VarDecl *OmpOutParm = 20152 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out"); 20153 if (S != nullptr) { 20154 PushOnScopeChains(OmpInParm, S); 20155 PushOnScopeChains(OmpOutParm, S); 20156 } else { 20157 DRD->addDecl(OmpInParm); 20158 DRD->addDecl(OmpOutParm); 20159 } 20160 Expr *InE = 20161 ::buildDeclRefExpr(*this, OmpInParm, ReductionType, D->getLocation()); 20162 Expr *OutE = 20163 ::buildDeclRefExpr(*this, OmpOutParm, ReductionType, D->getLocation()); 20164 DRD->setCombinerData(InE, OutE); 20165 } 20166 20167 void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) { 20168 auto *DRD = cast<OMPDeclareReductionDecl>(D); 20169 DiscardCleanupsInEvaluationContext(); 20170 PopExpressionEvaluationContext(); 20171 20172 PopDeclContext(); 20173 PopFunctionScopeInfo(); 20174 20175 if (Combiner != nullptr) 20176 DRD->setCombiner(Combiner); 20177 else 20178 DRD->setInvalidDecl(); 20179 } 20180 20181 VarDecl *Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) { 20182 auto *DRD = cast<OMPDeclareReductionDecl>(D); 20183 20184 // Enter new function scope. 20185 PushFunctionScope(); 20186 setFunctionHasBranchProtectedScope(); 20187 20188 if (S != nullptr) 20189 PushDeclContext(S, DRD); 20190 else 20191 CurContext = DRD; 20192 20193 PushExpressionEvaluationContext( 20194 ExpressionEvaluationContext::PotentiallyEvaluated); 20195 20196 QualType ReductionType = DRD->getType(); 20197 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will 20198 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv' 20199 // uses semantics of argument handles by value, but it should be passed by 20200 // reference. C lang does not support references, so pass all parameters as 20201 // pointers. 20202 // Create 'T omp_priv;' variable. 20203 VarDecl *OmpPrivParm = 20204 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv"); 20205 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will 20206 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig' 20207 // uses semantics of argument handles by value, but it should be passed by 20208 // reference. C lang does not support references, so pass all parameters as 20209 // pointers. 20210 // Create 'T omp_orig;' variable. 20211 VarDecl *OmpOrigParm = 20212 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig"); 20213 if (S != nullptr) { 20214 PushOnScopeChains(OmpPrivParm, S); 20215 PushOnScopeChains(OmpOrigParm, S); 20216 } else { 20217 DRD->addDecl(OmpPrivParm); 20218 DRD->addDecl(OmpOrigParm); 20219 } 20220 Expr *OrigE = 20221 ::buildDeclRefExpr(*this, OmpOrigParm, ReductionType, D->getLocation()); 20222 Expr *PrivE = 20223 ::buildDeclRefExpr(*this, OmpPrivParm, ReductionType, D->getLocation()); 20224 DRD->setInitializerData(OrigE, PrivE); 20225 return OmpPrivParm; 20226 } 20227 20228 void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, Expr *Initializer, 20229 VarDecl *OmpPrivParm) { 20230 auto *DRD = cast<OMPDeclareReductionDecl>(D); 20231 DiscardCleanupsInEvaluationContext(); 20232 PopExpressionEvaluationContext(); 20233 20234 PopDeclContext(); 20235 PopFunctionScopeInfo(); 20236 20237 if (Initializer != nullptr) { 20238 DRD->setInitializer(Initializer, OMPDeclareReductionDecl::CallInit); 20239 } else if (OmpPrivParm->hasInit()) { 20240 DRD->setInitializer(OmpPrivParm->getInit(), 20241 OmpPrivParm->isDirectInit() 20242 ? OMPDeclareReductionDecl::DirectInit 20243 : OMPDeclareReductionDecl::CopyInit); 20244 } else { 20245 DRD->setInvalidDecl(); 20246 } 20247 } 20248 20249 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd( 20250 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) { 20251 for (Decl *D : DeclReductions.get()) { 20252 if (IsValid) { 20253 if (S) 20254 PushOnScopeChains(cast<OMPDeclareReductionDecl>(D), S, 20255 /*AddToContext=*/false); 20256 } else { 20257 D->setInvalidDecl(); 20258 } 20259 } 20260 return DeclReductions; 20261 } 20262 20263 TypeResult Sema::ActOnOpenMPDeclareMapperVarDecl(Scope *S, Declarator &D) { 20264 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); 20265 QualType T = TInfo->getType(); 20266 if (D.isInvalidType()) 20267 return true; 20268 20269 if (getLangOpts().CPlusPlus) { 20270 // Check that there are no default arguments (C++ only). 20271 CheckExtraCXXDefaultArguments(D); 20272 } 20273 20274 return CreateParsedType(T, TInfo); 20275 } 20276 20277 QualType Sema::ActOnOpenMPDeclareMapperType(SourceLocation TyLoc, 20278 TypeResult ParsedType) { 20279 assert(ParsedType.isUsable() && "Expect usable parsed mapper type"); 20280 20281 QualType MapperType = GetTypeFromParser(ParsedType.get()); 20282 assert(!MapperType.isNull() && "Expect valid mapper type"); 20283 20284 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions 20285 // The type must be of struct, union or class type in C and C++ 20286 if (!MapperType->isStructureOrClassType() && !MapperType->isUnionType()) { 20287 Diag(TyLoc, diag::err_omp_mapper_wrong_type); 20288 return QualType(); 20289 } 20290 return MapperType; 20291 } 20292 20293 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareMapperDirective( 20294 Scope *S, DeclContext *DC, DeclarationName Name, QualType MapperType, 20295 SourceLocation StartLoc, DeclarationName VN, AccessSpecifier AS, 20296 Expr *MapperVarRef, ArrayRef<OMPClause *> Clauses, Decl *PrevDeclInScope) { 20297 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPMapperName, 20298 forRedeclarationInCurContext()); 20299 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions 20300 // A mapper-identifier may not be redeclared in the current scope for the 20301 // same type or for a type that is compatible according to the base language 20302 // rules. 20303 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes; 20304 OMPDeclareMapperDecl *PrevDMD = nullptr; 20305 bool InCompoundScope = true; 20306 if (S != nullptr) { 20307 // Find previous declaration with the same name not referenced in other 20308 // declarations. 20309 FunctionScopeInfo *ParentFn = getEnclosingFunction(); 20310 InCompoundScope = 20311 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty(); 20312 LookupName(Lookup, S); 20313 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false, 20314 /*AllowInlineNamespace=*/false); 20315 llvm::DenseMap<OMPDeclareMapperDecl *, bool> UsedAsPrevious; 20316 LookupResult::Filter Filter = Lookup.makeFilter(); 20317 while (Filter.hasNext()) { 20318 auto *PrevDecl = cast<OMPDeclareMapperDecl>(Filter.next()); 20319 if (InCompoundScope) { 20320 auto I = UsedAsPrevious.find(PrevDecl); 20321 if (I == UsedAsPrevious.end()) 20322 UsedAsPrevious[PrevDecl] = false; 20323 if (OMPDeclareMapperDecl *D = PrevDecl->getPrevDeclInScope()) 20324 UsedAsPrevious[D] = true; 20325 } 20326 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] = 20327 PrevDecl->getLocation(); 20328 } 20329 Filter.done(); 20330 if (InCompoundScope) { 20331 for (const auto &PrevData : UsedAsPrevious) { 20332 if (!PrevData.second) { 20333 PrevDMD = PrevData.first; 20334 break; 20335 } 20336 } 20337 } 20338 } else if (PrevDeclInScope) { 20339 auto *PrevDMDInScope = PrevDMD = 20340 cast<OMPDeclareMapperDecl>(PrevDeclInScope); 20341 do { 20342 PreviousRedeclTypes[PrevDMDInScope->getType().getCanonicalType()] = 20343 PrevDMDInScope->getLocation(); 20344 PrevDMDInScope = PrevDMDInScope->getPrevDeclInScope(); 20345 } while (PrevDMDInScope != nullptr); 20346 } 20347 const auto I = PreviousRedeclTypes.find(MapperType.getCanonicalType()); 20348 bool Invalid = false; 20349 if (I != PreviousRedeclTypes.end()) { 20350 Diag(StartLoc, diag::err_omp_declare_mapper_redefinition) 20351 << MapperType << Name; 20352 Diag(I->second, diag::note_previous_definition); 20353 Invalid = true; 20354 } 20355 // Build expressions for implicit maps of data members with 'default' 20356 // mappers. 20357 SmallVector<OMPClause *, 4> ClausesWithImplicit(Clauses.begin(), 20358 Clauses.end()); 20359 if (LangOpts.OpenMP >= 50) 20360 processImplicitMapsWithDefaultMappers(*this, DSAStack, ClausesWithImplicit); 20361 auto *DMD = 20362 OMPDeclareMapperDecl::Create(Context, DC, StartLoc, Name, MapperType, VN, 20363 ClausesWithImplicit, PrevDMD); 20364 if (S) 20365 PushOnScopeChains(DMD, S); 20366 else 20367 DC->addDecl(DMD); 20368 DMD->setAccess(AS); 20369 if (Invalid) 20370 DMD->setInvalidDecl(); 20371 20372 auto *VD = cast<DeclRefExpr>(MapperVarRef)->getDecl(); 20373 VD->setDeclContext(DMD); 20374 VD->setLexicalDeclContext(DMD); 20375 DMD->addDecl(VD); 20376 DMD->setMapperVarRef(MapperVarRef); 20377 20378 return DeclGroupPtrTy::make(DeclGroupRef(DMD)); 20379 } 20380 20381 ExprResult 20382 Sema::ActOnOpenMPDeclareMapperDirectiveVarDecl(Scope *S, QualType MapperType, 20383 SourceLocation StartLoc, 20384 DeclarationName VN) { 20385 TypeSourceInfo *TInfo = 20386 Context.getTrivialTypeSourceInfo(MapperType, StartLoc); 20387 auto *VD = VarDecl::Create(Context, Context.getTranslationUnitDecl(), 20388 StartLoc, StartLoc, VN.getAsIdentifierInfo(), 20389 MapperType, TInfo, SC_None); 20390 if (S) 20391 PushOnScopeChains(VD, S, /*AddToContext=*/false); 20392 Expr *E = buildDeclRefExpr(*this, VD, MapperType, StartLoc); 20393 DSAStack->addDeclareMapperVarRef(E); 20394 return E; 20395 } 20396 20397 bool Sema::isOpenMPDeclareMapperVarDeclAllowed(const VarDecl *VD) const { 20398 assert(LangOpts.OpenMP && "Expected OpenMP mode."); 20399 const Expr *Ref = DSAStack->getDeclareMapperVarRef(); 20400 if (const auto *DRE = cast_or_null<DeclRefExpr>(Ref)) { 20401 if (VD->getCanonicalDecl() == DRE->getDecl()->getCanonicalDecl()) 20402 return true; 20403 if (VD->isUsableInConstantExpressions(Context)) 20404 return true; 20405 return false; 20406 } 20407 return true; 20408 } 20409 20410 const ValueDecl *Sema::getOpenMPDeclareMapperVarName() const { 20411 assert(LangOpts.OpenMP && "Expected OpenMP mode."); 20412 return cast<DeclRefExpr>(DSAStack->getDeclareMapperVarRef())->getDecl(); 20413 } 20414 20415 OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams, 20416 SourceLocation StartLoc, 20417 SourceLocation LParenLoc, 20418 SourceLocation EndLoc) { 20419 Expr *ValExpr = NumTeams; 20420 Stmt *HelperValStmt = nullptr; 20421 20422 // OpenMP [teams Constrcut, Restrictions] 20423 // The num_teams expression must evaluate to a positive integer value. 20424 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams, 20425 /*StrictlyPositive=*/true)) 20426 return nullptr; 20427 20428 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 20429 OpenMPDirectiveKind CaptureRegion = 20430 getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams, LangOpts.OpenMP); 20431 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 20432 ValExpr = MakeFullExpr(ValExpr).get(); 20433 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 20434 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 20435 HelperValStmt = buildPreInits(Context, Captures); 20436 } 20437 20438 return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion, 20439 StartLoc, LParenLoc, EndLoc); 20440 } 20441 20442 OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit, 20443 SourceLocation StartLoc, 20444 SourceLocation LParenLoc, 20445 SourceLocation EndLoc) { 20446 Expr *ValExpr = ThreadLimit; 20447 Stmt *HelperValStmt = nullptr; 20448 20449 // OpenMP [teams Constrcut, Restrictions] 20450 // The thread_limit expression must evaluate to a positive integer value. 20451 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit, 20452 /*StrictlyPositive=*/true)) 20453 return nullptr; 20454 20455 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 20456 OpenMPDirectiveKind CaptureRegion = getOpenMPCaptureRegionForClause( 20457 DKind, OMPC_thread_limit, LangOpts.OpenMP); 20458 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 20459 ValExpr = MakeFullExpr(ValExpr).get(); 20460 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 20461 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 20462 HelperValStmt = buildPreInits(Context, Captures); 20463 } 20464 20465 return new (Context) OMPThreadLimitClause( 20466 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc); 20467 } 20468 20469 OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority, 20470 SourceLocation StartLoc, 20471 SourceLocation LParenLoc, 20472 SourceLocation EndLoc) { 20473 Expr *ValExpr = Priority; 20474 Stmt *HelperValStmt = nullptr; 20475 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 20476 20477 // OpenMP [2.9.1, task Constrcut] 20478 // The priority-value is a non-negative numerical scalar expression. 20479 if (!isNonNegativeIntegerValue( 20480 ValExpr, *this, OMPC_priority, 20481 /*StrictlyPositive=*/false, /*BuildCapture=*/true, 20482 DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt)) 20483 return nullptr; 20484 20485 return new (Context) OMPPriorityClause(ValExpr, HelperValStmt, CaptureRegion, 20486 StartLoc, LParenLoc, EndLoc); 20487 } 20488 20489 OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize, 20490 SourceLocation StartLoc, 20491 SourceLocation LParenLoc, 20492 SourceLocation EndLoc) { 20493 Expr *ValExpr = Grainsize; 20494 Stmt *HelperValStmt = nullptr; 20495 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 20496 20497 // OpenMP [2.9.2, taskloop Constrcut] 20498 // The parameter of the grainsize clause must be a positive integer 20499 // expression. 20500 if (!isNonNegativeIntegerValue( 20501 ValExpr, *this, OMPC_grainsize, 20502 /*StrictlyPositive=*/true, /*BuildCapture=*/true, 20503 DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt)) 20504 return nullptr; 20505 20506 return new (Context) OMPGrainsizeClause(ValExpr, HelperValStmt, CaptureRegion, 20507 StartLoc, LParenLoc, EndLoc); 20508 } 20509 20510 OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks, 20511 SourceLocation StartLoc, 20512 SourceLocation LParenLoc, 20513 SourceLocation EndLoc) { 20514 Expr *ValExpr = NumTasks; 20515 Stmt *HelperValStmt = nullptr; 20516 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 20517 20518 // OpenMP [2.9.2, taskloop Constrcut] 20519 // The parameter of the num_tasks clause must be a positive integer 20520 // expression. 20521 if (!isNonNegativeIntegerValue( 20522 ValExpr, *this, OMPC_num_tasks, 20523 /*StrictlyPositive=*/true, /*BuildCapture=*/true, 20524 DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt)) 20525 return nullptr; 20526 20527 return new (Context) OMPNumTasksClause(ValExpr, HelperValStmt, CaptureRegion, 20528 StartLoc, LParenLoc, EndLoc); 20529 } 20530 20531 OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc, 20532 SourceLocation LParenLoc, 20533 SourceLocation EndLoc) { 20534 // OpenMP [2.13.2, critical construct, Description] 20535 // ... where hint-expression is an integer constant expression that evaluates 20536 // to a valid lock hint. 20537 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint); 20538 if (HintExpr.isInvalid()) 20539 return nullptr; 20540 return new (Context) 20541 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc); 20542 } 20543 20544 /// Tries to find omp_event_handle_t type. 20545 static bool findOMPEventHandleT(Sema &S, SourceLocation Loc, 20546 DSAStackTy *Stack) { 20547 QualType OMPEventHandleT = Stack->getOMPEventHandleT(); 20548 if (!OMPEventHandleT.isNull()) 20549 return true; 20550 IdentifierInfo *II = &S.PP.getIdentifierTable().get("omp_event_handle_t"); 20551 ParsedType PT = S.getTypeName(*II, Loc, S.getCurScope()); 20552 if (!PT.getAsOpaquePtr() || PT.get().isNull()) { 20553 S.Diag(Loc, diag::err_omp_implied_type_not_found) << "omp_event_handle_t"; 20554 return false; 20555 } 20556 Stack->setOMPEventHandleT(PT.get()); 20557 return true; 20558 } 20559 20560 OMPClause *Sema::ActOnOpenMPDetachClause(Expr *Evt, SourceLocation StartLoc, 20561 SourceLocation LParenLoc, 20562 SourceLocation EndLoc) { 20563 if (!Evt->isValueDependent() && !Evt->isTypeDependent() && 20564 !Evt->isInstantiationDependent() && 20565 !Evt->containsUnexpandedParameterPack()) { 20566 if (!findOMPEventHandleT(*this, Evt->getExprLoc(), DSAStack)) 20567 return nullptr; 20568 // OpenMP 5.0, 2.10.1 task Construct. 20569 // event-handle is a variable of the omp_event_handle_t type. 20570 auto *Ref = dyn_cast<DeclRefExpr>(Evt->IgnoreParenImpCasts()); 20571 if (!Ref) { 20572 Diag(Evt->getExprLoc(), diag::err_omp_var_expected) 20573 << "omp_event_handle_t" << 0 << Evt->getSourceRange(); 20574 return nullptr; 20575 } 20576 auto *VD = dyn_cast_or_null<VarDecl>(Ref->getDecl()); 20577 if (!VD) { 20578 Diag(Evt->getExprLoc(), diag::err_omp_var_expected) 20579 << "omp_event_handle_t" << 0 << Evt->getSourceRange(); 20580 return nullptr; 20581 } 20582 if (!Context.hasSameUnqualifiedType(DSAStack->getOMPEventHandleT(), 20583 VD->getType()) || 20584 VD->getType().isConstant(Context)) { 20585 Diag(Evt->getExprLoc(), diag::err_omp_var_expected) 20586 << "omp_event_handle_t" << 1 << VD->getType() 20587 << Evt->getSourceRange(); 20588 return nullptr; 20589 } 20590 // OpenMP 5.0, 2.10.1 task Construct 20591 // [detach clause]... The event-handle will be considered as if it was 20592 // specified on a firstprivate clause. 20593 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, /*FromParent=*/false); 20594 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate && 20595 DVar.RefExpr) { 20596 Diag(Evt->getExprLoc(), diag::err_omp_wrong_dsa) 20597 << getOpenMPClauseName(DVar.CKind) 20598 << getOpenMPClauseName(OMPC_firstprivate); 20599 reportOriginalDsa(*this, DSAStack, VD, DVar); 20600 return nullptr; 20601 } 20602 } 20603 20604 return new (Context) OMPDetachClause(Evt, StartLoc, LParenLoc, EndLoc); 20605 } 20606 20607 OMPClause *Sema::ActOnOpenMPDistScheduleClause( 20608 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, 20609 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc, 20610 SourceLocation EndLoc) { 20611 if (Kind == OMPC_DIST_SCHEDULE_unknown) { 20612 std::string Values; 20613 Values += "'"; 20614 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0); 20615 Values += "'"; 20616 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 20617 << Values << getOpenMPClauseName(OMPC_dist_schedule); 20618 return nullptr; 20619 } 20620 Expr *ValExpr = ChunkSize; 20621 Stmt *HelperValStmt = nullptr; 20622 if (ChunkSize) { 20623 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && 20624 !ChunkSize->isInstantiationDependent() && 20625 !ChunkSize->containsUnexpandedParameterPack()) { 20626 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc(); 20627 ExprResult Val = 20628 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); 20629 if (Val.isInvalid()) 20630 return nullptr; 20631 20632 ValExpr = Val.get(); 20633 20634 // OpenMP [2.7.1, Restrictions] 20635 // chunk_size must be a loop invariant integer expression with a positive 20636 // value. 20637 if (Optional<llvm::APSInt> Result = 20638 ValExpr->getIntegerConstantExpr(Context)) { 20639 if (Result->isSigned() && !Result->isStrictlyPositive()) { 20640 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) 20641 << "dist_schedule" << ChunkSize->getSourceRange(); 20642 return nullptr; 20643 } 20644 } else if (getOpenMPCaptureRegionForClause( 20645 DSAStack->getCurrentDirective(), OMPC_dist_schedule, 20646 LangOpts.OpenMP) != OMPD_unknown && 20647 !CurContext->isDependentContext()) { 20648 ValExpr = MakeFullExpr(ValExpr).get(); 20649 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 20650 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 20651 HelperValStmt = buildPreInits(Context, Captures); 20652 } 20653 } 20654 } 20655 20656 return new (Context) 20657 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, 20658 Kind, ValExpr, HelperValStmt); 20659 } 20660 20661 OMPClause *Sema::ActOnOpenMPDefaultmapClause( 20662 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind, 20663 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc, 20664 SourceLocation KindLoc, SourceLocation EndLoc) { 20665 if (getLangOpts().OpenMP < 50) { 20666 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || 20667 Kind != OMPC_DEFAULTMAP_scalar) { 20668 std::string Value; 20669 SourceLocation Loc; 20670 Value += "'"; 20671 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) { 20672 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap, 20673 OMPC_DEFAULTMAP_MODIFIER_tofrom); 20674 Loc = MLoc; 20675 } else { 20676 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap, 20677 OMPC_DEFAULTMAP_scalar); 20678 Loc = KindLoc; 20679 } 20680 Value += "'"; 20681 Diag(Loc, diag::err_omp_unexpected_clause_value) 20682 << Value << getOpenMPClauseName(OMPC_defaultmap); 20683 return nullptr; 20684 } 20685 } else { 20686 bool isDefaultmapModifier = (M != OMPC_DEFAULTMAP_MODIFIER_unknown); 20687 bool isDefaultmapKind = (Kind != OMPC_DEFAULTMAP_unknown) || 20688 (LangOpts.OpenMP >= 50 && KindLoc.isInvalid()); 20689 if (!isDefaultmapKind || !isDefaultmapModifier) { 20690 StringRef KindValue = "'scalar', 'aggregate', 'pointer'"; 20691 if (LangOpts.OpenMP == 50) { 20692 StringRef ModifierValue = "'alloc', 'from', 'to', 'tofrom', " 20693 "'firstprivate', 'none', 'default'"; 20694 if (!isDefaultmapKind && isDefaultmapModifier) { 20695 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 20696 << KindValue << getOpenMPClauseName(OMPC_defaultmap); 20697 } else if (isDefaultmapKind && !isDefaultmapModifier) { 20698 Diag(MLoc, diag::err_omp_unexpected_clause_value) 20699 << ModifierValue << getOpenMPClauseName(OMPC_defaultmap); 20700 } else { 20701 Diag(MLoc, diag::err_omp_unexpected_clause_value) 20702 << ModifierValue << getOpenMPClauseName(OMPC_defaultmap); 20703 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 20704 << KindValue << getOpenMPClauseName(OMPC_defaultmap); 20705 } 20706 } else { 20707 StringRef ModifierValue = 20708 "'alloc', 'from', 'to', 'tofrom', " 20709 "'firstprivate', 'none', 'default', 'present'"; 20710 if (!isDefaultmapKind && isDefaultmapModifier) { 20711 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 20712 << KindValue << getOpenMPClauseName(OMPC_defaultmap); 20713 } else if (isDefaultmapKind && !isDefaultmapModifier) { 20714 Diag(MLoc, diag::err_omp_unexpected_clause_value) 20715 << ModifierValue << getOpenMPClauseName(OMPC_defaultmap); 20716 } else { 20717 Diag(MLoc, diag::err_omp_unexpected_clause_value) 20718 << ModifierValue << getOpenMPClauseName(OMPC_defaultmap); 20719 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 20720 << KindValue << getOpenMPClauseName(OMPC_defaultmap); 20721 } 20722 } 20723 return nullptr; 20724 } 20725 20726 // OpenMP [5.0, 2.12.5, Restrictions, p. 174] 20727 // At most one defaultmap clause for each category can appear on the 20728 // directive. 20729 if (DSAStack->checkDefaultmapCategory(Kind)) { 20730 Diag(StartLoc, diag::err_omp_one_defaultmap_each_category); 20731 return nullptr; 20732 } 20733 } 20734 if (Kind == OMPC_DEFAULTMAP_unknown) { 20735 // Variable category is not specified - mark all categories. 20736 DSAStack->setDefaultDMAAttr(M, OMPC_DEFAULTMAP_aggregate, StartLoc); 20737 DSAStack->setDefaultDMAAttr(M, OMPC_DEFAULTMAP_scalar, StartLoc); 20738 DSAStack->setDefaultDMAAttr(M, OMPC_DEFAULTMAP_pointer, StartLoc); 20739 } else { 20740 DSAStack->setDefaultDMAAttr(M, Kind, StartLoc); 20741 } 20742 20743 return new (Context) 20744 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M); 20745 } 20746 20747 bool Sema::ActOnStartOpenMPDeclareTargetContext( 20748 DeclareTargetContextInfo &DTCI) { 20749 DeclContext *CurLexicalContext = getCurLexicalContext(); 20750 if (!CurLexicalContext->isFileContext() && 20751 !CurLexicalContext->isExternCContext() && 20752 !CurLexicalContext->isExternCXXContext() && 20753 !isa<CXXRecordDecl>(CurLexicalContext) && 20754 !isa<ClassTemplateDecl>(CurLexicalContext) && 20755 !isa<ClassTemplatePartialSpecializationDecl>(CurLexicalContext) && 20756 !isa<ClassTemplateSpecializationDecl>(CurLexicalContext)) { 20757 Diag(DTCI.Loc, diag::err_omp_region_not_file_context); 20758 return false; 20759 } 20760 DeclareTargetNesting.push_back(DTCI); 20761 return true; 20762 } 20763 20764 const Sema::DeclareTargetContextInfo 20765 Sema::ActOnOpenMPEndDeclareTargetDirective() { 20766 assert(!DeclareTargetNesting.empty() && 20767 "check isInOpenMPDeclareTargetContext() first!"); 20768 return DeclareTargetNesting.pop_back_val(); 20769 } 20770 20771 void Sema::ActOnFinishedOpenMPDeclareTargetContext( 20772 DeclareTargetContextInfo &DTCI) { 20773 for (auto &It : DTCI.ExplicitlyMapped) 20774 ActOnOpenMPDeclareTargetName(It.first, It.second.Loc, It.second.MT, DTCI); 20775 } 20776 20777 NamedDecl *Sema::lookupOpenMPDeclareTargetName(Scope *CurScope, 20778 CXXScopeSpec &ScopeSpec, 20779 const DeclarationNameInfo &Id) { 20780 LookupResult Lookup(*this, Id, LookupOrdinaryName); 20781 LookupParsedName(Lookup, CurScope, &ScopeSpec, true); 20782 20783 if (Lookup.isAmbiguous()) 20784 return nullptr; 20785 Lookup.suppressDiagnostics(); 20786 20787 if (!Lookup.isSingleResult()) { 20788 VarOrFuncDeclFilterCCC CCC(*this); 20789 if (TypoCorrection Corrected = 20790 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, CCC, 20791 CTK_ErrorRecovery)) { 20792 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest) 20793 << Id.getName()); 20794 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl()); 20795 return nullptr; 20796 } 20797 20798 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName(); 20799 return nullptr; 20800 } 20801 20802 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>(); 20803 if (!isa<VarDecl>(ND) && !isa<FunctionDecl>(ND) && 20804 !isa<FunctionTemplateDecl>(ND)) { 20805 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName(); 20806 return nullptr; 20807 } 20808 return ND; 20809 } 20810 20811 void Sema::ActOnOpenMPDeclareTargetName(NamedDecl *ND, SourceLocation Loc, 20812 OMPDeclareTargetDeclAttr::MapTypeTy MT, 20813 DeclareTargetContextInfo &DTCI) { 20814 assert((isa<VarDecl>(ND) || isa<FunctionDecl>(ND) || 20815 isa<FunctionTemplateDecl>(ND)) && 20816 "Expected variable, function or function template."); 20817 20818 // Diagnose marking after use as it may lead to incorrect diagnosis and 20819 // codegen. 20820 if (LangOpts.OpenMP >= 50 && 20821 (ND->isUsed(/*CheckUsedAttr=*/false) || ND->isReferenced())) 20822 Diag(Loc, diag::warn_omp_declare_target_after_first_use); 20823 20824 // Explicit declare target lists have precedence. 20825 const unsigned Level = -1; 20826 20827 auto *VD = cast<ValueDecl>(ND); 20828 llvm::Optional<OMPDeclareTargetDeclAttr *> ActiveAttr = 20829 OMPDeclareTargetDeclAttr::getActiveAttr(VD); 20830 if (ActiveAttr.hasValue() && ActiveAttr.getValue()->getDevType() != DTCI.DT && 20831 ActiveAttr.getValue()->getLevel() == Level) { 20832 Diag(Loc, diag::err_omp_device_type_mismatch) 20833 << OMPDeclareTargetDeclAttr::ConvertDevTypeTyToStr(DTCI.DT) 20834 << OMPDeclareTargetDeclAttr::ConvertDevTypeTyToStr( 20835 ActiveAttr.getValue()->getDevType()); 20836 return; 20837 } 20838 if (ActiveAttr.hasValue() && ActiveAttr.getValue()->getMapType() != MT && 20839 ActiveAttr.getValue()->getLevel() == Level) { 20840 Diag(Loc, diag::err_omp_declare_target_to_and_link) << ND; 20841 return; 20842 } 20843 20844 if (ActiveAttr.hasValue() && ActiveAttr.getValue()->getLevel() == Level) 20845 return; 20846 20847 Expr *IndirectE = nullptr; 20848 bool IsIndirect = false; 20849 if (DTCI.Indirect.hasValue()) { 20850 IndirectE = DTCI.Indirect.getValue(); 20851 if (!IndirectE) 20852 IsIndirect = true; 20853 } 20854 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit( 20855 Context, MT, DTCI.DT, IndirectE, IsIndirect, Level, 20856 SourceRange(Loc, Loc)); 20857 ND->addAttr(A); 20858 if (ASTMutationListener *ML = Context.getASTMutationListener()) 20859 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A); 20860 checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Loc); 20861 } 20862 20863 static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR, 20864 Sema &SemaRef, Decl *D) { 20865 if (!D || !isa<VarDecl>(D)) 20866 return; 20867 auto *VD = cast<VarDecl>(D); 20868 Optional<OMPDeclareTargetDeclAttr::MapTypeTy> MapTy = 20869 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); 20870 if (SemaRef.LangOpts.OpenMP >= 50 && 20871 (SemaRef.getCurLambda(/*IgnoreNonLambdaCapturingScope=*/true) || 20872 SemaRef.getCurBlock() || SemaRef.getCurCapturedRegion()) && 20873 VD->hasGlobalStorage()) { 20874 if (!MapTy || *MapTy != OMPDeclareTargetDeclAttr::MT_To) { 20875 // OpenMP 5.0, 2.12.7 declare target Directive, Restrictions 20876 // If a lambda declaration and definition appears between a 20877 // declare target directive and the matching end declare target 20878 // directive, all variables that are captured by the lambda 20879 // expression must also appear in a to clause. 20880 SemaRef.Diag(VD->getLocation(), 20881 diag::err_omp_lambda_capture_in_declare_target_not_to); 20882 SemaRef.Diag(SL, diag::note_var_explicitly_captured_here) 20883 << VD << 0 << SR; 20884 return; 20885 } 20886 } 20887 if (MapTy.hasValue()) 20888 return; 20889 SemaRef.Diag(VD->getLocation(), diag::warn_omp_not_in_target_context); 20890 SemaRef.Diag(SL, diag::note_used_here) << SR; 20891 } 20892 20893 static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR, 20894 Sema &SemaRef, DSAStackTy *Stack, 20895 ValueDecl *VD) { 20896 return OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD) || 20897 checkTypeMappable(SL, SR, SemaRef, Stack, VD->getType(), 20898 /*FullCheck=*/false); 20899 } 20900 20901 void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D, 20902 SourceLocation IdLoc) { 20903 if (!D || D->isInvalidDecl()) 20904 return; 20905 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange(); 20906 SourceLocation SL = E ? E->getBeginLoc() : D->getLocation(); 20907 if (auto *VD = dyn_cast<VarDecl>(D)) { 20908 // Only global variables can be marked as declare target. 20909 if (!VD->isFileVarDecl() && !VD->isStaticLocal() && 20910 !VD->isStaticDataMember()) 20911 return; 20912 // 2.10.6: threadprivate variable cannot appear in a declare target 20913 // directive. 20914 if (DSAStack->isThreadPrivate(VD)) { 20915 Diag(SL, diag::err_omp_threadprivate_in_target); 20916 reportOriginalDsa(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false)); 20917 return; 20918 } 20919 } 20920 if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(D)) 20921 D = FTD->getTemplatedDecl(); 20922 if (auto *FD = dyn_cast<FunctionDecl>(D)) { 20923 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 20924 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(FD); 20925 if (IdLoc.isValid() && Res && *Res == OMPDeclareTargetDeclAttr::MT_Link) { 20926 Diag(IdLoc, diag::err_omp_function_in_link_clause); 20927 Diag(FD->getLocation(), diag::note_defined_here) << FD; 20928 return; 20929 } 20930 } 20931 if (auto *VD = dyn_cast<ValueDecl>(D)) { 20932 // Problem if any with var declared with incomplete type will be reported 20933 // as normal, so no need to check it here. 20934 if ((E || !VD->getType()->isIncompleteType()) && 20935 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) 20936 return; 20937 if (!E && isInOpenMPDeclareTargetContext()) { 20938 // Checking declaration inside declare target region. 20939 if (isa<VarDecl>(D) || isa<FunctionDecl>(D) || 20940 isa<FunctionTemplateDecl>(D)) { 20941 llvm::Optional<OMPDeclareTargetDeclAttr *> ActiveAttr = 20942 OMPDeclareTargetDeclAttr::getActiveAttr(VD); 20943 unsigned Level = DeclareTargetNesting.size(); 20944 if (ActiveAttr.hasValue() && ActiveAttr.getValue()->getLevel() >= Level) 20945 return; 20946 DeclareTargetContextInfo &DTCI = DeclareTargetNesting.back(); 20947 Expr *IndirectE = nullptr; 20948 bool IsIndirect = false; 20949 if (DTCI.Indirect.hasValue()) { 20950 IndirectE = DTCI.Indirect.getValue(); 20951 if (!IndirectE) 20952 IsIndirect = true; 20953 } 20954 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit( 20955 Context, OMPDeclareTargetDeclAttr::MT_To, DTCI.DT, IndirectE, 20956 IsIndirect, Level, SourceRange(DTCI.Loc, DTCI.Loc)); 20957 D->addAttr(A); 20958 if (ASTMutationListener *ML = Context.getASTMutationListener()) 20959 ML->DeclarationMarkedOpenMPDeclareTarget(D, A); 20960 } 20961 return; 20962 } 20963 } 20964 if (!E) 20965 return; 20966 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D); 20967 } 20968 20969 OMPClause *Sema::ActOnOpenMPToClause( 20970 ArrayRef<OpenMPMotionModifierKind> MotionModifiers, 20971 ArrayRef<SourceLocation> MotionModifiersLoc, 20972 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId, 20973 SourceLocation ColonLoc, ArrayRef<Expr *> VarList, 20974 const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) { 20975 OpenMPMotionModifierKind Modifiers[] = {OMPC_MOTION_MODIFIER_unknown, 20976 OMPC_MOTION_MODIFIER_unknown}; 20977 SourceLocation ModifiersLoc[NumberOfOMPMotionModifiers]; 20978 20979 // Process motion-modifiers, flag errors for duplicate modifiers. 20980 unsigned Count = 0; 20981 for (unsigned I = 0, E = MotionModifiers.size(); I < E; ++I) { 20982 if (MotionModifiers[I] != OMPC_MOTION_MODIFIER_unknown && 20983 llvm::is_contained(Modifiers, MotionModifiers[I])) { 20984 Diag(MotionModifiersLoc[I], diag::err_omp_duplicate_motion_modifier); 20985 continue; 20986 } 20987 assert(Count < NumberOfOMPMotionModifiers && 20988 "Modifiers exceed the allowed number of motion modifiers"); 20989 Modifiers[Count] = MotionModifiers[I]; 20990 ModifiersLoc[Count] = MotionModifiersLoc[I]; 20991 ++Count; 20992 } 20993 20994 MappableVarListInfo MVLI(VarList); 20995 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, Locs.StartLoc, 20996 MapperIdScopeSpec, MapperId, UnresolvedMappers); 20997 if (MVLI.ProcessedVarList.empty()) 20998 return nullptr; 20999 21000 return OMPToClause::Create( 21001 Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, 21002 MVLI.VarComponents, MVLI.UDMapperList, Modifiers, ModifiersLoc, 21003 MapperIdScopeSpec.getWithLocInContext(Context), MapperId); 21004 } 21005 21006 OMPClause *Sema::ActOnOpenMPFromClause( 21007 ArrayRef<OpenMPMotionModifierKind> MotionModifiers, 21008 ArrayRef<SourceLocation> MotionModifiersLoc, 21009 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId, 21010 SourceLocation ColonLoc, ArrayRef<Expr *> VarList, 21011 const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) { 21012 OpenMPMotionModifierKind Modifiers[] = {OMPC_MOTION_MODIFIER_unknown, 21013 OMPC_MOTION_MODIFIER_unknown}; 21014 SourceLocation ModifiersLoc[NumberOfOMPMotionModifiers]; 21015 21016 // Process motion-modifiers, flag errors for duplicate modifiers. 21017 unsigned Count = 0; 21018 for (unsigned I = 0, E = MotionModifiers.size(); I < E; ++I) { 21019 if (MotionModifiers[I] != OMPC_MOTION_MODIFIER_unknown && 21020 llvm::is_contained(Modifiers, MotionModifiers[I])) { 21021 Diag(MotionModifiersLoc[I], diag::err_omp_duplicate_motion_modifier); 21022 continue; 21023 } 21024 assert(Count < NumberOfOMPMotionModifiers && 21025 "Modifiers exceed the allowed number of motion modifiers"); 21026 Modifiers[Count] = MotionModifiers[I]; 21027 ModifiersLoc[Count] = MotionModifiersLoc[I]; 21028 ++Count; 21029 } 21030 21031 MappableVarListInfo MVLI(VarList); 21032 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, Locs.StartLoc, 21033 MapperIdScopeSpec, MapperId, UnresolvedMappers); 21034 if (MVLI.ProcessedVarList.empty()) 21035 return nullptr; 21036 21037 return OMPFromClause::Create( 21038 Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, 21039 MVLI.VarComponents, MVLI.UDMapperList, Modifiers, ModifiersLoc, 21040 MapperIdScopeSpec.getWithLocInContext(Context), MapperId); 21041 } 21042 21043 OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList, 21044 const OMPVarListLocTy &Locs) { 21045 MappableVarListInfo MVLI(VarList); 21046 SmallVector<Expr *, 8> PrivateCopies; 21047 SmallVector<Expr *, 8> Inits; 21048 21049 for (Expr *RefExpr : VarList) { 21050 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause."); 21051 SourceLocation ELoc; 21052 SourceRange ERange; 21053 Expr *SimpleRefExpr = RefExpr; 21054 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 21055 if (Res.second) { 21056 // It will be analyzed later. 21057 MVLI.ProcessedVarList.push_back(RefExpr); 21058 PrivateCopies.push_back(nullptr); 21059 Inits.push_back(nullptr); 21060 } 21061 ValueDecl *D = Res.first; 21062 if (!D) 21063 continue; 21064 21065 QualType Type = D->getType(); 21066 Type = Type.getNonReferenceType().getUnqualifiedType(); 21067 21068 auto *VD = dyn_cast<VarDecl>(D); 21069 21070 // Item should be a pointer or reference to pointer. 21071 if (!Type->isPointerType()) { 21072 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer) 21073 << 0 << RefExpr->getSourceRange(); 21074 continue; 21075 } 21076 21077 // Build the private variable and the expression that refers to it. 21078 auto VDPrivate = 21079 buildVarDecl(*this, ELoc, Type, D->getName(), 21080 D->hasAttrs() ? &D->getAttrs() : nullptr, 21081 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 21082 if (VDPrivate->isInvalidDecl()) 21083 continue; 21084 21085 CurContext->addDecl(VDPrivate); 21086 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 21087 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc); 21088 21089 // Add temporary variable to initialize the private copy of the pointer. 21090 VarDecl *VDInit = 21091 buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp"); 21092 DeclRefExpr *VDInitRefExpr = buildDeclRefExpr( 21093 *this, VDInit, RefExpr->getType(), RefExpr->getExprLoc()); 21094 AddInitializerToDecl(VDPrivate, 21095 DefaultLvalueConversion(VDInitRefExpr).get(), 21096 /*DirectInit=*/false); 21097 21098 // If required, build a capture to implement the privatization initialized 21099 // with the current list item value. 21100 DeclRefExpr *Ref = nullptr; 21101 if (!VD) 21102 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 21103 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref); 21104 PrivateCopies.push_back(VDPrivateRefExpr); 21105 Inits.push_back(VDInitRefExpr); 21106 21107 // We need to add a data sharing attribute for this variable to make sure it 21108 // is correctly captured. A variable that shows up in a use_device_ptr has 21109 // similar properties of a first private variable. 21110 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 21111 21112 // Create a mappable component for the list item. List items in this clause 21113 // only need a component. 21114 MVLI.VarBaseDeclarations.push_back(D); 21115 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 21116 MVLI.VarComponents.back().emplace_back(SimpleRefExpr, D, 21117 /*IsNonContiguous=*/false); 21118 } 21119 21120 if (MVLI.ProcessedVarList.empty()) 21121 return nullptr; 21122 21123 return OMPUseDevicePtrClause::Create( 21124 Context, Locs, MVLI.ProcessedVarList, PrivateCopies, Inits, 21125 MVLI.VarBaseDeclarations, MVLI.VarComponents); 21126 } 21127 21128 OMPClause *Sema::ActOnOpenMPUseDeviceAddrClause(ArrayRef<Expr *> VarList, 21129 const OMPVarListLocTy &Locs) { 21130 MappableVarListInfo MVLI(VarList); 21131 21132 for (Expr *RefExpr : VarList) { 21133 assert(RefExpr && "NULL expr in OpenMP use_device_addr clause."); 21134 SourceLocation ELoc; 21135 SourceRange ERange; 21136 Expr *SimpleRefExpr = RefExpr; 21137 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, 21138 /*AllowArraySection=*/true); 21139 if (Res.second) { 21140 // It will be analyzed later. 21141 MVLI.ProcessedVarList.push_back(RefExpr); 21142 } 21143 ValueDecl *D = Res.first; 21144 if (!D) 21145 continue; 21146 auto *VD = dyn_cast<VarDecl>(D); 21147 21148 // If required, build a capture to implement the privatization initialized 21149 // with the current list item value. 21150 DeclRefExpr *Ref = nullptr; 21151 if (!VD) 21152 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 21153 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref); 21154 21155 // We need to add a data sharing attribute for this variable to make sure it 21156 // is correctly captured. A variable that shows up in a use_device_addr has 21157 // similar properties of a first private variable. 21158 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 21159 21160 // Create a mappable component for the list item. List items in this clause 21161 // only need a component. 21162 MVLI.VarBaseDeclarations.push_back(D); 21163 MVLI.VarComponents.emplace_back(); 21164 Expr *Component = SimpleRefExpr; 21165 if (VD && (isa<OMPArraySectionExpr>(RefExpr->IgnoreParenImpCasts()) || 21166 isa<ArraySubscriptExpr>(RefExpr->IgnoreParenImpCasts()))) 21167 Component = DefaultFunctionArrayLvalueConversion(SimpleRefExpr).get(); 21168 MVLI.VarComponents.back().emplace_back(Component, D, 21169 /*IsNonContiguous=*/false); 21170 } 21171 21172 if (MVLI.ProcessedVarList.empty()) 21173 return nullptr; 21174 21175 return OMPUseDeviceAddrClause::Create(Context, Locs, MVLI.ProcessedVarList, 21176 MVLI.VarBaseDeclarations, 21177 MVLI.VarComponents); 21178 } 21179 21180 OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList, 21181 const OMPVarListLocTy &Locs) { 21182 MappableVarListInfo MVLI(VarList); 21183 for (Expr *RefExpr : VarList) { 21184 assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause."); 21185 SourceLocation ELoc; 21186 SourceRange ERange; 21187 Expr *SimpleRefExpr = RefExpr; 21188 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 21189 if (Res.second) { 21190 // It will be analyzed later. 21191 MVLI.ProcessedVarList.push_back(RefExpr); 21192 } 21193 ValueDecl *D = Res.first; 21194 if (!D) 21195 continue; 21196 21197 QualType Type = D->getType(); 21198 // item should be a pointer or array or reference to pointer or array 21199 if (!Type.getNonReferenceType()->isPointerType() && 21200 !Type.getNonReferenceType()->isArrayType()) { 21201 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr) 21202 << 0 << RefExpr->getSourceRange(); 21203 continue; 21204 } 21205 21206 // Check if the declaration in the clause does not show up in any data 21207 // sharing attribute. 21208 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 21209 if (isOpenMPPrivate(DVar.CKind)) { 21210 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 21211 << getOpenMPClauseName(DVar.CKind) 21212 << getOpenMPClauseName(OMPC_is_device_ptr) 21213 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 21214 reportOriginalDsa(*this, DSAStack, D, DVar); 21215 continue; 21216 } 21217 21218 const Expr *ConflictExpr; 21219 if (DSAStack->checkMappableExprComponentListsForDecl( 21220 D, /*CurrentRegionOnly=*/true, 21221 [&ConflictExpr]( 21222 OMPClauseMappableExprCommon::MappableExprComponentListRef R, 21223 OpenMPClauseKind) -> bool { 21224 ConflictExpr = R.front().getAssociatedExpression(); 21225 return true; 21226 })) { 21227 Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange(); 21228 Diag(ConflictExpr->getExprLoc(), diag::note_used_here) 21229 << ConflictExpr->getSourceRange(); 21230 continue; 21231 } 21232 21233 // Store the components in the stack so that they can be used to check 21234 // against other clauses later on. 21235 OMPClauseMappableExprCommon::MappableComponent MC( 21236 SimpleRefExpr, D, /*IsNonContiguous=*/false); 21237 DSAStack->addMappableExpressionComponents( 21238 D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr); 21239 21240 // Record the expression we've just processed. 21241 MVLI.ProcessedVarList.push_back(SimpleRefExpr); 21242 21243 // Create a mappable component for the list item. List items in this clause 21244 // only need a component. We use a null declaration to signal fields in 21245 // 'this'. 21246 assert((isa<DeclRefExpr>(SimpleRefExpr) || 21247 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) && 21248 "Unexpected device pointer expression!"); 21249 MVLI.VarBaseDeclarations.push_back( 21250 isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr); 21251 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 21252 MVLI.VarComponents.back().push_back(MC); 21253 } 21254 21255 if (MVLI.ProcessedVarList.empty()) 21256 return nullptr; 21257 21258 return OMPIsDevicePtrClause::Create(Context, Locs, MVLI.ProcessedVarList, 21259 MVLI.VarBaseDeclarations, 21260 MVLI.VarComponents); 21261 } 21262 21263 OMPClause *Sema::ActOnOpenMPAllocateClause( 21264 Expr *Allocator, ArrayRef<Expr *> VarList, SourceLocation StartLoc, 21265 SourceLocation ColonLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 21266 if (Allocator) { 21267 // OpenMP [2.11.4 allocate Clause, Description] 21268 // allocator is an expression of omp_allocator_handle_t type. 21269 if (!findOMPAllocatorHandleT(*this, Allocator->getExprLoc(), DSAStack)) 21270 return nullptr; 21271 21272 ExprResult AllocatorRes = DefaultLvalueConversion(Allocator); 21273 if (AllocatorRes.isInvalid()) 21274 return nullptr; 21275 AllocatorRes = PerformImplicitConversion(AllocatorRes.get(), 21276 DSAStack->getOMPAllocatorHandleT(), 21277 Sema::AA_Initializing, 21278 /*AllowExplicit=*/true); 21279 if (AllocatorRes.isInvalid()) 21280 return nullptr; 21281 Allocator = AllocatorRes.get(); 21282 } else { 21283 // OpenMP 5.0, 2.11.4 allocate Clause, Restrictions. 21284 // allocate clauses that appear on a target construct or on constructs in a 21285 // target region must specify an allocator expression unless a requires 21286 // directive with the dynamic_allocators clause is present in the same 21287 // compilation unit. 21288 if (LangOpts.OpenMPIsDevice && 21289 !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>()) 21290 targetDiag(StartLoc, diag::err_expected_allocator_expression); 21291 } 21292 // Analyze and build list of variables. 21293 SmallVector<Expr *, 8> Vars; 21294 for (Expr *RefExpr : VarList) { 21295 assert(RefExpr && "NULL expr in OpenMP private clause."); 21296 SourceLocation ELoc; 21297 SourceRange ERange; 21298 Expr *SimpleRefExpr = RefExpr; 21299 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 21300 if (Res.second) { 21301 // It will be analyzed later. 21302 Vars.push_back(RefExpr); 21303 } 21304 ValueDecl *D = Res.first; 21305 if (!D) 21306 continue; 21307 21308 auto *VD = dyn_cast<VarDecl>(D); 21309 DeclRefExpr *Ref = nullptr; 21310 if (!VD && !CurContext->isDependentContext()) 21311 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 21312 Vars.push_back((VD || CurContext->isDependentContext()) 21313 ? RefExpr->IgnoreParens() 21314 : Ref); 21315 } 21316 21317 if (Vars.empty()) 21318 return nullptr; 21319 21320 if (Allocator) 21321 DSAStack->addInnerAllocatorExpr(Allocator); 21322 return OMPAllocateClause::Create(Context, StartLoc, LParenLoc, Allocator, 21323 ColonLoc, EndLoc, Vars); 21324 } 21325 21326 OMPClause *Sema::ActOnOpenMPNontemporalClause(ArrayRef<Expr *> VarList, 21327 SourceLocation StartLoc, 21328 SourceLocation LParenLoc, 21329 SourceLocation EndLoc) { 21330 SmallVector<Expr *, 8> Vars; 21331 for (Expr *RefExpr : VarList) { 21332 assert(RefExpr && "NULL expr in OpenMP nontemporal clause."); 21333 SourceLocation ELoc; 21334 SourceRange ERange; 21335 Expr *SimpleRefExpr = RefExpr; 21336 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 21337 if (Res.second) 21338 // It will be analyzed later. 21339 Vars.push_back(RefExpr); 21340 ValueDecl *D = Res.first; 21341 if (!D) 21342 continue; 21343 21344 // OpenMP 5.0, 2.9.3.1 simd Construct, Restrictions. 21345 // A list-item cannot appear in more than one nontemporal clause. 21346 if (const Expr *PrevRef = 21347 DSAStack->addUniqueNontemporal(D, SimpleRefExpr)) { 21348 Diag(ELoc, diag::err_omp_used_in_clause_twice) 21349 << 0 << getOpenMPClauseName(OMPC_nontemporal) << ERange; 21350 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa) 21351 << getOpenMPClauseName(OMPC_nontemporal); 21352 continue; 21353 } 21354 21355 Vars.push_back(RefExpr); 21356 } 21357 21358 if (Vars.empty()) 21359 return nullptr; 21360 21361 return OMPNontemporalClause::Create(Context, StartLoc, LParenLoc, EndLoc, 21362 Vars); 21363 } 21364 21365 OMPClause *Sema::ActOnOpenMPInclusiveClause(ArrayRef<Expr *> VarList, 21366 SourceLocation StartLoc, 21367 SourceLocation LParenLoc, 21368 SourceLocation EndLoc) { 21369 SmallVector<Expr *, 8> Vars; 21370 for (Expr *RefExpr : VarList) { 21371 assert(RefExpr && "NULL expr in OpenMP nontemporal clause."); 21372 SourceLocation ELoc; 21373 SourceRange ERange; 21374 Expr *SimpleRefExpr = RefExpr; 21375 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, 21376 /*AllowArraySection=*/true); 21377 if (Res.second) 21378 // It will be analyzed later. 21379 Vars.push_back(RefExpr); 21380 ValueDecl *D = Res.first; 21381 if (!D) 21382 continue; 21383 21384 const DSAStackTy::DSAVarData DVar = 21385 DSAStack->getTopDSA(D, /*FromParent=*/true); 21386 // OpenMP 5.0, 2.9.6, scan Directive, Restrictions. 21387 // A list item that appears in the inclusive or exclusive clause must appear 21388 // in a reduction clause with the inscan modifier on the enclosing 21389 // worksharing-loop, worksharing-loop SIMD, or simd construct. 21390 if (DVar.CKind != OMPC_reduction || DVar.Modifier != OMPC_REDUCTION_inscan) 21391 Diag(ELoc, diag::err_omp_inclusive_exclusive_not_reduction) 21392 << RefExpr->getSourceRange(); 21393 21394 if (DSAStack->getParentDirective() != OMPD_unknown) 21395 DSAStack->markDeclAsUsedInScanDirective(D); 21396 Vars.push_back(RefExpr); 21397 } 21398 21399 if (Vars.empty()) 21400 return nullptr; 21401 21402 return OMPInclusiveClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 21403 } 21404 21405 OMPClause *Sema::ActOnOpenMPExclusiveClause(ArrayRef<Expr *> VarList, 21406 SourceLocation StartLoc, 21407 SourceLocation LParenLoc, 21408 SourceLocation EndLoc) { 21409 SmallVector<Expr *, 8> Vars; 21410 for (Expr *RefExpr : VarList) { 21411 assert(RefExpr && "NULL expr in OpenMP nontemporal clause."); 21412 SourceLocation ELoc; 21413 SourceRange ERange; 21414 Expr *SimpleRefExpr = RefExpr; 21415 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, 21416 /*AllowArraySection=*/true); 21417 if (Res.second) 21418 // It will be analyzed later. 21419 Vars.push_back(RefExpr); 21420 ValueDecl *D = Res.first; 21421 if (!D) 21422 continue; 21423 21424 OpenMPDirectiveKind ParentDirective = DSAStack->getParentDirective(); 21425 DSAStackTy::DSAVarData DVar; 21426 if (ParentDirective != OMPD_unknown) 21427 DVar = DSAStack->getTopDSA(D, /*FromParent=*/true); 21428 // OpenMP 5.0, 2.9.6, scan Directive, Restrictions. 21429 // A list item that appears in the inclusive or exclusive clause must appear 21430 // in a reduction clause with the inscan modifier on the enclosing 21431 // worksharing-loop, worksharing-loop SIMD, or simd construct. 21432 if (ParentDirective == OMPD_unknown || DVar.CKind != OMPC_reduction || 21433 DVar.Modifier != OMPC_REDUCTION_inscan) { 21434 Diag(ELoc, diag::err_omp_inclusive_exclusive_not_reduction) 21435 << RefExpr->getSourceRange(); 21436 } else { 21437 DSAStack->markDeclAsUsedInScanDirective(D); 21438 } 21439 Vars.push_back(RefExpr); 21440 } 21441 21442 if (Vars.empty()) 21443 return nullptr; 21444 21445 return OMPExclusiveClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 21446 } 21447 21448 /// Tries to find omp_alloctrait_t type. 21449 static bool findOMPAlloctraitT(Sema &S, SourceLocation Loc, DSAStackTy *Stack) { 21450 QualType OMPAlloctraitT = Stack->getOMPAlloctraitT(); 21451 if (!OMPAlloctraitT.isNull()) 21452 return true; 21453 IdentifierInfo &II = S.PP.getIdentifierTable().get("omp_alloctrait_t"); 21454 ParsedType PT = S.getTypeName(II, Loc, S.getCurScope()); 21455 if (!PT.getAsOpaquePtr() || PT.get().isNull()) { 21456 S.Diag(Loc, diag::err_omp_implied_type_not_found) << "omp_alloctrait_t"; 21457 return false; 21458 } 21459 Stack->setOMPAlloctraitT(PT.get()); 21460 return true; 21461 } 21462 21463 OMPClause *Sema::ActOnOpenMPUsesAllocatorClause( 21464 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc, 21465 ArrayRef<UsesAllocatorsData> Data) { 21466 // OpenMP [2.12.5, target Construct] 21467 // allocator is an identifier of omp_allocator_handle_t type. 21468 if (!findOMPAllocatorHandleT(*this, StartLoc, DSAStack)) 21469 return nullptr; 21470 // OpenMP [2.12.5, target Construct] 21471 // allocator-traits-array is an identifier of const omp_alloctrait_t * type. 21472 if (llvm::any_of( 21473 Data, 21474 [](const UsesAllocatorsData &D) { return D.AllocatorTraits; }) && 21475 !findOMPAlloctraitT(*this, StartLoc, DSAStack)) 21476 return nullptr; 21477 llvm::SmallPtrSet<CanonicalDeclPtr<Decl>, 4> PredefinedAllocators; 21478 for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) { 21479 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I); 21480 StringRef Allocator = 21481 OMPAllocateDeclAttr::ConvertAllocatorTypeTyToStr(AllocatorKind); 21482 DeclarationName AllocatorName = &Context.Idents.get(Allocator); 21483 PredefinedAllocators.insert(LookupSingleName( 21484 TUScope, AllocatorName, StartLoc, Sema::LookupAnyName)); 21485 } 21486 21487 SmallVector<OMPUsesAllocatorsClause::Data, 4> NewData; 21488 for (const UsesAllocatorsData &D : Data) { 21489 Expr *AllocatorExpr = nullptr; 21490 // Check allocator expression. 21491 if (D.Allocator->isTypeDependent()) { 21492 AllocatorExpr = D.Allocator; 21493 } else { 21494 // Traits were specified - need to assign new allocator to the specified 21495 // allocator, so it must be an lvalue. 21496 AllocatorExpr = D.Allocator->IgnoreParenImpCasts(); 21497 auto *DRE = dyn_cast<DeclRefExpr>(AllocatorExpr); 21498 bool IsPredefinedAllocator = false; 21499 if (DRE) 21500 IsPredefinedAllocator = PredefinedAllocators.count(DRE->getDecl()); 21501 if (!DRE || 21502 !(Context.hasSameUnqualifiedType( 21503 AllocatorExpr->getType(), DSAStack->getOMPAllocatorHandleT()) || 21504 Context.typesAreCompatible(AllocatorExpr->getType(), 21505 DSAStack->getOMPAllocatorHandleT(), 21506 /*CompareUnqualified=*/true)) || 21507 (!IsPredefinedAllocator && 21508 (AllocatorExpr->getType().isConstant(Context) || 21509 !AllocatorExpr->isLValue()))) { 21510 Diag(D.Allocator->getExprLoc(), diag::err_omp_var_expected) 21511 << "omp_allocator_handle_t" << (DRE ? 1 : 0) 21512 << AllocatorExpr->getType() << D.Allocator->getSourceRange(); 21513 continue; 21514 } 21515 // OpenMP [2.12.5, target Construct] 21516 // Predefined allocators appearing in a uses_allocators clause cannot have 21517 // traits specified. 21518 if (IsPredefinedAllocator && D.AllocatorTraits) { 21519 Diag(D.AllocatorTraits->getExprLoc(), 21520 diag::err_omp_predefined_allocator_with_traits) 21521 << D.AllocatorTraits->getSourceRange(); 21522 Diag(D.Allocator->getExprLoc(), diag::note_omp_predefined_allocator) 21523 << cast<NamedDecl>(DRE->getDecl())->getName() 21524 << D.Allocator->getSourceRange(); 21525 continue; 21526 } 21527 // OpenMP [2.12.5, target Construct] 21528 // Non-predefined allocators appearing in a uses_allocators clause must 21529 // have traits specified. 21530 if (!IsPredefinedAllocator && !D.AllocatorTraits) { 21531 Diag(D.Allocator->getExprLoc(), 21532 diag::err_omp_nonpredefined_allocator_without_traits); 21533 continue; 21534 } 21535 // No allocator traits - just convert it to rvalue. 21536 if (!D.AllocatorTraits) 21537 AllocatorExpr = DefaultLvalueConversion(AllocatorExpr).get(); 21538 DSAStack->addUsesAllocatorsDecl( 21539 DRE->getDecl(), 21540 IsPredefinedAllocator 21541 ? DSAStackTy::UsesAllocatorsDeclKind::PredefinedAllocator 21542 : DSAStackTy::UsesAllocatorsDeclKind::UserDefinedAllocator); 21543 } 21544 Expr *AllocatorTraitsExpr = nullptr; 21545 if (D.AllocatorTraits) { 21546 if (D.AllocatorTraits->isTypeDependent()) { 21547 AllocatorTraitsExpr = D.AllocatorTraits; 21548 } else { 21549 // OpenMP [2.12.5, target Construct] 21550 // Arrays that contain allocator traits that appear in a uses_allocators 21551 // clause must be constant arrays, have constant values and be defined 21552 // in the same scope as the construct in which the clause appears. 21553 AllocatorTraitsExpr = D.AllocatorTraits->IgnoreParenImpCasts(); 21554 // Check that traits expr is a constant array. 21555 QualType TraitTy; 21556 if (const ArrayType *Ty = 21557 AllocatorTraitsExpr->getType()->getAsArrayTypeUnsafe()) 21558 if (const auto *ConstArrayTy = dyn_cast<ConstantArrayType>(Ty)) 21559 TraitTy = ConstArrayTy->getElementType(); 21560 if (TraitTy.isNull() || 21561 !(Context.hasSameUnqualifiedType(TraitTy, 21562 DSAStack->getOMPAlloctraitT()) || 21563 Context.typesAreCompatible(TraitTy, DSAStack->getOMPAlloctraitT(), 21564 /*CompareUnqualified=*/true))) { 21565 Diag(D.AllocatorTraits->getExprLoc(), 21566 diag::err_omp_expected_array_alloctraits) 21567 << AllocatorTraitsExpr->getType(); 21568 continue; 21569 } 21570 // Do not map by default allocator traits if it is a standalone 21571 // variable. 21572 if (auto *DRE = dyn_cast<DeclRefExpr>(AllocatorTraitsExpr)) 21573 DSAStack->addUsesAllocatorsDecl( 21574 DRE->getDecl(), 21575 DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait); 21576 } 21577 } 21578 OMPUsesAllocatorsClause::Data &NewD = NewData.emplace_back(); 21579 NewD.Allocator = AllocatorExpr; 21580 NewD.AllocatorTraits = AllocatorTraitsExpr; 21581 NewD.LParenLoc = D.LParenLoc; 21582 NewD.RParenLoc = D.RParenLoc; 21583 } 21584 return OMPUsesAllocatorsClause::Create(Context, StartLoc, LParenLoc, EndLoc, 21585 NewData); 21586 } 21587 21588 OMPClause *Sema::ActOnOpenMPAffinityClause( 21589 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc, 21590 SourceLocation EndLoc, Expr *Modifier, ArrayRef<Expr *> Locators) { 21591 SmallVector<Expr *, 8> Vars; 21592 for (Expr *RefExpr : Locators) { 21593 assert(RefExpr && "NULL expr in OpenMP shared clause."); 21594 if (isa<DependentScopeDeclRefExpr>(RefExpr) || RefExpr->isTypeDependent()) { 21595 // It will be analyzed later. 21596 Vars.push_back(RefExpr); 21597 continue; 21598 } 21599 21600 SourceLocation ELoc = RefExpr->getExprLoc(); 21601 Expr *SimpleExpr = RefExpr->IgnoreParenImpCasts(); 21602 21603 if (!SimpleExpr->isLValue()) { 21604 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 21605 << 1 << 0 << RefExpr->getSourceRange(); 21606 continue; 21607 } 21608 21609 ExprResult Res; 21610 { 21611 Sema::TentativeAnalysisScope Trap(*this); 21612 Res = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, SimpleExpr); 21613 } 21614 if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr) && 21615 !isa<OMPArrayShapingExpr>(SimpleExpr)) { 21616 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 21617 << 1 << 0 << RefExpr->getSourceRange(); 21618 continue; 21619 } 21620 Vars.push_back(SimpleExpr); 21621 } 21622 21623 return OMPAffinityClause::Create(Context, StartLoc, LParenLoc, ColonLoc, 21624 EndLoc, Modifier, Vars); 21625 } 21626 21627 OMPClause *Sema::ActOnOpenMPBindClause(OpenMPBindClauseKind Kind, 21628 SourceLocation KindLoc, 21629 SourceLocation StartLoc, 21630 SourceLocation LParenLoc, 21631 SourceLocation EndLoc) { 21632 if (Kind == OMPC_BIND_unknown) { 21633 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 21634 << getListOfPossibleValues(OMPC_bind, /*First=*/0, 21635 /*Last=*/unsigned(OMPC_BIND_unknown)) 21636 << getOpenMPClauseName(OMPC_bind); 21637 return nullptr; 21638 } 21639 21640 return OMPBindClause::Create(Context, Kind, KindLoc, StartLoc, LParenLoc, 21641 EndLoc); 21642 } 21643