1 //===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 /// \file 9 /// This file implements semantic analysis for OpenMP directives and 10 /// clauses. 11 /// 12 //===----------------------------------------------------------------------===// 13 14 #include "TreeTransform.h" 15 #include "clang/AST/ASTContext.h" 16 #include "clang/AST/ASTMutationListener.h" 17 #include "clang/AST/CXXInheritance.h" 18 #include "clang/AST/Decl.h" 19 #include "clang/AST/DeclCXX.h" 20 #include "clang/AST/DeclOpenMP.h" 21 #include "clang/AST/OpenMPClause.h" 22 #include "clang/AST/StmtCXX.h" 23 #include "clang/AST/StmtOpenMP.h" 24 #include "clang/AST/StmtVisitor.h" 25 #include "clang/AST/TypeOrdering.h" 26 #include "clang/Basic/DiagnosticSema.h" 27 #include "clang/Basic/OpenMPKinds.h" 28 #include "clang/Basic/PartialDiagnostic.h" 29 #include "clang/Basic/TargetInfo.h" 30 #include "clang/Sema/Initialization.h" 31 #include "clang/Sema/Lookup.h" 32 #include "clang/Sema/Scope.h" 33 #include "clang/Sema/ScopeInfo.h" 34 #include "clang/Sema/SemaInternal.h" 35 #include "llvm/ADT/IndexedMap.h" 36 #include "llvm/ADT/PointerEmbeddedInt.h" 37 #include "llvm/ADT/STLExtras.h" 38 #include "llvm/ADT/StringExtras.h" 39 #include "llvm/Frontend/OpenMP/OMPConstants.h" 40 #include <set> 41 42 using namespace clang; 43 using namespace llvm::omp; 44 45 //===----------------------------------------------------------------------===// 46 // Stack of data-sharing attributes for variables 47 //===----------------------------------------------------------------------===// 48 49 static const Expr *checkMapClauseExpressionBase( 50 Sema &SemaRef, Expr *E, 51 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents, 52 OpenMPClauseKind CKind, OpenMPDirectiveKind DKind, bool NoDiagnose); 53 54 namespace { 55 /// Default data sharing attributes, which can be applied to directive. 56 enum DefaultDataSharingAttributes { 57 DSA_unspecified = 0, /// Data sharing attribute not specified. 58 DSA_none = 1 << 0, /// Default data sharing attribute 'none'. 59 DSA_shared = 1 << 1, /// Default data sharing attribute 'shared'. 60 DSA_firstprivate = 1 << 2, /// Default data sharing attribute 'firstprivate'. 61 }; 62 63 /// Stack for tracking declarations used in OpenMP directives and 64 /// clauses and their data-sharing attributes. 65 class DSAStackTy { 66 public: 67 struct DSAVarData { 68 OpenMPDirectiveKind DKind = OMPD_unknown; 69 OpenMPClauseKind CKind = OMPC_unknown; 70 unsigned Modifier = 0; 71 const Expr *RefExpr = nullptr; 72 DeclRefExpr *PrivateCopy = nullptr; 73 SourceLocation ImplicitDSALoc; 74 bool AppliedToPointee = false; 75 DSAVarData() = default; 76 DSAVarData(OpenMPDirectiveKind DKind, OpenMPClauseKind CKind, 77 const Expr *RefExpr, DeclRefExpr *PrivateCopy, 78 SourceLocation ImplicitDSALoc, unsigned Modifier, 79 bool AppliedToPointee) 80 : DKind(DKind), CKind(CKind), Modifier(Modifier), RefExpr(RefExpr), 81 PrivateCopy(PrivateCopy), ImplicitDSALoc(ImplicitDSALoc), 82 AppliedToPointee(AppliedToPointee) {} 83 }; 84 using OperatorOffsetTy = 85 llvm::SmallVector<std::pair<Expr *, OverloadedOperatorKind>, 4>; 86 using DoacrossDependMapTy = 87 llvm::DenseMap<OMPDependClause *, OperatorOffsetTy>; 88 /// Kind of the declaration used in the uses_allocators clauses. 89 enum class UsesAllocatorsDeclKind { 90 /// Predefined allocator 91 PredefinedAllocator, 92 /// User-defined allocator 93 UserDefinedAllocator, 94 /// The declaration that represent allocator trait 95 AllocatorTrait, 96 }; 97 98 private: 99 struct DSAInfo { 100 OpenMPClauseKind Attributes = OMPC_unknown; 101 unsigned Modifier = 0; 102 /// Pointer to a reference expression and a flag which shows that the 103 /// variable is marked as lastprivate(true) or not (false). 104 llvm::PointerIntPair<const Expr *, 1, bool> RefExpr; 105 DeclRefExpr *PrivateCopy = nullptr; 106 /// true if the attribute is applied to the pointee, not the variable 107 /// itself. 108 bool AppliedToPointee = false; 109 }; 110 using DeclSAMapTy = llvm::SmallDenseMap<const ValueDecl *, DSAInfo, 8>; 111 using UsedRefMapTy = llvm::SmallDenseMap<const ValueDecl *, const Expr *, 8>; 112 using LCDeclInfo = std::pair<unsigned, VarDecl *>; 113 using LoopControlVariablesMapTy = 114 llvm::SmallDenseMap<const ValueDecl *, LCDeclInfo, 8>; 115 /// Struct that associates a component with the clause kind where they are 116 /// found. 117 struct MappedExprComponentTy { 118 OMPClauseMappableExprCommon::MappableExprComponentLists Components; 119 OpenMPClauseKind Kind = OMPC_unknown; 120 }; 121 using MappedExprComponentsTy = 122 llvm::DenseMap<const ValueDecl *, MappedExprComponentTy>; 123 using CriticalsWithHintsTy = 124 llvm::StringMap<std::pair<const OMPCriticalDirective *, llvm::APSInt>>; 125 struct ReductionData { 126 using BOKPtrType = llvm::PointerEmbeddedInt<BinaryOperatorKind, 16>; 127 SourceRange ReductionRange; 128 llvm::PointerUnion<const Expr *, BOKPtrType> ReductionOp; 129 ReductionData() = default; 130 void set(BinaryOperatorKind BO, SourceRange RR) { 131 ReductionRange = RR; 132 ReductionOp = BO; 133 } 134 void set(const Expr *RefExpr, SourceRange RR) { 135 ReductionRange = RR; 136 ReductionOp = RefExpr; 137 } 138 }; 139 using DeclReductionMapTy = 140 llvm::SmallDenseMap<const ValueDecl *, ReductionData, 4>; 141 struct DefaultmapInfo { 142 OpenMPDefaultmapClauseModifier ImplicitBehavior = 143 OMPC_DEFAULTMAP_MODIFIER_unknown; 144 SourceLocation SLoc; 145 DefaultmapInfo() = default; 146 DefaultmapInfo(OpenMPDefaultmapClauseModifier M, SourceLocation Loc) 147 : ImplicitBehavior(M), SLoc(Loc) {} 148 }; 149 150 struct SharingMapTy { 151 DeclSAMapTy SharingMap; 152 DeclReductionMapTy ReductionMap; 153 UsedRefMapTy AlignedMap; 154 UsedRefMapTy NontemporalMap; 155 MappedExprComponentsTy MappedExprComponents; 156 LoopControlVariablesMapTy LCVMap; 157 DefaultDataSharingAttributes DefaultAttr = DSA_unspecified; 158 SourceLocation DefaultAttrLoc; 159 DefaultmapInfo DefaultmapMap[OMPC_DEFAULTMAP_unknown]; 160 OpenMPDirectiveKind Directive = OMPD_unknown; 161 DeclarationNameInfo DirectiveName; 162 Scope *CurScope = nullptr; 163 DeclContext *Context = nullptr; 164 SourceLocation ConstructLoc; 165 /// Set of 'depend' clauses with 'sink|source' dependence kind. Required to 166 /// get the data (loop counters etc.) about enclosing loop-based construct. 167 /// This data is required during codegen. 168 DoacrossDependMapTy DoacrossDepends; 169 /// First argument (Expr *) contains optional argument of the 170 /// 'ordered' clause, the second one is true if the regions has 'ordered' 171 /// clause, false otherwise. 172 llvm::Optional<std::pair<const Expr *, OMPOrderedClause *>> OrderedRegion; 173 unsigned AssociatedLoops = 1; 174 bool HasMutipleLoops = false; 175 const Decl *PossiblyLoopCounter = nullptr; 176 bool NowaitRegion = false; 177 bool CancelRegion = false; 178 bool LoopStart = false; 179 bool BodyComplete = false; 180 SourceLocation PrevScanLocation; 181 SourceLocation PrevOrderedLocation; 182 SourceLocation InnerTeamsRegionLoc; 183 /// Reference to the taskgroup task_reduction reference expression. 184 Expr *TaskgroupReductionRef = nullptr; 185 llvm::DenseSet<QualType> MappedClassesQualTypes; 186 SmallVector<Expr *, 4> InnerUsedAllocators; 187 llvm::DenseSet<CanonicalDeclPtr<Decl>> ImplicitTaskFirstprivates; 188 /// List of globals marked as declare target link in this target region 189 /// (isOpenMPTargetExecutionDirective(Directive) == true). 190 llvm::SmallVector<DeclRefExpr *, 4> DeclareTargetLinkVarDecls; 191 /// List of decls used in inclusive/exclusive clauses of the scan directive. 192 llvm::DenseSet<CanonicalDeclPtr<Decl>> UsedInScanDirective; 193 llvm::DenseMap<CanonicalDeclPtr<const Decl>, UsesAllocatorsDeclKind> 194 UsesAllocatorsDecls; 195 Expr *DeclareMapperVar = nullptr; 196 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name, 197 Scope *CurScope, SourceLocation Loc) 198 : Directive(DKind), DirectiveName(Name), CurScope(CurScope), 199 ConstructLoc(Loc) {} 200 SharingMapTy() = default; 201 }; 202 203 using StackTy = SmallVector<SharingMapTy, 4>; 204 205 /// Stack of used declaration and their data-sharing attributes. 206 DeclSAMapTy Threadprivates; 207 const FunctionScopeInfo *CurrentNonCapturingFunctionScope = nullptr; 208 SmallVector<std::pair<StackTy, const FunctionScopeInfo *>, 4> Stack; 209 /// true, if check for DSA must be from parent directive, false, if 210 /// from current directive. 211 OpenMPClauseKind ClauseKindMode = OMPC_unknown; 212 Sema &SemaRef; 213 bool ForceCapturing = false; 214 /// true if all the variables in the target executable directives must be 215 /// captured by reference. 216 bool ForceCaptureByReferenceInTargetExecutable = false; 217 CriticalsWithHintsTy Criticals; 218 unsigned IgnoredStackElements = 0; 219 220 /// Iterators over the stack iterate in order from innermost to outermost 221 /// directive. 222 using const_iterator = StackTy::const_reverse_iterator; 223 const_iterator begin() const { 224 return Stack.empty() ? const_iterator() 225 : Stack.back().first.rbegin() + IgnoredStackElements; 226 } 227 const_iterator end() const { 228 return Stack.empty() ? const_iterator() : Stack.back().first.rend(); 229 } 230 using iterator = StackTy::reverse_iterator; 231 iterator begin() { 232 return Stack.empty() ? iterator() 233 : Stack.back().first.rbegin() + IgnoredStackElements; 234 } 235 iterator end() { 236 return Stack.empty() ? iterator() : Stack.back().first.rend(); 237 } 238 239 // Convenience operations to get at the elements of the stack. 240 241 bool isStackEmpty() const { 242 return Stack.empty() || 243 Stack.back().second != CurrentNonCapturingFunctionScope || 244 Stack.back().first.size() <= IgnoredStackElements; 245 } 246 size_t getStackSize() const { 247 return isStackEmpty() ? 0 248 : Stack.back().first.size() - IgnoredStackElements; 249 } 250 251 SharingMapTy *getTopOfStackOrNull() { 252 size_t Size = getStackSize(); 253 if (Size == 0) 254 return nullptr; 255 return &Stack.back().first[Size - 1]; 256 } 257 const SharingMapTy *getTopOfStackOrNull() const { 258 return const_cast<DSAStackTy&>(*this).getTopOfStackOrNull(); 259 } 260 SharingMapTy &getTopOfStack() { 261 assert(!isStackEmpty() && "no current directive"); 262 return *getTopOfStackOrNull(); 263 } 264 const SharingMapTy &getTopOfStack() const { 265 return const_cast<DSAStackTy&>(*this).getTopOfStack(); 266 } 267 268 SharingMapTy *getSecondOnStackOrNull() { 269 size_t Size = getStackSize(); 270 if (Size <= 1) 271 return nullptr; 272 return &Stack.back().first[Size - 2]; 273 } 274 const SharingMapTy *getSecondOnStackOrNull() const { 275 return const_cast<DSAStackTy&>(*this).getSecondOnStackOrNull(); 276 } 277 278 /// Get the stack element at a certain level (previously returned by 279 /// \c getNestingLevel). 280 /// 281 /// Note that nesting levels count from outermost to innermost, and this is 282 /// the reverse of our iteration order where new inner levels are pushed at 283 /// the front of the stack. 284 SharingMapTy &getStackElemAtLevel(unsigned Level) { 285 assert(Level < getStackSize() && "no such stack element"); 286 return Stack.back().first[Level]; 287 } 288 const SharingMapTy &getStackElemAtLevel(unsigned Level) const { 289 return const_cast<DSAStackTy&>(*this).getStackElemAtLevel(Level); 290 } 291 292 DSAVarData getDSA(const_iterator &Iter, ValueDecl *D) const; 293 294 /// Checks if the variable is a local for OpenMP region. 295 bool isOpenMPLocal(VarDecl *D, const_iterator Iter) const; 296 297 /// Vector of previously declared requires directives 298 SmallVector<const OMPRequiresDecl *, 2> RequiresDecls; 299 /// omp_allocator_handle_t type. 300 QualType OMPAllocatorHandleT; 301 /// omp_depend_t type. 302 QualType OMPDependT; 303 /// omp_event_handle_t type. 304 QualType OMPEventHandleT; 305 /// omp_alloctrait_t type. 306 QualType OMPAlloctraitT; 307 /// Expression for the predefined allocators. 308 Expr *OMPPredefinedAllocators[OMPAllocateDeclAttr::OMPUserDefinedMemAlloc] = { 309 nullptr}; 310 /// Vector of previously encountered target directives 311 SmallVector<SourceLocation, 2> TargetLocations; 312 SourceLocation AtomicLocation; 313 /// Vector of declare variant construct traits. 314 SmallVector<llvm::omp::TraitProperty, 8> ConstructTraits; 315 316 public: 317 explicit DSAStackTy(Sema &S) : SemaRef(S) {} 318 319 /// Sets omp_allocator_handle_t type. 320 void setOMPAllocatorHandleT(QualType Ty) { OMPAllocatorHandleT = Ty; } 321 /// Gets omp_allocator_handle_t type. 322 QualType getOMPAllocatorHandleT() const { return OMPAllocatorHandleT; } 323 /// Sets omp_alloctrait_t type. 324 void setOMPAlloctraitT(QualType Ty) { OMPAlloctraitT = Ty; } 325 /// Gets omp_alloctrait_t type. 326 QualType getOMPAlloctraitT() const { return OMPAlloctraitT; } 327 /// Sets the given default allocator. 328 void setAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, 329 Expr *Allocator) { 330 OMPPredefinedAllocators[AllocatorKind] = Allocator; 331 } 332 /// Returns the specified default allocator. 333 Expr *getAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind) const { 334 return OMPPredefinedAllocators[AllocatorKind]; 335 } 336 /// Sets omp_depend_t type. 337 void setOMPDependT(QualType Ty) { OMPDependT = Ty; } 338 /// Gets omp_depend_t type. 339 QualType getOMPDependT() const { return OMPDependT; } 340 341 /// Sets omp_event_handle_t type. 342 void setOMPEventHandleT(QualType Ty) { OMPEventHandleT = Ty; } 343 /// Gets omp_event_handle_t type. 344 QualType getOMPEventHandleT() const { return OMPEventHandleT; } 345 346 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; } 347 OpenMPClauseKind getClauseParsingMode() const { 348 assert(isClauseParsingMode() && "Must be in clause parsing mode."); 349 return ClauseKindMode; 350 } 351 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; } 352 353 bool isBodyComplete() const { 354 const SharingMapTy *Top = getTopOfStackOrNull(); 355 return Top && Top->BodyComplete; 356 } 357 void setBodyComplete() { 358 getTopOfStack().BodyComplete = true; 359 } 360 361 bool isForceVarCapturing() const { return ForceCapturing; } 362 void setForceVarCapturing(bool V) { ForceCapturing = V; } 363 364 void setForceCaptureByReferenceInTargetExecutable(bool V) { 365 ForceCaptureByReferenceInTargetExecutable = V; 366 } 367 bool isForceCaptureByReferenceInTargetExecutable() const { 368 return ForceCaptureByReferenceInTargetExecutable; 369 } 370 371 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName, 372 Scope *CurScope, SourceLocation Loc) { 373 assert(!IgnoredStackElements && 374 "cannot change stack while ignoring elements"); 375 if (Stack.empty() || 376 Stack.back().second != CurrentNonCapturingFunctionScope) 377 Stack.emplace_back(StackTy(), CurrentNonCapturingFunctionScope); 378 Stack.back().first.emplace_back(DKind, DirName, CurScope, Loc); 379 Stack.back().first.back().DefaultAttrLoc = Loc; 380 } 381 382 void pop() { 383 assert(!IgnoredStackElements && 384 "cannot change stack while ignoring elements"); 385 assert(!Stack.back().first.empty() && 386 "Data-sharing attributes stack is empty!"); 387 Stack.back().first.pop_back(); 388 } 389 390 /// RAII object to temporarily leave the scope of a directive when we want to 391 /// logically operate in its parent. 392 class ParentDirectiveScope { 393 DSAStackTy &Self; 394 bool Active; 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 = 437 D ? D->getCanonicalDecl() : D; 438 } 439 /// Gets the possible loop counter decl. 440 const Decl *getPossiblyLoopCunter() const { 441 return getTopOfStack().PossiblyLoopCounter; 442 } 443 /// Start new OpenMP region stack in new non-capturing function. 444 void pushFunction() { 445 assert(!IgnoredStackElements && 446 "cannot change stack while ignoring elements"); 447 const FunctionScopeInfo *CurFnScope = SemaRef.getCurFunction(); 448 assert(!isa<CapturingScopeInfo>(CurFnScope)); 449 CurrentNonCapturingFunctionScope = CurFnScope; 450 } 451 /// Pop region stack for non-capturing function. 452 void popFunction(const FunctionScopeInfo *OldFSI) { 453 assert(!IgnoredStackElements && 454 "cannot change stack while ignoring elements"); 455 if (!Stack.empty() && Stack.back().second == OldFSI) { 456 assert(Stack.back().first.empty()); 457 Stack.pop_back(); 458 } 459 CurrentNonCapturingFunctionScope = nullptr; 460 for (const FunctionScopeInfo *FSI : llvm::reverse(SemaRef.FunctionScopes)) { 461 if (!isa<CapturingScopeInfo>(FSI)) { 462 CurrentNonCapturingFunctionScope = FSI; 463 break; 464 } 465 } 466 } 467 468 void addCriticalWithHint(const OMPCriticalDirective *D, llvm::APSInt Hint) { 469 Criticals.try_emplace(D->getDirectiveName().getAsString(), D, Hint); 470 } 471 const std::pair<const OMPCriticalDirective *, llvm::APSInt> 472 getCriticalWithHint(const DeclarationNameInfo &Name) const { 473 auto I = Criticals.find(Name.getAsString()); 474 if (I != Criticals.end()) 475 return I->second; 476 return std::make_pair(nullptr, llvm::APSInt()); 477 } 478 /// If 'aligned' declaration for given variable \a D was not seen yet, 479 /// add it and return NULL; otherwise return previous occurrence's expression 480 /// for diagnostics. 481 const Expr *addUniqueAligned(const ValueDecl *D, const Expr *NewDE); 482 /// If 'nontemporal' declaration for given variable \a D was not seen yet, 483 /// add it and return NULL; otherwise return previous occurrence's expression 484 /// for diagnostics. 485 const Expr *addUniqueNontemporal(const ValueDecl *D, const Expr *NewDE); 486 487 /// Register specified variable as loop control variable. 488 void addLoopControlVariable(const ValueDecl *D, VarDecl *Capture); 489 /// Check if the specified variable is a loop control variable for 490 /// current region. 491 /// \return The index of the loop control variable in the list of associated 492 /// for-loops (from outer to inner). 493 const LCDeclInfo isLoopControlVariable(const ValueDecl *D) const; 494 /// Check if the specified variable is a loop control variable for 495 /// parent region. 496 /// \return The index of the loop control variable in the list of associated 497 /// for-loops (from outer to inner). 498 const LCDeclInfo isParentLoopControlVariable(const ValueDecl *D) const; 499 /// Check if the specified variable is a loop control variable for 500 /// current region. 501 /// \return The index of the loop control variable in the list of associated 502 /// for-loops (from outer to inner). 503 const LCDeclInfo isLoopControlVariable(const ValueDecl *D, 504 unsigned Level) const; 505 /// Get the loop control variable for the I-th loop (or nullptr) in 506 /// parent directive. 507 const ValueDecl *getParentLoopControlVariable(unsigned I) const; 508 509 /// Marks the specified decl \p D as used in scan directive. 510 void markDeclAsUsedInScanDirective(ValueDecl *D) { 511 if (SharingMapTy *Stack = getSecondOnStackOrNull()) 512 Stack->UsedInScanDirective.insert(D); 513 } 514 515 /// Checks if the specified declaration was used in the inner scan directive. 516 bool isUsedInScanDirective(ValueDecl *D) const { 517 if (const SharingMapTy *Stack = getTopOfStackOrNull()) 518 return Stack->UsedInScanDirective.count(D) > 0; 519 return false; 520 } 521 522 /// Adds explicit data sharing attribute to the specified declaration. 523 void addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A, 524 DeclRefExpr *PrivateCopy = nullptr, unsigned Modifier = 0, 525 bool AppliedToPointee = false); 526 527 /// Adds additional information for the reduction items with the reduction id 528 /// represented as an operator. 529 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 530 BinaryOperatorKind BOK); 531 /// Adds additional information for the reduction items with the reduction id 532 /// represented as reduction identifier. 533 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 534 const Expr *ReductionRef); 535 /// Returns the location and reduction operation from the innermost parent 536 /// region for the given \p D. 537 const DSAVarData 538 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR, 539 BinaryOperatorKind &BOK, 540 Expr *&TaskgroupDescriptor) const; 541 /// Returns the location and reduction operation from the innermost parent 542 /// region for the given \p D. 543 const DSAVarData 544 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR, 545 const Expr *&ReductionRef, 546 Expr *&TaskgroupDescriptor) const; 547 /// Return reduction reference expression for the current taskgroup or 548 /// parallel/worksharing directives with task reductions. 549 Expr *getTaskgroupReductionRef() const { 550 assert((getTopOfStack().Directive == OMPD_taskgroup || 551 ((isOpenMPParallelDirective(getTopOfStack().Directive) || 552 isOpenMPWorksharingDirective(getTopOfStack().Directive)) && 553 !isOpenMPSimdDirective(getTopOfStack().Directive))) && 554 "taskgroup reference expression requested for non taskgroup or " 555 "parallel/worksharing directive."); 556 return getTopOfStack().TaskgroupReductionRef; 557 } 558 /// Checks if the given \p VD declaration is actually a taskgroup reduction 559 /// descriptor variable at the \p Level of OpenMP regions. 560 bool isTaskgroupReductionRef(const ValueDecl *VD, unsigned Level) const { 561 return getStackElemAtLevel(Level).TaskgroupReductionRef && 562 cast<DeclRefExpr>(getStackElemAtLevel(Level).TaskgroupReductionRef) 563 ->getDecl() == VD; 564 } 565 566 /// Returns data sharing attributes from top of the stack for the 567 /// specified declaration. 568 const DSAVarData getTopDSA(ValueDecl *D, bool FromParent); 569 /// Returns data-sharing attributes for the specified declaration. 570 const DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent) const; 571 /// Returns data-sharing attributes for the specified declaration. 572 const DSAVarData getImplicitDSA(ValueDecl *D, unsigned Level) const; 573 /// Checks if the specified variables has data-sharing attributes which 574 /// match specified \a CPred predicate in any directive which matches \a DPred 575 /// predicate. 576 const DSAVarData 577 hasDSA(ValueDecl *D, 578 const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred, 579 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 580 bool FromParent) const; 581 /// Checks if the specified variables has data-sharing attributes which 582 /// match specified \a CPred predicate in any innermost directive which 583 /// matches \a DPred predicate. 584 const DSAVarData 585 hasInnermostDSA(ValueDecl *D, 586 const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred, 587 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 588 bool FromParent) const; 589 /// Checks if the specified variables has explicit data-sharing 590 /// attributes which match specified \a CPred predicate at the specified 591 /// OpenMP region. 592 bool 593 hasExplicitDSA(const ValueDecl *D, 594 const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred, 595 unsigned Level, bool NotLastprivate = false) const; 596 597 /// Returns true if the directive at level \Level matches in the 598 /// specified \a DPred predicate. 599 bool hasExplicitDirective( 600 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 601 unsigned Level) const; 602 603 /// Finds a directive which matches specified \a DPred predicate. 604 bool hasDirective( 605 const llvm::function_ref<bool( 606 OpenMPDirectiveKind, const DeclarationNameInfo &, SourceLocation)> 607 DPred, 608 bool FromParent) const; 609 610 /// Returns currently analyzed directive. 611 OpenMPDirectiveKind getCurrentDirective() const { 612 const SharingMapTy *Top = getTopOfStackOrNull(); 613 return Top ? Top->Directive : OMPD_unknown; 614 } 615 /// Returns directive kind at specified level. 616 OpenMPDirectiveKind getDirective(unsigned Level) const { 617 assert(!isStackEmpty() && "No directive at specified level."); 618 return getStackElemAtLevel(Level).Directive; 619 } 620 /// Returns the capture region at the specified level. 621 OpenMPDirectiveKind getCaptureRegion(unsigned Level, 622 unsigned OpenMPCaptureLevel) const { 623 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 624 getOpenMPCaptureRegions(CaptureRegions, getDirective(Level)); 625 return CaptureRegions[OpenMPCaptureLevel]; 626 } 627 /// Returns parent directive. 628 OpenMPDirectiveKind getParentDirective() const { 629 const SharingMapTy *Parent = getSecondOnStackOrNull(); 630 return Parent ? Parent->Directive : OMPD_unknown; 631 } 632 633 /// Add requires decl to internal vector 634 void addRequiresDecl(OMPRequiresDecl *RD) { 635 RequiresDecls.push_back(RD); 636 } 637 638 /// Checks if the defined 'requires' directive has specified type of clause. 639 template <typename ClauseType> 640 bool hasRequiresDeclWithClause() const { 641 return llvm::any_of(RequiresDecls, [](const OMPRequiresDecl *D) { 642 return llvm::any_of(D->clauselists(), [](const OMPClause *C) { 643 return isa<ClauseType>(C); 644 }); 645 }); 646 } 647 648 /// Checks for a duplicate clause amongst previously declared requires 649 /// directives 650 bool hasDuplicateRequiresClause(ArrayRef<OMPClause *> ClauseList) const { 651 bool IsDuplicate = false; 652 for (OMPClause *CNew : ClauseList) { 653 for (const OMPRequiresDecl *D : RequiresDecls) { 654 for (const OMPClause *CPrev : D->clauselists()) { 655 if (CNew->getClauseKind() == CPrev->getClauseKind()) { 656 SemaRef.Diag(CNew->getBeginLoc(), 657 diag::err_omp_requires_clause_redeclaration) 658 << getOpenMPClauseName(CNew->getClauseKind()); 659 SemaRef.Diag(CPrev->getBeginLoc(), 660 diag::note_omp_requires_previous_clause) 661 << getOpenMPClauseName(CPrev->getClauseKind()); 662 IsDuplicate = true; 663 } 664 } 665 } 666 } 667 return IsDuplicate; 668 } 669 670 /// Add location of previously encountered target to internal vector 671 void addTargetDirLocation(SourceLocation LocStart) { 672 TargetLocations.push_back(LocStart); 673 } 674 675 /// Add location for the first encountered atomicc directive. 676 void addAtomicDirectiveLoc(SourceLocation Loc) { 677 if (AtomicLocation.isInvalid()) 678 AtomicLocation = Loc; 679 } 680 681 /// Returns the location of the first encountered atomic directive in the 682 /// module. 683 SourceLocation getAtomicDirectiveLoc() const { 684 return AtomicLocation; 685 } 686 687 // Return previously encountered target region locations. 688 ArrayRef<SourceLocation> getEncounteredTargetLocs() const { 689 return TargetLocations; 690 } 691 692 /// Set default data sharing attribute to none. 693 void setDefaultDSANone(SourceLocation Loc) { 694 getTopOfStack().DefaultAttr = DSA_none; 695 getTopOfStack().DefaultAttrLoc = Loc; 696 } 697 /// Set default data sharing attribute to shared. 698 void setDefaultDSAShared(SourceLocation Loc) { 699 getTopOfStack().DefaultAttr = DSA_shared; 700 getTopOfStack().DefaultAttrLoc = Loc; 701 } 702 /// Set default data sharing attribute to firstprivate. 703 void setDefaultDSAFirstPrivate(SourceLocation Loc) { 704 getTopOfStack().DefaultAttr = DSA_firstprivate; 705 getTopOfStack().DefaultAttrLoc = Loc; 706 } 707 /// Set default data mapping attribute to Modifier:Kind 708 void setDefaultDMAAttr(OpenMPDefaultmapClauseModifier M, 709 OpenMPDefaultmapClauseKind Kind, 710 SourceLocation Loc) { 711 DefaultmapInfo &DMI = getTopOfStack().DefaultmapMap[Kind]; 712 DMI.ImplicitBehavior = M; 713 DMI.SLoc = Loc; 714 } 715 /// Check whether the implicit-behavior has been set in defaultmap 716 bool checkDefaultmapCategory(OpenMPDefaultmapClauseKind VariableCategory) { 717 if (VariableCategory == OMPC_DEFAULTMAP_unknown) 718 return getTopOfStack() 719 .DefaultmapMap[OMPC_DEFAULTMAP_aggregate] 720 .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown || 721 getTopOfStack() 722 .DefaultmapMap[OMPC_DEFAULTMAP_scalar] 723 .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown || 724 getTopOfStack() 725 .DefaultmapMap[OMPC_DEFAULTMAP_pointer] 726 .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown; 727 return getTopOfStack().DefaultmapMap[VariableCategory].ImplicitBehavior != 728 OMPC_DEFAULTMAP_MODIFIER_unknown; 729 } 730 731 ArrayRef<llvm::omp::TraitProperty> getConstructTraits() { 732 return ConstructTraits; 733 } 734 void handleConstructTrait(ArrayRef<llvm::omp::TraitProperty> Traits, 735 bool ScopeEntry) { 736 if (ScopeEntry) 737 ConstructTraits.append(Traits.begin(), Traits.end()); 738 else 739 for (llvm::omp::TraitProperty Trait : llvm::reverse(Traits)) { 740 llvm::omp::TraitProperty Top = ConstructTraits.pop_back_val(); 741 assert(Top == Trait && "Something left a trait on the stack!"); 742 (void)Trait; 743 (void)Top; 744 } 745 } 746 747 DefaultDataSharingAttributes getDefaultDSA(unsigned Level) const { 748 return getStackSize() <= Level ? DSA_unspecified 749 : getStackElemAtLevel(Level).DefaultAttr; 750 } 751 DefaultDataSharingAttributes getDefaultDSA() const { 752 return isStackEmpty() ? DSA_unspecified 753 : getTopOfStack().DefaultAttr; 754 } 755 SourceLocation getDefaultDSALocation() const { 756 return isStackEmpty() ? SourceLocation() 757 : getTopOfStack().DefaultAttrLoc; 758 } 759 OpenMPDefaultmapClauseModifier 760 getDefaultmapModifier(OpenMPDefaultmapClauseKind Kind) const { 761 return isStackEmpty() 762 ? OMPC_DEFAULTMAP_MODIFIER_unknown 763 : getTopOfStack().DefaultmapMap[Kind].ImplicitBehavior; 764 } 765 OpenMPDefaultmapClauseModifier 766 getDefaultmapModifierAtLevel(unsigned Level, 767 OpenMPDefaultmapClauseKind Kind) const { 768 return getStackElemAtLevel(Level).DefaultmapMap[Kind].ImplicitBehavior; 769 } 770 bool isDefaultmapCapturedByRef(unsigned Level, 771 OpenMPDefaultmapClauseKind Kind) const { 772 OpenMPDefaultmapClauseModifier M = 773 getDefaultmapModifierAtLevel(Level, Kind); 774 if (Kind == OMPC_DEFAULTMAP_scalar || Kind == OMPC_DEFAULTMAP_pointer) { 775 return (M == OMPC_DEFAULTMAP_MODIFIER_alloc) || 776 (M == OMPC_DEFAULTMAP_MODIFIER_to) || 777 (M == OMPC_DEFAULTMAP_MODIFIER_from) || 778 (M == OMPC_DEFAULTMAP_MODIFIER_tofrom); 779 } 780 return true; 781 } 782 static bool mustBeFirstprivateBase(OpenMPDefaultmapClauseModifier M, 783 OpenMPDefaultmapClauseKind Kind) { 784 switch (Kind) { 785 case OMPC_DEFAULTMAP_scalar: 786 case OMPC_DEFAULTMAP_pointer: 787 return (M == OMPC_DEFAULTMAP_MODIFIER_unknown) || 788 (M == OMPC_DEFAULTMAP_MODIFIER_firstprivate) || 789 (M == OMPC_DEFAULTMAP_MODIFIER_default); 790 case OMPC_DEFAULTMAP_aggregate: 791 return M == OMPC_DEFAULTMAP_MODIFIER_firstprivate; 792 default: 793 break; 794 } 795 llvm_unreachable("Unexpected OpenMPDefaultmapClauseKind enum"); 796 } 797 bool mustBeFirstprivateAtLevel(unsigned Level, 798 OpenMPDefaultmapClauseKind Kind) const { 799 OpenMPDefaultmapClauseModifier M = 800 getDefaultmapModifierAtLevel(Level, Kind); 801 return mustBeFirstprivateBase(M, Kind); 802 } 803 bool mustBeFirstprivate(OpenMPDefaultmapClauseKind Kind) const { 804 OpenMPDefaultmapClauseModifier M = getDefaultmapModifier(Kind); 805 return mustBeFirstprivateBase(M, Kind); 806 } 807 808 /// Checks if the specified variable is a threadprivate. 809 bool isThreadPrivate(VarDecl *D) { 810 const DSAVarData DVar = getTopDSA(D, false); 811 return isOpenMPThreadPrivate(DVar.CKind); 812 } 813 814 /// Marks current region as ordered (it has an 'ordered' clause). 815 void setOrderedRegion(bool IsOrdered, const Expr *Param, 816 OMPOrderedClause *Clause) { 817 if (IsOrdered) 818 getTopOfStack().OrderedRegion.emplace(Param, Clause); 819 else 820 getTopOfStack().OrderedRegion.reset(); 821 } 822 /// Returns true, if region is ordered (has associated 'ordered' clause), 823 /// false - otherwise. 824 bool isOrderedRegion() const { 825 if (const SharingMapTy *Top = getTopOfStackOrNull()) 826 return Top->OrderedRegion.hasValue(); 827 return false; 828 } 829 /// Returns optional parameter for the ordered region. 830 std::pair<const Expr *, OMPOrderedClause *> getOrderedRegionParam() const { 831 if (const SharingMapTy *Top = getTopOfStackOrNull()) 832 if (Top->OrderedRegion.hasValue()) 833 return Top->OrderedRegion.getValue(); 834 return std::make_pair(nullptr, nullptr); 835 } 836 /// Returns true, if parent region is ordered (has associated 837 /// 'ordered' clause), false - otherwise. 838 bool isParentOrderedRegion() const { 839 if (const SharingMapTy *Parent = getSecondOnStackOrNull()) 840 return Parent->OrderedRegion.hasValue(); 841 return false; 842 } 843 /// Returns optional parameter for the ordered region. 844 std::pair<const Expr *, OMPOrderedClause *> 845 getParentOrderedRegionParam() const { 846 if (const SharingMapTy *Parent = getSecondOnStackOrNull()) 847 if (Parent->OrderedRegion.hasValue()) 848 return Parent->OrderedRegion.getValue(); 849 return std::make_pair(nullptr, nullptr); 850 } 851 /// Marks current region as nowait (it has a 'nowait' clause). 852 void setNowaitRegion(bool IsNowait = true) { 853 getTopOfStack().NowaitRegion = IsNowait; 854 } 855 /// Returns true, if parent region is nowait (has associated 856 /// 'nowait' clause), false - otherwise. 857 bool isParentNowaitRegion() const { 858 if (const SharingMapTy *Parent = getSecondOnStackOrNull()) 859 return Parent->NowaitRegion; 860 return false; 861 } 862 /// Marks parent region as cancel region. 863 void setParentCancelRegion(bool Cancel = true) { 864 if (SharingMapTy *Parent = getSecondOnStackOrNull()) 865 Parent->CancelRegion |= Cancel; 866 } 867 /// Return true if current region has inner cancel construct. 868 bool isCancelRegion() const { 869 const SharingMapTy *Top = getTopOfStackOrNull(); 870 return Top ? Top->CancelRegion : false; 871 } 872 873 /// Mark that parent region already has scan directive. 874 void setParentHasScanDirective(SourceLocation Loc) { 875 if (SharingMapTy *Parent = getSecondOnStackOrNull()) 876 Parent->PrevScanLocation = Loc; 877 } 878 /// Return true if current region has inner cancel construct. 879 bool doesParentHasScanDirective() const { 880 const SharingMapTy *Top = getSecondOnStackOrNull(); 881 return Top ? Top->PrevScanLocation.isValid() : false; 882 } 883 /// Return true if current region has inner cancel construct. 884 SourceLocation getParentScanDirectiveLoc() const { 885 const SharingMapTy *Top = getSecondOnStackOrNull(); 886 return Top ? Top->PrevScanLocation : SourceLocation(); 887 } 888 /// Mark that parent region already has ordered directive. 889 void setParentHasOrderedDirective(SourceLocation Loc) { 890 if (SharingMapTy *Parent = getSecondOnStackOrNull()) 891 Parent->PrevOrderedLocation = Loc; 892 } 893 /// Return true if current region has inner ordered construct. 894 bool doesParentHasOrderedDirective() const { 895 const SharingMapTy *Top = getSecondOnStackOrNull(); 896 return Top ? Top->PrevOrderedLocation.isValid() : false; 897 } 898 /// Returns the location of the previously specified ordered directive. 899 SourceLocation getParentOrderedDirectiveLoc() const { 900 const SharingMapTy *Top = getSecondOnStackOrNull(); 901 return Top ? Top->PrevOrderedLocation : SourceLocation(); 902 } 903 904 /// Set collapse value for the region. 905 void setAssociatedLoops(unsigned Val) { 906 getTopOfStack().AssociatedLoops = Val; 907 if (Val > 1) 908 getTopOfStack().HasMutipleLoops = true; 909 } 910 /// Return collapse value for region. 911 unsigned getAssociatedLoops() const { 912 const SharingMapTy *Top = getTopOfStackOrNull(); 913 return Top ? Top->AssociatedLoops : 0; 914 } 915 /// Returns true if the construct is associated with multiple loops. 916 bool hasMutipleLoops() const { 917 const SharingMapTy *Top = getTopOfStackOrNull(); 918 return Top ? Top->HasMutipleLoops : false; 919 } 920 921 /// Marks current target region as one with closely nested teams 922 /// region. 923 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) { 924 if (SharingMapTy *Parent = getSecondOnStackOrNull()) 925 Parent->InnerTeamsRegionLoc = TeamsRegionLoc; 926 } 927 /// Returns true, if current region has closely nested teams region. 928 bool hasInnerTeamsRegion() const { 929 return getInnerTeamsRegionLoc().isValid(); 930 } 931 /// Returns location of the nested teams region (if any). 932 SourceLocation getInnerTeamsRegionLoc() const { 933 const SharingMapTy *Top = getTopOfStackOrNull(); 934 return Top ? Top->InnerTeamsRegionLoc : SourceLocation(); 935 } 936 937 Scope *getCurScope() const { 938 const SharingMapTy *Top = getTopOfStackOrNull(); 939 return Top ? Top->CurScope : nullptr; 940 } 941 void setContext(DeclContext *DC) { getTopOfStack().Context = DC; } 942 SourceLocation getConstructLoc() const { 943 const SharingMapTy *Top = getTopOfStackOrNull(); 944 return Top ? Top->ConstructLoc : SourceLocation(); 945 } 946 947 /// Do the check specified in \a Check to all component lists and return true 948 /// if any issue is found. 949 bool checkMappableExprComponentListsForDecl( 950 const ValueDecl *VD, bool CurrentRegionOnly, 951 const llvm::function_ref< 952 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef, 953 OpenMPClauseKind)> 954 Check) const { 955 if (isStackEmpty()) 956 return false; 957 auto SI = begin(); 958 auto SE = end(); 959 960 if (SI == SE) 961 return false; 962 963 if (CurrentRegionOnly) 964 SE = std::next(SI); 965 else 966 std::advance(SI, 1); 967 968 for (; SI != SE; ++SI) { 969 auto MI = SI->MappedExprComponents.find(VD); 970 if (MI != SI->MappedExprComponents.end()) 971 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L : 972 MI->second.Components) 973 if (Check(L, MI->second.Kind)) 974 return true; 975 } 976 return false; 977 } 978 979 /// Do the check specified in \a Check to all component lists at a given level 980 /// and return true if any issue is found. 981 bool checkMappableExprComponentListsForDeclAtLevel( 982 const ValueDecl *VD, unsigned Level, 983 const llvm::function_ref< 984 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef, 985 OpenMPClauseKind)> 986 Check) const { 987 if (getStackSize() <= Level) 988 return false; 989 990 const SharingMapTy &StackElem = getStackElemAtLevel(Level); 991 auto MI = StackElem.MappedExprComponents.find(VD); 992 if (MI != StackElem.MappedExprComponents.end()) 993 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L : 994 MI->second.Components) 995 if (Check(L, MI->second.Kind)) 996 return true; 997 return false; 998 } 999 1000 /// Create a new mappable expression component list associated with a given 1001 /// declaration and initialize it with the provided list of components. 1002 void addMappableExpressionComponents( 1003 const ValueDecl *VD, 1004 OMPClauseMappableExprCommon::MappableExprComponentListRef Components, 1005 OpenMPClauseKind WhereFoundClauseKind) { 1006 MappedExprComponentTy &MEC = getTopOfStack().MappedExprComponents[VD]; 1007 // Create new entry and append the new components there. 1008 MEC.Components.resize(MEC.Components.size() + 1); 1009 MEC.Components.back().append(Components.begin(), Components.end()); 1010 MEC.Kind = WhereFoundClauseKind; 1011 } 1012 1013 unsigned getNestingLevel() const { 1014 assert(!isStackEmpty()); 1015 return getStackSize() - 1; 1016 } 1017 void addDoacrossDependClause(OMPDependClause *C, 1018 const OperatorOffsetTy &OpsOffs) { 1019 SharingMapTy *Parent = getSecondOnStackOrNull(); 1020 assert(Parent && isOpenMPWorksharingDirective(Parent->Directive)); 1021 Parent->DoacrossDepends.try_emplace(C, OpsOffs); 1022 } 1023 llvm::iterator_range<DoacrossDependMapTy::const_iterator> 1024 getDoacrossDependClauses() const { 1025 const SharingMapTy &StackElem = getTopOfStack(); 1026 if (isOpenMPWorksharingDirective(StackElem.Directive)) { 1027 const DoacrossDependMapTy &Ref = StackElem.DoacrossDepends; 1028 return llvm::make_range(Ref.begin(), Ref.end()); 1029 } 1030 return llvm::make_range(StackElem.DoacrossDepends.end(), 1031 StackElem.DoacrossDepends.end()); 1032 } 1033 1034 // Store types of classes which have been explicitly mapped 1035 void addMappedClassesQualTypes(QualType QT) { 1036 SharingMapTy &StackElem = getTopOfStack(); 1037 StackElem.MappedClassesQualTypes.insert(QT); 1038 } 1039 1040 // Return set of mapped classes types 1041 bool isClassPreviouslyMapped(QualType QT) const { 1042 const SharingMapTy &StackElem = getTopOfStack(); 1043 return StackElem.MappedClassesQualTypes.count(QT) != 0; 1044 } 1045 1046 /// Adds global declare target to the parent target region. 1047 void addToParentTargetRegionLinkGlobals(DeclRefExpr *E) { 1048 assert(*OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration( 1049 E->getDecl()) == OMPDeclareTargetDeclAttr::MT_Link && 1050 "Expected declare target link global."); 1051 for (auto &Elem : *this) { 1052 if (isOpenMPTargetExecutionDirective(Elem.Directive)) { 1053 Elem.DeclareTargetLinkVarDecls.push_back(E); 1054 return; 1055 } 1056 } 1057 } 1058 1059 /// Returns the list of globals with declare target link if current directive 1060 /// is target. 1061 ArrayRef<DeclRefExpr *> getLinkGlobals() const { 1062 assert(isOpenMPTargetExecutionDirective(getCurrentDirective()) && 1063 "Expected target executable directive."); 1064 return getTopOfStack().DeclareTargetLinkVarDecls; 1065 } 1066 1067 /// Adds list of allocators expressions. 1068 void addInnerAllocatorExpr(Expr *E) { 1069 getTopOfStack().InnerUsedAllocators.push_back(E); 1070 } 1071 /// Return list of used allocators. 1072 ArrayRef<Expr *> getInnerAllocators() const { 1073 return getTopOfStack().InnerUsedAllocators; 1074 } 1075 /// Marks the declaration as implicitly firstprivate nin the task-based 1076 /// regions. 1077 void addImplicitTaskFirstprivate(unsigned Level, Decl *D) { 1078 getStackElemAtLevel(Level).ImplicitTaskFirstprivates.insert(D); 1079 } 1080 /// Checks if the decl is implicitly firstprivate in the task-based region. 1081 bool isImplicitTaskFirstprivate(Decl *D) const { 1082 return getTopOfStack().ImplicitTaskFirstprivates.count(D) > 0; 1083 } 1084 1085 /// Marks decl as used in uses_allocators clause as the allocator. 1086 void addUsesAllocatorsDecl(const Decl *D, UsesAllocatorsDeclKind Kind) { 1087 getTopOfStack().UsesAllocatorsDecls.try_emplace(D, Kind); 1088 } 1089 /// Checks if specified decl is used in uses allocator clause as the 1090 /// allocator. 1091 Optional<UsesAllocatorsDeclKind> isUsesAllocatorsDecl(unsigned Level, 1092 const Decl *D) const { 1093 const SharingMapTy &StackElem = getTopOfStack(); 1094 auto I = StackElem.UsesAllocatorsDecls.find(D); 1095 if (I == StackElem.UsesAllocatorsDecls.end()) 1096 return None; 1097 return I->getSecond(); 1098 } 1099 Optional<UsesAllocatorsDeclKind> isUsesAllocatorsDecl(const Decl *D) const { 1100 const SharingMapTy &StackElem = getTopOfStack(); 1101 auto I = StackElem.UsesAllocatorsDecls.find(D); 1102 if (I == StackElem.UsesAllocatorsDecls.end()) 1103 return None; 1104 return I->getSecond(); 1105 } 1106 1107 void addDeclareMapperVarRef(Expr *Ref) { 1108 SharingMapTy &StackElem = getTopOfStack(); 1109 StackElem.DeclareMapperVar = Ref; 1110 } 1111 const Expr *getDeclareMapperVarRef() const { 1112 const SharingMapTy *Top = getTopOfStackOrNull(); 1113 return Top ? Top->DeclareMapperVar : nullptr; 1114 } 1115 }; 1116 1117 bool isImplicitTaskingRegion(OpenMPDirectiveKind DKind) { 1118 return isOpenMPParallelDirective(DKind) || isOpenMPTeamsDirective(DKind); 1119 } 1120 1121 bool isImplicitOrExplicitTaskingRegion(OpenMPDirectiveKind DKind) { 1122 return isImplicitTaskingRegion(DKind) || isOpenMPTaskingDirective(DKind) || 1123 DKind == OMPD_unknown; 1124 } 1125 1126 } // namespace 1127 1128 static const Expr *getExprAsWritten(const Expr *E) { 1129 if (const auto *FE = dyn_cast<FullExpr>(E)) 1130 E = FE->getSubExpr(); 1131 1132 if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E)) 1133 E = MTE->getSubExpr(); 1134 1135 while (const auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E)) 1136 E = Binder->getSubExpr(); 1137 1138 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E)) 1139 E = ICE->getSubExprAsWritten(); 1140 return E->IgnoreParens(); 1141 } 1142 1143 static Expr *getExprAsWritten(Expr *E) { 1144 return const_cast<Expr *>(getExprAsWritten(const_cast<const Expr *>(E))); 1145 } 1146 1147 static const ValueDecl *getCanonicalDecl(const ValueDecl *D) { 1148 if (const auto *CED = dyn_cast<OMPCapturedExprDecl>(D)) 1149 if (const auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 1150 D = ME->getMemberDecl(); 1151 const auto *VD = dyn_cast<VarDecl>(D); 1152 const auto *FD = dyn_cast<FieldDecl>(D); 1153 if (VD != nullptr) { 1154 VD = VD->getCanonicalDecl(); 1155 D = VD; 1156 } else { 1157 assert(FD); 1158 FD = FD->getCanonicalDecl(); 1159 D = FD; 1160 } 1161 return D; 1162 } 1163 1164 static ValueDecl *getCanonicalDecl(ValueDecl *D) { 1165 return const_cast<ValueDecl *>( 1166 getCanonicalDecl(const_cast<const ValueDecl *>(D))); 1167 } 1168 1169 DSAStackTy::DSAVarData DSAStackTy::getDSA(const_iterator &Iter, 1170 ValueDecl *D) const { 1171 D = getCanonicalDecl(D); 1172 auto *VD = dyn_cast<VarDecl>(D); 1173 const auto *FD = dyn_cast<FieldDecl>(D); 1174 DSAVarData DVar; 1175 if (Iter == end()) { 1176 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1177 // in a region but not in construct] 1178 // File-scope or namespace-scope variables referenced in called routines 1179 // in the region are shared unless they appear in a threadprivate 1180 // directive. 1181 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(VD)) 1182 DVar.CKind = OMPC_shared; 1183 1184 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced 1185 // in a region but not in construct] 1186 // Variables with static storage duration that are declared in called 1187 // routines in the region are shared. 1188 if (VD && VD->hasGlobalStorage()) 1189 DVar.CKind = OMPC_shared; 1190 1191 // Non-static data members are shared by default. 1192 if (FD) 1193 DVar.CKind = OMPC_shared; 1194 1195 return DVar; 1196 } 1197 1198 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1199 // in a Construct, C/C++, predetermined, p.1] 1200 // Variables with automatic storage duration that are declared in a scope 1201 // inside the construct are private. 1202 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() && 1203 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) { 1204 DVar.CKind = OMPC_private; 1205 return DVar; 1206 } 1207 1208 DVar.DKind = Iter->Directive; 1209 // Explicitly specified attributes and local variables with predetermined 1210 // attributes. 1211 if (Iter->SharingMap.count(D)) { 1212 const DSAInfo &Data = Iter->SharingMap.lookup(D); 1213 DVar.RefExpr = Data.RefExpr.getPointer(); 1214 DVar.PrivateCopy = Data.PrivateCopy; 1215 DVar.CKind = Data.Attributes; 1216 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 1217 DVar.Modifier = Data.Modifier; 1218 DVar.AppliedToPointee = Data.AppliedToPointee; 1219 return DVar; 1220 } 1221 1222 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1223 // in a Construct, C/C++, implicitly determined, p.1] 1224 // In a parallel or task construct, the data-sharing attributes of these 1225 // variables are determined by the default clause, if present. 1226 switch (Iter->DefaultAttr) { 1227 case DSA_shared: 1228 DVar.CKind = OMPC_shared; 1229 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 1230 return DVar; 1231 case DSA_none: 1232 return DVar; 1233 case DSA_firstprivate: 1234 if (VD->getStorageDuration() == SD_Static && 1235 VD->getDeclContext()->isFileContext()) { 1236 DVar.CKind = OMPC_unknown; 1237 } else { 1238 DVar.CKind = OMPC_firstprivate; 1239 } 1240 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 1241 return DVar; 1242 case DSA_unspecified: 1243 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1244 // in a Construct, implicitly determined, p.2] 1245 // In a parallel construct, if no default clause is present, these 1246 // variables are shared. 1247 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 1248 if ((isOpenMPParallelDirective(DVar.DKind) && 1249 !isOpenMPTaskLoopDirective(DVar.DKind)) || 1250 isOpenMPTeamsDirective(DVar.DKind)) { 1251 DVar.CKind = OMPC_shared; 1252 return DVar; 1253 } 1254 1255 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1256 // in a Construct, implicitly determined, p.4] 1257 // In a task construct, if no default clause is present, a variable that in 1258 // the enclosing context is determined to be shared by all implicit tasks 1259 // bound to the current team is shared. 1260 if (isOpenMPTaskingDirective(DVar.DKind)) { 1261 DSAVarData DVarTemp; 1262 const_iterator I = Iter, E = end(); 1263 do { 1264 ++I; 1265 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables 1266 // Referenced in a Construct, implicitly determined, p.6] 1267 // In a task construct, if no default clause is present, a variable 1268 // whose data-sharing attribute is not determined by the rules above is 1269 // firstprivate. 1270 DVarTemp = getDSA(I, D); 1271 if (DVarTemp.CKind != OMPC_shared) { 1272 DVar.RefExpr = nullptr; 1273 DVar.CKind = OMPC_firstprivate; 1274 return DVar; 1275 } 1276 } while (I != E && !isImplicitTaskingRegion(I->Directive)); 1277 DVar.CKind = 1278 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared; 1279 return DVar; 1280 } 1281 } 1282 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1283 // in a Construct, implicitly determined, p.3] 1284 // For constructs other than task, if no default clause is present, these 1285 // variables inherit their data-sharing attributes from the enclosing 1286 // context. 1287 return getDSA(++Iter, D); 1288 } 1289 1290 const Expr *DSAStackTy::addUniqueAligned(const ValueDecl *D, 1291 const Expr *NewDE) { 1292 assert(!isStackEmpty() && "Data sharing attributes stack is empty"); 1293 D = getCanonicalDecl(D); 1294 SharingMapTy &StackElem = getTopOfStack(); 1295 auto It = StackElem.AlignedMap.find(D); 1296 if (It == StackElem.AlignedMap.end()) { 1297 assert(NewDE && "Unexpected nullptr expr to be added into aligned map"); 1298 StackElem.AlignedMap[D] = NewDE; 1299 return nullptr; 1300 } 1301 assert(It->second && "Unexpected nullptr expr in the aligned map"); 1302 return It->second; 1303 } 1304 1305 const Expr *DSAStackTy::addUniqueNontemporal(const ValueDecl *D, 1306 const Expr *NewDE) { 1307 assert(!isStackEmpty() && "Data sharing attributes stack is empty"); 1308 D = getCanonicalDecl(D); 1309 SharingMapTy &StackElem = getTopOfStack(); 1310 auto It = StackElem.NontemporalMap.find(D); 1311 if (It == StackElem.NontemporalMap.end()) { 1312 assert(NewDE && "Unexpected nullptr expr to be added into aligned map"); 1313 StackElem.NontemporalMap[D] = NewDE; 1314 return nullptr; 1315 } 1316 assert(It->second && "Unexpected nullptr expr in the aligned map"); 1317 return It->second; 1318 } 1319 1320 void DSAStackTy::addLoopControlVariable(const ValueDecl *D, VarDecl *Capture) { 1321 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1322 D = getCanonicalDecl(D); 1323 SharingMapTy &StackElem = getTopOfStack(); 1324 StackElem.LCVMap.try_emplace( 1325 D, LCDeclInfo(StackElem.LCVMap.size() + 1, Capture)); 1326 } 1327 1328 const DSAStackTy::LCDeclInfo 1329 DSAStackTy::isLoopControlVariable(const ValueDecl *D) const { 1330 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1331 D = getCanonicalDecl(D); 1332 const SharingMapTy &StackElem = getTopOfStack(); 1333 auto It = StackElem.LCVMap.find(D); 1334 if (It != StackElem.LCVMap.end()) 1335 return It->second; 1336 return {0, nullptr}; 1337 } 1338 1339 const DSAStackTy::LCDeclInfo 1340 DSAStackTy::isLoopControlVariable(const ValueDecl *D, unsigned Level) const { 1341 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1342 D = getCanonicalDecl(D); 1343 for (unsigned I = Level + 1; I > 0; --I) { 1344 const SharingMapTy &StackElem = getStackElemAtLevel(I - 1); 1345 auto It = StackElem.LCVMap.find(D); 1346 if (It != StackElem.LCVMap.end()) 1347 return It->second; 1348 } 1349 return {0, nullptr}; 1350 } 1351 1352 const DSAStackTy::LCDeclInfo 1353 DSAStackTy::isParentLoopControlVariable(const ValueDecl *D) const { 1354 const SharingMapTy *Parent = getSecondOnStackOrNull(); 1355 assert(Parent && "Data-sharing attributes stack is empty"); 1356 D = getCanonicalDecl(D); 1357 auto It = Parent->LCVMap.find(D); 1358 if (It != Parent->LCVMap.end()) 1359 return It->second; 1360 return {0, nullptr}; 1361 } 1362 1363 const ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) const { 1364 const SharingMapTy *Parent = getSecondOnStackOrNull(); 1365 assert(Parent && "Data-sharing attributes stack is empty"); 1366 if (Parent->LCVMap.size() < I) 1367 return nullptr; 1368 for (const auto &Pair : Parent->LCVMap) 1369 if (Pair.second.first == I) 1370 return Pair.first; 1371 return nullptr; 1372 } 1373 1374 void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A, 1375 DeclRefExpr *PrivateCopy, unsigned Modifier, 1376 bool AppliedToPointee) { 1377 D = getCanonicalDecl(D); 1378 if (A == OMPC_threadprivate) { 1379 DSAInfo &Data = Threadprivates[D]; 1380 Data.Attributes = A; 1381 Data.RefExpr.setPointer(E); 1382 Data.PrivateCopy = nullptr; 1383 Data.Modifier = Modifier; 1384 } else { 1385 DSAInfo &Data = getTopOfStack().SharingMap[D]; 1386 assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) || 1387 (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) || 1388 (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) || 1389 (isLoopControlVariable(D).first && A == OMPC_private)); 1390 Data.Modifier = Modifier; 1391 if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) { 1392 Data.RefExpr.setInt(/*IntVal=*/true); 1393 return; 1394 } 1395 const bool IsLastprivate = 1396 A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate; 1397 Data.Attributes = A; 1398 Data.RefExpr.setPointerAndInt(E, IsLastprivate); 1399 Data.PrivateCopy = PrivateCopy; 1400 Data.AppliedToPointee = AppliedToPointee; 1401 if (PrivateCopy) { 1402 DSAInfo &Data = getTopOfStack().SharingMap[PrivateCopy->getDecl()]; 1403 Data.Modifier = Modifier; 1404 Data.Attributes = A; 1405 Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate); 1406 Data.PrivateCopy = nullptr; 1407 Data.AppliedToPointee = AppliedToPointee; 1408 } 1409 } 1410 } 1411 1412 /// Build a variable declaration for OpenMP loop iteration variable. 1413 static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type, 1414 StringRef Name, const AttrVec *Attrs = nullptr, 1415 DeclRefExpr *OrigRef = nullptr) { 1416 DeclContext *DC = SemaRef.CurContext; 1417 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name); 1418 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc); 1419 auto *Decl = 1420 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None); 1421 if (Attrs) { 1422 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end()); 1423 I != E; ++I) 1424 Decl->addAttr(*I); 1425 } 1426 Decl->setImplicit(); 1427 if (OrigRef) { 1428 Decl->addAttr( 1429 OMPReferencedVarAttr::CreateImplicit(SemaRef.Context, OrigRef)); 1430 } 1431 return Decl; 1432 } 1433 1434 static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty, 1435 SourceLocation Loc, 1436 bool RefersToCapture = false) { 1437 D->setReferenced(); 1438 D->markUsed(S.Context); 1439 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(), 1440 SourceLocation(), D, RefersToCapture, Loc, Ty, 1441 VK_LValue); 1442 } 1443 1444 void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 1445 BinaryOperatorKind BOK) { 1446 D = getCanonicalDecl(D); 1447 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1448 assert( 1449 getTopOfStack().SharingMap[D].Attributes == OMPC_reduction && 1450 "Additional reduction info may be specified only for reduction items."); 1451 ReductionData &ReductionData = getTopOfStack().ReductionMap[D]; 1452 assert(ReductionData.ReductionRange.isInvalid() && 1453 (getTopOfStack().Directive == OMPD_taskgroup || 1454 ((isOpenMPParallelDirective(getTopOfStack().Directive) || 1455 isOpenMPWorksharingDirective(getTopOfStack().Directive)) && 1456 !isOpenMPSimdDirective(getTopOfStack().Directive))) && 1457 "Additional reduction info may be specified only once for reduction " 1458 "items."); 1459 ReductionData.set(BOK, SR); 1460 Expr *&TaskgroupReductionRef = 1461 getTopOfStack().TaskgroupReductionRef; 1462 if (!TaskgroupReductionRef) { 1463 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(), 1464 SemaRef.Context.VoidPtrTy, ".task_red."); 1465 TaskgroupReductionRef = 1466 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin()); 1467 } 1468 } 1469 1470 void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 1471 const Expr *ReductionRef) { 1472 D = getCanonicalDecl(D); 1473 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1474 assert( 1475 getTopOfStack().SharingMap[D].Attributes == OMPC_reduction && 1476 "Additional reduction info may be specified only for reduction items."); 1477 ReductionData &ReductionData = getTopOfStack().ReductionMap[D]; 1478 assert(ReductionData.ReductionRange.isInvalid() && 1479 (getTopOfStack().Directive == OMPD_taskgroup || 1480 ((isOpenMPParallelDirective(getTopOfStack().Directive) || 1481 isOpenMPWorksharingDirective(getTopOfStack().Directive)) && 1482 !isOpenMPSimdDirective(getTopOfStack().Directive))) && 1483 "Additional reduction info may be specified only once for reduction " 1484 "items."); 1485 ReductionData.set(ReductionRef, SR); 1486 Expr *&TaskgroupReductionRef = 1487 getTopOfStack().TaskgroupReductionRef; 1488 if (!TaskgroupReductionRef) { 1489 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(), 1490 SemaRef.Context.VoidPtrTy, ".task_red."); 1491 TaskgroupReductionRef = 1492 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin()); 1493 } 1494 } 1495 1496 const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData( 1497 const ValueDecl *D, SourceRange &SR, BinaryOperatorKind &BOK, 1498 Expr *&TaskgroupDescriptor) const { 1499 D = getCanonicalDecl(D); 1500 assert(!isStackEmpty() && "Data-sharing attributes stack is empty."); 1501 for (const_iterator I = begin() + 1, E = end(); I != E; ++I) { 1502 const DSAInfo &Data = I->SharingMap.lookup(D); 1503 if (Data.Attributes != OMPC_reduction || 1504 Data.Modifier != OMPC_REDUCTION_task) 1505 continue; 1506 const ReductionData &ReductionData = I->ReductionMap.lookup(D); 1507 if (!ReductionData.ReductionOp || 1508 ReductionData.ReductionOp.is<const Expr *>()) 1509 return DSAVarData(); 1510 SR = ReductionData.ReductionRange; 1511 BOK = ReductionData.ReductionOp.get<ReductionData::BOKPtrType>(); 1512 assert(I->TaskgroupReductionRef && "taskgroup reduction reference " 1513 "expression for the descriptor is not " 1514 "set."); 1515 TaskgroupDescriptor = I->TaskgroupReductionRef; 1516 return DSAVarData(I->Directive, OMPC_reduction, Data.RefExpr.getPointer(), 1517 Data.PrivateCopy, I->DefaultAttrLoc, OMPC_REDUCTION_task, 1518 /*AppliedToPointee=*/false); 1519 } 1520 return DSAVarData(); 1521 } 1522 1523 const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData( 1524 const ValueDecl *D, SourceRange &SR, const Expr *&ReductionRef, 1525 Expr *&TaskgroupDescriptor) const { 1526 D = getCanonicalDecl(D); 1527 assert(!isStackEmpty() && "Data-sharing attributes stack is empty."); 1528 for (const_iterator I = begin() + 1, E = end(); I != E; ++I) { 1529 const DSAInfo &Data = I->SharingMap.lookup(D); 1530 if (Data.Attributes != OMPC_reduction || 1531 Data.Modifier != OMPC_REDUCTION_task) 1532 continue; 1533 const ReductionData &ReductionData = I->ReductionMap.lookup(D); 1534 if (!ReductionData.ReductionOp || 1535 !ReductionData.ReductionOp.is<const Expr *>()) 1536 return DSAVarData(); 1537 SR = ReductionData.ReductionRange; 1538 ReductionRef = ReductionData.ReductionOp.get<const Expr *>(); 1539 assert(I->TaskgroupReductionRef && "taskgroup reduction reference " 1540 "expression for the descriptor is not " 1541 "set."); 1542 TaskgroupDescriptor = I->TaskgroupReductionRef; 1543 return DSAVarData(I->Directive, OMPC_reduction, Data.RefExpr.getPointer(), 1544 Data.PrivateCopy, I->DefaultAttrLoc, OMPC_REDUCTION_task, 1545 /*AppliedToPointee=*/false); 1546 } 1547 return DSAVarData(); 1548 } 1549 1550 bool DSAStackTy::isOpenMPLocal(VarDecl *D, const_iterator I) const { 1551 D = D->getCanonicalDecl(); 1552 for (const_iterator E = end(); I != E; ++I) { 1553 if (isImplicitOrExplicitTaskingRegion(I->Directive) || 1554 isOpenMPTargetExecutionDirective(I->Directive)) { 1555 if (I->CurScope) { 1556 Scope *TopScope = I->CurScope->getParent(); 1557 Scope *CurScope = getCurScope(); 1558 while (CurScope && CurScope != TopScope && !CurScope->isDeclScope(D)) 1559 CurScope = CurScope->getParent(); 1560 return CurScope != TopScope; 1561 } 1562 for (DeclContext *DC = D->getDeclContext(); DC; DC = DC->getParent()) 1563 if (I->Context == DC) 1564 return true; 1565 return false; 1566 } 1567 } 1568 return false; 1569 } 1570 1571 static bool isConstNotMutableType(Sema &SemaRef, QualType Type, 1572 bool AcceptIfMutable = true, 1573 bool *IsClassType = nullptr) { 1574 ASTContext &Context = SemaRef.getASTContext(); 1575 Type = Type.getNonReferenceType().getCanonicalType(); 1576 bool IsConstant = Type.isConstant(Context); 1577 Type = Context.getBaseElementType(Type); 1578 const CXXRecordDecl *RD = AcceptIfMutable && SemaRef.getLangOpts().CPlusPlus 1579 ? Type->getAsCXXRecordDecl() 1580 : nullptr; 1581 if (const auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD)) 1582 if (const ClassTemplateDecl *CTD = CTSD->getSpecializedTemplate()) 1583 RD = CTD->getTemplatedDecl(); 1584 if (IsClassType) 1585 *IsClassType = RD; 1586 return IsConstant && !(SemaRef.getLangOpts().CPlusPlus && RD && 1587 RD->hasDefinition() && RD->hasMutableFields()); 1588 } 1589 1590 static bool rejectConstNotMutableType(Sema &SemaRef, const ValueDecl *D, 1591 QualType Type, OpenMPClauseKind CKind, 1592 SourceLocation ELoc, 1593 bool AcceptIfMutable = true, 1594 bool ListItemNotVar = false) { 1595 ASTContext &Context = SemaRef.getASTContext(); 1596 bool IsClassType; 1597 if (isConstNotMutableType(SemaRef, Type, AcceptIfMutable, &IsClassType)) { 1598 unsigned Diag = ListItemNotVar 1599 ? diag::err_omp_const_list_item 1600 : IsClassType ? diag::err_omp_const_not_mutable_variable 1601 : diag::err_omp_const_variable; 1602 SemaRef.Diag(ELoc, Diag) << getOpenMPClauseName(CKind); 1603 if (!ListItemNotVar && D) { 1604 const VarDecl *VD = dyn_cast<VarDecl>(D); 1605 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 1606 VarDecl::DeclarationOnly; 1607 SemaRef.Diag(D->getLocation(), 1608 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1609 << D; 1610 } 1611 return true; 1612 } 1613 return false; 1614 } 1615 1616 const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, 1617 bool FromParent) { 1618 D = getCanonicalDecl(D); 1619 DSAVarData DVar; 1620 1621 auto *VD = dyn_cast<VarDecl>(D); 1622 auto TI = Threadprivates.find(D); 1623 if (TI != Threadprivates.end()) { 1624 DVar.RefExpr = TI->getSecond().RefExpr.getPointer(); 1625 DVar.CKind = OMPC_threadprivate; 1626 DVar.Modifier = TI->getSecond().Modifier; 1627 return DVar; 1628 } 1629 if (VD && VD->hasAttr<OMPThreadPrivateDeclAttr>()) { 1630 DVar.RefExpr = buildDeclRefExpr( 1631 SemaRef, VD, D->getType().getNonReferenceType(), 1632 VD->getAttr<OMPThreadPrivateDeclAttr>()->getLocation()); 1633 DVar.CKind = OMPC_threadprivate; 1634 addDSA(D, DVar.RefExpr, OMPC_threadprivate); 1635 return DVar; 1636 } 1637 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1638 // in a Construct, C/C++, predetermined, p.1] 1639 // Variables appearing in threadprivate directives are threadprivate. 1640 if ((VD && VD->getTLSKind() != VarDecl::TLS_None && 1641 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() && 1642 SemaRef.getLangOpts().OpenMPUseTLS && 1643 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) || 1644 (VD && VD->getStorageClass() == SC_Register && 1645 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) { 1646 DVar.RefExpr = buildDeclRefExpr( 1647 SemaRef, VD, D->getType().getNonReferenceType(), D->getLocation()); 1648 DVar.CKind = OMPC_threadprivate; 1649 addDSA(D, DVar.RefExpr, OMPC_threadprivate); 1650 return DVar; 1651 } 1652 if (SemaRef.getLangOpts().OpenMPCUDAMode && VD && 1653 VD->isLocalVarDeclOrParm() && !isStackEmpty() && 1654 !isLoopControlVariable(D).first) { 1655 const_iterator IterTarget = 1656 std::find_if(begin(), end(), [](const SharingMapTy &Data) { 1657 return isOpenMPTargetExecutionDirective(Data.Directive); 1658 }); 1659 if (IterTarget != end()) { 1660 const_iterator ParentIterTarget = IterTarget + 1; 1661 for (const_iterator Iter = begin(); 1662 Iter != ParentIterTarget; ++Iter) { 1663 if (isOpenMPLocal(VD, Iter)) { 1664 DVar.RefExpr = 1665 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(), 1666 D->getLocation()); 1667 DVar.CKind = OMPC_threadprivate; 1668 return DVar; 1669 } 1670 } 1671 if (!isClauseParsingMode() || IterTarget != begin()) { 1672 auto DSAIter = IterTarget->SharingMap.find(D); 1673 if (DSAIter != IterTarget->SharingMap.end() && 1674 isOpenMPPrivate(DSAIter->getSecond().Attributes)) { 1675 DVar.RefExpr = DSAIter->getSecond().RefExpr.getPointer(); 1676 DVar.CKind = OMPC_threadprivate; 1677 return DVar; 1678 } 1679 const_iterator End = end(); 1680 if (!SemaRef.isOpenMPCapturedByRef( 1681 D, std::distance(ParentIterTarget, End), 1682 /*OpenMPCaptureLevel=*/0)) { 1683 DVar.RefExpr = 1684 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(), 1685 IterTarget->ConstructLoc); 1686 DVar.CKind = OMPC_threadprivate; 1687 return DVar; 1688 } 1689 } 1690 } 1691 } 1692 1693 if (isStackEmpty()) 1694 // Not in OpenMP execution region and top scope was already checked. 1695 return DVar; 1696 1697 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1698 // in a Construct, C/C++, predetermined, p.4] 1699 // Static data members are shared. 1700 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1701 // in a Construct, C/C++, predetermined, p.7] 1702 // Variables with static storage duration that are declared in a scope 1703 // inside the construct are shared. 1704 if (VD && VD->isStaticDataMember()) { 1705 // Check for explicitly specified attributes. 1706 const_iterator I = begin(); 1707 const_iterator EndI = end(); 1708 if (FromParent && I != EndI) 1709 ++I; 1710 if (I != EndI) { 1711 auto It = I->SharingMap.find(D); 1712 if (It != I->SharingMap.end()) { 1713 const DSAInfo &Data = It->getSecond(); 1714 DVar.RefExpr = Data.RefExpr.getPointer(); 1715 DVar.PrivateCopy = Data.PrivateCopy; 1716 DVar.CKind = Data.Attributes; 1717 DVar.ImplicitDSALoc = I->DefaultAttrLoc; 1718 DVar.DKind = I->Directive; 1719 DVar.Modifier = Data.Modifier; 1720 DVar.AppliedToPointee = Data.AppliedToPointee; 1721 return DVar; 1722 } 1723 } 1724 1725 DVar.CKind = OMPC_shared; 1726 return DVar; 1727 } 1728 1729 auto &&MatchesAlways = [](OpenMPDirectiveKind) { return true; }; 1730 // The predetermined shared attribute for const-qualified types having no 1731 // mutable members was removed after OpenMP 3.1. 1732 if (SemaRef.LangOpts.OpenMP <= 31) { 1733 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1734 // in a Construct, C/C++, predetermined, p.6] 1735 // Variables with const qualified type having no mutable member are 1736 // shared. 1737 if (isConstNotMutableType(SemaRef, D->getType())) { 1738 // Variables with const-qualified type having no mutable member may be 1739 // listed in a firstprivate clause, even if they are static data members. 1740 DSAVarData DVarTemp = hasInnermostDSA( 1741 D, 1742 [](OpenMPClauseKind C, bool) { 1743 return C == OMPC_firstprivate || C == OMPC_shared; 1744 }, 1745 MatchesAlways, FromParent); 1746 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr) 1747 return DVarTemp; 1748 1749 DVar.CKind = OMPC_shared; 1750 return DVar; 1751 } 1752 } 1753 1754 // Explicitly specified attributes and local variables with predetermined 1755 // attributes. 1756 const_iterator I = begin(); 1757 const_iterator EndI = end(); 1758 if (FromParent && I != EndI) 1759 ++I; 1760 if (I == EndI) 1761 return DVar; 1762 auto It = I->SharingMap.find(D); 1763 if (It != I->SharingMap.end()) { 1764 const DSAInfo &Data = It->getSecond(); 1765 DVar.RefExpr = Data.RefExpr.getPointer(); 1766 DVar.PrivateCopy = Data.PrivateCopy; 1767 DVar.CKind = Data.Attributes; 1768 DVar.ImplicitDSALoc = I->DefaultAttrLoc; 1769 DVar.DKind = I->Directive; 1770 DVar.Modifier = Data.Modifier; 1771 DVar.AppliedToPointee = Data.AppliedToPointee; 1772 } 1773 1774 return DVar; 1775 } 1776 1777 const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D, 1778 bool FromParent) const { 1779 if (isStackEmpty()) { 1780 const_iterator I; 1781 return getDSA(I, D); 1782 } 1783 D = getCanonicalDecl(D); 1784 const_iterator StartI = begin(); 1785 const_iterator EndI = end(); 1786 if (FromParent && StartI != EndI) 1787 ++StartI; 1788 return getDSA(StartI, D); 1789 } 1790 1791 const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D, 1792 unsigned Level) const { 1793 if (getStackSize() <= Level) 1794 return DSAVarData(); 1795 D = getCanonicalDecl(D); 1796 const_iterator StartI = std::next(begin(), getStackSize() - 1 - Level); 1797 return getDSA(StartI, D); 1798 } 1799 1800 const DSAStackTy::DSAVarData 1801 DSAStackTy::hasDSA(ValueDecl *D, 1802 const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred, 1803 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1804 bool FromParent) const { 1805 if (isStackEmpty()) 1806 return {}; 1807 D = getCanonicalDecl(D); 1808 const_iterator I = begin(); 1809 const_iterator EndI = end(); 1810 if (FromParent && I != EndI) 1811 ++I; 1812 for (; I != EndI; ++I) { 1813 if (!DPred(I->Directive) && 1814 !isImplicitOrExplicitTaskingRegion(I->Directive)) 1815 continue; 1816 const_iterator NewI = I; 1817 DSAVarData DVar = getDSA(NewI, D); 1818 if (I == NewI && CPred(DVar.CKind, DVar.AppliedToPointee)) 1819 return DVar; 1820 } 1821 return {}; 1822 } 1823 1824 const DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA( 1825 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred, 1826 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1827 bool FromParent) const { 1828 if (isStackEmpty()) 1829 return {}; 1830 D = getCanonicalDecl(D); 1831 const_iterator StartI = begin(); 1832 const_iterator EndI = end(); 1833 if (FromParent && StartI != EndI) 1834 ++StartI; 1835 if (StartI == EndI || !DPred(StartI->Directive)) 1836 return {}; 1837 const_iterator NewI = StartI; 1838 DSAVarData DVar = getDSA(NewI, D); 1839 return (NewI == StartI && CPred(DVar.CKind, DVar.AppliedToPointee)) 1840 ? DVar 1841 : DSAVarData(); 1842 } 1843 1844 bool DSAStackTy::hasExplicitDSA( 1845 const ValueDecl *D, 1846 const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred, 1847 unsigned Level, bool NotLastprivate) const { 1848 if (getStackSize() <= Level) 1849 return false; 1850 D = getCanonicalDecl(D); 1851 const SharingMapTy &StackElem = getStackElemAtLevel(Level); 1852 auto I = StackElem.SharingMap.find(D); 1853 if (I != StackElem.SharingMap.end() && I->getSecond().RefExpr.getPointer() && 1854 CPred(I->getSecond().Attributes, I->getSecond().AppliedToPointee) && 1855 (!NotLastprivate || !I->getSecond().RefExpr.getInt())) 1856 return true; 1857 // Check predetermined rules for the loop control variables. 1858 auto LI = StackElem.LCVMap.find(D); 1859 if (LI != StackElem.LCVMap.end()) 1860 return CPred(OMPC_private, /*AppliedToPointee=*/false); 1861 return false; 1862 } 1863 1864 bool DSAStackTy::hasExplicitDirective( 1865 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1866 unsigned Level) const { 1867 if (getStackSize() <= Level) 1868 return false; 1869 const SharingMapTy &StackElem = getStackElemAtLevel(Level); 1870 return DPred(StackElem.Directive); 1871 } 1872 1873 bool DSAStackTy::hasDirective( 1874 const llvm::function_ref<bool(OpenMPDirectiveKind, 1875 const DeclarationNameInfo &, SourceLocation)> 1876 DPred, 1877 bool FromParent) const { 1878 // We look only in the enclosing region. 1879 size_t Skip = FromParent ? 2 : 1; 1880 for (const_iterator I = begin() + std::min(Skip, getStackSize()), E = end(); 1881 I != E; ++I) { 1882 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc)) 1883 return true; 1884 } 1885 return false; 1886 } 1887 1888 void Sema::InitDataSharingAttributesStack() { 1889 VarDataSharingAttributesStack = new DSAStackTy(*this); 1890 } 1891 1892 #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack) 1893 1894 void Sema::pushOpenMPFunctionRegion() { 1895 DSAStack->pushFunction(); 1896 } 1897 1898 void Sema::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) { 1899 DSAStack->popFunction(OldFSI); 1900 } 1901 1902 static bool isOpenMPDeviceDelayedContext(Sema &S) { 1903 assert(S.LangOpts.OpenMP && S.LangOpts.OpenMPIsDevice && 1904 "Expected OpenMP device compilation."); 1905 return !S.isInOpenMPTargetExecutionDirective(); 1906 } 1907 1908 namespace { 1909 /// Status of the function emission on the host/device. 1910 enum class FunctionEmissionStatus { 1911 Emitted, 1912 Discarded, 1913 Unknown, 1914 }; 1915 } // anonymous namespace 1916 1917 Sema::SemaDiagnosticBuilder Sema::diagIfOpenMPDeviceCode(SourceLocation Loc, 1918 unsigned DiagID, 1919 FunctionDecl *FD) { 1920 assert(LangOpts.OpenMP && LangOpts.OpenMPIsDevice && 1921 "Expected OpenMP device compilation."); 1922 1923 SemaDiagnosticBuilder::Kind Kind = SemaDiagnosticBuilder::K_Nop; 1924 if (FD) { 1925 FunctionEmissionStatus FES = getEmissionStatus(FD); 1926 switch (FES) { 1927 case FunctionEmissionStatus::Emitted: 1928 Kind = SemaDiagnosticBuilder::K_Immediate; 1929 break; 1930 case FunctionEmissionStatus::Unknown: 1931 // TODO: We should always delay diagnostics here in case a target 1932 // region is in a function we do not emit. However, as the 1933 // current diagnostics are associated with the function containing 1934 // the target region and we do not emit that one, we would miss out 1935 // on diagnostics for the target region itself. We need to anchor 1936 // the diagnostics with the new generated function *or* ensure we 1937 // emit diagnostics associated with the surrounding function. 1938 Kind = isOpenMPDeviceDelayedContext(*this) 1939 ? SemaDiagnosticBuilder::K_Deferred 1940 : SemaDiagnosticBuilder::K_Immediate; 1941 break; 1942 case FunctionEmissionStatus::TemplateDiscarded: 1943 case FunctionEmissionStatus::OMPDiscarded: 1944 Kind = SemaDiagnosticBuilder::K_Nop; 1945 break; 1946 case FunctionEmissionStatus::CUDADiscarded: 1947 llvm_unreachable("CUDADiscarded unexpected in OpenMP device compilation"); 1948 break; 1949 } 1950 } 1951 1952 return SemaDiagnosticBuilder(Kind, Loc, DiagID, FD, *this); 1953 } 1954 1955 Sema::SemaDiagnosticBuilder Sema::diagIfOpenMPHostCode(SourceLocation Loc, 1956 unsigned DiagID, 1957 FunctionDecl *FD) { 1958 assert(LangOpts.OpenMP && !LangOpts.OpenMPIsDevice && 1959 "Expected OpenMP host compilation."); 1960 1961 SemaDiagnosticBuilder::Kind Kind = SemaDiagnosticBuilder::K_Nop; 1962 if (FD) { 1963 FunctionEmissionStatus FES = getEmissionStatus(FD); 1964 switch (FES) { 1965 case FunctionEmissionStatus::Emitted: 1966 Kind = SemaDiagnosticBuilder::K_Immediate; 1967 break; 1968 case FunctionEmissionStatus::Unknown: 1969 Kind = SemaDiagnosticBuilder::K_Deferred; 1970 break; 1971 case FunctionEmissionStatus::TemplateDiscarded: 1972 case FunctionEmissionStatus::OMPDiscarded: 1973 case FunctionEmissionStatus::CUDADiscarded: 1974 Kind = SemaDiagnosticBuilder::K_Nop; 1975 break; 1976 } 1977 } 1978 1979 return SemaDiagnosticBuilder(Kind, Loc, DiagID, FD, *this); 1980 } 1981 1982 static OpenMPDefaultmapClauseKind 1983 getVariableCategoryFromDecl(const LangOptions &LO, const ValueDecl *VD) { 1984 if (LO.OpenMP <= 45) { 1985 if (VD->getType().getNonReferenceType()->isScalarType()) 1986 return OMPC_DEFAULTMAP_scalar; 1987 return OMPC_DEFAULTMAP_aggregate; 1988 } 1989 if (VD->getType().getNonReferenceType()->isAnyPointerType()) 1990 return OMPC_DEFAULTMAP_pointer; 1991 if (VD->getType().getNonReferenceType()->isScalarType()) 1992 return OMPC_DEFAULTMAP_scalar; 1993 return OMPC_DEFAULTMAP_aggregate; 1994 } 1995 1996 bool Sema::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level, 1997 unsigned OpenMPCaptureLevel) const { 1998 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1999 2000 ASTContext &Ctx = getASTContext(); 2001 bool IsByRef = true; 2002 2003 // Find the directive that is associated with the provided scope. 2004 D = cast<ValueDecl>(D->getCanonicalDecl()); 2005 QualType Ty = D->getType(); 2006 2007 bool IsVariableUsedInMapClause = false; 2008 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) { 2009 // This table summarizes how a given variable should be passed to the device 2010 // given its type and the clauses where it appears. This table is based on 2011 // the description in OpenMP 4.5 [2.10.4, target Construct] and 2012 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses]. 2013 // 2014 // ========================================================================= 2015 // | type | defaultmap | pvt | first | is_device_ptr | map | res. | 2016 // | |(tofrom:scalar)| | pvt | | | | 2017 // ========================================================================= 2018 // | scl | | | | - | | bycopy| 2019 // | scl | | - | x | - | - | bycopy| 2020 // | scl | | x | - | - | - | null | 2021 // | scl | x | | | - | | byref | 2022 // | scl | x | - | x | - | - | bycopy| 2023 // | scl | x | x | - | - | - | null | 2024 // | scl | | - | - | - | x | byref | 2025 // | scl | x | - | - | - | x | byref | 2026 // 2027 // | agg | n.a. | | | - | | byref | 2028 // | agg | n.a. | - | x | - | - | byref | 2029 // | agg | n.a. | x | - | - | - | null | 2030 // | agg | n.a. | - | - | - | x | byref | 2031 // | agg | n.a. | - | - | - | x[] | byref | 2032 // 2033 // | ptr | n.a. | | | - | | bycopy| 2034 // | ptr | n.a. | - | x | - | - | bycopy| 2035 // | ptr | n.a. | x | - | - | - | null | 2036 // | ptr | n.a. | - | - | - | x | byref | 2037 // | ptr | n.a. | - | - | - | x[] | bycopy| 2038 // | ptr | n.a. | - | - | x | | bycopy| 2039 // | ptr | n.a. | - | - | x | x | bycopy| 2040 // | ptr | n.a. | - | - | x | x[] | bycopy| 2041 // ========================================================================= 2042 // Legend: 2043 // scl - scalar 2044 // ptr - pointer 2045 // agg - aggregate 2046 // x - applies 2047 // - - invalid in this combination 2048 // [] - mapped with an array section 2049 // byref - should be mapped by reference 2050 // byval - should be mapped by value 2051 // null - initialize a local variable to null on the device 2052 // 2053 // Observations: 2054 // - All scalar declarations that show up in a map clause have to be passed 2055 // by reference, because they may have been mapped in the enclosing data 2056 // environment. 2057 // - If the scalar value does not fit the size of uintptr, it has to be 2058 // passed by reference, regardless the result in the table above. 2059 // - For pointers mapped by value that have either an implicit map or an 2060 // array section, the runtime library may pass the NULL value to the 2061 // device instead of the value passed to it by the compiler. 2062 2063 if (Ty->isReferenceType()) 2064 Ty = Ty->castAs<ReferenceType>()->getPointeeType(); 2065 2066 // Locate map clauses and see if the variable being captured is referred to 2067 // in any of those clauses. Here we only care about variables, not fields, 2068 // because fields are part of aggregates. 2069 bool IsVariableAssociatedWithSection = false; 2070 2071 DSAStack->checkMappableExprComponentListsForDeclAtLevel( 2072 D, Level, 2073 [&IsVariableUsedInMapClause, &IsVariableAssociatedWithSection, D]( 2074 OMPClauseMappableExprCommon::MappableExprComponentListRef 2075 MapExprComponents, 2076 OpenMPClauseKind WhereFoundClauseKind) { 2077 // Only the map clause information influences how a variable is 2078 // captured. E.g. is_device_ptr does not require changing the default 2079 // behavior. 2080 if (WhereFoundClauseKind != OMPC_map) 2081 return false; 2082 2083 auto EI = MapExprComponents.rbegin(); 2084 auto EE = MapExprComponents.rend(); 2085 2086 assert(EI != EE && "Invalid map expression!"); 2087 2088 if (isa<DeclRefExpr>(EI->getAssociatedExpression())) 2089 IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D; 2090 2091 ++EI; 2092 if (EI == EE) 2093 return false; 2094 2095 if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) || 2096 isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) || 2097 isa<MemberExpr>(EI->getAssociatedExpression()) || 2098 isa<OMPArrayShapingExpr>(EI->getAssociatedExpression())) { 2099 IsVariableAssociatedWithSection = true; 2100 // There is nothing more we need to know about this variable. 2101 return true; 2102 } 2103 2104 // Keep looking for more map info. 2105 return false; 2106 }); 2107 2108 if (IsVariableUsedInMapClause) { 2109 // If variable is identified in a map clause it is always captured by 2110 // reference except if it is a pointer that is dereferenced somehow. 2111 IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection); 2112 } else { 2113 // By default, all the data that has a scalar type is mapped by copy 2114 // (except for reduction variables). 2115 // Defaultmap scalar is mutual exclusive to defaultmap pointer 2116 IsByRef = (DSAStack->isForceCaptureByReferenceInTargetExecutable() && 2117 !Ty->isAnyPointerType()) || 2118 !Ty->isScalarType() || 2119 DSAStack->isDefaultmapCapturedByRef( 2120 Level, getVariableCategoryFromDecl(LangOpts, D)) || 2121 DSAStack->hasExplicitDSA( 2122 D, 2123 [](OpenMPClauseKind K, bool AppliedToPointee) { 2124 return K == OMPC_reduction && !AppliedToPointee; 2125 }, 2126 Level); 2127 } 2128 } 2129 2130 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) { 2131 IsByRef = 2132 ((IsVariableUsedInMapClause && 2133 DSAStack->getCaptureRegion(Level, OpenMPCaptureLevel) == 2134 OMPD_target) || 2135 !(DSAStack->hasExplicitDSA( 2136 D, 2137 [](OpenMPClauseKind K, bool AppliedToPointee) -> bool { 2138 return K == OMPC_firstprivate || 2139 (K == OMPC_reduction && AppliedToPointee); 2140 }, 2141 Level, /*NotLastprivate=*/true) || 2142 DSAStack->isUsesAllocatorsDecl(Level, D))) && 2143 // If the variable is artificial and must be captured by value - try to 2144 // capture by value. 2145 !(isa<OMPCapturedExprDecl>(D) && !D->hasAttr<OMPCaptureNoInitAttr>() && 2146 !cast<OMPCapturedExprDecl>(D)->getInit()->isGLValue()) && 2147 // If the variable is implicitly firstprivate and scalar - capture by 2148 // copy 2149 !(DSAStack->getDefaultDSA() == DSA_firstprivate && 2150 !DSAStack->hasExplicitDSA( 2151 D, [](OpenMPClauseKind K, bool) { return K != OMPC_unknown; }, 2152 Level) && 2153 !DSAStack->isLoopControlVariable(D, Level).first); 2154 } 2155 2156 // When passing data by copy, we need to make sure it fits the uintptr size 2157 // and alignment, because the runtime library only deals with uintptr types. 2158 // If it does not fit the uintptr size, we need to pass the data by reference 2159 // instead. 2160 if (!IsByRef && 2161 (Ctx.getTypeSizeInChars(Ty) > 2162 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) || 2163 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) { 2164 IsByRef = true; 2165 } 2166 2167 return IsByRef; 2168 } 2169 2170 unsigned Sema::getOpenMPNestingLevel() const { 2171 assert(getLangOpts().OpenMP); 2172 return DSAStack->getNestingLevel(); 2173 } 2174 2175 bool Sema::isInOpenMPTargetExecutionDirective() const { 2176 return (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) && 2177 !DSAStack->isClauseParsingMode()) || 2178 DSAStack->hasDirective( 2179 [](OpenMPDirectiveKind K, const DeclarationNameInfo &, 2180 SourceLocation) -> bool { 2181 return isOpenMPTargetExecutionDirective(K); 2182 }, 2183 false); 2184 } 2185 2186 VarDecl *Sema::isOpenMPCapturedDecl(ValueDecl *D, bool CheckScopeInfo, 2187 unsigned StopAt) { 2188 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 2189 D = getCanonicalDecl(D); 2190 2191 auto *VD = dyn_cast<VarDecl>(D); 2192 // Do not capture constexpr variables. 2193 if (VD && VD->isConstexpr()) 2194 return nullptr; 2195 2196 // If we want to determine whether the variable should be captured from the 2197 // perspective of the current capturing scope, and we've already left all the 2198 // capturing scopes of the top directive on the stack, check from the 2199 // perspective of its parent directive (if any) instead. 2200 DSAStackTy::ParentDirectiveScope InParentDirectiveRAII( 2201 *DSAStack, CheckScopeInfo && DSAStack->isBodyComplete()); 2202 2203 // If we are attempting to capture a global variable in a directive with 2204 // 'target' we return true so that this global is also mapped to the device. 2205 // 2206 if (VD && !VD->hasLocalStorage() && 2207 (getCurCapturedRegion() || getCurBlock() || getCurLambda())) { 2208 if (isInOpenMPTargetExecutionDirective()) { 2209 DSAStackTy::DSAVarData DVarTop = 2210 DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode()); 2211 if (DVarTop.CKind != OMPC_unknown && DVarTop.RefExpr) 2212 return VD; 2213 // If the declaration is enclosed in a 'declare target' directive, 2214 // then it should not be captured. 2215 // 2216 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) 2217 return nullptr; 2218 CapturedRegionScopeInfo *CSI = nullptr; 2219 for (FunctionScopeInfo *FSI : llvm::drop_begin( 2220 llvm::reverse(FunctionScopes), 2221 CheckScopeInfo ? (FunctionScopes.size() - (StopAt + 1)) : 0)) { 2222 if (!isa<CapturingScopeInfo>(FSI)) 2223 return nullptr; 2224 if (auto *RSI = dyn_cast<CapturedRegionScopeInfo>(FSI)) 2225 if (RSI->CapRegionKind == CR_OpenMP) { 2226 CSI = RSI; 2227 break; 2228 } 2229 } 2230 assert(CSI && "Failed to find CapturedRegionScopeInfo"); 2231 SmallVector<OpenMPDirectiveKind, 4> Regions; 2232 getOpenMPCaptureRegions(Regions, 2233 DSAStack->getDirective(CSI->OpenMPLevel)); 2234 if (Regions[CSI->OpenMPCaptureLevel] != OMPD_task) 2235 return VD; 2236 } 2237 if (isInOpenMPDeclareTargetContext()) { 2238 // Try to mark variable as declare target if it is used in capturing 2239 // regions. 2240 if (LangOpts.OpenMP <= 45 && 2241 !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) 2242 checkDeclIsAllowedInOpenMPTarget(nullptr, VD); 2243 return nullptr; 2244 } 2245 } 2246 2247 if (CheckScopeInfo) { 2248 bool OpenMPFound = false; 2249 for (unsigned I = StopAt + 1; I > 0; --I) { 2250 FunctionScopeInfo *FSI = FunctionScopes[I - 1]; 2251 if(!isa<CapturingScopeInfo>(FSI)) 2252 return nullptr; 2253 if (auto *RSI = dyn_cast<CapturedRegionScopeInfo>(FSI)) 2254 if (RSI->CapRegionKind == CR_OpenMP) { 2255 OpenMPFound = true; 2256 break; 2257 } 2258 } 2259 if (!OpenMPFound) 2260 return nullptr; 2261 } 2262 2263 if (DSAStack->getCurrentDirective() != OMPD_unknown && 2264 (!DSAStack->isClauseParsingMode() || 2265 DSAStack->getParentDirective() != OMPD_unknown)) { 2266 auto &&Info = DSAStack->isLoopControlVariable(D); 2267 if (Info.first || 2268 (VD && VD->hasLocalStorage() && 2269 isImplicitOrExplicitTaskingRegion(DSAStack->getCurrentDirective())) || 2270 (VD && DSAStack->isForceVarCapturing())) 2271 return VD ? VD : Info.second; 2272 DSAStackTy::DSAVarData DVarTop = 2273 DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode()); 2274 if (DVarTop.CKind != OMPC_unknown && isOpenMPPrivate(DVarTop.CKind) && 2275 (!VD || VD->hasLocalStorage() || !DVarTop.AppliedToPointee)) 2276 return VD ? VD : cast<VarDecl>(DVarTop.PrivateCopy->getDecl()); 2277 // Threadprivate variables must not be captured. 2278 if (isOpenMPThreadPrivate(DVarTop.CKind)) 2279 return nullptr; 2280 // The variable is not private or it is the variable in the directive with 2281 // default(none) clause and not used in any clause. 2282 DSAStackTy::DSAVarData DVarPrivate = DSAStack->hasDSA( 2283 D, 2284 [](OpenMPClauseKind C, bool AppliedToPointee) { 2285 return isOpenMPPrivate(C) && !AppliedToPointee; 2286 }, 2287 [](OpenMPDirectiveKind) { return true; }, 2288 DSAStack->isClauseParsingMode()); 2289 // Global shared must not be captured. 2290 if (VD && !VD->hasLocalStorage() && DVarPrivate.CKind == OMPC_unknown && 2291 ((DSAStack->getDefaultDSA() != DSA_none && 2292 DSAStack->getDefaultDSA() != DSA_firstprivate) || 2293 DVarTop.CKind == OMPC_shared)) 2294 return nullptr; 2295 if (DVarPrivate.CKind != OMPC_unknown || 2296 (VD && (DSAStack->getDefaultDSA() == DSA_none || 2297 DSAStack->getDefaultDSA() == DSA_firstprivate))) 2298 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl()); 2299 } 2300 return nullptr; 2301 } 2302 2303 void Sema::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex, 2304 unsigned Level) const { 2305 FunctionScopesIndex -= getOpenMPCaptureLevels(DSAStack->getDirective(Level)); 2306 } 2307 2308 void Sema::startOpenMPLoop() { 2309 assert(LangOpts.OpenMP && "OpenMP must be enabled."); 2310 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) 2311 DSAStack->loopInit(); 2312 } 2313 2314 void Sema::startOpenMPCXXRangeFor() { 2315 assert(LangOpts.OpenMP && "OpenMP must be enabled."); 2316 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { 2317 DSAStack->resetPossibleLoopCounter(); 2318 DSAStack->loopStart(); 2319 } 2320 } 2321 2322 OpenMPClauseKind Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level, 2323 unsigned CapLevel) const { 2324 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 2325 if (DSAStack->hasExplicitDirective( 2326 [](OpenMPDirectiveKind K) { return isOpenMPTaskingDirective(K); }, 2327 Level)) { 2328 bool IsTriviallyCopyable = 2329 D->getType().getNonReferenceType().isTriviallyCopyableType(Context) && 2330 !D->getType() 2331 .getNonReferenceType() 2332 .getCanonicalType() 2333 ->getAsCXXRecordDecl(); 2334 OpenMPDirectiveKind DKind = DSAStack->getDirective(Level); 2335 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 2336 getOpenMPCaptureRegions(CaptureRegions, DKind); 2337 if (isOpenMPTaskingDirective(CaptureRegions[CapLevel]) && 2338 (IsTriviallyCopyable || 2339 !isOpenMPTaskLoopDirective(CaptureRegions[CapLevel]))) { 2340 if (DSAStack->hasExplicitDSA( 2341 D, 2342 [](OpenMPClauseKind K, bool) { return K == OMPC_firstprivate; }, 2343 Level, /*NotLastprivate=*/true)) 2344 return OMPC_firstprivate; 2345 DSAStackTy::DSAVarData DVar = DSAStack->getImplicitDSA(D, Level); 2346 if (DVar.CKind != OMPC_shared && 2347 !DSAStack->isLoopControlVariable(D, Level).first && !DVar.RefExpr) { 2348 DSAStack->addImplicitTaskFirstprivate(Level, D); 2349 return OMPC_firstprivate; 2350 } 2351 } 2352 } 2353 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { 2354 if (DSAStack->getAssociatedLoops() > 0 && 2355 !DSAStack->isLoopStarted()) { 2356 DSAStack->resetPossibleLoopCounter(D); 2357 DSAStack->loopStart(); 2358 return OMPC_private; 2359 } 2360 if ((DSAStack->getPossiblyLoopCunter() == D->getCanonicalDecl() || 2361 DSAStack->isLoopControlVariable(D).first) && 2362 !DSAStack->hasExplicitDSA( 2363 D, [](OpenMPClauseKind K, bool) { return K != OMPC_private; }, 2364 Level) && 2365 !isOpenMPSimdDirective(DSAStack->getCurrentDirective())) 2366 return OMPC_private; 2367 } 2368 if (const auto *VD = dyn_cast<VarDecl>(D)) { 2369 if (DSAStack->isThreadPrivate(const_cast<VarDecl *>(VD)) && 2370 DSAStack->isForceVarCapturing() && 2371 !DSAStack->hasExplicitDSA( 2372 D, [](OpenMPClauseKind K, bool) { return K == OMPC_copyin; }, 2373 Level)) 2374 return OMPC_private; 2375 } 2376 // User-defined allocators are private since they must be defined in the 2377 // context of target region. 2378 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level) && 2379 DSAStack->isUsesAllocatorsDecl(Level, D).getValueOr( 2380 DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait) == 2381 DSAStackTy::UsesAllocatorsDeclKind::UserDefinedAllocator) 2382 return OMPC_private; 2383 return (DSAStack->hasExplicitDSA( 2384 D, [](OpenMPClauseKind K, bool) { return K == OMPC_private; }, 2385 Level) || 2386 (DSAStack->isClauseParsingMode() && 2387 DSAStack->getClauseParsingMode() == OMPC_private) || 2388 // Consider taskgroup reduction descriptor variable a private 2389 // to avoid possible capture in the region. 2390 (DSAStack->hasExplicitDirective( 2391 [](OpenMPDirectiveKind K) { 2392 return K == OMPD_taskgroup || 2393 ((isOpenMPParallelDirective(K) || 2394 isOpenMPWorksharingDirective(K)) && 2395 !isOpenMPSimdDirective(K)); 2396 }, 2397 Level) && 2398 DSAStack->isTaskgroupReductionRef(D, Level))) 2399 ? OMPC_private 2400 : OMPC_unknown; 2401 } 2402 2403 void Sema::setOpenMPCaptureKind(FieldDecl *FD, const ValueDecl *D, 2404 unsigned Level) { 2405 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 2406 D = getCanonicalDecl(D); 2407 OpenMPClauseKind OMPC = OMPC_unknown; 2408 for (unsigned I = DSAStack->getNestingLevel() + 1; I > Level; --I) { 2409 const unsigned NewLevel = I - 1; 2410 if (DSAStack->hasExplicitDSA( 2411 D, 2412 [&OMPC](const OpenMPClauseKind K, bool AppliedToPointee) { 2413 if (isOpenMPPrivate(K) && !AppliedToPointee) { 2414 OMPC = K; 2415 return true; 2416 } 2417 return false; 2418 }, 2419 NewLevel)) 2420 break; 2421 if (DSAStack->checkMappableExprComponentListsForDeclAtLevel( 2422 D, NewLevel, 2423 [](OMPClauseMappableExprCommon::MappableExprComponentListRef, 2424 OpenMPClauseKind) { return true; })) { 2425 OMPC = OMPC_map; 2426 break; 2427 } 2428 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, 2429 NewLevel)) { 2430 OMPC = OMPC_map; 2431 if (DSAStack->mustBeFirstprivateAtLevel( 2432 NewLevel, getVariableCategoryFromDecl(LangOpts, D))) 2433 OMPC = OMPC_firstprivate; 2434 break; 2435 } 2436 } 2437 if (OMPC != OMPC_unknown) 2438 FD->addAttr(OMPCaptureKindAttr::CreateImplicit(Context, unsigned(OMPC))); 2439 } 2440 2441 bool Sema::isOpenMPTargetCapturedDecl(const ValueDecl *D, unsigned Level, 2442 unsigned CaptureLevel) const { 2443 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 2444 // Return true if the current level is no longer enclosed in a target region. 2445 2446 SmallVector<OpenMPDirectiveKind, 4> Regions; 2447 getOpenMPCaptureRegions(Regions, DSAStack->getDirective(Level)); 2448 const auto *VD = dyn_cast<VarDecl>(D); 2449 return VD && !VD->hasLocalStorage() && 2450 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, 2451 Level) && 2452 Regions[CaptureLevel] != OMPD_task; 2453 } 2454 2455 bool Sema::isOpenMPGlobalCapturedDecl(ValueDecl *D, unsigned Level, 2456 unsigned CaptureLevel) const { 2457 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 2458 // Return true if the current level is no longer enclosed in a target region. 2459 2460 if (const auto *VD = dyn_cast<VarDecl>(D)) { 2461 if (!VD->hasLocalStorage()) { 2462 if (isInOpenMPTargetExecutionDirective()) 2463 return true; 2464 DSAStackTy::DSAVarData TopDVar = 2465 DSAStack->getTopDSA(D, /*FromParent=*/false); 2466 unsigned NumLevels = 2467 getOpenMPCaptureLevels(DSAStack->getDirective(Level)); 2468 if (Level == 0) 2469 return (NumLevels == CaptureLevel + 1) && TopDVar.CKind != OMPC_shared; 2470 do { 2471 --Level; 2472 DSAStackTy::DSAVarData DVar = DSAStack->getImplicitDSA(D, Level); 2473 if (DVar.CKind != OMPC_shared) 2474 return true; 2475 } while (Level > 0); 2476 } 2477 } 2478 return true; 2479 } 2480 2481 void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; } 2482 2483 void Sema::ActOnOpenMPBeginDeclareVariant(SourceLocation Loc, 2484 OMPTraitInfo &TI) { 2485 OMPDeclareVariantScopes.push_back(OMPDeclareVariantScope(TI)); 2486 } 2487 2488 void Sema::ActOnOpenMPEndDeclareVariant() { 2489 assert(isInOpenMPDeclareVariantScope() && 2490 "Not in OpenMP declare variant scope!"); 2491 2492 OMPDeclareVariantScopes.pop_back(); 2493 } 2494 2495 void Sema::finalizeOpenMPDelayedAnalysis(const FunctionDecl *Caller, 2496 const FunctionDecl *Callee, 2497 SourceLocation Loc) { 2498 assert(LangOpts.OpenMP && "Expected OpenMP compilation mode."); 2499 Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy = 2500 OMPDeclareTargetDeclAttr::getDeviceType(Caller->getMostRecentDecl()); 2501 // Ignore host functions during device analyzis. 2502 if (LangOpts.OpenMPIsDevice && 2503 (!DevTy || *DevTy == OMPDeclareTargetDeclAttr::DT_Host)) 2504 return; 2505 // Ignore nohost functions during host analyzis. 2506 if (!LangOpts.OpenMPIsDevice && DevTy && 2507 *DevTy == OMPDeclareTargetDeclAttr::DT_NoHost) 2508 return; 2509 const FunctionDecl *FD = Callee->getMostRecentDecl(); 2510 DevTy = OMPDeclareTargetDeclAttr::getDeviceType(FD); 2511 if (LangOpts.OpenMPIsDevice && DevTy && 2512 *DevTy == OMPDeclareTargetDeclAttr::DT_Host) { 2513 // Diagnose host function called during device codegen. 2514 StringRef HostDevTy = 2515 getOpenMPSimpleClauseTypeName(OMPC_device_type, OMPC_DEVICE_TYPE_host); 2516 Diag(Loc, diag::err_omp_wrong_device_function_call) << HostDevTy << 0; 2517 Diag(*OMPDeclareTargetDeclAttr::getLocation(FD), 2518 diag::note_omp_marked_device_type_here) 2519 << HostDevTy; 2520 return; 2521 } 2522 if (!LangOpts.OpenMPIsDevice && DevTy && 2523 *DevTy == OMPDeclareTargetDeclAttr::DT_NoHost) { 2524 // Diagnose nohost function called during host codegen. 2525 StringRef NoHostDevTy = getOpenMPSimpleClauseTypeName( 2526 OMPC_device_type, OMPC_DEVICE_TYPE_nohost); 2527 Diag(Loc, diag::err_omp_wrong_device_function_call) << NoHostDevTy << 1; 2528 Diag(*OMPDeclareTargetDeclAttr::getLocation(FD), 2529 diag::note_omp_marked_device_type_here) 2530 << NoHostDevTy; 2531 } 2532 } 2533 2534 void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind, 2535 const DeclarationNameInfo &DirName, 2536 Scope *CurScope, SourceLocation Loc) { 2537 DSAStack->push(DKind, DirName, CurScope, Loc); 2538 PushExpressionEvaluationContext( 2539 ExpressionEvaluationContext::PotentiallyEvaluated); 2540 } 2541 2542 void Sema::StartOpenMPClause(OpenMPClauseKind K) { 2543 DSAStack->setClauseParsingMode(K); 2544 } 2545 2546 void Sema::EndOpenMPClause() { 2547 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown); 2548 CleanupVarDeclMarking(); 2549 } 2550 2551 static std::pair<ValueDecl *, bool> 2552 getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc, 2553 SourceRange &ERange, bool AllowArraySection = false); 2554 2555 /// Check consistency of the reduction clauses. 2556 static void checkReductionClauses(Sema &S, DSAStackTy *Stack, 2557 ArrayRef<OMPClause *> Clauses) { 2558 bool InscanFound = false; 2559 SourceLocation InscanLoc; 2560 // OpenMP 5.0, 2.19.5.4 reduction Clause, Restrictions. 2561 // A reduction clause without the inscan reduction-modifier may not appear on 2562 // a construct on which a reduction clause with the inscan reduction-modifier 2563 // appears. 2564 for (OMPClause *C : Clauses) { 2565 if (C->getClauseKind() != OMPC_reduction) 2566 continue; 2567 auto *RC = cast<OMPReductionClause>(C); 2568 if (RC->getModifier() == OMPC_REDUCTION_inscan) { 2569 InscanFound = true; 2570 InscanLoc = RC->getModifierLoc(); 2571 continue; 2572 } 2573 if (RC->getModifier() == OMPC_REDUCTION_task) { 2574 // OpenMP 5.0, 2.19.5.4 reduction Clause. 2575 // A reduction clause with the task reduction-modifier may only appear on 2576 // a parallel construct, a worksharing construct or a combined or 2577 // composite construct for which any of the aforementioned constructs is a 2578 // constituent construct and simd or loop are not constituent constructs. 2579 OpenMPDirectiveKind CurDir = Stack->getCurrentDirective(); 2580 if (!(isOpenMPParallelDirective(CurDir) || 2581 isOpenMPWorksharingDirective(CurDir)) || 2582 isOpenMPSimdDirective(CurDir)) 2583 S.Diag(RC->getModifierLoc(), 2584 diag::err_omp_reduction_task_not_parallel_or_worksharing); 2585 continue; 2586 } 2587 } 2588 if (InscanFound) { 2589 for (OMPClause *C : Clauses) { 2590 if (C->getClauseKind() != OMPC_reduction) 2591 continue; 2592 auto *RC = cast<OMPReductionClause>(C); 2593 if (RC->getModifier() != OMPC_REDUCTION_inscan) { 2594 S.Diag(RC->getModifier() == OMPC_REDUCTION_unknown 2595 ? RC->getBeginLoc() 2596 : RC->getModifierLoc(), 2597 diag::err_omp_inscan_reduction_expected); 2598 S.Diag(InscanLoc, diag::note_omp_previous_inscan_reduction); 2599 continue; 2600 } 2601 for (Expr *Ref : RC->varlists()) { 2602 assert(Ref && "NULL expr in OpenMP nontemporal clause."); 2603 SourceLocation ELoc; 2604 SourceRange ERange; 2605 Expr *SimpleRefExpr = Ref; 2606 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange, 2607 /*AllowArraySection=*/true); 2608 ValueDecl *D = Res.first; 2609 if (!D) 2610 continue; 2611 if (!Stack->isUsedInScanDirective(getCanonicalDecl(D))) { 2612 S.Diag(Ref->getExprLoc(), 2613 diag::err_omp_reduction_not_inclusive_exclusive) 2614 << Ref->getSourceRange(); 2615 } 2616 } 2617 } 2618 } 2619 } 2620 2621 static void checkAllocateClauses(Sema &S, DSAStackTy *Stack, 2622 ArrayRef<OMPClause *> Clauses); 2623 static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr, 2624 bool WithInit); 2625 2626 static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack, 2627 const ValueDecl *D, 2628 const DSAStackTy::DSAVarData &DVar, 2629 bool IsLoopIterVar = false); 2630 2631 void Sema::EndOpenMPDSABlock(Stmt *CurDirective) { 2632 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1] 2633 // A variable of class type (or array thereof) that appears in a lastprivate 2634 // clause requires an accessible, unambiguous default constructor for the 2635 // class type, unless the list item is also specified in a firstprivate 2636 // clause. 2637 if (const auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) { 2638 for (OMPClause *C : D->clauses()) { 2639 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) { 2640 SmallVector<Expr *, 8> PrivateCopies; 2641 for (Expr *DE : Clause->varlists()) { 2642 if (DE->isValueDependent() || DE->isTypeDependent()) { 2643 PrivateCopies.push_back(nullptr); 2644 continue; 2645 } 2646 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens()); 2647 auto *VD = cast<VarDecl>(DRE->getDecl()); 2648 QualType Type = VD->getType().getNonReferenceType(); 2649 const DSAStackTy::DSAVarData DVar = 2650 DSAStack->getTopDSA(VD, /*FromParent=*/false); 2651 if (DVar.CKind == OMPC_lastprivate) { 2652 // Generate helper private variable and initialize it with the 2653 // default value. The address of the original variable is replaced 2654 // by the address of the new private variable in CodeGen. This new 2655 // variable is not added to IdResolver, so the code in the OpenMP 2656 // region uses original variable for proper diagnostics. 2657 VarDecl *VDPrivate = buildVarDecl( 2658 *this, DE->getExprLoc(), Type.getUnqualifiedType(), 2659 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr, DRE); 2660 ActOnUninitializedDecl(VDPrivate); 2661 if (VDPrivate->isInvalidDecl()) { 2662 PrivateCopies.push_back(nullptr); 2663 continue; 2664 } 2665 PrivateCopies.push_back(buildDeclRefExpr( 2666 *this, VDPrivate, DE->getType(), DE->getExprLoc())); 2667 } else { 2668 // The variable is also a firstprivate, so initialization sequence 2669 // for private copy is generated already. 2670 PrivateCopies.push_back(nullptr); 2671 } 2672 } 2673 Clause->setPrivateCopies(PrivateCopies); 2674 continue; 2675 } 2676 // Finalize nontemporal clause by handling private copies, if any. 2677 if (auto *Clause = dyn_cast<OMPNontemporalClause>(C)) { 2678 SmallVector<Expr *, 8> PrivateRefs; 2679 for (Expr *RefExpr : Clause->varlists()) { 2680 assert(RefExpr && "NULL expr in OpenMP nontemporal clause."); 2681 SourceLocation ELoc; 2682 SourceRange ERange; 2683 Expr *SimpleRefExpr = RefExpr; 2684 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 2685 if (Res.second) 2686 // It will be analyzed later. 2687 PrivateRefs.push_back(RefExpr); 2688 ValueDecl *D = Res.first; 2689 if (!D) 2690 continue; 2691 2692 const DSAStackTy::DSAVarData DVar = 2693 DSAStack->getTopDSA(D, /*FromParent=*/false); 2694 PrivateRefs.push_back(DVar.PrivateCopy ? DVar.PrivateCopy 2695 : SimpleRefExpr); 2696 } 2697 Clause->setPrivateRefs(PrivateRefs); 2698 continue; 2699 } 2700 if (auto *Clause = dyn_cast<OMPUsesAllocatorsClause>(C)) { 2701 for (unsigned I = 0, E = Clause->getNumberOfAllocators(); I < E; ++I) { 2702 OMPUsesAllocatorsClause::Data D = Clause->getAllocatorData(I); 2703 auto *DRE = dyn_cast<DeclRefExpr>(D.Allocator->IgnoreParenImpCasts()); 2704 if (!DRE) 2705 continue; 2706 ValueDecl *VD = DRE->getDecl(); 2707 if (!VD || !isa<VarDecl>(VD)) 2708 continue; 2709 DSAStackTy::DSAVarData DVar = 2710 DSAStack->getTopDSA(VD, /*FromParent=*/false); 2711 // OpenMP [2.12.5, target Construct] 2712 // Memory allocators that appear in a uses_allocators clause cannot 2713 // appear in other data-sharing attribute clauses or data-mapping 2714 // attribute clauses in the same construct. 2715 Expr *MapExpr = nullptr; 2716 if (DVar.RefExpr || 2717 DSAStack->checkMappableExprComponentListsForDecl( 2718 VD, /*CurrentRegionOnly=*/true, 2719 [VD, &MapExpr]( 2720 OMPClauseMappableExprCommon::MappableExprComponentListRef 2721 MapExprComponents, 2722 OpenMPClauseKind C) { 2723 auto MI = MapExprComponents.rbegin(); 2724 auto ME = MapExprComponents.rend(); 2725 if (MI != ME && 2726 MI->getAssociatedDeclaration()->getCanonicalDecl() == 2727 VD->getCanonicalDecl()) { 2728 MapExpr = MI->getAssociatedExpression(); 2729 return true; 2730 } 2731 return false; 2732 })) { 2733 Diag(D.Allocator->getExprLoc(), 2734 diag::err_omp_allocator_used_in_clauses) 2735 << D.Allocator->getSourceRange(); 2736 if (DVar.RefExpr) 2737 reportOriginalDsa(*this, DSAStack, VD, DVar); 2738 else 2739 Diag(MapExpr->getExprLoc(), diag::note_used_here) 2740 << MapExpr->getSourceRange(); 2741 } 2742 } 2743 continue; 2744 } 2745 } 2746 // Check allocate clauses. 2747 if (!CurContext->isDependentContext()) 2748 checkAllocateClauses(*this, DSAStack, D->clauses()); 2749 checkReductionClauses(*this, DSAStack, D->clauses()); 2750 } 2751 2752 DSAStack->pop(); 2753 DiscardCleanupsInEvaluationContext(); 2754 PopExpressionEvaluationContext(); 2755 } 2756 2757 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, 2758 Expr *NumIterations, Sema &SemaRef, 2759 Scope *S, DSAStackTy *Stack); 2760 2761 namespace { 2762 2763 class VarDeclFilterCCC final : public CorrectionCandidateCallback { 2764 private: 2765 Sema &SemaRef; 2766 2767 public: 2768 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {} 2769 bool ValidateCandidate(const TypoCorrection &Candidate) override { 2770 NamedDecl *ND = Candidate.getCorrectionDecl(); 2771 if (const auto *VD = dyn_cast_or_null<VarDecl>(ND)) { 2772 return VD->hasGlobalStorage() && 2773 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), 2774 SemaRef.getCurScope()); 2775 } 2776 return false; 2777 } 2778 2779 std::unique_ptr<CorrectionCandidateCallback> clone() override { 2780 return std::make_unique<VarDeclFilterCCC>(*this); 2781 } 2782 2783 }; 2784 2785 class VarOrFuncDeclFilterCCC final : public CorrectionCandidateCallback { 2786 private: 2787 Sema &SemaRef; 2788 2789 public: 2790 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {} 2791 bool ValidateCandidate(const TypoCorrection &Candidate) override { 2792 NamedDecl *ND = Candidate.getCorrectionDecl(); 2793 if (ND && ((isa<VarDecl>(ND) && ND->getKind() == Decl::Var) || 2794 isa<FunctionDecl>(ND))) { 2795 return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), 2796 SemaRef.getCurScope()); 2797 } 2798 return false; 2799 } 2800 2801 std::unique_ptr<CorrectionCandidateCallback> clone() override { 2802 return std::make_unique<VarOrFuncDeclFilterCCC>(*this); 2803 } 2804 }; 2805 2806 } // namespace 2807 2808 ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope, 2809 CXXScopeSpec &ScopeSpec, 2810 const DeclarationNameInfo &Id, 2811 OpenMPDirectiveKind Kind) { 2812 LookupResult Lookup(*this, Id, LookupOrdinaryName); 2813 LookupParsedName(Lookup, CurScope, &ScopeSpec, true); 2814 2815 if (Lookup.isAmbiguous()) 2816 return ExprError(); 2817 2818 VarDecl *VD; 2819 if (!Lookup.isSingleResult()) { 2820 VarDeclFilterCCC CCC(*this); 2821 if (TypoCorrection Corrected = 2822 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, CCC, 2823 CTK_ErrorRecovery)) { 2824 diagnoseTypo(Corrected, 2825 PDiag(Lookup.empty() 2826 ? diag::err_undeclared_var_use_suggest 2827 : diag::err_omp_expected_var_arg_suggest) 2828 << Id.getName()); 2829 VD = Corrected.getCorrectionDeclAs<VarDecl>(); 2830 } else { 2831 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use 2832 : diag::err_omp_expected_var_arg) 2833 << Id.getName(); 2834 return ExprError(); 2835 } 2836 } else if (!(VD = Lookup.getAsSingle<VarDecl>())) { 2837 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName(); 2838 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at); 2839 return ExprError(); 2840 } 2841 Lookup.suppressDiagnostics(); 2842 2843 // OpenMP [2.9.2, Syntax, C/C++] 2844 // Variables must be file-scope, namespace-scope, or static block-scope. 2845 if (Kind == OMPD_threadprivate && !VD->hasGlobalStorage()) { 2846 Diag(Id.getLoc(), diag::err_omp_global_var_arg) 2847 << getOpenMPDirectiveName(Kind) << !VD->isStaticLocal(); 2848 bool IsDecl = 2849 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2850 Diag(VD->getLocation(), 2851 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2852 << VD; 2853 return ExprError(); 2854 } 2855 2856 VarDecl *CanonicalVD = VD->getCanonicalDecl(); 2857 NamedDecl *ND = CanonicalVD; 2858 // OpenMP [2.9.2, Restrictions, C/C++, p.2] 2859 // A threadprivate directive for file-scope variables must appear outside 2860 // any definition or declaration. 2861 if (CanonicalVD->getDeclContext()->isTranslationUnit() && 2862 !getCurLexicalContext()->isTranslationUnit()) { 2863 Diag(Id.getLoc(), diag::err_omp_var_scope) 2864 << getOpenMPDirectiveName(Kind) << VD; 2865 bool IsDecl = 2866 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2867 Diag(VD->getLocation(), 2868 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2869 << VD; 2870 return ExprError(); 2871 } 2872 // OpenMP [2.9.2, Restrictions, C/C++, p.3] 2873 // A threadprivate directive for static class member variables must appear 2874 // in the class definition, in the same scope in which the member 2875 // variables are declared. 2876 if (CanonicalVD->isStaticDataMember() && 2877 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) { 2878 Diag(Id.getLoc(), diag::err_omp_var_scope) 2879 << getOpenMPDirectiveName(Kind) << VD; 2880 bool IsDecl = 2881 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2882 Diag(VD->getLocation(), 2883 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2884 << VD; 2885 return ExprError(); 2886 } 2887 // OpenMP [2.9.2, Restrictions, C/C++, p.4] 2888 // A threadprivate directive for namespace-scope variables must appear 2889 // outside any definition or declaration other than the namespace 2890 // definition itself. 2891 if (CanonicalVD->getDeclContext()->isNamespace() && 2892 (!getCurLexicalContext()->isFileContext() || 2893 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) { 2894 Diag(Id.getLoc(), diag::err_omp_var_scope) 2895 << getOpenMPDirectiveName(Kind) << VD; 2896 bool IsDecl = 2897 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2898 Diag(VD->getLocation(), 2899 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2900 << VD; 2901 return ExprError(); 2902 } 2903 // OpenMP [2.9.2, Restrictions, C/C++, p.6] 2904 // A threadprivate directive for static block-scope variables must appear 2905 // in the scope of the variable and not in a nested scope. 2906 if (CanonicalVD->isLocalVarDecl() && CurScope && 2907 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) { 2908 Diag(Id.getLoc(), diag::err_omp_var_scope) 2909 << getOpenMPDirectiveName(Kind) << VD; 2910 bool IsDecl = 2911 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2912 Diag(VD->getLocation(), 2913 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2914 << VD; 2915 return ExprError(); 2916 } 2917 2918 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6] 2919 // A threadprivate directive must lexically precede all references to any 2920 // of the variables in its list. 2921 if (Kind == OMPD_threadprivate && VD->isUsed() && 2922 !DSAStack->isThreadPrivate(VD)) { 2923 Diag(Id.getLoc(), diag::err_omp_var_used) 2924 << getOpenMPDirectiveName(Kind) << VD; 2925 return ExprError(); 2926 } 2927 2928 QualType ExprType = VD->getType().getNonReferenceType(); 2929 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(), 2930 SourceLocation(), VD, 2931 /*RefersToEnclosingVariableOrCapture=*/false, 2932 Id.getLoc(), ExprType, VK_LValue); 2933 } 2934 2935 Sema::DeclGroupPtrTy 2936 Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc, 2937 ArrayRef<Expr *> VarList) { 2938 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) { 2939 CurContext->addDecl(D); 2940 return DeclGroupPtrTy::make(DeclGroupRef(D)); 2941 } 2942 return nullptr; 2943 } 2944 2945 namespace { 2946 class LocalVarRefChecker final 2947 : public ConstStmtVisitor<LocalVarRefChecker, bool> { 2948 Sema &SemaRef; 2949 2950 public: 2951 bool VisitDeclRefExpr(const DeclRefExpr *E) { 2952 if (const auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 2953 if (VD->hasLocalStorage()) { 2954 SemaRef.Diag(E->getBeginLoc(), 2955 diag::err_omp_local_var_in_threadprivate_init) 2956 << E->getSourceRange(); 2957 SemaRef.Diag(VD->getLocation(), diag::note_defined_here) 2958 << VD << VD->getSourceRange(); 2959 return true; 2960 } 2961 } 2962 return false; 2963 } 2964 bool VisitStmt(const Stmt *S) { 2965 for (const Stmt *Child : S->children()) { 2966 if (Child && Visit(Child)) 2967 return true; 2968 } 2969 return false; 2970 } 2971 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {} 2972 }; 2973 } // namespace 2974 2975 OMPThreadPrivateDecl * 2976 Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) { 2977 SmallVector<Expr *, 8> Vars; 2978 for (Expr *RefExpr : VarList) { 2979 auto *DE = cast<DeclRefExpr>(RefExpr); 2980 auto *VD = cast<VarDecl>(DE->getDecl()); 2981 SourceLocation ILoc = DE->getExprLoc(); 2982 2983 // Mark variable as used. 2984 VD->setReferenced(); 2985 VD->markUsed(Context); 2986 2987 QualType QType = VD->getType(); 2988 if (QType->isDependentType() || QType->isInstantiationDependentType()) { 2989 // It will be analyzed later. 2990 Vars.push_back(DE); 2991 continue; 2992 } 2993 2994 // OpenMP [2.9.2, Restrictions, C/C++, p.10] 2995 // A threadprivate variable must not have an incomplete type. 2996 if (RequireCompleteType(ILoc, VD->getType(), 2997 diag::err_omp_threadprivate_incomplete_type)) { 2998 continue; 2999 } 3000 3001 // OpenMP [2.9.2, Restrictions, C/C++, p.10] 3002 // A threadprivate variable must not have a reference type. 3003 if (VD->getType()->isReferenceType()) { 3004 Diag(ILoc, diag::err_omp_ref_type_arg) 3005 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType(); 3006 bool IsDecl = 3007 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 3008 Diag(VD->getLocation(), 3009 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 3010 << VD; 3011 continue; 3012 } 3013 3014 // Check if this is a TLS variable. If TLS is not being supported, produce 3015 // the corresponding diagnostic. 3016 if ((VD->getTLSKind() != VarDecl::TLS_None && 3017 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() && 3018 getLangOpts().OpenMPUseTLS && 3019 getASTContext().getTargetInfo().isTLSSupported())) || 3020 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() && 3021 !VD->isLocalVarDecl())) { 3022 Diag(ILoc, diag::err_omp_var_thread_local) 3023 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1); 3024 bool IsDecl = 3025 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 3026 Diag(VD->getLocation(), 3027 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 3028 << VD; 3029 continue; 3030 } 3031 3032 // Check if initial value of threadprivate variable reference variable with 3033 // local storage (it is not supported by runtime). 3034 if (const Expr *Init = VD->getAnyInitializer()) { 3035 LocalVarRefChecker Checker(*this); 3036 if (Checker.Visit(Init)) 3037 continue; 3038 } 3039 3040 Vars.push_back(RefExpr); 3041 DSAStack->addDSA(VD, DE, OMPC_threadprivate); 3042 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit( 3043 Context, SourceRange(Loc, Loc))); 3044 if (ASTMutationListener *ML = Context.getASTMutationListener()) 3045 ML->DeclarationMarkedOpenMPThreadPrivate(VD); 3046 } 3047 OMPThreadPrivateDecl *D = nullptr; 3048 if (!Vars.empty()) { 3049 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc, 3050 Vars); 3051 D->setAccess(AS_public); 3052 } 3053 return D; 3054 } 3055 3056 static OMPAllocateDeclAttr::AllocatorTypeTy 3057 getAllocatorKind(Sema &S, DSAStackTy *Stack, Expr *Allocator) { 3058 if (!Allocator) 3059 return OMPAllocateDeclAttr::OMPNullMemAlloc; 3060 if (Allocator->isTypeDependent() || Allocator->isValueDependent() || 3061 Allocator->isInstantiationDependent() || 3062 Allocator->containsUnexpandedParameterPack()) 3063 return OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; 3064 auto AllocatorKindRes = OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; 3065 const Expr *AE = Allocator->IgnoreParenImpCasts(); 3066 for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) { 3067 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I); 3068 const Expr *DefAllocator = Stack->getAllocator(AllocatorKind); 3069 llvm::FoldingSetNodeID AEId, DAEId; 3070 AE->Profile(AEId, S.getASTContext(), /*Canonical=*/true); 3071 DefAllocator->Profile(DAEId, S.getASTContext(), /*Canonical=*/true); 3072 if (AEId == DAEId) { 3073 AllocatorKindRes = AllocatorKind; 3074 break; 3075 } 3076 } 3077 return AllocatorKindRes; 3078 } 3079 3080 static bool checkPreviousOMPAllocateAttribute( 3081 Sema &S, DSAStackTy *Stack, Expr *RefExpr, VarDecl *VD, 3082 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, Expr *Allocator) { 3083 if (!VD->hasAttr<OMPAllocateDeclAttr>()) 3084 return false; 3085 const auto *A = VD->getAttr<OMPAllocateDeclAttr>(); 3086 Expr *PrevAllocator = A->getAllocator(); 3087 OMPAllocateDeclAttr::AllocatorTypeTy PrevAllocatorKind = 3088 getAllocatorKind(S, Stack, PrevAllocator); 3089 bool AllocatorsMatch = AllocatorKind == PrevAllocatorKind; 3090 if (AllocatorsMatch && 3091 AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc && 3092 Allocator && PrevAllocator) { 3093 const Expr *AE = Allocator->IgnoreParenImpCasts(); 3094 const Expr *PAE = PrevAllocator->IgnoreParenImpCasts(); 3095 llvm::FoldingSetNodeID AEId, PAEId; 3096 AE->Profile(AEId, S.Context, /*Canonical=*/true); 3097 PAE->Profile(PAEId, S.Context, /*Canonical=*/true); 3098 AllocatorsMatch = AEId == PAEId; 3099 } 3100 if (!AllocatorsMatch) { 3101 SmallString<256> AllocatorBuffer; 3102 llvm::raw_svector_ostream AllocatorStream(AllocatorBuffer); 3103 if (Allocator) 3104 Allocator->printPretty(AllocatorStream, nullptr, S.getPrintingPolicy()); 3105 SmallString<256> PrevAllocatorBuffer; 3106 llvm::raw_svector_ostream PrevAllocatorStream(PrevAllocatorBuffer); 3107 if (PrevAllocator) 3108 PrevAllocator->printPretty(PrevAllocatorStream, nullptr, 3109 S.getPrintingPolicy()); 3110 3111 SourceLocation AllocatorLoc = 3112 Allocator ? Allocator->getExprLoc() : RefExpr->getExprLoc(); 3113 SourceRange AllocatorRange = 3114 Allocator ? Allocator->getSourceRange() : RefExpr->getSourceRange(); 3115 SourceLocation PrevAllocatorLoc = 3116 PrevAllocator ? PrevAllocator->getExprLoc() : A->getLocation(); 3117 SourceRange PrevAllocatorRange = 3118 PrevAllocator ? PrevAllocator->getSourceRange() : A->getRange(); 3119 S.Diag(AllocatorLoc, diag::warn_omp_used_different_allocator) 3120 << (Allocator ? 1 : 0) << AllocatorStream.str() 3121 << (PrevAllocator ? 1 : 0) << PrevAllocatorStream.str() 3122 << AllocatorRange; 3123 S.Diag(PrevAllocatorLoc, diag::note_omp_previous_allocator) 3124 << PrevAllocatorRange; 3125 return true; 3126 } 3127 return false; 3128 } 3129 3130 static void 3131 applyOMPAllocateAttribute(Sema &S, VarDecl *VD, 3132 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, 3133 Expr *Allocator, SourceRange SR) { 3134 if (VD->hasAttr<OMPAllocateDeclAttr>()) 3135 return; 3136 if (Allocator && 3137 (Allocator->isTypeDependent() || Allocator->isValueDependent() || 3138 Allocator->isInstantiationDependent() || 3139 Allocator->containsUnexpandedParameterPack())) 3140 return; 3141 auto *A = OMPAllocateDeclAttr::CreateImplicit(S.Context, AllocatorKind, 3142 Allocator, SR); 3143 VD->addAttr(A); 3144 if (ASTMutationListener *ML = S.Context.getASTMutationListener()) 3145 ML->DeclarationMarkedOpenMPAllocate(VD, A); 3146 } 3147 3148 Sema::DeclGroupPtrTy Sema::ActOnOpenMPAllocateDirective( 3149 SourceLocation Loc, ArrayRef<Expr *> VarList, 3150 ArrayRef<OMPClause *> Clauses, DeclContext *Owner) { 3151 assert(Clauses.size() <= 1 && "Expected at most one clause."); 3152 Expr *Allocator = nullptr; 3153 if (Clauses.empty()) { 3154 // OpenMP 5.0, 2.11.3 allocate Directive, Restrictions. 3155 // allocate directives that appear in a target region must specify an 3156 // allocator clause unless a requires directive with the dynamic_allocators 3157 // clause is present in the same compilation unit. 3158 if (LangOpts.OpenMPIsDevice && 3159 !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>()) 3160 targetDiag(Loc, diag::err_expected_allocator_clause); 3161 } else { 3162 Allocator = cast<OMPAllocatorClause>(Clauses.back())->getAllocator(); 3163 } 3164 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind = 3165 getAllocatorKind(*this, DSAStack, Allocator); 3166 SmallVector<Expr *, 8> Vars; 3167 for (Expr *RefExpr : VarList) { 3168 auto *DE = cast<DeclRefExpr>(RefExpr); 3169 auto *VD = cast<VarDecl>(DE->getDecl()); 3170 3171 // Check if this is a TLS variable or global register. 3172 if (VD->getTLSKind() != VarDecl::TLS_None || 3173 VD->hasAttr<OMPThreadPrivateDeclAttr>() || 3174 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() && 3175 !VD->isLocalVarDecl())) 3176 continue; 3177 3178 // If the used several times in the allocate directive, the same allocator 3179 // must be used. 3180 if (checkPreviousOMPAllocateAttribute(*this, DSAStack, RefExpr, VD, 3181 AllocatorKind, Allocator)) 3182 continue; 3183 3184 // OpenMP, 2.11.3 allocate Directive, Restrictions, C / C++ 3185 // If a list item has a static storage type, the allocator expression in the 3186 // allocator clause must be a constant expression that evaluates to one of 3187 // the predefined memory allocator values. 3188 if (Allocator && VD->hasGlobalStorage()) { 3189 if (AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc) { 3190 Diag(Allocator->getExprLoc(), 3191 diag::err_omp_expected_predefined_allocator) 3192 << Allocator->getSourceRange(); 3193 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 3194 VarDecl::DeclarationOnly; 3195 Diag(VD->getLocation(), 3196 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 3197 << VD; 3198 continue; 3199 } 3200 } 3201 3202 Vars.push_back(RefExpr); 3203 applyOMPAllocateAttribute(*this, VD, AllocatorKind, Allocator, 3204 DE->getSourceRange()); 3205 } 3206 if (Vars.empty()) 3207 return nullptr; 3208 if (!Owner) 3209 Owner = getCurLexicalContext(); 3210 auto *D = OMPAllocateDecl::Create(Context, Owner, Loc, Vars, Clauses); 3211 D->setAccess(AS_public); 3212 Owner->addDecl(D); 3213 return DeclGroupPtrTy::make(DeclGroupRef(D)); 3214 } 3215 3216 Sema::DeclGroupPtrTy 3217 Sema::ActOnOpenMPRequiresDirective(SourceLocation Loc, 3218 ArrayRef<OMPClause *> ClauseList) { 3219 OMPRequiresDecl *D = nullptr; 3220 if (!CurContext->isFileContext()) { 3221 Diag(Loc, diag::err_omp_invalid_scope) << "requires"; 3222 } else { 3223 D = CheckOMPRequiresDecl(Loc, ClauseList); 3224 if (D) { 3225 CurContext->addDecl(D); 3226 DSAStack->addRequiresDecl(D); 3227 } 3228 } 3229 return DeclGroupPtrTy::make(DeclGroupRef(D)); 3230 } 3231 3232 void Sema::ActOnOpenMPAssumesDirective(SourceLocation Loc, 3233 OpenMPDirectiveKind DKind, 3234 ArrayRef<std::string> Assumptions, 3235 bool SkippedClauses) { 3236 if (!SkippedClauses && Assumptions.empty()) 3237 Diag(Loc, diag::err_omp_no_clause_for_directive) 3238 << llvm::omp::getAllAssumeClauseOptions() 3239 << llvm::omp::getOpenMPDirectiveName(DKind); 3240 3241 auto *AA = AssumptionAttr::Create(Context, llvm::join(Assumptions, ","), Loc); 3242 if (DKind == llvm::omp::Directive::OMPD_begin_assumes) { 3243 OMPAssumeScoped.push_back(AA); 3244 return; 3245 } 3246 3247 // Global assumes without assumption clauses are ignored. 3248 if (Assumptions.empty()) 3249 return; 3250 3251 assert(DKind == llvm::omp::Directive::OMPD_assumes && 3252 "Unexpected omp assumption directive!"); 3253 OMPAssumeGlobal.push_back(AA); 3254 3255 // The OMPAssumeGlobal scope above will take care of new declarations but 3256 // we also want to apply the assumption to existing ones, e.g., to 3257 // declarations in included headers. To this end, we traverse all existing 3258 // declaration contexts and annotate function declarations here. 3259 SmallVector<DeclContext *, 8> DeclContexts; 3260 auto *Ctx = CurContext; 3261 while (Ctx->getLexicalParent()) 3262 Ctx = Ctx->getLexicalParent(); 3263 DeclContexts.push_back(Ctx); 3264 while (!DeclContexts.empty()) { 3265 DeclContext *DC = DeclContexts.pop_back_val(); 3266 for (auto *SubDC : DC->decls()) { 3267 if (SubDC->isInvalidDecl()) 3268 continue; 3269 if (auto *CTD = dyn_cast<ClassTemplateDecl>(SubDC)) { 3270 DeclContexts.push_back(CTD->getTemplatedDecl()); 3271 for (auto *S : CTD->specializations()) 3272 DeclContexts.push_back(S); 3273 continue; 3274 } 3275 if (auto *DC = dyn_cast<DeclContext>(SubDC)) 3276 DeclContexts.push_back(DC); 3277 if (auto *F = dyn_cast<FunctionDecl>(SubDC)) { 3278 F->addAttr(AA); 3279 continue; 3280 } 3281 } 3282 } 3283 } 3284 3285 void Sema::ActOnOpenMPEndAssumesDirective() { 3286 assert(isInOpenMPAssumeScope() && "Not in OpenMP assumes scope!"); 3287 OMPAssumeScoped.pop_back(); 3288 } 3289 3290 OMPRequiresDecl *Sema::CheckOMPRequiresDecl(SourceLocation Loc, 3291 ArrayRef<OMPClause *> ClauseList) { 3292 /// For target specific clauses, the requires directive cannot be 3293 /// specified after the handling of any of the target regions in the 3294 /// current compilation unit. 3295 ArrayRef<SourceLocation> TargetLocations = 3296 DSAStack->getEncounteredTargetLocs(); 3297 SourceLocation AtomicLoc = DSAStack->getAtomicDirectiveLoc(); 3298 if (!TargetLocations.empty() || !AtomicLoc.isInvalid()) { 3299 for (const OMPClause *CNew : ClauseList) { 3300 // Check if any of the requires clauses affect target regions. 3301 if (isa<OMPUnifiedSharedMemoryClause>(CNew) || 3302 isa<OMPUnifiedAddressClause>(CNew) || 3303 isa<OMPReverseOffloadClause>(CNew) || 3304 isa<OMPDynamicAllocatorsClause>(CNew)) { 3305 Diag(Loc, diag::err_omp_directive_before_requires) 3306 << "target" << getOpenMPClauseName(CNew->getClauseKind()); 3307 for (SourceLocation TargetLoc : TargetLocations) { 3308 Diag(TargetLoc, diag::note_omp_requires_encountered_directive) 3309 << "target"; 3310 } 3311 } else if (!AtomicLoc.isInvalid() && 3312 isa<OMPAtomicDefaultMemOrderClause>(CNew)) { 3313 Diag(Loc, diag::err_omp_directive_before_requires) 3314 << "atomic" << getOpenMPClauseName(CNew->getClauseKind()); 3315 Diag(AtomicLoc, diag::note_omp_requires_encountered_directive) 3316 << "atomic"; 3317 } 3318 } 3319 } 3320 3321 if (!DSAStack->hasDuplicateRequiresClause(ClauseList)) 3322 return OMPRequiresDecl::Create(Context, getCurLexicalContext(), Loc, 3323 ClauseList); 3324 return nullptr; 3325 } 3326 3327 static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack, 3328 const ValueDecl *D, 3329 const DSAStackTy::DSAVarData &DVar, 3330 bool IsLoopIterVar) { 3331 if (DVar.RefExpr) { 3332 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa) 3333 << getOpenMPClauseName(DVar.CKind); 3334 return; 3335 } 3336 enum { 3337 PDSA_StaticMemberShared, 3338 PDSA_StaticLocalVarShared, 3339 PDSA_LoopIterVarPrivate, 3340 PDSA_LoopIterVarLinear, 3341 PDSA_LoopIterVarLastprivate, 3342 PDSA_ConstVarShared, 3343 PDSA_GlobalVarShared, 3344 PDSA_TaskVarFirstprivate, 3345 PDSA_LocalVarPrivate, 3346 PDSA_Implicit 3347 } Reason = PDSA_Implicit; 3348 bool ReportHint = false; 3349 auto ReportLoc = D->getLocation(); 3350 auto *VD = dyn_cast<VarDecl>(D); 3351 if (IsLoopIterVar) { 3352 if (DVar.CKind == OMPC_private) 3353 Reason = PDSA_LoopIterVarPrivate; 3354 else if (DVar.CKind == OMPC_lastprivate) 3355 Reason = PDSA_LoopIterVarLastprivate; 3356 else 3357 Reason = PDSA_LoopIterVarLinear; 3358 } else if (isOpenMPTaskingDirective(DVar.DKind) && 3359 DVar.CKind == OMPC_firstprivate) { 3360 Reason = PDSA_TaskVarFirstprivate; 3361 ReportLoc = DVar.ImplicitDSALoc; 3362 } else if (VD && VD->isStaticLocal()) 3363 Reason = PDSA_StaticLocalVarShared; 3364 else if (VD && VD->isStaticDataMember()) 3365 Reason = PDSA_StaticMemberShared; 3366 else if (VD && VD->isFileVarDecl()) 3367 Reason = PDSA_GlobalVarShared; 3368 else if (D->getType().isConstant(SemaRef.getASTContext())) 3369 Reason = PDSA_ConstVarShared; 3370 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) { 3371 ReportHint = true; 3372 Reason = PDSA_LocalVarPrivate; 3373 } 3374 if (Reason != PDSA_Implicit) { 3375 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa) 3376 << Reason << ReportHint 3377 << getOpenMPDirectiveName(Stack->getCurrentDirective()); 3378 } else if (DVar.ImplicitDSALoc.isValid()) { 3379 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa) 3380 << getOpenMPClauseName(DVar.CKind); 3381 } 3382 } 3383 3384 static OpenMPMapClauseKind 3385 getMapClauseKindFromModifier(OpenMPDefaultmapClauseModifier M, 3386 bool IsAggregateOrDeclareTarget) { 3387 OpenMPMapClauseKind Kind = OMPC_MAP_unknown; 3388 switch (M) { 3389 case OMPC_DEFAULTMAP_MODIFIER_alloc: 3390 Kind = OMPC_MAP_alloc; 3391 break; 3392 case OMPC_DEFAULTMAP_MODIFIER_to: 3393 Kind = OMPC_MAP_to; 3394 break; 3395 case OMPC_DEFAULTMAP_MODIFIER_from: 3396 Kind = OMPC_MAP_from; 3397 break; 3398 case OMPC_DEFAULTMAP_MODIFIER_tofrom: 3399 Kind = OMPC_MAP_tofrom; 3400 break; 3401 case OMPC_DEFAULTMAP_MODIFIER_present: 3402 // OpenMP 5.1 [2.21.7.3] defaultmap clause, Description] 3403 // If implicit-behavior is present, each variable referenced in the 3404 // construct in the category specified by variable-category is treated as if 3405 // it had been listed in a map clause with the map-type of alloc and 3406 // map-type-modifier of present. 3407 Kind = OMPC_MAP_alloc; 3408 break; 3409 case OMPC_DEFAULTMAP_MODIFIER_firstprivate: 3410 case OMPC_DEFAULTMAP_MODIFIER_last: 3411 llvm_unreachable("Unexpected defaultmap implicit behavior"); 3412 case OMPC_DEFAULTMAP_MODIFIER_none: 3413 case OMPC_DEFAULTMAP_MODIFIER_default: 3414 case OMPC_DEFAULTMAP_MODIFIER_unknown: 3415 // IsAggregateOrDeclareTarget could be true if: 3416 // 1. the implicit behavior for aggregate is tofrom 3417 // 2. it's a declare target link 3418 if (IsAggregateOrDeclareTarget) { 3419 Kind = OMPC_MAP_tofrom; 3420 break; 3421 } 3422 llvm_unreachable("Unexpected defaultmap implicit behavior"); 3423 } 3424 assert(Kind != OMPC_MAP_unknown && "Expect map kind to be known"); 3425 return Kind; 3426 } 3427 3428 namespace { 3429 class DSAAttrChecker final : public StmtVisitor<DSAAttrChecker, void> { 3430 DSAStackTy *Stack; 3431 Sema &SemaRef; 3432 bool ErrorFound = false; 3433 bool TryCaptureCXXThisMembers = false; 3434 CapturedStmt *CS = nullptr; 3435 const static unsigned DefaultmapKindNum = OMPC_DEFAULTMAP_pointer + 1; 3436 llvm::SmallVector<Expr *, 4> ImplicitFirstprivate; 3437 llvm::SmallVector<Expr *, 4> ImplicitMap[DefaultmapKindNum][OMPC_MAP_delete]; 3438 llvm::SmallVector<OpenMPMapModifierKind, NumberOfOMPMapClauseModifiers> 3439 ImplicitMapModifier[DefaultmapKindNum]; 3440 Sema::VarsWithInheritedDSAType VarsWithInheritedDSA; 3441 llvm::SmallDenseSet<const ValueDecl *, 4> ImplicitDeclarations; 3442 3443 void VisitSubCaptures(OMPExecutableDirective *S) { 3444 // Check implicitly captured variables. 3445 if (!S->hasAssociatedStmt() || !S->getAssociatedStmt()) 3446 return; 3447 if (S->getDirectiveKind() == OMPD_atomic || 3448 S->getDirectiveKind() == OMPD_critical || 3449 S->getDirectiveKind() == OMPD_section || 3450 S->getDirectiveKind() == OMPD_master || 3451 S->getDirectiveKind() == OMPD_masked || 3452 isOpenMPLoopTransformationDirective(S->getDirectiveKind())) { 3453 Visit(S->getAssociatedStmt()); 3454 return; 3455 } 3456 visitSubCaptures(S->getInnermostCapturedStmt()); 3457 // Try to capture inner this->member references to generate correct mappings 3458 // and diagnostics. 3459 if (TryCaptureCXXThisMembers || 3460 (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) && 3461 llvm::any_of(S->getInnermostCapturedStmt()->captures(), 3462 [](const CapturedStmt::Capture &C) { 3463 return C.capturesThis(); 3464 }))) { 3465 bool SavedTryCaptureCXXThisMembers = TryCaptureCXXThisMembers; 3466 TryCaptureCXXThisMembers = true; 3467 Visit(S->getInnermostCapturedStmt()->getCapturedStmt()); 3468 TryCaptureCXXThisMembers = SavedTryCaptureCXXThisMembers; 3469 } 3470 // In tasks firstprivates are not captured anymore, need to analyze them 3471 // explicitly. 3472 if (isOpenMPTaskingDirective(S->getDirectiveKind()) && 3473 !isOpenMPTaskLoopDirective(S->getDirectiveKind())) { 3474 for (OMPClause *C : S->clauses()) 3475 if (auto *FC = dyn_cast<OMPFirstprivateClause>(C)) { 3476 for (Expr *Ref : FC->varlists()) 3477 Visit(Ref); 3478 } 3479 } 3480 } 3481 3482 public: 3483 void VisitDeclRefExpr(DeclRefExpr *E) { 3484 if (TryCaptureCXXThisMembers || E->isTypeDependent() || 3485 E->isValueDependent() || E->containsUnexpandedParameterPack() || 3486 E->isInstantiationDependent()) 3487 return; 3488 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 3489 // Check the datasharing rules for the expressions in the clauses. 3490 if (!CS) { 3491 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD)) 3492 if (!CED->hasAttr<OMPCaptureNoInitAttr>()) { 3493 Visit(CED->getInit()); 3494 return; 3495 } 3496 } else if (VD->isImplicit() || isa<OMPCapturedExprDecl>(VD)) 3497 // Do not analyze internal variables and do not enclose them into 3498 // implicit clauses. 3499 return; 3500 VD = VD->getCanonicalDecl(); 3501 // Skip internally declared variables. 3502 if (VD->hasLocalStorage() && CS && !CS->capturesVariable(VD) && 3503 !Stack->isImplicitTaskFirstprivate(VD)) 3504 return; 3505 // Skip allocators in uses_allocators clauses. 3506 if (Stack->isUsesAllocatorsDecl(VD).hasValue()) 3507 return; 3508 3509 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false); 3510 // Check if the variable has explicit DSA set and stop analysis if it so. 3511 if (DVar.RefExpr || !ImplicitDeclarations.insert(VD).second) 3512 return; 3513 3514 // Skip internally declared static variables. 3515 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 3516 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); 3517 if (VD->hasGlobalStorage() && CS && !CS->capturesVariable(VD) && 3518 (Stack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() || 3519 !Res || *Res != OMPDeclareTargetDeclAttr::MT_Link) && 3520 !Stack->isImplicitTaskFirstprivate(VD)) 3521 return; 3522 3523 SourceLocation ELoc = E->getExprLoc(); 3524 OpenMPDirectiveKind DKind = Stack->getCurrentDirective(); 3525 // The default(none) clause requires that each variable that is referenced 3526 // in the construct, and does not have a predetermined data-sharing 3527 // attribute, must have its data-sharing attribute explicitly determined 3528 // by being listed in a data-sharing attribute clause. 3529 if (DVar.CKind == OMPC_unknown && 3530 (Stack->getDefaultDSA() == DSA_none || 3531 Stack->getDefaultDSA() == DSA_firstprivate) && 3532 isImplicitOrExplicitTaskingRegion(DKind) && 3533 VarsWithInheritedDSA.count(VD) == 0) { 3534 bool InheritedDSA = Stack->getDefaultDSA() == DSA_none; 3535 if (!InheritedDSA && Stack->getDefaultDSA() == DSA_firstprivate) { 3536 DSAStackTy::DSAVarData DVar = 3537 Stack->getImplicitDSA(VD, /*FromParent=*/false); 3538 InheritedDSA = DVar.CKind == OMPC_unknown; 3539 } 3540 if (InheritedDSA) 3541 VarsWithInheritedDSA[VD] = E; 3542 return; 3543 } 3544 3545 // OpenMP 5.0 [2.19.7.2, defaultmap clause, Description] 3546 // If implicit-behavior is none, each variable referenced in the 3547 // construct that does not have a predetermined data-sharing attribute 3548 // and does not appear in a to or link clause on a declare target 3549 // directive must be listed in a data-mapping attribute clause, a 3550 // data-haring attribute clause (including a data-sharing attribute 3551 // clause on a combined construct where target. is one of the 3552 // constituent constructs), or an is_device_ptr clause. 3553 OpenMPDefaultmapClauseKind ClauseKind = 3554 getVariableCategoryFromDecl(SemaRef.getLangOpts(), VD); 3555 if (SemaRef.getLangOpts().OpenMP >= 50) { 3556 bool IsModifierNone = Stack->getDefaultmapModifier(ClauseKind) == 3557 OMPC_DEFAULTMAP_MODIFIER_none; 3558 if (DVar.CKind == OMPC_unknown && IsModifierNone && 3559 VarsWithInheritedDSA.count(VD) == 0 && !Res) { 3560 // Only check for data-mapping attribute and is_device_ptr here 3561 // since we have already make sure that the declaration does not 3562 // have a data-sharing attribute above 3563 if (!Stack->checkMappableExprComponentListsForDecl( 3564 VD, /*CurrentRegionOnly=*/true, 3565 [VD](OMPClauseMappableExprCommon::MappableExprComponentListRef 3566 MapExprComponents, 3567 OpenMPClauseKind) { 3568 auto MI = MapExprComponents.rbegin(); 3569 auto ME = MapExprComponents.rend(); 3570 return MI != ME && MI->getAssociatedDeclaration() == VD; 3571 })) { 3572 VarsWithInheritedDSA[VD] = E; 3573 return; 3574 } 3575 } 3576 } 3577 if (SemaRef.getLangOpts().OpenMP > 50) { 3578 bool IsModifierPresent = Stack->getDefaultmapModifier(ClauseKind) == 3579 OMPC_DEFAULTMAP_MODIFIER_present; 3580 if (IsModifierPresent) { 3581 if (llvm::find(ImplicitMapModifier[ClauseKind], 3582 OMPC_MAP_MODIFIER_present) == 3583 std::end(ImplicitMapModifier[ClauseKind])) { 3584 ImplicitMapModifier[ClauseKind].push_back( 3585 OMPC_MAP_MODIFIER_present); 3586 } 3587 } 3588 } 3589 3590 if (isOpenMPTargetExecutionDirective(DKind) && 3591 !Stack->isLoopControlVariable(VD).first) { 3592 if (!Stack->checkMappableExprComponentListsForDecl( 3593 VD, /*CurrentRegionOnly=*/true, 3594 [this](OMPClauseMappableExprCommon::MappableExprComponentListRef 3595 StackComponents, 3596 OpenMPClauseKind) { 3597 if (SemaRef.LangOpts.OpenMP >= 50) 3598 return !StackComponents.empty(); 3599 // Variable is used if it has been marked as an array, array 3600 // section, array shaping or the variable iself. 3601 return StackComponents.size() == 1 || 3602 std::all_of( 3603 std::next(StackComponents.rbegin()), 3604 StackComponents.rend(), 3605 [](const OMPClauseMappableExprCommon:: 3606 MappableComponent &MC) { 3607 return MC.getAssociatedDeclaration() == 3608 nullptr && 3609 (isa<OMPArraySectionExpr>( 3610 MC.getAssociatedExpression()) || 3611 isa<OMPArrayShapingExpr>( 3612 MC.getAssociatedExpression()) || 3613 isa<ArraySubscriptExpr>( 3614 MC.getAssociatedExpression())); 3615 }); 3616 })) { 3617 bool IsFirstprivate = false; 3618 // By default lambdas are captured as firstprivates. 3619 if (const auto *RD = 3620 VD->getType().getNonReferenceType()->getAsCXXRecordDecl()) 3621 IsFirstprivate = RD->isLambda(); 3622 IsFirstprivate = 3623 IsFirstprivate || (Stack->mustBeFirstprivate(ClauseKind) && !Res); 3624 if (IsFirstprivate) { 3625 ImplicitFirstprivate.emplace_back(E); 3626 } else { 3627 OpenMPDefaultmapClauseModifier M = 3628 Stack->getDefaultmapModifier(ClauseKind); 3629 OpenMPMapClauseKind Kind = getMapClauseKindFromModifier( 3630 M, ClauseKind == OMPC_DEFAULTMAP_aggregate || Res); 3631 ImplicitMap[ClauseKind][Kind].emplace_back(E); 3632 } 3633 return; 3634 } 3635 } 3636 3637 // OpenMP [2.9.3.6, Restrictions, p.2] 3638 // A list item that appears in a reduction clause of the innermost 3639 // enclosing worksharing or parallel construct may not be accessed in an 3640 // explicit task. 3641 DVar = Stack->hasInnermostDSA( 3642 VD, 3643 [](OpenMPClauseKind C, bool AppliedToPointee) { 3644 return C == OMPC_reduction && !AppliedToPointee; 3645 }, 3646 [](OpenMPDirectiveKind K) { 3647 return isOpenMPParallelDirective(K) || 3648 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K); 3649 }, 3650 /*FromParent=*/true); 3651 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) { 3652 ErrorFound = true; 3653 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); 3654 reportOriginalDsa(SemaRef, Stack, VD, DVar); 3655 return; 3656 } 3657 3658 // Define implicit data-sharing attributes for task. 3659 DVar = Stack->getImplicitDSA(VD, /*FromParent=*/false); 3660 if (((isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared) || 3661 (Stack->getDefaultDSA() == DSA_firstprivate && 3662 DVar.CKind == OMPC_firstprivate && !DVar.RefExpr)) && 3663 !Stack->isLoopControlVariable(VD).first) { 3664 ImplicitFirstprivate.push_back(E); 3665 return; 3666 } 3667 3668 // Store implicitly used globals with declare target link for parent 3669 // target. 3670 if (!isOpenMPTargetExecutionDirective(DKind) && Res && 3671 *Res == OMPDeclareTargetDeclAttr::MT_Link) { 3672 Stack->addToParentTargetRegionLinkGlobals(E); 3673 return; 3674 } 3675 } 3676 } 3677 void VisitMemberExpr(MemberExpr *E) { 3678 if (E->isTypeDependent() || E->isValueDependent() || 3679 E->containsUnexpandedParameterPack() || E->isInstantiationDependent()) 3680 return; 3681 auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); 3682 OpenMPDirectiveKind DKind = Stack->getCurrentDirective(); 3683 if (auto *TE = dyn_cast<CXXThisExpr>(E->getBase()->IgnoreParenCasts())) { 3684 if (!FD) 3685 return; 3686 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(FD, /*FromParent=*/false); 3687 // Check if the variable has explicit DSA set and stop analysis if it 3688 // so. 3689 if (DVar.RefExpr || !ImplicitDeclarations.insert(FD).second) 3690 return; 3691 3692 if (isOpenMPTargetExecutionDirective(DKind) && 3693 !Stack->isLoopControlVariable(FD).first && 3694 !Stack->checkMappableExprComponentListsForDecl( 3695 FD, /*CurrentRegionOnly=*/true, 3696 [](OMPClauseMappableExprCommon::MappableExprComponentListRef 3697 StackComponents, 3698 OpenMPClauseKind) { 3699 return isa<CXXThisExpr>( 3700 cast<MemberExpr>( 3701 StackComponents.back().getAssociatedExpression()) 3702 ->getBase() 3703 ->IgnoreParens()); 3704 })) { 3705 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3] 3706 // A bit-field cannot appear in a map clause. 3707 // 3708 if (FD->isBitField()) 3709 return; 3710 3711 // Check to see if the member expression is referencing a class that 3712 // has already been explicitly mapped 3713 if (Stack->isClassPreviouslyMapped(TE->getType())) 3714 return; 3715 3716 OpenMPDefaultmapClauseModifier Modifier = 3717 Stack->getDefaultmapModifier(OMPC_DEFAULTMAP_aggregate); 3718 OpenMPDefaultmapClauseKind ClauseKind = 3719 getVariableCategoryFromDecl(SemaRef.getLangOpts(), FD); 3720 OpenMPMapClauseKind Kind = getMapClauseKindFromModifier( 3721 Modifier, /*IsAggregateOrDeclareTarget*/ true); 3722 ImplicitMap[ClauseKind][Kind].emplace_back(E); 3723 return; 3724 } 3725 3726 SourceLocation ELoc = E->getExprLoc(); 3727 // OpenMP [2.9.3.6, Restrictions, p.2] 3728 // A list item that appears in a reduction clause of the innermost 3729 // enclosing worksharing or parallel construct may not be accessed in 3730 // an explicit task. 3731 DVar = Stack->hasInnermostDSA( 3732 FD, 3733 [](OpenMPClauseKind C, bool AppliedToPointee) { 3734 return C == OMPC_reduction && !AppliedToPointee; 3735 }, 3736 [](OpenMPDirectiveKind K) { 3737 return isOpenMPParallelDirective(K) || 3738 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K); 3739 }, 3740 /*FromParent=*/true); 3741 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) { 3742 ErrorFound = true; 3743 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); 3744 reportOriginalDsa(SemaRef, Stack, FD, DVar); 3745 return; 3746 } 3747 3748 // Define implicit data-sharing attributes for task. 3749 DVar = Stack->getImplicitDSA(FD, /*FromParent=*/false); 3750 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared && 3751 !Stack->isLoopControlVariable(FD).first) { 3752 // Check if there is a captured expression for the current field in the 3753 // region. Do not mark it as firstprivate unless there is no captured 3754 // expression. 3755 // TODO: try to make it firstprivate. 3756 if (DVar.CKind != OMPC_unknown) 3757 ImplicitFirstprivate.push_back(E); 3758 } 3759 return; 3760 } 3761 if (isOpenMPTargetExecutionDirective(DKind)) { 3762 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents; 3763 if (!checkMapClauseExpressionBase(SemaRef, E, CurComponents, OMPC_map, 3764 Stack->getCurrentDirective(), 3765 /*NoDiagnose=*/true)) 3766 return; 3767 const auto *VD = cast<ValueDecl>( 3768 CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl()); 3769 if (!Stack->checkMappableExprComponentListsForDecl( 3770 VD, /*CurrentRegionOnly=*/true, 3771 [&CurComponents]( 3772 OMPClauseMappableExprCommon::MappableExprComponentListRef 3773 StackComponents, 3774 OpenMPClauseKind) { 3775 auto CCI = CurComponents.rbegin(); 3776 auto CCE = CurComponents.rend(); 3777 for (const auto &SC : llvm::reverse(StackComponents)) { 3778 // Do both expressions have the same kind? 3779 if (CCI->getAssociatedExpression()->getStmtClass() != 3780 SC.getAssociatedExpression()->getStmtClass()) 3781 if (!((isa<OMPArraySectionExpr>( 3782 SC.getAssociatedExpression()) || 3783 isa<OMPArrayShapingExpr>( 3784 SC.getAssociatedExpression())) && 3785 isa<ArraySubscriptExpr>( 3786 CCI->getAssociatedExpression()))) 3787 return false; 3788 3789 const Decl *CCD = CCI->getAssociatedDeclaration(); 3790 const Decl *SCD = SC.getAssociatedDeclaration(); 3791 CCD = CCD ? CCD->getCanonicalDecl() : nullptr; 3792 SCD = SCD ? SCD->getCanonicalDecl() : nullptr; 3793 if (SCD != CCD) 3794 return false; 3795 std::advance(CCI, 1); 3796 if (CCI == CCE) 3797 break; 3798 } 3799 return true; 3800 })) { 3801 Visit(E->getBase()); 3802 } 3803 } else if (!TryCaptureCXXThisMembers) { 3804 Visit(E->getBase()); 3805 } 3806 } 3807 void VisitOMPExecutableDirective(OMPExecutableDirective *S) { 3808 for (OMPClause *C : S->clauses()) { 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 VisitStmt(Stmt *S) { 3832 for (Stmt *C : S->children()) { 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 } 3840 3841 void visitSubCaptures(CapturedStmt *S) { 3842 for (const CapturedStmt::Capture &Cap : S->captures()) { 3843 if (!Cap.capturesVariable() && !Cap.capturesVariableByCopy()) 3844 continue; 3845 VarDecl *VD = Cap.getCapturedVar(); 3846 // Do not try to map the variable if it or its sub-component was mapped 3847 // already. 3848 if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) && 3849 Stack->checkMappableExprComponentListsForDecl( 3850 VD, /*CurrentRegionOnly=*/true, 3851 [](OMPClauseMappableExprCommon::MappableExprComponentListRef, 3852 OpenMPClauseKind) { return true; })) 3853 continue; 3854 DeclRefExpr *DRE = buildDeclRefExpr( 3855 SemaRef, VD, VD->getType().getNonLValueExprType(SemaRef.Context), 3856 Cap.getLocation(), /*RefersToCapture=*/true); 3857 Visit(DRE); 3858 } 3859 } 3860 bool isErrorFound() const { return ErrorFound; } 3861 ArrayRef<Expr *> getImplicitFirstprivate() const { 3862 return ImplicitFirstprivate; 3863 } 3864 ArrayRef<Expr *> getImplicitMap(OpenMPDefaultmapClauseKind DK, 3865 OpenMPMapClauseKind MK) const { 3866 return ImplicitMap[DK][MK]; 3867 } 3868 ArrayRef<OpenMPMapModifierKind> 3869 getImplicitMapModifier(OpenMPDefaultmapClauseKind Kind) const { 3870 return ImplicitMapModifier[Kind]; 3871 } 3872 const Sema::VarsWithInheritedDSAType &getVarsWithInheritedDSA() const { 3873 return VarsWithInheritedDSA; 3874 } 3875 3876 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS) 3877 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) { 3878 // Process declare target link variables for the target directives. 3879 if (isOpenMPTargetExecutionDirective(S->getCurrentDirective())) { 3880 for (DeclRefExpr *E : Stack->getLinkGlobals()) 3881 Visit(E); 3882 } 3883 } 3884 }; 3885 } // namespace 3886 3887 static void handleDeclareVariantConstructTrait(DSAStackTy *Stack, 3888 OpenMPDirectiveKind DKind, 3889 bool ScopeEntry) { 3890 SmallVector<llvm::omp::TraitProperty, 8> Traits; 3891 if (isOpenMPTargetExecutionDirective(DKind)) 3892 Traits.emplace_back(llvm::omp::TraitProperty::construct_target_target); 3893 if (isOpenMPTeamsDirective(DKind)) 3894 Traits.emplace_back(llvm::omp::TraitProperty::construct_teams_teams); 3895 if (isOpenMPParallelDirective(DKind)) 3896 Traits.emplace_back(llvm::omp::TraitProperty::construct_parallel_parallel); 3897 if (isOpenMPWorksharingDirective(DKind)) 3898 Traits.emplace_back(llvm::omp::TraitProperty::construct_for_for); 3899 if (isOpenMPSimdDirective(DKind)) 3900 Traits.emplace_back(llvm::omp::TraitProperty::construct_simd_simd); 3901 Stack->handleConstructTrait(Traits, ScopeEntry); 3902 } 3903 3904 void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) { 3905 switch (DKind) { 3906 case OMPD_parallel: 3907 case OMPD_parallel_for: 3908 case OMPD_parallel_for_simd: 3909 case OMPD_parallel_sections: 3910 case OMPD_parallel_master: 3911 case OMPD_teams: 3912 case OMPD_teams_distribute: 3913 case OMPD_teams_distribute_simd: { 3914 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3915 QualType KmpInt32PtrTy = 3916 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3917 Sema::CapturedParamNameType Params[] = { 3918 std::make_pair(".global_tid.", KmpInt32PtrTy), 3919 std::make_pair(".bound_tid.", KmpInt32PtrTy), 3920 std::make_pair(StringRef(), QualType()) // __context with shared vars 3921 }; 3922 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3923 Params); 3924 break; 3925 } 3926 case OMPD_target_teams: 3927 case OMPD_target_parallel: 3928 case OMPD_target_parallel_for: 3929 case OMPD_target_parallel_for_simd: 3930 case OMPD_target_teams_distribute: 3931 case OMPD_target_teams_distribute_simd: { 3932 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3933 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 3934 QualType KmpInt32PtrTy = 3935 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3936 QualType Args[] = {VoidPtrTy}; 3937 FunctionProtoType::ExtProtoInfo EPI; 3938 EPI.Variadic = true; 3939 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 3940 Sema::CapturedParamNameType Params[] = { 3941 std::make_pair(".global_tid.", KmpInt32Ty), 3942 std::make_pair(".part_id.", KmpInt32PtrTy), 3943 std::make_pair(".privates.", VoidPtrTy), 3944 std::make_pair( 3945 ".copy_fn.", 3946 Context.getPointerType(CopyFnType).withConst().withRestrict()), 3947 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 3948 std::make_pair(StringRef(), QualType()) // __context with shared vars 3949 }; 3950 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3951 Params, /*OpenMPCaptureLevel=*/0); 3952 // Mark this captured region as inlined, because we don't use outlined 3953 // function directly. 3954 getCurCapturedRegion()->TheCapturedDecl->addAttr( 3955 AlwaysInlineAttr::CreateImplicit( 3956 Context, {}, AttributeCommonInfo::AS_Keyword, 3957 AlwaysInlineAttr::Keyword_forceinline)); 3958 Sema::CapturedParamNameType ParamsTarget[] = { 3959 std::make_pair(StringRef(), QualType()) // __context with shared vars 3960 }; 3961 // Start a captured region for 'target' with no implicit parameters. 3962 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3963 ParamsTarget, /*OpenMPCaptureLevel=*/1); 3964 Sema::CapturedParamNameType ParamsTeamsOrParallel[] = { 3965 std::make_pair(".global_tid.", KmpInt32PtrTy), 3966 std::make_pair(".bound_tid.", KmpInt32PtrTy), 3967 std::make_pair(StringRef(), QualType()) // __context with shared vars 3968 }; 3969 // Start a captured region for 'teams' or 'parallel'. Both regions have 3970 // the same implicit parameters. 3971 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3972 ParamsTeamsOrParallel, /*OpenMPCaptureLevel=*/2); 3973 break; 3974 } 3975 case OMPD_target: 3976 case OMPD_target_simd: { 3977 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3978 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 3979 QualType KmpInt32PtrTy = 3980 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3981 QualType Args[] = {VoidPtrTy}; 3982 FunctionProtoType::ExtProtoInfo EPI; 3983 EPI.Variadic = true; 3984 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 3985 Sema::CapturedParamNameType Params[] = { 3986 std::make_pair(".global_tid.", KmpInt32Ty), 3987 std::make_pair(".part_id.", KmpInt32PtrTy), 3988 std::make_pair(".privates.", VoidPtrTy), 3989 std::make_pair( 3990 ".copy_fn.", 3991 Context.getPointerType(CopyFnType).withConst().withRestrict()), 3992 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 3993 std::make_pair(StringRef(), QualType()) // __context with shared vars 3994 }; 3995 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3996 Params, /*OpenMPCaptureLevel=*/0); 3997 // Mark this captured region as inlined, because we don't use outlined 3998 // function directly. 3999 getCurCapturedRegion()->TheCapturedDecl->addAttr( 4000 AlwaysInlineAttr::CreateImplicit( 4001 Context, {}, AttributeCommonInfo::AS_Keyword, 4002 AlwaysInlineAttr::Keyword_forceinline)); 4003 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4004 std::make_pair(StringRef(), QualType()), 4005 /*OpenMPCaptureLevel=*/1); 4006 break; 4007 } 4008 case OMPD_atomic: 4009 case OMPD_critical: 4010 case OMPD_section: 4011 case OMPD_master: 4012 case OMPD_masked: 4013 case OMPD_tile: 4014 case OMPD_unroll: 4015 break; 4016 case OMPD_simd: 4017 case OMPD_for: 4018 case OMPD_for_simd: 4019 case OMPD_sections: 4020 case OMPD_single: 4021 case OMPD_taskgroup: 4022 case OMPD_distribute: 4023 case OMPD_distribute_simd: 4024 case OMPD_ordered: 4025 case OMPD_target_data: 4026 case OMPD_dispatch: { 4027 Sema::CapturedParamNameType Params[] = { 4028 std::make_pair(StringRef(), QualType()) // __context with shared vars 4029 }; 4030 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4031 Params); 4032 break; 4033 } 4034 case OMPD_task: { 4035 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 4036 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 4037 QualType KmpInt32PtrTy = 4038 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4039 QualType Args[] = {VoidPtrTy}; 4040 FunctionProtoType::ExtProtoInfo EPI; 4041 EPI.Variadic = true; 4042 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 4043 Sema::CapturedParamNameType Params[] = { 4044 std::make_pair(".global_tid.", KmpInt32Ty), 4045 std::make_pair(".part_id.", KmpInt32PtrTy), 4046 std::make_pair(".privates.", VoidPtrTy), 4047 std::make_pair( 4048 ".copy_fn.", 4049 Context.getPointerType(CopyFnType).withConst().withRestrict()), 4050 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 4051 std::make_pair(StringRef(), QualType()) // __context with shared vars 4052 }; 4053 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4054 Params); 4055 // Mark this captured region as inlined, because we don't use outlined 4056 // function directly. 4057 getCurCapturedRegion()->TheCapturedDecl->addAttr( 4058 AlwaysInlineAttr::CreateImplicit( 4059 Context, {}, AttributeCommonInfo::AS_Keyword, 4060 AlwaysInlineAttr::Keyword_forceinline)); 4061 break; 4062 } 4063 case OMPD_taskloop: 4064 case OMPD_taskloop_simd: 4065 case OMPD_master_taskloop: 4066 case OMPD_master_taskloop_simd: { 4067 QualType KmpInt32Ty = 4068 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1) 4069 .withConst(); 4070 QualType KmpUInt64Ty = 4071 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0) 4072 .withConst(); 4073 QualType KmpInt64Ty = 4074 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1) 4075 .withConst(); 4076 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 4077 QualType KmpInt32PtrTy = 4078 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4079 QualType Args[] = {VoidPtrTy}; 4080 FunctionProtoType::ExtProtoInfo EPI; 4081 EPI.Variadic = true; 4082 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 4083 Sema::CapturedParamNameType Params[] = { 4084 std::make_pair(".global_tid.", KmpInt32Ty), 4085 std::make_pair(".part_id.", KmpInt32PtrTy), 4086 std::make_pair(".privates.", VoidPtrTy), 4087 std::make_pair( 4088 ".copy_fn.", 4089 Context.getPointerType(CopyFnType).withConst().withRestrict()), 4090 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 4091 std::make_pair(".lb.", KmpUInt64Ty), 4092 std::make_pair(".ub.", KmpUInt64Ty), 4093 std::make_pair(".st.", KmpInt64Ty), 4094 std::make_pair(".liter.", KmpInt32Ty), 4095 std::make_pair(".reductions.", VoidPtrTy), 4096 std::make_pair(StringRef(), QualType()) // __context with shared vars 4097 }; 4098 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4099 Params); 4100 // Mark this captured region as inlined, because we don't use outlined 4101 // function directly. 4102 getCurCapturedRegion()->TheCapturedDecl->addAttr( 4103 AlwaysInlineAttr::CreateImplicit( 4104 Context, {}, AttributeCommonInfo::AS_Keyword, 4105 AlwaysInlineAttr::Keyword_forceinline)); 4106 break; 4107 } 4108 case OMPD_parallel_master_taskloop: 4109 case OMPD_parallel_master_taskloop_simd: { 4110 QualType KmpInt32Ty = 4111 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1) 4112 .withConst(); 4113 QualType KmpUInt64Ty = 4114 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0) 4115 .withConst(); 4116 QualType KmpInt64Ty = 4117 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1) 4118 .withConst(); 4119 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 4120 QualType KmpInt32PtrTy = 4121 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4122 Sema::CapturedParamNameType ParamsParallel[] = { 4123 std::make_pair(".global_tid.", KmpInt32PtrTy), 4124 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4125 std::make_pair(StringRef(), QualType()) // __context with shared vars 4126 }; 4127 // Start a captured region for 'parallel'. 4128 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4129 ParamsParallel, /*OpenMPCaptureLevel=*/0); 4130 QualType Args[] = {VoidPtrTy}; 4131 FunctionProtoType::ExtProtoInfo EPI; 4132 EPI.Variadic = true; 4133 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 4134 Sema::CapturedParamNameType Params[] = { 4135 std::make_pair(".global_tid.", KmpInt32Ty), 4136 std::make_pair(".part_id.", KmpInt32PtrTy), 4137 std::make_pair(".privates.", VoidPtrTy), 4138 std::make_pair( 4139 ".copy_fn.", 4140 Context.getPointerType(CopyFnType).withConst().withRestrict()), 4141 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 4142 std::make_pair(".lb.", KmpUInt64Ty), 4143 std::make_pair(".ub.", KmpUInt64Ty), 4144 std::make_pair(".st.", KmpInt64Ty), 4145 std::make_pair(".liter.", KmpInt32Ty), 4146 std::make_pair(".reductions.", VoidPtrTy), 4147 std::make_pair(StringRef(), QualType()) // __context with shared vars 4148 }; 4149 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4150 Params, /*OpenMPCaptureLevel=*/1); 4151 // Mark this captured region as inlined, because we don't use outlined 4152 // function directly. 4153 getCurCapturedRegion()->TheCapturedDecl->addAttr( 4154 AlwaysInlineAttr::CreateImplicit( 4155 Context, {}, AttributeCommonInfo::AS_Keyword, 4156 AlwaysInlineAttr::Keyword_forceinline)); 4157 break; 4158 } 4159 case OMPD_distribute_parallel_for_simd: 4160 case OMPD_distribute_parallel_for: { 4161 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 4162 QualType KmpInt32PtrTy = 4163 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4164 Sema::CapturedParamNameType Params[] = { 4165 std::make_pair(".global_tid.", KmpInt32PtrTy), 4166 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4167 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 4168 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 4169 std::make_pair(StringRef(), QualType()) // __context with shared vars 4170 }; 4171 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4172 Params); 4173 break; 4174 } 4175 case OMPD_target_teams_distribute_parallel_for: 4176 case OMPD_target_teams_distribute_parallel_for_simd: { 4177 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 4178 QualType KmpInt32PtrTy = 4179 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4180 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 4181 4182 QualType Args[] = {VoidPtrTy}; 4183 FunctionProtoType::ExtProtoInfo EPI; 4184 EPI.Variadic = true; 4185 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 4186 Sema::CapturedParamNameType Params[] = { 4187 std::make_pair(".global_tid.", KmpInt32Ty), 4188 std::make_pair(".part_id.", KmpInt32PtrTy), 4189 std::make_pair(".privates.", VoidPtrTy), 4190 std::make_pair( 4191 ".copy_fn.", 4192 Context.getPointerType(CopyFnType).withConst().withRestrict()), 4193 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 4194 std::make_pair(StringRef(), QualType()) // __context with shared vars 4195 }; 4196 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4197 Params, /*OpenMPCaptureLevel=*/0); 4198 // Mark this captured region as inlined, because we don't use outlined 4199 // function directly. 4200 getCurCapturedRegion()->TheCapturedDecl->addAttr( 4201 AlwaysInlineAttr::CreateImplicit( 4202 Context, {}, AttributeCommonInfo::AS_Keyword, 4203 AlwaysInlineAttr::Keyword_forceinline)); 4204 Sema::CapturedParamNameType ParamsTarget[] = { 4205 std::make_pair(StringRef(), QualType()) // __context with shared vars 4206 }; 4207 // Start a captured region for 'target' with no implicit parameters. 4208 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4209 ParamsTarget, /*OpenMPCaptureLevel=*/1); 4210 4211 Sema::CapturedParamNameType ParamsTeams[] = { 4212 std::make_pair(".global_tid.", KmpInt32PtrTy), 4213 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4214 std::make_pair(StringRef(), QualType()) // __context with shared vars 4215 }; 4216 // Start a captured region for 'target' with no implicit parameters. 4217 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4218 ParamsTeams, /*OpenMPCaptureLevel=*/2); 4219 4220 Sema::CapturedParamNameType ParamsParallel[] = { 4221 std::make_pair(".global_tid.", KmpInt32PtrTy), 4222 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4223 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 4224 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 4225 std::make_pair(StringRef(), QualType()) // __context with shared vars 4226 }; 4227 // Start a captured region for 'teams' or 'parallel'. Both regions have 4228 // the same implicit parameters. 4229 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4230 ParamsParallel, /*OpenMPCaptureLevel=*/3); 4231 break; 4232 } 4233 4234 case OMPD_teams_distribute_parallel_for: 4235 case OMPD_teams_distribute_parallel_for_simd: { 4236 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 4237 QualType KmpInt32PtrTy = 4238 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4239 4240 Sema::CapturedParamNameType ParamsTeams[] = { 4241 std::make_pair(".global_tid.", KmpInt32PtrTy), 4242 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4243 std::make_pair(StringRef(), QualType()) // __context with shared vars 4244 }; 4245 // Start a captured region for 'target' with no implicit parameters. 4246 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4247 ParamsTeams, /*OpenMPCaptureLevel=*/0); 4248 4249 Sema::CapturedParamNameType ParamsParallel[] = { 4250 std::make_pair(".global_tid.", KmpInt32PtrTy), 4251 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4252 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 4253 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 4254 std::make_pair(StringRef(), QualType()) // __context with shared vars 4255 }; 4256 // Start a captured region for 'teams' or 'parallel'. Both regions have 4257 // the same implicit parameters. 4258 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4259 ParamsParallel, /*OpenMPCaptureLevel=*/1); 4260 break; 4261 } 4262 case OMPD_target_update: 4263 case OMPD_target_enter_data: 4264 case OMPD_target_exit_data: { 4265 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 4266 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 4267 QualType KmpInt32PtrTy = 4268 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4269 QualType Args[] = {VoidPtrTy}; 4270 FunctionProtoType::ExtProtoInfo EPI; 4271 EPI.Variadic = true; 4272 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 4273 Sema::CapturedParamNameType Params[] = { 4274 std::make_pair(".global_tid.", KmpInt32Ty), 4275 std::make_pair(".part_id.", KmpInt32PtrTy), 4276 std::make_pair(".privates.", VoidPtrTy), 4277 std::make_pair( 4278 ".copy_fn.", 4279 Context.getPointerType(CopyFnType).withConst().withRestrict()), 4280 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 4281 std::make_pair(StringRef(), QualType()) // __context with shared vars 4282 }; 4283 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4284 Params); 4285 // Mark this captured region as inlined, because we don't use outlined 4286 // function directly. 4287 getCurCapturedRegion()->TheCapturedDecl->addAttr( 4288 AlwaysInlineAttr::CreateImplicit( 4289 Context, {}, AttributeCommonInfo::AS_Keyword, 4290 AlwaysInlineAttr::Keyword_forceinline)); 4291 break; 4292 } 4293 case OMPD_threadprivate: 4294 case OMPD_allocate: 4295 case OMPD_taskyield: 4296 case OMPD_barrier: 4297 case OMPD_taskwait: 4298 case OMPD_cancellation_point: 4299 case OMPD_cancel: 4300 case OMPD_flush: 4301 case OMPD_depobj: 4302 case OMPD_scan: 4303 case OMPD_declare_reduction: 4304 case OMPD_declare_mapper: 4305 case OMPD_declare_simd: 4306 case OMPD_declare_target: 4307 case OMPD_end_declare_target: 4308 case OMPD_requires: 4309 case OMPD_declare_variant: 4310 case OMPD_begin_declare_variant: 4311 case OMPD_end_declare_variant: 4312 case OMPD_metadirective: 4313 llvm_unreachable("OpenMP Directive is not allowed"); 4314 case OMPD_unknown: 4315 default: 4316 llvm_unreachable("Unknown OpenMP directive"); 4317 } 4318 DSAStack->setContext(CurContext); 4319 handleDeclareVariantConstructTrait(DSAStack, DKind, /* ScopeEntry */ true); 4320 } 4321 4322 int Sema::getNumberOfConstructScopes(unsigned Level) const { 4323 return getOpenMPCaptureLevels(DSAStack->getDirective(Level)); 4324 } 4325 4326 int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) { 4327 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 4328 getOpenMPCaptureRegions(CaptureRegions, DKind); 4329 return CaptureRegions.size(); 4330 } 4331 4332 static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id, 4333 Expr *CaptureExpr, bool WithInit, 4334 bool AsExpression) { 4335 assert(CaptureExpr); 4336 ASTContext &C = S.getASTContext(); 4337 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts(); 4338 QualType Ty = Init->getType(); 4339 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) { 4340 if (S.getLangOpts().CPlusPlus) { 4341 Ty = C.getLValueReferenceType(Ty); 4342 } else { 4343 Ty = C.getPointerType(Ty); 4344 ExprResult Res = 4345 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init); 4346 if (!Res.isUsable()) 4347 return nullptr; 4348 Init = Res.get(); 4349 } 4350 WithInit = true; 4351 } 4352 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty, 4353 CaptureExpr->getBeginLoc()); 4354 if (!WithInit) 4355 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C)); 4356 S.CurContext->addHiddenDecl(CED); 4357 Sema::TentativeAnalysisScope Trap(S); 4358 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false); 4359 return CED; 4360 } 4361 4362 static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr, 4363 bool WithInit) { 4364 OMPCapturedExprDecl *CD; 4365 if (VarDecl *VD = S.isOpenMPCapturedDecl(D)) 4366 CD = cast<OMPCapturedExprDecl>(VD); 4367 else 4368 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit, 4369 /*AsExpression=*/false); 4370 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(), 4371 CaptureExpr->getExprLoc()); 4372 } 4373 4374 static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) { 4375 CaptureExpr = S.DefaultLvalueConversion(CaptureExpr).get(); 4376 if (!Ref) { 4377 OMPCapturedExprDecl *CD = buildCaptureDecl( 4378 S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr, 4379 /*WithInit=*/true, /*AsExpression=*/true); 4380 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(), 4381 CaptureExpr->getExprLoc()); 4382 } 4383 ExprResult Res = Ref; 4384 if (!S.getLangOpts().CPlusPlus && 4385 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() && 4386 Ref->getType()->isPointerType()) { 4387 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref); 4388 if (!Res.isUsable()) 4389 return ExprError(); 4390 } 4391 return S.DefaultLvalueConversion(Res.get()); 4392 } 4393 4394 namespace { 4395 // OpenMP directives parsed in this section are represented as a 4396 // CapturedStatement with an associated statement. If a syntax error 4397 // is detected during the parsing of the associated statement, the 4398 // compiler must abort processing and close the CapturedStatement. 4399 // 4400 // Combined directives such as 'target parallel' have more than one 4401 // nested CapturedStatements. This RAII ensures that we unwind out 4402 // of all the nested CapturedStatements when an error is found. 4403 class CaptureRegionUnwinderRAII { 4404 private: 4405 Sema &S; 4406 bool &ErrorFound; 4407 OpenMPDirectiveKind DKind = OMPD_unknown; 4408 4409 public: 4410 CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound, 4411 OpenMPDirectiveKind DKind) 4412 : S(S), ErrorFound(ErrorFound), DKind(DKind) {} 4413 ~CaptureRegionUnwinderRAII() { 4414 if (ErrorFound) { 4415 int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind); 4416 while (--ThisCaptureLevel >= 0) 4417 S.ActOnCapturedRegionError(); 4418 } 4419 } 4420 }; 4421 } // namespace 4422 4423 void Sema::tryCaptureOpenMPLambdas(ValueDecl *V) { 4424 // Capture variables captured by reference in lambdas for target-based 4425 // directives. 4426 if (!CurContext->isDependentContext() && 4427 (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) || 4428 isOpenMPTargetDataManagementDirective( 4429 DSAStack->getCurrentDirective()))) { 4430 QualType Type = V->getType(); 4431 if (const auto *RD = Type.getCanonicalType() 4432 .getNonReferenceType() 4433 ->getAsCXXRecordDecl()) { 4434 bool SavedForceCaptureByReferenceInTargetExecutable = 4435 DSAStack->isForceCaptureByReferenceInTargetExecutable(); 4436 DSAStack->setForceCaptureByReferenceInTargetExecutable( 4437 /*V=*/true); 4438 if (RD->isLambda()) { 4439 llvm::DenseMap<const VarDecl *, FieldDecl *> Captures; 4440 FieldDecl *ThisCapture; 4441 RD->getCaptureFields(Captures, ThisCapture); 4442 for (const LambdaCapture &LC : RD->captures()) { 4443 if (LC.getCaptureKind() == LCK_ByRef) { 4444 VarDecl *VD = LC.getCapturedVar(); 4445 DeclContext *VDC = VD->getDeclContext(); 4446 if (!VDC->Encloses(CurContext)) 4447 continue; 4448 MarkVariableReferenced(LC.getLocation(), VD); 4449 } else if (LC.getCaptureKind() == LCK_This) { 4450 QualType ThisTy = getCurrentThisType(); 4451 if (!ThisTy.isNull() && 4452 Context.typesAreCompatible(ThisTy, ThisCapture->getType())) 4453 CheckCXXThisCapture(LC.getLocation()); 4454 } 4455 } 4456 } 4457 DSAStack->setForceCaptureByReferenceInTargetExecutable( 4458 SavedForceCaptureByReferenceInTargetExecutable); 4459 } 4460 } 4461 } 4462 4463 static bool checkOrderedOrderSpecified(Sema &S, 4464 const ArrayRef<OMPClause *> Clauses) { 4465 const OMPOrderedClause *Ordered = nullptr; 4466 const OMPOrderClause *Order = nullptr; 4467 4468 for (const OMPClause *Clause : Clauses) { 4469 if (Clause->getClauseKind() == OMPC_ordered) 4470 Ordered = cast<OMPOrderedClause>(Clause); 4471 else if (Clause->getClauseKind() == OMPC_order) { 4472 Order = cast<OMPOrderClause>(Clause); 4473 if (Order->getKind() != OMPC_ORDER_concurrent) 4474 Order = nullptr; 4475 } 4476 if (Ordered && Order) 4477 break; 4478 } 4479 4480 if (Ordered && Order) { 4481 S.Diag(Order->getKindKwLoc(), 4482 diag::err_omp_simple_clause_incompatible_with_ordered) 4483 << getOpenMPClauseName(OMPC_order) 4484 << getOpenMPSimpleClauseTypeName(OMPC_order, OMPC_ORDER_concurrent) 4485 << SourceRange(Order->getBeginLoc(), Order->getEndLoc()); 4486 S.Diag(Ordered->getBeginLoc(), diag::note_omp_ordered_param) 4487 << 0 << SourceRange(Ordered->getBeginLoc(), Ordered->getEndLoc()); 4488 return true; 4489 } 4490 return false; 4491 } 4492 4493 StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S, 4494 ArrayRef<OMPClause *> Clauses) { 4495 handleDeclareVariantConstructTrait(DSAStack, DSAStack->getCurrentDirective(), 4496 /* ScopeEntry */ false); 4497 if (DSAStack->getCurrentDirective() == OMPD_atomic || 4498 DSAStack->getCurrentDirective() == OMPD_critical || 4499 DSAStack->getCurrentDirective() == OMPD_section || 4500 DSAStack->getCurrentDirective() == OMPD_master || 4501 DSAStack->getCurrentDirective() == OMPD_masked) 4502 return S; 4503 4504 bool ErrorFound = false; 4505 CaptureRegionUnwinderRAII CaptureRegionUnwinder( 4506 *this, ErrorFound, DSAStack->getCurrentDirective()); 4507 if (!S.isUsable()) { 4508 ErrorFound = true; 4509 return StmtError(); 4510 } 4511 4512 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 4513 getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective()); 4514 OMPOrderedClause *OC = nullptr; 4515 OMPScheduleClause *SC = nullptr; 4516 SmallVector<const OMPLinearClause *, 4> LCs; 4517 SmallVector<const OMPClauseWithPreInit *, 4> PICs; 4518 // This is required for proper codegen. 4519 for (OMPClause *Clause : Clauses) { 4520 if (!LangOpts.OpenMPSimd && 4521 isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) && 4522 Clause->getClauseKind() == OMPC_in_reduction) { 4523 // Capture taskgroup task_reduction descriptors inside the tasking regions 4524 // with the corresponding in_reduction items. 4525 auto *IRC = cast<OMPInReductionClause>(Clause); 4526 for (Expr *E : IRC->taskgroup_descriptors()) 4527 if (E) 4528 MarkDeclarationsReferencedInExpr(E); 4529 } 4530 if (isOpenMPPrivate(Clause->getClauseKind()) || 4531 Clause->getClauseKind() == OMPC_copyprivate || 4532 (getLangOpts().OpenMPUseTLS && 4533 getASTContext().getTargetInfo().isTLSSupported() && 4534 Clause->getClauseKind() == OMPC_copyin)) { 4535 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin); 4536 // Mark all variables in private list clauses as used in inner region. 4537 for (Stmt *VarRef : Clause->children()) { 4538 if (auto *E = cast_or_null<Expr>(VarRef)) { 4539 MarkDeclarationsReferencedInExpr(E); 4540 } 4541 } 4542 DSAStack->setForceVarCapturing(/*V=*/false); 4543 } else if (isOpenMPLoopTransformationDirective( 4544 DSAStack->getCurrentDirective())) { 4545 assert(CaptureRegions.empty() && 4546 "No captured regions in loop transformation directives."); 4547 } else if (CaptureRegions.size() > 1 || 4548 CaptureRegions.back() != OMPD_unknown) { 4549 if (auto *C = OMPClauseWithPreInit::get(Clause)) 4550 PICs.push_back(C); 4551 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) { 4552 if (Expr *E = C->getPostUpdateExpr()) 4553 MarkDeclarationsReferencedInExpr(E); 4554 } 4555 } 4556 if (Clause->getClauseKind() == OMPC_schedule) 4557 SC = cast<OMPScheduleClause>(Clause); 4558 else if (Clause->getClauseKind() == OMPC_ordered) 4559 OC = cast<OMPOrderedClause>(Clause); 4560 else if (Clause->getClauseKind() == OMPC_linear) 4561 LCs.push_back(cast<OMPLinearClause>(Clause)); 4562 } 4563 // Capture allocator expressions if used. 4564 for (Expr *E : DSAStack->getInnerAllocators()) 4565 MarkDeclarationsReferencedInExpr(E); 4566 // OpenMP, 2.7.1 Loop Construct, Restrictions 4567 // The nonmonotonic modifier cannot be specified if an ordered clause is 4568 // specified. 4569 if (SC && 4570 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic || 4571 SC->getSecondScheduleModifier() == 4572 OMPC_SCHEDULE_MODIFIER_nonmonotonic) && 4573 OC) { 4574 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic 4575 ? SC->getFirstScheduleModifierLoc() 4576 : SC->getSecondScheduleModifierLoc(), 4577 diag::err_omp_simple_clause_incompatible_with_ordered) 4578 << getOpenMPClauseName(OMPC_schedule) 4579 << getOpenMPSimpleClauseTypeName(OMPC_schedule, 4580 OMPC_SCHEDULE_MODIFIER_nonmonotonic) 4581 << SourceRange(OC->getBeginLoc(), OC->getEndLoc()); 4582 ErrorFound = true; 4583 } 4584 // OpenMP 5.0, 2.9.2 Worksharing-Loop Construct, Restrictions. 4585 // If an order(concurrent) clause is present, an ordered clause may not appear 4586 // on the same directive. 4587 if (checkOrderedOrderSpecified(*this, Clauses)) 4588 ErrorFound = true; 4589 if (!LCs.empty() && OC && OC->getNumForLoops()) { 4590 for (const OMPLinearClause *C : LCs) { 4591 Diag(C->getBeginLoc(), diag::err_omp_linear_ordered) 4592 << SourceRange(OC->getBeginLoc(), OC->getEndLoc()); 4593 } 4594 ErrorFound = true; 4595 } 4596 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) && 4597 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC && 4598 OC->getNumForLoops()) { 4599 Diag(OC->getBeginLoc(), diag::err_omp_ordered_simd) 4600 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 4601 ErrorFound = true; 4602 } 4603 if (ErrorFound) { 4604 return StmtError(); 4605 } 4606 StmtResult SR = S; 4607 unsigned CompletedRegions = 0; 4608 for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(CaptureRegions)) { 4609 // Mark all variables in private list clauses as used in inner region. 4610 // Required for proper codegen of combined directives. 4611 // TODO: add processing for other clauses. 4612 if (ThisCaptureRegion != OMPD_unknown) { 4613 for (const clang::OMPClauseWithPreInit *C : PICs) { 4614 OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion(); 4615 // Find the particular capture region for the clause if the 4616 // directive is a combined one with multiple capture regions. 4617 // If the directive is not a combined one, the capture region 4618 // associated with the clause is OMPD_unknown and is generated 4619 // only once. 4620 if (CaptureRegion == ThisCaptureRegion || 4621 CaptureRegion == OMPD_unknown) { 4622 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) { 4623 for (Decl *D : DS->decls()) 4624 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D)); 4625 } 4626 } 4627 } 4628 } 4629 if (ThisCaptureRegion == OMPD_target) { 4630 // Capture allocator traits in the target region. They are used implicitly 4631 // and, thus, are not captured by default. 4632 for (OMPClause *C : Clauses) { 4633 if (const auto *UAC = dyn_cast<OMPUsesAllocatorsClause>(C)) { 4634 for (unsigned I = 0, End = UAC->getNumberOfAllocators(); I < End; 4635 ++I) { 4636 OMPUsesAllocatorsClause::Data D = UAC->getAllocatorData(I); 4637 if (Expr *E = D.AllocatorTraits) 4638 MarkDeclarationsReferencedInExpr(E); 4639 } 4640 continue; 4641 } 4642 } 4643 } 4644 if (ThisCaptureRegion == OMPD_parallel) { 4645 // Capture temp arrays for inscan reductions and locals in aligned 4646 // clauses. 4647 for (OMPClause *C : Clauses) { 4648 if (auto *RC = dyn_cast<OMPReductionClause>(C)) { 4649 if (RC->getModifier() != OMPC_REDUCTION_inscan) 4650 continue; 4651 for (Expr *E : RC->copy_array_temps()) 4652 MarkDeclarationsReferencedInExpr(E); 4653 } 4654 if (auto *AC = dyn_cast<OMPAlignedClause>(C)) { 4655 for (Expr *E : AC->varlists()) 4656 MarkDeclarationsReferencedInExpr(E); 4657 } 4658 } 4659 } 4660 if (++CompletedRegions == CaptureRegions.size()) 4661 DSAStack->setBodyComplete(); 4662 SR = ActOnCapturedRegionEnd(SR.get()); 4663 } 4664 return SR; 4665 } 4666 4667 static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion, 4668 OpenMPDirectiveKind CancelRegion, 4669 SourceLocation StartLoc) { 4670 // CancelRegion is only needed for cancel and cancellation_point. 4671 if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point) 4672 return false; 4673 4674 if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for || 4675 CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup) 4676 return false; 4677 4678 SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region) 4679 << getOpenMPDirectiveName(CancelRegion); 4680 return true; 4681 } 4682 4683 static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack, 4684 OpenMPDirectiveKind CurrentRegion, 4685 const DeclarationNameInfo &CurrentName, 4686 OpenMPDirectiveKind CancelRegion, 4687 SourceLocation StartLoc) { 4688 if (Stack->getCurScope()) { 4689 OpenMPDirectiveKind ParentRegion = Stack->getParentDirective(); 4690 OpenMPDirectiveKind OffendingRegion = ParentRegion; 4691 bool NestingProhibited = false; 4692 bool CloseNesting = true; 4693 bool OrphanSeen = false; 4694 enum { 4695 NoRecommend, 4696 ShouldBeInParallelRegion, 4697 ShouldBeInOrderedRegion, 4698 ShouldBeInTargetRegion, 4699 ShouldBeInTeamsRegion, 4700 ShouldBeInLoopSimdRegion, 4701 } Recommend = NoRecommend; 4702 if (isOpenMPSimdDirective(ParentRegion) && 4703 ((SemaRef.LangOpts.OpenMP <= 45 && CurrentRegion != OMPD_ordered) || 4704 (SemaRef.LangOpts.OpenMP >= 50 && CurrentRegion != OMPD_ordered && 4705 CurrentRegion != OMPD_simd && CurrentRegion != OMPD_atomic && 4706 CurrentRegion != OMPD_scan))) { 4707 // OpenMP [2.16, Nesting of Regions] 4708 // OpenMP constructs may not be nested inside a simd region. 4709 // OpenMP [2.8.1,simd Construct, Restrictions] 4710 // An ordered construct with the simd clause is the only OpenMP 4711 // construct that can appear in the simd region. 4712 // Allowing a SIMD construct nested in another SIMD construct is an 4713 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning 4714 // message. 4715 // OpenMP 5.0 [2.9.3.1, simd Construct, Restrictions] 4716 // The only OpenMP constructs that can be encountered during execution of 4717 // a simd region are the atomic construct, the loop construct, the simd 4718 // construct and the ordered construct with the simd clause. 4719 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd) 4720 ? diag::err_omp_prohibited_region_simd 4721 : diag::warn_omp_nesting_simd) 4722 << (SemaRef.LangOpts.OpenMP >= 50 ? 1 : 0); 4723 return CurrentRegion != OMPD_simd; 4724 } 4725 if (ParentRegion == OMPD_atomic) { 4726 // OpenMP [2.16, Nesting of Regions] 4727 // OpenMP constructs may not be nested inside an atomic region. 4728 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic); 4729 return true; 4730 } 4731 if (CurrentRegion == OMPD_section) { 4732 // OpenMP [2.7.2, sections Construct, Restrictions] 4733 // Orphaned section directives are prohibited. That is, the section 4734 // directives must appear within the sections construct and must not be 4735 // encountered elsewhere in the sections region. 4736 if (ParentRegion != OMPD_sections && 4737 ParentRegion != OMPD_parallel_sections) { 4738 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive) 4739 << (ParentRegion != OMPD_unknown) 4740 << getOpenMPDirectiveName(ParentRegion); 4741 return true; 4742 } 4743 return false; 4744 } 4745 // Allow some constructs (except teams and cancellation constructs) to be 4746 // orphaned (they could be used in functions, called from OpenMP regions 4747 // with the required preconditions). 4748 if (ParentRegion == OMPD_unknown && 4749 !isOpenMPNestingTeamsDirective(CurrentRegion) && 4750 CurrentRegion != OMPD_cancellation_point && 4751 CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_scan) 4752 return false; 4753 if (CurrentRegion == OMPD_cancellation_point || 4754 CurrentRegion == OMPD_cancel) { 4755 // OpenMP [2.16, Nesting of Regions] 4756 // A cancellation point construct for which construct-type-clause is 4757 // taskgroup must be nested inside a task construct. A cancellation 4758 // point construct for which construct-type-clause is not taskgroup must 4759 // be closely nested inside an OpenMP construct that matches the type 4760 // specified in construct-type-clause. 4761 // A cancel construct for which construct-type-clause is taskgroup must be 4762 // nested inside a task construct. A cancel construct for which 4763 // construct-type-clause is not taskgroup must be closely nested inside an 4764 // OpenMP construct that matches the type specified in 4765 // construct-type-clause. 4766 NestingProhibited = 4767 !((CancelRegion == OMPD_parallel && 4768 (ParentRegion == OMPD_parallel || 4769 ParentRegion == OMPD_target_parallel)) || 4770 (CancelRegion == OMPD_for && 4771 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for || 4772 ParentRegion == OMPD_target_parallel_for || 4773 ParentRegion == OMPD_distribute_parallel_for || 4774 ParentRegion == OMPD_teams_distribute_parallel_for || 4775 ParentRegion == OMPD_target_teams_distribute_parallel_for)) || 4776 (CancelRegion == OMPD_taskgroup && 4777 (ParentRegion == OMPD_task || 4778 (SemaRef.getLangOpts().OpenMP >= 50 && 4779 (ParentRegion == OMPD_taskloop || 4780 ParentRegion == OMPD_master_taskloop || 4781 ParentRegion == OMPD_parallel_master_taskloop)))) || 4782 (CancelRegion == OMPD_sections && 4783 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections || 4784 ParentRegion == OMPD_parallel_sections))); 4785 OrphanSeen = ParentRegion == OMPD_unknown; 4786 } else if (CurrentRegion == OMPD_master || CurrentRegion == OMPD_masked) { 4787 // OpenMP 5.1 [2.22, Nesting of Regions] 4788 // A masked region may not be closely nested inside a worksharing, loop, 4789 // atomic, task, or taskloop region. 4790 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 4791 isOpenMPTaskingDirective(ParentRegion); 4792 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) { 4793 // OpenMP [2.16, Nesting of Regions] 4794 // A critical region may not be nested (closely or otherwise) inside a 4795 // critical region with the same name. Note that this restriction is not 4796 // sufficient to prevent deadlock. 4797 SourceLocation PreviousCriticalLoc; 4798 bool DeadLock = Stack->hasDirective( 4799 [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K, 4800 const DeclarationNameInfo &DNI, 4801 SourceLocation Loc) { 4802 if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) { 4803 PreviousCriticalLoc = Loc; 4804 return true; 4805 } 4806 return false; 4807 }, 4808 false /* skip top directive */); 4809 if (DeadLock) { 4810 SemaRef.Diag(StartLoc, 4811 diag::err_omp_prohibited_region_critical_same_name) 4812 << CurrentName.getName(); 4813 if (PreviousCriticalLoc.isValid()) 4814 SemaRef.Diag(PreviousCriticalLoc, 4815 diag::note_omp_previous_critical_region); 4816 return true; 4817 } 4818 } else if (CurrentRegion == OMPD_barrier) { 4819 // OpenMP 5.1 [2.22, Nesting of Regions] 4820 // A barrier region may not be closely nested inside a worksharing, loop, 4821 // task, taskloop, critical, ordered, atomic, or masked region. 4822 NestingProhibited = 4823 isOpenMPWorksharingDirective(ParentRegion) || 4824 isOpenMPTaskingDirective(ParentRegion) || 4825 ParentRegion == OMPD_master || ParentRegion == OMPD_masked || 4826 ParentRegion == OMPD_parallel_master || 4827 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered; 4828 } else if (isOpenMPWorksharingDirective(CurrentRegion) && 4829 !isOpenMPParallelDirective(CurrentRegion) && 4830 !isOpenMPTeamsDirective(CurrentRegion)) { 4831 // OpenMP 5.1 [2.22, Nesting of Regions] 4832 // A loop region that binds to a parallel region or a worksharing region 4833 // may not be closely nested inside a worksharing, loop, task, taskloop, 4834 // critical, ordered, atomic, or masked region. 4835 NestingProhibited = 4836 isOpenMPWorksharingDirective(ParentRegion) || 4837 isOpenMPTaskingDirective(ParentRegion) || 4838 ParentRegion == OMPD_master || ParentRegion == OMPD_masked || 4839 ParentRegion == OMPD_parallel_master || 4840 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered; 4841 Recommend = ShouldBeInParallelRegion; 4842 } else if (CurrentRegion == OMPD_ordered) { 4843 // OpenMP [2.16, Nesting of Regions] 4844 // An ordered region may not be closely nested inside a critical, 4845 // atomic, or explicit task region. 4846 // An ordered region must be closely nested inside a loop region (or 4847 // parallel loop region) with an ordered clause. 4848 // OpenMP [2.8.1,simd Construct, Restrictions] 4849 // An ordered construct with the simd clause is the only OpenMP construct 4850 // that can appear in the simd region. 4851 NestingProhibited = ParentRegion == OMPD_critical || 4852 isOpenMPTaskingDirective(ParentRegion) || 4853 !(isOpenMPSimdDirective(ParentRegion) || 4854 Stack->isParentOrderedRegion()); 4855 Recommend = ShouldBeInOrderedRegion; 4856 } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) { 4857 // OpenMP [2.16, Nesting of Regions] 4858 // If specified, a teams construct must be contained within a target 4859 // construct. 4860 NestingProhibited = 4861 (SemaRef.LangOpts.OpenMP <= 45 && ParentRegion != OMPD_target) || 4862 (SemaRef.LangOpts.OpenMP >= 50 && ParentRegion != OMPD_unknown && 4863 ParentRegion != OMPD_target); 4864 OrphanSeen = ParentRegion == OMPD_unknown; 4865 Recommend = ShouldBeInTargetRegion; 4866 } else if (CurrentRegion == OMPD_scan) { 4867 // OpenMP [2.16, Nesting of Regions] 4868 // If specified, a teams construct must be contained within a target 4869 // construct. 4870 NestingProhibited = 4871 SemaRef.LangOpts.OpenMP < 50 || 4872 (ParentRegion != OMPD_simd && ParentRegion != OMPD_for && 4873 ParentRegion != OMPD_for_simd && ParentRegion != OMPD_parallel_for && 4874 ParentRegion != OMPD_parallel_for_simd); 4875 OrphanSeen = ParentRegion == OMPD_unknown; 4876 Recommend = ShouldBeInLoopSimdRegion; 4877 } 4878 if (!NestingProhibited && 4879 !isOpenMPTargetExecutionDirective(CurrentRegion) && 4880 !isOpenMPTargetDataManagementDirective(CurrentRegion) && 4881 (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) { 4882 // OpenMP [2.16, Nesting of Regions] 4883 // distribute, parallel, parallel sections, parallel workshare, and the 4884 // parallel loop and parallel loop SIMD constructs are the only OpenMP 4885 // constructs that can be closely nested in the teams region. 4886 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) && 4887 !isOpenMPDistributeDirective(CurrentRegion); 4888 Recommend = ShouldBeInParallelRegion; 4889 } 4890 if (!NestingProhibited && 4891 isOpenMPNestingDistributeDirective(CurrentRegion)) { 4892 // OpenMP 4.5 [2.17 Nesting of Regions] 4893 // The region associated with the distribute construct must be strictly 4894 // nested inside a teams region 4895 NestingProhibited = 4896 (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams); 4897 Recommend = ShouldBeInTeamsRegion; 4898 } 4899 if (!NestingProhibited && 4900 (isOpenMPTargetExecutionDirective(CurrentRegion) || 4901 isOpenMPTargetDataManagementDirective(CurrentRegion))) { 4902 // OpenMP 4.5 [2.17 Nesting of Regions] 4903 // If a target, target update, target data, target enter data, or 4904 // target exit data construct is encountered during execution of a 4905 // target region, the behavior is unspecified. 4906 NestingProhibited = Stack->hasDirective( 4907 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &, 4908 SourceLocation) { 4909 if (isOpenMPTargetExecutionDirective(K)) { 4910 OffendingRegion = K; 4911 return true; 4912 } 4913 return false; 4914 }, 4915 false /* don't skip top directive */); 4916 CloseNesting = false; 4917 } 4918 if (NestingProhibited) { 4919 if (OrphanSeen) { 4920 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive) 4921 << getOpenMPDirectiveName(CurrentRegion) << Recommend; 4922 } else { 4923 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region) 4924 << CloseNesting << getOpenMPDirectiveName(OffendingRegion) 4925 << Recommend << getOpenMPDirectiveName(CurrentRegion); 4926 } 4927 return true; 4928 } 4929 } 4930 return false; 4931 } 4932 4933 struct Kind2Unsigned { 4934 using argument_type = OpenMPDirectiveKind; 4935 unsigned operator()(argument_type DK) { return unsigned(DK); } 4936 }; 4937 static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind, 4938 ArrayRef<OMPClause *> Clauses, 4939 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) { 4940 bool ErrorFound = false; 4941 unsigned NamedModifiersNumber = 0; 4942 llvm::IndexedMap<const OMPIfClause *, Kind2Unsigned> FoundNameModifiers; 4943 FoundNameModifiers.resize(llvm::omp::Directive_enumSize + 1); 4944 SmallVector<SourceLocation, 4> NameModifierLoc; 4945 for (const OMPClause *C : Clauses) { 4946 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) { 4947 // At most one if clause without a directive-name-modifier can appear on 4948 // the directive. 4949 OpenMPDirectiveKind CurNM = IC->getNameModifier(); 4950 if (FoundNameModifiers[CurNM]) { 4951 S.Diag(C->getBeginLoc(), diag::err_omp_more_one_clause) 4952 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if) 4953 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM); 4954 ErrorFound = true; 4955 } else if (CurNM != OMPD_unknown) { 4956 NameModifierLoc.push_back(IC->getNameModifierLoc()); 4957 ++NamedModifiersNumber; 4958 } 4959 FoundNameModifiers[CurNM] = IC; 4960 if (CurNM == OMPD_unknown) 4961 continue; 4962 // Check if the specified name modifier is allowed for the current 4963 // directive. 4964 // At most one if clause with the particular directive-name-modifier can 4965 // appear on the directive. 4966 bool MatchFound = false; 4967 for (auto NM : AllowedNameModifiers) { 4968 if (CurNM == NM) { 4969 MatchFound = true; 4970 break; 4971 } 4972 } 4973 if (!MatchFound) { 4974 S.Diag(IC->getNameModifierLoc(), 4975 diag::err_omp_wrong_if_directive_name_modifier) 4976 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind); 4977 ErrorFound = true; 4978 } 4979 } 4980 } 4981 // If any if clause on the directive includes a directive-name-modifier then 4982 // all if clauses on the directive must include a directive-name-modifier. 4983 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) { 4984 if (NamedModifiersNumber == AllowedNameModifiers.size()) { 4985 S.Diag(FoundNameModifiers[OMPD_unknown]->getBeginLoc(), 4986 diag::err_omp_no_more_if_clause); 4987 } else { 4988 std::string Values; 4989 std::string Sep(", "); 4990 unsigned AllowedCnt = 0; 4991 unsigned TotalAllowedNum = 4992 AllowedNameModifiers.size() - NamedModifiersNumber; 4993 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End; 4994 ++Cnt) { 4995 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt]; 4996 if (!FoundNameModifiers[NM]) { 4997 Values += "'"; 4998 Values += getOpenMPDirectiveName(NM); 4999 Values += "'"; 5000 if (AllowedCnt + 2 == TotalAllowedNum) 5001 Values += " or "; 5002 else if (AllowedCnt + 1 != TotalAllowedNum) 5003 Values += Sep; 5004 ++AllowedCnt; 5005 } 5006 } 5007 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getBeginLoc(), 5008 diag::err_omp_unnamed_if_clause) 5009 << (TotalAllowedNum > 1) << Values; 5010 } 5011 for (SourceLocation Loc : NameModifierLoc) { 5012 S.Diag(Loc, diag::note_omp_previous_named_if_clause); 5013 } 5014 ErrorFound = true; 5015 } 5016 return ErrorFound; 5017 } 5018 5019 static std::pair<ValueDecl *, bool> getPrivateItem(Sema &S, Expr *&RefExpr, 5020 SourceLocation &ELoc, 5021 SourceRange &ERange, 5022 bool AllowArraySection) { 5023 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() || 5024 RefExpr->containsUnexpandedParameterPack()) 5025 return std::make_pair(nullptr, true); 5026 5027 // OpenMP [3.1, C/C++] 5028 // A list item is a variable name. 5029 // OpenMP [2.9.3.3, Restrictions, p.1] 5030 // A variable that is part of another variable (as an array or 5031 // structure element) cannot appear in a private clause. 5032 RefExpr = RefExpr->IgnoreParens(); 5033 enum { 5034 NoArrayExpr = -1, 5035 ArraySubscript = 0, 5036 OMPArraySection = 1 5037 } IsArrayExpr = NoArrayExpr; 5038 if (AllowArraySection) { 5039 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) { 5040 Expr *Base = ASE->getBase()->IgnoreParenImpCasts(); 5041 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 5042 Base = TempASE->getBase()->IgnoreParenImpCasts(); 5043 RefExpr = Base; 5044 IsArrayExpr = ArraySubscript; 5045 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) { 5046 Expr *Base = OASE->getBase()->IgnoreParenImpCasts(); 5047 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) 5048 Base = TempOASE->getBase()->IgnoreParenImpCasts(); 5049 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 5050 Base = TempASE->getBase()->IgnoreParenImpCasts(); 5051 RefExpr = Base; 5052 IsArrayExpr = OMPArraySection; 5053 } 5054 } 5055 ELoc = RefExpr->getExprLoc(); 5056 ERange = RefExpr->getSourceRange(); 5057 RefExpr = RefExpr->IgnoreParenImpCasts(); 5058 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr); 5059 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr); 5060 if ((!DE || !isa<VarDecl>(DE->getDecl())) && 5061 (S.getCurrentThisType().isNull() || !ME || 5062 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) || 5063 !isa<FieldDecl>(ME->getMemberDecl()))) { 5064 if (IsArrayExpr != NoArrayExpr) { 5065 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr 5066 << ERange; 5067 } else { 5068 S.Diag(ELoc, 5069 AllowArraySection 5070 ? diag::err_omp_expected_var_name_member_expr_or_array_item 5071 : diag::err_omp_expected_var_name_member_expr) 5072 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange; 5073 } 5074 return std::make_pair(nullptr, false); 5075 } 5076 return std::make_pair( 5077 getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false); 5078 } 5079 5080 namespace { 5081 /// Checks if the allocator is used in uses_allocators clause to be allowed in 5082 /// target regions. 5083 class AllocatorChecker final : public ConstStmtVisitor<AllocatorChecker, bool> { 5084 DSAStackTy *S = nullptr; 5085 5086 public: 5087 bool VisitDeclRefExpr(const DeclRefExpr *E) { 5088 return S->isUsesAllocatorsDecl(E->getDecl()) 5089 .getValueOr( 5090 DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait) == 5091 DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait; 5092 } 5093 bool VisitStmt(const Stmt *S) { 5094 for (const Stmt *Child : S->children()) { 5095 if (Child && Visit(Child)) 5096 return true; 5097 } 5098 return false; 5099 } 5100 explicit AllocatorChecker(DSAStackTy *S) : S(S) {} 5101 }; 5102 } // namespace 5103 5104 static void checkAllocateClauses(Sema &S, DSAStackTy *Stack, 5105 ArrayRef<OMPClause *> Clauses) { 5106 assert(!S.CurContext->isDependentContext() && 5107 "Expected non-dependent context."); 5108 auto AllocateRange = 5109 llvm::make_filter_range(Clauses, OMPAllocateClause::classof); 5110 llvm::DenseMap<CanonicalDeclPtr<Decl>, CanonicalDeclPtr<VarDecl>> 5111 DeclToCopy; 5112 auto PrivateRange = llvm::make_filter_range(Clauses, [](const OMPClause *C) { 5113 return isOpenMPPrivate(C->getClauseKind()); 5114 }); 5115 for (OMPClause *Cl : PrivateRange) { 5116 MutableArrayRef<Expr *>::iterator I, It, Et; 5117 if (Cl->getClauseKind() == OMPC_private) { 5118 auto *PC = cast<OMPPrivateClause>(Cl); 5119 I = PC->private_copies().begin(); 5120 It = PC->varlist_begin(); 5121 Et = PC->varlist_end(); 5122 } else if (Cl->getClauseKind() == OMPC_firstprivate) { 5123 auto *PC = cast<OMPFirstprivateClause>(Cl); 5124 I = PC->private_copies().begin(); 5125 It = PC->varlist_begin(); 5126 Et = PC->varlist_end(); 5127 } else if (Cl->getClauseKind() == OMPC_lastprivate) { 5128 auto *PC = cast<OMPLastprivateClause>(Cl); 5129 I = PC->private_copies().begin(); 5130 It = PC->varlist_begin(); 5131 Et = PC->varlist_end(); 5132 } else if (Cl->getClauseKind() == OMPC_linear) { 5133 auto *PC = cast<OMPLinearClause>(Cl); 5134 I = PC->privates().begin(); 5135 It = PC->varlist_begin(); 5136 Et = PC->varlist_end(); 5137 } else if (Cl->getClauseKind() == OMPC_reduction) { 5138 auto *PC = cast<OMPReductionClause>(Cl); 5139 I = PC->privates().begin(); 5140 It = PC->varlist_begin(); 5141 Et = PC->varlist_end(); 5142 } else if (Cl->getClauseKind() == OMPC_task_reduction) { 5143 auto *PC = cast<OMPTaskReductionClause>(Cl); 5144 I = PC->privates().begin(); 5145 It = PC->varlist_begin(); 5146 Et = PC->varlist_end(); 5147 } else if (Cl->getClauseKind() == OMPC_in_reduction) { 5148 auto *PC = cast<OMPInReductionClause>(Cl); 5149 I = PC->privates().begin(); 5150 It = PC->varlist_begin(); 5151 Et = PC->varlist_end(); 5152 } else { 5153 llvm_unreachable("Expected private clause."); 5154 } 5155 for (Expr *E : llvm::make_range(It, Et)) { 5156 if (!*I) { 5157 ++I; 5158 continue; 5159 } 5160 SourceLocation ELoc; 5161 SourceRange ERange; 5162 Expr *SimpleRefExpr = E; 5163 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange, 5164 /*AllowArraySection=*/true); 5165 DeclToCopy.try_emplace(Res.first, 5166 cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl())); 5167 ++I; 5168 } 5169 } 5170 for (OMPClause *C : AllocateRange) { 5171 auto *AC = cast<OMPAllocateClause>(C); 5172 if (S.getLangOpts().OpenMP >= 50 && 5173 !Stack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>() && 5174 isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) && 5175 AC->getAllocator()) { 5176 Expr *Allocator = AC->getAllocator(); 5177 // OpenMP, 2.12.5 target Construct 5178 // Memory allocators that do not appear in a uses_allocators clause cannot 5179 // appear as an allocator in an allocate clause or be used in the target 5180 // region unless a requires directive with the dynamic_allocators clause 5181 // is present in the same compilation unit. 5182 AllocatorChecker Checker(Stack); 5183 if (Checker.Visit(Allocator)) 5184 S.Diag(Allocator->getExprLoc(), 5185 diag::err_omp_allocator_not_in_uses_allocators) 5186 << Allocator->getSourceRange(); 5187 } 5188 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind = 5189 getAllocatorKind(S, Stack, AC->getAllocator()); 5190 // OpenMP, 2.11.4 allocate Clause, Restrictions. 5191 // For task, taskloop or target directives, allocation requests to memory 5192 // allocators with the trait access set to thread result in unspecified 5193 // behavior. 5194 if (AllocatorKind == OMPAllocateDeclAttr::OMPThreadMemAlloc && 5195 (isOpenMPTaskingDirective(Stack->getCurrentDirective()) || 5196 isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()))) { 5197 S.Diag(AC->getAllocator()->getExprLoc(), 5198 diag::warn_omp_allocate_thread_on_task_target_directive) 5199 << getOpenMPDirectiveName(Stack->getCurrentDirective()); 5200 } 5201 for (Expr *E : AC->varlists()) { 5202 SourceLocation ELoc; 5203 SourceRange ERange; 5204 Expr *SimpleRefExpr = E; 5205 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange); 5206 ValueDecl *VD = Res.first; 5207 DSAStackTy::DSAVarData Data = Stack->getTopDSA(VD, /*FromParent=*/false); 5208 if (!isOpenMPPrivate(Data.CKind)) { 5209 S.Diag(E->getExprLoc(), 5210 diag::err_omp_expected_private_copy_for_allocate); 5211 continue; 5212 } 5213 VarDecl *PrivateVD = DeclToCopy[VD]; 5214 if (checkPreviousOMPAllocateAttribute(S, Stack, E, PrivateVD, 5215 AllocatorKind, AC->getAllocator())) 5216 continue; 5217 applyOMPAllocateAttribute(S, PrivateVD, AllocatorKind, AC->getAllocator(), 5218 E->getSourceRange()); 5219 } 5220 } 5221 } 5222 5223 namespace { 5224 /// Rewrite statements and expressions for Sema \p Actions CurContext. 5225 /// 5226 /// Used to wrap already parsed statements/expressions into a new CapturedStmt 5227 /// context. DeclRefExpr used inside the new context are changed to refer to the 5228 /// captured variable instead. 5229 class CaptureVars : public TreeTransform<CaptureVars> { 5230 using BaseTransform = TreeTransform<CaptureVars>; 5231 5232 public: 5233 CaptureVars(Sema &Actions) : BaseTransform(Actions) {} 5234 5235 bool AlwaysRebuild() { return true; } 5236 }; 5237 } // namespace 5238 5239 static VarDecl *precomputeExpr(Sema &Actions, 5240 SmallVectorImpl<Stmt *> &BodyStmts, Expr *E, 5241 StringRef Name) { 5242 Expr *NewE = AssertSuccess(CaptureVars(Actions).TransformExpr(E)); 5243 VarDecl *NewVar = buildVarDecl(Actions, {}, NewE->getType(), Name, nullptr, 5244 dyn_cast<DeclRefExpr>(E->IgnoreImplicit())); 5245 auto *NewDeclStmt = cast<DeclStmt>(AssertSuccess( 5246 Actions.ActOnDeclStmt(Actions.ConvertDeclToDeclGroup(NewVar), {}, {}))); 5247 Actions.AddInitializerToDecl(NewDeclStmt->getSingleDecl(), NewE, false); 5248 BodyStmts.push_back(NewDeclStmt); 5249 return NewVar; 5250 } 5251 5252 /// Create a closure that computes the number of iterations of a loop. 5253 /// 5254 /// \param Actions The Sema object. 5255 /// \param LogicalTy Type for the logical iteration number. 5256 /// \param Rel Comparison operator of the loop condition. 5257 /// \param StartExpr Value of the loop counter at the first iteration. 5258 /// \param StopExpr Expression the loop counter is compared against in the loop 5259 /// condition. \param StepExpr Amount of increment after each iteration. 5260 /// 5261 /// \return Closure (CapturedStmt) of the distance calculation. 5262 static CapturedStmt *buildDistanceFunc(Sema &Actions, QualType LogicalTy, 5263 BinaryOperator::Opcode Rel, 5264 Expr *StartExpr, Expr *StopExpr, 5265 Expr *StepExpr) { 5266 ASTContext &Ctx = Actions.getASTContext(); 5267 TypeSourceInfo *LogicalTSI = Ctx.getTrivialTypeSourceInfo(LogicalTy); 5268 5269 // Captured regions currently don't support return values, we use an 5270 // out-parameter instead. All inputs are implicit captures. 5271 // TODO: Instead of capturing each DeclRefExpr occurring in 5272 // StartExpr/StopExpr/Step, these could also be passed as a value capture. 5273 QualType ResultTy = Ctx.getLValueReferenceType(LogicalTy); 5274 Sema::CapturedParamNameType Params[] = {{"Distance", ResultTy}, 5275 {StringRef(), QualType()}}; 5276 Actions.ActOnCapturedRegionStart({}, nullptr, CR_Default, Params); 5277 5278 Stmt *Body; 5279 { 5280 Sema::CompoundScopeRAII CompoundScope(Actions); 5281 CapturedDecl *CS = cast<CapturedDecl>(Actions.CurContext); 5282 5283 // Get the LValue expression for the result. 5284 ImplicitParamDecl *DistParam = CS->getParam(0); 5285 DeclRefExpr *DistRef = Actions.BuildDeclRefExpr( 5286 DistParam, LogicalTy, VK_LValue, {}, nullptr, nullptr, {}, nullptr); 5287 5288 SmallVector<Stmt *, 4> BodyStmts; 5289 5290 // Capture all referenced variable references. 5291 // TODO: Instead of computing NewStart/NewStop/NewStep inside the 5292 // CapturedStmt, we could compute them before and capture the result, to be 5293 // used jointly with the LoopVar function. 5294 VarDecl *NewStart = precomputeExpr(Actions, BodyStmts, StartExpr, ".start"); 5295 VarDecl *NewStop = precomputeExpr(Actions, BodyStmts, StopExpr, ".stop"); 5296 VarDecl *NewStep = precomputeExpr(Actions, BodyStmts, StepExpr, ".step"); 5297 auto BuildVarRef = [&](VarDecl *VD) { 5298 return buildDeclRefExpr(Actions, VD, VD->getType(), {}); 5299 }; 5300 5301 IntegerLiteral *Zero = IntegerLiteral::Create( 5302 Ctx, llvm::APInt(Ctx.getIntWidth(LogicalTy), 0), LogicalTy, {}); 5303 Expr *Dist; 5304 if (Rel == BO_NE) { 5305 // When using a != comparison, the increment can be +1 or -1. This can be 5306 // dynamic at runtime, so we need to check for the direction. 5307 Expr *IsNegStep = AssertSuccess( 5308 Actions.BuildBinOp(nullptr, {}, BO_LT, BuildVarRef(NewStep), Zero)); 5309 5310 // Positive increment. 5311 Expr *ForwardRange = AssertSuccess(Actions.BuildBinOp( 5312 nullptr, {}, BO_Sub, BuildVarRef(NewStop), BuildVarRef(NewStart))); 5313 ForwardRange = AssertSuccess( 5314 Actions.BuildCStyleCastExpr({}, LogicalTSI, {}, ForwardRange)); 5315 Expr *ForwardDist = AssertSuccess(Actions.BuildBinOp( 5316 nullptr, {}, BO_Div, ForwardRange, BuildVarRef(NewStep))); 5317 5318 // Negative increment. 5319 Expr *BackwardRange = AssertSuccess(Actions.BuildBinOp( 5320 nullptr, {}, BO_Sub, BuildVarRef(NewStart), BuildVarRef(NewStop))); 5321 BackwardRange = AssertSuccess( 5322 Actions.BuildCStyleCastExpr({}, LogicalTSI, {}, BackwardRange)); 5323 Expr *NegIncAmount = AssertSuccess( 5324 Actions.BuildUnaryOp(nullptr, {}, UO_Minus, BuildVarRef(NewStep))); 5325 Expr *BackwardDist = AssertSuccess( 5326 Actions.BuildBinOp(nullptr, {}, BO_Div, BackwardRange, NegIncAmount)); 5327 5328 // Use the appropriate case. 5329 Dist = AssertSuccess(Actions.ActOnConditionalOp( 5330 {}, {}, IsNegStep, BackwardDist, ForwardDist)); 5331 } else { 5332 assert((Rel == BO_LT || Rel == BO_LE || Rel == BO_GE || Rel == BO_GT) && 5333 "Expected one of these relational operators"); 5334 5335 // We can derive the direction from any other comparison operator. It is 5336 // non well-formed OpenMP if Step increments/decrements in the other 5337 // directions. Whether at least the first iteration passes the loop 5338 // condition. 5339 Expr *HasAnyIteration = AssertSuccess(Actions.BuildBinOp( 5340 nullptr, {}, Rel, BuildVarRef(NewStart), BuildVarRef(NewStop))); 5341 5342 // Compute the range between first and last counter value. 5343 Expr *Range; 5344 if (Rel == BO_GE || Rel == BO_GT) 5345 Range = AssertSuccess(Actions.BuildBinOp( 5346 nullptr, {}, BO_Sub, BuildVarRef(NewStart), BuildVarRef(NewStop))); 5347 else 5348 Range = AssertSuccess(Actions.BuildBinOp( 5349 nullptr, {}, BO_Sub, BuildVarRef(NewStop), BuildVarRef(NewStart))); 5350 5351 // Ensure unsigned range space. 5352 Range = 5353 AssertSuccess(Actions.BuildCStyleCastExpr({}, LogicalTSI, {}, Range)); 5354 5355 if (Rel == BO_LE || Rel == BO_GE) { 5356 // Add one to the range if the relational operator is inclusive. 5357 Range = AssertSuccess(Actions.BuildBinOp( 5358 nullptr, {}, BO_Add, Range, 5359 Actions.ActOnIntegerConstant(SourceLocation(), 1).get())); 5360 } 5361 5362 // Divide by the absolute step amount. 5363 Expr *Divisor = BuildVarRef(NewStep); 5364 if (Rel == BO_GE || Rel == BO_GT) 5365 Divisor = 5366 AssertSuccess(Actions.BuildUnaryOp(nullptr, {}, UO_Minus, Divisor)); 5367 Dist = AssertSuccess( 5368 Actions.BuildBinOp(nullptr, {}, BO_Div, Range, Divisor)); 5369 5370 // If there is not at least one iteration, the range contains garbage. Fix 5371 // to zero in this case. 5372 Dist = AssertSuccess( 5373 Actions.ActOnConditionalOp({}, {}, HasAnyIteration, Dist, Zero)); 5374 } 5375 5376 // Assign the result to the out-parameter. 5377 Stmt *ResultAssign = AssertSuccess(Actions.BuildBinOp( 5378 Actions.getCurScope(), {}, BO_Assign, DistRef, Dist)); 5379 BodyStmts.push_back(ResultAssign); 5380 5381 Body = AssertSuccess(Actions.ActOnCompoundStmt({}, {}, BodyStmts, false)); 5382 } 5383 5384 return cast<CapturedStmt>( 5385 AssertSuccess(Actions.ActOnCapturedRegionEnd(Body))); 5386 } 5387 5388 /// Create a closure that computes the loop variable from the logical iteration 5389 /// number. 5390 /// 5391 /// \param Actions The Sema object. 5392 /// \param LoopVarTy Type for the loop variable used for result value. 5393 /// \param LogicalTy Type for the logical iteration number. 5394 /// \param StartExpr Value of the loop counter at the first iteration. 5395 /// \param Step Amount of increment after each iteration. 5396 /// \param Deref Whether the loop variable is a dereference of the loop 5397 /// counter variable. 5398 /// 5399 /// \return Closure (CapturedStmt) of the loop value calculation. 5400 static CapturedStmt *buildLoopVarFunc(Sema &Actions, QualType LoopVarTy, 5401 QualType LogicalTy, 5402 DeclRefExpr *StartExpr, Expr *Step, 5403 bool Deref) { 5404 ASTContext &Ctx = Actions.getASTContext(); 5405 5406 // Pass the result as an out-parameter. Passing as return value would require 5407 // the OpenMPIRBuilder to know additional C/C++ semantics, such as how to 5408 // invoke a copy constructor. 5409 QualType TargetParamTy = Ctx.getLValueReferenceType(LoopVarTy); 5410 Sema::CapturedParamNameType Params[] = {{"LoopVar", TargetParamTy}, 5411 {"Logical", LogicalTy}, 5412 {StringRef(), QualType()}}; 5413 Actions.ActOnCapturedRegionStart({}, nullptr, CR_Default, Params); 5414 5415 // Capture the initial iterator which represents the LoopVar value at the 5416 // zero's logical iteration. Since the original ForStmt/CXXForRangeStmt update 5417 // it in every iteration, capture it by value before it is modified. 5418 VarDecl *StartVar = cast<VarDecl>(StartExpr->getDecl()); 5419 bool Invalid = Actions.tryCaptureVariable(StartVar, {}, 5420 Sema::TryCapture_ExplicitByVal, {}); 5421 (void)Invalid; 5422 assert(!Invalid && "Expecting capture-by-value to work."); 5423 5424 Expr *Body; 5425 { 5426 Sema::CompoundScopeRAII CompoundScope(Actions); 5427 auto *CS = cast<CapturedDecl>(Actions.CurContext); 5428 5429 ImplicitParamDecl *TargetParam = CS->getParam(0); 5430 DeclRefExpr *TargetRef = Actions.BuildDeclRefExpr( 5431 TargetParam, LoopVarTy, VK_LValue, {}, nullptr, nullptr, {}, nullptr); 5432 ImplicitParamDecl *IndvarParam = CS->getParam(1); 5433 DeclRefExpr *LogicalRef = Actions.BuildDeclRefExpr( 5434 IndvarParam, LogicalTy, VK_LValue, {}, nullptr, nullptr, {}, nullptr); 5435 5436 // Capture the Start expression. 5437 CaptureVars Recap(Actions); 5438 Expr *NewStart = AssertSuccess(Recap.TransformExpr(StartExpr)); 5439 Expr *NewStep = AssertSuccess(Recap.TransformExpr(Step)); 5440 5441 Expr *Skip = AssertSuccess( 5442 Actions.BuildBinOp(nullptr, {}, BO_Mul, NewStep, LogicalRef)); 5443 // TODO: Explicitly cast to the iterator's difference_type instead of 5444 // relying on implicit conversion. 5445 Expr *Advanced = 5446 AssertSuccess(Actions.BuildBinOp(nullptr, {}, BO_Add, NewStart, Skip)); 5447 5448 if (Deref) { 5449 // For range-based for-loops convert the loop counter value to a concrete 5450 // loop variable value by dereferencing the iterator. 5451 Advanced = 5452 AssertSuccess(Actions.BuildUnaryOp(nullptr, {}, UO_Deref, Advanced)); 5453 } 5454 5455 // Assign the result to the output parameter. 5456 Body = AssertSuccess(Actions.BuildBinOp(Actions.getCurScope(), {}, 5457 BO_Assign, TargetRef, Advanced)); 5458 } 5459 return cast<CapturedStmt>( 5460 AssertSuccess(Actions.ActOnCapturedRegionEnd(Body))); 5461 } 5462 5463 StmtResult Sema::ActOnOpenMPCanonicalLoop(Stmt *AStmt) { 5464 ASTContext &Ctx = getASTContext(); 5465 5466 // Extract the common elements of ForStmt and CXXForRangeStmt: 5467 // Loop variable, repeat condition, increment 5468 Expr *Cond, *Inc; 5469 VarDecl *LIVDecl, *LUVDecl; 5470 if (auto *For = dyn_cast<ForStmt>(AStmt)) { 5471 Stmt *Init = For->getInit(); 5472 if (auto *LCVarDeclStmt = dyn_cast<DeclStmt>(Init)) { 5473 // For statement declares loop variable. 5474 LIVDecl = cast<VarDecl>(LCVarDeclStmt->getSingleDecl()); 5475 } else if (auto *LCAssign = dyn_cast<BinaryOperator>(Init)) { 5476 // For statement reuses variable. 5477 assert(LCAssign->getOpcode() == BO_Assign && 5478 "init part must be a loop variable assignment"); 5479 auto *CounterRef = cast<DeclRefExpr>(LCAssign->getLHS()); 5480 LIVDecl = cast<VarDecl>(CounterRef->getDecl()); 5481 } else 5482 llvm_unreachable("Cannot determine loop variable"); 5483 LUVDecl = LIVDecl; 5484 5485 Cond = For->getCond(); 5486 Inc = For->getInc(); 5487 } else if (auto *RangeFor = dyn_cast<CXXForRangeStmt>(AStmt)) { 5488 DeclStmt *BeginStmt = RangeFor->getBeginStmt(); 5489 LIVDecl = cast<VarDecl>(BeginStmt->getSingleDecl()); 5490 LUVDecl = RangeFor->getLoopVariable(); 5491 5492 Cond = RangeFor->getCond(); 5493 Inc = RangeFor->getInc(); 5494 } else 5495 llvm_unreachable("unhandled kind of loop"); 5496 5497 QualType CounterTy = LIVDecl->getType(); 5498 QualType LVTy = LUVDecl->getType(); 5499 5500 // Analyze the loop condition. 5501 Expr *LHS, *RHS; 5502 BinaryOperator::Opcode CondRel; 5503 Cond = Cond->IgnoreImplicit(); 5504 if (auto *CondBinExpr = dyn_cast<BinaryOperator>(Cond)) { 5505 LHS = CondBinExpr->getLHS(); 5506 RHS = CondBinExpr->getRHS(); 5507 CondRel = CondBinExpr->getOpcode(); 5508 } else if (auto *CondCXXOp = dyn_cast<CXXOperatorCallExpr>(Cond)) { 5509 assert(CondCXXOp->getNumArgs() == 2 && "Comparison should have 2 operands"); 5510 LHS = CondCXXOp->getArg(0); 5511 RHS = CondCXXOp->getArg(1); 5512 switch (CondCXXOp->getOperator()) { 5513 case OO_ExclaimEqual: 5514 CondRel = BO_NE; 5515 break; 5516 case OO_Less: 5517 CondRel = BO_LT; 5518 break; 5519 case OO_LessEqual: 5520 CondRel = BO_LE; 5521 break; 5522 case OO_Greater: 5523 CondRel = BO_GT; 5524 break; 5525 case OO_GreaterEqual: 5526 CondRel = BO_GE; 5527 break; 5528 default: 5529 llvm_unreachable("unexpected iterator operator"); 5530 } 5531 } else 5532 llvm_unreachable("unexpected loop condition"); 5533 5534 // Normalize such that the loop counter is on the LHS. 5535 if (!isa<DeclRefExpr>(LHS->IgnoreImplicit()) || 5536 cast<DeclRefExpr>(LHS->IgnoreImplicit())->getDecl() != LIVDecl) { 5537 std::swap(LHS, RHS); 5538 CondRel = BinaryOperator::reverseComparisonOp(CondRel); 5539 } 5540 auto *CounterRef = cast<DeclRefExpr>(LHS->IgnoreImplicit()); 5541 5542 // Decide the bit width for the logical iteration counter. By default use the 5543 // unsigned ptrdiff_t integer size (for iterators and pointers). 5544 // TODO: For iterators, use iterator::difference_type, 5545 // std::iterator_traits<>::difference_type or decltype(it - end). 5546 QualType LogicalTy = Ctx.getUnsignedPointerDiffType(); 5547 if (CounterTy->isIntegerType()) { 5548 unsigned BitWidth = Ctx.getIntWidth(CounterTy); 5549 LogicalTy = Ctx.getIntTypeForBitwidth(BitWidth, false); 5550 } 5551 5552 // Analyze the loop increment. 5553 Expr *Step; 5554 if (auto *IncUn = dyn_cast<UnaryOperator>(Inc)) { 5555 int Direction; 5556 switch (IncUn->getOpcode()) { 5557 case UO_PreInc: 5558 case UO_PostInc: 5559 Direction = 1; 5560 break; 5561 case UO_PreDec: 5562 case UO_PostDec: 5563 Direction = -1; 5564 break; 5565 default: 5566 llvm_unreachable("unhandled unary increment operator"); 5567 } 5568 Step = IntegerLiteral::Create( 5569 Ctx, llvm::APInt(Ctx.getIntWidth(LogicalTy), Direction), LogicalTy, {}); 5570 } else if (auto *IncBin = dyn_cast<BinaryOperator>(Inc)) { 5571 if (IncBin->getOpcode() == BO_AddAssign) { 5572 Step = IncBin->getRHS(); 5573 } else if (IncBin->getOpcode() == BO_SubAssign) { 5574 Step = 5575 AssertSuccess(BuildUnaryOp(nullptr, {}, UO_Minus, IncBin->getRHS())); 5576 } else 5577 llvm_unreachable("unhandled binary increment operator"); 5578 } else if (auto *CondCXXOp = dyn_cast<CXXOperatorCallExpr>(Inc)) { 5579 switch (CondCXXOp->getOperator()) { 5580 case OO_PlusPlus: 5581 Step = IntegerLiteral::Create( 5582 Ctx, llvm::APInt(Ctx.getIntWidth(LogicalTy), 1), LogicalTy, {}); 5583 break; 5584 case OO_MinusMinus: 5585 Step = IntegerLiteral::Create( 5586 Ctx, llvm::APInt(Ctx.getIntWidth(LogicalTy), -1), LogicalTy, {}); 5587 break; 5588 case OO_PlusEqual: 5589 Step = CondCXXOp->getArg(1); 5590 break; 5591 case OO_MinusEqual: 5592 Step = AssertSuccess( 5593 BuildUnaryOp(nullptr, {}, UO_Minus, CondCXXOp->getArg(1))); 5594 break; 5595 default: 5596 llvm_unreachable("unhandled overloaded increment operator"); 5597 } 5598 } else 5599 llvm_unreachable("unknown increment expression"); 5600 5601 CapturedStmt *DistanceFunc = 5602 buildDistanceFunc(*this, LogicalTy, CondRel, LHS, RHS, Step); 5603 CapturedStmt *LoopVarFunc = buildLoopVarFunc( 5604 *this, LVTy, LogicalTy, CounterRef, Step, isa<CXXForRangeStmt>(AStmt)); 5605 DeclRefExpr *LVRef = BuildDeclRefExpr(LUVDecl, LUVDecl->getType(), VK_LValue, 5606 {}, nullptr, nullptr, {}, nullptr); 5607 return OMPCanonicalLoop::create(getASTContext(), AStmt, DistanceFunc, 5608 LoopVarFunc, LVRef); 5609 } 5610 5611 StmtResult Sema::ActOnOpenMPLoopnest(Stmt *AStmt) { 5612 // Handle a literal loop. 5613 if (isa<ForStmt>(AStmt) || isa<CXXForRangeStmt>(AStmt)) 5614 return ActOnOpenMPCanonicalLoop(AStmt); 5615 5616 // If not a literal loop, it must be the result of a loop transformation. 5617 OMPExecutableDirective *LoopTransform = cast<OMPExecutableDirective>(AStmt); 5618 assert( 5619 isOpenMPLoopTransformationDirective(LoopTransform->getDirectiveKind()) && 5620 "Loop transformation directive expected"); 5621 return LoopTransform; 5622 } 5623 5624 static ExprResult buildUserDefinedMapperRef(Sema &SemaRef, Scope *S, 5625 CXXScopeSpec &MapperIdScopeSpec, 5626 const DeclarationNameInfo &MapperId, 5627 QualType Type, 5628 Expr *UnresolvedMapper); 5629 5630 /// Perform DFS through the structure/class data members trying to find 5631 /// member(s) with user-defined 'default' mapper and generate implicit map 5632 /// clauses for such members with the found 'default' mapper. 5633 static void 5634 processImplicitMapsWithDefaultMappers(Sema &S, DSAStackTy *Stack, 5635 SmallVectorImpl<OMPClause *> &Clauses) { 5636 // Check for the deault mapper for data members. 5637 if (S.getLangOpts().OpenMP < 50) 5638 return; 5639 SmallVector<OMPClause *, 4> ImplicitMaps; 5640 for (int Cnt = 0, EndCnt = Clauses.size(); Cnt < EndCnt; ++Cnt) { 5641 auto *C = dyn_cast<OMPMapClause>(Clauses[Cnt]); 5642 if (!C) 5643 continue; 5644 SmallVector<Expr *, 4> SubExprs; 5645 auto *MI = C->mapperlist_begin(); 5646 for (auto I = C->varlist_begin(), End = C->varlist_end(); I != End; 5647 ++I, ++MI) { 5648 // Expression is mapped using mapper - skip it. 5649 if (*MI) 5650 continue; 5651 Expr *E = *I; 5652 // Expression is dependent - skip it, build the mapper when it gets 5653 // instantiated. 5654 if (E->isTypeDependent() || E->isValueDependent() || 5655 E->containsUnexpandedParameterPack()) 5656 continue; 5657 // Array section - need to check for the mapping of the array section 5658 // element. 5659 QualType CanonType = E->getType().getCanonicalType(); 5660 if (CanonType->isSpecificBuiltinType(BuiltinType::OMPArraySection)) { 5661 const auto *OASE = cast<OMPArraySectionExpr>(E->IgnoreParenImpCasts()); 5662 QualType BaseType = 5663 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 5664 QualType ElemType; 5665 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 5666 ElemType = ATy->getElementType(); 5667 else 5668 ElemType = BaseType->getPointeeType(); 5669 CanonType = ElemType; 5670 } 5671 5672 // DFS over data members in structures/classes. 5673 SmallVector<std::pair<QualType, FieldDecl *>, 4> Types( 5674 1, {CanonType, nullptr}); 5675 llvm::DenseMap<const Type *, Expr *> Visited; 5676 SmallVector<std::pair<FieldDecl *, unsigned>, 4> ParentChain( 5677 1, {nullptr, 1}); 5678 while (!Types.empty()) { 5679 QualType BaseType; 5680 FieldDecl *CurFD; 5681 std::tie(BaseType, CurFD) = Types.pop_back_val(); 5682 while (ParentChain.back().second == 0) 5683 ParentChain.pop_back(); 5684 --ParentChain.back().second; 5685 if (BaseType.isNull()) 5686 continue; 5687 // Only structs/classes are allowed to have mappers. 5688 const RecordDecl *RD = BaseType.getCanonicalType()->getAsRecordDecl(); 5689 if (!RD) 5690 continue; 5691 auto It = Visited.find(BaseType.getTypePtr()); 5692 if (It == Visited.end()) { 5693 // Try to find the associated user-defined mapper. 5694 CXXScopeSpec MapperIdScopeSpec; 5695 DeclarationNameInfo DefaultMapperId; 5696 DefaultMapperId.setName(S.Context.DeclarationNames.getIdentifier( 5697 &S.Context.Idents.get("default"))); 5698 DefaultMapperId.setLoc(E->getExprLoc()); 5699 ExprResult ER = buildUserDefinedMapperRef( 5700 S, Stack->getCurScope(), MapperIdScopeSpec, DefaultMapperId, 5701 BaseType, /*UnresolvedMapper=*/nullptr); 5702 if (ER.isInvalid()) 5703 continue; 5704 It = Visited.try_emplace(BaseType.getTypePtr(), ER.get()).first; 5705 } 5706 // Found default mapper. 5707 if (It->second) { 5708 auto *OE = new (S.Context) OpaqueValueExpr(E->getExprLoc(), CanonType, 5709 VK_LValue, OK_Ordinary, E); 5710 OE->setIsUnique(/*V=*/true); 5711 Expr *BaseExpr = OE; 5712 for (const auto &P : ParentChain) { 5713 if (P.first) { 5714 BaseExpr = S.BuildMemberExpr( 5715 BaseExpr, /*IsArrow=*/false, E->getExprLoc(), 5716 NestedNameSpecifierLoc(), SourceLocation(), P.first, 5717 DeclAccessPair::make(P.first, P.first->getAccess()), 5718 /*HadMultipleCandidates=*/false, DeclarationNameInfo(), 5719 P.first->getType(), VK_LValue, OK_Ordinary); 5720 BaseExpr = S.DefaultLvalueConversion(BaseExpr).get(); 5721 } 5722 } 5723 if (CurFD) 5724 BaseExpr = S.BuildMemberExpr( 5725 BaseExpr, /*IsArrow=*/false, E->getExprLoc(), 5726 NestedNameSpecifierLoc(), SourceLocation(), CurFD, 5727 DeclAccessPair::make(CurFD, CurFD->getAccess()), 5728 /*HadMultipleCandidates=*/false, DeclarationNameInfo(), 5729 CurFD->getType(), VK_LValue, OK_Ordinary); 5730 SubExprs.push_back(BaseExpr); 5731 continue; 5732 } 5733 // Check for the "default" mapper for data members. 5734 bool FirstIter = true; 5735 for (FieldDecl *FD : RD->fields()) { 5736 if (!FD) 5737 continue; 5738 QualType FieldTy = FD->getType(); 5739 if (FieldTy.isNull() || 5740 !(FieldTy->isStructureOrClassType() || FieldTy->isUnionType())) 5741 continue; 5742 if (FirstIter) { 5743 FirstIter = false; 5744 ParentChain.emplace_back(CurFD, 1); 5745 } else { 5746 ++ParentChain.back().second; 5747 } 5748 Types.emplace_back(FieldTy, FD); 5749 } 5750 } 5751 } 5752 if (SubExprs.empty()) 5753 continue; 5754 CXXScopeSpec MapperIdScopeSpec; 5755 DeclarationNameInfo MapperId; 5756 if (OMPClause *NewClause = S.ActOnOpenMPMapClause( 5757 C->getMapTypeModifiers(), C->getMapTypeModifiersLoc(), 5758 MapperIdScopeSpec, MapperId, C->getMapType(), 5759 /*IsMapTypeImplicit=*/true, SourceLocation(), SourceLocation(), 5760 SubExprs, OMPVarListLocTy())) 5761 Clauses.push_back(NewClause); 5762 } 5763 } 5764 5765 StmtResult Sema::ActOnOpenMPExecutableDirective( 5766 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName, 5767 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses, 5768 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { 5769 StmtResult Res = StmtError(); 5770 // First check CancelRegion which is then used in checkNestingOfRegions. 5771 if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) || 5772 checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion, 5773 StartLoc)) 5774 return StmtError(); 5775 5776 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit; 5777 VarsWithInheritedDSAType VarsWithInheritedDSA; 5778 bool ErrorFound = false; 5779 ClausesWithImplicit.append(Clauses.begin(), Clauses.end()); 5780 if (AStmt && !CurContext->isDependentContext() && Kind != OMPD_atomic && 5781 Kind != OMPD_critical && Kind != OMPD_section && Kind != OMPD_master && 5782 Kind != OMPD_masked && !isOpenMPLoopTransformationDirective(Kind)) { 5783 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5784 5785 // Check default data sharing attributes for referenced variables. 5786 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt)); 5787 int ThisCaptureLevel = getOpenMPCaptureLevels(Kind); 5788 Stmt *S = AStmt; 5789 while (--ThisCaptureLevel >= 0) 5790 S = cast<CapturedStmt>(S)->getCapturedStmt(); 5791 DSAChecker.Visit(S); 5792 if (!isOpenMPTargetDataManagementDirective(Kind) && 5793 !isOpenMPTaskingDirective(Kind)) { 5794 // Visit subcaptures to generate implicit clauses for captured vars. 5795 auto *CS = cast<CapturedStmt>(AStmt); 5796 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 5797 getOpenMPCaptureRegions(CaptureRegions, Kind); 5798 // Ignore outer tasking regions for target directives. 5799 if (CaptureRegions.size() > 1 && CaptureRegions.front() == OMPD_task) 5800 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 5801 DSAChecker.visitSubCaptures(CS); 5802 } 5803 if (DSAChecker.isErrorFound()) 5804 return StmtError(); 5805 // Generate list of implicitly defined firstprivate variables. 5806 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA(); 5807 5808 SmallVector<Expr *, 4> ImplicitFirstprivates( 5809 DSAChecker.getImplicitFirstprivate().begin(), 5810 DSAChecker.getImplicitFirstprivate().end()); 5811 const unsigned DefaultmapKindNum = OMPC_DEFAULTMAP_pointer + 1; 5812 SmallVector<Expr *, 4> ImplicitMaps[DefaultmapKindNum][OMPC_MAP_delete]; 5813 SmallVector<OpenMPMapModifierKind, NumberOfOMPMapClauseModifiers> 5814 ImplicitMapModifiers[DefaultmapKindNum]; 5815 SmallVector<SourceLocation, NumberOfOMPMapClauseModifiers> 5816 ImplicitMapModifiersLoc[DefaultmapKindNum]; 5817 // Get the original location of present modifier from Defaultmap clause. 5818 SourceLocation PresentModifierLocs[DefaultmapKindNum]; 5819 for (OMPClause *C : Clauses) { 5820 if (auto *DMC = dyn_cast<OMPDefaultmapClause>(C)) 5821 if (DMC->getDefaultmapModifier() == OMPC_DEFAULTMAP_MODIFIER_present) 5822 PresentModifierLocs[DMC->getDefaultmapKind()] = 5823 DMC->getDefaultmapModifierLoc(); 5824 } 5825 for (unsigned VC = 0; VC < DefaultmapKindNum; ++VC) { 5826 auto Kind = static_cast<OpenMPDefaultmapClauseKind>(VC); 5827 for (unsigned I = 0; I < OMPC_MAP_delete; ++I) { 5828 ArrayRef<Expr *> ImplicitMap = DSAChecker.getImplicitMap( 5829 Kind, static_cast<OpenMPMapClauseKind>(I)); 5830 ImplicitMaps[VC][I].append(ImplicitMap.begin(), ImplicitMap.end()); 5831 } 5832 ArrayRef<OpenMPMapModifierKind> ImplicitModifier = 5833 DSAChecker.getImplicitMapModifier(Kind); 5834 ImplicitMapModifiers[VC].append(ImplicitModifier.begin(), 5835 ImplicitModifier.end()); 5836 std::fill_n(std::back_inserter(ImplicitMapModifiersLoc[VC]), 5837 ImplicitModifier.size(), PresentModifierLocs[VC]); 5838 } 5839 // Mark taskgroup task_reduction descriptors as implicitly firstprivate. 5840 for (OMPClause *C : Clauses) { 5841 if (auto *IRC = dyn_cast<OMPInReductionClause>(C)) { 5842 for (Expr *E : IRC->taskgroup_descriptors()) 5843 if (E) 5844 ImplicitFirstprivates.emplace_back(E); 5845 } 5846 // OpenMP 5.0, 2.10.1 task Construct 5847 // [detach clause]... The event-handle will be considered as if it was 5848 // specified on a firstprivate clause. 5849 if (auto *DC = dyn_cast<OMPDetachClause>(C)) 5850 ImplicitFirstprivates.push_back(DC->getEventHandler()); 5851 } 5852 if (!ImplicitFirstprivates.empty()) { 5853 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause( 5854 ImplicitFirstprivates, SourceLocation(), SourceLocation(), 5855 SourceLocation())) { 5856 ClausesWithImplicit.push_back(Implicit); 5857 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() != 5858 ImplicitFirstprivates.size(); 5859 } else { 5860 ErrorFound = true; 5861 } 5862 } 5863 // OpenMP 5.0 [2.19.7] 5864 // If a list item appears in a reduction, lastprivate or linear 5865 // clause on a combined target construct then it is treated as 5866 // if it also appears in a map clause with a map-type of tofrom 5867 if (getLangOpts().OpenMP >= 50 && Kind != OMPD_target && 5868 isOpenMPTargetExecutionDirective(Kind)) { 5869 SmallVector<Expr *, 4> ImplicitExprs; 5870 for (OMPClause *C : Clauses) { 5871 if (auto *RC = dyn_cast<OMPReductionClause>(C)) 5872 for (Expr *E : RC->varlists()) 5873 if (!isa<DeclRefExpr>(E->IgnoreParenImpCasts())) 5874 ImplicitExprs.emplace_back(E); 5875 } 5876 if (!ImplicitExprs.empty()) { 5877 ArrayRef<Expr *> Exprs = ImplicitExprs; 5878 CXXScopeSpec MapperIdScopeSpec; 5879 DeclarationNameInfo MapperId; 5880 if (OMPClause *Implicit = ActOnOpenMPMapClause( 5881 OMPC_MAP_MODIFIER_unknown, SourceLocation(), MapperIdScopeSpec, 5882 MapperId, OMPC_MAP_tofrom, 5883 /*IsMapTypeImplicit=*/true, SourceLocation(), SourceLocation(), 5884 Exprs, OMPVarListLocTy(), /*NoDiagnose=*/true)) 5885 ClausesWithImplicit.emplace_back(Implicit); 5886 } 5887 } 5888 for (unsigned I = 0, E = DefaultmapKindNum; I < E; ++I) { 5889 int ClauseKindCnt = -1; 5890 for (ArrayRef<Expr *> ImplicitMap : ImplicitMaps[I]) { 5891 ++ClauseKindCnt; 5892 if (ImplicitMap.empty()) 5893 continue; 5894 CXXScopeSpec MapperIdScopeSpec; 5895 DeclarationNameInfo MapperId; 5896 auto Kind = static_cast<OpenMPMapClauseKind>(ClauseKindCnt); 5897 if (OMPClause *Implicit = ActOnOpenMPMapClause( 5898 ImplicitMapModifiers[I], ImplicitMapModifiersLoc[I], 5899 MapperIdScopeSpec, MapperId, Kind, /*IsMapTypeImplicit=*/true, 5900 SourceLocation(), SourceLocation(), ImplicitMap, 5901 OMPVarListLocTy())) { 5902 ClausesWithImplicit.emplace_back(Implicit); 5903 ErrorFound |= cast<OMPMapClause>(Implicit)->varlist_size() != 5904 ImplicitMap.size(); 5905 } else { 5906 ErrorFound = true; 5907 } 5908 } 5909 } 5910 // Build expressions for implicit maps of data members with 'default' 5911 // mappers. 5912 if (LangOpts.OpenMP >= 50) 5913 processImplicitMapsWithDefaultMappers(*this, DSAStack, 5914 ClausesWithImplicit); 5915 } 5916 5917 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers; 5918 switch (Kind) { 5919 case OMPD_parallel: 5920 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc, 5921 EndLoc); 5922 AllowedNameModifiers.push_back(OMPD_parallel); 5923 break; 5924 case OMPD_simd: 5925 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, 5926 VarsWithInheritedDSA); 5927 if (LangOpts.OpenMP >= 50) 5928 AllowedNameModifiers.push_back(OMPD_simd); 5929 break; 5930 case OMPD_tile: 5931 Res = 5932 ActOnOpenMPTileDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 5933 break; 5934 case OMPD_unroll: 5935 Res = ActOnOpenMPUnrollDirective(ClausesWithImplicit, AStmt, StartLoc, 5936 EndLoc); 5937 break; 5938 case OMPD_for: 5939 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, 5940 VarsWithInheritedDSA); 5941 break; 5942 case OMPD_for_simd: 5943 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 5944 EndLoc, VarsWithInheritedDSA); 5945 if (LangOpts.OpenMP >= 50) 5946 AllowedNameModifiers.push_back(OMPD_simd); 5947 break; 5948 case OMPD_sections: 5949 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc, 5950 EndLoc); 5951 break; 5952 case OMPD_section: 5953 assert(ClausesWithImplicit.empty() && 5954 "No clauses are allowed for 'omp section' directive"); 5955 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc); 5956 break; 5957 case OMPD_single: 5958 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc, 5959 EndLoc); 5960 break; 5961 case OMPD_master: 5962 assert(ClausesWithImplicit.empty() && 5963 "No clauses are allowed for 'omp master' directive"); 5964 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc); 5965 break; 5966 case OMPD_masked: 5967 Res = ActOnOpenMPMaskedDirective(ClausesWithImplicit, AStmt, StartLoc, 5968 EndLoc); 5969 break; 5970 case OMPD_critical: 5971 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt, 5972 StartLoc, EndLoc); 5973 break; 5974 case OMPD_parallel_for: 5975 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc, 5976 EndLoc, VarsWithInheritedDSA); 5977 AllowedNameModifiers.push_back(OMPD_parallel); 5978 break; 5979 case OMPD_parallel_for_simd: 5980 Res = ActOnOpenMPParallelForSimdDirective( 5981 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5982 AllowedNameModifiers.push_back(OMPD_parallel); 5983 if (LangOpts.OpenMP >= 50) 5984 AllowedNameModifiers.push_back(OMPD_simd); 5985 break; 5986 case OMPD_parallel_master: 5987 Res = ActOnOpenMPParallelMasterDirective(ClausesWithImplicit, AStmt, 5988 StartLoc, EndLoc); 5989 AllowedNameModifiers.push_back(OMPD_parallel); 5990 break; 5991 case OMPD_parallel_sections: 5992 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt, 5993 StartLoc, EndLoc); 5994 AllowedNameModifiers.push_back(OMPD_parallel); 5995 break; 5996 case OMPD_task: 5997 Res = 5998 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 5999 AllowedNameModifiers.push_back(OMPD_task); 6000 break; 6001 case OMPD_taskyield: 6002 assert(ClausesWithImplicit.empty() && 6003 "No clauses are allowed for 'omp taskyield' directive"); 6004 assert(AStmt == nullptr && 6005 "No associated statement allowed for 'omp taskyield' directive"); 6006 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc); 6007 break; 6008 case OMPD_barrier: 6009 assert(ClausesWithImplicit.empty() && 6010 "No clauses are allowed for 'omp barrier' directive"); 6011 assert(AStmt == nullptr && 6012 "No associated statement allowed for 'omp barrier' directive"); 6013 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc); 6014 break; 6015 case OMPD_taskwait: 6016 assert(ClausesWithImplicit.empty() && 6017 "No clauses are allowed for 'omp taskwait' directive"); 6018 assert(AStmt == nullptr && 6019 "No associated statement allowed for 'omp taskwait' directive"); 6020 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc); 6021 break; 6022 case OMPD_taskgroup: 6023 Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc, 6024 EndLoc); 6025 break; 6026 case OMPD_flush: 6027 assert(AStmt == nullptr && 6028 "No associated statement allowed for 'omp flush' directive"); 6029 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc); 6030 break; 6031 case OMPD_depobj: 6032 assert(AStmt == nullptr && 6033 "No associated statement allowed for 'omp depobj' directive"); 6034 Res = ActOnOpenMPDepobjDirective(ClausesWithImplicit, StartLoc, EndLoc); 6035 break; 6036 case OMPD_scan: 6037 assert(AStmt == nullptr && 6038 "No associated statement allowed for 'omp scan' directive"); 6039 Res = ActOnOpenMPScanDirective(ClausesWithImplicit, StartLoc, EndLoc); 6040 break; 6041 case OMPD_ordered: 6042 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc, 6043 EndLoc); 6044 break; 6045 case OMPD_atomic: 6046 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc, 6047 EndLoc); 6048 break; 6049 case OMPD_teams: 6050 Res = 6051 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 6052 break; 6053 case OMPD_target: 6054 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc, 6055 EndLoc); 6056 AllowedNameModifiers.push_back(OMPD_target); 6057 break; 6058 case OMPD_target_parallel: 6059 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt, 6060 StartLoc, EndLoc); 6061 AllowedNameModifiers.push_back(OMPD_target); 6062 AllowedNameModifiers.push_back(OMPD_parallel); 6063 break; 6064 case OMPD_target_parallel_for: 6065 Res = ActOnOpenMPTargetParallelForDirective( 6066 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6067 AllowedNameModifiers.push_back(OMPD_target); 6068 AllowedNameModifiers.push_back(OMPD_parallel); 6069 break; 6070 case OMPD_cancellation_point: 6071 assert(ClausesWithImplicit.empty() && 6072 "No clauses are allowed for 'omp cancellation point' directive"); 6073 assert(AStmt == nullptr && "No associated statement allowed for 'omp " 6074 "cancellation point' directive"); 6075 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion); 6076 break; 6077 case OMPD_cancel: 6078 assert(AStmt == nullptr && 6079 "No associated statement allowed for 'omp cancel' directive"); 6080 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc, 6081 CancelRegion); 6082 AllowedNameModifiers.push_back(OMPD_cancel); 6083 break; 6084 case OMPD_target_data: 6085 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc, 6086 EndLoc); 6087 AllowedNameModifiers.push_back(OMPD_target_data); 6088 break; 6089 case OMPD_target_enter_data: 6090 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc, 6091 EndLoc, AStmt); 6092 AllowedNameModifiers.push_back(OMPD_target_enter_data); 6093 break; 6094 case OMPD_target_exit_data: 6095 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc, 6096 EndLoc, AStmt); 6097 AllowedNameModifiers.push_back(OMPD_target_exit_data); 6098 break; 6099 case OMPD_taskloop: 6100 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc, 6101 EndLoc, VarsWithInheritedDSA); 6102 AllowedNameModifiers.push_back(OMPD_taskloop); 6103 break; 6104 case OMPD_taskloop_simd: 6105 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 6106 EndLoc, VarsWithInheritedDSA); 6107 AllowedNameModifiers.push_back(OMPD_taskloop); 6108 if (LangOpts.OpenMP >= 50) 6109 AllowedNameModifiers.push_back(OMPD_simd); 6110 break; 6111 case OMPD_master_taskloop: 6112 Res = ActOnOpenMPMasterTaskLoopDirective( 6113 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6114 AllowedNameModifiers.push_back(OMPD_taskloop); 6115 break; 6116 case OMPD_master_taskloop_simd: 6117 Res = ActOnOpenMPMasterTaskLoopSimdDirective( 6118 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6119 AllowedNameModifiers.push_back(OMPD_taskloop); 6120 if (LangOpts.OpenMP >= 50) 6121 AllowedNameModifiers.push_back(OMPD_simd); 6122 break; 6123 case OMPD_parallel_master_taskloop: 6124 Res = ActOnOpenMPParallelMasterTaskLoopDirective( 6125 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6126 AllowedNameModifiers.push_back(OMPD_taskloop); 6127 AllowedNameModifiers.push_back(OMPD_parallel); 6128 break; 6129 case OMPD_parallel_master_taskloop_simd: 6130 Res = ActOnOpenMPParallelMasterTaskLoopSimdDirective( 6131 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6132 AllowedNameModifiers.push_back(OMPD_taskloop); 6133 AllowedNameModifiers.push_back(OMPD_parallel); 6134 if (LangOpts.OpenMP >= 50) 6135 AllowedNameModifiers.push_back(OMPD_simd); 6136 break; 6137 case OMPD_distribute: 6138 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc, 6139 EndLoc, VarsWithInheritedDSA); 6140 break; 6141 case OMPD_target_update: 6142 Res = ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc, 6143 EndLoc, AStmt); 6144 AllowedNameModifiers.push_back(OMPD_target_update); 6145 break; 6146 case OMPD_distribute_parallel_for: 6147 Res = ActOnOpenMPDistributeParallelForDirective( 6148 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6149 AllowedNameModifiers.push_back(OMPD_parallel); 6150 break; 6151 case OMPD_distribute_parallel_for_simd: 6152 Res = ActOnOpenMPDistributeParallelForSimdDirective( 6153 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6154 AllowedNameModifiers.push_back(OMPD_parallel); 6155 if (LangOpts.OpenMP >= 50) 6156 AllowedNameModifiers.push_back(OMPD_simd); 6157 break; 6158 case OMPD_distribute_simd: 6159 Res = ActOnOpenMPDistributeSimdDirective( 6160 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6161 if (LangOpts.OpenMP >= 50) 6162 AllowedNameModifiers.push_back(OMPD_simd); 6163 break; 6164 case OMPD_target_parallel_for_simd: 6165 Res = ActOnOpenMPTargetParallelForSimdDirective( 6166 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6167 AllowedNameModifiers.push_back(OMPD_target); 6168 AllowedNameModifiers.push_back(OMPD_parallel); 6169 if (LangOpts.OpenMP >= 50) 6170 AllowedNameModifiers.push_back(OMPD_simd); 6171 break; 6172 case OMPD_target_simd: 6173 Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 6174 EndLoc, VarsWithInheritedDSA); 6175 AllowedNameModifiers.push_back(OMPD_target); 6176 if (LangOpts.OpenMP >= 50) 6177 AllowedNameModifiers.push_back(OMPD_simd); 6178 break; 6179 case OMPD_teams_distribute: 6180 Res = ActOnOpenMPTeamsDistributeDirective( 6181 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6182 break; 6183 case OMPD_teams_distribute_simd: 6184 Res = ActOnOpenMPTeamsDistributeSimdDirective( 6185 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6186 if (LangOpts.OpenMP >= 50) 6187 AllowedNameModifiers.push_back(OMPD_simd); 6188 break; 6189 case OMPD_teams_distribute_parallel_for_simd: 6190 Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective( 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_teams_distribute_parallel_for: 6197 Res = ActOnOpenMPTeamsDistributeParallelForDirective( 6198 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6199 AllowedNameModifiers.push_back(OMPD_parallel); 6200 break; 6201 case OMPD_target_teams: 6202 Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, 6203 EndLoc); 6204 AllowedNameModifiers.push_back(OMPD_target); 6205 break; 6206 case OMPD_target_teams_distribute: 6207 Res = ActOnOpenMPTargetTeamsDistributeDirective( 6208 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6209 AllowedNameModifiers.push_back(OMPD_target); 6210 break; 6211 case OMPD_target_teams_distribute_parallel_for: 6212 Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective( 6213 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6214 AllowedNameModifiers.push_back(OMPD_target); 6215 AllowedNameModifiers.push_back(OMPD_parallel); 6216 break; 6217 case OMPD_target_teams_distribute_parallel_for_simd: 6218 Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective( 6219 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6220 AllowedNameModifiers.push_back(OMPD_target); 6221 AllowedNameModifiers.push_back(OMPD_parallel); 6222 if (LangOpts.OpenMP >= 50) 6223 AllowedNameModifiers.push_back(OMPD_simd); 6224 break; 6225 case OMPD_target_teams_distribute_simd: 6226 Res = ActOnOpenMPTargetTeamsDistributeSimdDirective( 6227 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6228 AllowedNameModifiers.push_back(OMPD_target); 6229 if (LangOpts.OpenMP >= 50) 6230 AllowedNameModifiers.push_back(OMPD_simd); 6231 break; 6232 case OMPD_interop: 6233 assert(AStmt == nullptr && 6234 "No associated statement allowed for 'omp interop' directive"); 6235 Res = ActOnOpenMPInteropDirective(ClausesWithImplicit, StartLoc, EndLoc); 6236 break; 6237 case OMPD_dispatch: 6238 Res = ActOnOpenMPDispatchDirective(ClausesWithImplicit, AStmt, StartLoc, 6239 EndLoc); 6240 break; 6241 case OMPD_declare_target: 6242 case OMPD_end_declare_target: 6243 case OMPD_threadprivate: 6244 case OMPD_allocate: 6245 case OMPD_declare_reduction: 6246 case OMPD_declare_mapper: 6247 case OMPD_declare_simd: 6248 case OMPD_requires: 6249 case OMPD_declare_variant: 6250 case OMPD_begin_declare_variant: 6251 case OMPD_end_declare_variant: 6252 llvm_unreachable("OpenMP Directive is not allowed"); 6253 case OMPD_unknown: 6254 default: 6255 llvm_unreachable("Unknown OpenMP directive"); 6256 } 6257 6258 ErrorFound = Res.isInvalid() || ErrorFound; 6259 6260 // Check variables in the clauses if default(none) or 6261 // default(firstprivate) was specified. 6262 if (DSAStack->getDefaultDSA() == DSA_none || 6263 DSAStack->getDefaultDSA() == DSA_firstprivate) { 6264 DSAAttrChecker DSAChecker(DSAStack, *this, nullptr); 6265 for (OMPClause *C : Clauses) { 6266 switch (C->getClauseKind()) { 6267 case OMPC_num_threads: 6268 case OMPC_dist_schedule: 6269 // Do not analyse if no parent teams directive. 6270 if (isOpenMPTeamsDirective(Kind)) 6271 break; 6272 continue; 6273 case OMPC_if: 6274 if (isOpenMPTeamsDirective(Kind) && 6275 cast<OMPIfClause>(C)->getNameModifier() != OMPD_target) 6276 break; 6277 if (isOpenMPParallelDirective(Kind) && 6278 isOpenMPTaskLoopDirective(Kind) && 6279 cast<OMPIfClause>(C)->getNameModifier() != OMPD_parallel) 6280 break; 6281 continue; 6282 case OMPC_schedule: 6283 case OMPC_detach: 6284 break; 6285 case OMPC_grainsize: 6286 case OMPC_num_tasks: 6287 case OMPC_final: 6288 case OMPC_priority: 6289 case OMPC_novariants: 6290 case OMPC_nocontext: 6291 // Do not analyze if no parent parallel directive. 6292 if (isOpenMPParallelDirective(Kind)) 6293 break; 6294 continue; 6295 case OMPC_ordered: 6296 case OMPC_device: 6297 case OMPC_num_teams: 6298 case OMPC_thread_limit: 6299 case OMPC_hint: 6300 case OMPC_collapse: 6301 case OMPC_safelen: 6302 case OMPC_simdlen: 6303 case OMPC_sizes: 6304 case OMPC_default: 6305 case OMPC_proc_bind: 6306 case OMPC_private: 6307 case OMPC_firstprivate: 6308 case OMPC_lastprivate: 6309 case OMPC_shared: 6310 case OMPC_reduction: 6311 case OMPC_task_reduction: 6312 case OMPC_in_reduction: 6313 case OMPC_linear: 6314 case OMPC_aligned: 6315 case OMPC_copyin: 6316 case OMPC_copyprivate: 6317 case OMPC_nowait: 6318 case OMPC_untied: 6319 case OMPC_mergeable: 6320 case OMPC_allocate: 6321 case OMPC_read: 6322 case OMPC_write: 6323 case OMPC_update: 6324 case OMPC_capture: 6325 case OMPC_seq_cst: 6326 case OMPC_acq_rel: 6327 case OMPC_acquire: 6328 case OMPC_release: 6329 case OMPC_relaxed: 6330 case OMPC_depend: 6331 case OMPC_threads: 6332 case OMPC_simd: 6333 case OMPC_map: 6334 case OMPC_nogroup: 6335 case OMPC_defaultmap: 6336 case OMPC_to: 6337 case OMPC_from: 6338 case OMPC_use_device_ptr: 6339 case OMPC_use_device_addr: 6340 case OMPC_is_device_ptr: 6341 case OMPC_nontemporal: 6342 case OMPC_order: 6343 case OMPC_destroy: 6344 case OMPC_inclusive: 6345 case OMPC_exclusive: 6346 case OMPC_uses_allocators: 6347 case OMPC_affinity: 6348 continue; 6349 case OMPC_allocator: 6350 case OMPC_flush: 6351 case OMPC_depobj: 6352 case OMPC_threadprivate: 6353 case OMPC_uniform: 6354 case OMPC_unknown: 6355 case OMPC_unified_address: 6356 case OMPC_unified_shared_memory: 6357 case OMPC_reverse_offload: 6358 case OMPC_dynamic_allocators: 6359 case OMPC_atomic_default_mem_order: 6360 case OMPC_device_type: 6361 case OMPC_match: 6362 case OMPC_when: 6363 default: 6364 llvm_unreachable("Unexpected clause"); 6365 } 6366 for (Stmt *CC : C->children()) { 6367 if (CC) 6368 DSAChecker.Visit(CC); 6369 } 6370 } 6371 for (const auto &P : DSAChecker.getVarsWithInheritedDSA()) 6372 VarsWithInheritedDSA[P.getFirst()] = P.getSecond(); 6373 } 6374 for (const auto &P : VarsWithInheritedDSA) { 6375 if (P.getFirst()->isImplicit() || isa<OMPCapturedExprDecl>(P.getFirst())) 6376 continue; 6377 ErrorFound = true; 6378 if (DSAStack->getDefaultDSA() == DSA_none || 6379 DSAStack->getDefaultDSA() == DSA_firstprivate) { 6380 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable) 6381 << P.first << P.second->getSourceRange(); 6382 Diag(DSAStack->getDefaultDSALocation(), diag::note_omp_default_dsa_none); 6383 } else if (getLangOpts().OpenMP >= 50) { 6384 Diag(P.second->getExprLoc(), 6385 diag::err_omp_defaultmap_no_attr_for_variable) 6386 << P.first << P.second->getSourceRange(); 6387 Diag(DSAStack->getDefaultDSALocation(), 6388 diag::note_omp_defaultmap_attr_none); 6389 } 6390 } 6391 6392 if (!AllowedNameModifiers.empty()) 6393 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) || 6394 ErrorFound; 6395 6396 if (ErrorFound) 6397 return StmtError(); 6398 6399 if (!CurContext->isDependentContext() && 6400 isOpenMPTargetExecutionDirective(Kind) && 6401 !(DSAStack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() || 6402 DSAStack->hasRequiresDeclWithClause<OMPUnifiedAddressClause>() || 6403 DSAStack->hasRequiresDeclWithClause<OMPReverseOffloadClause>() || 6404 DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())) { 6405 // Register target to DSA Stack. 6406 DSAStack->addTargetDirLocation(StartLoc); 6407 } 6408 6409 return Res; 6410 } 6411 6412 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective( 6413 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen, 6414 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds, 6415 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears, 6416 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) { 6417 assert(Aligneds.size() == Alignments.size()); 6418 assert(Linears.size() == LinModifiers.size()); 6419 assert(Linears.size() == Steps.size()); 6420 if (!DG || DG.get().isNull()) 6421 return DeclGroupPtrTy(); 6422 6423 const int SimdId = 0; 6424 if (!DG.get().isSingleDecl()) { 6425 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd_variant) 6426 << SimdId; 6427 return DG; 6428 } 6429 Decl *ADecl = DG.get().getSingleDecl(); 6430 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl)) 6431 ADecl = FTD->getTemplatedDecl(); 6432 6433 auto *FD = dyn_cast<FunctionDecl>(ADecl); 6434 if (!FD) { 6435 Diag(ADecl->getLocation(), diag::err_omp_function_expected) << SimdId; 6436 return DeclGroupPtrTy(); 6437 } 6438 6439 // OpenMP [2.8.2, declare simd construct, Description] 6440 // The parameter of the simdlen clause must be a constant positive integer 6441 // expression. 6442 ExprResult SL; 6443 if (Simdlen) 6444 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen); 6445 // OpenMP [2.8.2, declare simd construct, Description] 6446 // The special this pointer can be used as if was one of the arguments to the 6447 // function in any of the linear, aligned, or uniform clauses. 6448 // The uniform clause declares one or more arguments to have an invariant 6449 // value for all concurrent invocations of the function in the execution of a 6450 // single SIMD loop. 6451 llvm::DenseMap<const Decl *, const Expr *> UniformedArgs; 6452 const Expr *UniformedLinearThis = nullptr; 6453 for (const Expr *E : Uniforms) { 6454 E = E->IgnoreParenImpCasts(); 6455 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 6456 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) 6457 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 6458 FD->getParamDecl(PVD->getFunctionScopeIndex()) 6459 ->getCanonicalDecl() == PVD->getCanonicalDecl()) { 6460 UniformedArgs.try_emplace(PVD->getCanonicalDecl(), E); 6461 continue; 6462 } 6463 if (isa<CXXThisExpr>(E)) { 6464 UniformedLinearThis = E; 6465 continue; 6466 } 6467 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 6468 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 6469 } 6470 // OpenMP [2.8.2, declare simd construct, Description] 6471 // The aligned clause declares that the object to which each list item points 6472 // is aligned to the number of bytes expressed in the optional parameter of 6473 // the aligned clause. 6474 // The special this pointer can be used as if was one of the arguments to the 6475 // function in any of the linear, aligned, or uniform clauses. 6476 // The type of list items appearing in the aligned clause must be array, 6477 // pointer, reference to array, or reference to pointer. 6478 llvm::DenseMap<const Decl *, const Expr *> AlignedArgs; 6479 const Expr *AlignedThis = nullptr; 6480 for (const Expr *E : Aligneds) { 6481 E = E->IgnoreParenImpCasts(); 6482 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 6483 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 6484 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 6485 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 6486 FD->getParamDecl(PVD->getFunctionScopeIndex()) 6487 ->getCanonicalDecl() == CanonPVD) { 6488 // OpenMP [2.8.1, simd construct, Restrictions] 6489 // A list-item cannot appear in more than one aligned clause. 6490 if (AlignedArgs.count(CanonPVD) > 0) { 6491 Diag(E->getExprLoc(), diag::err_omp_used_in_clause_twice) 6492 << 1 << getOpenMPClauseName(OMPC_aligned) 6493 << E->getSourceRange(); 6494 Diag(AlignedArgs[CanonPVD]->getExprLoc(), 6495 diag::note_omp_explicit_dsa) 6496 << getOpenMPClauseName(OMPC_aligned); 6497 continue; 6498 } 6499 AlignedArgs[CanonPVD] = E; 6500 QualType QTy = PVD->getType() 6501 .getNonReferenceType() 6502 .getUnqualifiedType() 6503 .getCanonicalType(); 6504 const Type *Ty = QTy.getTypePtrOrNull(); 6505 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) { 6506 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr) 6507 << QTy << getLangOpts().CPlusPlus << E->getSourceRange(); 6508 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD; 6509 } 6510 continue; 6511 } 6512 } 6513 if (isa<CXXThisExpr>(E)) { 6514 if (AlignedThis) { 6515 Diag(E->getExprLoc(), diag::err_omp_used_in_clause_twice) 6516 << 2 << getOpenMPClauseName(OMPC_aligned) << E->getSourceRange(); 6517 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa) 6518 << getOpenMPClauseName(OMPC_aligned); 6519 } 6520 AlignedThis = E; 6521 continue; 6522 } 6523 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 6524 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 6525 } 6526 // The optional parameter of the aligned clause, alignment, must be a constant 6527 // positive integer expression. If no optional parameter is specified, 6528 // implementation-defined default alignments for SIMD instructions on the 6529 // target platforms are assumed. 6530 SmallVector<const Expr *, 4> NewAligns; 6531 for (Expr *E : Alignments) { 6532 ExprResult Align; 6533 if (E) 6534 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned); 6535 NewAligns.push_back(Align.get()); 6536 } 6537 // OpenMP [2.8.2, declare simd construct, Description] 6538 // The linear clause declares one or more list items to be private to a SIMD 6539 // lane and to have a linear relationship with respect to the iteration space 6540 // of a loop. 6541 // The special this pointer can be used as if was one of the arguments to the 6542 // function in any of the linear, aligned, or uniform clauses. 6543 // When a linear-step expression is specified in a linear clause it must be 6544 // either a constant integer expression or an integer-typed parameter that is 6545 // specified in a uniform clause on the directive. 6546 llvm::DenseMap<const Decl *, const Expr *> LinearArgs; 6547 const bool IsUniformedThis = UniformedLinearThis != nullptr; 6548 auto MI = LinModifiers.begin(); 6549 for (const Expr *E : Linears) { 6550 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI); 6551 ++MI; 6552 E = E->IgnoreParenImpCasts(); 6553 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 6554 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 6555 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 6556 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 6557 FD->getParamDecl(PVD->getFunctionScopeIndex()) 6558 ->getCanonicalDecl() == CanonPVD) { 6559 // OpenMP [2.15.3.7, linear Clause, Restrictions] 6560 // A list-item cannot appear in more than one linear clause. 6561 if (LinearArgs.count(CanonPVD) > 0) { 6562 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 6563 << getOpenMPClauseName(OMPC_linear) 6564 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange(); 6565 Diag(LinearArgs[CanonPVD]->getExprLoc(), 6566 diag::note_omp_explicit_dsa) 6567 << getOpenMPClauseName(OMPC_linear); 6568 continue; 6569 } 6570 // Each argument can appear in at most one uniform or linear clause. 6571 if (UniformedArgs.count(CanonPVD) > 0) { 6572 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 6573 << getOpenMPClauseName(OMPC_linear) 6574 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange(); 6575 Diag(UniformedArgs[CanonPVD]->getExprLoc(), 6576 diag::note_omp_explicit_dsa) 6577 << getOpenMPClauseName(OMPC_uniform); 6578 continue; 6579 } 6580 LinearArgs[CanonPVD] = E; 6581 if (E->isValueDependent() || E->isTypeDependent() || 6582 E->isInstantiationDependent() || 6583 E->containsUnexpandedParameterPack()) 6584 continue; 6585 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind, 6586 PVD->getOriginalType(), 6587 /*IsDeclareSimd=*/true); 6588 continue; 6589 } 6590 } 6591 if (isa<CXXThisExpr>(E)) { 6592 if (UniformedLinearThis) { 6593 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 6594 << getOpenMPClauseName(OMPC_linear) 6595 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear) 6596 << E->getSourceRange(); 6597 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa) 6598 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform 6599 : OMPC_linear); 6600 continue; 6601 } 6602 UniformedLinearThis = E; 6603 if (E->isValueDependent() || E->isTypeDependent() || 6604 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 6605 continue; 6606 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind, 6607 E->getType(), /*IsDeclareSimd=*/true); 6608 continue; 6609 } 6610 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 6611 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 6612 } 6613 Expr *Step = nullptr; 6614 Expr *NewStep = nullptr; 6615 SmallVector<Expr *, 4> NewSteps; 6616 for (Expr *E : Steps) { 6617 // Skip the same step expression, it was checked already. 6618 if (Step == E || !E) { 6619 NewSteps.push_back(E ? NewStep : nullptr); 6620 continue; 6621 } 6622 Step = E; 6623 if (const auto *DRE = dyn_cast<DeclRefExpr>(Step)) 6624 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 6625 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 6626 if (UniformedArgs.count(CanonPVD) == 0) { 6627 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param) 6628 << Step->getSourceRange(); 6629 } else if (E->isValueDependent() || E->isTypeDependent() || 6630 E->isInstantiationDependent() || 6631 E->containsUnexpandedParameterPack() || 6632 CanonPVD->getType()->hasIntegerRepresentation()) { 6633 NewSteps.push_back(Step); 6634 } else { 6635 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param) 6636 << Step->getSourceRange(); 6637 } 6638 continue; 6639 } 6640 NewStep = Step; 6641 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && 6642 !Step->isInstantiationDependent() && 6643 !Step->containsUnexpandedParameterPack()) { 6644 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step) 6645 .get(); 6646 if (NewStep) 6647 NewStep = 6648 VerifyIntegerConstantExpression(NewStep, /*FIXME*/ AllowFold).get(); 6649 } 6650 NewSteps.push_back(NewStep); 6651 } 6652 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit( 6653 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()), 6654 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(), 6655 const_cast<Expr **>(NewAligns.data()), NewAligns.size(), 6656 const_cast<Expr **>(Linears.data()), Linears.size(), 6657 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(), 6658 NewSteps.data(), NewSteps.size(), SR); 6659 ADecl->addAttr(NewAttr); 6660 return DG; 6661 } 6662 6663 static void setPrototype(Sema &S, FunctionDecl *FD, FunctionDecl *FDWithProto, 6664 QualType NewType) { 6665 assert(NewType->isFunctionProtoType() && 6666 "Expected function type with prototype."); 6667 assert(FD->getType()->isFunctionNoProtoType() && 6668 "Expected function with type with no prototype."); 6669 assert(FDWithProto->getType()->isFunctionProtoType() && 6670 "Expected function with prototype."); 6671 // Synthesize parameters with the same types. 6672 FD->setType(NewType); 6673 SmallVector<ParmVarDecl *, 16> Params; 6674 for (const ParmVarDecl *P : FDWithProto->parameters()) { 6675 auto *Param = ParmVarDecl::Create(S.getASTContext(), FD, SourceLocation(), 6676 SourceLocation(), nullptr, P->getType(), 6677 /*TInfo=*/nullptr, SC_None, nullptr); 6678 Param->setScopeInfo(0, Params.size()); 6679 Param->setImplicit(); 6680 Params.push_back(Param); 6681 } 6682 6683 FD->setParams(Params); 6684 } 6685 6686 void Sema::ActOnFinishedFunctionDefinitionInOpenMPAssumeScope(Decl *D) { 6687 if (D->isInvalidDecl()) 6688 return; 6689 FunctionDecl *FD = nullptr; 6690 if (auto *UTemplDecl = dyn_cast<FunctionTemplateDecl>(D)) 6691 FD = UTemplDecl->getTemplatedDecl(); 6692 else 6693 FD = cast<FunctionDecl>(D); 6694 assert(FD && "Expected a function declaration!"); 6695 6696 // If we are instantiating templates we do *not* apply scoped assumptions but 6697 // only global ones. We apply scoped assumption to the template definition 6698 // though. 6699 if (!inTemplateInstantiation()) { 6700 for (AssumptionAttr *AA : OMPAssumeScoped) 6701 FD->addAttr(AA); 6702 } 6703 for (AssumptionAttr *AA : OMPAssumeGlobal) 6704 FD->addAttr(AA); 6705 } 6706 6707 Sema::OMPDeclareVariantScope::OMPDeclareVariantScope(OMPTraitInfo &TI) 6708 : TI(&TI), NameSuffix(TI.getMangledName()) {} 6709 6710 void Sema::ActOnStartOfFunctionDefinitionInOpenMPDeclareVariantScope( 6711 Scope *S, Declarator &D, MultiTemplateParamsArg TemplateParamLists, 6712 SmallVectorImpl<FunctionDecl *> &Bases) { 6713 if (!D.getIdentifier()) 6714 return; 6715 6716 OMPDeclareVariantScope &DVScope = OMPDeclareVariantScopes.back(); 6717 6718 // Template specialization is an extension, check if we do it. 6719 bool IsTemplated = !TemplateParamLists.empty(); 6720 if (IsTemplated & 6721 !DVScope.TI->isExtensionActive( 6722 llvm::omp::TraitProperty::implementation_extension_allow_templates)) 6723 return; 6724 6725 IdentifierInfo *BaseII = D.getIdentifier(); 6726 LookupResult Lookup(*this, DeclarationName(BaseII), D.getIdentifierLoc(), 6727 LookupOrdinaryName); 6728 LookupParsedName(Lookup, S, &D.getCXXScopeSpec()); 6729 6730 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); 6731 QualType FType = TInfo->getType(); 6732 6733 bool IsConstexpr = 6734 D.getDeclSpec().getConstexprSpecifier() == ConstexprSpecKind::Constexpr; 6735 bool IsConsteval = 6736 D.getDeclSpec().getConstexprSpecifier() == ConstexprSpecKind::Consteval; 6737 6738 for (auto *Candidate : Lookup) { 6739 auto *CandidateDecl = Candidate->getUnderlyingDecl(); 6740 FunctionDecl *UDecl = nullptr; 6741 if (IsTemplated && isa<FunctionTemplateDecl>(CandidateDecl)) 6742 UDecl = cast<FunctionTemplateDecl>(CandidateDecl)->getTemplatedDecl(); 6743 else if (!IsTemplated) 6744 UDecl = dyn_cast<FunctionDecl>(CandidateDecl); 6745 if (!UDecl) 6746 continue; 6747 6748 // Don't specialize constexpr/consteval functions with 6749 // non-constexpr/consteval functions. 6750 if (UDecl->isConstexpr() && !IsConstexpr) 6751 continue; 6752 if (UDecl->isConsteval() && !IsConsteval) 6753 continue; 6754 6755 QualType UDeclTy = UDecl->getType(); 6756 if (!UDeclTy->isDependentType()) { 6757 QualType NewType = Context.mergeFunctionTypes( 6758 FType, UDeclTy, /* OfBlockPointer */ false, 6759 /* Unqualified */ false, /* AllowCXX */ true); 6760 if (NewType.isNull()) 6761 continue; 6762 } 6763 6764 // Found a base! 6765 Bases.push_back(UDecl); 6766 } 6767 6768 bool UseImplicitBase = !DVScope.TI->isExtensionActive( 6769 llvm::omp::TraitProperty::implementation_extension_disable_implicit_base); 6770 // If no base was found we create a declaration that we use as base. 6771 if (Bases.empty() && UseImplicitBase) { 6772 D.setFunctionDefinitionKind(FunctionDefinitionKind::Declaration); 6773 Decl *BaseD = HandleDeclarator(S, D, TemplateParamLists); 6774 BaseD->setImplicit(true); 6775 if (auto *BaseTemplD = dyn_cast<FunctionTemplateDecl>(BaseD)) 6776 Bases.push_back(BaseTemplD->getTemplatedDecl()); 6777 else 6778 Bases.push_back(cast<FunctionDecl>(BaseD)); 6779 } 6780 6781 std::string MangledName; 6782 MangledName += D.getIdentifier()->getName(); 6783 MangledName += getOpenMPVariantManglingSeparatorStr(); 6784 MangledName += DVScope.NameSuffix; 6785 IdentifierInfo &VariantII = Context.Idents.get(MangledName); 6786 6787 VariantII.setMangledOpenMPVariantName(true); 6788 D.SetIdentifier(&VariantII, D.getBeginLoc()); 6789 } 6790 6791 void Sema::ActOnFinishedFunctionDefinitionInOpenMPDeclareVariantScope( 6792 Decl *D, SmallVectorImpl<FunctionDecl *> &Bases) { 6793 // Do not mark function as is used to prevent its emission if this is the 6794 // only place where it is used. 6795 EnterExpressionEvaluationContext Unevaluated( 6796 *this, Sema::ExpressionEvaluationContext::Unevaluated); 6797 6798 FunctionDecl *FD = nullptr; 6799 if (auto *UTemplDecl = dyn_cast<FunctionTemplateDecl>(D)) 6800 FD = UTemplDecl->getTemplatedDecl(); 6801 else 6802 FD = cast<FunctionDecl>(D); 6803 auto *VariantFuncRef = DeclRefExpr::Create( 6804 Context, NestedNameSpecifierLoc(), SourceLocation(), FD, 6805 /* RefersToEnclosingVariableOrCapture */ false, 6806 /* NameLoc */ FD->getLocation(), FD->getType(), 6807 ExprValueKind::VK_PRValue); 6808 6809 OMPDeclareVariantScope &DVScope = OMPDeclareVariantScopes.back(); 6810 auto *OMPDeclareVariantA = OMPDeclareVariantAttr::CreateImplicit( 6811 Context, VariantFuncRef, DVScope.TI, 6812 /*NothingArgs=*/nullptr, /*NothingArgsSize=*/0, 6813 /*NeedDevicePtrArgs=*/nullptr, /*NeedDevicePtrArgsSize=*/0); 6814 for (FunctionDecl *BaseFD : Bases) 6815 BaseFD->addAttr(OMPDeclareVariantA); 6816 } 6817 6818 ExprResult Sema::ActOnOpenMPCall(ExprResult Call, Scope *Scope, 6819 SourceLocation LParenLoc, 6820 MultiExprArg ArgExprs, 6821 SourceLocation RParenLoc, Expr *ExecConfig) { 6822 // The common case is a regular call we do not want to specialize at all. Try 6823 // to make that case fast by bailing early. 6824 CallExpr *CE = dyn_cast<CallExpr>(Call.get()); 6825 if (!CE) 6826 return Call; 6827 6828 FunctionDecl *CalleeFnDecl = CE->getDirectCallee(); 6829 if (!CalleeFnDecl) 6830 return Call; 6831 6832 if (!CalleeFnDecl->hasAttr<OMPDeclareVariantAttr>()) 6833 return Call; 6834 6835 ASTContext &Context = getASTContext(); 6836 std::function<void(StringRef)> DiagUnknownTrait = [this, 6837 CE](StringRef ISATrait) { 6838 // TODO Track the selector locations in a way that is accessible here to 6839 // improve the diagnostic location. 6840 Diag(CE->getBeginLoc(), diag::warn_unknown_declare_variant_isa_trait) 6841 << ISATrait; 6842 }; 6843 TargetOMPContext OMPCtx(Context, std::move(DiagUnknownTrait), 6844 getCurFunctionDecl(), DSAStack->getConstructTraits()); 6845 6846 QualType CalleeFnType = CalleeFnDecl->getType(); 6847 6848 SmallVector<Expr *, 4> Exprs; 6849 SmallVector<VariantMatchInfo, 4> VMIs; 6850 while (CalleeFnDecl) { 6851 for (OMPDeclareVariantAttr *A : 6852 CalleeFnDecl->specific_attrs<OMPDeclareVariantAttr>()) { 6853 Expr *VariantRef = A->getVariantFuncRef(); 6854 6855 VariantMatchInfo VMI; 6856 OMPTraitInfo &TI = A->getTraitInfo(); 6857 TI.getAsVariantMatchInfo(Context, VMI); 6858 if (!isVariantApplicableInContext(VMI, OMPCtx, 6859 /* DeviceSetOnly */ false)) 6860 continue; 6861 6862 VMIs.push_back(VMI); 6863 Exprs.push_back(VariantRef); 6864 } 6865 6866 CalleeFnDecl = CalleeFnDecl->getPreviousDecl(); 6867 } 6868 6869 ExprResult NewCall; 6870 do { 6871 int BestIdx = getBestVariantMatchForContext(VMIs, OMPCtx); 6872 if (BestIdx < 0) 6873 return Call; 6874 Expr *BestExpr = cast<DeclRefExpr>(Exprs[BestIdx]); 6875 Decl *BestDecl = cast<DeclRefExpr>(BestExpr)->getDecl(); 6876 6877 { 6878 // Try to build a (member) call expression for the current best applicable 6879 // variant expression. We allow this to fail in which case we continue 6880 // with the next best variant expression. The fail case is part of the 6881 // implementation defined behavior in the OpenMP standard when it talks 6882 // about what differences in the function prototypes: "Any differences 6883 // that the specific OpenMP context requires in the prototype of the 6884 // variant from the base function prototype are implementation defined." 6885 // This wording is there to allow the specialized variant to have a 6886 // different type than the base function. This is intended and OK but if 6887 // we cannot create a call the difference is not in the "implementation 6888 // defined range" we allow. 6889 Sema::TentativeAnalysisScope Trap(*this); 6890 6891 if (auto *SpecializedMethod = dyn_cast<CXXMethodDecl>(BestDecl)) { 6892 auto *MemberCall = dyn_cast<CXXMemberCallExpr>(CE); 6893 BestExpr = MemberExpr::CreateImplicit( 6894 Context, MemberCall->getImplicitObjectArgument(), 6895 /* IsArrow */ false, SpecializedMethod, Context.BoundMemberTy, 6896 MemberCall->getValueKind(), MemberCall->getObjectKind()); 6897 } 6898 NewCall = BuildCallExpr(Scope, BestExpr, LParenLoc, ArgExprs, RParenLoc, 6899 ExecConfig); 6900 if (NewCall.isUsable()) { 6901 if (CallExpr *NCE = dyn_cast<CallExpr>(NewCall.get())) { 6902 FunctionDecl *NewCalleeFnDecl = NCE->getDirectCallee(); 6903 QualType NewType = Context.mergeFunctionTypes( 6904 CalleeFnType, NewCalleeFnDecl->getType(), 6905 /* OfBlockPointer */ false, 6906 /* Unqualified */ false, /* AllowCXX */ true); 6907 if (!NewType.isNull()) 6908 break; 6909 // Don't use the call if the function type was not compatible. 6910 NewCall = nullptr; 6911 } 6912 } 6913 } 6914 6915 VMIs.erase(VMIs.begin() + BestIdx); 6916 Exprs.erase(Exprs.begin() + BestIdx); 6917 } while (!VMIs.empty()); 6918 6919 if (!NewCall.isUsable()) 6920 return Call; 6921 return PseudoObjectExpr::Create(Context, CE, {NewCall.get()}, 0); 6922 } 6923 6924 Optional<std::pair<FunctionDecl *, Expr *>> 6925 Sema::checkOpenMPDeclareVariantFunction(Sema::DeclGroupPtrTy DG, 6926 Expr *VariantRef, OMPTraitInfo &TI, 6927 SourceRange SR) { 6928 if (!DG || DG.get().isNull()) 6929 return None; 6930 6931 const int VariantId = 1; 6932 // Must be applied only to single decl. 6933 if (!DG.get().isSingleDecl()) { 6934 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd_variant) 6935 << VariantId << SR; 6936 return None; 6937 } 6938 Decl *ADecl = DG.get().getSingleDecl(); 6939 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl)) 6940 ADecl = FTD->getTemplatedDecl(); 6941 6942 // Decl must be a function. 6943 auto *FD = dyn_cast<FunctionDecl>(ADecl); 6944 if (!FD) { 6945 Diag(ADecl->getLocation(), diag::err_omp_function_expected) 6946 << VariantId << SR; 6947 return None; 6948 } 6949 6950 auto &&HasMultiVersionAttributes = [](const FunctionDecl *FD) { 6951 return FD->hasAttrs() && 6952 (FD->hasAttr<CPUDispatchAttr>() || FD->hasAttr<CPUSpecificAttr>() || 6953 FD->hasAttr<TargetAttr>()); 6954 }; 6955 // OpenMP is not compatible with CPU-specific attributes. 6956 if (HasMultiVersionAttributes(FD)) { 6957 Diag(FD->getLocation(), diag::err_omp_declare_variant_incompat_attributes) 6958 << SR; 6959 return None; 6960 } 6961 6962 // Allow #pragma omp declare variant only if the function is not used. 6963 if (FD->isUsed(false)) 6964 Diag(SR.getBegin(), diag::warn_omp_declare_variant_after_used) 6965 << FD->getLocation(); 6966 6967 // Check if the function was emitted already. 6968 const FunctionDecl *Definition; 6969 if (!FD->isThisDeclarationADefinition() && FD->isDefined(Definition) && 6970 (LangOpts.EmitAllDecls || Context.DeclMustBeEmitted(Definition))) 6971 Diag(SR.getBegin(), diag::warn_omp_declare_variant_after_emitted) 6972 << FD->getLocation(); 6973 6974 // The VariantRef must point to function. 6975 if (!VariantRef) { 6976 Diag(SR.getBegin(), diag::err_omp_function_expected) << VariantId; 6977 return None; 6978 } 6979 6980 auto ShouldDelayChecks = [](Expr *&E, bool) { 6981 return E && (E->isTypeDependent() || E->isValueDependent() || 6982 E->containsUnexpandedParameterPack() || 6983 E->isInstantiationDependent()); 6984 }; 6985 // Do not check templates, wait until instantiation. 6986 if (FD->isDependentContext() || ShouldDelayChecks(VariantRef, false) || 6987 TI.anyScoreOrCondition(ShouldDelayChecks)) 6988 return std::make_pair(FD, VariantRef); 6989 6990 // Deal with non-constant score and user condition expressions. 6991 auto HandleNonConstantScoresAndConditions = [this](Expr *&E, 6992 bool IsScore) -> bool { 6993 if (!E || E->isIntegerConstantExpr(Context)) 6994 return false; 6995 6996 if (IsScore) { 6997 // We warn on non-constant scores and pretend they were not present. 6998 Diag(E->getExprLoc(), diag::warn_omp_declare_variant_score_not_constant) 6999 << E; 7000 E = nullptr; 7001 } else { 7002 // We could replace a non-constant user condition with "false" but we 7003 // will soon need to handle these anyway for the dynamic version of 7004 // OpenMP context selectors. 7005 Diag(E->getExprLoc(), 7006 diag::err_omp_declare_variant_user_condition_not_constant) 7007 << E; 7008 } 7009 return true; 7010 }; 7011 if (TI.anyScoreOrCondition(HandleNonConstantScoresAndConditions)) 7012 return None; 7013 7014 // Convert VariantRef expression to the type of the original function to 7015 // resolve possible conflicts. 7016 ExprResult VariantRefCast = VariantRef; 7017 if (LangOpts.CPlusPlus) { 7018 QualType FnPtrType; 7019 auto *Method = dyn_cast<CXXMethodDecl>(FD); 7020 if (Method && !Method->isStatic()) { 7021 const Type *ClassType = 7022 Context.getTypeDeclType(Method->getParent()).getTypePtr(); 7023 FnPtrType = Context.getMemberPointerType(FD->getType(), ClassType); 7024 ExprResult ER; 7025 { 7026 // Build adrr_of unary op to correctly handle type checks for member 7027 // functions. 7028 Sema::TentativeAnalysisScope Trap(*this); 7029 ER = CreateBuiltinUnaryOp(VariantRef->getBeginLoc(), UO_AddrOf, 7030 VariantRef); 7031 } 7032 if (!ER.isUsable()) { 7033 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected) 7034 << VariantId << VariantRef->getSourceRange(); 7035 return None; 7036 } 7037 VariantRef = ER.get(); 7038 } else { 7039 FnPtrType = Context.getPointerType(FD->getType()); 7040 } 7041 QualType VarianPtrType = Context.getPointerType(VariantRef->getType()); 7042 if (VarianPtrType.getUnqualifiedType() != FnPtrType.getUnqualifiedType()) { 7043 ImplicitConversionSequence ICS = TryImplicitConversion( 7044 VariantRef, FnPtrType.getUnqualifiedType(), 7045 /*SuppressUserConversions=*/false, AllowedExplicit::None, 7046 /*InOverloadResolution=*/false, 7047 /*CStyle=*/false, 7048 /*AllowObjCWritebackConversion=*/false); 7049 if (ICS.isFailure()) { 7050 Diag(VariantRef->getExprLoc(), 7051 diag::err_omp_declare_variant_incompat_types) 7052 << VariantRef->getType() 7053 << ((Method && !Method->isStatic()) ? FnPtrType : FD->getType()) 7054 << VariantRef->getSourceRange(); 7055 return None; 7056 } 7057 VariantRefCast = PerformImplicitConversion( 7058 VariantRef, FnPtrType.getUnqualifiedType(), AA_Converting); 7059 if (!VariantRefCast.isUsable()) 7060 return None; 7061 } 7062 // Drop previously built artificial addr_of unary op for member functions. 7063 if (Method && !Method->isStatic()) { 7064 Expr *PossibleAddrOfVariantRef = VariantRefCast.get(); 7065 if (auto *UO = dyn_cast<UnaryOperator>( 7066 PossibleAddrOfVariantRef->IgnoreImplicit())) 7067 VariantRefCast = UO->getSubExpr(); 7068 } 7069 } 7070 7071 ExprResult ER = CheckPlaceholderExpr(VariantRefCast.get()); 7072 if (!ER.isUsable() || 7073 !ER.get()->IgnoreParenImpCasts()->getType()->isFunctionType()) { 7074 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected) 7075 << VariantId << VariantRef->getSourceRange(); 7076 return None; 7077 } 7078 7079 // The VariantRef must point to function. 7080 auto *DRE = dyn_cast<DeclRefExpr>(ER.get()->IgnoreParenImpCasts()); 7081 if (!DRE) { 7082 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected) 7083 << VariantId << VariantRef->getSourceRange(); 7084 return None; 7085 } 7086 auto *NewFD = dyn_cast_or_null<FunctionDecl>(DRE->getDecl()); 7087 if (!NewFD) { 7088 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected) 7089 << VariantId << VariantRef->getSourceRange(); 7090 return None; 7091 } 7092 7093 // Check if function types are compatible in C. 7094 if (!LangOpts.CPlusPlus) { 7095 QualType NewType = 7096 Context.mergeFunctionTypes(FD->getType(), NewFD->getType()); 7097 if (NewType.isNull()) { 7098 Diag(VariantRef->getExprLoc(), 7099 diag::err_omp_declare_variant_incompat_types) 7100 << NewFD->getType() << FD->getType() << VariantRef->getSourceRange(); 7101 return None; 7102 } 7103 if (NewType->isFunctionProtoType()) { 7104 if (FD->getType()->isFunctionNoProtoType()) 7105 setPrototype(*this, FD, NewFD, NewType); 7106 else if (NewFD->getType()->isFunctionNoProtoType()) 7107 setPrototype(*this, NewFD, FD, NewType); 7108 } 7109 } 7110 7111 // Check if variant function is not marked with declare variant directive. 7112 if (NewFD->hasAttrs() && NewFD->hasAttr<OMPDeclareVariantAttr>()) { 7113 Diag(VariantRef->getExprLoc(), 7114 diag::warn_omp_declare_variant_marked_as_declare_variant) 7115 << VariantRef->getSourceRange(); 7116 SourceRange SR = 7117 NewFD->specific_attr_begin<OMPDeclareVariantAttr>()->getRange(); 7118 Diag(SR.getBegin(), diag::note_omp_marked_declare_variant_here) << SR; 7119 return None; 7120 } 7121 7122 enum DoesntSupport { 7123 VirtFuncs = 1, 7124 Constructors = 3, 7125 Destructors = 4, 7126 DeletedFuncs = 5, 7127 DefaultedFuncs = 6, 7128 ConstexprFuncs = 7, 7129 ConstevalFuncs = 8, 7130 }; 7131 if (const auto *CXXFD = dyn_cast<CXXMethodDecl>(FD)) { 7132 if (CXXFD->isVirtual()) { 7133 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 7134 << VirtFuncs; 7135 return None; 7136 } 7137 7138 if (isa<CXXConstructorDecl>(FD)) { 7139 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 7140 << Constructors; 7141 return None; 7142 } 7143 7144 if (isa<CXXDestructorDecl>(FD)) { 7145 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 7146 << Destructors; 7147 return None; 7148 } 7149 } 7150 7151 if (FD->isDeleted()) { 7152 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 7153 << DeletedFuncs; 7154 return None; 7155 } 7156 7157 if (FD->isDefaulted()) { 7158 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 7159 << DefaultedFuncs; 7160 return None; 7161 } 7162 7163 if (FD->isConstexpr()) { 7164 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 7165 << (NewFD->isConsteval() ? ConstevalFuncs : ConstexprFuncs); 7166 return None; 7167 } 7168 7169 // Check general compatibility. 7170 if (areMultiversionVariantFunctionsCompatible( 7171 FD, NewFD, PartialDiagnostic::NullDiagnostic(), 7172 PartialDiagnosticAt(SourceLocation(), 7173 PartialDiagnostic::NullDiagnostic()), 7174 PartialDiagnosticAt( 7175 VariantRef->getExprLoc(), 7176 PDiag(diag::err_omp_declare_variant_doesnt_support)), 7177 PartialDiagnosticAt(VariantRef->getExprLoc(), 7178 PDiag(diag::err_omp_declare_variant_diff) 7179 << FD->getLocation()), 7180 /*TemplatesSupported=*/true, /*ConstexprSupported=*/false, 7181 /*CLinkageMayDiffer=*/true)) 7182 return None; 7183 return std::make_pair(FD, cast<Expr>(DRE)); 7184 } 7185 7186 void Sema::ActOnOpenMPDeclareVariantDirective( 7187 FunctionDecl *FD, Expr *VariantRef, OMPTraitInfo &TI, 7188 ArrayRef<Expr *> AdjustArgsNothing, 7189 ArrayRef<Expr *> AdjustArgsNeedDevicePtr, SourceRange SR) { 7190 7191 // OpenMP 5.1 [2.3.5, declare variant directive, Restrictions] 7192 // An adjust_args clause or append_args clause can only be specified if the 7193 // dispatch selector of the construct selector set appears in the match 7194 // clause. 7195 7196 SmallVector<Expr *, 8> AllAdjustArgs; 7197 llvm::append_range(AllAdjustArgs, AdjustArgsNothing); 7198 llvm::append_range(AllAdjustArgs, AdjustArgsNeedDevicePtr); 7199 7200 if (!AllAdjustArgs.empty()) { 7201 VariantMatchInfo VMI; 7202 TI.getAsVariantMatchInfo(Context, VMI); 7203 if (!llvm::is_contained( 7204 VMI.ConstructTraits, 7205 llvm::omp::TraitProperty::construct_dispatch_dispatch)) { 7206 Diag(AllAdjustArgs[0]->getExprLoc(), 7207 diag::err_omp_clause_requires_dispatch_construct) 7208 << getOpenMPClauseName(OMPC_adjust_args); 7209 return; 7210 } 7211 } 7212 7213 // OpenMP 5.1 [2.3.5, declare variant directive, Restrictions] 7214 // Each argument can only appear in a single adjust_args clause for each 7215 // declare variant directive. 7216 llvm::SmallPtrSet<const VarDecl *, 4> AdjustVars; 7217 7218 for (Expr *E : AllAdjustArgs) { 7219 E = E->IgnoreParenImpCasts(); 7220 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) { 7221 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 7222 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 7223 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 7224 FD->getParamDecl(PVD->getFunctionScopeIndex()) 7225 ->getCanonicalDecl() == CanonPVD) { 7226 // It's a parameter of the function, check duplicates. 7227 if (!AdjustVars.insert(CanonPVD).second) { 7228 Diag(DRE->getLocation(), diag::err_omp_adjust_arg_multiple_clauses) 7229 << PVD; 7230 return; 7231 } 7232 continue; 7233 } 7234 } 7235 } 7236 // Anything that is not a function parameter is an error. 7237 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) << FD << 0; 7238 return; 7239 } 7240 7241 auto *NewAttr = OMPDeclareVariantAttr::CreateImplicit( 7242 Context, VariantRef, &TI, const_cast<Expr **>(AdjustArgsNothing.data()), 7243 AdjustArgsNothing.size(), 7244 const_cast<Expr **>(AdjustArgsNeedDevicePtr.data()), 7245 AdjustArgsNeedDevicePtr.size(), SR); 7246 FD->addAttr(NewAttr); 7247 } 7248 7249 StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses, 7250 Stmt *AStmt, 7251 SourceLocation StartLoc, 7252 SourceLocation EndLoc) { 7253 if (!AStmt) 7254 return StmtError(); 7255 7256 auto *CS = cast<CapturedStmt>(AStmt); 7257 // 1.2.2 OpenMP Language Terminology 7258 // Structured block - An executable statement with a single entry at the 7259 // top and a single exit at the bottom. 7260 // The point of exit cannot be a branch out of the structured block. 7261 // longjmp() and throw() must not violate the entry/exit criteria. 7262 CS->getCapturedDecl()->setNothrow(); 7263 7264 setFunctionHasBranchProtectedScope(); 7265 7266 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 7267 DSAStack->getTaskgroupReductionRef(), 7268 DSAStack->isCancelRegion()); 7269 } 7270 7271 namespace { 7272 /// Iteration space of a single for loop. 7273 struct LoopIterationSpace final { 7274 /// True if the condition operator is the strict compare operator (<, > or 7275 /// !=). 7276 bool IsStrictCompare = false; 7277 /// Condition of the loop. 7278 Expr *PreCond = nullptr; 7279 /// This expression calculates the number of iterations in the loop. 7280 /// It is always possible to calculate it before starting the loop. 7281 Expr *NumIterations = nullptr; 7282 /// The loop counter variable. 7283 Expr *CounterVar = nullptr; 7284 /// Private loop counter variable. 7285 Expr *PrivateCounterVar = nullptr; 7286 /// This is initializer for the initial value of #CounterVar. 7287 Expr *CounterInit = nullptr; 7288 /// This is step for the #CounterVar used to generate its update: 7289 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration. 7290 Expr *CounterStep = nullptr; 7291 /// Should step be subtracted? 7292 bool Subtract = false; 7293 /// Source range of the loop init. 7294 SourceRange InitSrcRange; 7295 /// Source range of the loop condition. 7296 SourceRange CondSrcRange; 7297 /// Source range of the loop increment. 7298 SourceRange IncSrcRange; 7299 /// Minimum value that can have the loop control variable. Used to support 7300 /// non-rectangular loops. Applied only for LCV with the non-iterator types, 7301 /// since only such variables can be used in non-loop invariant expressions. 7302 Expr *MinValue = nullptr; 7303 /// Maximum value that can have the loop control variable. Used to support 7304 /// non-rectangular loops. Applied only for LCV with the non-iterator type, 7305 /// since only such variables can be used in non-loop invariant expressions. 7306 Expr *MaxValue = nullptr; 7307 /// true, if the lower bound depends on the outer loop control var. 7308 bool IsNonRectangularLB = false; 7309 /// true, if the upper bound depends on the outer loop control var. 7310 bool IsNonRectangularUB = false; 7311 /// Index of the loop this loop depends on and forms non-rectangular loop 7312 /// nest. 7313 unsigned LoopDependentIdx = 0; 7314 /// Final condition for the non-rectangular loop nest support. It is used to 7315 /// check that the number of iterations for this particular counter must be 7316 /// finished. 7317 Expr *FinalCondition = nullptr; 7318 }; 7319 7320 /// Helper class for checking canonical form of the OpenMP loops and 7321 /// extracting iteration space of each loop in the loop nest, that will be used 7322 /// for IR generation. 7323 class OpenMPIterationSpaceChecker { 7324 /// Reference to Sema. 7325 Sema &SemaRef; 7326 /// Does the loop associated directive support non-rectangular loops? 7327 bool SupportsNonRectangular; 7328 /// Data-sharing stack. 7329 DSAStackTy &Stack; 7330 /// A location for diagnostics (when there is no some better location). 7331 SourceLocation DefaultLoc; 7332 /// A location for diagnostics (when increment is not compatible). 7333 SourceLocation ConditionLoc; 7334 /// A source location for referring to loop init later. 7335 SourceRange InitSrcRange; 7336 /// A source location for referring to condition later. 7337 SourceRange ConditionSrcRange; 7338 /// A source location for referring to increment later. 7339 SourceRange IncrementSrcRange; 7340 /// Loop variable. 7341 ValueDecl *LCDecl = nullptr; 7342 /// Reference to loop variable. 7343 Expr *LCRef = nullptr; 7344 /// Lower bound (initializer for the var). 7345 Expr *LB = nullptr; 7346 /// Upper bound. 7347 Expr *UB = nullptr; 7348 /// Loop step (increment). 7349 Expr *Step = nullptr; 7350 /// This flag is true when condition is one of: 7351 /// Var < UB 7352 /// Var <= UB 7353 /// UB > Var 7354 /// UB >= Var 7355 /// This will have no value when the condition is != 7356 llvm::Optional<bool> TestIsLessOp; 7357 /// This flag is true when condition is strict ( < or > ). 7358 bool TestIsStrictOp = false; 7359 /// This flag is true when step is subtracted on each iteration. 7360 bool SubtractStep = false; 7361 /// The outer loop counter this loop depends on (if any). 7362 const ValueDecl *DepDecl = nullptr; 7363 /// Contains number of loop (starts from 1) on which loop counter init 7364 /// expression of this loop depends on. 7365 Optional<unsigned> InitDependOnLC; 7366 /// Contains number of loop (starts from 1) on which loop counter condition 7367 /// expression of this loop depends on. 7368 Optional<unsigned> CondDependOnLC; 7369 /// Checks if the provide statement depends on the loop counter. 7370 Optional<unsigned> doesDependOnLoopCounter(const Stmt *S, bool IsInitializer); 7371 /// Original condition required for checking of the exit condition for 7372 /// non-rectangular loop. 7373 Expr *Condition = nullptr; 7374 7375 public: 7376 OpenMPIterationSpaceChecker(Sema &SemaRef, bool SupportsNonRectangular, 7377 DSAStackTy &Stack, SourceLocation DefaultLoc) 7378 : SemaRef(SemaRef), SupportsNonRectangular(SupportsNonRectangular), 7379 Stack(Stack), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {} 7380 /// Check init-expr for canonical loop form and save loop counter 7381 /// variable - #Var and its initialization value - #LB. 7382 bool checkAndSetInit(Stmt *S, bool EmitDiags = true); 7383 /// Check test-expr for canonical form, save upper-bound (#UB), flags 7384 /// for less/greater and for strict/non-strict comparison. 7385 bool checkAndSetCond(Expr *S); 7386 /// Check incr-expr for canonical loop form and return true if it 7387 /// does not conform, otherwise save loop step (#Step). 7388 bool checkAndSetInc(Expr *S); 7389 /// Return the loop counter variable. 7390 ValueDecl *getLoopDecl() const { return LCDecl; } 7391 /// Return the reference expression to loop counter variable. 7392 Expr *getLoopDeclRefExpr() const { return LCRef; } 7393 /// Source range of the loop init. 7394 SourceRange getInitSrcRange() const { return InitSrcRange; } 7395 /// Source range of the loop condition. 7396 SourceRange getConditionSrcRange() const { return ConditionSrcRange; } 7397 /// Source range of the loop increment. 7398 SourceRange getIncrementSrcRange() const { return IncrementSrcRange; } 7399 /// True if the step should be subtracted. 7400 bool shouldSubtractStep() const { return SubtractStep; } 7401 /// True, if the compare operator is strict (<, > or !=). 7402 bool isStrictTestOp() const { return TestIsStrictOp; } 7403 /// Build the expression to calculate the number of iterations. 7404 Expr *buildNumIterations( 7405 Scope *S, ArrayRef<LoopIterationSpace> ResultIterSpaces, bool LimitedType, 7406 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 7407 /// Build the precondition expression for the loops. 7408 Expr * 7409 buildPreCond(Scope *S, Expr *Cond, 7410 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 7411 /// Build reference expression to the counter be used for codegen. 7412 DeclRefExpr * 7413 buildCounterVar(llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 7414 DSAStackTy &DSA) const; 7415 /// Build reference expression to the private counter be used for 7416 /// codegen. 7417 Expr *buildPrivateCounterVar() const; 7418 /// Build initialization of the counter be used for codegen. 7419 Expr *buildCounterInit() const; 7420 /// Build step of the counter be used for codegen. 7421 Expr *buildCounterStep() const; 7422 /// Build loop data with counter value for depend clauses in ordered 7423 /// directives. 7424 Expr * 7425 buildOrderedLoopData(Scope *S, Expr *Counter, 7426 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 7427 SourceLocation Loc, Expr *Inc = nullptr, 7428 OverloadedOperatorKind OOK = OO_Amp); 7429 /// Builds the minimum value for the loop counter. 7430 std::pair<Expr *, Expr *> buildMinMaxValues( 7431 Scope *S, llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 7432 /// Builds final condition for the non-rectangular loops. 7433 Expr *buildFinalCondition(Scope *S) const; 7434 /// Return true if any expression is dependent. 7435 bool dependent() const; 7436 /// Returns true if the initializer forms non-rectangular loop. 7437 bool doesInitDependOnLC() const { return InitDependOnLC.hasValue(); } 7438 /// Returns true if the condition forms non-rectangular loop. 7439 bool doesCondDependOnLC() const { return CondDependOnLC.hasValue(); } 7440 /// Returns index of the loop we depend on (starting from 1), or 0 otherwise. 7441 unsigned getLoopDependentIdx() const { 7442 return InitDependOnLC.getValueOr(CondDependOnLC.getValueOr(0)); 7443 } 7444 7445 private: 7446 /// Check the right-hand side of an assignment in the increment 7447 /// expression. 7448 bool checkAndSetIncRHS(Expr *RHS); 7449 /// Helper to set loop counter variable and its initializer. 7450 bool setLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB, 7451 bool EmitDiags); 7452 /// Helper to set upper bound. 7453 bool setUB(Expr *NewUB, llvm::Optional<bool> LessOp, bool StrictOp, 7454 SourceRange SR, SourceLocation SL); 7455 /// Helper to set loop increment. 7456 bool setStep(Expr *NewStep, bool Subtract); 7457 }; 7458 7459 bool OpenMPIterationSpaceChecker::dependent() const { 7460 if (!LCDecl) { 7461 assert(!LB && !UB && !Step); 7462 return false; 7463 } 7464 return LCDecl->getType()->isDependentType() || 7465 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) || 7466 (Step && Step->isValueDependent()); 7467 } 7468 7469 bool OpenMPIterationSpaceChecker::setLCDeclAndLB(ValueDecl *NewLCDecl, 7470 Expr *NewLCRefExpr, 7471 Expr *NewLB, bool EmitDiags) { 7472 // State consistency checking to ensure correct usage. 7473 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr && 7474 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp); 7475 if (!NewLCDecl || !NewLB || NewLB->containsErrors()) 7476 return true; 7477 LCDecl = getCanonicalDecl(NewLCDecl); 7478 LCRef = NewLCRefExpr; 7479 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB)) 7480 if (const CXXConstructorDecl *Ctor = CE->getConstructor()) 7481 if ((Ctor->isCopyOrMoveConstructor() || 7482 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && 7483 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) 7484 NewLB = CE->getArg(0)->IgnoreParenImpCasts(); 7485 LB = NewLB; 7486 if (EmitDiags) 7487 InitDependOnLC = doesDependOnLoopCounter(LB, /*IsInitializer=*/true); 7488 return false; 7489 } 7490 7491 bool OpenMPIterationSpaceChecker::setUB(Expr *NewUB, 7492 llvm::Optional<bool> LessOp, 7493 bool StrictOp, SourceRange SR, 7494 SourceLocation SL) { 7495 // State consistency checking to ensure correct usage. 7496 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr && 7497 Step == nullptr && !TestIsLessOp && !TestIsStrictOp); 7498 if (!NewUB || NewUB->containsErrors()) 7499 return true; 7500 UB = NewUB; 7501 if (LessOp) 7502 TestIsLessOp = LessOp; 7503 TestIsStrictOp = StrictOp; 7504 ConditionSrcRange = SR; 7505 ConditionLoc = SL; 7506 CondDependOnLC = doesDependOnLoopCounter(UB, /*IsInitializer=*/false); 7507 return false; 7508 } 7509 7510 bool OpenMPIterationSpaceChecker::setStep(Expr *NewStep, bool Subtract) { 7511 // State consistency checking to ensure correct usage. 7512 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr); 7513 if (!NewStep || NewStep->containsErrors()) 7514 return true; 7515 if (!NewStep->isValueDependent()) { 7516 // Check that the step is integer expression. 7517 SourceLocation StepLoc = NewStep->getBeginLoc(); 7518 ExprResult Val = SemaRef.PerformOpenMPImplicitIntegerConversion( 7519 StepLoc, getExprAsWritten(NewStep)); 7520 if (Val.isInvalid()) 7521 return true; 7522 NewStep = Val.get(); 7523 7524 // OpenMP [2.6, Canonical Loop Form, Restrictions] 7525 // If test-expr is of form var relational-op b and relational-op is < or 7526 // <= then incr-expr must cause var to increase on each iteration of the 7527 // loop. If test-expr is of form var relational-op b and relational-op is 7528 // > or >= then incr-expr must cause var to decrease on each iteration of 7529 // the loop. 7530 // If test-expr is of form b relational-op var and relational-op is < or 7531 // <= then incr-expr must cause var to decrease on each iteration of the 7532 // loop. If test-expr is of form b relational-op var and relational-op is 7533 // > or >= then incr-expr must cause var to increase on each iteration of 7534 // the loop. 7535 Optional<llvm::APSInt> Result = 7536 NewStep->getIntegerConstantExpr(SemaRef.Context); 7537 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation(); 7538 bool IsConstNeg = 7539 Result && Result->isSigned() && (Subtract != Result->isNegative()); 7540 bool IsConstPos = 7541 Result && Result->isSigned() && (Subtract == Result->isNegative()); 7542 bool IsConstZero = Result && !Result->getBoolValue(); 7543 7544 // != with increment is treated as <; != with decrement is treated as > 7545 if (!TestIsLessOp.hasValue()) 7546 TestIsLessOp = IsConstPos || (IsUnsigned && !Subtract); 7547 if (UB && (IsConstZero || 7548 (TestIsLessOp.getValue() ? 7549 (IsConstNeg || (IsUnsigned && Subtract)) : 7550 (IsConstPos || (IsUnsigned && !Subtract))))) { 7551 SemaRef.Diag(NewStep->getExprLoc(), 7552 diag::err_omp_loop_incr_not_compatible) 7553 << LCDecl << TestIsLessOp.getValue() << NewStep->getSourceRange(); 7554 SemaRef.Diag(ConditionLoc, 7555 diag::note_omp_loop_cond_requres_compatible_incr) 7556 << TestIsLessOp.getValue() << ConditionSrcRange; 7557 return true; 7558 } 7559 if (TestIsLessOp.getValue() == Subtract) { 7560 NewStep = 7561 SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep) 7562 .get(); 7563 Subtract = !Subtract; 7564 } 7565 } 7566 7567 Step = NewStep; 7568 SubtractStep = Subtract; 7569 return false; 7570 } 7571 7572 namespace { 7573 /// Checker for the non-rectangular loops. Checks if the initializer or 7574 /// condition expression references loop counter variable. 7575 class LoopCounterRefChecker final 7576 : public ConstStmtVisitor<LoopCounterRefChecker, bool> { 7577 Sema &SemaRef; 7578 DSAStackTy &Stack; 7579 const ValueDecl *CurLCDecl = nullptr; 7580 const ValueDecl *DepDecl = nullptr; 7581 const ValueDecl *PrevDepDecl = nullptr; 7582 bool IsInitializer = true; 7583 bool SupportsNonRectangular; 7584 unsigned BaseLoopId = 0; 7585 bool checkDecl(const Expr *E, const ValueDecl *VD) { 7586 if (getCanonicalDecl(VD) == getCanonicalDecl(CurLCDecl)) { 7587 SemaRef.Diag(E->getExprLoc(), diag::err_omp_stmt_depends_on_loop_counter) 7588 << (IsInitializer ? 0 : 1); 7589 return false; 7590 } 7591 const auto &&Data = Stack.isLoopControlVariable(VD); 7592 // OpenMP, 2.9.1 Canonical Loop Form, Restrictions. 7593 // The type of the loop iterator on which we depend may not have a random 7594 // access iterator type. 7595 if (Data.first && VD->getType()->isRecordType()) { 7596 SmallString<128> Name; 7597 llvm::raw_svector_ostream OS(Name); 7598 VD->getNameForDiagnostic(OS, SemaRef.getPrintingPolicy(), 7599 /*Qualified=*/true); 7600 SemaRef.Diag(E->getExprLoc(), 7601 diag::err_omp_wrong_dependency_iterator_type) 7602 << OS.str(); 7603 SemaRef.Diag(VD->getLocation(), diag::note_previous_decl) << VD; 7604 return false; 7605 } 7606 if (Data.first && !SupportsNonRectangular) { 7607 SemaRef.Diag(E->getExprLoc(), diag::err_omp_invariant_dependency); 7608 return false; 7609 } 7610 if (Data.first && 7611 (DepDecl || (PrevDepDecl && 7612 getCanonicalDecl(VD) != getCanonicalDecl(PrevDepDecl)))) { 7613 if (!DepDecl && PrevDepDecl) 7614 DepDecl = PrevDepDecl; 7615 SmallString<128> Name; 7616 llvm::raw_svector_ostream OS(Name); 7617 DepDecl->getNameForDiagnostic(OS, SemaRef.getPrintingPolicy(), 7618 /*Qualified=*/true); 7619 SemaRef.Diag(E->getExprLoc(), 7620 diag::err_omp_invariant_or_linear_dependency) 7621 << OS.str(); 7622 return false; 7623 } 7624 if (Data.first) { 7625 DepDecl = VD; 7626 BaseLoopId = Data.first; 7627 } 7628 return Data.first; 7629 } 7630 7631 public: 7632 bool VisitDeclRefExpr(const DeclRefExpr *E) { 7633 const ValueDecl *VD = E->getDecl(); 7634 if (isa<VarDecl>(VD)) 7635 return checkDecl(E, VD); 7636 return false; 7637 } 7638 bool VisitMemberExpr(const MemberExpr *E) { 7639 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) { 7640 const ValueDecl *VD = E->getMemberDecl(); 7641 if (isa<VarDecl>(VD) || isa<FieldDecl>(VD)) 7642 return checkDecl(E, VD); 7643 } 7644 return false; 7645 } 7646 bool VisitStmt(const Stmt *S) { 7647 bool Res = false; 7648 for (const Stmt *Child : S->children()) 7649 Res = (Child && Visit(Child)) || Res; 7650 return Res; 7651 } 7652 explicit LoopCounterRefChecker(Sema &SemaRef, DSAStackTy &Stack, 7653 const ValueDecl *CurLCDecl, bool IsInitializer, 7654 const ValueDecl *PrevDepDecl = nullptr, 7655 bool SupportsNonRectangular = true) 7656 : SemaRef(SemaRef), Stack(Stack), CurLCDecl(CurLCDecl), 7657 PrevDepDecl(PrevDepDecl), IsInitializer(IsInitializer), 7658 SupportsNonRectangular(SupportsNonRectangular) {} 7659 unsigned getBaseLoopId() const { 7660 assert(CurLCDecl && "Expected loop dependency."); 7661 return BaseLoopId; 7662 } 7663 const ValueDecl *getDepDecl() const { 7664 assert(CurLCDecl && "Expected loop dependency."); 7665 return DepDecl; 7666 } 7667 }; 7668 } // namespace 7669 7670 Optional<unsigned> 7671 OpenMPIterationSpaceChecker::doesDependOnLoopCounter(const Stmt *S, 7672 bool IsInitializer) { 7673 // Check for the non-rectangular loops. 7674 LoopCounterRefChecker LoopStmtChecker(SemaRef, Stack, LCDecl, IsInitializer, 7675 DepDecl, SupportsNonRectangular); 7676 if (LoopStmtChecker.Visit(S)) { 7677 DepDecl = LoopStmtChecker.getDepDecl(); 7678 return LoopStmtChecker.getBaseLoopId(); 7679 } 7680 return llvm::None; 7681 } 7682 7683 bool OpenMPIterationSpaceChecker::checkAndSetInit(Stmt *S, bool EmitDiags) { 7684 // Check init-expr for canonical loop form and save loop counter 7685 // variable - #Var and its initialization value - #LB. 7686 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following: 7687 // var = lb 7688 // integer-type var = lb 7689 // random-access-iterator-type var = lb 7690 // pointer-type var = lb 7691 // 7692 if (!S) { 7693 if (EmitDiags) { 7694 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init); 7695 } 7696 return true; 7697 } 7698 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S)) 7699 if (!ExprTemp->cleanupsHaveSideEffects()) 7700 S = ExprTemp->getSubExpr(); 7701 7702 InitSrcRange = S->getSourceRange(); 7703 if (Expr *E = dyn_cast<Expr>(S)) 7704 S = E->IgnoreParens(); 7705 if (auto *BO = dyn_cast<BinaryOperator>(S)) { 7706 if (BO->getOpcode() == BO_Assign) { 7707 Expr *LHS = BO->getLHS()->IgnoreParens(); 7708 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) { 7709 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) 7710 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 7711 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 7712 EmitDiags); 7713 return setLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS(), EmitDiags); 7714 } 7715 if (auto *ME = dyn_cast<MemberExpr>(LHS)) { 7716 if (ME->isArrow() && 7717 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 7718 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 7719 EmitDiags); 7720 } 7721 } 7722 } else if (auto *DS = dyn_cast<DeclStmt>(S)) { 7723 if (DS->isSingleDecl()) { 7724 if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) { 7725 if (Var->hasInit() && !Var->getType()->isReferenceType()) { 7726 // Accept non-canonical init form here but emit ext. warning. 7727 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags) 7728 SemaRef.Diag(S->getBeginLoc(), 7729 diag::ext_omp_loop_not_canonical_init) 7730 << S->getSourceRange(); 7731 return setLCDeclAndLB( 7732 Var, 7733 buildDeclRefExpr(SemaRef, Var, 7734 Var->getType().getNonReferenceType(), 7735 DS->getBeginLoc()), 7736 Var->getInit(), EmitDiags); 7737 } 7738 } 7739 } 7740 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 7741 if (CE->getOperator() == OO_Equal) { 7742 Expr *LHS = CE->getArg(0); 7743 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) { 7744 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) 7745 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 7746 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 7747 EmitDiags); 7748 return setLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1), EmitDiags); 7749 } 7750 if (auto *ME = dyn_cast<MemberExpr>(LHS)) { 7751 if (ME->isArrow() && 7752 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 7753 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 7754 EmitDiags); 7755 } 7756 } 7757 } 7758 7759 if (dependent() || SemaRef.CurContext->isDependentContext()) 7760 return false; 7761 if (EmitDiags) { 7762 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_init) 7763 << S->getSourceRange(); 7764 } 7765 return true; 7766 } 7767 7768 /// Ignore parenthesizes, implicit casts, copy constructor and return the 7769 /// variable (which may be the loop variable) if possible. 7770 static const ValueDecl *getInitLCDecl(const Expr *E) { 7771 if (!E) 7772 return nullptr; 7773 E = getExprAsWritten(E); 7774 if (const auto *CE = dyn_cast_or_null<CXXConstructExpr>(E)) 7775 if (const CXXConstructorDecl *Ctor = CE->getConstructor()) 7776 if ((Ctor->isCopyOrMoveConstructor() || 7777 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && 7778 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) 7779 E = CE->getArg(0)->IgnoreParenImpCasts(); 7780 if (const auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) { 7781 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) 7782 return getCanonicalDecl(VD); 7783 } 7784 if (const auto *ME = dyn_cast_or_null<MemberExpr>(E)) 7785 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 7786 return getCanonicalDecl(ME->getMemberDecl()); 7787 return nullptr; 7788 } 7789 7790 bool OpenMPIterationSpaceChecker::checkAndSetCond(Expr *S) { 7791 // Check test-expr for canonical form, save upper-bound UB, flags for 7792 // less/greater and for strict/non-strict comparison. 7793 // OpenMP [2.9] Canonical loop form. Test-expr may be one of the following: 7794 // var relational-op b 7795 // b relational-op var 7796 // 7797 bool IneqCondIsCanonical = SemaRef.getLangOpts().OpenMP >= 50; 7798 if (!S) { 7799 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) 7800 << (IneqCondIsCanonical ? 1 : 0) << LCDecl; 7801 return true; 7802 } 7803 Condition = S; 7804 S = getExprAsWritten(S); 7805 SourceLocation CondLoc = S->getBeginLoc(); 7806 auto &&CheckAndSetCond = [this, IneqCondIsCanonical]( 7807 BinaryOperatorKind Opcode, const Expr *LHS, 7808 const Expr *RHS, SourceRange SR, 7809 SourceLocation OpLoc) -> llvm::Optional<bool> { 7810 if (BinaryOperator::isRelationalOp(Opcode)) { 7811 if (getInitLCDecl(LHS) == LCDecl) 7812 return setUB(const_cast<Expr *>(RHS), 7813 (Opcode == BO_LT || Opcode == BO_LE), 7814 (Opcode == BO_LT || Opcode == BO_GT), SR, OpLoc); 7815 if (getInitLCDecl(RHS) == LCDecl) 7816 return setUB(const_cast<Expr *>(LHS), 7817 (Opcode == BO_GT || Opcode == BO_GE), 7818 (Opcode == BO_LT || Opcode == BO_GT), SR, OpLoc); 7819 } else if (IneqCondIsCanonical && Opcode == BO_NE) { 7820 return setUB(const_cast<Expr *>(getInitLCDecl(LHS) == LCDecl ? RHS : LHS), 7821 /*LessOp=*/llvm::None, 7822 /*StrictOp=*/true, SR, OpLoc); 7823 } 7824 return llvm::None; 7825 }; 7826 llvm::Optional<bool> Res; 7827 if (auto *RBO = dyn_cast<CXXRewrittenBinaryOperator>(S)) { 7828 CXXRewrittenBinaryOperator::DecomposedForm DF = RBO->getDecomposedForm(); 7829 Res = CheckAndSetCond(DF.Opcode, DF.LHS, DF.RHS, RBO->getSourceRange(), 7830 RBO->getOperatorLoc()); 7831 } else if (auto *BO = dyn_cast<BinaryOperator>(S)) { 7832 Res = CheckAndSetCond(BO->getOpcode(), BO->getLHS(), BO->getRHS(), 7833 BO->getSourceRange(), BO->getOperatorLoc()); 7834 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 7835 if (CE->getNumArgs() == 2) { 7836 Res = CheckAndSetCond( 7837 BinaryOperator::getOverloadedOpcode(CE->getOperator()), CE->getArg(0), 7838 CE->getArg(1), CE->getSourceRange(), CE->getOperatorLoc()); 7839 } 7840 } 7841 if (Res.hasValue()) 7842 return *Res; 7843 if (dependent() || SemaRef.CurContext->isDependentContext()) 7844 return false; 7845 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond) 7846 << (IneqCondIsCanonical ? 1 : 0) << S->getSourceRange() << LCDecl; 7847 return true; 7848 } 7849 7850 bool OpenMPIterationSpaceChecker::checkAndSetIncRHS(Expr *RHS) { 7851 // RHS of canonical loop form increment can be: 7852 // var + incr 7853 // incr + var 7854 // var - incr 7855 // 7856 RHS = RHS->IgnoreParenImpCasts(); 7857 if (auto *BO = dyn_cast<BinaryOperator>(RHS)) { 7858 if (BO->isAdditiveOp()) { 7859 bool IsAdd = BO->getOpcode() == BO_Add; 7860 if (getInitLCDecl(BO->getLHS()) == LCDecl) 7861 return setStep(BO->getRHS(), !IsAdd); 7862 if (IsAdd && getInitLCDecl(BO->getRHS()) == LCDecl) 7863 return setStep(BO->getLHS(), /*Subtract=*/false); 7864 } 7865 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) { 7866 bool IsAdd = CE->getOperator() == OO_Plus; 7867 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) { 7868 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 7869 return setStep(CE->getArg(1), !IsAdd); 7870 if (IsAdd && getInitLCDecl(CE->getArg(1)) == LCDecl) 7871 return setStep(CE->getArg(0), /*Subtract=*/false); 7872 } 7873 } 7874 if (dependent() || SemaRef.CurContext->isDependentContext()) 7875 return false; 7876 SemaRef.Diag(RHS->getBeginLoc(), diag::err_omp_loop_not_canonical_incr) 7877 << RHS->getSourceRange() << LCDecl; 7878 return true; 7879 } 7880 7881 bool OpenMPIterationSpaceChecker::checkAndSetInc(Expr *S) { 7882 // Check incr-expr for canonical loop form and return true if it 7883 // does not conform. 7884 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following: 7885 // ++var 7886 // var++ 7887 // --var 7888 // var-- 7889 // var += incr 7890 // var -= incr 7891 // var = var + incr 7892 // var = incr + var 7893 // var = var - incr 7894 // 7895 if (!S) { 7896 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl; 7897 return true; 7898 } 7899 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S)) 7900 if (!ExprTemp->cleanupsHaveSideEffects()) 7901 S = ExprTemp->getSubExpr(); 7902 7903 IncrementSrcRange = S->getSourceRange(); 7904 S = S->IgnoreParens(); 7905 if (auto *UO = dyn_cast<UnaryOperator>(S)) { 7906 if (UO->isIncrementDecrementOp() && 7907 getInitLCDecl(UO->getSubExpr()) == LCDecl) 7908 return setStep(SemaRef 7909 .ActOnIntegerConstant(UO->getBeginLoc(), 7910 (UO->isDecrementOp() ? -1 : 1)) 7911 .get(), 7912 /*Subtract=*/false); 7913 } else if (auto *BO = dyn_cast<BinaryOperator>(S)) { 7914 switch (BO->getOpcode()) { 7915 case BO_AddAssign: 7916 case BO_SubAssign: 7917 if (getInitLCDecl(BO->getLHS()) == LCDecl) 7918 return setStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign); 7919 break; 7920 case BO_Assign: 7921 if (getInitLCDecl(BO->getLHS()) == LCDecl) 7922 return checkAndSetIncRHS(BO->getRHS()); 7923 break; 7924 default: 7925 break; 7926 } 7927 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 7928 switch (CE->getOperator()) { 7929 case OO_PlusPlus: 7930 case OO_MinusMinus: 7931 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 7932 return setStep(SemaRef 7933 .ActOnIntegerConstant( 7934 CE->getBeginLoc(), 7935 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)) 7936 .get(), 7937 /*Subtract=*/false); 7938 break; 7939 case OO_PlusEqual: 7940 case OO_MinusEqual: 7941 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 7942 return setStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual); 7943 break; 7944 case OO_Equal: 7945 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 7946 return checkAndSetIncRHS(CE->getArg(1)); 7947 break; 7948 default: 7949 break; 7950 } 7951 } 7952 if (dependent() || SemaRef.CurContext->isDependentContext()) 7953 return false; 7954 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_incr) 7955 << S->getSourceRange() << LCDecl; 7956 return true; 7957 } 7958 7959 static ExprResult 7960 tryBuildCapture(Sema &SemaRef, Expr *Capture, 7961 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 7962 if (SemaRef.CurContext->isDependentContext() || Capture->containsErrors()) 7963 return Capture; 7964 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects)) 7965 return SemaRef.PerformImplicitConversion( 7966 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting, 7967 /*AllowExplicit=*/true); 7968 auto I = Captures.find(Capture); 7969 if (I != Captures.end()) 7970 return buildCapture(SemaRef, Capture, I->second); 7971 DeclRefExpr *Ref = nullptr; 7972 ExprResult Res = buildCapture(SemaRef, Capture, Ref); 7973 Captures[Capture] = Ref; 7974 return Res; 7975 } 7976 7977 /// Calculate number of iterations, transforming to unsigned, if number of 7978 /// iterations may be larger than the original type. 7979 static Expr * 7980 calculateNumIters(Sema &SemaRef, Scope *S, SourceLocation DefaultLoc, 7981 Expr *Lower, Expr *Upper, Expr *Step, QualType LCTy, 7982 bool TestIsStrictOp, bool RoundToStep, 7983 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 7984 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures); 7985 if (!NewStep.isUsable()) 7986 return nullptr; 7987 llvm::APSInt LRes, SRes; 7988 bool IsLowerConst = false, IsStepConst = false; 7989 if (Optional<llvm::APSInt> Res = Lower->getIntegerConstantExpr(SemaRef.Context)) { 7990 LRes = *Res; 7991 IsLowerConst = true; 7992 } 7993 if (Optional<llvm::APSInt> Res = Step->getIntegerConstantExpr(SemaRef.Context)) { 7994 SRes = *Res; 7995 IsStepConst = true; 7996 } 7997 bool NoNeedToConvert = IsLowerConst && !RoundToStep && 7998 ((!TestIsStrictOp && LRes.isNonNegative()) || 7999 (TestIsStrictOp && LRes.isStrictlyPositive())); 8000 bool NeedToReorganize = false; 8001 // Check if any subexpressions in Lower -Step [+ 1] lead to overflow. 8002 if (!NoNeedToConvert && IsLowerConst && 8003 (TestIsStrictOp || (RoundToStep && IsStepConst))) { 8004 NoNeedToConvert = true; 8005 if (RoundToStep) { 8006 unsigned BW = LRes.getBitWidth() > SRes.getBitWidth() 8007 ? LRes.getBitWidth() 8008 : SRes.getBitWidth(); 8009 LRes = LRes.extend(BW + 1); 8010 LRes.setIsSigned(true); 8011 SRes = SRes.extend(BW + 1); 8012 SRes.setIsSigned(true); 8013 LRes -= SRes; 8014 NoNeedToConvert = LRes.trunc(BW).extend(BW + 1) == LRes; 8015 LRes = LRes.trunc(BW); 8016 } 8017 if (TestIsStrictOp) { 8018 unsigned BW = LRes.getBitWidth(); 8019 LRes = LRes.extend(BW + 1); 8020 LRes.setIsSigned(true); 8021 ++LRes; 8022 NoNeedToConvert = 8023 NoNeedToConvert && LRes.trunc(BW).extend(BW + 1) == LRes; 8024 // truncate to the original bitwidth. 8025 LRes = LRes.trunc(BW); 8026 } 8027 NeedToReorganize = NoNeedToConvert; 8028 } 8029 llvm::APSInt URes; 8030 bool IsUpperConst = false; 8031 if (Optional<llvm::APSInt> Res = Upper->getIntegerConstantExpr(SemaRef.Context)) { 8032 URes = *Res; 8033 IsUpperConst = true; 8034 } 8035 if (NoNeedToConvert && IsLowerConst && IsUpperConst && 8036 (!RoundToStep || IsStepConst)) { 8037 unsigned BW = LRes.getBitWidth() > URes.getBitWidth() ? LRes.getBitWidth() 8038 : URes.getBitWidth(); 8039 LRes = LRes.extend(BW + 1); 8040 LRes.setIsSigned(true); 8041 URes = URes.extend(BW + 1); 8042 URes.setIsSigned(true); 8043 URes -= LRes; 8044 NoNeedToConvert = URes.trunc(BW).extend(BW + 1) == URes; 8045 NeedToReorganize = NoNeedToConvert; 8046 } 8047 // If the boundaries are not constant or (Lower - Step [+ 1]) is not constant 8048 // or less than zero (Upper - (Lower - Step [+ 1]) may overflow) - promote to 8049 // unsigned. 8050 if ((!NoNeedToConvert || (LRes.isNegative() && !IsUpperConst)) && 8051 !LCTy->isDependentType() && LCTy->isIntegerType()) { 8052 QualType LowerTy = Lower->getType(); 8053 QualType UpperTy = Upper->getType(); 8054 uint64_t LowerSize = SemaRef.Context.getTypeSize(LowerTy); 8055 uint64_t UpperSize = SemaRef.Context.getTypeSize(UpperTy); 8056 if ((LowerSize <= UpperSize && UpperTy->hasSignedIntegerRepresentation()) || 8057 (LowerSize > UpperSize && LowerTy->hasSignedIntegerRepresentation())) { 8058 QualType CastType = SemaRef.Context.getIntTypeForBitwidth( 8059 LowerSize > UpperSize ? LowerSize : UpperSize, /*Signed=*/0); 8060 Upper = 8061 SemaRef 8062 .PerformImplicitConversion( 8063 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Upper).get(), 8064 CastType, Sema::AA_Converting) 8065 .get(); 8066 Lower = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Lower).get(); 8067 NewStep = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, NewStep.get()); 8068 } 8069 } 8070 if (!Lower || !Upper || NewStep.isInvalid()) 8071 return nullptr; 8072 8073 ExprResult Diff; 8074 // If need to reorganize, then calculate the form as Upper - (Lower - Step [+ 8075 // 1]). 8076 if (NeedToReorganize) { 8077 Diff = Lower; 8078 8079 if (RoundToStep) { 8080 // Lower - Step 8081 Diff = 8082 SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Diff.get(), NewStep.get()); 8083 if (!Diff.isUsable()) 8084 return nullptr; 8085 } 8086 8087 // Lower - Step [+ 1] 8088 if (TestIsStrictOp) 8089 Diff = SemaRef.BuildBinOp( 8090 S, DefaultLoc, BO_Add, Diff.get(), 8091 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 8092 if (!Diff.isUsable()) 8093 return nullptr; 8094 8095 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 8096 if (!Diff.isUsable()) 8097 return nullptr; 8098 8099 // Upper - (Lower - Step [+ 1]). 8100 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Diff.get()); 8101 if (!Diff.isUsable()) 8102 return nullptr; 8103 } else { 8104 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower); 8105 8106 if (!Diff.isUsable() && LCTy->getAsCXXRecordDecl()) { 8107 // BuildBinOp already emitted error, this one is to point user to upper 8108 // and lower bound, and to tell what is passed to 'operator-'. 8109 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx) 8110 << Upper->getSourceRange() << Lower->getSourceRange(); 8111 return nullptr; 8112 } 8113 8114 if (!Diff.isUsable()) 8115 return nullptr; 8116 8117 // Upper - Lower [- 1] 8118 if (TestIsStrictOp) 8119 Diff = SemaRef.BuildBinOp( 8120 S, DefaultLoc, BO_Sub, Diff.get(), 8121 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 8122 if (!Diff.isUsable()) 8123 return nullptr; 8124 8125 if (RoundToStep) { 8126 // Upper - Lower [- 1] + Step 8127 Diff = 8128 SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get()); 8129 if (!Diff.isUsable()) 8130 return nullptr; 8131 } 8132 } 8133 8134 // Parentheses (for dumping/debugging purposes only). 8135 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 8136 if (!Diff.isUsable()) 8137 return nullptr; 8138 8139 // (Upper - Lower [- 1] + Step) / Step or (Upper - Lower) / Step 8140 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get()); 8141 if (!Diff.isUsable()) 8142 return nullptr; 8143 8144 return Diff.get(); 8145 } 8146 8147 /// Build the expression to calculate the number of iterations. 8148 Expr *OpenMPIterationSpaceChecker::buildNumIterations( 8149 Scope *S, ArrayRef<LoopIterationSpace> ResultIterSpaces, bool LimitedType, 8150 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 8151 QualType VarType = LCDecl->getType().getNonReferenceType(); 8152 if (!VarType->isIntegerType() && !VarType->isPointerType() && 8153 !SemaRef.getLangOpts().CPlusPlus) 8154 return nullptr; 8155 Expr *LBVal = LB; 8156 Expr *UBVal = UB; 8157 // LB = TestIsLessOp.getValue() ? min(LB(MinVal), LB(MaxVal)) : 8158 // max(LB(MinVal), LB(MaxVal)) 8159 if (InitDependOnLC) { 8160 const LoopIterationSpace &IS = ResultIterSpaces[*InitDependOnLC - 1]; 8161 if (!IS.MinValue || !IS.MaxValue) 8162 return nullptr; 8163 // OuterVar = Min 8164 ExprResult MinValue = 8165 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MinValue); 8166 if (!MinValue.isUsable()) 8167 return nullptr; 8168 8169 ExprResult LBMinVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign, 8170 IS.CounterVar, MinValue.get()); 8171 if (!LBMinVal.isUsable()) 8172 return nullptr; 8173 // OuterVar = Min, LBVal 8174 LBMinVal = 8175 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, LBMinVal.get(), LBVal); 8176 if (!LBMinVal.isUsable()) 8177 return nullptr; 8178 // (OuterVar = Min, LBVal) 8179 LBMinVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, LBMinVal.get()); 8180 if (!LBMinVal.isUsable()) 8181 return nullptr; 8182 8183 // OuterVar = Max 8184 ExprResult MaxValue = 8185 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MaxValue); 8186 if (!MaxValue.isUsable()) 8187 return nullptr; 8188 8189 ExprResult LBMaxVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign, 8190 IS.CounterVar, MaxValue.get()); 8191 if (!LBMaxVal.isUsable()) 8192 return nullptr; 8193 // OuterVar = Max, LBVal 8194 LBMaxVal = 8195 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, LBMaxVal.get(), LBVal); 8196 if (!LBMaxVal.isUsable()) 8197 return nullptr; 8198 // (OuterVar = Max, LBVal) 8199 LBMaxVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, LBMaxVal.get()); 8200 if (!LBMaxVal.isUsable()) 8201 return nullptr; 8202 8203 Expr *LBMin = tryBuildCapture(SemaRef, LBMinVal.get(), Captures).get(); 8204 Expr *LBMax = tryBuildCapture(SemaRef, LBMaxVal.get(), Captures).get(); 8205 if (!LBMin || !LBMax) 8206 return nullptr; 8207 // LB(MinVal) < LB(MaxVal) 8208 ExprResult MinLessMaxRes = 8209 SemaRef.BuildBinOp(S, DefaultLoc, BO_LT, LBMin, LBMax); 8210 if (!MinLessMaxRes.isUsable()) 8211 return nullptr; 8212 Expr *MinLessMax = 8213 tryBuildCapture(SemaRef, MinLessMaxRes.get(), Captures).get(); 8214 if (!MinLessMax) 8215 return nullptr; 8216 if (TestIsLessOp.getValue()) { 8217 // LB(MinVal) < LB(MaxVal) ? LB(MinVal) : LB(MaxVal) - min(LB(MinVal), 8218 // LB(MaxVal)) 8219 ExprResult MinLB = SemaRef.ActOnConditionalOp(DefaultLoc, DefaultLoc, 8220 MinLessMax, LBMin, LBMax); 8221 if (!MinLB.isUsable()) 8222 return nullptr; 8223 LBVal = MinLB.get(); 8224 } else { 8225 // LB(MinVal) < LB(MaxVal) ? LB(MaxVal) : LB(MinVal) - max(LB(MinVal), 8226 // LB(MaxVal)) 8227 ExprResult MaxLB = SemaRef.ActOnConditionalOp(DefaultLoc, DefaultLoc, 8228 MinLessMax, LBMax, LBMin); 8229 if (!MaxLB.isUsable()) 8230 return nullptr; 8231 LBVal = MaxLB.get(); 8232 } 8233 } 8234 // UB = TestIsLessOp.getValue() ? max(UB(MinVal), UB(MaxVal)) : 8235 // min(UB(MinVal), UB(MaxVal)) 8236 if (CondDependOnLC) { 8237 const LoopIterationSpace &IS = ResultIterSpaces[*CondDependOnLC - 1]; 8238 if (!IS.MinValue || !IS.MaxValue) 8239 return nullptr; 8240 // OuterVar = Min 8241 ExprResult MinValue = 8242 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MinValue); 8243 if (!MinValue.isUsable()) 8244 return nullptr; 8245 8246 ExprResult UBMinVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign, 8247 IS.CounterVar, MinValue.get()); 8248 if (!UBMinVal.isUsable()) 8249 return nullptr; 8250 // OuterVar = Min, UBVal 8251 UBMinVal = 8252 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, UBMinVal.get(), UBVal); 8253 if (!UBMinVal.isUsable()) 8254 return nullptr; 8255 // (OuterVar = Min, UBVal) 8256 UBMinVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, UBMinVal.get()); 8257 if (!UBMinVal.isUsable()) 8258 return nullptr; 8259 8260 // OuterVar = Max 8261 ExprResult MaxValue = 8262 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MaxValue); 8263 if (!MaxValue.isUsable()) 8264 return nullptr; 8265 8266 ExprResult UBMaxVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign, 8267 IS.CounterVar, MaxValue.get()); 8268 if (!UBMaxVal.isUsable()) 8269 return nullptr; 8270 // OuterVar = Max, UBVal 8271 UBMaxVal = 8272 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, UBMaxVal.get(), UBVal); 8273 if (!UBMaxVal.isUsable()) 8274 return nullptr; 8275 // (OuterVar = Max, UBVal) 8276 UBMaxVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, UBMaxVal.get()); 8277 if (!UBMaxVal.isUsable()) 8278 return nullptr; 8279 8280 Expr *UBMin = tryBuildCapture(SemaRef, UBMinVal.get(), Captures).get(); 8281 Expr *UBMax = tryBuildCapture(SemaRef, UBMaxVal.get(), Captures).get(); 8282 if (!UBMin || !UBMax) 8283 return nullptr; 8284 // UB(MinVal) > UB(MaxVal) 8285 ExprResult MinGreaterMaxRes = 8286 SemaRef.BuildBinOp(S, DefaultLoc, BO_GT, UBMin, UBMax); 8287 if (!MinGreaterMaxRes.isUsable()) 8288 return nullptr; 8289 Expr *MinGreaterMax = 8290 tryBuildCapture(SemaRef, MinGreaterMaxRes.get(), Captures).get(); 8291 if (!MinGreaterMax) 8292 return nullptr; 8293 if (TestIsLessOp.getValue()) { 8294 // UB(MinVal) > UB(MaxVal) ? UB(MinVal) : UB(MaxVal) - max(UB(MinVal), 8295 // UB(MaxVal)) 8296 ExprResult MaxUB = SemaRef.ActOnConditionalOp( 8297 DefaultLoc, DefaultLoc, MinGreaterMax, UBMin, UBMax); 8298 if (!MaxUB.isUsable()) 8299 return nullptr; 8300 UBVal = MaxUB.get(); 8301 } else { 8302 // UB(MinVal) > UB(MaxVal) ? UB(MaxVal) : UB(MinVal) - min(UB(MinVal), 8303 // UB(MaxVal)) 8304 ExprResult MinUB = SemaRef.ActOnConditionalOp( 8305 DefaultLoc, DefaultLoc, MinGreaterMax, UBMax, UBMin); 8306 if (!MinUB.isUsable()) 8307 return nullptr; 8308 UBVal = MinUB.get(); 8309 } 8310 } 8311 Expr *UBExpr = TestIsLessOp.getValue() ? UBVal : LBVal; 8312 Expr *LBExpr = TestIsLessOp.getValue() ? LBVal : UBVal; 8313 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get(); 8314 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get(); 8315 if (!Upper || !Lower) 8316 return nullptr; 8317 8318 ExprResult Diff = calculateNumIters(SemaRef, S, DefaultLoc, Lower, Upper, 8319 Step, VarType, TestIsStrictOp, 8320 /*RoundToStep=*/true, Captures); 8321 if (!Diff.isUsable()) 8322 return nullptr; 8323 8324 // OpenMP runtime requires 32-bit or 64-bit loop variables. 8325 QualType Type = Diff.get()->getType(); 8326 ASTContext &C = SemaRef.Context; 8327 bool UseVarType = VarType->hasIntegerRepresentation() && 8328 C.getTypeSize(Type) > C.getTypeSize(VarType); 8329 if (!Type->isIntegerType() || UseVarType) { 8330 unsigned NewSize = 8331 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type); 8332 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation() 8333 : Type->hasSignedIntegerRepresentation(); 8334 Type = C.getIntTypeForBitwidth(NewSize, IsSigned); 8335 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) { 8336 Diff = SemaRef.PerformImplicitConversion( 8337 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true); 8338 if (!Diff.isUsable()) 8339 return nullptr; 8340 } 8341 } 8342 if (LimitedType) { 8343 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32; 8344 if (NewSize != C.getTypeSize(Type)) { 8345 if (NewSize < C.getTypeSize(Type)) { 8346 assert(NewSize == 64 && "incorrect loop var size"); 8347 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var) 8348 << InitSrcRange << ConditionSrcRange; 8349 } 8350 QualType NewType = C.getIntTypeForBitwidth( 8351 NewSize, Type->hasSignedIntegerRepresentation() || 8352 C.getTypeSize(Type) < NewSize); 8353 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) { 8354 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType, 8355 Sema::AA_Converting, true); 8356 if (!Diff.isUsable()) 8357 return nullptr; 8358 } 8359 } 8360 } 8361 8362 return Diff.get(); 8363 } 8364 8365 std::pair<Expr *, Expr *> OpenMPIterationSpaceChecker::buildMinMaxValues( 8366 Scope *S, llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 8367 // Do not build for iterators, they cannot be used in non-rectangular loop 8368 // nests. 8369 if (LCDecl->getType()->isRecordType()) 8370 return std::make_pair(nullptr, nullptr); 8371 // If we subtract, the min is in the condition, otherwise the min is in the 8372 // init value. 8373 Expr *MinExpr = nullptr; 8374 Expr *MaxExpr = nullptr; 8375 Expr *LBExpr = TestIsLessOp.getValue() ? LB : UB; 8376 Expr *UBExpr = TestIsLessOp.getValue() ? UB : LB; 8377 bool LBNonRect = TestIsLessOp.getValue() ? InitDependOnLC.hasValue() 8378 : CondDependOnLC.hasValue(); 8379 bool UBNonRect = TestIsLessOp.getValue() ? CondDependOnLC.hasValue() 8380 : InitDependOnLC.hasValue(); 8381 Expr *Lower = 8382 LBNonRect ? LBExpr : tryBuildCapture(SemaRef, LBExpr, Captures).get(); 8383 Expr *Upper = 8384 UBNonRect ? UBExpr : tryBuildCapture(SemaRef, UBExpr, Captures).get(); 8385 if (!Upper || !Lower) 8386 return std::make_pair(nullptr, nullptr); 8387 8388 if (TestIsLessOp.getValue()) 8389 MinExpr = Lower; 8390 else 8391 MaxExpr = Upper; 8392 8393 // Build minimum/maximum value based on number of iterations. 8394 QualType VarType = LCDecl->getType().getNonReferenceType(); 8395 8396 ExprResult Diff = calculateNumIters(SemaRef, S, DefaultLoc, Lower, Upper, 8397 Step, VarType, TestIsStrictOp, 8398 /*RoundToStep=*/false, Captures); 8399 if (!Diff.isUsable()) 8400 return std::make_pair(nullptr, nullptr); 8401 8402 // ((Upper - Lower [- 1]) / Step) * Step 8403 // Parentheses (for dumping/debugging purposes only). 8404 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 8405 if (!Diff.isUsable()) 8406 return std::make_pair(nullptr, nullptr); 8407 8408 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures); 8409 if (!NewStep.isUsable()) 8410 return std::make_pair(nullptr, nullptr); 8411 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Mul, Diff.get(), NewStep.get()); 8412 if (!Diff.isUsable()) 8413 return std::make_pair(nullptr, nullptr); 8414 8415 // Parentheses (for dumping/debugging purposes only). 8416 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 8417 if (!Diff.isUsable()) 8418 return std::make_pair(nullptr, nullptr); 8419 8420 // Convert to the ptrdiff_t, if original type is pointer. 8421 if (VarType->isAnyPointerType() && 8422 !SemaRef.Context.hasSameType( 8423 Diff.get()->getType(), 8424 SemaRef.Context.getUnsignedPointerDiffType())) { 8425 Diff = SemaRef.PerformImplicitConversion( 8426 Diff.get(), SemaRef.Context.getUnsignedPointerDiffType(), 8427 Sema::AA_Converting, /*AllowExplicit=*/true); 8428 } 8429 if (!Diff.isUsable()) 8430 return std::make_pair(nullptr, nullptr); 8431 8432 if (TestIsLessOp.getValue()) { 8433 // MinExpr = Lower; 8434 // MaxExpr = Lower + (((Upper - Lower [- 1]) / Step) * Step) 8435 Diff = SemaRef.BuildBinOp( 8436 S, DefaultLoc, BO_Add, 8437 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Lower).get(), 8438 Diff.get()); 8439 if (!Diff.isUsable()) 8440 return std::make_pair(nullptr, nullptr); 8441 } else { 8442 // MaxExpr = Upper; 8443 // MinExpr = Upper - (((Upper - Lower [- 1]) / Step) * Step) 8444 Diff = SemaRef.BuildBinOp( 8445 S, DefaultLoc, BO_Sub, 8446 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Upper).get(), 8447 Diff.get()); 8448 if (!Diff.isUsable()) 8449 return std::make_pair(nullptr, nullptr); 8450 } 8451 8452 // Convert to the original type. 8453 if (SemaRef.Context.hasSameType(Diff.get()->getType(), VarType)) 8454 Diff = SemaRef.PerformImplicitConversion(Diff.get(), VarType, 8455 Sema::AA_Converting, 8456 /*AllowExplicit=*/true); 8457 if (!Diff.isUsable()) 8458 return std::make_pair(nullptr, nullptr); 8459 8460 Sema::TentativeAnalysisScope Trap(SemaRef); 8461 Diff = SemaRef.ActOnFinishFullExpr(Diff.get(), /*DiscardedValue=*/false); 8462 if (!Diff.isUsable()) 8463 return std::make_pair(nullptr, nullptr); 8464 8465 if (TestIsLessOp.getValue()) 8466 MaxExpr = Diff.get(); 8467 else 8468 MinExpr = Diff.get(); 8469 8470 return std::make_pair(MinExpr, MaxExpr); 8471 } 8472 8473 Expr *OpenMPIterationSpaceChecker::buildFinalCondition(Scope *S) const { 8474 if (InitDependOnLC || CondDependOnLC) 8475 return Condition; 8476 return nullptr; 8477 } 8478 8479 Expr *OpenMPIterationSpaceChecker::buildPreCond( 8480 Scope *S, Expr *Cond, 8481 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 8482 // Do not build a precondition when the condition/initialization is dependent 8483 // to prevent pessimistic early loop exit. 8484 // TODO: this can be improved by calculating min/max values but not sure that 8485 // it will be very effective. 8486 if (CondDependOnLC || InitDependOnLC) 8487 return SemaRef.PerformImplicitConversion( 8488 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(), 8489 SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting, 8490 /*AllowExplicit=*/true).get(); 8491 8492 // Try to build LB <op> UB, where <op> is <, >, <=, or >=. 8493 Sema::TentativeAnalysisScope Trap(SemaRef); 8494 8495 ExprResult NewLB = tryBuildCapture(SemaRef, LB, Captures); 8496 ExprResult NewUB = tryBuildCapture(SemaRef, UB, Captures); 8497 if (!NewLB.isUsable() || !NewUB.isUsable()) 8498 return nullptr; 8499 8500 ExprResult CondExpr = 8501 SemaRef.BuildBinOp(S, DefaultLoc, 8502 TestIsLessOp.getValue() ? 8503 (TestIsStrictOp ? BO_LT : BO_LE) : 8504 (TestIsStrictOp ? BO_GT : BO_GE), 8505 NewLB.get(), NewUB.get()); 8506 if (CondExpr.isUsable()) { 8507 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(), 8508 SemaRef.Context.BoolTy)) 8509 CondExpr = SemaRef.PerformImplicitConversion( 8510 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting, 8511 /*AllowExplicit=*/true); 8512 } 8513 8514 // Otherwise use original loop condition and evaluate it in runtime. 8515 return CondExpr.isUsable() ? CondExpr.get() : Cond; 8516 } 8517 8518 /// Build reference expression to the counter be used for codegen. 8519 DeclRefExpr *OpenMPIterationSpaceChecker::buildCounterVar( 8520 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 8521 DSAStackTy &DSA) const { 8522 auto *VD = dyn_cast<VarDecl>(LCDecl); 8523 if (!VD) { 8524 VD = SemaRef.isOpenMPCapturedDecl(LCDecl); 8525 DeclRefExpr *Ref = buildDeclRefExpr( 8526 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc); 8527 const DSAStackTy::DSAVarData Data = 8528 DSA.getTopDSA(LCDecl, /*FromParent=*/false); 8529 // If the loop control decl is explicitly marked as private, do not mark it 8530 // as captured again. 8531 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr) 8532 Captures.insert(std::make_pair(LCRef, Ref)); 8533 return Ref; 8534 } 8535 return cast<DeclRefExpr>(LCRef); 8536 } 8537 8538 Expr *OpenMPIterationSpaceChecker::buildPrivateCounterVar() const { 8539 if (LCDecl && !LCDecl->isInvalidDecl()) { 8540 QualType Type = LCDecl->getType().getNonReferenceType(); 8541 VarDecl *PrivateVar = buildVarDecl( 8542 SemaRef, DefaultLoc, Type, LCDecl->getName(), 8543 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr, 8544 isa<VarDecl>(LCDecl) 8545 ? buildDeclRefExpr(SemaRef, cast<VarDecl>(LCDecl), Type, DefaultLoc) 8546 : nullptr); 8547 if (PrivateVar->isInvalidDecl()) 8548 return nullptr; 8549 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc); 8550 } 8551 return nullptr; 8552 } 8553 8554 /// Build initialization of the counter to be used for codegen. 8555 Expr *OpenMPIterationSpaceChecker::buildCounterInit() const { return LB; } 8556 8557 /// Build step of the counter be used for codegen. 8558 Expr *OpenMPIterationSpaceChecker::buildCounterStep() const { return Step; } 8559 8560 Expr *OpenMPIterationSpaceChecker::buildOrderedLoopData( 8561 Scope *S, Expr *Counter, 8562 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, SourceLocation Loc, 8563 Expr *Inc, OverloadedOperatorKind OOK) { 8564 Expr *Cnt = SemaRef.DefaultLvalueConversion(Counter).get(); 8565 if (!Cnt) 8566 return nullptr; 8567 if (Inc) { 8568 assert((OOK == OO_Plus || OOK == OO_Minus) && 8569 "Expected only + or - operations for depend clauses."); 8570 BinaryOperatorKind BOK = (OOK == OO_Plus) ? BO_Add : BO_Sub; 8571 Cnt = SemaRef.BuildBinOp(S, Loc, BOK, Cnt, Inc).get(); 8572 if (!Cnt) 8573 return nullptr; 8574 } 8575 QualType VarType = LCDecl->getType().getNonReferenceType(); 8576 if (!VarType->isIntegerType() && !VarType->isPointerType() && 8577 !SemaRef.getLangOpts().CPlusPlus) 8578 return nullptr; 8579 // Upper - Lower 8580 Expr *Upper = TestIsLessOp.getValue() 8581 ? Cnt 8582 : tryBuildCapture(SemaRef, LB, Captures).get(); 8583 Expr *Lower = TestIsLessOp.getValue() 8584 ? tryBuildCapture(SemaRef, LB, Captures).get() 8585 : Cnt; 8586 if (!Upper || !Lower) 8587 return nullptr; 8588 8589 ExprResult Diff = calculateNumIters( 8590 SemaRef, S, DefaultLoc, Lower, Upper, Step, VarType, 8591 /*TestIsStrictOp=*/false, /*RoundToStep=*/false, Captures); 8592 if (!Diff.isUsable()) 8593 return nullptr; 8594 8595 return Diff.get(); 8596 } 8597 } // namespace 8598 8599 void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) { 8600 assert(getLangOpts().OpenMP && "OpenMP is not active."); 8601 assert(Init && "Expected loop in canonical form."); 8602 unsigned AssociatedLoops = DSAStack->getAssociatedLoops(); 8603 if (AssociatedLoops > 0 && 8604 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { 8605 DSAStack->loopStart(); 8606 OpenMPIterationSpaceChecker ISC(*this, /*SupportsNonRectangular=*/true, 8607 *DSAStack, ForLoc); 8608 if (!ISC.checkAndSetInit(Init, /*EmitDiags=*/false)) { 8609 if (ValueDecl *D = ISC.getLoopDecl()) { 8610 auto *VD = dyn_cast<VarDecl>(D); 8611 DeclRefExpr *PrivateRef = nullptr; 8612 if (!VD) { 8613 if (VarDecl *Private = isOpenMPCapturedDecl(D)) { 8614 VD = Private; 8615 } else { 8616 PrivateRef = buildCapture(*this, D, ISC.getLoopDeclRefExpr(), 8617 /*WithInit=*/false); 8618 VD = cast<VarDecl>(PrivateRef->getDecl()); 8619 } 8620 } 8621 DSAStack->addLoopControlVariable(D, VD); 8622 const Decl *LD = DSAStack->getPossiblyLoopCunter(); 8623 if (LD != D->getCanonicalDecl()) { 8624 DSAStack->resetPossibleLoopCounter(); 8625 if (auto *Var = dyn_cast_or_null<VarDecl>(LD)) 8626 MarkDeclarationsReferencedInExpr( 8627 buildDeclRefExpr(*this, const_cast<VarDecl *>(Var), 8628 Var->getType().getNonLValueExprType(Context), 8629 ForLoc, /*RefersToCapture=*/true)); 8630 } 8631 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 8632 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables 8633 // Referenced in a Construct, C/C++]. The loop iteration variable in the 8634 // associated for-loop of a simd construct with just one associated 8635 // for-loop may be listed in a linear clause with a constant-linear-step 8636 // that is the increment of the associated for-loop. The loop iteration 8637 // variable(s) in the associated for-loop(s) of a for or parallel for 8638 // construct may be listed in a private or lastprivate clause. 8639 DSAStackTy::DSAVarData DVar = 8640 DSAStack->getTopDSA(D, /*FromParent=*/false); 8641 // If LoopVarRefExpr is nullptr it means the corresponding loop variable 8642 // is declared in the loop and it is predetermined as a private. 8643 Expr *LoopDeclRefExpr = ISC.getLoopDeclRefExpr(); 8644 OpenMPClauseKind PredeterminedCKind = 8645 isOpenMPSimdDirective(DKind) 8646 ? (DSAStack->hasMutipleLoops() ? OMPC_lastprivate : OMPC_linear) 8647 : OMPC_private; 8648 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && 8649 DVar.CKind != PredeterminedCKind && DVar.RefExpr && 8650 (LangOpts.OpenMP <= 45 || (DVar.CKind != OMPC_lastprivate && 8651 DVar.CKind != OMPC_private))) || 8652 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop || 8653 DKind == OMPD_master_taskloop || 8654 DKind == OMPD_parallel_master_taskloop || 8655 isOpenMPDistributeDirective(DKind)) && 8656 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && 8657 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) && 8658 (DVar.CKind != OMPC_private || DVar.RefExpr)) { 8659 Diag(Init->getBeginLoc(), diag::err_omp_loop_var_dsa) 8660 << getOpenMPClauseName(DVar.CKind) 8661 << getOpenMPDirectiveName(DKind) 8662 << getOpenMPClauseName(PredeterminedCKind); 8663 if (DVar.RefExpr == nullptr) 8664 DVar.CKind = PredeterminedCKind; 8665 reportOriginalDsa(*this, DSAStack, D, DVar, 8666 /*IsLoopIterVar=*/true); 8667 } else if (LoopDeclRefExpr) { 8668 // Make the loop iteration variable private (for worksharing 8669 // constructs), linear (for simd directives with the only one 8670 // associated loop) or lastprivate (for simd directives with several 8671 // collapsed or ordered loops). 8672 if (DVar.CKind == OMPC_unknown) 8673 DSAStack->addDSA(D, LoopDeclRefExpr, PredeterminedCKind, 8674 PrivateRef); 8675 } 8676 } 8677 } 8678 DSAStack->setAssociatedLoops(AssociatedLoops - 1); 8679 } 8680 } 8681 8682 /// Called on a for stmt to check and extract its iteration space 8683 /// for further processing (such as collapsing). 8684 static bool checkOpenMPIterationSpace( 8685 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA, 8686 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount, 8687 unsigned TotalNestedLoopCount, Expr *CollapseLoopCountExpr, 8688 Expr *OrderedLoopCountExpr, 8689 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA, 8690 llvm::MutableArrayRef<LoopIterationSpace> ResultIterSpaces, 8691 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 8692 bool SupportsNonRectangular = !isOpenMPLoopTransformationDirective(DKind); 8693 // OpenMP [2.9.1, Canonical Loop Form] 8694 // for (init-expr; test-expr; incr-expr) structured-block 8695 // for (range-decl: range-expr) structured-block 8696 if (auto *CanonLoop = dyn_cast_or_null<OMPCanonicalLoop>(S)) 8697 S = CanonLoop->getLoopStmt(); 8698 auto *For = dyn_cast_or_null<ForStmt>(S); 8699 auto *CXXFor = dyn_cast_or_null<CXXForRangeStmt>(S); 8700 // Ranged for is supported only in OpenMP 5.0. 8701 if (!For && (SemaRef.LangOpts.OpenMP <= 45 || !CXXFor)) { 8702 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_not_for) 8703 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr) 8704 << getOpenMPDirectiveName(DKind) << TotalNestedLoopCount 8705 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount; 8706 if (TotalNestedLoopCount > 1) { 8707 if (CollapseLoopCountExpr && OrderedLoopCountExpr) 8708 SemaRef.Diag(DSA.getConstructLoc(), 8709 diag::note_omp_collapse_ordered_expr) 8710 << 2 << CollapseLoopCountExpr->getSourceRange() 8711 << OrderedLoopCountExpr->getSourceRange(); 8712 else if (CollapseLoopCountExpr) 8713 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), 8714 diag::note_omp_collapse_ordered_expr) 8715 << 0 << CollapseLoopCountExpr->getSourceRange(); 8716 else 8717 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), 8718 diag::note_omp_collapse_ordered_expr) 8719 << 1 << OrderedLoopCountExpr->getSourceRange(); 8720 } 8721 return true; 8722 } 8723 assert(((For && For->getBody()) || (CXXFor && CXXFor->getBody())) && 8724 "No loop body."); 8725 8726 OpenMPIterationSpaceChecker ISC(SemaRef, SupportsNonRectangular, DSA, 8727 For ? For->getForLoc() : CXXFor->getForLoc()); 8728 8729 // Check init. 8730 Stmt *Init = For ? For->getInit() : CXXFor->getBeginStmt(); 8731 if (ISC.checkAndSetInit(Init)) 8732 return true; 8733 8734 bool HasErrors = false; 8735 8736 // Check loop variable's type. 8737 if (ValueDecl *LCDecl = ISC.getLoopDecl()) { 8738 // OpenMP [2.6, Canonical Loop Form] 8739 // Var is one of the following: 8740 // A variable of signed or unsigned integer type. 8741 // For C++, a variable of a random access iterator type. 8742 // For C, a variable of a pointer type. 8743 QualType VarType = LCDecl->getType().getNonReferenceType(); 8744 if (!VarType->isDependentType() && !VarType->isIntegerType() && 8745 !VarType->isPointerType() && 8746 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) { 8747 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_variable_type) 8748 << SemaRef.getLangOpts().CPlusPlus; 8749 HasErrors = true; 8750 } 8751 8752 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in 8753 // a Construct 8754 // The loop iteration variable(s) in the associated for-loop(s) of a for or 8755 // parallel for construct is (are) private. 8756 // The loop iteration variable in the associated for-loop of a simd 8757 // construct with just one associated for-loop is linear with a 8758 // constant-linear-step that is the increment of the associated for-loop. 8759 // Exclude loop var from the list of variables with implicitly defined data 8760 // sharing attributes. 8761 VarsWithImplicitDSA.erase(LCDecl); 8762 8763 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars"); 8764 8765 // Check test-expr. 8766 HasErrors |= ISC.checkAndSetCond(For ? For->getCond() : CXXFor->getCond()); 8767 8768 // Check incr-expr. 8769 HasErrors |= ISC.checkAndSetInc(For ? For->getInc() : CXXFor->getInc()); 8770 } 8771 8772 if (ISC.dependent() || SemaRef.CurContext->isDependentContext() || HasErrors) 8773 return HasErrors; 8774 8775 // Build the loop's iteration space representation. 8776 ResultIterSpaces[CurrentNestedLoopCount].PreCond = ISC.buildPreCond( 8777 DSA.getCurScope(), For ? For->getCond() : CXXFor->getCond(), Captures); 8778 ResultIterSpaces[CurrentNestedLoopCount].NumIterations = 8779 ISC.buildNumIterations(DSA.getCurScope(), ResultIterSpaces, 8780 (isOpenMPWorksharingDirective(DKind) || 8781 isOpenMPTaskLoopDirective(DKind) || 8782 isOpenMPDistributeDirective(DKind) || 8783 isOpenMPLoopTransformationDirective(DKind)), 8784 Captures); 8785 ResultIterSpaces[CurrentNestedLoopCount].CounterVar = 8786 ISC.buildCounterVar(Captures, DSA); 8787 ResultIterSpaces[CurrentNestedLoopCount].PrivateCounterVar = 8788 ISC.buildPrivateCounterVar(); 8789 ResultIterSpaces[CurrentNestedLoopCount].CounterInit = ISC.buildCounterInit(); 8790 ResultIterSpaces[CurrentNestedLoopCount].CounterStep = ISC.buildCounterStep(); 8791 ResultIterSpaces[CurrentNestedLoopCount].InitSrcRange = ISC.getInitSrcRange(); 8792 ResultIterSpaces[CurrentNestedLoopCount].CondSrcRange = 8793 ISC.getConditionSrcRange(); 8794 ResultIterSpaces[CurrentNestedLoopCount].IncSrcRange = 8795 ISC.getIncrementSrcRange(); 8796 ResultIterSpaces[CurrentNestedLoopCount].Subtract = ISC.shouldSubtractStep(); 8797 ResultIterSpaces[CurrentNestedLoopCount].IsStrictCompare = 8798 ISC.isStrictTestOp(); 8799 std::tie(ResultIterSpaces[CurrentNestedLoopCount].MinValue, 8800 ResultIterSpaces[CurrentNestedLoopCount].MaxValue) = 8801 ISC.buildMinMaxValues(DSA.getCurScope(), Captures); 8802 ResultIterSpaces[CurrentNestedLoopCount].FinalCondition = 8803 ISC.buildFinalCondition(DSA.getCurScope()); 8804 ResultIterSpaces[CurrentNestedLoopCount].IsNonRectangularLB = 8805 ISC.doesInitDependOnLC(); 8806 ResultIterSpaces[CurrentNestedLoopCount].IsNonRectangularUB = 8807 ISC.doesCondDependOnLC(); 8808 ResultIterSpaces[CurrentNestedLoopCount].LoopDependentIdx = 8809 ISC.getLoopDependentIdx(); 8810 8811 HasErrors |= 8812 (ResultIterSpaces[CurrentNestedLoopCount].PreCond == nullptr || 8813 ResultIterSpaces[CurrentNestedLoopCount].NumIterations == nullptr || 8814 ResultIterSpaces[CurrentNestedLoopCount].CounterVar == nullptr || 8815 ResultIterSpaces[CurrentNestedLoopCount].PrivateCounterVar == nullptr || 8816 ResultIterSpaces[CurrentNestedLoopCount].CounterInit == nullptr || 8817 ResultIterSpaces[CurrentNestedLoopCount].CounterStep == nullptr); 8818 if (!HasErrors && DSA.isOrderedRegion()) { 8819 if (DSA.getOrderedRegionParam().second->getNumForLoops()) { 8820 if (CurrentNestedLoopCount < 8821 DSA.getOrderedRegionParam().second->getLoopNumIterations().size()) { 8822 DSA.getOrderedRegionParam().second->setLoopNumIterations( 8823 CurrentNestedLoopCount, 8824 ResultIterSpaces[CurrentNestedLoopCount].NumIterations); 8825 DSA.getOrderedRegionParam().second->setLoopCounter( 8826 CurrentNestedLoopCount, 8827 ResultIterSpaces[CurrentNestedLoopCount].CounterVar); 8828 } 8829 } 8830 for (auto &Pair : DSA.getDoacrossDependClauses()) { 8831 if (CurrentNestedLoopCount >= Pair.first->getNumLoops()) { 8832 // Erroneous case - clause has some problems. 8833 continue; 8834 } 8835 if (Pair.first->getDependencyKind() == OMPC_DEPEND_sink && 8836 Pair.second.size() <= CurrentNestedLoopCount) { 8837 // Erroneous case - clause has some problems. 8838 Pair.first->setLoopData(CurrentNestedLoopCount, nullptr); 8839 continue; 8840 } 8841 Expr *CntValue; 8842 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source) 8843 CntValue = ISC.buildOrderedLoopData( 8844 DSA.getCurScope(), 8845 ResultIterSpaces[CurrentNestedLoopCount].CounterVar, Captures, 8846 Pair.first->getDependencyLoc()); 8847 else 8848 CntValue = ISC.buildOrderedLoopData( 8849 DSA.getCurScope(), 8850 ResultIterSpaces[CurrentNestedLoopCount].CounterVar, Captures, 8851 Pair.first->getDependencyLoc(), 8852 Pair.second[CurrentNestedLoopCount].first, 8853 Pair.second[CurrentNestedLoopCount].second); 8854 Pair.first->setLoopData(CurrentNestedLoopCount, CntValue); 8855 } 8856 } 8857 8858 return HasErrors; 8859 } 8860 8861 /// Build 'VarRef = Start. 8862 static ExprResult 8863 buildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef, 8864 ExprResult Start, bool IsNonRectangularLB, 8865 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 8866 // Build 'VarRef = Start. 8867 ExprResult NewStart = IsNonRectangularLB 8868 ? Start.get() 8869 : tryBuildCapture(SemaRef, Start.get(), Captures); 8870 if (!NewStart.isUsable()) 8871 return ExprError(); 8872 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(), 8873 VarRef.get()->getType())) { 8874 NewStart = SemaRef.PerformImplicitConversion( 8875 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting, 8876 /*AllowExplicit=*/true); 8877 if (!NewStart.isUsable()) 8878 return ExprError(); 8879 } 8880 8881 ExprResult Init = 8882 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); 8883 return Init; 8884 } 8885 8886 /// Build 'VarRef = Start + Iter * Step'. 8887 static ExprResult buildCounterUpdate( 8888 Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef, 8889 ExprResult Start, ExprResult Iter, ExprResult Step, bool Subtract, 8890 bool IsNonRectangularLB, 8891 llvm::MapVector<const Expr *, DeclRefExpr *> *Captures = nullptr) { 8892 // Add parentheses (for debugging purposes only). 8893 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get()); 8894 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() || 8895 !Step.isUsable()) 8896 return ExprError(); 8897 8898 ExprResult NewStep = Step; 8899 if (Captures) 8900 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures); 8901 if (NewStep.isInvalid()) 8902 return ExprError(); 8903 ExprResult Update = 8904 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get()); 8905 if (!Update.isUsable()) 8906 return ExprError(); 8907 8908 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or 8909 // 'VarRef = Start (+|-) Iter * Step'. 8910 if (!Start.isUsable()) 8911 return ExprError(); 8912 ExprResult NewStart = SemaRef.ActOnParenExpr(Loc, Loc, Start.get()); 8913 if (!NewStart.isUsable()) 8914 return ExprError(); 8915 if (Captures && !IsNonRectangularLB) 8916 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures); 8917 if (NewStart.isInvalid()) 8918 return ExprError(); 8919 8920 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'. 8921 ExprResult SavedUpdate = Update; 8922 ExprResult UpdateVal; 8923 if (VarRef.get()->getType()->isOverloadableType() || 8924 NewStart.get()->getType()->isOverloadableType() || 8925 Update.get()->getType()->isOverloadableType()) { 8926 Sema::TentativeAnalysisScope Trap(SemaRef); 8927 8928 Update = 8929 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); 8930 if (Update.isUsable()) { 8931 UpdateVal = 8932 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign, 8933 VarRef.get(), SavedUpdate.get()); 8934 if (UpdateVal.isUsable()) { 8935 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(), 8936 UpdateVal.get()); 8937 } 8938 } 8939 } 8940 8941 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'. 8942 if (!Update.isUsable() || !UpdateVal.isUsable()) { 8943 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add, 8944 NewStart.get(), SavedUpdate.get()); 8945 if (!Update.isUsable()) 8946 return ExprError(); 8947 8948 if (!SemaRef.Context.hasSameType(Update.get()->getType(), 8949 VarRef.get()->getType())) { 8950 Update = SemaRef.PerformImplicitConversion( 8951 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true); 8952 if (!Update.isUsable()) 8953 return ExprError(); 8954 } 8955 8956 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get()); 8957 } 8958 return Update; 8959 } 8960 8961 /// Convert integer expression \a E to make it have at least \a Bits 8962 /// bits. 8963 static ExprResult widenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) { 8964 if (E == nullptr) 8965 return ExprError(); 8966 ASTContext &C = SemaRef.Context; 8967 QualType OldType = E->getType(); 8968 unsigned HasBits = C.getTypeSize(OldType); 8969 if (HasBits >= Bits) 8970 return ExprResult(E); 8971 // OK to convert to signed, because new type has more bits than old. 8972 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true); 8973 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting, 8974 true); 8975 } 8976 8977 /// Check if the given expression \a E is a constant integer that fits 8978 /// into \a Bits bits. 8979 static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) { 8980 if (E == nullptr) 8981 return false; 8982 if (Optional<llvm::APSInt> Result = 8983 E->getIntegerConstantExpr(SemaRef.Context)) 8984 return Signed ? Result->isSignedIntN(Bits) : Result->isIntN(Bits); 8985 return false; 8986 } 8987 8988 /// Build preinits statement for the given declarations. 8989 static Stmt *buildPreInits(ASTContext &Context, 8990 MutableArrayRef<Decl *> PreInits) { 8991 if (!PreInits.empty()) { 8992 return new (Context) DeclStmt( 8993 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()), 8994 SourceLocation(), SourceLocation()); 8995 } 8996 return nullptr; 8997 } 8998 8999 /// Build preinits statement for the given declarations. 9000 static Stmt * 9001 buildPreInits(ASTContext &Context, 9002 const llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 9003 if (!Captures.empty()) { 9004 SmallVector<Decl *, 16> PreInits; 9005 for (const auto &Pair : Captures) 9006 PreInits.push_back(Pair.second->getDecl()); 9007 return buildPreInits(Context, PreInits); 9008 } 9009 return nullptr; 9010 } 9011 9012 /// Build postupdate expression for the given list of postupdates expressions. 9013 static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) { 9014 Expr *PostUpdate = nullptr; 9015 if (!PostUpdates.empty()) { 9016 for (Expr *E : PostUpdates) { 9017 Expr *ConvE = S.BuildCStyleCastExpr( 9018 E->getExprLoc(), 9019 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy), 9020 E->getExprLoc(), E) 9021 .get(); 9022 PostUpdate = PostUpdate 9023 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma, 9024 PostUpdate, ConvE) 9025 .get() 9026 : ConvE; 9027 } 9028 } 9029 return PostUpdate; 9030 } 9031 9032 /// Called on a for stmt to check itself and nested loops (if any). 9033 /// \return Returns 0 if one of the collapsed stmts is not canonical for loop, 9034 /// number of collapsed loops otherwise. 9035 static unsigned 9036 checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr, 9037 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef, 9038 DSAStackTy &DSA, 9039 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA, 9040 OMPLoopBasedDirective::HelperExprs &Built) { 9041 unsigned NestedLoopCount = 1; 9042 bool SupportsNonPerfectlyNested = (SemaRef.LangOpts.OpenMP >= 50) && 9043 !isOpenMPLoopTransformationDirective(DKind); 9044 9045 if (CollapseLoopCountExpr) { 9046 // Found 'collapse' clause - calculate collapse number. 9047 Expr::EvalResult Result; 9048 if (!CollapseLoopCountExpr->isValueDependent() && 9049 CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) { 9050 NestedLoopCount = Result.Val.getInt().getLimitedValue(); 9051 } else { 9052 Built.clear(/*Size=*/1); 9053 return 1; 9054 } 9055 } 9056 unsigned OrderedLoopCount = 1; 9057 if (OrderedLoopCountExpr) { 9058 // Found 'ordered' clause - calculate collapse number. 9059 Expr::EvalResult EVResult; 9060 if (!OrderedLoopCountExpr->isValueDependent() && 9061 OrderedLoopCountExpr->EvaluateAsInt(EVResult, 9062 SemaRef.getASTContext())) { 9063 llvm::APSInt Result = EVResult.Val.getInt(); 9064 if (Result.getLimitedValue() < NestedLoopCount) { 9065 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), 9066 diag::err_omp_wrong_ordered_loop_count) 9067 << OrderedLoopCountExpr->getSourceRange(); 9068 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), 9069 diag::note_collapse_loop_count) 9070 << CollapseLoopCountExpr->getSourceRange(); 9071 } 9072 OrderedLoopCount = Result.getLimitedValue(); 9073 } else { 9074 Built.clear(/*Size=*/1); 9075 return 1; 9076 } 9077 } 9078 // This is helper routine for loop directives (e.g., 'for', 'simd', 9079 // 'for simd', etc.). 9080 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 9081 unsigned NumLoops = std::max(OrderedLoopCount, NestedLoopCount); 9082 SmallVector<LoopIterationSpace, 4> IterSpaces(NumLoops); 9083 if (!OMPLoopBasedDirective::doForAllLoops( 9084 AStmt->IgnoreContainers(!isOpenMPLoopTransformationDirective(DKind)), 9085 SupportsNonPerfectlyNested, NumLoops, 9086 [DKind, &SemaRef, &DSA, NumLoops, NestedLoopCount, 9087 CollapseLoopCountExpr, OrderedLoopCountExpr, &VarsWithImplicitDSA, 9088 &IterSpaces, &Captures](unsigned Cnt, Stmt *CurStmt) { 9089 if (checkOpenMPIterationSpace( 9090 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount, 9091 NumLoops, CollapseLoopCountExpr, OrderedLoopCountExpr, 9092 VarsWithImplicitDSA, IterSpaces, Captures)) 9093 return true; 9094 if (Cnt > 0 && Cnt >= NestedLoopCount && 9095 IterSpaces[Cnt].CounterVar) { 9096 // Handle initialization of captured loop iterator variables. 9097 auto *DRE = cast<DeclRefExpr>(IterSpaces[Cnt].CounterVar); 9098 if (isa<OMPCapturedExprDecl>(DRE->getDecl())) { 9099 Captures[DRE] = DRE; 9100 } 9101 } 9102 return false; 9103 }, 9104 [&SemaRef, &Captures](OMPLoopTransformationDirective *Transform) { 9105 Stmt *DependentPreInits = Transform->getPreInits(); 9106 if (!DependentPreInits) 9107 return; 9108 for (Decl *C : cast<DeclStmt>(DependentPreInits)->getDeclGroup()) { 9109 auto *D = cast<VarDecl>(C); 9110 DeclRefExpr *Ref = buildDeclRefExpr(SemaRef, D, D->getType(), 9111 Transform->getBeginLoc()); 9112 Captures[Ref] = Ref; 9113 } 9114 })) 9115 return 0; 9116 9117 Built.clear(/* size */ NestedLoopCount); 9118 9119 if (SemaRef.CurContext->isDependentContext()) 9120 return NestedLoopCount; 9121 9122 // An example of what is generated for the following code: 9123 // 9124 // #pragma omp simd collapse(2) ordered(2) 9125 // for (i = 0; i < NI; ++i) 9126 // for (k = 0; k < NK; ++k) 9127 // for (j = J0; j < NJ; j+=2) { 9128 // <loop body> 9129 // } 9130 // 9131 // We generate the code below. 9132 // Note: the loop body may be outlined in CodeGen. 9133 // Note: some counters may be C++ classes, operator- is used to find number of 9134 // iterations and operator+= to calculate counter value. 9135 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32 9136 // or i64 is currently supported). 9137 // 9138 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2)) 9139 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) { 9140 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2); 9141 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2; 9142 // // similar updates for vars in clauses (e.g. 'linear') 9143 // <loop body (using local i and j)> 9144 // } 9145 // i = NI; // assign final values of counters 9146 // j = NJ; 9147 // 9148 9149 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are 9150 // the iteration counts of the collapsed for loops. 9151 // Precondition tests if there is at least one iteration (all conditions are 9152 // true). 9153 auto PreCond = ExprResult(IterSpaces[0].PreCond); 9154 Expr *N0 = IterSpaces[0].NumIterations; 9155 ExprResult LastIteration32 = 9156 widenIterationCount(/*Bits=*/32, 9157 SemaRef 9158 .PerformImplicitConversion( 9159 N0->IgnoreImpCasts(), N0->getType(), 9160 Sema::AA_Converting, /*AllowExplicit=*/true) 9161 .get(), 9162 SemaRef); 9163 ExprResult LastIteration64 = widenIterationCount( 9164 /*Bits=*/64, 9165 SemaRef 9166 .PerformImplicitConversion(N0->IgnoreImpCasts(), N0->getType(), 9167 Sema::AA_Converting, 9168 /*AllowExplicit=*/true) 9169 .get(), 9170 SemaRef); 9171 9172 if (!LastIteration32.isUsable() || !LastIteration64.isUsable()) 9173 return NestedLoopCount; 9174 9175 ASTContext &C = SemaRef.Context; 9176 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32; 9177 9178 Scope *CurScope = DSA.getCurScope(); 9179 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) { 9180 if (PreCond.isUsable()) { 9181 PreCond = 9182 SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd, 9183 PreCond.get(), IterSpaces[Cnt].PreCond); 9184 } 9185 Expr *N = IterSpaces[Cnt].NumIterations; 9186 SourceLocation Loc = N->getExprLoc(); 9187 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32; 9188 if (LastIteration32.isUsable()) 9189 LastIteration32 = SemaRef.BuildBinOp( 9190 CurScope, Loc, BO_Mul, LastIteration32.get(), 9191 SemaRef 9192 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), 9193 Sema::AA_Converting, 9194 /*AllowExplicit=*/true) 9195 .get()); 9196 if (LastIteration64.isUsable()) 9197 LastIteration64 = SemaRef.BuildBinOp( 9198 CurScope, Loc, BO_Mul, LastIteration64.get(), 9199 SemaRef 9200 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), 9201 Sema::AA_Converting, 9202 /*AllowExplicit=*/true) 9203 .get()); 9204 } 9205 9206 // Choose either the 32-bit or 64-bit version. 9207 ExprResult LastIteration = LastIteration64; 9208 if (SemaRef.getLangOpts().OpenMPOptimisticCollapse || 9209 (LastIteration32.isUsable() && 9210 C.getTypeSize(LastIteration32.get()->getType()) == 32 && 9211 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 || 9212 fitsInto( 9213 /*Bits=*/32, 9214 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(), 9215 LastIteration64.get(), SemaRef)))) 9216 LastIteration = LastIteration32; 9217 QualType VType = LastIteration.get()->getType(); 9218 QualType RealVType = VType; 9219 QualType StrideVType = VType; 9220 if (isOpenMPTaskLoopDirective(DKind)) { 9221 VType = 9222 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0); 9223 StrideVType = 9224 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1); 9225 } 9226 9227 if (!LastIteration.isUsable()) 9228 return 0; 9229 9230 // Save the number of iterations. 9231 ExprResult NumIterations = LastIteration; 9232 { 9233 LastIteration = SemaRef.BuildBinOp( 9234 CurScope, LastIteration.get()->getExprLoc(), BO_Sub, 9235 LastIteration.get(), 9236 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 9237 if (!LastIteration.isUsable()) 9238 return 0; 9239 } 9240 9241 // Calculate the last iteration number beforehand instead of doing this on 9242 // each iteration. Do not do this if the number of iterations may be kfold-ed. 9243 bool IsConstant = LastIteration.get()->isIntegerConstantExpr(SemaRef.Context); 9244 ExprResult CalcLastIteration; 9245 if (!IsConstant) { 9246 ExprResult SaveRef = 9247 tryBuildCapture(SemaRef, LastIteration.get(), Captures); 9248 LastIteration = SaveRef; 9249 9250 // Prepare SaveRef + 1. 9251 NumIterations = SemaRef.BuildBinOp( 9252 CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(), 9253 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 9254 if (!NumIterations.isUsable()) 9255 return 0; 9256 } 9257 9258 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin(); 9259 9260 // Build variables passed into runtime, necessary for worksharing directives. 9261 ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB; 9262 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || 9263 isOpenMPDistributeDirective(DKind) || 9264 isOpenMPLoopTransformationDirective(DKind)) { 9265 // Lower bound variable, initialized with zero. 9266 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb"); 9267 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc); 9268 SemaRef.AddInitializerToDecl(LBDecl, 9269 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 9270 /*DirectInit*/ false); 9271 9272 // Upper bound variable, initialized with last iteration number. 9273 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub"); 9274 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc); 9275 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(), 9276 /*DirectInit*/ false); 9277 9278 // A 32-bit variable-flag where runtime returns 1 for the last iteration. 9279 // This will be used to implement clause 'lastprivate'. 9280 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true); 9281 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last"); 9282 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc); 9283 SemaRef.AddInitializerToDecl(ILDecl, 9284 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 9285 /*DirectInit*/ false); 9286 9287 // Stride variable returned by runtime (we initialize it to 1 by default). 9288 VarDecl *STDecl = 9289 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride"); 9290 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc); 9291 SemaRef.AddInitializerToDecl(STDecl, 9292 SemaRef.ActOnIntegerConstant(InitLoc, 1).get(), 9293 /*DirectInit*/ false); 9294 9295 // Build expression: UB = min(UB, LastIteration) 9296 // It is necessary for CodeGen of directives with static scheduling. 9297 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT, 9298 UB.get(), LastIteration.get()); 9299 ExprResult CondOp = SemaRef.ActOnConditionalOp( 9300 LastIteration.get()->getExprLoc(), InitLoc, IsUBGreater.get(), 9301 LastIteration.get(), UB.get()); 9302 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(), 9303 CondOp.get()); 9304 EUB = SemaRef.ActOnFinishFullExpr(EUB.get(), /*DiscardedValue*/ false); 9305 9306 // If we have a combined directive that combines 'distribute', 'for' or 9307 // 'simd' we need to be able to access the bounds of the schedule of the 9308 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained 9309 // by scheduling 'distribute' have to be passed to the schedule of 'for'. 9310 if (isOpenMPLoopBoundSharingDirective(DKind)) { 9311 // Lower bound variable, initialized with zero. 9312 VarDecl *CombLBDecl = 9313 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb"); 9314 CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc); 9315 SemaRef.AddInitializerToDecl( 9316 CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 9317 /*DirectInit*/ false); 9318 9319 // Upper bound variable, initialized with last iteration number. 9320 VarDecl *CombUBDecl = 9321 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub"); 9322 CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc); 9323 SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(), 9324 /*DirectInit*/ false); 9325 9326 ExprResult CombIsUBGreater = SemaRef.BuildBinOp( 9327 CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get()); 9328 ExprResult CombCondOp = 9329 SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(), 9330 LastIteration.get(), CombUB.get()); 9331 CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(), 9332 CombCondOp.get()); 9333 CombEUB = 9334 SemaRef.ActOnFinishFullExpr(CombEUB.get(), /*DiscardedValue*/ false); 9335 9336 const CapturedDecl *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl(); 9337 // We expect to have at least 2 more parameters than the 'parallel' 9338 // directive does - the lower and upper bounds of the previous schedule. 9339 assert(CD->getNumParams() >= 4 && 9340 "Unexpected number of parameters in loop combined directive"); 9341 9342 // Set the proper type for the bounds given what we learned from the 9343 // enclosed loops. 9344 ImplicitParamDecl *PrevLBDecl = CD->getParam(/*PrevLB=*/2); 9345 ImplicitParamDecl *PrevUBDecl = CD->getParam(/*PrevUB=*/3); 9346 9347 // Previous lower and upper bounds are obtained from the region 9348 // parameters. 9349 PrevLB = 9350 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc); 9351 PrevUB = 9352 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc); 9353 } 9354 } 9355 9356 // Build the iteration variable and its initialization before loop. 9357 ExprResult IV; 9358 ExprResult Init, CombInit; 9359 { 9360 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv"); 9361 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc); 9362 Expr *RHS = (isOpenMPWorksharingDirective(DKind) || 9363 isOpenMPTaskLoopDirective(DKind) || 9364 isOpenMPDistributeDirective(DKind) || 9365 isOpenMPLoopTransformationDirective(DKind)) 9366 ? LB.get() 9367 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get(); 9368 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS); 9369 Init = SemaRef.ActOnFinishFullExpr(Init.get(), /*DiscardedValue*/ false); 9370 9371 if (isOpenMPLoopBoundSharingDirective(DKind)) { 9372 Expr *CombRHS = 9373 (isOpenMPWorksharingDirective(DKind) || 9374 isOpenMPTaskLoopDirective(DKind) || 9375 isOpenMPDistributeDirective(DKind)) 9376 ? CombLB.get() 9377 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get(); 9378 CombInit = 9379 SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS); 9380 CombInit = 9381 SemaRef.ActOnFinishFullExpr(CombInit.get(), /*DiscardedValue*/ false); 9382 } 9383 } 9384 9385 bool UseStrictCompare = 9386 RealVType->hasUnsignedIntegerRepresentation() && 9387 llvm::all_of(IterSpaces, [](const LoopIterationSpace &LIS) { 9388 return LIS.IsStrictCompare; 9389 }); 9390 // Loop condition (IV < NumIterations) or (IV <= UB or IV < UB + 1 (for 9391 // unsigned IV)) for worksharing loops. 9392 SourceLocation CondLoc = AStmt->getBeginLoc(); 9393 Expr *BoundUB = UB.get(); 9394 if (UseStrictCompare) { 9395 BoundUB = 9396 SemaRef 9397 .BuildBinOp(CurScope, CondLoc, BO_Add, BoundUB, 9398 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()) 9399 .get(); 9400 BoundUB = 9401 SemaRef.ActOnFinishFullExpr(BoundUB, /*DiscardedValue*/ false).get(); 9402 } 9403 ExprResult Cond = 9404 (isOpenMPWorksharingDirective(DKind) || 9405 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind) || 9406 isOpenMPLoopTransformationDirective(DKind)) 9407 ? SemaRef.BuildBinOp(CurScope, CondLoc, 9408 UseStrictCompare ? BO_LT : BO_LE, IV.get(), 9409 BoundUB) 9410 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(), 9411 NumIterations.get()); 9412 ExprResult CombDistCond; 9413 if (isOpenMPLoopBoundSharingDirective(DKind)) { 9414 CombDistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(), 9415 NumIterations.get()); 9416 } 9417 9418 ExprResult CombCond; 9419 if (isOpenMPLoopBoundSharingDirective(DKind)) { 9420 Expr *BoundCombUB = CombUB.get(); 9421 if (UseStrictCompare) { 9422 BoundCombUB = 9423 SemaRef 9424 .BuildBinOp( 9425 CurScope, CondLoc, BO_Add, BoundCombUB, 9426 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()) 9427 .get(); 9428 BoundCombUB = 9429 SemaRef.ActOnFinishFullExpr(BoundCombUB, /*DiscardedValue*/ false) 9430 .get(); 9431 } 9432 CombCond = 9433 SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, 9434 IV.get(), BoundCombUB); 9435 } 9436 // Loop increment (IV = IV + 1) 9437 SourceLocation IncLoc = AStmt->getBeginLoc(); 9438 ExprResult Inc = 9439 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(), 9440 SemaRef.ActOnIntegerConstant(IncLoc, 1).get()); 9441 if (!Inc.isUsable()) 9442 return 0; 9443 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get()); 9444 Inc = SemaRef.ActOnFinishFullExpr(Inc.get(), /*DiscardedValue*/ false); 9445 if (!Inc.isUsable()) 9446 return 0; 9447 9448 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST). 9449 // Used for directives with static scheduling. 9450 // In combined construct, add combined version that use CombLB and CombUB 9451 // base variables for the update 9452 ExprResult NextLB, NextUB, CombNextLB, CombNextUB; 9453 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || 9454 isOpenMPDistributeDirective(DKind) || 9455 isOpenMPLoopTransformationDirective(DKind)) { 9456 // LB + ST 9457 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get()); 9458 if (!NextLB.isUsable()) 9459 return 0; 9460 // LB = LB + ST 9461 NextLB = 9462 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get()); 9463 NextLB = 9464 SemaRef.ActOnFinishFullExpr(NextLB.get(), /*DiscardedValue*/ false); 9465 if (!NextLB.isUsable()) 9466 return 0; 9467 // UB + ST 9468 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get()); 9469 if (!NextUB.isUsable()) 9470 return 0; 9471 // UB = UB + ST 9472 NextUB = 9473 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get()); 9474 NextUB = 9475 SemaRef.ActOnFinishFullExpr(NextUB.get(), /*DiscardedValue*/ false); 9476 if (!NextUB.isUsable()) 9477 return 0; 9478 if (isOpenMPLoopBoundSharingDirective(DKind)) { 9479 CombNextLB = 9480 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get()); 9481 if (!NextLB.isUsable()) 9482 return 0; 9483 // LB = LB + ST 9484 CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(), 9485 CombNextLB.get()); 9486 CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get(), 9487 /*DiscardedValue*/ false); 9488 if (!CombNextLB.isUsable()) 9489 return 0; 9490 // UB + ST 9491 CombNextUB = 9492 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get()); 9493 if (!CombNextUB.isUsable()) 9494 return 0; 9495 // UB = UB + ST 9496 CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(), 9497 CombNextUB.get()); 9498 CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get(), 9499 /*DiscardedValue*/ false); 9500 if (!CombNextUB.isUsable()) 9501 return 0; 9502 } 9503 } 9504 9505 // Create increment expression for distribute loop when combined in a same 9506 // directive with for as IV = IV + ST; ensure upper bound expression based 9507 // on PrevUB instead of NumIterations - used to implement 'for' when found 9508 // in combination with 'distribute', like in 'distribute parallel for' 9509 SourceLocation DistIncLoc = AStmt->getBeginLoc(); 9510 ExprResult DistCond, DistInc, PrevEUB, ParForInDistCond; 9511 if (isOpenMPLoopBoundSharingDirective(DKind)) { 9512 DistCond = SemaRef.BuildBinOp( 9513 CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, IV.get(), BoundUB); 9514 assert(DistCond.isUsable() && "distribute cond expr was not built"); 9515 9516 DistInc = 9517 SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get()); 9518 assert(DistInc.isUsable() && "distribute inc expr was not built"); 9519 DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(), 9520 DistInc.get()); 9521 DistInc = 9522 SemaRef.ActOnFinishFullExpr(DistInc.get(), /*DiscardedValue*/ false); 9523 assert(DistInc.isUsable() && "distribute inc expr was not built"); 9524 9525 // Build expression: UB = min(UB, prevUB) for #for in composite or combined 9526 // construct 9527 ExprResult NewPrevUB = PrevUB; 9528 SourceLocation DistEUBLoc = AStmt->getBeginLoc(); 9529 if (!SemaRef.Context.hasSameType(UB.get()->getType(), 9530 PrevUB.get()->getType())) { 9531 NewPrevUB = SemaRef.BuildCStyleCastExpr( 9532 DistEUBLoc, 9533 SemaRef.Context.getTrivialTypeSourceInfo(UB.get()->getType()), 9534 DistEUBLoc, NewPrevUB.get()); 9535 if (!NewPrevUB.isUsable()) 9536 return 0; 9537 } 9538 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, 9539 UB.get(), NewPrevUB.get()); 9540 ExprResult CondOp = SemaRef.ActOnConditionalOp( 9541 DistEUBLoc, DistEUBLoc, IsUBGreater.get(), NewPrevUB.get(), UB.get()); 9542 PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(), 9543 CondOp.get()); 9544 PrevEUB = 9545 SemaRef.ActOnFinishFullExpr(PrevEUB.get(), /*DiscardedValue*/ false); 9546 9547 // Build IV <= PrevUB or IV < PrevUB + 1 for unsigned IV to be used in 9548 // parallel for is in combination with a distribute directive with 9549 // schedule(static, 1) 9550 Expr *BoundPrevUB = PrevUB.get(); 9551 if (UseStrictCompare) { 9552 BoundPrevUB = 9553 SemaRef 9554 .BuildBinOp( 9555 CurScope, CondLoc, BO_Add, BoundPrevUB, 9556 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()) 9557 .get(); 9558 BoundPrevUB = 9559 SemaRef.ActOnFinishFullExpr(BoundPrevUB, /*DiscardedValue*/ false) 9560 .get(); 9561 } 9562 ParForInDistCond = 9563 SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, 9564 IV.get(), BoundPrevUB); 9565 } 9566 9567 // Build updates and final values of the loop counters. 9568 bool HasErrors = false; 9569 Built.Counters.resize(NestedLoopCount); 9570 Built.Inits.resize(NestedLoopCount); 9571 Built.Updates.resize(NestedLoopCount); 9572 Built.Finals.resize(NestedLoopCount); 9573 Built.DependentCounters.resize(NestedLoopCount); 9574 Built.DependentInits.resize(NestedLoopCount); 9575 Built.FinalsConditions.resize(NestedLoopCount); 9576 { 9577 // We implement the following algorithm for obtaining the 9578 // original loop iteration variable values based on the 9579 // value of the collapsed loop iteration variable IV. 9580 // 9581 // Let n+1 be the number of collapsed loops in the nest. 9582 // Iteration variables (I0, I1, .... In) 9583 // Iteration counts (N0, N1, ... Nn) 9584 // 9585 // Acc = IV; 9586 // 9587 // To compute Ik for loop k, 0 <= k <= n, generate: 9588 // Prod = N(k+1) * N(k+2) * ... * Nn; 9589 // Ik = Acc / Prod; 9590 // Acc -= Ik * Prod; 9591 // 9592 ExprResult Acc = IV; 9593 for (unsigned int Cnt = 0; Cnt < NestedLoopCount; ++Cnt) { 9594 LoopIterationSpace &IS = IterSpaces[Cnt]; 9595 SourceLocation UpdLoc = IS.IncSrcRange.getBegin(); 9596 ExprResult Iter; 9597 9598 // Compute prod 9599 ExprResult Prod = 9600 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(); 9601 for (unsigned int K = Cnt+1; K < NestedLoopCount; ++K) 9602 Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Prod.get(), 9603 IterSpaces[K].NumIterations); 9604 9605 // Iter = Acc / Prod 9606 // If there is at least one more inner loop to avoid 9607 // multiplication by 1. 9608 if (Cnt + 1 < NestedLoopCount) 9609 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, 9610 Acc.get(), Prod.get()); 9611 else 9612 Iter = Acc; 9613 if (!Iter.isUsable()) { 9614 HasErrors = true; 9615 break; 9616 } 9617 9618 // Update Acc: 9619 // Acc -= Iter * Prod 9620 // Check if there is at least one more inner loop to avoid 9621 // multiplication by 1. 9622 if (Cnt + 1 < NestedLoopCount) 9623 Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, 9624 Iter.get(), Prod.get()); 9625 else 9626 Prod = Iter; 9627 Acc = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Sub, 9628 Acc.get(), Prod.get()); 9629 9630 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step 9631 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()); 9632 DeclRefExpr *CounterVar = buildDeclRefExpr( 9633 SemaRef, VD, IS.CounterVar->getType(), IS.CounterVar->getExprLoc(), 9634 /*RefersToCapture=*/true); 9635 ExprResult Init = 9636 buildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar, 9637 IS.CounterInit, IS.IsNonRectangularLB, Captures); 9638 if (!Init.isUsable()) { 9639 HasErrors = true; 9640 break; 9641 } 9642 ExprResult Update = buildCounterUpdate( 9643 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter, 9644 IS.CounterStep, IS.Subtract, IS.IsNonRectangularLB, &Captures); 9645 if (!Update.isUsable()) { 9646 HasErrors = true; 9647 break; 9648 } 9649 9650 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step 9651 ExprResult Final = 9652 buildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar, 9653 IS.CounterInit, IS.NumIterations, IS.CounterStep, 9654 IS.Subtract, IS.IsNonRectangularLB, &Captures); 9655 if (!Final.isUsable()) { 9656 HasErrors = true; 9657 break; 9658 } 9659 9660 if (!Update.isUsable() || !Final.isUsable()) { 9661 HasErrors = true; 9662 break; 9663 } 9664 // Save results 9665 Built.Counters[Cnt] = IS.CounterVar; 9666 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar; 9667 Built.Inits[Cnt] = Init.get(); 9668 Built.Updates[Cnt] = Update.get(); 9669 Built.Finals[Cnt] = Final.get(); 9670 Built.DependentCounters[Cnt] = nullptr; 9671 Built.DependentInits[Cnt] = nullptr; 9672 Built.FinalsConditions[Cnt] = nullptr; 9673 if (IS.IsNonRectangularLB || IS.IsNonRectangularUB) { 9674 Built.DependentCounters[Cnt] = 9675 Built.Counters[NestedLoopCount - 1 - IS.LoopDependentIdx]; 9676 Built.DependentInits[Cnt] = 9677 Built.Inits[NestedLoopCount - 1 - IS.LoopDependentIdx]; 9678 Built.FinalsConditions[Cnt] = IS.FinalCondition; 9679 } 9680 } 9681 } 9682 9683 if (HasErrors) 9684 return 0; 9685 9686 // Save results 9687 Built.IterationVarRef = IV.get(); 9688 Built.LastIteration = LastIteration.get(); 9689 Built.NumIterations = NumIterations.get(); 9690 Built.CalcLastIteration = SemaRef 9691 .ActOnFinishFullExpr(CalcLastIteration.get(), 9692 /*DiscardedValue=*/false) 9693 .get(); 9694 Built.PreCond = PreCond.get(); 9695 Built.PreInits = buildPreInits(C, Captures); 9696 Built.Cond = Cond.get(); 9697 Built.Init = Init.get(); 9698 Built.Inc = Inc.get(); 9699 Built.LB = LB.get(); 9700 Built.UB = UB.get(); 9701 Built.IL = IL.get(); 9702 Built.ST = ST.get(); 9703 Built.EUB = EUB.get(); 9704 Built.NLB = NextLB.get(); 9705 Built.NUB = NextUB.get(); 9706 Built.PrevLB = PrevLB.get(); 9707 Built.PrevUB = PrevUB.get(); 9708 Built.DistInc = DistInc.get(); 9709 Built.PrevEUB = PrevEUB.get(); 9710 Built.DistCombinedFields.LB = CombLB.get(); 9711 Built.DistCombinedFields.UB = CombUB.get(); 9712 Built.DistCombinedFields.EUB = CombEUB.get(); 9713 Built.DistCombinedFields.Init = CombInit.get(); 9714 Built.DistCombinedFields.Cond = CombCond.get(); 9715 Built.DistCombinedFields.NLB = CombNextLB.get(); 9716 Built.DistCombinedFields.NUB = CombNextUB.get(); 9717 Built.DistCombinedFields.DistCond = CombDistCond.get(); 9718 Built.DistCombinedFields.ParForInDistCond = ParForInDistCond.get(); 9719 9720 return NestedLoopCount; 9721 } 9722 9723 static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) { 9724 auto CollapseClauses = 9725 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses); 9726 if (CollapseClauses.begin() != CollapseClauses.end()) 9727 return (*CollapseClauses.begin())->getNumForLoops(); 9728 return nullptr; 9729 } 9730 9731 static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) { 9732 auto OrderedClauses = 9733 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses); 9734 if (OrderedClauses.begin() != OrderedClauses.end()) 9735 return (*OrderedClauses.begin())->getNumForLoops(); 9736 return nullptr; 9737 } 9738 9739 static bool checkSimdlenSafelenSpecified(Sema &S, 9740 const ArrayRef<OMPClause *> Clauses) { 9741 const OMPSafelenClause *Safelen = nullptr; 9742 const OMPSimdlenClause *Simdlen = nullptr; 9743 9744 for (const OMPClause *Clause : Clauses) { 9745 if (Clause->getClauseKind() == OMPC_safelen) 9746 Safelen = cast<OMPSafelenClause>(Clause); 9747 else if (Clause->getClauseKind() == OMPC_simdlen) 9748 Simdlen = cast<OMPSimdlenClause>(Clause); 9749 if (Safelen && Simdlen) 9750 break; 9751 } 9752 9753 if (Simdlen && Safelen) { 9754 const Expr *SimdlenLength = Simdlen->getSimdlen(); 9755 const Expr *SafelenLength = Safelen->getSafelen(); 9756 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() || 9757 SimdlenLength->isInstantiationDependent() || 9758 SimdlenLength->containsUnexpandedParameterPack()) 9759 return false; 9760 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() || 9761 SafelenLength->isInstantiationDependent() || 9762 SafelenLength->containsUnexpandedParameterPack()) 9763 return false; 9764 Expr::EvalResult SimdlenResult, SafelenResult; 9765 SimdlenLength->EvaluateAsInt(SimdlenResult, S.Context); 9766 SafelenLength->EvaluateAsInt(SafelenResult, S.Context); 9767 llvm::APSInt SimdlenRes = SimdlenResult.Val.getInt(); 9768 llvm::APSInt SafelenRes = SafelenResult.Val.getInt(); 9769 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions] 9770 // If both simdlen and safelen clauses are specified, the value of the 9771 // simdlen parameter must be less than or equal to the value of the safelen 9772 // parameter. 9773 if (SimdlenRes > SafelenRes) { 9774 S.Diag(SimdlenLength->getExprLoc(), 9775 diag::err_omp_wrong_simdlen_safelen_values) 9776 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange(); 9777 return true; 9778 } 9779 } 9780 return false; 9781 } 9782 9783 StmtResult 9784 Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt, 9785 SourceLocation StartLoc, SourceLocation EndLoc, 9786 VarsWithInheritedDSAType &VarsWithImplicitDSA) { 9787 if (!AStmt) 9788 return StmtError(); 9789 9790 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9791 OMPLoopBasedDirective::HelperExprs B; 9792 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 9793 // define the nested loops number. 9794 unsigned NestedLoopCount = checkOpenMPLoop( 9795 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), 9796 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 9797 if (NestedLoopCount == 0) 9798 return StmtError(); 9799 9800 assert((CurContext->isDependentContext() || B.builtAll()) && 9801 "omp simd loop exprs were not built"); 9802 9803 if (!CurContext->isDependentContext()) { 9804 // Finalize the clauses that need pre-built expressions for CodeGen. 9805 for (OMPClause *C : Clauses) { 9806 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 9807 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 9808 B.NumIterations, *this, CurScope, 9809 DSAStack)) 9810 return StmtError(); 9811 } 9812 } 9813 9814 if (checkSimdlenSafelenSpecified(*this, Clauses)) 9815 return StmtError(); 9816 9817 setFunctionHasBranchProtectedScope(); 9818 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 9819 Clauses, AStmt, B); 9820 } 9821 9822 StmtResult 9823 Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt, 9824 SourceLocation StartLoc, SourceLocation EndLoc, 9825 VarsWithInheritedDSAType &VarsWithImplicitDSA) { 9826 if (!AStmt) 9827 return StmtError(); 9828 9829 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9830 OMPLoopBasedDirective::HelperExprs B; 9831 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 9832 // define the nested loops number. 9833 unsigned NestedLoopCount = checkOpenMPLoop( 9834 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), 9835 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 9836 if (NestedLoopCount == 0) 9837 return StmtError(); 9838 9839 assert((CurContext->isDependentContext() || B.builtAll()) && 9840 "omp for loop exprs were not built"); 9841 9842 if (!CurContext->isDependentContext()) { 9843 // Finalize the clauses that need pre-built expressions for CodeGen. 9844 for (OMPClause *C : Clauses) { 9845 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 9846 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 9847 B.NumIterations, *this, CurScope, 9848 DSAStack)) 9849 return StmtError(); 9850 } 9851 } 9852 9853 setFunctionHasBranchProtectedScope(); 9854 return OMPForDirective::Create( 9855 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 9856 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 9857 } 9858 9859 StmtResult Sema::ActOnOpenMPForSimdDirective( 9860 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 9861 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 9862 if (!AStmt) 9863 return StmtError(); 9864 9865 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9866 OMPLoopBasedDirective::HelperExprs B; 9867 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 9868 // define the nested loops number. 9869 unsigned NestedLoopCount = 9870 checkOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses), 9871 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 9872 VarsWithImplicitDSA, B); 9873 if (NestedLoopCount == 0) 9874 return StmtError(); 9875 9876 assert((CurContext->isDependentContext() || B.builtAll()) && 9877 "omp for simd loop exprs were not built"); 9878 9879 if (!CurContext->isDependentContext()) { 9880 // Finalize the clauses that need pre-built expressions for CodeGen. 9881 for (OMPClause *C : Clauses) { 9882 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 9883 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 9884 B.NumIterations, *this, CurScope, 9885 DSAStack)) 9886 return StmtError(); 9887 } 9888 } 9889 9890 if (checkSimdlenSafelenSpecified(*this, Clauses)) 9891 return StmtError(); 9892 9893 setFunctionHasBranchProtectedScope(); 9894 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 9895 Clauses, AStmt, B); 9896 } 9897 9898 StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses, 9899 Stmt *AStmt, 9900 SourceLocation StartLoc, 9901 SourceLocation EndLoc) { 9902 if (!AStmt) 9903 return StmtError(); 9904 9905 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9906 auto BaseStmt = AStmt; 9907 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 9908 BaseStmt = CS->getCapturedStmt(); 9909 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 9910 auto S = C->children(); 9911 if (S.begin() == S.end()) 9912 return StmtError(); 9913 // All associated statements must be '#pragma omp section' except for 9914 // the first one. 9915 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) { 9916 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 9917 if (SectionStmt) 9918 Diag(SectionStmt->getBeginLoc(), 9919 diag::err_omp_sections_substmt_not_section); 9920 return StmtError(); 9921 } 9922 cast<OMPSectionDirective>(SectionStmt) 9923 ->setHasCancel(DSAStack->isCancelRegion()); 9924 } 9925 } else { 9926 Diag(AStmt->getBeginLoc(), diag::err_omp_sections_not_compound_stmt); 9927 return StmtError(); 9928 } 9929 9930 setFunctionHasBranchProtectedScope(); 9931 9932 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 9933 DSAStack->getTaskgroupReductionRef(), 9934 DSAStack->isCancelRegion()); 9935 } 9936 9937 StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt, 9938 SourceLocation StartLoc, 9939 SourceLocation EndLoc) { 9940 if (!AStmt) 9941 return StmtError(); 9942 9943 setFunctionHasBranchProtectedScope(); 9944 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion()); 9945 9946 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt, 9947 DSAStack->isCancelRegion()); 9948 } 9949 9950 static Expr *getDirectCallExpr(Expr *E) { 9951 E = E->IgnoreParenCasts()->IgnoreImplicit(); 9952 if (auto *CE = dyn_cast<CallExpr>(E)) 9953 if (CE->getDirectCallee()) 9954 return E; 9955 return nullptr; 9956 } 9957 9958 StmtResult Sema::ActOnOpenMPDispatchDirective(ArrayRef<OMPClause *> Clauses, 9959 Stmt *AStmt, 9960 SourceLocation StartLoc, 9961 SourceLocation EndLoc) { 9962 if (!AStmt) 9963 return StmtError(); 9964 9965 Stmt *S = cast<CapturedStmt>(AStmt)->getCapturedStmt(); 9966 9967 // 5.1 OpenMP 9968 // expression-stmt : an expression statement with one of the following forms: 9969 // expression = target-call ( [expression-list] ); 9970 // target-call ( [expression-list] ); 9971 9972 SourceLocation TargetCallLoc; 9973 9974 if (!CurContext->isDependentContext()) { 9975 Expr *TargetCall = nullptr; 9976 9977 auto *E = dyn_cast<Expr>(S); 9978 if (!E) { 9979 Diag(S->getBeginLoc(), diag::err_omp_dispatch_statement_call); 9980 return StmtError(); 9981 } 9982 9983 E = E->IgnoreParenCasts()->IgnoreImplicit(); 9984 9985 if (auto *BO = dyn_cast<BinaryOperator>(E)) { 9986 if (BO->getOpcode() == BO_Assign) 9987 TargetCall = getDirectCallExpr(BO->getRHS()); 9988 } else { 9989 if (auto *COCE = dyn_cast<CXXOperatorCallExpr>(E)) 9990 if (COCE->getOperator() == OO_Equal) 9991 TargetCall = getDirectCallExpr(COCE->getArg(1)); 9992 if (!TargetCall) 9993 TargetCall = getDirectCallExpr(E); 9994 } 9995 if (!TargetCall) { 9996 Diag(E->getBeginLoc(), diag::err_omp_dispatch_statement_call); 9997 return StmtError(); 9998 } 9999 TargetCallLoc = TargetCall->getExprLoc(); 10000 } 10001 10002 setFunctionHasBranchProtectedScope(); 10003 10004 return OMPDispatchDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 10005 TargetCallLoc); 10006 } 10007 10008 StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses, 10009 Stmt *AStmt, 10010 SourceLocation StartLoc, 10011 SourceLocation EndLoc) { 10012 if (!AStmt) 10013 return StmtError(); 10014 10015 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10016 10017 setFunctionHasBranchProtectedScope(); 10018 10019 // OpenMP [2.7.3, single Construct, Restrictions] 10020 // The copyprivate clause must not be used with the nowait clause. 10021 const OMPClause *Nowait = nullptr; 10022 const OMPClause *Copyprivate = nullptr; 10023 for (const OMPClause *Clause : Clauses) { 10024 if (Clause->getClauseKind() == OMPC_nowait) 10025 Nowait = Clause; 10026 else if (Clause->getClauseKind() == OMPC_copyprivate) 10027 Copyprivate = Clause; 10028 if (Copyprivate && Nowait) { 10029 Diag(Copyprivate->getBeginLoc(), 10030 diag::err_omp_single_copyprivate_with_nowait); 10031 Diag(Nowait->getBeginLoc(), diag::note_omp_nowait_clause_here); 10032 return StmtError(); 10033 } 10034 } 10035 10036 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 10037 } 10038 10039 StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt, 10040 SourceLocation StartLoc, 10041 SourceLocation EndLoc) { 10042 if (!AStmt) 10043 return StmtError(); 10044 10045 setFunctionHasBranchProtectedScope(); 10046 10047 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt); 10048 } 10049 10050 StmtResult Sema::ActOnOpenMPMaskedDirective(ArrayRef<OMPClause *> Clauses, 10051 Stmt *AStmt, 10052 SourceLocation StartLoc, 10053 SourceLocation EndLoc) { 10054 if (!AStmt) 10055 return StmtError(); 10056 10057 setFunctionHasBranchProtectedScope(); 10058 10059 return OMPMaskedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 10060 } 10061 10062 StmtResult Sema::ActOnOpenMPCriticalDirective( 10063 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses, 10064 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { 10065 if (!AStmt) 10066 return StmtError(); 10067 10068 bool ErrorFound = false; 10069 llvm::APSInt Hint; 10070 SourceLocation HintLoc; 10071 bool DependentHint = false; 10072 for (const OMPClause *C : Clauses) { 10073 if (C->getClauseKind() == OMPC_hint) { 10074 if (!DirName.getName()) { 10075 Diag(C->getBeginLoc(), diag::err_omp_hint_clause_no_name); 10076 ErrorFound = true; 10077 } 10078 Expr *E = cast<OMPHintClause>(C)->getHint(); 10079 if (E->isTypeDependent() || E->isValueDependent() || 10080 E->isInstantiationDependent()) { 10081 DependentHint = true; 10082 } else { 10083 Hint = E->EvaluateKnownConstInt(Context); 10084 HintLoc = C->getBeginLoc(); 10085 } 10086 } 10087 } 10088 if (ErrorFound) 10089 return StmtError(); 10090 const auto Pair = DSAStack->getCriticalWithHint(DirName); 10091 if (Pair.first && DirName.getName() && !DependentHint) { 10092 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) { 10093 Diag(StartLoc, diag::err_omp_critical_with_hint); 10094 if (HintLoc.isValid()) 10095 Diag(HintLoc, diag::note_omp_critical_hint_here) 10096 << 0 << toString(Hint, /*Radix=*/10, /*Signed=*/false); 10097 else 10098 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0; 10099 if (const auto *C = Pair.first->getSingleClause<OMPHintClause>()) { 10100 Diag(C->getBeginLoc(), diag::note_omp_critical_hint_here) 10101 << 1 10102 << toString(C->getHint()->EvaluateKnownConstInt(Context), 10103 /*Radix=*/10, /*Signed=*/false); 10104 } else { 10105 Diag(Pair.first->getBeginLoc(), diag::note_omp_critical_no_hint) << 1; 10106 } 10107 } 10108 } 10109 10110 setFunctionHasBranchProtectedScope(); 10111 10112 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc, 10113 Clauses, AStmt); 10114 if (!Pair.first && DirName.getName() && !DependentHint) 10115 DSAStack->addCriticalWithHint(Dir, Hint); 10116 return Dir; 10117 } 10118 10119 StmtResult Sema::ActOnOpenMPParallelForDirective( 10120 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10121 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10122 if (!AStmt) 10123 return StmtError(); 10124 10125 auto *CS = cast<CapturedStmt>(AStmt); 10126 // 1.2.2 OpenMP Language Terminology 10127 // Structured block - An executable statement with a single entry at the 10128 // top and a single exit at the bottom. 10129 // The point of exit cannot be a branch out of the structured block. 10130 // longjmp() and throw() must not violate the entry/exit criteria. 10131 CS->getCapturedDecl()->setNothrow(); 10132 10133 OMPLoopBasedDirective::HelperExprs B; 10134 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 10135 // define the nested loops number. 10136 unsigned NestedLoopCount = 10137 checkOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses), 10138 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 10139 VarsWithImplicitDSA, B); 10140 if (NestedLoopCount == 0) 10141 return StmtError(); 10142 10143 assert((CurContext->isDependentContext() || B.builtAll()) && 10144 "omp parallel for loop exprs were not built"); 10145 10146 if (!CurContext->isDependentContext()) { 10147 // Finalize the clauses that need pre-built expressions for CodeGen. 10148 for (OMPClause *C : Clauses) { 10149 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 10150 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 10151 B.NumIterations, *this, CurScope, 10152 DSAStack)) 10153 return StmtError(); 10154 } 10155 } 10156 10157 setFunctionHasBranchProtectedScope(); 10158 return OMPParallelForDirective::Create( 10159 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 10160 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 10161 } 10162 10163 StmtResult Sema::ActOnOpenMPParallelForSimdDirective( 10164 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10165 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10166 if (!AStmt) 10167 return StmtError(); 10168 10169 auto *CS = cast<CapturedStmt>(AStmt); 10170 // 1.2.2 OpenMP Language Terminology 10171 // Structured block - An executable statement with a single entry at the 10172 // top and a single exit at the bottom. 10173 // The point of exit cannot be a branch out of the structured block. 10174 // longjmp() and throw() must not violate the entry/exit criteria. 10175 CS->getCapturedDecl()->setNothrow(); 10176 10177 OMPLoopBasedDirective::HelperExprs B; 10178 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 10179 // define the nested loops number. 10180 unsigned NestedLoopCount = 10181 checkOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses), 10182 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 10183 VarsWithImplicitDSA, B); 10184 if (NestedLoopCount == 0) 10185 return StmtError(); 10186 10187 if (!CurContext->isDependentContext()) { 10188 // Finalize the clauses that need pre-built expressions for CodeGen. 10189 for (OMPClause *C : Clauses) { 10190 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 10191 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 10192 B.NumIterations, *this, CurScope, 10193 DSAStack)) 10194 return StmtError(); 10195 } 10196 } 10197 10198 if (checkSimdlenSafelenSpecified(*this, Clauses)) 10199 return StmtError(); 10200 10201 setFunctionHasBranchProtectedScope(); 10202 return OMPParallelForSimdDirective::Create( 10203 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 10204 } 10205 10206 StmtResult 10207 Sema::ActOnOpenMPParallelMasterDirective(ArrayRef<OMPClause *> Clauses, 10208 Stmt *AStmt, SourceLocation StartLoc, 10209 SourceLocation EndLoc) { 10210 if (!AStmt) 10211 return StmtError(); 10212 10213 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10214 auto *CS = cast<CapturedStmt>(AStmt); 10215 // 1.2.2 OpenMP Language Terminology 10216 // Structured block - An executable statement with a single entry at the 10217 // top and a single exit at the bottom. 10218 // The point of exit cannot be a branch out of the structured block. 10219 // longjmp() and throw() must not violate the entry/exit criteria. 10220 CS->getCapturedDecl()->setNothrow(); 10221 10222 setFunctionHasBranchProtectedScope(); 10223 10224 return OMPParallelMasterDirective::Create( 10225 Context, StartLoc, EndLoc, Clauses, AStmt, 10226 DSAStack->getTaskgroupReductionRef()); 10227 } 10228 10229 StmtResult 10230 Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses, 10231 Stmt *AStmt, SourceLocation StartLoc, 10232 SourceLocation EndLoc) { 10233 if (!AStmt) 10234 return StmtError(); 10235 10236 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10237 auto BaseStmt = AStmt; 10238 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 10239 BaseStmt = CS->getCapturedStmt(); 10240 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 10241 auto S = C->children(); 10242 if (S.begin() == S.end()) 10243 return StmtError(); 10244 // All associated statements must be '#pragma omp section' except for 10245 // the first one. 10246 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) { 10247 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 10248 if (SectionStmt) 10249 Diag(SectionStmt->getBeginLoc(), 10250 diag::err_omp_parallel_sections_substmt_not_section); 10251 return StmtError(); 10252 } 10253 cast<OMPSectionDirective>(SectionStmt) 10254 ->setHasCancel(DSAStack->isCancelRegion()); 10255 } 10256 } else { 10257 Diag(AStmt->getBeginLoc(), 10258 diag::err_omp_parallel_sections_not_compound_stmt); 10259 return StmtError(); 10260 } 10261 10262 setFunctionHasBranchProtectedScope(); 10263 10264 return OMPParallelSectionsDirective::Create( 10265 Context, StartLoc, EndLoc, Clauses, AStmt, 10266 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 10267 } 10268 10269 /// Find and diagnose mutually exclusive clause kinds. 10270 static bool checkMutuallyExclusiveClauses( 10271 Sema &S, ArrayRef<OMPClause *> Clauses, 10272 ArrayRef<OpenMPClauseKind> MutuallyExclusiveClauses) { 10273 const OMPClause *PrevClause = nullptr; 10274 bool ErrorFound = false; 10275 for (const OMPClause *C : Clauses) { 10276 if (llvm::is_contained(MutuallyExclusiveClauses, C->getClauseKind())) { 10277 if (!PrevClause) { 10278 PrevClause = C; 10279 } else if (PrevClause->getClauseKind() != C->getClauseKind()) { 10280 S.Diag(C->getBeginLoc(), diag::err_omp_clauses_mutually_exclusive) 10281 << getOpenMPClauseName(C->getClauseKind()) 10282 << getOpenMPClauseName(PrevClause->getClauseKind()); 10283 S.Diag(PrevClause->getBeginLoc(), diag::note_omp_previous_clause) 10284 << getOpenMPClauseName(PrevClause->getClauseKind()); 10285 ErrorFound = true; 10286 } 10287 } 10288 } 10289 return ErrorFound; 10290 } 10291 10292 StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses, 10293 Stmt *AStmt, SourceLocation StartLoc, 10294 SourceLocation EndLoc) { 10295 if (!AStmt) 10296 return StmtError(); 10297 10298 // OpenMP 5.0, 2.10.1 task Construct 10299 // If a detach clause appears on the directive, then a mergeable clause cannot 10300 // appear on the same directive. 10301 if (checkMutuallyExclusiveClauses(*this, Clauses, 10302 {OMPC_detach, OMPC_mergeable})) 10303 return StmtError(); 10304 10305 auto *CS = cast<CapturedStmt>(AStmt); 10306 // 1.2.2 OpenMP Language Terminology 10307 // Structured block - An executable statement with a single entry at the 10308 // top and a single exit at the bottom. 10309 // The point of exit cannot be a branch out of the structured block. 10310 // longjmp() and throw() must not violate the entry/exit criteria. 10311 CS->getCapturedDecl()->setNothrow(); 10312 10313 setFunctionHasBranchProtectedScope(); 10314 10315 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 10316 DSAStack->isCancelRegion()); 10317 } 10318 10319 StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc, 10320 SourceLocation EndLoc) { 10321 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc); 10322 } 10323 10324 StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc, 10325 SourceLocation EndLoc) { 10326 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc); 10327 } 10328 10329 StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc, 10330 SourceLocation EndLoc) { 10331 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc); 10332 } 10333 10334 StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses, 10335 Stmt *AStmt, 10336 SourceLocation StartLoc, 10337 SourceLocation EndLoc) { 10338 if (!AStmt) 10339 return StmtError(); 10340 10341 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10342 10343 setFunctionHasBranchProtectedScope(); 10344 10345 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses, 10346 AStmt, 10347 DSAStack->getTaskgroupReductionRef()); 10348 } 10349 10350 StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses, 10351 SourceLocation StartLoc, 10352 SourceLocation EndLoc) { 10353 OMPFlushClause *FC = nullptr; 10354 OMPClause *OrderClause = nullptr; 10355 for (OMPClause *C : Clauses) { 10356 if (C->getClauseKind() == OMPC_flush) 10357 FC = cast<OMPFlushClause>(C); 10358 else 10359 OrderClause = C; 10360 } 10361 OpenMPClauseKind MemOrderKind = OMPC_unknown; 10362 SourceLocation MemOrderLoc; 10363 for (const OMPClause *C : Clauses) { 10364 if (C->getClauseKind() == OMPC_acq_rel || 10365 C->getClauseKind() == OMPC_acquire || 10366 C->getClauseKind() == OMPC_release) { 10367 if (MemOrderKind != OMPC_unknown) { 10368 Diag(C->getBeginLoc(), diag::err_omp_several_mem_order_clauses) 10369 << getOpenMPDirectiveName(OMPD_flush) << 1 10370 << SourceRange(C->getBeginLoc(), C->getEndLoc()); 10371 Diag(MemOrderLoc, diag::note_omp_previous_mem_order_clause) 10372 << getOpenMPClauseName(MemOrderKind); 10373 } else { 10374 MemOrderKind = C->getClauseKind(); 10375 MemOrderLoc = C->getBeginLoc(); 10376 } 10377 } 10378 } 10379 if (FC && OrderClause) { 10380 Diag(FC->getLParenLoc(), diag::err_omp_flush_order_clause_and_list) 10381 << getOpenMPClauseName(OrderClause->getClauseKind()); 10382 Diag(OrderClause->getBeginLoc(), diag::note_omp_flush_order_clause_here) 10383 << getOpenMPClauseName(OrderClause->getClauseKind()); 10384 return StmtError(); 10385 } 10386 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses); 10387 } 10388 10389 StmtResult Sema::ActOnOpenMPDepobjDirective(ArrayRef<OMPClause *> Clauses, 10390 SourceLocation StartLoc, 10391 SourceLocation EndLoc) { 10392 if (Clauses.empty()) { 10393 Diag(StartLoc, diag::err_omp_depobj_expected); 10394 return StmtError(); 10395 } else if (Clauses[0]->getClauseKind() != OMPC_depobj) { 10396 Diag(Clauses[0]->getBeginLoc(), diag::err_omp_depobj_expected); 10397 return StmtError(); 10398 } 10399 // Only depobj expression and another single clause is allowed. 10400 if (Clauses.size() > 2) { 10401 Diag(Clauses[2]->getBeginLoc(), 10402 diag::err_omp_depobj_single_clause_expected); 10403 return StmtError(); 10404 } else if (Clauses.size() < 1) { 10405 Diag(Clauses[0]->getEndLoc(), diag::err_omp_depobj_single_clause_expected); 10406 return StmtError(); 10407 } 10408 return OMPDepobjDirective::Create(Context, StartLoc, EndLoc, Clauses); 10409 } 10410 10411 StmtResult Sema::ActOnOpenMPScanDirective(ArrayRef<OMPClause *> Clauses, 10412 SourceLocation StartLoc, 10413 SourceLocation EndLoc) { 10414 // Check that exactly one clause is specified. 10415 if (Clauses.size() != 1) { 10416 Diag(Clauses.empty() ? EndLoc : Clauses[1]->getBeginLoc(), 10417 diag::err_omp_scan_single_clause_expected); 10418 return StmtError(); 10419 } 10420 // Check that scan directive is used in the scopeof the OpenMP loop body. 10421 if (Scope *S = DSAStack->getCurScope()) { 10422 Scope *ParentS = S->getParent(); 10423 if (!ParentS || ParentS->getParent() != ParentS->getBreakParent() || 10424 !ParentS->getBreakParent()->isOpenMPLoopScope()) 10425 return StmtError(Diag(StartLoc, diag::err_omp_orphaned_device_directive) 10426 << getOpenMPDirectiveName(OMPD_scan) << 5); 10427 } 10428 // Check that only one instance of scan directives is used in the same outer 10429 // region. 10430 if (DSAStack->doesParentHasScanDirective()) { 10431 Diag(StartLoc, diag::err_omp_several_directives_in_region) << "scan"; 10432 Diag(DSAStack->getParentScanDirectiveLoc(), 10433 diag::note_omp_previous_directive) 10434 << "scan"; 10435 return StmtError(); 10436 } 10437 DSAStack->setParentHasScanDirective(StartLoc); 10438 return OMPScanDirective::Create(Context, StartLoc, EndLoc, Clauses); 10439 } 10440 10441 StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses, 10442 Stmt *AStmt, 10443 SourceLocation StartLoc, 10444 SourceLocation EndLoc) { 10445 const OMPClause *DependFound = nullptr; 10446 const OMPClause *DependSourceClause = nullptr; 10447 const OMPClause *DependSinkClause = nullptr; 10448 bool ErrorFound = false; 10449 const OMPThreadsClause *TC = nullptr; 10450 const OMPSIMDClause *SC = nullptr; 10451 for (const OMPClause *C : Clauses) { 10452 if (auto *DC = dyn_cast<OMPDependClause>(C)) { 10453 DependFound = C; 10454 if (DC->getDependencyKind() == OMPC_DEPEND_source) { 10455 if (DependSourceClause) { 10456 Diag(C->getBeginLoc(), diag::err_omp_more_one_clause) 10457 << getOpenMPDirectiveName(OMPD_ordered) 10458 << getOpenMPClauseName(OMPC_depend) << 2; 10459 ErrorFound = true; 10460 } else { 10461 DependSourceClause = C; 10462 } 10463 if (DependSinkClause) { 10464 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed) 10465 << 0; 10466 ErrorFound = true; 10467 } 10468 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) { 10469 if (DependSourceClause) { 10470 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed) 10471 << 1; 10472 ErrorFound = true; 10473 } 10474 DependSinkClause = C; 10475 } 10476 } else if (C->getClauseKind() == OMPC_threads) { 10477 TC = cast<OMPThreadsClause>(C); 10478 } else if (C->getClauseKind() == OMPC_simd) { 10479 SC = cast<OMPSIMDClause>(C); 10480 } 10481 } 10482 if (!ErrorFound && !SC && 10483 isOpenMPSimdDirective(DSAStack->getParentDirective())) { 10484 // OpenMP [2.8.1,simd Construct, Restrictions] 10485 // An ordered construct with the simd clause is the only OpenMP construct 10486 // that can appear in the simd region. 10487 Diag(StartLoc, diag::err_omp_prohibited_region_simd) 10488 << (LangOpts.OpenMP >= 50 ? 1 : 0); 10489 ErrorFound = true; 10490 } else if (DependFound && (TC || SC)) { 10491 Diag(DependFound->getBeginLoc(), diag::err_omp_depend_clause_thread_simd) 10492 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind()); 10493 ErrorFound = true; 10494 } else if (DependFound && !DSAStack->getParentOrderedRegionParam().first) { 10495 Diag(DependFound->getBeginLoc(), 10496 diag::err_omp_ordered_directive_without_param); 10497 ErrorFound = true; 10498 } else if (TC || Clauses.empty()) { 10499 if (const Expr *Param = DSAStack->getParentOrderedRegionParam().first) { 10500 SourceLocation ErrLoc = TC ? TC->getBeginLoc() : StartLoc; 10501 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param) 10502 << (TC != nullptr); 10503 Diag(Param->getBeginLoc(), diag::note_omp_ordered_param) << 1; 10504 ErrorFound = true; 10505 } 10506 } 10507 if ((!AStmt && !DependFound) || ErrorFound) 10508 return StmtError(); 10509 10510 // OpenMP 5.0, 2.17.9, ordered Construct, Restrictions. 10511 // During execution of an iteration of a worksharing-loop or a loop nest 10512 // within a worksharing-loop, simd, or worksharing-loop SIMD region, a thread 10513 // must not execute more than one ordered region corresponding to an ordered 10514 // construct without a depend clause. 10515 if (!DependFound) { 10516 if (DSAStack->doesParentHasOrderedDirective()) { 10517 Diag(StartLoc, diag::err_omp_several_directives_in_region) << "ordered"; 10518 Diag(DSAStack->getParentOrderedDirectiveLoc(), 10519 diag::note_omp_previous_directive) 10520 << "ordered"; 10521 return StmtError(); 10522 } 10523 DSAStack->setParentHasOrderedDirective(StartLoc); 10524 } 10525 10526 if (AStmt) { 10527 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10528 10529 setFunctionHasBranchProtectedScope(); 10530 } 10531 10532 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 10533 } 10534 10535 namespace { 10536 /// Helper class for checking expression in 'omp atomic [update]' 10537 /// construct. 10538 class OpenMPAtomicUpdateChecker { 10539 /// Error results for atomic update expressions. 10540 enum ExprAnalysisErrorCode { 10541 /// A statement is not an expression statement. 10542 NotAnExpression, 10543 /// Expression is not builtin binary or unary operation. 10544 NotABinaryOrUnaryExpression, 10545 /// Unary operation is not post-/pre- increment/decrement operation. 10546 NotAnUnaryIncDecExpression, 10547 /// An expression is not of scalar type. 10548 NotAScalarType, 10549 /// A binary operation is not an assignment operation. 10550 NotAnAssignmentOp, 10551 /// RHS part of the binary operation is not a binary expression. 10552 NotABinaryExpression, 10553 /// RHS part is not additive/multiplicative/shift/biwise binary 10554 /// expression. 10555 NotABinaryOperator, 10556 /// RHS binary operation does not have reference to the updated LHS 10557 /// part. 10558 NotAnUpdateExpression, 10559 /// No errors is found. 10560 NoError 10561 }; 10562 /// Reference to Sema. 10563 Sema &SemaRef; 10564 /// A location for note diagnostics (when error is found). 10565 SourceLocation NoteLoc; 10566 /// 'x' lvalue part of the source atomic expression. 10567 Expr *X; 10568 /// 'expr' rvalue part of the source atomic expression. 10569 Expr *E; 10570 /// Helper expression of the form 10571 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or 10572 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. 10573 Expr *UpdateExpr; 10574 /// Is 'x' a LHS in a RHS part of full update expression. It is 10575 /// important for non-associative operations. 10576 bool IsXLHSInRHSPart; 10577 BinaryOperatorKind Op; 10578 SourceLocation OpLoc; 10579 /// true if the source expression is a postfix unary operation, false 10580 /// if it is a prefix unary operation. 10581 bool IsPostfixUpdate; 10582 10583 public: 10584 OpenMPAtomicUpdateChecker(Sema &SemaRef) 10585 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr), 10586 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {} 10587 /// Check specified statement that it is suitable for 'atomic update' 10588 /// constructs and extract 'x', 'expr' and Operation from the original 10589 /// expression. If DiagId and NoteId == 0, then only check is performed 10590 /// without error notification. 10591 /// \param DiagId Diagnostic which should be emitted if error is found. 10592 /// \param NoteId Diagnostic note for the main error message. 10593 /// \return true if statement is not an update expression, false otherwise. 10594 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0); 10595 /// Return the 'x' lvalue part of the source atomic expression. 10596 Expr *getX() const { return X; } 10597 /// Return the 'expr' rvalue part of the source atomic expression. 10598 Expr *getExpr() const { return E; } 10599 /// Return the update expression used in calculation of the updated 10600 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or 10601 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. 10602 Expr *getUpdateExpr() const { return UpdateExpr; } 10603 /// Return true if 'x' is LHS in RHS part of full update expression, 10604 /// false otherwise. 10605 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; } 10606 10607 /// true if the source expression is a postfix unary operation, false 10608 /// if it is a prefix unary operation. 10609 bool isPostfixUpdate() const { return IsPostfixUpdate; } 10610 10611 private: 10612 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0, 10613 unsigned NoteId = 0); 10614 }; 10615 } // namespace 10616 10617 bool OpenMPAtomicUpdateChecker::checkBinaryOperation( 10618 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) { 10619 ExprAnalysisErrorCode ErrorFound = NoError; 10620 SourceLocation ErrorLoc, NoteLoc; 10621 SourceRange ErrorRange, NoteRange; 10622 // Allowed constructs are: 10623 // x = x binop expr; 10624 // x = expr binop x; 10625 if (AtomicBinOp->getOpcode() == BO_Assign) { 10626 X = AtomicBinOp->getLHS(); 10627 if (const auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>( 10628 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) { 10629 if (AtomicInnerBinOp->isMultiplicativeOp() || 10630 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() || 10631 AtomicInnerBinOp->isBitwiseOp()) { 10632 Op = AtomicInnerBinOp->getOpcode(); 10633 OpLoc = AtomicInnerBinOp->getOperatorLoc(); 10634 Expr *LHS = AtomicInnerBinOp->getLHS(); 10635 Expr *RHS = AtomicInnerBinOp->getRHS(); 10636 llvm::FoldingSetNodeID XId, LHSId, RHSId; 10637 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(), 10638 /*Canonical=*/true); 10639 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(), 10640 /*Canonical=*/true); 10641 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(), 10642 /*Canonical=*/true); 10643 if (XId == LHSId) { 10644 E = RHS; 10645 IsXLHSInRHSPart = true; 10646 } else if (XId == RHSId) { 10647 E = LHS; 10648 IsXLHSInRHSPart = false; 10649 } else { 10650 ErrorLoc = AtomicInnerBinOp->getExprLoc(); 10651 ErrorRange = AtomicInnerBinOp->getSourceRange(); 10652 NoteLoc = X->getExprLoc(); 10653 NoteRange = X->getSourceRange(); 10654 ErrorFound = NotAnUpdateExpression; 10655 } 10656 } else { 10657 ErrorLoc = AtomicInnerBinOp->getExprLoc(); 10658 ErrorRange = AtomicInnerBinOp->getSourceRange(); 10659 NoteLoc = AtomicInnerBinOp->getOperatorLoc(); 10660 NoteRange = SourceRange(NoteLoc, NoteLoc); 10661 ErrorFound = NotABinaryOperator; 10662 } 10663 } else { 10664 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc(); 10665 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange(); 10666 ErrorFound = NotABinaryExpression; 10667 } 10668 } else { 10669 ErrorLoc = AtomicBinOp->getExprLoc(); 10670 ErrorRange = AtomicBinOp->getSourceRange(); 10671 NoteLoc = AtomicBinOp->getOperatorLoc(); 10672 NoteRange = SourceRange(NoteLoc, NoteLoc); 10673 ErrorFound = NotAnAssignmentOp; 10674 } 10675 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { 10676 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; 10677 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; 10678 return true; 10679 } 10680 if (SemaRef.CurContext->isDependentContext()) 10681 E = X = UpdateExpr = nullptr; 10682 return ErrorFound != NoError; 10683 } 10684 10685 bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId, 10686 unsigned NoteId) { 10687 ExprAnalysisErrorCode ErrorFound = NoError; 10688 SourceLocation ErrorLoc, NoteLoc; 10689 SourceRange ErrorRange, NoteRange; 10690 // Allowed constructs are: 10691 // x++; 10692 // x--; 10693 // ++x; 10694 // --x; 10695 // x binop= expr; 10696 // x = x binop expr; 10697 // x = expr binop x; 10698 if (auto *AtomicBody = dyn_cast<Expr>(S)) { 10699 AtomicBody = AtomicBody->IgnoreParenImpCasts(); 10700 if (AtomicBody->getType()->isScalarType() || 10701 AtomicBody->isInstantiationDependent()) { 10702 if (const auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>( 10703 AtomicBody->IgnoreParenImpCasts())) { 10704 // Check for Compound Assignment Operation 10705 Op = BinaryOperator::getOpForCompoundAssignment( 10706 AtomicCompAssignOp->getOpcode()); 10707 OpLoc = AtomicCompAssignOp->getOperatorLoc(); 10708 E = AtomicCompAssignOp->getRHS(); 10709 X = AtomicCompAssignOp->getLHS()->IgnoreParens(); 10710 IsXLHSInRHSPart = true; 10711 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>( 10712 AtomicBody->IgnoreParenImpCasts())) { 10713 // Check for Binary Operation 10714 if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId)) 10715 return true; 10716 } else if (const auto *AtomicUnaryOp = dyn_cast<UnaryOperator>( 10717 AtomicBody->IgnoreParenImpCasts())) { 10718 // Check for Unary Operation 10719 if (AtomicUnaryOp->isIncrementDecrementOp()) { 10720 IsPostfixUpdate = AtomicUnaryOp->isPostfix(); 10721 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub; 10722 OpLoc = AtomicUnaryOp->getOperatorLoc(); 10723 X = AtomicUnaryOp->getSubExpr()->IgnoreParens(); 10724 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get(); 10725 IsXLHSInRHSPart = true; 10726 } else { 10727 ErrorFound = NotAnUnaryIncDecExpression; 10728 ErrorLoc = AtomicUnaryOp->getExprLoc(); 10729 ErrorRange = AtomicUnaryOp->getSourceRange(); 10730 NoteLoc = AtomicUnaryOp->getOperatorLoc(); 10731 NoteRange = SourceRange(NoteLoc, NoteLoc); 10732 } 10733 } else if (!AtomicBody->isInstantiationDependent()) { 10734 ErrorFound = NotABinaryOrUnaryExpression; 10735 NoteLoc = ErrorLoc = AtomicBody->getExprLoc(); 10736 NoteRange = ErrorRange = AtomicBody->getSourceRange(); 10737 } 10738 } else { 10739 ErrorFound = NotAScalarType; 10740 NoteLoc = ErrorLoc = AtomicBody->getBeginLoc(); 10741 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 10742 } 10743 } else { 10744 ErrorFound = NotAnExpression; 10745 NoteLoc = ErrorLoc = S->getBeginLoc(); 10746 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 10747 } 10748 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { 10749 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; 10750 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; 10751 return true; 10752 } 10753 if (SemaRef.CurContext->isDependentContext()) 10754 E = X = UpdateExpr = nullptr; 10755 if (ErrorFound == NoError && E && X) { 10756 // Build an update expression of form 'OpaqueValueExpr(x) binop 10757 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop 10758 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression. 10759 auto *OVEX = new (SemaRef.getASTContext()) 10760 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_PRValue); 10761 auto *OVEExpr = new (SemaRef.getASTContext()) 10762 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_PRValue); 10763 ExprResult Update = 10764 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr, 10765 IsXLHSInRHSPart ? OVEExpr : OVEX); 10766 if (Update.isInvalid()) 10767 return true; 10768 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(), 10769 Sema::AA_Casting); 10770 if (Update.isInvalid()) 10771 return true; 10772 UpdateExpr = Update.get(); 10773 } 10774 return ErrorFound != NoError; 10775 } 10776 10777 StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses, 10778 Stmt *AStmt, 10779 SourceLocation StartLoc, 10780 SourceLocation EndLoc) { 10781 // Register location of the first atomic directive. 10782 DSAStack->addAtomicDirectiveLoc(StartLoc); 10783 if (!AStmt) 10784 return StmtError(); 10785 10786 // 1.2.2 OpenMP Language Terminology 10787 // Structured block - An executable statement with a single entry at the 10788 // top and a single exit at the bottom. 10789 // The point of exit cannot be a branch out of the structured block. 10790 // longjmp() and throw() must not violate the entry/exit criteria. 10791 OpenMPClauseKind AtomicKind = OMPC_unknown; 10792 SourceLocation AtomicKindLoc; 10793 OpenMPClauseKind MemOrderKind = OMPC_unknown; 10794 SourceLocation MemOrderLoc; 10795 for (const OMPClause *C : Clauses) { 10796 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write || 10797 C->getClauseKind() == OMPC_update || 10798 C->getClauseKind() == OMPC_capture) { 10799 if (AtomicKind != OMPC_unknown) { 10800 Diag(C->getBeginLoc(), diag::err_omp_atomic_several_clauses) 10801 << SourceRange(C->getBeginLoc(), C->getEndLoc()); 10802 Diag(AtomicKindLoc, diag::note_omp_previous_mem_order_clause) 10803 << getOpenMPClauseName(AtomicKind); 10804 } else { 10805 AtomicKind = C->getClauseKind(); 10806 AtomicKindLoc = C->getBeginLoc(); 10807 } 10808 } 10809 if (C->getClauseKind() == OMPC_seq_cst || 10810 C->getClauseKind() == OMPC_acq_rel || 10811 C->getClauseKind() == OMPC_acquire || 10812 C->getClauseKind() == OMPC_release || 10813 C->getClauseKind() == OMPC_relaxed) { 10814 if (MemOrderKind != OMPC_unknown) { 10815 Diag(C->getBeginLoc(), diag::err_omp_several_mem_order_clauses) 10816 << getOpenMPDirectiveName(OMPD_atomic) << 0 10817 << SourceRange(C->getBeginLoc(), C->getEndLoc()); 10818 Diag(MemOrderLoc, diag::note_omp_previous_mem_order_clause) 10819 << getOpenMPClauseName(MemOrderKind); 10820 } else { 10821 MemOrderKind = C->getClauseKind(); 10822 MemOrderLoc = C->getBeginLoc(); 10823 } 10824 } 10825 } 10826 // OpenMP 5.0, 2.17.7 atomic Construct, Restrictions 10827 // If atomic-clause is read then memory-order-clause must not be acq_rel or 10828 // release. 10829 // If atomic-clause is write then memory-order-clause must not be acq_rel or 10830 // acquire. 10831 // If atomic-clause is update or not present then memory-order-clause must not 10832 // be acq_rel or acquire. 10833 if ((AtomicKind == OMPC_read && 10834 (MemOrderKind == OMPC_acq_rel || MemOrderKind == OMPC_release)) || 10835 ((AtomicKind == OMPC_write || AtomicKind == OMPC_update || 10836 AtomicKind == OMPC_unknown) && 10837 (MemOrderKind == OMPC_acq_rel || MemOrderKind == OMPC_acquire))) { 10838 SourceLocation Loc = AtomicKindLoc; 10839 if (AtomicKind == OMPC_unknown) 10840 Loc = StartLoc; 10841 Diag(Loc, diag::err_omp_atomic_incompatible_mem_order_clause) 10842 << getOpenMPClauseName(AtomicKind) 10843 << (AtomicKind == OMPC_unknown ? 1 : 0) 10844 << getOpenMPClauseName(MemOrderKind); 10845 Diag(MemOrderLoc, diag::note_omp_previous_mem_order_clause) 10846 << getOpenMPClauseName(MemOrderKind); 10847 } 10848 10849 Stmt *Body = AStmt; 10850 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body)) 10851 Body = EWC->getSubExpr(); 10852 10853 Expr *X = nullptr; 10854 Expr *V = nullptr; 10855 Expr *E = nullptr; 10856 Expr *UE = nullptr; 10857 bool IsXLHSInRHSPart = false; 10858 bool IsPostfixUpdate = false; 10859 // OpenMP [2.12.6, atomic Construct] 10860 // In the next expressions: 10861 // * x and v (as applicable) are both l-value expressions with scalar type. 10862 // * During the execution of an atomic region, multiple syntactic 10863 // occurrences of x must designate the same storage location. 10864 // * Neither of v and expr (as applicable) may access the storage location 10865 // designated by x. 10866 // * Neither of x and expr (as applicable) may access the storage location 10867 // designated by v. 10868 // * expr is an expression with scalar type. 10869 // * binop is one of +, *, -, /, &, ^, |, <<, or >>. 10870 // * binop, binop=, ++, and -- are not overloaded operators. 10871 // * The expression x binop expr must be numerically equivalent to x binop 10872 // (expr). This requirement is satisfied if the operators in expr have 10873 // precedence greater than binop, or by using parentheses around expr or 10874 // subexpressions of expr. 10875 // * The expression expr binop x must be numerically equivalent to (expr) 10876 // binop x. This requirement is satisfied if the operators in expr have 10877 // precedence equal to or greater than binop, or by using parentheses around 10878 // expr or subexpressions of expr. 10879 // * For forms that allow multiple occurrences of x, the number of times 10880 // that x is evaluated is unspecified. 10881 if (AtomicKind == OMPC_read) { 10882 enum { 10883 NotAnExpression, 10884 NotAnAssignmentOp, 10885 NotAScalarType, 10886 NotAnLValue, 10887 NoError 10888 } ErrorFound = NoError; 10889 SourceLocation ErrorLoc, NoteLoc; 10890 SourceRange ErrorRange, NoteRange; 10891 // If clause is read: 10892 // v = x; 10893 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 10894 const auto *AtomicBinOp = 10895 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 10896 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 10897 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); 10898 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts(); 10899 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && 10900 (V->isInstantiationDependent() || V->getType()->isScalarType())) { 10901 if (!X->isLValue() || !V->isLValue()) { 10902 const Expr *NotLValueExpr = X->isLValue() ? V : X; 10903 ErrorFound = NotAnLValue; 10904 ErrorLoc = AtomicBinOp->getExprLoc(); 10905 ErrorRange = AtomicBinOp->getSourceRange(); 10906 NoteLoc = NotLValueExpr->getExprLoc(); 10907 NoteRange = NotLValueExpr->getSourceRange(); 10908 } 10909 } else if (!X->isInstantiationDependent() || 10910 !V->isInstantiationDependent()) { 10911 const Expr *NotScalarExpr = 10912 (X->isInstantiationDependent() || X->getType()->isScalarType()) 10913 ? V 10914 : X; 10915 ErrorFound = NotAScalarType; 10916 ErrorLoc = AtomicBinOp->getExprLoc(); 10917 ErrorRange = AtomicBinOp->getSourceRange(); 10918 NoteLoc = NotScalarExpr->getExprLoc(); 10919 NoteRange = NotScalarExpr->getSourceRange(); 10920 } 10921 } else if (!AtomicBody->isInstantiationDependent()) { 10922 ErrorFound = NotAnAssignmentOp; 10923 ErrorLoc = AtomicBody->getExprLoc(); 10924 ErrorRange = AtomicBody->getSourceRange(); 10925 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 10926 : AtomicBody->getExprLoc(); 10927 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 10928 : AtomicBody->getSourceRange(); 10929 } 10930 } else { 10931 ErrorFound = NotAnExpression; 10932 NoteLoc = ErrorLoc = Body->getBeginLoc(); 10933 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 10934 } 10935 if (ErrorFound != NoError) { 10936 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement) 10937 << ErrorRange; 10938 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound 10939 << NoteRange; 10940 return StmtError(); 10941 } 10942 if (CurContext->isDependentContext()) 10943 V = X = nullptr; 10944 } else if (AtomicKind == OMPC_write) { 10945 enum { 10946 NotAnExpression, 10947 NotAnAssignmentOp, 10948 NotAScalarType, 10949 NotAnLValue, 10950 NoError 10951 } ErrorFound = NoError; 10952 SourceLocation ErrorLoc, NoteLoc; 10953 SourceRange ErrorRange, NoteRange; 10954 // If clause is write: 10955 // x = expr; 10956 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 10957 const auto *AtomicBinOp = 10958 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 10959 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 10960 X = AtomicBinOp->getLHS(); 10961 E = AtomicBinOp->getRHS(); 10962 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && 10963 (E->isInstantiationDependent() || E->getType()->isScalarType())) { 10964 if (!X->isLValue()) { 10965 ErrorFound = NotAnLValue; 10966 ErrorLoc = AtomicBinOp->getExprLoc(); 10967 ErrorRange = AtomicBinOp->getSourceRange(); 10968 NoteLoc = X->getExprLoc(); 10969 NoteRange = X->getSourceRange(); 10970 } 10971 } else if (!X->isInstantiationDependent() || 10972 !E->isInstantiationDependent()) { 10973 const Expr *NotScalarExpr = 10974 (X->isInstantiationDependent() || X->getType()->isScalarType()) 10975 ? E 10976 : X; 10977 ErrorFound = NotAScalarType; 10978 ErrorLoc = AtomicBinOp->getExprLoc(); 10979 ErrorRange = AtomicBinOp->getSourceRange(); 10980 NoteLoc = NotScalarExpr->getExprLoc(); 10981 NoteRange = NotScalarExpr->getSourceRange(); 10982 } 10983 } else if (!AtomicBody->isInstantiationDependent()) { 10984 ErrorFound = NotAnAssignmentOp; 10985 ErrorLoc = AtomicBody->getExprLoc(); 10986 ErrorRange = AtomicBody->getSourceRange(); 10987 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 10988 : AtomicBody->getExprLoc(); 10989 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 10990 : AtomicBody->getSourceRange(); 10991 } 10992 } else { 10993 ErrorFound = NotAnExpression; 10994 NoteLoc = ErrorLoc = Body->getBeginLoc(); 10995 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 10996 } 10997 if (ErrorFound != NoError) { 10998 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement) 10999 << ErrorRange; 11000 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound 11001 << NoteRange; 11002 return StmtError(); 11003 } 11004 if (CurContext->isDependentContext()) 11005 E = X = nullptr; 11006 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) { 11007 // If clause is update: 11008 // x++; 11009 // x--; 11010 // ++x; 11011 // --x; 11012 // x binop= expr; 11013 // x = x binop expr; 11014 // x = expr binop x; 11015 OpenMPAtomicUpdateChecker Checker(*this); 11016 if (Checker.checkStatement( 11017 Body, (AtomicKind == OMPC_update) 11018 ? diag::err_omp_atomic_update_not_expression_statement 11019 : diag::err_omp_atomic_not_expression_statement, 11020 diag::note_omp_atomic_update)) 11021 return StmtError(); 11022 if (!CurContext->isDependentContext()) { 11023 E = Checker.getExpr(); 11024 X = Checker.getX(); 11025 UE = Checker.getUpdateExpr(); 11026 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 11027 } 11028 } else if (AtomicKind == OMPC_capture) { 11029 enum { 11030 NotAnAssignmentOp, 11031 NotACompoundStatement, 11032 NotTwoSubstatements, 11033 NotASpecificExpression, 11034 NoError 11035 } ErrorFound = NoError; 11036 SourceLocation ErrorLoc, NoteLoc; 11037 SourceRange ErrorRange, NoteRange; 11038 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 11039 // If clause is a capture: 11040 // v = x++; 11041 // v = x--; 11042 // v = ++x; 11043 // v = --x; 11044 // v = x binop= expr; 11045 // v = x = x binop expr; 11046 // v = x = expr binop x; 11047 const auto *AtomicBinOp = 11048 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 11049 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 11050 V = AtomicBinOp->getLHS(); 11051 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); 11052 OpenMPAtomicUpdateChecker Checker(*this); 11053 if (Checker.checkStatement( 11054 Body, diag::err_omp_atomic_capture_not_expression_statement, 11055 diag::note_omp_atomic_update)) 11056 return StmtError(); 11057 E = Checker.getExpr(); 11058 X = Checker.getX(); 11059 UE = Checker.getUpdateExpr(); 11060 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 11061 IsPostfixUpdate = Checker.isPostfixUpdate(); 11062 } else if (!AtomicBody->isInstantiationDependent()) { 11063 ErrorLoc = AtomicBody->getExprLoc(); 11064 ErrorRange = AtomicBody->getSourceRange(); 11065 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 11066 : AtomicBody->getExprLoc(); 11067 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 11068 : AtomicBody->getSourceRange(); 11069 ErrorFound = NotAnAssignmentOp; 11070 } 11071 if (ErrorFound != NoError) { 11072 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement) 11073 << ErrorRange; 11074 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; 11075 return StmtError(); 11076 } 11077 if (CurContext->isDependentContext()) 11078 UE = V = E = X = nullptr; 11079 } else { 11080 // If clause is a capture: 11081 // { v = x; x = expr; } 11082 // { v = x; x++; } 11083 // { v = x; x--; } 11084 // { v = x; ++x; } 11085 // { v = x; --x; } 11086 // { v = x; x binop= expr; } 11087 // { v = x; x = x binop expr; } 11088 // { v = x; x = expr binop x; } 11089 // { x++; v = x; } 11090 // { x--; v = x; } 11091 // { ++x; v = x; } 11092 // { --x; v = x; } 11093 // { x binop= expr; v = x; } 11094 // { x = x binop expr; v = x; } 11095 // { x = expr binop x; v = x; } 11096 if (auto *CS = dyn_cast<CompoundStmt>(Body)) { 11097 // Check that this is { expr1; expr2; } 11098 if (CS->size() == 2) { 11099 Stmt *First = CS->body_front(); 11100 Stmt *Second = CS->body_back(); 11101 if (auto *EWC = dyn_cast<ExprWithCleanups>(First)) 11102 First = EWC->getSubExpr()->IgnoreParenImpCasts(); 11103 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second)) 11104 Second = EWC->getSubExpr()->IgnoreParenImpCasts(); 11105 // Need to find what subexpression is 'v' and what is 'x'. 11106 OpenMPAtomicUpdateChecker Checker(*this); 11107 bool IsUpdateExprFound = !Checker.checkStatement(Second); 11108 BinaryOperator *BinOp = nullptr; 11109 if (IsUpdateExprFound) { 11110 BinOp = dyn_cast<BinaryOperator>(First); 11111 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; 11112 } 11113 if (IsUpdateExprFound && !CurContext->isDependentContext()) { 11114 // { v = x; x++; } 11115 // { v = x; x--; } 11116 // { v = x; ++x; } 11117 // { v = x; --x; } 11118 // { v = x; x binop= expr; } 11119 // { v = x; x = x binop expr; } 11120 // { v = x; x = expr binop x; } 11121 // Check that the first expression has form v = x. 11122 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); 11123 llvm::FoldingSetNodeID XId, PossibleXId; 11124 Checker.getX()->Profile(XId, Context, /*Canonical=*/true); 11125 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); 11126 IsUpdateExprFound = XId == PossibleXId; 11127 if (IsUpdateExprFound) { 11128 V = BinOp->getLHS(); 11129 X = Checker.getX(); 11130 E = Checker.getExpr(); 11131 UE = Checker.getUpdateExpr(); 11132 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 11133 IsPostfixUpdate = true; 11134 } 11135 } 11136 if (!IsUpdateExprFound) { 11137 IsUpdateExprFound = !Checker.checkStatement(First); 11138 BinOp = nullptr; 11139 if (IsUpdateExprFound) { 11140 BinOp = dyn_cast<BinaryOperator>(Second); 11141 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; 11142 } 11143 if (IsUpdateExprFound && !CurContext->isDependentContext()) { 11144 // { x++; v = x; } 11145 // { x--; v = x; } 11146 // { ++x; v = x; } 11147 // { --x; v = x; } 11148 // { x binop= expr; v = x; } 11149 // { x = x binop expr; v = x; } 11150 // { x = expr binop x; v = x; } 11151 // Check that the second expression has form v = x. 11152 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); 11153 llvm::FoldingSetNodeID XId, PossibleXId; 11154 Checker.getX()->Profile(XId, Context, /*Canonical=*/true); 11155 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); 11156 IsUpdateExprFound = XId == PossibleXId; 11157 if (IsUpdateExprFound) { 11158 V = BinOp->getLHS(); 11159 X = Checker.getX(); 11160 E = Checker.getExpr(); 11161 UE = Checker.getUpdateExpr(); 11162 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 11163 IsPostfixUpdate = false; 11164 } 11165 } 11166 } 11167 if (!IsUpdateExprFound) { 11168 // { v = x; x = expr; } 11169 auto *FirstExpr = dyn_cast<Expr>(First); 11170 auto *SecondExpr = dyn_cast<Expr>(Second); 11171 if (!FirstExpr || !SecondExpr || 11172 !(FirstExpr->isInstantiationDependent() || 11173 SecondExpr->isInstantiationDependent())) { 11174 auto *FirstBinOp = dyn_cast<BinaryOperator>(First); 11175 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) { 11176 ErrorFound = NotAnAssignmentOp; 11177 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc() 11178 : First->getBeginLoc(); 11179 NoteRange = ErrorRange = FirstBinOp 11180 ? FirstBinOp->getSourceRange() 11181 : SourceRange(ErrorLoc, ErrorLoc); 11182 } else { 11183 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second); 11184 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) { 11185 ErrorFound = NotAnAssignmentOp; 11186 NoteLoc = ErrorLoc = SecondBinOp 11187 ? SecondBinOp->getOperatorLoc() 11188 : Second->getBeginLoc(); 11189 NoteRange = ErrorRange = 11190 SecondBinOp ? SecondBinOp->getSourceRange() 11191 : SourceRange(ErrorLoc, ErrorLoc); 11192 } else { 11193 Expr *PossibleXRHSInFirst = 11194 FirstBinOp->getRHS()->IgnoreParenImpCasts(); 11195 Expr *PossibleXLHSInSecond = 11196 SecondBinOp->getLHS()->IgnoreParenImpCasts(); 11197 llvm::FoldingSetNodeID X1Id, X2Id; 11198 PossibleXRHSInFirst->Profile(X1Id, Context, 11199 /*Canonical=*/true); 11200 PossibleXLHSInSecond->Profile(X2Id, Context, 11201 /*Canonical=*/true); 11202 IsUpdateExprFound = X1Id == X2Id; 11203 if (IsUpdateExprFound) { 11204 V = FirstBinOp->getLHS(); 11205 X = SecondBinOp->getLHS(); 11206 E = SecondBinOp->getRHS(); 11207 UE = nullptr; 11208 IsXLHSInRHSPart = false; 11209 IsPostfixUpdate = true; 11210 } else { 11211 ErrorFound = NotASpecificExpression; 11212 ErrorLoc = FirstBinOp->getExprLoc(); 11213 ErrorRange = FirstBinOp->getSourceRange(); 11214 NoteLoc = SecondBinOp->getLHS()->getExprLoc(); 11215 NoteRange = SecondBinOp->getRHS()->getSourceRange(); 11216 } 11217 } 11218 } 11219 } 11220 } 11221 } else { 11222 NoteLoc = ErrorLoc = Body->getBeginLoc(); 11223 NoteRange = ErrorRange = 11224 SourceRange(Body->getBeginLoc(), Body->getBeginLoc()); 11225 ErrorFound = NotTwoSubstatements; 11226 } 11227 } else { 11228 NoteLoc = ErrorLoc = Body->getBeginLoc(); 11229 NoteRange = ErrorRange = 11230 SourceRange(Body->getBeginLoc(), Body->getBeginLoc()); 11231 ErrorFound = NotACompoundStatement; 11232 } 11233 if (ErrorFound != NoError) { 11234 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement) 11235 << ErrorRange; 11236 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; 11237 return StmtError(); 11238 } 11239 if (CurContext->isDependentContext()) 11240 UE = V = E = X = nullptr; 11241 } 11242 } 11243 11244 setFunctionHasBranchProtectedScope(); 11245 11246 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 11247 X, V, E, UE, IsXLHSInRHSPart, 11248 IsPostfixUpdate); 11249 } 11250 11251 StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses, 11252 Stmt *AStmt, 11253 SourceLocation StartLoc, 11254 SourceLocation EndLoc) { 11255 if (!AStmt) 11256 return StmtError(); 11257 11258 auto *CS = cast<CapturedStmt>(AStmt); 11259 // 1.2.2 OpenMP Language Terminology 11260 // Structured block - An executable statement with a single entry at the 11261 // top and a single exit at the bottom. 11262 // The point of exit cannot be a branch out of the structured block. 11263 // longjmp() and throw() must not violate the entry/exit criteria. 11264 CS->getCapturedDecl()->setNothrow(); 11265 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target); 11266 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11267 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11268 // 1.2.2 OpenMP Language Terminology 11269 // Structured block - An executable statement with a single entry at the 11270 // top and a single exit at the bottom. 11271 // The point of exit cannot be a branch out of the structured block. 11272 // longjmp() and throw() must not violate the entry/exit criteria. 11273 CS->getCapturedDecl()->setNothrow(); 11274 } 11275 11276 // OpenMP [2.16, Nesting of Regions] 11277 // If specified, a teams construct must be contained within a target 11278 // construct. That target construct must contain no statements or directives 11279 // outside of the teams construct. 11280 if (DSAStack->hasInnerTeamsRegion()) { 11281 const Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true); 11282 bool OMPTeamsFound = true; 11283 if (const auto *CS = dyn_cast<CompoundStmt>(S)) { 11284 auto I = CS->body_begin(); 11285 while (I != CS->body_end()) { 11286 const auto *OED = dyn_cast<OMPExecutableDirective>(*I); 11287 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind()) || 11288 OMPTeamsFound) { 11289 11290 OMPTeamsFound = false; 11291 break; 11292 } 11293 ++I; 11294 } 11295 assert(I != CS->body_end() && "Not found statement"); 11296 S = *I; 11297 } else { 11298 const auto *OED = dyn_cast<OMPExecutableDirective>(S); 11299 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind()); 11300 } 11301 if (!OMPTeamsFound) { 11302 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams); 11303 Diag(DSAStack->getInnerTeamsRegionLoc(), 11304 diag::note_omp_nested_teams_construct_here); 11305 Diag(S->getBeginLoc(), diag::note_omp_nested_statement_here) 11306 << isa<OMPExecutableDirective>(S); 11307 return StmtError(); 11308 } 11309 } 11310 11311 setFunctionHasBranchProtectedScope(); 11312 11313 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 11314 } 11315 11316 StmtResult 11317 Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses, 11318 Stmt *AStmt, SourceLocation StartLoc, 11319 SourceLocation EndLoc) { 11320 if (!AStmt) 11321 return StmtError(); 11322 11323 auto *CS = cast<CapturedStmt>(AStmt); 11324 // 1.2.2 OpenMP Language Terminology 11325 // Structured block - An executable statement with a single entry at the 11326 // top and a single exit at the bottom. 11327 // The point of exit cannot be a branch out of the structured block. 11328 // longjmp() and throw() must not violate the entry/exit criteria. 11329 CS->getCapturedDecl()->setNothrow(); 11330 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel); 11331 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11332 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11333 // 1.2.2 OpenMP Language Terminology 11334 // Structured block - An executable statement with a single entry at the 11335 // top and a single exit at the bottom. 11336 // The point of exit cannot be a branch out of the structured block. 11337 // longjmp() and throw() must not violate the entry/exit criteria. 11338 CS->getCapturedDecl()->setNothrow(); 11339 } 11340 11341 setFunctionHasBranchProtectedScope(); 11342 11343 return OMPTargetParallelDirective::Create( 11344 Context, StartLoc, EndLoc, Clauses, AStmt, 11345 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 11346 } 11347 11348 StmtResult Sema::ActOnOpenMPTargetParallelForDirective( 11349 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11350 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11351 if (!AStmt) 11352 return StmtError(); 11353 11354 auto *CS = cast<CapturedStmt>(AStmt); 11355 // 1.2.2 OpenMP Language Terminology 11356 // Structured block - An executable statement with a single entry at the 11357 // top and a single exit at the bottom. 11358 // The point of exit cannot be a branch out of the structured block. 11359 // longjmp() and throw() must not violate the entry/exit criteria. 11360 CS->getCapturedDecl()->setNothrow(); 11361 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for); 11362 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11363 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11364 // 1.2.2 OpenMP Language Terminology 11365 // Structured block - An executable statement with a single entry at the 11366 // top and a single exit at the bottom. 11367 // The point of exit cannot be a branch out of the structured block. 11368 // longjmp() and throw() must not violate the entry/exit criteria. 11369 CS->getCapturedDecl()->setNothrow(); 11370 } 11371 11372 OMPLoopBasedDirective::HelperExprs B; 11373 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11374 // define the nested loops number. 11375 unsigned NestedLoopCount = 11376 checkOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses), 11377 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 11378 VarsWithImplicitDSA, B); 11379 if (NestedLoopCount == 0) 11380 return StmtError(); 11381 11382 assert((CurContext->isDependentContext() || B.builtAll()) && 11383 "omp target parallel for loop exprs were not built"); 11384 11385 if (!CurContext->isDependentContext()) { 11386 // Finalize the clauses that need pre-built expressions for CodeGen. 11387 for (OMPClause *C : Clauses) { 11388 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11389 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11390 B.NumIterations, *this, CurScope, 11391 DSAStack)) 11392 return StmtError(); 11393 } 11394 } 11395 11396 setFunctionHasBranchProtectedScope(); 11397 return OMPTargetParallelForDirective::Create( 11398 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 11399 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 11400 } 11401 11402 /// Check for existence of a map clause in the list of clauses. 11403 static bool hasClauses(ArrayRef<OMPClause *> Clauses, 11404 const OpenMPClauseKind K) { 11405 return llvm::any_of( 11406 Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; }); 11407 } 11408 11409 template <typename... Params> 11410 static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K, 11411 const Params... ClauseTypes) { 11412 return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...); 11413 } 11414 11415 StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses, 11416 Stmt *AStmt, 11417 SourceLocation StartLoc, 11418 SourceLocation EndLoc) { 11419 if (!AStmt) 11420 return StmtError(); 11421 11422 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11423 11424 // OpenMP [2.12.2, target data Construct, Restrictions] 11425 // At least one map, use_device_addr or use_device_ptr clause must appear on 11426 // the directive. 11427 if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr) && 11428 (LangOpts.OpenMP < 50 || !hasClauses(Clauses, OMPC_use_device_addr))) { 11429 StringRef Expected; 11430 if (LangOpts.OpenMP < 50) 11431 Expected = "'map' or 'use_device_ptr'"; 11432 else 11433 Expected = "'map', 'use_device_ptr', or 'use_device_addr'"; 11434 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 11435 << Expected << getOpenMPDirectiveName(OMPD_target_data); 11436 return StmtError(); 11437 } 11438 11439 setFunctionHasBranchProtectedScope(); 11440 11441 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 11442 AStmt); 11443 } 11444 11445 StmtResult 11446 Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses, 11447 SourceLocation StartLoc, 11448 SourceLocation EndLoc, Stmt *AStmt) { 11449 if (!AStmt) 11450 return StmtError(); 11451 11452 auto *CS = cast<CapturedStmt>(AStmt); 11453 // 1.2.2 OpenMP Language Terminology 11454 // Structured block - An executable statement with a single entry at the 11455 // top and a single exit at the bottom. 11456 // The point of exit cannot be a branch out of the structured block. 11457 // longjmp() and throw() must not violate the entry/exit criteria. 11458 CS->getCapturedDecl()->setNothrow(); 11459 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_enter_data); 11460 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11461 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11462 // 1.2.2 OpenMP Language Terminology 11463 // Structured block - An executable statement with a single entry at the 11464 // top and a single exit at the bottom. 11465 // The point of exit cannot be a branch out of the structured block. 11466 // longjmp() and throw() must not violate the entry/exit criteria. 11467 CS->getCapturedDecl()->setNothrow(); 11468 } 11469 11470 // OpenMP [2.10.2, Restrictions, p. 99] 11471 // At least one map clause must appear on the directive. 11472 if (!hasClauses(Clauses, OMPC_map)) { 11473 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 11474 << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data); 11475 return StmtError(); 11476 } 11477 11478 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 11479 AStmt); 11480 } 11481 11482 StmtResult 11483 Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses, 11484 SourceLocation StartLoc, 11485 SourceLocation EndLoc, Stmt *AStmt) { 11486 if (!AStmt) 11487 return StmtError(); 11488 11489 auto *CS = cast<CapturedStmt>(AStmt); 11490 // 1.2.2 OpenMP Language Terminology 11491 // Structured block - An executable statement with a single entry at the 11492 // top and a single exit at the bottom. 11493 // The point of exit cannot be a branch out of the structured block. 11494 // longjmp() and throw() must not violate the entry/exit criteria. 11495 CS->getCapturedDecl()->setNothrow(); 11496 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_exit_data); 11497 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11498 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11499 // 1.2.2 OpenMP Language Terminology 11500 // Structured block - An executable statement with a single entry at the 11501 // top and a single exit at the bottom. 11502 // The point of exit cannot be a branch out of the structured block. 11503 // longjmp() and throw() must not violate the entry/exit criteria. 11504 CS->getCapturedDecl()->setNothrow(); 11505 } 11506 11507 // OpenMP [2.10.3, Restrictions, p. 102] 11508 // At least one map clause must appear on the directive. 11509 if (!hasClauses(Clauses, OMPC_map)) { 11510 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 11511 << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data); 11512 return StmtError(); 11513 } 11514 11515 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 11516 AStmt); 11517 } 11518 11519 StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses, 11520 SourceLocation StartLoc, 11521 SourceLocation EndLoc, 11522 Stmt *AStmt) { 11523 if (!AStmt) 11524 return StmtError(); 11525 11526 auto *CS = cast<CapturedStmt>(AStmt); 11527 // 1.2.2 OpenMP Language Terminology 11528 // Structured block - An executable statement with a single entry at the 11529 // top and a single exit at the bottom. 11530 // The point of exit cannot be a branch out of the structured block. 11531 // longjmp() and throw() must not violate the entry/exit criteria. 11532 CS->getCapturedDecl()->setNothrow(); 11533 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_update); 11534 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11535 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11536 // 1.2.2 OpenMP Language Terminology 11537 // Structured block - An executable statement with a single entry at the 11538 // top and a single exit at the bottom. 11539 // The point of exit cannot be a branch out of the structured block. 11540 // longjmp() and throw() must not violate the entry/exit criteria. 11541 CS->getCapturedDecl()->setNothrow(); 11542 } 11543 11544 if (!hasClauses(Clauses, OMPC_to, OMPC_from)) { 11545 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required); 11546 return StmtError(); 11547 } 11548 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses, 11549 AStmt); 11550 } 11551 11552 StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses, 11553 Stmt *AStmt, SourceLocation StartLoc, 11554 SourceLocation EndLoc) { 11555 if (!AStmt) 11556 return StmtError(); 11557 11558 auto *CS = cast<CapturedStmt>(AStmt); 11559 // 1.2.2 OpenMP Language Terminology 11560 // Structured block - An executable statement with a single entry at the 11561 // top and a single exit at the bottom. 11562 // The point of exit cannot be a branch out of the structured block. 11563 // longjmp() and throw() must not violate the entry/exit criteria. 11564 CS->getCapturedDecl()->setNothrow(); 11565 11566 setFunctionHasBranchProtectedScope(); 11567 11568 DSAStack->setParentTeamsRegionLoc(StartLoc); 11569 11570 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 11571 } 11572 11573 StmtResult 11574 Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc, 11575 SourceLocation EndLoc, 11576 OpenMPDirectiveKind CancelRegion) { 11577 if (DSAStack->isParentNowaitRegion()) { 11578 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0; 11579 return StmtError(); 11580 } 11581 if (DSAStack->isParentOrderedRegion()) { 11582 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0; 11583 return StmtError(); 11584 } 11585 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc, 11586 CancelRegion); 11587 } 11588 11589 StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses, 11590 SourceLocation StartLoc, 11591 SourceLocation EndLoc, 11592 OpenMPDirectiveKind CancelRegion) { 11593 if (DSAStack->isParentNowaitRegion()) { 11594 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1; 11595 return StmtError(); 11596 } 11597 if (DSAStack->isParentOrderedRegion()) { 11598 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1; 11599 return StmtError(); 11600 } 11601 DSAStack->setParentCancelRegion(/*Cancel=*/true); 11602 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses, 11603 CancelRegion); 11604 } 11605 11606 static bool checkReductionClauseWithNogroup(Sema &S, 11607 ArrayRef<OMPClause *> Clauses) { 11608 const OMPClause *ReductionClause = nullptr; 11609 const OMPClause *NogroupClause = nullptr; 11610 for (const OMPClause *C : Clauses) { 11611 if (C->getClauseKind() == OMPC_reduction) { 11612 ReductionClause = C; 11613 if (NogroupClause) 11614 break; 11615 continue; 11616 } 11617 if (C->getClauseKind() == OMPC_nogroup) { 11618 NogroupClause = C; 11619 if (ReductionClause) 11620 break; 11621 continue; 11622 } 11623 } 11624 if (ReductionClause && NogroupClause) { 11625 S.Diag(ReductionClause->getBeginLoc(), diag::err_omp_reduction_with_nogroup) 11626 << SourceRange(NogroupClause->getBeginLoc(), 11627 NogroupClause->getEndLoc()); 11628 return true; 11629 } 11630 return false; 11631 } 11632 11633 StmtResult Sema::ActOnOpenMPTaskLoopDirective( 11634 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11635 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11636 if (!AStmt) 11637 return StmtError(); 11638 11639 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11640 OMPLoopBasedDirective::HelperExprs B; 11641 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11642 // define the nested loops number. 11643 unsigned NestedLoopCount = 11644 checkOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses), 11645 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 11646 VarsWithImplicitDSA, B); 11647 if (NestedLoopCount == 0) 11648 return StmtError(); 11649 11650 assert((CurContext->isDependentContext() || B.builtAll()) && 11651 "omp for loop exprs were not built"); 11652 11653 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11654 // The grainsize clause and num_tasks clause are mutually exclusive and may 11655 // not appear on the same taskloop directive. 11656 if (checkMutuallyExclusiveClauses(*this, Clauses, 11657 {OMPC_grainsize, OMPC_num_tasks})) 11658 return StmtError(); 11659 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11660 // If a reduction clause is present on the taskloop directive, the nogroup 11661 // clause must not be specified. 11662 if (checkReductionClauseWithNogroup(*this, Clauses)) 11663 return StmtError(); 11664 11665 setFunctionHasBranchProtectedScope(); 11666 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc, 11667 NestedLoopCount, Clauses, AStmt, B, 11668 DSAStack->isCancelRegion()); 11669 } 11670 11671 StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective( 11672 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11673 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11674 if (!AStmt) 11675 return StmtError(); 11676 11677 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11678 OMPLoopBasedDirective::HelperExprs B; 11679 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11680 // define the nested loops number. 11681 unsigned NestedLoopCount = 11682 checkOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses), 11683 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 11684 VarsWithImplicitDSA, B); 11685 if (NestedLoopCount == 0) 11686 return StmtError(); 11687 11688 assert((CurContext->isDependentContext() || B.builtAll()) && 11689 "omp for loop exprs were not built"); 11690 11691 if (!CurContext->isDependentContext()) { 11692 // Finalize the clauses that need pre-built expressions for CodeGen. 11693 for (OMPClause *C : Clauses) { 11694 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11695 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11696 B.NumIterations, *this, CurScope, 11697 DSAStack)) 11698 return StmtError(); 11699 } 11700 } 11701 11702 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11703 // The grainsize clause and num_tasks clause are mutually exclusive and may 11704 // not appear on the same taskloop directive. 11705 if (checkMutuallyExclusiveClauses(*this, Clauses, 11706 {OMPC_grainsize, OMPC_num_tasks})) 11707 return StmtError(); 11708 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11709 // If a reduction clause is present on the taskloop directive, the nogroup 11710 // clause must not be specified. 11711 if (checkReductionClauseWithNogroup(*this, Clauses)) 11712 return StmtError(); 11713 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11714 return StmtError(); 11715 11716 setFunctionHasBranchProtectedScope(); 11717 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc, 11718 NestedLoopCount, Clauses, AStmt, B); 11719 } 11720 11721 StmtResult Sema::ActOnOpenMPMasterTaskLoopDirective( 11722 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11723 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11724 if (!AStmt) 11725 return StmtError(); 11726 11727 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11728 OMPLoopBasedDirective::HelperExprs B; 11729 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11730 // define the nested loops number. 11731 unsigned NestedLoopCount = 11732 checkOpenMPLoop(OMPD_master_taskloop, getCollapseNumberExpr(Clauses), 11733 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 11734 VarsWithImplicitDSA, B); 11735 if (NestedLoopCount == 0) 11736 return StmtError(); 11737 11738 assert((CurContext->isDependentContext() || B.builtAll()) && 11739 "omp for loop exprs were not built"); 11740 11741 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11742 // The grainsize clause and num_tasks clause are mutually exclusive and may 11743 // not appear on the same taskloop directive. 11744 if (checkMutuallyExclusiveClauses(*this, Clauses, 11745 {OMPC_grainsize, OMPC_num_tasks})) 11746 return StmtError(); 11747 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11748 // If a reduction clause is present on the taskloop directive, the nogroup 11749 // clause must not be specified. 11750 if (checkReductionClauseWithNogroup(*this, Clauses)) 11751 return StmtError(); 11752 11753 setFunctionHasBranchProtectedScope(); 11754 return OMPMasterTaskLoopDirective::Create(Context, StartLoc, EndLoc, 11755 NestedLoopCount, Clauses, AStmt, B, 11756 DSAStack->isCancelRegion()); 11757 } 11758 11759 StmtResult Sema::ActOnOpenMPMasterTaskLoopSimdDirective( 11760 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11761 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11762 if (!AStmt) 11763 return StmtError(); 11764 11765 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11766 OMPLoopBasedDirective::HelperExprs B; 11767 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11768 // define the nested loops number. 11769 unsigned NestedLoopCount = 11770 checkOpenMPLoop(OMPD_master_taskloop_simd, getCollapseNumberExpr(Clauses), 11771 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 11772 VarsWithImplicitDSA, B); 11773 if (NestedLoopCount == 0) 11774 return StmtError(); 11775 11776 assert((CurContext->isDependentContext() || B.builtAll()) && 11777 "omp for loop exprs were not built"); 11778 11779 if (!CurContext->isDependentContext()) { 11780 // Finalize the clauses that need pre-built expressions for CodeGen. 11781 for (OMPClause *C : Clauses) { 11782 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11783 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11784 B.NumIterations, *this, CurScope, 11785 DSAStack)) 11786 return StmtError(); 11787 } 11788 } 11789 11790 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11791 // The grainsize clause and num_tasks clause are mutually exclusive and may 11792 // not appear on the same taskloop directive. 11793 if (checkMutuallyExclusiveClauses(*this, Clauses, 11794 {OMPC_grainsize, OMPC_num_tasks})) 11795 return StmtError(); 11796 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11797 // If a reduction clause is present on the taskloop directive, the nogroup 11798 // clause must not be specified. 11799 if (checkReductionClauseWithNogroup(*this, Clauses)) 11800 return StmtError(); 11801 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11802 return StmtError(); 11803 11804 setFunctionHasBranchProtectedScope(); 11805 return OMPMasterTaskLoopSimdDirective::Create( 11806 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 11807 } 11808 11809 StmtResult Sema::ActOnOpenMPParallelMasterTaskLoopDirective( 11810 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11811 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11812 if (!AStmt) 11813 return StmtError(); 11814 11815 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11816 auto *CS = cast<CapturedStmt>(AStmt); 11817 // 1.2.2 OpenMP Language Terminology 11818 // Structured block - An executable statement with a single entry at the 11819 // top and a single exit at the bottom. 11820 // The point of exit cannot be a branch out of the structured block. 11821 // longjmp() and throw() must not violate the entry/exit criteria. 11822 CS->getCapturedDecl()->setNothrow(); 11823 for (int ThisCaptureLevel = 11824 getOpenMPCaptureLevels(OMPD_parallel_master_taskloop); 11825 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11826 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11827 // 1.2.2 OpenMP Language Terminology 11828 // Structured block - An executable statement with a single entry at the 11829 // top and a single exit at the bottom. 11830 // The point of exit cannot be a branch out of the structured block. 11831 // longjmp() and throw() must not violate the entry/exit criteria. 11832 CS->getCapturedDecl()->setNothrow(); 11833 } 11834 11835 OMPLoopBasedDirective::HelperExprs B; 11836 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11837 // define the nested loops number. 11838 unsigned NestedLoopCount = checkOpenMPLoop( 11839 OMPD_parallel_master_taskloop, getCollapseNumberExpr(Clauses), 11840 /*OrderedLoopCountExpr=*/nullptr, CS, *this, *DSAStack, 11841 VarsWithImplicitDSA, B); 11842 if (NestedLoopCount == 0) 11843 return StmtError(); 11844 11845 assert((CurContext->isDependentContext() || B.builtAll()) && 11846 "omp for loop exprs were not built"); 11847 11848 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11849 // The grainsize clause and num_tasks clause are mutually exclusive and may 11850 // not appear on the same taskloop directive. 11851 if (checkMutuallyExclusiveClauses(*this, Clauses, 11852 {OMPC_grainsize, OMPC_num_tasks})) 11853 return StmtError(); 11854 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11855 // If a reduction clause is present on the taskloop directive, the nogroup 11856 // clause must not be specified. 11857 if (checkReductionClauseWithNogroup(*this, Clauses)) 11858 return StmtError(); 11859 11860 setFunctionHasBranchProtectedScope(); 11861 return OMPParallelMasterTaskLoopDirective::Create( 11862 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 11863 DSAStack->isCancelRegion()); 11864 } 11865 11866 StmtResult Sema::ActOnOpenMPParallelMasterTaskLoopSimdDirective( 11867 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11868 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11869 if (!AStmt) 11870 return StmtError(); 11871 11872 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11873 auto *CS = cast<CapturedStmt>(AStmt); 11874 // 1.2.2 OpenMP Language Terminology 11875 // Structured block - An executable statement with a single entry at the 11876 // top and a single exit at the bottom. 11877 // The point of exit cannot be a branch out of the structured block. 11878 // longjmp() and throw() must not violate the entry/exit criteria. 11879 CS->getCapturedDecl()->setNothrow(); 11880 for (int ThisCaptureLevel = 11881 getOpenMPCaptureLevels(OMPD_parallel_master_taskloop_simd); 11882 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11883 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11884 // 1.2.2 OpenMP Language Terminology 11885 // Structured block - An executable statement with a single entry at the 11886 // top and a single exit at the bottom. 11887 // The point of exit cannot be a branch out of the structured block. 11888 // longjmp() and throw() must not violate the entry/exit criteria. 11889 CS->getCapturedDecl()->setNothrow(); 11890 } 11891 11892 OMPLoopBasedDirective::HelperExprs B; 11893 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11894 // define the nested loops number. 11895 unsigned NestedLoopCount = checkOpenMPLoop( 11896 OMPD_parallel_master_taskloop_simd, getCollapseNumberExpr(Clauses), 11897 /*OrderedLoopCountExpr=*/nullptr, CS, *this, *DSAStack, 11898 VarsWithImplicitDSA, B); 11899 if (NestedLoopCount == 0) 11900 return StmtError(); 11901 11902 assert((CurContext->isDependentContext() || B.builtAll()) && 11903 "omp for loop exprs were not built"); 11904 11905 if (!CurContext->isDependentContext()) { 11906 // Finalize the clauses that need pre-built expressions for CodeGen. 11907 for (OMPClause *C : Clauses) { 11908 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11909 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11910 B.NumIterations, *this, CurScope, 11911 DSAStack)) 11912 return StmtError(); 11913 } 11914 } 11915 11916 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11917 // The grainsize clause and num_tasks clause are mutually exclusive and may 11918 // not appear on the same taskloop directive. 11919 if (checkMutuallyExclusiveClauses(*this, Clauses, 11920 {OMPC_grainsize, OMPC_num_tasks})) 11921 return StmtError(); 11922 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11923 // If a reduction clause is present on the taskloop directive, the nogroup 11924 // clause must not be specified. 11925 if (checkReductionClauseWithNogroup(*this, Clauses)) 11926 return StmtError(); 11927 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11928 return StmtError(); 11929 11930 setFunctionHasBranchProtectedScope(); 11931 return OMPParallelMasterTaskLoopSimdDirective::Create( 11932 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 11933 } 11934 11935 StmtResult Sema::ActOnOpenMPDistributeDirective( 11936 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11937 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11938 if (!AStmt) 11939 return StmtError(); 11940 11941 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11942 OMPLoopBasedDirective::HelperExprs B; 11943 // In presence of clause 'collapse' with number of loops, it will 11944 // define the nested loops number. 11945 unsigned NestedLoopCount = 11946 checkOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses), 11947 nullptr /*ordered not a clause on distribute*/, AStmt, 11948 *this, *DSAStack, VarsWithImplicitDSA, B); 11949 if (NestedLoopCount == 0) 11950 return StmtError(); 11951 11952 assert((CurContext->isDependentContext() || B.builtAll()) && 11953 "omp for loop exprs were not built"); 11954 11955 setFunctionHasBranchProtectedScope(); 11956 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc, 11957 NestedLoopCount, Clauses, AStmt, B); 11958 } 11959 11960 StmtResult Sema::ActOnOpenMPDistributeParallelForDirective( 11961 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11962 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11963 if (!AStmt) 11964 return StmtError(); 11965 11966 auto *CS = cast<CapturedStmt>(AStmt); 11967 // 1.2.2 OpenMP Language Terminology 11968 // Structured block - An executable statement with a single entry at the 11969 // top and a single exit at the bottom. 11970 // The point of exit cannot be a branch out of the structured block. 11971 // longjmp() and throw() must not violate the entry/exit criteria. 11972 CS->getCapturedDecl()->setNothrow(); 11973 for (int ThisCaptureLevel = 11974 getOpenMPCaptureLevels(OMPD_distribute_parallel_for); 11975 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11976 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11977 // 1.2.2 OpenMP Language Terminology 11978 // Structured block - An executable statement with a single entry at the 11979 // top and a single exit at the bottom. 11980 // The point of exit cannot be a branch out of the structured block. 11981 // longjmp() and throw() must not violate the entry/exit criteria. 11982 CS->getCapturedDecl()->setNothrow(); 11983 } 11984 11985 OMPLoopBasedDirective::HelperExprs B; 11986 // In presence of clause 'collapse' with number of loops, it will 11987 // define the nested loops number. 11988 unsigned NestedLoopCount = checkOpenMPLoop( 11989 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses), 11990 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 11991 VarsWithImplicitDSA, B); 11992 if (NestedLoopCount == 0) 11993 return StmtError(); 11994 11995 assert((CurContext->isDependentContext() || B.builtAll()) && 11996 "omp for loop exprs were not built"); 11997 11998 setFunctionHasBranchProtectedScope(); 11999 return OMPDistributeParallelForDirective::Create( 12000 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 12001 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 12002 } 12003 12004 StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective( 12005 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12006 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12007 if (!AStmt) 12008 return StmtError(); 12009 12010 auto *CS = cast<CapturedStmt>(AStmt); 12011 // 1.2.2 OpenMP Language Terminology 12012 // Structured block - An executable statement with a single entry at the 12013 // top and a single exit at the bottom. 12014 // The point of exit cannot be a branch out of the structured block. 12015 // longjmp() and throw() must not violate the entry/exit criteria. 12016 CS->getCapturedDecl()->setNothrow(); 12017 for (int ThisCaptureLevel = 12018 getOpenMPCaptureLevels(OMPD_distribute_parallel_for_simd); 12019 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12020 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12021 // 1.2.2 OpenMP Language Terminology 12022 // Structured block - An executable statement with a single entry at the 12023 // top and a single exit at the bottom. 12024 // The point of exit cannot be a branch out of the structured block. 12025 // longjmp() and throw() must not violate the entry/exit criteria. 12026 CS->getCapturedDecl()->setNothrow(); 12027 } 12028 12029 OMPLoopBasedDirective::HelperExprs B; 12030 // In presence of clause 'collapse' with number of loops, it will 12031 // define the nested loops number. 12032 unsigned NestedLoopCount = checkOpenMPLoop( 12033 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses), 12034 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 12035 VarsWithImplicitDSA, B); 12036 if (NestedLoopCount == 0) 12037 return StmtError(); 12038 12039 assert((CurContext->isDependentContext() || B.builtAll()) && 12040 "omp for loop exprs were not built"); 12041 12042 if (!CurContext->isDependentContext()) { 12043 // Finalize the clauses that need pre-built expressions for CodeGen. 12044 for (OMPClause *C : Clauses) { 12045 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12046 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12047 B.NumIterations, *this, CurScope, 12048 DSAStack)) 12049 return StmtError(); 12050 } 12051 } 12052 12053 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12054 return StmtError(); 12055 12056 setFunctionHasBranchProtectedScope(); 12057 return OMPDistributeParallelForSimdDirective::Create( 12058 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12059 } 12060 12061 StmtResult Sema::ActOnOpenMPDistributeSimdDirective( 12062 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12063 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12064 if (!AStmt) 12065 return StmtError(); 12066 12067 auto *CS = cast<CapturedStmt>(AStmt); 12068 // 1.2.2 OpenMP Language Terminology 12069 // Structured block - An executable statement with a single entry at the 12070 // top and a single exit at the bottom. 12071 // The point of exit cannot be a branch out of the structured block. 12072 // longjmp() and throw() must not violate the entry/exit criteria. 12073 CS->getCapturedDecl()->setNothrow(); 12074 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_distribute_simd); 12075 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12076 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12077 // 1.2.2 OpenMP Language Terminology 12078 // Structured block - An executable statement with a single entry at the 12079 // top and a single exit at the bottom. 12080 // The point of exit cannot be a branch out of the structured block. 12081 // longjmp() and throw() must not violate the entry/exit criteria. 12082 CS->getCapturedDecl()->setNothrow(); 12083 } 12084 12085 OMPLoopBasedDirective::HelperExprs B; 12086 // In presence of clause 'collapse' with number of loops, it will 12087 // define the nested loops number. 12088 unsigned NestedLoopCount = 12089 checkOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses), 12090 nullptr /*ordered not a clause on distribute*/, CS, *this, 12091 *DSAStack, VarsWithImplicitDSA, B); 12092 if (NestedLoopCount == 0) 12093 return StmtError(); 12094 12095 assert((CurContext->isDependentContext() || B.builtAll()) && 12096 "omp for loop exprs were not built"); 12097 12098 if (!CurContext->isDependentContext()) { 12099 // Finalize the clauses that need pre-built expressions for CodeGen. 12100 for (OMPClause *C : Clauses) { 12101 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12102 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12103 B.NumIterations, *this, CurScope, 12104 DSAStack)) 12105 return StmtError(); 12106 } 12107 } 12108 12109 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12110 return StmtError(); 12111 12112 setFunctionHasBranchProtectedScope(); 12113 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc, 12114 NestedLoopCount, Clauses, AStmt, B); 12115 } 12116 12117 StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective( 12118 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12119 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12120 if (!AStmt) 12121 return StmtError(); 12122 12123 auto *CS = cast<CapturedStmt>(AStmt); 12124 // 1.2.2 OpenMP Language Terminology 12125 // Structured block - An executable statement with a single entry at the 12126 // top and a single exit at the bottom. 12127 // The point of exit cannot be a branch out of the structured block. 12128 // longjmp() and throw() must not violate the entry/exit criteria. 12129 CS->getCapturedDecl()->setNothrow(); 12130 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for); 12131 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12132 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12133 // 1.2.2 OpenMP Language Terminology 12134 // Structured block - An executable statement with a single entry at the 12135 // top and a single exit at the bottom. 12136 // The point of exit cannot be a branch out of the structured block. 12137 // longjmp() and throw() must not violate the entry/exit criteria. 12138 CS->getCapturedDecl()->setNothrow(); 12139 } 12140 12141 OMPLoopBasedDirective::HelperExprs B; 12142 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 12143 // define the nested loops number. 12144 unsigned NestedLoopCount = checkOpenMPLoop( 12145 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses), 12146 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 12147 VarsWithImplicitDSA, B); 12148 if (NestedLoopCount == 0) 12149 return StmtError(); 12150 12151 assert((CurContext->isDependentContext() || B.builtAll()) && 12152 "omp target parallel for simd loop exprs were not built"); 12153 12154 if (!CurContext->isDependentContext()) { 12155 // Finalize the clauses that need pre-built expressions for CodeGen. 12156 for (OMPClause *C : Clauses) { 12157 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12158 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12159 B.NumIterations, *this, CurScope, 12160 DSAStack)) 12161 return StmtError(); 12162 } 12163 } 12164 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12165 return StmtError(); 12166 12167 setFunctionHasBranchProtectedScope(); 12168 return OMPTargetParallelForSimdDirective::Create( 12169 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12170 } 12171 12172 StmtResult Sema::ActOnOpenMPTargetSimdDirective( 12173 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12174 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12175 if (!AStmt) 12176 return StmtError(); 12177 12178 auto *CS = cast<CapturedStmt>(AStmt); 12179 // 1.2.2 OpenMP Language Terminology 12180 // Structured block - An executable statement with a single entry at the 12181 // top and a single exit at the bottom. 12182 // The point of exit cannot be a branch out of the structured block. 12183 // longjmp() and throw() must not violate the entry/exit criteria. 12184 CS->getCapturedDecl()->setNothrow(); 12185 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_simd); 12186 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12187 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12188 // 1.2.2 OpenMP Language Terminology 12189 // Structured block - An executable statement with a single entry at the 12190 // top and a single exit at the bottom. 12191 // The point of exit cannot be a branch out of the structured block. 12192 // longjmp() and throw() must not violate the entry/exit criteria. 12193 CS->getCapturedDecl()->setNothrow(); 12194 } 12195 12196 OMPLoopBasedDirective::HelperExprs B; 12197 // In presence of clause 'collapse' with number of loops, it will define the 12198 // nested loops number. 12199 unsigned NestedLoopCount = 12200 checkOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses), 12201 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 12202 VarsWithImplicitDSA, B); 12203 if (NestedLoopCount == 0) 12204 return StmtError(); 12205 12206 assert((CurContext->isDependentContext() || B.builtAll()) && 12207 "omp target simd loop exprs were not built"); 12208 12209 if (!CurContext->isDependentContext()) { 12210 // Finalize the clauses that need pre-built expressions for CodeGen. 12211 for (OMPClause *C : Clauses) { 12212 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12213 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12214 B.NumIterations, *this, CurScope, 12215 DSAStack)) 12216 return StmtError(); 12217 } 12218 } 12219 12220 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12221 return StmtError(); 12222 12223 setFunctionHasBranchProtectedScope(); 12224 return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc, 12225 NestedLoopCount, Clauses, AStmt, B); 12226 } 12227 12228 StmtResult Sema::ActOnOpenMPTeamsDistributeDirective( 12229 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12230 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12231 if (!AStmt) 12232 return StmtError(); 12233 12234 auto *CS = cast<CapturedStmt>(AStmt); 12235 // 1.2.2 OpenMP Language Terminology 12236 // Structured block - An executable statement with a single entry at the 12237 // top and a single exit at the bottom. 12238 // The point of exit cannot be a branch out of the structured block. 12239 // longjmp() and throw() must not violate the entry/exit criteria. 12240 CS->getCapturedDecl()->setNothrow(); 12241 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_teams_distribute); 12242 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12243 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12244 // 1.2.2 OpenMP Language Terminology 12245 // Structured block - An executable statement with a single entry at the 12246 // top and a single exit at the bottom. 12247 // The point of exit cannot be a branch out of the structured block. 12248 // longjmp() and throw() must not violate the entry/exit criteria. 12249 CS->getCapturedDecl()->setNothrow(); 12250 } 12251 12252 OMPLoopBasedDirective::HelperExprs B; 12253 // In presence of clause 'collapse' with number of loops, it will 12254 // define the nested loops number. 12255 unsigned NestedLoopCount = 12256 checkOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses), 12257 nullptr /*ordered not a clause on distribute*/, CS, *this, 12258 *DSAStack, VarsWithImplicitDSA, B); 12259 if (NestedLoopCount == 0) 12260 return StmtError(); 12261 12262 assert((CurContext->isDependentContext() || B.builtAll()) && 12263 "omp teams distribute loop exprs were not built"); 12264 12265 setFunctionHasBranchProtectedScope(); 12266 12267 DSAStack->setParentTeamsRegionLoc(StartLoc); 12268 12269 return OMPTeamsDistributeDirective::Create( 12270 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12271 } 12272 12273 StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective( 12274 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12275 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12276 if (!AStmt) 12277 return StmtError(); 12278 12279 auto *CS = cast<CapturedStmt>(AStmt); 12280 // 1.2.2 OpenMP Language Terminology 12281 // Structured block - An executable statement with a single entry at the 12282 // top and a single exit at the bottom. 12283 // The point of exit cannot be a branch out of the structured block. 12284 // longjmp() and throw() must not violate the entry/exit criteria. 12285 CS->getCapturedDecl()->setNothrow(); 12286 for (int ThisCaptureLevel = 12287 getOpenMPCaptureLevels(OMPD_teams_distribute_simd); 12288 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12289 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12290 // 1.2.2 OpenMP Language Terminology 12291 // Structured block - An executable statement with a single entry at the 12292 // top and a single exit at the bottom. 12293 // The point of exit cannot be a branch out of the structured block. 12294 // longjmp() and throw() must not violate the entry/exit criteria. 12295 CS->getCapturedDecl()->setNothrow(); 12296 } 12297 12298 OMPLoopBasedDirective::HelperExprs B; 12299 // In presence of clause 'collapse' with number of loops, it will 12300 // define the nested loops number. 12301 unsigned NestedLoopCount = checkOpenMPLoop( 12302 OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses), 12303 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 12304 VarsWithImplicitDSA, B); 12305 12306 if (NestedLoopCount == 0) 12307 return StmtError(); 12308 12309 assert((CurContext->isDependentContext() || B.builtAll()) && 12310 "omp teams distribute simd loop exprs were not built"); 12311 12312 if (!CurContext->isDependentContext()) { 12313 // Finalize the clauses that need pre-built expressions for CodeGen. 12314 for (OMPClause *C : Clauses) { 12315 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12316 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12317 B.NumIterations, *this, CurScope, 12318 DSAStack)) 12319 return StmtError(); 12320 } 12321 } 12322 12323 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12324 return StmtError(); 12325 12326 setFunctionHasBranchProtectedScope(); 12327 12328 DSAStack->setParentTeamsRegionLoc(StartLoc); 12329 12330 return OMPTeamsDistributeSimdDirective::Create( 12331 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12332 } 12333 12334 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective( 12335 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12336 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12337 if (!AStmt) 12338 return StmtError(); 12339 12340 auto *CS = cast<CapturedStmt>(AStmt); 12341 // 1.2.2 OpenMP Language Terminology 12342 // Structured block - An executable statement with a single entry at the 12343 // top and a single exit at the bottom. 12344 // The point of exit cannot be a branch out of the structured block. 12345 // longjmp() and throw() must not violate the entry/exit criteria. 12346 CS->getCapturedDecl()->setNothrow(); 12347 12348 for (int ThisCaptureLevel = 12349 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for_simd); 12350 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12351 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12352 // 1.2.2 OpenMP Language Terminology 12353 // Structured block - An executable statement with a single entry at the 12354 // top and a single exit at the bottom. 12355 // The point of exit cannot be a branch out of the structured block. 12356 // longjmp() and throw() must not violate the entry/exit criteria. 12357 CS->getCapturedDecl()->setNothrow(); 12358 } 12359 12360 OMPLoopBasedDirective::HelperExprs B; 12361 // In presence of clause 'collapse' with number of loops, it will 12362 // define the nested loops number. 12363 unsigned NestedLoopCount = checkOpenMPLoop( 12364 OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses), 12365 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 12366 VarsWithImplicitDSA, B); 12367 12368 if (NestedLoopCount == 0) 12369 return StmtError(); 12370 12371 assert((CurContext->isDependentContext() || B.builtAll()) && 12372 "omp for loop exprs were not built"); 12373 12374 if (!CurContext->isDependentContext()) { 12375 // Finalize the clauses that need pre-built expressions for CodeGen. 12376 for (OMPClause *C : Clauses) { 12377 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12378 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12379 B.NumIterations, *this, CurScope, 12380 DSAStack)) 12381 return StmtError(); 12382 } 12383 } 12384 12385 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12386 return StmtError(); 12387 12388 setFunctionHasBranchProtectedScope(); 12389 12390 DSAStack->setParentTeamsRegionLoc(StartLoc); 12391 12392 return OMPTeamsDistributeParallelForSimdDirective::Create( 12393 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12394 } 12395 12396 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective( 12397 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12398 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12399 if (!AStmt) 12400 return StmtError(); 12401 12402 auto *CS = cast<CapturedStmt>(AStmt); 12403 // 1.2.2 OpenMP Language Terminology 12404 // Structured block - An executable statement with a single entry at the 12405 // top and a single exit at the bottom. 12406 // The point of exit cannot be a branch out of the structured block. 12407 // longjmp() and throw() must not violate the entry/exit criteria. 12408 CS->getCapturedDecl()->setNothrow(); 12409 12410 for (int ThisCaptureLevel = 12411 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for); 12412 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12413 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12414 // 1.2.2 OpenMP Language Terminology 12415 // Structured block - An executable statement with a single entry at the 12416 // top and a single exit at the bottom. 12417 // The point of exit cannot be a branch out of the structured block. 12418 // longjmp() and throw() must not violate the entry/exit criteria. 12419 CS->getCapturedDecl()->setNothrow(); 12420 } 12421 12422 OMPLoopBasedDirective::HelperExprs B; 12423 // In presence of clause 'collapse' with number of loops, it will 12424 // define the nested loops number. 12425 unsigned NestedLoopCount = checkOpenMPLoop( 12426 OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses), 12427 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 12428 VarsWithImplicitDSA, B); 12429 12430 if (NestedLoopCount == 0) 12431 return StmtError(); 12432 12433 assert((CurContext->isDependentContext() || B.builtAll()) && 12434 "omp for loop exprs were not built"); 12435 12436 setFunctionHasBranchProtectedScope(); 12437 12438 DSAStack->setParentTeamsRegionLoc(StartLoc); 12439 12440 return OMPTeamsDistributeParallelForDirective::Create( 12441 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 12442 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 12443 } 12444 12445 StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses, 12446 Stmt *AStmt, 12447 SourceLocation StartLoc, 12448 SourceLocation EndLoc) { 12449 if (!AStmt) 12450 return StmtError(); 12451 12452 auto *CS = cast<CapturedStmt>(AStmt); 12453 // 1.2.2 OpenMP Language Terminology 12454 // Structured block - An executable statement with a single entry at the 12455 // top and a single exit at the bottom. 12456 // The point of exit cannot be a branch out of the structured block. 12457 // longjmp() and throw() must not violate the entry/exit criteria. 12458 CS->getCapturedDecl()->setNothrow(); 12459 12460 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_teams); 12461 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12462 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12463 // 1.2.2 OpenMP Language Terminology 12464 // Structured block - An executable statement with a single entry at the 12465 // top and a single exit at the bottom. 12466 // The point of exit cannot be a branch out of the structured block. 12467 // longjmp() and throw() must not violate the entry/exit criteria. 12468 CS->getCapturedDecl()->setNothrow(); 12469 } 12470 setFunctionHasBranchProtectedScope(); 12471 12472 return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, 12473 AStmt); 12474 } 12475 12476 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective( 12477 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12478 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12479 if (!AStmt) 12480 return StmtError(); 12481 12482 auto *CS = cast<CapturedStmt>(AStmt); 12483 // 1.2.2 OpenMP Language Terminology 12484 // Structured block - An executable statement with a single entry at the 12485 // top and a single exit at the bottom. 12486 // The point of exit cannot be a branch out of the structured block. 12487 // longjmp() and throw() must not violate the entry/exit criteria. 12488 CS->getCapturedDecl()->setNothrow(); 12489 for (int ThisCaptureLevel = 12490 getOpenMPCaptureLevels(OMPD_target_teams_distribute); 12491 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12492 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12493 // 1.2.2 OpenMP Language Terminology 12494 // Structured block - An executable statement with a single entry at the 12495 // top and a single exit at the bottom. 12496 // The point of exit cannot be a branch out of the structured block. 12497 // longjmp() and throw() must not violate the entry/exit criteria. 12498 CS->getCapturedDecl()->setNothrow(); 12499 } 12500 12501 OMPLoopBasedDirective::HelperExprs B; 12502 // In presence of clause 'collapse' with number of loops, it will 12503 // define the nested loops number. 12504 unsigned NestedLoopCount = checkOpenMPLoop( 12505 OMPD_target_teams_distribute, getCollapseNumberExpr(Clauses), 12506 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 12507 VarsWithImplicitDSA, B); 12508 if (NestedLoopCount == 0) 12509 return StmtError(); 12510 12511 assert((CurContext->isDependentContext() || B.builtAll()) && 12512 "omp target teams distribute loop exprs were not built"); 12513 12514 setFunctionHasBranchProtectedScope(); 12515 return OMPTargetTeamsDistributeDirective::Create( 12516 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12517 } 12518 12519 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective( 12520 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12521 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12522 if (!AStmt) 12523 return StmtError(); 12524 12525 auto *CS = cast<CapturedStmt>(AStmt); 12526 // 1.2.2 OpenMP Language Terminology 12527 // Structured block - An executable statement with a single entry at the 12528 // top and a single exit at the bottom. 12529 // The point of exit cannot be a branch out of the structured block. 12530 // longjmp() and throw() must not violate the entry/exit criteria. 12531 CS->getCapturedDecl()->setNothrow(); 12532 for (int ThisCaptureLevel = 12533 getOpenMPCaptureLevels(OMPD_target_teams_distribute_parallel_for); 12534 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12535 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12536 // 1.2.2 OpenMP Language Terminology 12537 // Structured block - An executable statement with a single entry at the 12538 // top and a single exit at the bottom. 12539 // The point of exit cannot be a branch out of the structured block. 12540 // longjmp() and throw() must not violate the entry/exit criteria. 12541 CS->getCapturedDecl()->setNothrow(); 12542 } 12543 12544 OMPLoopBasedDirective::HelperExprs B; 12545 // In presence of clause 'collapse' with number of loops, it will 12546 // define the nested loops number. 12547 unsigned NestedLoopCount = checkOpenMPLoop( 12548 OMPD_target_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses), 12549 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 12550 VarsWithImplicitDSA, B); 12551 if (NestedLoopCount == 0) 12552 return StmtError(); 12553 12554 assert((CurContext->isDependentContext() || B.builtAll()) && 12555 "omp target teams distribute parallel for loop exprs were not built"); 12556 12557 if (!CurContext->isDependentContext()) { 12558 // Finalize the clauses that need pre-built expressions for CodeGen. 12559 for (OMPClause *C : Clauses) { 12560 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12561 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12562 B.NumIterations, *this, CurScope, 12563 DSAStack)) 12564 return StmtError(); 12565 } 12566 } 12567 12568 setFunctionHasBranchProtectedScope(); 12569 return OMPTargetTeamsDistributeParallelForDirective::Create( 12570 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 12571 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 12572 } 12573 12574 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective( 12575 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12576 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12577 if (!AStmt) 12578 return StmtError(); 12579 12580 auto *CS = cast<CapturedStmt>(AStmt); 12581 // 1.2.2 OpenMP Language Terminology 12582 // Structured block - An executable statement with a single entry at the 12583 // top and a single exit at the bottom. 12584 // The point of exit cannot be a branch out of the structured block. 12585 // longjmp() and throw() must not violate the entry/exit criteria. 12586 CS->getCapturedDecl()->setNothrow(); 12587 for (int ThisCaptureLevel = getOpenMPCaptureLevels( 12588 OMPD_target_teams_distribute_parallel_for_simd); 12589 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12590 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12591 // 1.2.2 OpenMP Language Terminology 12592 // Structured block - An executable statement with a single entry at the 12593 // top and a single exit at the bottom. 12594 // The point of exit cannot be a branch out of the structured block. 12595 // longjmp() and throw() must not violate the entry/exit criteria. 12596 CS->getCapturedDecl()->setNothrow(); 12597 } 12598 12599 OMPLoopBasedDirective::HelperExprs B; 12600 // In presence of clause 'collapse' with number of loops, it will 12601 // define the nested loops number. 12602 unsigned NestedLoopCount = 12603 checkOpenMPLoop(OMPD_target_teams_distribute_parallel_for_simd, 12604 getCollapseNumberExpr(Clauses), 12605 nullptr /*ordered not a clause on distribute*/, CS, *this, 12606 *DSAStack, VarsWithImplicitDSA, B); 12607 if (NestedLoopCount == 0) 12608 return StmtError(); 12609 12610 assert((CurContext->isDependentContext() || B.builtAll()) && 12611 "omp target teams distribute parallel for simd loop exprs were not " 12612 "built"); 12613 12614 if (!CurContext->isDependentContext()) { 12615 // Finalize the clauses that need pre-built expressions for CodeGen. 12616 for (OMPClause *C : Clauses) { 12617 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12618 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12619 B.NumIterations, *this, CurScope, 12620 DSAStack)) 12621 return StmtError(); 12622 } 12623 } 12624 12625 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12626 return StmtError(); 12627 12628 setFunctionHasBranchProtectedScope(); 12629 return OMPTargetTeamsDistributeParallelForSimdDirective::Create( 12630 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12631 } 12632 12633 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective( 12634 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12635 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12636 if (!AStmt) 12637 return StmtError(); 12638 12639 auto *CS = cast<CapturedStmt>(AStmt); 12640 // 1.2.2 OpenMP Language Terminology 12641 // Structured block - An executable statement with a single entry at the 12642 // top and a single exit at the bottom. 12643 // The point of exit cannot be a branch out of the structured block. 12644 // longjmp() and throw() must not violate the entry/exit criteria. 12645 CS->getCapturedDecl()->setNothrow(); 12646 for (int ThisCaptureLevel = 12647 getOpenMPCaptureLevels(OMPD_target_teams_distribute_simd); 12648 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12649 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12650 // 1.2.2 OpenMP Language Terminology 12651 // Structured block - An executable statement with a single entry at the 12652 // top and a single exit at the bottom. 12653 // The point of exit cannot be a branch out of the structured block. 12654 // longjmp() and throw() must not violate the entry/exit criteria. 12655 CS->getCapturedDecl()->setNothrow(); 12656 } 12657 12658 OMPLoopBasedDirective::HelperExprs B; 12659 // In presence of clause 'collapse' with number of loops, it will 12660 // define the nested loops number. 12661 unsigned NestedLoopCount = checkOpenMPLoop( 12662 OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses), 12663 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 12664 VarsWithImplicitDSA, B); 12665 if (NestedLoopCount == 0) 12666 return StmtError(); 12667 12668 assert((CurContext->isDependentContext() || B.builtAll()) && 12669 "omp target teams distribute simd loop exprs were not built"); 12670 12671 if (!CurContext->isDependentContext()) { 12672 // Finalize the clauses that need pre-built expressions for CodeGen. 12673 for (OMPClause *C : Clauses) { 12674 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12675 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12676 B.NumIterations, *this, CurScope, 12677 DSAStack)) 12678 return StmtError(); 12679 } 12680 } 12681 12682 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12683 return StmtError(); 12684 12685 setFunctionHasBranchProtectedScope(); 12686 return OMPTargetTeamsDistributeSimdDirective::Create( 12687 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12688 } 12689 12690 bool Sema::checkTransformableLoopNest( 12691 OpenMPDirectiveKind Kind, Stmt *AStmt, int NumLoops, 12692 SmallVectorImpl<OMPLoopBasedDirective::HelperExprs> &LoopHelpers, 12693 Stmt *&Body, 12694 SmallVectorImpl<SmallVector<llvm::PointerUnion<Stmt *, Decl *>, 0>> 12695 &OriginalInits) { 12696 OriginalInits.emplace_back(); 12697 bool Result = OMPLoopBasedDirective::doForAllLoops( 12698 AStmt->IgnoreContainers(), /*TryImperfectlyNestedLoops=*/false, NumLoops, 12699 [this, &LoopHelpers, &Body, &OriginalInits, Kind](unsigned Cnt, 12700 Stmt *CurStmt) { 12701 VarsWithInheritedDSAType TmpDSA; 12702 unsigned SingleNumLoops = 12703 checkOpenMPLoop(Kind, nullptr, nullptr, CurStmt, *this, *DSAStack, 12704 TmpDSA, LoopHelpers[Cnt]); 12705 if (SingleNumLoops == 0) 12706 return true; 12707 assert(SingleNumLoops == 1 && "Expect single loop iteration space"); 12708 if (auto *For = dyn_cast<ForStmt>(CurStmt)) { 12709 OriginalInits.back().push_back(For->getInit()); 12710 Body = For->getBody(); 12711 } else { 12712 assert(isa<CXXForRangeStmt>(CurStmt) && 12713 "Expected canonical for or range-based for loops."); 12714 auto *CXXFor = cast<CXXForRangeStmt>(CurStmt); 12715 OriginalInits.back().push_back(CXXFor->getBeginStmt()); 12716 Body = CXXFor->getBody(); 12717 } 12718 OriginalInits.emplace_back(); 12719 return false; 12720 }, 12721 [&OriginalInits](OMPLoopBasedDirective *Transform) { 12722 Stmt *DependentPreInits; 12723 if (auto *Dir = dyn_cast<OMPTileDirective>(Transform)) 12724 DependentPreInits = Dir->getPreInits(); 12725 else if (auto *Dir = dyn_cast<OMPUnrollDirective>(Transform)) 12726 DependentPreInits = Dir->getPreInits(); 12727 else 12728 llvm_unreachable("Unhandled loop transformation"); 12729 if (!DependentPreInits) 12730 return; 12731 for (Decl *C : cast<DeclStmt>(DependentPreInits)->getDeclGroup()) 12732 OriginalInits.back().push_back(C); 12733 }); 12734 assert(OriginalInits.back().empty() && "No preinit after innermost loop"); 12735 OriginalInits.pop_back(); 12736 return Result; 12737 } 12738 12739 StmtResult Sema::ActOnOpenMPTileDirective(ArrayRef<OMPClause *> Clauses, 12740 Stmt *AStmt, SourceLocation StartLoc, 12741 SourceLocation EndLoc) { 12742 auto SizesClauses = 12743 OMPExecutableDirective::getClausesOfKind<OMPSizesClause>(Clauses); 12744 if (SizesClauses.empty()) { 12745 // A missing 'sizes' clause is already reported by the parser. 12746 return StmtError(); 12747 } 12748 const OMPSizesClause *SizesClause = *SizesClauses.begin(); 12749 unsigned NumLoops = SizesClause->getNumSizes(); 12750 12751 // Empty statement should only be possible if there already was an error. 12752 if (!AStmt) 12753 return StmtError(); 12754 12755 // Verify and diagnose loop nest. 12756 SmallVector<OMPLoopBasedDirective::HelperExprs, 4> LoopHelpers(NumLoops); 12757 Stmt *Body = nullptr; 12758 SmallVector<SmallVector<llvm::PointerUnion<Stmt *, Decl *>, 0>, 4> 12759 OriginalInits; 12760 if (!checkTransformableLoopNest(OMPD_tile, AStmt, NumLoops, LoopHelpers, Body, 12761 OriginalInits)) 12762 return StmtError(); 12763 12764 // Delay tiling to when template is completely instantiated. 12765 if (CurContext->isDependentContext()) 12766 return OMPTileDirective::Create(Context, StartLoc, EndLoc, Clauses, 12767 NumLoops, AStmt, nullptr, nullptr); 12768 12769 SmallVector<Decl *, 4> PreInits; 12770 12771 // Create iteration variables for the generated loops. 12772 SmallVector<VarDecl *, 4> FloorIndVars; 12773 SmallVector<VarDecl *, 4> TileIndVars; 12774 FloorIndVars.resize(NumLoops); 12775 TileIndVars.resize(NumLoops); 12776 for (unsigned I = 0; I < NumLoops; ++I) { 12777 OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers[I]; 12778 12779 assert(LoopHelper.Counters.size() == 1 && 12780 "Expect single-dimensional loop iteration space"); 12781 auto *OrigCntVar = cast<DeclRefExpr>(LoopHelper.Counters.front()); 12782 std::string OrigVarName = OrigCntVar->getNameInfo().getAsString(); 12783 DeclRefExpr *IterVarRef = cast<DeclRefExpr>(LoopHelper.IterationVarRef); 12784 QualType CntTy = IterVarRef->getType(); 12785 12786 // Iteration variable for the floor (i.e. outer) loop. 12787 { 12788 std::string FloorCntName = 12789 (Twine(".floor_") + llvm::utostr(I) + ".iv." + OrigVarName).str(); 12790 VarDecl *FloorCntDecl = 12791 buildVarDecl(*this, {}, CntTy, FloorCntName, nullptr, OrigCntVar); 12792 FloorIndVars[I] = FloorCntDecl; 12793 } 12794 12795 // Iteration variable for the tile (i.e. inner) loop. 12796 { 12797 std::string TileCntName = 12798 (Twine(".tile_") + llvm::utostr(I) + ".iv." + OrigVarName).str(); 12799 12800 // Reuse the iteration variable created by checkOpenMPLoop. It is also 12801 // used by the expressions to derive the original iteration variable's 12802 // value from the logical iteration number. 12803 auto *TileCntDecl = cast<VarDecl>(IterVarRef->getDecl()); 12804 TileCntDecl->setDeclName(&PP.getIdentifierTable().get(TileCntName)); 12805 TileIndVars[I] = TileCntDecl; 12806 } 12807 for (auto &P : OriginalInits[I]) { 12808 if (auto *D = P.dyn_cast<Decl *>()) 12809 PreInits.push_back(D); 12810 else if (auto *PI = dyn_cast_or_null<DeclStmt>(P.dyn_cast<Stmt *>())) 12811 PreInits.append(PI->decl_begin(), PI->decl_end()); 12812 } 12813 if (auto *PI = cast_or_null<DeclStmt>(LoopHelper.PreInits)) 12814 PreInits.append(PI->decl_begin(), PI->decl_end()); 12815 // Gather declarations for the data members used as counters. 12816 for (Expr *CounterRef : LoopHelper.Counters) { 12817 auto *CounterDecl = cast<DeclRefExpr>(CounterRef)->getDecl(); 12818 if (isa<OMPCapturedExprDecl>(CounterDecl)) 12819 PreInits.push_back(CounterDecl); 12820 } 12821 } 12822 12823 // Once the original iteration values are set, append the innermost body. 12824 Stmt *Inner = Body; 12825 12826 // Create tile loops from the inside to the outside. 12827 for (int I = NumLoops - 1; I >= 0; --I) { 12828 OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers[I]; 12829 Expr *NumIterations = LoopHelper.NumIterations; 12830 auto *OrigCntVar = cast<DeclRefExpr>(LoopHelper.Counters[0]); 12831 QualType CntTy = OrigCntVar->getType(); 12832 Expr *DimTileSize = SizesClause->getSizesRefs()[I]; 12833 Scope *CurScope = getCurScope(); 12834 12835 // Commonly used variables. 12836 DeclRefExpr *TileIV = buildDeclRefExpr(*this, TileIndVars[I], CntTy, 12837 OrigCntVar->getExprLoc()); 12838 DeclRefExpr *FloorIV = buildDeclRefExpr(*this, FloorIndVars[I], CntTy, 12839 OrigCntVar->getExprLoc()); 12840 12841 // For init-statement: auto .tile.iv = .floor.iv 12842 AddInitializerToDecl(TileIndVars[I], DefaultLvalueConversion(FloorIV).get(), 12843 /*DirectInit=*/false); 12844 Decl *CounterDecl = TileIndVars[I]; 12845 StmtResult InitStmt = new (Context) 12846 DeclStmt(DeclGroupRef::Create(Context, &CounterDecl, 1), 12847 OrigCntVar->getBeginLoc(), OrigCntVar->getEndLoc()); 12848 if (!InitStmt.isUsable()) 12849 return StmtError(); 12850 12851 // For cond-expression: .tile.iv < min(.floor.iv + DimTileSize, 12852 // NumIterations) 12853 ExprResult EndOfTile = BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), 12854 BO_Add, FloorIV, DimTileSize); 12855 if (!EndOfTile.isUsable()) 12856 return StmtError(); 12857 ExprResult IsPartialTile = 12858 BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), BO_LT, 12859 NumIterations, EndOfTile.get()); 12860 if (!IsPartialTile.isUsable()) 12861 return StmtError(); 12862 ExprResult MinTileAndIterSpace = ActOnConditionalOp( 12863 LoopHelper.Cond->getBeginLoc(), LoopHelper.Cond->getEndLoc(), 12864 IsPartialTile.get(), NumIterations, EndOfTile.get()); 12865 if (!MinTileAndIterSpace.isUsable()) 12866 return StmtError(); 12867 ExprResult CondExpr = BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), 12868 BO_LT, TileIV, MinTileAndIterSpace.get()); 12869 if (!CondExpr.isUsable()) 12870 return StmtError(); 12871 12872 // For incr-statement: ++.tile.iv 12873 ExprResult IncrStmt = 12874 BuildUnaryOp(CurScope, LoopHelper.Inc->getExprLoc(), UO_PreInc, TileIV); 12875 if (!IncrStmt.isUsable()) 12876 return StmtError(); 12877 12878 // Statements to set the original iteration variable's value from the 12879 // logical iteration number. 12880 // Generated for loop is: 12881 // Original_for_init; 12882 // for (auto .tile.iv = .floor.iv; .tile.iv < min(.floor.iv + DimTileSize, 12883 // NumIterations); ++.tile.iv) { 12884 // Original_Body; 12885 // Original_counter_update; 12886 // } 12887 // FIXME: If the innermost body is an loop itself, inserting these 12888 // statements stops it being recognized as a perfectly nested loop (e.g. 12889 // for applying tiling again). If this is the case, sink the expressions 12890 // further into the inner loop. 12891 SmallVector<Stmt *, 4> BodyParts; 12892 BodyParts.append(LoopHelper.Updates.begin(), LoopHelper.Updates.end()); 12893 BodyParts.push_back(Inner); 12894 Inner = CompoundStmt::Create(Context, BodyParts, Inner->getBeginLoc(), 12895 Inner->getEndLoc()); 12896 Inner = new (Context) 12897 ForStmt(Context, InitStmt.get(), CondExpr.get(), nullptr, 12898 IncrStmt.get(), Inner, LoopHelper.Init->getBeginLoc(), 12899 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc()); 12900 } 12901 12902 // Create floor loops from the inside to the outside. 12903 for (int I = NumLoops - 1; I >= 0; --I) { 12904 auto &LoopHelper = LoopHelpers[I]; 12905 Expr *NumIterations = LoopHelper.NumIterations; 12906 DeclRefExpr *OrigCntVar = cast<DeclRefExpr>(LoopHelper.Counters[0]); 12907 QualType CntTy = OrigCntVar->getType(); 12908 Expr *DimTileSize = SizesClause->getSizesRefs()[I]; 12909 Scope *CurScope = getCurScope(); 12910 12911 // Commonly used variables. 12912 DeclRefExpr *FloorIV = buildDeclRefExpr(*this, FloorIndVars[I], CntTy, 12913 OrigCntVar->getExprLoc()); 12914 12915 // For init-statement: auto .floor.iv = 0 12916 AddInitializerToDecl( 12917 FloorIndVars[I], 12918 ActOnIntegerConstant(LoopHelper.Init->getExprLoc(), 0).get(), 12919 /*DirectInit=*/false); 12920 Decl *CounterDecl = FloorIndVars[I]; 12921 StmtResult InitStmt = new (Context) 12922 DeclStmt(DeclGroupRef::Create(Context, &CounterDecl, 1), 12923 OrigCntVar->getBeginLoc(), OrigCntVar->getEndLoc()); 12924 if (!InitStmt.isUsable()) 12925 return StmtError(); 12926 12927 // For cond-expression: .floor.iv < NumIterations 12928 ExprResult CondExpr = BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), 12929 BO_LT, FloorIV, NumIterations); 12930 if (!CondExpr.isUsable()) 12931 return StmtError(); 12932 12933 // For incr-statement: .floor.iv += DimTileSize 12934 ExprResult IncrStmt = BuildBinOp(CurScope, LoopHelper.Inc->getExprLoc(), 12935 BO_AddAssign, FloorIV, DimTileSize); 12936 if (!IncrStmt.isUsable()) 12937 return StmtError(); 12938 12939 Inner = new (Context) 12940 ForStmt(Context, InitStmt.get(), CondExpr.get(), nullptr, 12941 IncrStmt.get(), Inner, LoopHelper.Init->getBeginLoc(), 12942 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc()); 12943 } 12944 12945 return OMPTileDirective::Create(Context, StartLoc, EndLoc, Clauses, NumLoops, 12946 AStmt, Inner, 12947 buildPreInits(Context, PreInits)); 12948 } 12949 12950 StmtResult Sema::ActOnOpenMPUnrollDirective(ArrayRef<OMPClause *> Clauses, 12951 Stmt *AStmt, 12952 SourceLocation StartLoc, 12953 SourceLocation EndLoc) { 12954 // Empty statement should only be possible if there already was an error. 12955 if (!AStmt) 12956 return StmtError(); 12957 12958 if (checkMutuallyExclusiveClauses(*this, Clauses, {OMPC_partial, OMPC_full})) 12959 return StmtError(); 12960 12961 const OMPFullClause *FullClause = 12962 OMPExecutableDirective::getSingleClause<OMPFullClause>(Clauses); 12963 const OMPPartialClause *PartialClause = 12964 OMPExecutableDirective::getSingleClause<OMPPartialClause>(Clauses); 12965 assert(!(FullClause && PartialClause) && 12966 "mutual exclusivity must have been checked before"); 12967 12968 constexpr unsigned NumLoops = 1; 12969 Stmt *Body = nullptr; 12970 SmallVector<OMPLoopBasedDirective::HelperExprs, NumLoops> LoopHelpers( 12971 NumLoops); 12972 SmallVector<SmallVector<llvm::PointerUnion<Stmt *, Decl *>, 0>, NumLoops + 1> 12973 OriginalInits; 12974 if (!checkTransformableLoopNest(OMPD_unroll, AStmt, NumLoops, LoopHelpers, 12975 Body, OriginalInits)) 12976 return StmtError(); 12977 12978 unsigned NumGeneratedLoops = PartialClause ? 1 : 0; 12979 12980 // Delay unrolling to when template is completely instantiated. 12981 if (CurContext->isDependentContext()) 12982 return OMPUnrollDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 12983 NumGeneratedLoops, nullptr, nullptr); 12984 12985 OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers.front(); 12986 12987 if (FullClause) { 12988 if (!VerifyPositiveIntegerConstantInClause( 12989 LoopHelper.NumIterations, OMPC_full, /*StrictlyPositive=*/false, 12990 /*SuppressExprDigs=*/true) 12991 .isUsable()) { 12992 Diag(AStmt->getBeginLoc(), diag::err_omp_unroll_full_variable_trip_count); 12993 Diag(FullClause->getBeginLoc(), diag::note_omp_directive_here) 12994 << "#pragma omp unroll full"; 12995 return StmtError(); 12996 } 12997 } 12998 12999 // The generated loop may only be passed to other loop-associated directive 13000 // when a partial clause is specified. Without the requirement it is 13001 // sufficient to generate loop unroll metadata at code-generation. 13002 if (NumGeneratedLoops == 0) 13003 return OMPUnrollDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 13004 NumGeneratedLoops, nullptr, nullptr); 13005 13006 // Otherwise, we need to provide a de-sugared/transformed AST that can be 13007 // associated with another loop directive. 13008 // 13009 // The canonical loop analysis return by checkTransformableLoopNest assumes 13010 // the following structure to be the same loop without transformations or 13011 // directives applied: \code OriginalInits; LoopHelper.PreInits; 13012 // LoopHelper.Counters; 13013 // for (; IV < LoopHelper.NumIterations; ++IV) { 13014 // LoopHelper.Updates; 13015 // Body; 13016 // } 13017 // \endcode 13018 // where IV is a variable declared and initialized to 0 in LoopHelper.PreInits 13019 // and referenced by LoopHelper.IterationVarRef. 13020 // 13021 // The unrolling directive transforms this into the following loop: 13022 // \code 13023 // OriginalInits; \ 13024 // LoopHelper.PreInits; > NewPreInits 13025 // LoopHelper.Counters; / 13026 // for (auto UIV = 0; UIV < LoopHelper.NumIterations; UIV+=Factor) { 13027 // #pragma clang loop unroll_count(Factor) 13028 // for (IV = UIV; IV < UIV + Factor && UIV < LoopHelper.NumIterations; ++IV) 13029 // { 13030 // LoopHelper.Updates; 13031 // Body; 13032 // } 13033 // } 13034 // \endcode 13035 // where UIV is a new logical iteration counter. IV must be the same VarDecl 13036 // as the original LoopHelper.IterationVarRef because LoopHelper.Updates 13037 // references it. If the partially unrolled loop is associated with another 13038 // loop directive (like an OMPForDirective), it will use checkOpenMPLoop to 13039 // analyze this loop, i.e. the outer loop must fulfill the constraints of an 13040 // OpenMP canonical loop. The inner loop is not an associable canonical loop 13041 // and only exists to defer its unrolling to LLVM's LoopUnroll instead of 13042 // doing it in the frontend (by adding loop metadata). NewPreInits becomes a 13043 // property of the OMPLoopBasedDirective instead of statements in 13044 // CompoundStatement. This is to allow the loop to become a non-outermost loop 13045 // of a canonical loop nest where these PreInits are emitted before the 13046 // outermost directive. 13047 13048 // Determine the PreInit declarations. 13049 SmallVector<Decl *, 4> PreInits; 13050 assert(OriginalInits.size() == 1 && 13051 "Expecting a single-dimensional loop iteration space"); 13052 for (auto &P : OriginalInits[0]) { 13053 if (auto *D = P.dyn_cast<Decl *>()) 13054 PreInits.push_back(D); 13055 else if (auto *PI = dyn_cast_or_null<DeclStmt>(P.dyn_cast<Stmt *>())) 13056 PreInits.append(PI->decl_begin(), PI->decl_end()); 13057 } 13058 if (auto *PI = cast_or_null<DeclStmt>(LoopHelper.PreInits)) 13059 PreInits.append(PI->decl_begin(), PI->decl_end()); 13060 // Gather declarations for the data members used as counters. 13061 for (Expr *CounterRef : LoopHelper.Counters) { 13062 auto *CounterDecl = cast<DeclRefExpr>(CounterRef)->getDecl(); 13063 if (isa<OMPCapturedExprDecl>(CounterDecl)) 13064 PreInits.push_back(CounterDecl); 13065 } 13066 13067 auto *IterationVarRef = cast<DeclRefExpr>(LoopHelper.IterationVarRef); 13068 QualType IVTy = IterationVarRef->getType(); 13069 assert(LoopHelper.Counters.size() == 1 && 13070 "Expecting a single-dimensional loop iteration space"); 13071 auto *OrigVar = cast<DeclRefExpr>(LoopHelper.Counters.front()); 13072 13073 // Determine the unroll factor. 13074 uint64_t Factor; 13075 SourceLocation FactorLoc; 13076 if (Expr *FactorVal = PartialClause->getFactor()) { 13077 Factor = 13078 FactorVal->getIntegerConstantExpr(Context).getValue().getZExtValue(); 13079 FactorLoc = FactorVal->getExprLoc(); 13080 } else { 13081 // TODO: Use a better profitability model. 13082 Factor = 2; 13083 } 13084 assert(Factor > 0 && "Expected positive unroll factor"); 13085 auto MakeFactorExpr = [this, Factor, IVTy, FactorLoc]() { 13086 return IntegerLiteral::Create( 13087 Context, llvm::APInt(Context.getIntWidth(IVTy), Factor), IVTy, 13088 FactorLoc); 13089 }; 13090 13091 // Iteration variable SourceLocations. 13092 SourceLocation OrigVarLoc = OrigVar->getExprLoc(); 13093 SourceLocation OrigVarLocBegin = OrigVar->getBeginLoc(); 13094 SourceLocation OrigVarLocEnd = OrigVar->getEndLoc(); 13095 13096 // Internal variable names. 13097 std::string OrigVarName = OrigVar->getNameInfo().getAsString(); 13098 std::string OuterIVName = (Twine(".unrolled.iv.") + OrigVarName).str(); 13099 std::string InnerIVName = (Twine(".unroll_inner.iv.") + OrigVarName).str(); 13100 std::string InnerTripCountName = 13101 (Twine(".unroll_inner.tripcount.") + OrigVarName).str(); 13102 13103 // Create the iteration variable for the unrolled loop. 13104 VarDecl *OuterIVDecl = 13105 buildVarDecl(*this, {}, IVTy, OuterIVName, nullptr, OrigVar); 13106 auto MakeOuterRef = [this, OuterIVDecl, IVTy, OrigVarLoc]() { 13107 return buildDeclRefExpr(*this, OuterIVDecl, IVTy, OrigVarLoc); 13108 }; 13109 13110 // Iteration variable for the inner loop: Reuse the iteration variable created 13111 // by checkOpenMPLoop. 13112 auto *InnerIVDecl = cast<VarDecl>(IterationVarRef->getDecl()); 13113 InnerIVDecl->setDeclName(&PP.getIdentifierTable().get(InnerIVName)); 13114 auto MakeInnerRef = [this, InnerIVDecl, IVTy, OrigVarLoc]() { 13115 return buildDeclRefExpr(*this, InnerIVDecl, IVTy, OrigVarLoc); 13116 }; 13117 13118 // Make a copy of the NumIterations expression for each use: By the AST 13119 // constraints, every expression object in a DeclContext must be unique. 13120 CaptureVars CopyTransformer(*this); 13121 auto MakeNumIterations = [&CopyTransformer, &LoopHelper]() -> Expr * { 13122 return AssertSuccess( 13123 CopyTransformer.TransformExpr(LoopHelper.NumIterations)); 13124 }; 13125 13126 // Inner For init-statement: auto .unroll_inner.iv = .unrolled.iv 13127 ExprResult LValueConv = DefaultLvalueConversion(MakeOuterRef()); 13128 AddInitializerToDecl(InnerIVDecl, LValueConv.get(), /*DirectInit=*/false); 13129 StmtResult InnerInit = new (Context) 13130 DeclStmt(DeclGroupRef(InnerIVDecl), OrigVarLocBegin, OrigVarLocEnd); 13131 if (!InnerInit.isUsable()) 13132 return StmtError(); 13133 13134 // Inner For cond-expression: 13135 // \code 13136 // .unroll_inner.iv < .unrolled.iv + Factor && 13137 // .unroll_inner.iv < NumIterations 13138 // \endcode 13139 // This conjunction of two conditions allows ScalarEvolution to derive the 13140 // maximum trip count of the inner loop. 13141 ExprResult EndOfTile = BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), 13142 BO_Add, MakeOuterRef(), MakeFactorExpr()); 13143 if (!EndOfTile.isUsable()) 13144 return StmtError(); 13145 ExprResult InnerCond1 = BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), 13146 BO_LE, MakeInnerRef(), EndOfTile.get()); 13147 if (!InnerCond1.isUsable()) 13148 return StmtError(); 13149 ExprResult InnerCond2 = 13150 BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), BO_LE, MakeInnerRef(), 13151 MakeNumIterations()); 13152 if (!InnerCond2.isUsable()) 13153 return StmtError(); 13154 ExprResult InnerCond = 13155 BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), BO_LAnd, 13156 InnerCond1.get(), InnerCond2.get()); 13157 if (!InnerCond.isUsable()) 13158 return StmtError(); 13159 13160 // Inner For incr-statement: ++.unroll_inner.iv 13161 ExprResult InnerIncr = BuildUnaryOp(CurScope, LoopHelper.Inc->getExprLoc(), 13162 UO_PreInc, MakeInnerRef()); 13163 if (!InnerIncr.isUsable()) 13164 return StmtError(); 13165 13166 // Inner For statement. 13167 SmallVector<Stmt *> InnerBodyStmts; 13168 InnerBodyStmts.append(LoopHelper.Updates.begin(), LoopHelper.Updates.end()); 13169 InnerBodyStmts.push_back(Body); 13170 CompoundStmt *InnerBody = CompoundStmt::Create( 13171 Context, InnerBodyStmts, Body->getBeginLoc(), Body->getEndLoc()); 13172 ForStmt *InnerFor = new (Context) 13173 ForStmt(Context, InnerInit.get(), InnerCond.get(), nullptr, 13174 InnerIncr.get(), InnerBody, LoopHelper.Init->getBeginLoc(), 13175 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc()); 13176 13177 // Unroll metadata for the inner loop. 13178 // This needs to take into account the remainder portion of the unrolled loop, 13179 // hence `unroll(full)` does not apply here, even though the LoopUnroll pass 13180 // supports multiple loop exits. Instead, unroll using a factor equivalent to 13181 // the maximum trip count, which will also generate a remainder loop. Just 13182 // `unroll(enable)` (which could have been useful if the user has not 13183 // specified a concrete factor; even though the outer loop cannot be 13184 // influenced anymore, would avoid more code bloat than necessary) will refuse 13185 // the loop because "Won't unroll; remainder loop could not be generated when 13186 // assuming runtime trip count". Even if it did work, it must not choose a 13187 // larger unroll factor than the maximum loop length, or it would always just 13188 // execute the remainder loop. 13189 LoopHintAttr *UnrollHintAttr = 13190 LoopHintAttr::CreateImplicit(Context, LoopHintAttr::UnrollCount, 13191 LoopHintAttr::Numeric, MakeFactorExpr()); 13192 AttributedStmt *InnerUnrolled = 13193 AttributedStmt::Create(Context, StartLoc, {UnrollHintAttr}, InnerFor); 13194 13195 // Outer For init-statement: auto .unrolled.iv = 0 13196 AddInitializerToDecl( 13197 OuterIVDecl, ActOnIntegerConstant(LoopHelper.Init->getExprLoc(), 0).get(), 13198 /*DirectInit=*/false); 13199 StmtResult OuterInit = new (Context) 13200 DeclStmt(DeclGroupRef(OuterIVDecl), OrigVarLocBegin, OrigVarLocEnd); 13201 if (!OuterInit.isUsable()) 13202 return StmtError(); 13203 13204 // Outer For cond-expression: .unrolled.iv < NumIterations 13205 ExprResult OuterConde = 13206 BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), BO_LT, MakeOuterRef(), 13207 MakeNumIterations()); 13208 if (!OuterConde.isUsable()) 13209 return StmtError(); 13210 13211 // Outer For incr-statement: .unrolled.iv += Factor 13212 ExprResult OuterIncr = 13213 BuildBinOp(CurScope, LoopHelper.Inc->getExprLoc(), BO_AddAssign, 13214 MakeOuterRef(), MakeFactorExpr()); 13215 if (!OuterIncr.isUsable()) 13216 return StmtError(); 13217 13218 // Outer For statement. 13219 ForStmt *OuterFor = new (Context) 13220 ForStmt(Context, OuterInit.get(), OuterConde.get(), nullptr, 13221 OuterIncr.get(), InnerUnrolled, LoopHelper.Init->getBeginLoc(), 13222 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc()); 13223 13224 return OMPUnrollDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 13225 NumGeneratedLoops, OuterFor, 13226 buildPreInits(Context, PreInits)); 13227 } 13228 13229 OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr, 13230 SourceLocation StartLoc, 13231 SourceLocation LParenLoc, 13232 SourceLocation EndLoc) { 13233 OMPClause *Res = nullptr; 13234 switch (Kind) { 13235 case OMPC_final: 13236 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc); 13237 break; 13238 case OMPC_num_threads: 13239 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc); 13240 break; 13241 case OMPC_safelen: 13242 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc); 13243 break; 13244 case OMPC_simdlen: 13245 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc); 13246 break; 13247 case OMPC_allocator: 13248 Res = ActOnOpenMPAllocatorClause(Expr, StartLoc, LParenLoc, EndLoc); 13249 break; 13250 case OMPC_collapse: 13251 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc); 13252 break; 13253 case OMPC_ordered: 13254 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr); 13255 break; 13256 case OMPC_num_teams: 13257 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc); 13258 break; 13259 case OMPC_thread_limit: 13260 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc); 13261 break; 13262 case OMPC_priority: 13263 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc); 13264 break; 13265 case OMPC_grainsize: 13266 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc); 13267 break; 13268 case OMPC_num_tasks: 13269 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc); 13270 break; 13271 case OMPC_hint: 13272 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc); 13273 break; 13274 case OMPC_depobj: 13275 Res = ActOnOpenMPDepobjClause(Expr, StartLoc, LParenLoc, EndLoc); 13276 break; 13277 case OMPC_detach: 13278 Res = ActOnOpenMPDetachClause(Expr, StartLoc, LParenLoc, EndLoc); 13279 break; 13280 case OMPC_novariants: 13281 Res = ActOnOpenMPNovariantsClause(Expr, StartLoc, LParenLoc, EndLoc); 13282 break; 13283 case OMPC_nocontext: 13284 Res = ActOnOpenMPNocontextClause(Expr, StartLoc, LParenLoc, EndLoc); 13285 break; 13286 case OMPC_filter: 13287 Res = ActOnOpenMPFilterClause(Expr, StartLoc, LParenLoc, EndLoc); 13288 break; 13289 case OMPC_partial: 13290 Res = ActOnOpenMPPartialClause(Expr, StartLoc, LParenLoc, EndLoc); 13291 break; 13292 case OMPC_device: 13293 case OMPC_if: 13294 case OMPC_default: 13295 case OMPC_proc_bind: 13296 case OMPC_schedule: 13297 case OMPC_private: 13298 case OMPC_firstprivate: 13299 case OMPC_lastprivate: 13300 case OMPC_shared: 13301 case OMPC_reduction: 13302 case OMPC_task_reduction: 13303 case OMPC_in_reduction: 13304 case OMPC_linear: 13305 case OMPC_aligned: 13306 case OMPC_copyin: 13307 case OMPC_copyprivate: 13308 case OMPC_nowait: 13309 case OMPC_untied: 13310 case OMPC_mergeable: 13311 case OMPC_threadprivate: 13312 case OMPC_sizes: 13313 case OMPC_allocate: 13314 case OMPC_flush: 13315 case OMPC_read: 13316 case OMPC_write: 13317 case OMPC_update: 13318 case OMPC_capture: 13319 case OMPC_seq_cst: 13320 case OMPC_acq_rel: 13321 case OMPC_acquire: 13322 case OMPC_release: 13323 case OMPC_relaxed: 13324 case OMPC_depend: 13325 case OMPC_threads: 13326 case OMPC_simd: 13327 case OMPC_map: 13328 case OMPC_nogroup: 13329 case OMPC_dist_schedule: 13330 case OMPC_defaultmap: 13331 case OMPC_unknown: 13332 case OMPC_uniform: 13333 case OMPC_to: 13334 case OMPC_from: 13335 case OMPC_use_device_ptr: 13336 case OMPC_use_device_addr: 13337 case OMPC_is_device_ptr: 13338 case OMPC_unified_address: 13339 case OMPC_unified_shared_memory: 13340 case OMPC_reverse_offload: 13341 case OMPC_dynamic_allocators: 13342 case OMPC_atomic_default_mem_order: 13343 case OMPC_device_type: 13344 case OMPC_match: 13345 case OMPC_nontemporal: 13346 case OMPC_order: 13347 case OMPC_destroy: 13348 case OMPC_inclusive: 13349 case OMPC_exclusive: 13350 case OMPC_uses_allocators: 13351 case OMPC_affinity: 13352 case OMPC_when: 13353 default: 13354 llvm_unreachable("Clause is not allowed."); 13355 } 13356 return Res; 13357 } 13358 13359 // An OpenMP directive such as 'target parallel' has two captured regions: 13360 // for the 'target' and 'parallel' respectively. This function returns 13361 // the region in which to capture expressions associated with a clause. 13362 // A return value of OMPD_unknown signifies that the expression should not 13363 // be captured. 13364 static OpenMPDirectiveKind getOpenMPCaptureRegionForClause( 13365 OpenMPDirectiveKind DKind, OpenMPClauseKind CKind, unsigned OpenMPVersion, 13366 OpenMPDirectiveKind NameModifier = OMPD_unknown) { 13367 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 13368 switch (CKind) { 13369 case OMPC_if: 13370 switch (DKind) { 13371 case OMPD_target_parallel_for_simd: 13372 if (OpenMPVersion >= 50 && 13373 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) { 13374 CaptureRegion = OMPD_parallel; 13375 break; 13376 } 13377 LLVM_FALLTHROUGH; 13378 case OMPD_target_parallel: 13379 case OMPD_target_parallel_for: 13380 // If this clause applies to the nested 'parallel' region, capture within 13381 // the 'target' region, otherwise do not capture. 13382 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel) 13383 CaptureRegion = OMPD_target; 13384 break; 13385 case OMPD_target_teams_distribute_parallel_for_simd: 13386 if (OpenMPVersion >= 50 && 13387 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) { 13388 CaptureRegion = OMPD_parallel; 13389 break; 13390 } 13391 LLVM_FALLTHROUGH; 13392 case OMPD_target_teams_distribute_parallel_for: 13393 // If this clause applies to the nested 'parallel' region, capture within 13394 // the 'teams' region, otherwise do not capture. 13395 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel) 13396 CaptureRegion = OMPD_teams; 13397 break; 13398 case OMPD_teams_distribute_parallel_for_simd: 13399 if (OpenMPVersion >= 50 && 13400 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) { 13401 CaptureRegion = OMPD_parallel; 13402 break; 13403 } 13404 LLVM_FALLTHROUGH; 13405 case OMPD_teams_distribute_parallel_for: 13406 CaptureRegion = OMPD_teams; 13407 break; 13408 case OMPD_target_update: 13409 case OMPD_target_enter_data: 13410 case OMPD_target_exit_data: 13411 CaptureRegion = OMPD_task; 13412 break; 13413 case OMPD_parallel_master_taskloop: 13414 if (NameModifier == OMPD_unknown || NameModifier == OMPD_taskloop) 13415 CaptureRegion = OMPD_parallel; 13416 break; 13417 case OMPD_parallel_master_taskloop_simd: 13418 if ((OpenMPVersion <= 45 && NameModifier == OMPD_unknown) || 13419 NameModifier == OMPD_taskloop) { 13420 CaptureRegion = OMPD_parallel; 13421 break; 13422 } 13423 if (OpenMPVersion <= 45) 13424 break; 13425 if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd) 13426 CaptureRegion = OMPD_taskloop; 13427 break; 13428 case OMPD_parallel_for_simd: 13429 if (OpenMPVersion <= 45) 13430 break; 13431 if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd) 13432 CaptureRegion = OMPD_parallel; 13433 break; 13434 case OMPD_taskloop_simd: 13435 case OMPD_master_taskloop_simd: 13436 if (OpenMPVersion <= 45) 13437 break; 13438 if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd) 13439 CaptureRegion = OMPD_taskloop; 13440 break; 13441 case OMPD_distribute_parallel_for_simd: 13442 if (OpenMPVersion <= 45) 13443 break; 13444 if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd) 13445 CaptureRegion = OMPD_parallel; 13446 break; 13447 case OMPD_target_simd: 13448 if (OpenMPVersion >= 50 && 13449 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) 13450 CaptureRegion = OMPD_target; 13451 break; 13452 case OMPD_teams_distribute_simd: 13453 case OMPD_target_teams_distribute_simd: 13454 if (OpenMPVersion >= 50 && 13455 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) 13456 CaptureRegion = OMPD_teams; 13457 break; 13458 case OMPD_cancel: 13459 case OMPD_parallel: 13460 case OMPD_parallel_master: 13461 case OMPD_parallel_sections: 13462 case OMPD_parallel_for: 13463 case OMPD_target: 13464 case OMPD_target_teams: 13465 case OMPD_target_teams_distribute: 13466 case OMPD_distribute_parallel_for: 13467 case OMPD_task: 13468 case OMPD_taskloop: 13469 case OMPD_master_taskloop: 13470 case OMPD_target_data: 13471 case OMPD_simd: 13472 case OMPD_for_simd: 13473 case OMPD_distribute_simd: 13474 // Do not capture if-clause expressions. 13475 break; 13476 case OMPD_threadprivate: 13477 case OMPD_allocate: 13478 case OMPD_taskyield: 13479 case OMPD_barrier: 13480 case OMPD_taskwait: 13481 case OMPD_cancellation_point: 13482 case OMPD_flush: 13483 case OMPD_depobj: 13484 case OMPD_scan: 13485 case OMPD_declare_reduction: 13486 case OMPD_declare_mapper: 13487 case OMPD_declare_simd: 13488 case OMPD_declare_variant: 13489 case OMPD_begin_declare_variant: 13490 case OMPD_end_declare_variant: 13491 case OMPD_declare_target: 13492 case OMPD_end_declare_target: 13493 case OMPD_teams: 13494 case OMPD_tile: 13495 case OMPD_unroll: 13496 case OMPD_for: 13497 case OMPD_sections: 13498 case OMPD_section: 13499 case OMPD_single: 13500 case OMPD_master: 13501 case OMPD_masked: 13502 case OMPD_critical: 13503 case OMPD_taskgroup: 13504 case OMPD_distribute: 13505 case OMPD_ordered: 13506 case OMPD_atomic: 13507 case OMPD_teams_distribute: 13508 case OMPD_requires: 13509 case OMPD_metadirective: 13510 llvm_unreachable("Unexpected OpenMP directive with if-clause"); 13511 case OMPD_unknown: 13512 default: 13513 llvm_unreachable("Unknown OpenMP directive"); 13514 } 13515 break; 13516 case OMPC_num_threads: 13517 switch (DKind) { 13518 case OMPD_target_parallel: 13519 case OMPD_target_parallel_for: 13520 case OMPD_target_parallel_for_simd: 13521 CaptureRegion = OMPD_target; 13522 break; 13523 case OMPD_teams_distribute_parallel_for: 13524 case OMPD_teams_distribute_parallel_for_simd: 13525 case OMPD_target_teams_distribute_parallel_for: 13526 case OMPD_target_teams_distribute_parallel_for_simd: 13527 CaptureRegion = OMPD_teams; 13528 break; 13529 case OMPD_parallel: 13530 case OMPD_parallel_master: 13531 case OMPD_parallel_sections: 13532 case OMPD_parallel_for: 13533 case OMPD_parallel_for_simd: 13534 case OMPD_distribute_parallel_for: 13535 case OMPD_distribute_parallel_for_simd: 13536 case OMPD_parallel_master_taskloop: 13537 case OMPD_parallel_master_taskloop_simd: 13538 // Do not capture num_threads-clause expressions. 13539 break; 13540 case OMPD_target_data: 13541 case OMPD_target_enter_data: 13542 case OMPD_target_exit_data: 13543 case OMPD_target_update: 13544 case OMPD_target: 13545 case OMPD_target_simd: 13546 case OMPD_target_teams: 13547 case OMPD_target_teams_distribute: 13548 case OMPD_target_teams_distribute_simd: 13549 case OMPD_cancel: 13550 case OMPD_task: 13551 case OMPD_taskloop: 13552 case OMPD_taskloop_simd: 13553 case OMPD_master_taskloop: 13554 case OMPD_master_taskloop_simd: 13555 case OMPD_threadprivate: 13556 case OMPD_allocate: 13557 case OMPD_taskyield: 13558 case OMPD_barrier: 13559 case OMPD_taskwait: 13560 case OMPD_cancellation_point: 13561 case OMPD_flush: 13562 case OMPD_depobj: 13563 case OMPD_scan: 13564 case OMPD_declare_reduction: 13565 case OMPD_declare_mapper: 13566 case OMPD_declare_simd: 13567 case OMPD_declare_variant: 13568 case OMPD_begin_declare_variant: 13569 case OMPD_end_declare_variant: 13570 case OMPD_declare_target: 13571 case OMPD_end_declare_target: 13572 case OMPD_teams: 13573 case OMPD_simd: 13574 case OMPD_tile: 13575 case OMPD_unroll: 13576 case OMPD_for: 13577 case OMPD_for_simd: 13578 case OMPD_sections: 13579 case OMPD_section: 13580 case OMPD_single: 13581 case OMPD_master: 13582 case OMPD_masked: 13583 case OMPD_critical: 13584 case OMPD_taskgroup: 13585 case OMPD_distribute: 13586 case OMPD_ordered: 13587 case OMPD_atomic: 13588 case OMPD_distribute_simd: 13589 case OMPD_teams_distribute: 13590 case OMPD_teams_distribute_simd: 13591 case OMPD_requires: 13592 case OMPD_metadirective: 13593 llvm_unreachable("Unexpected OpenMP directive with num_threads-clause"); 13594 case OMPD_unknown: 13595 default: 13596 llvm_unreachable("Unknown OpenMP directive"); 13597 } 13598 break; 13599 case OMPC_num_teams: 13600 switch (DKind) { 13601 case OMPD_target_teams: 13602 case OMPD_target_teams_distribute: 13603 case OMPD_target_teams_distribute_simd: 13604 case OMPD_target_teams_distribute_parallel_for: 13605 case OMPD_target_teams_distribute_parallel_for_simd: 13606 CaptureRegion = OMPD_target; 13607 break; 13608 case OMPD_teams_distribute_parallel_for: 13609 case OMPD_teams_distribute_parallel_for_simd: 13610 case OMPD_teams: 13611 case OMPD_teams_distribute: 13612 case OMPD_teams_distribute_simd: 13613 // Do not capture num_teams-clause expressions. 13614 break; 13615 case OMPD_distribute_parallel_for: 13616 case OMPD_distribute_parallel_for_simd: 13617 case OMPD_task: 13618 case OMPD_taskloop: 13619 case OMPD_taskloop_simd: 13620 case OMPD_master_taskloop: 13621 case OMPD_master_taskloop_simd: 13622 case OMPD_parallel_master_taskloop: 13623 case OMPD_parallel_master_taskloop_simd: 13624 case OMPD_target_data: 13625 case OMPD_target_enter_data: 13626 case OMPD_target_exit_data: 13627 case OMPD_target_update: 13628 case OMPD_cancel: 13629 case OMPD_parallel: 13630 case OMPD_parallel_master: 13631 case OMPD_parallel_sections: 13632 case OMPD_parallel_for: 13633 case OMPD_parallel_for_simd: 13634 case OMPD_target: 13635 case OMPD_target_simd: 13636 case OMPD_target_parallel: 13637 case OMPD_target_parallel_for: 13638 case OMPD_target_parallel_for_simd: 13639 case OMPD_threadprivate: 13640 case OMPD_allocate: 13641 case OMPD_taskyield: 13642 case OMPD_barrier: 13643 case OMPD_taskwait: 13644 case OMPD_cancellation_point: 13645 case OMPD_flush: 13646 case OMPD_depobj: 13647 case OMPD_scan: 13648 case OMPD_declare_reduction: 13649 case OMPD_declare_mapper: 13650 case OMPD_declare_simd: 13651 case OMPD_declare_variant: 13652 case OMPD_begin_declare_variant: 13653 case OMPD_end_declare_variant: 13654 case OMPD_declare_target: 13655 case OMPD_end_declare_target: 13656 case OMPD_simd: 13657 case OMPD_tile: 13658 case OMPD_unroll: 13659 case OMPD_for: 13660 case OMPD_for_simd: 13661 case OMPD_sections: 13662 case OMPD_section: 13663 case OMPD_single: 13664 case OMPD_master: 13665 case OMPD_masked: 13666 case OMPD_critical: 13667 case OMPD_taskgroup: 13668 case OMPD_distribute: 13669 case OMPD_ordered: 13670 case OMPD_atomic: 13671 case OMPD_distribute_simd: 13672 case OMPD_requires: 13673 case OMPD_metadirective: 13674 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause"); 13675 case OMPD_unknown: 13676 default: 13677 llvm_unreachable("Unknown OpenMP directive"); 13678 } 13679 break; 13680 case OMPC_thread_limit: 13681 switch (DKind) { 13682 case OMPD_target_teams: 13683 case OMPD_target_teams_distribute: 13684 case OMPD_target_teams_distribute_simd: 13685 case OMPD_target_teams_distribute_parallel_for: 13686 case OMPD_target_teams_distribute_parallel_for_simd: 13687 CaptureRegion = OMPD_target; 13688 break; 13689 case OMPD_teams_distribute_parallel_for: 13690 case OMPD_teams_distribute_parallel_for_simd: 13691 case OMPD_teams: 13692 case OMPD_teams_distribute: 13693 case OMPD_teams_distribute_simd: 13694 // Do not capture thread_limit-clause expressions. 13695 break; 13696 case OMPD_distribute_parallel_for: 13697 case OMPD_distribute_parallel_for_simd: 13698 case OMPD_task: 13699 case OMPD_taskloop: 13700 case OMPD_taskloop_simd: 13701 case OMPD_master_taskloop: 13702 case OMPD_master_taskloop_simd: 13703 case OMPD_parallel_master_taskloop: 13704 case OMPD_parallel_master_taskloop_simd: 13705 case OMPD_target_data: 13706 case OMPD_target_enter_data: 13707 case OMPD_target_exit_data: 13708 case OMPD_target_update: 13709 case OMPD_cancel: 13710 case OMPD_parallel: 13711 case OMPD_parallel_master: 13712 case OMPD_parallel_sections: 13713 case OMPD_parallel_for: 13714 case OMPD_parallel_for_simd: 13715 case OMPD_target: 13716 case OMPD_target_simd: 13717 case OMPD_target_parallel: 13718 case OMPD_target_parallel_for: 13719 case OMPD_target_parallel_for_simd: 13720 case OMPD_threadprivate: 13721 case OMPD_allocate: 13722 case OMPD_taskyield: 13723 case OMPD_barrier: 13724 case OMPD_taskwait: 13725 case OMPD_cancellation_point: 13726 case OMPD_flush: 13727 case OMPD_depobj: 13728 case OMPD_scan: 13729 case OMPD_declare_reduction: 13730 case OMPD_declare_mapper: 13731 case OMPD_declare_simd: 13732 case OMPD_declare_variant: 13733 case OMPD_begin_declare_variant: 13734 case OMPD_end_declare_variant: 13735 case OMPD_declare_target: 13736 case OMPD_end_declare_target: 13737 case OMPD_simd: 13738 case OMPD_tile: 13739 case OMPD_unroll: 13740 case OMPD_for: 13741 case OMPD_for_simd: 13742 case OMPD_sections: 13743 case OMPD_section: 13744 case OMPD_single: 13745 case OMPD_master: 13746 case OMPD_masked: 13747 case OMPD_critical: 13748 case OMPD_taskgroup: 13749 case OMPD_distribute: 13750 case OMPD_ordered: 13751 case OMPD_atomic: 13752 case OMPD_distribute_simd: 13753 case OMPD_requires: 13754 case OMPD_metadirective: 13755 llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause"); 13756 case OMPD_unknown: 13757 default: 13758 llvm_unreachable("Unknown OpenMP directive"); 13759 } 13760 break; 13761 case OMPC_schedule: 13762 switch (DKind) { 13763 case OMPD_parallel_for: 13764 case OMPD_parallel_for_simd: 13765 case OMPD_distribute_parallel_for: 13766 case OMPD_distribute_parallel_for_simd: 13767 case OMPD_teams_distribute_parallel_for: 13768 case OMPD_teams_distribute_parallel_for_simd: 13769 case OMPD_target_parallel_for: 13770 case OMPD_target_parallel_for_simd: 13771 case OMPD_target_teams_distribute_parallel_for: 13772 case OMPD_target_teams_distribute_parallel_for_simd: 13773 CaptureRegion = OMPD_parallel; 13774 break; 13775 case OMPD_for: 13776 case OMPD_for_simd: 13777 // Do not capture schedule-clause expressions. 13778 break; 13779 case OMPD_task: 13780 case OMPD_taskloop: 13781 case OMPD_taskloop_simd: 13782 case OMPD_master_taskloop: 13783 case OMPD_master_taskloop_simd: 13784 case OMPD_parallel_master_taskloop: 13785 case OMPD_parallel_master_taskloop_simd: 13786 case OMPD_target_data: 13787 case OMPD_target_enter_data: 13788 case OMPD_target_exit_data: 13789 case OMPD_target_update: 13790 case OMPD_teams: 13791 case OMPD_teams_distribute: 13792 case OMPD_teams_distribute_simd: 13793 case OMPD_target_teams_distribute: 13794 case OMPD_target_teams_distribute_simd: 13795 case OMPD_target: 13796 case OMPD_target_simd: 13797 case OMPD_target_parallel: 13798 case OMPD_cancel: 13799 case OMPD_parallel: 13800 case OMPD_parallel_master: 13801 case OMPD_parallel_sections: 13802 case OMPD_threadprivate: 13803 case OMPD_allocate: 13804 case OMPD_taskyield: 13805 case OMPD_barrier: 13806 case OMPD_taskwait: 13807 case OMPD_cancellation_point: 13808 case OMPD_flush: 13809 case OMPD_depobj: 13810 case OMPD_scan: 13811 case OMPD_declare_reduction: 13812 case OMPD_declare_mapper: 13813 case OMPD_declare_simd: 13814 case OMPD_declare_variant: 13815 case OMPD_begin_declare_variant: 13816 case OMPD_end_declare_variant: 13817 case OMPD_declare_target: 13818 case OMPD_end_declare_target: 13819 case OMPD_simd: 13820 case OMPD_tile: 13821 case OMPD_unroll: 13822 case OMPD_sections: 13823 case OMPD_section: 13824 case OMPD_single: 13825 case OMPD_master: 13826 case OMPD_masked: 13827 case OMPD_critical: 13828 case OMPD_taskgroup: 13829 case OMPD_distribute: 13830 case OMPD_ordered: 13831 case OMPD_atomic: 13832 case OMPD_distribute_simd: 13833 case OMPD_target_teams: 13834 case OMPD_requires: 13835 case OMPD_metadirective: 13836 llvm_unreachable("Unexpected OpenMP directive with schedule clause"); 13837 case OMPD_unknown: 13838 default: 13839 llvm_unreachable("Unknown OpenMP directive"); 13840 } 13841 break; 13842 case OMPC_dist_schedule: 13843 switch (DKind) { 13844 case OMPD_teams_distribute_parallel_for: 13845 case OMPD_teams_distribute_parallel_for_simd: 13846 case OMPD_teams_distribute: 13847 case OMPD_teams_distribute_simd: 13848 case OMPD_target_teams_distribute_parallel_for: 13849 case OMPD_target_teams_distribute_parallel_for_simd: 13850 case OMPD_target_teams_distribute: 13851 case OMPD_target_teams_distribute_simd: 13852 CaptureRegion = OMPD_teams; 13853 break; 13854 case OMPD_distribute_parallel_for: 13855 case OMPD_distribute_parallel_for_simd: 13856 case OMPD_distribute: 13857 case OMPD_distribute_simd: 13858 // Do not capture dist_schedule-clause expressions. 13859 break; 13860 case OMPD_parallel_for: 13861 case OMPD_parallel_for_simd: 13862 case OMPD_target_parallel_for_simd: 13863 case OMPD_target_parallel_for: 13864 case OMPD_task: 13865 case OMPD_taskloop: 13866 case OMPD_taskloop_simd: 13867 case OMPD_master_taskloop: 13868 case OMPD_master_taskloop_simd: 13869 case OMPD_parallel_master_taskloop: 13870 case OMPD_parallel_master_taskloop_simd: 13871 case OMPD_target_data: 13872 case OMPD_target_enter_data: 13873 case OMPD_target_exit_data: 13874 case OMPD_target_update: 13875 case OMPD_teams: 13876 case OMPD_target: 13877 case OMPD_target_simd: 13878 case OMPD_target_parallel: 13879 case OMPD_cancel: 13880 case OMPD_parallel: 13881 case OMPD_parallel_master: 13882 case OMPD_parallel_sections: 13883 case OMPD_threadprivate: 13884 case OMPD_allocate: 13885 case OMPD_taskyield: 13886 case OMPD_barrier: 13887 case OMPD_taskwait: 13888 case OMPD_cancellation_point: 13889 case OMPD_flush: 13890 case OMPD_depobj: 13891 case OMPD_scan: 13892 case OMPD_declare_reduction: 13893 case OMPD_declare_mapper: 13894 case OMPD_declare_simd: 13895 case OMPD_declare_variant: 13896 case OMPD_begin_declare_variant: 13897 case OMPD_end_declare_variant: 13898 case OMPD_declare_target: 13899 case OMPD_end_declare_target: 13900 case OMPD_simd: 13901 case OMPD_tile: 13902 case OMPD_unroll: 13903 case OMPD_for: 13904 case OMPD_for_simd: 13905 case OMPD_sections: 13906 case OMPD_section: 13907 case OMPD_single: 13908 case OMPD_master: 13909 case OMPD_masked: 13910 case OMPD_critical: 13911 case OMPD_taskgroup: 13912 case OMPD_ordered: 13913 case OMPD_atomic: 13914 case OMPD_target_teams: 13915 case OMPD_requires: 13916 case OMPD_metadirective: 13917 llvm_unreachable("Unexpected OpenMP directive with dist_schedule clause"); 13918 case OMPD_unknown: 13919 default: 13920 llvm_unreachable("Unknown OpenMP directive"); 13921 } 13922 break; 13923 case OMPC_device: 13924 switch (DKind) { 13925 case OMPD_target_update: 13926 case OMPD_target_enter_data: 13927 case OMPD_target_exit_data: 13928 case OMPD_target: 13929 case OMPD_target_simd: 13930 case OMPD_target_teams: 13931 case OMPD_target_parallel: 13932 case OMPD_target_teams_distribute: 13933 case OMPD_target_teams_distribute_simd: 13934 case OMPD_target_parallel_for: 13935 case OMPD_target_parallel_for_simd: 13936 case OMPD_target_teams_distribute_parallel_for: 13937 case OMPD_target_teams_distribute_parallel_for_simd: 13938 case OMPD_dispatch: 13939 CaptureRegion = OMPD_task; 13940 break; 13941 case OMPD_target_data: 13942 case OMPD_interop: 13943 // Do not capture device-clause expressions. 13944 break; 13945 case OMPD_teams_distribute_parallel_for: 13946 case OMPD_teams_distribute_parallel_for_simd: 13947 case OMPD_teams: 13948 case OMPD_teams_distribute: 13949 case OMPD_teams_distribute_simd: 13950 case OMPD_distribute_parallel_for: 13951 case OMPD_distribute_parallel_for_simd: 13952 case OMPD_task: 13953 case OMPD_taskloop: 13954 case OMPD_taskloop_simd: 13955 case OMPD_master_taskloop: 13956 case OMPD_master_taskloop_simd: 13957 case OMPD_parallel_master_taskloop: 13958 case OMPD_parallel_master_taskloop_simd: 13959 case OMPD_cancel: 13960 case OMPD_parallel: 13961 case OMPD_parallel_master: 13962 case OMPD_parallel_sections: 13963 case OMPD_parallel_for: 13964 case OMPD_parallel_for_simd: 13965 case OMPD_threadprivate: 13966 case OMPD_allocate: 13967 case OMPD_taskyield: 13968 case OMPD_barrier: 13969 case OMPD_taskwait: 13970 case OMPD_cancellation_point: 13971 case OMPD_flush: 13972 case OMPD_depobj: 13973 case OMPD_scan: 13974 case OMPD_declare_reduction: 13975 case OMPD_declare_mapper: 13976 case OMPD_declare_simd: 13977 case OMPD_declare_variant: 13978 case OMPD_begin_declare_variant: 13979 case OMPD_end_declare_variant: 13980 case OMPD_declare_target: 13981 case OMPD_end_declare_target: 13982 case OMPD_simd: 13983 case OMPD_tile: 13984 case OMPD_unroll: 13985 case OMPD_for: 13986 case OMPD_for_simd: 13987 case OMPD_sections: 13988 case OMPD_section: 13989 case OMPD_single: 13990 case OMPD_master: 13991 case OMPD_masked: 13992 case OMPD_critical: 13993 case OMPD_taskgroup: 13994 case OMPD_distribute: 13995 case OMPD_ordered: 13996 case OMPD_atomic: 13997 case OMPD_distribute_simd: 13998 case OMPD_requires: 13999 case OMPD_metadirective: 14000 llvm_unreachable("Unexpected OpenMP directive with device-clause"); 14001 case OMPD_unknown: 14002 default: 14003 llvm_unreachable("Unknown OpenMP directive"); 14004 } 14005 break; 14006 case OMPC_grainsize: 14007 case OMPC_num_tasks: 14008 case OMPC_final: 14009 case OMPC_priority: 14010 switch (DKind) { 14011 case OMPD_task: 14012 case OMPD_taskloop: 14013 case OMPD_taskloop_simd: 14014 case OMPD_master_taskloop: 14015 case OMPD_master_taskloop_simd: 14016 break; 14017 case OMPD_parallel_master_taskloop: 14018 case OMPD_parallel_master_taskloop_simd: 14019 CaptureRegion = OMPD_parallel; 14020 break; 14021 case OMPD_target_update: 14022 case OMPD_target_enter_data: 14023 case OMPD_target_exit_data: 14024 case OMPD_target: 14025 case OMPD_target_simd: 14026 case OMPD_target_teams: 14027 case OMPD_target_parallel: 14028 case OMPD_target_teams_distribute: 14029 case OMPD_target_teams_distribute_simd: 14030 case OMPD_target_parallel_for: 14031 case OMPD_target_parallel_for_simd: 14032 case OMPD_target_teams_distribute_parallel_for: 14033 case OMPD_target_teams_distribute_parallel_for_simd: 14034 case OMPD_target_data: 14035 case OMPD_teams_distribute_parallel_for: 14036 case OMPD_teams_distribute_parallel_for_simd: 14037 case OMPD_teams: 14038 case OMPD_teams_distribute: 14039 case OMPD_teams_distribute_simd: 14040 case OMPD_distribute_parallel_for: 14041 case OMPD_distribute_parallel_for_simd: 14042 case OMPD_cancel: 14043 case OMPD_parallel: 14044 case OMPD_parallel_master: 14045 case OMPD_parallel_sections: 14046 case OMPD_parallel_for: 14047 case OMPD_parallel_for_simd: 14048 case OMPD_threadprivate: 14049 case OMPD_allocate: 14050 case OMPD_taskyield: 14051 case OMPD_barrier: 14052 case OMPD_taskwait: 14053 case OMPD_cancellation_point: 14054 case OMPD_flush: 14055 case OMPD_depobj: 14056 case OMPD_scan: 14057 case OMPD_declare_reduction: 14058 case OMPD_declare_mapper: 14059 case OMPD_declare_simd: 14060 case OMPD_declare_variant: 14061 case OMPD_begin_declare_variant: 14062 case OMPD_end_declare_variant: 14063 case OMPD_declare_target: 14064 case OMPD_end_declare_target: 14065 case OMPD_simd: 14066 case OMPD_tile: 14067 case OMPD_unroll: 14068 case OMPD_for: 14069 case OMPD_for_simd: 14070 case OMPD_sections: 14071 case OMPD_section: 14072 case OMPD_single: 14073 case OMPD_master: 14074 case OMPD_masked: 14075 case OMPD_critical: 14076 case OMPD_taskgroup: 14077 case OMPD_distribute: 14078 case OMPD_ordered: 14079 case OMPD_atomic: 14080 case OMPD_distribute_simd: 14081 case OMPD_requires: 14082 case OMPD_metadirective: 14083 llvm_unreachable("Unexpected OpenMP directive with grainsize-clause"); 14084 case OMPD_unknown: 14085 default: 14086 llvm_unreachable("Unknown OpenMP directive"); 14087 } 14088 break; 14089 case OMPC_novariants: 14090 case OMPC_nocontext: 14091 switch (DKind) { 14092 case OMPD_dispatch: 14093 CaptureRegion = OMPD_task; 14094 break; 14095 default: 14096 llvm_unreachable("Unexpected OpenMP directive"); 14097 } 14098 break; 14099 case OMPC_filter: 14100 // Do not capture filter-clause expressions. 14101 break; 14102 case OMPC_when: 14103 if (DKind == OMPD_metadirective) { 14104 CaptureRegion = OMPD_metadirective; 14105 } else if (DKind == OMPD_unknown) { 14106 llvm_unreachable("Unknown OpenMP directive"); 14107 } else { 14108 llvm_unreachable("Unexpected OpenMP directive with when clause"); 14109 } 14110 break; 14111 case OMPC_firstprivate: 14112 case OMPC_lastprivate: 14113 case OMPC_reduction: 14114 case OMPC_task_reduction: 14115 case OMPC_in_reduction: 14116 case OMPC_linear: 14117 case OMPC_default: 14118 case OMPC_proc_bind: 14119 case OMPC_safelen: 14120 case OMPC_simdlen: 14121 case OMPC_sizes: 14122 case OMPC_allocator: 14123 case OMPC_collapse: 14124 case OMPC_private: 14125 case OMPC_shared: 14126 case OMPC_aligned: 14127 case OMPC_copyin: 14128 case OMPC_copyprivate: 14129 case OMPC_ordered: 14130 case OMPC_nowait: 14131 case OMPC_untied: 14132 case OMPC_mergeable: 14133 case OMPC_threadprivate: 14134 case OMPC_allocate: 14135 case OMPC_flush: 14136 case OMPC_depobj: 14137 case OMPC_read: 14138 case OMPC_write: 14139 case OMPC_update: 14140 case OMPC_capture: 14141 case OMPC_seq_cst: 14142 case OMPC_acq_rel: 14143 case OMPC_acquire: 14144 case OMPC_release: 14145 case OMPC_relaxed: 14146 case OMPC_depend: 14147 case OMPC_threads: 14148 case OMPC_simd: 14149 case OMPC_map: 14150 case OMPC_nogroup: 14151 case OMPC_hint: 14152 case OMPC_defaultmap: 14153 case OMPC_unknown: 14154 case OMPC_uniform: 14155 case OMPC_to: 14156 case OMPC_from: 14157 case OMPC_use_device_ptr: 14158 case OMPC_use_device_addr: 14159 case OMPC_is_device_ptr: 14160 case OMPC_unified_address: 14161 case OMPC_unified_shared_memory: 14162 case OMPC_reverse_offload: 14163 case OMPC_dynamic_allocators: 14164 case OMPC_atomic_default_mem_order: 14165 case OMPC_device_type: 14166 case OMPC_match: 14167 case OMPC_nontemporal: 14168 case OMPC_order: 14169 case OMPC_destroy: 14170 case OMPC_detach: 14171 case OMPC_inclusive: 14172 case OMPC_exclusive: 14173 case OMPC_uses_allocators: 14174 case OMPC_affinity: 14175 default: 14176 llvm_unreachable("Unexpected OpenMP clause."); 14177 } 14178 return CaptureRegion; 14179 } 14180 14181 OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier, 14182 Expr *Condition, SourceLocation StartLoc, 14183 SourceLocation LParenLoc, 14184 SourceLocation NameModifierLoc, 14185 SourceLocation ColonLoc, 14186 SourceLocation EndLoc) { 14187 Expr *ValExpr = Condition; 14188 Stmt *HelperValStmt = nullptr; 14189 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 14190 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 14191 !Condition->isInstantiationDependent() && 14192 !Condition->containsUnexpandedParameterPack()) { 14193 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 14194 if (Val.isInvalid()) 14195 return nullptr; 14196 14197 ValExpr = Val.get(); 14198 14199 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 14200 CaptureRegion = getOpenMPCaptureRegionForClause( 14201 DKind, OMPC_if, LangOpts.OpenMP, NameModifier); 14202 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 14203 ValExpr = MakeFullExpr(ValExpr).get(); 14204 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 14205 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 14206 HelperValStmt = buildPreInits(Context, Captures); 14207 } 14208 } 14209 14210 return new (Context) 14211 OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc, 14212 LParenLoc, NameModifierLoc, ColonLoc, EndLoc); 14213 } 14214 14215 OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition, 14216 SourceLocation StartLoc, 14217 SourceLocation LParenLoc, 14218 SourceLocation EndLoc) { 14219 Expr *ValExpr = Condition; 14220 Stmt *HelperValStmt = nullptr; 14221 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 14222 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 14223 !Condition->isInstantiationDependent() && 14224 !Condition->containsUnexpandedParameterPack()) { 14225 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 14226 if (Val.isInvalid()) 14227 return nullptr; 14228 14229 ValExpr = MakeFullExpr(Val.get()).get(); 14230 14231 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 14232 CaptureRegion = 14233 getOpenMPCaptureRegionForClause(DKind, OMPC_final, LangOpts.OpenMP); 14234 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 14235 ValExpr = MakeFullExpr(ValExpr).get(); 14236 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 14237 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 14238 HelperValStmt = buildPreInits(Context, Captures); 14239 } 14240 } 14241 14242 return new (Context) OMPFinalClause(ValExpr, HelperValStmt, CaptureRegion, 14243 StartLoc, LParenLoc, EndLoc); 14244 } 14245 14246 ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc, 14247 Expr *Op) { 14248 if (!Op) 14249 return ExprError(); 14250 14251 class IntConvertDiagnoser : public ICEConvertDiagnoser { 14252 public: 14253 IntConvertDiagnoser() 14254 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {} 14255 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, 14256 QualType T) override { 14257 return S.Diag(Loc, diag::err_omp_not_integral) << T; 14258 } 14259 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc, 14260 QualType T) override { 14261 return S.Diag(Loc, diag::err_omp_incomplete_type) << T; 14262 } 14263 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc, 14264 QualType T, 14265 QualType ConvTy) override { 14266 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy; 14267 } 14268 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv, 14269 QualType ConvTy) override { 14270 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 14271 << ConvTy->isEnumeralType() << ConvTy; 14272 } 14273 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc, 14274 QualType T) override { 14275 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T; 14276 } 14277 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv, 14278 QualType ConvTy) override { 14279 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 14280 << ConvTy->isEnumeralType() << ConvTy; 14281 } 14282 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType, 14283 QualType) override { 14284 llvm_unreachable("conversion functions are permitted"); 14285 } 14286 } ConvertDiagnoser; 14287 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser); 14288 } 14289 14290 static bool 14291 isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef, OpenMPClauseKind CKind, 14292 bool StrictlyPositive, bool BuildCapture = false, 14293 OpenMPDirectiveKind DKind = OMPD_unknown, 14294 OpenMPDirectiveKind *CaptureRegion = nullptr, 14295 Stmt **HelperValStmt = nullptr) { 14296 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() && 14297 !ValExpr->isInstantiationDependent()) { 14298 SourceLocation Loc = ValExpr->getExprLoc(); 14299 ExprResult Value = 14300 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr); 14301 if (Value.isInvalid()) 14302 return false; 14303 14304 ValExpr = Value.get(); 14305 // The expression must evaluate to a non-negative integer value. 14306 if (Optional<llvm::APSInt> Result = 14307 ValExpr->getIntegerConstantExpr(SemaRef.Context)) { 14308 if (Result->isSigned() && 14309 !((!StrictlyPositive && Result->isNonNegative()) || 14310 (StrictlyPositive && Result->isStrictlyPositive()))) { 14311 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause) 14312 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) 14313 << ValExpr->getSourceRange(); 14314 return false; 14315 } 14316 } 14317 if (!BuildCapture) 14318 return true; 14319 *CaptureRegion = 14320 getOpenMPCaptureRegionForClause(DKind, CKind, SemaRef.LangOpts.OpenMP); 14321 if (*CaptureRegion != OMPD_unknown && 14322 !SemaRef.CurContext->isDependentContext()) { 14323 ValExpr = SemaRef.MakeFullExpr(ValExpr).get(); 14324 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 14325 ValExpr = tryBuildCapture(SemaRef, ValExpr, Captures).get(); 14326 *HelperValStmt = buildPreInits(SemaRef.Context, Captures); 14327 } 14328 } 14329 return true; 14330 } 14331 14332 OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads, 14333 SourceLocation StartLoc, 14334 SourceLocation LParenLoc, 14335 SourceLocation EndLoc) { 14336 Expr *ValExpr = NumThreads; 14337 Stmt *HelperValStmt = nullptr; 14338 14339 // OpenMP [2.5, Restrictions] 14340 // The num_threads expression must evaluate to a positive integer value. 14341 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads, 14342 /*StrictlyPositive=*/true)) 14343 return nullptr; 14344 14345 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 14346 OpenMPDirectiveKind CaptureRegion = 14347 getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads, LangOpts.OpenMP); 14348 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 14349 ValExpr = MakeFullExpr(ValExpr).get(); 14350 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 14351 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 14352 HelperValStmt = buildPreInits(Context, Captures); 14353 } 14354 14355 return new (Context) OMPNumThreadsClause( 14356 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc); 14357 } 14358 14359 ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E, 14360 OpenMPClauseKind CKind, 14361 bool StrictlyPositive, 14362 bool SuppressExprDiags) { 14363 if (!E) 14364 return ExprError(); 14365 if (E->isValueDependent() || E->isTypeDependent() || 14366 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 14367 return E; 14368 14369 llvm::APSInt Result; 14370 ExprResult ICE; 14371 if (SuppressExprDiags) { 14372 // Use a custom diagnoser that suppresses 'note' diagnostics about the 14373 // expression. 14374 struct SuppressedDiagnoser : public Sema::VerifyICEDiagnoser { 14375 SuppressedDiagnoser() : VerifyICEDiagnoser(/*Suppress=*/true) {} 14376 Sema::SemaDiagnosticBuilder diagnoseNotICE(Sema &S, 14377 SourceLocation Loc) override { 14378 llvm_unreachable("Diagnostic suppressed"); 14379 } 14380 } Diagnoser; 14381 ICE = VerifyIntegerConstantExpression(E, &Result, Diagnoser, AllowFold); 14382 } else { 14383 ICE = VerifyIntegerConstantExpression(E, &Result, /*FIXME*/ AllowFold); 14384 } 14385 if (ICE.isInvalid()) 14386 return ExprError(); 14387 14388 if ((StrictlyPositive && !Result.isStrictlyPositive()) || 14389 (!StrictlyPositive && !Result.isNonNegative())) { 14390 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause) 14391 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) 14392 << E->getSourceRange(); 14393 return ExprError(); 14394 } 14395 if (CKind == OMPC_aligned && !Result.isPowerOf2()) { 14396 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two) 14397 << E->getSourceRange(); 14398 return ExprError(); 14399 } 14400 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1) 14401 DSAStack->setAssociatedLoops(Result.getExtValue()); 14402 else if (CKind == OMPC_ordered) 14403 DSAStack->setAssociatedLoops(Result.getExtValue()); 14404 return ICE; 14405 } 14406 14407 OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc, 14408 SourceLocation LParenLoc, 14409 SourceLocation EndLoc) { 14410 // OpenMP [2.8.1, simd construct, Description] 14411 // The parameter of the safelen clause must be a constant 14412 // positive integer expression. 14413 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen); 14414 if (Safelen.isInvalid()) 14415 return nullptr; 14416 return new (Context) 14417 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc); 14418 } 14419 14420 OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc, 14421 SourceLocation LParenLoc, 14422 SourceLocation EndLoc) { 14423 // OpenMP [2.8.1, simd construct, Description] 14424 // The parameter of the simdlen clause must be a constant 14425 // positive integer expression. 14426 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen); 14427 if (Simdlen.isInvalid()) 14428 return nullptr; 14429 return new (Context) 14430 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc); 14431 } 14432 14433 /// Tries to find omp_allocator_handle_t type. 14434 static bool findOMPAllocatorHandleT(Sema &S, SourceLocation Loc, 14435 DSAStackTy *Stack) { 14436 QualType OMPAllocatorHandleT = Stack->getOMPAllocatorHandleT(); 14437 if (!OMPAllocatorHandleT.isNull()) 14438 return true; 14439 // Build the predefined allocator expressions. 14440 bool ErrorFound = false; 14441 for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) { 14442 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I); 14443 StringRef Allocator = 14444 OMPAllocateDeclAttr::ConvertAllocatorTypeTyToStr(AllocatorKind); 14445 DeclarationName AllocatorName = &S.getASTContext().Idents.get(Allocator); 14446 auto *VD = dyn_cast_or_null<ValueDecl>( 14447 S.LookupSingleName(S.TUScope, AllocatorName, Loc, Sema::LookupAnyName)); 14448 if (!VD) { 14449 ErrorFound = true; 14450 break; 14451 } 14452 QualType AllocatorType = 14453 VD->getType().getNonLValueExprType(S.getASTContext()); 14454 ExprResult Res = S.BuildDeclRefExpr(VD, AllocatorType, VK_LValue, Loc); 14455 if (!Res.isUsable()) { 14456 ErrorFound = true; 14457 break; 14458 } 14459 if (OMPAllocatorHandleT.isNull()) 14460 OMPAllocatorHandleT = AllocatorType; 14461 if (!S.getASTContext().hasSameType(OMPAllocatorHandleT, AllocatorType)) { 14462 ErrorFound = true; 14463 break; 14464 } 14465 Stack->setAllocator(AllocatorKind, Res.get()); 14466 } 14467 if (ErrorFound) { 14468 S.Diag(Loc, diag::err_omp_implied_type_not_found) 14469 << "omp_allocator_handle_t"; 14470 return false; 14471 } 14472 OMPAllocatorHandleT.addConst(); 14473 Stack->setOMPAllocatorHandleT(OMPAllocatorHandleT); 14474 return true; 14475 } 14476 14477 OMPClause *Sema::ActOnOpenMPAllocatorClause(Expr *A, SourceLocation StartLoc, 14478 SourceLocation LParenLoc, 14479 SourceLocation EndLoc) { 14480 // OpenMP [2.11.3, allocate Directive, Description] 14481 // allocator is an expression of omp_allocator_handle_t type. 14482 if (!findOMPAllocatorHandleT(*this, A->getExprLoc(), DSAStack)) 14483 return nullptr; 14484 14485 ExprResult Allocator = DefaultLvalueConversion(A); 14486 if (Allocator.isInvalid()) 14487 return nullptr; 14488 Allocator = PerformImplicitConversion(Allocator.get(), 14489 DSAStack->getOMPAllocatorHandleT(), 14490 Sema::AA_Initializing, 14491 /*AllowExplicit=*/true); 14492 if (Allocator.isInvalid()) 14493 return nullptr; 14494 return new (Context) 14495 OMPAllocatorClause(Allocator.get(), StartLoc, LParenLoc, EndLoc); 14496 } 14497 14498 OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops, 14499 SourceLocation StartLoc, 14500 SourceLocation LParenLoc, 14501 SourceLocation EndLoc) { 14502 // OpenMP [2.7.1, loop construct, Description] 14503 // OpenMP [2.8.1, simd construct, Description] 14504 // OpenMP [2.9.6, distribute construct, Description] 14505 // The parameter of the collapse clause must be a constant 14506 // positive integer expression. 14507 ExprResult NumForLoopsResult = 14508 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse); 14509 if (NumForLoopsResult.isInvalid()) 14510 return nullptr; 14511 return new (Context) 14512 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc); 14513 } 14514 14515 OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc, 14516 SourceLocation EndLoc, 14517 SourceLocation LParenLoc, 14518 Expr *NumForLoops) { 14519 // OpenMP [2.7.1, loop construct, Description] 14520 // OpenMP [2.8.1, simd construct, Description] 14521 // OpenMP [2.9.6, distribute construct, Description] 14522 // The parameter of the ordered clause must be a constant 14523 // positive integer expression if any. 14524 if (NumForLoops && LParenLoc.isValid()) { 14525 ExprResult NumForLoopsResult = 14526 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered); 14527 if (NumForLoopsResult.isInvalid()) 14528 return nullptr; 14529 NumForLoops = NumForLoopsResult.get(); 14530 } else { 14531 NumForLoops = nullptr; 14532 } 14533 auto *Clause = OMPOrderedClause::Create( 14534 Context, NumForLoops, NumForLoops ? DSAStack->getAssociatedLoops() : 0, 14535 StartLoc, LParenLoc, EndLoc); 14536 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops, Clause); 14537 return Clause; 14538 } 14539 14540 OMPClause *Sema::ActOnOpenMPSimpleClause( 14541 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc, 14542 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 14543 OMPClause *Res = nullptr; 14544 switch (Kind) { 14545 case OMPC_default: 14546 Res = ActOnOpenMPDefaultClause(static_cast<DefaultKind>(Argument), 14547 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 14548 break; 14549 case OMPC_proc_bind: 14550 Res = ActOnOpenMPProcBindClause(static_cast<ProcBindKind>(Argument), 14551 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 14552 break; 14553 case OMPC_atomic_default_mem_order: 14554 Res = ActOnOpenMPAtomicDefaultMemOrderClause( 14555 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Argument), 14556 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 14557 break; 14558 case OMPC_order: 14559 Res = ActOnOpenMPOrderClause(static_cast<OpenMPOrderClauseKind>(Argument), 14560 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 14561 break; 14562 case OMPC_update: 14563 Res = ActOnOpenMPUpdateClause(static_cast<OpenMPDependClauseKind>(Argument), 14564 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 14565 break; 14566 case OMPC_if: 14567 case OMPC_final: 14568 case OMPC_num_threads: 14569 case OMPC_safelen: 14570 case OMPC_simdlen: 14571 case OMPC_sizes: 14572 case OMPC_allocator: 14573 case OMPC_collapse: 14574 case OMPC_schedule: 14575 case OMPC_private: 14576 case OMPC_firstprivate: 14577 case OMPC_lastprivate: 14578 case OMPC_shared: 14579 case OMPC_reduction: 14580 case OMPC_task_reduction: 14581 case OMPC_in_reduction: 14582 case OMPC_linear: 14583 case OMPC_aligned: 14584 case OMPC_copyin: 14585 case OMPC_copyprivate: 14586 case OMPC_ordered: 14587 case OMPC_nowait: 14588 case OMPC_untied: 14589 case OMPC_mergeable: 14590 case OMPC_threadprivate: 14591 case OMPC_allocate: 14592 case OMPC_flush: 14593 case OMPC_depobj: 14594 case OMPC_read: 14595 case OMPC_write: 14596 case OMPC_capture: 14597 case OMPC_seq_cst: 14598 case OMPC_acq_rel: 14599 case OMPC_acquire: 14600 case OMPC_release: 14601 case OMPC_relaxed: 14602 case OMPC_depend: 14603 case OMPC_device: 14604 case OMPC_threads: 14605 case OMPC_simd: 14606 case OMPC_map: 14607 case OMPC_num_teams: 14608 case OMPC_thread_limit: 14609 case OMPC_priority: 14610 case OMPC_grainsize: 14611 case OMPC_nogroup: 14612 case OMPC_num_tasks: 14613 case OMPC_hint: 14614 case OMPC_dist_schedule: 14615 case OMPC_defaultmap: 14616 case OMPC_unknown: 14617 case OMPC_uniform: 14618 case OMPC_to: 14619 case OMPC_from: 14620 case OMPC_use_device_ptr: 14621 case OMPC_use_device_addr: 14622 case OMPC_is_device_ptr: 14623 case OMPC_unified_address: 14624 case OMPC_unified_shared_memory: 14625 case OMPC_reverse_offload: 14626 case OMPC_dynamic_allocators: 14627 case OMPC_device_type: 14628 case OMPC_match: 14629 case OMPC_nontemporal: 14630 case OMPC_destroy: 14631 case OMPC_novariants: 14632 case OMPC_nocontext: 14633 case OMPC_detach: 14634 case OMPC_inclusive: 14635 case OMPC_exclusive: 14636 case OMPC_uses_allocators: 14637 case OMPC_affinity: 14638 case OMPC_when: 14639 default: 14640 llvm_unreachable("Clause is not allowed."); 14641 } 14642 return Res; 14643 } 14644 14645 static std::string 14646 getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last, 14647 ArrayRef<unsigned> Exclude = llvm::None) { 14648 SmallString<256> Buffer; 14649 llvm::raw_svector_ostream Out(Buffer); 14650 unsigned Skipped = Exclude.size(); 14651 auto S = Exclude.begin(), E = Exclude.end(); 14652 for (unsigned I = First; I < Last; ++I) { 14653 if (std::find(S, E, I) != E) { 14654 --Skipped; 14655 continue; 14656 } 14657 Out << "'" << getOpenMPSimpleClauseTypeName(K, I) << "'"; 14658 if (I + Skipped + 2 == Last) 14659 Out << " or "; 14660 else if (I + Skipped + 1 != Last) 14661 Out << ", "; 14662 } 14663 return std::string(Out.str()); 14664 } 14665 14666 OMPClause *Sema::ActOnOpenMPDefaultClause(DefaultKind Kind, 14667 SourceLocation KindKwLoc, 14668 SourceLocation StartLoc, 14669 SourceLocation LParenLoc, 14670 SourceLocation EndLoc) { 14671 if (Kind == OMP_DEFAULT_unknown) { 14672 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 14673 << getListOfPossibleValues(OMPC_default, /*First=*/0, 14674 /*Last=*/unsigned(OMP_DEFAULT_unknown)) 14675 << getOpenMPClauseName(OMPC_default); 14676 return nullptr; 14677 } 14678 14679 switch (Kind) { 14680 case OMP_DEFAULT_none: 14681 DSAStack->setDefaultDSANone(KindKwLoc); 14682 break; 14683 case OMP_DEFAULT_shared: 14684 DSAStack->setDefaultDSAShared(KindKwLoc); 14685 break; 14686 case OMP_DEFAULT_firstprivate: 14687 DSAStack->setDefaultDSAFirstPrivate(KindKwLoc); 14688 break; 14689 default: 14690 llvm_unreachable("DSA unexpected in OpenMP default clause"); 14691 } 14692 14693 return new (Context) 14694 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 14695 } 14696 14697 OMPClause *Sema::ActOnOpenMPProcBindClause(ProcBindKind Kind, 14698 SourceLocation KindKwLoc, 14699 SourceLocation StartLoc, 14700 SourceLocation LParenLoc, 14701 SourceLocation EndLoc) { 14702 if (Kind == OMP_PROC_BIND_unknown) { 14703 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 14704 << getListOfPossibleValues(OMPC_proc_bind, 14705 /*First=*/unsigned(OMP_PROC_BIND_master), 14706 /*Last=*/ 14707 unsigned(LangOpts.OpenMP > 50 14708 ? OMP_PROC_BIND_primary 14709 : OMP_PROC_BIND_spread) + 14710 1) 14711 << getOpenMPClauseName(OMPC_proc_bind); 14712 return nullptr; 14713 } 14714 if (Kind == OMP_PROC_BIND_primary && LangOpts.OpenMP < 51) 14715 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 14716 << getListOfPossibleValues(OMPC_proc_bind, 14717 /*First=*/unsigned(OMP_PROC_BIND_master), 14718 /*Last=*/ 14719 unsigned(OMP_PROC_BIND_spread) + 1) 14720 << getOpenMPClauseName(OMPC_proc_bind); 14721 return new (Context) 14722 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 14723 } 14724 14725 OMPClause *Sema::ActOnOpenMPAtomicDefaultMemOrderClause( 14726 OpenMPAtomicDefaultMemOrderClauseKind Kind, SourceLocation KindKwLoc, 14727 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 14728 if (Kind == OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) { 14729 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 14730 << getListOfPossibleValues( 14731 OMPC_atomic_default_mem_order, /*First=*/0, 14732 /*Last=*/OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) 14733 << getOpenMPClauseName(OMPC_atomic_default_mem_order); 14734 return nullptr; 14735 } 14736 return new (Context) OMPAtomicDefaultMemOrderClause(Kind, KindKwLoc, StartLoc, 14737 LParenLoc, EndLoc); 14738 } 14739 14740 OMPClause *Sema::ActOnOpenMPOrderClause(OpenMPOrderClauseKind Kind, 14741 SourceLocation KindKwLoc, 14742 SourceLocation StartLoc, 14743 SourceLocation LParenLoc, 14744 SourceLocation EndLoc) { 14745 if (Kind == OMPC_ORDER_unknown) { 14746 static_assert(OMPC_ORDER_unknown > 0, 14747 "OMPC_ORDER_unknown not greater than 0"); 14748 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 14749 << getListOfPossibleValues(OMPC_order, /*First=*/0, 14750 /*Last=*/OMPC_ORDER_unknown) 14751 << getOpenMPClauseName(OMPC_order); 14752 return nullptr; 14753 } 14754 return new (Context) 14755 OMPOrderClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 14756 } 14757 14758 OMPClause *Sema::ActOnOpenMPUpdateClause(OpenMPDependClauseKind Kind, 14759 SourceLocation KindKwLoc, 14760 SourceLocation StartLoc, 14761 SourceLocation LParenLoc, 14762 SourceLocation EndLoc) { 14763 if (Kind == OMPC_DEPEND_unknown || Kind == OMPC_DEPEND_source || 14764 Kind == OMPC_DEPEND_sink || Kind == OMPC_DEPEND_depobj) { 14765 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink, 14766 OMPC_DEPEND_depobj}; 14767 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 14768 << getListOfPossibleValues(OMPC_depend, /*First=*/0, 14769 /*Last=*/OMPC_DEPEND_unknown, Except) 14770 << getOpenMPClauseName(OMPC_update); 14771 return nullptr; 14772 } 14773 return OMPUpdateClause::Create(Context, StartLoc, LParenLoc, KindKwLoc, Kind, 14774 EndLoc); 14775 } 14776 14777 OMPClause *Sema::ActOnOpenMPSizesClause(ArrayRef<Expr *> SizeExprs, 14778 SourceLocation StartLoc, 14779 SourceLocation LParenLoc, 14780 SourceLocation EndLoc) { 14781 for (Expr *SizeExpr : SizeExprs) { 14782 ExprResult NumForLoopsResult = VerifyPositiveIntegerConstantInClause( 14783 SizeExpr, OMPC_sizes, /*StrictlyPositive=*/true); 14784 if (!NumForLoopsResult.isUsable()) 14785 return nullptr; 14786 } 14787 14788 DSAStack->setAssociatedLoops(SizeExprs.size()); 14789 return OMPSizesClause::Create(Context, StartLoc, LParenLoc, EndLoc, 14790 SizeExprs); 14791 } 14792 14793 OMPClause *Sema::ActOnOpenMPFullClause(SourceLocation StartLoc, 14794 SourceLocation EndLoc) { 14795 return OMPFullClause::Create(Context, StartLoc, EndLoc); 14796 } 14797 14798 OMPClause *Sema::ActOnOpenMPPartialClause(Expr *FactorExpr, 14799 SourceLocation StartLoc, 14800 SourceLocation LParenLoc, 14801 SourceLocation EndLoc) { 14802 if (FactorExpr) { 14803 // If an argument is specified, it must be a constant (or an unevaluated 14804 // template expression). 14805 ExprResult FactorResult = VerifyPositiveIntegerConstantInClause( 14806 FactorExpr, OMPC_partial, /*StrictlyPositive=*/true); 14807 if (FactorResult.isInvalid()) 14808 return nullptr; 14809 FactorExpr = FactorResult.get(); 14810 } 14811 14812 return OMPPartialClause::Create(Context, StartLoc, LParenLoc, EndLoc, 14813 FactorExpr); 14814 } 14815 14816 OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause( 14817 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr, 14818 SourceLocation StartLoc, SourceLocation LParenLoc, 14819 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc, 14820 SourceLocation EndLoc) { 14821 OMPClause *Res = nullptr; 14822 switch (Kind) { 14823 case OMPC_schedule: 14824 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements }; 14825 assert(Argument.size() == NumberOfElements && 14826 ArgumentLoc.size() == NumberOfElements); 14827 Res = ActOnOpenMPScheduleClause( 14828 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]), 14829 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]), 14830 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr, 14831 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2], 14832 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc); 14833 break; 14834 case OMPC_if: 14835 assert(Argument.size() == 1 && ArgumentLoc.size() == 1); 14836 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()), 14837 Expr, StartLoc, LParenLoc, ArgumentLoc.back(), 14838 DelimLoc, EndLoc); 14839 break; 14840 case OMPC_dist_schedule: 14841 Res = ActOnOpenMPDistScheduleClause( 14842 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr, 14843 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc); 14844 break; 14845 case OMPC_defaultmap: 14846 enum { Modifier, DefaultmapKind }; 14847 Res = ActOnOpenMPDefaultmapClause( 14848 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]), 14849 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]), 14850 StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind], 14851 EndLoc); 14852 break; 14853 case OMPC_device: 14854 assert(Argument.size() == 1 && ArgumentLoc.size() == 1); 14855 Res = ActOnOpenMPDeviceClause( 14856 static_cast<OpenMPDeviceClauseModifier>(Argument.back()), Expr, 14857 StartLoc, LParenLoc, ArgumentLoc.back(), EndLoc); 14858 break; 14859 case OMPC_final: 14860 case OMPC_num_threads: 14861 case OMPC_safelen: 14862 case OMPC_simdlen: 14863 case OMPC_sizes: 14864 case OMPC_allocator: 14865 case OMPC_collapse: 14866 case OMPC_default: 14867 case OMPC_proc_bind: 14868 case OMPC_private: 14869 case OMPC_firstprivate: 14870 case OMPC_lastprivate: 14871 case OMPC_shared: 14872 case OMPC_reduction: 14873 case OMPC_task_reduction: 14874 case OMPC_in_reduction: 14875 case OMPC_linear: 14876 case OMPC_aligned: 14877 case OMPC_copyin: 14878 case OMPC_copyprivate: 14879 case OMPC_ordered: 14880 case OMPC_nowait: 14881 case OMPC_untied: 14882 case OMPC_mergeable: 14883 case OMPC_threadprivate: 14884 case OMPC_allocate: 14885 case OMPC_flush: 14886 case OMPC_depobj: 14887 case OMPC_read: 14888 case OMPC_write: 14889 case OMPC_update: 14890 case OMPC_capture: 14891 case OMPC_seq_cst: 14892 case OMPC_acq_rel: 14893 case OMPC_acquire: 14894 case OMPC_release: 14895 case OMPC_relaxed: 14896 case OMPC_depend: 14897 case OMPC_threads: 14898 case OMPC_simd: 14899 case OMPC_map: 14900 case OMPC_num_teams: 14901 case OMPC_thread_limit: 14902 case OMPC_priority: 14903 case OMPC_grainsize: 14904 case OMPC_nogroup: 14905 case OMPC_num_tasks: 14906 case OMPC_hint: 14907 case OMPC_unknown: 14908 case OMPC_uniform: 14909 case OMPC_to: 14910 case OMPC_from: 14911 case OMPC_use_device_ptr: 14912 case OMPC_use_device_addr: 14913 case OMPC_is_device_ptr: 14914 case OMPC_unified_address: 14915 case OMPC_unified_shared_memory: 14916 case OMPC_reverse_offload: 14917 case OMPC_dynamic_allocators: 14918 case OMPC_atomic_default_mem_order: 14919 case OMPC_device_type: 14920 case OMPC_match: 14921 case OMPC_nontemporal: 14922 case OMPC_order: 14923 case OMPC_destroy: 14924 case OMPC_novariants: 14925 case OMPC_nocontext: 14926 case OMPC_detach: 14927 case OMPC_inclusive: 14928 case OMPC_exclusive: 14929 case OMPC_uses_allocators: 14930 case OMPC_affinity: 14931 case OMPC_when: 14932 default: 14933 llvm_unreachable("Clause is not allowed."); 14934 } 14935 return Res; 14936 } 14937 14938 static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1, 14939 OpenMPScheduleClauseModifier M2, 14940 SourceLocation M1Loc, SourceLocation M2Loc) { 14941 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) { 14942 SmallVector<unsigned, 2> Excluded; 14943 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown) 14944 Excluded.push_back(M2); 14945 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) 14946 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic); 14947 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic) 14948 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic); 14949 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value) 14950 << getListOfPossibleValues(OMPC_schedule, 14951 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1, 14952 /*Last=*/OMPC_SCHEDULE_MODIFIER_last, 14953 Excluded) 14954 << getOpenMPClauseName(OMPC_schedule); 14955 return true; 14956 } 14957 return false; 14958 } 14959 14960 OMPClause *Sema::ActOnOpenMPScheduleClause( 14961 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2, 14962 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, 14963 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc, 14964 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) { 14965 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) || 14966 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc)) 14967 return nullptr; 14968 // OpenMP, 2.7.1, Loop Construct, Restrictions 14969 // Either the monotonic modifier or the nonmonotonic modifier can be specified 14970 // but not both. 14971 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) || 14972 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic && 14973 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) || 14974 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic && 14975 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) { 14976 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier) 14977 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2) 14978 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1); 14979 return nullptr; 14980 } 14981 if (Kind == OMPC_SCHEDULE_unknown) { 14982 std::string Values; 14983 if (M1Loc.isInvalid() && M2Loc.isInvalid()) { 14984 unsigned Exclude[] = {OMPC_SCHEDULE_unknown}; 14985 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0, 14986 /*Last=*/OMPC_SCHEDULE_MODIFIER_last, 14987 Exclude); 14988 } else { 14989 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0, 14990 /*Last=*/OMPC_SCHEDULE_unknown); 14991 } 14992 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 14993 << Values << getOpenMPClauseName(OMPC_schedule); 14994 return nullptr; 14995 } 14996 // OpenMP, 2.7.1, Loop Construct, Restrictions 14997 // The nonmonotonic modifier can only be specified with schedule(dynamic) or 14998 // schedule(guided). 14999 // OpenMP 5.0 does not have this restriction. 15000 if (LangOpts.OpenMP < 50 && 15001 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic || 15002 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) && 15003 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) { 15004 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc, 15005 diag::err_omp_schedule_nonmonotonic_static); 15006 return nullptr; 15007 } 15008 Expr *ValExpr = ChunkSize; 15009 Stmt *HelperValStmt = nullptr; 15010 if (ChunkSize) { 15011 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && 15012 !ChunkSize->isInstantiationDependent() && 15013 !ChunkSize->containsUnexpandedParameterPack()) { 15014 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc(); 15015 ExprResult Val = 15016 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); 15017 if (Val.isInvalid()) 15018 return nullptr; 15019 15020 ValExpr = Val.get(); 15021 15022 // OpenMP [2.7.1, Restrictions] 15023 // chunk_size must be a loop invariant integer expression with a positive 15024 // value. 15025 if (Optional<llvm::APSInt> Result = 15026 ValExpr->getIntegerConstantExpr(Context)) { 15027 if (Result->isSigned() && !Result->isStrictlyPositive()) { 15028 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) 15029 << "schedule" << 1 << ChunkSize->getSourceRange(); 15030 return nullptr; 15031 } 15032 } else if (getOpenMPCaptureRegionForClause( 15033 DSAStack->getCurrentDirective(), OMPC_schedule, 15034 LangOpts.OpenMP) != OMPD_unknown && 15035 !CurContext->isDependentContext()) { 15036 ValExpr = MakeFullExpr(ValExpr).get(); 15037 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 15038 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 15039 HelperValStmt = buildPreInits(Context, Captures); 15040 } 15041 } 15042 } 15043 15044 return new (Context) 15045 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind, 15046 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc); 15047 } 15048 15049 OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind, 15050 SourceLocation StartLoc, 15051 SourceLocation EndLoc) { 15052 OMPClause *Res = nullptr; 15053 switch (Kind) { 15054 case OMPC_ordered: 15055 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc); 15056 break; 15057 case OMPC_nowait: 15058 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc); 15059 break; 15060 case OMPC_untied: 15061 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc); 15062 break; 15063 case OMPC_mergeable: 15064 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc); 15065 break; 15066 case OMPC_read: 15067 Res = ActOnOpenMPReadClause(StartLoc, EndLoc); 15068 break; 15069 case OMPC_write: 15070 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc); 15071 break; 15072 case OMPC_update: 15073 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc); 15074 break; 15075 case OMPC_capture: 15076 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc); 15077 break; 15078 case OMPC_seq_cst: 15079 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc); 15080 break; 15081 case OMPC_acq_rel: 15082 Res = ActOnOpenMPAcqRelClause(StartLoc, EndLoc); 15083 break; 15084 case OMPC_acquire: 15085 Res = ActOnOpenMPAcquireClause(StartLoc, EndLoc); 15086 break; 15087 case OMPC_release: 15088 Res = ActOnOpenMPReleaseClause(StartLoc, EndLoc); 15089 break; 15090 case OMPC_relaxed: 15091 Res = ActOnOpenMPRelaxedClause(StartLoc, EndLoc); 15092 break; 15093 case OMPC_threads: 15094 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc); 15095 break; 15096 case OMPC_simd: 15097 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc); 15098 break; 15099 case OMPC_nogroup: 15100 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc); 15101 break; 15102 case OMPC_unified_address: 15103 Res = ActOnOpenMPUnifiedAddressClause(StartLoc, EndLoc); 15104 break; 15105 case OMPC_unified_shared_memory: 15106 Res = ActOnOpenMPUnifiedSharedMemoryClause(StartLoc, EndLoc); 15107 break; 15108 case OMPC_reverse_offload: 15109 Res = ActOnOpenMPReverseOffloadClause(StartLoc, EndLoc); 15110 break; 15111 case OMPC_dynamic_allocators: 15112 Res = ActOnOpenMPDynamicAllocatorsClause(StartLoc, EndLoc); 15113 break; 15114 case OMPC_destroy: 15115 Res = ActOnOpenMPDestroyClause(/*InteropVar=*/nullptr, StartLoc, 15116 /*LParenLoc=*/SourceLocation(), 15117 /*VarLoc=*/SourceLocation(), EndLoc); 15118 break; 15119 case OMPC_full: 15120 Res = ActOnOpenMPFullClause(StartLoc, EndLoc); 15121 break; 15122 case OMPC_partial: 15123 Res = ActOnOpenMPPartialClause(nullptr, StartLoc, /*LParenLoc=*/{}, EndLoc); 15124 break; 15125 case OMPC_if: 15126 case OMPC_final: 15127 case OMPC_num_threads: 15128 case OMPC_safelen: 15129 case OMPC_simdlen: 15130 case OMPC_sizes: 15131 case OMPC_allocator: 15132 case OMPC_collapse: 15133 case OMPC_schedule: 15134 case OMPC_private: 15135 case OMPC_firstprivate: 15136 case OMPC_lastprivate: 15137 case OMPC_shared: 15138 case OMPC_reduction: 15139 case OMPC_task_reduction: 15140 case OMPC_in_reduction: 15141 case OMPC_linear: 15142 case OMPC_aligned: 15143 case OMPC_copyin: 15144 case OMPC_copyprivate: 15145 case OMPC_default: 15146 case OMPC_proc_bind: 15147 case OMPC_threadprivate: 15148 case OMPC_allocate: 15149 case OMPC_flush: 15150 case OMPC_depobj: 15151 case OMPC_depend: 15152 case OMPC_device: 15153 case OMPC_map: 15154 case OMPC_num_teams: 15155 case OMPC_thread_limit: 15156 case OMPC_priority: 15157 case OMPC_grainsize: 15158 case OMPC_num_tasks: 15159 case OMPC_hint: 15160 case OMPC_dist_schedule: 15161 case OMPC_defaultmap: 15162 case OMPC_unknown: 15163 case OMPC_uniform: 15164 case OMPC_to: 15165 case OMPC_from: 15166 case OMPC_use_device_ptr: 15167 case OMPC_use_device_addr: 15168 case OMPC_is_device_ptr: 15169 case OMPC_atomic_default_mem_order: 15170 case OMPC_device_type: 15171 case OMPC_match: 15172 case OMPC_nontemporal: 15173 case OMPC_order: 15174 case OMPC_novariants: 15175 case OMPC_nocontext: 15176 case OMPC_detach: 15177 case OMPC_inclusive: 15178 case OMPC_exclusive: 15179 case OMPC_uses_allocators: 15180 case OMPC_affinity: 15181 case OMPC_when: 15182 default: 15183 llvm_unreachable("Clause is not allowed."); 15184 } 15185 return Res; 15186 } 15187 15188 OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc, 15189 SourceLocation EndLoc) { 15190 DSAStack->setNowaitRegion(); 15191 return new (Context) OMPNowaitClause(StartLoc, EndLoc); 15192 } 15193 15194 OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc, 15195 SourceLocation EndLoc) { 15196 return new (Context) OMPUntiedClause(StartLoc, EndLoc); 15197 } 15198 15199 OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc, 15200 SourceLocation EndLoc) { 15201 return new (Context) OMPMergeableClause(StartLoc, EndLoc); 15202 } 15203 15204 OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc, 15205 SourceLocation EndLoc) { 15206 return new (Context) OMPReadClause(StartLoc, EndLoc); 15207 } 15208 15209 OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc, 15210 SourceLocation EndLoc) { 15211 return new (Context) OMPWriteClause(StartLoc, EndLoc); 15212 } 15213 15214 OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc, 15215 SourceLocation EndLoc) { 15216 return OMPUpdateClause::Create(Context, StartLoc, EndLoc); 15217 } 15218 15219 OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc, 15220 SourceLocation EndLoc) { 15221 return new (Context) OMPCaptureClause(StartLoc, EndLoc); 15222 } 15223 15224 OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc, 15225 SourceLocation EndLoc) { 15226 return new (Context) OMPSeqCstClause(StartLoc, EndLoc); 15227 } 15228 15229 OMPClause *Sema::ActOnOpenMPAcqRelClause(SourceLocation StartLoc, 15230 SourceLocation EndLoc) { 15231 return new (Context) OMPAcqRelClause(StartLoc, EndLoc); 15232 } 15233 15234 OMPClause *Sema::ActOnOpenMPAcquireClause(SourceLocation StartLoc, 15235 SourceLocation EndLoc) { 15236 return new (Context) OMPAcquireClause(StartLoc, EndLoc); 15237 } 15238 15239 OMPClause *Sema::ActOnOpenMPReleaseClause(SourceLocation StartLoc, 15240 SourceLocation EndLoc) { 15241 return new (Context) OMPReleaseClause(StartLoc, EndLoc); 15242 } 15243 15244 OMPClause *Sema::ActOnOpenMPRelaxedClause(SourceLocation StartLoc, 15245 SourceLocation EndLoc) { 15246 return new (Context) OMPRelaxedClause(StartLoc, EndLoc); 15247 } 15248 15249 OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc, 15250 SourceLocation EndLoc) { 15251 return new (Context) OMPThreadsClause(StartLoc, EndLoc); 15252 } 15253 15254 OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc, 15255 SourceLocation EndLoc) { 15256 return new (Context) OMPSIMDClause(StartLoc, EndLoc); 15257 } 15258 15259 OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc, 15260 SourceLocation EndLoc) { 15261 return new (Context) OMPNogroupClause(StartLoc, EndLoc); 15262 } 15263 15264 OMPClause *Sema::ActOnOpenMPUnifiedAddressClause(SourceLocation StartLoc, 15265 SourceLocation EndLoc) { 15266 return new (Context) OMPUnifiedAddressClause(StartLoc, EndLoc); 15267 } 15268 15269 OMPClause *Sema::ActOnOpenMPUnifiedSharedMemoryClause(SourceLocation StartLoc, 15270 SourceLocation EndLoc) { 15271 return new (Context) OMPUnifiedSharedMemoryClause(StartLoc, EndLoc); 15272 } 15273 15274 OMPClause *Sema::ActOnOpenMPReverseOffloadClause(SourceLocation StartLoc, 15275 SourceLocation EndLoc) { 15276 return new (Context) OMPReverseOffloadClause(StartLoc, EndLoc); 15277 } 15278 15279 OMPClause *Sema::ActOnOpenMPDynamicAllocatorsClause(SourceLocation StartLoc, 15280 SourceLocation EndLoc) { 15281 return new (Context) OMPDynamicAllocatorsClause(StartLoc, EndLoc); 15282 } 15283 15284 StmtResult Sema::ActOnOpenMPInteropDirective(ArrayRef<OMPClause *> Clauses, 15285 SourceLocation StartLoc, 15286 SourceLocation EndLoc) { 15287 15288 // OpenMP 5.1 [2.15.1, interop Construct, Restrictions] 15289 // At least one action-clause must appear on a directive. 15290 if (!hasClauses(Clauses, OMPC_init, OMPC_use, OMPC_destroy, OMPC_nowait)) { 15291 StringRef Expected = "'init', 'use', 'destroy', or 'nowait'"; 15292 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 15293 << Expected << getOpenMPDirectiveName(OMPD_interop); 15294 return StmtError(); 15295 } 15296 15297 // OpenMP 5.1 [2.15.1, interop Construct, Restrictions] 15298 // A depend clause can only appear on the directive if a targetsync 15299 // interop-type is present or the interop-var was initialized with 15300 // the targetsync interop-type. 15301 15302 // If there is any 'init' clause diagnose if there is no 'init' clause with 15303 // interop-type of 'targetsync'. Cases involving other directives cannot be 15304 // diagnosed. 15305 const OMPDependClause *DependClause = nullptr; 15306 bool HasInitClause = false; 15307 bool IsTargetSync = false; 15308 for (const OMPClause *C : Clauses) { 15309 if (IsTargetSync) 15310 break; 15311 if (const auto *InitClause = dyn_cast<OMPInitClause>(C)) { 15312 HasInitClause = true; 15313 if (InitClause->getIsTargetSync()) 15314 IsTargetSync = true; 15315 } else if (const auto *DC = dyn_cast<OMPDependClause>(C)) { 15316 DependClause = DC; 15317 } 15318 } 15319 if (DependClause && HasInitClause && !IsTargetSync) { 15320 Diag(DependClause->getBeginLoc(), diag::err_omp_interop_bad_depend_clause); 15321 return StmtError(); 15322 } 15323 15324 // OpenMP 5.1 [2.15.1, interop Construct, Restrictions] 15325 // Each interop-var may be specified for at most one action-clause of each 15326 // interop construct. 15327 llvm::SmallPtrSet<const VarDecl *, 4> InteropVars; 15328 for (const OMPClause *C : Clauses) { 15329 OpenMPClauseKind ClauseKind = C->getClauseKind(); 15330 const DeclRefExpr *DRE = nullptr; 15331 SourceLocation VarLoc; 15332 15333 if (ClauseKind == OMPC_init) { 15334 const auto *IC = cast<OMPInitClause>(C); 15335 VarLoc = IC->getVarLoc(); 15336 DRE = dyn_cast_or_null<DeclRefExpr>(IC->getInteropVar()); 15337 } else if (ClauseKind == OMPC_use) { 15338 const auto *UC = cast<OMPUseClause>(C); 15339 VarLoc = UC->getVarLoc(); 15340 DRE = dyn_cast_or_null<DeclRefExpr>(UC->getInteropVar()); 15341 } else if (ClauseKind == OMPC_destroy) { 15342 const auto *DC = cast<OMPDestroyClause>(C); 15343 VarLoc = DC->getVarLoc(); 15344 DRE = dyn_cast_or_null<DeclRefExpr>(DC->getInteropVar()); 15345 } 15346 15347 if (!DRE) 15348 continue; 15349 15350 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) { 15351 if (!InteropVars.insert(VD->getCanonicalDecl()).second) { 15352 Diag(VarLoc, diag::err_omp_interop_var_multiple_actions) << VD; 15353 return StmtError(); 15354 } 15355 } 15356 } 15357 15358 return OMPInteropDirective::Create(Context, StartLoc, EndLoc, Clauses); 15359 } 15360 15361 static bool isValidInteropVariable(Sema &SemaRef, Expr *InteropVarExpr, 15362 SourceLocation VarLoc, 15363 OpenMPClauseKind Kind) { 15364 if (InteropVarExpr->isValueDependent() || InteropVarExpr->isTypeDependent() || 15365 InteropVarExpr->isInstantiationDependent() || 15366 InteropVarExpr->containsUnexpandedParameterPack()) 15367 return true; 15368 15369 const auto *DRE = dyn_cast<DeclRefExpr>(InteropVarExpr); 15370 if (!DRE || !isa<VarDecl>(DRE->getDecl())) { 15371 SemaRef.Diag(VarLoc, diag::err_omp_interop_variable_expected) << 0; 15372 return false; 15373 } 15374 15375 // Interop variable should be of type omp_interop_t. 15376 bool HasError = false; 15377 QualType InteropType; 15378 LookupResult Result(SemaRef, &SemaRef.Context.Idents.get("omp_interop_t"), 15379 VarLoc, Sema::LookupOrdinaryName); 15380 if (SemaRef.LookupName(Result, SemaRef.getCurScope())) { 15381 NamedDecl *ND = Result.getFoundDecl(); 15382 if (const auto *TD = dyn_cast<TypeDecl>(ND)) { 15383 InteropType = QualType(TD->getTypeForDecl(), 0); 15384 } else { 15385 HasError = true; 15386 } 15387 } else { 15388 HasError = true; 15389 } 15390 15391 if (HasError) { 15392 SemaRef.Diag(VarLoc, diag::err_omp_implied_type_not_found) 15393 << "omp_interop_t"; 15394 return false; 15395 } 15396 15397 QualType VarType = InteropVarExpr->getType().getUnqualifiedType(); 15398 if (!SemaRef.Context.hasSameType(InteropType, VarType)) { 15399 SemaRef.Diag(VarLoc, diag::err_omp_interop_variable_wrong_type); 15400 return false; 15401 } 15402 15403 // OpenMP 5.1 [2.15.1, interop Construct, Restrictions] 15404 // The interop-var passed to init or destroy must be non-const. 15405 if ((Kind == OMPC_init || Kind == OMPC_destroy) && 15406 isConstNotMutableType(SemaRef, InteropVarExpr->getType())) { 15407 SemaRef.Diag(VarLoc, diag::err_omp_interop_variable_expected) 15408 << /*non-const*/ 1; 15409 return false; 15410 } 15411 return true; 15412 } 15413 15414 OMPClause * 15415 Sema::ActOnOpenMPInitClause(Expr *InteropVar, ArrayRef<Expr *> PrefExprs, 15416 bool IsTarget, bool IsTargetSync, 15417 SourceLocation StartLoc, SourceLocation LParenLoc, 15418 SourceLocation VarLoc, SourceLocation EndLoc) { 15419 15420 if (!isValidInteropVariable(*this, InteropVar, VarLoc, OMPC_init)) 15421 return nullptr; 15422 15423 // Check prefer_type values. These foreign-runtime-id values are either 15424 // string literals or constant integral expressions. 15425 for (const Expr *E : PrefExprs) { 15426 if (E->isValueDependent() || E->isTypeDependent() || 15427 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 15428 continue; 15429 if (E->isIntegerConstantExpr(Context)) 15430 continue; 15431 if (isa<StringLiteral>(E)) 15432 continue; 15433 Diag(E->getExprLoc(), diag::err_omp_interop_prefer_type); 15434 return nullptr; 15435 } 15436 15437 return OMPInitClause::Create(Context, InteropVar, PrefExprs, IsTarget, 15438 IsTargetSync, StartLoc, LParenLoc, VarLoc, 15439 EndLoc); 15440 } 15441 15442 OMPClause *Sema::ActOnOpenMPUseClause(Expr *InteropVar, SourceLocation StartLoc, 15443 SourceLocation LParenLoc, 15444 SourceLocation VarLoc, 15445 SourceLocation EndLoc) { 15446 15447 if (!isValidInteropVariable(*this, InteropVar, VarLoc, OMPC_use)) 15448 return nullptr; 15449 15450 return new (Context) 15451 OMPUseClause(InteropVar, StartLoc, LParenLoc, VarLoc, EndLoc); 15452 } 15453 15454 OMPClause *Sema::ActOnOpenMPDestroyClause(Expr *InteropVar, 15455 SourceLocation StartLoc, 15456 SourceLocation LParenLoc, 15457 SourceLocation VarLoc, 15458 SourceLocation EndLoc) { 15459 if (InteropVar && 15460 !isValidInteropVariable(*this, InteropVar, VarLoc, OMPC_destroy)) 15461 return nullptr; 15462 15463 return new (Context) 15464 OMPDestroyClause(InteropVar, StartLoc, LParenLoc, VarLoc, EndLoc); 15465 } 15466 15467 OMPClause *Sema::ActOnOpenMPNovariantsClause(Expr *Condition, 15468 SourceLocation StartLoc, 15469 SourceLocation LParenLoc, 15470 SourceLocation EndLoc) { 15471 Expr *ValExpr = Condition; 15472 Stmt *HelperValStmt = nullptr; 15473 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 15474 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 15475 !Condition->isInstantiationDependent() && 15476 !Condition->containsUnexpandedParameterPack()) { 15477 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 15478 if (Val.isInvalid()) 15479 return nullptr; 15480 15481 ValExpr = MakeFullExpr(Val.get()).get(); 15482 15483 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 15484 CaptureRegion = getOpenMPCaptureRegionForClause(DKind, OMPC_novariants, 15485 LangOpts.OpenMP); 15486 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 15487 ValExpr = MakeFullExpr(ValExpr).get(); 15488 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 15489 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 15490 HelperValStmt = buildPreInits(Context, Captures); 15491 } 15492 } 15493 15494 return new (Context) OMPNovariantsClause( 15495 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc); 15496 } 15497 15498 OMPClause *Sema::ActOnOpenMPNocontextClause(Expr *Condition, 15499 SourceLocation StartLoc, 15500 SourceLocation LParenLoc, 15501 SourceLocation EndLoc) { 15502 Expr *ValExpr = Condition; 15503 Stmt *HelperValStmt = nullptr; 15504 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 15505 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 15506 !Condition->isInstantiationDependent() && 15507 !Condition->containsUnexpandedParameterPack()) { 15508 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 15509 if (Val.isInvalid()) 15510 return nullptr; 15511 15512 ValExpr = MakeFullExpr(Val.get()).get(); 15513 15514 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 15515 CaptureRegion = 15516 getOpenMPCaptureRegionForClause(DKind, OMPC_nocontext, LangOpts.OpenMP); 15517 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 15518 ValExpr = MakeFullExpr(ValExpr).get(); 15519 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 15520 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 15521 HelperValStmt = buildPreInits(Context, Captures); 15522 } 15523 } 15524 15525 return new (Context) OMPNocontextClause(ValExpr, HelperValStmt, CaptureRegion, 15526 StartLoc, LParenLoc, EndLoc); 15527 } 15528 15529 OMPClause *Sema::ActOnOpenMPFilterClause(Expr *ThreadID, 15530 SourceLocation StartLoc, 15531 SourceLocation LParenLoc, 15532 SourceLocation EndLoc) { 15533 Expr *ValExpr = ThreadID; 15534 Stmt *HelperValStmt = nullptr; 15535 15536 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 15537 OpenMPDirectiveKind CaptureRegion = 15538 getOpenMPCaptureRegionForClause(DKind, OMPC_filter, LangOpts.OpenMP); 15539 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 15540 ValExpr = MakeFullExpr(ValExpr).get(); 15541 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 15542 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 15543 HelperValStmt = buildPreInits(Context, Captures); 15544 } 15545 15546 return new (Context) OMPFilterClause(ValExpr, HelperValStmt, CaptureRegion, 15547 StartLoc, LParenLoc, EndLoc); 15548 } 15549 15550 OMPClause *Sema::ActOnOpenMPVarListClause( 15551 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *DepModOrTailExpr, 15552 const OMPVarListLocTy &Locs, SourceLocation ColonLoc, 15553 CXXScopeSpec &ReductionOrMapperIdScopeSpec, 15554 DeclarationNameInfo &ReductionOrMapperId, int ExtraModifier, 15555 ArrayRef<OpenMPMapModifierKind> MapTypeModifiers, 15556 ArrayRef<SourceLocation> MapTypeModifiersLoc, bool IsMapTypeImplicit, 15557 SourceLocation ExtraModifierLoc, 15558 ArrayRef<OpenMPMotionModifierKind> MotionModifiers, 15559 ArrayRef<SourceLocation> MotionModifiersLoc) { 15560 SourceLocation StartLoc = Locs.StartLoc; 15561 SourceLocation LParenLoc = Locs.LParenLoc; 15562 SourceLocation EndLoc = Locs.EndLoc; 15563 OMPClause *Res = nullptr; 15564 switch (Kind) { 15565 case OMPC_private: 15566 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc); 15567 break; 15568 case OMPC_firstprivate: 15569 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 15570 break; 15571 case OMPC_lastprivate: 15572 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_LASTPRIVATE_unknown && 15573 "Unexpected lastprivate modifier."); 15574 Res = ActOnOpenMPLastprivateClause( 15575 VarList, static_cast<OpenMPLastprivateModifier>(ExtraModifier), 15576 ExtraModifierLoc, ColonLoc, StartLoc, LParenLoc, EndLoc); 15577 break; 15578 case OMPC_shared: 15579 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc); 15580 break; 15581 case OMPC_reduction: 15582 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_REDUCTION_unknown && 15583 "Unexpected lastprivate modifier."); 15584 Res = ActOnOpenMPReductionClause( 15585 VarList, static_cast<OpenMPReductionClauseModifier>(ExtraModifier), 15586 StartLoc, LParenLoc, ExtraModifierLoc, ColonLoc, EndLoc, 15587 ReductionOrMapperIdScopeSpec, ReductionOrMapperId); 15588 break; 15589 case OMPC_task_reduction: 15590 Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 15591 EndLoc, ReductionOrMapperIdScopeSpec, 15592 ReductionOrMapperId); 15593 break; 15594 case OMPC_in_reduction: 15595 Res = ActOnOpenMPInReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 15596 EndLoc, ReductionOrMapperIdScopeSpec, 15597 ReductionOrMapperId); 15598 break; 15599 case OMPC_linear: 15600 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_LINEAR_unknown && 15601 "Unexpected linear modifier."); 15602 Res = ActOnOpenMPLinearClause( 15603 VarList, DepModOrTailExpr, StartLoc, LParenLoc, 15604 static_cast<OpenMPLinearClauseKind>(ExtraModifier), ExtraModifierLoc, 15605 ColonLoc, EndLoc); 15606 break; 15607 case OMPC_aligned: 15608 Res = ActOnOpenMPAlignedClause(VarList, DepModOrTailExpr, StartLoc, 15609 LParenLoc, ColonLoc, EndLoc); 15610 break; 15611 case OMPC_copyin: 15612 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc); 15613 break; 15614 case OMPC_copyprivate: 15615 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 15616 break; 15617 case OMPC_flush: 15618 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc); 15619 break; 15620 case OMPC_depend: 15621 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_DEPEND_unknown && 15622 "Unexpected depend modifier."); 15623 Res = ActOnOpenMPDependClause( 15624 DepModOrTailExpr, static_cast<OpenMPDependClauseKind>(ExtraModifier), 15625 ExtraModifierLoc, ColonLoc, VarList, StartLoc, LParenLoc, EndLoc); 15626 break; 15627 case OMPC_map: 15628 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_MAP_unknown && 15629 "Unexpected map modifier."); 15630 Res = ActOnOpenMPMapClause( 15631 MapTypeModifiers, MapTypeModifiersLoc, ReductionOrMapperIdScopeSpec, 15632 ReductionOrMapperId, static_cast<OpenMPMapClauseKind>(ExtraModifier), 15633 IsMapTypeImplicit, ExtraModifierLoc, ColonLoc, VarList, Locs); 15634 break; 15635 case OMPC_to: 15636 Res = ActOnOpenMPToClause(MotionModifiers, MotionModifiersLoc, 15637 ReductionOrMapperIdScopeSpec, ReductionOrMapperId, 15638 ColonLoc, VarList, Locs); 15639 break; 15640 case OMPC_from: 15641 Res = ActOnOpenMPFromClause(MotionModifiers, MotionModifiersLoc, 15642 ReductionOrMapperIdScopeSpec, 15643 ReductionOrMapperId, ColonLoc, VarList, Locs); 15644 break; 15645 case OMPC_use_device_ptr: 15646 Res = ActOnOpenMPUseDevicePtrClause(VarList, Locs); 15647 break; 15648 case OMPC_use_device_addr: 15649 Res = ActOnOpenMPUseDeviceAddrClause(VarList, Locs); 15650 break; 15651 case OMPC_is_device_ptr: 15652 Res = ActOnOpenMPIsDevicePtrClause(VarList, Locs); 15653 break; 15654 case OMPC_allocate: 15655 Res = ActOnOpenMPAllocateClause(DepModOrTailExpr, VarList, StartLoc, 15656 LParenLoc, ColonLoc, EndLoc); 15657 break; 15658 case OMPC_nontemporal: 15659 Res = ActOnOpenMPNontemporalClause(VarList, StartLoc, LParenLoc, EndLoc); 15660 break; 15661 case OMPC_inclusive: 15662 Res = ActOnOpenMPInclusiveClause(VarList, StartLoc, LParenLoc, EndLoc); 15663 break; 15664 case OMPC_exclusive: 15665 Res = ActOnOpenMPExclusiveClause(VarList, StartLoc, LParenLoc, EndLoc); 15666 break; 15667 case OMPC_affinity: 15668 Res = ActOnOpenMPAffinityClause(StartLoc, LParenLoc, ColonLoc, EndLoc, 15669 DepModOrTailExpr, VarList); 15670 break; 15671 case OMPC_if: 15672 case OMPC_depobj: 15673 case OMPC_final: 15674 case OMPC_num_threads: 15675 case OMPC_safelen: 15676 case OMPC_simdlen: 15677 case OMPC_sizes: 15678 case OMPC_allocator: 15679 case OMPC_collapse: 15680 case OMPC_default: 15681 case OMPC_proc_bind: 15682 case OMPC_schedule: 15683 case OMPC_ordered: 15684 case OMPC_nowait: 15685 case OMPC_untied: 15686 case OMPC_mergeable: 15687 case OMPC_threadprivate: 15688 case OMPC_read: 15689 case OMPC_write: 15690 case OMPC_update: 15691 case OMPC_capture: 15692 case OMPC_seq_cst: 15693 case OMPC_acq_rel: 15694 case OMPC_acquire: 15695 case OMPC_release: 15696 case OMPC_relaxed: 15697 case OMPC_device: 15698 case OMPC_threads: 15699 case OMPC_simd: 15700 case OMPC_num_teams: 15701 case OMPC_thread_limit: 15702 case OMPC_priority: 15703 case OMPC_grainsize: 15704 case OMPC_nogroup: 15705 case OMPC_num_tasks: 15706 case OMPC_hint: 15707 case OMPC_dist_schedule: 15708 case OMPC_defaultmap: 15709 case OMPC_unknown: 15710 case OMPC_uniform: 15711 case OMPC_unified_address: 15712 case OMPC_unified_shared_memory: 15713 case OMPC_reverse_offload: 15714 case OMPC_dynamic_allocators: 15715 case OMPC_atomic_default_mem_order: 15716 case OMPC_device_type: 15717 case OMPC_match: 15718 case OMPC_order: 15719 case OMPC_destroy: 15720 case OMPC_novariants: 15721 case OMPC_nocontext: 15722 case OMPC_detach: 15723 case OMPC_uses_allocators: 15724 case OMPC_when: 15725 default: 15726 llvm_unreachable("Clause is not allowed."); 15727 } 15728 return Res; 15729 } 15730 15731 ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK, 15732 ExprObjectKind OK, SourceLocation Loc) { 15733 ExprResult Res = BuildDeclRefExpr( 15734 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc); 15735 if (!Res.isUsable()) 15736 return ExprError(); 15737 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) { 15738 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get()); 15739 if (!Res.isUsable()) 15740 return ExprError(); 15741 } 15742 if (VK != VK_LValue && Res.get()->isGLValue()) { 15743 Res = DefaultLvalueConversion(Res.get()); 15744 if (!Res.isUsable()) 15745 return ExprError(); 15746 } 15747 return Res; 15748 } 15749 15750 OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList, 15751 SourceLocation StartLoc, 15752 SourceLocation LParenLoc, 15753 SourceLocation EndLoc) { 15754 SmallVector<Expr *, 8> Vars; 15755 SmallVector<Expr *, 8> PrivateCopies; 15756 for (Expr *RefExpr : VarList) { 15757 assert(RefExpr && "NULL expr in OpenMP private clause."); 15758 SourceLocation ELoc; 15759 SourceRange ERange; 15760 Expr *SimpleRefExpr = RefExpr; 15761 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 15762 if (Res.second) { 15763 // It will be analyzed later. 15764 Vars.push_back(RefExpr); 15765 PrivateCopies.push_back(nullptr); 15766 } 15767 ValueDecl *D = Res.first; 15768 if (!D) 15769 continue; 15770 15771 QualType Type = D->getType(); 15772 auto *VD = dyn_cast<VarDecl>(D); 15773 15774 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 15775 // A variable that appears in a private clause must not have an incomplete 15776 // type or a reference type. 15777 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type)) 15778 continue; 15779 Type = Type.getNonReferenceType(); 15780 15781 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions] 15782 // A variable that is privatized must not have a const-qualified type 15783 // unless it is of class type with a mutable member. This restriction does 15784 // not apply to the firstprivate clause. 15785 // 15786 // OpenMP 3.1 [2.9.3.3, private clause, Restrictions] 15787 // A variable that appears in a private clause must not have a 15788 // const-qualified type unless it is of class type with a mutable member. 15789 if (rejectConstNotMutableType(*this, D, Type, OMPC_private, ELoc)) 15790 continue; 15791 15792 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 15793 // in a Construct] 15794 // Variables with the predetermined data-sharing attributes may not be 15795 // listed in data-sharing attributes clauses, except for the cases 15796 // listed below. For these exceptions only, listing a predetermined 15797 // variable in a data-sharing attribute clause is allowed and overrides 15798 // the variable's predetermined data-sharing attributes. 15799 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 15800 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) { 15801 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 15802 << getOpenMPClauseName(OMPC_private); 15803 reportOriginalDsa(*this, DSAStack, D, DVar); 15804 continue; 15805 } 15806 15807 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 15808 // Variably modified types are not supported for tasks. 15809 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && 15810 isOpenMPTaskingDirective(CurrDir)) { 15811 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 15812 << getOpenMPClauseName(OMPC_private) << Type 15813 << getOpenMPDirectiveName(CurrDir); 15814 bool IsDecl = 15815 !VD || 15816 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 15817 Diag(D->getLocation(), 15818 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 15819 << D; 15820 continue; 15821 } 15822 15823 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 15824 // A list item cannot appear in both a map clause and a data-sharing 15825 // attribute clause on the same construct 15826 // 15827 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7] 15828 // A list item cannot appear in both a map clause and a data-sharing 15829 // attribute clause on the same construct unless the construct is a 15830 // combined construct. 15831 if ((LangOpts.OpenMP <= 45 && isOpenMPTargetExecutionDirective(CurrDir)) || 15832 CurrDir == OMPD_target) { 15833 OpenMPClauseKind ConflictKind; 15834 if (DSAStack->checkMappableExprComponentListsForDecl( 15835 VD, /*CurrentRegionOnly=*/true, 15836 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef, 15837 OpenMPClauseKind WhereFoundClauseKind) -> bool { 15838 ConflictKind = WhereFoundClauseKind; 15839 return true; 15840 })) { 15841 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 15842 << getOpenMPClauseName(OMPC_private) 15843 << getOpenMPClauseName(ConflictKind) 15844 << getOpenMPDirectiveName(CurrDir); 15845 reportOriginalDsa(*this, DSAStack, D, DVar); 15846 continue; 15847 } 15848 } 15849 15850 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1] 15851 // A variable of class type (or array thereof) that appears in a private 15852 // clause requires an accessible, unambiguous default constructor for the 15853 // class type. 15854 // Generate helper private variable and initialize it with the default 15855 // value. The address of the original variable is replaced by the address of 15856 // the new private variable in CodeGen. This new variable is not added to 15857 // IdResolver, so the code in the OpenMP region uses original variable for 15858 // proper diagnostics. 15859 Type = Type.getUnqualifiedType(); 15860 VarDecl *VDPrivate = 15861 buildVarDecl(*this, ELoc, Type, D->getName(), 15862 D->hasAttrs() ? &D->getAttrs() : nullptr, 15863 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 15864 ActOnUninitializedDecl(VDPrivate); 15865 if (VDPrivate->isInvalidDecl()) 15866 continue; 15867 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 15868 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc); 15869 15870 DeclRefExpr *Ref = nullptr; 15871 if (!VD && !CurContext->isDependentContext()) 15872 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 15873 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref); 15874 Vars.push_back((VD || CurContext->isDependentContext()) 15875 ? RefExpr->IgnoreParens() 15876 : Ref); 15877 PrivateCopies.push_back(VDPrivateRefExpr); 15878 } 15879 15880 if (Vars.empty()) 15881 return nullptr; 15882 15883 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 15884 PrivateCopies); 15885 } 15886 15887 OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList, 15888 SourceLocation StartLoc, 15889 SourceLocation LParenLoc, 15890 SourceLocation EndLoc) { 15891 SmallVector<Expr *, 8> Vars; 15892 SmallVector<Expr *, 8> PrivateCopies; 15893 SmallVector<Expr *, 8> Inits; 15894 SmallVector<Decl *, 4> ExprCaptures; 15895 bool IsImplicitClause = 15896 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid(); 15897 SourceLocation ImplicitClauseLoc = DSAStack->getConstructLoc(); 15898 15899 for (Expr *RefExpr : VarList) { 15900 assert(RefExpr && "NULL expr in OpenMP firstprivate clause."); 15901 SourceLocation ELoc; 15902 SourceRange ERange; 15903 Expr *SimpleRefExpr = RefExpr; 15904 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 15905 if (Res.second) { 15906 // It will be analyzed later. 15907 Vars.push_back(RefExpr); 15908 PrivateCopies.push_back(nullptr); 15909 Inits.push_back(nullptr); 15910 } 15911 ValueDecl *D = Res.first; 15912 if (!D) 15913 continue; 15914 15915 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc; 15916 QualType Type = D->getType(); 15917 auto *VD = dyn_cast<VarDecl>(D); 15918 15919 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 15920 // A variable that appears in a private clause must not have an incomplete 15921 // type or a reference type. 15922 if (RequireCompleteType(ELoc, Type, 15923 diag::err_omp_firstprivate_incomplete_type)) 15924 continue; 15925 Type = Type.getNonReferenceType(); 15926 15927 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1] 15928 // A variable of class type (or array thereof) that appears in a private 15929 // clause requires an accessible, unambiguous copy constructor for the 15930 // class type. 15931 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType(); 15932 15933 // If an implicit firstprivate variable found it was checked already. 15934 DSAStackTy::DSAVarData TopDVar; 15935 if (!IsImplicitClause) { 15936 DSAStackTy::DSAVarData DVar = 15937 DSAStack->getTopDSA(D, /*FromParent=*/false); 15938 TopDVar = DVar; 15939 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 15940 bool IsConstant = ElemType.isConstant(Context); 15941 // OpenMP [2.4.13, Data-sharing Attribute Clauses] 15942 // A list item that specifies a given variable may not appear in more 15943 // than one clause on the same directive, except that a variable may be 15944 // specified in both firstprivate and lastprivate clauses. 15945 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3] 15946 // A list item may appear in a firstprivate or lastprivate clause but not 15947 // both. 15948 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate && 15949 (isOpenMPDistributeDirective(CurrDir) || 15950 DVar.CKind != OMPC_lastprivate) && 15951 DVar.RefExpr) { 15952 Diag(ELoc, diag::err_omp_wrong_dsa) 15953 << getOpenMPClauseName(DVar.CKind) 15954 << getOpenMPClauseName(OMPC_firstprivate); 15955 reportOriginalDsa(*this, DSAStack, D, DVar); 15956 continue; 15957 } 15958 15959 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 15960 // in a Construct] 15961 // Variables with the predetermined data-sharing attributes may not be 15962 // listed in data-sharing attributes clauses, except for the cases 15963 // listed below. For these exceptions only, listing a predetermined 15964 // variable in a data-sharing attribute clause is allowed and overrides 15965 // the variable's predetermined data-sharing attributes. 15966 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 15967 // in a Construct, C/C++, p.2] 15968 // Variables with const-qualified type having no mutable member may be 15969 // listed in a firstprivate clause, even if they are static data members. 15970 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr && 15971 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) { 15972 Diag(ELoc, diag::err_omp_wrong_dsa) 15973 << getOpenMPClauseName(DVar.CKind) 15974 << getOpenMPClauseName(OMPC_firstprivate); 15975 reportOriginalDsa(*this, DSAStack, D, DVar); 15976 continue; 15977 } 15978 15979 // OpenMP [2.9.3.4, Restrictions, p.2] 15980 // A list item that is private within a parallel region must not appear 15981 // in a firstprivate clause on a worksharing construct if any of the 15982 // worksharing regions arising from the worksharing construct ever bind 15983 // to any of the parallel regions arising from the parallel construct. 15984 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3] 15985 // A list item that is private within a teams region must not appear in a 15986 // firstprivate clause on a distribute construct if any of the distribute 15987 // regions arising from the distribute construct ever bind to any of the 15988 // teams regions arising from the teams construct. 15989 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3] 15990 // A list item that appears in a reduction clause of a teams construct 15991 // must not appear in a firstprivate clause on a distribute construct if 15992 // any of the distribute regions arising from the distribute construct 15993 // ever bind to any of the teams regions arising from the teams construct. 15994 if ((isOpenMPWorksharingDirective(CurrDir) || 15995 isOpenMPDistributeDirective(CurrDir)) && 15996 !isOpenMPParallelDirective(CurrDir) && 15997 !isOpenMPTeamsDirective(CurrDir)) { 15998 DVar = DSAStack->getImplicitDSA(D, true); 15999 if (DVar.CKind != OMPC_shared && 16000 (isOpenMPParallelDirective(DVar.DKind) || 16001 isOpenMPTeamsDirective(DVar.DKind) || 16002 DVar.DKind == OMPD_unknown)) { 16003 Diag(ELoc, diag::err_omp_required_access) 16004 << getOpenMPClauseName(OMPC_firstprivate) 16005 << getOpenMPClauseName(OMPC_shared); 16006 reportOriginalDsa(*this, DSAStack, D, DVar); 16007 continue; 16008 } 16009 } 16010 // OpenMP [2.9.3.4, Restrictions, p.3] 16011 // A list item that appears in a reduction clause of a parallel construct 16012 // must not appear in a firstprivate clause on a worksharing or task 16013 // construct if any of the worksharing or task regions arising from the 16014 // worksharing or task construct ever bind to any of the parallel regions 16015 // arising from the parallel construct. 16016 // OpenMP [2.9.3.4, Restrictions, p.4] 16017 // A list item that appears in a reduction clause in worksharing 16018 // construct must not appear in a firstprivate clause in a task construct 16019 // encountered during execution of any of the worksharing regions arising 16020 // from the worksharing construct. 16021 if (isOpenMPTaskingDirective(CurrDir)) { 16022 DVar = DSAStack->hasInnermostDSA( 16023 D, 16024 [](OpenMPClauseKind C, bool AppliedToPointee) { 16025 return C == OMPC_reduction && !AppliedToPointee; 16026 }, 16027 [](OpenMPDirectiveKind K) { 16028 return isOpenMPParallelDirective(K) || 16029 isOpenMPWorksharingDirective(K) || 16030 isOpenMPTeamsDirective(K); 16031 }, 16032 /*FromParent=*/true); 16033 if (DVar.CKind == OMPC_reduction && 16034 (isOpenMPParallelDirective(DVar.DKind) || 16035 isOpenMPWorksharingDirective(DVar.DKind) || 16036 isOpenMPTeamsDirective(DVar.DKind))) { 16037 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate) 16038 << getOpenMPDirectiveName(DVar.DKind); 16039 reportOriginalDsa(*this, DSAStack, D, DVar); 16040 continue; 16041 } 16042 } 16043 16044 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 16045 // A list item cannot appear in both a map clause and a data-sharing 16046 // attribute clause on the same construct 16047 // 16048 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7] 16049 // A list item cannot appear in both a map clause and a data-sharing 16050 // attribute clause on the same construct unless the construct is a 16051 // combined construct. 16052 if ((LangOpts.OpenMP <= 45 && 16053 isOpenMPTargetExecutionDirective(CurrDir)) || 16054 CurrDir == OMPD_target) { 16055 OpenMPClauseKind ConflictKind; 16056 if (DSAStack->checkMappableExprComponentListsForDecl( 16057 VD, /*CurrentRegionOnly=*/true, 16058 [&ConflictKind]( 16059 OMPClauseMappableExprCommon::MappableExprComponentListRef, 16060 OpenMPClauseKind WhereFoundClauseKind) { 16061 ConflictKind = WhereFoundClauseKind; 16062 return true; 16063 })) { 16064 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 16065 << getOpenMPClauseName(OMPC_firstprivate) 16066 << getOpenMPClauseName(ConflictKind) 16067 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 16068 reportOriginalDsa(*this, DSAStack, D, DVar); 16069 continue; 16070 } 16071 } 16072 } 16073 16074 // Variably modified types are not supported for tasks. 16075 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && 16076 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) { 16077 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 16078 << getOpenMPClauseName(OMPC_firstprivate) << Type 16079 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 16080 bool IsDecl = 16081 !VD || 16082 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 16083 Diag(D->getLocation(), 16084 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 16085 << D; 16086 continue; 16087 } 16088 16089 Type = Type.getUnqualifiedType(); 16090 VarDecl *VDPrivate = 16091 buildVarDecl(*this, ELoc, Type, D->getName(), 16092 D->hasAttrs() ? &D->getAttrs() : nullptr, 16093 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 16094 // Generate helper private variable and initialize it with the value of the 16095 // original variable. The address of the original variable is replaced by 16096 // the address of the new private variable in the CodeGen. This new variable 16097 // is not added to IdResolver, so the code in the OpenMP region uses 16098 // original variable for proper diagnostics and variable capturing. 16099 Expr *VDInitRefExpr = nullptr; 16100 // For arrays generate initializer for single element and replace it by the 16101 // original array element in CodeGen. 16102 if (Type->isArrayType()) { 16103 VarDecl *VDInit = 16104 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName()); 16105 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc); 16106 Expr *Init = DefaultLvalueConversion(VDInitRefExpr).get(); 16107 ElemType = ElemType.getUnqualifiedType(); 16108 VarDecl *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, 16109 ".firstprivate.temp"); 16110 InitializedEntity Entity = 16111 InitializedEntity::InitializeVariable(VDInitTemp); 16112 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc); 16113 16114 InitializationSequence InitSeq(*this, Entity, Kind, Init); 16115 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init); 16116 if (Result.isInvalid()) 16117 VDPrivate->setInvalidDecl(); 16118 else 16119 VDPrivate->setInit(Result.getAs<Expr>()); 16120 // Remove temp variable declaration. 16121 Context.Deallocate(VDInitTemp); 16122 } else { 16123 VarDecl *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type, 16124 ".firstprivate.temp"); 16125 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(), 16126 RefExpr->getExprLoc()); 16127 AddInitializerToDecl(VDPrivate, 16128 DefaultLvalueConversion(VDInitRefExpr).get(), 16129 /*DirectInit=*/false); 16130 } 16131 if (VDPrivate->isInvalidDecl()) { 16132 if (IsImplicitClause) { 16133 Diag(RefExpr->getExprLoc(), 16134 diag::note_omp_task_predetermined_firstprivate_here); 16135 } 16136 continue; 16137 } 16138 CurContext->addDecl(VDPrivate); 16139 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 16140 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), 16141 RefExpr->getExprLoc()); 16142 DeclRefExpr *Ref = nullptr; 16143 if (!VD && !CurContext->isDependentContext()) { 16144 if (TopDVar.CKind == OMPC_lastprivate) { 16145 Ref = TopDVar.PrivateCopy; 16146 } else { 16147 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 16148 if (!isOpenMPCapturedDecl(D)) 16149 ExprCaptures.push_back(Ref->getDecl()); 16150 } 16151 } 16152 if (!IsImplicitClause) 16153 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 16154 Vars.push_back((VD || CurContext->isDependentContext()) 16155 ? RefExpr->IgnoreParens() 16156 : Ref); 16157 PrivateCopies.push_back(VDPrivateRefExpr); 16158 Inits.push_back(VDInitRefExpr); 16159 } 16160 16161 if (Vars.empty()) 16162 return nullptr; 16163 16164 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 16165 Vars, PrivateCopies, Inits, 16166 buildPreInits(Context, ExprCaptures)); 16167 } 16168 16169 OMPClause *Sema::ActOnOpenMPLastprivateClause( 16170 ArrayRef<Expr *> VarList, OpenMPLastprivateModifier LPKind, 16171 SourceLocation LPKindLoc, SourceLocation ColonLoc, SourceLocation StartLoc, 16172 SourceLocation LParenLoc, SourceLocation EndLoc) { 16173 if (LPKind == OMPC_LASTPRIVATE_unknown && LPKindLoc.isValid()) { 16174 assert(ColonLoc.isValid() && "Colon location must be valid."); 16175 Diag(LPKindLoc, diag::err_omp_unexpected_clause_value) 16176 << getListOfPossibleValues(OMPC_lastprivate, /*First=*/0, 16177 /*Last=*/OMPC_LASTPRIVATE_unknown) 16178 << getOpenMPClauseName(OMPC_lastprivate); 16179 return nullptr; 16180 } 16181 16182 SmallVector<Expr *, 8> Vars; 16183 SmallVector<Expr *, 8> SrcExprs; 16184 SmallVector<Expr *, 8> DstExprs; 16185 SmallVector<Expr *, 8> AssignmentOps; 16186 SmallVector<Decl *, 4> ExprCaptures; 16187 SmallVector<Expr *, 4> ExprPostUpdates; 16188 for (Expr *RefExpr : VarList) { 16189 assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); 16190 SourceLocation ELoc; 16191 SourceRange ERange; 16192 Expr *SimpleRefExpr = RefExpr; 16193 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 16194 if (Res.second) { 16195 // It will be analyzed later. 16196 Vars.push_back(RefExpr); 16197 SrcExprs.push_back(nullptr); 16198 DstExprs.push_back(nullptr); 16199 AssignmentOps.push_back(nullptr); 16200 } 16201 ValueDecl *D = Res.first; 16202 if (!D) 16203 continue; 16204 16205 QualType Type = D->getType(); 16206 auto *VD = dyn_cast<VarDecl>(D); 16207 16208 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2] 16209 // A variable that appears in a lastprivate clause must not have an 16210 // incomplete type or a reference type. 16211 if (RequireCompleteType(ELoc, Type, 16212 diag::err_omp_lastprivate_incomplete_type)) 16213 continue; 16214 Type = Type.getNonReferenceType(); 16215 16216 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions] 16217 // A variable that is privatized must not have a const-qualified type 16218 // unless it is of class type with a mutable member. This restriction does 16219 // not apply to the firstprivate clause. 16220 // 16221 // OpenMP 3.1 [2.9.3.5, lastprivate clause, Restrictions] 16222 // A variable that appears in a lastprivate clause must not have a 16223 // const-qualified type unless it is of class type with a mutable member. 16224 if (rejectConstNotMutableType(*this, D, Type, OMPC_lastprivate, ELoc)) 16225 continue; 16226 16227 // OpenMP 5.0 [2.19.4.5 lastprivate Clause, Restrictions] 16228 // A list item that appears in a lastprivate clause with the conditional 16229 // modifier must be a scalar variable. 16230 if (LPKind == OMPC_LASTPRIVATE_conditional && !Type->isScalarType()) { 16231 Diag(ELoc, diag::err_omp_lastprivate_conditional_non_scalar); 16232 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 16233 VarDecl::DeclarationOnly; 16234 Diag(D->getLocation(), 16235 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 16236 << D; 16237 continue; 16238 } 16239 16240 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 16241 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 16242 // in a Construct] 16243 // Variables with the predetermined data-sharing attributes may not be 16244 // listed in data-sharing attributes clauses, except for the cases 16245 // listed below. 16246 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3] 16247 // A list item may appear in a firstprivate or lastprivate clause but not 16248 // both. 16249 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 16250 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate && 16251 (isOpenMPDistributeDirective(CurrDir) || 16252 DVar.CKind != OMPC_firstprivate) && 16253 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) { 16254 Diag(ELoc, diag::err_omp_wrong_dsa) 16255 << getOpenMPClauseName(DVar.CKind) 16256 << getOpenMPClauseName(OMPC_lastprivate); 16257 reportOriginalDsa(*this, DSAStack, D, DVar); 16258 continue; 16259 } 16260 16261 // OpenMP [2.14.3.5, Restrictions, p.2] 16262 // A list item that is private within a parallel region, or that appears in 16263 // the reduction clause of a parallel construct, must not appear in a 16264 // lastprivate clause on a worksharing construct if any of the corresponding 16265 // worksharing regions ever binds to any of the corresponding parallel 16266 // regions. 16267 DSAStackTy::DSAVarData TopDVar = DVar; 16268 if (isOpenMPWorksharingDirective(CurrDir) && 16269 !isOpenMPParallelDirective(CurrDir) && 16270 !isOpenMPTeamsDirective(CurrDir)) { 16271 DVar = DSAStack->getImplicitDSA(D, true); 16272 if (DVar.CKind != OMPC_shared) { 16273 Diag(ELoc, diag::err_omp_required_access) 16274 << getOpenMPClauseName(OMPC_lastprivate) 16275 << getOpenMPClauseName(OMPC_shared); 16276 reportOriginalDsa(*this, DSAStack, D, DVar); 16277 continue; 16278 } 16279 } 16280 16281 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2] 16282 // A variable of class type (or array thereof) that appears in a 16283 // lastprivate clause requires an accessible, unambiguous default 16284 // constructor for the class type, unless the list item is also specified 16285 // in a firstprivate clause. 16286 // A variable of class type (or array thereof) that appears in a 16287 // lastprivate clause requires an accessible, unambiguous copy assignment 16288 // operator for the class type. 16289 Type = Context.getBaseElementType(Type).getNonReferenceType(); 16290 VarDecl *SrcVD = buildVarDecl(*this, ERange.getBegin(), 16291 Type.getUnqualifiedType(), ".lastprivate.src", 16292 D->hasAttrs() ? &D->getAttrs() : nullptr); 16293 DeclRefExpr *PseudoSrcExpr = 16294 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc); 16295 VarDecl *DstVD = 16296 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst", 16297 D->hasAttrs() ? &D->getAttrs() : nullptr); 16298 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc); 16299 // For arrays generate assignment operation for single element and replace 16300 // it by the original array element in CodeGen. 16301 ExprResult AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign, 16302 PseudoDstExpr, PseudoSrcExpr); 16303 if (AssignmentOp.isInvalid()) 16304 continue; 16305 AssignmentOp = 16306 ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false); 16307 if (AssignmentOp.isInvalid()) 16308 continue; 16309 16310 DeclRefExpr *Ref = nullptr; 16311 if (!VD && !CurContext->isDependentContext()) { 16312 if (TopDVar.CKind == OMPC_firstprivate) { 16313 Ref = TopDVar.PrivateCopy; 16314 } else { 16315 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 16316 if (!isOpenMPCapturedDecl(D)) 16317 ExprCaptures.push_back(Ref->getDecl()); 16318 } 16319 if ((TopDVar.CKind == OMPC_firstprivate && !TopDVar.PrivateCopy) || 16320 (!isOpenMPCapturedDecl(D) && 16321 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) { 16322 ExprResult RefRes = DefaultLvalueConversion(Ref); 16323 if (!RefRes.isUsable()) 16324 continue; 16325 ExprResult PostUpdateRes = 16326 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr, 16327 RefRes.get()); 16328 if (!PostUpdateRes.isUsable()) 16329 continue; 16330 ExprPostUpdates.push_back( 16331 IgnoredValueConversions(PostUpdateRes.get()).get()); 16332 } 16333 } 16334 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref); 16335 Vars.push_back((VD || CurContext->isDependentContext()) 16336 ? RefExpr->IgnoreParens() 16337 : Ref); 16338 SrcExprs.push_back(PseudoSrcExpr); 16339 DstExprs.push_back(PseudoDstExpr); 16340 AssignmentOps.push_back(AssignmentOp.get()); 16341 } 16342 16343 if (Vars.empty()) 16344 return nullptr; 16345 16346 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 16347 Vars, SrcExprs, DstExprs, AssignmentOps, 16348 LPKind, LPKindLoc, ColonLoc, 16349 buildPreInits(Context, ExprCaptures), 16350 buildPostUpdate(*this, ExprPostUpdates)); 16351 } 16352 16353 OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList, 16354 SourceLocation StartLoc, 16355 SourceLocation LParenLoc, 16356 SourceLocation EndLoc) { 16357 SmallVector<Expr *, 8> Vars; 16358 for (Expr *RefExpr : VarList) { 16359 assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); 16360 SourceLocation ELoc; 16361 SourceRange ERange; 16362 Expr *SimpleRefExpr = RefExpr; 16363 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 16364 if (Res.second) { 16365 // It will be analyzed later. 16366 Vars.push_back(RefExpr); 16367 } 16368 ValueDecl *D = Res.first; 16369 if (!D) 16370 continue; 16371 16372 auto *VD = dyn_cast<VarDecl>(D); 16373 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 16374 // in a Construct] 16375 // Variables with the predetermined data-sharing attributes may not be 16376 // listed in data-sharing attributes clauses, except for the cases 16377 // listed below. For these exceptions only, listing a predetermined 16378 // variable in a data-sharing attribute clause is allowed and overrides 16379 // the variable's predetermined data-sharing attributes. 16380 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 16381 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared && 16382 DVar.RefExpr) { 16383 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 16384 << getOpenMPClauseName(OMPC_shared); 16385 reportOriginalDsa(*this, DSAStack, D, DVar); 16386 continue; 16387 } 16388 16389 DeclRefExpr *Ref = nullptr; 16390 if (!VD && isOpenMPCapturedDecl(D) && !CurContext->isDependentContext()) 16391 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 16392 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref); 16393 Vars.push_back((VD || !Ref || CurContext->isDependentContext()) 16394 ? RefExpr->IgnoreParens() 16395 : Ref); 16396 } 16397 16398 if (Vars.empty()) 16399 return nullptr; 16400 16401 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 16402 } 16403 16404 namespace { 16405 class DSARefChecker : public StmtVisitor<DSARefChecker, bool> { 16406 DSAStackTy *Stack; 16407 16408 public: 16409 bool VisitDeclRefExpr(DeclRefExpr *E) { 16410 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 16411 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false); 16412 if (DVar.CKind == OMPC_shared && !DVar.RefExpr) 16413 return false; 16414 if (DVar.CKind != OMPC_unknown) 16415 return true; 16416 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA( 16417 VD, 16418 [](OpenMPClauseKind C, bool AppliedToPointee) { 16419 return isOpenMPPrivate(C) && !AppliedToPointee; 16420 }, 16421 [](OpenMPDirectiveKind) { return true; }, 16422 /*FromParent=*/true); 16423 return DVarPrivate.CKind != OMPC_unknown; 16424 } 16425 return false; 16426 } 16427 bool VisitStmt(Stmt *S) { 16428 for (Stmt *Child : S->children()) { 16429 if (Child && Visit(Child)) 16430 return true; 16431 } 16432 return false; 16433 } 16434 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {} 16435 }; 16436 } // namespace 16437 16438 namespace { 16439 // Transform MemberExpression for specified FieldDecl of current class to 16440 // DeclRefExpr to specified OMPCapturedExprDecl. 16441 class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> { 16442 typedef TreeTransform<TransformExprToCaptures> BaseTransform; 16443 ValueDecl *Field = nullptr; 16444 DeclRefExpr *CapturedExpr = nullptr; 16445 16446 public: 16447 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl) 16448 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {} 16449 16450 ExprResult TransformMemberExpr(MemberExpr *E) { 16451 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) && 16452 E->getMemberDecl() == Field) { 16453 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false); 16454 return CapturedExpr; 16455 } 16456 return BaseTransform::TransformMemberExpr(E); 16457 } 16458 DeclRefExpr *getCapturedExpr() { return CapturedExpr; } 16459 }; 16460 } // namespace 16461 16462 template <typename T, typename U> 16463 static T filterLookupForUDReductionAndMapper( 16464 SmallVectorImpl<U> &Lookups, const llvm::function_ref<T(ValueDecl *)> Gen) { 16465 for (U &Set : Lookups) { 16466 for (auto *D : Set) { 16467 if (T Res = Gen(cast<ValueDecl>(D))) 16468 return Res; 16469 } 16470 } 16471 return T(); 16472 } 16473 16474 static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D) { 16475 assert(!LookupResult::isVisible(SemaRef, D) && "not in slow case"); 16476 16477 for (auto RD : D->redecls()) { 16478 // Don't bother with extra checks if we already know this one isn't visible. 16479 if (RD == D) 16480 continue; 16481 16482 auto ND = cast<NamedDecl>(RD); 16483 if (LookupResult::isVisible(SemaRef, ND)) 16484 return ND; 16485 } 16486 16487 return nullptr; 16488 } 16489 16490 static void 16491 argumentDependentLookup(Sema &SemaRef, const DeclarationNameInfo &Id, 16492 SourceLocation Loc, QualType Ty, 16493 SmallVectorImpl<UnresolvedSet<8>> &Lookups) { 16494 // Find all of the associated namespaces and classes based on the 16495 // arguments we have. 16496 Sema::AssociatedNamespaceSet AssociatedNamespaces; 16497 Sema::AssociatedClassSet AssociatedClasses; 16498 OpaqueValueExpr OVE(Loc, Ty, VK_LValue); 16499 SemaRef.FindAssociatedClassesAndNamespaces(Loc, &OVE, AssociatedNamespaces, 16500 AssociatedClasses); 16501 16502 // C++ [basic.lookup.argdep]p3: 16503 // Let X be the lookup set produced by unqualified lookup (3.4.1) 16504 // and let Y be the lookup set produced by argument dependent 16505 // lookup (defined as follows). If X contains [...] then Y is 16506 // empty. Otherwise Y is the set of declarations found in the 16507 // namespaces associated with the argument types as described 16508 // below. The set of declarations found by the lookup of the name 16509 // is the union of X and Y. 16510 // 16511 // Here, we compute Y and add its members to the overloaded 16512 // candidate set. 16513 for (auto *NS : AssociatedNamespaces) { 16514 // When considering an associated namespace, the lookup is the 16515 // same as the lookup performed when the associated namespace is 16516 // used as a qualifier (3.4.3.2) except that: 16517 // 16518 // -- Any using-directives in the associated namespace are 16519 // ignored. 16520 // 16521 // -- Any namespace-scope friend functions declared in 16522 // associated classes are visible within their respective 16523 // namespaces even if they are not visible during an ordinary 16524 // lookup (11.4). 16525 DeclContext::lookup_result R = NS->lookup(Id.getName()); 16526 for (auto *D : R) { 16527 auto *Underlying = D; 16528 if (auto *USD = dyn_cast<UsingShadowDecl>(D)) 16529 Underlying = USD->getTargetDecl(); 16530 16531 if (!isa<OMPDeclareReductionDecl>(Underlying) && 16532 !isa<OMPDeclareMapperDecl>(Underlying)) 16533 continue; 16534 16535 if (!SemaRef.isVisible(D)) { 16536 D = findAcceptableDecl(SemaRef, D); 16537 if (!D) 16538 continue; 16539 if (auto *USD = dyn_cast<UsingShadowDecl>(D)) 16540 Underlying = USD->getTargetDecl(); 16541 } 16542 Lookups.emplace_back(); 16543 Lookups.back().addDecl(Underlying); 16544 } 16545 } 16546 } 16547 16548 static ExprResult 16549 buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range, 16550 Scope *S, CXXScopeSpec &ReductionIdScopeSpec, 16551 const DeclarationNameInfo &ReductionId, QualType Ty, 16552 CXXCastPath &BasePath, Expr *UnresolvedReduction) { 16553 if (ReductionIdScopeSpec.isInvalid()) 16554 return ExprError(); 16555 SmallVector<UnresolvedSet<8>, 4> Lookups; 16556 if (S) { 16557 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName); 16558 Lookup.suppressDiagnostics(); 16559 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) { 16560 NamedDecl *D = Lookup.getRepresentativeDecl(); 16561 do { 16562 S = S->getParent(); 16563 } while (S && !S->isDeclScope(D)); 16564 if (S) 16565 S = S->getParent(); 16566 Lookups.emplace_back(); 16567 Lookups.back().append(Lookup.begin(), Lookup.end()); 16568 Lookup.clear(); 16569 } 16570 } else if (auto *ULE = 16571 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) { 16572 Lookups.push_back(UnresolvedSet<8>()); 16573 Decl *PrevD = nullptr; 16574 for (NamedDecl *D : ULE->decls()) { 16575 if (D == PrevD) 16576 Lookups.push_back(UnresolvedSet<8>()); 16577 else if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(D)) 16578 Lookups.back().addDecl(DRD); 16579 PrevD = D; 16580 } 16581 } 16582 if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() || 16583 Ty->isInstantiationDependentType() || 16584 Ty->containsUnexpandedParameterPack() || 16585 filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) { 16586 return !D->isInvalidDecl() && 16587 (D->getType()->isDependentType() || 16588 D->getType()->isInstantiationDependentType() || 16589 D->getType()->containsUnexpandedParameterPack()); 16590 })) { 16591 UnresolvedSet<8> ResSet; 16592 for (const UnresolvedSet<8> &Set : Lookups) { 16593 if (Set.empty()) 16594 continue; 16595 ResSet.append(Set.begin(), Set.end()); 16596 // The last item marks the end of all declarations at the specified scope. 16597 ResSet.addDecl(Set[Set.size() - 1]); 16598 } 16599 return UnresolvedLookupExpr::Create( 16600 SemaRef.Context, /*NamingClass=*/nullptr, 16601 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId, 16602 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end()); 16603 } 16604 // Lookup inside the classes. 16605 // C++ [over.match.oper]p3: 16606 // For a unary operator @ with an operand of a type whose 16607 // cv-unqualified version is T1, and for a binary operator @ with 16608 // a left operand of a type whose cv-unqualified version is T1 and 16609 // a right operand of a type whose cv-unqualified version is T2, 16610 // three sets of candidate functions, designated member 16611 // candidates, non-member candidates and built-in candidates, are 16612 // constructed as follows: 16613 // -- If T1 is a complete class type or a class currently being 16614 // defined, the set of member candidates is the result of the 16615 // qualified lookup of T1::operator@ (13.3.1.1.1); otherwise, 16616 // the set of member candidates is empty. 16617 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName); 16618 Lookup.suppressDiagnostics(); 16619 if (const auto *TyRec = Ty->getAs<RecordType>()) { 16620 // Complete the type if it can be completed. 16621 // If the type is neither complete nor being defined, bail out now. 16622 if (SemaRef.isCompleteType(Loc, Ty) || TyRec->isBeingDefined() || 16623 TyRec->getDecl()->getDefinition()) { 16624 Lookup.clear(); 16625 SemaRef.LookupQualifiedName(Lookup, TyRec->getDecl()); 16626 if (Lookup.empty()) { 16627 Lookups.emplace_back(); 16628 Lookups.back().append(Lookup.begin(), Lookup.end()); 16629 } 16630 } 16631 } 16632 // Perform ADL. 16633 if (SemaRef.getLangOpts().CPlusPlus) 16634 argumentDependentLookup(SemaRef, ReductionId, Loc, Ty, Lookups); 16635 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 16636 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * { 16637 if (!D->isInvalidDecl() && 16638 SemaRef.Context.hasSameType(D->getType(), Ty)) 16639 return D; 16640 return nullptr; 16641 })) 16642 return SemaRef.BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(), 16643 VK_LValue, Loc); 16644 if (SemaRef.getLangOpts().CPlusPlus) { 16645 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 16646 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * { 16647 if (!D->isInvalidDecl() && 16648 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) && 16649 !Ty.isMoreQualifiedThan(D->getType())) 16650 return D; 16651 return nullptr; 16652 })) { 16653 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, 16654 /*DetectVirtual=*/false); 16655 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) { 16656 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType( 16657 VD->getType().getUnqualifiedType()))) { 16658 if (SemaRef.CheckBaseClassAccess( 16659 Loc, VD->getType(), Ty, Paths.front(), 16660 /*DiagID=*/0) != Sema::AR_inaccessible) { 16661 SemaRef.BuildBasePathArray(Paths, BasePath); 16662 return SemaRef.BuildDeclRefExpr( 16663 VD, VD->getType().getNonReferenceType(), VK_LValue, Loc); 16664 } 16665 } 16666 } 16667 } 16668 } 16669 if (ReductionIdScopeSpec.isSet()) { 16670 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) 16671 << Ty << Range; 16672 return ExprError(); 16673 } 16674 return ExprEmpty(); 16675 } 16676 16677 namespace { 16678 /// Data for the reduction-based clauses. 16679 struct ReductionData { 16680 /// List of original reduction items. 16681 SmallVector<Expr *, 8> Vars; 16682 /// List of private copies of the reduction items. 16683 SmallVector<Expr *, 8> Privates; 16684 /// LHS expressions for the reduction_op expressions. 16685 SmallVector<Expr *, 8> LHSs; 16686 /// RHS expressions for the reduction_op expressions. 16687 SmallVector<Expr *, 8> RHSs; 16688 /// Reduction operation expression. 16689 SmallVector<Expr *, 8> ReductionOps; 16690 /// inscan copy operation expressions. 16691 SmallVector<Expr *, 8> InscanCopyOps; 16692 /// inscan copy temp array expressions for prefix sums. 16693 SmallVector<Expr *, 8> InscanCopyArrayTemps; 16694 /// inscan copy temp array element expressions for prefix sums. 16695 SmallVector<Expr *, 8> InscanCopyArrayElems; 16696 /// Taskgroup descriptors for the corresponding reduction items in 16697 /// in_reduction clauses. 16698 SmallVector<Expr *, 8> TaskgroupDescriptors; 16699 /// List of captures for clause. 16700 SmallVector<Decl *, 4> ExprCaptures; 16701 /// List of postupdate expressions. 16702 SmallVector<Expr *, 4> ExprPostUpdates; 16703 /// Reduction modifier. 16704 unsigned RedModifier = 0; 16705 ReductionData() = delete; 16706 /// Reserves required memory for the reduction data. 16707 ReductionData(unsigned Size, unsigned Modifier = 0) : RedModifier(Modifier) { 16708 Vars.reserve(Size); 16709 Privates.reserve(Size); 16710 LHSs.reserve(Size); 16711 RHSs.reserve(Size); 16712 ReductionOps.reserve(Size); 16713 if (RedModifier == OMPC_REDUCTION_inscan) { 16714 InscanCopyOps.reserve(Size); 16715 InscanCopyArrayTemps.reserve(Size); 16716 InscanCopyArrayElems.reserve(Size); 16717 } 16718 TaskgroupDescriptors.reserve(Size); 16719 ExprCaptures.reserve(Size); 16720 ExprPostUpdates.reserve(Size); 16721 } 16722 /// Stores reduction item and reduction operation only (required for dependent 16723 /// reduction item). 16724 void push(Expr *Item, Expr *ReductionOp) { 16725 Vars.emplace_back(Item); 16726 Privates.emplace_back(nullptr); 16727 LHSs.emplace_back(nullptr); 16728 RHSs.emplace_back(nullptr); 16729 ReductionOps.emplace_back(ReductionOp); 16730 TaskgroupDescriptors.emplace_back(nullptr); 16731 if (RedModifier == OMPC_REDUCTION_inscan) { 16732 InscanCopyOps.push_back(nullptr); 16733 InscanCopyArrayTemps.push_back(nullptr); 16734 InscanCopyArrayElems.push_back(nullptr); 16735 } 16736 } 16737 /// Stores reduction data. 16738 void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp, 16739 Expr *TaskgroupDescriptor, Expr *CopyOp, Expr *CopyArrayTemp, 16740 Expr *CopyArrayElem) { 16741 Vars.emplace_back(Item); 16742 Privates.emplace_back(Private); 16743 LHSs.emplace_back(LHS); 16744 RHSs.emplace_back(RHS); 16745 ReductionOps.emplace_back(ReductionOp); 16746 TaskgroupDescriptors.emplace_back(TaskgroupDescriptor); 16747 if (RedModifier == OMPC_REDUCTION_inscan) { 16748 InscanCopyOps.push_back(CopyOp); 16749 InscanCopyArrayTemps.push_back(CopyArrayTemp); 16750 InscanCopyArrayElems.push_back(CopyArrayElem); 16751 } else { 16752 assert(CopyOp == nullptr && CopyArrayTemp == nullptr && 16753 CopyArrayElem == nullptr && 16754 "Copy operation must be used for inscan reductions only."); 16755 } 16756 } 16757 }; 16758 } // namespace 16759 16760 static bool checkOMPArraySectionConstantForReduction( 16761 ASTContext &Context, const OMPArraySectionExpr *OASE, bool &SingleElement, 16762 SmallVectorImpl<llvm::APSInt> &ArraySizes) { 16763 const Expr *Length = OASE->getLength(); 16764 if (Length == nullptr) { 16765 // For array sections of the form [1:] or [:], we would need to analyze 16766 // the lower bound... 16767 if (OASE->getColonLocFirst().isValid()) 16768 return false; 16769 16770 // This is an array subscript which has implicit length 1! 16771 SingleElement = true; 16772 ArraySizes.push_back(llvm::APSInt::get(1)); 16773 } else { 16774 Expr::EvalResult Result; 16775 if (!Length->EvaluateAsInt(Result, Context)) 16776 return false; 16777 16778 llvm::APSInt ConstantLengthValue = Result.Val.getInt(); 16779 SingleElement = (ConstantLengthValue.getSExtValue() == 1); 16780 ArraySizes.push_back(ConstantLengthValue); 16781 } 16782 16783 // Get the base of this array section and walk up from there. 16784 const Expr *Base = OASE->getBase()->IgnoreParenImpCasts(); 16785 16786 // We require length = 1 for all array sections except the right-most to 16787 // guarantee that the memory region is contiguous and has no holes in it. 16788 while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) { 16789 Length = TempOASE->getLength(); 16790 if (Length == nullptr) { 16791 // For array sections of the form [1:] or [:], we would need to analyze 16792 // the lower bound... 16793 if (OASE->getColonLocFirst().isValid()) 16794 return false; 16795 16796 // This is an array subscript which has implicit length 1! 16797 ArraySizes.push_back(llvm::APSInt::get(1)); 16798 } else { 16799 Expr::EvalResult Result; 16800 if (!Length->EvaluateAsInt(Result, Context)) 16801 return false; 16802 16803 llvm::APSInt ConstantLengthValue = Result.Val.getInt(); 16804 if (ConstantLengthValue.getSExtValue() != 1) 16805 return false; 16806 16807 ArraySizes.push_back(ConstantLengthValue); 16808 } 16809 Base = TempOASE->getBase()->IgnoreParenImpCasts(); 16810 } 16811 16812 // If we have a single element, we don't need to add the implicit lengths. 16813 if (!SingleElement) { 16814 while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) { 16815 // Has implicit length 1! 16816 ArraySizes.push_back(llvm::APSInt::get(1)); 16817 Base = TempASE->getBase()->IgnoreParenImpCasts(); 16818 } 16819 } 16820 16821 // This array section can be privatized as a single value or as a constant 16822 // sized array. 16823 return true; 16824 } 16825 16826 static BinaryOperatorKind 16827 getRelatedCompoundReductionOp(BinaryOperatorKind BOK) { 16828 if (BOK == BO_Add) 16829 return BO_AddAssign; 16830 if (BOK == BO_Mul) 16831 return BO_MulAssign; 16832 if (BOK == BO_And) 16833 return BO_AndAssign; 16834 if (BOK == BO_Or) 16835 return BO_OrAssign; 16836 if (BOK == BO_Xor) 16837 return BO_XorAssign; 16838 return BOK; 16839 } 16840 16841 static bool actOnOMPReductionKindClause( 16842 Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind, 16843 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 16844 SourceLocation ColonLoc, SourceLocation EndLoc, 16845 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 16846 ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) { 16847 DeclarationName DN = ReductionId.getName(); 16848 OverloadedOperatorKind OOK = DN.getCXXOverloadedOperator(); 16849 BinaryOperatorKind BOK = BO_Comma; 16850 16851 ASTContext &Context = S.Context; 16852 // OpenMP [2.14.3.6, reduction clause] 16853 // C 16854 // reduction-identifier is either an identifier or one of the following 16855 // operators: +, -, *, &, |, ^, && and || 16856 // C++ 16857 // reduction-identifier is either an id-expression or one of the following 16858 // operators: +, -, *, &, |, ^, && and || 16859 switch (OOK) { 16860 case OO_Plus: 16861 case OO_Minus: 16862 BOK = BO_Add; 16863 break; 16864 case OO_Star: 16865 BOK = BO_Mul; 16866 break; 16867 case OO_Amp: 16868 BOK = BO_And; 16869 break; 16870 case OO_Pipe: 16871 BOK = BO_Or; 16872 break; 16873 case OO_Caret: 16874 BOK = BO_Xor; 16875 break; 16876 case OO_AmpAmp: 16877 BOK = BO_LAnd; 16878 break; 16879 case OO_PipePipe: 16880 BOK = BO_LOr; 16881 break; 16882 case OO_New: 16883 case OO_Delete: 16884 case OO_Array_New: 16885 case OO_Array_Delete: 16886 case OO_Slash: 16887 case OO_Percent: 16888 case OO_Tilde: 16889 case OO_Exclaim: 16890 case OO_Equal: 16891 case OO_Less: 16892 case OO_Greater: 16893 case OO_LessEqual: 16894 case OO_GreaterEqual: 16895 case OO_PlusEqual: 16896 case OO_MinusEqual: 16897 case OO_StarEqual: 16898 case OO_SlashEqual: 16899 case OO_PercentEqual: 16900 case OO_CaretEqual: 16901 case OO_AmpEqual: 16902 case OO_PipeEqual: 16903 case OO_LessLess: 16904 case OO_GreaterGreater: 16905 case OO_LessLessEqual: 16906 case OO_GreaterGreaterEqual: 16907 case OO_EqualEqual: 16908 case OO_ExclaimEqual: 16909 case OO_Spaceship: 16910 case OO_PlusPlus: 16911 case OO_MinusMinus: 16912 case OO_Comma: 16913 case OO_ArrowStar: 16914 case OO_Arrow: 16915 case OO_Call: 16916 case OO_Subscript: 16917 case OO_Conditional: 16918 case OO_Coawait: 16919 case NUM_OVERLOADED_OPERATORS: 16920 llvm_unreachable("Unexpected reduction identifier"); 16921 case OO_None: 16922 if (IdentifierInfo *II = DN.getAsIdentifierInfo()) { 16923 if (II->isStr("max")) 16924 BOK = BO_GT; 16925 else if (II->isStr("min")) 16926 BOK = BO_LT; 16927 } 16928 break; 16929 } 16930 SourceRange ReductionIdRange; 16931 if (ReductionIdScopeSpec.isValid()) 16932 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc()); 16933 else 16934 ReductionIdRange.setBegin(ReductionId.getBeginLoc()); 16935 ReductionIdRange.setEnd(ReductionId.getEndLoc()); 16936 16937 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end(); 16938 bool FirstIter = true; 16939 for (Expr *RefExpr : VarList) { 16940 assert(RefExpr && "nullptr expr in OpenMP reduction clause."); 16941 // OpenMP [2.1, C/C++] 16942 // A list item is a variable or array section, subject to the restrictions 16943 // specified in Section 2.4 on page 42 and in each of the sections 16944 // describing clauses and directives for which a list appears. 16945 // OpenMP [2.14.3.3, Restrictions, p.1] 16946 // A variable that is part of another variable (as an array or 16947 // structure element) cannot appear in a private clause. 16948 if (!FirstIter && IR != ER) 16949 ++IR; 16950 FirstIter = false; 16951 SourceLocation ELoc; 16952 SourceRange ERange; 16953 Expr *SimpleRefExpr = RefExpr; 16954 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange, 16955 /*AllowArraySection=*/true); 16956 if (Res.second) { 16957 // Try to find 'declare reduction' corresponding construct before using 16958 // builtin/overloaded operators. 16959 QualType Type = Context.DependentTy; 16960 CXXCastPath BasePath; 16961 ExprResult DeclareReductionRef = buildDeclareReductionRef( 16962 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec, 16963 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR); 16964 Expr *ReductionOp = nullptr; 16965 if (S.CurContext->isDependentContext() && 16966 (DeclareReductionRef.isUnset() || 16967 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) 16968 ReductionOp = DeclareReductionRef.get(); 16969 // It will be analyzed later. 16970 RD.push(RefExpr, ReductionOp); 16971 } 16972 ValueDecl *D = Res.first; 16973 if (!D) 16974 continue; 16975 16976 Expr *TaskgroupDescriptor = nullptr; 16977 QualType Type; 16978 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens()); 16979 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens()); 16980 if (ASE) { 16981 Type = ASE->getType().getNonReferenceType(); 16982 } else if (OASE) { 16983 QualType BaseType = 16984 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 16985 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 16986 Type = ATy->getElementType(); 16987 else 16988 Type = BaseType->getPointeeType(); 16989 Type = Type.getNonReferenceType(); 16990 } else { 16991 Type = Context.getBaseElementType(D->getType().getNonReferenceType()); 16992 } 16993 auto *VD = dyn_cast<VarDecl>(D); 16994 16995 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 16996 // A variable that appears in a private clause must not have an incomplete 16997 // type or a reference type. 16998 if (S.RequireCompleteType(ELoc, D->getType(), 16999 diag::err_omp_reduction_incomplete_type)) 17000 continue; 17001 // OpenMP [2.14.3.6, reduction clause, Restrictions] 17002 // A list item that appears in a reduction clause must not be 17003 // const-qualified. 17004 if (rejectConstNotMutableType(S, D, Type, ClauseKind, ELoc, 17005 /*AcceptIfMutable*/ false, ASE || OASE)) 17006 continue; 17007 17008 OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective(); 17009 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4] 17010 // If a list-item is a reference type then it must bind to the same object 17011 // for all threads of the team. 17012 if (!ASE && !OASE) { 17013 if (VD) { 17014 VarDecl *VDDef = VD->getDefinition(); 17015 if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) { 17016 DSARefChecker Check(Stack); 17017 if (Check.Visit(VDDef->getInit())) { 17018 S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg) 17019 << getOpenMPClauseName(ClauseKind) << ERange; 17020 S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef; 17021 continue; 17022 } 17023 } 17024 } 17025 17026 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 17027 // in a Construct] 17028 // Variables with the predetermined data-sharing attributes may not be 17029 // listed in data-sharing attributes clauses, except for the cases 17030 // listed below. For these exceptions only, listing a predetermined 17031 // variable in a data-sharing attribute clause is allowed and overrides 17032 // the variable's predetermined data-sharing attributes. 17033 // OpenMP [2.14.3.6, Restrictions, p.3] 17034 // Any number of reduction clauses can be specified on the directive, 17035 // but a list item can appear only once in the reduction clauses for that 17036 // directive. 17037 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false); 17038 if (DVar.CKind == OMPC_reduction) { 17039 S.Diag(ELoc, diag::err_omp_once_referenced) 17040 << getOpenMPClauseName(ClauseKind); 17041 if (DVar.RefExpr) 17042 S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced); 17043 continue; 17044 } 17045 if (DVar.CKind != OMPC_unknown) { 17046 S.Diag(ELoc, diag::err_omp_wrong_dsa) 17047 << getOpenMPClauseName(DVar.CKind) 17048 << getOpenMPClauseName(OMPC_reduction); 17049 reportOriginalDsa(S, Stack, D, DVar); 17050 continue; 17051 } 17052 17053 // OpenMP [2.14.3.6, Restrictions, p.1] 17054 // A list item that appears in a reduction clause of a worksharing 17055 // construct must be shared in the parallel regions to which any of the 17056 // worksharing regions arising from the worksharing construct bind. 17057 if (isOpenMPWorksharingDirective(CurrDir) && 17058 !isOpenMPParallelDirective(CurrDir) && 17059 !isOpenMPTeamsDirective(CurrDir)) { 17060 DVar = Stack->getImplicitDSA(D, true); 17061 if (DVar.CKind != OMPC_shared) { 17062 S.Diag(ELoc, diag::err_omp_required_access) 17063 << getOpenMPClauseName(OMPC_reduction) 17064 << getOpenMPClauseName(OMPC_shared); 17065 reportOriginalDsa(S, Stack, D, DVar); 17066 continue; 17067 } 17068 } 17069 } else { 17070 // Threadprivates cannot be shared between threads, so dignose if the base 17071 // is a threadprivate variable. 17072 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false); 17073 if (DVar.CKind == OMPC_threadprivate) { 17074 S.Diag(ELoc, diag::err_omp_wrong_dsa) 17075 << getOpenMPClauseName(DVar.CKind) 17076 << getOpenMPClauseName(OMPC_reduction); 17077 reportOriginalDsa(S, Stack, D, DVar); 17078 continue; 17079 } 17080 } 17081 17082 // Try to find 'declare reduction' corresponding construct before using 17083 // builtin/overloaded operators. 17084 CXXCastPath BasePath; 17085 ExprResult DeclareReductionRef = buildDeclareReductionRef( 17086 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec, 17087 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR); 17088 if (DeclareReductionRef.isInvalid()) 17089 continue; 17090 if (S.CurContext->isDependentContext() && 17091 (DeclareReductionRef.isUnset() || 17092 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) { 17093 RD.push(RefExpr, DeclareReductionRef.get()); 17094 continue; 17095 } 17096 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) { 17097 // Not allowed reduction identifier is found. 17098 S.Diag(ReductionId.getBeginLoc(), 17099 diag::err_omp_unknown_reduction_identifier) 17100 << Type << ReductionIdRange; 17101 continue; 17102 } 17103 17104 // OpenMP [2.14.3.6, reduction clause, Restrictions] 17105 // The type of a list item that appears in a reduction clause must be valid 17106 // for the reduction-identifier. For a max or min reduction in C, the type 17107 // of the list item must be an allowed arithmetic data type: char, int, 17108 // float, double, or _Bool, possibly modified with long, short, signed, or 17109 // unsigned. For a max or min reduction in C++, the type of the list item 17110 // must be an allowed arithmetic data type: char, wchar_t, int, float, 17111 // double, or bool, possibly modified with long, short, signed, or unsigned. 17112 if (DeclareReductionRef.isUnset()) { 17113 if ((BOK == BO_GT || BOK == BO_LT) && 17114 !(Type->isScalarType() || 17115 (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) { 17116 S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg) 17117 << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus; 17118 if (!ASE && !OASE) { 17119 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 17120 VarDecl::DeclarationOnly; 17121 S.Diag(D->getLocation(), 17122 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 17123 << D; 17124 } 17125 continue; 17126 } 17127 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) && 17128 !S.getLangOpts().CPlusPlus && Type->isFloatingType()) { 17129 S.Diag(ELoc, diag::err_omp_clause_floating_type_arg) 17130 << getOpenMPClauseName(ClauseKind); 17131 if (!ASE && !OASE) { 17132 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 17133 VarDecl::DeclarationOnly; 17134 S.Diag(D->getLocation(), 17135 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 17136 << D; 17137 } 17138 continue; 17139 } 17140 } 17141 17142 Type = Type.getNonLValueExprType(Context).getUnqualifiedType(); 17143 VarDecl *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs", 17144 D->hasAttrs() ? &D->getAttrs() : nullptr); 17145 VarDecl *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(), 17146 D->hasAttrs() ? &D->getAttrs() : nullptr); 17147 QualType PrivateTy = Type; 17148 17149 // Try if we can determine constant lengths for all array sections and avoid 17150 // the VLA. 17151 bool ConstantLengthOASE = false; 17152 if (OASE) { 17153 bool SingleElement; 17154 llvm::SmallVector<llvm::APSInt, 4> ArraySizes; 17155 ConstantLengthOASE = checkOMPArraySectionConstantForReduction( 17156 Context, OASE, SingleElement, ArraySizes); 17157 17158 // If we don't have a single element, we must emit a constant array type. 17159 if (ConstantLengthOASE && !SingleElement) { 17160 for (llvm::APSInt &Size : ArraySizes) 17161 PrivateTy = Context.getConstantArrayType(PrivateTy, Size, nullptr, 17162 ArrayType::Normal, 17163 /*IndexTypeQuals=*/0); 17164 } 17165 } 17166 17167 if ((OASE && !ConstantLengthOASE) || 17168 (!OASE && !ASE && 17169 D->getType().getNonReferenceType()->isVariablyModifiedType())) { 17170 if (!Context.getTargetInfo().isVLASupported()) { 17171 if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective())) { 17172 S.Diag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE; 17173 S.Diag(ELoc, diag::note_vla_unsupported); 17174 continue; 17175 } else { 17176 S.targetDiag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE; 17177 S.targetDiag(ELoc, diag::note_vla_unsupported); 17178 } 17179 } 17180 // For arrays/array sections only: 17181 // Create pseudo array type for private copy. The size for this array will 17182 // be generated during codegen. 17183 // For array subscripts or single variables Private Ty is the same as Type 17184 // (type of the variable or single array element). 17185 PrivateTy = Context.getVariableArrayType( 17186 Type, 17187 new (Context) 17188 OpaqueValueExpr(ELoc, Context.getSizeType(), VK_PRValue), 17189 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange()); 17190 } else if (!ASE && !OASE && 17191 Context.getAsArrayType(D->getType().getNonReferenceType())) { 17192 PrivateTy = D->getType().getNonReferenceType(); 17193 } 17194 // Private copy. 17195 VarDecl *PrivateVD = 17196 buildVarDecl(S, ELoc, PrivateTy, D->getName(), 17197 D->hasAttrs() ? &D->getAttrs() : nullptr, 17198 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 17199 // Add initializer for private variable. 17200 Expr *Init = nullptr; 17201 DeclRefExpr *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc); 17202 DeclRefExpr *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc); 17203 if (DeclareReductionRef.isUsable()) { 17204 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>(); 17205 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl()); 17206 if (DRD->getInitializer()) { 17207 Init = DRDRef; 17208 RHSVD->setInit(DRDRef); 17209 RHSVD->setInitStyle(VarDecl::CallInit); 17210 } 17211 } else { 17212 switch (BOK) { 17213 case BO_Add: 17214 case BO_Xor: 17215 case BO_Or: 17216 case BO_LOr: 17217 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'. 17218 if (Type->isScalarType() || Type->isAnyComplexType()) 17219 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get(); 17220 break; 17221 case BO_Mul: 17222 case BO_LAnd: 17223 if (Type->isScalarType() || Type->isAnyComplexType()) { 17224 // '*' and '&&' reduction ops - initializer is '1'. 17225 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get(); 17226 } 17227 break; 17228 case BO_And: { 17229 // '&' reduction op - initializer is '~0'. 17230 QualType OrigType = Type; 17231 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) 17232 Type = ComplexTy->getElementType(); 17233 if (Type->isRealFloatingType()) { 17234 llvm::APFloat InitValue = llvm::APFloat::getAllOnesValue( 17235 Context.getFloatTypeSemantics(Type)); 17236 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, 17237 Type, ELoc); 17238 } else if (Type->isScalarType()) { 17239 uint64_t Size = Context.getTypeSize(Type); 17240 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0); 17241 llvm::APInt InitValue = llvm::APInt::getAllOnes(Size); 17242 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); 17243 } 17244 if (Init && OrigType->isAnyComplexType()) { 17245 // Init = 0xFFFF + 0xFFFFi; 17246 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType); 17247 Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get(); 17248 } 17249 Type = OrigType; 17250 break; 17251 } 17252 case BO_LT: 17253 case BO_GT: { 17254 // 'min' reduction op - initializer is 'Largest representable number in 17255 // the reduction list item type'. 17256 // 'max' reduction op - initializer is 'Least representable number in 17257 // the reduction list item type'. 17258 if (Type->isIntegerType() || Type->isPointerType()) { 17259 bool IsSigned = Type->hasSignedIntegerRepresentation(); 17260 uint64_t Size = Context.getTypeSize(Type); 17261 QualType IntTy = 17262 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned); 17263 llvm::APInt InitValue = 17264 (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size) 17265 : llvm::APInt::getMinValue(Size) 17266 : IsSigned ? llvm::APInt::getSignedMaxValue(Size) 17267 : llvm::APInt::getMaxValue(Size); 17268 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); 17269 if (Type->isPointerType()) { 17270 // Cast to pointer type. 17271 ExprResult CastExpr = S.BuildCStyleCastExpr( 17272 ELoc, Context.getTrivialTypeSourceInfo(Type, ELoc), ELoc, Init); 17273 if (CastExpr.isInvalid()) 17274 continue; 17275 Init = CastExpr.get(); 17276 } 17277 } else if (Type->isRealFloatingType()) { 17278 llvm::APFloat InitValue = llvm::APFloat::getLargest( 17279 Context.getFloatTypeSemantics(Type), BOK != BO_LT); 17280 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, 17281 Type, ELoc); 17282 } 17283 break; 17284 } 17285 case BO_PtrMemD: 17286 case BO_PtrMemI: 17287 case BO_MulAssign: 17288 case BO_Div: 17289 case BO_Rem: 17290 case BO_Sub: 17291 case BO_Shl: 17292 case BO_Shr: 17293 case BO_LE: 17294 case BO_GE: 17295 case BO_EQ: 17296 case BO_NE: 17297 case BO_Cmp: 17298 case BO_AndAssign: 17299 case BO_XorAssign: 17300 case BO_OrAssign: 17301 case BO_Assign: 17302 case BO_AddAssign: 17303 case BO_SubAssign: 17304 case BO_DivAssign: 17305 case BO_RemAssign: 17306 case BO_ShlAssign: 17307 case BO_ShrAssign: 17308 case BO_Comma: 17309 llvm_unreachable("Unexpected reduction operation"); 17310 } 17311 } 17312 if (Init && DeclareReductionRef.isUnset()) { 17313 S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false); 17314 // Store initializer for single element in private copy. Will be used 17315 // during codegen. 17316 PrivateVD->setInit(RHSVD->getInit()); 17317 PrivateVD->setInitStyle(RHSVD->getInitStyle()); 17318 } else if (!Init) { 17319 S.ActOnUninitializedDecl(RHSVD); 17320 // Store initializer for single element in private copy. Will be used 17321 // during codegen. 17322 PrivateVD->setInit(RHSVD->getInit()); 17323 PrivateVD->setInitStyle(RHSVD->getInitStyle()); 17324 } 17325 if (RHSVD->isInvalidDecl()) 17326 continue; 17327 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) { 17328 S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible) 17329 << Type << ReductionIdRange; 17330 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 17331 VarDecl::DeclarationOnly; 17332 S.Diag(D->getLocation(), 17333 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 17334 << D; 17335 continue; 17336 } 17337 DeclRefExpr *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc); 17338 ExprResult ReductionOp; 17339 if (DeclareReductionRef.isUsable()) { 17340 QualType RedTy = DeclareReductionRef.get()->getType(); 17341 QualType PtrRedTy = Context.getPointerType(RedTy); 17342 ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE); 17343 ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE); 17344 if (!BasePath.empty()) { 17345 LHS = S.DefaultLvalueConversion(LHS.get()); 17346 RHS = S.DefaultLvalueConversion(RHS.get()); 17347 LHS = ImplicitCastExpr::Create( 17348 Context, PtrRedTy, CK_UncheckedDerivedToBase, LHS.get(), &BasePath, 17349 LHS.get()->getValueKind(), FPOptionsOverride()); 17350 RHS = ImplicitCastExpr::Create( 17351 Context, PtrRedTy, CK_UncheckedDerivedToBase, RHS.get(), &BasePath, 17352 RHS.get()->getValueKind(), FPOptionsOverride()); 17353 } 17354 FunctionProtoType::ExtProtoInfo EPI; 17355 QualType Params[] = {PtrRedTy, PtrRedTy}; 17356 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI); 17357 auto *OVE = new (Context) OpaqueValueExpr( 17358 ELoc, Context.getPointerType(FnTy), VK_PRValue, OK_Ordinary, 17359 S.DefaultLvalueConversion(DeclareReductionRef.get()).get()); 17360 Expr *Args[] = {LHS.get(), RHS.get()}; 17361 ReductionOp = 17362 CallExpr::Create(Context, OVE, Args, Context.VoidTy, VK_PRValue, ELoc, 17363 S.CurFPFeatureOverrides()); 17364 } else { 17365 BinaryOperatorKind CombBOK = getRelatedCompoundReductionOp(BOK); 17366 if (Type->isRecordType() && CombBOK != BOK) { 17367 Sema::TentativeAnalysisScope Trap(S); 17368 ReductionOp = 17369 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(), 17370 CombBOK, LHSDRE, RHSDRE); 17371 } 17372 if (!ReductionOp.isUsable()) { 17373 ReductionOp = 17374 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(), BOK, 17375 LHSDRE, RHSDRE); 17376 if (ReductionOp.isUsable()) { 17377 if (BOK != BO_LT && BOK != BO_GT) { 17378 ReductionOp = 17379 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(), 17380 BO_Assign, LHSDRE, ReductionOp.get()); 17381 } else { 17382 auto *ConditionalOp = new (Context) 17383 ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc, 17384 RHSDRE, Type, VK_LValue, OK_Ordinary); 17385 ReductionOp = 17386 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(), 17387 BO_Assign, LHSDRE, ConditionalOp); 17388 } 17389 } 17390 } 17391 if (ReductionOp.isUsable()) 17392 ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get(), 17393 /*DiscardedValue*/ false); 17394 if (!ReductionOp.isUsable()) 17395 continue; 17396 } 17397 17398 // Add copy operations for inscan reductions. 17399 // LHS = RHS; 17400 ExprResult CopyOpRes, TempArrayRes, TempArrayElem; 17401 if (ClauseKind == OMPC_reduction && 17402 RD.RedModifier == OMPC_REDUCTION_inscan) { 17403 ExprResult RHS = S.DefaultLvalueConversion(RHSDRE); 17404 CopyOpRes = S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, LHSDRE, 17405 RHS.get()); 17406 if (!CopyOpRes.isUsable()) 17407 continue; 17408 CopyOpRes = 17409 S.ActOnFinishFullExpr(CopyOpRes.get(), /*DiscardedValue=*/true); 17410 if (!CopyOpRes.isUsable()) 17411 continue; 17412 // For simd directive and simd-based directives in simd mode no need to 17413 // construct temp array, need just a single temp element. 17414 if (Stack->getCurrentDirective() == OMPD_simd || 17415 (S.getLangOpts().OpenMPSimd && 17416 isOpenMPSimdDirective(Stack->getCurrentDirective()))) { 17417 VarDecl *TempArrayVD = 17418 buildVarDecl(S, ELoc, PrivateTy, D->getName(), 17419 D->hasAttrs() ? &D->getAttrs() : nullptr); 17420 // Add a constructor to the temp decl. 17421 S.ActOnUninitializedDecl(TempArrayVD); 17422 TempArrayRes = buildDeclRefExpr(S, TempArrayVD, PrivateTy, ELoc); 17423 } else { 17424 // Build temp array for prefix sum. 17425 auto *Dim = new (S.Context) 17426 OpaqueValueExpr(ELoc, S.Context.getSizeType(), VK_PRValue); 17427 QualType ArrayTy = 17428 S.Context.getVariableArrayType(PrivateTy, Dim, ArrayType::Normal, 17429 /*IndexTypeQuals=*/0, {ELoc, ELoc}); 17430 VarDecl *TempArrayVD = 17431 buildVarDecl(S, ELoc, ArrayTy, D->getName(), 17432 D->hasAttrs() ? &D->getAttrs() : nullptr); 17433 // Add a constructor to the temp decl. 17434 S.ActOnUninitializedDecl(TempArrayVD); 17435 TempArrayRes = buildDeclRefExpr(S, TempArrayVD, ArrayTy, ELoc); 17436 TempArrayElem = 17437 S.DefaultFunctionArrayLvalueConversion(TempArrayRes.get()); 17438 auto *Idx = new (S.Context) 17439 OpaqueValueExpr(ELoc, S.Context.getSizeType(), VK_PRValue); 17440 TempArrayElem = S.CreateBuiltinArraySubscriptExpr(TempArrayElem.get(), 17441 ELoc, Idx, ELoc); 17442 } 17443 } 17444 17445 // OpenMP [2.15.4.6, Restrictions, p.2] 17446 // A list item that appears in an in_reduction clause of a task construct 17447 // must appear in a task_reduction clause of a construct associated with a 17448 // taskgroup region that includes the participating task in its taskgroup 17449 // set. The construct associated with the innermost region that meets this 17450 // condition must specify the same reduction-identifier as the in_reduction 17451 // clause. 17452 if (ClauseKind == OMPC_in_reduction) { 17453 SourceRange ParentSR; 17454 BinaryOperatorKind ParentBOK; 17455 const Expr *ParentReductionOp = nullptr; 17456 Expr *ParentBOKTD = nullptr, *ParentReductionOpTD = nullptr; 17457 DSAStackTy::DSAVarData ParentBOKDSA = 17458 Stack->getTopMostTaskgroupReductionData(D, ParentSR, ParentBOK, 17459 ParentBOKTD); 17460 DSAStackTy::DSAVarData ParentReductionOpDSA = 17461 Stack->getTopMostTaskgroupReductionData( 17462 D, ParentSR, ParentReductionOp, ParentReductionOpTD); 17463 bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown; 17464 bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown; 17465 if ((DeclareReductionRef.isUnset() && IsParentReductionOp) || 17466 (DeclareReductionRef.isUsable() && IsParentBOK) || 17467 (IsParentBOK && BOK != ParentBOK) || IsParentReductionOp) { 17468 bool EmitError = true; 17469 if (IsParentReductionOp && DeclareReductionRef.isUsable()) { 17470 llvm::FoldingSetNodeID RedId, ParentRedId; 17471 ParentReductionOp->Profile(ParentRedId, Context, /*Canonical=*/true); 17472 DeclareReductionRef.get()->Profile(RedId, Context, 17473 /*Canonical=*/true); 17474 EmitError = RedId != ParentRedId; 17475 } 17476 if (EmitError) { 17477 S.Diag(ReductionId.getBeginLoc(), 17478 diag::err_omp_reduction_identifier_mismatch) 17479 << ReductionIdRange << RefExpr->getSourceRange(); 17480 S.Diag(ParentSR.getBegin(), 17481 diag::note_omp_previous_reduction_identifier) 17482 << ParentSR 17483 << (IsParentBOK ? ParentBOKDSA.RefExpr 17484 : ParentReductionOpDSA.RefExpr) 17485 ->getSourceRange(); 17486 continue; 17487 } 17488 } 17489 TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD; 17490 } 17491 17492 DeclRefExpr *Ref = nullptr; 17493 Expr *VarsExpr = RefExpr->IgnoreParens(); 17494 if (!VD && !S.CurContext->isDependentContext()) { 17495 if (ASE || OASE) { 17496 TransformExprToCaptures RebuildToCapture(S, D); 17497 VarsExpr = 17498 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get(); 17499 Ref = RebuildToCapture.getCapturedExpr(); 17500 } else { 17501 VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false); 17502 } 17503 if (!S.isOpenMPCapturedDecl(D)) { 17504 RD.ExprCaptures.emplace_back(Ref->getDecl()); 17505 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) { 17506 ExprResult RefRes = S.DefaultLvalueConversion(Ref); 17507 if (!RefRes.isUsable()) 17508 continue; 17509 ExprResult PostUpdateRes = 17510 S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr, 17511 RefRes.get()); 17512 if (!PostUpdateRes.isUsable()) 17513 continue; 17514 if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) || 17515 Stack->getCurrentDirective() == OMPD_taskgroup) { 17516 S.Diag(RefExpr->getExprLoc(), 17517 diag::err_omp_reduction_non_addressable_expression) 17518 << RefExpr->getSourceRange(); 17519 continue; 17520 } 17521 RD.ExprPostUpdates.emplace_back( 17522 S.IgnoredValueConversions(PostUpdateRes.get()).get()); 17523 } 17524 } 17525 } 17526 // All reduction items are still marked as reduction (to do not increase 17527 // code base size). 17528 unsigned Modifier = RD.RedModifier; 17529 // Consider task_reductions as reductions with task modifier. Required for 17530 // correct analysis of in_reduction clauses. 17531 if (CurrDir == OMPD_taskgroup && ClauseKind == OMPC_task_reduction) 17532 Modifier = OMPC_REDUCTION_task; 17533 Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref, Modifier, 17534 ASE || OASE); 17535 if (Modifier == OMPC_REDUCTION_task && 17536 (CurrDir == OMPD_taskgroup || 17537 ((isOpenMPParallelDirective(CurrDir) || 17538 isOpenMPWorksharingDirective(CurrDir)) && 17539 !isOpenMPSimdDirective(CurrDir)))) { 17540 if (DeclareReductionRef.isUsable()) 17541 Stack->addTaskgroupReductionData(D, ReductionIdRange, 17542 DeclareReductionRef.get()); 17543 else 17544 Stack->addTaskgroupReductionData(D, ReductionIdRange, BOK); 17545 } 17546 RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get(), 17547 TaskgroupDescriptor, CopyOpRes.get(), TempArrayRes.get(), 17548 TempArrayElem.get()); 17549 } 17550 return RD.Vars.empty(); 17551 } 17552 17553 OMPClause *Sema::ActOnOpenMPReductionClause( 17554 ArrayRef<Expr *> VarList, OpenMPReductionClauseModifier Modifier, 17555 SourceLocation StartLoc, SourceLocation LParenLoc, 17556 SourceLocation ModifierLoc, SourceLocation ColonLoc, SourceLocation EndLoc, 17557 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 17558 ArrayRef<Expr *> UnresolvedReductions) { 17559 if (ModifierLoc.isValid() && Modifier == OMPC_REDUCTION_unknown) { 17560 Diag(LParenLoc, diag::err_omp_unexpected_clause_value) 17561 << getListOfPossibleValues(OMPC_reduction, /*First=*/0, 17562 /*Last=*/OMPC_REDUCTION_unknown) 17563 << getOpenMPClauseName(OMPC_reduction); 17564 return nullptr; 17565 } 17566 // OpenMP 5.0, 2.19.5.4 reduction Clause, Restrictions 17567 // A reduction clause with the inscan reduction-modifier may only appear on a 17568 // worksharing-loop construct, a worksharing-loop SIMD construct, a simd 17569 // construct, a parallel worksharing-loop construct or a parallel 17570 // worksharing-loop SIMD construct. 17571 if (Modifier == OMPC_REDUCTION_inscan && 17572 (DSAStack->getCurrentDirective() != OMPD_for && 17573 DSAStack->getCurrentDirective() != OMPD_for_simd && 17574 DSAStack->getCurrentDirective() != OMPD_simd && 17575 DSAStack->getCurrentDirective() != OMPD_parallel_for && 17576 DSAStack->getCurrentDirective() != OMPD_parallel_for_simd)) { 17577 Diag(ModifierLoc, diag::err_omp_wrong_inscan_reduction); 17578 return nullptr; 17579 } 17580 17581 ReductionData RD(VarList.size(), Modifier); 17582 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList, 17583 StartLoc, LParenLoc, ColonLoc, EndLoc, 17584 ReductionIdScopeSpec, ReductionId, 17585 UnresolvedReductions, RD)) 17586 return nullptr; 17587 17588 return OMPReductionClause::Create( 17589 Context, StartLoc, LParenLoc, ModifierLoc, ColonLoc, EndLoc, Modifier, 17590 RD.Vars, ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 17591 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.InscanCopyOps, 17592 RD.InscanCopyArrayTemps, RD.InscanCopyArrayElems, 17593 buildPreInits(Context, RD.ExprCaptures), 17594 buildPostUpdate(*this, RD.ExprPostUpdates)); 17595 } 17596 17597 OMPClause *Sema::ActOnOpenMPTaskReductionClause( 17598 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 17599 SourceLocation ColonLoc, SourceLocation EndLoc, 17600 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 17601 ArrayRef<Expr *> UnresolvedReductions) { 17602 ReductionData RD(VarList.size()); 17603 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction, VarList, 17604 StartLoc, LParenLoc, ColonLoc, EndLoc, 17605 ReductionIdScopeSpec, ReductionId, 17606 UnresolvedReductions, RD)) 17607 return nullptr; 17608 17609 return OMPTaskReductionClause::Create( 17610 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 17611 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 17612 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, 17613 buildPreInits(Context, RD.ExprCaptures), 17614 buildPostUpdate(*this, RD.ExprPostUpdates)); 17615 } 17616 17617 OMPClause *Sema::ActOnOpenMPInReductionClause( 17618 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 17619 SourceLocation ColonLoc, SourceLocation EndLoc, 17620 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 17621 ArrayRef<Expr *> UnresolvedReductions) { 17622 ReductionData RD(VarList.size()); 17623 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_in_reduction, VarList, 17624 StartLoc, LParenLoc, ColonLoc, EndLoc, 17625 ReductionIdScopeSpec, ReductionId, 17626 UnresolvedReductions, RD)) 17627 return nullptr; 17628 17629 return OMPInReductionClause::Create( 17630 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 17631 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 17632 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.TaskgroupDescriptors, 17633 buildPreInits(Context, RD.ExprCaptures), 17634 buildPostUpdate(*this, RD.ExprPostUpdates)); 17635 } 17636 17637 bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind, 17638 SourceLocation LinLoc) { 17639 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) || 17640 LinKind == OMPC_LINEAR_unknown) { 17641 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus; 17642 return true; 17643 } 17644 return false; 17645 } 17646 17647 bool Sema::CheckOpenMPLinearDecl(const ValueDecl *D, SourceLocation ELoc, 17648 OpenMPLinearClauseKind LinKind, QualType Type, 17649 bool IsDeclareSimd) { 17650 const auto *VD = dyn_cast_or_null<VarDecl>(D); 17651 // A variable must not have an incomplete type or a reference type. 17652 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type)) 17653 return true; 17654 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) && 17655 !Type->isReferenceType()) { 17656 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference) 17657 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind); 17658 return true; 17659 } 17660 Type = Type.getNonReferenceType(); 17661 17662 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions] 17663 // A variable that is privatized must not have a const-qualified type 17664 // unless it is of class type with a mutable member. This restriction does 17665 // not apply to the firstprivate clause, nor to the linear clause on 17666 // declarative directives (like declare simd). 17667 if (!IsDeclareSimd && 17668 rejectConstNotMutableType(*this, D, Type, OMPC_linear, ELoc)) 17669 return true; 17670 17671 // A list item must be of integral or pointer type. 17672 Type = Type.getUnqualifiedType().getCanonicalType(); 17673 const auto *Ty = Type.getTypePtrOrNull(); 17674 if (!Ty || (LinKind != OMPC_LINEAR_ref && !Ty->isDependentType() && 17675 !Ty->isIntegralType(Context) && !Ty->isPointerType())) { 17676 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type; 17677 if (D) { 17678 bool IsDecl = 17679 !VD || 17680 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 17681 Diag(D->getLocation(), 17682 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 17683 << D; 17684 } 17685 return true; 17686 } 17687 return false; 17688 } 17689 17690 OMPClause *Sema::ActOnOpenMPLinearClause( 17691 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc, 17692 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind, 17693 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 17694 SmallVector<Expr *, 8> Vars; 17695 SmallVector<Expr *, 8> Privates; 17696 SmallVector<Expr *, 8> Inits; 17697 SmallVector<Decl *, 4> ExprCaptures; 17698 SmallVector<Expr *, 4> ExprPostUpdates; 17699 if (CheckOpenMPLinearModifier(LinKind, LinLoc)) 17700 LinKind = OMPC_LINEAR_val; 17701 for (Expr *RefExpr : VarList) { 17702 assert(RefExpr && "NULL expr in OpenMP linear clause."); 17703 SourceLocation ELoc; 17704 SourceRange ERange; 17705 Expr *SimpleRefExpr = RefExpr; 17706 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 17707 if (Res.second) { 17708 // It will be analyzed later. 17709 Vars.push_back(RefExpr); 17710 Privates.push_back(nullptr); 17711 Inits.push_back(nullptr); 17712 } 17713 ValueDecl *D = Res.first; 17714 if (!D) 17715 continue; 17716 17717 QualType Type = D->getType(); 17718 auto *VD = dyn_cast<VarDecl>(D); 17719 17720 // OpenMP [2.14.3.7, linear clause] 17721 // A list-item cannot appear in more than one linear clause. 17722 // A list-item that appears in a linear clause cannot appear in any 17723 // other data-sharing attribute clause. 17724 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 17725 if (DVar.RefExpr) { 17726 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 17727 << getOpenMPClauseName(OMPC_linear); 17728 reportOriginalDsa(*this, DSAStack, D, DVar); 17729 continue; 17730 } 17731 17732 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type)) 17733 continue; 17734 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType(); 17735 17736 // Build private copy of original var. 17737 VarDecl *Private = 17738 buildVarDecl(*this, ELoc, Type, D->getName(), 17739 D->hasAttrs() ? &D->getAttrs() : nullptr, 17740 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 17741 DeclRefExpr *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc); 17742 // Build var to save initial value. 17743 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start"); 17744 Expr *InitExpr; 17745 DeclRefExpr *Ref = nullptr; 17746 if (!VD && !CurContext->isDependentContext()) { 17747 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 17748 if (!isOpenMPCapturedDecl(D)) { 17749 ExprCaptures.push_back(Ref->getDecl()); 17750 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) { 17751 ExprResult RefRes = DefaultLvalueConversion(Ref); 17752 if (!RefRes.isUsable()) 17753 continue; 17754 ExprResult PostUpdateRes = 17755 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, 17756 SimpleRefExpr, RefRes.get()); 17757 if (!PostUpdateRes.isUsable()) 17758 continue; 17759 ExprPostUpdates.push_back( 17760 IgnoredValueConversions(PostUpdateRes.get()).get()); 17761 } 17762 } 17763 } 17764 if (LinKind == OMPC_LINEAR_uval) 17765 InitExpr = VD ? VD->getInit() : SimpleRefExpr; 17766 else 17767 InitExpr = VD ? SimpleRefExpr : Ref; 17768 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(), 17769 /*DirectInit=*/false); 17770 DeclRefExpr *InitRef = buildDeclRefExpr(*this, Init, Type, ELoc); 17771 17772 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref); 17773 Vars.push_back((VD || CurContext->isDependentContext()) 17774 ? RefExpr->IgnoreParens() 17775 : Ref); 17776 Privates.push_back(PrivateRef); 17777 Inits.push_back(InitRef); 17778 } 17779 17780 if (Vars.empty()) 17781 return nullptr; 17782 17783 Expr *StepExpr = Step; 17784 Expr *CalcStepExpr = nullptr; 17785 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && 17786 !Step->isInstantiationDependent() && 17787 !Step->containsUnexpandedParameterPack()) { 17788 SourceLocation StepLoc = Step->getBeginLoc(); 17789 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step); 17790 if (Val.isInvalid()) 17791 return nullptr; 17792 StepExpr = Val.get(); 17793 17794 // Build var to save the step value. 17795 VarDecl *SaveVar = 17796 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step"); 17797 ExprResult SaveRef = 17798 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc); 17799 ExprResult CalcStep = 17800 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr); 17801 CalcStep = ActOnFinishFullExpr(CalcStep.get(), /*DiscardedValue*/ false); 17802 17803 // Warn about zero linear step (it would be probably better specified as 17804 // making corresponding variables 'const'). 17805 if (Optional<llvm::APSInt> Result = 17806 StepExpr->getIntegerConstantExpr(Context)) { 17807 if (!Result->isNegative() && !Result->isStrictlyPositive()) 17808 Diag(StepLoc, diag::warn_omp_linear_step_zero) 17809 << Vars[0] << (Vars.size() > 1); 17810 } else if (CalcStep.isUsable()) { 17811 // Calculate the step beforehand instead of doing this on each iteration. 17812 // (This is not used if the number of iterations may be kfold-ed). 17813 CalcStepExpr = CalcStep.get(); 17814 } 17815 } 17816 17817 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc, 17818 ColonLoc, EndLoc, Vars, Privates, Inits, 17819 StepExpr, CalcStepExpr, 17820 buildPreInits(Context, ExprCaptures), 17821 buildPostUpdate(*this, ExprPostUpdates)); 17822 } 17823 17824 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, 17825 Expr *NumIterations, Sema &SemaRef, 17826 Scope *S, DSAStackTy *Stack) { 17827 // Walk the vars and build update/final expressions for the CodeGen. 17828 SmallVector<Expr *, 8> Updates; 17829 SmallVector<Expr *, 8> Finals; 17830 SmallVector<Expr *, 8> UsedExprs; 17831 Expr *Step = Clause.getStep(); 17832 Expr *CalcStep = Clause.getCalcStep(); 17833 // OpenMP [2.14.3.7, linear clause] 17834 // If linear-step is not specified it is assumed to be 1. 17835 if (!Step) 17836 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(); 17837 else if (CalcStep) 17838 Step = cast<BinaryOperator>(CalcStep)->getLHS(); 17839 bool HasErrors = false; 17840 auto CurInit = Clause.inits().begin(); 17841 auto CurPrivate = Clause.privates().begin(); 17842 OpenMPLinearClauseKind LinKind = Clause.getModifier(); 17843 for (Expr *RefExpr : Clause.varlists()) { 17844 SourceLocation ELoc; 17845 SourceRange ERange; 17846 Expr *SimpleRefExpr = RefExpr; 17847 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange); 17848 ValueDecl *D = Res.first; 17849 if (Res.second || !D) { 17850 Updates.push_back(nullptr); 17851 Finals.push_back(nullptr); 17852 HasErrors = true; 17853 continue; 17854 } 17855 auto &&Info = Stack->isLoopControlVariable(D); 17856 // OpenMP [2.15.11, distribute simd Construct] 17857 // A list item may not appear in a linear clause, unless it is the loop 17858 // iteration variable. 17859 if (isOpenMPDistributeDirective(Stack->getCurrentDirective()) && 17860 isOpenMPSimdDirective(Stack->getCurrentDirective()) && !Info.first) { 17861 SemaRef.Diag(ELoc, 17862 diag::err_omp_linear_distribute_var_non_loop_iteration); 17863 Updates.push_back(nullptr); 17864 Finals.push_back(nullptr); 17865 HasErrors = true; 17866 continue; 17867 } 17868 Expr *InitExpr = *CurInit; 17869 17870 // Build privatized reference to the current linear var. 17871 auto *DE = cast<DeclRefExpr>(SimpleRefExpr); 17872 Expr *CapturedRef; 17873 if (LinKind == OMPC_LINEAR_uval) 17874 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit(); 17875 else 17876 CapturedRef = 17877 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()), 17878 DE->getType().getUnqualifiedType(), DE->getExprLoc(), 17879 /*RefersToCapture=*/true); 17880 17881 // Build update: Var = InitExpr + IV * Step 17882 ExprResult Update; 17883 if (!Info.first) 17884 Update = buildCounterUpdate( 17885 SemaRef, S, RefExpr->getExprLoc(), *CurPrivate, InitExpr, IV, Step, 17886 /*Subtract=*/false, /*IsNonRectangularLB=*/false); 17887 else 17888 Update = *CurPrivate; 17889 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getBeginLoc(), 17890 /*DiscardedValue*/ false); 17891 17892 // Build final: Var = InitExpr + NumIterations * Step 17893 ExprResult Final; 17894 if (!Info.first) 17895 Final = 17896 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef, 17897 InitExpr, NumIterations, Step, /*Subtract=*/false, 17898 /*IsNonRectangularLB=*/false); 17899 else 17900 Final = *CurPrivate; 17901 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getBeginLoc(), 17902 /*DiscardedValue*/ false); 17903 17904 if (!Update.isUsable() || !Final.isUsable()) { 17905 Updates.push_back(nullptr); 17906 Finals.push_back(nullptr); 17907 UsedExprs.push_back(nullptr); 17908 HasErrors = true; 17909 } else { 17910 Updates.push_back(Update.get()); 17911 Finals.push_back(Final.get()); 17912 if (!Info.first) 17913 UsedExprs.push_back(SimpleRefExpr); 17914 } 17915 ++CurInit; 17916 ++CurPrivate; 17917 } 17918 if (Expr *S = Clause.getStep()) 17919 UsedExprs.push_back(S); 17920 // Fill the remaining part with the nullptr. 17921 UsedExprs.append(Clause.varlist_size() + 1 - UsedExprs.size(), nullptr); 17922 Clause.setUpdates(Updates); 17923 Clause.setFinals(Finals); 17924 Clause.setUsedExprs(UsedExprs); 17925 return HasErrors; 17926 } 17927 17928 OMPClause *Sema::ActOnOpenMPAlignedClause( 17929 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc, 17930 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 17931 SmallVector<Expr *, 8> Vars; 17932 for (Expr *RefExpr : VarList) { 17933 assert(RefExpr && "NULL expr in OpenMP linear clause."); 17934 SourceLocation ELoc; 17935 SourceRange ERange; 17936 Expr *SimpleRefExpr = RefExpr; 17937 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 17938 if (Res.second) { 17939 // It will be analyzed later. 17940 Vars.push_back(RefExpr); 17941 } 17942 ValueDecl *D = Res.first; 17943 if (!D) 17944 continue; 17945 17946 QualType QType = D->getType(); 17947 auto *VD = dyn_cast<VarDecl>(D); 17948 17949 // OpenMP [2.8.1, simd construct, Restrictions] 17950 // The type of list items appearing in the aligned clause must be 17951 // array, pointer, reference to array, or reference to pointer. 17952 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType(); 17953 const Type *Ty = QType.getTypePtrOrNull(); 17954 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) { 17955 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr) 17956 << QType << getLangOpts().CPlusPlus << ERange; 17957 bool IsDecl = 17958 !VD || 17959 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 17960 Diag(D->getLocation(), 17961 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 17962 << D; 17963 continue; 17964 } 17965 17966 // OpenMP [2.8.1, simd construct, Restrictions] 17967 // A list-item cannot appear in more than one aligned clause. 17968 if (const Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) { 17969 Diag(ELoc, diag::err_omp_used_in_clause_twice) 17970 << 0 << getOpenMPClauseName(OMPC_aligned) << ERange; 17971 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa) 17972 << getOpenMPClauseName(OMPC_aligned); 17973 continue; 17974 } 17975 17976 DeclRefExpr *Ref = nullptr; 17977 if (!VD && isOpenMPCapturedDecl(D)) 17978 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 17979 Vars.push_back(DefaultFunctionArrayConversion( 17980 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref) 17981 .get()); 17982 } 17983 17984 // OpenMP [2.8.1, simd construct, Description] 17985 // The parameter of the aligned clause, alignment, must be a constant 17986 // positive integer expression. 17987 // If no optional parameter is specified, implementation-defined default 17988 // alignments for SIMD instructions on the target platforms are assumed. 17989 if (Alignment != nullptr) { 17990 ExprResult AlignResult = 17991 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned); 17992 if (AlignResult.isInvalid()) 17993 return nullptr; 17994 Alignment = AlignResult.get(); 17995 } 17996 if (Vars.empty()) 17997 return nullptr; 17998 17999 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc, 18000 EndLoc, Vars, Alignment); 18001 } 18002 18003 OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList, 18004 SourceLocation StartLoc, 18005 SourceLocation LParenLoc, 18006 SourceLocation EndLoc) { 18007 SmallVector<Expr *, 8> Vars; 18008 SmallVector<Expr *, 8> SrcExprs; 18009 SmallVector<Expr *, 8> DstExprs; 18010 SmallVector<Expr *, 8> AssignmentOps; 18011 for (Expr *RefExpr : VarList) { 18012 assert(RefExpr && "NULL expr in OpenMP copyin clause."); 18013 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 18014 // It will be analyzed later. 18015 Vars.push_back(RefExpr); 18016 SrcExprs.push_back(nullptr); 18017 DstExprs.push_back(nullptr); 18018 AssignmentOps.push_back(nullptr); 18019 continue; 18020 } 18021 18022 SourceLocation ELoc = RefExpr->getExprLoc(); 18023 // OpenMP [2.1, C/C++] 18024 // A list item is a variable name. 18025 // OpenMP [2.14.4.1, Restrictions, p.1] 18026 // A list item that appears in a copyin clause must be threadprivate. 18027 auto *DE = dyn_cast<DeclRefExpr>(RefExpr); 18028 if (!DE || !isa<VarDecl>(DE->getDecl())) { 18029 Diag(ELoc, diag::err_omp_expected_var_name_member_expr) 18030 << 0 << RefExpr->getSourceRange(); 18031 continue; 18032 } 18033 18034 Decl *D = DE->getDecl(); 18035 auto *VD = cast<VarDecl>(D); 18036 18037 QualType Type = VD->getType(); 18038 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 18039 // It will be analyzed later. 18040 Vars.push_back(DE); 18041 SrcExprs.push_back(nullptr); 18042 DstExprs.push_back(nullptr); 18043 AssignmentOps.push_back(nullptr); 18044 continue; 18045 } 18046 18047 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1] 18048 // A list item that appears in a copyin clause must be threadprivate. 18049 if (!DSAStack->isThreadPrivate(VD)) { 18050 Diag(ELoc, diag::err_omp_required_access) 18051 << getOpenMPClauseName(OMPC_copyin) 18052 << getOpenMPDirectiveName(OMPD_threadprivate); 18053 continue; 18054 } 18055 18056 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 18057 // A variable of class type (or array thereof) that appears in a 18058 // copyin clause requires an accessible, unambiguous copy assignment 18059 // operator for the class type. 18060 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType(); 18061 VarDecl *SrcVD = 18062 buildVarDecl(*this, DE->getBeginLoc(), ElemType.getUnqualifiedType(), 18063 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr); 18064 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr( 18065 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc()); 18066 VarDecl *DstVD = 18067 buildVarDecl(*this, DE->getBeginLoc(), ElemType, ".copyin.dst", 18068 VD->hasAttrs() ? &VD->getAttrs() : nullptr); 18069 DeclRefExpr *PseudoDstExpr = 18070 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc()); 18071 // For arrays generate assignment operation for single element and replace 18072 // it by the original array element in CodeGen. 18073 ExprResult AssignmentOp = 18074 BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, PseudoDstExpr, 18075 PseudoSrcExpr); 18076 if (AssignmentOp.isInvalid()) 18077 continue; 18078 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(), 18079 /*DiscardedValue*/ false); 18080 if (AssignmentOp.isInvalid()) 18081 continue; 18082 18083 DSAStack->addDSA(VD, DE, OMPC_copyin); 18084 Vars.push_back(DE); 18085 SrcExprs.push_back(PseudoSrcExpr); 18086 DstExprs.push_back(PseudoDstExpr); 18087 AssignmentOps.push_back(AssignmentOp.get()); 18088 } 18089 18090 if (Vars.empty()) 18091 return nullptr; 18092 18093 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 18094 SrcExprs, DstExprs, AssignmentOps); 18095 } 18096 18097 OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList, 18098 SourceLocation StartLoc, 18099 SourceLocation LParenLoc, 18100 SourceLocation EndLoc) { 18101 SmallVector<Expr *, 8> Vars; 18102 SmallVector<Expr *, 8> SrcExprs; 18103 SmallVector<Expr *, 8> DstExprs; 18104 SmallVector<Expr *, 8> AssignmentOps; 18105 for (Expr *RefExpr : VarList) { 18106 assert(RefExpr && "NULL expr in OpenMP linear clause."); 18107 SourceLocation ELoc; 18108 SourceRange ERange; 18109 Expr *SimpleRefExpr = RefExpr; 18110 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 18111 if (Res.second) { 18112 // It will be analyzed later. 18113 Vars.push_back(RefExpr); 18114 SrcExprs.push_back(nullptr); 18115 DstExprs.push_back(nullptr); 18116 AssignmentOps.push_back(nullptr); 18117 } 18118 ValueDecl *D = Res.first; 18119 if (!D) 18120 continue; 18121 18122 QualType Type = D->getType(); 18123 auto *VD = dyn_cast<VarDecl>(D); 18124 18125 // OpenMP [2.14.4.2, Restrictions, p.2] 18126 // A list item that appears in a copyprivate clause may not appear in a 18127 // private or firstprivate clause on the single construct. 18128 if (!VD || !DSAStack->isThreadPrivate(VD)) { 18129 DSAStackTy::DSAVarData DVar = 18130 DSAStack->getTopDSA(D, /*FromParent=*/false); 18131 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate && 18132 DVar.RefExpr) { 18133 Diag(ELoc, diag::err_omp_wrong_dsa) 18134 << getOpenMPClauseName(DVar.CKind) 18135 << getOpenMPClauseName(OMPC_copyprivate); 18136 reportOriginalDsa(*this, DSAStack, D, DVar); 18137 continue; 18138 } 18139 18140 // OpenMP [2.11.4.2, Restrictions, p.1] 18141 // All list items that appear in a copyprivate clause must be either 18142 // threadprivate or private in the enclosing context. 18143 if (DVar.CKind == OMPC_unknown) { 18144 DVar = DSAStack->getImplicitDSA(D, false); 18145 if (DVar.CKind == OMPC_shared) { 18146 Diag(ELoc, diag::err_omp_required_access) 18147 << getOpenMPClauseName(OMPC_copyprivate) 18148 << "threadprivate or private in the enclosing context"; 18149 reportOriginalDsa(*this, DSAStack, D, DVar); 18150 continue; 18151 } 18152 } 18153 } 18154 18155 // Variably modified types are not supported. 18156 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) { 18157 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 18158 << getOpenMPClauseName(OMPC_copyprivate) << Type 18159 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 18160 bool IsDecl = 18161 !VD || 18162 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 18163 Diag(D->getLocation(), 18164 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 18165 << D; 18166 continue; 18167 } 18168 18169 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 18170 // A variable of class type (or array thereof) that appears in a 18171 // copyin clause requires an accessible, unambiguous copy assignment 18172 // operator for the class type. 18173 Type = Context.getBaseElementType(Type.getNonReferenceType()) 18174 .getUnqualifiedType(); 18175 VarDecl *SrcVD = 18176 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.src", 18177 D->hasAttrs() ? &D->getAttrs() : nullptr); 18178 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc); 18179 VarDecl *DstVD = 18180 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.dst", 18181 D->hasAttrs() ? &D->getAttrs() : nullptr); 18182 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc); 18183 ExprResult AssignmentOp = BuildBinOp( 18184 DSAStack->getCurScope(), ELoc, BO_Assign, PseudoDstExpr, PseudoSrcExpr); 18185 if (AssignmentOp.isInvalid()) 18186 continue; 18187 AssignmentOp = 18188 ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false); 18189 if (AssignmentOp.isInvalid()) 18190 continue; 18191 18192 // No need to mark vars as copyprivate, they are already threadprivate or 18193 // implicitly private. 18194 assert(VD || isOpenMPCapturedDecl(D)); 18195 Vars.push_back( 18196 VD ? RefExpr->IgnoreParens() 18197 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false)); 18198 SrcExprs.push_back(PseudoSrcExpr); 18199 DstExprs.push_back(PseudoDstExpr); 18200 AssignmentOps.push_back(AssignmentOp.get()); 18201 } 18202 18203 if (Vars.empty()) 18204 return nullptr; 18205 18206 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 18207 Vars, SrcExprs, DstExprs, AssignmentOps); 18208 } 18209 18210 OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList, 18211 SourceLocation StartLoc, 18212 SourceLocation LParenLoc, 18213 SourceLocation EndLoc) { 18214 if (VarList.empty()) 18215 return nullptr; 18216 18217 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList); 18218 } 18219 18220 /// Tries to find omp_depend_t. type. 18221 static bool findOMPDependT(Sema &S, SourceLocation Loc, DSAStackTy *Stack, 18222 bool Diagnose = true) { 18223 QualType OMPDependT = Stack->getOMPDependT(); 18224 if (!OMPDependT.isNull()) 18225 return true; 18226 IdentifierInfo *II = &S.PP.getIdentifierTable().get("omp_depend_t"); 18227 ParsedType PT = S.getTypeName(*II, Loc, S.getCurScope()); 18228 if (!PT.getAsOpaquePtr() || PT.get().isNull()) { 18229 if (Diagnose) 18230 S.Diag(Loc, diag::err_omp_implied_type_not_found) << "omp_depend_t"; 18231 return false; 18232 } 18233 Stack->setOMPDependT(PT.get()); 18234 return true; 18235 } 18236 18237 OMPClause *Sema::ActOnOpenMPDepobjClause(Expr *Depobj, SourceLocation StartLoc, 18238 SourceLocation LParenLoc, 18239 SourceLocation EndLoc) { 18240 if (!Depobj) 18241 return nullptr; 18242 18243 bool OMPDependTFound = findOMPDependT(*this, StartLoc, DSAStack); 18244 18245 // OpenMP 5.0, 2.17.10.1 depobj Construct 18246 // depobj is an lvalue expression of type omp_depend_t. 18247 if (!Depobj->isTypeDependent() && !Depobj->isValueDependent() && 18248 !Depobj->isInstantiationDependent() && 18249 !Depobj->containsUnexpandedParameterPack() && 18250 (OMPDependTFound && 18251 !Context.typesAreCompatible(DSAStack->getOMPDependT(), Depobj->getType(), 18252 /*CompareUnqualified=*/true))) { 18253 Diag(Depobj->getExprLoc(), diag::err_omp_expected_omp_depend_t_lvalue) 18254 << 0 << Depobj->getType() << Depobj->getSourceRange(); 18255 } 18256 18257 if (!Depobj->isLValue()) { 18258 Diag(Depobj->getExprLoc(), diag::err_omp_expected_omp_depend_t_lvalue) 18259 << 1 << Depobj->getSourceRange(); 18260 } 18261 18262 return OMPDepobjClause::Create(Context, StartLoc, LParenLoc, EndLoc, Depobj); 18263 } 18264 18265 OMPClause * 18266 Sema::ActOnOpenMPDependClause(Expr *DepModifier, OpenMPDependClauseKind DepKind, 18267 SourceLocation DepLoc, SourceLocation ColonLoc, 18268 ArrayRef<Expr *> VarList, SourceLocation StartLoc, 18269 SourceLocation LParenLoc, SourceLocation EndLoc) { 18270 if (DSAStack->getCurrentDirective() == OMPD_ordered && 18271 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) { 18272 Diag(DepLoc, diag::err_omp_unexpected_clause_value) 18273 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend); 18274 return nullptr; 18275 } 18276 if ((DSAStack->getCurrentDirective() != OMPD_ordered || 18277 DSAStack->getCurrentDirective() == OMPD_depobj) && 18278 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source || 18279 DepKind == OMPC_DEPEND_sink || 18280 ((LangOpts.OpenMP < 50 || 18281 DSAStack->getCurrentDirective() == OMPD_depobj) && 18282 DepKind == OMPC_DEPEND_depobj))) { 18283 SmallVector<unsigned, 3> Except; 18284 Except.push_back(OMPC_DEPEND_source); 18285 Except.push_back(OMPC_DEPEND_sink); 18286 if (LangOpts.OpenMP < 50 || DSAStack->getCurrentDirective() == OMPD_depobj) 18287 Except.push_back(OMPC_DEPEND_depobj); 18288 std::string Expected = (LangOpts.OpenMP >= 50 && !DepModifier) 18289 ? "depend modifier(iterator) or " 18290 : ""; 18291 Diag(DepLoc, diag::err_omp_unexpected_clause_value) 18292 << Expected + getListOfPossibleValues(OMPC_depend, /*First=*/0, 18293 /*Last=*/OMPC_DEPEND_unknown, 18294 Except) 18295 << getOpenMPClauseName(OMPC_depend); 18296 return nullptr; 18297 } 18298 if (DepModifier && 18299 (DepKind == OMPC_DEPEND_source || DepKind == OMPC_DEPEND_sink)) { 18300 Diag(DepModifier->getExprLoc(), 18301 diag::err_omp_depend_sink_source_with_modifier); 18302 return nullptr; 18303 } 18304 if (DepModifier && 18305 !DepModifier->getType()->isSpecificBuiltinType(BuiltinType::OMPIterator)) 18306 Diag(DepModifier->getExprLoc(), diag::err_omp_depend_modifier_not_iterator); 18307 18308 SmallVector<Expr *, 8> Vars; 18309 DSAStackTy::OperatorOffsetTy OpsOffs; 18310 llvm::APSInt DepCounter(/*BitWidth=*/32); 18311 llvm::APSInt TotalDepCount(/*BitWidth=*/32); 18312 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) { 18313 if (const Expr *OrderedCountExpr = 18314 DSAStack->getParentOrderedRegionParam().first) { 18315 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context); 18316 TotalDepCount.setIsUnsigned(/*Val=*/true); 18317 } 18318 } 18319 for (Expr *RefExpr : VarList) { 18320 assert(RefExpr && "NULL expr in OpenMP shared clause."); 18321 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 18322 // It will be analyzed later. 18323 Vars.push_back(RefExpr); 18324 continue; 18325 } 18326 18327 SourceLocation ELoc = RefExpr->getExprLoc(); 18328 Expr *SimpleExpr = RefExpr->IgnoreParenCasts(); 18329 if (DepKind == OMPC_DEPEND_sink) { 18330 if (DSAStack->getParentOrderedRegionParam().first && 18331 DepCounter >= TotalDepCount) { 18332 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr); 18333 continue; 18334 } 18335 ++DepCounter; 18336 // OpenMP [2.13.9, Summary] 18337 // depend(dependence-type : vec), where dependence-type is: 18338 // 'sink' and where vec is the iteration vector, which has the form: 18339 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn] 18340 // where n is the value specified by the ordered clause in the loop 18341 // directive, xi denotes the loop iteration variable of the i-th nested 18342 // loop associated with the loop directive, and di is a constant 18343 // non-negative integer. 18344 if (CurContext->isDependentContext()) { 18345 // It will be analyzed later. 18346 Vars.push_back(RefExpr); 18347 continue; 18348 } 18349 SimpleExpr = SimpleExpr->IgnoreImplicit(); 18350 OverloadedOperatorKind OOK = OO_None; 18351 SourceLocation OOLoc; 18352 Expr *LHS = SimpleExpr; 18353 Expr *RHS = nullptr; 18354 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) { 18355 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode()); 18356 OOLoc = BO->getOperatorLoc(); 18357 LHS = BO->getLHS()->IgnoreParenImpCasts(); 18358 RHS = BO->getRHS()->IgnoreParenImpCasts(); 18359 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) { 18360 OOK = OCE->getOperator(); 18361 OOLoc = OCE->getOperatorLoc(); 18362 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); 18363 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts(); 18364 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) { 18365 OOK = MCE->getMethodDecl() 18366 ->getNameInfo() 18367 .getName() 18368 .getCXXOverloadedOperator(); 18369 OOLoc = MCE->getCallee()->getExprLoc(); 18370 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts(); 18371 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); 18372 } 18373 SourceLocation ELoc; 18374 SourceRange ERange; 18375 auto Res = getPrivateItem(*this, LHS, ELoc, ERange); 18376 if (Res.second) { 18377 // It will be analyzed later. 18378 Vars.push_back(RefExpr); 18379 } 18380 ValueDecl *D = Res.first; 18381 if (!D) 18382 continue; 18383 18384 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) { 18385 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus); 18386 continue; 18387 } 18388 if (RHS) { 18389 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause( 18390 RHS, OMPC_depend, /*StrictlyPositive=*/false); 18391 if (RHSRes.isInvalid()) 18392 continue; 18393 } 18394 if (!CurContext->isDependentContext() && 18395 DSAStack->getParentOrderedRegionParam().first && 18396 DepCounter != DSAStack->isParentLoopControlVariable(D).first) { 18397 const ValueDecl *VD = 18398 DSAStack->getParentLoopControlVariable(DepCounter.getZExtValue()); 18399 if (VD) 18400 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) 18401 << 1 << VD; 18402 else 18403 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) << 0; 18404 continue; 18405 } 18406 OpsOffs.emplace_back(RHS, OOK); 18407 } else { 18408 bool OMPDependTFound = LangOpts.OpenMP >= 50; 18409 if (OMPDependTFound) 18410 OMPDependTFound = findOMPDependT(*this, StartLoc, DSAStack, 18411 DepKind == OMPC_DEPEND_depobj); 18412 if (DepKind == OMPC_DEPEND_depobj) { 18413 // OpenMP 5.0, 2.17.11 depend Clause, Restrictions, C/C++ 18414 // List items used in depend clauses with the depobj dependence type 18415 // must be expressions of the omp_depend_t type. 18416 if (!RefExpr->isValueDependent() && !RefExpr->isTypeDependent() && 18417 !RefExpr->isInstantiationDependent() && 18418 !RefExpr->containsUnexpandedParameterPack() && 18419 (OMPDependTFound && 18420 !Context.hasSameUnqualifiedType(DSAStack->getOMPDependT(), 18421 RefExpr->getType()))) { 18422 Diag(ELoc, diag::err_omp_expected_omp_depend_t_lvalue) 18423 << 0 << RefExpr->getType() << RefExpr->getSourceRange(); 18424 continue; 18425 } 18426 if (!RefExpr->isLValue()) { 18427 Diag(ELoc, diag::err_omp_expected_omp_depend_t_lvalue) 18428 << 1 << RefExpr->getType() << RefExpr->getSourceRange(); 18429 continue; 18430 } 18431 } else { 18432 // OpenMP 5.0 [2.17.11, Restrictions] 18433 // List items used in depend clauses cannot be zero-length array 18434 // sections. 18435 QualType ExprTy = RefExpr->getType().getNonReferenceType(); 18436 const auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr); 18437 if (OASE) { 18438 QualType BaseType = 18439 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 18440 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 18441 ExprTy = ATy->getElementType(); 18442 else 18443 ExprTy = BaseType->getPointeeType(); 18444 ExprTy = ExprTy.getNonReferenceType(); 18445 const Expr *Length = OASE->getLength(); 18446 Expr::EvalResult Result; 18447 if (Length && !Length->isValueDependent() && 18448 Length->EvaluateAsInt(Result, Context) && 18449 Result.Val.getInt().isZero()) { 18450 Diag(ELoc, 18451 diag::err_omp_depend_zero_length_array_section_not_allowed) 18452 << SimpleExpr->getSourceRange(); 18453 continue; 18454 } 18455 } 18456 18457 // OpenMP 5.0, 2.17.11 depend Clause, Restrictions, C/C++ 18458 // List items used in depend clauses with the in, out, inout or 18459 // mutexinoutset dependence types cannot be expressions of the 18460 // omp_depend_t type. 18461 if (!RefExpr->isValueDependent() && !RefExpr->isTypeDependent() && 18462 !RefExpr->isInstantiationDependent() && 18463 !RefExpr->containsUnexpandedParameterPack() && 18464 (OMPDependTFound && 18465 DSAStack->getOMPDependT().getTypePtr() == ExprTy.getTypePtr())) { 18466 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 18467 << (LangOpts.OpenMP >= 50 ? 1 : 0) << 1 18468 << RefExpr->getSourceRange(); 18469 continue; 18470 } 18471 18472 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr); 18473 if (!RefExpr->IgnoreParenImpCasts()->isLValue() || 18474 (ASE && !ASE->getBase()->isTypeDependent() && 18475 !ASE->getBase() 18476 ->getType() 18477 .getNonReferenceType() 18478 ->isPointerType() && 18479 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) { 18480 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 18481 << (LangOpts.OpenMP >= 50 ? 1 : 0) 18482 << (LangOpts.OpenMP >= 50 ? 1 : 0) << RefExpr->getSourceRange(); 18483 continue; 18484 } 18485 18486 ExprResult Res; 18487 { 18488 Sema::TentativeAnalysisScope Trap(*this); 18489 Res = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, 18490 RefExpr->IgnoreParenImpCasts()); 18491 } 18492 if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr) && 18493 !isa<OMPArrayShapingExpr>(SimpleExpr)) { 18494 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 18495 << (LangOpts.OpenMP >= 50 ? 1 : 0) 18496 << (LangOpts.OpenMP >= 50 ? 1 : 0) << RefExpr->getSourceRange(); 18497 continue; 18498 } 18499 } 18500 } 18501 Vars.push_back(RefExpr->IgnoreParenImpCasts()); 18502 } 18503 18504 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink && 18505 TotalDepCount > VarList.size() && 18506 DSAStack->getParentOrderedRegionParam().first && 18507 DSAStack->getParentLoopControlVariable(VarList.size() + 1)) { 18508 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration) 18509 << 1 << DSAStack->getParentLoopControlVariable(VarList.size() + 1); 18510 } 18511 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink && 18512 Vars.empty()) 18513 return nullptr; 18514 18515 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, 18516 DepModifier, DepKind, DepLoc, ColonLoc, 18517 Vars, TotalDepCount.getZExtValue()); 18518 if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) && 18519 DSAStack->isParentOrderedRegion()) 18520 DSAStack->addDoacrossDependClause(C, OpsOffs); 18521 return C; 18522 } 18523 18524 OMPClause *Sema::ActOnOpenMPDeviceClause(OpenMPDeviceClauseModifier Modifier, 18525 Expr *Device, SourceLocation StartLoc, 18526 SourceLocation LParenLoc, 18527 SourceLocation ModifierLoc, 18528 SourceLocation EndLoc) { 18529 assert((ModifierLoc.isInvalid() || LangOpts.OpenMP >= 50) && 18530 "Unexpected device modifier in OpenMP < 50."); 18531 18532 bool ErrorFound = false; 18533 if (ModifierLoc.isValid() && Modifier == OMPC_DEVICE_unknown) { 18534 std::string Values = 18535 getListOfPossibleValues(OMPC_device, /*First=*/0, OMPC_DEVICE_unknown); 18536 Diag(ModifierLoc, diag::err_omp_unexpected_clause_value) 18537 << Values << getOpenMPClauseName(OMPC_device); 18538 ErrorFound = true; 18539 } 18540 18541 Expr *ValExpr = Device; 18542 Stmt *HelperValStmt = nullptr; 18543 18544 // OpenMP [2.9.1, Restrictions] 18545 // The device expression must evaluate to a non-negative integer value. 18546 ErrorFound = !isNonNegativeIntegerValue(ValExpr, *this, OMPC_device, 18547 /*StrictlyPositive=*/false) || 18548 ErrorFound; 18549 if (ErrorFound) 18550 return nullptr; 18551 18552 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 18553 OpenMPDirectiveKind CaptureRegion = 18554 getOpenMPCaptureRegionForClause(DKind, OMPC_device, LangOpts.OpenMP); 18555 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 18556 ValExpr = MakeFullExpr(ValExpr).get(); 18557 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 18558 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 18559 HelperValStmt = buildPreInits(Context, Captures); 18560 } 18561 18562 return new (Context) 18563 OMPDeviceClause(Modifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc, 18564 LParenLoc, ModifierLoc, EndLoc); 18565 } 18566 18567 static bool checkTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef, 18568 DSAStackTy *Stack, QualType QTy, 18569 bool FullCheck = true) { 18570 if (SemaRef.RequireCompleteType(SL, QTy, diag::err_incomplete_type)) 18571 return false; 18572 if (FullCheck && !SemaRef.CurContext->isDependentContext() && 18573 !QTy.isTriviallyCopyableType(SemaRef.Context)) 18574 SemaRef.Diag(SL, diag::warn_omp_non_trivial_type_mapped) << QTy << SR; 18575 return true; 18576 } 18577 18578 /// Return true if it can be proven that the provided array expression 18579 /// (array section or array subscript) does NOT specify the whole size of the 18580 /// array whose base type is \a BaseQTy. 18581 static bool checkArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef, 18582 const Expr *E, 18583 QualType BaseQTy) { 18584 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E); 18585 18586 // If this is an array subscript, it refers to the whole size if the size of 18587 // the dimension is constant and equals 1. Also, an array section assumes the 18588 // format of an array subscript if no colon is used. 18589 if (isa<ArraySubscriptExpr>(E) || 18590 (OASE && OASE->getColonLocFirst().isInvalid())) { 18591 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) 18592 return ATy->getSize().getSExtValue() != 1; 18593 // Size can't be evaluated statically. 18594 return false; 18595 } 18596 18597 assert(OASE && "Expecting array section if not an array subscript."); 18598 const Expr *LowerBound = OASE->getLowerBound(); 18599 const Expr *Length = OASE->getLength(); 18600 18601 // If there is a lower bound that does not evaluates to zero, we are not 18602 // covering the whole dimension. 18603 if (LowerBound) { 18604 Expr::EvalResult Result; 18605 if (!LowerBound->EvaluateAsInt(Result, SemaRef.getASTContext())) 18606 return false; // Can't get the integer value as a constant. 18607 18608 llvm::APSInt ConstLowerBound = Result.Val.getInt(); 18609 if (ConstLowerBound.getSExtValue()) 18610 return true; 18611 } 18612 18613 // If we don't have a length we covering the whole dimension. 18614 if (!Length) 18615 return false; 18616 18617 // If the base is a pointer, we don't have a way to get the size of the 18618 // pointee. 18619 if (BaseQTy->isPointerType()) 18620 return false; 18621 18622 // We can only check if the length is the same as the size of the dimension 18623 // if we have a constant array. 18624 const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()); 18625 if (!CATy) 18626 return false; 18627 18628 Expr::EvalResult Result; 18629 if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext())) 18630 return false; // Can't get the integer value as a constant. 18631 18632 llvm::APSInt ConstLength = Result.Val.getInt(); 18633 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue(); 18634 } 18635 18636 // Return true if it can be proven that the provided array expression (array 18637 // section or array subscript) does NOT specify a single element of the array 18638 // whose base type is \a BaseQTy. 18639 static bool checkArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef, 18640 const Expr *E, 18641 QualType BaseQTy) { 18642 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E); 18643 18644 // An array subscript always refer to a single element. Also, an array section 18645 // assumes the format of an array subscript if no colon is used. 18646 if (isa<ArraySubscriptExpr>(E) || 18647 (OASE && OASE->getColonLocFirst().isInvalid())) 18648 return false; 18649 18650 assert(OASE && "Expecting array section if not an array subscript."); 18651 const Expr *Length = OASE->getLength(); 18652 18653 // If we don't have a length we have to check if the array has unitary size 18654 // for this dimension. Also, we should always expect a length if the base type 18655 // is pointer. 18656 if (!Length) { 18657 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) 18658 return ATy->getSize().getSExtValue() != 1; 18659 // We cannot assume anything. 18660 return false; 18661 } 18662 18663 // Check if the length evaluates to 1. 18664 Expr::EvalResult Result; 18665 if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext())) 18666 return false; // Can't get the integer value as a constant. 18667 18668 llvm::APSInt ConstLength = Result.Val.getInt(); 18669 return ConstLength.getSExtValue() != 1; 18670 } 18671 18672 // The base of elements of list in a map clause have to be either: 18673 // - a reference to variable or field. 18674 // - a member expression. 18675 // - an array expression. 18676 // 18677 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the 18678 // reference to 'r'. 18679 // 18680 // If we have: 18681 // 18682 // struct SS { 18683 // Bla S; 18684 // foo() { 18685 // #pragma omp target map (S.Arr[:12]); 18686 // } 18687 // } 18688 // 18689 // We want to retrieve the member expression 'this->S'; 18690 18691 // OpenMP 5.0 [2.19.7.1, map Clause, Restrictions, p.2] 18692 // If a list item is an array section, it must specify contiguous storage. 18693 // 18694 // For this restriction it is sufficient that we make sure only references 18695 // to variables or fields and array expressions, and that no array sections 18696 // exist except in the rightmost expression (unless they cover the whole 18697 // dimension of the array). E.g. these would be invalid: 18698 // 18699 // r.ArrS[3:5].Arr[6:7] 18700 // 18701 // r.ArrS[3:5].x 18702 // 18703 // but these would be valid: 18704 // r.ArrS[3].Arr[6:7] 18705 // 18706 // r.ArrS[3].x 18707 namespace { 18708 class MapBaseChecker final : public StmtVisitor<MapBaseChecker, bool> { 18709 Sema &SemaRef; 18710 OpenMPClauseKind CKind = OMPC_unknown; 18711 OpenMPDirectiveKind DKind = OMPD_unknown; 18712 OMPClauseMappableExprCommon::MappableExprComponentList &Components; 18713 bool IsNonContiguous = false; 18714 bool NoDiagnose = false; 18715 const Expr *RelevantExpr = nullptr; 18716 bool AllowUnitySizeArraySection = true; 18717 bool AllowWholeSizeArraySection = true; 18718 bool AllowAnotherPtr = true; 18719 SourceLocation ELoc; 18720 SourceRange ERange; 18721 18722 void emitErrorMsg() { 18723 // If nothing else worked, this is not a valid map clause expression. 18724 if (SemaRef.getLangOpts().OpenMP < 50) { 18725 SemaRef.Diag(ELoc, 18726 diag::err_omp_expected_named_var_member_or_array_expression) 18727 << ERange; 18728 } else { 18729 SemaRef.Diag(ELoc, diag::err_omp_non_lvalue_in_map_or_motion_clauses) 18730 << getOpenMPClauseName(CKind) << ERange; 18731 } 18732 } 18733 18734 public: 18735 bool VisitDeclRefExpr(DeclRefExpr *DRE) { 18736 if (!isa<VarDecl>(DRE->getDecl())) { 18737 emitErrorMsg(); 18738 return false; 18739 } 18740 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 18741 RelevantExpr = DRE; 18742 // Record the component. 18743 Components.emplace_back(DRE, DRE->getDecl(), IsNonContiguous); 18744 return true; 18745 } 18746 18747 bool VisitMemberExpr(MemberExpr *ME) { 18748 Expr *E = ME; 18749 Expr *BaseE = ME->getBase()->IgnoreParenCasts(); 18750 18751 if (isa<CXXThisExpr>(BaseE)) { 18752 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 18753 // We found a base expression: this->Val. 18754 RelevantExpr = ME; 18755 } else { 18756 E = BaseE; 18757 } 18758 18759 if (!isa<FieldDecl>(ME->getMemberDecl())) { 18760 if (!NoDiagnose) { 18761 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field) 18762 << ME->getSourceRange(); 18763 return false; 18764 } 18765 if (RelevantExpr) 18766 return false; 18767 return Visit(E); 18768 } 18769 18770 auto *FD = cast<FieldDecl>(ME->getMemberDecl()); 18771 18772 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3] 18773 // A bit-field cannot appear in a map clause. 18774 // 18775 if (FD->isBitField()) { 18776 if (!NoDiagnose) { 18777 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause) 18778 << ME->getSourceRange() << getOpenMPClauseName(CKind); 18779 return false; 18780 } 18781 if (RelevantExpr) 18782 return false; 18783 return Visit(E); 18784 } 18785 18786 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 18787 // If the type of a list item is a reference to a type T then the type 18788 // will be considered to be T for all purposes of this clause. 18789 QualType CurType = BaseE->getType().getNonReferenceType(); 18790 18791 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2] 18792 // A list item cannot be a variable that is a member of a structure with 18793 // a union type. 18794 // 18795 if (CurType->isUnionType()) { 18796 if (!NoDiagnose) { 18797 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed) 18798 << ME->getSourceRange(); 18799 return false; 18800 } 18801 return RelevantExpr || Visit(E); 18802 } 18803 18804 // If we got a member expression, we should not expect any array section 18805 // before that: 18806 // 18807 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7] 18808 // If a list item is an element of a structure, only the rightmost symbol 18809 // of the variable reference can be an array section. 18810 // 18811 AllowUnitySizeArraySection = false; 18812 AllowWholeSizeArraySection = false; 18813 18814 // Record the component. 18815 Components.emplace_back(ME, FD, IsNonContiguous); 18816 return RelevantExpr || Visit(E); 18817 } 18818 18819 bool VisitArraySubscriptExpr(ArraySubscriptExpr *AE) { 18820 Expr *E = AE->getBase()->IgnoreParenImpCasts(); 18821 18822 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) { 18823 if (!NoDiagnose) { 18824 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name) 18825 << 0 << AE->getSourceRange(); 18826 return false; 18827 } 18828 return RelevantExpr || Visit(E); 18829 } 18830 18831 // If we got an array subscript that express the whole dimension we 18832 // can have any array expressions before. If it only expressing part of 18833 // the dimension, we can only have unitary-size array expressions. 18834 if (checkArrayExpressionDoesNotReferToWholeSize(SemaRef, AE, 18835 E->getType())) 18836 AllowWholeSizeArraySection = false; 18837 18838 if (const auto *TE = dyn_cast<CXXThisExpr>(E->IgnoreParenCasts())) { 18839 Expr::EvalResult Result; 18840 if (!AE->getIdx()->isValueDependent() && 18841 AE->getIdx()->EvaluateAsInt(Result, SemaRef.getASTContext()) && 18842 !Result.Val.getInt().isZero()) { 18843 SemaRef.Diag(AE->getIdx()->getExprLoc(), 18844 diag::err_omp_invalid_map_this_expr); 18845 SemaRef.Diag(AE->getIdx()->getExprLoc(), 18846 diag::note_omp_invalid_subscript_on_this_ptr_map); 18847 } 18848 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 18849 RelevantExpr = TE; 18850 } 18851 18852 // Record the component - we don't have any declaration associated. 18853 Components.emplace_back(AE, nullptr, IsNonContiguous); 18854 18855 return RelevantExpr || Visit(E); 18856 } 18857 18858 bool VisitOMPArraySectionExpr(OMPArraySectionExpr *OASE) { 18859 // After OMP 5.0 Array section in reduction clause will be implicitly 18860 // mapped 18861 assert(!(SemaRef.getLangOpts().OpenMP < 50 && NoDiagnose) && 18862 "Array sections cannot be implicitly mapped."); 18863 Expr *E = OASE->getBase()->IgnoreParenImpCasts(); 18864 QualType CurType = 18865 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType(); 18866 18867 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 18868 // If the type of a list item is a reference to a type T then the type 18869 // will be considered to be T for all purposes of this clause. 18870 if (CurType->isReferenceType()) 18871 CurType = CurType->getPointeeType(); 18872 18873 bool IsPointer = CurType->isAnyPointerType(); 18874 18875 if (!IsPointer && !CurType->isArrayType()) { 18876 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name) 18877 << 0 << OASE->getSourceRange(); 18878 return false; 18879 } 18880 18881 bool NotWhole = 18882 checkArrayExpressionDoesNotReferToWholeSize(SemaRef, OASE, CurType); 18883 bool NotUnity = 18884 checkArrayExpressionDoesNotReferToUnitySize(SemaRef, OASE, CurType); 18885 18886 if (AllowWholeSizeArraySection) { 18887 // Any array section is currently allowed. Allowing a whole size array 18888 // section implies allowing a unity array section as well. 18889 // 18890 // If this array section refers to the whole dimension we can still 18891 // accept other array sections before this one, except if the base is a 18892 // pointer. Otherwise, only unitary sections are accepted. 18893 if (NotWhole || IsPointer) 18894 AllowWholeSizeArraySection = false; 18895 } else if (DKind == OMPD_target_update && 18896 SemaRef.getLangOpts().OpenMP >= 50) { 18897 if (IsPointer && !AllowAnotherPtr) 18898 SemaRef.Diag(ELoc, diag::err_omp_section_length_undefined) 18899 << /*array of unknown bound */ 1; 18900 else 18901 IsNonContiguous = true; 18902 } else if (AllowUnitySizeArraySection && NotUnity) { 18903 // A unity or whole array section is not allowed and that is not 18904 // compatible with the properties of the current array section. 18905 if (NoDiagnose) 18906 return false; 18907 SemaRef.Diag( 18908 ELoc, diag::err_array_section_does_not_specify_contiguous_storage) 18909 << OASE->getSourceRange(); 18910 return false; 18911 } 18912 18913 if (IsPointer) 18914 AllowAnotherPtr = false; 18915 18916 if (const auto *TE = dyn_cast<CXXThisExpr>(E)) { 18917 Expr::EvalResult ResultR; 18918 Expr::EvalResult ResultL; 18919 if (!OASE->getLength()->isValueDependent() && 18920 OASE->getLength()->EvaluateAsInt(ResultR, SemaRef.getASTContext()) && 18921 !ResultR.Val.getInt().isOne()) { 18922 SemaRef.Diag(OASE->getLength()->getExprLoc(), 18923 diag::err_omp_invalid_map_this_expr); 18924 SemaRef.Diag(OASE->getLength()->getExprLoc(), 18925 diag::note_omp_invalid_length_on_this_ptr_mapping); 18926 } 18927 if (OASE->getLowerBound() && !OASE->getLowerBound()->isValueDependent() && 18928 OASE->getLowerBound()->EvaluateAsInt(ResultL, 18929 SemaRef.getASTContext()) && 18930 !ResultL.Val.getInt().isZero()) { 18931 SemaRef.Diag(OASE->getLowerBound()->getExprLoc(), 18932 diag::err_omp_invalid_map_this_expr); 18933 SemaRef.Diag(OASE->getLowerBound()->getExprLoc(), 18934 diag::note_omp_invalid_lower_bound_on_this_ptr_mapping); 18935 } 18936 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 18937 RelevantExpr = TE; 18938 } 18939 18940 // Record the component - we don't have any declaration associated. 18941 Components.emplace_back(OASE, nullptr, /*IsNonContiguous=*/false); 18942 return RelevantExpr || Visit(E); 18943 } 18944 bool VisitOMPArrayShapingExpr(OMPArrayShapingExpr *E) { 18945 Expr *Base = E->getBase(); 18946 18947 // Record the component - we don't have any declaration associated. 18948 Components.emplace_back(E, nullptr, IsNonContiguous); 18949 18950 return Visit(Base->IgnoreParenImpCasts()); 18951 } 18952 18953 bool VisitUnaryOperator(UnaryOperator *UO) { 18954 if (SemaRef.getLangOpts().OpenMP < 50 || !UO->isLValue() || 18955 UO->getOpcode() != UO_Deref) { 18956 emitErrorMsg(); 18957 return false; 18958 } 18959 if (!RelevantExpr) { 18960 // Record the component if haven't found base decl. 18961 Components.emplace_back(UO, nullptr, /*IsNonContiguous=*/false); 18962 } 18963 return RelevantExpr || Visit(UO->getSubExpr()->IgnoreParenImpCasts()); 18964 } 18965 bool VisitBinaryOperator(BinaryOperator *BO) { 18966 if (SemaRef.getLangOpts().OpenMP < 50 || !BO->getType()->isPointerType()) { 18967 emitErrorMsg(); 18968 return false; 18969 } 18970 18971 // Pointer arithmetic is the only thing we expect to happen here so after we 18972 // make sure the binary operator is a pointer type, the we only thing need 18973 // to to is to visit the subtree that has the same type as root (so that we 18974 // know the other subtree is just an offset) 18975 Expr *LE = BO->getLHS()->IgnoreParenImpCasts(); 18976 Expr *RE = BO->getRHS()->IgnoreParenImpCasts(); 18977 Components.emplace_back(BO, nullptr, false); 18978 assert((LE->getType().getTypePtr() == BO->getType().getTypePtr() || 18979 RE->getType().getTypePtr() == BO->getType().getTypePtr()) && 18980 "Either LHS or RHS have base decl inside"); 18981 if (BO->getType().getTypePtr() == LE->getType().getTypePtr()) 18982 return RelevantExpr || Visit(LE); 18983 return RelevantExpr || Visit(RE); 18984 } 18985 bool VisitCXXThisExpr(CXXThisExpr *CTE) { 18986 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 18987 RelevantExpr = CTE; 18988 Components.emplace_back(CTE, nullptr, IsNonContiguous); 18989 return true; 18990 } 18991 bool VisitCXXOperatorCallExpr(CXXOperatorCallExpr *COCE) { 18992 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 18993 Components.emplace_back(COCE, nullptr, IsNonContiguous); 18994 return true; 18995 } 18996 bool VisitOpaqueValueExpr(OpaqueValueExpr *E) { 18997 Expr *Source = E->getSourceExpr(); 18998 if (!Source) { 18999 emitErrorMsg(); 19000 return false; 19001 } 19002 return Visit(Source); 19003 } 19004 bool VisitStmt(Stmt *) { 19005 emitErrorMsg(); 19006 return false; 19007 } 19008 const Expr *getFoundBase() const { 19009 return RelevantExpr; 19010 } 19011 explicit MapBaseChecker( 19012 Sema &SemaRef, OpenMPClauseKind CKind, OpenMPDirectiveKind DKind, 19013 OMPClauseMappableExprCommon::MappableExprComponentList &Components, 19014 bool NoDiagnose, SourceLocation &ELoc, SourceRange &ERange) 19015 : SemaRef(SemaRef), CKind(CKind), DKind(DKind), Components(Components), 19016 NoDiagnose(NoDiagnose), ELoc(ELoc), ERange(ERange) {} 19017 }; 19018 } // namespace 19019 19020 /// Return the expression of the base of the mappable expression or null if it 19021 /// cannot be determined and do all the necessary checks to see if the expression 19022 /// is valid as a standalone mappable expression. In the process, record all the 19023 /// components of the expression. 19024 static const Expr *checkMapClauseExpressionBase( 19025 Sema &SemaRef, Expr *E, 19026 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents, 19027 OpenMPClauseKind CKind, OpenMPDirectiveKind DKind, bool NoDiagnose) { 19028 SourceLocation ELoc = E->getExprLoc(); 19029 SourceRange ERange = E->getSourceRange(); 19030 MapBaseChecker Checker(SemaRef, CKind, DKind, CurComponents, NoDiagnose, ELoc, 19031 ERange); 19032 if (Checker.Visit(E->IgnoreParens())) { 19033 // Check if the highest dimension array section has length specified 19034 if (SemaRef.getLangOpts().OpenMP >= 50 && !CurComponents.empty() && 19035 (CKind == OMPC_to || CKind == OMPC_from)) { 19036 auto CI = CurComponents.rbegin(); 19037 auto CE = CurComponents.rend(); 19038 for (; CI != CE; ++CI) { 19039 const auto *OASE = 19040 dyn_cast<OMPArraySectionExpr>(CI->getAssociatedExpression()); 19041 if (!OASE) 19042 continue; 19043 if (OASE && OASE->getLength()) 19044 break; 19045 SemaRef.Diag(ELoc, diag::err_array_section_does_not_specify_length) 19046 << ERange; 19047 } 19048 } 19049 return Checker.getFoundBase(); 19050 } 19051 return nullptr; 19052 } 19053 19054 // Return true if expression E associated with value VD has conflicts with other 19055 // map information. 19056 static bool checkMapConflicts( 19057 Sema &SemaRef, DSAStackTy *DSAS, const ValueDecl *VD, const Expr *E, 19058 bool CurrentRegionOnly, 19059 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents, 19060 OpenMPClauseKind CKind) { 19061 assert(VD && E); 19062 SourceLocation ELoc = E->getExprLoc(); 19063 SourceRange ERange = E->getSourceRange(); 19064 19065 // In order to easily check the conflicts we need to match each component of 19066 // the expression under test with the components of the expressions that are 19067 // already in the stack. 19068 19069 assert(!CurComponents.empty() && "Map clause expression with no components!"); 19070 assert(CurComponents.back().getAssociatedDeclaration() == VD && 19071 "Map clause expression with unexpected base!"); 19072 19073 // Variables to help detecting enclosing problems in data environment nests. 19074 bool IsEnclosedByDataEnvironmentExpr = false; 19075 const Expr *EnclosingExpr = nullptr; 19076 19077 bool FoundError = DSAS->checkMappableExprComponentListsForDecl( 19078 VD, CurrentRegionOnly, 19079 [&IsEnclosedByDataEnvironmentExpr, &SemaRef, VD, CurrentRegionOnly, ELoc, 19080 ERange, CKind, &EnclosingExpr, 19081 CurComponents](OMPClauseMappableExprCommon::MappableExprComponentListRef 19082 StackComponents, 19083 OpenMPClauseKind Kind) { 19084 if (CKind == Kind && SemaRef.LangOpts.OpenMP >= 50) 19085 return false; 19086 assert(!StackComponents.empty() && 19087 "Map clause expression with no components!"); 19088 assert(StackComponents.back().getAssociatedDeclaration() == VD && 19089 "Map clause expression with unexpected base!"); 19090 (void)VD; 19091 19092 // The whole expression in the stack. 19093 const Expr *RE = StackComponents.front().getAssociatedExpression(); 19094 19095 // Expressions must start from the same base. Here we detect at which 19096 // point both expressions diverge from each other and see if we can 19097 // detect if the memory referred to both expressions is contiguous and 19098 // do not overlap. 19099 auto CI = CurComponents.rbegin(); 19100 auto CE = CurComponents.rend(); 19101 auto SI = StackComponents.rbegin(); 19102 auto SE = StackComponents.rend(); 19103 for (; CI != CE && SI != SE; ++CI, ++SI) { 19104 19105 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3] 19106 // At most one list item can be an array item derived from a given 19107 // variable in map clauses of the same construct. 19108 if (CurrentRegionOnly && 19109 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) || 19110 isa<OMPArraySectionExpr>(CI->getAssociatedExpression()) || 19111 isa<OMPArrayShapingExpr>(CI->getAssociatedExpression())) && 19112 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) || 19113 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()) || 19114 isa<OMPArrayShapingExpr>(SI->getAssociatedExpression()))) { 19115 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(), 19116 diag::err_omp_multiple_array_items_in_map_clause) 19117 << CI->getAssociatedExpression()->getSourceRange(); 19118 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(), 19119 diag::note_used_here) 19120 << SI->getAssociatedExpression()->getSourceRange(); 19121 return true; 19122 } 19123 19124 // Do both expressions have the same kind? 19125 if (CI->getAssociatedExpression()->getStmtClass() != 19126 SI->getAssociatedExpression()->getStmtClass()) 19127 break; 19128 19129 // Are we dealing with different variables/fields? 19130 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration()) 19131 break; 19132 } 19133 // Check if the extra components of the expressions in the enclosing 19134 // data environment are redundant for the current base declaration. 19135 // If they are, the maps completely overlap, which is legal. 19136 for (; SI != SE; ++SI) { 19137 QualType Type; 19138 if (const auto *ASE = 19139 dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) { 19140 Type = ASE->getBase()->IgnoreParenImpCasts()->getType(); 19141 } else if (const auto *OASE = dyn_cast<OMPArraySectionExpr>( 19142 SI->getAssociatedExpression())) { 19143 const Expr *E = OASE->getBase()->IgnoreParenImpCasts(); 19144 Type = 19145 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType(); 19146 } else if (const auto *OASE = dyn_cast<OMPArrayShapingExpr>( 19147 SI->getAssociatedExpression())) { 19148 Type = OASE->getBase()->getType()->getPointeeType(); 19149 } 19150 if (Type.isNull() || Type->isAnyPointerType() || 19151 checkArrayExpressionDoesNotReferToWholeSize( 19152 SemaRef, SI->getAssociatedExpression(), Type)) 19153 break; 19154 } 19155 19156 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4] 19157 // List items of map clauses in the same construct must not share 19158 // original storage. 19159 // 19160 // If the expressions are exactly the same or one is a subset of the 19161 // other, it means they are sharing storage. 19162 if (CI == CE && SI == SE) { 19163 if (CurrentRegionOnly) { 19164 if (CKind == OMPC_map) { 19165 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange; 19166 } else { 19167 assert(CKind == OMPC_to || CKind == OMPC_from); 19168 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update) 19169 << ERange; 19170 } 19171 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 19172 << RE->getSourceRange(); 19173 return true; 19174 } 19175 // If we find the same expression in the enclosing data environment, 19176 // that is legal. 19177 IsEnclosedByDataEnvironmentExpr = true; 19178 return false; 19179 } 19180 19181 QualType DerivedType = 19182 std::prev(CI)->getAssociatedDeclaration()->getType(); 19183 SourceLocation DerivedLoc = 19184 std::prev(CI)->getAssociatedExpression()->getExprLoc(); 19185 19186 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 19187 // If the type of a list item is a reference to a type T then the type 19188 // will be considered to be T for all purposes of this clause. 19189 DerivedType = DerivedType.getNonReferenceType(); 19190 19191 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1] 19192 // A variable for which the type is pointer and an array section 19193 // derived from that variable must not appear as list items of map 19194 // clauses of the same construct. 19195 // 19196 // Also, cover one of the cases in: 19197 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5] 19198 // If any part of the original storage of a list item has corresponding 19199 // storage in the device data environment, all of the original storage 19200 // must have corresponding storage in the device data environment. 19201 // 19202 if (DerivedType->isAnyPointerType()) { 19203 if (CI == CE || SI == SE) { 19204 SemaRef.Diag( 19205 DerivedLoc, 19206 diag::err_omp_pointer_mapped_along_with_derived_section) 19207 << DerivedLoc; 19208 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 19209 << RE->getSourceRange(); 19210 return true; 19211 } 19212 if (CI->getAssociatedExpression()->getStmtClass() != 19213 SI->getAssociatedExpression()->getStmtClass() || 19214 CI->getAssociatedDeclaration()->getCanonicalDecl() == 19215 SI->getAssociatedDeclaration()->getCanonicalDecl()) { 19216 assert(CI != CE && SI != SE); 19217 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_dereferenced) 19218 << DerivedLoc; 19219 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 19220 << RE->getSourceRange(); 19221 return true; 19222 } 19223 } 19224 19225 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4] 19226 // List items of map clauses in the same construct must not share 19227 // original storage. 19228 // 19229 // An expression is a subset of the other. 19230 if (CurrentRegionOnly && (CI == CE || SI == SE)) { 19231 if (CKind == OMPC_map) { 19232 if (CI != CE || SI != SE) { 19233 // Allow constructs like this: map(s, s.ptr[0:1]), where s.ptr is 19234 // a pointer. 19235 auto Begin = 19236 CI != CE ? CurComponents.begin() : StackComponents.begin(); 19237 auto End = CI != CE ? CurComponents.end() : StackComponents.end(); 19238 auto It = Begin; 19239 while (It != End && !It->getAssociatedDeclaration()) 19240 std::advance(It, 1); 19241 assert(It != End && 19242 "Expected at least one component with the declaration."); 19243 if (It != Begin && It->getAssociatedDeclaration() 19244 ->getType() 19245 .getCanonicalType() 19246 ->isAnyPointerType()) { 19247 IsEnclosedByDataEnvironmentExpr = false; 19248 EnclosingExpr = nullptr; 19249 return false; 19250 } 19251 } 19252 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange; 19253 } else { 19254 assert(CKind == OMPC_to || CKind == OMPC_from); 19255 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update) 19256 << ERange; 19257 } 19258 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 19259 << RE->getSourceRange(); 19260 return true; 19261 } 19262 19263 // The current expression uses the same base as other expression in the 19264 // data environment but does not contain it completely. 19265 if (!CurrentRegionOnly && SI != SE) 19266 EnclosingExpr = RE; 19267 19268 // The current expression is a subset of the expression in the data 19269 // environment. 19270 IsEnclosedByDataEnvironmentExpr |= 19271 (!CurrentRegionOnly && CI != CE && SI == SE); 19272 19273 return false; 19274 }); 19275 19276 if (CurrentRegionOnly) 19277 return FoundError; 19278 19279 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5] 19280 // If any part of the original storage of a list item has corresponding 19281 // storage in the device data environment, all of the original storage must 19282 // have corresponding storage in the device data environment. 19283 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6] 19284 // If a list item is an element of a structure, and a different element of 19285 // the structure has a corresponding list item in the device data environment 19286 // prior to a task encountering the construct associated with the map clause, 19287 // then the list item must also have a corresponding list item in the device 19288 // data environment prior to the task encountering the construct. 19289 // 19290 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) { 19291 SemaRef.Diag(ELoc, 19292 diag::err_omp_original_storage_is_shared_and_does_not_contain) 19293 << ERange; 19294 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here) 19295 << EnclosingExpr->getSourceRange(); 19296 return true; 19297 } 19298 19299 return FoundError; 19300 } 19301 19302 // Look up the user-defined mapper given the mapper name and mapped type, and 19303 // build a reference to it. 19304 static ExprResult buildUserDefinedMapperRef(Sema &SemaRef, Scope *S, 19305 CXXScopeSpec &MapperIdScopeSpec, 19306 const DeclarationNameInfo &MapperId, 19307 QualType Type, 19308 Expr *UnresolvedMapper) { 19309 if (MapperIdScopeSpec.isInvalid()) 19310 return ExprError(); 19311 // Get the actual type for the array type. 19312 if (Type->isArrayType()) { 19313 assert(Type->getAsArrayTypeUnsafe() && "Expect to get a valid array type"); 19314 Type = Type->getAsArrayTypeUnsafe()->getElementType().getCanonicalType(); 19315 } 19316 // Find all user-defined mappers with the given MapperId. 19317 SmallVector<UnresolvedSet<8>, 4> Lookups; 19318 LookupResult Lookup(SemaRef, MapperId, Sema::LookupOMPMapperName); 19319 Lookup.suppressDiagnostics(); 19320 if (S) { 19321 while (S && SemaRef.LookupParsedName(Lookup, S, &MapperIdScopeSpec)) { 19322 NamedDecl *D = Lookup.getRepresentativeDecl(); 19323 while (S && !S->isDeclScope(D)) 19324 S = S->getParent(); 19325 if (S) 19326 S = S->getParent(); 19327 Lookups.emplace_back(); 19328 Lookups.back().append(Lookup.begin(), Lookup.end()); 19329 Lookup.clear(); 19330 } 19331 } else if (auto *ULE = cast_or_null<UnresolvedLookupExpr>(UnresolvedMapper)) { 19332 // Extract the user-defined mappers with the given MapperId. 19333 Lookups.push_back(UnresolvedSet<8>()); 19334 for (NamedDecl *D : ULE->decls()) { 19335 auto *DMD = cast<OMPDeclareMapperDecl>(D); 19336 assert(DMD && "Expect valid OMPDeclareMapperDecl during instantiation."); 19337 Lookups.back().addDecl(DMD); 19338 } 19339 } 19340 // Defer the lookup for dependent types. The results will be passed through 19341 // UnresolvedMapper on instantiation. 19342 if (SemaRef.CurContext->isDependentContext() || Type->isDependentType() || 19343 Type->isInstantiationDependentType() || 19344 Type->containsUnexpandedParameterPack() || 19345 filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) { 19346 return !D->isInvalidDecl() && 19347 (D->getType()->isDependentType() || 19348 D->getType()->isInstantiationDependentType() || 19349 D->getType()->containsUnexpandedParameterPack()); 19350 })) { 19351 UnresolvedSet<8> URS; 19352 for (const UnresolvedSet<8> &Set : Lookups) { 19353 if (Set.empty()) 19354 continue; 19355 URS.append(Set.begin(), Set.end()); 19356 } 19357 return UnresolvedLookupExpr::Create( 19358 SemaRef.Context, /*NamingClass=*/nullptr, 19359 MapperIdScopeSpec.getWithLocInContext(SemaRef.Context), MapperId, 19360 /*ADL=*/false, /*Overloaded=*/true, URS.begin(), URS.end()); 19361 } 19362 SourceLocation Loc = MapperId.getLoc(); 19363 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions 19364 // The type must be of struct, union or class type in C and C++ 19365 if (!Type->isStructureOrClassType() && !Type->isUnionType() && 19366 (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default")) { 19367 SemaRef.Diag(Loc, diag::err_omp_mapper_wrong_type); 19368 return ExprError(); 19369 } 19370 // Perform argument dependent lookup. 19371 if (SemaRef.getLangOpts().CPlusPlus && !MapperIdScopeSpec.isSet()) 19372 argumentDependentLookup(SemaRef, MapperId, Loc, Type, Lookups); 19373 // Return the first user-defined mapper with the desired type. 19374 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 19375 Lookups, [&SemaRef, Type](ValueDecl *D) -> ValueDecl * { 19376 if (!D->isInvalidDecl() && 19377 SemaRef.Context.hasSameType(D->getType(), Type)) 19378 return D; 19379 return nullptr; 19380 })) 19381 return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc); 19382 // Find the first user-defined mapper with a type derived from the desired 19383 // type. 19384 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 19385 Lookups, [&SemaRef, Type, Loc](ValueDecl *D) -> ValueDecl * { 19386 if (!D->isInvalidDecl() && 19387 SemaRef.IsDerivedFrom(Loc, Type, D->getType()) && 19388 !Type.isMoreQualifiedThan(D->getType())) 19389 return D; 19390 return nullptr; 19391 })) { 19392 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, 19393 /*DetectVirtual=*/false); 19394 if (SemaRef.IsDerivedFrom(Loc, Type, VD->getType(), Paths)) { 19395 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType( 19396 VD->getType().getUnqualifiedType()))) { 19397 if (SemaRef.CheckBaseClassAccess( 19398 Loc, VD->getType(), Type, Paths.front(), 19399 /*DiagID=*/0) != Sema::AR_inaccessible) { 19400 return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc); 19401 } 19402 } 19403 } 19404 } 19405 // Report error if a mapper is specified, but cannot be found. 19406 if (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default") { 19407 SemaRef.Diag(Loc, diag::err_omp_invalid_mapper) 19408 << Type << MapperId.getName(); 19409 return ExprError(); 19410 } 19411 return ExprEmpty(); 19412 } 19413 19414 namespace { 19415 // Utility struct that gathers all the related lists associated with a mappable 19416 // expression. 19417 struct MappableVarListInfo { 19418 // The list of expressions. 19419 ArrayRef<Expr *> VarList; 19420 // The list of processed expressions. 19421 SmallVector<Expr *, 16> ProcessedVarList; 19422 // The mappble components for each expression. 19423 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents; 19424 // The base declaration of the variable. 19425 SmallVector<ValueDecl *, 16> VarBaseDeclarations; 19426 // The reference to the user-defined mapper associated with every expression. 19427 SmallVector<Expr *, 16> UDMapperList; 19428 19429 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) { 19430 // We have a list of components and base declarations for each entry in the 19431 // variable list. 19432 VarComponents.reserve(VarList.size()); 19433 VarBaseDeclarations.reserve(VarList.size()); 19434 } 19435 }; 19436 } 19437 19438 // Check the validity of the provided variable list for the provided clause kind 19439 // \a CKind. In the check process the valid expressions, mappable expression 19440 // components, variables, and user-defined mappers are extracted and used to 19441 // fill \a ProcessedVarList, \a VarComponents, \a VarBaseDeclarations, and \a 19442 // UDMapperList in MVLI. \a MapType, \a IsMapTypeImplicit, \a MapperIdScopeSpec, 19443 // and \a MapperId are expected to be valid if the clause kind is 'map'. 19444 static void checkMappableExpressionList( 19445 Sema &SemaRef, DSAStackTy *DSAS, OpenMPClauseKind CKind, 19446 MappableVarListInfo &MVLI, SourceLocation StartLoc, 19447 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo MapperId, 19448 ArrayRef<Expr *> UnresolvedMappers, 19449 OpenMPMapClauseKind MapType = OMPC_MAP_unknown, 19450 ArrayRef<OpenMPMapModifierKind> Modifiers = None, 19451 bool IsMapTypeImplicit = false, bool NoDiagnose = false) { 19452 // We only expect mappable expressions in 'to', 'from', and 'map' clauses. 19453 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) && 19454 "Unexpected clause kind with mappable expressions!"); 19455 19456 // If the identifier of user-defined mapper is not specified, it is "default". 19457 // We do not change the actual name in this clause to distinguish whether a 19458 // mapper is specified explicitly, i.e., it is not explicitly specified when 19459 // MapperId.getName() is empty. 19460 if (!MapperId.getName() || MapperId.getName().isEmpty()) { 19461 auto &DeclNames = SemaRef.getASTContext().DeclarationNames; 19462 MapperId.setName(DeclNames.getIdentifier( 19463 &SemaRef.getASTContext().Idents.get("default"))); 19464 MapperId.setLoc(StartLoc); 19465 } 19466 19467 // Iterators to find the current unresolved mapper expression. 19468 auto UMIt = UnresolvedMappers.begin(), UMEnd = UnresolvedMappers.end(); 19469 bool UpdateUMIt = false; 19470 Expr *UnresolvedMapper = nullptr; 19471 19472 bool HasHoldModifier = 19473 llvm::is_contained(Modifiers, OMPC_MAP_MODIFIER_ompx_hold); 19474 19475 // Keep track of the mappable components and base declarations in this clause. 19476 // Each entry in the list is going to have a list of components associated. We 19477 // record each set of the components so that we can build the clause later on. 19478 // In the end we should have the same amount of declarations and component 19479 // lists. 19480 19481 for (Expr *RE : MVLI.VarList) { 19482 assert(RE && "Null expr in omp to/from/map clause"); 19483 SourceLocation ELoc = RE->getExprLoc(); 19484 19485 // Find the current unresolved mapper expression. 19486 if (UpdateUMIt && UMIt != UMEnd) { 19487 UMIt++; 19488 assert( 19489 UMIt != UMEnd && 19490 "Expect the size of UnresolvedMappers to match with that of VarList"); 19491 } 19492 UpdateUMIt = true; 19493 if (UMIt != UMEnd) 19494 UnresolvedMapper = *UMIt; 19495 19496 const Expr *VE = RE->IgnoreParenLValueCasts(); 19497 19498 if (VE->isValueDependent() || VE->isTypeDependent() || 19499 VE->isInstantiationDependent() || 19500 VE->containsUnexpandedParameterPack()) { 19501 // Try to find the associated user-defined mapper. 19502 ExprResult ER = buildUserDefinedMapperRef( 19503 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId, 19504 VE->getType().getCanonicalType(), UnresolvedMapper); 19505 if (ER.isInvalid()) 19506 continue; 19507 MVLI.UDMapperList.push_back(ER.get()); 19508 // We can only analyze this information once the missing information is 19509 // resolved. 19510 MVLI.ProcessedVarList.push_back(RE); 19511 continue; 19512 } 19513 19514 Expr *SimpleExpr = RE->IgnoreParenCasts(); 19515 19516 if (!RE->isLValue()) { 19517 if (SemaRef.getLangOpts().OpenMP < 50) { 19518 SemaRef.Diag( 19519 ELoc, diag::err_omp_expected_named_var_member_or_array_expression) 19520 << RE->getSourceRange(); 19521 } else { 19522 SemaRef.Diag(ELoc, diag::err_omp_non_lvalue_in_map_or_motion_clauses) 19523 << getOpenMPClauseName(CKind) << RE->getSourceRange(); 19524 } 19525 continue; 19526 } 19527 19528 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents; 19529 ValueDecl *CurDeclaration = nullptr; 19530 19531 // Obtain the array or member expression bases if required. Also, fill the 19532 // components array with all the components identified in the process. 19533 const Expr *BE = 19534 checkMapClauseExpressionBase(SemaRef, SimpleExpr, CurComponents, CKind, 19535 DSAS->getCurrentDirective(), NoDiagnose); 19536 if (!BE) 19537 continue; 19538 19539 assert(!CurComponents.empty() && 19540 "Invalid mappable expression information."); 19541 19542 if (const auto *TE = dyn_cast<CXXThisExpr>(BE)) { 19543 // Add store "this" pointer to class in DSAStackTy for future checking 19544 DSAS->addMappedClassesQualTypes(TE->getType()); 19545 // Try to find the associated user-defined mapper. 19546 ExprResult ER = buildUserDefinedMapperRef( 19547 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId, 19548 VE->getType().getCanonicalType(), UnresolvedMapper); 19549 if (ER.isInvalid()) 19550 continue; 19551 MVLI.UDMapperList.push_back(ER.get()); 19552 // Skip restriction checking for variable or field declarations 19553 MVLI.ProcessedVarList.push_back(RE); 19554 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 19555 MVLI.VarComponents.back().append(CurComponents.begin(), 19556 CurComponents.end()); 19557 MVLI.VarBaseDeclarations.push_back(nullptr); 19558 continue; 19559 } 19560 19561 // For the following checks, we rely on the base declaration which is 19562 // expected to be associated with the last component. The declaration is 19563 // expected to be a variable or a field (if 'this' is being mapped). 19564 CurDeclaration = CurComponents.back().getAssociatedDeclaration(); 19565 assert(CurDeclaration && "Null decl on map clause."); 19566 assert( 19567 CurDeclaration->isCanonicalDecl() && 19568 "Expecting components to have associated only canonical declarations."); 19569 19570 auto *VD = dyn_cast<VarDecl>(CurDeclaration); 19571 const auto *FD = dyn_cast<FieldDecl>(CurDeclaration); 19572 19573 assert((VD || FD) && "Only variables or fields are expected here!"); 19574 (void)FD; 19575 19576 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10] 19577 // threadprivate variables cannot appear in a map clause. 19578 // OpenMP 4.5 [2.10.5, target update Construct] 19579 // threadprivate variables cannot appear in a from clause. 19580 if (VD && DSAS->isThreadPrivate(VD)) { 19581 if (NoDiagnose) 19582 continue; 19583 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false); 19584 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause) 19585 << getOpenMPClauseName(CKind); 19586 reportOriginalDsa(SemaRef, DSAS, VD, DVar); 19587 continue; 19588 } 19589 19590 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9] 19591 // A list item cannot appear in both a map clause and a data-sharing 19592 // attribute clause on the same construct. 19593 19594 // Check conflicts with other map clause expressions. We check the conflicts 19595 // with the current construct separately from the enclosing data 19596 // environment, because the restrictions are different. We only have to 19597 // check conflicts across regions for the map clauses. 19598 if (checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr, 19599 /*CurrentRegionOnly=*/true, CurComponents, CKind)) 19600 break; 19601 if (CKind == OMPC_map && 19602 (SemaRef.getLangOpts().OpenMP <= 45 || StartLoc.isValid()) && 19603 checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr, 19604 /*CurrentRegionOnly=*/false, CurComponents, CKind)) 19605 break; 19606 19607 // OpenMP 4.5 [2.10.5, target update Construct] 19608 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 19609 // If the type of a list item is a reference to a type T then the type will 19610 // be considered to be T for all purposes of this clause. 19611 auto I = llvm::find_if( 19612 CurComponents, 19613 [](const OMPClauseMappableExprCommon::MappableComponent &MC) { 19614 return MC.getAssociatedDeclaration(); 19615 }); 19616 assert(I != CurComponents.end() && "Null decl on map clause."); 19617 (void)I; 19618 QualType Type; 19619 auto *ASE = dyn_cast<ArraySubscriptExpr>(VE->IgnoreParens()); 19620 auto *OASE = dyn_cast<OMPArraySectionExpr>(VE->IgnoreParens()); 19621 auto *OAShE = dyn_cast<OMPArrayShapingExpr>(VE->IgnoreParens()); 19622 if (ASE) { 19623 Type = ASE->getType().getNonReferenceType(); 19624 } else if (OASE) { 19625 QualType BaseType = 19626 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 19627 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 19628 Type = ATy->getElementType(); 19629 else 19630 Type = BaseType->getPointeeType(); 19631 Type = Type.getNonReferenceType(); 19632 } else if (OAShE) { 19633 Type = OAShE->getBase()->getType()->getPointeeType(); 19634 } else { 19635 Type = VE->getType(); 19636 } 19637 19638 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4] 19639 // A list item in a to or from clause must have a mappable type. 19640 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9] 19641 // A list item must have a mappable type. 19642 if (!checkTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef, 19643 DSAS, Type, /*FullCheck=*/true)) 19644 continue; 19645 19646 if (CKind == OMPC_map) { 19647 // target enter data 19648 // OpenMP [2.10.2, Restrictions, p. 99] 19649 // A map-type must be specified in all map clauses and must be either 19650 // to or alloc. 19651 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective(); 19652 if (DKind == OMPD_target_enter_data && 19653 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) { 19654 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 19655 << (IsMapTypeImplicit ? 1 : 0) 19656 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 19657 << getOpenMPDirectiveName(DKind); 19658 continue; 19659 } 19660 19661 // target exit_data 19662 // OpenMP [2.10.3, Restrictions, p. 102] 19663 // A map-type must be specified in all map clauses and must be either 19664 // from, release, or delete. 19665 if (DKind == OMPD_target_exit_data && 19666 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release || 19667 MapType == OMPC_MAP_delete)) { 19668 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 19669 << (IsMapTypeImplicit ? 1 : 0) 19670 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 19671 << getOpenMPDirectiveName(DKind); 19672 continue; 19673 } 19674 19675 // The 'ompx_hold' modifier is specifically intended to be used on a 19676 // 'target' or 'target data' directive to prevent data from being unmapped 19677 // during the associated statement. It is not permitted on a 'target 19678 // enter data' or 'target exit data' directive, which have no associated 19679 // statement. 19680 if ((DKind == OMPD_target_enter_data || DKind == OMPD_target_exit_data) && 19681 HasHoldModifier) { 19682 SemaRef.Diag(StartLoc, 19683 diag::err_omp_invalid_map_type_modifier_for_directive) 19684 << getOpenMPSimpleClauseTypeName(OMPC_map, 19685 OMPC_MAP_MODIFIER_ompx_hold) 19686 << getOpenMPDirectiveName(DKind); 19687 continue; 19688 } 19689 19690 // target, target data 19691 // OpenMP 5.0 [2.12.2, Restrictions, p. 163] 19692 // OpenMP 5.0 [2.12.5, Restrictions, p. 174] 19693 // A map-type in a map clause must be to, from, tofrom or alloc 19694 if ((DKind == OMPD_target_data || 19695 isOpenMPTargetExecutionDirective(DKind)) && 19696 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_from || 19697 MapType == OMPC_MAP_tofrom || MapType == OMPC_MAP_alloc)) { 19698 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 19699 << (IsMapTypeImplicit ? 1 : 0) 19700 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 19701 << getOpenMPDirectiveName(DKind); 19702 continue; 19703 } 19704 19705 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 19706 // A list item cannot appear in both a map clause and a data-sharing 19707 // attribute clause on the same construct 19708 // 19709 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7] 19710 // A list item cannot appear in both a map clause and a data-sharing 19711 // attribute clause on the same construct unless the construct is a 19712 // combined construct. 19713 if (VD && ((SemaRef.LangOpts.OpenMP <= 45 && 19714 isOpenMPTargetExecutionDirective(DKind)) || 19715 DKind == OMPD_target)) { 19716 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false); 19717 if (isOpenMPPrivate(DVar.CKind)) { 19718 SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 19719 << getOpenMPClauseName(DVar.CKind) 19720 << getOpenMPClauseName(OMPC_map) 19721 << getOpenMPDirectiveName(DSAS->getCurrentDirective()); 19722 reportOriginalDsa(SemaRef, DSAS, CurDeclaration, DVar); 19723 continue; 19724 } 19725 } 19726 } 19727 19728 // Try to find the associated user-defined mapper. 19729 ExprResult ER = buildUserDefinedMapperRef( 19730 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId, 19731 Type.getCanonicalType(), UnresolvedMapper); 19732 if (ER.isInvalid()) 19733 continue; 19734 MVLI.UDMapperList.push_back(ER.get()); 19735 19736 // Save the current expression. 19737 MVLI.ProcessedVarList.push_back(RE); 19738 19739 // Store the components in the stack so that they can be used to check 19740 // against other clauses later on. 19741 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents, 19742 /*WhereFoundClauseKind=*/OMPC_map); 19743 19744 // Save the components and declaration to create the clause. For purposes of 19745 // the clause creation, any component list that has has base 'this' uses 19746 // null as base declaration. 19747 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 19748 MVLI.VarComponents.back().append(CurComponents.begin(), 19749 CurComponents.end()); 19750 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr 19751 : CurDeclaration); 19752 } 19753 } 19754 19755 OMPClause *Sema::ActOnOpenMPMapClause( 19756 ArrayRef<OpenMPMapModifierKind> MapTypeModifiers, 19757 ArrayRef<SourceLocation> MapTypeModifiersLoc, 19758 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId, 19759 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, SourceLocation MapLoc, 19760 SourceLocation ColonLoc, ArrayRef<Expr *> VarList, 19761 const OMPVarListLocTy &Locs, bool NoDiagnose, 19762 ArrayRef<Expr *> UnresolvedMappers) { 19763 OpenMPMapModifierKind Modifiers[] = { 19764 OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown, 19765 OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown, 19766 OMPC_MAP_MODIFIER_unknown}; 19767 SourceLocation ModifiersLoc[NumberOfOMPMapClauseModifiers]; 19768 19769 // Process map-type-modifiers, flag errors for duplicate modifiers. 19770 unsigned Count = 0; 19771 for (unsigned I = 0, E = MapTypeModifiers.size(); I < E; ++I) { 19772 if (MapTypeModifiers[I] != OMPC_MAP_MODIFIER_unknown && 19773 llvm::is_contained(Modifiers, MapTypeModifiers[I])) { 19774 Diag(MapTypeModifiersLoc[I], diag::err_omp_duplicate_map_type_modifier); 19775 continue; 19776 } 19777 assert(Count < NumberOfOMPMapClauseModifiers && 19778 "Modifiers exceed the allowed number of map type modifiers"); 19779 Modifiers[Count] = MapTypeModifiers[I]; 19780 ModifiersLoc[Count] = MapTypeModifiersLoc[I]; 19781 ++Count; 19782 } 19783 19784 MappableVarListInfo MVLI(VarList); 19785 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, Locs.StartLoc, 19786 MapperIdScopeSpec, MapperId, UnresolvedMappers, 19787 MapType, Modifiers, IsMapTypeImplicit, 19788 NoDiagnose); 19789 19790 // We need to produce a map clause even if we don't have variables so that 19791 // other diagnostics related with non-existing map clauses are accurate. 19792 return OMPMapClause::Create(Context, Locs, MVLI.ProcessedVarList, 19793 MVLI.VarBaseDeclarations, MVLI.VarComponents, 19794 MVLI.UDMapperList, Modifiers, ModifiersLoc, 19795 MapperIdScopeSpec.getWithLocInContext(Context), 19796 MapperId, MapType, IsMapTypeImplicit, MapLoc); 19797 } 19798 19799 QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc, 19800 TypeResult ParsedType) { 19801 assert(ParsedType.isUsable()); 19802 19803 QualType ReductionType = GetTypeFromParser(ParsedType.get()); 19804 if (ReductionType.isNull()) 19805 return QualType(); 19806 19807 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++ 19808 // A type name in a declare reduction directive cannot be a function type, an 19809 // array type, a reference type, or a type qualified with const, volatile or 19810 // restrict. 19811 if (ReductionType.hasQualifiers()) { 19812 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0; 19813 return QualType(); 19814 } 19815 19816 if (ReductionType->isFunctionType()) { 19817 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1; 19818 return QualType(); 19819 } 19820 if (ReductionType->isReferenceType()) { 19821 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2; 19822 return QualType(); 19823 } 19824 if (ReductionType->isArrayType()) { 19825 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3; 19826 return QualType(); 19827 } 19828 return ReductionType; 19829 } 19830 19831 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart( 19832 Scope *S, DeclContext *DC, DeclarationName Name, 19833 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes, 19834 AccessSpecifier AS, Decl *PrevDeclInScope) { 19835 SmallVector<Decl *, 8> Decls; 19836 Decls.reserve(ReductionTypes.size()); 19837 19838 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName, 19839 forRedeclarationInCurContext()); 19840 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions 19841 // A reduction-identifier may not be re-declared in the current scope for the 19842 // same type or for a type that is compatible according to the base language 19843 // rules. 19844 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes; 19845 OMPDeclareReductionDecl *PrevDRD = nullptr; 19846 bool InCompoundScope = true; 19847 if (S != nullptr) { 19848 // Find previous declaration with the same name not referenced in other 19849 // declarations. 19850 FunctionScopeInfo *ParentFn = getEnclosingFunction(); 19851 InCompoundScope = 19852 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty(); 19853 LookupName(Lookup, S); 19854 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false, 19855 /*AllowInlineNamespace=*/false); 19856 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious; 19857 LookupResult::Filter Filter = Lookup.makeFilter(); 19858 while (Filter.hasNext()) { 19859 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next()); 19860 if (InCompoundScope) { 19861 auto I = UsedAsPrevious.find(PrevDecl); 19862 if (I == UsedAsPrevious.end()) 19863 UsedAsPrevious[PrevDecl] = false; 19864 if (OMPDeclareReductionDecl *D = PrevDecl->getPrevDeclInScope()) 19865 UsedAsPrevious[D] = true; 19866 } 19867 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] = 19868 PrevDecl->getLocation(); 19869 } 19870 Filter.done(); 19871 if (InCompoundScope) { 19872 for (const auto &PrevData : UsedAsPrevious) { 19873 if (!PrevData.second) { 19874 PrevDRD = PrevData.first; 19875 break; 19876 } 19877 } 19878 } 19879 } else if (PrevDeclInScope != nullptr) { 19880 auto *PrevDRDInScope = PrevDRD = 19881 cast<OMPDeclareReductionDecl>(PrevDeclInScope); 19882 do { 19883 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] = 19884 PrevDRDInScope->getLocation(); 19885 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope(); 19886 } while (PrevDRDInScope != nullptr); 19887 } 19888 for (const auto &TyData : ReductionTypes) { 19889 const auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType()); 19890 bool Invalid = false; 19891 if (I != PreviousRedeclTypes.end()) { 19892 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition) 19893 << TyData.first; 19894 Diag(I->second, diag::note_previous_definition); 19895 Invalid = true; 19896 } 19897 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second; 19898 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second, 19899 Name, TyData.first, PrevDRD); 19900 DC->addDecl(DRD); 19901 DRD->setAccess(AS); 19902 Decls.push_back(DRD); 19903 if (Invalid) 19904 DRD->setInvalidDecl(); 19905 else 19906 PrevDRD = DRD; 19907 } 19908 19909 return DeclGroupPtrTy::make( 19910 DeclGroupRef::Create(Context, Decls.begin(), Decls.size())); 19911 } 19912 19913 void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) { 19914 auto *DRD = cast<OMPDeclareReductionDecl>(D); 19915 19916 // Enter new function scope. 19917 PushFunctionScope(); 19918 setFunctionHasBranchProtectedScope(); 19919 getCurFunction()->setHasOMPDeclareReductionCombiner(); 19920 19921 if (S != nullptr) 19922 PushDeclContext(S, DRD); 19923 else 19924 CurContext = DRD; 19925 19926 PushExpressionEvaluationContext( 19927 ExpressionEvaluationContext::PotentiallyEvaluated); 19928 19929 QualType ReductionType = DRD->getType(); 19930 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will 19931 // be replaced by '*omp_parm' during codegen. This required because 'omp_in' 19932 // uses semantics of argument handles by value, but it should be passed by 19933 // reference. C lang does not support references, so pass all parameters as 19934 // pointers. 19935 // Create 'T omp_in;' variable. 19936 VarDecl *OmpInParm = 19937 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in"); 19938 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will 19939 // be replaced by '*omp_parm' during codegen. This required because 'omp_out' 19940 // uses semantics of argument handles by value, but it should be passed by 19941 // reference. C lang does not support references, so pass all parameters as 19942 // pointers. 19943 // Create 'T omp_out;' variable. 19944 VarDecl *OmpOutParm = 19945 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out"); 19946 if (S != nullptr) { 19947 PushOnScopeChains(OmpInParm, S); 19948 PushOnScopeChains(OmpOutParm, S); 19949 } else { 19950 DRD->addDecl(OmpInParm); 19951 DRD->addDecl(OmpOutParm); 19952 } 19953 Expr *InE = 19954 ::buildDeclRefExpr(*this, OmpInParm, ReductionType, D->getLocation()); 19955 Expr *OutE = 19956 ::buildDeclRefExpr(*this, OmpOutParm, ReductionType, D->getLocation()); 19957 DRD->setCombinerData(InE, OutE); 19958 } 19959 19960 void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) { 19961 auto *DRD = cast<OMPDeclareReductionDecl>(D); 19962 DiscardCleanupsInEvaluationContext(); 19963 PopExpressionEvaluationContext(); 19964 19965 PopDeclContext(); 19966 PopFunctionScopeInfo(); 19967 19968 if (Combiner != nullptr) 19969 DRD->setCombiner(Combiner); 19970 else 19971 DRD->setInvalidDecl(); 19972 } 19973 19974 VarDecl *Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) { 19975 auto *DRD = cast<OMPDeclareReductionDecl>(D); 19976 19977 // Enter new function scope. 19978 PushFunctionScope(); 19979 setFunctionHasBranchProtectedScope(); 19980 19981 if (S != nullptr) 19982 PushDeclContext(S, DRD); 19983 else 19984 CurContext = DRD; 19985 19986 PushExpressionEvaluationContext( 19987 ExpressionEvaluationContext::PotentiallyEvaluated); 19988 19989 QualType ReductionType = DRD->getType(); 19990 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will 19991 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv' 19992 // uses semantics of argument handles by value, but it should be passed by 19993 // reference. C lang does not support references, so pass all parameters as 19994 // pointers. 19995 // Create 'T omp_priv;' variable. 19996 VarDecl *OmpPrivParm = 19997 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv"); 19998 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will 19999 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig' 20000 // uses semantics of argument handles by value, but it should be passed by 20001 // reference. C lang does not support references, so pass all parameters as 20002 // pointers. 20003 // Create 'T omp_orig;' variable. 20004 VarDecl *OmpOrigParm = 20005 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig"); 20006 if (S != nullptr) { 20007 PushOnScopeChains(OmpPrivParm, S); 20008 PushOnScopeChains(OmpOrigParm, S); 20009 } else { 20010 DRD->addDecl(OmpPrivParm); 20011 DRD->addDecl(OmpOrigParm); 20012 } 20013 Expr *OrigE = 20014 ::buildDeclRefExpr(*this, OmpOrigParm, ReductionType, D->getLocation()); 20015 Expr *PrivE = 20016 ::buildDeclRefExpr(*this, OmpPrivParm, ReductionType, D->getLocation()); 20017 DRD->setInitializerData(OrigE, PrivE); 20018 return OmpPrivParm; 20019 } 20020 20021 void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, Expr *Initializer, 20022 VarDecl *OmpPrivParm) { 20023 auto *DRD = cast<OMPDeclareReductionDecl>(D); 20024 DiscardCleanupsInEvaluationContext(); 20025 PopExpressionEvaluationContext(); 20026 20027 PopDeclContext(); 20028 PopFunctionScopeInfo(); 20029 20030 if (Initializer != nullptr) { 20031 DRD->setInitializer(Initializer, OMPDeclareReductionDecl::CallInit); 20032 } else if (OmpPrivParm->hasInit()) { 20033 DRD->setInitializer(OmpPrivParm->getInit(), 20034 OmpPrivParm->isDirectInit() 20035 ? OMPDeclareReductionDecl::DirectInit 20036 : OMPDeclareReductionDecl::CopyInit); 20037 } else { 20038 DRD->setInvalidDecl(); 20039 } 20040 } 20041 20042 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd( 20043 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) { 20044 for (Decl *D : DeclReductions.get()) { 20045 if (IsValid) { 20046 if (S) 20047 PushOnScopeChains(cast<OMPDeclareReductionDecl>(D), S, 20048 /*AddToContext=*/false); 20049 } else { 20050 D->setInvalidDecl(); 20051 } 20052 } 20053 return DeclReductions; 20054 } 20055 20056 TypeResult Sema::ActOnOpenMPDeclareMapperVarDecl(Scope *S, Declarator &D) { 20057 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); 20058 QualType T = TInfo->getType(); 20059 if (D.isInvalidType()) 20060 return true; 20061 20062 if (getLangOpts().CPlusPlus) { 20063 // Check that there are no default arguments (C++ only). 20064 CheckExtraCXXDefaultArguments(D); 20065 } 20066 20067 return CreateParsedType(T, TInfo); 20068 } 20069 20070 QualType Sema::ActOnOpenMPDeclareMapperType(SourceLocation TyLoc, 20071 TypeResult ParsedType) { 20072 assert(ParsedType.isUsable() && "Expect usable parsed mapper type"); 20073 20074 QualType MapperType = GetTypeFromParser(ParsedType.get()); 20075 assert(!MapperType.isNull() && "Expect valid mapper type"); 20076 20077 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions 20078 // The type must be of struct, union or class type in C and C++ 20079 if (!MapperType->isStructureOrClassType() && !MapperType->isUnionType()) { 20080 Diag(TyLoc, diag::err_omp_mapper_wrong_type); 20081 return QualType(); 20082 } 20083 return MapperType; 20084 } 20085 20086 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareMapperDirective( 20087 Scope *S, DeclContext *DC, DeclarationName Name, QualType MapperType, 20088 SourceLocation StartLoc, DeclarationName VN, AccessSpecifier AS, 20089 Expr *MapperVarRef, ArrayRef<OMPClause *> Clauses, Decl *PrevDeclInScope) { 20090 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPMapperName, 20091 forRedeclarationInCurContext()); 20092 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions 20093 // A mapper-identifier may not be redeclared in the current scope for the 20094 // same type or for a type that is compatible according to the base language 20095 // rules. 20096 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes; 20097 OMPDeclareMapperDecl *PrevDMD = nullptr; 20098 bool InCompoundScope = true; 20099 if (S != nullptr) { 20100 // Find previous declaration with the same name not referenced in other 20101 // declarations. 20102 FunctionScopeInfo *ParentFn = getEnclosingFunction(); 20103 InCompoundScope = 20104 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty(); 20105 LookupName(Lookup, S); 20106 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false, 20107 /*AllowInlineNamespace=*/false); 20108 llvm::DenseMap<OMPDeclareMapperDecl *, bool> UsedAsPrevious; 20109 LookupResult::Filter Filter = Lookup.makeFilter(); 20110 while (Filter.hasNext()) { 20111 auto *PrevDecl = cast<OMPDeclareMapperDecl>(Filter.next()); 20112 if (InCompoundScope) { 20113 auto I = UsedAsPrevious.find(PrevDecl); 20114 if (I == UsedAsPrevious.end()) 20115 UsedAsPrevious[PrevDecl] = false; 20116 if (OMPDeclareMapperDecl *D = PrevDecl->getPrevDeclInScope()) 20117 UsedAsPrevious[D] = true; 20118 } 20119 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] = 20120 PrevDecl->getLocation(); 20121 } 20122 Filter.done(); 20123 if (InCompoundScope) { 20124 for (const auto &PrevData : UsedAsPrevious) { 20125 if (!PrevData.second) { 20126 PrevDMD = PrevData.first; 20127 break; 20128 } 20129 } 20130 } 20131 } else if (PrevDeclInScope) { 20132 auto *PrevDMDInScope = PrevDMD = 20133 cast<OMPDeclareMapperDecl>(PrevDeclInScope); 20134 do { 20135 PreviousRedeclTypes[PrevDMDInScope->getType().getCanonicalType()] = 20136 PrevDMDInScope->getLocation(); 20137 PrevDMDInScope = PrevDMDInScope->getPrevDeclInScope(); 20138 } while (PrevDMDInScope != nullptr); 20139 } 20140 const auto I = PreviousRedeclTypes.find(MapperType.getCanonicalType()); 20141 bool Invalid = false; 20142 if (I != PreviousRedeclTypes.end()) { 20143 Diag(StartLoc, diag::err_omp_declare_mapper_redefinition) 20144 << MapperType << Name; 20145 Diag(I->second, diag::note_previous_definition); 20146 Invalid = true; 20147 } 20148 // Build expressions for implicit maps of data members with 'default' 20149 // mappers. 20150 SmallVector<OMPClause *, 4> ClausesWithImplicit(Clauses.begin(), 20151 Clauses.end()); 20152 if (LangOpts.OpenMP >= 50) 20153 processImplicitMapsWithDefaultMappers(*this, DSAStack, ClausesWithImplicit); 20154 auto *DMD = 20155 OMPDeclareMapperDecl::Create(Context, DC, StartLoc, Name, MapperType, VN, 20156 ClausesWithImplicit, PrevDMD); 20157 if (S) 20158 PushOnScopeChains(DMD, S); 20159 else 20160 DC->addDecl(DMD); 20161 DMD->setAccess(AS); 20162 if (Invalid) 20163 DMD->setInvalidDecl(); 20164 20165 auto *VD = cast<DeclRefExpr>(MapperVarRef)->getDecl(); 20166 VD->setDeclContext(DMD); 20167 VD->setLexicalDeclContext(DMD); 20168 DMD->addDecl(VD); 20169 DMD->setMapperVarRef(MapperVarRef); 20170 20171 return DeclGroupPtrTy::make(DeclGroupRef(DMD)); 20172 } 20173 20174 ExprResult 20175 Sema::ActOnOpenMPDeclareMapperDirectiveVarDecl(Scope *S, QualType MapperType, 20176 SourceLocation StartLoc, 20177 DeclarationName VN) { 20178 TypeSourceInfo *TInfo = 20179 Context.getTrivialTypeSourceInfo(MapperType, StartLoc); 20180 auto *VD = VarDecl::Create(Context, Context.getTranslationUnitDecl(), 20181 StartLoc, StartLoc, VN.getAsIdentifierInfo(), 20182 MapperType, TInfo, SC_None); 20183 if (S) 20184 PushOnScopeChains(VD, S, /*AddToContext=*/false); 20185 Expr *E = buildDeclRefExpr(*this, VD, MapperType, StartLoc); 20186 DSAStack->addDeclareMapperVarRef(E); 20187 return E; 20188 } 20189 20190 bool Sema::isOpenMPDeclareMapperVarDeclAllowed(const VarDecl *VD) const { 20191 assert(LangOpts.OpenMP && "Expected OpenMP mode."); 20192 const Expr *Ref = DSAStack->getDeclareMapperVarRef(); 20193 if (const auto *DRE = cast_or_null<DeclRefExpr>(Ref)) { 20194 if (VD->getCanonicalDecl() == DRE->getDecl()->getCanonicalDecl()) 20195 return true; 20196 if (VD->isUsableInConstantExpressions(Context)) 20197 return true; 20198 return false; 20199 } 20200 return true; 20201 } 20202 20203 const ValueDecl *Sema::getOpenMPDeclareMapperVarName() const { 20204 assert(LangOpts.OpenMP && "Expected OpenMP mode."); 20205 return cast<DeclRefExpr>(DSAStack->getDeclareMapperVarRef())->getDecl(); 20206 } 20207 20208 OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams, 20209 SourceLocation StartLoc, 20210 SourceLocation LParenLoc, 20211 SourceLocation EndLoc) { 20212 Expr *ValExpr = NumTeams; 20213 Stmt *HelperValStmt = nullptr; 20214 20215 // OpenMP [teams Constrcut, Restrictions] 20216 // The num_teams expression must evaluate to a positive integer value. 20217 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams, 20218 /*StrictlyPositive=*/true)) 20219 return nullptr; 20220 20221 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 20222 OpenMPDirectiveKind CaptureRegion = 20223 getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams, LangOpts.OpenMP); 20224 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 20225 ValExpr = MakeFullExpr(ValExpr).get(); 20226 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 20227 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 20228 HelperValStmt = buildPreInits(Context, Captures); 20229 } 20230 20231 return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion, 20232 StartLoc, LParenLoc, EndLoc); 20233 } 20234 20235 OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit, 20236 SourceLocation StartLoc, 20237 SourceLocation LParenLoc, 20238 SourceLocation EndLoc) { 20239 Expr *ValExpr = ThreadLimit; 20240 Stmt *HelperValStmt = nullptr; 20241 20242 // OpenMP [teams Constrcut, Restrictions] 20243 // The thread_limit expression must evaluate to a positive integer value. 20244 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit, 20245 /*StrictlyPositive=*/true)) 20246 return nullptr; 20247 20248 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 20249 OpenMPDirectiveKind CaptureRegion = getOpenMPCaptureRegionForClause( 20250 DKind, OMPC_thread_limit, LangOpts.OpenMP); 20251 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 20252 ValExpr = MakeFullExpr(ValExpr).get(); 20253 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 20254 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 20255 HelperValStmt = buildPreInits(Context, Captures); 20256 } 20257 20258 return new (Context) OMPThreadLimitClause( 20259 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc); 20260 } 20261 20262 OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority, 20263 SourceLocation StartLoc, 20264 SourceLocation LParenLoc, 20265 SourceLocation EndLoc) { 20266 Expr *ValExpr = Priority; 20267 Stmt *HelperValStmt = nullptr; 20268 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 20269 20270 // OpenMP [2.9.1, task Constrcut] 20271 // The priority-value is a non-negative numerical scalar expression. 20272 if (!isNonNegativeIntegerValue( 20273 ValExpr, *this, OMPC_priority, 20274 /*StrictlyPositive=*/false, /*BuildCapture=*/true, 20275 DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt)) 20276 return nullptr; 20277 20278 return new (Context) OMPPriorityClause(ValExpr, HelperValStmt, CaptureRegion, 20279 StartLoc, LParenLoc, EndLoc); 20280 } 20281 20282 OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize, 20283 SourceLocation StartLoc, 20284 SourceLocation LParenLoc, 20285 SourceLocation EndLoc) { 20286 Expr *ValExpr = Grainsize; 20287 Stmt *HelperValStmt = nullptr; 20288 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 20289 20290 // OpenMP [2.9.2, taskloop Constrcut] 20291 // The parameter of the grainsize clause must be a positive integer 20292 // expression. 20293 if (!isNonNegativeIntegerValue( 20294 ValExpr, *this, OMPC_grainsize, 20295 /*StrictlyPositive=*/true, /*BuildCapture=*/true, 20296 DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt)) 20297 return nullptr; 20298 20299 return new (Context) OMPGrainsizeClause(ValExpr, HelperValStmt, CaptureRegion, 20300 StartLoc, LParenLoc, EndLoc); 20301 } 20302 20303 OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks, 20304 SourceLocation StartLoc, 20305 SourceLocation LParenLoc, 20306 SourceLocation EndLoc) { 20307 Expr *ValExpr = NumTasks; 20308 Stmt *HelperValStmt = nullptr; 20309 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 20310 20311 // OpenMP [2.9.2, taskloop Constrcut] 20312 // The parameter of the num_tasks clause must be a positive integer 20313 // expression. 20314 if (!isNonNegativeIntegerValue( 20315 ValExpr, *this, OMPC_num_tasks, 20316 /*StrictlyPositive=*/true, /*BuildCapture=*/true, 20317 DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt)) 20318 return nullptr; 20319 20320 return new (Context) OMPNumTasksClause(ValExpr, HelperValStmt, CaptureRegion, 20321 StartLoc, LParenLoc, EndLoc); 20322 } 20323 20324 OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc, 20325 SourceLocation LParenLoc, 20326 SourceLocation EndLoc) { 20327 // OpenMP [2.13.2, critical construct, Description] 20328 // ... where hint-expression is an integer constant expression that evaluates 20329 // to a valid lock hint. 20330 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint); 20331 if (HintExpr.isInvalid()) 20332 return nullptr; 20333 return new (Context) 20334 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc); 20335 } 20336 20337 /// Tries to find omp_event_handle_t type. 20338 static bool findOMPEventHandleT(Sema &S, SourceLocation Loc, 20339 DSAStackTy *Stack) { 20340 QualType OMPEventHandleT = Stack->getOMPEventHandleT(); 20341 if (!OMPEventHandleT.isNull()) 20342 return true; 20343 IdentifierInfo *II = &S.PP.getIdentifierTable().get("omp_event_handle_t"); 20344 ParsedType PT = S.getTypeName(*II, Loc, S.getCurScope()); 20345 if (!PT.getAsOpaquePtr() || PT.get().isNull()) { 20346 S.Diag(Loc, diag::err_omp_implied_type_not_found) << "omp_event_handle_t"; 20347 return false; 20348 } 20349 Stack->setOMPEventHandleT(PT.get()); 20350 return true; 20351 } 20352 20353 OMPClause *Sema::ActOnOpenMPDetachClause(Expr *Evt, SourceLocation StartLoc, 20354 SourceLocation LParenLoc, 20355 SourceLocation EndLoc) { 20356 if (!Evt->isValueDependent() && !Evt->isTypeDependent() && 20357 !Evt->isInstantiationDependent() && 20358 !Evt->containsUnexpandedParameterPack()) { 20359 if (!findOMPEventHandleT(*this, Evt->getExprLoc(), DSAStack)) 20360 return nullptr; 20361 // OpenMP 5.0, 2.10.1 task Construct. 20362 // event-handle is a variable of the omp_event_handle_t type. 20363 auto *Ref = dyn_cast<DeclRefExpr>(Evt->IgnoreParenImpCasts()); 20364 if (!Ref) { 20365 Diag(Evt->getExprLoc(), diag::err_omp_var_expected) 20366 << "omp_event_handle_t" << 0 << Evt->getSourceRange(); 20367 return nullptr; 20368 } 20369 auto *VD = dyn_cast_or_null<VarDecl>(Ref->getDecl()); 20370 if (!VD) { 20371 Diag(Evt->getExprLoc(), diag::err_omp_var_expected) 20372 << "omp_event_handle_t" << 0 << Evt->getSourceRange(); 20373 return nullptr; 20374 } 20375 if (!Context.hasSameUnqualifiedType(DSAStack->getOMPEventHandleT(), 20376 VD->getType()) || 20377 VD->getType().isConstant(Context)) { 20378 Diag(Evt->getExprLoc(), diag::err_omp_var_expected) 20379 << "omp_event_handle_t" << 1 << VD->getType() 20380 << Evt->getSourceRange(); 20381 return nullptr; 20382 } 20383 // OpenMP 5.0, 2.10.1 task Construct 20384 // [detach clause]... The event-handle will be considered as if it was 20385 // specified on a firstprivate clause. 20386 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, /*FromParent=*/false); 20387 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate && 20388 DVar.RefExpr) { 20389 Diag(Evt->getExprLoc(), diag::err_omp_wrong_dsa) 20390 << getOpenMPClauseName(DVar.CKind) 20391 << getOpenMPClauseName(OMPC_firstprivate); 20392 reportOriginalDsa(*this, DSAStack, VD, DVar); 20393 return nullptr; 20394 } 20395 } 20396 20397 return new (Context) OMPDetachClause(Evt, StartLoc, LParenLoc, EndLoc); 20398 } 20399 20400 OMPClause *Sema::ActOnOpenMPDistScheduleClause( 20401 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, 20402 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc, 20403 SourceLocation EndLoc) { 20404 if (Kind == OMPC_DIST_SCHEDULE_unknown) { 20405 std::string Values; 20406 Values += "'"; 20407 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0); 20408 Values += "'"; 20409 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 20410 << Values << getOpenMPClauseName(OMPC_dist_schedule); 20411 return nullptr; 20412 } 20413 Expr *ValExpr = ChunkSize; 20414 Stmt *HelperValStmt = nullptr; 20415 if (ChunkSize) { 20416 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && 20417 !ChunkSize->isInstantiationDependent() && 20418 !ChunkSize->containsUnexpandedParameterPack()) { 20419 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc(); 20420 ExprResult Val = 20421 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); 20422 if (Val.isInvalid()) 20423 return nullptr; 20424 20425 ValExpr = Val.get(); 20426 20427 // OpenMP [2.7.1, Restrictions] 20428 // chunk_size must be a loop invariant integer expression with a positive 20429 // value. 20430 if (Optional<llvm::APSInt> Result = 20431 ValExpr->getIntegerConstantExpr(Context)) { 20432 if (Result->isSigned() && !Result->isStrictlyPositive()) { 20433 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) 20434 << "dist_schedule" << ChunkSize->getSourceRange(); 20435 return nullptr; 20436 } 20437 } else if (getOpenMPCaptureRegionForClause( 20438 DSAStack->getCurrentDirective(), OMPC_dist_schedule, 20439 LangOpts.OpenMP) != OMPD_unknown && 20440 !CurContext->isDependentContext()) { 20441 ValExpr = MakeFullExpr(ValExpr).get(); 20442 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 20443 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 20444 HelperValStmt = buildPreInits(Context, Captures); 20445 } 20446 } 20447 } 20448 20449 return new (Context) 20450 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, 20451 Kind, ValExpr, HelperValStmt); 20452 } 20453 20454 OMPClause *Sema::ActOnOpenMPDefaultmapClause( 20455 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind, 20456 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc, 20457 SourceLocation KindLoc, SourceLocation EndLoc) { 20458 if (getLangOpts().OpenMP < 50) { 20459 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || 20460 Kind != OMPC_DEFAULTMAP_scalar) { 20461 std::string Value; 20462 SourceLocation Loc; 20463 Value += "'"; 20464 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) { 20465 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap, 20466 OMPC_DEFAULTMAP_MODIFIER_tofrom); 20467 Loc = MLoc; 20468 } else { 20469 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap, 20470 OMPC_DEFAULTMAP_scalar); 20471 Loc = KindLoc; 20472 } 20473 Value += "'"; 20474 Diag(Loc, diag::err_omp_unexpected_clause_value) 20475 << Value << getOpenMPClauseName(OMPC_defaultmap); 20476 return nullptr; 20477 } 20478 } else { 20479 bool isDefaultmapModifier = (M != OMPC_DEFAULTMAP_MODIFIER_unknown); 20480 bool isDefaultmapKind = (Kind != OMPC_DEFAULTMAP_unknown) || 20481 (LangOpts.OpenMP >= 50 && KindLoc.isInvalid()); 20482 if (!isDefaultmapKind || !isDefaultmapModifier) { 20483 StringRef KindValue = "'scalar', 'aggregate', 'pointer'"; 20484 if (LangOpts.OpenMP == 50) { 20485 StringRef ModifierValue = "'alloc', 'from', 'to', 'tofrom', " 20486 "'firstprivate', 'none', 'default'"; 20487 if (!isDefaultmapKind && isDefaultmapModifier) { 20488 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 20489 << KindValue << getOpenMPClauseName(OMPC_defaultmap); 20490 } else if (isDefaultmapKind && !isDefaultmapModifier) { 20491 Diag(MLoc, diag::err_omp_unexpected_clause_value) 20492 << ModifierValue << getOpenMPClauseName(OMPC_defaultmap); 20493 } else { 20494 Diag(MLoc, diag::err_omp_unexpected_clause_value) 20495 << ModifierValue << getOpenMPClauseName(OMPC_defaultmap); 20496 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 20497 << KindValue << getOpenMPClauseName(OMPC_defaultmap); 20498 } 20499 } else { 20500 StringRef ModifierValue = 20501 "'alloc', 'from', 'to', 'tofrom', " 20502 "'firstprivate', 'none', 'default', 'present'"; 20503 if (!isDefaultmapKind && isDefaultmapModifier) { 20504 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 20505 << KindValue << getOpenMPClauseName(OMPC_defaultmap); 20506 } else if (isDefaultmapKind && !isDefaultmapModifier) { 20507 Diag(MLoc, diag::err_omp_unexpected_clause_value) 20508 << ModifierValue << getOpenMPClauseName(OMPC_defaultmap); 20509 } else { 20510 Diag(MLoc, diag::err_omp_unexpected_clause_value) 20511 << ModifierValue << getOpenMPClauseName(OMPC_defaultmap); 20512 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 20513 << KindValue << getOpenMPClauseName(OMPC_defaultmap); 20514 } 20515 } 20516 return nullptr; 20517 } 20518 20519 // OpenMP [5.0, 2.12.5, Restrictions, p. 174] 20520 // At most one defaultmap clause for each category can appear on the 20521 // directive. 20522 if (DSAStack->checkDefaultmapCategory(Kind)) { 20523 Diag(StartLoc, diag::err_omp_one_defaultmap_each_category); 20524 return nullptr; 20525 } 20526 } 20527 if (Kind == OMPC_DEFAULTMAP_unknown) { 20528 // Variable category is not specified - mark all categories. 20529 DSAStack->setDefaultDMAAttr(M, OMPC_DEFAULTMAP_aggregate, StartLoc); 20530 DSAStack->setDefaultDMAAttr(M, OMPC_DEFAULTMAP_scalar, StartLoc); 20531 DSAStack->setDefaultDMAAttr(M, OMPC_DEFAULTMAP_pointer, StartLoc); 20532 } else { 20533 DSAStack->setDefaultDMAAttr(M, Kind, StartLoc); 20534 } 20535 20536 return new (Context) 20537 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M); 20538 } 20539 20540 bool Sema::ActOnStartOpenMPDeclareTargetContext( 20541 DeclareTargetContextInfo &DTCI) { 20542 DeclContext *CurLexicalContext = getCurLexicalContext(); 20543 if (!CurLexicalContext->isFileContext() && 20544 !CurLexicalContext->isExternCContext() && 20545 !CurLexicalContext->isExternCXXContext() && 20546 !isa<CXXRecordDecl>(CurLexicalContext) && 20547 !isa<ClassTemplateDecl>(CurLexicalContext) && 20548 !isa<ClassTemplatePartialSpecializationDecl>(CurLexicalContext) && 20549 !isa<ClassTemplateSpecializationDecl>(CurLexicalContext)) { 20550 Diag(DTCI.Loc, diag::err_omp_region_not_file_context); 20551 return false; 20552 } 20553 DeclareTargetNesting.push_back(DTCI); 20554 return true; 20555 } 20556 20557 const Sema::DeclareTargetContextInfo 20558 Sema::ActOnOpenMPEndDeclareTargetDirective() { 20559 assert(!DeclareTargetNesting.empty() && 20560 "check isInOpenMPDeclareTargetContext() first!"); 20561 return DeclareTargetNesting.pop_back_val(); 20562 } 20563 20564 void Sema::ActOnFinishedOpenMPDeclareTargetContext( 20565 DeclareTargetContextInfo &DTCI) { 20566 for (auto &It : DTCI.ExplicitlyMapped) 20567 ActOnOpenMPDeclareTargetName(It.first, It.second.Loc, It.second.MT, 20568 DTCI.DT); 20569 } 20570 20571 NamedDecl *Sema::lookupOpenMPDeclareTargetName(Scope *CurScope, 20572 CXXScopeSpec &ScopeSpec, 20573 const DeclarationNameInfo &Id) { 20574 LookupResult Lookup(*this, Id, LookupOrdinaryName); 20575 LookupParsedName(Lookup, CurScope, &ScopeSpec, true); 20576 20577 if (Lookup.isAmbiguous()) 20578 return nullptr; 20579 Lookup.suppressDiagnostics(); 20580 20581 if (!Lookup.isSingleResult()) { 20582 VarOrFuncDeclFilterCCC CCC(*this); 20583 if (TypoCorrection Corrected = 20584 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, CCC, 20585 CTK_ErrorRecovery)) { 20586 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest) 20587 << Id.getName()); 20588 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl()); 20589 return nullptr; 20590 } 20591 20592 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName(); 20593 return nullptr; 20594 } 20595 20596 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>(); 20597 if (!isa<VarDecl>(ND) && !isa<FunctionDecl>(ND) && 20598 !isa<FunctionTemplateDecl>(ND)) { 20599 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName(); 20600 return nullptr; 20601 } 20602 return ND; 20603 } 20604 20605 void Sema::ActOnOpenMPDeclareTargetName( 20606 NamedDecl *ND, SourceLocation Loc, OMPDeclareTargetDeclAttr::MapTypeTy MT, 20607 OMPDeclareTargetDeclAttr::DevTypeTy DT) { 20608 assert((isa<VarDecl>(ND) || isa<FunctionDecl>(ND) || 20609 isa<FunctionTemplateDecl>(ND)) && 20610 "Expected variable, function or function template."); 20611 20612 // Diagnose marking after use as it may lead to incorrect diagnosis and 20613 // codegen. 20614 if (LangOpts.OpenMP >= 50 && 20615 (ND->isUsed(/*CheckUsedAttr=*/false) || ND->isReferenced())) 20616 Diag(Loc, diag::warn_omp_declare_target_after_first_use); 20617 20618 // Explicit declare target lists have precedence. 20619 const unsigned Level = -1; 20620 20621 auto *VD = cast<ValueDecl>(ND); 20622 llvm::Optional<OMPDeclareTargetDeclAttr *> ActiveAttr = 20623 OMPDeclareTargetDeclAttr::getActiveAttr(VD); 20624 if (ActiveAttr.hasValue() && ActiveAttr.getValue()->getDevType() != DT && 20625 ActiveAttr.getValue()->getLevel() == Level) { 20626 Diag(Loc, diag::err_omp_device_type_mismatch) 20627 << OMPDeclareTargetDeclAttr::ConvertDevTypeTyToStr(DT) 20628 << OMPDeclareTargetDeclAttr::ConvertDevTypeTyToStr( 20629 ActiveAttr.getValue()->getDevType()); 20630 return; 20631 } 20632 if (ActiveAttr.hasValue() && ActiveAttr.getValue()->getMapType() != MT && 20633 ActiveAttr.getValue()->getLevel() == Level) { 20634 Diag(Loc, diag::err_omp_declare_target_to_and_link) << ND; 20635 return; 20636 } 20637 20638 if (ActiveAttr.hasValue() && ActiveAttr.getValue()->getLevel() == Level) 20639 return; 20640 20641 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT, DT, Level, 20642 SourceRange(Loc, Loc)); 20643 ND->addAttr(A); 20644 if (ASTMutationListener *ML = Context.getASTMutationListener()) 20645 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A); 20646 checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Loc); 20647 } 20648 20649 static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR, 20650 Sema &SemaRef, Decl *D) { 20651 if (!D || !isa<VarDecl>(D)) 20652 return; 20653 auto *VD = cast<VarDecl>(D); 20654 Optional<OMPDeclareTargetDeclAttr::MapTypeTy> MapTy = 20655 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); 20656 if (SemaRef.LangOpts.OpenMP >= 50 && 20657 (SemaRef.getCurLambda(/*IgnoreNonLambdaCapturingScope=*/true) || 20658 SemaRef.getCurBlock() || SemaRef.getCurCapturedRegion()) && 20659 VD->hasGlobalStorage()) { 20660 if (!MapTy || *MapTy != OMPDeclareTargetDeclAttr::MT_To) { 20661 // OpenMP 5.0, 2.12.7 declare target Directive, Restrictions 20662 // If a lambda declaration and definition appears between a 20663 // declare target directive and the matching end declare target 20664 // directive, all variables that are captured by the lambda 20665 // expression must also appear in a to clause. 20666 SemaRef.Diag(VD->getLocation(), 20667 diag::err_omp_lambda_capture_in_declare_target_not_to); 20668 SemaRef.Diag(SL, diag::note_var_explicitly_captured_here) 20669 << VD << 0 << SR; 20670 return; 20671 } 20672 } 20673 if (MapTy.hasValue()) 20674 return; 20675 SemaRef.Diag(VD->getLocation(), diag::warn_omp_not_in_target_context); 20676 SemaRef.Diag(SL, diag::note_used_here) << SR; 20677 } 20678 20679 static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR, 20680 Sema &SemaRef, DSAStackTy *Stack, 20681 ValueDecl *VD) { 20682 return OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD) || 20683 checkTypeMappable(SL, SR, SemaRef, Stack, VD->getType(), 20684 /*FullCheck=*/false); 20685 } 20686 20687 void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D, 20688 SourceLocation IdLoc) { 20689 if (!D || D->isInvalidDecl()) 20690 return; 20691 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange(); 20692 SourceLocation SL = E ? E->getBeginLoc() : D->getLocation(); 20693 if (auto *VD = dyn_cast<VarDecl>(D)) { 20694 // Only global variables can be marked as declare target. 20695 if (!VD->isFileVarDecl() && !VD->isStaticLocal() && 20696 !VD->isStaticDataMember()) 20697 return; 20698 // 2.10.6: threadprivate variable cannot appear in a declare target 20699 // directive. 20700 if (DSAStack->isThreadPrivate(VD)) { 20701 Diag(SL, diag::err_omp_threadprivate_in_target); 20702 reportOriginalDsa(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false)); 20703 return; 20704 } 20705 } 20706 if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(D)) 20707 D = FTD->getTemplatedDecl(); 20708 if (auto *FD = dyn_cast<FunctionDecl>(D)) { 20709 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 20710 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(FD); 20711 if (IdLoc.isValid() && Res && *Res == OMPDeclareTargetDeclAttr::MT_Link) { 20712 Diag(IdLoc, diag::err_omp_function_in_link_clause); 20713 Diag(FD->getLocation(), diag::note_defined_here) << FD; 20714 return; 20715 } 20716 } 20717 if (auto *VD = dyn_cast<ValueDecl>(D)) { 20718 // Problem if any with var declared with incomplete type will be reported 20719 // as normal, so no need to check it here. 20720 if ((E || !VD->getType()->isIncompleteType()) && 20721 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) 20722 return; 20723 if (!E && isInOpenMPDeclareTargetContext()) { 20724 // Checking declaration inside declare target region. 20725 if (isa<VarDecl>(D) || isa<FunctionDecl>(D) || 20726 isa<FunctionTemplateDecl>(D)) { 20727 llvm::Optional<OMPDeclareTargetDeclAttr *> ActiveAttr = 20728 OMPDeclareTargetDeclAttr::getActiveAttr(VD); 20729 unsigned Level = DeclareTargetNesting.size(); 20730 if (ActiveAttr.hasValue() && ActiveAttr.getValue()->getLevel() >= Level) 20731 return; 20732 DeclareTargetContextInfo &DTCI = DeclareTargetNesting.back(); 20733 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit( 20734 Context, OMPDeclareTargetDeclAttr::MT_To, DTCI.DT, Level, 20735 SourceRange(DTCI.Loc, DTCI.Loc)); 20736 D->addAttr(A); 20737 if (ASTMutationListener *ML = Context.getASTMutationListener()) 20738 ML->DeclarationMarkedOpenMPDeclareTarget(D, A); 20739 } 20740 return; 20741 } 20742 } 20743 if (!E) 20744 return; 20745 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D); 20746 } 20747 20748 OMPClause *Sema::ActOnOpenMPToClause( 20749 ArrayRef<OpenMPMotionModifierKind> MotionModifiers, 20750 ArrayRef<SourceLocation> MotionModifiersLoc, 20751 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId, 20752 SourceLocation ColonLoc, ArrayRef<Expr *> VarList, 20753 const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) { 20754 OpenMPMotionModifierKind Modifiers[] = {OMPC_MOTION_MODIFIER_unknown, 20755 OMPC_MOTION_MODIFIER_unknown}; 20756 SourceLocation ModifiersLoc[NumberOfOMPMotionModifiers]; 20757 20758 // Process motion-modifiers, flag errors for duplicate modifiers. 20759 unsigned Count = 0; 20760 for (unsigned I = 0, E = MotionModifiers.size(); I < E; ++I) { 20761 if (MotionModifiers[I] != OMPC_MOTION_MODIFIER_unknown && 20762 llvm::is_contained(Modifiers, MotionModifiers[I])) { 20763 Diag(MotionModifiersLoc[I], diag::err_omp_duplicate_motion_modifier); 20764 continue; 20765 } 20766 assert(Count < NumberOfOMPMotionModifiers && 20767 "Modifiers exceed the allowed number of motion modifiers"); 20768 Modifiers[Count] = MotionModifiers[I]; 20769 ModifiersLoc[Count] = MotionModifiersLoc[I]; 20770 ++Count; 20771 } 20772 20773 MappableVarListInfo MVLI(VarList); 20774 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, Locs.StartLoc, 20775 MapperIdScopeSpec, MapperId, UnresolvedMappers); 20776 if (MVLI.ProcessedVarList.empty()) 20777 return nullptr; 20778 20779 return OMPToClause::Create( 20780 Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, 20781 MVLI.VarComponents, MVLI.UDMapperList, Modifiers, ModifiersLoc, 20782 MapperIdScopeSpec.getWithLocInContext(Context), MapperId); 20783 } 20784 20785 OMPClause *Sema::ActOnOpenMPFromClause( 20786 ArrayRef<OpenMPMotionModifierKind> MotionModifiers, 20787 ArrayRef<SourceLocation> MotionModifiersLoc, 20788 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId, 20789 SourceLocation ColonLoc, ArrayRef<Expr *> VarList, 20790 const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) { 20791 OpenMPMotionModifierKind Modifiers[] = {OMPC_MOTION_MODIFIER_unknown, 20792 OMPC_MOTION_MODIFIER_unknown}; 20793 SourceLocation ModifiersLoc[NumberOfOMPMotionModifiers]; 20794 20795 // Process motion-modifiers, flag errors for duplicate modifiers. 20796 unsigned Count = 0; 20797 for (unsigned I = 0, E = MotionModifiers.size(); I < E; ++I) { 20798 if (MotionModifiers[I] != OMPC_MOTION_MODIFIER_unknown && 20799 llvm::is_contained(Modifiers, MotionModifiers[I])) { 20800 Diag(MotionModifiersLoc[I], diag::err_omp_duplicate_motion_modifier); 20801 continue; 20802 } 20803 assert(Count < NumberOfOMPMotionModifiers && 20804 "Modifiers exceed the allowed number of motion modifiers"); 20805 Modifiers[Count] = MotionModifiers[I]; 20806 ModifiersLoc[Count] = MotionModifiersLoc[I]; 20807 ++Count; 20808 } 20809 20810 MappableVarListInfo MVLI(VarList); 20811 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, Locs.StartLoc, 20812 MapperIdScopeSpec, MapperId, UnresolvedMappers); 20813 if (MVLI.ProcessedVarList.empty()) 20814 return nullptr; 20815 20816 return OMPFromClause::Create( 20817 Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, 20818 MVLI.VarComponents, MVLI.UDMapperList, Modifiers, ModifiersLoc, 20819 MapperIdScopeSpec.getWithLocInContext(Context), MapperId); 20820 } 20821 20822 OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList, 20823 const OMPVarListLocTy &Locs) { 20824 MappableVarListInfo MVLI(VarList); 20825 SmallVector<Expr *, 8> PrivateCopies; 20826 SmallVector<Expr *, 8> Inits; 20827 20828 for (Expr *RefExpr : VarList) { 20829 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause."); 20830 SourceLocation ELoc; 20831 SourceRange ERange; 20832 Expr *SimpleRefExpr = RefExpr; 20833 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 20834 if (Res.second) { 20835 // It will be analyzed later. 20836 MVLI.ProcessedVarList.push_back(RefExpr); 20837 PrivateCopies.push_back(nullptr); 20838 Inits.push_back(nullptr); 20839 } 20840 ValueDecl *D = Res.first; 20841 if (!D) 20842 continue; 20843 20844 QualType Type = D->getType(); 20845 Type = Type.getNonReferenceType().getUnqualifiedType(); 20846 20847 auto *VD = dyn_cast<VarDecl>(D); 20848 20849 // Item should be a pointer or reference to pointer. 20850 if (!Type->isPointerType()) { 20851 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer) 20852 << 0 << RefExpr->getSourceRange(); 20853 continue; 20854 } 20855 20856 // Build the private variable and the expression that refers to it. 20857 auto VDPrivate = 20858 buildVarDecl(*this, ELoc, Type, D->getName(), 20859 D->hasAttrs() ? &D->getAttrs() : nullptr, 20860 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 20861 if (VDPrivate->isInvalidDecl()) 20862 continue; 20863 20864 CurContext->addDecl(VDPrivate); 20865 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 20866 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc); 20867 20868 // Add temporary variable to initialize the private copy of the pointer. 20869 VarDecl *VDInit = 20870 buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp"); 20871 DeclRefExpr *VDInitRefExpr = buildDeclRefExpr( 20872 *this, VDInit, RefExpr->getType(), RefExpr->getExprLoc()); 20873 AddInitializerToDecl(VDPrivate, 20874 DefaultLvalueConversion(VDInitRefExpr).get(), 20875 /*DirectInit=*/false); 20876 20877 // If required, build a capture to implement the privatization initialized 20878 // with the current list item value. 20879 DeclRefExpr *Ref = nullptr; 20880 if (!VD) 20881 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 20882 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref); 20883 PrivateCopies.push_back(VDPrivateRefExpr); 20884 Inits.push_back(VDInitRefExpr); 20885 20886 // We need to add a data sharing attribute for this variable to make sure it 20887 // is correctly captured. A variable that shows up in a use_device_ptr has 20888 // similar properties of a first private variable. 20889 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 20890 20891 // Create a mappable component for the list item. List items in this clause 20892 // only need a component. 20893 MVLI.VarBaseDeclarations.push_back(D); 20894 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 20895 MVLI.VarComponents.back().emplace_back(SimpleRefExpr, D, 20896 /*IsNonContiguous=*/false); 20897 } 20898 20899 if (MVLI.ProcessedVarList.empty()) 20900 return nullptr; 20901 20902 return OMPUseDevicePtrClause::Create( 20903 Context, Locs, MVLI.ProcessedVarList, PrivateCopies, Inits, 20904 MVLI.VarBaseDeclarations, MVLI.VarComponents); 20905 } 20906 20907 OMPClause *Sema::ActOnOpenMPUseDeviceAddrClause(ArrayRef<Expr *> VarList, 20908 const OMPVarListLocTy &Locs) { 20909 MappableVarListInfo MVLI(VarList); 20910 20911 for (Expr *RefExpr : VarList) { 20912 assert(RefExpr && "NULL expr in OpenMP use_device_addr clause."); 20913 SourceLocation ELoc; 20914 SourceRange ERange; 20915 Expr *SimpleRefExpr = RefExpr; 20916 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, 20917 /*AllowArraySection=*/true); 20918 if (Res.second) { 20919 // It will be analyzed later. 20920 MVLI.ProcessedVarList.push_back(RefExpr); 20921 } 20922 ValueDecl *D = Res.first; 20923 if (!D) 20924 continue; 20925 auto *VD = dyn_cast<VarDecl>(D); 20926 20927 // If required, build a capture to implement the privatization initialized 20928 // with the current list item value. 20929 DeclRefExpr *Ref = nullptr; 20930 if (!VD) 20931 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 20932 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref); 20933 20934 // We need to add a data sharing attribute for this variable to make sure it 20935 // is correctly captured. A variable that shows up in a use_device_addr has 20936 // similar properties of a first private variable. 20937 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 20938 20939 // Create a mappable component for the list item. List items in this clause 20940 // only need a component. 20941 MVLI.VarBaseDeclarations.push_back(D); 20942 MVLI.VarComponents.emplace_back(); 20943 Expr *Component = SimpleRefExpr; 20944 if (VD && (isa<OMPArraySectionExpr>(RefExpr->IgnoreParenImpCasts()) || 20945 isa<ArraySubscriptExpr>(RefExpr->IgnoreParenImpCasts()))) 20946 Component = DefaultFunctionArrayLvalueConversion(SimpleRefExpr).get(); 20947 MVLI.VarComponents.back().emplace_back(Component, D, 20948 /*IsNonContiguous=*/false); 20949 } 20950 20951 if (MVLI.ProcessedVarList.empty()) 20952 return nullptr; 20953 20954 return OMPUseDeviceAddrClause::Create(Context, Locs, MVLI.ProcessedVarList, 20955 MVLI.VarBaseDeclarations, 20956 MVLI.VarComponents); 20957 } 20958 20959 OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList, 20960 const OMPVarListLocTy &Locs) { 20961 MappableVarListInfo MVLI(VarList); 20962 for (Expr *RefExpr : VarList) { 20963 assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause."); 20964 SourceLocation ELoc; 20965 SourceRange ERange; 20966 Expr *SimpleRefExpr = RefExpr; 20967 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 20968 if (Res.second) { 20969 // It will be analyzed later. 20970 MVLI.ProcessedVarList.push_back(RefExpr); 20971 } 20972 ValueDecl *D = Res.first; 20973 if (!D) 20974 continue; 20975 20976 QualType Type = D->getType(); 20977 // item should be a pointer or array or reference to pointer or array 20978 if (!Type.getNonReferenceType()->isPointerType() && 20979 !Type.getNonReferenceType()->isArrayType()) { 20980 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr) 20981 << 0 << RefExpr->getSourceRange(); 20982 continue; 20983 } 20984 20985 // Check if the declaration in the clause does not show up in any data 20986 // sharing attribute. 20987 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 20988 if (isOpenMPPrivate(DVar.CKind)) { 20989 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 20990 << getOpenMPClauseName(DVar.CKind) 20991 << getOpenMPClauseName(OMPC_is_device_ptr) 20992 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 20993 reportOriginalDsa(*this, DSAStack, D, DVar); 20994 continue; 20995 } 20996 20997 const Expr *ConflictExpr; 20998 if (DSAStack->checkMappableExprComponentListsForDecl( 20999 D, /*CurrentRegionOnly=*/true, 21000 [&ConflictExpr]( 21001 OMPClauseMappableExprCommon::MappableExprComponentListRef R, 21002 OpenMPClauseKind) -> bool { 21003 ConflictExpr = R.front().getAssociatedExpression(); 21004 return true; 21005 })) { 21006 Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange(); 21007 Diag(ConflictExpr->getExprLoc(), diag::note_used_here) 21008 << ConflictExpr->getSourceRange(); 21009 continue; 21010 } 21011 21012 // Store the components in the stack so that they can be used to check 21013 // against other clauses later on. 21014 OMPClauseMappableExprCommon::MappableComponent MC( 21015 SimpleRefExpr, D, /*IsNonContiguous=*/false); 21016 DSAStack->addMappableExpressionComponents( 21017 D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr); 21018 21019 // Record the expression we've just processed. 21020 MVLI.ProcessedVarList.push_back(SimpleRefExpr); 21021 21022 // Create a mappable component for the list item. List items in this clause 21023 // only need a component. We use a null declaration to signal fields in 21024 // 'this'. 21025 assert((isa<DeclRefExpr>(SimpleRefExpr) || 21026 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) && 21027 "Unexpected device pointer expression!"); 21028 MVLI.VarBaseDeclarations.push_back( 21029 isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr); 21030 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 21031 MVLI.VarComponents.back().push_back(MC); 21032 } 21033 21034 if (MVLI.ProcessedVarList.empty()) 21035 return nullptr; 21036 21037 return OMPIsDevicePtrClause::Create(Context, Locs, MVLI.ProcessedVarList, 21038 MVLI.VarBaseDeclarations, 21039 MVLI.VarComponents); 21040 } 21041 21042 OMPClause *Sema::ActOnOpenMPAllocateClause( 21043 Expr *Allocator, ArrayRef<Expr *> VarList, SourceLocation StartLoc, 21044 SourceLocation ColonLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 21045 if (Allocator) { 21046 // OpenMP [2.11.4 allocate Clause, Description] 21047 // allocator is an expression of omp_allocator_handle_t type. 21048 if (!findOMPAllocatorHandleT(*this, Allocator->getExprLoc(), DSAStack)) 21049 return nullptr; 21050 21051 ExprResult AllocatorRes = DefaultLvalueConversion(Allocator); 21052 if (AllocatorRes.isInvalid()) 21053 return nullptr; 21054 AllocatorRes = PerformImplicitConversion(AllocatorRes.get(), 21055 DSAStack->getOMPAllocatorHandleT(), 21056 Sema::AA_Initializing, 21057 /*AllowExplicit=*/true); 21058 if (AllocatorRes.isInvalid()) 21059 return nullptr; 21060 Allocator = AllocatorRes.get(); 21061 } else { 21062 // OpenMP 5.0, 2.11.4 allocate Clause, Restrictions. 21063 // allocate clauses that appear on a target construct or on constructs in a 21064 // target region must specify an allocator expression unless a requires 21065 // directive with the dynamic_allocators clause is present in the same 21066 // compilation unit. 21067 if (LangOpts.OpenMPIsDevice && 21068 !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>()) 21069 targetDiag(StartLoc, diag::err_expected_allocator_expression); 21070 } 21071 // Analyze and build list of variables. 21072 SmallVector<Expr *, 8> Vars; 21073 for (Expr *RefExpr : VarList) { 21074 assert(RefExpr && "NULL expr in OpenMP private clause."); 21075 SourceLocation ELoc; 21076 SourceRange ERange; 21077 Expr *SimpleRefExpr = RefExpr; 21078 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 21079 if (Res.second) { 21080 // It will be analyzed later. 21081 Vars.push_back(RefExpr); 21082 } 21083 ValueDecl *D = Res.first; 21084 if (!D) 21085 continue; 21086 21087 auto *VD = dyn_cast<VarDecl>(D); 21088 DeclRefExpr *Ref = nullptr; 21089 if (!VD && !CurContext->isDependentContext()) 21090 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 21091 Vars.push_back((VD || CurContext->isDependentContext()) 21092 ? RefExpr->IgnoreParens() 21093 : Ref); 21094 } 21095 21096 if (Vars.empty()) 21097 return nullptr; 21098 21099 if (Allocator) 21100 DSAStack->addInnerAllocatorExpr(Allocator); 21101 return OMPAllocateClause::Create(Context, StartLoc, LParenLoc, Allocator, 21102 ColonLoc, EndLoc, Vars); 21103 } 21104 21105 OMPClause *Sema::ActOnOpenMPNontemporalClause(ArrayRef<Expr *> VarList, 21106 SourceLocation StartLoc, 21107 SourceLocation LParenLoc, 21108 SourceLocation EndLoc) { 21109 SmallVector<Expr *, 8> Vars; 21110 for (Expr *RefExpr : VarList) { 21111 assert(RefExpr && "NULL expr in OpenMP nontemporal clause."); 21112 SourceLocation ELoc; 21113 SourceRange ERange; 21114 Expr *SimpleRefExpr = RefExpr; 21115 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 21116 if (Res.second) 21117 // It will be analyzed later. 21118 Vars.push_back(RefExpr); 21119 ValueDecl *D = Res.first; 21120 if (!D) 21121 continue; 21122 21123 // OpenMP 5.0, 2.9.3.1 simd Construct, Restrictions. 21124 // A list-item cannot appear in more than one nontemporal clause. 21125 if (const Expr *PrevRef = 21126 DSAStack->addUniqueNontemporal(D, SimpleRefExpr)) { 21127 Diag(ELoc, diag::err_omp_used_in_clause_twice) 21128 << 0 << getOpenMPClauseName(OMPC_nontemporal) << ERange; 21129 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa) 21130 << getOpenMPClauseName(OMPC_nontemporal); 21131 continue; 21132 } 21133 21134 Vars.push_back(RefExpr); 21135 } 21136 21137 if (Vars.empty()) 21138 return nullptr; 21139 21140 return OMPNontemporalClause::Create(Context, StartLoc, LParenLoc, EndLoc, 21141 Vars); 21142 } 21143 21144 OMPClause *Sema::ActOnOpenMPInclusiveClause(ArrayRef<Expr *> VarList, 21145 SourceLocation StartLoc, 21146 SourceLocation LParenLoc, 21147 SourceLocation EndLoc) { 21148 SmallVector<Expr *, 8> Vars; 21149 for (Expr *RefExpr : VarList) { 21150 assert(RefExpr && "NULL expr in OpenMP nontemporal clause."); 21151 SourceLocation ELoc; 21152 SourceRange ERange; 21153 Expr *SimpleRefExpr = RefExpr; 21154 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, 21155 /*AllowArraySection=*/true); 21156 if (Res.second) 21157 // It will be analyzed later. 21158 Vars.push_back(RefExpr); 21159 ValueDecl *D = Res.first; 21160 if (!D) 21161 continue; 21162 21163 const DSAStackTy::DSAVarData DVar = 21164 DSAStack->getTopDSA(D, /*FromParent=*/true); 21165 // OpenMP 5.0, 2.9.6, scan Directive, Restrictions. 21166 // A list item that appears in the inclusive or exclusive clause must appear 21167 // in a reduction clause with the inscan modifier on the enclosing 21168 // worksharing-loop, worksharing-loop SIMD, or simd construct. 21169 if (DVar.CKind != OMPC_reduction || 21170 DVar.Modifier != OMPC_REDUCTION_inscan) 21171 Diag(ELoc, diag::err_omp_inclusive_exclusive_not_reduction) 21172 << RefExpr->getSourceRange(); 21173 21174 if (DSAStack->getParentDirective() != OMPD_unknown) 21175 DSAStack->markDeclAsUsedInScanDirective(D); 21176 Vars.push_back(RefExpr); 21177 } 21178 21179 if (Vars.empty()) 21180 return nullptr; 21181 21182 return OMPInclusiveClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 21183 } 21184 21185 OMPClause *Sema::ActOnOpenMPExclusiveClause(ArrayRef<Expr *> VarList, 21186 SourceLocation StartLoc, 21187 SourceLocation LParenLoc, 21188 SourceLocation EndLoc) { 21189 SmallVector<Expr *, 8> Vars; 21190 for (Expr *RefExpr : VarList) { 21191 assert(RefExpr && "NULL expr in OpenMP nontemporal clause."); 21192 SourceLocation ELoc; 21193 SourceRange ERange; 21194 Expr *SimpleRefExpr = RefExpr; 21195 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, 21196 /*AllowArraySection=*/true); 21197 if (Res.second) 21198 // It will be analyzed later. 21199 Vars.push_back(RefExpr); 21200 ValueDecl *D = Res.first; 21201 if (!D) 21202 continue; 21203 21204 OpenMPDirectiveKind ParentDirective = DSAStack->getParentDirective(); 21205 DSAStackTy::DSAVarData DVar; 21206 if (ParentDirective != OMPD_unknown) 21207 DVar = DSAStack->getTopDSA(D, /*FromParent=*/true); 21208 // OpenMP 5.0, 2.9.6, scan Directive, Restrictions. 21209 // A list item that appears in the inclusive or exclusive clause must appear 21210 // in a reduction clause with the inscan modifier on the enclosing 21211 // worksharing-loop, worksharing-loop SIMD, or simd construct. 21212 if (ParentDirective == OMPD_unknown || DVar.CKind != OMPC_reduction || 21213 DVar.Modifier != OMPC_REDUCTION_inscan) { 21214 Diag(ELoc, diag::err_omp_inclusive_exclusive_not_reduction) 21215 << RefExpr->getSourceRange(); 21216 } else { 21217 DSAStack->markDeclAsUsedInScanDirective(D); 21218 } 21219 Vars.push_back(RefExpr); 21220 } 21221 21222 if (Vars.empty()) 21223 return nullptr; 21224 21225 return OMPExclusiveClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 21226 } 21227 21228 /// Tries to find omp_alloctrait_t type. 21229 static bool findOMPAlloctraitT(Sema &S, SourceLocation Loc, DSAStackTy *Stack) { 21230 QualType OMPAlloctraitT = Stack->getOMPAlloctraitT(); 21231 if (!OMPAlloctraitT.isNull()) 21232 return true; 21233 IdentifierInfo &II = S.PP.getIdentifierTable().get("omp_alloctrait_t"); 21234 ParsedType PT = S.getTypeName(II, Loc, S.getCurScope()); 21235 if (!PT.getAsOpaquePtr() || PT.get().isNull()) { 21236 S.Diag(Loc, diag::err_omp_implied_type_not_found) << "omp_alloctrait_t"; 21237 return false; 21238 } 21239 Stack->setOMPAlloctraitT(PT.get()); 21240 return true; 21241 } 21242 21243 OMPClause *Sema::ActOnOpenMPUsesAllocatorClause( 21244 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc, 21245 ArrayRef<UsesAllocatorsData> Data) { 21246 // OpenMP [2.12.5, target Construct] 21247 // allocator is an identifier of omp_allocator_handle_t type. 21248 if (!findOMPAllocatorHandleT(*this, StartLoc, DSAStack)) 21249 return nullptr; 21250 // OpenMP [2.12.5, target Construct] 21251 // allocator-traits-array is an identifier of const omp_alloctrait_t * type. 21252 if (llvm::any_of( 21253 Data, 21254 [](const UsesAllocatorsData &D) { return D.AllocatorTraits; }) && 21255 !findOMPAlloctraitT(*this, StartLoc, DSAStack)) 21256 return nullptr; 21257 llvm::SmallPtrSet<CanonicalDeclPtr<Decl>, 4> PredefinedAllocators; 21258 for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) { 21259 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I); 21260 StringRef Allocator = 21261 OMPAllocateDeclAttr::ConvertAllocatorTypeTyToStr(AllocatorKind); 21262 DeclarationName AllocatorName = &Context.Idents.get(Allocator); 21263 PredefinedAllocators.insert(LookupSingleName( 21264 TUScope, AllocatorName, StartLoc, Sema::LookupAnyName)); 21265 } 21266 21267 SmallVector<OMPUsesAllocatorsClause::Data, 4> NewData; 21268 for (const UsesAllocatorsData &D : Data) { 21269 Expr *AllocatorExpr = nullptr; 21270 // Check allocator expression. 21271 if (D.Allocator->isTypeDependent()) { 21272 AllocatorExpr = D.Allocator; 21273 } else { 21274 // Traits were specified - need to assign new allocator to the specified 21275 // allocator, so it must be an lvalue. 21276 AllocatorExpr = D.Allocator->IgnoreParenImpCasts(); 21277 auto *DRE = dyn_cast<DeclRefExpr>(AllocatorExpr); 21278 bool IsPredefinedAllocator = false; 21279 if (DRE) 21280 IsPredefinedAllocator = PredefinedAllocators.count(DRE->getDecl()); 21281 if (!DRE || 21282 !(Context.hasSameUnqualifiedType( 21283 AllocatorExpr->getType(), DSAStack->getOMPAllocatorHandleT()) || 21284 Context.typesAreCompatible(AllocatorExpr->getType(), 21285 DSAStack->getOMPAllocatorHandleT(), 21286 /*CompareUnqualified=*/true)) || 21287 (!IsPredefinedAllocator && 21288 (AllocatorExpr->getType().isConstant(Context) || 21289 !AllocatorExpr->isLValue()))) { 21290 Diag(D.Allocator->getExprLoc(), diag::err_omp_var_expected) 21291 << "omp_allocator_handle_t" << (DRE ? 1 : 0) 21292 << AllocatorExpr->getType() << D.Allocator->getSourceRange(); 21293 continue; 21294 } 21295 // OpenMP [2.12.5, target Construct] 21296 // Predefined allocators appearing in a uses_allocators clause cannot have 21297 // traits specified. 21298 if (IsPredefinedAllocator && D.AllocatorTraits) { 21299 Diag(D.AllocatorTraits->getExprLoc(), 21300 diag::err_omp_predefined_allocator_with_traits) 21301 << D.AllocatorTraits->getSourceRange(); 21302 Diag(D.Allocator->getExprLoc(), diag::note_omp_predefined_allocator) 21303 << cast<NamedDecl>(DRE->getDecl())->getName() 21304 << D.Allocator->getSourceRange(); 21305 continue; 21306 } 21307 // OpenMP [2.12.5, target Construct] 21308 // Non-predefined allocators appearing in a uses_allocators clause must 21309 // have traits specified. 21310 if (!IsPredefinedAllocator && !D.AllocatorTraits) { 21311 Diag(D.Allocator->getExprLoc(), 21312 diag::err_omp_nonpredefined_allocator_without_traits); 21313 continue; 21314 } 21315 // No allocator traits - just convert it to rvalue. 21316 if (!D.AllocatorTraits) 21317 AllocatorExpr = DefaultLvalueConversion(AllocatorExpr).get(); 21318 DSAStack->addUsesAllocatorsDecl( 21319 DRE->getDecl(), 21320 IsPredefinedAllocator 21321 ? DSAStackTy::UsesAllocatorsDeclKind::PredefinedAllocator 21322 : DSAStackTy::UsesAllocatorsDeclKind::UserDefinedAllocator); 21323 } 21324 Expr *AllocatorTraitsExpr = nullptr; 21325 if (D.AllocatorTraits) { 21326 if (D.AllocatorTraits->isTypeDependent()) { 21327 AllocatorTraitsExpr = D.AllocatorTraits; 21328 } else { 21329 // OpenMP [2.12.5, target Construct] 21330 // Arrays that contain allocator traits that appear in a uses_allocators 21331 // clause must be constant arrays, have constant values and be defined 21332 // in the same scope as the construct in which the clause appears. 21333 AllocatorTraitsExpr = D.AllocatorTraits->IgnoreParenImpCasts(); 21334 // Check that traits expr is a constant array. 21335 QualType TraitTy; 21336 if (const ArrayType *Ty = 21337 AllocatorTraitsExpr->getType()->getAsArrayTypeUnsafe()) 21338 if (const auto *ConstArrayTy = dyn_cast<ConstantArrayType>(Ty)) 21339 TraitTy = ConstArrayTy->getElementType(); 21340 if (TraitTy.isNull() || 21341 !(Context.hasSameUnqualifiedType(TraitTy, 21342 DSAStack->getOMPAlloctraitT()) || 21343 Context.typesAreCompatible(TraitTy, DSAStack->getOMPAlloctraitT(), 21344 /*CompareUnqualified=*/true))) { 21345 Diag(D.AllocatorTraits->getExprLoc(), 21346 diag::err_omp_expected_array_alloctraits) 21347 << AllocatorTraitsExpr->getType(); 21348 continue; 21349 } 21350 // Do not map by default allocator traits if it is a standalone 21351 // variable. 21352 if (auto *DRE = dyn_cast<DeclRefExpr>(AllocatorTraitsExpr)) 21353 DSAStack->addUsesAllocatorsDecl( 21354 DRE->getDecl(), 21355 DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait); 21356 } 21357 } 21358 OMPUsesAllocatorsClause::Data &NewD = NewData.emplace_back(); 21359 NewD.Allocator = AllocatorExpr; 21360 NewD.AllocatorTraits = AllocatorTraitsExpr; 21361 NewD.LParenLoc = D.LParenLoc; 21362 NewD.RParenLoc = D.RParenLoc; 21363 } 21364 return OMPUsesAllocatorsClause::Create(Context, StartLoc, LParenLoc, EndLoc, 21365 NewData); 21366 } 21367 21368 OMPClause *Sema::ActOnOpenMPAffinityClause( 21369 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc, 21370 SourceLocation EndLoc, Expr *Modifier, ArrayRef<Expr *> Locators) { 21371 SmallVector<Expr *, 8> Vars; 21372 for (Expr *RefExpr : Locators) { 21373 assert(RefExpr && "NULL expr in OpenMP shared clause."); 21374 if (isa<DependentScopeDeclRefExpr>(RefExpr) || RefExpr->isTypeDependent()) { 21375 // It will be analyzed later. 21376 Vars.push_back(RefExpr); 21377 continue; 21378 } 21379 21380 SourceLocation ELoc = RefExpr->getExprLoc(); 21381 Expr *SimpleExpr = RefExpr->IgnoreParenImpCasts(); 21382 21383 if (!SimpleExpr->isLValue()) { 21384 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 21385 << 1 << 0 << RefExpr->getSourceRange(); 21386 continue; 21387 } 21388 21389 ExprResult Res; 21390 { 21391 Sema::TentativeAnalysisScope Trap(*this); 21392 Res = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, SimpleExpr); 21393 } 21394 if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr) && 21395 !isa<OMPArrayShapingExpr>(SimpleExpr)) { 21396 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 21397 << 1 << 0 << RefExpr->getSourceRange(); 21398 continue; 21399 } 21400 Vars.push_back(SimpleExpr); 21401 } 21402 21403 return OMPAffinityClause::Create(Context, StartLoc, LParenLoc, ColonLoc, 21404 EndLoc, Modifier, Vars); 21405 } 21406