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 if (!llvm::is_contained(AllowedNameModifiers, CurNM)) { 4967 S.Diag(IC->getNameModifierLoc(), 4968 diag::err_omp_wrong_if_directive_name_modifier) 4969 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind); 4970 ErrorFound = true; 4971 } 4972 } 4973 } 4974 // If any if clause on the directive includes a directive-name-modifier then 4975 // all if clauses on the directive must include a directive-name-modifier. 4976 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) { 4977 if (NamedModifiersNumber == AllowedNameModifiers.size()) { 4978 S.Diag(FoundNameModifiers[OMPD_unknown]->getBeginLoc(), 4979 diag::err_omp_no_more_if_clause); 4980 } else { 4981 std::string Values; 4982 std::string Sep(", "); 4983 unsigned AllowedCnt = 0; 4984 unsigned TotalAllowedNum = 4985 AllowedNameModifiers.size() - NamedModifiersNumber; 4986 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End; 4987 ++Cnt) { 4988 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt]; 4989 if (!FoundNameModifiers[NM]) { 4990 Values += "'"; 4991 Values += getOpenMPDirectiveName(NM); 4992 Values += "'"; 4993 if (AllowedCnt + 2 == TotalAllowedNum) 4994 Values += " or "; 4995 else if (AllowedCnt + 1 != TotalAllowedNum) 4996 Values += Sep; 4997 ++AllowedCnt; 4998 } 4999 } 5000 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getBeginLoc(), 5001 diag::err_omp_unnamed_if_clause) 5002 << (TotalAllowedNum > 1) << Values; 5003 } 5004 for (SourceLocation Loc : NameModifierLoc) { 5005 S.Diag(Loc, diag::note_omp_previous_named_if_clause); 5006 } 5007 ErrorFound = true; 5008 } 5009 return ErrorFound; 5010 } 5011 5012 static std::pair<ValueDecl *, bool> getPrivateItem(Sema &S, Expr *&RefExpr, 5013 SourceLocation &ELoc, 5014 SourceRange &ERange, 5015 bool AllowArraySection) { 5016 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() || 5017 RefExpr->containsUnexpandedParameterPack()) 5018 return std::make_pair(nullptr, true); 5019 5020 // OpenMP [3.1, C/C++] 5021 // A list item is a variable name. 5022 // OpenMP [2.9.3.3, Restrictions, p.1] 5023 // A variable that is part of another variable (as an array or 5024 // structure element) cannot appear in a private clause. 5025 RefExpr = RefExpr->IgnoreParens(); 5026 enum { 5027 NoArrayExpr = -1, 5028 ArraySubscript = 0, 5029 OMPArraySection = 1 5030 } IsArrayExpr = NoArrayExpr; 5031 if (AllowArraySection) { 5032 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) { 5033 Expr *Base = ASE->getBase()->IgnoreParenImpCasts(); 5034 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 5035 Base = TempASE->getBase()->IgnoreParenImpCasts(); 5036 RefExpr = Base; 5037 IsArrayExpr = ArraySubscript; 5038 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) { 5039 Expr *Base = OASE->getBase()->IgnoreParenImpCasts(); 5040 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) 5041 Base = TempOASE->getBase()->IgnoreParenImpCasts(); 5042 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 5043 Base = TempASE->getBase()->IgnoreParenImpCasts(); 5044 RefExpr = Base; 5045 IsArrayExpr = OMPArraySection; 5046 } 5047 } 5048 ELoc = RefExpr->getExprLoc(); 5049 ERange = RefExpr->getSourceRange(); 5050 RefExpr = RefExpr->IgnoreParenImpCasts(); 5051 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr); 5052 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr); 5053 if ((!DE || !isa<VarDecl>(DE->getDecl())) && 5054 (S.getCurrentThisType().isNull() || !ME || 5055 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) || 5056 !isa<FieldDecl>(ME->getMemberDecl()))) { 5057 if (IsArrayExpr != NoArrayExpr) { 5058 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr 5059 << ERange; 5060 } else { 5061 S.Diag(ELoc, 5062 AllowArraySection 5063 ? diag::err_omp_expected_var_name_member_expr_or_array_item 5064 : diag::err_omp_expected_var_name_member_expr) 5065 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange; 5066 } 5067 return std::make_pair(nullptr, false); 5068 } 5069 return std::make_pair( 5070 getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false); 5071 } 5072 5073 namespace { 5074 /// Checks if the allocator is used in uses_allocators clause to be allowed in 5075 /// target regions. 5076 class AllocatorChecker final : public ConstStmtVisitor<AllocatorChecker, bool> { 5077 DSAStackTy *S = nullptr; 5078 5079 public: 5080 bool VisitDeclRefExpr(const DeclRefExpr *E) { 5081 return S->isUsesAllocatorsDecl(E->getDecl()) 5082 .getValueOr( 5083 DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait) == 5084 DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait; 5085 } 5086 bool VisitStmt(const Stmt *S) { 5087 for (const Stmt *Child : S->children()) { 5088 if (Child && Visit(Child)) 5089 return true; 5090 } 5091 return false; 5092 } 5093 explicit AllocatorChecker(DSAStackTy *S) : S(S) {} 5094 }; 5095 } // namespace 5096 5097 static void checkAllocateClauses(Sema &S, DSAStackTy *Stack, 5098 ArrayRef<OMPClause *> Clauses) { 5099 assert(!S.CurContext->isDependentContext() && 5100 "Expected non-dependent context."); 5101 auto AllocateRange = 5102 llvm::make_filter_range(Clauses, OMPAllocateClause::classof); 5103 llvm::DenseMap<CanonicalDeclPtr<Decl>, CanonicalDeclPtr<VarDecl>> 5104 DeclToCopy; 5105 auto PrivateRange = llvm::make_filter_range(Clauses, [](const OMPClause *C) { 5106 return isOpenMPPrivate(C->getClauseKind()); 5107 }); 5108 for (OMPClause *Cl : PrivateRange) { 5109 MutableArrayRef<Expr *>::iterator I, It, Et; 5110 if (Cl->getClauseKind() == OMPC_private) { 5111 auto *PC = cast<OMPPrivateClause>(Cl); 5112 I = PC->private_copies().begin(); 5113 It = PC->varlist_begin(); 5114 Et = PC->varlist_end(); 5115 } else if (Cl->getClauseKind() == OMPC_firstprivate) { 5116 auto *PC = cast<OMPFirstprivateClause>(Cl); 5117 I = PC->private_copies().begin(); 5118 It = PC->varlist_begin(); 5119 Et = PC->varlist_end(); 5120 } else if (Cl->getClauseKind() == OMPC_lastprivate) { 5121 auto *PC = cast<OMPLastprivateClause>(Cl); 5122 I = PC->private_copies().begin(); 5123 It = PC->varlist_begin(); 5124 Et = PC->varlist_end(); 5125 } else if (Cl->getClauseKind() == OMPC_linear) { 5126 auto *PC = cast<OMPLinearClause>(Cl); 5127 I = PC->privates().begin(); 5128 It = PC->varlist_begin(); 5129 Et = PC->varlist_end(); 5130 } else if (Cl->getClauseKind() == OMPC_reduction) { 5131 auto *PC = cast<OMPReductionClause>(Cl); 5132 I = PC->privates().begin(); 5133 It = PC->varlist_begin(); 5134 Et = PC->varlist_end(); 5135 } else if (Cl->getClauseKind() == OMPC_task_reduction) { 5136 auto *PC = cast<OMPTaskReductionClause>(Cl); 5137 I = PC->privates().begin(); 5138 It = PC->varlist_begin(); 5139 Et = PC->varlist_end(); 5140 } else if (Cl->getClauseKind() == OMPC_in_reduction) { 5141 auto *PC = cast<OMPInReductionClause>(Cl); 5142 I = PC->privates().begin(); 5143 It = PC->varlist_begin(); 5144 Et = PC->varlist_end(); 5145 } else { 5146 llvm_unreachable("Expected private clause."); 5147 } 5148 for (Expr *E : llvm::make_range(It, Et)) { 5149 if (!*I) { 5150 ++I; 5151 continue; 5152 } 5153 SourceLocation ELoc; 5154 SourceRange ERange; 5155 Expr *SimpleRefExpr = E; 5156 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange, 5157 /*AllowArraySection=*/true); 5158 DeclToCopy.try_emplace(Res.first, 5159 cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl())); 5160 ++I; 5161 } 5162 } 5163 for (OMPClause *C : AllocateRange) { 5164 auto *AC = cast<OMPAllocateClause>(C); 5165 if (S.getLangOpts().OpenMP >= 50 && 5166 !Stack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>() && 5167 isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) && 5168 AC->getAllocator()) { 5169 Expr *Allocator = AC->getAllocator(); 5170 // OpenMP, 2.12.5 target Construct 5171 // Memory allocators that do not appear in a uses_allocators clause cannot 5172 // appear as an allocator in an allocate clause or be used in the target 5173 // region unless a requires directive with the dynamic_allocators clause 5174 // is present in the same compilation unit. 5175 AllocatorChecker Checker(Stack); 5176 if (Checker.Visit(Allocator)) 5177 S.Diag(Allocator->getExprLoc(), 5178 diag::err_omp_allocator_not_in_uses_allocators) 5179 << Allocator->getSourceRange(); 5180 } 5181 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind = 5182 getAllocatorKind(S, Stack, AC->getAllocator()); 5183 // OpenMP, 2.11.4 allocate Clause, Restrictions. 5184 // For task, taskloop or target directives, allocation requests to memory 5185 // allocators with the trait access set to thread result in unspecified 5186 // behavior. 5187 if (AllocatorKind == OMPAllocateDeclAttr::OMPThreadMemAlloc && 5188 (isOpenMPTaskingDirective(Stack->getCurrentDirective()) || 5189 isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()))) { 5190 S.Diag(AC->getAllocator()->getExprLoc(), 5191 diag::warn_omp_allocate_thread_on_task_target_directive) 5192 << getOpenMPDirectiveName(Stack->getCurrentDirective()); 5193 } 5194 for (Expr *E : AC->varlists()) { 5195 SourceLocation ELoc; 5196 SourceRange ERange; 5197 Expr *SimpleRefExpr = E; 5198 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange); 5199 ValueDecl *VD = Res.first; 5200 DSAStackTy::DSAVarData Data = Stack->getTopDSA(VD, /*FromParent=*/false); 5201 if (!isOpenMPPrivate(Data.CKind)) { 5202 S.Diag(E->getExprLoc(), 5203 diag::err_omp_expected_private_copy_for_allocate); 5204 continue; 5205 } 5206 VarDecl *PrivateVD = DeclToCopy[VD]; 5207 if (checkPreviousOMPAllocateAttribute(S, Stack, E, PrivateVD, 5208 AllocatorKind, AC->getAllocator())) 5209 continue; 5210 applyOMPAllocateAttribute(S, PrivateVD, AllocatorKind, AC->getAllocator(), 5211 E->getSourceRange()); 5212 } 5213 } 5214 } 5215 5216 namespace { 5217 /// Rewrite statements and expressions for Sema \p Actions CurContext. 5218 /// 5219 /// Used to wrap already parsed statements/expressions into a new CapturedStmt 5220 /// context. DeclRefExpr used inside the new context are changed to refer to the 5221 /// captured variable instead. 5222 class CaptureVars : public TreeTransform<CaptureVars> { 5223 using BaseTransform = TreeTransform<CaptureVars>; 5224 5225 public: 5226 CaptureVars(Sema &Actions) : BaseTransform(Actions) {} 5227 5228 bool AlwaysRebuild() { return true; } 5229 }; 5230 } // namespace 5231 5232 static VarDecl *precomputeExpr(Sema &Actions, 5233 SmallVectorImpl<Stmt *> &BodyStmts, Expr *E, 5234 StringRef Name) { 5235 Expr *NewE = AssertSuccess(CaptureVars(Actions).TransformExpr(E)); 5236 VarDecl *NewVar = buildVarDecl(Actions, {}, NewE->getType(), Name, nullptr, 5237 dyn_cast<DeclRefExpr>(E->IgnoreImplicit())); 5238 auto *NewDeclStmt = cast<DeclStmt>(AssertSuccess( 5239 Actions.ActOnDeclStmt(Actions.ConvertDeclToDeclGroup(NewVar), {}, {}))); 5240 Actions.AddInitializerToDecl(NewDeclStmt->getSingleDecl(), NewE, false); 5241 BodyStmts.push_back(NewDeclStmt); 5242 return NewVar; 5243 } 5244 5245 /// Create a closure that computes the number of iterations of a loop. 5246 /// 5247 /// \param Actions The Sema object. 5248 /// \param LogicalTy Type for the logical iteration number. 5249 /// \param Rel Comparison operator of the loop condition. 5250 /// \param StartExpr Value of the loop counter at the first iteration. 5251 /// \param StopExpr Expression the loop counter is compared against in the loop 5252 /// condition. \param StepExpr Amount of increment after each iteration. 5253 /// 5254 /// \return Closure (CapturedStmt) of the distance calculation. 5255 static CapturedStmt *buildDistanceFunc(Sema &Actions, QualType LogicalTy, 5256 BinaryOperator::Opcode Rel, 5257 Expr *StartExpr, Expr *StopExpr, 5258 Expr *StepExpr) { 5259 ASTContext &Ctx = Actions.getASTContext(); 5260 TypeSourceInfo *LogicalTSI = Ctx.getTrivialTypeSourceInfo(LogicalTy); 5261 5262 // Captured regions currently don't support return values, we use an 5263 // out-parameter instead. All inputs are implicit captures. 5264 // TODO: Instead of capturing each DeclRefExpr occurring in 5265 // StartExpr/StopExpr/Step, these could also be passed as a value capture. 5266 QualType ResultTy = Ctx.getLValueReferenceType(LogicalTy); 5267 Sema::CapturedParamNameType Params[] = {{"Distance", ResultTy}, 5268 {StringRef(), QualType()}}; 5269 Actions.ActOnCapturedRegionStart({}, nullptr, CR_Default, Params); 5270 5271 Stmt *Body; 5272 { 5273 Sema::CompoundScopeRAII CompoundScope(Actions); 5274 CapturedDecl *CS = cast<CapturedDecl>(Actions.CurContext); 5275 5276 // Get the LValue expression for the result. 5277 ImplicitParamDecl *DistParam = CS->getParam(0); 5278 DeclRefExpr *DistRef = Actions.BuildDeclRefExpr( 5279 DistParam, LogicalTy, VK_LValue, {}, nullptr, nullptr, {}, nullptr); 5280 5281 SmallVector<Stmt *, 4> BodyStmts; 5282 5283 // Capture all referenced variable references. 5284 // TODO: Instead of computing NewStart/NewStop/NewStep inside the 5285 // CapturedStmt, we could compute them before and capture the result, to be 5286 // used jointly with the LoopVar function. 5287 VarDecl *NewStart = precomputeExpr(Actions, BodyStmts, StartExpr, ".start"); 5288 VarDecl *NewStop = precomputeExpr(Actions, BodyStmts, StopExpr, ".stop"); 5289 VarDecl *NewStep = precomputeExpr(Actions, BodyStmts, StepExpr, ".step"); 5290 auto BuildVarRef = [&](VarDecl *VD) { 5291 return buildDeclRefExpr(Actions, VD, VD->getType(), {}); 5292 }; 5293 5294 IntegerLiteral *Zero = IntegerLiteral::Create( 5295 Ctx, llvm::APInt(Ctx.getIntWidth(LogicalTy), 0), LogicalTy, {}); 5296 Expr *Dist; 5297 if (Rel == BO_NE) { 5298 // When using a != comparison, the increment can be +1 or -1. This can be 5299 // dynamic at runtime, so we need to check for the direction. 5300 Expr *IsNegStep = AssertSuccess( 5301 Actions.BuildBinOp(nullptr, {}, BO_LT, BuildVarRef(NewStep), Zero)); 5302 5303 // Positive increment. 5304 Expr *ForwardRange = AssertSuccess(Actions.BuildBinOp( 5305 nullptr, {}, BO_Sub, BuildVarRef(NewStop), BuildVarRef(NewStart))); 5306 ForwardRange = AssertSuccess( 5307 Actions.BuildCStyleCastExpr({}, LogicalTSI, {}, ForwardRange)); 5308 Expr *ForwardDist = AssertSuccess(Actions.BuildBinOp( 5309 nullptr, {}, BO_Div, ForwardRange, BuildVarRef(NewStep))); 5310 5311 // Negative increment. 5312 Expr *BackwardRange = AssertSuccess(Actions.BuildBinOp( 5313 nullptr, {}, BO_Sub, BuildVarRef(NewStart), BuildVarRef(NewStop))); 5314 BackwardRange = AssertSuccess( 5315 Actions.BuildCStyleCastExpr({}, LogicalTSI, {}, BackwardRange)); 5316 Expr *NegIncAmount = AssertSuccess( 5317 Actions.BuildUnaryOp(nullptr, {}, UO_Minus, BuildVarRef(NewStep))); 5318 Expr *BackwardDist = AssertSuccess( 5319 Actions.BuildBinOp(nullptr, {}, BO_Div, BackwardRange, NegIncAmount)); 5320 5321 // Use the appropriate case. 5322 Dist = AssertSuccess(Actions.ActOnConditionalOp( 5323 {}, {}, IsNegStep, BackwardDist, ForwardDist)); 5324 } else { 5325 assert((Rel == BO_LT || Rel == BO_LE || Rel == BO_GE || Rel == BO_GT) && 5326 "Expected one of these relational operators"); 5327 5328 // We can derive the direction from any other comparison operator. It is 5329 // non well-formed OpenMP if Step increments/decrements in the other 5330 // directions. Whether at least the first iteration passes the loop 5331 // condition. 5332 Expr *HasAnyIteration = AssertSuccess(Actions.BuildBinOp( 5333 nullptr, {}, Rel, BuildVarRef(NewStart), BuildVarRef(NewStop))); 5334 5335 // Compute the range between first and last counter value. 5336 Expr *Range; 5337 if (Rel == BO_GE || Rel == BO_GT) 5338 Range = AssertSuccess(Actions.BuildBinOp( 5339 nullptr, {}, BO_Sub, BuildVarRef(NewStart), BuildVarRef(NewStop))); 5340 else 5341 Range = AssertSuccess(Actions.BuildBinOp( 5342 nullptr, {}, BO_Sub, BuildVarRef(NewStop), BuildVarRef(NewStart))); 5343 5344 // Ensure unsigned range space. 5345 Range = 5346 AssertSuccess(Actions.BuildCStyleCastExpr({}, LogicalTSI, {}, Range)); 5347 5348 if (Rel == BO_LE || Rel == BO_GE) { 5349 // Add one to the range if the relational operator is inclusive. 5350 Range = AssertSuccess(Actions.BuildBinOp( 5351 nullptr, {}, BO_Add, Range, 5352 Actions.ActOnIntegerConstant(SourceLocation(), 1).get())); 5353 } 5354 5355 // Divide by the absolute step amount. 5356 Expr *Divisor = BuildVarRef(NewStep); 5357 if (Rel == BO_GE || Rel == BO_GT) 5358 Divisor = 5359 AssertSuccess(Actions.BuildUnaryOp(nullptr, {}, UO_Minus, Divisor)); 5360 Dist = AssertSuccess( 5361 Actions.BuildBinOp(nullptr, {}, BO_Div, Range, Divisor)); 5362 5363 // If there is not at least one iteration, the range contains garbage. Fix 5364 // to zero in this case. 5365 Dist = AssertSuccess( 5366 Actions.ActOnConditionalOp({}, {}, HasAnyIteration, Dist, Zero)); 5367 } 5368 5369 // Assign the result to the out-parameter. 5370 Stmt *ResultAssign = AssertSuccess(Actions.BuildBinOp( 5371 Actions.getCurScope(), {}, BO_Assign, DistRef, Dist)); 5372 BodyStmts.push_back(ResultAssign); 5373 5374 Body = AssertSuccess(Actions.ActOnCompoundStmt({}, {}, BodyStmts, false)); 5375 } 5376 5377 return cast<CapturedStmt>( 5378 AssertSuccess(Actions.ActOnCapturedRegionEnd(Body))); 5379 } 5380 5381 /// Create a closure that computes the loop variable from the logical iteration 5382 /// number. 5383 /// 5384 /// \param Actions The Sema object. 5385 /// \param LoopVarTy Type for the loop variable used for result value. 5386 /// \param LogicalTy Type for the logical iteration number. 5387 /// \param StartExpr Value of the loop counter at the first iteration. 5388 /// \param Step Amount of increment after each iteration. 5389 /// \param Deref Whether the loop variable is a dereference of the loop 5390 /// counter variable. 5391 /// 5392 /// \return Closure (CapturedStmt) of the loop value calculation. 5393 static CapturedStmt *buildLoopVarFunc(Sema &Actions, QualType LoopVarTy, 5394 QualType LogicalTy, 5395 DeclRefExpr *StartExpr, Expr *Step, 5396 bool Deref) { 5397 ASTContext &Ctx = Actions.getASTContext(); 5398 5399 // Pass the result as an out-parameter. Passing as return value would require 5400 // the OpenMPIRBuilder to know additional C/C++ semantics, such as how to 5401 // invoke a copy constructor. 5402 QualType TargetParamTy = Ctx.getLValueReferenceType(LoopVarTy); 5403 Sema::CapturedParamNameType Params[] = {{"LoopVar", TargetParamTy}, 5404 {"Logical", LogicalTy}, 5405 {StringRef(), QualType()}}; 5406 Actions.ActOnCapturedRegionStart({}, nullptr, CR_Default, Params); 5407 5408 // Capture the initial iterator which represents the LoopVar value at the 5409 // zero's logical iteration. Since the original ForStmt/CXXForRangeStmt update 5410 // it in every iteration, capture it by value before it is modified. 5411 VarDecl *StartVar = cast<VarDecl>(StartExpr->getDecl()); 5412 bool Invalid = Actions.tryCaptureVariable(StartVar, {}, 5413 Sema::TryCapture_ExplicitByVal, {}); 5414 (void)Invalid; 5415 assert(!Invalid && "Expecting capture-by-value to work."); 5416 5417 Expr *Body; 5418 { 5419 Sema::CompoundScopeRAII CompoundScope(Actions); 5420 auto *CS = cast<CapturedDecl>(Actions.CurContext); 5421 5422 ImplicitParamDecl *TargetParam = CS->getParam(0); 5423 DeclRefExpr *TargetRef = Actions.BuildDeclRefExpr( 5424 TargetParam, LoopVarTy, VK_LValue, {}, nullptr, nullptr, {}, nullptr); 5425 ImplicitParamDecl *IndvarParam = CS->getParam(1); 5426 DeclRefExpr *LogicalRef = Actions.BuildDeclRefExpr( 5427 IndvarParam, LogicalTy, VK_LValue, {}, nullptr, nullptr, {}, nullptr); 5428 5429 // Capture the Start expression. 5430 CaptureVars Recap(Actions); 5431 Expr *NewStart = AssertSuccess(Recap.TransformExpr(StartExpr)); 5432 Expr *NewStep = AssertSuccess(Recap.TransformExpr(Step)); 5433 5434 Expr *Skip = AssertSuccess( 5435 Actions.BuildBinOp(nullptr, {}, BO_Mul, NewStep, LogicalRef)); 5436 // TODO: Explicitly cast to the iterator's difference_type instead of 5437 // relying on implicit conversion. 5438 Expr *Advanced = 5439 AssertSuccess(Actions.BuildBinOp(nullptr, {}, BO_Add, NewStart, Skip)); 5440 5441 if (Deref) { 5442 // For range-based for-loops convert the loop counter value to a concrete 5443 // loop variable value by dereferencing the iterator. 5444 Advanced = 5445 AssertSuccess(Actions.BuildUnaryOp(nullptr, {}, UO_Deref, Advanced)); 5446 } 5447 5448 // Assign the result to the output parameter. 5449 Body = AssertSuccess(Actions.BuildBinOp(Actions.getCurScope(), {}, 5450 BO_Assign, TargetRef, Advanced)); 5451 } 5452 return cast<CapturedStmt>( 5453 AssertSuccess(Actions.ActOnCapturedRegionEnd(Body))); 5454 } 5455 5456 StmtResult Sema::ActOnOpenMPCanonicalLoop(Stmt *AStmt) { 5457 ASTContext &Ctx = getASTContext(); 5458 5459 // Extract the common elements of ForStmt and CXXForRangeStmt: 5460 // Loop variable, repeat condition, increment 5461 Expr *Cond, *Inc; 5462 VarDecl *LIVDecl, *LUVDecl; 5463 if (auto *For = dyn_cast<ForStmt>(AStmt)) { 5464 Stmt *Init = For->getInit(); 5465 if (auto *LCVarDeclStmt = dyn_cast<DeclStmt>(Init)) { 5466 // For statement declares loop variable. 5467 LIVDecl = cast<VarDecl>(LCVarDeclStmt->getSingleDecl()); 5468 } else if (auto *LCAssign = dyn_cast<BinaryOperator>(Init)) { 5469 // For statement reuses variable. 5470 assert(LCAssign->getOpcode() == BO_Assign && 5471 "init part must be a loop variable assignment"); 5472 auto *CounterRef = cast<DeclRefExpr>(LCAssign->getLHS()); 5473 LIVDecl = cast<VarDecl>(CounterRef->getDecl()); 5474 } else 5475 llvm_unreachable("Cannot determine loop variable"); 5476 LUVDecl = LIVDecl; 5477 5478 Cond = For->getCond(); 5479 Inc = For->getInc(); 5480 } else if (auto *RangeFor = dyn_cast<CXXForRangeStmt>(AStmt)) { 5481 DeclStmt *BeginStmt = RangeFor->getBeginStmt(); 5482 LIVDecl = cast<VarDecl>(BeginStmt->getSingleDecl()); 5483 LUVDecl = RangeFor->getLoopVariable(); 5484 5485 Cond = RangeFor->getCond(); 5486 Inc = RangeFor->getInc(); 5487 } else 5488 llvm_unreachable("unhandled kind of loop"); 5489 5490 QualType CounterTy = LIVDecl->getType(); 5491 QualType LVTy = LUVDecl->getType(); 5492 5493 // Analyze the loop condition. 5494 Expr *LHS, *RHS; 5495 BinaryOperator::Opcode CondRel; 5496 Cond = Cond->IgnoreImplicit(); 5497 if (auto *CondBinExpr = dyn_cast<BinaryOperator>(Cond)) { 5498 LHS = CondBinExpr->getLHS(); 5499 RHS = CondBinExpr->getRHS(); 5500 CondRel = CondBinExpr->getOpcode(); 5501 } else if (auto *CondCXXOp = dyn_cast<CXXOperatorCallExpr>(Cond)) { 5502 assert(CondCXXOp->getNumArgs() == 2 && "Comparison should have 2 operands"); 5503 LHS = CondCXXOp->getArg(0); 5504 RHS = CondCXXOp->getArg(1); 5505 switch (CondCXXOp->getOperator()) { 5506 case OO_ExclaimEqual: 5507 CondRel = BO_NE; 5508 break; 5509 case OO_Less: 5510 CondRel = BO_LT; 5511 break; 5512 case OO_LessEqual: 5513 CondRel = BO_LE; 5514 break; 5515 case OO_Greater: 5516 CondRel = BO_GT; 5517 break; 5518 case OO_GreaterEqual: 5519 CondRel = BO_GE; 5520 break; 5521 default: 5522 llvm_unreachable("unexpected iterator operator"); 5523 } 5524 } else 5525 llvm_unreachable("unexpected loop condition"); 5526 5527 // Normalize such that the loop counter is on the LHS. 5528 if (!isa<DeclRefExpr>(LHS->IgnoreImplicit()) || 5529 cast<DeclRefExpr>(LHS->IgnoreImplicit())->getDecl() != LIVDecl) { 5530 std::swap(LHS, RHS); 5531 CondRel = BinaryOperator::reverseComparisonOp(CondRel); 5532 } 5533 auto *CounterRef = cast<DeclRefExpr>(LHS->IgnoreImplicit()); 5534 5535 // Decide the bit width for the logical iteration counter. By default use the 5536 // unsigned ptrdiff_t integer size (for iterators and pointers). 5537 // TODO: For iterators, use iterator::difference_type, 5538 // std::iterator_traits<>::difference_type or decltype(it - end). 5539 QualType LogicalTy = Ctx.getUnsignedPointerDiffType(); 5540 if (CounterTy->isIntegerType()) { 5541 unsigned BitWidth = Ctx.getIntWidth(CounterTy); 5542 LogicalTy = Ctx.getIntTypeForBitwidth(BitWidth, false); 5543 } 5544 5545 // Analyze the loop increment. 5546 Expr *Step; 5547 if (auto *IncUn = dyn_cast<UnaryOperator>(Inc)) { 5548 int Direction; 5549 switch (IncUn->getOpcode()) { 5550 case UO_PreInc: 5551 case UO_PostInc: 5552 Direction = 1; 5553 break; 5554 case UO_PreDec: 5555 case UO_PostDec: 5556 Direction = -1; 5557 break; 5558 default: 5559 llvm_unreachable("unhandled unary increment operator"); 5560 } 5561 Step = IntegerLiteral::Create( 5562 Ctx, llvm::APInt(Ctx.getIntWidth(LogicalTy), Direction), LogicalTy, {}); 5563 } else if (auto *IncBin = dyn_cast<BinaryOperator>(Inc)) { 5564 if (IncBin->getOpcode() == BO_AddAssign) { 5565 Step = IncBin->getRHS(); 5566 } else if (IncBin->getOpcode() == BO_SubAssign) { 5567 Step = 5568 AssertSuccess(BuildUnaryOp(nullptr, {}, UO_Minus, IncBin->getRHS())); 5569 } else 5570 llvm_unreachable("unhandled binary increment operator"); 5571 } else if (auto *CondCXXOp = dyn_cast<CXXOperatorCallExpr>(Inc)) { 5572 switch (CondCXXOp->getOperator()) { 5573 case OO_PlusPlus: 5574 Step = IntegerLiteral::Create( 5575 Ctx, llvm::APInt(Ctx.getIntWidth(LogicalTy), 1), LogicalTy, {}); 5576 break; 5577 case OO_MinusMinus: 5578 Step = IntegerLiteral::Create( 5579 Ctx, llvm::APInt(Ctx.getIntWidth(LogicalTy), -1), LogicalTy, {}); 5580 break; 5581 case OO_PlusEqual: 5582 Step = CondCXXOp->getArg(1); 5583 break; 5584 case OO_MinusEqual: 5585 Step = AssertSuccess( 5586 BuildUnaryOp(nullptr, {}, UO_Minus, CondCXXOp->getArg(1))); 5587 break; 5588 default: 5589 llvm_unreachable("unhandled overloaded increment operator"); 5590 } 5591 } else 5592 llvm_unreachable("unknown increment expression"); 5593 5594 CapturedStmt *DistanceFunc = 5595 buildDistanceFunc(*this, LogicalTy, CondRel, LHS, RHS, Step); 5596 CapturedStmt *LoopVarFunc = buildLoopVarFunc( 5597 *this, LVTy, LogicalTy, CounterRef, Step, isa<CXXForRangeStmt>(AStmt)); 5598 DeclRefExpr *LVRef = BuildDeclRefExpr(LUVDecl, LUVDecl->getType(), VK_LValue, 5599 {}, nullptr, nullptr, {}, nullptr); 5600 return OMPCanonicalLoop::create(getASTContext(), AStmt, DistanceFunc, 5601 LoopVarFunc, LVRef); 5602 } 5603 5604 StmtResult Sema::ActOnOpenMPLoopnest(Stmt *AStmt) { 5605 // Handle a literal loop. 5606 if (isa<ForStmt>(AStmt) || isa<CXXForRangeStmt>(AStmt)) 5607 return ActOnOpenMPCanonicalLoop(AStmt); 5608 5609 // If not a literal loop, it must be the result of a loop transformation. 5610 OMPExecutableDirective *LoopTransform = cast<OMPExecutableDirective>(AStmt); 5611 assert( 5612 isOpenMPLoopTransformationDirective(LoopTransform->getDirectiveKind()) && 5613 "Loop transformation directive expected"); 5614 return LoopTransform; 5615 } 5616 5617 static ExprResult buildUserDefinedMapperRef(Sema &SemaRef, Scope *S, 5618 CXXScopeSpec &MapperIdScopeSpec, 5619 const DeclarationNameInfo &MapperId, 5620 QualType Type, 5621 Expr *UnresolvedMapper); 5622 5623 /// Perform DFS through the structure/class data members trying to find 5624 /// member(s) with user-defined 'default' mapper and generate implicit map 5625 /// clauses for such members with the found 'default' mapper. 5626 static void 5627 processImplicitMapsWithDefaultMappers(Sema &S, DSAStackTy *Stack, 5628 SmallVectorImpl<OMPClause *> &Clauses) { 5629 // Check for the deault mapper for data members. 5630 if (S.getLangOpts().OpenMP < 50) 5631 return; 5632 SmallVector<OMPClause *, 4> ImplicitMaps; 5633 for (int Cnt = 0, EndCnt = Clauses.size(); Cnt < EndCnt; ++Cnt) { 5634 auto *C = dyn_cast<OMPMapClause>(Clauses[Cnt]); 5635 if (!C) 5636 continue; 5637 SmallVector<Expr *, 4> SubExprs; 5638 auto *MI = C->mapperlist_begin(); 5639 for (auto I = C->varlist_begin(), End = C->varlist_end(); I != End; 5640 ++I, ++MI) { 5641 // Expression is mapped using mapper - skip it. 5642 if (*MI) 5643 continue; 5644 Expr *E = *I; 5645 // Expression is dependent - skip it, build the mapper when it gets 5646 // instantiated. 5647 if (E->isTypeDependent() || E->isValueDependent() || 5648 E->containsUnexpandedParameterPack()) 5649 continue; 5650 // Array section - need to check for the mapping of the array section 5651 // element. 5652 QualType CanonType = E->getType().getCanonicalType(); 5653 if (CanonType->isSpecificBuiltinType(BuiltinType::OMPArraySection)) { 5654 const auto *OASE = cast<OMPArraySectionExpr>(E->IgnoreParenImpCasts()); 5655 QualType BaseType = 5656 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 5657 QualType ElemType; 5658 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 5659 ElemType = ATy->getElementType(); 5660 else 5661 ElemType = BaseType->getPointeeType(); 5662 CanonType = ElemType; 5663 } 5664 5665 // DFS over data members in structures/classes. 5666 SmallVector<std::pair<QualType, FieldDecl *>, 4> Types( 5667 1, {CanonType, nullptr}); 5668 llvm::DenseMap<const Type *, Expr *> Visited; 5669 SmallVector<std::pair<FieldDecl *, unsigned>, 4> ParentChain( 5670 1, {nullptr, 1}); 5671 while (!Types.empty()) { 5672 QualType BaseType; 5673 FieldDecl *CurFD; 5674 std::tie(BaseType, CurFD) = Types.pop_back_val(); 5675 while (ParentChain.back().second == 0) 5676 ParentChain.pop_back(); 5677 --ParentChain.back().second; 5678 if (BaseType.isNull()) 5679 continue; 5680 // Only structs/classes are allowed to have mappers. 5681 const RecordDecl *RD = BaseType.getCanonicalType()->getAsRecordDecl(); 5682 if (!RD) 5683 continue; 5684 auto It = Visited.find(BaseType.getTypePtr()); 5685 if (It == Visited.end()) { 5686 // Try to find the associated user-defined mapper. 5687 CXXScopeSpec MapperIdScopeSpec; 5688 DeclarationNameInfo DefaultMapperId; 5689 DefaultMapperId.setName(S.Context.DeclarationNames.getIdentifier( 5690 &S.Context.Idents.get("default"))); 5691 DefaultMapperId.setLoc(E->getExprLoc()); 5692 ExprResult ER = buildUserDefinedMapperRef( 5693 S, Stack->getCurScope(), MapperIdScopeSpec, DefaultMapperId, 5694 BaseType, /*UnresolvedMapper=*/nullptr); 5695 if (ER.isInvalid()) 5696 continue; 5697 It = Visited.try_emplace(BaseType.getTypePtr(), ER.get()).first; 5698 } 5699 // Found default mapper. 5700 if (It->second) { 5701 auto *OE = new (S.Context) OpaqueValueExpr(E->getExprLoc(), CanonType, 5702 VK_LValue, OK_Ordinary, E); 5703 OE->setIsUnique(/*V=*/true); 5704 Expr *BaseExpr = OE; 5705 for (const auto &P : ParentChain) { 5706 if (P.first) { 5707 BaseExpr = S.BuildMemberExpr( 5708 BaseExpr, /*IsArrow=*/false, E->getExprLoc(), 5709 NestedNameSpecifierLoc(), SourceLocation(), P.first, 5710 DeclAccessPair::make(P.first, P.first->getAccess()), 5711 /*HadMultipleCandidates=*/false, DeclarationNameInfo(), 5712 P.first->getType(), VK_LValue, OK_Ordinary); 5713 BaseExpr = S.DefaultLvalueConversion(BaseExpr).get(); 5714 } 5715 } 5716 if (CurFD) 5717 BaseExpr = S.BuildMemberExpr( 5718 BaseExpr, /*IsArrow=*/false, E->getExprLoc(), 5719 NestedNameSpecifierLoc(), SourceLocation(), CurFD, 5720 DeclAccessPair::make(CurFD, CurFD->getAccess()), 5721 /*HadMultipleCandidates=*/false, DeclarationNameInfo(), 5722 CurFD->getType(), VK_LValue, OK_Ordinary); 5723 SubExprs.push_back(BaseExpr); 5724 continue; 5725 } 5726 // Check for the "default" mapper for data members. 5727 bool FirstIter = true; 5728 for (FieldDecl *FD : RD->fields()) { 5729 if (!FD) 5730 continue; 5731 QualType FieldTy = FD->getType(); 5732 if (FieldTy.isNull() || 5733 !(FieldTy->isStructureOrClassType() || FieldTy->isUnionType())) 5734 continue; 5735 if (FirstIter) { 5736 FirstIter = false; 5737 ParentChain.emplace_back(CurFD, 1); 5738 } else { 5739 ++ParentChain.back().second; 5740 } 5741 Types.emplace_back(FieldTy, FD); 5742 } 5743 } 5744 } 5745 if (SubExprs.empty()) 5746 continue; 5747 CXXScopeSpec MapperIdScopeSpec; 5748 DeclarationNameInfo MapperId; 5749 if (OMPClause *NewClause = S.ActOnOpenMPMapClause( 5750 C->getMapTypeModifiers(), C->getMapTypeModifiersLoc(), 5751 MapperIdScopeSpec, MapperId, C->getMapType(), 5752 /*IsMapTypeImplicit=*/true, SourceLocation(), SourceLocation(), 5753 SubExprs, OMPVarListLocTy())) 5754 Clauses.push_back(NewClause); 5755 } 5756 } 5757 5758 StmtResult Sema::ActOnOpenMPExecutableDirective( 5759 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName, 5760 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses, 5761 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { 5762 StmtResult Res = StmtError(); 5763 // First check CancelRegion which is then used in checkNestingOfRegions. 5764 if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) || 5765 checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion, 5766 StartLoc)) 5767 return StmtError(); 5768 5769 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit; 5770 VarsWithInheritedDSAType VarsWithInheritedDSA; 5771 bool ErrorFound = false; 5772 ClausesWithImplicit.append(Clauses.begin(), Clauses.end()); 5773 if (AStmt && !CurContext->isDependentContext() && Kind != OMPD_atomic && 5774 Kind != OMPD_critical && Kind != OMPD_section && Kind != OMPD_master && 5775 Kind != OMPD_masked && !isOpenMPLoopTransformationDirective(Kind)) { 5776 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5777 5778 // Check default data sharing attributes for referenced variables. 5779 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt)); 5780 int ThisCaptureLevel = getOpenMPCaptureLevels(Kind); 5781 Stmt *S = AStmt; 5782 while (--ThisCaptureLevel >= 0) 5783 S = cast<CapturedStmt>(S)->getCapturedStmt(); 5784 DSAChecker.Visit(S); 5785 if (!isOpenMPTargetDataManagementDirective(Kind) && 5786 !isOpenMPTaskingDirective(Kind)) { 5787 // Visit subcaptures to generate implicit clauses for captured vars. 5788 auto *CS = cast<CapturedStmt>(AStmt); 5789 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 5790 getOpenMPCaptureRegions(CaptureRegions, Kind); 5791 // Ignore outer tasking regions for target directives. 5792 if (CaptureRegions.size() > 1 && CaptureRegions.front() == OMPD_task) 5793 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 5794 DSAChecker.visitSubCaptures(CS); 5795 } 5796 if (DSAChecker.isErrorFound()) 5797 return StmtError(); 5798 // Generate list of implicitly defined firstprivate variables. 5799 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA(); 5800 5801 SmallVector<Expr *, 4> ImplicitFirstprivates( 5802 DSAChecker.getImplicitFirstprivate().begin(), 5803 DSAChecker.getImplicitFirstprivate().end()); 5804 const unsigned DefaultmapKindNum = OMPC_DEFAULTMAP_pointer + 1; 5805 SmallVector<Expr *, 4> ImplicitMaps[DefaultmapKindNum][OMPC_MAP_delete]; 5806 SmallVector<OpenMPMapModifierKind, NumberOfOMPMapClauseModifiers> 5807 ImplicitMapModifiers[DefaultmapKindNum]; 5808 SmallVector<SourceLocation, NumberOfOMPMapClauseModifiers> 5809 ImplicitMapModifiersLoc[DefaultmapKindNum]; 5810 // Get the original location of present modifier from Defaultmap clause. 5811 SourceLocation PresentModifierLocs[DefaultmapKindNum]; 5812 for (OMPClause *C : Clauses) { 5813 if (auto *DMC = dyn_cast<OMPDefaultmapClause>(C)) 5814 if (DMC->getDefaultmapModifier() == OMPC_DEFAULTMAP_MODIFIER_present) 5815 PresentModifierLocs[DMC->getDefaultmapKind()] = 5816 DMC->getDefaultmapModifierLoc(); 5817 } 5818 for (unsigned VC = 0; VC < DefaultmapKindNum; ++VC) { 5819 auto Kind = static_cast<OpenMPDefaultmapClauseKind>(VC); 5820 for (unsigned I = 0; I < OMPC_MAP_delete; ++I) { 5821 ArrayRef<Expr *> ImplicitMap = DSAChecker.getImplicitMap( 5822 Kind, static_cast<OpenMPMapClauseKind>(I)); 5823 ImplicitMaps[VC][I].append(ImplicitMap.begin(), ImplicitMap.end()); 5824 } 5825 ArrayRef<OpenMPMapModifierKind> ImplicitModifier = 5826 DSAChecker.getImplicitMapModifier(Kind); 5827 ImplicitMapModifiers[VC].append(ImplicitModifier.begin(), 5828 ImplicitModifier.end()); 5829 std::fill_n(std::back_inserter(ImplicitMapModifiersLoc[VC]), 5830 ImplicitModifier.size(), PresentModifierLocs[VC]); 5831 } 5832 // Mark taskgroup task_reduction descriptors as implicitly firstprivate. 5833 for (OMPClause *C : Clauses) { 5834 if (auto *IRC = dyn_cast<OMPInReductionClause>(C)) { 5835 for (Expr *E : IRC->taskgroup_descriptors()) 5836 if (E) 5837 ImplicitFirstprivates.emplace_back(E); 5838 } 5839 // OpenMP 5.0, 2.10.1 task Construct 5840 // [detach clause]... The event-handle will be considered as if it was 5841 // specified on a firstprivate clause. 5842 if (auto *DC = dyn_cast<OMPDetachClause>(C)) 5843 ImplicitFirstprivates.push_back(DC->getEventHandler()); 5844 } 5845 if (!ImplicitFirstprivates.empty()) { 5846 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause( 5847 ImplicitFirstprivates, SourceLocation(), SourceLocation(), 5848 SourceLocation())) { 5849 ClausesWithImplicit.push_back(Implicit); 5850 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() != 5851 ImplicitFirstprivates.size(); 5852 } else { 5853 ErrorFound = true; 5854 } 5855 } 5856 // OpenMP 5.0 [2.19.7] 5857 // If a list item appears in a reduction, lastprivate or linear 5858 // clause on a combined target construct then it is treated as 5859 // if it also appears in a map clause with a map-type of tofrom 5860 if (getLangOpts().OpenMP >= 50 && Kind != OMPD_target && 5861 isOpenMPTargetExecutionDirective(Kind)) { 5862 SmallVector<Expr *, 4> ImplicitExprs; 5863 for (OMPClause *C : Clauses) { 5864 if (auto *RC = dyn_cast<OMPReductionClause>(C)) 5865 for (Expr *E : RC->varlists()) 5866 if (!isa<DeclRefExpr>(E->IgnoreParenImpCasts())) 5867 ImplicitExprs.emplace_back(E); 5868 } 5869 if (!ImplicitExprs.empty()) { 5870 ArrayRef<Expr *> Exprs = ImplicitExprs; 5871 CXXScopeSpec MapperIdScopeSpec; 5872 DeclarationNameInfo MapperId; 5873 if (OMPClause *Implicit = ActOnOpenMPMapClause( 5874 OMPC_MAP_MODIFIER_unknown, SourceLocation(), MapperIdScopeSpec, 5875 MapperId, OMPC_MAP_tofrom, 5876 /*IsMapTypeImplicit=*/true, SourceLocation(), SourceLocation(), 5877 Exprs, OMPVarListLocTy(), /*NoDiagnose=*/true)) 5878 ClausesWithImplicit.emplace_back(Implicit); 5879 } 5880 } 5881 for (unsigned I = 0, E = DefaultmapKindNum; I < E; ++I) { 5882 int ClauseKindCnt = -1; 5883 for (ArrayRef<Expr *> ImplicitMap : ImplicitMaps[I]) { 5884 ++ClauseKindCnt; 5885 if (ImplicitMap.empty()) 5886 continue; 5887 CXXScopeSpec MapperIdScopeSpec; 5888 DeclarationNameInfo MapperId; 5889 auto Kind = static_cast<OpenMPMapClauseKind>(ClauseKindCnt); 5890 if (OMPClause *Implicit = ActOnOpenMPMapClause( 5891 ImplicitMapModifiers[I], ImplicitMapModifiersLoc[I], 5892 MapperIdScopeSpec, MapperId, Kind, /*IsMapTypeImplicit=*/true, 5893 SourceLocation(), SourceLocation(), ImplicitMap, 5894 OMPVarListLocTy())) { 5895 ClausesWithImplicit.emplace_back(Implicit); 5896 ErrorFound |= cast<OMPMapClause>(Implicit)->varlist_size() != 5897 ImplicitMap.size(); 5898 } else { 5899 ErrorFound = true; 5900 } 5901 } 5902 } 5903 // Build expressions for implicit maps of data members with 'default' 5904 // mappers. 5905 if (LangOpts.OpenMP >= 50) 5906 processImplicitMapsWithDefaultMappers(*this, DSAStack, 5907 ClausesWithImplicit); 5908 } 5909 5910 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers; 5911 switch (Kind) { 5912 case OMPD_parallel: 5913 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc, 5914 EndLoc); 5915 AllowedNameModifiers.push_back(OMPD_parallel); 5916 break; 5917 case OMPD_simd: 5918 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, 5919 VarsWithInheritedDSA); 5920 if (LangOpts.OpenMP >= 50) 5921 AllowedNameModifiers.push_back(OMPD_simd); 5922 break; 5923 case OMPD_tile: 5924 Res = 5925 ActOnOpenMPTileDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 5926 break; 5927 case OMPD_unroll: 5928 Res = ActOnOpenMPUnrollDirective(ClausesWithImplicit, AStmt, StartLoc, 5929 EndLoc); 5930 break; 5931 case OMPD_for: 5932 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, 5933 VarsWithInheritedDSA); 5934 break; 5935 case OMPD_for_simd: 5936 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 5937 EndLoc, VarsWithInheritedDSA); 5938 if (LangOpts.OpenMP >= 50) 5939 AllowedNameModifiers.push_back(OMPD_simd); 5940 break; 5941 case OMPD_sections: 5942 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc, 5943 EndLoc); 5944 break; 5945 case OMPD_section: 5946 assert(ClausesWithImplicit.empty() && 5947 "No clauses are allowed for 'omp section' directive"); 5948 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc); 5949 break; 5950 case OMPD_single: 5951 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc, 5952 EndLoc); 5953 break; 5954 case OMPD_master: 5955 assert(ClausesWithImplicit.empty() && 5956 "No clauses are allowed for 'omp master' directive"); 5957 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc); 5958 break; 5959 case OMPD_masked: 5960 Res = ActOnOpenMPMaskedDirective(ClausesWithImplicit, AStmt, StartLoc, 5961 EndLoc); 5962 break; 5963 case OMPD_critical: 5964 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt, 5965 StartLoc, EndLoc); 5966 break; 5967 case OMPD_parallel_for: 5968 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc, 5969 EndLoc, VarsWithInheritedDSA); 5970 AllowedNameModifiers.push_back(OMPD_parallel); 5971 break; 5972 case OMPD_parallel_for_simd: 5973 Res = ActOnOpenMPParallelForSimdDirective( 5974 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5975 AllowedNameModifiers.push_back(OMPD_parallel); 5976 if (LangOpts.OpenMP >= 50) 5977 AllowedNameModifiers.push_back(OMPD_simd); 5978 break; 5979 case OMPD_parallel_master: 5980 Res = ActOnOpenMPParallelMasterDirective(ClausesWithImplicit, AStmt, 5981 StartLoc, EndLoc); 5982 AllowedNameModifiers.push_back(OMPD_parallel); 5983 break; 5984 case OMPD_parallel_sections: 5985 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt, 5986 StartLoc, EndLoc); 5987 AllowedNameModifiers.push_back(OMPD_parallel); 5988 break; 5989 case OMPD_task: 5990 Res = 5991 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 5992 AllowedNameModifiers.push_back(OMPD_task); 5993 break; 5994 case OMPD_taskyield: 5995 assert(ClausesWithImplicit.empty() && 5996 "No clauses are allowed for 'omp taskyield' directive"); 5997 assert(AStmt == nullptr && 5998 "No associated statement allowed for 'omp taskyield' directive"); 5999 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc); 6000 break; 6001 case OMPD_barrier: 6002 assert(ClausesWithImplicit.empty() && 6003 "No clauses are allowed for 'omp barrier' directive"); 6004 assert(AStmt == nullptr && 6005 "No associated statement allowed for 'omp barrier' directive"); 6006 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc); 6007 break; 6008 case OMPD_taskwait: 6009 assert(ClausesWithImplicit.empty() && 6010 "No clauses are allowed for 'omp taskwait' directive"); 6011 assert(AStmt == nullptr && 6012 "No associated statement allowed for 'omp taskwait' directive"); 6013 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc); 6014 break; 6015 case OMPD_taskgroup: 6016 Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc, 6017 EndLoc); 6018 break; 6019 case OMPD_flush: 6020 assert(AStmt == nullptr && 6021 "No associated statement allowed for 'omp flush' directive"); 6022 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc); 6023 break; 6024 case OMPD_depobj: 6025 assert(AStmt == nullptr && 6026 "No associated statement allowed for 'omp depobj' directive"); 6027 Res = ActOnOpenMPDepobjDirective(ClausesWithImplicit, StartLoc, EndLoc); 6028 break; 6029 case OMPD_scan: 6030 assert(AStmt == nullptr && 6031 "No associated statement allowed for 'omp scan' directive"); 6032 Res = ActOnOpenMPScanDirective(ClausesWithImplicit, StartLoc, EndLoc); 6033 break; 6034 case OMPD_ordered: 6035 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc, 6036 EndLoc); 6037 break; 6038 case OMPD_atomic: 6039 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc, 6040 EndLoc); 6041 break; 6042 case OMPD_teams: 6043 Res = 6044 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 6045 break; 6046 case OMPD_target: 6047 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc, 6048 EndLoc); 6049 AllowedNameModifiers.push_back(OMPD_target); 6050 break; 6051 case OMPD_target_parallel: 6052 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt, 6053 StartLoc, EndLoc); 6054 AllowedNameModifiers.push_back(OMPD_target); 6055 AllowedNameModifiers.push_back(OMPD_parallel); 6056 break; 6057 case OMPD_target_parallel_for: 6058 Res = ActOnOpenMPTargetParallelForDirective( 6059 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6060 AllowedNameModifiers.push_back(OMPD_target); 6061 AllowedNameModifiers.push_back(OMPD_parallel); 6062 break; 6063 case OMPD_cancellation_point: 6064 assert(ClausesWithImplicit.empty() && 6065 "No clauses are allowed for 'omp cancellation point' directive"); 6066 assert(AStmt == nullptr && "No associated statement allowed for 'omp " 6067 "cancellation point' directive"); 6068 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion); 6069 break; 6070 case OMPD_cancel: 6071 assert(AStmt == nullptr && 6072 "No associated statement allowed for 'omp cancel' directive"); 6073 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc, 6074 CancelRegion); 6075 AllowedNameModifiers.push_back(OMPD_cancel); 6076 break; 6077 case OMPD_target_data: 6078 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc, 6079 EndLoc); 6080 AllowedNameModifiers.push_back(OMPD_target_data); 6081 break; 6082 case OMPD_target_enter_data: 6083 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc, 6084 EndLoc, AStmt); 6085 AllowedNameModifiers.push_back(OMPD_target_enter_data); 6086 break; 6087 case OMPD_target_exit_data: 6088 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc, 6089 EndLoc, AStmt); 6090 AllowedNameModifiers.push_back(OMPD_target_exit_data); 6091 break; 6092 case OMPD_taskloop: 6093 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc, 6094 EndLoc, VarsWithInheritedDSA); 6095 AllowedNameModifiers.push_back(OMPD_taskloop); 6096 break; 6097 case OMPD_taskloop_simd: 6098 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 6099 EndLoc, VarsWithInheritedDSA); 6100 AllowedNameModifiers.push_back(OMPD_taskloop); 6101 if (LangOpts.OpenMP >= 50) 6102 AllowedNameModifiers.push_back(OMPD_simd); 6103 break; 6104 case OMPD_master_taskloop: 6105 Res = ActOnOpenMPMasterTaskLoopDirective( 6106 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6107 AllowedNameModifiers.push_back(OMPD_taskloop); 6108 break; 6109 case OMPD_master_taskloop_simd: 6110 Res = ActOnOpenMPMasterTaskLoopSimdDirective( 6111 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6112 AllowedNameModifiers.push_back(OMPD_taskloop); 6113 if (LangOpts.OpenMP >= 50) 6114 AllowedNameModifiers.push_back(OMPD_simd); 6115 break; 6116 case OMPD_parallel_master_taskloop: 6117 Res = ActOnOpenMPParallelMasterTaskLoopDirective( 6118 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6119 AllowedNameModifiers.push_back(OMPD_taskloop); 6120 AllowedNameModifiers.push_back(OMPD_parallel); 6121 break; 6122 case OMPD_parallel_master_taskloop_simd: 6123 Res = ActOnOpenMPParallelMasterTaskLoopSimdDirective( 6124 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6125 AllowedNameModifiers.push_back(OMPD_taskloop); 6126 AllowedNameModifiers.push_back(OMPD_parallel); 6127 if (LangOpts.OpenMP >= 50) 6128 AllowedNameModifiers.push_back(OMPD_simd); 6129 break; 6130 case OMPD_distribute: 6131 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc, 6132 EndLoc, VarsWithInheritedDSA); 6133 break; 6134 case OMPD_target_update: 6135 Res = ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc, 6136 EndLoc, AStmt); 6137 AllowedNameModifiers.push_back(OMPD_target_update); 6138 break; 6139 case OMPD_distribute_parallel_for: 6140 Res = ActOnOpenMPDistributeParallelForDirective( 6141 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6142 AllowedNameModifiers.push_back(OMPD_parallel); 6143 break; 6144 case OMPD_distribute_parallel_for_simd: 6145 Res = ActOnOpenMPDistributeParallelForSimdDirective( 6146 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6147 AllowedNameModifiers.push_back(OMPD_parallel); 6148 if (LangOpts.OpenMP >= 50) 6149 AllowedNameModifiers.push_back(OMPD_simd); 6150 break; 6151 case OMPD_distribute_simd: 6152 Res = ActOnOpenMPDistributeSimdDirective( 6153 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6154 if (LangOpts.OpenMP >= 50) 6155 AllowedNameModifiers.push_back(OMPD_simd); 6156 break; 6157 case OMPD_target_parallel_for_simd: 6158 Res = ActOnOpenMPTargetParallelForSimdDirective( 6159 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6160 AllowedNameModifiers.push_back(OMPD_target); 6161 AllowedNameModifiers.push_back(OMPD_parallel); 6162 if (LangOpts.OpenMP >= 50) 6163 AllowedNameModifiers.push_back(OMPD_simd); 6164 break; 6165 case OMPD_target_simd: 6166 Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 6167 EndLoc, VarsWithInheritedDSA); 6168 AllowedNameModifiers.push_back(OMPD_target); 6169 if (LangOpts.OpenMP >= 50) 6170 AllowedNameModifiers.push_back(OMPD_simd); 6171 break; 6172 case OMPD_teams_distribute: 6173 Res = ActOnOpenMPTeamsDistributeDirective( 6174 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6175 break; 6176 case OMPD_teams_distribute_simd: 6177 Res = ActOnOpenMPTeamsDistributeSimdDirective( 6178 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6179 if (LangOpts.OpenMP >= 50) 6180 AllowedNameModifiers.push_back(OMPD_simd); 6181 break; 6182 case OMPD_teams_distribute_parallel_for_simd: 6183 Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective( 6184 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6185 AllowedNameModifiers.push_back(OMPD_parallel); 6186 if (LangOpts.OpenMP >= 50) 6187 AllowedNameModifiers.push_back(OMPD_simd); 6188 break; 6189 case OMPD_teams_distribute_parallel_for: 6190 Res = ActOnOpenMPTeamsDistributeParallelForDirective( 6191 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6192 AllowedNameModifiers.push_back(OMPD_parallel); 6193 break; 6194 case OMPD_target_teams: 6195 Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, 6196 EndLoc); 6197 AllowedNameModifiers.push_back(OMPD_target); 6198 break; 6199 case OMPD_target_teams_distribute: 6200 Res = ActOnOpenMPTargetTeamsDistributeDirective( 6201 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6202 AllowedNameModifiers.push_back(OMPD_target); 6203 break; 6204 case OMPD_target_teams_distribute_parallel_for: 6205 Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective( 6206 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6207 AllowedNameModifiers.push_back(OMPD_target); 6208 AllowedNameModifiers.push_back(OMPD_parallel); 6209 break; 6210 case OMPD_target_teams_distribute_parallel_for_simd: 6211 Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective( 6212 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6213 AllowedNameModifiers.push_back(OMPD_target); 6214 AllowedNameModifiers.push_back(OMPD_parallel); 6215 if (LangOpts.OpenMP >= 50) 6216 AllowedNameModifiers.push_back(OMPD_simd); 6217 break; 6218 case OMPD_target_teams_distribute_simd: 6219 Res = ActOnOpenMPTargetTeamsDistributeSimdDirective( 6220 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6221 AllowedNameModifiers.push_back(OMPD_target); 6222 if (LangOpts.OpenMP >= 50) 6223 AllowedNameModifiers.push_back(OMPD_simd); 6224 break; 6225 case OMPD_interop: 6226 assert(AStmt == nullptr && 6227 "No associated statement allowed for 'omp interop' directive"); 6228 Res = ActOnOpenMPInteropDirective(ClausesWithImplicit, StartLoc, EndLoc); 6229 break; 6230 case OMPD_dispatch: 6231 Res = ActOnOpenMPDispatchDirective(ClausesWithImplicit, AStmt, StartLoc, 6232 EndLoc); 6233 break; 6234 case OMPD_declare_target: 6235 case OMPD_end_declare_target: 6236 case OMPD_threadprivate: 6237 case OMPD_allocate: 6238 case OMPD_declare_reduction: 6239 case OMPD_declare_mapper: 6240 case OMPD_declare_simd: 6241 case OMPD_requires: 6242 case OMPD_declare_variant: 6243 case OMPD_begin_declare_variant: 6244 case OMPD_end_declare_variant: 6245 llvm_unreachable("OpenMP Directive is not allowed"); 6246 case OMPD_unknown: 6247 default: 6248 llvm_unreachable("Unknown OpenMP directive"); 6249 } 6250 6251 ErrorFound = Res.isInvalid() || ErrorFound; 6252 6253 // Check variables in the clauses if default(none) or 6254 // default(firstprivate) was specified. 6255 if (DSAStack->getDefaultDSA() == DSA_none || 6256 DSAStack->getDefaultDSA() == DSA_firstprivate) { 6257 DSAAttrChecker DSAChecker(DSAStack, *this, nullptr); 6258 for (OMPClause *C : Clauses) { 6259 switch (C->getClauseKind()) { 6260 case OMPC_num_threads: 6261 case OMPC_dist_schedule: 6262 // Do not analyse if no parent teams directive. 6263 if (isOpenMPTeamsDirective(Kind)) 6264 break; 6265 continue; 6266 case OMPC_if: 6267 if (isOpenMPTeamsDirective(Kind) && 6268 cast<OMPIfClause>(C)->getNameModifier() != OMPD_target) 6269 break; 6270 if (isOpenMPParallelDirective(Kind) && 6271 isOpenMPTaskLoopDirective(Kind) && 6272 cast<OMPIfClause>(C)->getNameModifier() != OMPD_parallel) 6273 break; 6274 continue; 6275 case OMPC_schedule: 6276 case OMPC_detach: 6277 break; 6278 case OMPC_grainsize: 6279 case OMPC_num_tasks: 6280 case OMPC_final: 6281 case OMPC_priority: 6282 case OMPC_novariants: 6283 case OMPC_nocontext: 6284 // Do not analyze if no parent parallel directive. 6285 if (isOpenMPParallelDirective(Kind)) 6286 break; 6287 continue; 6288 case OMPC_ordered: 6289 case OMPC_device: 6290 case OMPC_num_teams: 6291 case OMPC_thread_limit: 6292 case OMPC_hint: 6293 case OMPC_collapse: 6294 case OMPC_safelen: 6295 case OMPC_simdlen: 6296 case OMPC_sizes: 6297 case OMPC_default: 6298 case OMPC_proc_bind: 6299 case OMPC_private: 6300 case OMPC_firstprivate: 6301 case OMPC_lastprivate: 6302 case OMPC_shared: 6303 case OMPC_reduction: 6304 case OMPC_task_reduction: 6305 case OMPC_in_reduction: 6306 case OMPC_linear: 6307 case OMPC_aligned: 6308 case OMPC_copyin: 6309 case OMPC_copyprivate: 6310 case OMPC_nowait: 6311 case OMPC_untied: 6312 case OMPC_mergeable: 6313 case OMPC_allocate: 6314 case OMPC_read: 6315 case OMPC_write: 6316 case OMPC_update: 6317 case OMPC_capture: 6318 case OMPC_seq_cst: 6319 case OMPC_acq_rel: 6320 case OMPC_acquire: 6321 case OMPC_release: 6322 case OMPC_relaxed: 6323 case OMPC_depend: 6324 case OMPC_threads: 6325 case OMPC_simd: 6326 case OMPC_map: 6327 case OMPC_nogroup: 6328 case OMPC_defaultmap: 6329 case OMPC_to: 6330 case OMPC_from: 6331 case OMPC_use_device_ptr: 6332 case OMPC_use_device_addr: 6333 case OMPC_is_device_ptr: 6334 case OMPC_nontemporal: 6335 case OMPC_order: 6336 case OMPC_destroy: 6337 case OMPC_inclusive: 6338 case OMPC_exclusive: 6339 case OMPC_uses_allocators: 6340 case OMPC_affinity: 6341 continue; 6342 case OMPC_allocator: 6343 case OMPC_flush: 6344 case OMPC_depobj: 6345 case OMPC_threadprivate: 6346 case OMPC_uniform: 6347 case OMPC_unknown: 6348 case OMPC_unified_address: 6349 case OMPC_unified_shared_memory: 6350 case OMPC_reverse_offload: 6351 case OMPC_dynamic_allocators: 6352 case OMPC_atomic_default_mem_order: 6353 case OMPC_device_type: 6354 case OMPC_match: 6355 case OMPC_when: 6356 default: 6357 llvm_unreachable("Unexpected clause"); 6358 } 6359 for (Stmt *CC : C->children()) { 6360 if (CC) 6361 DSAChecker.Visit(CC); 6362 } 6363 } 6364 for (const auto &P : DSAChecker.getVarsWithInheritedDSA()) 6365 VarsWithInheritedDSA[P.getFirst()] = P.getSecond(); 6366 } 6367 for (const auto &P : VarsWithInheritedDSA) { 6368 if (P.getFirst()->isImplicit() || isa<OMPCapturedExprDecl>(P.getFirst())) 6369 continue; 6370 ErrorFound = true; 6371 if (DSAStack->getDefaultDSA() == DSA_none || 6372 DSAStack->getDefaultDSA() == DSA_firstprivate) { 6373 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable) 6374 << P.first << P.second->getSourceRange(); 6375 Diag(DSAStack->getDefaultDSALocation(), diag::note_omp_default_dsa_none); 6376 } else if (getLangOpts().OpenMP >= 50) { 6377 Diag(P.second->getExprLoc(), 6378 diag::err_omp_defaultmap_no_attr_for_variable) 6379 << P.first << P.second->getSourceRange(); 6380 Diag(DSAStack->getDefaultDSALocation(), 6381 diag::note_omp_defaultmap_attr_none); 6382 } 6383 } 6384 6385 if (!AllowedNameModifiers.empty()) 6386 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) || 6387 ErrorFound; 6388 6389 if (ErrorFound) 6390 return StmtError(); 6391 6392 if (!CurContext->isDependentContext() && 6393 isOpenMPTargetExecutionDirective(Kind) && 6394 !(DSAStack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() || 6395 DSAStack->hasRequiresDeclWithClause<OMPUnifiedAddressClause>() || 6396 DSAStack->hasRequiresDeclWithClause<OMPReverseOffloadClause>() || 6397 DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())) { 6398 // Register target to DSA Stack. 6399 DSAStack->addTargetDirLocation(StartLoc); 6400 } 6401 6402 return Res; 6403 } 6404 6405 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective( 6406 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen, 6407 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds, 6408 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears, 6409 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) { 6410 assert(Aligneds.size() == Alignments.size()); 6411 assert(Linears.size() == LinModifiers.size()); 6412 assert(Linears.size() == Steps.size()); 6413 if (!DG || DG.get().isNull()) 6414 return DeclGroupPtrTy(); 6415 6416 const int SimdId = 0; 6417 if (!DG.get().isSingleDecl()) { 6418 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd_variant) 6419 << SimdId; 6420 return DG; 6421 } 6422 Decl *ADecl = DG.get().getSingleDecl(); 6423 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl)) 6424 ADecl = FTD->getTemplatedDecl(); 6425 6426 auto *FD = dyn_cast<FunctionDecl>(ADecl); 6427 if (!FD) { 6428 Diag(ADecl->getLocation(), diag::err_omp_function_expected) << SimdId; 6429 return DeclGroupPtrTy(); 6430 } 6431 6432 // OpenMP [2.8.2, declare simd construct, Description] 6433 // The parameter of the simdlen clause must be a constant positive integer 6434 // expression. 6435 ExprResult SL; 6436 if (Simdlen) 6437 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen); 6438 // OpenMP [2.8.2, declare simd construct, Description] 6439 // The special this pointer can be used as if was one of the arguments to the 6440 // function in any of the linear, aligned, or uniform clauses. 6441 // The uniform clause declares one or more arguments to have an invariant 6442 // value for all concurrent invocations of the function in the execution of a 6443 // single SIMD loop. 6444 llvm::DenseMap<const Decl *, const Expr *> UniformedArgs; 6445 const Expr *UniformedLinearThis = nullptr; 6446 for (const Expr *E : Uniforms) { 6447 E = E->IgnoreParenImpCasts(); 6448 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 6449 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) 6450 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 6451 FD->getParamDecl(PVD->getFunctionScopeIndex()) 6452 ->getCanonicalDecl() == PVD->getCanonicalDecl()) { 6453 UniformedArgs.try_emplace(PVD->getCanonicalDecl(), E); 6454 continue; 6455 } 6456 if (isa<CXXThisExpr>(E)) { 6457 UniformedLinearThis = E; 6458 continue; 6459 } 6460 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 6461 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 6462 } 6463 // OpenMP [2.8.2, declare simd construct, Description] 6464 // The aligned clause declares that the object to which each list item points 6465 // is aligned to the number of bytes expressed in the optional parameter of 6466 // the aligned clause. 6467 // The special this pointer can be used as if was one of the arguments to the 6468 // function in any of the linear, aligned, or uniform clauses. 6469 // The type of list items appearing in the aligned clause must be array, 6470 // pointer, reference to array, or reference to pointer. 6471 llvm::DenseMap<const Decl *, const Expr *> AlignedArgs; 6472 const Expr *AlignedThis = nullptr; 6473 for (const Expr *E : Aligneds) { 6474 E = E->IgnoreParenImpCasts(); 6475 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 6476 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 6477 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 6478 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 6479 FD->getParamDecl(PVD->getFunctionScopeIndex()) 6480 ->getCanonicalDecl() == CanonPVD) { 6481 // OpenMP [2.8.1, simd construct, Restrictions] 6482 // A list-item cannot appear in more than one aligned clause. 6483 if (AlignedArgs.count(CanonPVD) > 0) { 6484 Diag(E->getExprLoc(), diag::err_omp_used_in_clause_twice) 6485 << 1 << getOpenMPClauseName(OMPC_aligned) 6486 << E->getSourceRange(); 6487 Diag(AlignedArgs[CanonPVD]->getExprLoc(), 6488 diag::note_omp_explicit_dsa) 6489 << getOpenMPClauseName(OMPC_aligned); 6490 continue; 6491 } 6492 AlignedArgs[CanonPVD] = E; 6493 QualType QTy = PVD->getType() 6494 .getNonReferenceType() 6495 .getUnqualifiedType() 6496 .getCanonicalType(); 6497 const Type *Ty = QTy.getTypePtrOrNull(); 6498 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) { 6499 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr) 6500 << QTy << getLangOpts().CPlusPlus << E->getSourceRange(); 6501 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD; 6502 } 6503 continue; 6504 } 6505 } 6506 if (isa<CXXThisExpr>(E)) { 6507 if (AlignedThis) { 6508 Diag(E->getExprLoc(), diag::err_omp_used_in_clause_twice) 6509 << 2 << getOpenMPClauseName(OMPC_aligned) << E->getSourceRange(); 6510 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa) 6511 << getOpenMPClauseName(OMPC_aligned); 6512 } 6513 AlignedThis = E; 6514 continue; 6515 } 6516 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 6517 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 6518 } 6519 // The optional parameter of the aligned clause, alignment, must be a constant 6520 // positive integer expression. If no optional parameter is specified, 6521 // implementation-defined default alignments for SIMD instructions on the 6522 // target platforms are assumed. 6523 SmallVector<const Expr *, 4> NewAligns; 6524 for (Expr *E : Alignments) { 6525 ExprResult Align; 6526 if (E) 6527 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned); 6528 NewAligns.push_back(Align.get()); 6529 } 6530 // OpenMP [2.8.2, declare simd construct, Description] 6531 // The linear clause declares one or more list items to be private to a SIMD 6532 // lane and to have a linear relationship with respect to the iteration space 6533 // of a loop. 6534 // The special this pointer can be used as if was one of the arguments to the 6535 // function in any of the linear, aligned, or uniform clauses. 6536 // When a linear-step expression is specified in a linear clause it must be 6537 // either a constant integer expression or an integer-typed parameter that is 6538 // specified in a uniform clause on the directive. 6539 llvm::DenseMap<const Decl *, const Expr *> LinearArgs; 6540 const bool IsUniformedThis = UniformedLinearThis != nullptr; 6541 auto MI = LinModifiers.begin(); 6542 for (const Expr *E : Linears) { 6543 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI); 6544 ++MI; 6545 E = E->IgnoreParenImpCasts(); 6546 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 6547 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 6548 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 6549 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 6550 FD->getParamDecl(PVD->getFunctionScopeIndex()) 6551 ->getCanonicalDecl() == CanonPVD) { 6552 // OpenMP [2.15.3.7, linear Clause, Restrictions] 6553 // A list-item cannot appear in more than one linear clause. 6554 if (LinearArgs.count(CanonPVD) > 0) { 6555 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 6556 << getOpenMPClauseName(OMPC_linear) 6557 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange(); 6558 Diag(LinearArgs[CanonPVD]->getExprLoc(), 6559 diag::note_omp_explicit_dsa) 6560 << getOpenMPClauseName(OMPC_linear); 6561 continue; 6562 } 6563 // Each argument can appear in at most one uniform or linear clause. 6564 if (UniformedArgs.count(CanonPVD) > 0) { 6565 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 6566 << getOpenMPClauseName(OMPC_linear) 6567 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange(); 6568 Diag(UniformedArgs[CanonPVD]->getExprLoc(), 6569 diag::note_omp_explicit_dsa) 6570 << getOpenMPClauseName(OMPC_uniform); 6571 continue; 6572 } 6573 LinearArgs[CanonPVD] = E; 6574 if (E->isValueDependent() || E->isTypeDependent() || 6575 E->isInstantiationDependent() || 6576 E->containsUnexpandedParameterPack()) 6577 continue; 6578 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind, 6579 PVD->getOriginalType(), 6580 /*IsDeclareSimd=*/true); 6581 continue; 6582 } 6583 } 6584 if (isa<CXXThisExpr>(E)) { 6585 if (UniformedLinearThis) { 6586 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 6587 << getOpenMPClauseName(OMPC_linear) 6588 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear) 6589 << E->getSourceRange(); 6590 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa) 6591 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform 6592 : OMPC_linear); 6593 continue; 6594 } 6595 UniformedLinearThis = E; 6596 if (E->isValueDependent() || E->isTypeDependent() || 6597 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 6598 continue; 6599 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind, 6600 E->getType(), /*IsDeclareSimd=*/true); 6601 continue; 6602 } 6603 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 6604 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 6605 } 6606 Expr *Step = nullptr; 6607 Expr *NewStep = nullptr; 6608 SmallVector<Expr *, 4> NewSteps; 6609 for (Expr *E : Steps) { 6610 // Skip the same step expression, it was checked already. 6611 if (Step == E || !E) { 6612 NewSteps.push_back(E ? NewStep : nullptr); 6613 continue; 6614 } 6615 Step = E; 6616 if (const auto *DRE = dyn_cast<DeclRefExpr>(Step)) 6617 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 6618 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 6619 if (UniformedArgs.count(CanonPVD) == 0) { 6620 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param) 6621 << Step->getSourceRange(); 6622 } else if (E->isValueDependent() || E->isTypeDependent() || 6623 E->isInstantiationDependent() || 6624 E->containsUnexpandedParameterPack() || 6625 CanonPVD->getType()->hasIntegerRepresentation()) { 6626 NewSteps.push_back(Step); 6627 } else { 6628 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param) 6629 << Step->getSourceRange(); 6630 } 6631 continue; 6632 } 6633 NewStep = Step; 6634 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && 6635 !Step->isInstantiationDependent() && 6636 !Step->containsUnexpandedParameterPack()) { 6637 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step) 6638 .get(); 6639 if (NewStep) 6640 NewStep = 6641 VerifyIntegerConstantExpression(NewStep, /*FIXME*/ AllowFold).get(); 6642 } 6643 NewSteps.push_back(NewStep); 6644 } 6645 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit( 6646 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()), 6647 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(), 6648 const_cast<Expr **>(NewAligns.data()), NewAligns.size(), 6649 const_cast<Expr **>(Linears.data()), Linears.size(), 6650 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(), 6651 NewSteps.data(), NewSteps.size(), SR); 6652 ADecl->addAttr(NewAttr); 6653 return DG; 6654 } 6655 6656 static void setPrototype(Sema &S, FunctionDecl *FD, FunctionDecl *FDWithProto, 6657 QualType NewType) { 6658 assert(NewType->isFunctionProtoType() && 6659 "Expected function type with prototype."); 6660 assert(FD->getType()->isFunctionNoProtoType() && 6661 "Expected function with type with no prototype."); 6662 assert(FDWithProto->getType()->isFunctionProtoType() && 6663 "Expected function with prototype."); 6664 // Synthesize parameters with the same types. 6665 FD->setType(NewType); 6666 SmallVector<ParmVarDecl *, 16> Params; 6667 for (const ParmVarDecl *P : FDWithProto->parameters()) { 6668 auto *Param = ParmVarDecl::Create(S.getASTContext(), FD, SourceLocation(), 6669 SourceLocation(), nullptr, P->getType(), 6670 /*TInfo=*/nullptr, SC_None, nullptr); 6671 Param->setScopeInfo(0, Params.size()); 6672 Param->setImplicit(); 6673 Params.push_back(Param); 6674 } 6675 6676 FD->setParams(Params); 6677 } 6678 6679 void Sema::ActOnFinishedFunctionDefinitionInOpenMPAssumeScope(Decl *D) { 6680 if (D->isInvalidDecl()) 6681 return; 6682 FunctionDecl *FD = nullptr; 6683 if (auto *UTemplDecl = dyn_cast<FunctionTemplateDecl>(D)) 6684 FD = UTemplDecl->getTemplatedDecl(); 6685 else 6686 FD = cast<FunctionDecl>(D); 6687 assert(FD && "Expected a function declaration!"); 6688 6689 // If we are instantiating templates we do *not* apply scoped assumptions but 6690 // only global ones. We apply scoped assumption to the template definition 6691 // though. 6692 if (!inTemplateInstantiation()) { 6693 for (AssumptionAttr *AA : OMPAssumeScoped) 6694 FD->addAttr(AA); 6695 } 6696 for (AssumptionAttr *AA : OMPAssumeGlobal) 6697 FD->addAttr(AA); 6698 } 6699 6700 Sema::OMPDeclareVariantScope::OMPDeclareVariantScope(OMPTraitInfo &TI) 6701 : TI(&TI), NameSuffix(TI.getMangledName()) {} 6702 6703 void Sema::ActOnStartOfFunctionDefinitionInOpenMPDeclareVariantScope( 6704 Scope *S, Declarator &D, MultiTemplateParamsArg TemplateParamLists, 6705 SmallVectorImpl<FunctionDecl *> &Bases) { 6706 if (!D.getIdentifier()) 6707 return; 6708 6709 OMPDeclareVariantScope &DVScope = OMPDeclareVariantScopes.back(); 6710 6711 // Template specialization is an extension, check if we do it. 6712 bool IsTemplated = !TemplateParamLists.empty(); 6713 if (IsTemplated & 6714 !DVScope.TI->isExtensionActive( 6715 llvm::omp::TraitProperty::implementation_extension_allow_templates)) 6716 return; 6717 6718 IdentifierInfo *BaseII = D.getIdentifier(); 6719 LookupResult Lookup(*this, DeclarationName(BaseII), D.getIdentifierLoc(), 6720 LookupOrdinaryName); 6721 LookupParsedName(Lookup, S, &D.getCXXScopeSpec()); 6722 6723 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); 6724 QualType FType = TInfo->getType(); 6725 6726 bool IsConstexpr = 6727 D.getDeclSpec().getConstexprSpecifier() == ConstexprSpecKind::Constexpr; 6728 bool IsConsteval = 6729 D.getDeclSpec().getConstexprSpecifier() == ConstexprSpecKind::Consteval; 6730 6731 for (auto *Candidate : Lookup) { 6732 auto *CandidateDecl = Candidate->getUnderlyingDecl(); 6733 FunctionDecl *UDecl = nullptr; 6734 if (IsTemplated && isa<FunctionTemplateDecl>(CandidateDecl)) 6735 UDecl = cast<FunctionTemplateDecl>(CandidateDecl)->getTemplatedDecl(); 6736 else if (!IsTemplated) 6737 UDecl = dyn_cast<FunctionDecl>(CandidateDecl); 6738 if (!UDecl) 6739 continue; 6740 6741 // Don't specialize constexpr/consteval functions with 6742 // non-constexpr/consteval functions. 6743 if (UDecl->isConstexpr() && !IsConstexpr) 6744 continue; 6745 if (UDecl->isConsteval() && !IsConsteval) 6746 continue; 6747 6748 QualType UDeclTy = UDecl->getType(); 6749 if (!UDeclTy->isDependentType()) { 6750 QualType NewType = Context.mergeFunctionTypes( 6751 FType, UDeclTy, /* OfBlockPointer */ false, 6752 /* Unqualified */ false, /* AllowCXX */ true); 6753 if (NewType.isNull()) 6754 continue; 6755 } 6756 6757 // Found a base! 6758 Bases.push_back(UDecl); 6759 } 6760 6761 bool UseImplicitBase = !DVScope.TI->isExtensionActive( 6762 llvm::omp::TraitProperty::implementation_extension_disable_implicit_base); 6763 // If no base was found we create a declaration that we use as base. 6764 if (Bases.empty() && UseImplicitBase) { 6765 D.setFunctionDefinitionKind(FunctionDefinitionKind::Declaration); 6766 Decl *BaseD = HandleDeclarator(S, D, TemplateParamLists); 6767 BaseD->setImplicit(true); 6768 if (auto *BaseTemplD = dyn_cast<FunctionTemplateDecl>(BaseD)) 6769 Bases.push_back(BaseTemplD->getTemplatedDecl()); 6770 else 6771 Bases.push_back(cast<FunctionDecl>(BaseD)); 6772 } 6773 6774 std::string MangledName; 6775 MangledName += D.getIdentifier()->getName(); 6776 MangledName += getOpenMPVariantManglingSeparatorStr(); 6777 MangledName += DVScope.NameSuffix; 6778 IdentifierInfo &VariantII = Context.Idents.get(MangledName); 6779 6780 VariantII.setMangledOpenMPVariantName(true); 6781 D.SetIdentifier(&VariantII, D.getBeginLoc()); 6782 } 6783 6784 void Sema::ActOnFinishedFunctionDefinitionInOpenMPDeclareVariantScope( 6785 Decl *D, SmallVectorImpl<FunctionDecl *> &Bases) { 6786 // Do not mark function as is used to prevent its emission if this is the 6787 // only place where it is used. 6788 EnterExpressionEvaluationContext Unevaluated( 6789 *this, Sema::ExpressionEvaluationContext::Unevaluated); 6790 6791 FunctionDecl *FD = nullptr; 6792 if (auto *UTemplDecl = dyn_cast<FunctionTemplateDecl>(D)) 6793 FD = UTemplDecl->getTemplatedDecl(); 6794 else 6795 FD = cast<FunctionDecl>(D); 6796 auto *VariantFuncRef = DeclRefExpr::Create( 6797 Context, NestedNameSpecifierLoc(), SourceLocation(), FD, 6798 /* RefersToEnclosingVariableOrCapture */ false, 6799 /* NameLoc */ FD->getLocation(), FD->getType(), 6800 ExprValueKind::VK_PRValue); 6801 6802 OMPDeclareVariantScope &DVScope = OMPDeclareVariantScopes.back(); 6803 auto *OMPDeclareVariantA = OMPDeclareVariantAttr::CreateImplicit( 6804 Context, VariantFuncRef, DVScope.TI, 6805 /*NothingArgs=*/nullptr, /*NothingArgsSize=*/0, 6806 /*NeedDevicePtrArgs=*/nullptr, /*NeedDevicePtrArgsSize=*/0); 6807 for (FunctionDecl *BaseFD : Bases) 6808 BaseFD->addAttr(OMPDeclareVariantA); 6809 } 6810 6811 ExprResult Sema::ActOnOpenMPCall(ExprResult Call, Scope *Scope, 6812 SourceLocation LParenLoc, 6813 MultiExprArg ArgExprs, 6814 SourceLocation RParenLoc, Expr *ExecConfig) { 6815 // The common case is a regular call we do not want to specialize at all. Try 6816 // to make that case fast by bailing early. 6817 CallExpr *CE = dyn_cast<CallExpr>(Call.get()); 6818 if (!CE) 6819 return Call; 6820 6821 FunctionDecl *CalleeFnDecl = CE->getDirectCallee(); 6822 if (!CalleeFnDecl) 6823 return Call; 6824 6825 if (!CalleeFnDecl->hasAttr<OMPDeclareVariantAttr>()) 6826 return Call; 6827 6828 ASTContext &Context = getASTContext(); 6829 std::function<void(StringRef)> DiagUnknownTrait = [this, 6830 CE](StringRef ISATrait) { 6831 // TODO Track the selector locations in a way that is accessible here to 6832 // improve the diagnostic location. 6833 Diag(CE->getBeginLoc(), diag::warn_unknown_declare_variant_isa_trait) 6834 << ISATrait; 6835 }; 6836 TargetOMPContext OMPCtx(Context, std::move(DiagUnknownTrait), 6837 getCurFunctionDecl(), DSAStack->getConstructTraits()); 6838 6839 QualType CalleeFnType = CalleeFnDecl->getType(); 6840 6841 SmallVector<Expr *, 4> Exprs; 6842 SmallVector<VariantMatchInfo, 4> VMIs; 6843 while (CalleeFnDecl) { 6844 for (OMPDeclareVariantAttr *A : 6845 CalleeFnDecl->specific_attrs<OMPDeclareVariantAttr>()) { 6846 Expr *VariantRef = A->getVariantFuncRef(); 6847 6848 VariantMatchInfo VMI; 6849 OMPTraitInfo &TI = A->getTraitInfo(); 6850 TI.getAsVariantMatchInfo(Context, VMI); 6851 if (!isVariantApplicableInContext(VMI, OMPCtx, 6852 /* DeviceSetOnly */ false)) 6853 continue; 6854 6855 VMIs.push_back(VMI); 6856 Exprs.push_back(VariantRef); 6857 } 6858 6859 CalleeFnDecl = CalleeFnDecl->getPreviousDecl(); 6860 } 6861 6862 ExprResult NewCall; 6863 do { 6864 int BestIdx = getBestVariantMatchForContext(VMIs, OMPCtx); 6865 if (BestIdx < 0) 6866 return Call; 6867 Expr *BestExpr = cast<DeclRefExpr>(Exprs[BestIdx]); 6868 Decl *BestDecl = cast<DeclRefExpr>(BestExpr)->getDecl(); 6869 6870 { 6871 // Try to build a (member) call expression for the current best applicable 6872 // variant expression. We allow this to fail in which case we continue 6873 // with the next best variant expression. The fail case is part of the 6874 // implementation defined behavior in the OpenMP standard when it talks 6875 // about what differences in the function prototypes: "Any differences 6876 // that the specific OpenMP context requires in the prototype of the 6877 // variant from the base function prototype are implementation defined." 6878 // This wording is there to allow the specialized variant to have a 6879 // different type than the base function. This is intended and OK but if 6880 // we cannot create a call the difference is not in the "implementation 6881 // defined range" we allow. 6882 Sema::TentativeAnalysisScope Trap(*this); 6883 6884 if (auto *SpecializedMethod = dyn_cast<CXXMethodDecl>(BestDecl)) { 6885 auto *MemberCall = dyn_cast<CXXMemberCallExpr>(CE); 6886 BestExpr = MemberExpr::CreateImplicit( 6887 Context, MemberCall->getImplicitObjectArgument(), 6888 /* IsArrow */ false, SpecializedMethod, Context.BoundMemberTy, 6889 MemberCall->getValueKind(), MemberCall->getObjectKind()); 6890 } 6891 NewCall = BuildCallExpr(Scope, BestExpr, LParenLoc, ArgExprs, RParenLoc, 6892 ExecConfig); 6893 if (NewCall.isUsable()) { 6894 if (CallExpr *NCE = dyn_cast<CallExpr>(NewCall.get())) { 6895 FunctionDecl *NewCalleeFnDecl = NCE->getDirectCallee(); 6896 QualType NewType = Context.mergeFunctionTypes( 6897 CalleeFnType, NewCalleeFnDecl->getType(), 6898 /* OfBlockPointer */ false, 6899 /* Unqualified */ false, /* AllowCXX */ true); 6900 if (!NewType.isNull()) 6901 break; 6902 // Don't use the call if the function type was not compatible. 6903 NewCall = nullptr; 6904 } 6905 } 6906 } 6907 6908 VMIs.erase(VMIs.begin() + BestIdx); 6909 Exprs.erase(Exprs.begin() + BestIdx); 6910 } while (!VMIs.empty()); 6911 6912 if (!NewCall.isUsable()) 6913 return Call; 6914 return PseudoObjectExpr::Create(Context, CE, {NewCall.get()}, 0); 6915 } 6916 6917 Optional<std::pair<FunctionDecl *, Expr *>> 6918 Sema::checkOpenMPDeclareVariantFunction(Sema::DeclGroupPtrTy DG, 6919 Expr *VariantRef, OMPTraitInfo &TI, 6920 SourceRange SR) { 6921 if (!DG || DG.get().isNull()) 6922 return None; 6923 6924 const int VariantId = 1; 6925 // Must be applied only to single decl. 6926 if (!DG.get().isSingleDecl()) { 6927 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd_variant) 6928 << VariantId << SR; 6929 return None; 6930 } 6931 Decl *ADecl = DG.get().getSingleDecl(); 6932 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl)) 6933 ADecl = FTD->getTemplatedDecl(); 6934 6935 // Decl must be a function. 6936 auto *FD = dyn_cast<FunctionDecl>(ADecl); 6937 if (!FD) { 6938 Diag(ADecl->getLocation(), diag::err_omp_function_expected) 6939 << VariantId << SR; 6940 return None; 6941 } 6942 6943 auto &&HasMultiVersionAttributes = [](const FunctionDecl *FD) { 6944 return FD->hasAttrs() && 6945 (FD->hasAttr<CPUDispatchAttr>() || FD->hasAttr<CPUSpecificAttr>() || 6946 FD->hasAttr<TargetAttr>()); 6947 }; 6948 // OpenMP is not compatible with CPU-specific attributes. 6949 if (HasMultiVersionAttributes(FD)) { 6950 Diag(FD->getLocation(), diag::err_omp_declare_variant_incompat_attributes) 6951 << SR; 6952 return None; 6953 } 6954 6955 // Allow #pragma omp declare variant only if the function is not used. 6956 if (FD->isUsed(false)) 6957 Diag(SR.getBegin(), diag::warn_omp_declare_variant_after_used) 6958 << FD->getLocation(); 6959 6960 // Check if the function was emitted already. 6961 const FunctionDecl *Definition; 6962 if (!FD->isThisDeclarationADefinition() && FD->isDefined(Definition) && 6963 (LangOpts.EmitAllDecls || Context.DeclMustBeEmitted(Definition))) 6964 Diag(SR.getBegin(), diag::warn_omp_declare_variant_after_emitted) 6965 << FD->getLocation(); 6966 6967 // The VariantRef must point to function. 6968 if (!VariantRef) { 6969 Diag(SR.getBegin(), diag::err_omp_function_expected) << VariantId; 6970 return None; 6971 } 6972 6973 auto ShouldDelayChecks = [](Expr *&E, bool) { 6974 return E && (E->isTypeDependent() || E->isValueDependent() || 6975 E->containsUnexpandedParameterPack() || 6976 E->isInstantiationDependent()); 6977 }; 6978 // Do not check templates, wait until instantiation. 6979 if (FD->isDependentContext() || ShouldDelayChecks(VariantRef, false) || 6980 TI.anyScoreOrCondition(ShouldDelayChecks)) 6981 return std::make_pair(FD, VariantRef); 6982 6983 // Deal with non-constant score and user condition expressions. 6984 auto HandleNonConstantScoresAndConditions = [this](Expr *&E, 6985 bool IsScore) -> bool { 6986 if (!E || E->isIntegerConstantExpr(Context)) 6987 return false; 6988 6989 if (IsScore) { 6990 // We warn on non-constant scores and pretend they were not present. 6991 Diag(E->getExprLoc(), diag::warn_omp_declare_variant_score_not_constant) 6992 << E; 6993 E = nullptr; 6994 } else { 6995 // We could replace a non-constant user condition with "false" but we 6996 // will soon need to handle these anyway for the dynamic version of 6997 // OpenMP context selectors. 6998 Diag(E->getExprLoc(), 6999 diag::err_omp_declare_variant_user_condition_not_constant) 7000 << E; 7001 } 7002 return true; 7003 }; 7004 if (TI.anyScoreOrCondition(HandleNonConstantScoresAndConditions)) 7005 return None; 7006 7007 // Convert VariantRef expression to the type of the original function to 7008 // resolve possible conflicts. 7009 ExprResult VariantRefCast = VariantRef; 7010 if (LangOpts.CPlusPlus) { 7011 QualType FnPtrType; 7012 auto *Method = dyn_cast<CXXMethodDecl>(FD); 7013 if (Method && !Method->isStatic()) { 7014 const Type *ClassType = 7015 Context.getTypeDeclType(Method->getParent()).getTypePtr(); 7016 FnPtrType = Context.getMemberPointerType(FD->getType(), ClassType); 7017 ExprResult ER; 7018 { 7019 // Build adrr_of unary op to correctly handle type checks for member 7020 // functions. 7021 Sema::TentativeAnalysisScope Trap(*this); 7022 ER = CreateBuiltinUnaryOp(VariantRef->getBeginLoc(), UO_AddrOf, 7023 VariantRef); 7024 } 7025 if (!ER.isUsable()) { 7026 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected) 7027 << VariantId << VariantRef->getSourceRange(); 7028 return None; 7029 } 7030 VariantRef = ER.get(); 7031 } else { 7032 FnPtrType = Context.getPointerType(FD->getType()); 7033 } 7034 QualType VarianPtrType = Context.getPointerType(VariantRef->getType()); 7035 if (VarianPtrType.getUnqualifiedType() != FnPtrType.getUnqualifiedType()) { 7036 ImplicitConversionSequence ICS = TryImplicitConversion( 7037 VariantRef, FnPtrType.getUnqualifiedType(), 7038 /*SuppressUserConversions=*/false, AllowedExplicit::None, 7039 /*InOverloadResolution=*/false, 7040 /*CStyle=*/false, 7041 /*AllowObjCWritebackConversion=*/false); 7042 if (ICS.isFailure()) { 7043 Diag(VariantRef->getExprLoc(), 7044 diag::err_omp_declare_variant_incompat_types) 7045 << VariantRef->getType() 7046 << ((Method && !Method->isStatic()) ? FnPtrType : FD->getType()) 7047 << VariantRef->getSourceRange(); 7048 return None; 7049 } 7050 VariantRefCast = PerformImplicitConversion( 7051 VariantRef, FnPtrType.getUnqualifiedType(), AA_Converting); 7052 if (!VariantRefCast.isUsable()) 7053 return None; 7054 } 7055 // Drop previously built artificial addr_of unary op for member functions. 7056 if (Method && !Method->isStatic()) { 7057 Expr *PossibleAddrOfVariantRef = VariantRefCast.get(); 7058 if (auto *UO = dyn_cast<UnaryOperator>( 7059 PossibleAddrOfVariantRef->IgnoreImplicit())) 7060 VariantRefCast = UO->getSubExpr(); 7061 } 7062 } 7063 7064 ExprResult ER = CheckPlaceholderExpr(VariantRefCast.get()); 7065 if (!ER.isUsable() || 7066 !ER.get()->IgnoreParenImpCasts()->getType()->isFunctionType()) { 7067 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected) 7068 << VariantId << VariantRef->getSourceRange(); 7069 return None; 7070 } 7071 7072 // The VariantRef must point to function. 7073 auto *DRE = dyn_cast<DeclRefExpr>(ER.get()->IgnoreParenImpCasts()); 7074 if (!DRE) { 7075 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected) 7076 << VariantId << VariantRef->getSourceRange(); 7077 return None; 7078 } 7079 auto *NewFD = dyn_cast_or_null<FunctionDecl>(DRE->getDecl()); 7080 if (!NewFD) { 7081 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected) 7082 << VariantId << VariantRef->getSourceRange(); 7083 return None; 7084 } 7085 7086 // Check if function types are compatible in C. 7087 if (!LangOpts.CPlusPlus) { 7088 QualType NewType = 7089 Context.mergeFunctionTypes(FD->getType(), NewFD->getType()); 7090 if (NewType.isNull()) { 7091 Diag(VariantRef->getExprLoc(), 7092 diag::err_omp_declare_variant_incompat_types) 7093 << NewFD->getType() << FD->getType() << VariantRef->getSourceRange(); 7094 return None; 7095 } 7096 if (NewType->isFunctionProtoType()) { 7097 if (FD->getType()->isFunctionNoProtoType()) 7098 setPrototype(*this, FD, NewFD, NewType); 7099 else if (NewFD->getType()->isFunctionNoProtoType()) 7100 setPrototype(*this, NewFD, FD, NewType); 7101 } 7102 } 7103 7104 // Check if variant function is not marked with declare variant directive. 7105 if (NewFD->hasAttrs() && NewFD->hasAttr<OMPDeclareVariantAttr>()) { 7106 Diag(VariantRef->getExprLoc(), 7107 diag::warn_omp_declare_variant_marked_as_declare_variant) 7108 << VariantRef->getSourceRange(); 7109 SourceRange SR = 7110 NewFD->specific_attr_begin<OMPDeclareVariantAttr>()->getRange(); 7111 Diag(SR.getBegin(), diag::note_omp_marked_declare_variant_here) << SR; 7112 return None; 7113 } 7114 7115 enum DoesntSupport { 7116 VirtFuncs = 1, 7117 Constructors = 3, 7118 Destructors = 4, 7119 DeletedFuncs = 5, 7120 DefaultedFuncs = 6, 7121 ConstexprFuncs = 7, 7122 ConstevalFuncs = 8, 7123 }; 7124 if (const auto *CXXFD = dyn_cast<CXXMethodDecl>(FD)) { 7125 if (CXXFD->isVirtual()) { 7126 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 7127 << VirtFuncs; 7128 return None; 7129 } 7130 7131 if (isa<CXXConstructorDecl>(FD)) { 7132 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 7133 << Constructors; 7134 return None; 7135 } 7136 7137 if (isa<CXXDestructorDecl>(FD)) { 7138 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 7139 << Destructors; 7140 return None; 7141 } 7142 } 7143 7144 if (FD->isDeleted()) { 7145 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 7146 << DeletedFuncs; 7147 return None; 7148 } 7149 7150 if (FD->isDefaulted()) { 7151 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 7152 << DefaultedFuncs; 7153 return None; 7154 } 7155 7156 if (FD->isConstexpr()) { 7157 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 7158 << (NewFD->isConsteval() ? ConstevalFuncs : ConstexprFuncs); 7159 return None; 7160 } 7161 7162 // Check general compatibility. 7163 if (areMultiversionVariantFunctionsCompatible( 7164 FD, NewFD, PartialDiagnostic::NullDiagnostic(), 7165 PartialDiagnosticAt(SourceLocation(), 7166 PartialDiagnostic::NullDiagnostic()), 7167 PartialDiagnosticAt( 7168 VariantRef->getExprLoc(), 7169 PDiag(diag::err_omp_declare_variant_doesnt_support)), 7170 PartialDiagnosticAt(VariantRef->getExprLoc(), 7171 PDiag(diag::err_omp_declare_variant_diff) 7172 << FD->getLocation()), 7173 /*TemplatesSupported=*/true, /*ConstexprSupported=*/false, 7174 /*CLinkageMayDiffer=*/true)) 7175 return None; 7176 return std::make_pair(FD, cast<Expr>(DRE)); 7177 } 7178 7179 void Sema::ActOnOpenMPDeclareVariantDirective( 7180 FunctionDecl *FD, Expr *VariantRef, OMPTraitInfo &TI, 7181 ArrayRef<Expr *> AdjustArgsNothing, 7182 ArrayRef<Expr *> AdjustArgsNeedDevicePtr, SourceRange SR) { 7183 7184 // OpenMP 5.1 [2.3.5, declare variant directive, Restrictions] 7185 // An adjust_args clause or append_args clause can only be specified if the 7186 // dispatch selector of the construct selector set appears in the match 7187 // clause. 7188 7189 SmallVector<Expr *, 8> AllAdjustArgs; 7190 llvm::append_range(AllAdjustArgs, AdjustArgsNothing); 7191 llvm::append_range(AllAdjustArgs, AdjustArgsNeedDevicePtr); 7192 7193 if (!AllAdjustArgs.empty()) { 7194 VariantMatchInfo VMI; 7195 TI.getAsVariantMatchInfo(Context, VMI); 7196 if (!llvm::is_contained( 7197 VMI.ConstructTraits, 7198 llvm::omp::TraitProperty::construct_dispatch_dispatch)) { 7199 Diag(AllAdjustArgs[0]->getExprLoc(), 7200 diag::err_omp_clause_requires_dispatch_construct) 7201 << getOpenMPClauseName(OMPC_adjust_args); 7202 return; 7203 } 7204 } 7205 7206 // OpenMP 5.1 [2.3.5, declare variant directive, Restrictions] 7207 // Each argument can only appear in a single adjust_args clause for each 7208 // declare variant directive. 7209 llvm::SmallPtrSet<const VarDecl *, 4> AdjustVars; 7210 7211 for (Expr *E : AllAdjustArgs) { 7212 E = E->IgnoreParenImpCasts(); 7213 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) { 7214 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 7215 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 7216 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 7217 FD->getParamDecl(PVD->getFunctionScopeIndex()) 7218 ->getCanonicalDecl() == CanonPVD) { 7219 // It's a parameter of the function, check duplicates. 7220 if (!AdjustVars.insert(CanonPVD).second) { 7221 Diag(DRE->getLocation(), diag::err_omp_adjust_arg_multiple_clauses) 7222 << PVD; 7223 return; 7224 } 7225 continue; 7226 } 7227 } 7228 } 7229 // Anything that is not a function parameter is an error. 7230 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) << FD << 0; 7231 return; 7232 } 7233 7234 auto *NewAttr = OMPDeclareVariantAttr::CreateImplicit( 7235 Context, VariantRef, &TI, const_cast<Expr **>(AdjustArgsNothing.data()), 7236 AdjustArgsNothing.size(), 7237 const_cast<Expr **>(AdjustArgsNeedDevicePtr.data()), 7238 AdjustArgsNeedDevicePtr.size(), SR); 7239 FD->addAttr(NewAttr); 7240 } 7241 7242 StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses, 7243 Stmt *AStmt, 7244 SourceLocation StartLoc, 7245 SourceLocation EndLoc) { 7246 if (!AStmt) 7247 return StmtError(); 7248 7249 auto *CS = cast<CapturedStmt>(AStmt); 7250 // 1.2.2 OpenMP Language Terminology 7251 // Structured block - An executable statement with a single entry at the 7252 // top and a single exit at the bottom. 7253 // The point of exit cannot be a branch out of the structured block. 7254 // longjmp() and throw() must not violate the entry/exit criteria. 7255 CS->getCapturedDecl()->setNothrow(); 7256 7257 setFunctionHasBranchProtectedScope(); 7258 7259 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 7260 DSAStack->getTaskgroupReductionRef(), 7261 DSAStack->isCancelRegion()); 7262 } 7263 7264 namespace { 7265 /// Iteration space of a single for loop. 7266 struct LoopIterationSpace final { 7267 /// True if the condition operator is the strict compare operator (<, > or 7268 /// !=). 7269 bool IsStrictCompare = false; 7270 /// Condition of the loop. 7271 Expr *PreCond = nullptr; 7272 /// This expression calculates the number of iterations in the loop. 7273 /// It is always possible to calculate it before starting the loop. 7274 Expr *NumIterations = nullptr; 7275 /// The loop counter variable. 7276 Expr *CounterVar = nullptr; 7277 /// Private loop counter variable. 7278 Expr *PrivateCounterVar = nullptr; 7279 /// This is initializer for the initial value of #CounterVar. 7280 Expr *CounterInit = nullptr; 7281 /// This is step for the #CounterVar used to generate its update: 7282 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration. 7283 Expr *CounterStep = nullptr; 7284 /// Should step be subtracted? 7285 bool Subtract = false; 7286 /// Source range of the loop init. 7287 SourceRange InitSrcRange; 7288 /// Source range of the loop condition. 7289 SourceRange CondSrcRange; 7290 /// Source range of the loop increment. 7291 SourceRange IncSrcRange; 7292 /// Minimum value that can have the loop control variable. Used to support 7293 /// non-rectangular loops. Applied only for LCV with the non-iterator types, 7294 /// since only such variables can be used in non-loop invariant expressions. 7295 Expr *MinValue = nullptr; 7296 /// Maximum value that can have the loop control variable. Used to support 7297 /// non-rectangular loops. Applied only for LCV with the non-iterator type, 7298 /// since only such variables can be used in non-loop invariant expressions. 7299 Expr *MaxValue = nullptr; 7300 /// true, if the lower bound depends on the outer loop control var. 7301 bool IsNonRectangularLB = false; 7302 /// true, if the upper bound depends on the outer loop control var. 7303 bool IsNonRectangularUB = false; 7304 /// Index of the loop this loop depends on and forms non-rectangular loop 7305 /// nest. 7306 unsigned LoopDependentIdx = 0; 7307 /// Final condition for the non-rectangular loop nest support. It is used to 7308 /// check that the number of iterations for this particular counter must be 7309 /// finished. 7310 Expr *FinalCondition = nullptr; 7311 }; 7312 7313 /// Helper class for checking canonical form of the OpenMP loops and 7314 /// extracting iteration space of each loop in the loop nest, that will be used 7315 /// for IR generation. 7316 class OpenMPIterationSpaceChecker { 7317 /// Reference to Sema. 7318 Sema &SemaRef; 7319 /// Does the loop associated directive support non-rectangular loops? 7320 bool SupportsNonRectangular; 7321 /// Data-sharing stack. 7322 DSAStackTy &Stack; 7323 /// A location for diagnostics (when there is no some better location). 7324 SourceLocation DefaultLoc; 7325 /// A location for diagnostics (when increment is not compatible). 7326 SourceLocation ConditionLoc; 7327 /// A source location for referring to loop init later. 7328 SourceRange InitSrcRange; 7329 /// A source location for referring to condition later. 7330 SourceRange ConditionSrcRange; 7331 /// A source location for referring to increment later. 7332 SourceRange IncrementSrcRange; 7333 /// Loop variable. 7334 ValueDecl *LCDecl = nullptr; 7335 /// Reference to loop variable. 7336 Expr *LCRef = nullptr; 7337 /// Lower bound (initializer for the var). 7338 Expr *LB = nullptr; 7339 /// Upper bound. 7340 Expr *UB = nullptr; 7341 /// Loop step (increment). 7342 Expr *Step = nullptr; 7343 /// This flag is true when condition is one of: 7344 /// Var < UB 7345 /// Var <= UB 7346 /// UB > Var 7347 /// UB >= Var 7348 /// This will have no value when the condition is != 7349 llvm::Optional<bool> TestIsLessOp; 7350 /// This flag is true when condition is strict ( < or > ). 7351 bool TestIsStrictOp = false; 7352 /// This flag is true when step is subtracted on each iteration. 7353 bool SubtractStep = false; 7354 /// The outer loop counter this loop depends on (if any). 7355 const ValueDecl *DepDecl = nullptr; 7356 /// Contains number of loop (starts from 1) on which loop counter init 7357 /// expression of this loop depends on. 7358 Optional<unsigned> InitDependOnLC; 7359 /// Contains number of loop (starts from 1) on which loop counter condition 7360 /// expression of this loop depends on. 7361 Optional<unsigned> CondDependOnLC; 7362 /// Checks if the provide statement depends on the loop counter. 7363 Optional<unsigned> doesDependOnLoopCounter(const Stmt *S, bool IsInitializer); 7364 /// Original condition required for checking of the exit condition for 7365 /// non-rectangular loop. 7366 Expr *Condition = nullptr; 7367 7368 public: 7369 OpenMPIterationSpaceChecker(Sema &SemaRef, bool SupportsNonRectangular, 7370 DSAStackTy &Stack, SourceLocation DefaultLoc) 7371 : SemaRef(SemaRef), SupportsNonRectangular(SupportsNonRectangular), 7372 Stack(Stack), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {} 7373 /// Check init-expr for canonical loop form and save loop counter 7374 /// variable - #Var and its initialization value - #LB. 7375 bool checkAndSetInit(Stmt *S, bool EmitDiags = true); 7376 /// Check test-expr for canonical form, save upper-bound (#UB), flags 7377 /// for less/greater and for strict/non-strict comparison. 7378 bool checkAndSetCond(Expr *S); 7379 /// Check incr-expr for canonical loop form and return true if it 7380 /// does not conform, otherwise save loop step (#Step). 7381 bool checkAndSetInc(Expr *S); 7382 /// Return the loop counter variable. 7383 ValueDecl *getLoopDecl() const { return LCDecl; } 7384 /// Return the reference expression to loop counter variable. 7385 Expr *getLoopDeclRefExpr() const { return LCRef; } 7386 /// Source range of the loop init. 7387 SourceRange getInitSrcRange() const { return InitSrcRange; } 7388 /// Source range of the loop condition. 7389 SourceRange getConditionSrcRange() const { return ConditionSrcRange; } 7390 /// Source range of the loop increment. 7391 SourceRange getIncrementSrcRange() const { return IncrementSrcRange; } 7392 /// True if the step should be subtracted. 7393 bool shouldSubtractStep() const { return SubtractStep; } 7394 /// True, if the compare operator is strict (<, > or !=). 7395 bool isStrictTestOp() const { return TestIsStrictOp; } 7396 /// Build the expression to calculate the number of iterations. 7397 Expr *buildNumIterations( 7398 Scope *S, ArrayRef<LoopIterationSpace> ResultIterSpaces, bool LimitedType, 7399 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 7400 /// Build the precondition expression for the loops. 7401 Expr * 7402 buildPreCond(Scope *S, Expr *Cond, 7403 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 7404 /// Build reference expression to the counter be used for codegen. 7405 DeclRefExpr * 7406 buildCounterVar(llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 7407 DSAStackTy &DSA) const; 7408 /// Build reference expression to the private counter be used for 7409 /// codegen. 7410 Expr *buildPrivateCounterVar() const; 7411 /// Build initialization of the counter be used for codegen. 7412 Expr *buildCounterInit() const; 7413 /// Build step of the counter be used for codegen. 7414 Expr *buildCounterStep() const; 7415 /// Build loop data with counter value for depend clauses in ordered 7416 /// directives. 7417 Expr * 7418 buildOrderedLoopData(Scope *S, Expr *Counter, 7419 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 7420 SourceLocation Loc, Expr *Inc = nullptr, 7421 OverloadedOperatorKind OOK = OO_Amp); 7422 /// Builds the minimum value for the loop counter. 7423 std::pair<Expr *, Expr *> buildMinMaxValues( 7424 Scope *S, llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 7425 /// Builds final condition for the non-rectangular loops. 7426 Expr *buildFinalCondition(Scope *S) const; 7427 /// Return true if any expression is dependent. 7428 bool dependent() const; 7429 /// Returns true if the initializer forms non-rectangular loop. 7430 bool doesInitDependOnLC() const { return InitDependOnLC.hasValue(); } 7431 /// Returns true if the condition forms non-rectangular loop. 7432 bool doesCondDependOnLC() const { return CondDependOnLC.hasValue(); } 7433 /// Returns index of the loop we depend on (starting from 1), or 0 otherwise. 7434 unsigned getLoopDependentIdx() const { 7435 return InitDependOnLC.getValueOr(CondDependOnLC.getValueOr(0)); 7436 } 7437 7438 private: 7439 /// Check the right-hand side of an assignment in the increment 7440 /// expression. 7441 bool checkAndSetIncRHS(Expr *RHS); 7442 /// Helper to set loop counter variable and its initializer. 7443 bool setLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB, 7444 bool EmitDiags); 7445 /// Helper to set upper bound. 7446 bool setUB(Expr *NewUB, llvm::Optional<bool> LessOp, bool StrictOp, 7447 SourceRange SR, SourceLocation SL); 7448 /// Helper to set loop increment. 7449 bool setStep(Expr *NewStep, bool Subtract); 7450 }; 7451 7452 bool OpenMPIterationSpaceChecker::dependent() const { 7453 if (!LCDecl) { 7454 assert(!LB && !UB && !Step); 7455 return false; 7456 } 7457 return LCDecl->getType()->isDependentType() || 7458 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) || 7459 (Step && Step->isValueDependent()); 7460 } 7461 7462 bool OpenMPIterationSpaceChecker::setLCDeclAndLB(ValueDecl *NewLCDecl, 7463 Expr *NewLCRefExpr, 7464 Expr *NewLB, bool EmitDiags) { 7465 // State consistency checking to ensure correct usage. 7466 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr && 7467 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp); 7468 if (!NewLCDecl || !NewLB || NewLB->containsErrors()) 7469 return true; 7470 LCDecl = getCanonicalDecl(NewLCDecl); 7471 LCRef = NewLCRefExpr; 7472 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB)) 7473 if (const CXXConstructorDecl *Ctor = CE->getConstructor()) 7474 if ((Ctor->isCopyOrMoveConstructor() || 7475 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && 7476 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) 7477 NewLB = CE->getArg(0)->IgnoreParenImpCasts(); 7478 LB = NewLB; 7479 if (EmitDiags) 7480 InitDependOnLC = doesDependOnLoopCounter(LB, /*IsInitializer=*/true); 7481 return false; 7482 } 7483 7484 bool OpenMPIterationSpaceChecker::setUB(Expr *NewUB, 7485 llvm::Optional<bool> LessOp, 7486 bool StrictOp, SourceRange SR, 7487 SourceLocation SL) { 7488 // State consistency checking to ensure correct usage. 7489 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr && 7490 Step == nullptr && !TestIsLessOp && !TestIsStrictOp); 7491 if (!NewUB || NewUB->containsErrors()) 7492 return true; 7493 UB = NewUB; 7494 if (LessOp) 7495 TestIsLessOp = LessOp; 7496 TestIsStrictOp = StrictOp; 7497 ConditionSrcRange = SR; 7498 ConditionLoc = SL; 7499 CondDependOnLC = doesDependOnLoopCounter(UB, /*IsInitializer=*/false); 7500 return false; 7501 } 7502 7503 bool OpenMPIterationSpaceChecker::setStep(Expr *NewStep, bool Subtract) { 7504 // State consistency checking to ensure correct usage. 7505 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr); 7506 if (!NewStep || NewStep->containsErrors()) 7507 return true; 7508 if (!NewStep->isValueDependent()) { 7509 // Check that the step is integer expression. 7510 SourceLocation StepLoc = NewStep->getBeginLoc(); 7511 ExprResult Val = SemaRef.PerformOpenMPImplicitIntegerConversion( 7512 StepLoc, getExprAsWritten(NewStep)); 7513 if (Val.isInvalid()) 7514 return true; 7515 NewStep = Val.get(); 7516 7517 // OpenMP [2.6, Canonical Loop Form, Restrictions] 7518 // If test-expr is of form var relational-op b and relational-op is < or 7519 // <= then incr-expr must cause var to increase on each iteration of the 7520 // loop. If test-expr is of form var relational-op b and relational-op is 7521 // > or >= then incr-expr must cause var to decrease on each iteration of 7522 // the loop. 7523 // If test-expr is of form b relational-op var and relational-op is < or 7524 // <= then incr-expr must cause var to decrease on each iteration of the 7525 // loop. If test-expr is of form b relational-op var and relational-op is 7526 // > or >= then incr-expr must cause var to increase on each iteration of 7527 // the loop. 7528 Optional<llvm::APSInt> Result = 7529 NewStep->getIntegerConstantExpr(SemaRef.Context); 7530 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation(); 7531 bool IsConstNeg = 7532 Result && Result->isSigned() && (Subtract != Result->isNegative()); 7533 bool IsConstPos = 7534 Result && Result->isSigned() && (Subtract == Result->isNegative()); 7535 bool IsConstZero = Result && !Result->getBoolValue(); 7536 7537 // != with increment is treated as <; != with decrement is treated as > 7538 if (!TestIsLessOp.hasValue()) 7539 TestIsLessOp = IsConstPos || (IsUnsigned && !Subtract); 7540 if (UB && (IsConstZero || 7541 (TestIsLessOp.getValue() ? 7542 (IsConstNeg || (IsUnsigned && Subtract)) : 7543 (IsConstPos || (IsUnsigned && !Subtract))))) { 7544 SemaRef.Diag(NewStep->getExprLoc(), 7545 diag::err_omp_loop_incr_not_compatible) 7546 << LCDecl << TestIsLessOp.getValue() << NewStep->getSourceRange(); 7547 SemaRef.Diag(ConditionLoc, 7548 diag::note_omp_loop_cond_requres_compatible_incr) 7549 << TestIsLessOp.getValue() << ConditionSrcRange; 7550 return true; 7551 } 7552 if (TestIsLessOp.getValue() == Subtract) { 7553 NewStep = 7554 SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep) 7555 .get(); 7556 Subtract = !Subtract; 7557 } 7558 } 7559 7560 Step = NewStep; 7561 SubtractStep = Subtract; 7562 return false; 7563 } 7564 7565 namespace { 7566 /// Checker for the non-rectangular loops. Checks if the initializer or 7567 /// condition expression references loop counter variable. 7568 class LoopCounterRefChecker final 7569 : public ConstStmtVisitor<LoopCounterRefChecker, bool> { 7570 Sema &SemaRef; 7571 DSAStackTy &Stack; 7572 const ValueDecl *CurLCDecl = nullptr; 7573 const ValueDecl *DepDecl = nullptr; 7574 const ValueDecl *PrevDepDecl = nullptr; 7575 bool IsInitializer = true; 7576 bool SupportsNonRectangular; 7577 unsigned BaseLoopId = 0; 7578 bool checkDecl(const Expr *E, const ValueDecl *VD) { 7579 if (getCanonicalDecl(VD) == getCanonicalDecl(CurLCDecl)) { 7580 SemaRef.Diag(E->getExprLoc(), diag::err_omp_stmt_depends_on_loop_counter) 7581 << (IsInitializer ? 0 : 1); 7582 return false; 7583 } 7584 const auto &&Data = Stack.isLoopControlVariable(VD); 7585 // OpenMP, 2.9.1 Canonical Loop Form, Restrictions. 7586 // The type of the loop iterator on which we depend may not have a random 7587 // access iterator type. 7588 if (Data.first && VD->getType()->isRecordType()) { 7589 SmallString<128> Name; 7590 llvm::raw_svector_ostream OS(Name); 7591 VD->getNameForDiagnostic(OS, SemaRef.getPrintingPolicy(), 7592 /*Qualified=*/true); 7593 SemaRef.Diag(E->getExprLoc(), 7594 diag::err_omp_wrong_dependency_iterator_type) 7595 << OS.str(); 7596 SemaRef.Diag(VD->getLocation(), diag::note_previous_decl) << VD; 7597 return false; 7598 } 7599 if (Data.first && !SupportsNonRectangular) { 7600 SemaRef.Diag(E->getExprLoc(), diag::err_omp_invariant_dependency); 7601 return false; 7602 } 7603 if (Data.first && 7604 (DepDecl || (PrevDepDecl && 7605 getCanonicalDecl(VD) != getCanonicalDecl(PrevDepDecl)))) { 7606 if (!DepDecl && PrevDepDecl) 7607 DepDecl = PrevDepDecl; 7608 SmallString<128> Name; 7609 llvm::raw_svector_ostream OS(Name); 7610 DepDecl->getNameForDiagnostic(OS, SemaRef.getPrintingPolicy(), 7611 /*Qualified=*/true); 7612 SemaRef.Diag(E->getExprLoc(), 7613 diag::err_omp_invariant_or_linear_dependency) 7614 << OS.str(); 7615 return false; 7616 } 7617 if (Data.first) { 7618 DepDecl = VD; 7619 BaseLoopId = Data.first; 7620 } 7621 return Data.first; 7622 } 7623 7624 public: 7625 bool VisitDeclRefExpr(const DeclRefExpr *E) { 7626 const ValueDecl *VD = E->getDecl(); 7627 if (isa<VarDecl>(VD)) 7628 return checkDecl(E, VD); 7629 return false; 7630 } 7631 bool VisitMemberExpr(const MemberExpr *E) { 7632 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) { 7633 const ValueDecl *VD = E->getMemberDecl(); 7634 if (isa<VarDecl>(VD) || isa<FieldDecl>(VD)) 7635 return checkDecl(E, VD); 7636 } 7637 return false; 7638 } 7639 bool VisitStmt(const Stmt *S) { 7640 bool Res = false; 7641 for (const Stmt *Child : S->children()) 7642 Res = (Child && Visit(Child)) || Res; 7643 return Res; 7644 } 7645 explicit LoopCounterRefChecker(Sema &SemaRef, DSAStackTy &Stack, 7646 const ValueDecl *CurLCDecl, bool IsInitializer, 7647 const ValueDecl *PrevDepDecl = nullptr, 7648 bool SupportsNonRectangular = true) 7649 : SemaRef(SemaRef), Stack(Stack), CurLCDecl(CurLCDecl), 7650 PrevDepDecl(PrevDepDecl), IsInitializer(IsInitializer), 7651 SupportsNonRectangular(SupportsNonRectangular) {} 7652 unsigned getBaseLoopId() const { 7653 assert(CurLCDecl && "Expected loop dependency."); 7654 return BaseLoopId; 7655 } 7656 const ValueDecl *getDepDecl() const { 7657 assert(CurLCDecl && "Expected loop dependency."); 7658 return DepDecl; 7659 } 7660 }; 7661 } // namespace 7662 7663 Optional<unsigned> 7664 OpenMPIterationSpaceChecker::doesDependOnLoopCounter(const Stmt *S, 7665 bool IsInitializer) { 7666 // Check for the non-rectangular loops. 7667 LoopCounterRefChecker LoopStmtChecker(SemaRef, Stack, LCDecl, IsInitializer, 7668 DepDecl, SupportsNonRectangular); 7669 if (LoopStmtChecker.Visit(S)) { 7670 DepDecl = LoopStmtChecker.getDepDecl(); 7671 return LoopStmtChecker.getBaseLoopId(); 7672 } 7673 return llvm::None; 7674 } 7675 7676 bool OpenMPIterationSpaceChecker::checkAndSetInit(Stmt *S, bool EmitDiags) { 7677 // Check init-expr for canonical loop form and save loop counter 7678 // variable - #Var and its initialization value - #LB. 7679 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following: 7680 // var = lb 7681 // integer-type var = lb 7682 // random-access-iterator-type var = lb 7683 // pointer-type var = lb 7684 // 7685 if (!S) { 7686 if (EmitDiags) { 7687 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init); 7688 } 7689 return true; 7690 } 7691 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S)) 7692 if (!ExprTemp->cleanupsHaveSideEffects()) 7693 S = ExprTemp->getSubExpr(); 7694 7695 InitSrcRange = S->getSourceRange(); 7696 if (Expr *E = dyn_cast<Expr>(S)) 7697 S = E->IgnoreParens(); 7698 if (auto *BO = dyn_cast<BinaryOperator>(S)) { 7699 if (BO->getOpcode() == BO_Assign) { 7700 Expr *LHS = BO->getLHS()->IgnoreParens(); 7701 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) { 7702 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) 7703 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 7704 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 7705 EmitDiags); 7706 return setLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS(), EmitDiags); 7707 } 7708 if (auto *ME = dyn_cast<MemberExpr>(LHS)) { 7709 if (ME->isArrow() && 7710 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 7711 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 7712 EmitDiags); 7713 } 7714 } 7715 } else if (auto *DS = dyn_cast<DeclStmt>(S)) { 7716 if (DS->isSingleDecl()) { 7717 if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) { 7718 if (Var->hasInit() && !Var->getType()->isReferenceType()) { 7719 // Accept non-canonical init form here but emit ext. warning. 7720 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags) 7721 SemaRef.Diag(S->getBeginLoc(), 7722 diag::ext_omp_loop_not_canonical_init) 7723 << S->getSourceRange(); 7724 return setLCDeclAndLB( 7725 Var, 7726 buildDeclRefExpr(SemaRef, Var, 7727 Var->getType().getNonReferenceType(), 7728 DS->getBeginLoc()), 7729 Var->getInit(), EmitDiags); 7730 } 7731 } 7732 } 7733 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 7734 if (CE->getOperator() == OO_Equal) { 7735 Expr *LHS = CE->getArg(0); 7736 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) { 7737 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) 7738 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 7739 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 7740 EmitDiags); 7741 return setLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1), EmitDiags); 7742 } 7743 if (auto *ME = dyn_cast<MemberExpr>(LHS)) { 7744 if (ME->isArrow() && 7745 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 7746 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 7747 EmitDiags); 7748 } 7749 } 7750 } 7751 7752 if (dependent() || SemaRef.CurContext->isDependentContext()) 7753 return false; 7754 if (EmitDiags) { 7755 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_init) 7756 << S->getSourceRange(); 7757 } 7758 return true; 7759 } 7760 7761 /// Ignore parenthesizes, implicit casts, copy constructor and return the 7762 /// variable (which may be the loop variable) if possible. 7763 static const ValueDecl *getInitLCDecl(const Expr *E) { 7764 if (!E) 7765 return nullptr; 7766 E = getExprAsWritten(E); 7767 if (const auto *CE = dyn_cast_or_null<CXXConstructExpr>(E)) 7768 if (const CXXConstructorDecl *Ctor = CE->getConstructor()) 7769 if ((Ctor->isCopyOrMoveConstructor() || 7770 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && 7771 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) 7772 E = CE->getArg(0)->IgnoreParenImpCasts(); 7773 if (const auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) { 7774 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) 7775 return getCanonicalDecl(VD); 7776 } 7777 if (const auto *ME = dyn_cast_or_null<MemberExpr>(E)) 7778 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 7779 return getCanonicalDecl(ME->getMemberDecl()); 7780 return nullptr; 7781 } 7782 7783 bool OpenMPIterationSpaceChecker::checkAndSetCond(Expr *S) { 7784 // Check test-expr for canonical form, save upper-bound UB, flags for 7785 // less/greater and for strict/non-strict comparison. 7786 // OpenMP [2.9] Canonical loop form. Test-expr may be one of the following: 7787 // var relational-op b 7788 // b relational-op var 7789 // 7790 bool IneqCondIsCanonical = SemaRef.getLangOpts().OpenMP >= 50; 7791 if (!S) { 7792 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) 7793 << (IneqCondIsCanonical ? 1 : 0) << LCDecl; 7794 return true; 7795 } 7796 Condition = S; 7797 S = getExprAsWritten(S); 7798 SourceLocation CondLoc = S->getBeginLoc(); 7799 auto &&CheckAndSetCond = [this, IneqCondIsCanonical]( 7800 BinaryOperatorKind Opcode, const Expr *LHS, 7801 const Expr *RHS, SourceRange SR, 7802 SourceLocation OpLoc) -> llvm::Optional<bool> { 7803 if (BinaryOperator::isRelationalOp(Opcode)) { 7804 if (getInitLCDecl(LHS) == LCDecl) 7805 return setUB(const_cast<Expr *>(RHS), 7806 (Opcode == BO_LT || Opcode == BO_LE), 7807 (Opcode == BO_LT || Opcode == BO_GT), SR, OpLoc); 7808 if (getInitLCDecl(RHS) == LCDecl) 7809 return setUB(const_cast<Expr *>(LHS), 7810 (Opcode == BO_GT || Opcode == BO_GE), 7811 (Opcode == BO_LT || Opcode == BO_GT), SR, OpLoc); 7812 } else if (IneqCondIsCanonical && Opcode == BO_NE) { 7813 return setUB(const_cast<Expr *>(getInitLCDecl(LHS) == LCDecl ? RHS : LHS), 7814 /*LessOp=*/llvm::None, 7815 /*StrictOp=*/true, SR, OpLoc); 7816 } 7817 return llvm::None; 7818 }; 7819 llvm::Optional<bool> Res; 7820 if (auto *RBO = dyn_cast<CXXRewrittenBinaryOperator>(S)) { 7821 CXXRewrittenBinaryOperator::DecomposedForm DF = RBO->getDecomposedForm(); 7822 Res = CheckAndSetCond(DF.Opcode, DF.LHS, DF.RHS, RBO->getSourceRange(), 7823 RBO->getOperatorLoc()); 7824 } else if (auto *BO = dyn_cast<BinaryOperator>(S)) { 7825 Res = CheckAndSetCond(BO->getOpcode(), BO->getLHS(), BO->getRHS(), 7826 BO->getSourceRange(), BO->getOperatorLoc()); 7827 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 7828 if (CE->getNumArgs() == 2) { 7829 Res = CheckAndSetCond( 7830 BinaryOperator::getOverloadedOpcode(CE->getOperator()), CE->getArg(0), 7831 CE->getArg(1), CE->getSourceRange(), CE->getOperatorLoc()); 7832 } 7833 } 7834 if (Res.hasValue()) 7835 return *Res; 7836 if (dependent() || SemaRef.CurContext->isDependentContext()) 7837 return false; 7838 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond) 7839 << (IneqCondIsCanonical ? 1 : 0) << S->getSourceRange() << LCDecl; 7840 return true; 7841 } 7842 7843 bool OpenMPIterationSpaceChecker::checkAndSetIncRHS(Expr *RHS) { 7844 // RHS of canonical loop form increment can be: 7845 // var + incr 7846 // incr + var 7847 // var - incr 7848 // 7849 RHS = RHS->IgnoreParenImpCasts(); 7850 if (auto *BO = dyn_cast<BinaryOperator>(RHS)) { 7851 if (BO->isAdditiveOp()) { 7852 bool IsAdd = BO->getOpcode() == BO_Add; 7853 if (getInitLCDecl(BO->getLHS()) == LCDecl) 7854 return setStep(BO->getRHS(), !IsAdd); 7855 if (IsAdd && getInitLCDecl(BO->getRHS()) == LCDecl) 7856 return setStep(BO->getLHS(), /*Subtract=*/false); 7857 } 7858 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) { 7859 bool IsAdd = CE->getOperator() == OO_Plus; 7860 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) { 7861 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 7862 return setStep(CE->getArg(1), !IsAdd); 7863 if (IsAdd && getInitLCDecl(CE->getArg(1)) == LCDecl) 7864 return setStep(CE->getArg(0), /*Subtract=*/false); 7865 } 7866 } 7867 if (dependent() || SemaRef.CurContext->isDependentContext()) 7868 return false; 7869 SemaRef.Diag(RHS->getBeginLoc(), diag::err_omp_loop_not_canonical_incr) 7870 << RHS->getSourceRange() << LCDecl; 7871 return true; 7872 } 7873 7874 bool OpenMPIterationSpaceChecker::checkAndSetInc(Expr *S) { 7875 // Check incr-expr for canonical loop form and return true if it 7876 // does not conform. 7877 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following: 7878 // ++var 7879 // var++ 7880 // --var 7881 // var-- 7882 // var += incr 7883 // var -= incr 7884 // var = var + incr 7885 // var = incr + var 7886 // var = var - incr 7887 // 7888 if (!S) { 7889 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl; 7890 return true; 7891 } 7892 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S)) 7893 if (!ExprTemp->cleanupsHaveSideEffects()) 7894 S = ExprTemp->getSubExpr(); 7895 7896 IncrementSrcRange = S->getSourceRange(); 7897 S = S->IgnoreParens(); 7898 if (auto *UO = dyn_cast<UnaryOperator>(S)) { 7899 if (UO->isIncrementDecrementOp() && 7900 getInitLCDecl(UO->getSubExpr()) == LCDecl) 7901 return setStep(SemaRef 7902 .ActOnIntegerConstant(UO->getBeginLoc(), 7903 (UO->isDecrementOp() ? -1 : 1)) 7904 .get(), 7905 /*Subtract=*/false); 7906 } else if (auto *BO = dyn_cast<BinaryOperator>(S)) { 7907 switch (BO->getOpcode()) { 7908 case BO_AddAssign: 7909 case BO_SubAssign: 7910 if (getInitLCDecl(BO->getLHS()) == LCDecl) 7911 return setStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign); 7912 break; 7913 case BO_Assign: 7914 if (getInitLCDecl(BO->getLHS()) == LCDecl) 7915 return checkAndSetIncRHS(BO->getRHS()); 7916 break; 7917 default: 7918 break; 7919 } 7920 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 7921 switch (CE->getOperator()) { 7922 case OO_PlusPlus: 7923 case OO_MinusMinus: 7924 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 7925 return setStep(SemaRef 7926 .ActOnIntegerConstant( 7927 CE->getBeginLoc(), 7928 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)) 7929 .get(), 7930 /*Subtract=*/false); 7931 break; 7932 case OO_PlusEqual: 7933 case OO_MinusEqual: 7934 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 7935 return setStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual); 7936 break; 7937 case OO_Equal: 7938 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 7939 return checkAndSetIncRHS(CE->getArg(1)); 7940 break; 7941 default: 7942 break; 7943 } 7944 } 7945 if (dependent() || SemaRef.CurContext->isDependentContext()) 7946 return false; 7947 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_incr) 7948 << S->getSourceRange() << LCDecl; 7949 return true; 7950 } 7951 7952 static ExprResult 7953 tryBuildCapture(Sema &SemaRef, Expr *Capture, 7954 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 7955 if (SemaRef.CurContext->isDependentContext() || Capture->containsErrors()) 7956 return Capture; 7957 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects)) 7958 return SemaRef.PerformImplicitConversion( 7959 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting, 7960 /*AllowExplicit=*/true); 7961 auto I = Captures.find(Capture); 7962 if (I != Captures.end()) 7963 return buildCapture(SemaRef, Capture, I->second); 7964 DeclRefExpr *Ref = nullptr; 7965 ExprResult Res = buildCapture(SemaRef, Capture, Ref); 7966 Captures[Capture] = Ref; 7967 return Res; 7968 } 7969 7970 /// Calculate number of iterations, transforming to unsigned, if number of 7971 /// iterations may be larger than the original type. 7972 static Expr * 7973 calculateNumIters(Sema &SemaRef, Scope *S, SourceLocation DefaultLoc, 7974 Expr *Lower, Expr *Upper, Expr *Step, QualType LCTy, 7975 bool TestIsStrictOp, bool RoundToStep, 7976 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 7977 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures); 7978 if (!NewStep.isUsable()) 7979 return nullptr; 7980 llvm::APSInt LRes, SRes; 7981 bool IsLowerConst = false, IsStepConst = false; 7982 if (Optional<llvm::APSInt> Res = Lower->getIntegerConstantExpr(SemaRef.Context)) { 7983 LRes = *Res; 7984 IsLowerConst = true; 7985 } 7986 if (Optional<llvm::APSInt> Res = Step->getIntegerConstantExpr(SemaRef.Context)) { 7987 SRes = *Res; 7988 IsStepConst = true; 7989 } 7990 bool NoNeedToConvert = IsLowerConst && !RoundToStep && 7991 ((!TestIsStrictOp && LRes.isNonNegative()) || 7992 (TestIsStrictOp && LRes.isStrictlyPositive())); 7993 bool NeedToReorganize = false; 7994 // Check if any subexpressions in Lower -Step [+ 1] lead to overflow. 7995 if (!NoNeedToConvert && IsLowerConst && 7996 (TestIsStrictOp || (RoundToStep && IsStepConst))) { 7997 NoNeedToConvert = true; 7998 if (RoundToStep) { 7999 unsigned BW = LRes.getBitWidth() > SRes.getBitWidth() 8000 ? LRes.getBitWidth() 8001 : SRes.getBitWidth(); 8002 LRes = LRes.extend(BW + 1); 8003 LRes.setIsSigned(true); 8004 SRes = SRes.extend(BW + 1); 8005 SRes.setIsSigned(true); 8006 LRes -= SRes; 8007 NoNeedToConvert = LRes.trunc(BW).extend(BW + 1) == LRes; 8008 LRes = LRes.trunc(BW); 8009 } 8010 if (TestIsStrictOp) { 8011 unsigned BW = LRes.getBitWidth(); 8012 LRes = LRes.extend(BW + 1); 8013 LRes.setIsSigned(true); 8014 ++LRes; 8015 NoNeedToConvert = 8016 NoNeedToConvert && LRes.trunc(BW).extend(BW + 1) == LRes; 8017 // truncate to the original bitwidth. 8018 LRes = LRes.trunc(BW); 8019 } 8020 NeedToReorganize = NoNeedToConvert; 8021 } 8022 llvm::APSInt URes; 8023 bool IsUpperConst = false; 8024 if (Optional<llvm::APSInt> Res = Upper->getIntegerConstantExpr(SemaRef.Context)) { 8025 URes = *Res; 8026 IsUpperConst = true; 8027 } 8028 if (NoNeedToConvert && IsLowerConst && IsUpperConst && 8029 (!RoundToStep || IsStepConst)) { 8030 unsigned BW = LRes.getBitWidth() > URes.getBitWidth() ? LRes.getBitWidth() 8031 : URes.getBitWidth(); 8032 LRes = LRes.extend(BW + 1); 8033 LRes.setIsSigned(true); 8034 URes = URes.extend(BW + 1); 8035 URes.setIsSigned(true); 8036 URes -= LRes; 8037 NoNeedToConvert = URes.trunc(BW).extend(BW + 1) == URes; 8038 NeedToReorganize = NoNeedToConvert; 8039 } 8040 // If the boundaries are not constant or (Lower - Step [+ 1]) is not constant 8041 // or less than zero (Upper - (Lower - Step [+ 1]) may overflow) - promote to 8042 // unsigned. 8043 if ((!NoNeedToConvert || (LRes.isNegative() && !IsUpperConst)) && 8044 !LCTy->isDependentType() && LCTy->isIntegerType()) { 8045 QualType LowerTy = Lower->getType(); 8046 QualType UpperTy = Upper->getType(); 8047 uint64_t LowerSize = SemaRef.Context.getTypeSize(LowerTy); 8048 uint64_t UpperSize = SemaRef.Context.getTypeSize(UpperTy); 8049 if ((LowerSize <= UpperSize && UpperTy->hasSignedIntegerRepresentation()) || 8050 (LowerSize > UpperSize && LowerTy->hasSignedIntegerRepresentation())) { 8051 QualType CastType = SemaRef.Context.getIntTypeForBitwidth( 8052 LowerSize > UpperSize ? LowerSize : UpperSize, /*Signed=*/0); 8053 Upper = 8054 SemaRef 8055 .PerformImplicitConversion( 8056 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Upper).get(), 8057 CastType, Sema::AA_Converting) 8058 .get(); 8059 Lower = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Lower).get(); 8060 NewStep = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, NewStep.get()); 8061 } 8062 } 8063 if (!Lower || !Upper || NewStep.isInvalid()) 8064 return nullptr; 8065 8066 ExprResult Diff; 8067 // If need to reorganize, then calculate the form as Upper - (Lower - Step [+ 8068 // 1]). 8069 if (NeedToReorganize) { 8070 Diff = Lower; 8071 8072 if (RoundToStep) { 8073 // Lower - Step 8074 Diff = 8075 SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Diff.get(), NewStep.get()); 8076 if (!Diff.isUsable()) 8077 return nullptr; 8078 } 8079 8080 // Lower - Step [+ 1] 8081 if (TestIsStrictOp) 8082 Diff = SemaRef.BuildBinOp( 8083 S, DefaultLoc, BO_Add, Diff.get(), 8084 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 8085 if (!Diff.isUsable()) 8086 return nullptr; 8087 8088 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 8089 if (!Diff.isUsable()) 8090 return nullptr; 8091 8092 // Upper - (Lower - Step [+ 1]). 8093 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Diff.get()); 8094 if (!Diff.isUsable()) 8095 return nullptr; 8096 } else { 8097 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower); 8098 8099 if (!Diff.isUsable() && LCTy->getAsCXXRecordDecl()) { 8100 // BuildBinOp already emitted error, this one is to point user to upper 8101 // and lower bound, and to tell what is passed to 'operator-'. 8102 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx) 8103 << Upper->getSourceRange() << Lower->getSourceRange(); 8104 return nullptr; 8105 } 8106 8107 if (!Diff.isUsable()) 8108 return nullptr; 8109 8110 // Upper - Lower [- 1] 8111 if (TestIsStrictOp) 8112 Diff = SemaRef.BuildBinOp( 8113 S, DefaultLoc, BO_Sub, Diff.get(), 8114 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 8115 if (!Diff.isUsable()) 8116 return nullptr; 8117 8118 if (RoundToStep) { 8119 // Upper - Lower [- 1] + Step 8120 Diff = 8121 SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get()); 8122 if (!Diff.isUsable()) 8123 return nullptr; 8124 } 8125 } 8126 8127 // Parentheses (for dumping/debugging purposes only). 8128 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 8129 if (!Diff.isUsable()) 8130 return nullptr; 8131 8132 // (Upper - Lower [- 1] + Step) / Step or (Upper - Lower) / Step 8133 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get()); 8134 if (!Diff.isUsable()) 8135 return nullptr; 8136 8137 return Diff.get(); 8138 } 8139 8140 /// Build the expression to calculate the number of iterations. 8141 Expr *OpenMPIterationSpaceChecker::buildNumIterations( 8142 Scope *S, ArrayRef<LoopIterationSpace> ResultIterSpaces, bool LimitedType, 8143 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 8144 QualType VarType = LCDecl->getType().getNonReferenceType(); 8145 if (!VarType->isIntegerType() && !VarType->isPointerType() && 8146 !SemaRef.getLangOpts().CPlusPlus) 8147 return nullptr; 8148 Expr *LBVal = LB; 8149 Expr *UBVal = UB; 8150 // LB = TestIsLessOp.getValue() ? min(LB(MinVal), LB(MaxVal)) : 8151 // max(LB(MinVal), LB(MaxVal)) 8152 if (InitDependOnLC) { 8153 const LoopIterationSpace &IS = ResultIterSpaces[*InitDependOnLC - 1]; 8154 if (!IS.MinValue || !IS.MaxValue) 8155 return nullptr; 8156 // OuterVar = Min 8157 ExprResult MinValue = 8158 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MinValue); 8159 if (!MinValue.isUsable()) 8160 return nullptr; 8161 8162 ExprResult LBMinVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign, 8163 IS.CounterVar, MinValue.get()); 8164 if (!LBMinVal.isUsable()) 8165 return nullptr; 8166 // OuterVar = Min, LBVal 8167 LBMinVal = 8168 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, LBMinVal.get(), LBVal); 8169 if (!LBMinVal.isUsable()) 8170 return nullptr; 8171 // (OuterVar = Min, LBVal) 8172 LBMinVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, LBMinVal.get()); 8173 if (!LBMinVal.isUsable()) 8174 return nullptr; 8175 8176 // OuterVar = Max 8177 ExprResult MaxValue = 8178 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MaxValue); 8179 if (!MaxValue.isUsable()) 8180 return nullptr; 8181 8182 ExprResult LBMaxVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign, 8183 IS.CounterVar, MaxValue.get()); 8184 if (!LBMaxVal.isUsable()) 8185 return nullptr; 8186 // OuterVar = Max, LBVal 8187 LBMaxVal = 8188 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, LBMaxVal.get(), LBVal); 8189 if (!LBMaxVal.isUsable()) 8190 return nullptr; 8191 // (OuterVar = Max, LBVal) 8192 LBMaxVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, LBMaxVal.get()); 8193 if (!LBMaxVal.isUsable()) 8194 return nullptr; 8195 8196 Expr *LBMin = tryBuildCapture(SemaRef, LBMinVal.get(), Captures).get(); 8197 Expr *LBMax = tryBuildCapture(SemaRef, LBMaxVal.get(), Captures).get(); 8198 if (!LBMin || !LBMax) 8199 return nullptr; 8200 // LB(MinVal) < LB(MaxVal) 8201 ExprResult MinLessMaxRes = 8202 SemaRef.BuildBinOp(S, DefaultLoc, BO_LT, LBMin, LBMax); 8203 if (!MinLessMaxRes.isUsable()) 8204 return nullptr; 8205 Expr *MinLessMax = 8206 tryBuildCapture(SemaRef, MinLessMaxRes.get(), Captures).get(); 8207 if (!MinLessMax) 8208 return nullptr; 8209 if (TestIsLessOp.getValue()) { 8210 // LB(MinVal) < LB(MaxVal) ? LB(MinVal) : LB(MaxVal) - min(LB(MinVal), 8211 // LB(MaxVal)) 8212 ExprResult MinLB = SemaRef.ActOnConditionalOp(DefaultLoc, DefaultLoc, 8213 MinLessMax, LBMin, LBMax); 8214 if (!MinLB.isUsable()) 8215 return nullptr; 8216 LBVal = MinLB.get(); 8217 } else { 8218 // LB(MinVal) < LB(MaxVal) ? LB(MaxVal) : LB(MinVal) - max(LB(MinVal), 8219 // LB(MaxVal)) 8220 ExprResult MaxLB = SemaRef.ActOnConditionalOp(DefaultLoc, DefaultLoc, 8221 MinLessMax, LBMax, LBMin); 8222 if (!MaxLB.isUsable()) 8223 return nullptr; 8224 LBVal = MaxLB.get(); 8225 } 8226 } 8227 // UB = TestIsLessOp.getValue() ? max(UB(MinVal), UB(MaxVal)) : 8228 // min(UB(MinVal), UB(MaxVal)) 8229 if (CondDependOnLC) { 8230 const LoopIterationSpace &IS = ResultIterSpaces[*CondDependOnLC - 1]; 8231 if (!IS.MinValue || !IS.MaxValue) 8232 return nullptr; 8233 // OuterVar = Min 8234 ExprResult MinValue = 8235 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MinValue); 8236 if (!MinValue.isUsable()) 8237 return nullptr; 8238 8239 ExprResult UBMinVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign, 8240 IS.CounterVar, MinValue.get()); 8241 if (!UBMinVal.isUsable()) 8242 return nullptr; 8243 // OuterVar = Min, UBVal 8244 UBMinVal = 8245 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, UBMinVal.get(), UBVal); 8246 if (!UBMinVal.isUsable()) 8247 return nullptr; 8248 // (OuterVar = Min, UBVal) 8249 UBMinVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, UBMinVal.get()); 8250 if (!UBMinVal.isUsable()) 8251 return nullptr; 8252 8253 // OuterVar = Max 8254 ExprResult MaxValue = 8255 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MaxValue); 8256 if (!MaxValue.isUsable()) 8257 return nullptr; 8258 8259 ExprResult UBMaxVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign, 8260 IS.CounterVar, MaxValue.get()); 8261 if (!UBMaxVal.isUsable()) 8262 return nullptr; 8263 // OuterVar = Max, UBVal 8264 UBMaxVal = 8265 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, UBMaxVal.get(), UBVal); 8266 if (!UBMaxVal.isUsable()) 8267 return nullptr; 8268 // (OuterVar = Max, UBVal) 8269 UBMaxVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, UBMaxVal.get()); 8270 if (!UBMaxVal.isUsable()) 8271 return nullptr; 8272 8273 Expr *UBMin = tryBuildCapture(SemaRef, UBMinVal.get(), Captures).get(); 8274 Expr *UBMax = tryBuildCapture(SemaRef, UBMaxVal.get(), Captures).get(); 8275 if (!UBMin || !UBMax) 8276 return nullptr; 8277 // UB(MinVal) > UB(MaxVal) 8278 ExprResult MinGreaterMaxRes = 8279 SemaRef.BuildBinOp(S, DefaultLoc, BO_GT, UBMin, UBMax); 8280 if (!MinGreaterMaxRes.isUsable()) 8281 return nullptr; 8282 Expr *MinGreaterMax = 8283 tryBuildCapture(SemaRef, MinGreaterMaxRes.get(), Captures).get(); 8284 if (!MinGreaterMax) 8285 return nullptr; 8286 if (TestIsLessOp.getValue()) { 8287 // UB(MinVal) > UB(MaxVal) ? UB(MinVal) : UB(MaxVal) - max(UB(MinVal), 8288 // UB(MaxVal)) 8289 ExprResult MaxUB = SemaRef.ActOnConditionalOp( 8290 DefaultLoc, DefaultLoc, MinGreaterMax, UBMin, UBMax); 8291 if (!MaxUB.isUsable()) 8292 return nullptr; 8293 UBVal = MaxUB.get(); 8294 } else { 8295 // UB(MinVal) > UB(MaxVal) ? UB(MaxVal) : UB(MinVal) - min(UB(MinVal), 8296 // UB(MaxVal)) 8297 ExprResult MinUB = SemaRef.ActOnConditionalOp( 8298 DefaultLoc, DefaultLoc, MinGreaterMax, UBMax, UBMin); 8299 if (!MinUB.isUsable()) 8300 return nullptr; 8301 UBVal = MinUB.get(); 8302 } 8303 } 8304 Expr *UBExpr = TestIsLessOp.getValue() ? UBVal : LBVal; 8305 Expr *LBExpr = TestIsLessOp.getValue() ? LBVal : UBVal; 8306 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get(); 8307 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get(); 8308 if (!Upper || !Lower) 8309 return nullptr; 8310 8311 ExprResult Diff = calculateNumIters(SemaRef, S, DefaultLoc, Lower, Upper, 8312 Step, VarType, TestIsStrictOp, 8313 /*RoundToStep=*/true, Captures); 8314 if (!Diff.isUsable()) 8315 return nullptr; 8316 8317 // OpenMP runtime requires 32-bit or 64-bit loop variables. 8318 QualType Type = Diff.get()->getType(); 8319 ASTContext &C = SemaRef.Context; 8320 bool UseVarType = VarType->hasIntegerRepresentation() && 8321 C.getTypeSize(Type) > C.getTypeSize(VarType); 8322 if (!Type->isIntegerType() || UseVarType) { 8323 unsigned NewSize = 8324 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type); 8325 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation() 8326 : Type->hasSignedIntegerRepresentation(); 8327 Type = C.getIntTypeForBitwidth(NewSize, IsSigned); 8328 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) { 8329 Diff = SemaRef.PerformImplicitConversion( 8330 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true); 8331 if (!Diff.isUsable()) 8332 return nullptr; 8333 } 8334 } 8335 if (LimitedType) { 8336 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32; 8337 if (NewSize != C.getTypeSize(Type)) { 8338 if (NewSize < C.getTypeSize(Type)) { 8339 assert(NewSize == 64 && "incorrect loop var size"); 8340 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var) 8341 << InitSrcRange << ConditionSrcRange; 8342 } 8343 QualType NewType = C.getIntTypeForBitwidth( 8344 NewSize, Type->hasSignedIntegerRepresentation() || 8345 C.getTypeSize(Type) < NewSize); 8346 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) { 8347 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType, 8348 Sema::AA_Converting, true); 8349 if (!Diff.isUsable()) 8350 return nullptr; 8351 } 8352 } 8353 } 8354 8355 return Diff.get(); 8356 } 8357 8358 std::pair<Expr *, Expr *> OpenMPIterationSpaceChecker::buildMinMaxValues( 8359 Scope *S, llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 8360 // Do not build for iterators, they cannot be used in non-rectangular loop 8361 // nests. 8362 if (LCDecl->getType()->isRecordType()) 8363 return std::make_pair(nullptr, nullptr); 8364 // If we subtract, the min is in the condition, otherwise the min is in the 8365 // init value. 8366 Expr *MinExpr = nullptr; 8367 Expr *MaxExpr = nullptr; 8368 Expr *LBExpr = TestIsLessOp.getValue() ? LB : UB; 8369 Expr *UBExpr = TestIsLessOp.getValue() ? UB : LB; 8370 bool LBNonRect = TestIsLessOp.getValue() ? InitDependOnLC.hasValue() 8371 : CondDependOnLC.hasValue(); 8372 bool UBNonRect = TestIsLessOp.getValue() ? CondDependOnLC.hasValue() 8373 : InitDependOnLC.hasValue(); 8374 Expr *Lower = 8375 LBNonRect ? LBExpr : tryBuildCapture(SemaRef, LBExpr, Captures).get(); 8376 Expr *Upper = 8377 UBNonRect ? UBExpr : tryBuildCapture(SemaRef, UBExpr, Captures).get(); 8378 if (!Upper || !Lower) 8379 return std::make_pair(nullptr, nullptr); 8380 8381 if (TestIsLessOp.getValue()) 8382 MinExpr = Lower; 8383 else 8384 MaxExpr = Upper; 8385 8386 // Build minimum/maximum value based on number of iterations. 8387 QualType VarType = LCDecl->getType().getNonReferenceType(); 8388 8389 ExprResult Diff = calculateNumIters(SemaRef, S, DefaultLoc, Lower, Upper, 8390 Step, VarType, TestIsStrictOp, 8391 /*RoundToStep=*/false, Captures); 8392 if (!Diff.isUsable()) 8393 return std::make_pair(nullptr, nullptr); 8394 8395 // ((Upper - Lower [- 1]) / Step) * Step 8396 // Parentheses (for dumping/debugging purposes only). 8397 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 8398 if (!Diff.isUsable()) 8399 return std::make_pair(nullptr, nullptr); 8400 8401 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures); 8402 if (!NewStep.isUsable()) 8403 return std::make_pair(nullptr, nullptr); 8404 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Mul, Diff.get(), NewStep.get()); 8405 if (!Diff.isUsable()) 8406 return std::make_pair(nullptr, nullptr); 8407 8408 // Parentheses (for dumping/debugging purposes only). 8409 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 8410 if (!Diff.isUsable()) 8411 return std::make_pair(nullptr, nullptr); 8412 8413 // Convert to the ptrdiff_t, if original type is pointer. 8414 if (VarType->isAnyPointerType() && 8415 !SemaRef.Context.hasSameType( 8416 Diff.get()->getType(), 8417 SemaRef.Context.getUnsignedPointerDiffType())) { 8418 Diff = SemaRef.PerformImplicitConversion( 8419 Diff.get(), SemaRef.Context.getUnsignedPointerDiffType(), 8420 Sema::AA_Converting, /*AllowExplicit=*/true); 8421 } 8422 if (!Diff.isUsable()) 8423 return std::make_pair(nullptr, nullptr); 8424 8425 if (TestIsLessOp.getValue()) { 8426 // MinExpr = Lower; 8427 // MaxExpr = Lower + (((Upper - Lower [- 1]) / Step) * Step) 8428 Diff = SemaRef.BuildBinOp( 8429 S, DefaultLoc, BO_Add, 8430 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Lower).get(), 8431 Diff.get()); 8432 if (!Diff.isUsable()) 8433 return std::make_pair(nullptr, nullptr); 8434 } else { 8435 // MaxExpr = Upper; 8436 // MinExpr = Upper - (((Upper - Lower [- 1]) / Step) * Step) 8437 Diff = SemaRef.BuildBinOp( 8438 S, DefaultLoc, BO_Sub, 8439 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Upper).get(), 8440 Diff.get()); 8441 if (!Diff.isUsable()) 8442 return std::make_pair(nullptr, nullptr); 8443 } 8444 8445 // Convert to the original type. 8446 if (SemaRef.Context.hasSameType(Diff.get()->getType(), VarType)) 8447 Diff = SemaRef.PerformImplicitConversion(Diff.get(), VarType, 8448 Sema::AA_Converting, 8449 /*AllowExplicit=*/true); 8450 if (!Diff.isUsable()) 8451 return std::make_pair(nullptr, nullptr); 8452 8453 Sema::TentativeAnalysisScope Trap(SemaRef); 8454 Diff = SemaRef.ActOnFinishFullExpr(Diff.get(), /*DiscardedValue=*/false); 8455 if (!Diff.isUsable()) 8456 return std::make_pair(nullptr, nullptr); 8457 8458 if (TestIsLessOp.getValue()) 8459 MaxExpr = Diff.get(); 8460 else 8461 MinExpr = Diff.get(); 8462 8463 return std::make_pair(MinExpr, MaxExpr); 8464 } 8465 8466 Expr *OpenMPIterationSpaceChecker::buildFinalCondition(Scope *S) const { 8467 if (InitDependOnLC || CondDependOnLC) 8468 return Condition; 8469 return nullptr; 8470 } 8471 8472 Expr *OpenMPIterationSpaceChecker::buildPreCond( 8473 Scope *S, Expr *Cond, 8474 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 8475 // Do not build a precondition when the condition/initialization is dependent 8476 // to prevent pessimistic early loop exit. 8477 // TODO: this can be improved by calculating min/max values but not sure that 8478 // it will be very effective. 8479 if (CondDependOnLC || InitDependOnLC) 8480 return SemaRef.PerformImplicitConversion( 8481 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(), 8482 SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting, 8483 /*AllowExplicit=*/true).get(); 8484 8485 // Try to build LB <op> UB, where <op> is <, >, <=, or >=. 8486 Sema::TentativeAnalysisScope Trap(SemaRef); 8487 8488 ExprResult NewLB = tryBuildCapture(SemaRef, LB, Captures); 8489 ExprResult NewUB = tryBuildCapture(SemaRef, UB, Captures); 8490 if (!NewLB.isUsable() || !NewUB.isUsable()) 8491 return nullptr; 8492 8493 ExprResult CondExpr = 8494 SemaRef.BuildBinOp(S, DefaultLoc, 8495 TestIsLessOp.getValue() ? 8496 (TestIsStrictOp ? BO_LT : BO_LE) : 8497 (TestIsStrictOp ? BO_GT : BO_GE), 8498 NewLB.get(), NewUB.get()); 8499 if (CondExpr.isUsable()) { 8500 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(), 8501 SemaRef.Context.BoolTy)) 8502 CondExpr = SemaRef.PerformImplicitConversion( 8503 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting, 8504 /*AllowExplicit=*/true); 8505 } 8506 8507 // Otherwise use original loop condition and evaluate it in runtime. 8508 return CondExpr.isUsable() ? CondExpr.get() : Cond; 8509 } 8510 8511 /// Build reference expression to the counter be used for codegen. 8512 DeclRefExpr *OpenMPIterationSpaceChecker::buildCounterVar( 8513 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 8514 DSAStackTy &DSA) const { 8515 auto *VD = dyn_cast<VarDecl>(LCDecl); 8516 if (!VD) { 8517 VD = SemaRef.isOpenMPCapturedDecl(LCDecl); 8518 DeclRefExpr *Ref = buildDeclRefExpr( 8519 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc); 8520 const DSAStackTy::DSAVarData Data = 8521 DSA.getTopDSA(LCDecl, /*FromParent=*/false); 8522 // If the loop control decl is explicitly marked as private, do not mark it 8523 // as captured again. 8524 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr) 8525 Captures.insert(std::make_pair(LCRef, Ref)); 8526 return Ref; 8527 } 8528 return cast<DeclRefExpr>(LCRef); 8529 } 8530 8531 Expr *OpenMPIterationSpaceChecker::buildPrivateCounterVar() const { 8532 if (LCDecl && !LCDecl->isInvalidDecl()) { 8533 QualType Type = LCDecl->getType().getNonReferenceType(); 8534 VarDecl *PrivateVar = buildVarDecl( 8535 SemaRef, DefaultLoc, Type, LCDecl->getName(), 8536 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr, 8537 isa<VarDecl>(LCDecl) 8538 ? buildDeclRefExpr(SemaRef, cast<VarDecl>(LCDecl), Type, DefaultLoc) 8539 : nullptr); 8540 if (PrivateVar->isInvalidDecl()) 8541 return nullptr; 8542 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc); 8543 } 8544 return nullptr; 8545 } 8546 8547 /// Build initialization of the counter to be used for codegen. 8548 Expr *OpenMPIterationSpaceChecker::buildCounterInit() const { return LB; } 8549 8550 /// Build step of the counter be used for codegen. 8551 Expr *OpenMPIterationSpaceChecker::buildCounterStep() const { return Step; } 8552 8553 Expr *OpenMPIterationSpaceChecker::buildOrderedLoopData( 8554 Scope *S, Expr *Counter, 8555 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, SourceLocation Loc, 8556 Expr *Inc, OverloadedOperatorKind OOK) { 8557 Expr *Cnt = SemaRef.DefaultLvalueConversion(Counter).get(); 8558 if (!Cnt) 8559 return nullptr; 8560 if (Inc) { 8561 assert((OOK == OO_Plus || OOK == OO_Minus) && 8562 "Expected only + or - operations for depend clauses."); 8563 BinaryOperatorKind BOK = (OOK == OO_Plus) ? BO_Add : BO_Sub; 8564 Cnt = SemaRef.BuildBinOp(S, Loc, BOK, Cnt, Inc).get(); 8565 if (!Cnt) 8566 return nullptr; 8567 } 8568 QualType VarType = LCDecl->getType().getNonReferenceType(); 8569 if (!VarType->isIntegerType() && !VarType->isPointerType() && 8570 !SemaRef.getLangOpts().CPlusPlus) 8571 return nullptr; 8572 // Upper - Lower 8573 Expr *Upper = TestIsLessOp.getValue() 8574 ? Cnt 8575 : tryBuildCapture(SemaRef, LB, Captures).get(); 8576 Expr *Lower = TestIsLessOp.getValue() 8577 ? tryBuildCapture(SemaRef, LB, Captures).get() 8578 : Cnt; 8579 if (!Upper || !Lower) 8580 return nullptr; 8581 8582 ExprResult Diff = calculateNumIters( 8583 SemaRef, S, DefaultLoc, Lower, Upper, Step, VarType, 8584 /*TestIsStrictOp=*/false, /*RoundToStep=*/false, Captures); 8585 if (!Diff.isUsable()) 8586 return nullptr; 8587 8588 return Diff.get(); 8589 } 8590 } // namespace 8591 8592 void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) { 8593 assert(getLangOpts().OpenMP && "OpenMP is not active."); 8594 assert(Init && "Expected loop in canonical form."); 8595 unsigned AssociatedLoops = DSAStack->getAssociatedLoops(); 8596 if (AssociatedLoops > 0 && 8597 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { 8598 DSAStack->loopStart(); 8599 OpenMPIterationSpaceChecker ISC(*this, /*SupportsNonRectangular=*/true, 8600 *DSAStack, ForLoc); 8601 if (!ISC.checkAndSetInit(Init, /*EmitDiags=*/false)) { 8602 if (ValueDecl *D = ISC.getLoopDecl()) { 8603 auto *VD = dyn_cast<VarDecl>(D); 8604 DeclRefExpr *PrivateRef = nullptr; 8605 if (!VD) { 8606 if (VarDecl *Private = isOpenMPCapturedDecl(D)) { 8607 VD = Private; 8608 } else { 8609 PrivateRef = buildCapture(*this, D, ISC.getLoopDeclRefExpr(), 8610 /*WithInit=*/false); 8611 VD = cast<VarDecl>(PrivateRef->getDecl()); 8612 } 8613 } 8614 DSAStack->addLoopControlVariable(D, VD); 8615 const Decl *LD = DSAStack->getPossiblyLoopCunter(); 8616 if (LD != D->getCanonicalDecl()) { 8617 DSAStack->resetPossibleLoopCounter(); 8618 if (auto *Var = dyn_cast_or_null<VarDecl>(LD)) 8619 MarkDeclarationsReferencedInExpr( 8620 buildDeclRefExpr(*this, const_cast<VarDecl *>(Var), 8621 Var->getType().getNonLValueExprType(Context), 8622 ForLoc, /*RefersToCapture=*/true)); 8623 } 8624 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 8625 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables 8626 // Referenced in a Construct, C/C++]. The loop iteration variable in the 8627 // associated for-loop of a simd construct with just one associated 8628 // for-loop may be listed in a linear clause with a constant-linear-step 8629 // that is the increment of the associated for-loop. The loop iteration 8630 // variable(s) in the associated for-loop(s) of a for or parallel for 8631 // construct may be listed in a private or lastprivate clause. 8632 DSAStackTy::DSAVarData DVar = 8633 DSAStack->getTopDSA(D, /*FromParent=*/false); 8634 // If LoopVarRefExpr is nullptr it means the corresponding loop variable 8635 // is declared in the loop and it is predetermined as a private. 8636 Expr *LoopDeclRefExpr = ISC.getLoopDeclRefExpr(); 8637 OpenMPClauseKind PredeterminedCKind = 8638 isOpenMPSimdDirective(DKind) 8639 ? (DSAStack->hasMutipleLoops() ? OMPC_lastprivate : OMPC_linear) 8640 : OMPC_private; 8641 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && 8642 DVar.CKind != PredeterminedCKind && DVar.RefExpr && 8643 (LangOpts.OpenMP <= 45 || (DVar.CKind != OMPC_lastprivate && 8644 DVar.CKind != OMPC_private))) || 8645 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop || 8646 DKind == OMPD_master_taskloop || 8647 DKind == OMPD_parallel_master_taskloop || 8648 isOpenMPDistributeDirective(DKind)) && 8649 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && 8650 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) && 8651 (DVar.CKind != OMPC_private || DVar.RefExpr)) { 8652 Diag(Init->getBeginLoc(), diag::err_omp_loop_var_dsa) 8653 << getOpenMPClauseName(DVar.CKind) 8654 << getOpenMPDirectiveName(DKind) 8655 << getOpenMPClauseName(PredeterminedCKind); 8656 if (DVar.RefExpr == nullptr) 8657 DVar.CKind = PredeterminedCKind; 8658 reportOriginalDsa(*this, DSAStack, D, DVar, 8659 /*IsLoopIterVar=*/true); 8660 } else if (LoopDeclRefExpr) { 8661 // Make the loop iteration variable private (for worksharing 8662 // constructs), linear (for simd directives with the only one 8663 // associated loop) or lastprivate (for simd directives with several 8664 // collapsed or ordered loops). 8665 if (DVar.CKind == OMPC_unknown) 8666 DSAStack->addDSA(D, LoopDeclRefExpr, PredeterminedCKind, 8667 PrivateRef); 8668 } 8669 } 8670 } 8671 DSAStack->setAssociatedLoops(AssociatedLoops - 1); 8672 } 8673 } 8674 8675 /// Called on a for stmt to check and extract its iteration space 8676 /// for further processing (such as collapsing). 8677 static bool checkOpenMPIterationSpace( 8678 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA, 8679 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount, 8680 unsigned TotalNestedLoopCount, Expr *CollapseLoopCountExpr, 8681 Expr *OrderedLoopCountExpr, 8682 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA, 8683 llvm::MutableArrayRef<LoopIterationSpace> ResultIterSpaces, 8684 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 8685 bool SupportsNonRectangular = !isOpenMPLoopTransformationDirective(DKind); 8686 // OpenMP [2.9.1, Canonical Loop Form] 8687 // for (init-expr; test-expr; incr-expr) structured-block 8688 // for (range-decl: range-expr) structured-block 8689 if (auto *CanonLoop = dyn_cast_or_null<OMPCanonicalLoop>(S)) 8690 S = CanonLoop->getLoopStmt(); 8691 auto *For = dyn_cast_or_null<ForStmt>(S); 8692 auto *CXXFor = dyn_cast_or_null<CXXForRangeStmt>(S); 8693 // Ranged for is supported only in OpenMP 5.0. 8694 if (!For && (SemaRef.LangOpts.OpenMP <= 45 || !CXXFor)) { 8695 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_not_for) 8696 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr) 8697 << getOpenMPDirectiveName(DKind) << TotalNestedLoopCount 8698 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount; 8699 if (TotalNestedLoopCount > 1) { 8700 if (CollapseLoopCountExpr && OrderedLoopCountExpr) 8701 SemaRef.Diag(DSA.getConstructLoc(), 8702 diag::note_omp_collapse_ordered_expr) 8703 << 2 << CollapseLoopCountExpr->getSourceRange() 8704 << OrderedLoopCountExpr->getSourceRange(); 8705 else if (CollapseLoopCountExpr) 8706 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), 8707 diag::note_omp_collapse_ordered_expr) 8708 << 0 << CollapseLoopCountExpr->getSourceRange(); 8709 else 8710 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), 8711 diag::note_omp_collapse_ordered_expr) 8712 << 1 << OrderedLoopCountExpr->getSourceRange(); 8713 } 8714 return true; 8715 } 8716 assert(((For && For->getBody()) || (CXXFor && CXXFor->getBody())) && 8717 "No loop body."); 8718 8719 OpenMPIterationSpaceChecker ISC(SemaRef, SupportsNonRectangular, DSA, 8720 For ? For->getForLoc() : CXXFor->getForLoc()); 8721 8722 // Check init. 8723 Stmt *Init = For ? For->getInit() : CXXFor->getBeginStmt(); 8724 if (ISC.checkAndSetInit(Init)) 8725 return true; 8726 8727 bool HasErrors = false; 8728 8729 // Check loop variable's type. 8730 if (ValueDecl *LCDecl = ISC.getLoopDecl()) { 8731 // OpenMP [2.6, Canonical Loop Form] 8732 // Var is one of the following: 8733 // A variable of signed or unsigned integer type. 8734 // For C++, a variable of a random access iterator type. 8735 // For C, a variable of a pointer type. 8736 QualType VarType = LCDecl->getType().getNonReferenceType(); 8737 if (!VarType->isDependentType() && !VarType->isIntegerType() && 8738 !VarType->isPointerType() && 8739 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) { 8740 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_variable_type) 8741 << SemaRef.getLangOpts().CPlusPlus; 8742 HasErrors = true; 8743 } 8744 8745 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in 8746 // a Construct 8747 // The loop iteration variable(s) in the associated for-loop(s) of a for or 8748 // parallel for construct is (are) private. 8749 // The loop iteration variable in the associated for-loop of a simd 8750 // construct with just one associated for-loop is linear with a 8751 // constant-linear-step that is the increment of the associated for-loop. 8752 // Exclude loop var from the list of variables with implicitly defined data 8753 // sharing attributes. 8754 VarsWithImplicitDSA.erase(LCDecl); 8755 8756 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars"); 8757 8758 // Check test-expr. 8759 HasErrors |= ISC.checkAndSetCond(For ? For->getCond() : CXXFor->getCond()); 8760 8761 // Check incr-expr. 8762 HasErrors |= ISC.checkAndSetInc(For ? For->getInc() : CXXFor->getInc()); 8763 } 8764 8765 if (ISC.dependent() || SemaRef.CurContext->isDependentContext() || HasErrors) 8766 return HasErrors; 8767 8768 // Build the loop's iteration space representation. 8769 ResultIterSpaces[CurrentNestedLoopCount].PreCond = ISC.buildPreCond( 8770 DSA.getCurScope(), For ? For->getCond() : CXXFor->getCond(), Captures); 8771 ResultIterSpaces[CurrentNestedLoopCount].NumIterations = 8772 ISC.buildNumIterations(DSA.getCurScope(), ResultIterSpaces, 8773 (isOpenMPWorksharingDirective(DKind) || 8774 isOpenMPTaskLoopDirective(DKind) || 8775 isOpenMPDistributeDirective(DKind) || 8776 isOpenMPLoopTransformationDirective(DKind)), 8777 Captures); 8778 ResultIterSpaces[CurrentNestedLoopCount].CounterVar = 8779 ISC.buildCounterVar(Captures, DSA); 8780 ResultIterSpaces[CurrentNestedLoopCount].PrivateCounterVar = 8781 ISC.buildPrivateCounterVar(); 8782 ResultIterSpaces[CurrentNestedLoopCount].CounterInit = ISC.buildCounterInit(); 8783 ResultIterSpaces[CurrentNestedLoopCount].CounterStep = ISC.buildCounterStep(); 8784 ResultIterSpaces[CurrentNestedLoopCount].InitSrcRange = ISC.getInitSrcRange(); 8785 ResultIterSpaces[CurrentNestedLoopCount].CondSrcRange = 8786 ISC.getConditionSrcRange(); 8787 ResultIterSpaces[CurrentNestedLoopCount].IncSrcRange = 8788 ISC.getIncrementSrcRange(); 8789 ResultIterSpaces[CurrentNestedLoopCount].Subtract = ISC.shouldSubtractStep(); 8790 ResultIterSpaces[CurrentNestedLoopCount].IsStrictCompare = 8791 ISC.isStrictTestOp(); 8792 std::tie(ResultIterSpaces[CurrentNestedLoopCount].MinValue, 8793 ResultIterSpaces[CurrentNestedLoopCount].MaxValue) = 8794 ISC.buildMinMaxValues(DSA.getCurScope(), Captures); 8795 ResultIterSpaces[CurrentNestedLoopCount].FinalCondition = 8796 ISC.buildFinalCondition(DSA.getCurScope()); 8797 ResultIterSpaces[CurrentNestedLoopCount].IsNonRectangularLB = 8798 ISC.doesInitDependOnLC(); 8799 ResultIterSpaces[CurrentNestedLoopCount].IsNonRectangularUB = 8800 ISC.doesCondDependOnLC(); 8801 ResultIterSpaces[CurrentNestedLoopCount].LoopDependentIdx = 8802 ISC.getLoopDependentIdx(); 8803 8804 HasErrors |= 8805 (ResultIterSpaces[CurrentNestedLoopCount].PreCond == nullptr || 8806 ResultIterSpaces[CurrentNestedLoopCount].NumIterations == nullptr || 8807 ResultIterSpaces[CurrentNestedLoopCount].CounterVar == nullptr || 8808 ResultIterSpaces[CurrentNestedLoopCount].PrivateCounterVar == nullptr || 8809 ResultIterSpaces[CurrentNestedLoopCount].CounterInit == nullptr || 8810 ResultIterSpaces[CurrentNestedLoopCount].CounterStep == nullptr); 8811 if (!HasErrors && DSA.isOrderedRegion()) { 8812 if (DSA.getOrderedRegionParam().second->getNumForLoops()) { 8813 if (CurrentNestedLoopCount < 8814 DSA.getOrderedRegionParam().second->getLoopNumIterations().size()) { 8815 DSA.getOrderedRegionParam().second->setLoopNumIterations( 8816 CurrentNestedLoopCount, 8817 ResultIterSpaces[CurrentNestedLoopCount].NumIterations); 8818 DSA.getOrderedRegionParam().second->setLoopCounter( 8819 CurrentNestedLoopCount, 8820 ResultIterSpaces[CurrentNestedLoopCount].CounterVar); 8821 } 8822 } 8823 for (auto &Pair : DSA.getDoacrossDependClauses()) { 8824 if (CurrentNestedLoopCount >= Pair.first->getNumLoops()) { 8825 // Erroneous case - clause has some problems. 8826 continue; 8827 } 8828 if (Pair.first->getDependencyKind() == OMPC_DEPEND_sink && 8829 Pair.second.size() <= CurrentNestedLoopCount) { 8830 // Erroneous case - clause has some problems. 8831 Pair.first->setLoopData(CurrentNestedLoopCount, nullptr); 8832 continue; 8833 } 8834 Expr *CntValue; 8835 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source) 8836 CntValue = ISC.buildOrderedLoopData( 8837 DSA.getCurScope(), 8838 ResultIterSpaces[CurrentNestedLoopCount].CounterVar, Captures, 8839 Pair.first->getDependencyLoc()); 8840 else 8841 CntValue = ISC.buildOrderedLoopData( 8842 DSA.getCurScope(), 8843 ResultIterSpaces[CurrentNestedLoopCount].CounterVar, Captures, 8844 Pair.first->getDependencyLoc(), 8845 Pair.second[CurrentNestedLoopCount].first, 8846 Pair.second[CurrentNestedLoopCount].second); 8847 Pair.first->setLoopData(CurrentNestedLoopCount, CntValue); 8848 } 8849 } 8850 8851 return HasErrors; 8852 } 8853 8854 /// Build 'VarRef = Start. 8855 static ExprResult 8856 buildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef, 8857 ExprResult Start, bool IsNonRectangularLB, 8858 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 8859 // Build 'VarRef = Start. 8860 ExprResult NewStart = IsNonRectangularLB 8861 ? Start.get() 8862 : tryBuildCapture(SemaRef, Start.get(), Captures); 8863 if (!NewStart.isUsable()) 8864 return ExprError(); 8865 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(), 8866 VarRef.get()->getType())) { 8867 NewStart = SemaRef.PerformImplicitConversion( 8868 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting, 8869 /*AllowExplicit=*/true); 8870 if (!NewStart.isUsable()) 8871 return ExprError(); 8872 } 8873 8874 ExprResult Init = 8875 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); 8876 return Init; 8877 } 8878 8879 /// Build 'VarRef = Start + Iter * Step'. 8880 static ExprResult buildCounterUpdate( 8881 Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef, 8882 ExprResult Start, ExprResult Iter, ExprResult Step, bool Subtract, 8883 bool IsNonRectangularLB, 8884 llvm::MapVector<const Expr *, DeclRefExpr *> *Captures = nullptr) { 8885 // Add parentheses (for debugging purposes only). 8886 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get()); 8887 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() || 8888 !Step.isUsable()) 8889 return ExprError(); 8890 8891 ExprResult NewStep = Step; 8892 if (Captures) 8893 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures); 8894 if (NewStep.isInvalid()) 8895 return ExprError(); 8896 ExprResult Update = 8897 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get()); 8898 if (!Update.isUsable()) 8899 return ExprError(); 8900 8901 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or 8902 // 'VarRef = Start (+|-) Iter * Step'. 8903 if (!Start.isUsable()) 8904 return ExprError(); 8905 ExprResult NewStart = SemaRef.ActOnParenExpr(Loc, Loc, Start.get()); 8906 if (!NewStart.isUsable()) 8907 return ExprError(); 8908 if (Captures && !IsNonRectangularLB) 8909 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures); 8910 if (NewStart.isInvalid()) 8911 return ExprError(); 8912 8913 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'. 8914 ExprResult SavedUpdate = Update; 8915 ExprResult UpdateVal; 8916 if (VarRef.get()->getType()->isOverloadableType() || 8917 NewStart.get()->getType()->isOverloadableType() || 8918 Update.get()->getType()->isOverloadableType()) { 8919 Sema::TentativeAnalysisScope Trap(SemaRef); 8920 8921 Update = 8922 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); 8923 if (Update.isUsable()) { 8924 UpdateVal = 8925 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign, 8926 VarRef.get(), SavedUpdate.get()); 8927 if (UpdateVal.isUsable()) { 8928 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(), 8929 UpdateVal.get()); 8930 } 8931 } 8932 } 8933 8934 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'. 8935 if (!Update.isUsable() || !UpdateVal.isUsable()) { 8936 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add, 8937 NewStart.get(), SavedUpdate.get()); 8938 if (!Update.isUsable()) 8939 return ExprError(); 8940 8941 if (!SemaRef.Context.hasSameType(Update.get()->getType(), 8942 VarRef.get()->getType())) { 8943 Update = SemaRef.PerformImplicitConversion( 8944 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true); 8945 if (!Update.isUsable()) 8946 return ExprError(); 8947 } 8948 8949 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get()); 8950 } 8951 return Update; 8952 } 8953 8954 /// Convert integer expression \a E to make it have at least \a Bits 8955 /// bits. 8956 static ExprResult widenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) { 8957 if (E == nullptr) 8958 return ExprError(); 8959 ASTContext &C = SemaRef.Context; 8960 QualType OldType = E->getType(); 8961 unsigned HasBits = C.getTypeSize(OldType); 8962 if (HasBits >= Bits) 8963 return ExprResult(E); 8964 // OK to convert to signed, because new type has more bits than old. 8965 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true); 8966 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting, 8967 true); 8968 } 8969 8970 /// Check if the given expression \a E is a constant integer that fits 8971 /// into \a Bits bits. 8972 static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) { 8973 if (E == nullptr) 8974 return false; 8975 if (Optional<llvm::APSInt> Result = 8976 E->getIntegerConstantExpr(SemaRef.Context)) 8977 return Signed ? Result->isSignedIntN(Bits) : Result->isIntN(Bits); 8978 return false; 8979 } 8980 8981 /// Build preinits statement for the given declarations. 8982 static Stmt *buildPreInits(ASTContext &Context, 8983 MutableArrayRef<Decl *> PreInits) { 8984 if (!PreInits.empty()) { 8985 return new (Context) DeclStmt( 8986 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()), 8987 SourceLocation(), SourceLocation()); 8988 } 8989 return nullptr; 8990 } 8991 8992 /// Build preinits statement for the given declarations. 8993 static Stmt * 8994 buildPreInits(ASTContext &Context, 8995 const llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 8996 if (!Captures.empty()) { 8997 SmallVector<Decl *, 16> PreInits; 8998 for (const auto &Pair : Captures) 8999 PreInits.push_back(Pair.second->getDecl()); 9000 return buildPreInits(Context, PreInits); 9001 } 9002 return nullptr; 9003 } 9004 9005 /// Build postupdate expression for the given list of postupdates expressions. 9006 static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) { 9007 Expr *PostUpdate = nullptr; 9008 if (!PostUpdates.empty()) { 9009 for (Expr *E : PostUpdates) { 9010 Expr *ConvE = S.BuildCStyleCastExpr( 9011 E->getExprLoc(), 9012 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy), 9013 E->getExprLoc(), E) 9014 .get(); 9015 PostUpdate = PostUpdate 9016 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma, 9017 PostUpdate, ConvE) 9018 .get() 9019 : ConvE; 9020 } 9021 } 9022 return PostUpdate; 9023 } 9024 9025 /// Called on a for stmt to check itself and nested loops (if any). 9026 /// \return Returns 0 if one of the collapsed stmts is not canonical for loop, 9027 /// number of collapsed loops otherwise. 9028 static unsigned 9029 checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr, 9030 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef, 9031 DSAStackTy &DSA, 9032 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA, 9033 OMPLoopBasedDirective::HelperExprs &Built) { 9034 unsigned NestedLoopCount = 1; 9035 bool SupportsNonPerfectlyNested = (SemaRef.LangOpts.OpenMP >= 50) && 9036 !isOpenMPLoopTransformationDirective(DKind); 9037 9038 if (CollapseLoopCountExpr) { 9039 // Found 'collapse' clause - calculate collapse number. 9040 Expr::EvalResult Result; 9041 if (!CollapseLoopCountExpr->isValueDependent() && 9042 CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) { 9043 NestedLoopCount = Result.Val.getInt().getLimitedValue(); 9044 } else { 9045 Built.clear(/*Size=*/1); 9046 return 1; 9047 } 9048 } 9049 unsigned OrderedLoopCount = 1; 9050 if (OrderedLoopCountExpr) { 9051 // Found 'ordered' clause - calculate collapse number. 9052 Expr::EvalResult EVResult; 9053 if (!OrderedLoopCountExpr->isValueDependent() && 9054 OrderedLoopCountExpr->EvaluateAsInt(EVResult, 9055 SemaRef.getASTContext())) { 9056 llvm::APSInt Result = EVResult.Val.getInt(); 9057 if (Result.getLimitedValue() < NestedLoopCount) { 9058 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), 9059 diag::err_omp_wrong_ordered_loop_count) 9060 << OrderedLoopCountExpr->getSourceRange(); 9061 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), 9062 diag::note_collapse_loop_count) 9063 << CollapseLoopCountExpr->getSourceRange(); 9064 } 9065 OrderedLoopCount = Result.getLimitedValue(); 9066 } else { 9067 Built.clear(/*Size=*/1); 9068 return 1; 9069 } 9070 } 9071 // This is helper routine for loop directives (e.g., 'for', 'simd', 9072 // 'for simd', etc.). 9073 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 9074 unsigned NumLoops = std::max(OrderedLoopCount, NestedLoopCount); 9075 SmallVector<LoopIterationSpace, 4> IterSpaces(NumLoops); 9076 if (!OMPLoopBasedDirective::doForAllLoops( 9077 AStmt->IgnoreContainers(!isOpenMPLoopTransformationDirective(DKind)), 9078 SupportsNonPerfectlyNested, NumLoops, 9079 [DKind, &SemaRef, &DSA, NumLoops, NestedLoopCount, 9080 CollapseLoopCountExpr, OrderedLoopCountExpr, &VarsWithImplicitDSA, 9081 &IterSpaces, &Captures](unsigned Cnt, Stmt *CurStmt) { 9082 if (checkOpenMPIterationSpace( 9083 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount, 9084 NumLoops, CollapseLoopCountExpr, OrderedLoopCountExpr, 9085 VarsWithImplicitDSA, IterSpaces, Captures)) 9086 return true; 9087 if (Cnt > 0 && Cnt >= NestedLoopCount && 9088 IterSpaces[Cnt].CounterVar) { 9089 // Handle initialization of captured loop iterator variables. 9090 auto *DRE = cast<DeclRefExpr>(IterSpaces[Cnt].CounterVar); 9091 if (isa<OMPCapturedExprDecl>(DRE->getDecl())) { 9092 Captures[DRE] = DRE; 9093 } 9094 } 9095 return false; 9096 }, 9097 [&SemaRef, &Captures](OMPLoopTransformationDirective *Transform) { 9098 Stmt *DependentPreInits = Transform->getPreInits(); 9099 if (!DependentPreInits) 9100 return; 9101 for (Decl *C : cast<DeclStmt>(DependentPreInits)->getDeclGroup()) { 9102 auto *D = cast<VarDecl>(C); 9103 DeclRefExpr *Ref = buildDeclRefExpr(SemaRef, D, D->getType(), 9104 Transform->getBeginLoc()); 9105 Captures[Ref] = Ref; 9106 } 9107 })) 9108 return 0; 9109 9110 Built.clear(/* size */ NestedLoopCount); 9111 9112 if (SemaRef.CurContext->isDependentContext()) 9113 return NestedLoopCount; 9114 9115 // An example of what is generated for the following code: 9116 // 9117 // #pragma omp simd collapse(2) ordered(2) 9118 // for (i = 0; i < NI; ++i) 9119 // for (k = 0; k < NK; ++k) 9120 // for (j = J0; j < NJ; j+=2) { 9121 // <loop body> 9122 // } 9123 // 9124 // We generate the code below. 9125 // Note: the loop body may be outlined in CodeGen. 9126 // Note: some counters may be C++ classes, operator- is used to find number of 9127 // iterations and operator+= to calculate counter value. 9128 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32 9129 // or i64 is currently supported). 9130 // 9131 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2)) 9132 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) { 9133 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2); 9134 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2; 9135 // // similar updates for vars in clauses (e.g. 'linear') 9136 // <loop body (using local i and j)> 9137 // } 9138 // i = NI; // assign final values of counters 9139 // j = NJ; 9140 // 9141 9142 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are 9143 // the iteration counts of the collapsed for loops. 9144 // Precondition tests if there is at least one iteration (all conditions are 9145 // true). 9146 auto PreCond = ExprResult(IterSpaces[0].PreCond); 9147 Expr *N0 = IterSpaces[0].NumIterations; 9148 ExprResult LastIteration32 = 9149 widenIterationCount(/*Bits=*/32, 9150 SemaRef 9151 .PerformImplicitConversion( 9152 N0->IgnoreImpCasts(), N0->getType(), 9153 Sema::AA_Converting, /*AllowExplicit=*/true) 9154 .get(), 9155 SemaRef); 9156 ExprResult LastIteration64 = widenIterationCount( 9157 /*Bits=*/64, 9158 SemaRef 9159 .PerformImplicitConversion(N0->IgnoreImpCasts(), N0->getType(), 9160 Sema::AA_Converting, 9161 /*AllowExplicit=*/true) 9162 .get(), 9163 SemaRef); 9164 9165 if (!LastIteration32.isUsable() || !LastIteration64.isUsable()) 9166 return NestedLoopCount; 9167 9168 ASTContext &C = SemaRef.Context; 9169 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32; 9170 9171 Scope *CurScope = DSA.getCurScope(); 9172 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) { 9173 if (PreCond.isUsable()) { 9174 PreCond = 9175 SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd, 9176 PreCond.get(), IterSpaces[Cnt].PreCond); 9177 } 9178 Expr *N = IterSpaces[Cnt].NumIterations; 9179 SourceLocation Loc = N->getExprLoc(); 9180 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32; 9181 if (LastIteration32.isUsable()) 9182 LastIteration32 = SemaRef.BuildBinOp( 9183 CurScope, Loc, BO_Mul, LastIteration32.get(), 9184 SemaRef 9185 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), 9186 Sema::AA_Converting, 9187 /*AllowExplicit=*/true) 9188 .get()); 9189 if (LastIteration64.isUsable()) 9190 LastIteration64 = SemaRef.BuildBinOp( 9191 CurScope, Loc, BO_Mul, LastIteration64.get(), 9192 SemaRef 9193 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), 9194 Sema::AA_Converting, 9195 /*AllowExplicit=*/true) 9196 .get()); 9197 } 9198 9199 // Choose either the 32-bit or 64-bit version. 9200 ExprResult LastIteration = LastIteration64; 9201 if (SemaRef.getLangOpts().OpenMPOptimisticCollapse || 9202 (LastIteration32.isUsable() && 9203 C.getTypeSize(LastIteration32.get()->getType()) == 32 && 9204 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 || 9205 fitsInto( 9206 /*Bits=*/32, 9207 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(), 9208 LastIteration64.get(), SemaRef)))) 9209 LastIteration = LastIteration32; 9210 QualType VType = LastIteration.get()->getType(); 9211 QualType RealVType = VType; 9212 QualType StrideVType = VType; 9213 if (isOpenMPTaskLoopDirective(DKind)) { 9214 VType = 9215 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0); 9216 StrideVType = 9217 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1); 9218 } 9219 9220 if (!LastIteration.isUsable()) 9221 return 0; 9222 9223 // Save the number of iterations. 9224 ExprResult NumIterations = LastIteration; 9225 { 9226 LastIteration = SemaRef.BuildBinOp( 9227 CurScope, LastIteration.get()->getExprLoc(), BO_Sub, 9228 LastIteration.get(), 9229 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 9230 if (!LastIteration.isUsable()) 9231 return 0; 9232 } 9233 9234 // Calculate the last iteration number beforehand instead of doing this on 9235 // each iteration. Do not do this if the number of iterations may be kfold-ed. 9236 bool IsConstant = LastIteration.get()->isIntegerConstantExpr(SemaRef.Context); 9237 ExprResult CalcLastIteration; 9238 if (!IsConstant) { 9239 ExprResult SaveRef = 9240 tryBuildCapture(SemaRef, LastIteration.get(), Captures); 9241 LastIteration = SaveRef; 9242 9243 // Prepare SaveRef + 1. 9244 NumIterations = SemaRef.BuildBinOp( 9245 CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(), 9246 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 9247 if (!NumIterations.isUsable()) 9248 return 0; 9249 } 9250 9251 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin(); 9252 9253 // Build variables passed into runtime, necessary for worksharing directives. 9254 ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB; 9255 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || 9256 isOpenMPDistributeDirective(DKind) || 9257 isOpenMPLoopTransformationDirective(DKind)) { 9258 // Lower bound variable, initialized with zero. 9259 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb"); 9260 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc); 9261 SemaRef.AddInitializerToDecl(LBDecl, 9262 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 9263 /*DirectInit*/ false); 9264 9265 // Upper bound variable, initialized with last iteration number. 9266 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub"); 9267 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc); 9268 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(), 9269 /*DirectInit*/ false); 9270 9271 // A 32-bit variable-flag where runtime returns 1 for the last iteration. 9272 // This will be used to implement clause 'lastprivate'. 9273 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true); 9274 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last"); 9275 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc); 9276 SemaRef.AddInitializerToDecl(ILDecl, 9277 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 9278 /*DirectInit*/ false); 9279 9280 // Stride variable returned by runtime (we initialize it to 1 by default). 9281 VarDecl *STDecl = 9282 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride"); 9283 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc); 9284 SemaRef.AddInitializerToDecl(STDecl, 9285 SemaRef.ActOnIntegerConstant(InitLoc, 1).get(), 9286 /*DirectInit*/ false); 9287 9288 // Build expression: UB = min(UB, LastIteration) 9289 // It is necessary for CodeGen of directives with static scheduling. 9290 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT, 9291 UB.get(), LastIteration.get()); 9292 ExprResult CondOp = SemaRef.ActOnConditionalOp( 9293 LastIteration.get()->getExprLoc(), InitLoc, IsUBGreater.get(), 9294 LastIteration.get(), UB.get()); 9295 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(), 9296 CondOp.get()); 9297 EUB = SemaRef.ActOnFinishFullExpr(EUB.get(), /*DiscardedValue*/ false); 9298 9299 // If we have a combined directive that combines 'distribute', 'for' or 9300 // 'simd' we need to be able to access the bounds of the schedule of the 9301 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained 9302 // by scheduling 'distribute' have to be passed to the schedule of 'for'. 9303 if (isOpenMPLoopBoundSharingDirective(DKind)) { 9304 // Lower bound variable, initialized with zero. 9305 VarDecl *CombLBDecl = 9306 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb"); 9307 CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc); 9308 SemaRef.AddInitializerToDecl( 9309 CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 9310 /*DirectInit*/ false); 9311 9312 // Upper bound variable, initialized with last iteration number. 9313 VarDecl *CombUBDecl = 9314 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub"); 9315 CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc); 9316 SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(), 9317 /*DirectInit*/ false); 9318 9319 ExprResult CombIsUBGreater = SemaRef.BuildBinOp( 9320 CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get()); 9321 ExprResult CombCondOp = 9322 SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(), 9323 LastIteration.get(), CombUB.get()); 9324 CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(), 9325 CombCondOp.get()); 9326 CombEUB = 9327 SemaRef.ActOnFinishFullExpr(CombEUB.get(), /*DiscardedValue*/ false); 9328 9329 const CapturedDecl *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl(); 9330 // We expect to have at least 2 more parameters than the 'parallel' 9331 // directive does - the lower and upper bounds of the previous schedule. 9332 assert(CD->getNumParams() >= 4 && 9333 "Unexpected number of parameters in loop combined directive"); 9334 9335 // Set the proper type for the bounds given what we learned from the 9336 // enclosed loops. 9337 ImplicitParamDecl *PrevLBDecl = CD->getParam(/*PrevLB=*/2); 9338 ImplicitParamDecl *PrevUBDecl = CD->getParam(/*PrevUB=*/3); 9339 9340 // Previous lower and upper bounds are obtained from the region 9341 // parameters. 9342 PrevLB = 9343 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc); 9344 PrevUB = 9345 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc); 9346 } 9347 } 9348 9349 // Build the iteration variable and its initialization before loop. 9350 ExprResult IV; 9351 ExprResult Init, CombInit; 9352 { 9353 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv"); 9354 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc); 9355 Expr *RHS = (isOpenMPWorksharingDirective(DKind) || 9356 isOpenMPTaskLoopDirective(DKind) || 9357 isOpenMPDistributeDirective(DKind) || 9358 isOpenMPLoopTransformationDirective(DKind)) 9359 ? LB.get() 9360 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get(); 9361 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS); 9362 Init = SemaRef.ActOnFinishFullExpr(Init.get(), /*DiscardedValue*/ false); 9363 9364 if (isOpenMPLoopBoundSharingDirective(DKind)) { 9365 Expr *CombRHS = 9366 (isOpenMPWorksharingDirective(DKind) || 9367 isOpenMPTaskLoopDirective(DKind) || 9368 isOpenMPDistributeDirective(DKind)) 9369 ? CombLB.get() 9370 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get(); 9371 CombInit = 9372 SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS); 9373 CombInit = 9374 SemaRef.ActOnFinishFullExpr(CombInit.get(), /*DiscardedValue*/ false); 9375 } 9376 } 9377 9378 bool UseStrictCompare = 9379 RealVType->hasUnsignedIntegerRepresentation() && 9380 llvm::all_of(IterSpaces, [](const LoopIterationSpace &LIS) { 9381 return LIS.IsStrictCompare; 9382 }); 9383 // Loop condition (IV < NumIterations) or (IV <= UB or IV < UB + 1 (for 9384 // unsigned IV)) for worksharing loops. 9385 SourceLocation CondLoc = AStmt->getBeginLoc(); 9386 Expr *BoundUB = UB.get(); 9387 if (UseStrictCompare) { 9388 BoundUB = 9389 SemaRef 9390 .BuildBinOp(CurScope, CondLoc, BO_Add, BoundUB, 9391 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()) 9392 .get(); 9393 BoundUB = 9394 SemaRef.ActOnFinishFullExpr(BoundUB, /*DiscardedValue*/ false).get(); 9395 } 9396 ExprResult Cond = 9397 (isOpenMPWorksharingDirective(DKind) || 9398 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind) || 9399 isOpenMPLoopTransformationDirective(DKind)) 9400 ? SemaRef.BuildBinOp(CurScope, CondLoc, 9401 UseStrictCompare ? BO_LT : BO_LE, IV.get(), 9402 BoundUB) 9403 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(), 9404 NumIterations.get()); 9405 ExprResult CombDistCond; 9406 if (isOpenMPLoopBoundSharingDirective(DKind)) { 9407 CombDistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(), 9408 NumIterations.get()); 9409 } 9410 9411 ExprResult CombCond; 9412 if (isOpenMPLoopBoundSharingDirective(DKind)) { 9413 Expr *BoundCombUB = CombUB.get(); 9414 if (UseStrictCompare) { 9415 BoundCombUB = 9416 SemaRef 9417 .BuildBinOp( 9418 CurScope, CondLoc, BO_Add, BoundCombUB, 9419 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()) 9420 .get(); 9421 BoundCombUB = 9422 SemaRef.ActOnFinishFullExpr(BoundCombUB, /*DiscardedValue*/ false) 9423 .get(); 9424 } 9425 CombCond = 9426 SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, 9427 IV.get(), BoundCombUB); 9428 } 9429 // Loop increment (IV = IV + 1) 9430 SourceLocation IncLoc = AStmt->getBeginLoc(); 9431 ExprResult Inc = 9432 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(), 9433 SemaRef.ActOnIntegerConstant(IncLoc, 1).get()); 9434 if (!Inc.isUsable()) 9435 return 0; 9436 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get()); 9437 Inc = SemaRef.ActOnFinishFullExpr(Inc.get(), /*DiscardedValue*/ false); 9438 if (!Inc.isUsable()) 9439 return 0; 9440 9441 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST). 9442 // Used for directives with static scheduling. 9443 // In combined construct, add combined version that use CombLB and CombUB 9444 // base variables for the update 9445 ExprResult NextLB, NextUB, CombNextLB, CombNextUB; 9446 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || 9447 isOpenMPDistributeDirective(DKind) || 9448 isOpenMPLoopTransformationDirective(DKind)) { 9449 // LB + ST 9450 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get()); 9451 if (!NextLB.isUsable()) 9452 return 0; 9453 // LB = LB + ST 9454 NextLB = 9455 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get()); 9456 NextLB = 9457 SemaRef.ActOnFinishFullExpr(NextLB.get(), /*DiscardedValue*/ false); 9458 if (!NextLB.isUsable()) 9459 return 0; 9460 // UB + ST 9461 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get()); 9462 if (!NextUB.isUsable()) 9463 return 0; 9464 // UB = UB + ST 9465 NextUB = 9466 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get()); 9467 NextUB = 9468 SemaRef.ActOnFinishFullExpr(NextUB.get(), /*DiscardedValue*/ false); 9469 if (!NextUB.isUsable()) 9470 return 0; 9471 if (isOpenMPLoopBoundSharingDirective(DKind)) { 9472 CombNextLB = 9473 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get()); 9474 if (!NextLB.isUsable()) 9475 return 0; 9476 // LB = LB + ST 9477 CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(), 9478 CombNextLB.get()); 9479 CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get(), 9480 /*DiscardedValue*/ false); 9481 if (!CombNextLB.isUsable()) 9482 return 0; 9483 // UB + ST 9484 CombNextUB = 9485 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get()); 9486 if (!CombNextUB.isUsable()) 9487 return 0; 9488 // UB = UB + ST 9489 CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(), 9490 CombNextUB.get()); 9491 CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get(), 9492 /*DiscardedValue*/ false); 9493 if (!CombNextUB.isUsable()) 9494 return 0; 9495 } 9496 } 9497 9498 // Create increment expression for distribute loop when combined in a same 9499 // directive with for as IV = IV + ST; ensure upper bound expression based 9500 // on PrevUB instead of NumIterations - used to implement 'for' when found 9501 // in combination with 'distribute', like in 'distribute parallel for' 9502 SourceLocation DistIncLoc = AStmt->getBeginLoc(); 9503 ExprResult DistCond, DistInc, PrevEUB, ParForInDistCond; 9504 if (isOpenMPLoopBoundSharingDirective(DKind)) { 9505 DistCond = SemaRef.BuildBinOp( 9506 CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, IV.get(), BoundUB); 9507 assert(DistCond.isUsable() && "distribute cond expr was not built"); 9508 9509 DistInc = 9510 SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get()); 9511 assert(DistInc.isUsable() && "distribute inc expr was not built"); 9512 DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(), 9513 DistInc.get()); 9514 DistInc = 9515 SemaRef.ActOnFinishFullExpr(DistInc.get(), /*DiscardedValue*/ false); 9516 assert(DistInc.isUsable() && "distribute inc expr was not built"); 9517 9518 // Build expression: UB = min(UB, prevUB) for #for in composite or combined 9519 // construct 9520 ExprResult NewPrevUB = PrevUB; 9521 SourceLocation DistEUBLoc = AStmt->getBeginLoc(); 9522 if (!SemaRef.Context.hasSameType(UB.get()->getType(), 9523 PrevUB.get()->getType())) { 9524 NewPrevUB = SemaRef.BuildCStyleCastExpr( 9525 DistEUBLoc, 9526 SemaRef.Context.getTrivialTypeSourceInfo(UB.get()->getType()), 9527 DistEUBLoc, NewPrevUB.get()); 9528 if (!NewPrevUB.isUsable()) 9529 return 0; 9530 } 9531 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, 9532 UB.get(), NewPrevUB.get()); 9533 ExprResult CondOp = SemaRef.ActOnConditionalOp( 9534 DistEUBLoc, DistEUBLoc, IsUBGreater.get(), NewPrevUB.get(), UB.get()); 9535 PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(), 9536 CondOp.get()); 9537 PrevEUB = 9538 SemaRef.ActOnFinishFullExpr(PrevEUB.get(), /*DiscardedValue*/ false); 9539 9540 // Build IV <= PrevUB or IV < PrevUB + 1 for unsigned IV to be used in 9541 // parallel for is in combination with a distribute directive with 9542 // schedule(static, 1) 9543 Expr *BoundPrevUB = PrevUB.get(); 9544 if (UseStrictCompare) { 9545 BoundPrevUB = 9546 SemaRef 9547 .BuildBinOp( 9548 CurScope, CondLoc, BO_Add, BoundPrevUB, 9549 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()) 9550 .get(); 9551 BoundPrevUB = 9552 SemaRef.ActOnFinishFullExpr(BoundPrevUB, /*DiscardedValue*/ false) 9553 .get(); 9554 } 9555 ParForInDistCond = 9556 SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, 9557 IV.get(), BoundPrevUB); 9558 } 9559 9560 // Build updates and final values of the loop counters. 9561 bool HasErrors = false; 9562 Built.Counters.resize(NestedLoopCount); 9563 Built.Inits.resize(NestedLoopCount); 9564 Built.Updates.resize(NestedLoopCount); 9565 Built.Finals.resize(NestedLoopCount); 9566 Built.DependentCounters.resize(NestedLoopCount); 9567 Built.DependentInits.resize(NestedLoopCount); 9568 Built.FinalsConditions.resize(NestedLoopCount); 9569 { 9570 // We implement the following algorithm for obtaining the 9571 // original loop iteration variable values based on the 9572 // value of the collapsed loop iteration variable IV. 9573 // 9574 // Let n+1 be the number of collapsed loops in the nest. 9575 // Iteration variables (I0, I1, .... In) 9576 // Iteration counts (N0, N1, ... Nn) 9577 // 9578 // Acc = IV; 9579 // 9580 // To compute Ik for loop k, 0 <= k <= n, generate: 9581 // Prod = N(k+1) * N(k+2) * ... * Nn; 9582 // Ik = Acc / Prod; 9583 // Acc -= Ik * Prod; 9584 // 9585 ExprResult Acc = IV; 9586 for (unsigned int Cnt = 0; Cnt < NestedLoopCount; ++Cnt) { 9587 LoopIterationSpace &IS = IterSpaces[Cnt]; 9588 SourceLocation UpdLoc = IS.IncSrcRange.getBegin(); 9589 ExprResult Iter; 9590 9591 // Compute prod 9592 ExprResult Prod = 9593 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(); 9594 for (unsigned int K = Cnt+1; K < NestedLoopCount; ++K) 9595 Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Prod.get(), 9596 IterSpaces[K].NumIterations); 9597 9598 // Iter = Acc / Prod 9599 // If there is at least one more inner loop to avoid 9600 // multiplication by 1. 9601 if (Cnt + 1 < NestedLoopCount) 9602 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, 9603 Acc.get(), Prod.get()); 9604 else 9605 Iter = Acc; 9606 if (!Iter.isUsable()) { 9607 HasErrors = true; 9608 break; 9609 } 9610 9611 // Update Acc: 9612 // Acc -= Iter * Prod 9613 // Check if there is at least one more inner loop to avoid 9614 // multiplication by 1. 9615 if (Cnt + 1 < NestedLoopCount) 9616 Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, 9617 Iter.get(), Prod.get()); 9618 else 9619 Prod = Iter; 9620 Acc = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Sub, 9621 Acc.get(), Prod.get()); 9622 9623 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step 9624 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()); 9625 DeclRefExpr *CounterVar = buildDeclRefExpr( 9626 SemaRef, VD, IS.CounterVar->getType(), IS.CounterVar->getExprLoc(), 9627 /*RefersToCapture=*/true); 9628 ExprResult Init = 9629 buildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar, 9630 IS.CounterInit, IS.IsNonRectangularLB, Captures); 9631 if (!Init.isUsable()) { 9632 HasErrors = true; 9633 break; 9634 } 9635 ExprResult Update = buildCounterUpdate( 9636 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter, 9637 IS.CounterStep, IS.Subtract, IS.IsNonRectangularLB, &Captures); 9638 if (!Update.isUsable()) { 9639 HasErrors = true; 9640 break; 9641 } 9642 9643 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step 9644 ExprResult Final = 9645 buildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar, 9646 IS.CounterInit, IS.NumIterations, IS.CounterStep, 9647 IS.Subtract, IS.IsNonRectangularLB, &Captures); 9648 if (!Final.isUsable()) { 9649 HasErrors = true; 9650 break; 9651 } 9652 9653 if (!Update.isUsable() || !Final.isUsable()) { 9654 HasErrors = true; 9655 break; 9656 } 9657 // Save results 9658 Built.Counters[Cnt] = IS.CounterVar; 9659 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar; 9660 Built.Inits[Cnt] = Init.get(); 9661 Built.Updates[Cnt] = Update.get(); 9662 Built.Finals[Cnt] = Final.get(); 9663 Built.DependentCounters[Cnt] = nullptr; 9664 Built.DependentInits[Cnt] = nullptr; 9665 Built.FinalsConditions[Cnt] = nullptr; 9666 if (IS.IsNonRectangularLB || IS.IsNonRectangularUB) { 9667 Built.DependentCounters[Cnt] = 9668 Built.Counters[NestedLoopCount - 1 - IS.LoopDependentIdx]; 9669 Built.DependentInits[Cnt] = 9670 Built.Inits[NestedLoopCount - 1 - IS.LoopDependentIdx]; 9671 Built.FinalsConditions[Cnt] = IS.FinalCondition; 9672 } 9673 } 9674 } 9675 9676 if (HasErrors) 9677 return 0; 9678 9679 // Save results 9680 Built.IterationVarRef = IV.get(); 9681 Built.LastIteration = LastIteration.get(); 9682 Built.NumIterations = NumIterations.get(); 9683 Built.CalcLastIteration = SemaRef 9684 .ActOnFinishFullExpr(CalcLastIteration.get(), 9685 /*DiscardedValue=*/false) 9686 .get(); 9687 Built.PreCond = PreCond.get(); 9688 Built.PreInits = buildPreInits(C, Captures); 9689 Built.Cond = Cond.get(); 9690 Built.Init = Init.get(); 9691 Built.Inc = Inc.get(); 9692 Built.LB = LB.get(); 9693 Built.UB = UB.get(); 9694 Built.IL = IL.get(); 9695 Built.ST = ST.get(); 9696 Built.EUB = EUB.get(); 9697 Built.NLB = NextLB.get(); 9698 Built.NUB = NextUB.get(); 9699 Built.PrevLB = PrevLB.get(); 9700 Built.PrevUB = PrevUB.get(); 9701 Built.DistInc = DistInc.get(); 9702 Built.PrevEUB = PrevEUB.get(); 9703 Built.DistCombinedFields.LB = CombLB.get(); 9704 Built.DistCombinedFields.UB = CombUB.get(); 9705 Built.DistCombinedFields.EUB = CombEUB.get(); 9706 Built.DistCombinedFields.Init = CombInit.get(); 9707 Built.DistCombinedFields.Cond = CombCond.get(); 9708 Built.DistCombinedFields.NLB = CombNextLB.get(); 9709 Built.DistCombinedFields.NUB = CombNextUB.get(); 9710 Built.DistCombinedFields.DistCond = CombDistCond.get(); 9711 Built.DistCombinedFields.ParForInDistCond = ParForInDistCond.get(); 9712 9713 return NestedLoopCount; 9714 } 9715 9716 static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) { 9717 auto CollapseClauses = 9718 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses); 9719 if (CollapseClauses.begin() != CollapseClauses.end()) 9720 return (*CollapseClauses.begin())->getNumForLoops(); 9721 return nullptr; 9722 } 9723 9724 static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) { 9725 auto OrderedClauses = 9726 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses); 9727 if (OrderedClauses.begin() != OrderedClauses.end()) 9728 return (*OrderedClauses.begin())->getNumForLoops(); 9729 return nullptr; 9730 } 9731 9732 static bool checkSimdlenSafelenSpecified(Sema &S, 9733 const ArrayRef<OMPClause *> Clauses) { 9734 const OMPSafelenClause *Safelen = nullptr; 9735 const OMPSimdlenClause *Simdlen = nullptr; 9736 9737 for (const OMPClause *Clause : Clauses) { 9738 if (Clause->getClauseKind() == OMPC_safelen) 9739 Safelen = cast<OMPSafelenClause>(Clause); 9740 else if (Clause->getClauseKind() == OMPC_simdlen) 9741 Simdlen = cast<OMPSimdlenClause>(Clause); 9742 if (Safelen && Simdlen) 9743 break; 9744 } 9745 9746 if (Simdlen && Safelen) { 9747 const Expr *SimdlenLength = Simdlen->getSimdlen(); 9748 const Expr *SafelenLength = Safelen->getSafelen(); 9749 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() || 9750 SimdlenLength->isInstantiationDependent() || 9751 SimdlenLength->containsUnexpandedParameterPack()) 9752 return false; 9753 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() || 9754 SafelenLength->isInstantiationDependent() || 9755 SafelenLength->containsUnexpandedParameterPack()) 9756 return false; 9757 Expr::EvalResult SimdlenResult, SafelenResult; 9758 SimdlenLength->EvaluateAsInt(SimdlenResult, S.Context); 9759 SafelenLength->EvaluateAsInt(SafelenResult, S.Context); 9760 llvm::APSInt SimdlenRes = SimdlenResult.Val.getInt(); 9761 llvm::APSInt SafelenRes = SafelenResult.Val.getInt(); 9762 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions] 9763 // If both simdlen and safelen clauses are specified, the value of the 9764 // simdlen parameter must be less than or equal to the value of the safelen 9765 // parameter. 9766 if (SimdlenRes > SafelenRes) { 9767 S.Diag(SimdlenLength->getExprLoc(), 9768 diag::err_omp_wrong_simdlen_safelen_values) 9769 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange(); 9770 return true; 9771 } 9772 } 9773 return false; 9774 } 9775 9776 StmtResult 9777 Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt, 9778 SourceLocation StartLoc, SourceLocation EndLoc, 9779 VarsWithInheritedDSAType &VarsWithImplicitDSA) { 9780 if (!AStmt) 9781 return StmtError(); 9782 9783 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9784 OMPLoopBasedDirective::HelperExprs B; 9785 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 9786 // define the nested loops number. 9787 unsigned NestedLoopCount = checkOpenMPLoop( 9788 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), 9789 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 9790 if (NestedLoopCount == 0) 9791 return StmtError(); 9792 9793 assert((CurContext->isDependentContext() || B.builtAll()) && 9794 "omp simd loop exprs were not built"); 9795 9796 if (!CurContext->isDependentContext()) { 9797 // Finalize the clauses that need pre-built expressions for CodeGen. 9798 for (OMPClause *C : Clauses) { 9799 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 9800 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 9801 B.NumIterations, *this, CurScope, 9802 DSAStack)) 9803 return StmtError(); 9804 } 9805 } 9806 9807 if (checkSimdlenSafelenSpecified(*this, Clauses)) 9808 return StmtError(); 9809 9810 setFunctionHasBranchProtectedScope(); 9811 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 9812 Clauses, AStmt, B); 9813 } 9814 9815 StmtResult 9816 Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt, 9817 SourceLocation StartLoc, SourceLocation EndLoc, 9818 VarsWithInheritedDSAType &VarsWithImplicitDSA) { 9819 if (!AStmt) 9820 return StmtError(); 9821 9822 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9823 OMPLoopBasedDirective::HelperExprs B; 9824 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 9825 // define the nested loops number. 9826 unsigned NestedLoopCount = checkOpenMPLoop( 9827 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), 9828 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 9829 if (NestedLoopCount == 0) 9830 return StmtError(); 9831 9832 assert((CurContext->isDependentContext() || B.builtAll()) && 9833 "omp for loop exprs were not built"); 9834 9835 if (!CurContext->isDependentContext()) { 9836 // Finalize the clauses that need pre-built expressions for CodeGen. 9837 for (OMPClause *C : Clauses) { 9838 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 9839 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 9840 B.NumIterations, *this, CurScope, 9841 DSAStack)) 9842 return StmtError(); 9843 } 9844 } 9845 9846 setFunctionHasBranchProtectedScope(); 9847 return OMPForDirective::Create( 9848 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 9849 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 9850 } 9851 9852 StmtResult Sema::ActOnOpenMPForSimdDirective( 9853 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 9854 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 9855 if (!AStmt) 9856 return StmtError(); 9857 9858 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9859 OMPLoopBasedDirective::HelperExprs B; 9860 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 9861 // define the nested loops number. 9862 unsigned NestedLoopCount = 9863 checkOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses), 9864 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 9865 VarsWithImplicitDSA, B); 9866 if (NestedLoopCount == 0) 9867 return StmtError(); 9868 9869 assert((CurContext->isDependentContext() || B.builtAll()) && 9870 "omp for simd loop exprs were not built"); 9871 9872 if (!CurContext->isDependentContext()) { 9873 // Finalize the clauses that need pre-built expressions for CodeGen. 9874 for (OMPClause *C : Clauses) { 9875 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 9876 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 9877 B.NumIterations, *this, CurScope, 9878 DSAStack)) 9879 return StmtError(); 9880 } 9881 } 9882 9883 if (checkSimdlenSafelenSpecified(*this, Clauses)) 9884 return StmtError(); 9885 9886 setFunctionHasBranchProtectedScope(); 9887 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 9888 Clauses, AStmt, B); 9889 } 9890 9891 StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses, 9892 Stmt *AStmt, 9893 SourceLocation StartLoc, 9894 SourceLocation EndLoc) { 9895 if (!AStmt) 9896 return StmtError(); 9897 9898 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9899 auto BaseStmt = AStmt; 9900 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 9901 BaseStmt = CS->getCapturedStmt(); 9902 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 9903 auto S = C->children(); 9904 if (S.begin() == S.end()) 9905 return StmtError(); 9906 // All associated statements must be '#pragma omp section' except for 9907 // the first one. 9908 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) { 9909 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 9910 if (SectionStmt) 9911 Diag(SectionStmt->getBeginLoc(), 9912 diag::err_omp_sections_substmt_not_section); 9913 return StmtError(); 9914 } 9915 cast<OMPSectionDirective>(SectionStmt) 9916 ->setHasCancel(DSAStack->isCancelRegion()); 9917 } 9918 } else { 9919 Diag(AStmt->getBeginLoc(), diag::err_omp_sections_not_compound_stmt); 9920 return StmtError(); 9921 } 9922 9923 setFunctionHasBranchProtectedScope(); 9924 9925 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 9926 DSAStack->getTaskgroupReductionRef(), 9927 DSAStack->isCancelRegion()); 9928 } 9929 9930 StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt, 9931 SourceLocation StartLoc, 9932 SourceLocation EndLoc) { 9933 if (!AStmt) 9934 return StmtError(); 9935 9936 setFunctionHasBranchProtectedScope(); 9937 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion()); 9938 9939 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt, 9940 DSAStack->isCancelRegion()); 9941 } 9942 9943 static Expr *getDirectCallExpr(Expr *E) { 9944 E = E->IgnoreParenCasts()->IgnoreImplicit(); 9945 if (auto *CE = dyn_cast<CallExpr>(E)) 9946 if (CE->getDirectCallee()) 9947 return E; 9948 return nullptr; 9949 } 9950 9951 StmtResult Sema::ActOnOpenMPDispatchDirective(ArrayRef<OMPClause *> Clauses, 9952 Stmt *AStmt, 9953 SourceLocation StartLoc, 9954 SourceLocation EndLoc) { 9955 if (!AStmt) 9956 return StmtError(); 9957 9958 Stmt *S = cast<CapturedStmt>(AStmt)->getCapturedStmt(); 9959 9960 // 5.1 OpenMP 9961 // expression-stmt : an expression statement with one of the following forms: 9962 // expression = target-call ( [expression-list] ); 9963 // target-call ( [expression-list] ); 9964 9965 SourceLocation TargetCallLoc; 9966 9967 if (!CurContext->isDependentContext()) { 9968 Expr *TargetCall = nullptr; 9969 9970 auto *E = dyn_cast<Expr>(S); 9971 if (!E) { 9972 Diag(S->getBeginLoc(), diag::err_omp_dispatch_statement_call); 9973 return StmtError(); 9974 } 9975 9976 E = E->IgnoreParenCasts()->IgnoreImplicit(); 9977 9978 if (auto *BO = dyn_cast<BinaryOperator>(E)) { 9979 if (BO->getOpcode() == BO_Assign) 9980 TargetCall = getDirectCallExpr(BO->getRHS()); 9981 } else { 9982 if (auto *COCE = dyn_cast<CXXOperatorCallExpr>(E)) 9983 if (COCE->getOperator() == OO_Equal) 9984 TargetCall = getDirectCallExpr(COCE->getArg(1)); 9985 if (!TargetCall) 9986 TargetCall = getDirectCallExpr(E); 9987 } 9988 if (!TargetCall) { 9989 Diag(E->getBeginLoc(), diag::err_omp_dispatch_statement_call); 9990 return StmtError(); 9991 } 9992 TargetCallLoc = TargetCall->getExprLoc(); 9993 } 9994 9995 setFunctionHasBranchProtectedScope(); 9996 9997 return OMPDispatchDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 9998 TargetCallLoc); 9999 } 10000 10001 StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses, 10002 Stmt *AStmt, 10003 SourceLocation StartLoc, 10004 SourceLocation EndLoc) { 10005 if (!AStmt) 10006 return StmtError(); 10007 10008 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10009 10010 setFunctionHasBranchProtectedScope(); 10011 10012 // OpenMP [2.7.3, single Construct, Restrictions] 10013 // The copyprivate clause must not be used with the nowait clause. 10014 const OMPClause *Nowait = nullptr; 10015 const OMPClause *Copyprivate = nullptr; 10016 for (const OMPClause *Clause : Clauses) { 10017 if (Clause->getClauseKind() == OMPC_nowait) 10018 Nowait = Clause; 10019 else if (Clause->getClauseKind() == OMPC_copyprivate) 10020 Copyprivate = Clause; 10021 if (Copyprivate && Nowait) { 10022 Diag(Copyprivate->getBeginLoc(), 10023 diag::err_omp_single_copyprivate_with_nowait); 10024 Diag(Nowait->getBeginLoc(), diag::note_omp_nowait_clause_here); 10025 return StmtError(); 10026 } 10027 } 10028 10029 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 10030 } 10031 10032 StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt, 10033 SourceLocation StartLoc, 10034 SourceLocation EndLoc) { 10035 if (!AStmt) 10036 return StmtError(); 10037 10038 setFunctionHasBranchProtectedScope(); 10039 10040 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt); 10041 } 10042 10043 StmtResult Sema::ActOnOpenMPMaskedDirective(ArrayRef<OMPClause *> Clauses, 10044 Stmt *AStmt, 10045 SourceLocation StartLoc, 10046 SourceLocation EndLoc) { 10047 if (!AStmt) 10048 return StmtError(); 10049 10050 setFunctionHasBranchProtectedScope(); 10051 10052 return OMPMaskedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 10053 } 10054 10055 StmtResult Sema::ActOnOpenMPCriticalDirective( 10056 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses, 10057 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { 10058 if (!AStmt) 10059 return StmtError(); 10060 10061 bool ErrorFound = false; 10062 llvm::APSInt Hint; 10063 SourceLocation HintLoc; 10064 bool DependentHint = false; 10065 for (const OMPClause *C : Clauses) { 10066 if (C->getClauseKind() == OMPC_hint) { 10067 if (!DirName.getName()) { 10068 Diag(C->getBeginLoc(), diag::err_omp_hint_clause_no_name); 10069 ErrorFound = true; 10070 } 10071 Expr *E = cast<OMPHintClause>(C)->getHint(); 10072 if (E->isTypeDependent() || E->isValueDependent() || 10073 E->isInstantiationDependent()) { 10074 DependentHint = true; 10075 } else { 10076 Hint = E->EvaluateKnownConstInt(Context); 10077 HintLoc = C->getBeginLoc(); 10078 } 10079 } 10080 } 10081 if (ErrorFound) 10082 return StmtError(); 10083 const auto Pair = DSAStack->getCriticalWithHint(DirName); 10084 if (Pair.first && DirName.getName() && !DependentHint) { 10085 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) { 10086 Diag(StartLoc, diag::err_omp_critical_with_hint); 10087 if (HintLoc.isValid()) 10088 Diag(HintLoc, diag::note_omp_critical_hint_here) 10089 << 0 << toString(Hint, /*Radix=*/10, /*Signed=*/false); 10090 else 10091 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0; 10092 if (const auto *C = Pair.first->getSingleClause<OMPHintClause>()) { 10093 Diag(C->getBeginLoc(), diag::note_omp_critical_hint_here) 10094 << 1 10095 << toString(C->getHint()->EvaluateKnownConstInt(Context), 10096 /*Radix=*/10, /*Signed=*/false); 10097 } else { 10098 Diag(Pair.first->getBeginLoc(), diag::note_omp_critical_no_hint) << 1; 10099 } 10100 } 10101 } 10102 10103 setFunctionHasBranchProtectedScope(); 10104 10105 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc, 10106 Clauses, AStmt); 10107 if (!Pair.first && DirName.getName() && !DependentHint) 10108 DSAStack->addCriticalWithHint(Dir, Hint); 10109 return Dir; 10110 } 10111 10112 StmtResult Sema::ActOnOpenMPParallelForDirective( 10113 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10114 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10115 if (!AStmt) 10116 return StmtError(); 10117 10118 auto *CS = cast<CapturedStmt>(AStmt); 10119 // 1.2.2 OpenMP Language Terminology 10120 // Structured block - An executable statement with a single entry at the 10121 // top and a single exit at the bottom. 10122 // The point of exit cannot be a branch out of the structured block. 10123 // longjmp() and throw() must not violate the entry/exit criteria. 10124 CS->getCapturedDecl()->setNothrow(); 10125 10126 OMPLoopBasedDirective::HelperExprs B; 10127 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 10128 // define the nested loops number. 10129 unsigned NestedLoopCount = 10130 checkOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses), 10131 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 10132 VarsWithImplicitDSA, B); 10133 if (NestedLoopCount == 0) 10134 return StmtError(); 10135 10136 assert((CurContext->isDependentContext() || B.builtAll()) && 10137 "omp parallel for loop exprs were not built"); 10138 10139 if (!CurContext->isDependentContext()) { 10140 // Finalize the clauses that need pre-built expressions for CodeGen. 10141 for (OMPClause *C : Clauses) { 10142 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 10143 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 10144 B.NumIterations, *this, CurScope, 10145 DSAStack)) 10146 return StmtError(); 10147 } 10148 } 10149 10150 setFunctionHasBranchProtectedScope(); 10151 return OMPParallelForDirective::Create( 10152 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 10153 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 10154 } 10155 10156 StmtResult Sema::ActOnOpenMPParallelForSimdDirective( 10157 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10158 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10159 if (!AStmt) 10160 return StmtError(); 10161 10162 auto *CS = cast<CapturedStmt>(AStmt); 10163 // 1.2.2 OpenMP Language Terminology 10164 // Structured block - An executable statement with a single entry at the 10165 // top and a single exit at the bottom. 10166 // The point of exit cannot be a branch out of the structured block. 10167 // longjmp() and throw() must not violate the entry/exit criteria. 10168 CS->getCapturedDecl()->setNothrow(); 10169 10170 OMPLoopBasedDirective::HelperExprs B; 10171 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 10172 // define the nested loops number. 10173 unsigned NestedLoopCount = 10174 checkOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses), 10175 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 10176 VarsWithImplicitDSA, B); 10177 if (NestedLoopCount == 0) 10178 return StmtError(); 10179 10180 if (!CurContext->isDependentContext()) { 10181 // Finalize the clauses that need pre-built expressions for CodeGen. 10182 for (OMPClause *C : Clauses) { 10183 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 10184 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 10185 B.NumIterations, *this, CurScope, 10186 DSAStack)) 10187 return StmtError(); 10188 } 10189 } 10190 10191 if (checkSimdlenSafelenSpecified(*this, Clauses)) 10192 return StmtError(); 10193 10194 setFunctionHasBranchProtectedScope(); 10195 return OMPParallelForSimdDirective::Create( 10196 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 10197 } 10198 10199 StmtResult 10200 Sema::ActOnOpenMPParallelMasterDirective(ArrayRef<OMPClause *> Clauses, 10201 Stmt *AStmt, SourceLocation StartLoc, 10202 SourceLocation EndLoc) { 10203 if (!AStmt) 10204 return StmtError(); 10205 10206 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10207 auto *CS = cast<CapturedStmt>(AStmt); 10208 // 1.2.2 OpenMP Language Terminology 10209 // Structured block - An executable statement with a single entry at the 10210 // top and a single exit at the bottom. 10211 // The point of exit cannot be a branch out of the structured block. 10212 // longjmp() and throw() must not violate the entry/exit criteria. 10213 CS->getCapturedDecl()->setNothrow(); 10214 10215 setFunctionHasBranchProtectedScope(); 10216 10217 return OMPParallelMasterDirective::Create( 10218 Context, StartLoc, EndLoc, Clauses, AStmt, 10219 DSAStack->getTaskgroupReductionRef()); 10220 } 10221 10222 StmtResult 10223 Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses, 10224 Stmt *AStmt, SourceLocation StartLoc, 10225 SourceLocation EndLoc) { 10226 if (!AStmt) 10227 return StmtError(); 10228 10229 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10230 auto BaseStmt = AStmt; 10231 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 10232 BaseStmt = CS->getCapturedStmt(); 10233 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 10234 auto S = C->children(); 10235 if (S.begin() == S.end()) 10236 return StmtError(); 10237 // All associated statements must be '#pragma omp section' except for 10238 // the first one. 10239 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) { 10240 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 10241 if (SectionStmt) 10242 Diag(SectionStmt->getBeginLoc(), 10243 diag::err_omp_parallel_sections_substmt_not_section); 10244 return StmtError(); 10245 } 10246 cast<OMPSectionDirective>(SectionStmt) 10247 ->setHasCancel(DSAStack->isCancelRegion()); 10248 } 10249 } else { 10250 Diag(AStmt->getBeginLoc(), 10251 diag::err_omp_parallel_sections_not_compound_stmt); 10252 return StmtError(); 10253 } 10254 10255 setFunctionHasBranchProtectedScope(); 10256 10257 return OMPParallelSectionsDirective::Create( 10258 Context, StartLoc, EndLoc, Clauses, AStmt, 10259 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 10260 } 10261 10262 /// Find and diagnose mutually exclusive clause kinds. 10263 static bool checkMutuallyExclusiveClauses( 10264 Sema &S, ArrayRef<OMPClause *> Clauses, 10265 ArrayRef<OpenMPClauseKind> MutuallyExclusiveClauses) { 10266 const OMPClause *PrevClause = nullptr; 10267 bool ErrorFound = false; 10268 for (const OMPClause *C : Clauses) { 10269 if (llvm::is_contained(MutuallyExclusiveClauses, C->getClauseKind())) { 10270 if (!PrevClause) { 10271 PrevClause = C; 10272 } else if (PrevClause->getClauseKind() != C->getClauseKind()) { 10273 S.Diag(C->getBeginLoc(), diag::err_omp_clauses_mutually_exclusive) 10274 << getOpenMPClauseName(C->getClauseKind()) 10275 << getOpenMPClauseName(PrevClause->getClauseKind()); 10276 S.Diag(PrevClause->getBeginLoc(), diag::note_omp_previous_clause) 10277 << getOpenMPClauseName(PrevClause->getClauseKind()); 10278 ErrorFound = true; 10279 } 10280 } 10281 } 10282 return ErrorFound; 10283 } 10284 10285 StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses, 10286 Stmt *AStmt, SourceLocation StartLoc, 10287 SourceLocation EndLoc) { 10288 if (!AStmt) 10289 return StmtError(); 10290 10291 // OpenMP 5.0, 2.10.1 task Construct 10292 // If a detach clause appears on the directive, then a mergeable clause cannot 10293 // appear on the same directive. 10294 if (checkMutuallyExclusiveClauses(*this, Clauses, 10295 {OMPC_detach, OMPC_mergeable})) 10296 return StmtError(); 10297 10298 auto *CS = cast<CapturedStmt>(AStmt); 10299 // 1.2.2 OpenMP Language Terminology 10300 // Structured block - An executable statement with a single entry at the 10301 // top and a single exit at the bottom. 10302 // The point of exit cannot be a branch out of the structured block. 10303 // longjmp() and throw() must not violate the entry/exit criteria. 10304 CS->getCapturedDecl()->setNothrow(); 10305 10306 setFunctionHasBranchProtectedScope(); 10307 10308 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 10309 DSAStack->isCancelRegion()); 10310 } 10311 10312 StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc, 10313 SourceLocation EndLoc) { 10314 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc); 10315 } 10316 10317 StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc, 10318 SourceLocation EndLoc) { 10319 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc); 10320 } 10321 10322 StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc, 10323 SourceLocation EndLoc) { 10324 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc); 10325 } 10326 10327 StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses, 10328 Stmt *AStmt, 10329 SourceLocation StartLoc, 10330 SourceLocation EndLoc) { 10331 if (!AStmt) 10332 return StmtError(); 10333 10334 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10335 10336 setFunctionHasBranchProtectedScope(); 10337 10338 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses, 10339 AStmt, 10340 DSAStack->getTaskgroupReductionRef()); 10341 } 10342 10343 StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses, 10344 SourceLocation StartLoc, 10345 SourceLocation EndLoc) { 10346 OMPFlushClause *FC = nullptr; 10347 OMPClause *OrderClause = nullptr; 10348 for (OMPClause *C : Clauses) { 10349 if (C->getClauseKind() == OMPC_flush) 10350 FC = cast<OMPFlushClause>(C); 10351 else 10352 OrderClause = C; 10353 } 10354 OpenMPClauseKind MemOrderKind = OMPC_unknown; 10355 SourceLocation MemOrderLoc; 10356 for (const OMPClause *C : Clauses) { 10357 if (C->getClauseKind() == OMPC_acq_rel || 10358 C->getClauseKind() == OMPC_acquire || 10359 C->getClauseKind() == OMPC_release) { 10360 if (MemOrderKind != OMPC_unknown) { 10361 Diag(C->getBeginLoc(), diag::err_omp_several_mem_order_clauses) 10362 << getOpenMPDirectiveName(OMPD_flush) << 1 10363 << SourceRange(C->getBeginLoc(), C->getEndLoc()); 10364 Diag(MemOrderLoc, diag::note_omp_previous_mem_order_clause) 10365 << getOpenMPClauseName(MemOrderKind); 10366 } else { 10367 MemOrderKind = C->getClauseKind(); 10368 MemOrderLoc = C->getBeginLoc(); 10369 } 10370 } 10371 } 10372 if (FC && OrderClause) { 10373 Diag(FC->getLParenLoc(), diag::err_omp_flush_order_clause_and_list) 10374 << getOpenMPClauseName(OrderClause->getClauseKind()); 10375 Diag(OrderClause->getBeginLoc(), diag::note_omp_flush_order_clause_here) 10376 << getOpenMPClauseName(OrderClause->getClauseKind()); 10377 return StmtError(); 10378 } 10379 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses); 10380 } 10381 10382 StmtResult Sema::ActOnOpenMPDepobjDirective(ArrayRef<OMPClause *> Clauses, 10383 SourceLocation StartLoc, 10384 SourceLocation EndLoc) { 10385 if (Clauses.empty()) { 10386 Diag(StartLoc, diag::err_omp_depobj_expected); 10387 return StmtError(); 10388 } else if (Clauses[0]->getClauseKind() != OMPC_depobj) { 10389 Diag(Clauses[0]->getBeginLoc(), diag::err_omp_depobj_expected); 10390 return StmtError(); 10391 } 10392 // Only depobj expression and another single clause is allowed. 10393 if (Clauses.size() > 2) { 10394 Diag(Clauses[2]->getBeginLoc(), 10395 diag::err_omp_depobj_single_clause_expected); 10396 return StmtError(); 10397 } else if (Clauses.size() < 1) { 10398 Diag(Clauses[0]->getEndLoc(), diag::err_omp_depobj_single_clause_expected); 10399 return StmtError(); 10400 } 10401 return OMPDepobjDirective::Create(Context, StartLoc, EndLoc, Clauses); 10402 } 10403 10404 StmtResult Sema::ActOnOpenMPScanDirective(ArrayRef<OMPClause *> Clauses, 10405 SourceLocation StartLoc, 10406 SourceLocation EndLoc) { 10407 // Check that exactly one clause is specified. 10408 if (Clauses.size() != 1) { 10409 Diag(Clauses.empty() ? EndLoc : Clauses[1]->getBeginLoc(), 10410 diag::err_omp_scan_single_clause_expected); 10411 return StmtError(); 10412 } 10413 // Check that scan directive is used in the scopeof the OpenMP loop body. 10414 if (Scope *S = DSAStack->getCurScope()) { 10415 Scope *ParentS = S->getParent(); 10416 if (!ParentS || ParentS->getParent() != ParentS->getBreakParent() || 10417 !ParentS->getBreakParent()->isOpenMPLoopScope()) 10418 return StmtError(Diag(StartLoc, diag::err_omp_orphaned_device_directive) 10419 << getOpenMPDirectiveName(OMPD_scan) << 5); 10420 } 10421 // Check that only one instance of scan directives is used in the same outer 10422 // region. 10423 if (DSAStack->doesParentHasScanDirective()) { 10424 Diag(StartLoc, diag::err_omp_several_directives_in_region) << "scan"; 10425 Diag(DSAStack->getParentScanDirectiveLoc(), 10426 diag::note_omp_previous_directive) 10427 << "scan"; 10428 return StmtError(); 10429 } 10430 DSAStack->setParentHasScanDirective(StartLoc); 10431 return OMPScanDirective::Create(Context, StartLoc, EndLoc, Clauses); 10432 } 10433 10434 StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses, 10435 Stmt *AStmt, 10436 SourceLocation StartLoc, 10437 SourceLocation EndLoc) { 10438 const OMPClause *DependFound = nullptr; 10439 const OMPClause *DependSourceClause = nullptr; 10440 const OMPClause *DependSinkClause = nullptr; 10441 bool ErrorFound = false; 10442 const OMPThreadsClause *TC = nullptr; 10443 const OMPSIMDClause *SC = nullptr; 10444 for (const OMPClause *C : Clauses) { 10445 if (auto *DC = dyn_cast<OMPDependClause>(C)) { 10446 DependFound = C; 10447 if (DC->getDependencyKind() == OMPC_DEPEND_source) { 10448 if (DependSourceClause) { 10449 Diag(C->getBeginLoc(), diag::err_omp_more_one_clause) 10450 << getOpenMPDirectiveName(OMPD_ordered) 10451 << getOpenMPClauseName(OMPC_depend) << 2; 10452 ErrorFound = true; 10453 } else { 10454 DependSourceClause = C; 10455 } 10456 if (DependSinkClause) { 10457 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed) 10458 << 0; 10459 ErrorFound = true; 10460 } 10461 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) { 10462 if (DependSourceClause) { 10463 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed) 10464 << 1; 10465 ErrorFound = true; 10466 } 10467 DependSinkClause = C; 10468 } 10469 } else if (C->getClauseKind() == OMPC_threads) { 10470 TC = cast<OMPThreadsClause>(C); 10471 } else if (C->getClauseKind() == OMPC_simd) { 10472 SC = cast<OMPSIMDClause>(C); 10473 } 10474 } 10475 if (!ErrorFound && !SC && 10476 isOpenMPSimdDirective(DSAStack->getParentDirective())) { 10477 // OpenMP [2.8.1,simd Construct, Restrictions] 10478 // An ordered construct with the simd clause is the only OpenMP construct 10479 // that can appear in the simd region. 10480 Diag(StartLoc, diag::err_omp_prohibited_region_simd) 10481 << (LangOpts.OpenMP >= 50 ? 1 : 0); 10482 ErrorFound = true; 10483 } else if (DependFound && (TC || SC)) { 10484 Diag(DependFound->getBeginLoc(), diag::err_omp_depend_clause_thread_simd) 10485 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind()); 10486 ErrorFound = true; 10487 } else if (DependFound && !DSAStack->getParentOrderedRegionParam().first) { 10488 Diag(DependFound->getBeginLoc(), 10489 diag::err_omp_ordered_directive_without_param); 10490 ErrorFound = true; 10491 } else if (TC || Clauses.empty()) { 10492 if (const Expr *Param = DSAStack->getParentOrderedRegionParam().first) { 10493 SourceLocation ErrLoc = TC ? TC->getBeginLoc() : StartLoc; 10494 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param) 10495 << (TC != nullptr); 10496 Diag(Param->getBeginLoc(), diag::note_omp_ordered_param) << 1; 10497 ErrorFound = true; 10498 } 10499 } 10500 if ((!AStmt && !DependFound) || ErrorFound) 10501 return StmtError(); 10502 10503 // OpenMP 5.0, 2.17.9, ordered Construct, Restrictions. 10504 // During execution of an iteration of a worksharing-loop or a loop nest 10505 // within a worksharing-loop, simd, or worksharing-loop SIMD region, a thread 10506 // must not execute more than one ordered region corresponding to an ordered 10507 // construct without a depend clause. 10508 if (!DependFound) { 10509 if (DSAStack->doesParentHasOrderedDirective()) { 10510 Diag(StartLoc, diag::err_omp_several_directives_in_region) << "ordered"; 10511 Diag(DSAStack->getParentOrderedDirectiveLoc(), 10512 diag::note_omp_previous_directive) 10513 << "ordered"; 10514 return StmtError(); 10515 } 10516 DSAStack->setParentHasOrderedDirective(StartLoc); 10517 } 10518 10519 if (AStmt) { 10520 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10521 10522 setFunctionHasBranchProtectedScope(); 10523 } 10524 10525 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 10526 } 10527 10528 namespace { 10529 /// Helper class for checking expression in 'omp atomic [update]' 10530 /// construct. 10531 class OpenMPAtomicUpdateChecker { 10532 /// Error results for atomic update expressions. 10533 enum ExprAnalysisErrorCode { 10534 /// A statement is not an expression statement. 10535 NotAnExpression, 10536 /// Expression is not builtin binary or unary operation. 10537 NotABinaryOrUnaryExpression, 10538 /// Unary operation is not post-/pre- increment/decrement operation. 10539 NotAnUnaryIncDecExpression, 10540 /// An expression is not of scalar type. 10541 NotAScalarType, 10542 /// A binary operation is not an assignment operation. 10543 NotAnAssignmentOp, 10544 /// RHS part of the binary operation is not a binary expression. 10545 NotABinaryExpression, 10546 /// RHS part is not additive/multiplicative/shift/biwise binary 10547 /// expression. 10548 NotABinaryOperator, 10549 /// RHS binary operation does not have reference to the updated LHS 10550 /// part. 10551 NotAnUpdateExpression, 10552 /// No errors is found. 10553 NoError 10554 }; 10555 /// Reference to Sema. 10556 Sema &SemaRef; 10557 /// A location for note diagnostics (when error is found). 10558 SourceLocation NoteLoc; 10559 /// 'x' lvalue part of the source atomic expression. 10560 Expr *X; 10561 /// 'expr' rvalue part of the source atomic expression. 10562 Expr *E; 10563 /// Helper expression of the form 10564 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or 10565 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. 10566 Expr *UpdateExpr; 10567 /// Is 'x' a LHS in a RHS part of full update expression. It is 10568 /// important for non-associative operations. 10569 bool IsXLHSInRHSPart; 10570 BinaryOperatorKind Op; 10571 SourceLocation OpLoc; 10572 /// true if the source expression is a postfix unary operation, false 10573 /// if it is a prefix unary operation. 10574 bool IsPostfixUpdate; 10575 10576 public: 10577 OpenMPAtomicUpdateChecker(Sema &SemaRef) 10578 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr), 10579 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {} 10580 /// Check specified statement that it is suitable for 'atomic update' 10581 /// constructs and extract 'x', 'expr' and Operation from the original 10582 /// expression. If DiagId and NoteId == 0, then only check is performed 10583 /// without error notification. 10584 /// \param DiagId Diagnostic which should be emitted if error is found. 10585 /// \param NoteId Diagnostic note for the main error message. 10586 /// \return true if statement is not an update expression, false otherwise. 10587 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0); 10588 /// Return the 'x' lvalue part of the source atomic expression. 10589 Expr *getX() const { return X; } 10590 /// Return the 'expr' rvalue part of the source atomic expression. 10591 Expr *getExpr() const { return E; } 10592 /// Return the update expression used in calculation of the updated 10593 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or 10594 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. 10595 Expr *getUpdateExpr() const { return UpdateExpr; } 10596 /// Return true if 'x' is LHS in RHS part of full update expression, 10597 /// false otherwise. 10598 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; } 10599 10600 /// true if the source expression is a postfix unary operation, false 10601 /// if it is a prefix unary operation. 10602 bool isPostfixUpdate() const { return IsPostfixUpdate; } 10603 10604 private: 10605 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0, 10606 unsigned NoteId = 0); 10607 }; 10608 } // namespace 10609 10610 bool OpenMPAtomicUpdateChecker::checkBinaryOperation( 10611 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) { 10612 ExprAnalysisErrorCode ErrorFound = NoError; 10613 SourceLocation ErrorLoc, NoteLoc; 10614 SourceRange ErrorRange, NoteRange; 10615 // Allowed constructs are: 10616 // x = x binop expr; 10617 // x = expr binop x; 10618 if (AtomicBinOp->getOpcode() == BO_Assign) { 10619 X = AtomicBinOp->getLHS(); 10620 if (const auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>( 10621 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) { 10622 if (AtomicInnerBinOp->isMultiplicativeOp() || 10623 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() || 10624 AtomicInnerBinOp->isBitwiseOp()) { 10625 Op = AtomicInnerBinOp->getOpcode(); 10626 OpLoc = AtomicInnerBinOp->getOperatorLoc(); 10627 Expr *LHS = AtomicInnerBinOp->getLHS(); 10628 Expr *RHS = AtomicInnerBinOp->getRHS(); 10629 llvm::FoldingSetNodeID XId, LHSId, RHSId; 10630 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(), 10631 /*Canonical=*/true); 10632 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(), 10633 /*Canonical=*/true); 10634 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(), 10635 /*Canonical=*/true); 10636 if (XId == LHSId) { 10637 E = RHS; 10638 IsXLHSInRHSPart = true; 10639 } else if (XId == RHSId) { 10640 E = LHS; 10641 IsXLHSInRHSPart = false; 10642 } else { 10643 ErrorLoc = AtomicInnerBinOp->getExprLoc(); 10644 ErrorRange = AtomicInnerBinOp->getSourceRange(); 10645 NoteLoc = X->getExprLoc(); 10646 NoteRange = X->getSourceRange(); 10647 ErrorFound = NotAnUpdateExpression; 10648 } 10649 } else { 10650 ErrorLoc = AtomicInnerBinOp->getExprLoc(); 10651 ErrorRange = AtomicInnerBinOp->getSourceRange(); 10652 NoteLoc = AtomicInnerBinOp->getOperatorLoc(); 10653 NoteRange = SourceRange(NoteLoc, NoteLoc); 10654 ErrorFound = NotABinaryOperator; 10655 } 10656 } else { 10657 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc(); 10658 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange(); 10659 ErrorFound = NotABinaryExpression; 10660 } 10661 } else { 10662 ErrorLoc = AtomicBinOp->getExprLoc(); 10663 ErrorRange = AtomicBinOp->getSourceRange(); 10664 NoteLoc = AtomicBinOp->getOperatorLoc(); 10665 NoteRange = SourceRange(NoteLoc, NoteLoc); 10666 ErrorFound = NotAnAssignmentOp; 10667 } 10668 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { 10669 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; 10670 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; 10671 return true; 10672 } 10673 if (SemaRef.CurContext->isDependentContext()) 10674 E = X = UpdateExpr = nullptr; 10675 return ErrorFound != NoError; 10676 } 10677 10678 bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId, 10679 unsigned NoteId) { 10680 ExprAnalysisErrorCode ErrorFound = NoError; 10681 SourceLocation ErrorLoc, NoteLoc; 10682 SourceRange ErrorRange, NoteRange; 10683 // Allowed constructs are: 10684 // x++; 10685 // x--; 10686 // ++x; 10687 // --x; 10688 // x binop= expr; 10689 // x = x binop expr; 10690 // x = expr binop x; 10691 if (auto *AtomicBody = dyn_cast<Expr>(S)) { 10692 AtomicBody = AtomicBody->IgnoreParenImpCasts(); 10693 if (AtomicBody->getType()->isScalarType() || 10694 AtomicBody->isInstantiationDependent()) { 10695 if (const auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>( 10696 AtomicBody->IgnoreParenImpCasts())) { 10697 // Check for Compound Assignment Operation 10698 Op = BinaryOperator::getOpForCompoundAssignment( 10699 AtomicCompAssignOp->getOpcode()); 10700 OpLoc = AtomicCompAssignOp->getOperatorLoc(); 10701 E = AtomicCompAssignOp->getRHS(); 10702 X = AtomicCompAssignOp->getLHS()->IgnoreParens(); 10703 IsXLHSInRHSPart = true; 10704 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>( 10705 AtomicBody->IgnoreParenImpCasts())) { 10706 // Check for Binary Operation 10707 if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId)) 10708 return true; 10709 } else if (const auto *AtomicUnaryOp = dyn_cast<UnaryOperator>( 10710 AtomicBody->IgnoreParenImpCasts())) { 10711 // Check for Unary Operation 10712 if (AtomicUnaryOp->isIncrementDecrementOp()) { 10713 IsPostfixUpdate = AtomicUnaryOp->isPostfix(); 10714 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub; 10715 OpLoc = AtomicUnaryOp->getOperatorLoc(); 10716 X = AtomicUnaryOp->getSubExpr()->IgnoreParens(); 10717 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get(); 10718 IsXLHSInRHSPart = true; 10719 } else { 10720 ErrorFound = NotAnUnaryIncDecExpression; 10721 ErrorLoc = AtomicUnaryOp->getExprLoc(); 10722 ErrorRange = AtomicUnaryOp->getSourceRange(); 10723 NoteLoc = AtomicUnaryOp->getOperatorLoc(); 10724 NoteRange = SourceRange(NoteLoc, NoteLoc); 10725 } 10726 } else if (!AtomicBody->isInstantiationDependent()) { 10727 ErrorFound = NotABinaryOrUnaryExpression; 10728 NoteLoc = ErrorLoc = AtomicBody->getExprLoc(); 10729 NoteRange = ErrorRange = AtomicBody->getSourceRange(); 10730 } 10731 } else { 10732 ErrorFound = NotAScalarType; 10733 NoteLoc = ErrorLoc = AtomicBody->getBeginLoc(); 10734 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 10735 } 10736 } else { 10737 ErrorFound = NotAnExpression; 10738 NoteLoc = ErrorLoc = S->getBeginLoc(); 10739 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 10740 } 10741 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { 10742 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; 10743 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; 10744 return true; 10745 } 10746 if (SemaRef.CurContext->isDependentContext()) 10747 E = X = UpdateExpr = nullptr; 10748 if (ErrorFound == NoError && E && X) { 10749 // Build an update expression of form 'OpaqueValueExpr(x) binop 10750 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop 10751 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression. 10752 auto *OVEX = new (SemaRef.getASTContext()) 10753 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_PRValue); 10754 auto *OVEExpr = new (SemaRef.getASTContext()) 10755 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_PRValue); 10756 ExprResult Update = 10757 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr, 10758 IsXLHSInRHSPart ? OVEExpr : OVEX); 10759 if (Update.isInvalid()) 10760 return true; 10761 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(), 10762 Sema::AA_Casting); 10763 if (Update.isInvalid()) 10764 return true; 10765 UpdateExpr = Update.get(); 10766 } 10767 return ErrorFound != NoError; 10768 } 10769 10770 StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses, 10771 Stmt *AStmt, 10772 SourceLocation StartLoc, 10773 SourceLocation EndLoc) { 10774 // Register location of the first atomic directive. 10775 DSAStack->addAtomicDirectiveLoc(StartLoc); 10776 if (!AStmt) 10777 return StmtError(); 10778 10779 // 1.2.2 OpenMP Language Terminology 10780 // Structured block - An executable statement with a single entry at the 10781 // top and a single exit at the bottom. 10782 // The point of exit cannot be a branch out of the structured block. 10783 // longjmp() and throw() must not violate the entry/exit criteria. 10784 OpenMPClauseKind AtomicKind = OMPC_unknown; 10785 SourceLocation AtomicKindLoc; 10786 OpenMPClauseKind MemOrderKind = OMPC_unknown; 10787 SourceLocation MemOrderLoc; 10788 for (const OMPClause *C : Clauses) { 10789 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write || 10790 C->getClauseKind() == OMPC_update || 10791 C->getClauseKind() == OMPC_capture) { 10792 if (AtomicKind != OMPC_unknown) { 10793 Diag(C->getBeginLoc(), diag::err_omp_atomic_several_clauses) 10794 << SourceRange(C->getBeginLoc(), C->getEndLoc()); 10795 Diag(AtomicKindLoc, diag::note_omp_previous_mem_order_clause) 10796 << getOpenMPClauseName(AtomicKind); 10797 } else { 10798 AtomicKind = C->getClauseKind(); 10799 AtomicKindLoc = C->getBeginLoc(); 10800 } 10801 } 10802 if (C->getClauseKind() == OMPC_seq_cst || 10803 C->getClauseKind() == OMPC_acq_rel || 10804 C->getClauseKind() == OMPC_acquire || 10805 C->getClauseKind() == OMPC_release || 10806 C->getClauseKind() == OMPC_relaxed) { 10807 if (MemOrderKind != OMPC_unknown) { 10808 Diag(C->getBeginLoc(), diag::err_omp_several_mem_order_clauses) 10809 << getOpenMPDirectiveName(OMPD_atomic) << 0 10810 << SourceRange(C->getBeginLoc(), C->getEndLoc()); 10811 Diag(MemOrderLoc, diag::note_omp_previous_mem_order_clause) 10812 << getOpenMPClauseName(MemOrderKind); 10813 } else { 10814 MemOrderKind = C->getClauseKind(); 10815 MemOrderLoc = C->getBeginLoc(); 10816 } 10817 } 10818 } 10819 // OpenMP 5.0, 2.17.7 atomic Construct, Restrictions 10820 // If atomic-clause is read then memory-order-clause must not be acq_rel or 10821 // release. 10822 // If atomic-clause is write then memory-order-clause must not be acq_rel or 10823 // acquire. 10824 // If atomic-clause is update or not present then memory-order-clause must not 10825 // be acq_rel or acquire. 10826 if ((AtomicKind == OMPC_read && 10827 (MemOrderKind == OMPC_acq_rel || MemOrderKind == OMPC_release)) || 10828 ((AtomicKind == OMPC_write || AtomicKind == OMPC_update || 10829 AtomicKind == OMPC_unknown) && 10830 (MemOrderKind == OMPC_acq_rel || MemOrderKind == OMPC_acquire))) { 10831 SourceLocation Loc = AtomicKindLoc; 10832 if (AtomicKind == OMPC_unknown) 10833 Loc = StartLoc; 10834 Diag(Loc, diag::err_omp_atomic_incompatible_mem_order_clause) 10835 << getOpenMPClauseName(AtomicKind) 10836 << (AtomicKind == OMPC_unknown ? 1 : 0) 10837 << getOpenMPClauseName(MemOrderKind); 10838 Diag(MemOrderLoc, diag::note_omp_previous_mem_order_clause) 10839 << getOpenMPClauseName(MemOrderKind); 10840 } 10841 10842 Stmt *Body = AStmt; 10843 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body)) 10844 Body = EWC->getSubExpr(); 10845 10846 Expr *X = nullptr; 10847 Expr *V = nullptr; 10848 Expr *E = nullptr; 10849 Expr *UE = nullptr; 10850 bool IsXLHSInRHSPart = false; 10851 bool IsPostfixUpdate = false; 10852 // OpenMP [2.12.6, atomic Construct] 10853 // In the next expressions: 10854 // * x and v (as applicable) are both l-value expressions with scalar type. 10855 // * During the execution of an atomic region, multiple syntactic 10856 // occurrences of x must designate the same storage location. 10857 // * Neither of v and expr (as applicable) may access the storage location 10858 // designated by x. 10859 // * Neither of x and expr (as applicable) may access the storage location 10860 // designated by v. 10861 // * expr is an expression with scalar type. 10862 // * binop is one of +, *, -, /, &, ^, |, <<, or >>. 10863 // * binop, binop=, ++, and -- are not overloaded operators. 10864 // * The expression x binop expr must be numerically equivalent to x binop 10865 // (expr). This requirement is satisfied if the operators in expr have 10866 // precedence greater than binop, or by using parentheses around expr or 10867 // subexpressions of expr. 10868 // * The expression expr binop x must be numerically equivalent to (expr) 10869 // binop x. This requirement is satisfied if the operators in expr have 10870 // precedence equal to or greater than binop, or by using parentheses around 10871 // expr or subexpressions of expr. 10872 // * For forms that allow multiple occurrences of x, the number of times 10873 // that x is evaluated is unspecified. 10874 if (AtomicKind == OMPC_read) { 10875 enum { 10876 NotAnExpression, 10877 NotAnAssignmentOp, 10878 NotAScalarType, 10879 NotAnLValue, 10880 NoError 10881 } ErrorFound = NoError; 10882 SourceLocation ErrorLoc, NoteLoc; 10883 SourceRange ErrorRange, NoteRange; 10884 // If clause is read: 10885 // v = x; 10886 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 10887 const auto *AtomicBinOp = 10888 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 10889 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 10890 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); 10891 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts(); 10892 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && 10893 (V->isInstantiationDependent() || V->getType()->isScalarType())) { 10894 if (!X->isLValue() || !V->isLValue()) { 10895 const Expr *NotLValueExpr = X->isLValue() ? V : X; 10896 ErrorFound = NotAnLValue; 10897 ErrorLoc = AtomicBinOp->getExprLoc(); 10898 ErrorRange = AtomicBinOp->getSourceRange(); 10899 NoteLoc = NotLValueExpr->getExprLoc(); 10900 NoteRange = NotLValueExpr->getSourceRange(); 10901 } 10902 } else if (!X->isInstantiationDependent() || 10903 !V->isInstantiationDependent()) { 10904 const Expr *NotScalarExpr = 10905 (X->isInstantiationDependent() || X->getType()->isScalarType()) 10906 ? V 10907 : X; 10908 ErrorFound = NotAScalarType; 10909 ErrorLoc = AtomicBinOp->getExprLoc(); 10910 ErrorRange = AtomicBinOp->getSourceRange(); 10911 NoteLoc = NotScalarExpr->getExprLoc(); 10912 NoteRange = NotScalarExpr->getSourceRange(); 10913 } 10914 } else if (!AtomicBody->isInstantiationDependent()) { 10915 ErrorFound = NotAnAssignmentOp; 10916 ErrorLoc = AtomicBody->getExprLoc(); 10917 ErrorRange = AtomicBody->getSourceRange(); 10918 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 10919 : AtomicBody->getExprLoc(); 10920 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 10921 : AtomicBody->getSourceRange(); 10922 } 10923 } else { 10924 ErrorFound = NotAnExpression; 10925 NoteLoc = ErrorLoc = Body->getBeginLoc(); 10926 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 10927 } 10928 if (ErrorFound != NoError) { 10929 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement) 10930 << ErrorRange; 10931 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound 10932 << NoteRange; 10933 return StmtError(); 10934 } 10935 if (CurContext->isDependentContext()) 10936 V = X = nullptr; 10937 } else if (AtomicKind == OMPC_write) { 10938 enum { 10939 NotAnExpression, 10940 NotAnAssignmentOp, 10941 NotAScalarType, 10942 NotAnLValue, 10943 NoError 10944 } ErrorFound = NoError; 10945 SourceLocation ErrorLoc, NoteLoc; 10946 SourceRange ErrorRange, NoteRange; 10947 // If clause is write: 10948 // x = expr; 10949 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 10950 const auto *AtomicBinOp = 10951 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 10952 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 10953 X = AtomicBinOp->getLHS(); 10954 E = AtomicBinOp->getRHS(); 10955 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && 10956 (E->isInstantiationDependent() || E->getType()->isScalarType())) { 10957 if (!X->isLValue()) { 10958 ErrorFound = NotAnLValue; 10959 ErrorLoc = AtomicBinOp->getExprLoc(); 10960 ErrorRange = AtomicBinOp->getSourceRange(); 10961 NoteLoc = X->getExprLoc(); 10962 NoteRange = X->getSourceRange(); 10963 } 10964 } else if (!X->isInstantiationDependent() || 10965 !E->isInstantiationDependent()) { 10966 const Expr *NotScalarExpr = 10967 (X->isInstantiationDependent() || X->getType()->isScalarType()) 10968 ? E 10969 : X; 10970 ErrorFound = NotAScalarType; 10971 ErrorLoc = AtomicBinOp->getExprLoc(); 10972 ErrorRange = AtomicBinOp->getSourceRange(); 10973 NoteLoc = NotScalarExpr->getExprLoc(); 10974 NoteRange = NotScalarExpr->getSourceRange(); 10975 } 10976 } else if (!AtomicBody->isInstantiationDependent()) { 10977 ErrorFound = NotAnAssignmentOp; 10978 ErrorLoc = AtomicBody->getExprLoc(); 10979 ErrorRange = AtomicBody->getSourceRange(); 10980 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 10981 : AtomicBody->getExprLoc(); 10982 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 10983 : AtomicBody->getSourceRange(); 10984 } 10985 } else { 10986 ErrorFound = NotAnExpression; 10987 NoteLoc = ErrorLoc = Body->getBeginLoc(); 10988 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 10989 } 10990 if (ErrorFound != NoError) { 10991 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement) 10992 << ErrorRange; 10993 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound 10994 << NoteRange; 10995 return StmtError(); 10996 } 10997 if (CurContext->isDependentContext()) 10998 E = X = nullptr; 10999 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) { 11000 // If clause is update: 11001 // x++; 11002 // x--; 11003 // ++x; 11004 // --x; 11005 // x binop= expr; 11006 // x = x binop expr; 11007 // x = expr binop x; 11008 OpenMPAtomicUpdateChecker Checker(*this); 11009 if (Checker.checkStatement( 11010 Body, (AtomicKind == OMPC_update) 11011 ? diag::err_omp_atomic_update_not_expression_statement 11012 : diag::err_omp_atomic_not_expression_statement, 11013 diag::note_omp_atomic_update)) 11014 return StmtError(); 11015 if (!CurContext->isDependentContext()) { 11016 E = Checker.getExpr(); 11017 X = Checker.getX(); 11018 UE = Checker.getUpdateExpr(); 11019 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 11020 } 11021 } else if (AtomicKind == OMPC_capture) { 11022 enum { 11023 NotAnAssignmentOp, 11024 NotACompoundStatement, 11025 NotTwoSubstatements, 11026 NotASpecificExpression, 11027 NoError 11028 } ErrorFound = NoError; 11029 SourceLocation ErrorLoc, NoteLoc; 11030 SourceRange ErrorRange, NoteRange; 11031 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 11032 // If clause is a capture: 11033 // v = x++; 11034 // v = x--; 11035 // v = ++x; 11036 // v = --x; 11037 // v = x binop= expr; 11038 // v = x = x binop expr; 11039 // v = x = expr binop x; 11040 const auto *AtomicBinOp = 11041 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 11042 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 11043 V = AtomicBinOp->getLHS(); 11044 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); 11045 OpenMPAtomicUpdateChecker Checker(*this); 11046 if (Checker.checkStatement( 11047 Body, diag::err_omp_atomic_capture_not_expression_statement, 11048 diag::note_omp_atomic_update)) 11049 return StmtError(); 11050 E = Checker.getExpr(); 11051 X = Checker.getX(); 11052 UE = Checker.getUpdateExpr(); 11053 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 11054 IsPostfixUpdate = Checker.isPostfixUpdate(); 11055 } else if (!AtomicBody->isInstantiationDependent()) { 11056 ErrorLoc = AtomicBody->getExprLoc(); 11057 ErrorRange = AtomicBody->getSourceRange(); 11058 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 11059 : AtomicBody->getExprLoc(); 11060 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 11061 : AtomicBody->getSourceRange(); 11062 ErrorFound = NotAnAssignmentOp; 11063 } 11064 if (ErrorFound != NoError) { 11065 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement) 11066 << ErrorRange; 11067 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; 11068 return StmtError(); 11069 } 11070 if (CurContext->isDependentContext()) 11071 UE = V = E = X = nullptr; 11072 } else { 11073 // If clause is a capture: 11074 // { v = x; x = expr; } 11075 // { v = x; x++; } 11076 // { v = x; x--; } 11077 // { v = x; ++x; } 11078 // { v = x; --x; } 11079 // { v = x; x binop= expr; } 11080 // { v = x; x = x binop expr; } 11081 // { v = x; x = expr binop x; } 11082 // { x++; v = x; } 11083 // { x--; v = x; } 11084 // { ++x; v = x; } 11085 // { --x; v = x; } 11086 // { x binop= expr; v = x; } 11087 // { x = x binop expr; v = x; } 11088 // { x = expr binop x; v = x; } 11089 if (auto *CS = dyn_cast<CompoundStmt>(Body)) { 11090 // Check that this is { expr1; expr2; } 11091 if (CS->size() == 2) { 11092 Stmt *First = CS->body_front(); 11093 Stmt *Second = CS->body_back(); 11094 if (auto *EWC = dyn_cast<ExprWithCleanups>(First)) 11095 First = EWC->getSubExpr()->IgnoreParenImpCasts(); 11096 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second)) 11097 Second = EWC->getSubExpr()->IgnoreParenImpCasts(); 11098 // Need to find what subexpression is 'v' and what is 'x'. 11099 OpenMPAtomicUpdateChecker Checker(*this); 11100 bool IsUpdateExprFound = !Checker.checkStatement(Second); 11101 BinaryOperator *BinOp = nullptr; 11102 if (IsUpdateExprFound) { 11103 BinOp = dyn_cast<BinaryOperator>(First); 11104 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; 11105 } 11106 if (IsUpdateExprFound && !CurContext->isDependentContext()) { 11107 // { v = x; x++; } 11108 // { v = x; x--; } 11109 // { v = x; ++x; } 11110 // { v = x; --x; } 11111 // { v = x; x binop= expr; } 11112 // { v = x; x = x binop expr; } 11113 // { v = x; x = expr binop x; } 11114 // Check that the first expression has form v = x. 11115 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); 11116 llvm::FoldingSetNodeID XId, PossibleXId; 11117 Checker.getX()->Profile(XId, Context, /*Canonical=*/true); 11118 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); 11119 IsUpdateExprFound = XId == PossibleXId; 11120 if (IsUpdateExprFound) { 11121 V = BinOp->getLHS(); 11122 X = Checker.getX(); 11123 E = Checker.getExpr(); 11124 UE = Checker.getUpdateExpr(); 11125 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 11126 IsPostfixUpdate = true; 11127 } 11128 } 11129 if (!IsUpdateExprFound) { 11130 IsUpdateExprFound = !Checker.checkStatement(First); 11131 BinOp = nullptr; 11132 if (IsUpdateExprFound) { 11133 BinOp = dyn_cast<BinaryOperator>(Second); 11134 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; 11135 } 11136 if (IsUpdateExprFound && !CurContext->isDependentContext()) { 11137 // { x++; v = x; } 11138 // { x--; v = x; } 11139 // { ++x; v = x; } 11140 // { --x; v = x; } 11141 // { x binop= expr; v = x; } 11142 // { x = x binop expr; v = x; } 11143 // { x = expr binop x; v = x; } 11144 // Check that the second expression has form v = x. 11145 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); 11146 llvm::FoldingSetNodeID XId, PossibleXId; 11147 Checker.getX()->Profile(XId, Context, /*Canonical=*/true); 11148 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); 11149 IsUpdateExprFound = XId == PossibleXId; 11150 if (IsUpdateExprFound) { 11151 V = BinOp->getLHS(); 11152 X = Checker.getX(); 11153 E = Checker.getExpr(); 11154 UE = Checker.getUpdateExpr(); 11155 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 11156 IsPostfixUpdate = false; 11157 } 11158 } 11159 } 11160 if (!IsUpdateExprFound) { 11161 // { v = x; x = expr; } 11162 auto *FirstExpr = dyn_cast<Expr>(First); 11163 auto *SecondExpr = dyn_cast<Expr>(Second); 11164 if (!FirstExpr || !SecondExpr || 11165 !(FirstExpr->isInstantiationDependent() || 11166 SecondExpr->isInstantiationDependent())) { 11167 auto *FirstBinOp = dyn_cast<BinaryOperator>(First); 11168 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) { 11169 ErrorFound = NotAnAssignmentOp; 11170 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc() 11171 : First->getBeginLoc(); 11172 NoteRange = ErrorRange = FirstBinOp 11173 ? FirstBinOp->getSourceRange() 11174 : SourceRange(ErrorLoc, ErrorLoc); 11175 } else { 11176 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second); 11177 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) { 11178 ErrorFound = NotAnAssignmentOp; 11179 NoteLoc = ErrorLoc = SecondBinOp 11180 ? SecondBinOp->getOperatorLoc() 11181 : Second->getBeginLoc(); 11182 NoteRange = ErrorRange = 11183 SecondBinOp ? SecondBinOp->getSourceRange() 11184 : SourceRange(ErrorLoc, ErrorLoc); 11185 } else { 11186 Expr *PossibleXRHSInFirst = 11187 FirstBinOp->getRHS()->IgnoreParenImpCasts(); 11188 Expr *PossibleXLHSInSecond = 11189 SecondBinOp->getLHS()->IgnoreParenImpCasts(); 11190 llvm::FoldingSetNodeID X1Id, X2Id; 11191 PossibleXRHSInFirst->Profile(X1Id, Context, 11192 /*Canonical=*/true); 11193 PossibleXLHSInSecond->Profile(X2Id, Context, 11194 /*Canonical=*/true); 11195 IsUpdateExprFound = X1Id == X2Id; 11196 if (IsUpdateExprFound) { 11197 V = FirstBinOp->getLHS(); 11198 X = SecondBinOp->getLHS(); 11199 E = SecondBinOp->getRHS(); 11200 UE = nullptr; 11201 IsXLHSInRHSPart = false; 11202 IsPostfixUpdate = true; 11203 } else { 11204 ErrorFound = NotASpecificExpression; 11205 ErrorLoc = FirstBinOp->getExprLoc(); 11206 ErrorRange = FirstBinOp->getSourceRange(); 11207 NoteLoc = SecondBinOp->getLHS()->getExprLoc(); 11208 NoteRange = SecondBinOp->getRHS()->getSourceRange(); 11209 } 11210 } 11211 } 11212 } 11213 } 11214 } else { 11215 NoteLoc = ErrorLoc = Body->getBeginLoc(); 11216 NoteRange = ErrorRange = 11217 SourceRange(Body->getBeginLoc(), Body->getBeginLoc()); 11218 ErrorFound = NotTwoSubstatements; 11219 } 11220 } else { 11221 NoteLoc = ErrorLoc = Body->getBeginLoc(); 11222 NoteRange = ErrorRange = 11223 SourceRange(Body->getBeginLoc(), Body->getBeginLoc()); 11224 ErrorFound = NotACompoundStatement; 11225 } 11226 if (ErrorFound != NoError) { 11227 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement) 11228 << ErrorRange; 11229 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; 11230 return StmtError(); 11231 } 11232 if (CurContext->isDependentContext()) 11233 UE = V = E = X = nullptr; 11234 } 11235 } 11236 11237 setFunctionHasBranchProtectedScope(); 11238 11239 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 11240 X, V, E, UE, IsXLHSInRHSPart, 11241 IsPostfixUpdate); 11242 } 11243 11244 StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses, 11245 Stmt *AStmt, 11246 SourceLocation StartLoc, 11247 SourceLocation EndLoc) { 11248 if (!AStmt) 11249 return StmtError(); 11250 11251 auto *CS = cast<CapturedStmt>(AStmt); 11252 // 1.2.2 OpenMP Language Terminology 11253 // Structured block - An executable statement with a single entry at the 11254 // top and a single exit at the bottom. 11255 // The point of exit cannot be a branch out of the structured block. 11256 // longjmp() and throw() must not violate the entry/exit criteria. 11257 CS->getCapturedDecl()->setNothrow(); 11258 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target); 11259 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11260 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11261 // 1.2.2 OpenMP Language Terminology 11262 // Structured block - An executable statement with a single entry at the 11263 // top and a single exit at the bottom. 11264 // The point of exit cannot be a branch out of the structured block. 11265 // longjmp() and throw() must not violate the entry/exit criteria. 11266 CS->getCapturedDecl()->setNothrow(); 11267 } 11268 11269 // OpenMP [2.16, Nesting of Regions] 11270 // If specified, a teams construct must be contained within a target 11271 // construct. That target construct must contain no statements or directives 11272 // outside of the teams construct. 11273 if (DSAStack->hasInnerTeamsRegion()) { 11274 const Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true); 11275 bool OMPTeamsFound = true; 11276 if (const auto *CS = dyn_cast<CompoundStmt>(S)) { 11277 auto I = CS->body_begin(); 11278 while (I != CS->body_end()) { 11279 const auto *OED = dyn_cast<OMPExecutableDirective>(*I); 11280 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind()) || 11281 OMPTeamsFound) { 11282 11283 OMPTeamsFound = false; 11284 break; 11285 } 11286 ++I; 11287 } 11288 assert(I != CS->body_end() && "Not found statement"); 11289 S = *I; 11290 } else { 11291 const auto *OED = dyn_cast<OMPExecutableDirective>(S); 11292 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind()); 11293 } 11294 if (!OMPTeamsFound) { 11295 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams); 11296 Diag(DSAStack->getInnerTeamsRegionLoc(), 11297 diag::note_omp_nested_teams_construct_here); 11298 Diag(S->getBeginLoc(), diag::note_omp_nested_statement_here) 11299 << isa<OMPExecutableDirective>(S); 11300 return StmtError(); 11301 } 11302 } 11303 11304 setFunctionHasBranchProtectedScope(); 11305 11306 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 11307 } 11308 11309 StmtResult 11310 Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses, 11311 Stmt *AStmt, SourceLocation StartLoc, 11312 SourceLocation EndLoc) { 11313 if (!AStmt) 11314 return StmtError(); 11315 11316 auto *CS = cast<CapturedStmt>(AStmt); 11317 // 1.2.2 OpenMP Language Terminology 11318 // Structured block - An executable statement with a single entry at the 11319 // top and a single exit at the bottom. 11320 // The point of exit cannot be a branch out of the structured block. 11321 // longjmp() and throw() must not violate the entry/exit criteria. 11322 CS->getCapturedDecl()->setNothrow(); 11323 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel); 11324 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11325 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11326 // 1.2.2 OpenMP Language Terminology 11327 // Structured block - An executable statement with a single entry at the 11328 // top and a single exit at the bottom. 11329 // The point of exit cannot be a branch out of the structured block. 11330 // longjmp() and throw() must not violate the entry/exit criteria. 11331 CS->getCapturedDecl()->setNothrow(); 11332 } 11333 11334 setFunctionHasBranchProtectedScope(); 11335 11336 return OMPTargetParallelDirective::Create( 11337 Context, StartLoc, EndLoc, Clauses, AStmt, 11338 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 11339 } 11340 11341 StmtResult Sema::ActOnOpenMPTargetParallelForDirective( 11342 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11343 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11344 if (!AStmt) 11345 return StmtError(); 11346 11347 auto *CS = cast<CapturedStmt>(AStmt); 11348 // 1.2.2 OpenMP Language Terminology 11349 // Structured block - An executable statement with a single entry at the 11350 // top and a single exit at the bottom. 11351 // The point of exit cannot be a branch out of the structured block. 11352 // longjmp() and throw() must not violate the entry/exit criteria. 11353 CS->getCapturedDecl()->setNothrow(); 11354 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for); 11355 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11356 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11357 // 1.2.2 OpenMP Language Terminology 11358 // Structured block - An executable statement with a single entry at the 11359 // top and a single exit at the bottom. 11360 // The point of exit cannot be a branch out of the structured block. 11361 // longjmp() and throw() must not violate the entry/exit criteria. 11362 CS->getCapturedDecl()->setNothrow(); 11363 } 11364 11365 OMPLoopBasedDirective::HelperExprs B; 11366 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11367 // define the nested loops number. 11368 unsigned NestedLoopCount = 11369 checkOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses), 11370 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 11371 VarsWithImplicitDSA, B); 11372 if (NestedLoopCount == 0) 11373 return StmtError(); 11374 11375 assert((CurContext->isDependentContext() || B.builtAll()) && 11376 "omp target parallel for loop exprs were not built"); 11377 11378 if (!CurContext->isDependentContext()) { 11379 // Finalize the clauses that need pre-built expressions for CodeGen. 11380 for (OMPClause *C : Clauses) { 11381 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11382 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11383 B.NumIterations, *this, CurScope, 11384 DSAStack)) 11385 return StmtError(); 11386 } 11387 } 11388 11389 setFunctionHasBranchProtectedScope(); 11390 return OMPTargetParallelForDirective::Create( 11391 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 11392 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 11393 } 11394 11395 /// Check for existence of a map clause in the list of clauses. 11396 static bool hasClauses(ArrayRef<OMPClause *> Clauses, 11397 const OpenMPClauseKind K) { 11398 return llvm::any_of( 11399 Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; }); 11400 } 11401 11402 template <typename... Params> 11403 static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K, 11404 const Params... ClauseTypes) { 11405 return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...); 11406 } 11407 11408 StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses, 11409 Stmt *AStmt, 11410 SourceLocation StartLoc, 11411 SourceLocation EndLoc) { 11412 if (!AStmt) 11413 return StmtError(); 11414 11415 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11416 11417 // OpenMP [2.12.2, target data Construct, Restrictions] 11418 // At least one map, use_device_addr or use_device_ptr clause must appear on 11419 // the directive. 11420 if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr) && 11421 (LangOpts.OpenMP < 50 || !hasClauses(Clauses, OMPC_use_device_addr))) { 11422 StringRef Expected; 11423 if (LangOpts.OpenMP < 50) 11424 Expected = "'map' or 'use_device_ptr'"; 11425 else 11426 Expected = "'map', 'use_device_ptr', or 'use_device_addr'"; 11427 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 11428 << Expected << getOpenMPDirectiveName(OMPD_target_data); 11429 return StmtError(); 11430 } 11431 11432 setFunctionHasBranchProtectedScope(); 11433 11434 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 11435 AStmt); 11436 } 11437 11438 StmtResult 11439 Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses, 11440 SourceLocation StartLoc, 11441 SourceLocation EndLoc, Stmt *AStmt) { 11442 if (!AStmt) 11443 return StmtError(); 11444 11445 auto *CS = cast<CapturedStmt>(AStmt); 11446 // 1.2.2 OpenMP Language Terminology 11447 // Structured block - An executable statement with a single entry at the 11448 // top and a single exit at the bottom. 11449 // The point of exit cannot be a branch out of the structured block. 11450 // longjmp() and throw() must not violate the entry/exit criteria. 11451 CS->getCapturedDecl()->setNothrow(); 11452 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_enter_data); 11453 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11454 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11455 // 1.2.2 OpenMP Language Terminology 11456 // Structured block - An executable statement with a single entry at the 11457 // top and a single exit at the bottom. 11458 // The point of exit cannot be a branch out of the structured block. 11459 // longjmp() and throw() must not violate the entry/exit criteria. 11460 CS->getCapturedDecl()->setNothrow(); 11461 } 11462 11463 // OpenMP [2.10.2, Restrictions, p. 99] 11464 // At least one map clause must appear on the directive. 11465 if (!hasClauses(Clauses, OMPC_map)) { 11466 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 11467 << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data); 11468 return StmtError(); 11469 } 11470 11471 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 11472 AStmt); 11473 } 11474 11475 StmtResult 11476 Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses, 11477 SourceLocation StartLoc, 11478 SourceLocation EndLoc, Stmt *AStmt) { 11479 if (!AStmt) 11480 return StmtError(); 11481 11482 auto *CS = cast<CapturedStmt>(AStmt); 11483 // 1.2.2 OpenMP Language Terminology 11484 // Structured block - An executable statement with a single entry at the 11485 // top and a single exit at the bottom. 11486 // The point of exit cannot be a branch out of the structured block. 11487 // longjmp() and throw() must not violate the entry/exit criteria. 11488 CS->getCapturedDecl()->setNothrow(); 11489 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_exit_data); 11490 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11491 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11492 // 1.2.2 OpenMP Language Terminology 11493 // Structured block - An executable statement with a single entry at the 11494 // top and a single exit at the bottom. 11495 // The point of exit cannot be a branch out of the structured block. 11496 // longjmp() and throw() must not violate the entry/exit criteria. 11497 CS->getCapturedDecl()->setNothrow(); 11498 } 11499 11500 // OpenMP [2.10.3, Restrictions, p. 102] 11501 // At least one map clause must appear on the directive. 11502 if (!hasClauses(Clauses, OMPC_map)) { 11503 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 11504 << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data); 11505 return StmtError(); 11506 } 11507 11508 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 11509 AStmt); 11510 } 11511 11512 StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses, 11513 SourceLocation StartLoc, 11514 SourceLocation EndLoc, 11515 Stmt *AStmt) { 11516 if (!AStmt) 11517 return StmtError(); 11518 11519 auto *CS = cast<CapturedStmt>(AStmt); 11520 // 1.2.2 OpenMP Language Terminology 11521 // Structured block - An executable statement with a single entry at the 11522 // top and a single exit at the bottom. 11523 // The point of exit cannot be a branch out of the structured block. 11524 // longjmp() and throw() must not violate the entry/exit criteria. 11525 CS->getCapturedDecl()->setNothrow(); 11526 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_update); 11527 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11528 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11529 // 1.2.2 OpenMP Language Terminology 11530 // Structured block - An executable statement with a single entry at the 11531 // top and a single exit at the bottom. 11532 // The point of exit cannot be a branch out of the structured block. 11533 // longjmp() and throw() must not violate the entry/exit criteria. 11534 CS->getCapturedDecl()->setNothrow(); 11535 } 11536 11537 if (!hasClauses(Clauses, OMPC_to, OMPC_from)) { 11538 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required); 11539 return StmtError(); 11540 } 11541 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses, 11542 AStmt); 11543 } 11544 11545 StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses, 11546 Stmt *AStmt, SourceLocation StartLoc, 11547 SourceLocation EndLoc) { 11548 if (!AStmt) 11549 return StmtError(); 11550 11551 auto *CS = cast<CapturedStmt>(AStmt); 11552 // 1.2.2 OpenMP Language Terminology 11553 // Structured block - An executable statement with a single entry at the 11554 // top and a single exit at the bottom. 11555 // The point of exit cannot be a branch out of the structured block. 11556 // longjmp() and throw() must not violate the entry/exit criteria. 11557 CS->getCapturedDecl()->setNothrow(); 11558 11559 setFunctionHasBranchProtectedScope(); 11560 11561 DSAStack->setParentTeamsRegionLoc(StartLoc); 11562 11563 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 11564 } 11565 11566 StmtResult 11567 Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc, 11568 SourceLocation EndLoc, 11569 OpenMPDirectiveKind CancelRegion) { 11570 if (DSAStack->isParentNowaitRegion()) { 11571 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0; 11572 return StmtError(); 11573 } 11574 if (DSAStack->isParentOrderedRegion()) { 11575 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0; 11576 return StmtError(); 11577 } 11578 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc, 11579 CancelRegion); 11580 } 11581 11582 StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses, 11583 SourceLocation StartLoc, 11584 SourceLocation EndLoc, 11585 OpenMPDirectiveKind CancelRegion) { 11586 if (DSAStack->isParentNowaitRegion()) { 11587 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1; 11588 return StmtError(); 11589 } 11590 if (DSAStack->isParentOrderedRegion()) { 11591 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1; 11592 return StmtError(); 11593 } 11594 DSAStack->setParentCancelRegion(/*Cancel=*/true); 11595 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses, 11596 CancelRegion); 11597 } 11598 11599 static bool checkReductionClauseWithNogroup(Sema &S, 11600 ArrayRef<OMPClause *> Clauses) { 11601 const OMPClause *ReductionClause = nullptr; 11602 const OMPClause *NogroupClause = nullptr; 11603 for (const OMPClause *C : Clauses) { 11604 if (C->getClauseKind() == OMPC_reduction) { 11605 ReductionClause = C; 11606 if (NogroupClause) 11607 break; 11608 continue; 11609 } 11610 if (C->getClauseKind() == OMPC_nogroup) { 11611 NogroupClause = C; 11612 if (ReductionClause) 11613 break; 11614 continue; 11615 } 11616 } 11617 if (ReductionClause && NogroupClause) { 11618 S.Diag(ReductionClause->getBeginLoc(), diag::err_omp_reduction_with_nogroup) 11619 << SourceRange(NogroupClause->getBeginLoc(), 11620 NogroupClause->getEndLoc()); 11621 return true; 11622 } 11623 return false; 11624 } 11625 11626 StmtResult Sema::ActOnOpenMPTaskLoopDirective( 11627 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11628 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11629 if (!AStmt) 11630 return StmtError(); 11631 11632 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11633 OMPLoopBasedDirective::HelperExprs B; 11634 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11635 // define the nested loops number. 11636 unsigned NestedLoopCount = 11637 checkOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses), 11638 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 11639 VarsWithImplicitDSA, B); 11640 if (NestedLoopCount == 0) 11641 return StmtError(); 11642 11643 assert((CurContext->isDependentContext() || B.builtAll()) && 11644 "omp for loop exprs were not built"); 11645 11646 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11647 // The grainsize clause and num_tasks clause are mutually exclusive and may 11648 // not appear on the same taskloop directive. 11649 if (checkMutuallyExclusiveClauses(*this, Clauses, 11650 {OMPC_grainsize, OMPC_num_tasks})) 11651 return StmtError(); 11652 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11653 // If a reduction clause is present on the taskloop directive, the nogroup 11654 // clause must not be specified. 11655 if (checkReductionClauseWithNogroup(*this, Clauses)) 11656 return StmtError(); 11657 11658 setFunctionHasBranchProtectedScope(); 11659 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc, 11660 NestedLoopCount, Clauses, AStmt, B, 11661 DSAStack->isCancelRegion()); 11662 } 11663 11664 StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective( 11665 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11666 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11667 if (!AStmt) 11668 return StmtError(); 11669 11670 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11671 OMPLoopBasedDirective::HelperExprs B; 11672 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11673 // define the nested loops number. 11674 unsigned NestedLoopCount = 11675 checkOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses), 11676 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 11677 VarsWithImplicitDSA, B); 11678 if (NestedLoopCount == 0) 11679 return StmtError(); 11680 11681 assert((CurContext->isDependentContext() || B.builtAll()) && 11682 "omp for loop exprs were not built"); 11683 11684 if (!CurContext->isDependentContext()) { 11685 // Finalize the clauses that need pre-built expressions for CodeGen. 11686 for (OMPClause *C : Clauses) { 11687 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11688 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11689 B.NumIterations, *this, CurScope, 11690 DSAStack)) 11691 return StmtError(); 11692 } 11693 } 11694 11695 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11696 // The grainsize clause and num_tasks clause are mutually exclusive and may 11697 // not appear on the same taskloop directive. 11698 if (checkMutuallyExclusiveClauses(*this, Clauses, 11699 {OMPC_grainsize, OMPC_num_tasks})) 11700 return StmtError(); 11701 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11702 // If a reduction clause is present on the taskloop directive, the nogroup 11703 // clause must not be specified. 11704 if (checkReductionClauseWithNogroup(*this, Clauses)) 11705 return StmtError(); 11706 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11707 return StmtError(); 11708 11709 setFunctionHasBranchProtectedScope(); 11710 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc, 11711 NestedLoopCount, Clauses, AStmt, B); 11712 } 11713 11714 StmtResult Sema::ActOnOpenMPMasterTaskLoopDirective( 11715 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11716 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11717 if (!AStmt) 11718 return StmtError(); 11719 11720 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11721 OMPLoopBasedDirective::HelperExprs B; 11722 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11723 // define the nested loops number. 11724 unsigned NestedLoopCount = 11725 checkOpenMPLoop(OMPD_master_taskloop, getCollapseNumberExpr(Clauses), 11726 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 11727 VarsWithImplicitDSA, B); 11728 if (NestedLoopCount == 0) 11729 return StmtError(); 11730 11731 assert((CurContext->isDependentContext() || B.builtAll()) && 11732 "omp for loop exprs were not built"); 11733 11734 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11735 // The grainsize clause and num_tasks clause are mutually exclusive and may 11736 // not appear on the same taskloop directive. 11737 if (checkMutuallyExclusiveClauses(*this, Clauses, 11738 {OMPC_grainsize, OMPC_num_tasks})) 11739 return StmtError(); 11740 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11741 // If a reduction clause is present on the taskloop directive, the nogroup 11742 // clause must not be specified. 11743 if (checkReductionClauseWithNogroup(*this, Clauses)) 11744 return StmtError(); 11745 11746 setFunctionHasBranchProtectedScope(); 11747 return OMPMasterTaskLoopDirective::Create(Context, StartLoc, EndLoc, 11748 NestedLoopCount, Clauses, AStmt, B, 11749 DSAStack->isCancelRegion()); 11750 } 11751 11752 StmtResult Sema::ActOnOpenMPMasterTaskLoopSimdDirective( 11753 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11754 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11755 if (!AStmt) 11756 return StmtError(); 11757 11758 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11759 OMPLoopBasedDirective::HelperExprs B; 11760 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11761 // define the nested loops number. 11762 unsigned NestedLoopCount = 11763 checkOpenMPLoop(OMPD_master_taskloop_simd, getCollapseNumberExpr(Clauses), 11764 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 11765 VarsWithImplicitDSA, B); 11766 if (NestedLoopCount == 0) 11767 return StmtError(); 11768 11769 assert((CurContext->isDependentContext() || B.builtAll()) && 11770 "omp for loop exprs were not built"); 11771 11772 if (!CurContext->isDependentContext()) { 11773 // Finalize the clauses that need pre-built expressions for CodeGen. 11774 for (OMPClause *C : Clauses) { 11775 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11776 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11777 B.NumIterations, *this, CurScope, 11778 DSAStack)) 11779 return StmtError(); 11780 } 11781 } 11782 11783 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11784 // The grainsize clause and num_tasks clause are mutually exclusive and may 11785 // not appear on the same taskloop directive. 11786 if (checkMutuallyExclusiveClauses(*this, Clauses, 11787 {OMPC_grainsize, OMPC_num_tasks})) 11788 return StmtError(); 11789 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11790 // If a reduction clause is present on the taskloop directive, the nogroup 11791 // clause must not be specified. 11792 if (checkReductionClauseWithNogroup(*this, Clauses)) 11793 return StmtError(); 11794 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11795 return StmtError(); 11796 11797 setFunctionHasBranchProtectedScope(); 11798 return OMPMasterTaskLoopSimdDirective::Create( 11799 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 11800 } 11801 11802 StmtResult Sema::ActOnOpenMPParallelMasterTaskLoopDirective( 11803 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11804 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11805 if (!AStmt) 11806 return StmtError(); 11807 11808 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11809 auto *CS = cast<CapturedStmt>(AStmt); 11810 // 1.2.2 OpenMP Language Terminology 11811 // Structured block - An executable statement with a single entry at the 11812 // top and a single exit at the bottom. 11813 // The point of exit cannot be a branch out of the structured block. 11814 // longjmp() and throw() must not violate the entry/exit criteria. 11815 CS->getCapturedDecl()->setNothrow(); 11816 for (int ThisCaptureLevel = 11817 getOpenMPCaptureLevels(OMPD_parallel_master_taskloop); 11818 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11819 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11820 // 1.2.2 OpenMP Language Terminology 11821 // Structured block - An executable statement with a single entry at the 11822 // top and a single exit at the bottom. 11823 // The point of exit cannot be a branch out of the structured block. 11824 // longjmp() and throw() must not violate the entry/exit criteria. 11825 CS->getCapturedDecl()->setNothrow(); 11826 } 11827 11828 OMPLoopBasedDirective::HelperExprs B; 11829 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11830 // define the nested loops number. 11831 unsigned NestedLoopCount = checkOpenMPLoop( 11832 OMPD_parallel_master_taskloop, getCollapseNumberExpr(Clauses), 11833 /*OrderedLoopCountExpr=*/nullptr, CS, *this, *DSAStack, 11834 VarsWithImplicitDSA, B); 11835 if (NestedLoopCount == 0) 11836 return StmtError(); 11837 11838 assert((CurContext->isDependentContext() || B.builtAll()) && 11839 "omp for loop exprs were not built"); 11840 11841 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11842 // The grainsize clause and num_tasks clause are mutually exclusive and may 11843 // not appear on the same taskloop directive. 11844 if (checkMutuallyExclusiveClauses(*this, Clauses, 11845 {OMPC_grainsize, OMPC_num_tasks})) 11846 return StmtError(); 11847 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11848 // If a reduction clause is present on the taskloop directive, the nogroup 11849 // clause must not be specified. 11850 if (checkReductionClauseWithNogroup(*this, Clauses)) 11851 return StmtError(); 11852 11853 setFunctionHasBranchProtectedScope(); 11854 return OMPParallelMasterTaskLoopDirective::Create( 11855 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 11856 DSAStack->isCancelRegion()); 11857 } 11858 11859 StmtResult Sema::ActOnOpenMPParallelMasterTaskLoopSimdDirective( 11860 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11861 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11862 if (!AStmt) 11863 return StmtError(); 11864 11865 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11866 auto *CS = cast<CapturedStmt>(AStmt); 11867 // 1.2.2 OpenMP Language Terminology 11868 // Structured block - An executable statement with a single entry at the 11869 // top and a single exit at the bottom. 11870 // The point of exit cannot be a branch out of the structured block. 11871 // longjmp() and throw() must not violate the entry/exit criteria. 11872 CS->getCapturedDecl()->setNothrow(); 11873 for (int ThisCaptureLevel = 11874 getOpenMPCaptureLevels(OMPD_parallel_master_taskloop_simd); 11875 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11876 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11877 // 1.2.2 OpenMP Language Terminology 11878 // Structured block - An executable statement with a single entry at the 11879 // top and a single exit at the bottom. 11880 // The point of exit cannot be a branch out of the structured block. 11881 // longjmp() and throw() must not violate the entry/exit criteria. 11882 CS->getCapturedDecl()->setNothrow(); 11883 } 11884 11885 OMPLoopBasedDirective::HelperExprs B; 11886 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11887 // define the nested loops number. 11888 unsigned NestedLoopCount = checkOpenMPLoop( 11889 OMPD_parallel_master_taskloop_simd, getCollapseNumberExpr(Clauses), 11890 /*OrderedLoopCountExpr=*/nullptr, CS, *this, *DSAStack, 11891 VarsWithImplicitDSA, B); 11892 if (NestedLoopCount == 0) 11893 return StmtError(); 11894 11895 assert((CurContext->isDependentContext() || B.builtAll()) && 11896 "omp for loop exprs were not built"); 11897 11898 if (!CurContext->isDependentContext()) { 11899 // Finalize the clauses that need pre-built expressions for CodeGen. 11900 for (OMPClause *C : Clauses) { 11901 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11902 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11903 B.NumIterations, *this, CurScope, 11904 DSAStack)) 11905 return StmtError(); 11906 } 11907 } 11908 11909 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11910 // The grainsize clause and num_tasks clause are mutually exclusive and may 11911 // not appear on the same taskloop directive. 11912 if (checkMutuallyExclusiveClauses(*this, Clauses, 11913 {OMPC_grainsize, OMPC_num_tasks})) 11914 return StmtError(); 11915 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11916 // If a reduction clause is present on the taskloop directive, the nogroup 11917 // clause must not be specified. 11918 if (checkReductionClauseWithNogroup(*this, Clauses)) 11919 return StmtError(); 11920 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11921 return StmtError(); 11922 11923 setFunctionHasBranchProtectedScope(); 11924 return OMPParallelMasterTaskLoopSimdDirective::Create( 11925 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 11926 } 11927 11928 StmtResult Sema::ActOnOpenMPDistributeDirective( 11929 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11930 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11931 if (!AStmt) 11932 return StmtError(); 11933 11934 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11935 OMPLoopBasedDirective::HelperExprs B; 11936 // In presence of clause 'collapse' with number of loops, it will 11937 // define the nested loops number. 11938 unsigned NestedLoopCount = 11939 checkOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses), 11940 nullptr /*ordered not a clause on distribute*/, AStmt, 11941 *this, *DSAStack, VarsWithImplicitDSA, B); 11942 if (NestedLoopCount == 0) 11943 return StmtError(); 11944 11945 assert((CurContext->isDependentContext() || B.builtAll()) && 11946 "omp for loop exprs were not built"); 11947 11948 setFunctionHasBranchProtectedScope(); 11949 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc, 11950 NestedLoopCount, Clauses, AStmt, B); 11951 } 11952 11953 StmtResult Sema::ActOnOpenMPDistributeParallelForDirective( 11954 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11955 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11956 if (!AStmt) 11957 return StmtError(); 11958 11959 auto *CS = cast<CapturedStmt>(AStmt); 11960 // 1.2.2 OpenMP Language Terminology 11961 // Structured block - An executable statement with a single entry at the 11962 // top and a single exit at the bottom. 11963 // The point of exit cannot be a branch out of the structured block. 11964 // longjmp() and throw() must not violate the entry/exit criteria. 11965 CS->getCapturedDecl()->setNothrow(); 11966 for (int ThisCaptureLevel = 11967 getOpenMPCaptureLevels(OMPD_distribute_parallel_for); 11968 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11969 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11970 // 1.2.2 OpenMP Language Terminology 11971 // Structured block - An executable statement with a single entry at the 11972 // top and a single exit at the bottom. 11973 // The point of exit cannot be a branch out of the structured block. 11974 // longjmp() and throw() must not violate the entry/exit criteria. 11975 CS->getCapturedDecl()->setNothrow(); 11976 } 11977 11978 OMPLoopBasedDirective::HelperExprs B; 11979 // In presence of clause 'collapse' with number of loops, it will 11980 // define the nested loops number. 11981 unsigned NestedLoopCount = checkOpenMPLoop( 11982 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses), 11983 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 11984 VarsWithImplicitDSA, B); 11985 if (NestedLoopCount == 0) 11986 return StmtError(); 11987 11988 assert((CurContext->isDependentContext() || B.builtAll()) && 11989 "omp for loop exprs were not built"); 11990 11991 setFunctionHasBranchProtectedScope(); 11992 return OMPDistributeParallelForDirective::Create( 11993 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 11994 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 11995 } 11996 11997 StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective( 11998 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11999 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12000 if (!AStmt) 12001 return StmtError(); 12002 12003 auto *CS = cast<CapturedStmt>(AStmt); 12004 // 1.2.2 OpenMP Language Terminology 12005 // Structured block - An executable statement with a single entry at the 12006 // top and a single exit at the bottom. 12007 // The point of exit cannot be a branch out of the structured block. 12008 // longjmp() and throw() must not violate the entry/exit criteria. 12009 CS->getCapturedDecl()->setNothrow(); 12010 for (int ThisCaptureLevel = 12011 getOpenMPCaptureLevels(OMPD_distribute_parallel_for_simd); 12012 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12013 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12014 // 1.2.2 OpenMP Language Terminology 12015 // Structured block - An executable statement with a single entry at the 12016 // top and a single exit at the bottom. 12017 // The point of exit cannot be a branch out of the structured block. 12018 // longjmp() and throw() must not violate the entry/exit criteria. 12019 CS->getCapturedDecl()->setNothrow(); 12020 } 12021 12022 OMPLoopBasedDirective::HelperExprs B; 12023 // In presence of clause 'collapse' with number of loops, it will 12024 // define the nested loops number. 12025 unsigned NestedLoopCount = checkOpenMPLoop( 12026 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses), 12027 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 12028 VarsWithImplicitDSA, B); 12029 if (NestedLoopCount == 0) 12030 return StmtError(); 12031 12032 assert((CurContext->isDependentContext() || B.builtAll()) && 12033 "omp for loop exprs were not built"); 12034 12035 if (!CurContext->isDependentContext()) { 12036 // Finalize the clauses that need pre-built expressions for CodeGen. 12037 for (OMPClause *C : Clauses) { 12038 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12039 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12040 B.NumIterations, *this, CurScope, 12041 DSAStack)) 12042 return StmtError(); 12043 } 12044 } 12045 12046 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12047 return StmtError(); 12048 12049 setFunctionHasBranchProtectedScope(); 12050 return OMPDistributeParallelForSimdDirective::Create( 12051 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12052 } 12053 12054 StmtResult Sema::ActOnOpenMPDistributeSimdDirective( 12055 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12056 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12057 if (!AStmt) 12058 return StmtError(); 12059 12060 auto *CS = cast<CapturedStmt>(AStmt); 12061 // 1.2.2 OpenMP Language Terminology 12062 // Structured block - An executable statement with a single entry at the 12063 // top and a single exit at the bottom. 12064 // The point of exit cannot be a branch out of the structured block. 12065 // longjmp() and throw() must not violate the entry/exit criteria. 12066 CS->getCapturedDecl()->setNothrow(); 12067 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_distribute_simd); 12068 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12069 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12070 // 1.2.2 OpenMP Language Terminology 12071 // Structured block - An executable statement with a single entry at the 12072 // top and a single exit at the bottom. 12073 // The point of exit cannot be a branch out of the structured block. 12074 // longjmp() and throw() must not violate the entry/exit criteria. 12075 CS->getCapturedDecl()->setNothrow(); 12076 } 12077 12078 OMPLoopBasedDirective::HelperExprs B; 12079 // In presence of clause 'collapse' with number of loops, it will 12080 // define the nested loops number. 12081 unsigned NestedLoopCount = 12082 checkOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses), 12083 nullptr /*ordered not a clause on distribute*/, CS, *this, 12084 *DSAStack, VarsWithImplicitDSA, B); 12085 if (NestedLoopCount == 0) 12086 return StmtError(); 12087 12088 assert((CurContext->isDependentContext() || B.builtAll()) && 12089 "omp for loop exprs were not built"); 12090 12091 if (!CurContext->isDependentContext()) { 12092 // Finalize the clauses that need pre-built expressions for CodeGen. 12093 for (OMPClause *C : Clauses) { 12094 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12095 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12096 B.NumIterations, *this, CurScope, 12097 DSAStack)) 12098 return StmtError(); 12099 } 12100 } 12101 12102 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12103 return StmtError(); 12104 12105 setFunctionHasBranchProtectedScope(); 12106 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc, 12107 NestedLoopCount, Clauses, AStmt, B); 12108 } 12109 12110 StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective( 12111 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12112 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12113 if (!AStmt) 12114 return StmtError(); 12115 12116 auto *CS = cast<CapturedStmt>(AStmt); 12117 // 1.2.2 OpenMP Language Terminology 12118 // Structured block - An executable statement with a single entry at the 12119 // top and a single exit at the bottom. 12120 // The point of exit cannot be a branch out of the structured block. 12121 // longjmp() and throw() must not violate the entry/exit criteria. 12122 CS->getCapturedDecl()->setNothrow(); 12123 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for); 12124 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12125 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12126 // 1.2.2 OpenMP Language Terminology 12127 // Structured block - An executable statement with a single entry at the 12128 // top and a single exit at the bottom. 12129 // The point of exit cannot be a branch out of the structured block. 12130 // longjmp() and throw() must not violate the entry/exit criteria. 12131 CS->getCapturedDecl()->setNothrow(); 12132 } 12133 12134 OMPLoopBasedDirective::HelperExprs B; 12135 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 12136 // define the nested loops number. 12137 unsigned NestedLoopCount = checkOpenMPLoop( 12138 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses), 12139 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 12140 VarsWithImplicitDSA, B); 12141 if (NestedLoopCount == 0) 12142 return StmtError(); 12143 12144 assert((CurContext->isDependentContext() || B.builtAll()) && 12145 "omp target parallel for simd loop exprs were not built"); 12146 12147 if (!CurContext->isDependentContext()) { 12148 // Finalize the clauses that need pre-built expressions for CodeGen. 12149 for (OMPClause *C : Clauses) { 12150 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12151 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12152 B.NumIterations, *this, CurScope, 12153 DSAStack)) 12154 return StmtError(); 12155 } 12156 } 12157 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12158 return StmtError(); 12159 12160 setFunctionHasBranchProtectedScope(); 12161 return OMPTargetParallelForSimdDirective::Create( 12162 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12163 } 12164 12165 StmtResult Sema::ActOnOpenMPTargetSimdDirective( 12166 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12167 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12168 if (!AStmt) 12169 return StmtError(); 12170 12171 auto *CS = cast<CapturedStmt>(AStmt); 12172 // 1.2.2 OpenMP Language Terminology 12173 // Structured block - An executable statement with a single entry at the 12174 // top and a single exit at the bottom. 12175 // The point of exit cannot be a branch out of the structured block. 12176 // longjmp() and throw() must not violate the entry/exit criteria. 12177 CS->getCapturedDecl()->setNothrow(); 12178 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_simd); 12179 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12180 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12181 // 1.2.2 OpenMP Language Terminology 12182 // Structured block - An executable statement with a single entry at the 12183 // top and a single exit at the bottom. 12184 // The point of exit cannot be a branch out of the structured block. 12185 // longjmp() and throw() must not violate the entry/exit criteria. 12186 CS->getCapturedDecl()->setNothrow(); 12187 } 12188 12189 OMPLoopBasedDirective::HelperExprs B; 12190 // In presence of clause 'collapse' with number of loops, it will define the 12191 // nested loops number. 12192 unsigned NestedLoopCount = 12193 checkOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses), 12194 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 12195 VarsWithImplicitDSA, B); 12196 if (NestedLoopCount == 0) 12197 return StmtError(); 12198 12199 assert((CurContext->isDependentContext() || B.builtAll()) && 12200 "omp target simd loop exprs were not built"); 12201 12202 if (!CurContext->isDependentContext()) { 12203 // Finalize the clauses that need pre-built expressions for CodeGen. 12204 for (OMPClause *C : Clauses) { 12205 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12206 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12207 B.NumIterations, *this, CurScope, 12208 DSAStack)) 12209 return StmtError(); 12210 } 12211 } 12212 12213 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12214 return StmtError(); 12215 12216 setFunctionHasBranchProtectedScope(); 12217 return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc, 12218 NestedLoopCount, Clauses, AStmt, B); 12219 } 12220 12221 StmtResult Sema::ActOnOpenMPTeamsDistributeDirective( 12222 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12223 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12224 if (!AStmt) 12225 return StmtError(); 12226 12227 auto *CS = cast<CapturedStmt>(AStmt); 12228 // 1.2.2 OpenMP Language Terminology 12229 // Structured block - An executable statement with a single entry at the 12230 // top and a single exit at the bottom. 12231 // The point of exit cannot be a branch out of the structured block. 12232 // longjmp() and throw() must not violate the entry/exit criteria. 12233 CS->getCapturedDecl()->setNothrow(); 12234 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_teams_distribute); 12235 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12236 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12237 // 1.2.2 OpenMP Language Terminology 12238 // Structured block - An executable statement with a single entry at the 12239 // top and a single exit at the bottom. 12240 // The point of exit cannot be a branch out of the structured block. 12241 // longjmp() and throw() must not violate the entry/exit criteria. 12242 CS->getCapturedDecl()->setNothrow(); 12243 } 12244 12245 OMPLoopBasedDirective::HelperExprs B; 12246 // In presence of clause 'collapse' with number of loops, it will 12247 // define the nested loops number. 12248 unsigned NestedLoopCount = 12249 checkOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses), 12250 nullptr /*ordered not a clause on distribute*/, CS, *this, 12251 *DSAStack, VarsWithImplicitDSA, B); 12252 if (NestedLoopCount == 0) 12253 return StmtError(); 12254 12255 assert((CurContext->isDependentContext() || B.builtAll()) && 12256 "omp teams distribute loop exprs were not built"); 12257 12258 setFunctionHasBranchProtectedScope(); 12259 12260 DSAStack->setParentTeamsRegionLoc(StartLoc); 12261 12262 return OMPTeamsDistributeDirective::Create( 12263 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12264 } 12265 12266 StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective( 12267 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12268 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12269 if (!AStmt) 12270 return StmtError(); 12271 12272 auto *CS = cast<CapturedStmt>(AStmt); 12273 // 1.2.2 OpenMP Language Terminology 12274 // Structured block - An executable statement with a single entry at the 12275 // top and a single exit at the bottom. 12276 // The point of exit cannot be a branch out of the structured block. 12277 // longjmp() and throw() must not violate the entry/exit criteria. 12278 CS->getCapturedDecl()->setNothrow(); 12279 for (int ThisCaptureLevel = 12280 getOpenMPCaptureLevels(OMPD_teams_distribute_simd); 12281 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12282 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12283 // 1.2.2 OpenMP Language Terminology 12284 // Structured block - An executable statement with a single entry at the 12285 // top and a single exit at the bottom. 12286 // The point of exit cannot be a branch out of the structured block. 12287 // longjmp() and throw() must not violate the entry/exit criteria. 12288 CS->getCapturedDecl()->setNothrow(); 12289 } 12290 12291 OMPLoopBasedDirective::HelperExprs B; 12292 // In presence of clause 'collapse' with number of loops, it will 12293 // define the nested loops number. 12294 unsigned NestedLoopCount = checkOpenMPLoop( 12295 OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses), 12296 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 12297 VarsWithImplicitDSA, B); 12298 12299 if (NestedLoopCount == 0) 12300 return StmtError(); 12301 12302 assert((CurContext->isDependentContext() || B.builtAll()) && 12303 "omp teams distribute simd loop exprs were not built"); 12304 12305 if (!CurContext->isDependentContext()) { 12306 // Finalize the clauses that need pre-built expressions for CodeGen. 12307 for (OMPClause *C : Clauses) { 12308 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12309 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12310 B.NumIterations, *this, CurScope, 12311 DSAStack)) 12312 return StmtError(); 12313 } 12314 } 12315 12316 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12317 return StmtError(); 12318 12319 setFunctionHasBranchProtectedScope(); 12320 12321 DSAStack->setParentTeamsRegionLoc(StartLoc); 12322 12323 return OMPTeamsDistributeSimdDirective::Create( 12324 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12325 } 12326 12327 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective( 12328 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12329 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12330 if (!AStmt) 12331 return StmtError(); 12332 12333 auto *CS = cast<CapturedStmt>(AStmt); 12334 // 1.2.2 OpenMP Language Terminology 12335 // Structured block - An executable statement with a single entry at the 12336 // top and a single exit at the bottom. 12337 // The point of exit cannot be a branch out of the structured block. 12338 // longjmp() and throw() must not violate the entry/exit criteria. 12339 CS->getCapturedDecl()->setNothrow(); 12340 12341 for (int ThisCaptureLevel = 12342 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for_simd); 12343 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12344 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12345 // 1.2.2 OpenMP Language Terminology 12346 // Structured block - An executable statement with a single entry at the 12347 // top and a single exit at the bottom. 12348 // The point of exit cannot be a branch out of the structured block. 12349 // longjmp() and throw() must not violate the entry/exit criteria. 12350 CS->getCapturedDecl()->setNothrow(); 12351 } 12352 12353 OMPLoopBasedDirective::HelperExprs B; 12354 // In presence of clause 'collapse' with number of loops, it will 12355 // define the nested loops number. 12356 unsigned NestedLoopCount = checkOpenMPLoop( 12357 OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses), 12358 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 12359 VarsWithImplicitDSA, B); 12360 12361 if (NestedLoopCount == 0) 12362 return StmtError(); 12363 12364 assert((CurContext->isDependentContext() || B.builtAll()) && 12365 "omp for loop exprs were not built"); 12366 12367 if (!CurContext->isDependentContext()) { 12368 // Finalize the clauses that need pre-built expressions for CodeGen. 12369 for (OMPClause *C : Clauses) { 12370 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12371 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12372 B.NumIterations, *this, CurScope, 12373 DSAStack)) 12374 return StmtError(); 12375 } 12376 } 12377 12378 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12379 return StmtError(); 12380 12381 setFunctionHasBranchProtectedScope(); 12382 12383 DSAStack->setParentTeamsRegionLoc(StartLoc); 12384 12385 return OMPTeamsDistributeParallelForSimdDirective::Create( 12386 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12387 } 12388 12389 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective( 12390 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12391 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12392 if (!AStmt) 12393 return StmtError(); 12394 12395 auto *CS = cast<CapturedStmt>(AStmt); 12396 // 1.2.2 OpenMP Language Terminology 12397 // Structured block - An executable statement with a single entry at the 12398 // top and a single exit at the bottom. 12399 // The point of exit cannot be a branch out of the structured block. 12400 // longjmp() and throw() must not violate the entry/exit criteria. 12401 CS->getCapturedDecl()->setNothrow(); 12402 12403 for (int ThisCaptureLevel = 12404 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for); 12405 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12406 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12407 // 1.2.2 OpenMP Language Terminology 12408 // Structured block - An executable statement with a single entry at the 12409 // top and a single exit at the bottom. 12410 // The point of exit cannot be a branch out of the structured block. 12411 // longjmp() and throw() must not violate the entry/exit criteria. 12412 CS->getCapturedDecl()->setNothrow(); 12413 } 12414 12415 OMPLoopBasedDirective::HelperExprs B; 12416 // In presence of clause 'collapse' with number of loops, it will 12417 // define the nested loops number. 12418 unsigned NestedLoopCount = checkOpenMPLoop( 12419 OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses), 12420 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 12421 VarsWithImplicitDSA, B); 12422 12423 if (NestedLoopCount == 0) 12424 return StmtError(); 12425 12426 assert((CurContext->isDependentContext() || B.builtAll()) && 12427 "omp for loop exprs were not built"); 12428 12429 setFunctionHasBranchProtectedScope(); 12430 12431 DSAStack->setParentTeamsRegionLoc(StartLoc); 12432 12433 return OMPTeamsDistributeParallelForDirective::Create( 12434 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 12435 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 12436 } 12437 12438 StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses, 12439 Stmt *AStmt, 12440 SourceLocation StartLoc, 12441 SourceLocation EndLoc) { 12442 if (!AStmt) 12443 return StmtError(); 12444 12445 auto *CS = cast<CapturedStmt>(AStmt); 12446 // 1.2.2 OpenMP Language Terminology 12447 // Structured block - An executable statement with a single entry at the 12448 // top and a single exit at the bottom. 12449 // The point of exit cannot be a branch out of the structured block. 12450 // longjmp() and throw() must not violate the entry/exit criteria. 12451 CS->getCapturedDecl()->setNothrow(); 12452 12453 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_teams); 12454 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12455 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12456 // 1.2.2 OpenMP Language Terminology 12457 // Structured block - An executable statement with a single entry at the 12458 // top and a single exit at the bottom. 12459 // The point of exit cannot be a branch out of the structured block. 12460 // longjmp() and throw() must not violate the entry/exit criteria. 12461 CS->getCapturedDecl()->setNothrow(); 12462 } 12463 setFunctionHasBranchProtectedScope(); 12464 12465 return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, 12466 AStmt); 12467 } 12468 12469 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective( 12470 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12471 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12472 if (!AStmt) 12473 return StmtError(); 12474 12475 auto *CS = cast<CapturedStmt>(AStmt); 12476 // 1.2.2 OpenMP Language Terminology 12477 // Structured block - An executable statement with a single entry at the 12478 // top and a single exit at the bottom. 12479 // The point of exit cannot be a branch out of the structured block. 12480 // longjmp() and throw() must not violate the entry/exit criteria. 12481 CS->getCapturedDecl()->setNothrow(); 12482 for (int ThisCaptureLevel = 12483 getOpenMPCaptureLevels(OMPD_target_teams_distribute); 12484 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12485 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12486 // 1.2.2 OpenMP Language Terminology 12487 // Structured block - An executable statement with a single entry at the 12488 // top and a single exit at the bottom. 12489 // The point of exit cannot be a branch out of the structured block. 12490 // longjmp() and throw() must not violate the entry/exit criteria. 12491 CS->getCapturedDecl()->setNothrow(); 12492 } 12493 12494 OMPLoopBasedDirective::HelperExprs B; 12495 // In presence of clause 'collapse' with number of loops, it will 12496 // define the nested loops number. 12497 unsigned NestedLoopCount = checkOpenMPLoop( 12498 OMPD_target_teams_distribute, getCollapseNumberExpr(Clauses), 12499 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 12500 VarsWithImplicitDSA, B); 12501 if (NestedLoopCount == 0) 12502 return StmtError(); 12503 12504 assert((CurContext->isDependentContext() || B.builtAll()) && 12505 "omp target teams distribute loop exprs were not built"); 12506 12507 setFunctionHasBranchProtectedScope(); 12508 return OMPTargetTeamsDistributeDirective::Create( 12509 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12510 } 12511 12512 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective( 12513 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12514 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12515 if (!AStmt) 12516 return StmtError(); 12517 12518 auto *CS = cast<CapturedStmt>(AStmt); 12519 // 1.2.2 OpenMP Language Terminology 12520 // Structured block - An executable statement with a single entry at the 12521 // top and a single exit at the bottom. 12522 // The point of exit cannot be a branch out of the structured block. 12523 // longjmp() and throw() must not violate the entry/exit criteria. 12524 CS->getCapturedDecl()->setNothrow(); 12525 for (int ThisCaptureLevel = 12526 getOpenMPCaptureLevels(OMPD_target_teams_distribute_parallel_for); 12527 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12528 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12529 // 1.2.2 OpenMP Language Terminology 12530 // Structured block - An executable statement with a single entry at the 12531 // top and a single exit at the bottom. 12532 // The point of exit cannot be a branch out of the structured block. 12533 // longjmp() and throw() must not violate the entry/exit criteria. 12534 CS->getCapturedDecl()->setNothrow(); 12535 } 12536 12537 OMPLoopBasedDirective::HelperExprs B; 12538 // In presence of clause 'collapse' with number of loops, it will 12539 // define the nested loops number. 12540 unsigned NestedLoopCount = checkOpenMPLoop( 12541 OMPD_target_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses), 12542 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 12543 VarsWithImplicitDSA, B); 12544 if (NestedLoopCount == 0) 12545 return StmtError(); 12546 12547 assert((CurContext->isDependentContext() || B.builtAll()) && 12548 "omp target teams distribute parallel for loop exprs were not built"); 12549 12550 if (!CurContext->isDependentContext()) { 12551 // Finalize the clauses that need pre-built expressions for CodeGen. 12552 for (OMPClause *C : Clauses) { 12553 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12554 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12555 B.NumIterations, *this, CurScope, 12556 DSAStack)) 12557 return StmtError(); 12558 } 12559 } 12560 12561 setFunctionHasBranchProtectedScope(); 12562 return OMPTargetTeamsDistributeParallelForDirective::Create( 12563 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 12564 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 12565 } 12566 12567 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective( 12568 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12569 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12570 if (!AStmt) 12571 return StmtError(); 12572 12573 auto *CS = cast<CapturedStmt>(AStmt); 12574 // 1.2.2 OpenMP Language Terminology 12575 // Structured block - An executable statement with a single entry at the 12576 // top and a single exit at the bottom. 12577 // The point of exit cannot be a branch out of the structured block. 12578 // longjmp() and throw() must not violate the entry/exit criteria. 12579 CS->getCapturedDecl()->setNothrow(); 12580 for (int ThisCaptureLevel = getOpenMPCaptureLevels( 12581 OMPD_target_teams_distribute_parallel_for_simd); 12582 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12583 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12584 // 1.2.2 OpenMP Language Terminology 12585 // Structured block - An executable statement with a single entry at the 12586 // top and a single exit at the bottom. 12587 // The point of exit cannot be a branch out of the structured block. 12588 // longjmp() and throw() must not violate the entry/exit criteria. 12589 CS->getCapturedDecl()->setNothrow(); 12590 } 12591 12592 OMPLoopBasedDirective::HelperExprs B; 12593 // In presence of clause 'collapse' with number of loops, it will 12594 // define the nested loops number. 12595 unsigned NestedLoopCount = 12596 checkOpenMPLoop(OMPD_target_teams_distribute_parallel_for_simd, 12597 getCollapseNumberExpr(Clauses), 12598 nullptr /*ordered not a clause on distribute*/, CS, *this, 12599 *DSAStack, VarsWithImplicitDSA, B); 12600 if (NestedLoopCount == 0) 12601 return StmtError(); 12602 12603 assert((CurContext->isDependentContext() || B.builtAll()) && 12604 "omp target teams distribute parallel for simd loop exprs were not " 12605 "built"); 12606 12607 if (!CurContext->isDependentContext()) { 12608 // Finalize the clauses that need pre-built expressions for CodeGen. 12609 for (OMPClause *C : Clauses) { 12610 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12611 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12612 B.NumIterations, *this, CurScope, 12613 DSAStack)) 12614 return StmtError(); 12615 } 12616 } 12617 12618 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12619 return StmtError(); 12620 12621 setFunctionHasBranchProtectedScope(); 12622 return OMPTargetTeamsDistributeParallelForSimdDirective::Create( 12623 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12624 } 12625 12626 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective( 12627 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12628 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12629 if (!AStmt) 12630 return StmtError(); 12631 12632 auto *CS = cast<CapturedStmt>(AStmt); 12633 // 1.2.2 OpenMP Language Terminology 12634 // Structured block - An executable statement with a single entry at the 12635 // top and a single exit at the bottom. 12636 // The point of exit cannot be a branch out of the structured block. 12637 // longjmp() and throw() must not violate the entry/exit criteria. 12638 CS->getCapturedDecl()->setNothrow(); 12639 for (int ThisCaptureLevel = 12640 getOpenMPCaptureLevels(OMPD_target_teams_distribute_simd); 12641 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12642 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12643 // 1.2.2 OpenMP Language Terminology 12644 // Structured block - An executable statement with a single entry at the 12645 // top and a single exit at the bottom. 12646 // The point of exit cannot be a branch out of the structured block. 12647 // longjmp() and throw() must not violate the entry/exit criteria. 12648 CS->getCapturedDecl()->setNothrow(); 12649 } 12650 12651 OMPLoopBasedDirective::HelperExprs B; 12652 // In presence of clause 'collapse' with number of loops, it will 12653 // define the nested loops number. 12654 unsigned NestedLoopCount = checkOpenMPLoop( 12655 OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses), 12656 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 12657 VarsWithImplicitDSA, B); 12658 if (NestedLoopCount == 0) 12659 return StmtError(); 12660 12661 assert((CurContext->isDependentContext() || B.builtAll()) && 12662 "omp target teams distribute simd loop exprs were not built"); 12663 12664 if (!CurContext->isDependentContext()) { 12665 // Finalize the clauses that need pre-built expressions for CodeGen. 12666 for (OMPClause *C : Clauses) { 12667 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12668 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12669 B.NumIterations, *this, CurScope, 12670 DSAStack)) 12671 return StmtError(); 12672 } 12673 } 12674 12675 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12676 return StmtError(); 12677 12678 setFunctionHasBranchProtectedScope(); 12679 return OMPTargetTeamsDistributeSimdDirective::Create( 12680 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12681 } 12682 12683 bool Sema::checkTransformableLoopNest( 12684 OpenMPDirectiveKind Kind, Stmt *AStmt, int NumLoops, 12685 SmallVectorImpl<OMPLoopBasedDirective::HelperExprs> &LoopHelpers, 12686 Stmt *&Body, 12687 SmallVectorImpl<SmallVector<llvm::PointerUnion<Stmt *, Decl *>, 0>> 12688 &OriginalInits) { 12689 OriginalInits.emplace_back(); 12690 bool Result = OMPLoopBasedDirective::doForAllLoops( 12691 AStmt->IgnoreContainers(), /*TryImperfectlyNestedLoops=*/false, NumLoops, 12692 [this, &LoopHelpers, &Body, &OriginalInits, Kind](unsigned Cnt, 12693 Stmt *CurStmt) { 12694 VarsWithInheritedDSAType TmpDSA; 12695 unsigned SingleNumLoops = 12696 checkOpenMPLoop(Kind, nullptr, nullptr, CurStmt, *this, *DSAStack, 12697 TmpDSA, LoopHelpers[Cnt]); 12698 if (SingleNumLoops == 0) 12699 return true; 12700 assert(SingleNumLoops == 1 && "Expect single loop iteration space"); 12701 if (auto *For = dyn_cast<ForStmt>(CurStmt)) { 12702 OriginalInits.back().push_back(For->getInit()); 12703 Body = For->getBody(); 12704 } else { 12705 assert(isa<CXXForRangeStmt>(CurStmt) && 12706 "Expected canonical for or range-based for loops."); 12707 auto *CXXFor = cast<CXXForRangeStmt>(CurStmt); 12708 OriginalInits.back().push_back(CXXFor->getBeginStmt()); 12709 Body = CXXFor->getBody(); 12710 } 12711 OriginalInits.emplace_back(); 12712 return false; 12713 }, 12714 [&OriginalInits](OMPLoopBasedDirective *Transform) { 12715 Stmt *DependentPreInits; 12716 if (auto *Dir = dyn_cast<OMPTileDirective>(Transform)) 12717 DependentPreInits = Dir->getPreInits(); 12718 else if (auto *Dir = dyn_cast<OMPUnrollDirective>(Transform)) 12719 DependentPreInits = Dir->getPreInits(); 12720 else 12721 llvm_unreachable("Unhandled loop transformation"); 12722 if (!DependentPreInits) 12723 return; 12724 for (Decl *C : cast<DeclStmt>(DependentPreInits)->getDeclGroup()) 12725 OriginalInits.back().push_back(C); 12726 }); 12727 assert(OriginalInits.back().empty() && "No preinit after innermost loop"); 12728 OriginalInits.pop_back(); 12729 return Result; 12730 } 12731 12732 StmtResult Sema::ActOnOpenMPTileDirective(ArrayRef<OMPClause *> Clauses, 12733 Stmt *AStmt, SourceLocation StartLoc, 12734 SourceLocation EndLoc) { 12735 auto SizesClauses = 12736 OMPExecutableDirective::getClausesOfKind<OMPSizesClause>(Clauses); 12737 if (SizesClauses.empty()) { 12738 // A missing 'sizes' clause is already reported by the parser. 12739 return StmtError(); 12740 } 12741 const OMPSizesClause *SizesClause = *SizesClauses.begin(); 12742 unsigned NumLoops = SizesClause->getNumSizes(); 12743 12744 // Empty statement should only be possible if there already was an error. 12745 if (!AStmt) 12746 return StmtError(); 12747 12748 // Verify and diagnose loop nest. 12749 SmallVector<OMPLoopBasedDirective::HelperExprs, 4> LoopHelpers(NumLoops); 12750 Stmt *Body = nullptr; 12751 SmallVector<SmallVector<llvm::PointerUnion<Stmt *, Decl *>, 0>, 4> 12752 OriginalInits; 12753 if (!checkTransformableLoopNest(OMPD_tile, AStmt, NumLoops, LoopHelpers, Body, 12754 OriginalInits)) 12755 return StmtError(); 12756 12757 // Delay tiling to when template is completely instantiated. 12758 if (CurContext->isDependentContext()) 12759 return OMPTileDirective::Create(Context, StartLoc, EndLoc, Clauses, 12760 NumLoops, AStmt, nullptr, nullptr); 12761 12762 SmallVector<Decl *, 4> PreInits; 12763 12764 // Create iteration variables for the generated loops. 12765 SmallVector<VarDecl *, 4> FloorIndVars; 12766 SmallVector<VarDecl *, 4> TileIndVars; 12767 FloorIndVars.resize(NumLoops); 12768 TileIndVars.resize(NumLoops); 12769 for (unsigned I = 0; I < NumLoops; ++I) { 12770 OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers[I]; 12771 12772 assert(LoopHelper.Counters.size() == 1 && 12773 "Expect single-dimensional loop iteration space"); 12774 auto *OrigCntVar = cast<DeclRefExpr>(LoopHelper.Counters.front()); 12775 std::string OrigVarName = OrigCntVar->getNameInfo().getAsString(); 12776 DeclRefExpr *IterVarRef = cast<DeclRefExpr>(LoopHelper.IterationVarRef); 12777 QualType CntTy = IterVarRef->getType(); 12778 12779 // Iteration variable for the floor (i.e. outer) loop. 12780 { 12781 std::string FloorCntName = 12782 (Twine(".floor_") + llvm::utostr(I) + ".iv." + OrigVarName).str(); 12783 VarDecl *FloorCntDecl = 12784 buildVarDecl(*this, {}, CntTy, FloorCntName, nullptr, OrigCntVar); 12785 FloorIndVars[I] = FloorCntDecl; 12786 } 12787 12788 // Iteration variable for the tile (i.e. inner) loop. 12789 { 12790 std::string TileCntName = 12791 (Twine(".tile_") + llvm::utostr(I) + ".iv." + OrigVarName).str(); 12792 12793 // Reuse the iteration variable created by checkOpenMPLoop. It is also 12794 // used by the expressions to derive the original iteration variable's 12795 // value from the logical iteration number. 12796 auto *TileCntDecl = cast<VarDecl>(IterVarRef->getDecl()); 12797 TileCntDecl->setDeclName(&PP.getIdentifierTable().get(TileCntName)); 12798 TileIndVars[I] = TileCntDecl; 12799 } 12800 for (auto &P : OriginalInits[I]) { 12801 if (auto *D = P.dyn_cast<Decl *>()) 12802 PreInits.push_back(D); 12803 else if (auto *PI = dyn_cast_or_null<DeclStmt>(P.dyn_cast<Stmt *>())) 12804 PreInits.append(PI->decl_begin(), PI->decl_end()); 12805 } 12806 if (auto *PI = cast_or_null<DeclStmt>(LoopHelper.PreInits)) 12807 PreInits.append(PI->decl_begin(), PI->decl_end()); 12808 // Gather declarations for the data members used as counters. 12809 for (Expr *CounterRef : LoopHelper.Counters) { 12810 auto *CounterDecl = cast<DeclRefExpr>(CounterRef)->getDecl(); 12811 if (isa<OMPCapturedExprDecl>(CounterDecl)) 12812 PreInits.push_back(CounterDecl); 12813 } 12814 } 12815 12816 // Once the original iteration values are set, append the innermost body. 12817 Stmt *Inner = Body; 12818 12819 // Create tile loops from the inside to the outside. 12820 for (int I = NumLoops - 1; I >= 0; --I) { 12821 OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers[I]; 12822 Expr *NumIterations = LoopHelper.NumIterations; 12823 auto *OrigCntVar = cast<DeclRefExpr>(LoopHelper.Counters[0]); 12824 QualType CntTy = OrigCntVar->getType(); 12825 Expr *DimTileSize = SizesClause->getSizesRefs()[I]; 12826 Scope *CurScope = getCurScope(); 12827 12828 // Commonly used variables. 12829 DeclRefExpr *TileIV = buildDeclRefExpr(*this, TileIndVars[I], CntTy, 12830 OrigCntVar->getExprLoc()); 12831 DeclRefExpr *FloorIV = buildDeclRefExpr(*this, FloorIndVars[I], CntTy, 12832 OrigCntVar->getExprLoc()); 12833 12834 // For init-statement: auto .tile.iv = .floor.iv 12835 AddInitializerToDecl(TileIndVars[I], DefaultLvalueConversion(FloorIV).get(), 12836 /*DirectInit=*/false); 12837 Decl *CounterDecl = TileIndVars[I]; 12838 StmtResult InitStmt = new (Context) 12839 DeclStmt(DeclGroupRef::Create(Context, &CounterDecl, 1), 12840 OrigCntVar->getBeginLoc(), OrigCntVar->getEndLoc()); 12841 if (!InitStmt.isUsable()) 12842 return StmtError(); 12843 12844 // For cond-expression: .tile.iv < min(.floor.iv + DimTileSize, 12845 // NumIterations) 12846 ExprResult EndOfTile = BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), 12847 BO_Add, FloorIV, DimTileSize); 12848 if (!EndOfTile.isUsable()) 12849 return StmtError(); 12850 ExprResult IsPartialTile = 12851 BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), BO_LT, 12852 NumIterations, EndOfTile.get()); 12853 if (!IsPartialTile.isUsable()) 12854 return StmtError(); 12855 ExprResult MinTileAndIterSpace = ActOnConditionalOp( 12856 LoopHelper.Cond->getBeginLoc(), LoopHelper.Cond->getEndLoc(), 12857 IsPartialTile.get(), NumIterations, EndOfTile.get()); 12858 if (!MinTileAndIterSpace.isUsable()) 12859 return StmtError(); 12860 ExprResult CondExpr = BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), 12861 BO_LT, TileIV, MinTileAndIterSpace.get()); 12862 if (!CondExpr.isUsable()) 12863 return StmtError(); 12864 12865 // For incr-statement: ++.tile.iv 12866 ExprResult IncrStmt = 12867 BuildUnaryOp(CurScope, LoopHelper.Inc->getExprLoc(), UO_PreInc, TileIV); 12868 if (!IncrStmt.isUsable()) 12869 return StmtError(); 12870 12871 // Statements to set the original iteration variable's value from the 12872 // logical iteration number. 12873 // Generated for loop is: 12874 // Original_for_init; 12875 // for (auto .tile.iv = .floor.iv; .tile.iv < min(.floor.iv + DimTileSize, 12876 // NumIterations); ++.tile.iv) { 12877 // Original_Body; 12878 // Original_counter_update; 12879 // } 12880 // FIXME: If the innermost body is an loop itself, inserting these 12881 // statements stops it being recognized as a perfectly nested loop (e.g. 12882 // for applying tiling again). If this is the case, sink the expressions 12883 // further into the inner loop. 12884 SmallVector<Stmt *, 4> BodyParts; 12885 BodyParts.append(LoopHelper.Updates.begin(), LoopHelper.Updates.end()); 12886 BodyParts.push_back(Inner); 12887 Inner = CompoundStmt::Create(Context, BodyParts, Inner->getBeginLoc(), 12888 Inner->getEndLoc()); 12889 Inner = new (Context) 12890 ForStmt(Context, InitStmt.get(), CondExpr.get(), nullptr, 12891 IncrStmt.get(), Inner, LoopHelper.Init->getBeginLoc(), 12892 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc()); 12893 } 12894 12895 // Create floor loops from the inside to the outside. 12896 for (int I = NumLoops - 1; I >= 0; --I) { 12897 auto &LoopHelper = LoopHelpers[I]; 12898 Expr *NumIterations = LoopHelper.NumIterations; 12899 DeclRefExpr *OrigCntVar = cast<DeclRefExpr>(LoopHelper.Counters[0]); 12900 QualType CntTy = OrigCntVar->getType(); 12901 Expr *DimTileSize = SizesClause->getSizesRefs()[I]; 12902 Scope *CurScope = getCurScope(); 12903 12904 // Commonly used variables. 12905 DeclRefExpr *FloorIV = buildDeclRefExpr(*this, FloorIndVars[I], CntTy, 12906 OrigCntVar->getExprLoc()); 12907 12908 // For init-statement: auto .floor.iv = 0 12909 AddInitializerToDecl( 12910 FloorIndVars[I], 12911 ActOnIntegerConstant(LoopHelper.Init->getExprLoc(), 0).get(), 12912 /*DirectInit=*/false); 12913 Decl *CounterDecl = FloorIndVars[I]; 12914 StmtResult InitStmt = new (Context) 12915 DeclStmt(DeclGroupRef::Create(Context, &CounterDecl, 1), 12916 OrigCntVar->getBeginLoc(), OrigCntVar->getEndLoc()); 12917 if (!InitStmt.isUsable()) 12918 return StmtError(); 12919 12920 // For cond-expression: .floor.iv < NumIterations 12921 ExprResult CondExpr = BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), 12922 BO_LT, FloorIV, NumIterations); 12923 if (!CondExpr.isUsable()) 12924 return StmtError(); 12925 12926 // For incr-statement: .floor.iv += DimTileSize 12927 ExprResult IncrStmt = BuildBinOp(CurScope, LoopHelper.Inc->getExprLoc(), 12928 BO_AddAssign, FloorIV, DimTileSize); 12929 if (!IncrStmt.isUsable()) 12930 return StmtError(); 12931 12932 Inner = new (Context) 12933 ForStmt(Context, InitStmt.get(), CondExpr.get(), nullptr, 12934 IncrStmt.get(), Inner, LoopHelper.Init->getBeginLoc(), 12935 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc()); 12936 } 12937 12938 return OMPTileDirective::Create(Context, StartLoc, EndLoc, Clauses, NumLoops, 12939 AStmt, Inner, 12940 buildPreInits(Context, PreInits)); 12941 } 12942 12943 StmtResult Sema::ActOnOpenMPUnrollDirective(ArrayRef<OMPClause *> Clauses, 12944 Stmt *AStmt, 12945 SourceLocation StartLoc, 12946 SourceLocation EndLoc) { 12947 // Empty statement should only be possible if there already was an error. 12948 if (!AStmt) 12949 return StmtError(); 12950 12951 if (checkMutuallyExclusiveClauses(*this, Clauses, {OMPC_partial, OMPC_full})) 12952 return StmtError(); 12953 12954 const OMPFullClause *FullClause = 12955 OMPExecutableDirective::getSingleClause<OMPFullClause>(Clauses); 12956 const OMPPartialClause *PartialClause = 12957 OMPExecutableDirective::getSingleClause<OMPPartialClause>(Clauses); 12958 assert(!(FullClause && PartialClause) && 12959 "mutual exclusivity must have been checked before"); 12960 12961 constexpr unsigned NumLoops = 1; 12962 Stmt *Body = nullptr; 12963 SmallVector<OMPLoopBasedDirective::HelperExprs, NumLoops> LoopHelpers( 12964 NumLoops); 12965 SmallVector<SmallVector<llvm::PointerUnion<Stmt *, Decl *>, 0>, NumLoops + 1> 12966 OriginalInits; 12967 if (!checkTransformableLoopNest(OMPD_unroll, AStmt, NumLoops, LoopHelpers, 12968 Body, OriginalInits)) 12969 return StmtError(); 12970 12971 unsigned NumGeneratedLoops = PartialClause ? 1 : 0; 12972 12973 // Delay unrolling to when template is completely instantiated. 12974 if (CurContext->isDependentContext()) 12975 return OMPUnrollDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 12976 NumGeneratedLoops, nullptr, nullptr); 12977 12978 OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers.front(); 12979 12980 if (FullClause) { 12981 if (!VerifyPositiveIntegerConstantInClause( 12982 LoopHelper.NumIterations, OMPC_full, /*StrictlyPositive=*/false, 12983 /*SuppressExprDigs=*/true) 12984 .isUsable()) { 12985 Diag(AStmt->getBeginLoc(), diag::err_omp_unroll_full_variable_trip_count); 12986 Diag(FullClause->getBeginLoc(), diag::note_omp_directive_here) 12987 << "#pragma omp unroll full"; 12988 return StmtError(); 12989 } 12990 } 12991 12992 // The generated loop may only be passed to other loop-associated directive 12993 // when a partial clause is specified. Without the requirement it is 12994 // sufficient to generate loop unroll metadata at code-generation. 12995 if (NumGeneratedLoops == 0) 12996 return OMPUnrollDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 12997 NumGeneratedLoops, nullptr, nullptr); 12998 12999 // Otherwise, we need to provide a de-sugared/transformed AST that can be 13000 // associated with another loop directive. 13001 // 13002 // The canonical loop analysis return by checkTransformableLoopNest assumes 13003 // the following structure to be the same loop without transformations or 13004 // directives applied: \code OriginalInits; LoopHelper.PreInits; 13005 // LoopHelper.Counters; 13006 // for (; IV < LoopHelper.NumIterations; ++IV) { 13007 // LoopHelper.Updates; 13008 // Body; 13009 // } 13010 // \endcode 13011 // where IV is a variable declared and initialized to 0 in LoopHelper.PreInits 13012 // and referenced by LoopHelper.IterationVarRef. 13013 // 13014 // The unrolling directive transforms this into the following loop: 13015 // \code 13016 // OriginalInits; \ 13017 // LoopHelper.PreInits; > NewPreInits 13018 // LoopHelper.Counters; / 13019 // for (auto UIV = 0; UIV < LoopHelper.NumIterations; UIV+=Factor) { 13020 // #pragma clang loop unroll_count(Factor) 13021 // for (IV = UIV; IV < UIV + Factor && UIV < LoopHelper.NumIterations; ++IV) 13022 // { 13023 // LoopHelper.Updates; 13024 // Body; 13025 // } 13026 // } 13027 // \endcode 13028 // where UIV is a new logical iteration counter. IV must be the same VarDecl 13029 // as the original LoopHelper.IterationVarRef because LoopHelper.Updates 13030 // references it. If the partially unrolled loop is associated with another 13031 // loop directive (like an OMPForDirective), it will use checkOpenMPLoop to 13032 // analyze this loop, i.e. the outer loop must fulfill the constraints of an 13033 // OpenMP canonical loop. The inner loop is not an associable canonical loop 13034 // and only exists to defer its unrolling to LLVM's LoopUnroll instead of 13035 // doing it in the frontend (by adding loop metadata). NewPreInits becomes a 13036 // property of the OMPLoopBasedDirective instead of statements in 13037 // CompoundStatement. This is to allow the loop to become a non-outermost loop 13038 // of a canonical loop nest where these PreInits are emitted before the 13039 // outermost directive. 13040 13041 // Determine the PreInit declarations. 13042 SmallVector<Decl *, 4> PreInits; 13043 assert(OriginalInits.size() == 1 && 13044 "Expecting a single-dimensional loop iteration space"); 13045 for (auto &P : OriginalInits[0]) { 13046 if (auto *D = P.dyn_cast<Decl *>()) 13047 PreInits.push_back(D); 13048 else if (auto *PI = dyn_cast_or_null<DeclStmt>(P.dyn_cast<Stmt *>())) 13049 PreInits.append(PI->decl_begin(), PI->decl_end()); 13050 } 13051 if (auto *PI = cast_or_null<DeclStmt>(LoopHelper.PreInits)) 13052 PreInits.append(PI->decl_begin(), PI->decl_end()); 13053 // Gather declarations for the data members used as counters. 13054 for (Expr *CounterRef : LoopHelper.Counters) { 13055 auto *CounterDecl = cast<DeclRefExpr>(CounterRef)->getDecl(); 13056 if (isa<OMPCapturedExprDecl>(CounterDecl)) 13057 PreInits.push_back(CounterDecl); 13058 } 13059 13060 auto *IterationVarRef = cast<DeclRefExpr>(LoopHelper.IterationVarRef); 13061 QualType IVTy = IterationVarRef->getType(); 13062 assert(LoopHelper.Counters.size() == 1 && 13063 "Expecting a single-dimensional loop iteration space"); 13064 auto *OrigVar = cast<DeclRefExpr>(LoopHelper.Counters.front()); 13065 13066 // Determine the unroll factor. 13067 uint64_t Factor; 13068 SourceLocation FactorLoc; 13069 if (Expr *FactorVal = PartialClause->getFactor()) { 13070 Factor = 13071 FactorVal->getIntegerConstantExpr(Context).getValue().getZExtValue(); 13072 FactorLoc = FactorVal->getExprLoc(); 13073 } else { 13074 // TODO: Use a better profitability model. 13075 Factor = 2; 13076 } 13077 assert(Factor > 0 && "Expected positive unroll factor"); 13078 auto MakeFactorExpr = [this, Factor, IVTy, FactorLoc]() { 13079 return IntegerLiteral::Create( 13080 Context, llvm::APInt(Context.getIntWidth(IVTy), Factor), IVTy, 13081 FactorLoc); 13082 }; 13083 13084 // Iteration variable SourceLocations. 13085 SourceLocation OrigVarLoc = OrigVar->getExprLoc(); 13086 SourceLocation OrigVarLocBegin = OrigVar->getBeginLoc(); 13087 SourceLocation OrigVarLocEnd = OrigVar->getEndLoc(); 13088 13089 // Internal variable names. 13090 std::string OrigVarName = OrigVar->getNameInfo().getAsString(); 13091 std::string OuterIVName = (Twine(".unrolled.iv.") + OrigVarName).str(); 13092 std::string InnerIVName = (Twine(".unroll_inner.iv.") + OrigVarName).str(); 13093 std::string InnerTripCountName = 13094 (Twine(".unroll_inner.tripcount.") + OrigVarName).str(); 13095 13096 // Create the iteration variable for the unrolled loop. 13097 VarDecl *OuterIVDecl = 13098 buildVarDecl(*this, {}, IVTy, OuterIVName, nullptr, OrigVar); 13099 auto MakeOuterRef = [this, OuterIVDecl, IVTy, OrigVarLoc]() { 13100 return buildDeclRefExpr(*this, OuterIVDecl, IVTy, OrigVarLoc); 13101 }; 13102 13103 // Iteration variable for the inner loop: Reuse the iteration variable created 13104 // by checkOpenMPLoop. 13105 auto *InnerIVDecl = cast<VarDecl>(IterationVarRef->getDecl()); 13106 InnerIVDecl->setDeclName(&PP.getIdentifierTable().get(InnerIVName)); 13107 auto MakeInnerRef = [this, InnerIVDecl, IVTy, OrigVarLoc]() { 13108 return buildDeclRefExpr(*this, InnerIVDecl, IVTy, OrigVarLoc); 13109 }; 13110 13111 // Make a copy of the NumIterations expression for each use: By the AST 13112 // constraints, every expression object in a DeclContext must be unique. 13113 CaptureVars CopyTransformer(*this); 13114 auto MakeNumIterations = [&CopyTransformer, &LoopHelper]() -> Expr * { 13115 return AssertSuccess( 13116 CopyTransformer.TransformExpr(LoopHelper.NumIterations)); 13117 }; 13118 13119 // Inner For init-statement: auto .unroll_inner.iv = .unrolled.iv 13120 ExprResult LValueConv = DefaultLvalueConversion(MakeOuterRef()); 13121 AddInitializerToDecl(InnerIVDecl, LValueConv.get(), /*DirectInit=*/false); 13122 StmtResult InnerInit = new (Context) 13123 DeclStmt(DeclGroupRef(InnerIVDecl), OrigVarLocBegin, OrigVarLocEnd); 13124 if (!InnerInit.isUsable()) 13125 return StmtError(); 13126 13127 // Inner For cond-expression: 13128 // \code 13129 // .unroll_inner.iv < .unrolled.iv + Factor && 13130 // .unroll_inner.iv < NumIterations 13131 // \endcode 13132 // This conjunction of two conditions allows ScalarEvolution to derive the 13133 // maximum trip count of the inner loop. 13134 ExprResult EndOfTile = BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), 13135 BO_Add, MakeOuterRef(), MakeFactorExpr()); 13136 if (!EndOfTile.isUsable()) 13137 return StmtError(); 13138 ExprResult InnerCond1 = BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), 13139 BO_LE, MakeInnerRef(), EndOfTile.get()); 13140 if (!InnerCond1.isUsable()) 13141 return StmtError(); 13142 ExprResult InnerCond2 = 13143 BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), BO_LE, MakeInnerRef(), 13144 MakeNumIterations()); 13145 if (!InnerCond2.isUsable()) 13146 return StmtError(); 13147 ExprResult InnerCond = 13148 BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), BO_LAnd, 13149 InnerCond1.get(), InnerCond2.get()); 13150 if (!InnerCond.isUsable()) 13151 return StmtError(); 13152 13153 // Inner For incr-statement: ++.unroll_inner.iv 13154 ExprResult InnerIncr = BuildUnaryOp(CurScope, LoopHelper.Inc->getExprLoc(), 13155 UO_PreInc, MakeInnerRef()); 13156 if (!InnerIncr.isUsable()) 13157 return StmtError(); 13158 13159 // Inner For statement. 13160 SmallVector<Stmt *> InnerBodyStmts; 13161 InnerBodyStmts.append(LoopHelper.Updates.begin(), LoopHelper.Updates.end()); 13162 InnerBodyStmts.push_back(Body); 13163 CompoundStmt *InnerBody = CompoundStmt::Create( 13164 Context, InnerBodyStmts, Body->getBeginLoc(), Body->getEndLoc()); 13165 ForStmt *InnerFor = new (Context) 13166 ForStmt(Context, InnerInit.get(), InnerCond.get(), nullptr, 13167 InnerIncr.get(), InnerBody, LoopHelper.Init->getBeginLoc(), 13168 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc()); 13169 13170 // Unroll metadata for the inner loop. 13171 // This needs to take into account the remainder portion of the unrolled loop, 13172 // hence `unroll(full)` does not apply here, even though the LoopUnroll pass 13173 // supports multiple loop exits. Instead, unroll using a factor equivalent to 13174 // the maximum trip count, which will also generate a remainder loop. Just 13175 // `unroll(enable)` (which could have been useful if the user has not 13176 // specified a concrete factor; even though the outer loop cannot be 13177 // influenced anymore, would avoid more code bloat than necessary) will refuse 13178 // the loop because "Won't unroll; remainder loop could not be generated when 13179 // assuming runtime trip count". Even if it did work, it must not choose a 13180 // larger unroll factor than the maximum loop length, or it would always just 13181 // execute the remainder loop. 13182 LoopHintAttr *UnrollHintAttr = 13183 LoopHintAttr::CreateImplicit(Context, LoopHintAttr::UnrollCount, 13184 LoopHintAttr::Numeric, MakeFactorExpr()); 13185 AttributedStmt *InnerUnrolled = 13186 AttributedStmt::Create(Context, StartLoc, {UnrollHintAttr}, InnerFor); 13187 13188 // Outer For init-statement: auto .unrolled.iv = 0 13189 AddInitializerToDecl( 13190 OuterIVDecl, ActOnIntegerConstant(LoopHelper.Init->getExprLoc(), 0).get(), 13191 /*DirectInit=*/false); 13192 StmtResult OuterInit = new (Context) 13193 DeclStmt(DeclGroupRef(OuterIVDecl), OrigVarLocBegin, OrigVarLocEnd); 13194 if (!OuterInit.isUsable()) 13195 return StmtError(); 13196 13197 // Outer For cond-expression: .unrolled.iv < NumIterations 13198 ExprResult OuterConde = 13199 BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), BO_LT, MakeOuterRef(), 13200 MakeNumIterations()); 13201 if (!OuterConde.isUsable()) 13202 return StmtError(); 13203 13204 // Outer For incr-statement: .unrolled.iv += Factor 13205 ExprResult OuterIncr = 13206 BuildBinOp(CurScope, LoopHelper.Inc->getExprLoc(), BO_AddAssign, 13207 MakeOuterRef(), MakeFactorExpr()); 13208 if (!OuterIncr.isUsable()) 13209 return StmtError(); 13210 13211 // Outer For statement. 13212 ForStmt *OuterFor = new (Context) 13213 ForStmt(Context, OuterInit.get(), OuterConde.get(), nullptr, 13214 OuterIncr.get(), InnerUnrolled, LoopHelper.Init->getBeginLoc(), 13215 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc()); 13216 13217 return OMPUnrollDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 13218 NumGeneratedLoops, OuterFor, 13219 buildPreInits(Context, PreInits)); 13220 } 13221 13222 OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr, 13223 SourceLocation StartLoc, 13224 SourceLocation LParenLoc, 13225 SourceLocation EndLoc) { 13226 OMPClause *Res = nullptr; 13227 switch (Kind) { 13228 case OMPC_final: 13229 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc); 13230 break; 13231 case OMPC_num_threads: 13232 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc); 13233 break; 13234 case OMPC_safelen: 13235 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc); 13236 break; 13237 case OMPC_simdlen: 13238 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc); 13239 break; 13240 case OMPC_allocator: 13241 Res = ActOnOpenMPAllocatorClause(Expr, StartLoc, LParenLoc, EndLoc); 13242 break; 13243 case OMPC_collapse: 13244 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc); 13245 break; 13246 case OMPC_ordered: 13247 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr); 13248 break; 13249 case OMPC_num_teams: 13250 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc); 13251 break; 13252 case OMPC_thread_limit: 13253 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc); 13254 break; 13255 case OMPC_priority: 13256 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc); 13257 break; 13258 case OMPC_grainsize: 13259 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc); 13260 break; 13261 case OMPC_num_tasks: 13262 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc); 13263 break; 13264 case OMPC_hint: 13265 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc); 13266 break; 13267 case OMPC_depobj: 13268 Res = ActOnOpenMPDepobjClause(Expr, StartLoc, LParenLoc, EndLoc); 13269 break; 13270 case OMPC_detach: 13271 Res = ActOnOpenMPDetachClause(Expr, StartLoc, LParenLoc, EndLoc); 13272 break; 13273 case OMPC_novariants: 13274 Res = ActOnOpenMPNovariantsClause(Expr, StartLoc, LParenLoc, EndLoc); 13275 break; 13276 case OMPC_nocontext: 13277 Res = ActOnOpenMPNocontextClause(Expr, StartLoc, LParenLoc, EndLoc); 13278 break; 13279 case OMPC_filter: 13280 Res = ActOnOpenMPFilterClause(Expr, StartLoc, LParenLoc, EndLoc); 13281 break; 13282 case OMPC_partial: 13283 Res = ActOnOpenMPPartialClause(Expr, StartLoc, LParenLoc, EndLoc); 13284 break; 13285 case OMPC_device: 13286 case OMPC_if: 13287 case OMPC_default: 13288 case OMPC_proc_bind: 13289 case OMPC_schedule: 13290 case OMPC_private: 13291 case OMPC_firstprivate: 13292 case OMPC_lastprivate: 13293 case OMPC_shared: 13294 case OMPC_reduction: 13295 case OMPC_task_reduction: 13296 case OMPC_in_reduction: 13297 case OMPC_linear: 13298 case OMPC_aligned: 13299 case OMPC_copyin: 13300 case OMPC_copyprivate: 13301 case OMPC_nowait: 13302 case OMPC_untied: 13303 case OMPC_mergeable: 13304 case OMPC_threadprivate: 13305 case OMPC_sizes: 13306 case OMPC_allocate: 13307 case OMPC_flush: 13308 case OMPC_read: 13309 case OMPC_write: 13310 case OMPC_update: 13311 case OMPC_capture: 13312 case OMPC_seq_cst: 13313 case OMPC_acq_rel: 13314 case OMPC_acquire: 13315 case OMPC_release: 13316 case OMPC_relaxed: 13317 case OMPC_depend: 13318 case OMPC_threads: 13319 case OMPC_simd: 13320 case OMPC_map: 13321 case OMPC_nogroup: 13322 case OMPC_dist_schedule: 13323 case OMPC_defaultmap: 13324 case OMPC_unknown: 13325 case OMPC_uniform: 13326 case OMPC_to: 13327 case OMPC_from: 13328 case OMPC_use_device_ptr: 13329 case OMPC_use_device_addr: 13330 case OMPC_is_device_ptr: 13331 case OMPC_unified_address: 13332 case OMPC_unified_shared_memory: 13333 case OMPC_reverse_offload: 13334 case OMPC_dynamic_allocators: 13335 case OMPC_atomic_default_mem_order: 13336 case OMPC_device_type: 13337 case OMPC_match: 13338 case OMPC_nontemporal: 13339 case OMPC_order: 13340 case OMPC_destroy: 13341 case OMPC_inclusive: 13342 case OMPC_exclusive: 13343 case OMPC_uses_allocators: 13344 case OMPC_affinity: 13345 case OMPC_when: 13346 default: 13347 llvm_unreachable("Clause is not allowed."); 13348 } 13349 return Res; 13350 } 13351 13352 // An OpenMP directive such as 'target parallel' has two captured regions: 13353 // for the 'target' and 'parallel' respectively. This function returns 13354 // the region in which to capture expressions associated with a clause. 13355 // A return value of OMPD_unknown signifies that the expression should not 13356 // be captured. 13357 static OpenMPDirectiveKind getOpenMPCaptureRegionForClause( 13358 OpenMPDirectiveKind DKind, OpenMPClauseKind CKind, unsigned OpenMPVersion, 13359 OpenMPDirectiveKind NameModifier = OMPD_unknown) { 13360 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 13361 switch (CKind) { 13362 case OMPC_if: 13363 switch (DKind) { 13364 case OMPD_target_parallel_for_simd: 13365 if (OpenMPVersion >= 50 && 13366 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) { 13367 CaptureRegion = OMPD_parallel; 13368 break; 13369 } 13370 LLVM_FALLTHROUGH; 13371 case OMPD_target_parallel: 13372 case OMPD_target_parallel_for: 13373 // If this clause applies to the nested 'parallel' region, capture within 13374 // the 'target' region, otherwise do not capture. 13375 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel) 13376 CaptureRegion = OMPD_target; 13377 break; 13378 case OMPD_target_teams_distribute_parallel_for_simd: 13379 if (OpenMPVersion >= 50 && 13380 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) { 13381 CaptureRegion = OMPD_parallel; 13382 break; 13383 } 13384 LLVM_FALLTHROUGH; 13385 case OMPD_target_teams_distribute_parallel_for: 13386 // If this clause applies to the nested 'parallel' region, capture within 13387 // the 'teams' region, otherwise do not capture. 13388 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel) 13389 CaptureRegion = OMPD_teams; 13390 break; 13391 case OMPD_teams_distribute_parallel_for_simd: 13392 if (OpenMPVersion >= 50 && 13393 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) { 13394 CaptureRegion = OMPD_parallel; 13395 break; 13396 } 13397 LLVM_FALLTHROUGH; 13398 case OMPD_teams_distribute_parallel_for: 13399 CaptureRegion = OMPD_teams; 13400 break; 13401 case OMPD_target_update: 13402 case OMPD_target_enter_data: 13403 case OMPD_target_exit_data: 13404 CaptureRegion = OMPD_task; 13405 break; 13406 case OMPD_parallel_master_taskloop: 13407 if (NameModifier == OMPD_unknown || NameModifier == OMPD_taskloop) 13408 CaptureRegion = OMPD_parallel; 13409 break; 13410 case OMPD_parallel_master_taskloop_simd: 13411 if ((OpenMPVersion <= 45 && NameModifier == OMPD_unknown) || 13412 NameModifier == OMPD_taskloop) { 13413 CaptureRegion = OMPD_parallel; 13414 break; 13415 } 13416 if (OpenMPVersion <= 45) 13417 break; 13418 if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd) 13419 CaptureRegion = OMPD_taskloop; 13420 break; 13421 case OMPD_parallel_for_simd: 13422 if (OpenMPVersion <= 45) 13423 break; 13424 if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd) 13425 CaptureRegion = OMPD_parallel; 13426 break; 13427 case OMPD_taskloop_simd: 13428 case OMPD_master_taskloop_simd: 13429 if (OpenMPVersion <= 45) 13430 break; 13431 if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd) 13432 CaptureRegion = OMPD_taskloop; 13433 break; 13434 case OMPD_distribute_parallel_for_simd: 13435 if (OpenMPVersion <= 45) 13436 break; 13437 if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd) 13438 CaptureRegion = OMPD_parallel; 13439 break; 13440 case OMPD_target_simd: 13441 if (OpenMPVersion >= 50 && 13442 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) 13443 CaptureRegion = OMPD_target; 13444 break; 13445 case OMPD_teams_distribute_simd: 13446 case OMPD_target_teams_distribute_simd: 13447 if (OpenMPVersion >= 50 && 13448 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) 13449 CaptureRegion = OMPD_teams; 13450 break; 13451 case OMPD_cancel: 13452 case OMPD_parallel: 13453 case OMPD_parallel_master: 13454 case OMPD_parallel_sections: 13455 case OMPD_parallel_for: 13456 case OMPD_target: 13457 case OMPD_target_teams: 13458 case OMPD_target_teams_distribute: 13459 case OMPD_distribute_parallel_for: 13460 case OMPD_task: 13461 case OMPD_taskloop: 13462 case OMPD_master_taskloop: 13463 case OMPD_target_data: 13464 case OMPD_simd: 13465 case OMPD_for_simd: 13466 case OMPD_distribute_simd: 13467 // Do not capture if-clause expressions. 13468 break; 13469 case OMPD_threadprivate: 13470 case OMPD_allocate: 13471 case OMPD_taskyield: 13472 case OMPD_barrier: 13473 case OMPD_taskwait: 13474 case OMPD_cancellation_point: 13475 case OMPD_flush: 13476 case OMPD_depobj: 13477 case OMPD_scan: 13478 case OMPD_declare_reduction: 13479 case OMPD_declare_mapper: 13480 case OMPD_declare_simd: 13481 case OMPD_declare_variant: 13482 case OMPD_begin_declare_variant: 13483 case OMPD_end_declare_variant: 13484 case OMPD_declare_target: 13485 case OMPD_end_declare_target: 13486 case OMPD_teams: 13487 case OMPD_tile: 13488 case OMPD_unroll: 13489 case OMPD_for: 13490 case OMPD_sections: 13491 case OMPD_section: 13492 case OMPD_single: 13493 case OMPD_master: 13494 case OMPD_masked: 13495 case OMPD_critical: 13496 case OMPD_taskgroup: 13497 case OMPD_distribute: 13498 case OMPD_ordered: 13499 case OMPD_atomic: 13500 case OMPD_teams_distribute: 13501 case OMPD_requires: 13502 case OMPD_metadirective: 13503 llvm_unreachable("Unexpected OpenMP directive with if-clause"); 13504 case OMPD_unknown: 13505 default: 13506 llvm_unreachable("Unknown OpenMP directive"); 13507 } 13508 break; 13509 case OMPC_num_threads: 13510 switch (DKind) { 13511 case OMPD_target_parallel: 13512 case OMPD_target_parallel_for: 13513 case OMPD_target_parallel_for_simd: 13514 CaptureRegion = OMPD_target; 13515 break; 13516 case OMPD_teams_distribute_parallel_for: 13517 case OMPD_teams_distribute_parallel_for_simd: 13518 case OMPD_target_teams_distribute_parallel_for: 13519 case OMPD_target_teams_distribute_parallel_for_simd: 13520 CaptureRegion = OMPD_teams; 13521 break; 13522 case OMPD_parallel: 13523 case OMPD_parallel_master: 13524 case OMPD_parallel_sections: 13525 case OMPD_parallel_for: 13526 case OMPD_parallel_for_simd: 13527 case OMPD_distribute_parallel_for: 13528 case OMPD_distribute_parallel_for_simd: 13529 case OMPD_parallel_master_taskloop: 13530 case OMPD_parallel_master_taskloop_simd: 13531 // Do not capture num_threads-clause expressions. 13532 break; 13533 case OMPD_target_data: 13534 case OMPD_target_enter_data: 13535 case OMPD_target_exit_data: 13536 case OMPD_target_update: 13537 case OMPD_target: 13538 case OMPD_target_simd: 13539 case OMPD_target_teams: 13540 case OMPD_target_teams_distribute: 13541 case OMPD_target_teams_distribute_simd: 13542 case OMPD_cancel: 13543 case OMPD_task: 13544 case OMPD_taskloop: 13545 case OMPD_taskloop_simd: 13546 case OMPD_master_taskloop: 13547 case OMPD_master_taskloop_simd: 13548 case OMPD_threadprivate: 13549 case OMPD_allocate: 13550 case OMPD_taskyield: 13551 case OMPD_barrier: 13552 case OMPD_taskwait: 13553 case OMPD_cancellation_point: 13554 case OMPD_flush: 13555 case OMPD_depobj: 13556 case OMPD_scan: 13557 case OMPD_declare_reduction: 13558 case OMPD_declare_mapper: 13559 case OMPD_declare_simd: 13560 case OMPD_declare_variant: 13561 case OMPD_begin_declare_variant: 13562 case OMPD_end_declare_variant: 13563 case OMPD_declare_target: 13564 case OMPD_end_declare_target: 13565 case OMPD_teams: 13566 case OMPD_simd: 13567 case OMPD_tile: 13568 case OMPD_unroll: 13569 case OMPD_for: 13570 case OMPD_for_simd: 13571 case OMPD_sections: 13572 case OMPD_section: 13573 case OMPD_single: 13574 case OMPD_master: 13575 case OMPD_masked: 13576 case OMPD_critical: 13577 case OMPD_taskgroup: 13578 case OMPD_distribute: 13579 case OMPD_ordered: 13580 case OMPD_atomic: 13581 case OMPD_distribute_simd: 13582 case OMPD_teams_distribute: 13583 case OMPD_teams_distribute_simd: 13584 case OMPD_requires: 13585 case OMPD_metadirective: 13586 llvm_unreachable("Unexpected OpenMP directive with num_threads-clause"); 13587 case OMPD_unknown: 13588 default: 13589 llvm_unreachable("Unknown OpenMP directive"); 13590 } 13591 break; 13592 case OMPC_num_teams: 13593 switch (DKind) { 13594 case OMPD_target_teams: 13595 case OMPD_target_teams_distribute: 13596 case OMPD_target_teams_distribute_simd: 13597 case OMPD_target_teams_distribute_parallel_for: 13598 case OMPD_target_teams_distribute_parallel_for_simd: 13599 CaptureRegion = OMPD_target; 13600 break; 13601 case OMPD_teams_distribute_parallel_for: 13602 case OMPD_teams_distribute_parallel_for_simd: 13603 case OMPD_teams: 13604 case OMPD_teams_distribute: 13605 case OMPD_teams_distribute_simd: 13606 // Do not capture num_teams-clause expressions. 13607 break; 13608 case OMPD_distribute_parallel_for: 13609 case OMPD_distribute_parallel_for_simd: 13610 case OMPD_task: 13611 case OMPD_taskloop: 13612 case OMPD_taskloop_simd: 13613 case OMPD_master_taskloop: 13614 case OMPD_master_taskloop_simd: 13615 case OMPD_parallel_master_taskloop: 13616 case OMPD_parallel_master_taskloop_simd: 13617 case OMPD_target_data: 13618 case OMPD_target_enter_data: 13619 case OMPD_target_exit_data: 13620 case OMPD_target_update: 13621 case OMPD_cancel: 13622 case OMPD_parallel: 13623 case OMPD_parallel_master: 13624 case OMPD_parallel_sections: 13625 case OMPD_parallel_for: 13626 case OMPD_parallel_for_simd: 13627 case OMPD_target: 13628 case OMPD_target_simd: 13629 case OMPD_target_parallel: 13630 case OMPD_target_parallel_for: 13631 case OMPD_target_parallel_for_simd: 13632 case OMPD_threadprivate: 13633 case OMPD_allocate: 13634 case OMPD_taskyield: 13635 case OMPD_barrier: 13636 case OMPD_taskwait: 13637 case OMPD_cancellation_point: 13638 case OMPD_flush: 13639 case OMPD_depobj: 13640 case OMPD_scan: 13641 case OMPD_declare_reduction: 13642 case OMPD_declare_mapper: 13643 case OMPD_declare_simd: 13644 case OMPD_declare_variant: 13645 case OMPD_begin_declare_variant: 13646 case OMPD_end_declare_variant: 13647 case OMPD_declare_target: 13648 case OMPD_end_declare_target: 13649 case OMPD_simd: 13650 case OMPD_tile: 13651 case OMPD_unroll: 13652 case OMPD_for: 13653 case OMPD_for_simd: 13654 case OMPD_sections: 13655 case OMPD_section: 13656 case OMPD_single: 13657 case OMPD_master: 13658 case OMPD_masked: 13659 case OMPD_critical: 13660 case OMPD_taskgroup: 13661 case OMPD_distribute: 13662 case OMPD_ordered: 13663 case OMPD_atomic: 13664 case OMPD_distribute_simd: 13665 case OMPD_requires: 13666 case OMPD_metadirective: 13667 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause"); 13668 case OMPD_unknown: 13669 default: 13670 llvm_unreachable("Unknown OpenMP directive"); 13671 } 13672 break; 13673 case OMPC_thread_limit: 13674 switch (DKind) { 13675 case OMPD_target_teams: 13676 case OMPD_target_teams_distribute: 13677 case OMPD_target_teams_distribute_simd: 13678 case OMPD_target_teams_distribute_parallel_for: 13679 case OMPD_target_teams_distribute_parallel_for_simd: 13680 CaptureRegion = OMPD_target; 13681 break; 13682 case OMPD_teams_distribute_parallel_for: 13683 case OMPD_teams_distribute_parallel_for_simd: 13684 case OMPD_teams: 13685 case OMPD_teams_distribute: 13686 case OMPD_teams_distribute_simd: 13687 // Do not capture thread_limit-clause expressions. 13688 break; 13689 case OMPD_distribute_parallel_for: 13690 case OMPD_distribute_parallel_for_simd: 13691 case OMPD_task: 13692 case OMPD_taskloop: 13693 case OMPD_taskloop_simd: 13694 case OMPD_master_taskloop: 13695 case OMPD_master_taskloop_simd: 13696 case OMPD_parallel_master_taskloop: 13697 case OMPD_parallel_master_taskloop_simd: 13698 case OMPD_target_data: 13699 case OMPD_target_enter_data: 13700 case OMPD_target_exit_data: 13701 case OMPD_target_update: 13702 case OMPD_cancel: 13703 case OMPD_parallel: 13704 case OMPD_parallel_master: 13705 case OMPD_parallel_sections: 13706 case OMPD_parallel_for: 13707 case OMPD_parallel_for_simd: 13708 case OMPD_target: 13709 case OMPD_target_simd: 13710 case OMPD_target_parallel: 13711 case OMPD_target_parallel_for: 13712 case OMPD_target_parallel_for_simd: 13713 case OMPD_threadprivate: 13714 case OMPD_allocate: 13715 case OMPD_taskyield: 13716 case OMPD_barrier: 13717 case OMPD_taskwait: 13718 case OMPD_cancellation_point: 13719 case OMPD_flush: 13720 case OMPD_depobj: 13721 case OMPD_scan: 13722 case OMPD_declare_reduction: 13723 case OMPD_declare_mapper: 13724 case OMPD_declare_simd: 13725 case OMPD_declare_variant: 13726 case OMPD_begin_declare_variant: 13727 case OMPD_end_declare_variant: 13728 case OMPD_declare_target: 13729 case OMPD_end_declare_target: 13730 case OMPD_simd: 13731 case OMPD_tile: 13732 case OMPD_unroll: 13733 case OMPD_for: 13734 case OMPD_for_simd: 13735 case OMPD_sections: 13736 case OMPD_section: 13737 case OMPD_single: 13738 case OMPD_master: 13739 case OMPD_masked: 13740 case OMPD_critical: 13741 case OMPD_taskgroup: 13742 case OMPD_distribute: 13743 case OMPD_ordered: 13744 case OMPD_atomic: 13745 case OMPD_distribute_simd: 13746 case OMPD_requires: 13747 case OMPD_metadirective: 13748 llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause"); 13749 case OMPD_unknown: 13750 default: 13751 llvm_unreachable("Unknown OpenMP directive"); 13752 } 13753 break; 13754 case OMPC_schedule: 13755 switch (DKind) { 13756 case OMPD_parallel_for: 13757 case OMPD_parallel_for_simd: 13758 case OMPD_distribute_parallel_for: 13759 case OMPD_distribute_parallel_for_simd: 13760 case OMPD_teams_distribute_parallel_for: 13761 case OMPD_teams_distribute_parallel_for_simd: 13762 case OMPD_target_parallel_for: 13763 case OMPD_target_parallel_for_simd: 13764 case OMPD_target_teams_distribute_parallel_for: 13765 case OMPD_target_teams_distribute_parallel_for_simd: 13766 CaptureRegion = OMPD_parallel; 13767 break; 13768 case OMPD_for: 13769 case OMPD_for_simd: 13770 // Do not capture schedule-clause expressions. 13771 break; 13772 case OMPD_task: 13773 case OMPD_taskloop: 13774 case OMPD_taskloop_simd: 13775 case OMPD_master_taskloop: 13776 case OMPD_master_taskloop_simd: 13777 case OMPD_parallel_master_taskloop: 13778 case OMPD_parallel_master_taskloop_simd: 13779 case OMPD_target_data: 13780 case OMPD_target_enter_data: 13781 case OMPD_target_exit_data: 13782 case OMPD_target_update: 13783 case OMPD_teams: 13784 case OMPD_teams_distribute: 13785 case OMPD_teams_distribute_simd: 13786 case OMPD_target_teams_distribute: 13787 case OMPD_target_teams_distribute_simd: 13788 case OMPD_target: 13789 case OMPD_target_simd: 13790 case OMPD_target_parallel: 13791 case OMPD_cancel: 13792 case OMPD_parallel: 13793 case OMPD_parallel_master: 13794 case OMPD_parallel_sections: 13795 case OMPD_threadprivate: 13796 case OMPD_allocate: 13797 case OMPD_taskyield: 13798 case OMPD_barrier: 13799 case OMPD_taskwait: 13800 case OMPD_cancellation_point: 13801 case OMPD_flush: 13802 case OMPD_depobj: 13803 case OMPD_scan: 13804 case OMPD_declare_reduction: 13805 case OMPD_declare_mapper: 13806 case OMPD_declare_simd: 13807 case OMPD_declare_variant: 13808 case OMPD_begin_declare_variant: 13809 case OMPD_end_declare_variant: 13810 case OMPD_declare_target: 13811 case OMPD_end_declare_target: 13812 case OMPD_simd: 13813 case OMPD_tile: 13814 case OMPD_unroll: 13815 case OMPD_sections: 13816 case OMPD_section: 13817 case OMPD_single: 13818 case OMPD_master: 13819 case OMPD_masked: 13820 case OMPD_critical: 13821 case OMPD_taskgroup: 13822 case OMPD_distribute: 13823 case OMPD_ordered: 13824 case OMPD_atomic: 13825 case OMPD_distribute_simd: 13826 case OMPD_target_teams: 13827 case OMPD_requires: 13828 case OMPD_metadirective: 13829 llvm_unreachable("Unexpected OpenMP directive with schedule clause"); 13830 case OMPD_unknown: 13831 default: 13832 llvm_unreachable("Unknown OpenMP directive"); 13833 } 13834 break; 13835 case OMPC_dist_schedule: 13836 switch (DKind) { 13837 case OMPD_teams_distribute_parallel_for: 13838 case OMPD_teams_distribute_parallel_for_simd: 13839 case OMPD_teams_distribute: 13840 case OMPD_teams_distribute_simd: 13841 case OMPD_target_teams_distribute_parallel_for: 13842 case OMPD_target_teams_distribute_parallel_for_simd: 13843 case OMPD_target_teams_distribute: 13844 case OMPD_target_teams_distribute_simd: 13845 CaptureRegion = OMPD_teams; 13846 break; 13847 case OMPD_distribute_parallel_for: 13848 case OMPD_distribute_parallel_for_simd: 13849 case OMPD_distribute: 13850 case OMPD_distribute_simd: 13851 // Do not capture dist_schedule-clause expressions. 13852 break; 13853 case OMPD_parallel_for: 13854 case OMPD_parallel_for_simd: 13855 case OMPD_target_parallel_for_simd: 13856 case OMPD_target_parallel_for: 13857 case OMPD_task: 13858 case OMPD_taskloop: 13859 case OMPD_taskloop_simd: 13860 case OMPD_master_taskloop: 13861 case OMPD_master_taskloop_simd: 13862 case OMPD_parallel_master_taskloop: 13863 case OMPD_parallel_master_taskloop_simd: 13864 case OMPD_target_data: 13865 case OMPD_target_enter_data: 13866 case OMPD_target_exit_data: 13867 case OMPD_target_update: 13868 case OMPD_teams: 13869 case OMPD_target: 13870 case OMPD_target_simd: 13871 case OMPD_target_parallel: 13872 case OMPD_cancel: 13873 case OMPD_parallel: 13874 case OMPD_parallel_master: 13875 case OMPD_parallel_sections: 13876 case OMPD_threadprivate: 13877 case OMPD_allocate: 13878 case OMPD_taskyield: 13879 case OMPD_barrier: 13880 case OMPD_taskwait: 13881 case OMPD_cancellation_point: 13882 case OMPD_flush: 13883 case OMPD_depobj: 13884 case OMPD_scan: 13885 case OMPD_declare_reduction: 13886 case OMPD_declare_mapper: 13887 case OMPD_declare_simd: 13888 case OMPD_declare_variant: 13889 case OMPD_begin_declare_variant: 13890 case OMPD_end_declare_variant: 13891 case OMPD_declare_target: 13892 case OMPD_end_declare_target: 13893 case OMPD_simd: 13894 case OMPD_tile: 13895 case OMPD_unroll: 13896 case OMPD_for: 13897 case OMPD_for_simd: 13898 case OMPD_sections: 13899 case OMPD_section: 13900 case OMPD_single: 13901 case OMPD_master: 13902 case OMPD_masked: 13903 case OMPD_critical: 13904 case OMPD_taskgroup: 13905 case OMPD_ordered: 13906 case OMPD_atomic: 13907 case OMPD_target_teams: 13908 case OMPD_requires: 13909 case OMPD_metadirective: 13910 llvm_unreachable("Unexpected OpenMP directive with dist_schedule clause"); 13911 case OMPD_unknown: 13912 default: 13913 llvm_unreachable("Unknown OpenMP directive"); 13914 } 13915 break; 13916 case OMPC_device: 13917 switch (DKind) { 13918 case OMPD_target_update: 13919 case OMPD_target_enter_data: 13920 case OMPD_target_exit_data: 13921 case OMPD_target: 13922 case OMPD_target_simd: 13923 case OMPD_target_teams: 13924 case OMPD_target_parallel: 13925 case OMPD_target_teams_distribute: 13926 case OMPD_target_teams_distribute_simd: 13927 case OMPD_target_parallel_for: 13928 case OMPD_target_parallel_for_simd: 13929 case OMPD_target_teams_distribute_parallel_for: 13930 case OMPD_target_teams_distribute_parallel_for_simd: 13931 case OMPD_dispatch: 13932 CaptureRegion = OMPD_task; 13933 break; 13934 case OMPD_target_data: 13935 case OMPD_interop: 13936 // Do not capture device-clause expressions. 13937 break; 13938 case OMPD_teams_distribute_parallel_for: 13939 case OMPD_teams_distribute_parallel_for_simd: 13940 case OMPD_teams: 13941 case OMPD_teams_distribute: 13942 case OMPD_teams_distribute_simd: 13943 case OMPD_distribute_parallel_for: 13944 case OMPD_distribute_parallel_for_simd: 13945 case OMPD_task: 13946 case OMPD_taskloop: 13947 case OMPD_taskloop_simd: 13948 case OMPD_master_taskloop: 13949 case OMPD_master_taskloop_simd: 13950 case OMPD_parallel_master_taskloop: 13951 case OMPD_parallel_master_taskloop_simd: 13952 case OMPD_cancel: 13953 case OMPD_parallel: 13954 case OMPD_parallel_master: 13955 case OMPD_parallel_sections: 13956 case OMPD_parallel_for: 13957 case OMPD_parallel_for_simd: 13958 case OMPD_threadprivate: 13959 case OMPD_allocate: 13960 case OMPD_taskyield: 13961 case OMPD_barrier: 13962 case OMPD_taskwait: 13963 case OMPD_cancellation_point: 13964 case OMPD_flush: 13965 case OMPD_depobj: 13966 case OMPD_scan: 13967 case OMPD_declare_reduction: 13968 case OMPD_declare_mapper: 13969 case OMPD_declare_simd: 13970 case OMPD_declare_variant: 13971 case OMPD_begin_declare_variant: 13972 case OMPD_end_declare_variant: 13973 case OMPD_declare_target: 13974 case OMPD_end_declare_target: 13975 case OMPD_simd: 13976 case OMPD_tile: 13977 case OMPD_unroll: 13978 case OMPD_for: 13979 case OMPD_for_simd: 13980 case OMPD_sections: 13981 case OMPD_section: 13982 case OMPD_single: 13983 case OMPD_master: 13984 case OMPD_masked: 13985 case OMPD_critical: 13986 case OMPD_taskgroup: 13987 case OMPD_distribute: 13988 case OMPD_ordered: 13989 case OMPD_atomic: 13990 case OMPD_distribute_simd: 13991 case OMPD_requires: 13992 case OMPD_metadirective: 13993 llvm_unreachable("Unexpected OpenMP directive with device-clause"); 13994 case OMPD_unknown: 13995 default: 13996 llvm_unreachable("Unknown OpenMP directive"); 13997 } 13998 break; 13999 case OMPC_grainsize: 14000 case OMPC_num_tasks: 14001 case OMPC_final: 14002 case OMPC_priority: 14003 switch (DKind) { 14004 case OMPD_task: 14005 case OMPD_taskloop: 14006 case OMPD_taskloop_simd: 14007 case OMPD_master_taskloop: 14008 case OMPD_master_taskloop_simd: 14009 break; 14010 case OMPD_parallel_master_taskloop: 14011 case OMPD_parallel_master_taskloop_simd: 14012 CaptureRegion = OMPD_parallel; 14013 break; 14014 case OMPD_target_update: 14015 case OMPD_target_enter_data: 14016 case OMPD_target_exit_data: 14017 case OMPD_target: 14018 case OMPD_target_simd: 14019 case OMPD_target_teams: 14020 case OMPD_target_parallel: 14021 case OMPD_target_teams_distribute: 14022 case OMPD_target_teams_distribute_simd: 14023 case OMPD_target_parallel_for: 14024 case OMPD_target_parallel_for_simd: 14025 case OMPD_target_teams_distribute_parallel_for: 14026 case OMPD_target_teams_distribute_parallel_for_simd: 14027 case OMPD_target_data: 14028 case OMPD_teams_distribute_parallel_for: 14029 case OMPD_teams_distribute_parallel_for_simd: 14030 case OMPD_teams: 14031 case OMPD_teams_distribute: 14032 case OMPD_teams_distribute_simd: 14033 case OMPD_distribute_parallel_for: 14034 case OMPD_distribute_parallel_for_simd: 14035 case OMPD_cancel: 14036 case OMPD_parallel: 14037 case OMPD_parallel_master: 14038 case OMPD_parallel_sections: 14039 case OMPD_parallel_for: 14040 case OMPD_parallel_for_simd: 14041 case OMPD_threadprivate: 14042 case OMPD_allocate: 14043 case OMPD_taskyield: 14044 case OMPD_barrier: 14045 case OMPD_taskwait: 14046 case OMPD_cancellation_point: 14047 case OMPD_flush: 14048 case OMPD_depobj: 14049 case OMPD_scan: 14050 case OMPD_declare_reduction: 14051 case OMPD_declare_mapper: 14052 case OMPD_declare_simd: 14053 case OMPD_declare_variant: 14054 case OMPD_begin_declare_variant: 14055 case OMPD_end_declare_variant: 14056 case OMPD_declare_target: 14057 case OMPD_end_declare_target: 14058 case OMPD_simd: 14059 case OMPD_tile: 14060 case OMPD_unroll: 14061 case OMPD_for: 14062 case OMPD_for_simd: 14063 case OMPD_sections: 14064 case OMPD_section: 14065 case OMPD_single: 14066 case OMPD_master: 14067 case OMPD_masked: 14068 case OMPD_critical: 14069 case OMPD_taskgroup: 14070 case OMPD_distribute: 14071 case OMPD_ordered: 14072 case OMPD_atomic: 14073 case OMPD_distribute_simd: 14074 case OMPD_requires: 14075 case OMPD_metadirective: 14076 llvm_unreachable("Unexpected OpenMP directive with grainsize-clause"); 14077 case OMPD_unknown: 14078 default: 14079 llvm_unreachable("Unknown OpenMP directive"); 14080 } 14081 break; 14082 case OMPC_novariants: 14083 case OMPC_nocontext: 14084 switch (DKind) { 14085 case OMPD_dispatch: 14086 CaptureRegion = OMPD_task; 14087 break; 14088 default: 14089 llvm_unreachable("Unexpected OpenMP directive"); 14090 } 14091 break; 14092 case OMPC_filter: 14093 // Do not capture filter-clause expressions. 14094 break; 14095 case OMPC_when: 14096 if (DKind == OMPD_metadirective) { 14097 CaptureRegion = OMPD_metadirective; 14098 } else if (DKind == OMPD_unknown) { 14099 llvm_unreachable("Unknown OpenMP directive"); 14100 } else { 14101 llvm_unreachable("Unexpected OpenMP directive with when clause"); 14102 } 14103 break; 14104 case OMPC_firstprivate: 14105 case OMPC_lastprivate: 14106 case OMPC_reduction: 14107 case OMPC_task_reduction: 14108 case OMPC_in_reduction: 14109 case OMPC_linear: 14110 case OMPC_default: 14111 case OMPC_proc_bind: 14112 case OMPC_safelen: 14113 case OMPC_simdlen: 14114 case OMPC_sizes: 14115 case OMPC_allocator: 14116 case OMPC_collapse: 14117 case OMPC_private: 14118 case OMPC_shared: 14119 case OMPC_aligned: 14120 case OMPC_copyin: 14121 case OMPC_copyprivate: 14122 case OMPC_ordered: 14123 case OMPC_nowait: 14124 case OMPC_untied: 14125 case OMPC_mergeable: 14126 case OMPC_threadprivate: 14127 case OMPC_allocate: 14128 case OMPC_flush: 14129 case OMPC_depobj: 14130 case OMPC_read: 14131 case OMPC_write: 14132 case OMPC_update: 14133 case OMPC_capture: 14134 case OMPC_seq_cst: 14135 case OMPC_acq_rel: 14136 case OMPC_acquire: 14137 case OMPC_release: 14138 case OMPC_relaxed: 14139 case OMPC_depend: 14140 case OMPC_threads: 14141 case OMPC_simd: 14142 case OMPC_map: 14143 case OMPC_nogroup: 14144 case OMPC_hint: 14145 case OMPC_defaultmap: 14146 case OMPC_unknown: 14147 case OMPC_uniform: 14148 case OMPC_to: 14149 case OMPC_from: 14150 case OMPC_use_device_ptr: 14151 case OMPC_use_device_addr: 14152 case OMPC_is_device_ptr: 14153 case OMPC_unified_address: 14154 case OMPC_unified_shared_memory: 14155 case OMPC_reverse_offload: 14156 case OMPC_dynamic_allocators: 14157 case OMPC_atomic_default_mem_order: 14158 case OMPC_device_type: 14159 case OMPC_match: 14160 case OMPC_nontemporal: 14161 case OMPC_order: 14162 case OMPC_destroy: 14163 case OMPC_detach: 14164 case OMPC_inclusive: 14165 case OMPC_exclusive: 14166 case OMPC_uses_allocators: 14167 case OMPC_affinity: 14168 default: 14169 llvm_unreachable("Unexpected OpenMP clause."); 14170 } 14171 return CaptureRegion; 14172 } 14173 14174 OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier, 14175 Expr *Condition, SourceLocation StartLoc, 14176 SourceLocation LParenLoc, 14177 SourceLocation NameModifierLoc, 14178 SourceLocation ColonLoc, 14179 SourceLocation EndLoc) { 14180 Expr *ValExpr = Condition; 14181 Stmt *HelperValStmt = nullptr; 14182 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 14183 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 14184 !Condition->isInstantiationDependent() && 14185 !Condition->containsUnexpandedParameterPack()) { 14186 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 14187 if (Val.isInvalid()) 14188 return nullptr; 14189 14190 ValExpr = Val.get(); 14191 14192 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 14193 CaptureRegion = getOpenMPCaptureRegionForClause( 14194 DKind, OMPC_if, LangOpts.OpenMP, NameModifier); 14195 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 14196 ValExpr = MakeFullExpr(ValExpr).get(); 14197 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 14198 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 14199 HelperValStmt = buildPreInits(Context, Captures); 14200 } 14201 } 14202 14203 return new (Context) 14204 OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc, 14205 LParenLoc, NameModifierLoc, ColonLoc, EndLoc); 14206 } 14207 14208 OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition, 14209 SourceLocation StartLoc, 14210 SourceLocation LParenLoc, 14211 SourceLocation EndLoc) { 14212 Expr *ValExpr = Condition; 14213 Stmt *HelperValStmt = nullptr; 14214 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 14215 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 14216 !Condition->isInstantiationDependent() && 14217 !Condition->containsUnexpandedParameterPack()) { 14218 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 14219 if (Val.isInvalid()) 14220 return nullptr; 14221 14222 ValExpr = MakeFullExpr(Val.get()).get(); 14223 14224 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 14225 CaptureRegion = 14226 getOpenMPCaptureRegionForClause(DKind, OMPC_final, LangOpts.OpenMP); 14227 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 14228 ValExpr = MakeFullExpr(ValExpr).get(); 14229 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 14230 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 14231 HelperValStmt = buildPreInits(Context, Captures); 14232 } 14233 } 14234 14235 return new (Context) OMPFinalClause(ValExpr, HelperValStmt, CaptureRegion, 14236 StartLoc, LParenLoc, EndLoc); 14237 } 14238 14239 ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc, 14240 Expr *Op) { 14241 if (!Op) 14242 return ExprError(); 14243 14244 class IntConvertDiagnoser : public ICEConvertDiagnoser { 14245 public: 14246 IntConvertDiagnoser() 14247 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {} 14248 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, 14249 QualType T) override { 14250 return S.Diag(Loc, diag::err_omp_not_integral) << T; 14251 } 14252 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc, 14253 QualType T) override { 14254 return S.Diag(Loc, diag::err_omp_incomplete_type) << T; 14255 } 14256 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc, 14257 QualType T, 14258 QualType ConvTy) override { 14259 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy; 14260 } 14261 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv, 14262 QualType ConvTy) override { 14263 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 14264 << ConvTy->isEnumeralType() << ConvTy; 14265 } 14266 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc, 14267 QualType T) override { 14268 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T; 14269 } 14270 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv, 14271 QualType ConvTy) override { 14272 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 14273 << ConvTy->isEnumeralType() << ConvTy; 14274 } 14275 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType, 14276 QualType) override { 14277 llvm_unreachable("conversion functions are permitted"); 14278 } 14279 } ConvertDiagnoser; 14280 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser); 14281 } 14282 14283 static bool 14284 isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef, OpenMPClauseKind CKind, 14285 bool StrictlyPositive, bool BuildCapture = false, 14286 OpenMPDirectiveKind DKind = OMPD_unknown, 14287 OpenMPDirectiveKind *CaptureRegion = nullptr, 14288 Stmt **HelperValStmt = nullptr) { 14289 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() && 14290 !ValExpr->isInstantiationDependent()) { 14291 SourceLocation Loc = ValExpr->getExprLoc(); 14292 ExprResult Value = 14293 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr); 14294 if (Value.isInvalid()) 14295 return false; 14296 14297 ValExpr = Value.get(); 14298 // The expression must evaluate to a non-negative integer value. 14299 if (Optional<llvm::APSInt> Result = 14300 ValExpr->getIntegerConstantExpr(SemaRef.Context)) { 14301 if (Result->isSigned() && 14302 !((!StrictlyPositive && Result->isNonNegative()) || 14303 (StrictlyPositive && Result->isStrictlyPositive()))) { 14304 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause) 14305 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) 14306 << ValExpr->getSourceRange(); 14307 return false; 14308 } 14309 } 14310 if (!BuildCapture) 14311 return true; 14312 *CaptureRegion = 14313 getOpenMPCaptureRegionForClause(DKind, CKind, SemaRef.LangOpts.OpenMP); 14314 if (*CaptureRegion != OMPD_unknown && 14315 !SemaRef.CurContext->isDependentContext()) { 14316 ValExpr = SemaRef.MakeFullExpr(ValExpr).get(); 14317 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 14318 ValExpr = tryBuildCapture(SemaRef, ValExpr, Captures).get(); 14319 *HelperValStmt = buildPreInits(SemaRef.Context, Captures); 14320 } 14321 } 14322 return true; 14323 } 14324 14325 OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads, 14326 SourceLocation StartLoc, 14327 SourceLocation LParenLoc, 14328 SourceLocation EndLoc) { 14329 Expr *ValExpr = NumThreads; 14330 Stmt *HelperValStmt = nullptr; 14331 14332 // OpenMP [2.5, Restrictions] 14333 // The num_threads expression must evaluate to a positive integer value. 14334 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads, 14335 /*StrictlyPositive=*/true)) 14336 return nullptr; 14337 14338 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 14339 OpenMPDirectiveKind CaptureRegion = 14340 getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads, LangOpts.OpenMP); 14341 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 14342 ValExpr = MakeFullExpr(ValExpr).get(); 14343 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 14344 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 14345 HelperValStmt = buildPreInits(Context, Captures); 14346 } 14347 14348 return new (Context) OMPNumThreadsClause( 14349 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc); 14350 } 14351 14352 ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E, 14353 OpenMPClauseKind CKind, 14354 bool StrictlyPositive, 14355 bool SuppressExprDiags) { 14356 if (!E) 14357 return ExprError(); 14358 if (E->isValueDependent() || E->isTypeDependent() || 14359 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 14360 return E; 14361 14362 llvm::APSInt Result; 14363 ExprResult ICE; 14364 if (SuppressExprDiags) { 14365 // Use a custom diagnoser that suppresses 'note' diagnostics about the 14366 // expression. 14367 struct SuppressedDiagnoser : public Sema::VerifyICEDiagnoser { 14368 SuppressedDiagnoser() : VerifyICEDiagnoser(/*Suppress=*/true) {} 14369 Sema::SemaDiagnosticBuilder diagnoseNotICE(Sema &S, 14370 SourceLocation Loc) override { 14371 llvm_unreachable("Diagnostic suppressed"); 14372 } 14373 } Diagnoser; 14374 ICE = VerifyIntegerConstantExpression(E, &Result, Diagnoser, AllowFold); 14375 } else { 14376 ICE = VerifyIntegerConstantExpression(E, &Result, /*FIXME*/ AllowFold); 14377 } 14378 if (ICE.isInvalid()) 14379 return ExprError(); 14380 14381 if ((StrictlyPositive && !Result.isStrictlyPositive()) || 14382 (!StrictlyPositive && !Result.isNonNegative())) { 14383 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause) 14384 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) 14385 << E->getSourceRange(); 14386 return ExprError(); 14387 } 14388 if (CKind == OMPC_aligned && !Result.isPowerOf2()) { 14389 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two) 14390 << E->getSourceRange(); 14391 return ExprError(); 14392 } 14393 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1) 14394 DSAStack->setAssociatedLoops(Result.getExtValue()); 14395 else if (CKind == OMPC_ordered) 14396 DSAStack->setAssociatedLoops(Result.getExtValue()); 14397 return ICE; 14398 } 14399 14400 OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc, 14401 SourceLocation LParenLoc, 14402 SourceLocation EndLoc) { 14403 // OpenMP [2.8.1, simd construct, Description] 14404 // The parameter of the safelen clause must be a constant 14405 // positive integer expression. 14406 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen); 14407 if (Safelen.isInvalid()) 14408 return nullptr; 14409 return new (Context) 14410 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc); 14411 } 14412 14413 OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc, 14414 SourceLocation LParenLoc, 14415 SourceLocation EndLoc) { 14416 // OpenMP [2.8.1, simd construct, Description] 14417 // The parameter of the simdlen clause must be a constant 14418 // positive integer expression. 14419 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen); 14420 if (Simdlen.isInvalid()) 14421 return nullptr; 14422 return new (Context) 14423 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc); 14424 } 14425 14426 /// Tries to find omp_allocator_handle_t type. 14427 static bool findOMPAllocatorHandleT(Sema &S, SourceLocation Loc, 14428 DSAStackTy *Stack) { 14429 QualType OMPAllocatorHandleT = Stack->getOMPAllocatorHandleT(); 14430 if (!OMPAllocatorHandleT.isNull()) 14431 return true; 14432 // Build the predefined allocator expressions. 14433 bool ErrorFound = false; 14434 for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) { 14435 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I); 14436 StringRef Allocator = 14437 OMPAllocateDeclAttr::ConvertAllocatorTypeTyToStr(AllocatorKind); 14438 DeclarationName AllocatorName = &S.getASTContext().Idents.get(Allocator); 14439 auto *VD = dyn_cast_or_null<ValueDecl>( 14440 S.LookupSingleName(S.TUScope, AllocatorName, Loc, Sema::LookupAnyName)); 14441 if (!VD) { 14442 ErrorFound = true; 14443 break; 14444 } 14445 QualType AllocatorType = 14446 VD->getType().getNonLValueExprType(S.getASTContext()); 14447 ExprResult Res = S.BuildDeclRefExpr(VD, AllocatorType, VK_LValue, Loc); 14448 if (!Res.isUsable()) { 14449 ErrorFound = true; 14450 break; 14451 } 14452 if (OMPAllocatorHandleT.isNull()) 14453 OMPAllocatorHandleT = AllocatorType; 14454 if (!S.getASTContext().hasSameType(OMPAllocatorHandleT, AllocatorType)) { 14455 ErrorFound = true; 14456 break; 14457 } 14458 Stack->setAllocator(AllocatorKind, Res.get()); 14459 } 14460 if (ErrorFound) { 14461 S.Diag(Loc, diag::err_omp_implied_type_not_found) 14462 << "omp_allocator_handle_t"; 14463 return false; 14464 } 14465 OMPAllocatorHandleT.addConst(); 14466 Stack->setOMPAllocatorHandleT(OMPAllocatorHandleT); 14467 return true; 14468 } 14469 14470 OMPClause *Sema::ActOnOpenMPAllocatorClause(Expr *A, SourceLocation StartLoc, 14471 SourceLocation LParenLoc, 14472 SourceLocation EndLoc) { 14473 // OpenMP [2.11.3, allocate Directive, Description] 14474 // allocator is an expression of omp_allocator_handle_t type. 14475 if (!findOMPAllocatorHandleT(*this, A->getExprLoc(), DSAStack)) 14476 return nullptr; 14477 14478 ExprResult Allocator = DefaultLvalueConversion(A); 14479 if (Allocator.isInvalid()) 14480 return nullptr; 14481 Allocator = PerformImplicitConversion(Allocator.get(), 14482 DSAStack->getOMPAllocatorHandleT(), 14483 Sema::AA_Initializing, 14484 /*AllowExplicit=*/true); 14485 if (Allocator.isInvalid()) 14486 return nullptr; 14487 return new (Context) 14488 OMPAllocatorClause(Allocator.get(), StartLoc, LParenLoc, EndLoc); 14489 } 14490 14491 OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops, 14492 SourceLocation StartLoc, 14493 SourceLocation LParenLoc, 14494 SourceLocation EndLoc) { 14495 // OpenMP [2.7.1, loop construct, Description] 14496 // OpenMP [2.8.1, simd construct, Description] 14497 // OpenMP [2.9.6, distribute construct, Description] 14498 // The parameter of the collapse clause must be a constant 14499 // positive integer expression. 14500 ExprResult NumForLoopsResult = 14501 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse); 14502 if (NumForLoopsResult.isInvalid()) 14503 return nullptr; 14504 return new (Context) 14505 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc); 14506 } 14507 14508 OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc, 14509 SourceLocation EndLoc, 14510 SourceLocation LParenLoc, 14511 Expr *NumForLoops) { 14512 // OpenMP [2.7.1, loop construct, Description] 14513 // OpenMP [2.8.1, simd construct, Description] 14514 // OpenMP [2.9.6, distribute construct, Description] 14515 // The parameter of the ordered clause must be a constant 14516 // positive integer expression if any. 14517 if (NumForLoops && LParenLoc.isValid()) { 14518 ExprResult NumForLoopsResult = 14519 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered); 14520 if (NumForLoopsResult.isInvalid()) 14521 return nullptr; 14522 NumForLoops = NumForLoopsResult.get(); 14523 } else { 14524 NumForLoops = nullptr; 14525 } 14526 auto *Clause = OMPOrderedClause::Create( 14527 Context, NumForLoops, NumForLoops ? DSAStack->getAssociatedLoops() : 0, 14528 StartLoc, LParenLoc, EndLoc); 14529 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops, Clause); 14530 return Clause; 14531 } 14532 14533 OMPClause *Sema::ActOnOpenMPSimpleClause( 14534 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc, 14535 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 14536 OMPClause *Res = nullptr; 14537 switch (Kind) { 14538 case OMPC_default: 14539 Res = ActOnOpenMPDefaultClause(static_cast<DefaultKind>(Argument), 14540 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 14541 break; 14542 case OMPC_proc_bind: 14543 Res = ActOnOpenMPProcBindClause(static_cast<ProcBindKind>(Argument), 14544 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 14545 break; 14546 case OMPC_atomic_default_mem_order: 14547 Res = ActOnOpenMPAtomicDefaultMemOrderClause( 14548 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Argument), 14549 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 14550 break; 14551 case OMPC_order: 14552 Res = ActOnOpenMPOrderClause(static_cast<OpenMPOrderClauseKind>(Argument), 14553 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 14554 break; 14555 case OMPC_update: 14556 Res = ActOnOpenMPUpdateClause(static_cast<OpenMPDependClauseKind>(Argument), 14557 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 14558 break; 14559 case OMPC_if: 14560 case OMPC_final: 14561 case OMPC_num_threads: 14562 case OMPC_safelen: 14563 case OMPC_simdlen: 14564 case OMPC_sizes: 14565 case OMPC_allocator: 14566 case OMPC_collapse: 14567 case OMPC_schedule: 14568 case OMPC_private: 14569 case OMPC_firstprivate: 14570 case OMPC_lastprivate: 14571 case OMPC_shared: 14572 case OMPC_reduction: 14573 case OMPC_task_reduction: 14574 case OMPC_in_reduction: 14575 case OMPC_linear: 14576 case OMPC_aligned: 14577 case OMPC_copyin: 14578 case OMPC_copyprivate: 14579 case OMPC_ordered: 14580 case OMPC_nowait: 14581 case OMPC_untied: 14582 case OMPC_mergeable: 14583 case OMPC_threadprivate: 14584 case OMPC_allocate: 14585 case OMPC_flush: 14586 case OMPC_depobj: 14587 case OMPC_read: 14588 case OMPC_write: 14589 case OMPC_capture: 14590 case OMPC_seq_cst: 14591 case OMPC_acq_rel: 14592 case OMPC_acquire: 14593 case OMPC_release: 14594 case OMPC_relaxed: 14595 case OMPC_depend: 14596 case OMPC_device: 14597 case OMPC_threads: 14598 case OMPC_simd: 14599 case OMPC_map: 14600 case OMPC_num_teams: 14601 case OMPC_thread_limit: 14602 case OMPC_priority: 14603 case OMPC_grainsize: 14604 case OMPC_nogroup: 14605 case OMPC_num_tasks: 14606 case OMPC_hint: 14607 case OMPC_dist_schedule: 14608 case OMPC_defaultmap: 14609 case OMPC_unknown: 14610 case OMPC_uniform: 14611 case OMPC_to: 14612 case OMPC_from: 14613 case OMPC_use_device_ptr: 14614 case OMPC_use_device_addr: 14615 case OMPC_is_device_ptr: 14616 case OMPC_unified_address: 14617 case OMPC_unified_shared_memory: 14618 case OMPC_reverse_offload: 14619 case OMPC_dynamic_allocators: 14620 case OMPC_device_type: 14621 case OMPC_match: 14622 case OMPC_nontemporal: 14623 case OMPC_destroy: 14624 case OMPC_novariants: 14625 case OMPC_nocontext: 14626 case OMPC_detach: 14627 case OMPC_inclusive: 14628 case OMPC_exclusive: 14629 case OMPC_uses_allocators: 14630 case OMPC_affinity: 14631 case OMPC_when: 14632 default: 14633 llvm_unreachable("Clause is not allowed."); 14634 } 14635 return Res; 14636 } 14637 14638 static std::string 14639 getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last, 14640 ArrayRef<unsigned> Exclude = llvm::None) { 14641 SmallString<256> Buffer; 14642 llvm::raw_svector_ostream Out(Buffer); 14643 unsigned Skipped = Exclude.size(); 14644 auto S = Exclude.begin(), E = Exclude.end(); 14645 for (unsigned I = First; I < Last; ++I) { 14646 if (std::find(S, E, I) != E) { 14647 --Skipped; 14648 continue; 14649 } 14650 Out << "'" << getOpenMPSimpleClauseTypeName(K, I) << "'"; 14651 if (I + Skipped + 2 == Last) 14652 Out << " or "; 14653 else if (I + Skipped + 1 != Last) 14654 Out << ", "; 14655 } 14656 return std::string(Out.str()); 14657 } 14658 14659 OMPClause *Sema::ActOnOpenMPDefaultClause(DefaultKind Kind, 14660 SourceLocation KindKwLoc, 14661 SourceLocation StartLoc, 14662 SourceLocation LParenLoc, 14663 SourceLocation EndLoc) { 14664 if (Kind == OMP_DEFAULT_unknown) { 14665 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 14666 << getListOfPossibleValues(OMPC_default, /*First=*/0, 14667 /*Last=*/unsigned(OMP_DEFAULT_unknown)) 14668 << getOpenMPClauseName(OMPC_default); 14669 return nullptr; 14670 } 14671 14672 switch (Kind) { 14673 case OMP_DEFAULT_none: 14674 DSAStack->setDefaultDSANone(KindKwLoc); 14675 break; 14676 case OMP_DEFAULT_shared: 14677 DSAStack->setDefaultDSAShared(KindKwLoc); 14678 break; 14679 case OMP_DEFAULT_firstprivate: 14680 DSAStack->setDefaultDSAFirstPrivate(KindKwLoc); 14681 break; 14682 default: 14683 llvm_unreachable("DSA unexpected in OpenMP default clause"); 14684 } 14685 14686 return new (Context) 14687 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 14688 } 14689 14690 OMPClause *Sema::ActOnOpenMPProcBindClause(ProcBindKind Kind, 14691 SourceLocation KindKwLoc, 14692 SourceLocation StartLoc, 14693 SourceLocation LParenLoc, 14694 SourceLocation EndLoc) { 14695 if (Kind == OMP_PROC_BIND_unknown) { 14696 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 14697 << getListOfPossibleValues(OMPC_proc_bind, 14698 /*First=*/unsigned(OMP_PROC_BIND_master), 14699 /*Last=*/ 14700 unsigned(LangOpts.OpenMP > 50 14701 ? OMP_PROC_BIND_primary 14702 : OMP_PROC_BIND_spread) + 14703 1) 14704 << getOpenMPClauseName(OMPC_proc_bind); 14705 return nullptr; 14706 } 14707 if (Kind == OMP_PROC_BIND_primary && LangOpts.OpenMP < 51) 14708 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 14709 << getListOfPossibleValues(OMPC_proc_bind, 14710 /*First=*/unsigned(OMP_PROC_BIND_master), 14711 /*Last=*/ 14712 unsigned(OMP_PROC_BIND_spread) + 1) 14713 << getOpenMPClauseName(OMPC_proc_bind); 14714 return new (Context) 14715 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 14716 } 14717 14718 OMPClause *Sema::ActOnOpenMPAtomicDefaultMemOrderClause( 14719 OpenMPAtomicDefaultMemOrderClauseKind Kind, SourceLocation KindKwLoc, 14720 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 14721 if (Kind == OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) { 14722 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 14723 << getListOfPossibleValues( 14724 OMPC_atomic_default_mem_order, /*First=*/0, 14725 /*Last=*/OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) 14726 << getOpenMPClauseName(OMPC_atomic_default_mem_order); 14727 return nullptr; 14728 } 14729 return new (Context) OMPAtomicDefaultMemOrderClause(Kind, KindKwLoc, StartLoc, 14730 LParenLoc, EndLoc); 14731 } 14732 14733 OMPClause *Sema::ActOnOpenMPOrderClause(OpenMPOrderClauseKind Kind, 14734 SourceLocation KindKwLoc, 14735 SourceLocation StartLoc, 14736 SourceLocation LParenLoc, 14737 SourceLocation EndLoc) { 14738 if (Kind == OMPC_ORDER_unknown) { 14739 static_assert(OMPC_ORDER_unknown > 0, 14740 "OMPC_ORDER_unknown not greater than 0"); 14741 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 14742 << getListOfPossibleValues(OMPC_order, /*First=*/0, 14743 /*Last=*/OMPC_ORDER_unknown) 14744 << getOpenMPClauseName(OMPC_order); 14745 return nullptr; 14746 } 14747 return new (Context) 14748 OMPOrderClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 14749 } 14750 14751 OMPClause *Sema::ActOnOpenMPUpdateClause(OpenMPDependClauseKind Kind, 14752 SourceLocation KindKwLoc, 14753 SourceLocation StartLoc, 14754 SourceLocation LParenLoc, 14755 SourceLocation EndLoc) { 14756 if (Kind == OMPC_DEPEND_unknown || Kind == OMPC_DEPEND_source || 14757 Kind == OMPC_DEPEND_sink || Kind == OMPC_DEPEND_depobj) { 14758 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink, 14759 OMPC_DEPEND_depobj}; 14760 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 14761 << getListOfPossibleValues(OMPC_depend, /*First=*/0, 14762 /*Last=*/OMPC_DEPEND_unknown, Except) 14763 << getOpenMPClauseName(OMPC_update); 14764 return nullptr; 14765 } 14766 return OMPUpdateClause::Create(Context, StartLoc, LParenLoc, KindKwLoc, Kind, 14767 EndLoc); 14768 } 14769 14770 OMPClause *Sema::ActOnOpenMPSizesClause(ArrayRef<Expr *> SizeExprs, 14771 SourceLocation StartLoc, 14772 SourceLocation LParenLoc, 14773 SourceLocation EndLoc) { 14774 for (Expr *SizeExpr : SizeExprs) { 14775 ExprResult NumForLoopsResult = VerifyPositiveIntegerConstantInClause( 14776 SizeExpr, OMPC_sizes, /*StrictlyPositive=*/true); 14777 if (!NumForLoopsResult.isUsable()) 14778 return nullptr; 14779 } 14780 14781 DSAStack->setAssociatedLoops(SizeExprs.size()); 14782 return OMPSizesClause::Create(Context, StartLoc, LParenLoc, EndLoc, 14783 SizeExprs); 14784 } 14785 14786 OMPClause *Sema::ActOnOpenMPFullClause(SourceLocation StartLoc, 14787 SourceLocation EndLoc) { 14788 return OMPFullClause::Create(Context, StartLoc, EndLoc); 14789 } 14790 14791 OMPClause *Sema::ActOnOpenMPPartialClause(Expr *FactorExpr, 14792 SourceLocation StartLoc, 14793 SourceLocation LParenLoc, 14794 SourceLocation EndLoc) { 14795 if (FactorExpr) { 14796 // If an argument is specified, it must be a constant (or an unevaluated 14797 // template expression). 14798 ExprResult FactorResult = VerifyPositiveIntegerConstantInClause( 14799 FactorExpr, OMPC_partial, /*StrictlyPositive=*/true); 14800 if (FactorResult.isInvalid()) 14801 return nullptr; 14802 FactorExpr = FactorResult.get(); 14803 } 14804 14805 return OMPPartialClause::Create(Context, StartLoc, LParenLoc, EndLoc, 14806 FactorExpr); 14807 } 14808 14809 OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause( 14810 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr, 14811 SourceLocation StartLoc, SourceLocation LParenLoc, 14812 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc, 14813 SourceLocation EndLoc) { 14814 OMPClause *Res = nullptr; 14815 switch (Kind) { 14816 case OMPC_schedule: 14817 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements }; 14818 assert(Argument.size() == NumberOfElements && 14819 ArgumentLoc.size() == NumberOfElements); 14820 Res = ActOnOpenMPScheduleClause( 14821 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]), 14822 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]), 14823 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr, 14824 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2], 14825 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc); 14826 break; 14827 case OMPC_if: 14828 assert(Argument.size() == 1 && ArgumentLoc.size() == 1); 14829 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()), 14830 Expr, StartLoc, LParenLoc, ArgumentLoc.back(), 14831 DelimLoc, EndLoc); 14832 break; 14833 case OMPC_dist_schedule: 14834 Res = ActOnOpenMPDistScheduleClause( 14835 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr, 14836 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc); 14837 break; 14838 case OMPC_defaultmap: 14839 enum { Modifier, DefaultmapKind }; 14840 Res = ActOnOpenMPDefaultmapClause( 14841 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]), 14842 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]), 14843 StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind], 14844 EndLoc); 14845 break; 14846 case OMPC_device: 14847 assert(Argument.size() == 1 && ArgumentLoc.size() == 1); 14848 Res = ActOnOpenMPDeviceClause( 14849 static_cast<OpenMPDeviceClauseModifier>(Argument.back()), Expr, 14850 StartLoc, LParenLoc, ArgumentLoc.back(), EndLoc); 14851 break; 14852 case OMPC_final: 14853 case OMPC_num_threads: 14854 case OMPC_safelen: 14855 case OMPC_simdlen: 14856 case OMPC_sizes: 14857 case OMPC_allocator: 14858 case OMPC_collapse: 14859 case OMPC_default: 14860 case OMPC_proc_bind: 14861 case OMPC_private: 14862 case OMPC_firstprivate: 14863 case OMPC_lastprivate: 14864 case OMPC_shared: 14865 case OMPC_reduction: 14866 case OMPC_task_reduction: 14867 case OMPC_in_reduction: 14868 case OMPC_linear: 14869 case OMPC_aligned: 14870 case OMPC_copyin: 14871 case OMPC_copyprivate: 14872 case OMPC_ordered: 14873 case OMPC_nowait: 14874 case OMPC_untied: 14875 case OMPC_mergeable: 14876 case OMPC_threadprivate: 14877 case OMPC_allocate: 14878 case OMPC_flush: 14879 case OMPC_depobj: 14880 case OMPC_read: 14881 case OMPC_write: 14882 case OMPC_update: 14883 case OMPC_capture: 14884 case OMPC_seq_cst: 14885 case OMPC_acq_rel: 14886 case OMPC_acquire: 14887 case OMPC_release: 14888 case OMPC_relaxed: 14889 case OMPC_depend: 14890 case OMPC_threads: 14891 case OMPC_simd: 14892 case OMPC_map: 14893 case OMPC_num_teams: 14894 case OMPC_thread_limit: 14895 case OMPC_priority: 14896 case OMPC_grainsize: 14897 case OMPC_nogroup: 14898 case OMPC_num_tasks: 14899 case OMPC_hint: 14900 case OMPC_unknown: 14901 case OMPC_uniform: 14902 case OMPC_to: 14903 case OMPC_from: 14904 case OMPC_use_device_ptr: 14905 case OMPC_use_device_addr: 14906 case OMPC_is_device_ptr: 14907 case OMPC_unified_address: 14908 case OMPC_unified_shared_memory: 14909 case OMPC_reverse_offload: 14910 case OMPC_dynamic_allocators: 14911 case OMPC_atomic_default_mem_order: 14912 case OMPC_device_type: 14913 case OMPC_match: 14914 case OMPC_nontemporal: 14915 case OMPC_order: 14916 case OMPC_destroy: 14917 case OMPC_novariants: 14918 case OMPC_nocontext: 14919 case OMPC_detach: 14920 case OMPC_inclusive: 14921 case OMPC_exclusive: 14922 case OMPC_uses_allocators: 14923 case OMPC_affinity: 14924 case OMPC_when: 14925 default: 14926 llvm_unreachable("Clause is not allowed."); 14927 } 14928 return Res; 14929 } 14930 14931 static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1, 14932 OpenMPScheduleClauseModifier M2, 14933 SourceLocation M1Loc, SourceLocation M2Loc) { 14934 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) { 14935 SmallVector<unsigned, 2> Excluded; 14936 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown) 14937 Excluded.push_back(M2); 14938 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) 14939 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic); 14940 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic) 14941 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic); 14942 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value) 14943 << getListOfPossibleValues(OMPC_schedule, 14944 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1, 14945 /*Last=*/OMPC_SCHEDULE_MODIFIER_last, 14946 Excluded) 14947 << getOpenMPClauseName(OMPC_schedule); 14948 return true; 14949 } 14950 return false; 14951 } 14952 14953 OMPClause *Sema::ActOnOpenMPScheduleClause( 14954 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2, 14955 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, 14956 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc, 14957 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) { 14958 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) || 14959 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc)) 14960 return nullptr; 14961 // OpenMP, 2.7.1, Loop Construct, Restrictions 14962 // Either the monotonic modifier or the nonmonotonic modifier can be specified 14963 // but not both. 14964 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) || 14965 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic && 14966 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) || 14967 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic && 14968 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) { 14969 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier) 14970 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2) 14971 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1); 14972 return nullptr; 14973 } 14974 if (Kind == OMPC_SCHEDULE_unknown) { 14975 std::string Values; 14976 if (M1Loc.isInvalid() && M2Loc.isInvalid()) { 14977 unsigned Exclude[] = {OMPC_SCHEDULE_unknown}; 14978 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0, 14979 /*Last=*/OMPC_SCHEDULE_MODIFIER_last, 14980 Exclude); 14981 } else { 14982 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0, 14983 /*Last=*/OMPC_SCHEDULE_unknown); 14984 } 14985 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 14986 << Values << getOpenMPClauseName(OMPC_schedule); 14987 return nullptr; 14988 } 14989 // OpenMP, 2.7.1, Loop Construct, Restrictions 14990 // The nonmonotonic modifier can only be specified with schedule(dynamic) or 14991 // schedule(guided). 14992 // OpenMP 5.0 does not have this restriction. 14993 if (LangOpts.OpenMP < 50 && 14994 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic || 14995 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) && 14996 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) { 14997 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc, 14998 diag::err_omp_schedule_nonmonotonic_static); 14999 return nullptr; 15000 } 15001 Expr *ValExpr = ChunkSize; 15002 Stmt *HelperValStmt = nullptr; 15003 if (ChunkSize) { 15004 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && 15005 !ChunkSize->isInstantiationDependent() && 15006 !ChunkSize->containsUnexpandedParameterPack()) { 15007 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc(); 15008 ExprResult Val = 15009 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); 15010 if (Val.isInvalid()) 15011 return nullptr; 15012 15013 ValExpr = Val.get(); 15014 15015 // OpenMP [2.7.1, Restrictions] 15016 // chunk_size must be a loop invariant integer expression with a positive 15017 // value. 15018 if (Optional<llvm::APSInt> Result = 15019 ValExpr->getIntegerConstantExpr(Context)) { 15020 if (Result->isSigned() && !Result->isStrictlyPositive()) { 15021 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) 15022 << "schedule" << 1 << ChunkSize->getSourceRange(); 15023 return nullptr; 15024 } 15025 } else if (getOpenMPCaptureRegionForClause( 15026 DSAStack->getCurrentDirective(), OMPC_schedule, 15027 LangOpts.OpenMP) != OMPD_unknown && 15028 !CurContext->isDependentContext()) { 15029 ValExpr = MakeFullExpr(ValExpr).get(); 15030 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 15031 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 15032 HelperValStmt = buildPreInits(Context, Captures); 15033 } 15034 } 15035 } 15036 15037 return new (Context) 15038 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind, 15039 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc); 15040 } 15041 15042 OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind, 15043 SourceLocation StartLoc, 15044 SourceLocation EndLoc) { 15045 OMPClause *Res = nullptr; 15046 switch (Kind) { 15047 case OMPC_ordered: 15048 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc); 15049 break; 15050 case OMPC_nowait: 15051 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc); 15052 break; 15053 case OMPC_untied: 15054 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc); 15055 break; 15056 case OMPC_mergeable: 15057 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc); 15058 break; 15059 case OMPC_read: 15060 Res = ActOnOpenMPReadClause(StartLoc, EndLoc); 15061 break; 15062 case OMPC_write: 15063 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc); 15064 break; 15065 case OMPC_update: 15066 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc); 15067 break; 15068 case OMPC_capture: 15069 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc); 15070 break; 15071 case OMPC_seq_cst: 15072 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc); 15073 break; 15074 case OMPC_acq_rel: 15075 Res = ActOnOpenMPAcqRelClause(StartLoc, EndLoc); 15076 break; 15077 case OMPC_acquire: 15078 Res = ActOnOpenMPAcquireClause(StartLoc, EndLoc); 15079 break; 15080 case OMPC_release: 15081 Res = ActOnOpenMPReleaseClause(StartLoc, EndLoc); 15082 break; 15083 case OMPC_relaxed: 15084 Res = ActOnOpenMPRelaxedClause(StartLoc, EndLoc); 15085 break; 15086 case OMPC_threads: 15087 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc); 15088 break; 15089 case OMPC_simd: 15090 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc); 15091 break; 15092 case OMPC_nogroup: 15093 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc); 15094 break; 15095 case OMPC_unified_address: 15096 Res = ActOnOpenMPUnifiedAddressClause(StartLoc, EndLoc); 15097 break; 15098 case OMPC_unified_shared_memory: 15099 Res = ActOnOpenMPUnifiedSharedMemoryClause(StartLoc, EndLoc); 15100 break; 15101 case OMPC_reverse_offload: 15102 Res = ActOnOpenMPReverseOffloadClause(StartLoc, EndLoc); 15103 break; 15104 case OMPC_dynamic_allocators: 15105 Res = ActOnOpenMPDynamicAllocatorsClause(StartLoc, EndLoc); 15106 break; 15107 case OMPC_destroy: 15108 Res = ActOnOpenMPDestroyClause(/*InteropVar=*/nullptr, StartLoc, 15109 /*LParenLoc=*/SourceLocation(), 15110 /*VarLoc=*/SourceLocation(), EndLoc); 15111 break; 15112 case OMPC_full: 15113 Res = ActOnOpenMPFullClause(StartLoc, EndLoc); 15114 break; 15115 case OMPC_partial: 15116 Res = ActOnOpenMPPartialClause(nullptr, StartLoc, /*LParenLoc=*/{}, EndLoc); 15117 break; 15118 case OMPC_if: 15119 case OMPC_final: 15120 case OMPC_num_threads: 15121 case OMPC_safelen: 15122 case OMPC_simdlen: 15123 case OMPC_sizes: 15124 case OMPC_allocator: 15125 case OMPC_collapse: 15126 case OMPC_schedule: 15127 case OMPC_private: 15128 case OMPC_firstprivate: 15129 case OMPC_lastprivate: 15130 case OMPC_shared: 15131 case OMPC_reduction: 15132 case OMPC_task_reduction: 15133 case OMPC_in_reduction: 15134 case OMPC_linear: 15135 case OMPC_aligned: 15136 case OMPC_copyin: 15137 case OMPC_copyprivate: 15138 case OMPC_default: 15139 case OMPC_proc_bind: 15140 case OMPC_threadprivate: 15141 case OMPC_allocate: 15142 case OMPC_flush: 15143 case OMPC_depobj: 15144 case OMPC_depend: 15145 case OMPC_device: 15146 case OMPC_map: 15147 case OMPC_num_teams: 15148 case OMPC_thread_limit: 15149 case OMPC_priority: 15150 case OMPC_grainsize: 15151 case OMPC_num_tasks: 15152 case OMPC_hint: 15153 case OMPC_dist_schedule: 15154 case OMPC_defaultmap: 15155 case OMPC_unknown: 15156 case OMPC_uniform: 15157 case OMPC_to: 15158 case OMPC_from: 15159 case OMPC_use_device_ptr: 15160 case OMPC_use_device_addr: 15161 case OMPC_is_device_ptr: 15162 case OMPC_atomic_default_mem_order: 15163 case OMPC_device_type: 15164 case OMPC_match: 15165 case OMPC_nontemporal: 15166 case OMPC_order: 15167 case OMPC_novariants: 15168 case OMPC_nocontext: 15169 case OMPC_detach: 15170 case OMPC_inclusive: 15171 case OMPC_exclusive: 15172 case OMPC_uses_allocators: 15173 case OMPC_affinity: 15174 case OMPC_when: 15175 default: 15176 llvm_unreachable("Clause is not allowed."); 15177 } 15178 return Res; 15179 } 15180 15181 OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc, 15182 SourceLocation EndLoc) { 15183 DSAStack->setNowaitRegion(); 15184 return new (Context) OMPNowaitClause(StartLoc, EndLoc); 15185 } 15186 15187 OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc, 15188 SourceLocation EndLoc) { 15189 return new (Context) OMPUntiedClause(StartLoc, EndLoc); 15190 } 15191 15192 OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc, 15193 SourceLocation EndLoc) { 15194 return new (Context) OMPMergeableClause(StartLoc, EndLoc); 15195 } 15196 15197 OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc, 15198 SourceLocation EndLoc) { 15199 return new (Context) OMPReadClause(StartLoc, EndLoc); 15200 } 15201 15202 OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc, 15203 SourceLocation EndLoc) { 15204 return new (Context) OMPWriteClause(StartLoc, EndLoc); 15205 } 15206 15207 OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc, 15208 SourceLocation EndLoc) { 15209 return OMPUpdateClause::Create(Context, StartLoc, EndLoc); 15210 } 15211 15212 OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc, 15213 SourceLocation EndLoc) { 15214 return new (Context) OMPCaptureClause(StartLoc, EndLoc); 15215 } 15216 15217 OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc, 15218 SourceLocation EndLoc) { 15219 return new (Context) OMPSeqCstClause(StartLoc, EndLoc); 15220 } 15221 15222 OMPClause *Sema::ActOnOpenMPAcqRelClause(SourceLocation StartLoc, 15223 SourceLocation EndLoc) { 15224 return new (Context) OMPAcqRelClause(StartLoc, EndLoc); 15225 } 15226 15227 OMPClause *Sema::ActOnOpenMPAcquireClause(SourceLocation StartLoc, 15228 SourceLocation EndLoc) { 15229 return new (Context) OMPAcquireClause(StartLoc, EndLoc); 15230 } 15231 15232 OMPClause *Sema::ActOnOpenMPReleaseClause(SourceLocation StartLoc, 15233 SourceLocation EndLoc) { 15234 return new (Context) OMPReleaseClause(StartLoc, EndLoc); 15235 } 15236 15237 OMPClause *Sema::ActOnOpenMPRelaxedClause(SourceLocation StartLoc, 15238 SourceLocation EndLoc) { 15239 return new (Context) OMPRelaxedClause(StartLoc, EndLoc); 15240 } 15241 15242 OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc, 15243 SourceLocation EndLoc) { 15244 return new (Context) OMPThreadsClause(StartLoc, EndLoc); 15245 } 15246 15247 OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc, 15248 SourceLocation EndLoc) { 15249 return new (Context) OMPSIMDClause(StartLoc, EndLoc); 15250 } 15251 15252 OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc, 15253 SourceLocation EndLoc) { 15254 return new (Context) OMPNogroupClause(StartLoc, EndLoc); 15255 } 15256 15257 OMPClause *Sema::ActOnOpenMPUnifiedAddressClause(SourceLocation StartLoc, 15258 SourceLocation EndLoc) { 15259 return new (Context) OMPUnifiedAddressClause(StartLoc, EndLoc); 15260 } 15261 15262 OMPClause *Sema::ActOnOpenMPUnifiedSharedMemoryClause(SourceLocation StartLoc, 15263 SourceLocation EndLoc) { 15264 return new (Context) OMPUnifiedSharedMemoryClause(StartLoc, EndLoc); 15265 } 15266 15267 OMPClause *Sema::ActOnOpenMPReverseOffloadClause(SourceLocation StartLoc, 15268 SourceLocation EndLoc) { 15269 return new (Context) OMPReverseOffloadClause(StartLoc, EndLoc); 15270 } 15271 15272 OMPClause *Sema::ActOnOpenMPDynamicAllocatorsClause(SourceLocation StartLoc, 15273 SourceLocation EndLoc) { 15274 return new (Context) OMPDynamicAllocatorsClause(StartLoc, EndLoc); 15275 } 15276 15277 StmtResult Sema::ActOnOpenMPInteropDirective(ArrayRef<OMPClause *> Clauses, 15278 SourceLocation StartLoc, 15279 SourceLocation EndLoc) { 15280 15281 // OpenMP 5.1 [2.15.1, interop Construct, Restrictions] 15282 // At least one action-clause must appear on a directive. 15283 if (!hasClauses(Clauses, OMPC_init, OMPC_use, OMPC_destroy, OMPC_nowait)) { 15284 StringRef Expected = "'init', 'use', 'destroy', or 'nowait'"; 15285 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 15286 << Expected << getOpenMPDirectiveName(OMPD_interop); 15287 return StmtError(); 15288 } 15289 15290 // OpenMP 5.1 [2.15.1, interop Construct, Restrictions] 15291 // A depend clause can only appear on the directive if a targetsync 15292 // interop-type is present or the interop-var was initialized with 15293 // the targetsync interop-type. 15294 15295 // If there is any 'init' clause diagnose if there is no 'init' clause with 15296 // interop-type of 'targetsync'. Cases involving other directives cannot be 15297 // diagnosed. 15298 const OMPDependClause *DependClause = nullptr; 15299 bool HasInitClause = false; 15300 bool IsTargetSync = false; 15301 for (const OMPClause *C : Clauses) { 15302 if (IsTargetSync) 15303 break; 15304 if (const auto *InitClause = dyn_cast<OMPInitClause>(C)) { 15305 HasInitClause = true; 15306 if (InitClause->getIsTargetSync()) 15307 IsTargetSync = true; 15308 } else if (const auto *DC = dyn_cast<OMPDependClause>(C)) { 15309 DependClause = DC; 15310 } 15311 } 15312 if (DependClause && HasInitClause && !IsTargetSync) { 15313 Diag(DependClause->getBeginLoc(), diag::err_omp_interop_bad_depend_clause); 15314 return StmtError(); 15315 } 15316 15317 // OpenMP 5.1 [2.15.1, interop Construct, Restrictions] 15318 // Each interop-var may be specified for at most one action-clause of each 15319 // interop construct. 15320 llvm::SmallPtrSet<const VarDecl *, 4> InteropVars; 15321 for (const OMPClause *C : Clauses) { 15322 OpenMPClauseKind ClauseKind = C->getClauseKind(); 15323 const DeclRefExpr *DRE = nullptr; 15324 SourceLocation VarLoc; 15325 15326 if (ClauseKind == OMPC_init) { 15327 const auto *IC = cast<OMPInitClause>(C); 15328 VarLoc = IC->getVarLoc(); 15329 DRE = dyn_cast_or_null<DeclRefExpr>(IC->getInteropVar()); 15330 } else if (ClauseKind == OMPC_use) { 15331 const auto *UC = cast<OMPUseClause>(C); 15332 VarLoc = UC->getVarLoc(); 15333 DRE = dyn_cast_or_null<DeclRefExpr>(UC->getInteropVar()); 15334 } else if (ClauseKind == OMPC_destroy) { 15335 const auto *DC = cast<OMPDestroyClause>(C); 15336 VarLoc = DC->getVarLoc(); 15337 DRE = dyn_cast_or_null<DeclRefExpr>(DC->getInteropVar()); 15338 } 15339 15340 if (!DRE) 15341 continue; 15342 15343 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) { 15344 if (!InteropVars.insert(VD->getCanonicalDecl()).second) { 15345 Diag(VarLoc, diag::err_omp_interop_var_multiple_actions) << VD; 15346 return StmtError(); 15347 } 15348 } 15349 } 15350 15351 return OMPInteropDirective::Create(Context, StartLoc, EndLoc, Clauses); 15352 } 15353 15354 static bool isValidInteropVariable(Sema &SemaRef, Expr *InteropVarExpr, 15355 SourceLocation VarLoc, 15356 OpenMPClauseKind Kind) { 15357 if (InteropVarExpr->isValueDependent() || InteropVarExpr->isTypeDependent() || 15358 InteropVarExpr->isInstantiationDependent() || 15359 InteropVarExpr->containsUnexpandedParameterPack()) 15360 return true; 15361 15362 const auto *DRE = dyn_cast<DeclRefExpr>(InteropVarExpr); 15363 if (!DRE || !isa<VarDecl>(DRE->getDecl())) { 15364 SemaRef.Diag(VarLoc, diag::err_omp_interop_variable_expected) << 0; 15365 return false; 15366 } 15367 15368 // Interop variable should be of type omp_interop_t. 15369 bool HasError = false; 15370 QualType InteropType; 15371 LookupResult Result(SemaRef, &SemaRef.Context.Idents.get("omp_interop_t"), 15372 VarLoc, Sema::LookupOrdinaryName); 15373 if (SemaRef.LookupName(Result, SemaRef.getCurScope())) { 15374 NamedDecl *ND = Result.getFoundDecl(); 15375 if (const auto *TD = dyn_cast<TypeDecl>(ND)) { 15376 InteropType = QualType(TD->getTypeForDecl(), 0); 15377 } else { 15378 HasError = true; 15379 } 15380 } else { 15381 HasError = true; 15382 } 15383 15384 if (HasError) { 15385 SemaRef.Diag(VarLoc, diag::err_omp_implied_type_not_found) 15386 << "omp_interop_t"; 15387 return false; 15388 } 15389 15390 QualType VarType = InteropVarExpr->getType().getUnqualifiedType(); 15391 if (!SemaRef.Context.hasSameType(InteropType, VarType)) { 15392 SemaRef.Diag(VarLoc, diag::err_omp_interop_variable_wrong_type); 15393 return false; 15394 } 15395 15396 // OpenMP 5.1 [2.15.1, interop Construct, Restrictions] 15397 // The interop-var passed to init or destroy must be non-const. 15398 if ((Kind == OMPC_init || Kind == OMPC_destroy) && 15399 isConstNotMutableType(SemaRef, InteropVarExpr->getType())) { 15400 SemaRef.Diag(VarLoc, diag::err_omp_interop_variable_expected) 15401 << /*non-const*/ 1; 15402 return false; 15403 } 15404 return true; 15405 } 15406 15407 OMPClause * 15408 Sema::ActOnOpenMPInitClause(Expr *InteropVar, ArrayRef<Expr *> PrefExprs, 15409 bool IsTarget, bool IsTargetSync, 15410 SourceLocation StartLoc, SourceLocation LParenLoc, 15411 SourceLocation VarLoc, SourceLocation EndLoc) { 15412 15413 if (!isValidInteropVariable(*this, InteropVar, VarLoc, OMPC_init)) 15414 return nullptr; 15415 15416 // Check prefer_type values. These foreign-runtime-id values are either 15417 // string literals or constant integral expressions. 15418 for (const Expr *E : PrefExprs) { 15419 if (E->isValueDependent() || E->isTypeDependent() || 15420 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 15421 continue; 15422 if (E->isIntegerConstantExpr(Context)) 15423 continue; 15424 if (isa<StringLiteral>(E)) 15425 continue; 15426 Diag(E->getExprLoc(), diag::err_omp_interop_prefer_type); 15427 return nullptr; 15428 } 15429 15430 return OMPInitClause::Create(Context, InteropVar, PrefExprs, IsTarget, 15431 IsTargetSync, StartLoc, LParenLoc, VarLoc, 15432 EndLoc); 15433 } 15434 15435 OMPClause *Sema::ActOnOpenMPUseClause(Expr *InteropVar, SourceLocation StartLoc, 15436 SourceLocation LParenLoc, 15437 SourceLocation VarLoc, 15438 SourceLocation EndLoc) { 15439 15440 if (!isValidInteropVariable(*this, InteropVar, VarLoc, OMPC_use)) 15441 return nullptr; 15442 15443 return new (Context) 15444 OMPUseClause(InteropVar, StartLoc, LParenLoc, VarLoc, EndLoc); 15445 } 15446 15447 OMPClause *Sema::ActOnOpenMPDestroyClause(Expr *InteropVar, 15448 SourceLocation StartLoc, 15449 SourceLocation LParenLoc, 15450 SourceLocation VarLoc, 15451 SourceLocation EndLoc) { 15452 if (InteropVar && 15453 !isValidInteropVariable(*this, InteropVar, VarLoc, OMPC_destroy)) 15454 return nullptr; 15455 15456 return new (Context) 15457 OMPDestroyClause(InteropVar, StartLoc, LParenLoc, VarLoc, EndLoc); 15458 } 15459 15460 OMPClause *Sema::ActOnOpenMPNovariantsClause(Expr *Condition, 15461 SourceLocation StartLoc, 15462 SourceLocation LParenLoc, 15463 SourceLocation EndLoc) { 15464 Expr *ValExpr = Condition; 15465 Stmt *HelperValStmt = nullptr; 15466 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 15467 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 15468 !Condition->isInstantiationDependent() && 15469 !Condition->containsUnexpandedParameterPack()) { 15470 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 15471 if (Val.isInvalid()) 15472 return nullptr; 15473 15474 ValExpr = MakeFullExpr(Val.get()).get(); 15475 15476 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 15477 CaptureRegion = getOpenMPCaptureRegionForClause(DKind, OMPC_novariants, 15478 LangOpts.OpenMP); 15479 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 15480 ValExpr = MakeFullExpr(ValExpr).get(); 15481 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 15482 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 15483 HelperValStmt = buildPreInits(Context, Captures); 15484 } 15485 } 15486 15487 return new (Context) OMPNovariantsClause( 15488 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc); 15489 } 15490 15491 OMPClause *Sema::ActOnOpenMPNocontextClause(Expr *Condition, 15492 SourceLocation StartLoc, 15493 SourceLocation LParenLoc, 15494 SourceLocation EndLoc) { 15495 Expr *ValExpr = Condition; 15496 Stmt *HelperValStmt = nullptr; 15497 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 15498 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 15499 !Condition->isInstantiationDependent() && 15500 !Condition->containsUnexpandedParameterPack()) { 15501 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 15502 if (Val.isInvalid()) 15503 return nullptr; 15504 15505 ValExpr = MakeFullExpr(Val.get()).get(); 15506 15507 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 15508 CaptureRegion = 15509 getOpenMPCaptureRegionForClause(DKind, OMPC_nocontext, LangOpts.OpenMP); 15510 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 15511 ValExpr = MakeFullExpr(ValExpr).get(); 15512 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 15513 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 15514 HelperValStmt = buildPreInits(Context, Captures); 15515 } 15516 } 15517 15518 return new (Context) OMPNocontextClause(ValExpr, HelperValStmt, CaptureRegion, 15519 StartLoc, LParenLoc, EndLoc); 15520 } 15521 15522 OMPClause *Sema::ActOnOpenMPFilterClause(Expr *ThreadID, 15523 SourceLocation StartLoc, 15524 SourceLocation LParenLoc, 15525 SourceLocation EndLoc) { 15526 Expr *ValExpr = ThreadID; 15527 Stmt *HelperValStmt = nullptr; 15528 15529 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 15530 OpenMPDirectiveKind CaptureRegion = 15531 getOpenMPCaptureRegionForClause(DKind, OMPC_filter, LangOpts.OpenMP); 15532 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 15533 ValExpr = MakeFullExpr(ValExpr).get(); 15534 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 15535 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 15536 HelperValStmt = buildPreInits(Context, Captures); 15537 } 15538 15539 return new (Context) OMPFilterClause(ValExpr, HelperValStmt, CaptureRegion, 15540 StartLoc, LParenLoc, EndLoc); 15541 } 15542 15543 OMPClause *Sema::ActOnOpenMPVarListClause( 15544 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *DepModOrTailExpr, 15545 const OMPVarListLocTy &Locs, SourceLocation ColonLoc, 15546 CXXScopeSpec &ReductionOrMapperIdScopeSpec, 15547 DeclarationNameInfo &ReductionOrMapperId, int ExtraModifier, 15548 ArrayRef<OpenMPMapModifierKind> MapTypeModifiers, 15549 ArrayRef<SourceLocation> MapTypeModifiersLoc, bool IsMapTypeImplicit, 15550 SourceLocation ExtraModifierLoc, 15551 ArrayRef<OpenMPMotionModifierKind> MotionModifiers, 15552 ArrayRef<SourceLocation> MotionModifiersLoc) { 15553 SourceLocation StartLoc = Locs.StartLoc; 15554 SourceLocation LParenLoc = Locs.LParenLoc; 15555 SourceLocation EndLoc = Locs.EndLoc; 15556 OMPClause *Res = nullptr; 15557 switch (Kind) { 15558 case OMPC_private: 15559 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc); 15560 break; 15561 case OMPC_firstprivate: 15562 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 15563 break; 15564 case OMPC_lastprivate: 15565 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_LASTPRIVATE_unknown && 15566 "Unexpected lastprivate modifier."); 15567 Res = ActOnOpenMPLastprivateClause( 15568 VarList, static_cast<OpenMPLastprivateModifier>(ExtraModifier), 15569 ExtraModifierLoc, ColonLoc, StartLoc, LParenLoc, EndLoc); 15570 break; 15571 case OMPC_shared: 15572 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc); 15573 break; 15574 case OMPC_reduction: 15575 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_REDUCTION_unknown && 15576 "Unexpected lastprivate modifier."); 15577 Res = ActOnOpenMPReductionClause( 15578 VarList, static_cast<OpenMPReductionClauseModifier>(ExtraModifier), 15579 StartLoc, LParenLoc, ExtraModifierLoc, ColonLoc, EndLoc, 15580 ReductionOrMapperIdScopeSpec, ReductionOrMapperId); 15581 break; 15582 case OMPC_task_reduction: 15583 Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 15584 EndLoc, ReductionOrMapperIdScopeSpec, 15585 ReductionOrMapperId); 15586 break; 15587 case OMPC_in_reduction: 15588 Res = ActOnOpenMPInReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 15589 EndLoc, ReductionOrMapperIdScopeSpec, 15590 ReductionOrMapperId); 15591 break; 15592 case OMPC_linear: 15593 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_LINEAR_unknown && 15594 "Unexpected linear modifier."); 15595 Res = ActOnOpenMPLinearClause( 15596 VarList, DepModOrTailExpr, StartLoc, LParenLoc, 15597 static_cast<OpenMPLinearClauseKind>(ExtraModifier), ExtraModifierLoc, 15598 ColonLoc, EndLoc); 15599 break; 15600 case OMPC_aligned: 15601 Res = ActOnOpenMPAlignedClause(VarList, DepModOrTailExpr, StartLoc, 15602 LParenLoc, ColonLoc, EndLoc); 15603 break; 15604 case OMPC_copyin: 15605 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc); 15606 break; 15607 case OMPC_copyprivate: 15608 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 15609 break; 15610 case OMPC_flush: 15611 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc); 15612 break; 15613 case OMPC_depend: 15614 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_DEPEND_unknown && 15615 "Unexpected depend modifier."); 15616 Res = ActOnOpenMPDependClause( 15617 DepModOrTailExpr, static_cast<OpenMPDependClauseKind>(ExtraModifier), 15618 ExtraModifierLoc, ColonLoc, VarList, StartLoc, LParenLoc, EndLoc); 15619 break; 15620 case OMPC_map: 15621 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_MAP_unknown && 15622 "Unexpected map modifier."); 15623 Res = ActOnOpenMPMapClause( 15624 MapTypeModifiers, MapTypeModifiersLoc, ReductionOrMapperIdScopeSpec, 15625 ReductionOrMapperId, static_cast<OpenMPMapClauseKind>(ExtraModifier), 15626 IsMapTypeImplicit, ExtraModifierLoc, ColonLoc, VarList, Locs); 15627 break; 15628 case OMPC_to: 15629 Res = ActOnOpenMPToClause(MotionModifiers, MotionModifiersLoc, 15630 ReductionOrMapperIdScopeSpec, ReductionOrMapperId, 15631 ColonLoc, VarList, Locs); 15632 break; 15633 case OMPC_from: 15634 Res = ActOnOpenMPFromClause(MotionModifiers, MotionModifiersLoc, 15635 ReductionOrMapperIdScopeSpec, 15636 ReductionOrMapperId, ColonLoc, VarList, Locs); 15637 break; 15638 case OMPC_use_device_ptr: 15639 Res = ActOnOpenMPUseDevicePtrClause(VarList, Locs); 15640 break; 15641 case OMPC_use_device_addr: 15642 Res = ActOnOpenMPUseDeviceAddrClause(VarList, Locs); 15643 break; 15644 case OMPC_is_device_ptr: 15645 Res = ActOnOpenMPIsDevicePtrClause(VarList, Locs); 15646 break; 15647 case OMPC_allocate: 15648 Res = ActOnOpenMPAllocateClause(DepModOrTailExpr, VarList, StartLoc, 15649 LParenLoc, ColonLoc, EndLoc); 15650 break; 15651 case OMPC_nontemporal: 15652 Res = ActOnOpenMPNontemporalClause(VarList, StartLoc, LParenLoc, EndLoc); 15653 break; 15654 case OMPC_inclusive: 15655 Res = ActOnOpenMPInclusiveClause(VarList, StartLoc, LParenLoc, EndLoc); 15656 break; 15657 case OMPC_exclusive: 15658 Res = ActOnOpenMPExclusiveClause(VarList, StartLoc, LParenLoc, EndLoc); 15659 break; 15660 case OMPC_affinity: 15661 Res = ActOnOpenMPAffinityClause(StartLoc, LParenLoc, ColonLoc, EndLoc, 15662 DepModOrTailExpr, VarList); 15663 break; 15664 case OMPC_if: 15665 case OMPC_depobj: 15666 case OMPC_final: 15667 case OMPC_num_threads: 15668 case OMPC_safelen: 15669 case OMPC_simdlen: 15670 case OMPC_sizes: 15671 case OMPC_allocator: 15672 case OMPC_collapse: 15673 case OMPC_default: 15674 case OMPC_proc_bind: 15675 case OMPC_schedule: 15676 case OMPC_ordered: 15677 case OMPC_nowait: 15678 case OMPC_untied: 15679 case OMPC_mergeable: 15680 case OMPC_threadprivate: 15681 case OMPC_read: 15682 case OMPC_write: 15683 case OMPC_update: 15684 case OMPC_capture: 15685 case OMPC_seq_cst: 15686 case OMPC_acq_rel: 15687 case OMPC_acquire: 15688 case OMPC_release: 15689 case OMPC_relaxed: 15690 case OMPC_device: 15691 case OMPC_threads: 15692 case OMPC_simd: 15693 case OMPC_num_teams: 15694 case OMPC_thread_limit: 15695 case OMPC_priority: 15696 case OMPC_grainsize: 15697 case OMPC_nogroup: 15698 case OMPC_num_tasks: 15699 case OMPC_hint: 15700 case OMPC_dist_schedule: 15701 case OMPC_defaultmap: 15702 case OMPC_unknown: 15703 case OMPC_uniform: 15704 case OMPC_unified_address: 15705 case OMPC_unified_shared_memory: 15706 case OMPC_reverse_offload: 15707 case OMPC_dynamic_allocators: 15708 case OMPC_atomic_default_mem_order: 15709 case OMPC_device_type: 15710 case OMPC_match: 15711 case OMPC_order: 15712 case OMPC_destroy: 15713 case OMPC_novariants: 15714 case OMPC_nocontext: 15715 case OMPC_detach: 15716 case OMPC_uses_allocators: 15717 case OMPC_when: 15718 default: 15719 llvm_unreachable("Clause is not allowed."); 15720 } 15721 return Res; 15722 } 15723 15724 ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK, 15725 ExprObjectKind OK, SourceLocation Loc) { 15726 ExprResult Res = BuildDeclRefExpr( 15727 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc); 15728 if (!Res.isUsable()) 15729 return ExprError(); 15730 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) { 15731 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get()); 15732 if (!Res.isUsable()) 15733 return ExprError(); 15734 } 15735 if (VK != VK_LValue && Res.get()->isGLValue()) { 15736 Res = DefaultLvalueConversion(Res.get()); 15737 if (!Res.isUsable()) 15738 return ExprError(); 15739 } 15740 return Res; 15741 } 15742 15743 OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList, 15744 SourceLocation StartLoc, 15745 SourceLocation LParenLoc, 15746 SourceLocation EndLoc) { 15747 SmallVector<Expr *, 8> Vars; 15748 SmallVector<Expr *, 8> PrivateCopies; 15749 for (Expr *RefExpr : VarList) { 15750 assert(RefExpr && "NULL expr in OpenMP private clause."); 15751 SourceLocation ELoc; 15752 SourceRange ERange; 15753 Expr *SimpleRefExpr = RefExpr; 15754 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 15755 if (Res.second) { 15756 // It will be analyzed later. 15757 Vars.push_back(RefExpr); 15758 PrivateCopies.push_back(nullptr); 15759 } 15760 ValueDecl *D = Res.first; 15761 if (!D) 15762 continue; 15763 15764 QualType Type = D->getType(); 15765 auto *VD = dyn_cast<VarDecl>(D); 15766 15767 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 15768 // A variable that appears in a private clause must not have an incomplete 15769 // type or a reference type. 15770 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type)) 15771 continue; 15772 Type = Type.getNonReferenceType(); 15773 15774 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions] 15775 // A variable that is privatized must not have a const-qualified type 15776 // unless it is of class type with a mutable member. This restriction does 15777 // not apply to the firstprivate clause. 15778 // 15779 // OpenMP 3.1 [2.9.3.3, private clause, Restrictions] 15780 // A variable that appears in a private clause must not have a 15781 // const-qualified type unless it is of class type with a mutable member. 15782 if (rejectConstNotMutableType(*this, D, Type, OMPC_private, ELoc)) 15783 continue; 15784 15785 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 15786 // in a Construct] 15787 // Variables with the predetermined data-sharing attributes may not be 15788 // listed in data-sharing attributes clauses, except for the cases 15789 // listed below. For these exceptions only, listing a predetermined 15790 // variable in a data-sharing attribute clause is allowed and overrides 15791 // the variable's predetermined data-sharing attributes. 15792 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 15793 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) { 15794 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 15795 << getOpenMPClauseName(OMPC_private); 15796 reportOriginalDsa(*this, DSAStack, D, DVar); 15797 continue; 15798 } 15799 15800 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 15801 // Variably modified types are not supported for tasks. 15802 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && 15803 isOpenMPTaskingDirective(CurrDir)) { 15804 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 15805 << getOpenMPClauseName(OMPC_private) << Type 15806 << getOpenMPDirectiveName(CurrDir); 15807 bool IsDecl = 15808 !VD || 15809 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 15810 Diag(D->getLocation(), 15811 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 15812 << D; 15813 continue; 15814 } 15815 15816 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 15817 // A list item cannot appear in both a map clause and a data-sharing 15818 // attribute clause on the same construct 15819 // 15820 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7] 15821 // A list item cannot appear in both a map clause and a data-sharing 15822 // attribute clause on the same construct unless the construct is a 15823 // combined construct. 15824 if ((LangOpts.OpenMP <= 45 && isOpenMPTargetExecutionDirective(CurrDir)) || 15825 CurrDir == OMPD_target) { 15826 OpenMPClauseKind ConflictKind; 15827 if (DSAStack->checkMappableExprComponentListsForDecl( 15828 VD, /*CurrentRegionOnly=*/true, 15829 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef, 15830 OpenMPClauseKind WhereFoundClauseKind) -> bool { 15831 ConflictKind = WhereFoundClauseKind; 15832 return true; 15833 })) { 15834 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 15835 << getOpenMPClauseName(OMPC_private) 15836 << getOpenMPClauseName(ConflictKind) 15837 << getOpenMPDirectiveName(CurrDir); 15838 reportOriginalDsa(*this, DSAStack, D, DVar); 15839 continue; 15840 } 15841 } 15842 15843 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1] 15844 // A variable of class type (or array thereof) that appears in a private 15845 // clause requires an accessible, unambiguous default constructor for the 15846 // class type. 15847 // Generate helper private variable and initialize it with the default 15848 // value. The address of the original variable is replaced by the address of 15849 // the new private variable in CodeGen. This new variable is not added to 15850 // IdResolver, so the code in the OpenMP region uses original variable for 15851 // proper diagnostics. 15852 Type = Type.getUnqualifiedType(); 15853 VarDecl *VDPrivate = 15854 buildVarDecl(*this, ELoc, Type, D->getName(), 15855 D->hasAttrs() ? &D->getAttrs() : nullptr, 15856 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 15857 ActOnUninitializedDecl(VDPrivate); 15858 if (VDPrivate->isInvalidDecl()) 15859 continue; 15860 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 15861 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc); 15862 15863 DeclRefExpr *Ref = nullptr; 15864 if (!VD && !CurContext->isDependentContext()) 15865 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 15866 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref); 15867 Vars.push_back((VD || CurContext->isDependentContext()) 15868 ? RefExpr->IgnoreParens() 15869 : Ref); 15870 PrivateCopies.push_back(VDPrivateRefExpr); 15871 } 15872 15873 if (Vars.empty()) 15874 return nullptr; 15875 15876 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 15877 PrivateCopies); 15878 } 15879 15880 OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList, 15881 SourceLocation StartLoc, 15882 SourceLocation LParenLoc, 15883 SourceLocation EndLoc) { 15884 SmallVector<Expr *, 8> Vars; 15885 SmallVector<Expr *, 8> PrivateCopies; 15886 SmallVector<Expr *, 8> Inits; 15887 SmallVector<Decl *, 4> ExprCaptures; 15888 bool IsImplicitClause = 15889 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid(); 15890 SourceLocation ImplicitClauseLoc = DSAStack->getConstructLoc(); 15891 15892 for (Expr *RefExpr : VarList) { 15893 assert(RefExpr && "NULL expr in OpenMP firstprivate clause."); 15894 SourceLocation ELoc; 15895 SourceRange ERange; 15896 Expr *SimpleRefExpr = RefExpr; 15897 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 15898 if (Res.second) { 15899 // It will be analyzed later. 15900 Vars.push_back(RefExpr); 15901 PrivateCopies.push_back(nullptr); 15902 Inits.push_back(nullptr); 15903 } 15904 ValueDecl *D = Res.first; 15905 if (!D) 15906 continue; 15907 15908 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc; 15909 QualType Type = D->getType(); 15910 auto *VD = dyn_cast<VarDecl>(D); 15911 15912 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 15913 // A variable that appears in a private clause must not have an incomplete 15914 // type or a reference type. 15915 if (RequireCompleteType(ELoc, Type, 15916 diag::err_omp_firstprivate_incomplete_type)) 15917 continue; 15918 Type = Type.getNonReferenceType(); 15919 15920 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1] 15921 // A variable of class type (or array thereof) that appears in a private 15922 // clause requires an accessible, unambiguous copy constructor for the 15923 // class type. 15924 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType(); 15925 15926 // If an implicit firstprivate variable found it was checked already. 15927 DSAStackTy::DSAVarData TopDVar; 15928 if (!IsImplicitClause) { 15929 DSAStackTy::DSAVarData DVar = 15930 DSAStack->getTopDSA(D, /*FromParent=*/false); 15931 TopDVar = DVar; 15932 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 15933 bool IsConstant = ElemType.isConstant(Context); 15934 // OpenMP [2.4.13, Data-sharing Attribute Clauses] 15935 // A list item that specifies a given variable may not appear in more 15936 // than one clause on the same directive, except that a variable may be 15937 // specified in both firstprivate and lastprivate clauses. 15938 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3] 15939 // A list item may appear in a firstprivate or lastprivate clause but not 15940 // both. 15941 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate && 15942 (isOpenMPDistributeDirective(CurrDir) || 15943 DVar.CKind != OMPC_lastprivate) && 15944 DVar.RefExpr) { 15945 Diag(ELoc, diag::err_omp_wrong_dsa) 15946 << getOpenMPClauseName(DVar.CKind) 15947 << getOpenMPClauseName(OMPC_firstprivate); 15948 reportOriginalDsa(*this, DSAStack, D, DVar); 15949 continue; 15950 } 15951 15952 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 15953 // in a Construct] 15954 // Variables with the predetermined data-sharing attributes may not be 15955 // listed in data-sharing attributes clauses, except for the cases 15956 // listed below. For these exceptions only, listing a predetermined 15957 // variable in a data-sharing attribute clause is allowed and overrides 15958 // the variable's predetermined data-sharing attributes. 15959 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 15960 // in a Construct, C/C++, p.2] 15961 // Variables with const-qualified type having no mutable member may be 15962 // listed in a firstprivate clause, even if they are static data members. 15963 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr && 15964 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) { 15965 Diag(ELoc, diag::err_omp_wrong_dsa) 15966 << getOpenMPClauseName(DVar.CKind) 15967 << getOpenMPClauseName(OMPC_firstprivate); 15968 reportOriginalDsa(*this, DSAStack, D, DVar); 15969 continue; 15970 } 15971 15972 // OpenMP [2.9.3.4, Restrictions, p.2] 15973 // A list item that is private within a parallel region must not appear 15974 // in a firstprivate clause on a worksharing construct if any of the 15975 // worksharing regions arising from the worksharing construct ever bind 15976 // to any of the parallel regions arising from the parallel construct. 15977 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3] 15978 // A list item that is private within a teams region must not appear in a 15979 // firstprivate clause on a distribute construct if any of the distribute 15980 // regions arising from the distribute construct ever bind to any of the 15981 // teams regions arising from the teams construct. 15982 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3] 15983 // A list item that appears in a reduction clause of a teams construct 15984 // must not appear in a firstprivate clause on a distribute construct if 15985 // any of the distribute regions arising from the distribute construct 15986 // ever bind to any of the teams regions arising from the teams construct. 15987 if ((isOpenMPWorksharingDirective(CurrDir) || 15988 isOpenMPDistributeDirective(CurrDir)) && 15989 !isOpenMPParallelDirective(CurrDir) && 15990 !isOpenMPTeamsDirective(CurrDir)) { 15991 DVar = DSAStack->getImplicitDSA(D, true); 15992 if (DVar.CKind != OMPC_shared && 15993 (isOpenMPParallelDirective(DVar.DKind) || 15994 isOpenMPTeamsDirective(DVar.DKind) || 15995 DVar.DKind == OMPD_unknown)) { 15996 Diag(ELoc, diag::err_omp_required_access) 15997 << getOpenMPClauseName(OMPC_firstprivate) 15998 << getOpenMPClauseName(OMPC_shared); 15999 reportOriginalDsa(*this, DSAStack, D, DVar); 16000 continue; 16001 } 16002 } 16003 // OpenMP [2.9.3.4, Restrictions, p.3] 16004 // A list item that appears in a reduction clause of a parallel construct 16005 // must not appear in a firstprivate clause on a worksharing or task 16006 // construct if any of the worksharing or task regions arising from the 16007 // worksharing or task construct ever bind to any of the parallel regions 16008 // arising from the parallel construct. 16009 // OpenMP [2.9.3.4, Restrictions, p.4] 16010 // A list item that appears in a reduction clause in worksharing 16011 // construct must not appear in a firstprivate clause in a task construct 16012 // encountered during execution of any of the worksharing regions arising 16013 // from the worksharing construct. 16014 if (isOpenMPTaskingDirective(CurrDir)) { 16015 DVar = DSAStack->hasInnermostDSA( 16016 D, 16017 [](OpenMPClauseKind C, bool AppliedToPointee) { 16018 return C == OMPC_reduction && !AppliedToPointee; 16019 }, 16020 [](OpenMPDirectiveKind K) { 16021 return isOpenMPParallelDirective(K) || 16022 isOpenMPWorksharingDirective(K) || 16023 isOpenMPTeamsDirective(K); 16024 }, 16025 /*FromParent=*/true); 16026 if (DVar.CKind == OMPC_reduction && 16027 (isOpenMPParallelDirective(DVar.DKind) || 16028 isOpenMPWorksharingDirective(DVar.DKind) || 16029 isOpenMPTeamsDirective(DVar.DKind))) { 16030 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate) 16031 << getOpenMPDirectiveName(DVar.DKind); 16032 reportOriginalDsa(*this, DSAStack, D, DVar); 16033 continue; 16034 } 16035 } 16036 16037 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 16038 // A list item cannot appear in both a map clause and a data-sharing 16039 // attribute clause on the same construct 16040 // 16041 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7] 16042 // A list item cannot appear in both a map clause and a data-sharing 16043 // attribute clause on the same construct unless the construct is a 16044 // combined construct. 16045 if ((LangOpts.OpenMP <= 45 && 16046 isOpenMPTargetExecutionDirective(CurrDir)) || 16047 CurrDir == OMPD_target) { 16048 OpenMPClauseKind ConflictKind; 16049 if (DSAStack->checkMappableExprComponentListsForDecl( 16050 VD, /*CurrentRegionOnly=*/true, 16051 [&ConflictKind]( 16052 OMPClauseMappableExprCommon::MappableExprComponentListRef, 16053 OpenMPClauseKind WhereFoundClauseKind) { 16054 ConflictKind = WhereFoundClauseKind; 16055 return true; 16056 })) { 16057 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 16058 << getOpenMPClauseName(OMPC_firstprivate) 16059 << getOpenMPClauseName(ConflictKind) 16060 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 16061 reportOriginalDsa(*this, DSAStack, D, DVar); 16062 continue; 16063 } 16064 } 16065 } 16066 16067 // Variably modified types are not supported for tasks. 16068 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && 16069 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) { 16070 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 16071 << getOpenMPClauseName(OMPC_firstprivate) << Type 16072 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 16073 bool IsDecl = 16074 !VD || 16075 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 16076 Diag(D->getLocation(), 16077 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 16078 << D; 16079 continue; 16080 } 16081 16082 Type = Type.getUnqualifiedType(); 16083 VarDecl *VDPrivate = 16084 buildVarDecl(*this, ELoc, Type, D->getName(), 16085 D->hasAttrs() ? &D->getAttrs() : nullptr, 16086 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 16087 // Generate helper private variable and initialize it with the value of the 16088 // original variable. The address of the original variable is replaced by 16089 // the address of the new private variable in the CodeGen. This new variable 16090 // is not added to IdResolver, so the code in the OpenMP region uses 16091 // original variable for proper diagnostics and variable capturing. 16092 Expr *VDInitRefExpr = nullptr; 16093 // For arrays generate initializer for single element and replace it by the 16094 // original array element in CodeGen. 16095 if (Type->isArrayType()) { 16096 VarDecl *VDInit = 16097 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName()); 16098 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc); 16099 Expr *Init = DefaultLvalueConversion(VDInitRefExpr).get(); 16100 ElemType = ElemType.getUnqualifiedType(); 16101 VarDecl *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, 16102 ".firstprivate.temp"); 16103 InitializedEntity Entity = 16104 InitializedEntity::InitializeVariable(VDInitTemp); 16105 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc); 16106 16107 InitializationSequence InitSeq(*this, Entity, Kind, Init); 16108 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init); 16109 if (Result.isInvalid()) 16110 VDPrivate->setInvalidDecl(); 16111 else 16112 VDPrivate->setInit(Result.getAs<Expr>()); 16113 // Remove temp variable declaration. 16114 Context.Deallocate(VDInitTemp); 16115 } else { 16116 VarDecl *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type, 16117 ".firstprivate.temp"); 16118 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(), 16119 RefExpr->getExprLoc()); 16120 AddInitializerToDecl(VDPrivate, 16121 DefaultLvalueConversion(VDInitRefExpr).get(), 16122 /*DirectInit=*/false); 16123 } 16124 if (VDPrivate->isInvalidDecl()) { 16125 if (IsImplicitClause) { 16126 Diag(RefExpr->getExprLoc(), 16127 diag::note_omp_task_predetermined_firstprivate_here); 16128 } 16129 continue; 16130 } 16131 CurContext->addDecl(VDPrivate); 16132 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 16133 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), 16134 RefExpr->getExprLoc()); 16135 DeclRefExpr *Ref = nullptr; 16136 if (!VD && !CurContext->isDependentContext()) { 16137 if (TopDVar.CKind == OMPC_lastprivate) { 16138 Ref = TopDVar.PrivateCopy; 16139 } else { 16140 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 16141 if (!isOpenMPCapturedDecl(D)) 16142 ExprCaptures.push_back(Ref->getDecl()); 16143 } 16144 } 16145 if (!IsImplicitClause) 16146 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 16147 Vars.push_back((VD || CurContext->isDependentContext()) 16148 ? RefExpr->IgnoreParens() 16149 : Ref); 16150 PrivateCopies.push_back(VDPrivateRefExpr); 16151 Inits.push_back(VDInitRefExpr); 16152 } 16153 16154 if (Vars.empty()) 16155 return nullptr; 16156 16157 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 16158 Vars, PrivateCopies, Inits, 16159 buildPreInits(Context, ExprCaptures)); 16160 } 16161 16162 OMPClause *Sema::ActOnOpenMPLastprivateClause( 16163 ArrayRef<Expr *> VarList, OpenMPLastprivateModifier LPKind, 16164 SourceLocation LPKindLoc, SourceLocation ColonLoc, SourceLocation StartLoc, 16165 SourceLocation LParenLoc, SourceLocation EndLoc) { 16166 if (LPKind == OMPC_LASTPRIVATE_unknown && LPKindLoc.isValid()) { 16167 assert(ColonLoc.isValid() && "Colon location must be valid."); 16168 Diag(LPKindLoc, diag::err_omp_unexpected_clause_value) 16169 << getListOfPossibleValues(OMPC_lastprivate, /*First=*/0, 16170 /*Last=*/OMPC_LASTPRIVATE_unknown) 16171 << getOpenMPClauseName(OMPC_lastprivate); 16172 return nullptr; 16173 } 16174 16175 SmallVector<Expr *, 8> Vars; 16176 SmallVector<Expr *, 8> SrcExprs; 16177 SmallVector<Expr *, 8> DstExprs; 16178 SmallVector<Expr *, 8> AssignmentOps; 16179 SmallVector<Decl *, 4> ExprCaptures; 16180 SmallVector<Expr *, 4> ExprPostUpdates; 16181 for (Expr *RefExpr : VarList) { 16182 assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); 16183 SourceLocation ELoc; 16184 SourceRange ERange; 16185 Expr *SimpleRefExpr = RefExpr; 16186 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 16187 if (Res.second) { 16188 // It will be analyzed later. 16189 Vars.push_back(RefExpr); 16190 SrcExprs.push_back(nullptr); 16191 DstExprs.push_back(nullptr); 16192 AssignmentOps.push_back(nullptr); 16193 } 16194 ValueDecl *D = Res.first; 16195 if (!D) 16196 continue; 16197 16198 QualType Type = D->getType(); 16199 auto *VD = dyn_cast<VarDecl>(D); 16200 16201 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2] 16202 // A variable that appears in a lastprivate clause must not have an 16203 // incomplete type or a reference type. 16204 if (RequireCompleteType(ELoc, Type, 16205 diag::err_omp_lastprivate_incomplete_type)) 16206 continue; 16207 Type = Type.getNonReferenceType(); 16208 16209 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions] 16210 // A variable that is privatized must not have a const-qualified type 16211 // unless it is of class type with a mutable member. This restriction does 16212 // not apply to the firstprivate clause. 16213 // 16214 // OpenMP 3.1 [2.9.3.5, lastprivate clause, Restrictions] 16215 // A variable that appears in a lastprivate clause must not have a 16216 // const-qualified type unless it is of class type with a mutable member. 16217 if (rejectConstNotMutableType(*this, D, Type, OMPC_lastprivate, ELoc)) 16218 continue; 16219 16220 // OpenMP 5.0 [2.19.4.5 lastprivate Clause, Restrictions] 16221 // A list item that appears in a lastprivate clause with the conditional 16222 // modifier must be a scalar variable. 16223 if (LPKind == OMPC_LASTPRIVATE_conditional && !Type->isScalarType()) { 16224 Diag(ELoc, diag::err_omp_lastprivate_conditional_non_scalar); 16225 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 16226 VarDecl::DeclarationOnly; 16227 Diag(D->getLocation(), 16228 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 16229 << D; 16230 continue; 16231 } 16232 16233 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 16234 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 16235 // in a Construct] 16236 // Variables with the predetermined data-sharing attributes may not be 16237 // listed in data-sharing attributes clauses, except for the cases 16238 // listed below. 16239 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3] 16240 // A list item may appear in a firstprivate or lastprivate clause but not 16241 // both. 16242 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 16243 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate && 16244 (isOpenMPDistributeDirective(CurrDir) || 16245 DVar.CKind != OMPC_firstprivate) && 16246 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) { 16247 Diag(ELoc, diag::err_omp_wrong_dsa) 16248 << getOpenMPClauseName(DVar.CKind) 16249 << getOpenMPClauseName(OMPC_lastprivate); 16250 reportOriginalDsa(*this, DSAStack, D, DVar); 16251 continue; 16252 } 16253 16254 // OpenMP [2.14.3.5, Restrictions, p.2] 16255 // A list item that is private within a parallel region, or that appears in 16256 // the reduction clause of a parallel construct, must not appear in a 16257 // lastprivate clause on a worksharing construct if any of the corresponding 16258 // worksharing regions ever binds to any of the corresponding parallel 16259 // regions. 16260 DSAStackTy::DSAVarData TopDVar = DVar; 16261 if (isOpenMPWorksharingDirective(CurrDir) && 16262 !isOpenMPParallelDirective(CurrDir) && 16263 !isOpenMPTeamsDirective(CurrDir)) { 16264 DVar = DSAStack->getImplicitDSA(D, true); 16265 if (DVar.CKind != OMPC_shared) { 16266 Diag(ELoc, diag::err_omp_required_access) 16267 << getOpenMPClauseName(OMPC_lastprivate) 16268 << getOpenMPClauseName(OMPC_shared); 16269 reportOriginalDsa(*this, DSAStack, D, DVar); 16270 continue; 16271 } 16272 } 16273 16274 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2] 16275 // A variable of class type (or array thereof) that appears in a 16276 // lastprivate clause requires an accessible, unambiguous default 16277 // constructor for the class type, unless the list item is also specified 16278 // in a firstprivate clause. 16279 // A variable of class type (or array thereof) that appears in a 16280 // lastprivate clause requires an accessible, unambiguous copy assignment 16281 // operator for the class type. 16282 Type = Context.getBaseElementType(Type).getNonReferenceType(); 16283 VarDecl *SrcVD = buildVarDecl(*this, ERange.getBegin(), 16284 Type.getUnqualifiedType(), ".lastprivate.src", 16285 D->hasAttrs() ? &D->getAttrs() : nullptr); 16286 DeclRefExpr *PseudoSrcExpr = 16287 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc); 16288 VarDecl *DstVD = 16289 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst", 16290 D->hasAttrs() ? &D->getAttrs() : nullptr); 16291 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc); 16292 // For arrays generate assignment operation for single element and replace 16293 // it by the original array element in CodeGen. 16294 ExprResult AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign, 16295 PseudoDstExpr, PseudoSrcExpr); 16296 if (AssignmentOp.isInvalid()) 16297 continue; 16298 AssignmentOp = 16299 ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false); 16300 if (AssignmentOp.isInvalid()) 16301 continue; 16302 16303 DeclRefExpr *Ref = nullptr; 16304 if (!VD && !CurContext->isDependentContext()) { 16305 if (TopDVar.CKind == OMPC_firstprivate) { 16306 Ref = TopDVar.PrivateCopy; 16307 } else { 16308 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 16309 if (!isOpenMPCapturedDecl(D)) 16310 ExprCaptures.push_back(Ref->getDecl()); 16311 } 16312 if ((TopDVar.CKind == OMPC_firstprivate && !TopDVar.PrivateCopy) || 16313 (!isOpenMPCapturedDecl(D) && 16314 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) { 16315 ExprResult RefRes = DefaultLvalueConversion(Ref); 16316 if (!RefRes.isUsable()) 16317 continue; 16318 ExprResult PostUpdateRes = 16319 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr, 16320 RefRes.get()); 16321 if (!PostUpdateRes.isUsable()) 16322 continue; 16323 ExprPostUpdates.push_back( 16324 IgnoredValueConversions(PostUpdateRes.get()).get()); 16325 } 16326 } 16327 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref); 16328 Vars.push_back((VD || CurContext->isDependentContext()) 16329 ? RefExpr->IgnoreParens() 16330 : Ref); 16331 SrcExprs.push_back(PseudoSrcExpr); 16332 DstExprs.push_back(PseudoDstExpr); 16333 AssignmentOps.push_back(AssignmentOp.get()); 16334 } 16335 16336 if (Vars.empty()) 16337 return nullptr; 16338 16339 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 16340 Vars, SrcExprs, DstExprs, AssignmentOps, 16341 LPKind, LPKindLoc, ColonLoc, 16342 buildPreInits(Context, ExprCaptures), 16343 buildPostUpdate(*this, ExprPostUpdates)); 16344 } 16345 16346 OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList, 16347 SourceLocation StartLoc, 16348 SourceLocation LParenLoc, 16349 SourceLocation EndLoc) { 16350 SmallVector<Expr *, 8> Vars; 16351 for (Expr *RefExpr : VarList) { 16352 assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); 16353 SourceLocation ELoc; 16354 SourceRange ERange; 16355 Expr *SimpleRefExpr = RefExpr; 16356 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 16357 if (Res.second) { 16358 // It will be analyzed later. 16359 Vars.push_back(RefExpr); 16360 } 16361 ValueDecl *D = Res.first; 16362 if (!D) 16363 continue; 16364 16365 auto *VD = dyn_cast<VarDecl>(D); 16366 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 16367 // in a Construct] 16368 // Variables with the predetermined data-sharing attributes may not be 16369 // listed in data-sharing attributes clauses, except for the cases 16370 // listed below. For these exceptions only, listing a predetermined 16371 // variable in a data-sharing attribute clause is allowed and overrides 16372 // the variable's predetermined data-sharing attributes. 16373 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 16374 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared && 16375 DVar.RefExpr) { 16376 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 16377 << getOpenMPClauseName(OMPC_shared); 16378 reportOriginalDsa(*this, DSAStack, D, DVar); 16379 continue; 16380 } 16381 16382 DeclRefExpr *Ref = nullptr; 16383 if (!VD && isOpenMPCapturedDecl(D) && !CurContext->isDependentContext()) 16384 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 16385 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref); 16386 Vars.push_back((VD || !Ref || CurContext->isDependentContext()) 16387 ? RefExpr->IgnoreParens() 16388 : Ref); 16389 } 16390 16391 if (Vars.empty()) 16392 return nullptr; 16393 16394 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 16395 } 16396 16397 namespace { 16398 class DSARefChecker : public StmtVisitor<DSARefChecker, bool> { 16399 DSAStackTy *Stack; 16400 16401 public: 16402 bool VisitDeclRefExpr(DeclRefExpr *E) { 16403 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 16404 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false); 16405 if (DVar.CKind == OMPC_shared && !DVar.RefExpr) 16406 return false; 16407 if (DVar.CKind != OMPC_unknown) 16408 return true; 16409 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA( 16410 VD, 16411 [](OpenMPClauseKind C, bool AppliedToPointee) { 16412 return isOpenMPPrivate(C) && !AppliedToPointee; 16413 }, 16414 [](OpenMPDirectiveKind) { return true; }, 16415 /*FromParent=*/true); 16416 return DVarPrivate.CKind != OMPC_unknown; 16417 } 16418 return false; 16419 } 16420 bool VisitStmt(Stmt *S) { 16421 for (Stmt *Child : S->children()) { 16422 if (Child && Visit(Child)) 16423 return true; 16424 } 16425 return false; 16426 } 16427 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {} 16428 }; 16429 } // namespace 16430 16431 namespace { 16432 // Transform MemberExpression for specified FieldDecl of current class to 16433 // DeclRefExpr to specified OMPCapturedExprDecl. 16434 class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> { 16435 typedef TreeTransform<TransformExprToCaptures> BaseTransform; 16436 ValueDecl *Field = nullptr; 16437 DeclRefExpr *CapturedExpr = nullptr; 16438 16439 public: 16440 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl) 16441 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {} 16442 16443 ExprResult TransformMemberExpr(MemberExpr *E) { 16444 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) && 16445 E->getMemberDecl() == Field) { 16446 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false); 16447 return CapturedExpr; 16448 } 16449 return BaseTransform::TransformMemberExpr(E); 16450 } 16451 DeclRefExpr *getCapturedExpr() { return CapturedExpr; } 16452 }; 16453 } // namespace 16454 16455 template <typename T, typename U> 16456 static T filterLookupForUDReductionAndMapper( 16457 SmallVectorImpl<U> &Lookups, const llvm::function_ref<T(ValueDecl *)> Gen) { 16458 for (U &Set : Lookups) { 16459 for (auto *D : Set) { 16460 if (T Res = Gen(cast<ValueDecl>(D))) 16461 return Res; 16462 } 16463 } 16464 return T(); 16465 } 16466 16467 static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D) { 16468 assert(!LookupResult::isVisible(SemaRef, D) && "not in slow case"); 16469 16470 for (auto RD : D->redecls()) { 16471 // Don't bother with extra checks if we already know this one isn't visible. 16472 if (RD == D) 16473 continue; 16474 16475 auto ND = cast<NamedDecl>(RD); 16476 if (LookupResult::isVisible(SemaRef, ND)) 16477 return ND; 16478 } 16479 16480 return nullptr; 16481 } 16482 16483 static void 16484 argumentDependentLookup(Sema &SemaRef, const DeclarationNameInfo &Id, 16485 SourceLocation Loc, QualType Ty, 16486 SmallVectorImpl<UnresolvedSet<8>> &Lookups) { 16487 // Find all of the associated namespaces and classes based on the 16488 // arguments we have. 16489 Sema::AssociatedNamespaceSet AssociatedNamespaces; 16490 Sema::AssociatedClassSet AssociatedClasses; 16491 OpaqueValueExpr OVE(Loc, Ty, VK_LValue); 16492 SemaRef.FindAssociatedClassesAndNamespaces(Loc, &OVE, AssociatedNamespaces, 16493 AssociatedClasses); 16494 16495 // C++ [basic.lookup.argdep]p3: 16496 // Let X be the lookup set produced by unqualified lookup (3.4.1) 16497 // and let Y be the lookup set produced by argument dependent 16498 // lookup (defined as follows). If X contains [...] then Y is 16499 // empty. Otherwise Y is the set of declarations found in the 16500 // namespaces associated with the argument types as described 16501 // below. The set of declarations found by the lookup of the name 16502 // is the union of X and Y. 16503 // 16504 // Here, we compute Y and add its members to the overloaded 16505 // candidate set. 16506 for (auto *NS : AssociatedNamespaces) { 16507 // When considering an associated namespace, the lookup is the 16508 // same as the lookup performed when the associated namespace is 16509 // used as a qualifier (3.4.3.2) except that: 16510 // 16511 // -- Any using-directives in the associated namespace are 16512 // ignored. 16513 // 16514 // -- Any namespace-scope friend functions declared in 16515 // associated classes are visible within their respective 16516 // namespaces even if they are not visible during an ordinary 16517 // lookup (11.4). 16518 DeclContext::lookup_result R = NS->lookup(Id.getName()); 16519 for (auto *D : R) { 16520 auto *Underlying = D; 16521 if (auto *USD = dyn_cast<UsingShadowDecl>(D)) 16522 Underlying = USD->getTargetDecl(); 16523 16524 if (!isa<OMPDeclareReductionDecl>(Underlying) && 16525 !isa<OMPDeclareMapperDecl>(Underlying)) 16526 continue; 16527 16528 if (!SemaRef.isVisible(D)) { 16529 D = findAcceptableDecl(SemaRef, D); 16530 if (!D) 16531 continue; 16532 if (auto *USD = dyn_cast<UsingShadowDecl>(D)) 16533 Underlying = USD->getTargetDecl(); 16534 } 16535 Lookups.emplace_back(); 16536 Lookups.back().addDecl(Underlying); 16537 } 16538 } 16539 } 16540 16541 static ExprResult 16542 buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range, 16543 Scope *S, CXXScopeSpec &ReductionIdScopeSpec, 16544 const DeclarationNameInfo &ReductionId, QualType Ty, 16545 CXXCastPath &BasePath, Expr *UnresolvedReduction) { 16546 if (ReductionIdScopeSpec.isInvalid()) 16547 return ExprError(); 16548 SmallVector<UnresolvedSet<8>, 4> Lookups; 16549 if (S) { 16550 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName); 16551 Lookup.suppressDiagnostics(); 16552 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) { 16553 NamedDecl *D = Lookup.getRepresentativeDecl(); 16554 do { 16555 S = S->getParent(); 16556 } while (S && !S->isDeclScope(D)); 16557 if (S) 16558 S = S->getParent(); 16559 Lookups.emplace_back(); 16560 Lookups.back().append(Lookup.begin(), Lookup.end()); 16561 Lookup.clear(); 16562 } 16563 } else if (auto *ULE = 16564 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) { 16565 Lookups.push_back(UnresolvedSet<8>()); 16566 Decl *PrevD = nullptr; 16567 for (NamedDecl *D : ULE->decls()) { 16568 if (D == PrevD) 16569 Lookups.push_back(UnresolvedSet<8>()); 16570 else if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(D)) 16571 Lookups.back().addDecl(DRD); 16572 PrevD = D; 16573 } 16574 } 16575 if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() || 16576 Ty->isInstantiationDependentType() || 16577 Ty->containsUnexpandedParameterPack() || 16578 filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) { 16579 return !D->isInvalidDecl() && 16580 (D->getType()->isDependentType() || 16581 D->getType()->isInstantiationDependentType() || 16582 D->getType()->containsUnexpandedParameterPack()); 16583 })) { 16584 UnresolvedSet<8> ResSet; 16585 for (const UnresolvedSet<8> &Set : Lookups) { 16586 if (Set.empty()) 16587 continue; 16588 ResSet.append(Set.begin(), Set.end()); 16589 // The last item marks the end of all declarations at the specified scope. 16590 ResSet.addDecl(Set[Set.size() - 1]); 16591 } 16592 return UnresolvedLookupExpr::Create( 16593 SemaRef.Context, /*NamingClass=*/nullptr, 16594 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId, 16595 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end()); 16596 } 16597 // Lookup inside the classes. 16598 // C++ [over.match.oper]p3: 16599 // For a unary operator @ with an operand of a type whose 16600 // cv-unqualified version is T1, and for a binary operator @ with 16601 // a left operand of a type whose cv-unqualified version is T1 and 16602 // a right operand of a type whose cv-unqualified version is T2, 16603 // three sets of candidate functions, designated member 16604 // candidates, non-member candidates and built-in candidates, are 16605 // constructed as follows: 16606 // -- If T1 is a complete class type or a class currently being 16607 // defined, the set of member candidates is the result of the 16608 // qualified lookup of T1::operator@ (13.3.1.1.1); otherwise, 16609 // the set of member candidates is empty. 16610 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName); 16611 Lookup.suppressDiagnostics(); 16612 if (const auto *TyRec = Ty->getAs<RecordType>()) { 16613 // Complete the type if it can be completed. 16614 // If the type is neither complete nor being defined, bail out now. 16615 if (SemaRef.isCompleteType(Loc, Ty) || TyRec->isBeingDefined() || 16616 TyRec->getDecl()->getDefinition()) { 16617 Lookup.clear(); 16618 SemaRef.LookupQualifiedName(Lookup, TyRec->getDecl()); 16619 if (Lookup.empty()) { 16620 Lookups.emplace_back(); 16621 Lookups.back().append(Lookup.begin(), Lookup.end()); 16622 } 16623 } 16624 } 16625 // Perform ADL. 16626 if (SemaRef.getLangOpts().CPlusPlus) 16627 argumentDependentLookup(SemaRef, ReductionId, Loc, Ty, Lookups); 16628 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 16629 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * { 16630 if (!D->isInvalidDecl() && 16631 SemaRef.Context.hasSameType(D->getType(), Ty)) 16632 return D; 16633 return nullptr; 16634 })) 16635 return SemaRef.BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(), 16636 VK_LValue, Loc); 16637 if (SemaRef.getLangOpts().CPlusPlus) { 16638 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 16639 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * { 16640 if (!D->isInvalidDecl() && 16641 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) && 16642 !Ty.isMoreQualifiedThan(D->getType())) 16643 return D; 16644 return nullptr; 16645 })) { 16646 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, 16647 /*DetectVirtual=*/false); 16648 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) { 16649 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType( 16650 VD->getType().getUnqualifiedType()))) { 16651 if (SemaRef.CheckBaseClassAccess( 16652 Loc, VD->getType(), Ty, Paths.front(), 16653 /*DiagID=*/0) != Sema::AR_inaccessible) { 16654 SemaRef.BuildBasePathArray(Paths, BasePath); 16655 return SemaRef.BuildDeclRefExpr( 16656 VD, VD->getType().getNonReferenceType(), VK_LValue, Loc); 16657 } 16658 } 16659 } 16660 } 16661 } 16662 if (ReductionIdScopeSpec.isSet()) { 16663 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) 16664 << Ty << Range; 16665 return ExprError(); 16666 } 16667 return ExprEmpty(); 16668 } 16669 16670 namespace { 16671 /// Data for the reduction-based clauses. 16672 struct ReductionData { 16673 /// List of original reduction items. 16674 SmallVector<Expr *, 8> Vars; 16675 /// List of private copies of the reduction items. 16676 SmallVector<Expr *, 8> Privates; 16677 /// LHS expressions for the reduction_op expressions. 16678 SmallVector<Expr *, 8> LHSs; 16679 /// RHS expressions for the reduction_op expressions. 16680 SmallVector<Expr *, 8> RHSs; 16681 /// Reduction operation expression. 16682 SmallVector<Expr *, 8> ReductionOps; 16683 /// inscan copy operation expressions. 16684 SmallVector<Expr *, 8> InscanCopyOps; 16685 /// inscan copy temp array expressions for prefix sums. 16686 SmallVector<Expr *, 8> InscanCopyArrayTemps; 16687 /// inscan copy temp array element expressions for prefix sums. 16688 SmallVector<Expr *, 8> InscanCopyArrayElems; 16689 /// Taskgroup descriptors for the corresponding reduction items in 16690 /// in_reduction clauses. 16691 SmallVector<Expr *, 8> TaskgroupDescriptors; 16692 /// List of captures for clause. 16693 SmallVector<Decl *, 4> ExprCaptures; 16694 /// List of postupdate expressions. 16695 SmallVector<Expr *, 4> ExprPostUpdates; 16696 /// Reduction modifier. 16697 unsigned RedModifier = 0; 16698 ReductionData() = delete; 16699 /// Reserves required memory for the reduction data. 16700 ReductionData(unsigned Size, unsigned Modifier = 0) : RedModifier(Modifier) { 16701 Vars.reserve(Size); 16702 Privates.reserve(Size); 16703 LHSs.reserve(Size); 16704 RHSs.reserve(Size); 16705 ReductionOps.reserve(Size); 16706 if (RedModifier == OMPC_REDUCTION_inscan) { 16707 InscanCopyOps.reserve(Size); 16708 InscanCopyArrayTemps.reserve(Size); 16709 InscanCopyArrayElems.reserve(Size); 16710 } 16711 TaskgroupDescriptors.reserve(Size); 16712 ExprCaptures.reserve(Size); 16713 ExprPostUpdates.reserve(Size); 16714 } 16715 /// Stores reduction item and reduction operation only (required for dependent 16716 /// reduction item). 16717 void push(Expr *Item, Expr *ReductionOp) { 16718 Vars.emplace_back(Item); 16719 Privates.emplace_back(nullptr); 16720 LHSs.emplace_back(nullptr); 16721 RHSs.emplace_back(nullptr); 16722 ReductionOps.emplace_back(ReductionOp); 16723 TaskgroupDescriptors.emplace_back(nullptr); 16724 if (RedModifier == OMPC_REDUCTION_inscan) { 16725 InscanCopyOps.push_back(nullptr); 16726 InscanCopyArrayTemps.push_back(nullptr); 16727 InscanCopyArrayElems.push_back(nullptr); 16728 } 16729 } 16730 /// Stores reduction data. 16731 void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp, 16732 Expr *TaskgroupDescriptor, Expr *CopyOp, Expr *CopyArrayTemp, 16733 Expr *CopyArrayElem) { 16734 Vars.emplace_back(Item); 16735 Privates.emplace_back(Private); 16736 LHSs.emplace_back(LHS); 16737 RHSs.emplace_back(RHS); 16738 ReductionOps.emplace_back(ReductionOp); 16739 TaskgroupDescriptors.emplace_back(TaskgroupDescriptor); 16740 if (RedModifier == OMPC_REDUCTION_inscan) { 16741 InscanCopyOps.push_back(CopyOp); 16742 InscanCopyArrayTemps.push_back(CopyArrayTemp); 16743 InscanCopyArrayElems.push_back(CopyArrayElem); 16744 } else { 16745 assert(CopyOp == nullptr && CopyArrayTemp == nullptr && 16746 CopyArrayElem == nullptr && 16747 "Copy operation must be used for inscan reductions only."); 16748 } 16749 } 16750 }; 16751 } // namespace 16752 16753 static bool checkOMPArraySectionConstantForReduction( 16754 ASTContext &Context, const OMPArraySectionExpr *OASE, bool &SingleElement, 16755 SmallVectorImpl<llvm::APSInt> &ArraySizes) { 16756 const Expr *Length = OASE->getLength(); 16757 if (Length == nullptr) { 16758 // For array sections of the form [1:] or [:], we would need to analyze 16759 // the lower bound... 16760 if (OASE->getColonLocFirst().isValid()) 16761 return false; 16762 16763 // This is an array subscript which has implicit length 1! 16764 SingleElement = true; 16765 ArraySizes.push_back(llvm::APSInt::get(1)); 16766 } else { 16767 Expr::EvalResult Result; 16768 if (!Length->EvaluateAsInt(Result, Context)) 16769 return false; 16770 16771 llvm::APSInt ConstantLengthValue = Result.Val.getInt(); 16772 SingleElement = (ConstantLengthValue.getSExtValue() == 1); 16773 ArraySizes.push_back(ConstantLengthValue); 16774 } 16775 16776 // Get the base of this array section and walk up from there. 16777 const Expr *Base = OASE->getBase()->IgnoreParenImpCasts(); 16778 16779 // We require length = 1 for all array sections except the right-most to 16780 // guarantee that the memory region is contiguous and has no holes in it. 16781 while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) { 16782 Length = TempOASE->getLength(); 16783 if (Length == nullptr) { 16784 // For array sections of the form [1:] or [:], we would need to analyze 16785 // the lower bound... 16786 if (OASE->getColonLocFirst().isValid()) 16787 return false; 16788 16789 // This is an array subscript which has implicit length 1! 16790 ArraySizes.push_back(llvm::APSInt::get(1)); 16791 } else { 16792 Expr::EvalResult Result; 16793 if (!Length->EvaluateAsInt(Result, Context)) 16794 return false; 16795 16796 llvm::APSInt ConstantLengthValue = Result.Val.getInt(); 16797 if (ConstantLengthValue.getSExtValue() != 1) 16798 return false; 16799 16800 ArraySizes.push_back(ConstantLengthValue); 16801 } 16802 Base = TempOASE->getBase()->IgnoreParenImpCasts(); 16803 } 16804 16805 // If we have a single element, we don't need to add the implicit lengths. 16806 if (!SingleElement) { 16807 while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) { 16808 // Has implicit length 1! 16809 ArraySizes.push_back(llvm::APSInt::get(1)); 16810 Base = TempASE->getBase()->IgnoreParenImpCasts(); 16811 } 16812 } 16813 16814 // This array section can be privatized as a single value or as a constant 16815 // sized array. 16816 return true; 16817 } 16818 16819 static BinaryOperatorKind 16820 getRelatedCompoundReductionOp(BinaryOperatorKind BOK) { 16821 if (BOK == BO_Add) 16822 return BO_AddAssign; 16823 if (BOK == BO_Mul) 16824 return BO_MulAssign; 16825 if (BOK == BO_And) 16826 return BO_AndAssign; 16827 if (BOK == BO_Or) 16828 return BO_OrAssign; 16829 if (BOK == BO_Xor) 16830 return BO_XorAssign; 16831 return BOK; 16832 } 16833 16834 static bool actOnOMPReductionKindClause( 16835 Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind, 16836 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 16837 SourceLocation ColonLoc, SourceLocation EndLoc, 16838 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 16839 ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) { 16840 DeclarationName DN = ReductionId.getName(); 16841 OverloadedOperatorKind OOK = DN.getCXXOverloadedOperator(); 16842 BinaryOperatorKind BOK = BO_Comma; 16843 16844 ASTContext &Context = S.Context; 16845 // OpenMP [2.14.3.6, reduction clause] 16846 // C 16847 // reduction-identifier is either an identifier or one of the following 16848 // operators: +, -, *, &, |, ^, && and || 16849 // C++ 16850 // reduction-identifier is either an id-expression or one of the following 16851 // operators: +, -, *, &, |, ^, && and || 16852 switch (OOK) { 16853 case OO_Plus: 16854 case OO_Minus: 16855 BOK = BO_Add; 16856 break; 16857 case OO_Star: 16858 BOK = BO_Mul; 16859 break; 16860 case OO_Amp: 16861 BOK = BO_And; 16862 break; 16863 case OO_Pipe: 16864 BOK = BO_Or; 16865 break; 16866 case OO_Caret: 16867 BOK = BO_Xor; 16868 break; 16869 case OO_AmpAmp: 16870 BOK = BO_LAnd; 16871 break; 16872 case OO_PipePipe: 16873 BOK = BO_LOr; 16874 break; 16875 case OO_New: 16876 case OO_Delete: 16877 case OO_Array_New: 16878 case OO_Array_Delete: 16879 case OO_Slash: 16880 case OO_Percent: 16881 case OO_Tilde: 16882 case OO_Exclaim: 16883 case OO_Equal: 16884 case OO_Less: 16885 case OO_Greater: 16886 case OO_LessEqual: 16887 case OO_GreaterEqual: 16888 case OO_PlusEqual: 16889 case OO_MinusEqual: 16890 case OO_StarEqual: 16891 case OO_SlashEqual: 16892 case OO_PercentEqual: 16893 case OO_CaretEqual: 16894 case OO_AmpEqual: 16895 case OO_PipeEqual: 16896 case OO_LessLess: 16897 case OO_GreaterGreater: 16898 case OO_LessLessEqual: 16899 case OO_GreaterGreaterEqual: 16900 case OO_EqualEqual: 16901 case OO_ExclaimEqual: 16902 case OO_Spaceship: 16903 case OO_PlusPlus: 16904 case OO_MinusMinus: 16905 case OO_Comma: 16906 case OO_ArrowStar: 16907 case OO_Arrow: 16908 case OO_Call: 16909 case OO_Subscript: 16910 case OO_Conditional: 16911 case OO_Coawait: 16912 case NUM_OVERLOADED_OPERATORS: 16913 llvm_unreachable("Unexpected reduction identifier"); 16914 case OO_None: 16915 if (IdentifierInfo *II = DN.getAsIdentifierInfo()) { 16916 if (II->isStr("max")) 16917 BOK = BO_GT; 16918 else if (II->isStr("min")) 16919 BOK = BO_LT; 16920 } 16921 break; 16922 } 16923 SourceRange ReductionIdRange; 16924 if (ReductionIdScopeSpec.isValid()) 16925 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc()); 16926 else 16927 ReductionIdRange.setBegin(ReductionId.getBeginLoc()); 16928 ReductionIdRange.setEnd(ReductionId.getEndLoc()); 16929 16930 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end(); 16931 bool FirstIter = true; 16932 for (Expr *RefExpr : VarList) { 16933 assert(RefExpr && "nullptr expr in OpenMP reduction clause."); 16934 // OpenMP [2.1, C/C++] 16935 // A list item is a variable or array section, subject to the restrictions 16936 // specified in Section 2.4 on page 42 and in each of the sections 16937 // describing clauses and directives for which a list appears. 16938 // OpenMP [2.14.3.3, Restrictions, p.1] 16939 // A variable that is part of another variable (as an array or 16940 // structure element) cannot appear in a private clause. 16941 if (!FirstIter && IR != ER) 16942 ++IR; 16943 FirstIter = false; 16944 SourceLocation ELoc; 16945 SourceRange ERange; 16946 Expr *SimpleRefExpr = RefExpr; 16947 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange, 16948 /*AllowArraySection=*/true); 16949 if (Res.second) { 16950 // Try to find 'declare reduction' corresponding construct before using 16951 // builtin/overloaded operators. 16952 QualType Type = Context.DependentTy; 16953 CXXCastPath BasePath; 16954 ExprResult DeclareReductionRef = buildDeclareReductionRef( 16955 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec, 16956 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR); 16957 Expr *ReductionOp = nullptr; 16958 if (S.CurContext->isDependentContext() && 16959 (DeclareReductionRef.isUnset() || 16960 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) 16961 ReductionOp = DeclareReductionRef.get(); 16962 // It will be analyzed later. 16963 RD.push(RefExpr, ReductionOp); 16964 } 16965 ValueDecl *D = Res.first; 16966 if (!D) 16967 continue; 16968 16969 Expr *TaskgroupDescriptor = nullptr; 16970 QualType Type; 16971 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens()); 16972 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens()); 16973 if (ASE) { 16974 Type = ASE->getType().getNonReferenceType(); 16975 } else if (OASE) { 16976 QualType BaseType = 16977 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 16978 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 16979 Type = ATy->getElementType(); 16980 else 16981 Type = BaseType->getPointeeType(); 16982 Type = Type.getNonReferenceType(); 16983 } else { 16984 Type = Context.getBaseElementType(D->getType().getNonReferenceType()); 16985 } 16986 auto *VD = dyn_cast<VarDecl>(D); 16987 16988 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 16989 // A variable that appears in a private clause must not have an incomplete 16990 // type or a reference type. 16991 if (S.RequireCompleteType(ELoc, D->getType(), 16992 diag::err_omp_reduction_incomplete_type)) 16993 continue; 16994 // OpenMP [2.14.3.6, reduction clause, Restrictions] 16995 // A list item that appears in a reduction clause must not be 16996 // const-qualified. 16997 if (rejectConstNotMutableType(S, D, Type, ClauseKind, ELoc, 16998 /*AcceptIfMutable*/ false, ASE || OASE)) 16999 continue; 17000 17001 OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective(); 17002 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4] 17003 // If a list-item is a reference type then it must bind to the same object 17004 // for all threads of the team. 17005 if (!ASE && !OASE) { 17006 if (VD) { 17007 VarDecl *VDDef = VD->getDefinition(); 17008 if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) { 17009 DSARefChecker Check(Stack); 17010 if (Check.Visit(VDDef->getInit())) { 17011 S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg) 17012 << getOpenMPClauseName(ClauseKind) << ERange; 17013 S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef; 17014 continue; 17015 } 17016 } 17017 } 17018 17019 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 17020 // in a Construct] 17021 // Variables with the predetermined data-sharing attributes may not be 17022 // listed in data-sharing attributes clauses, except for the cases 17023 // listed below. For these exceptions only, listing a predetermined 17024 // variable in a data-sharing attribute clause is allowed and overrides 17025 // the variable's predetermined data-sharing attributes. 17026 // OpenMP [2.14.3.6, Restrictions, p.3] 17027 // Any number of reduction clauses can be specified on the directive, 17028 // but a list item can appear only once in the reduction clauses for that 17029 // directive. 17030 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false); 17031 if (DVar.CKind == OMPC_reduction) { 17032 S.Diag(ELoc, diag::err_omp_once_referenced) 17033 << getOpenMPClauseName(ClauseKind); 17034 if (DVar.RefExpr) 17035 S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced); 17036 continue; 17037 } 17038 if (DVar.CKind != OMPC_unknown) { 17039 S.Diag(ELoc, diag::err_omp_wrong_dsa) 17040 << getOpenMPClauseName(DVar.CKind) 17041 << getOpenMPClauseName(OMPC_reduction); 17042 reportOriginalDsa(S, Stack, D, DVar); 17043 continue; 17044 } 17045 17046 // OpenMP [2.14.3.6, Restrictions, p.1] 17047 // A list item that appears in a reduction clause of a worksharing 17048 // construct must be shared in the parallel regions to which any of the 17049 // worksharing regions arising from the worksharing construct bind. 17050 if (isOpenMPWorksharingDirective(CurrDir) && 17051 !isOpenMPParallelDirective(CurrDir) && 17052 !isOpenMPTeamsDirective(CurrDir)) { 17053 DVar = Stack->getImplicitDSA(D, true); 17054 if (DVar.CKind != OMPC_shared) { 17055 S.Diag(ELoc, diag::err_omp_required_access) 17056 << getOpenMPClauseName(OMPC_reduction) 17057 << getOpenMPClauseName(OMPC_shared); 17058 reportOriginalDsa(S, Stack, D, DVar); 17059 continue; 17060 } 17061 } 17062 } else { 17063 // Threadprivates cannot be shared between threads, so dignose if the base 17064 // is a threadprivate variable. 17065 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false); 17066 if (DVar.CKind == OMPC_threadprivate) { 17067 S.Diag(ELoc, diag::err_omp_wrong_dsa) 17068 << getOpenMPClauseName(DVar.CKind) 17069 << getOpenMPClauseName(OMPC_reduction); 17070 reportOriginalDsa(S, Stack, D, DVar); 17071 continue; 17072 } 17073 } 17074 17075 // Try to find 'declare reduction' corresponding construct before using 17076 // builtin/overloaded operators. 17077 CXXCastPath BasePath; 17078 ExprResult DeclareReductionRef = buildDeclareReductionRef( 17079 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec, 17080 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR); 17081 if (DeclareReductionRef.isInvalid()) 17082 continue; 17083 if (S.CurContext->isDependentContext() && 17084 (DeclareReductionRef.isUnset() || 17085 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) { 17086 RD.push(RefExpr, DeclareReductionRef.get()); 17087 continue; 17088 } 17089 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) { 17090 // Not allowed reduction identifier is found. 17091 S.Diag(ReductionId.getBeginLoc(), 17092 diag::err_omp_unknown_reduction_identifier) 17093 << Type << ReductionIdRange; 17094 continue; 17095 } 17096 17097 // OpenMP [2.14.3.6, reduction clause, Restrictions] 17098 // The type of a list item that appears in a reduction clause must be valid 17099 // for the reduction-identifier. For a max or min reduction in C, the type 17100 // of the list item must be an allowed arithmetic data type: char, int, 17101 // float, double, or _Bool, possibly modified with long, short, signed, or 17102 // unsigned. For a max or min reduction in C++, the type of the list item 17103 // must be an allowed arithmetic data type: char, wchar_t, int, float, 17104 // double, or bool, possibly modified with long, short, signed, or unsigned. 17105 if (DeclareReductionRef.isUnset()) { 17106 if ((BOK == BO_GT || BOK == BO_LT) && 17107 !(Type->isScalarType() || 17108 (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) { 17109 S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg) 17110 << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus; 17111 if (!ASE && !OASE) { 17112 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 17113 VarDecl::DeclarationOnly; 17114 S.Diag(D->getLocation(), 17115 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 17116 << D; 17117 } 17118 continue; 17119 } 17120 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) && 17121 !S.getLangOpts().CPlusPlus && Type->isFloatingType()) { 17122 S.Diag(ELoc, diag::err_omp_clause_floating_type_arg) 17123 << getOpenMPClauseName(ClauseKind); 17124 if (!ASE && !OASE) { 17125 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 17126 VarDecl::DeclarationOnly; 17127 S.Diag(D->getLocation(), 17128 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 17129 << D; 17130 } 17131 continue; 17132 } 17133 } 17134 17135 Type = Type.getNonLValueExprType(Context).getUnqualifiedType(); 17136 VarDecl *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs", 17137 D->hasAttrs() ? &D->getAttrs() : nullptr); 17138 VarDecl *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(), 17139 D->hasAttrs() ? &D->getAttrs() : nullptr); 17140 QualType PrivateTy = Type; 17141 17142 // Try if we can determine constant lengths for all array sections and avoid 17143 // the VLA. 17144 bool ConstantLengthOASE = false; 17145 if (OASE) { 17146 bool SingleElement; 17147 llvm::SmallVector<llvm::APSInt, 4> ArraySizes; 17148 ConstantLengthOASE = checkOMPArraySectionConstantForReduction( 17149 Context, OASE, SingleElement, ArraySizes); 17150 17151 // If we don't have a single element, we must emit a constant array type. 17152 if (ConstantLengthOASE && !SingleElement) { 17153 for (llvm::APSInt &Size : ArraySizes) 17154 PrivateTy = Context.getConstantArrayType(PrivateTy, Size, nullptr, 17155 ArrayType::Normal, 17156 /*IndexTypeQuals=*/0); 17157 } 17158 } 17159 17160 if ((OASE && !ConstantLengthOASE) || 17161 (!OASE && !ASE && 17162 D->getType().getNonReferenceType()->isVariablyModifiedType())) { 17163 if (!Context.getTargetInfo().isVLASupported()) { 17164 if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective())) { 17165 S.Diag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE; 17166 S.Diag(ELoc, diag::note_vla_unsupported); 17167 continue; 17168 } else { 17169 S.targetDiag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE; 17170 S.targetDiag(ELoc, diag::note_vla_unsupported); 17171 } 17172 } 17173 // For arrays/array sections only: 17174 // Create pseudo array type for private copy. The size for this array will 17175 // be generated during codegen. 17176 // For array subscripts or single variables Private Ty is the same as Type 17177 // (type of the variable or single array element). 17178 PrivateTy = Context.getVariableArrayType( 17179 Type, 17180 new (Context) 17181 OpaqueValueExpr(ELoc, Context.getSizeType(), VK_PRValue), 17182 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange()); 17183 } else if (!ASE && !OASE && 17184 Context.getAsArrayType(D->getType().getNonReferenceType())) { 17185 PrivateTy = D->getType().getNonReferenceType(); 17186 } 17187 // Private copy. 17188 VarDecl *PrivateVD = 17189 buildVarDecl(S, ELoc, PrivateTy, D->getName(), 17190 D->hasAttrs() ? &D->getAttrs() : nullptr, 17191 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 17192 // Add initializer for private variable. 17193 Expr *Init = nullptr; 17194 DeclRefExpr *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc); 17195 DeclRefExpr *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc); 17196 if (DeclareReductionRef.isUsable()) { 17197 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>(); 17198 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl()); 17199 if (DRD->getInitializer()) { 17200 Init = DRDRef; 17201 RHSVD->setInit(DRDRef); 17202 RHSVD->setInitStyle(VarDecl::CallInit); 17203 } 17204 } else { 17205 switch (BOK) { 17206 case BO_Add: 17207 case BO_Xor: 17208 case BO_Or: 17209 case BO_LOr: 17210 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'. 17211 if (Type->isScalarType() || Type->isAnyComplexType()) 17212 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get(); 17213 break; 17214 case BO_Mul: 17215 case BO_LAnd: 17216 if (Type->isScalarType() || Type->isAnyComplexType()) { 17217 // '*' and '&&' reduction ops - initializer is '1'. 17218 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get(); 17219 } 17220 break; 17221 case BO_And: { 17222 // '&' reduction op - initializer is '~0'. 17223 QualType OrigType = Type; 17224 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) 17225 Type = ComplexTy->getElementType(); 17226 if (Type->isRealFloatingType()) { 17227 llvm::APFloat InitValue = llvm::APFloat::getAllOnesValue( 17228 Context.getFloatTypeSemantics(Type)); 17229 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, 17230 Type, ELoc); 17231 } else if (Type->isScalarType()) { 17232 uint64_t Size = Context.getTypeSize(Type); 17233 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0); 17234 llvm::APInt InitValue = llvm::APInt::getAllOnes(Size); 17235 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); 17236 } 17237 if (Init && OrigType->isAnyComplexType()) { 17238 // Init = 0xFFFF + 0xFFFFi; 17239 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType); 17240 Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get(); 17241 } 17242 Type = OrigType; 17243 break; 17244 } 17245 case BO_LT: 17246 case BO_GT: { 17247 // 'min' reduction op - initializer is 'Largest representable number in 17248 // the reduction list item type'. 17249 // 'max' reduction op - initializer is 'Least representable number in 17250 // the reduction list item type'. 17251 if (Type->isIntegerType() || Type->isPointerType()) { 17252 bool IsSigned = Type->hasSignedIntegerRepresentation(); 17253 uint64_t Size = Context.getTypeSize(Type); 17254 QualType IntTy = 17255 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned); 17256 llvm::APInt InitValue = 17257 (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size) 17258 : llvm::APInt::getMinValue(Size) 17259 : IsSigned ? llvm::APInt::getSignedMaxValue(Size) 17260 : llvm::APInt::getMaxValue(Size); 17261 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); 17262 if (Type->isPointerType()) { 17263 // Cast to pointer type. 17264 ExprResult CastExpr = S.BuildCStyleCastExpr( 17265 ELoc, Context.getTrivialTypeSourceInfo(Type, ELoc), ELoc, Init); 17266 if (CastExpr.isInvalid()) 17267 continue; 17268 Init = CastExpr.get(); 17269 } 17270 } else if (Type->isRealFloatingType()) { 17271 llvm::APFloat InitValue = llvm::APFloat::getLargest( 17272 Context.getFloatTypeSemantics(Type), BOK != BO_LT); 17273 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, 17274 Type, ELoc); 17275 } 17276 break; 17277 } 17278 case BO_PtrMemD: 17279 case BO_PtrMemI: 17280 case BO_MulAssign: 17281 case BO_Div: 17282 case BO_Rem: 17283 case BO_Sub: 17284 case BO_Shl: 17285 case BO_Shr: 17286 case BO_LE: 17287 case BO_GE: 17288 case BO_EQ: 17289 case BO_NE: 17290 case BO_Cmp: 17291 case BO_AndAssign: 17292 case BO_XorAssign: 17293 case BO_OrAssign: 17294 case BO_Assign: 17295 case BO_AddAssign: 17296 case BO_SubAssign: 17297 case BO_DivAssign: 17298 case BO_RemAssign: 17299 case BO_ShlAssign: 17300 case BO_ShrAssign: 17301 case BO_Comma: 17302 llvm_unreachable("Unexpected reduction operation"); 17303 } 17304 } 17305 if (Init && DeclareReductionRef.isUnset()) { 17306 S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false); 17307 // Store initializer for single element in private copy. Will be used 17308 // during codegen. 17309 PrivateVD->setInit(RHSVD->getInit()); 17310 PrivateVD->setInitStyle(RHSVD->getInitStyle()); 17311 } else if (!Init) { 17312 S.ActOnUninitializedDecl(RHSVD); 17313 // Store initializer for single element in private copy. Will be used 17314 // during codegen. 17315 PrivateVD->setInit(RHSVD->getInit()); 17316 PrivateVD->setInitStyle(RHSVD->getInitStyle()); 17317 } 17318 if (RHSVD->isInvalidDecl()) 17319 continue; 17320 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) { 17321 S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible) 17322 << Type << ReductionIdRange; 17323 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 17324 VarDecl::DeclarationOnly; 17325 S.Diag(D->getLocation(), 17326 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 17327 << D; 17328 continue; 17329 } 17330 DeclRefExpr *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc); 17331 ExprResult ReductionOp; 17332 if (DeclareReductionRef.isUsable()) { 17333 QualType RedTy = DeclareReductionRef.get()->getType(); 17334 QualType PtrRedTy = Context.getPointerType(RedTy); 17335 ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE); 17336 ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE); 17337 if (!BasePath.empty()) { 17338 LHS = S.DefaultLvalueConversion(LHS.get()); 17339 RHS = S.DefaultLvalueConversion(RHS.get()); 17340 LHS = ImplicitCastExpr::Create( 17341 Context, PtrRedTy, CK_UncheckedDerivedToBase, LHS.get(), &BasePath, 17342 LHS.get()->getValueKind(), FPOptionsOverride()); 17343 RHS = ImplicitCastExpr::Create( 17344 Context, PtrRedTy, CK_UncheckedDerivedToBase, RHS.get(), &BasePath, 17345 RHS.get()->getValueKind(), FPOptionsOverride()); 17346 } 17347 FunctionProtoType::ExtProtoInfo EPI; 17348 QualType Params[] = {PtrRedTy, PtrRedTy}; 17349 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI); 17350 auto *OVE = new (Context) OpaqueValueExpr( 17351 ELoc, Context.getPointerType(FnTy), VK_PRValue, OK_Ordinary, 17352 S.DefaultLvalueConversion(DeclareReductionRef.get()).get()); 17353 Expr *Args[] = {LHS.get(), RHS.get()}; 17354 ReductionOp = 17355 CallExpr::Create(Context, OVE, Args, Context.VoidTy, VK_PRValue, ELoc, 17356 S.CurFPFeatureOverrides()); 17357 } else { 17358 BinaryOperatorKind CombBOK = getRelatedCompoundReductionOp(BOK); 17359 if (Type->isRecordType() && CombBOK != BOK) { 17360 Sema::TentativeAnalysisScope Trap(S); 17361 ReductionOp = 17362 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(), 17363 CombBOK, LHSDRE, RHSDRE); 17364 } 17365 if (!ReductionOp.isUsable()) { 17366 ReductionOp = 17367 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(), BOK, 17368 LHSDRE, RHSDRE); 17369 if (ReductionOp.isUsable()) { 17370 if (BOK != BO_LT && BOK != BO_GT) { 17371 ReductionOp = 17372 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(), 17373 BO_Assign, LHSDRE, ReductionOp.get()); 17374 } else { 17375 auto *ConditionalOp = new (Context) 17376 ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc, 17377 RHSDRE, Type, VK_LValue, OK_Ordinary); 17378 ReductionOp = 17379 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(), 17380 BO_Assign, LHSDRE, ConditionalOp); 17381 } 17382 } 17383 } 17384 if (ReductionOp.isUsable()) 17385 ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get(), 17386 /*DiscardedValue*/ false); 17387 if (!ReductionOp.isUsable()) 17388 continue; 17389 } 17390 17391 // Add copy operations for inscan reductions. 17392 // LHS = RHS; 17393 ExprResult CopyOpRes, TempArrayRes, TempArrayElem; 17394 if (ClauseKind == OMPC_reduction && 17395 RD.RedModifier == OMPC_REDUCTION_inscan) { 17396 ExprResult RHS = S.DefaultLvalueConversion(RHSDRE); 17397 CopyOpRes = S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, LHSDRE, 17398 RHS.get()); 17399 if (!CopyOpRes.isUsable()) 17400 continue; 17401 CopyOpRes = 17402 S.ActOnFinishFullExpr(CopyOpRes.get(), /*DiscardedValue=*/true); 17403 if (!CopyOpRes.isUsable()) 17404 continue; 17405 // For simd directive and simd-based directives in simd mode no need to 17406 // construct temp array, need just a single temp element. 17407 if (Stack->getCurrentDirective() == OMPD_simd || 17408 (S.getLangOpts().OpenMPSimd && 17409 isOpenMPSimdDirective(Stack->getCurrentDirective()))) { 17410 VarDecl *TempArrayVD = 17411 buildVarDecl(S, ELoc, PrivateTy, D->getName(), 17412 D->hasAttrs() ? &D->getAttrs() : nullptr); 17413 // Add a constructor to the temp decl. 17414 S.ActOnUninitializedDecl(TempArrayVD); 17415 TempArrayRes = buildDeclRefExpr(S, TempArrayVD, PrivateTy, ELoc); 17416 } else { 17417 // Build temp array for prefix sum. 17418 auto *Dim = new (S.Context) 17419 OpaqueValueExpr(ELoc, S.Context.getSizeType(), VK_PRValue); 17420 QualType ArrayTy = 17421 S.Context.getVariableArrayType(PrivateTy, Dim, ArrayType::Normal, 17422 /*IndexTypeQuals=*/0, {ELoc, ELoc}); 17423 VarDecl *TempArrayVD = 17424 buildVarDecl(S, ELoc, ArrayTy, D->getName(), 17425 D->hasAttrs() ? &D->getAttrs() : nullptr); 17426 // Add a constructor to the temp decl. 17427 S.ActOnUninitializedDecl(TempArrayVD); 17428 TempArrayRes = buildDeclRefExpr(S, TempArrayVD, ArrayTy, ELoc); 17429 TempArrayElem = 17430 S.DefaultFunctionArrayLvalueConversion(TempArrayRes.get()); 17431 auto *Idx = new (S.Context) 17432 OpaqueValueExpr(ELoc, S.Context.getSizeType(), VK_PRValue); 17433 TempArrayElem = S.CreateBuiltinArraySubscriptExpr(TempArrayElem.get(), 17434 ELoc, Idx, ELoc); 17435 } 17436 } 17437 17438 // OpenMP [2.15.4.6, Restrictions, p.2] 17439 // A list item that appears in an in_reduction clause of a task construct 17440 // must appear in a task_reduction clause of a construct associated with a 17441 // taskgroup region that includes the participating task in its taskgroup 17442 // set. The construct associated with the innermost region that meets this 17443 // condition must specify the same reduction-identifier as the in_reduction 17444 // clause. 17445 if (ClauseKind == OMPC_in_reduction) { 17446 SourceRange ParentSR; 17447 BinaryOperatorKind ParentBOK; 17448 const Expr *ParentReductionOp = nullptr; 17449 Expr *ParentBOKTD = nullptr, *ParentReductionOpTD = nullptr; 17450 DSAStackTy::DSAVarData ParentBOKDSA = 17451 Stack->getTopMostTaskgroupReductionData(D, ParentSR, ParentBOK, 17452 ParentBOKTD); 17453 DSAStackTy::DSAVarData ParentReductionOpDSA = 17454 Stack->getTopMostTaskgroupReductionData( 17455 D, ParentSR, ParentReductionOp, ParentReductionOpTD); 17456 bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown; 17457 bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown; 17458 if ((DeclareReductionRef.isUnset() && IsParentReductionOp) || 17459 (DeclareReductionRef.isUsable() && IsParentBOK) || 17460 (IsParentBOK && BOK != ParentBOK) || IsParentReductionOp) { 17461 bool EmitError = true; 17462 if (IsParentReductionOp && DeclareReductionRef.isUsable()) { 17463 llvm::FoldingSetNodeID RedId, ParentRedId; 17464 ParentReductionOp->Profile(ParentRedId, Context, /*Canonical=*/true); 17465 DeclareReductionRef.get()->Profile(RedId, Context, 17466 /*Canonical=*/true); 17467 EmitError = RedId != ParentRedId; 17468 } 17469 if (EmitError) { 17470 S.Diag(ReductionId.getBeginLoc(), 17471 diag::err_omp_reduction_identifier_mismatch) 17472 << ReductionIdRange << RefExpr->getSourceRange(); 17473 S.Diag(ParentSR.getBegin(), 17474 diag::note_omp_previous_reduction_identifier) 17475 << ParentSR 17476 << (IsParentBOK ? ParentBOKDSA.RefExpr 17477 : ParentReductionOpDSA.RefExpr) 17478 ->getSourceRange(); 17479 continue; 17480 } 17481 } 17482 TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD; 17483 } 17484 17485 DeclRefExpr *Ref = nullptr; 17486 Expr *VarsExpr = RefExpr->IgnoreParens(); 17487 if (!VD && !S.CurContext->isDependentContext()) { 17488 if (ASE || OASE) { 17489 TransformExprToCaptures RebuildToCapture(S, D); 17490 VarsExpr = 17491 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get(); 17492 Ref = RebuildToCapture.getCapturedExpr(); 17493 } else { 17494 VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false); 17495 } 17496 if (!S.isOpenMPCapturedDecl(D)) { 17497 RD.ExprCaptures.emplace_back(Ref->getDecl()); 17498 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) { 17499 ExprResult RefRes = S.DefaultLvalueConversion(Ref); 17500 if (!RefRes.isUsable()) 17501 continue; 17502 ExprResult PostUpdateRes = 17503 S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr, 17504 RefRes.get()); 17505 if (!PostUpdateRes.isUsable()) 17506 continue; 17507 if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) || 17508 Stack->getCurrentDirective() == OMPD_taskgroup) { 17509 S.Diag(RefExpr->getExprLoc(), 17510 diag::err_omp_reduction_non_addressable_expression) 17511 << RefExpr->getSourceRange(); 17512 continue; 17513 } 17514 RD.ExprPostUpdates.emplace_back( 17515 S.IgnoredValueConversions(PostUpdateRes.get()).get()); 17516 } 17517 } 17518 } 17519 // All reduction items are still marked as reduction (to do not increase 17520 // code base size). 17521 unsigned Modifier = RD.RedModifier; 17522 // Consider task_reductions as reductions with task modifier. Required for 17523 // correct analysis of in_reduction clauses. 17524 if (CurrDir == OMPD_taskgroup && ClauseKind == OMPC_task_reduction) 17525 Modifier = OMPC_REDUCTION_task; 17526 Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref, Modifier, 17527 ASE || OASE); 17528 if (Modifier == OMPC_REDUCTION_task && 17529 (CurrDir == OMPD_taskgroup || 17530 ((isOpenMPParallelDirective(CurrDir) || 17531 isOpenMPWorksharingDirective(CurrDir)) && 17532 !isOpenMPSimdDirective(CurrDir)))) { 17533 if (DeclareReductionRef.isUsable()) 17534 Stack->addTaskgroupReductionData(D, ReductionIdRange, 17535 DeclareReductionRef.get()); 17536 else 17537 Stack->addTaskgroupReductionData(D, ReductionIdRange, BOK); 17538 } 17539 RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get(), 17540 TaskgroupDescriptor, CopyOpRes.get(), TempArrayRes.get(), 17541 TempArrayElem.get()); 17542 } 17543 return RD.Vars.empty(); 17544 } 17545 17546 OMPClause *Sema::ActOnOpenMPReductionClause( 17547 ArrayRef<Expr *> VarList, OpenMPReductionClauseModifier Modifier, 17548 SourceLocation StartLoc, SourceLocation LParenLoc, 17549 SourceLocation ModifierLoc, SourceLocation ColonLoc, SourceLocation EndLoc, 17550 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 17551 ArrayRef<Expr *> UnresolvedReductions) { 17552 if (ModifierLoc.isValid() && Modifier == OMPC_REDUCTION_unknown) { 17553 Diag(LParenLoc, diag::err_omp_unexpected_clause_value) 17554 << getListOfPossibleValues(OMPC_reduction, /*First=*/0, 17555 /*Last=*/OMPC_REDUCTION_unknown) 17556 << getOpenMPClauseName(OMPC_reduction); 17557 return nullptr; 17558 } 17559 // OpenMP 5.0, 2.19.5.4 reduction Clause, Restrictions 17560 // A reduction clause with the inscan reduction-modifier may only appear on a 17561 // worksharing-loop construct, a worksharing-loop SIMD construct, a simd 17562 // construct, a parallel worksharing-loop construct or a parallel 17563 // worksharing-loop SIMD construct. 17564 if (Modifier == OMPC_REDUCTION_inscan && 17565 (DSAStack->getCurrentDirective() != OMPD_for && 17566 DSAStack->getCurrentDirective() != OMPD_for_simd && 17567 DSAStack->getCurrentDirective() != OMPD_simd && 17568 DSAStack->getCurrentDirective() != OMPD_parallel_for && 17569 DSAStack->getCurrentDirective() != OMPD_parallel_for_simd)) { 17570 Diag(ModifierLoc, diag::err_omp_wrong_inscan_reduction); 17571 return nullptr; 17572 } 17573 17574 ReductionData RD(VarList.size(), Modifier); 17575 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList, 17576 StartLoc, LParenLoc, ColonLoc, EndLoc, 17577 ReductionIdScopeSpec, ReductionId, 17578 UnresolvedReductions, RD)) 17579 return nullptr; 17580 17581 return OMPReductionClause::Create( 17582 Context, StartLoc, LParenLoc, ModifierLoc, ColonLoc, EndLoc, Modifier, 17583 RD.Vars, ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 17584 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.InscanCopyOps, 17585 RD.InscanCopyArrayTemps, RD.InscanCopyArrayElems, 17586 buildPreInits(Context, RD.ExprCaptures), 17587 buildPostUpdate(*this, RD.ExprPostUpdates)); 17588 } 17589 17590 OMPClause *Sema::ActOnOpenMPTaskReductionClause( 17591 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 17592 SourceLocation ColonLoc, SourceLocation EndLoc, 17593 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 17594 ArrayRef<Expr *> UnresolvedReductions) { 17595 ReductionData RD(VarList.size()); 17596 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction, VarList, 17597 StartLoc, LParenLoc, ColonLoc, EndLoc, 17598 ReductionIdScopeSpec, ReductionId, 17599 UnresolvedReductions, RD)) 17600 return nullptr; 17601 17602 return OMPTaskReductionClause::Create( 17603 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 17604 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 17605 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, 17606 buildPreInits(Context, RD.ExprCaptures), 17607 buildPostUpdate(*this, RD.ExprPostUpdates)); 17608 } 17609 17610 OMPClause *Sema::ActOnOpenMPInReductionClause( 17611 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 17612 SourceLocation ColonLoc, SourceLocation EndLoc, 17613 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 17614 ArrayRef<Expr *> UnresolvedReductions) { 17615 ReductionData RD(VarList.size()); 17616 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_in_reduction, VarList, 17617 StartLoc, LParenLoc, ColonLoc, EndLoc, 17618 ReductionIdScopeSpec, ReductionId, 17619 UnresolvedReductions, RD)) 17620 return nullptr; 17621 17622 return OMPInReductionClause::Create( 17623 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 17624 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 17625 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.TaskgroupDescriptors, 17626 buildPreInits(Context, RD.ExprCaptures), 17627 buildPostUpdate(*this, RD.ExprPostUpdates)); 17628 } 17629 17630 bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind, 17631 SourceLocation LinLoc) { 17632 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) || 17633 LinKind == OMPC_LINEAR_unknown) { 17634 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus; 17635 return true; 17636 } 17637 return false; 17638 } 17639 17640 bool Sema::CheckOpenMPLinearDecl(const ValueDecl *D, SourceLocation ELoc, 17641 OpenMPLinearClauseKind LinKind, QualType Type, 17642 bool IsDeclareSimd) { 17643 const auto *VD = dyn_cast_or_null<VarDecl>(D); 17644 // A variable must not have an incomplete type or a reference type. 17645 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type)) 17646 return true; 17647 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) && 17648 !Type->isReferenceType()) { 17649 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference) 17650 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind); 17651 return true; 17652 } 17653 Type = Type.getNonReferenceType(); 17654 17655 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions] 17656 // A variable that is privatized must not have a const-qualified type 17657 // unless it is of class type with a mutable member. This restriction does 17658 // not apply to the firstprivate clause, nor to the linear clause on 17659 // declarative directives (like declare simd). 17660 if (!IsDeclareSimd && 17661 rejectConstNotMutableType(*this, D, Type, OMPC_linear, ELoc)) 17662 return true; 17663 17664 // A list item must be of integral or pointer type. 17665 Type = Type.getUnqualifiedType().getCanonicalType(); 17666 const auto *Ty = Type.getTypePtrOrNull(); 17667 if (!Ty || (LinKind != OMPC_LINEAR_ref && !Ty->isDependentType() && 17668 !Ty->isIntegralType(Context) && !Ty->isPointerType())) { 17669 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type; 17670 if (D) { 17671 bool IsDecl = 17672 !VD || 17673 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 17674 Diag(D->getLocation(), 17675 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 17676 << D; 17677 } 17678 return true; 17679 } 17680 return false; 17681 } 17682 17683 OMPClause *Sema::ActOnOpenMPLinearClause( 17684 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc, 17685 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind, 17686 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 17687 SmallVector<Expr *, 8> Vars; 17688 SmallVector<Expr *, 8> Privates; 17689 SmallVector<Expr *, 8> Inits; 17690 SmallVector<Decl *, 4> ExprCaptures; 17691 SmallVector<Expr *, 4> ExprPostUpdates; 17692 if (CheckOpenMPLinearModifier(LinKind, LinLoc)) 17693 LinKind = OMPC_LINEAR_val; 17694 for (Expr *RefExpr : VarList) { 17695 assert(RefExpr && "NULL expr in OpenMP linear clause."); 17696 SourceLocation ELoc; 17697 SourceRange ERange; 17698 Expr *SimpleRefExpr = RefExpr; 17699 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 17700 if (Res.second) { 17701 // It will be analyzed later. 17702 Vars.push_back(RefExpr); 17703 Privates.push_back(nullptr); 17704 Inits.push_back(nullptr); 17705 } 17706 ValueDecl *D = Res.first; 17707 if (!D) 17708 continue; 17709 17710 QualType Type = D->getType(); 17711 auto *VD = dyn_cast<VarDecl>(D); 17712 17713 // OpenMP [2.14.3.7, linear clause] 17714 // A list-item cannot appear in more than one linear clause. 17715 // A list-item that appears in a linear clause cannot appear in any 17716 // other data-sharing attribute clause. 17717 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 17718 if (DVar.RefExpr) { 17719 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 17720 << getOpenMPClauseName(OMPC_linear); 17721 reportOriginalDsa(*this, DSAStack, D, DVar); 17722 continue; 17723 } 17724 17725 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type)) 17726 continue; 17727 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType(); 17728 17729 // Build private copy of original var. 17730 VarDecl *Private = 17731 buildVarDecl(*this, ELoc, Type, D->getName(), 17732 D->hasAttrs() ? &D->getAttrs() : nullptr, 17733 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 17734 DeclRefExpr *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc); 17735 // Build var to save initial value. 17736 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start"); 17737 Expr *InitExpr; 17738 DeclRefExpr *Ref = nullptr; 17739 if (!VD && !CurContext->isDependentContext()) { 17740 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 17741 if (!isOpenMPCapturedDecl(D)) { 17742 ExprCaptures.push_back(Ref->getDecl()); 17743 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) { 17744 ExprResult RefRes = DefaultLvalueConversion(Ref); 17745 if (!RefRes.isUsable()) 17746 continue; 17747 ExprResult PostUpdateRes = 17748 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, 17749 SimpleRefExpr, RefRes.get()); 17750 if (!PostUpdateRes.isUsable()) 17751 continue; 17752 ExprPostUpdates.push_back( 17753 IgnoredValueConversions(PostUpdateRes.get()).get()); 17754 } 17755 } 17756 } 17757 if (LinKind == OMPC_LINEAR_uval) 17758 InitExpr = VD ? VD->getInit() : SimpleRefExpr; 17759 else 17760 InitExpr = VD ? SimpleRefExpr : Ref; 17761 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(), 17762 /*DirectInit=*/false); 17763 DeclRefExpr *InitRef = buildDeclRefExpr(*this, Init, Type, ELoc); 17764 17765 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref); 17766 Vars.push_back((VD || CurContext->isDependentContext()) 17767 ? RefExpr->IgnoreParens() 17768 : Ref); 17769 Privates.push_back(PrivateRef); 17770 Inits.push_back(InitRef); 17771 } 17772 17773 if (Vars.empty()) 17774 return nullptr; 17775 17776 Expr *StepExpr = Step; 17777 Expr *CalcStepExpr = nullptr; 17778 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && 17779 !Step->isInstantiationDependent() && 17780 !Step->containsUnexpandedParameterPack()) { 17781 SourceLocation StepLoc = Step->getBeginLoc(); 17782 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step); 17783 if (Val.isInvalid()) 17784 return nullptr; 17785 StepExpr = Val.get(); 17786 17787 // Build var to save the step value. 17788 VarDecl *SaveVar = 17789 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step"); 17790 ExprResult SaveRef = 17791 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc); 17792 ExprResult CalcStep = 17793 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr); 17794 CalcStep = ActOnFinishFullExpr(CalcStep.get(), /*DiscardedValue*/ false); 17795 17796 // Warn about zero linear step (it would be probably better specified as 17797 // making corresponding variables 'const'). 17798 if (Optional<llvm::APSInt> Result = 17799 StepExpr->getIntegerConstantExpr(Context)) { 17800 if (!Result->isNegative() && !Result->isStrictlyPositive()) 17801 Diag(StepLoc, diag::warn_omp_linear_step_zero) 17802 << Vars[0] << (Vars.size() > 1); 17803 } else if (CalcStep.isUsable()) { 17804 // Calculate the step beforehand instead of doing this on each iteration. 17805 // (This is not used if the number of iterations may be kfold-ed). 17806 CalcStepExpr = CalcStep.get(); 17807 } 17808 } 17809 17810 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc, 17811 ColonLoc, EndLoc, Vars, Privates, Inits, 17812 StepExpr, CalcStepExpr, 17813 buildPreInits(Context, ExprCaptures), 17814 buildPostUpdate(*this, ExprPostUpdates)); 17815 } 17816 17817 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, 17818 Expr *NumIterations, Sema &SemaRef, 17819 Scope *S, DSAStackTy *Stack) { 17820 // Walk the vars and build update/final expressions for the CodeGen. 17821 SmallVector<Expr *, 8> Updates; 17822 SmallVector<Expr *, 8> Finals; 17823 SmallVector<Expr *, 8> UsedExprs; 17824 Expr *Step = Clause.getStep(); 17825 Expr *CalcStep = Clause.getCalcStep(); 17826 // OpenMP [2.14.3.7, linear clause] 17827 // If linear-step is not specified it is assumed to be 1. 17828 if (!Step) 17829 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(); 17830 else if (CalcStep) 17831 Step = cast<BinaryOperator>(CalcStep)->getLHS(); 17832 bool HasErrors = false; 17833 auto CurInit = Clause.inits().begin(); 17834 auto CurPrivate = Clause.privates().begin(); 17835 OpenMPLinearClauseKind LinKind = Clause.getModifier(); 17836 for (Expr *RefExpr : Clause.varlists()) { 17837 SourceLocation ELoc; 17838 SourceRange ERange; 17839 Expr *SimpleRefExpr = RefExpr; 17840 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange); 17841 ValueDecl *D = Res.first; 17842 if (Res.second || !D) { 17843 Updates.push_back(nullptr); 17844 Finals.push_back(nullptr); 17845 HasErrors = true; 17846 continue; 17847 } 17848 auto &&Info = Stack->isLoopControlVariable(D); 17849 // OpenMP [2.15.11, distribute simd Construct] 17850 // A list item may not appear in a linear clause, unless it is the loop 17851 // iteration variable. 17852 if (isOpenMPDistributeDirective(Stack->getCurrentDirective()) && 17853 isOpenMPSimdDirective(Stack->getCurrentDirective()) && !Info.first) { 17854 SemaRef.Diag(ELoc, 17855 diag::err_omp_linear_distribute_var_non_loop_iteration); 17856 Updates.push_back(nullptr); 17857 Finals.push_back(nullptr); 17858 HasErrors = true; 17859 continue; 17860 } 17861 Expr *InitExpr = *CurInit; 17862 17863 // Build privatized reference to the current linear var. 17864 auto *DE = cast<DeclRefExpr>(SimpleRefExpr); 17865 Expr *CapturedRef; 17866 if (LinKind == OMPC_LINEAR_uval) 17867 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit(); 17868 else 17869 CapturedRef = 17870 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()), 17871 DE->getType().getUnqualifiedType(), DE->getExprLoc(), 17872 /*RefersToCapture=*/true); 17873 17874 // Build update: Var = InitExpr + IV * Step 17875 ExprResult Update; 17876 if (!Info.first) 17877 Update = buildCounterUpdate( 17878 SemaRef, S, RefExpr->getExprLoc(), *CurPrivate, InitExpr, IV, Step, 17879 /*Subtract=*/false, /*IsNonRectangularLB=*/false); 17880 else 17881 Update = *CurPrivate; 17882 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getBeginLoc(), 17883 /*DiscardedValue*/ false); 17884 17885 // Build final: Var = InitExpr + NumIterations * Step 17886 ExprResult Final; 17887 if (!Info.first) 17888 Final = 17889 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef, 17890 InitExpr, NumIterations, Step, /*Subtract=*/false, 17891 /*IsNonRectangularLB=*/false); 17892 else 17893 Final = *CurPrivate; 17894 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getBeginLoc(), 17895 /*DiscardedValue*/ false); 17896 17897 if (!Update.isUsable() || !Final.isUsable()) { 17898 Updates.push_back(nullptr); 17899 Finals.push_back(nullptr); 17900 UsedExprs.push_back(nullptr); 17901 HasErrors = true; 17902 } else { 17903 Updates.push_back(Update.get()); 17904 Finals.push_back(Final.get()); 17905 if (!Info.first) 17906 UsedExprs.push_back(SimpleRefExpr); 17907 } 17908 ++CurInit; 17909 ++CurPrivate; 17910 } 17911 if (Expr *S = Clause.getStep()) 17912 UsedExprs.push_back(S); 17913 // Fill the remaining part with the nullptr. 17914 UsedExprs.append(Clause.varlist_size() + 1 - UsedExprs.size(), nullptr); 17915 Clause.setUpdates(Updates); 17916 Clause.setFinals(Finals); 17917 Clause.setUsedExprs(UsedExprs); 17918 return HasErrors; 17919 } 17920 17921 OMPClause *Sema::ActOnOpenMPAlignedClause( 17922 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc, 17923 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 17924 SmallVector<Expr *, 8> Vars; 17925 for (Expr *RefExpr : VarList) { 17926 assert(RefExpr && "NULL expr in OpenMP linear clause."); 17927 SourceLocation ELoc; 17928 SourceRange ERange; 17929 Expr *SimpleRefExpr = RefExpr; 17930 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 17931 if (Res.second) { 17932 // It will be analyzed later. 17933 Vars.push_back(RefExpr); 17934 } 17935 ValueDecl *D = Res.first; 17936 if (!D) 17937 continue; 17938 17939 QualType QType = D->getType(); 17940 auto *VD = dyn_cast<VarDecl>(D); 17941 17942 // OpenMP [2.8.1, simd construct, Restrictions] 17943 // The type of list items appearing in the aligned clause must be 17944 // array, pointer, reference to array, or reference to pointer. 17945 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType(); 17946 const Type *Ty = QType.getTypePtrOrNull(); 17947 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) { 17948 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr) 17949 << QType << getLangOpts().CPlusPlus << ERange; 17950 bool IsDecl = 17951 !VD || 17952 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 17953 Diag(D->getLocation(), 17954 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 17955 << D; 17956 continue; 17957 } 17958 17959 // OpenMP [2.8.1, simd construct, Restrictions] 17960 // A list-item cannot appear in more than one aligned clause. 17961 if (const Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) { 17962 Diag(ELoc, diag::err_omp_used_in_clause_twice) 17963 << 0 << getOpenMPClauseName(OMPC_aligned) << ERange; 17964 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa) 17965 << getOpenMPClauseName(OMPC_aligned); 17966 continue; 17967 } 17968 17969 DeclRefExpr *Ref = nullptr; 17970 if (!VD && isOpenMPCapturedDecl(D)) 17971 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 17972 Vars.push_back(DefaultFunctionArrayConversion( 17973 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref) 17974 .get()); 17975 } 17976 17977 // OpenMP [2.8.1, simd construct, Description] 17978 // The parameter of the aligned clause, alignment, must be a constant 17979 // positive integer expression. 17980 // If no optional parameter is specified, implementation-defined default 17981 // alignments for SIMD instructions on the target platforms are assumed. 17982 if (Alignment != nullptr) { 17983 ExprResult AlignResult = 17984 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned); 17985 if (AlignResult.isInvalid()) 17986 return nullptr; 17987 Alignment = AlignResult.get(); 17988 } 17989 if (Vars.empty()) 17990 return nullptr; 17991 17992 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc, 17993 EndLoc, Vars, Alignment); 17994 } 17995 17996 OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList, 17997 SourceLocation StartLoc, 17998 SourceLocation LParenLoc, 17999 SourceLocation EndLoc) { 18000 SmallVector<Expr *, 8> Vars; 18001 SmallVector<Expr *, 8> SrcExprs; 18002 SmallVector<Expr *, 8> DstExprs; 18003 SmallVector<Expr *, 8> AssignmentOps; 18004 for (Expr *RefExpr : VarList) { 18005 assert(RefExpr && "NULL expr in OpenMP copyin clause."); 18006 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 18007 // It will be analyzed later. 18008 Vars.push_back(RefExpr); 18009 SrcExprs.push_back(nullptr); 18010 DstExprs.push_back(nullptr); 18011 AssignmentOps.push_back(nullptr); 18012 continue; 18013 } 18014 18015 SourceLocation ELoc = RefExpr->getExprLoc(); 18016 // OpenMP [2.1, C/C++] 18017 // A list item is a variable name. 18018 // OpenMP [2.14.4.1, Restrictions, p.1] 18019 // A list item that appears in a copyin clause must be threadprivate. 18020 auto *DE = dyn_cast<DeclRefExpr>(RefExpr); 18021 if (!DE || !isa<VarDecl>(DE->getDecl())) { 18022 Diag(ELoc, diag::err_omp_expected_var_name_member_expr) 18023 << 0 << RefExpr->getSourceRange(); 18024 continue; 18025 } 18026 18027 Decl *D = DE->getDecl(); 18028 auto *VD = cast<VarDecl>(D); 18029 18030 QualType Type = VD->getType(); 18031 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 18032 // It will be analyzed later. 18033 Vars.push_back(DE); 18034 SrcExprs.push_back(nullptr); 18035 DstExprs.push_back(nullptr); 18036 AssignmentOps.push_back(nullptr); 18037 continue; 18038 } 18039 18040 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1] 18041 // A list item that appears in a copyin clause must be threadprivate. 18042 if (!DSAStack->isThreadPrivate(VD)) { 18043 Diag(ELoc, diag::err_omp_required_access) 18044 << getOpenMPClauseName(OMPC_copyin) 18045 << getOpenMPDirectiveName(OMPD_threadprivate); 18046 continue; 18047 } 18048 18049 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 18050 // A variable of class type (or array thereof) that appears in a 18051 // copyin clause requires an accessible, unambiguous copy assignment 18052 // operator for the class type. 18053 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType(); 18054 VarDecl *SrcVD = 18055 buildVarDecl(*this, DE->getBeginLoc(), ElemType.getUnqualifiedType(), 18056 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr); 18057 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr( 18058 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc()); 18059 VarDecl *DstVD = 18060 buildVarDecl(*this, DE->getBeginLoc(), ElemType, ".copyin.dst", 18061 VD->hasAttrs() ? &VD->getAttrs() : nullptr); 18062 DeclRefExpr *PseudoDstExpr = 18063 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc()); 18064 // For arrays generate assignment operation for single element and replace 18065 // it by the original array element in CodeGen. 18066 ExprResult AssignmentOp = 18067 BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, PseudoDstExpr, 18068 PseudoSrcExpr); 18069 if (AssignmentOp.isInvalid()) 18070 continue; 18071 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(), 18072 /*DiscardedValue*/ false); 18073 if (AssignmentOp.isInvalid()) 18074 continue; 18075 18076 DSAStack->addDSA(VD, DE, OMPC_copyin); 18077 Vars.push_back(DE); 18078 SrcExprs.push_back(PseudoSrcExpr); 18079 DstExprs.push_back(PseudoDstExpr); 18080 AssignmentOps.push_back(AssignmentOp.get()); 18081 } 18082 18083 if (Vars.empty()) 18084 return nullptr; 18085 18086 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 18087 SrcExprs, DstExprs, AssignmentOps); 18088 } 18089 18090 OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList, 18091 SourceLocation StartLoc, 18092 SourceLocation LParenLoc, 18093 SourceLocation EndLoc) { 18094 SmallVector<Expr *, 8> Vars; 18095 SmallVector<Expr *, 8> SrcExprs; 18096 SmallVector<Expr *, 8> DstExprs; 18097 SmallVector<Expr *, 8> AssignmentOps; 18098 for (Expr *RefExpr : VarList) { 18099 assert(RefExpr && "NULL expr in OpenMP linear clause."); 18100 SourceLocation ELoc; 18101 SourceRange ERange; 18102 Expr *SimpleRefExpr = RefExpr; 18103 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 18104 if (Res.second) { 18105 // It will be analyzed later. 18106 Vars.push_back(RefExpr); 18107 SrcExprs.push_back(nullptr); 18108 DstExprs.push_back(nullptr); 18109 AssignmentOps.push_back(nullptr); 18110 } 18111 ValueDecl *D = Res.first; 18112 if (!D) 18113 continue; 18114 18115 QualType Type = D->getType(); 18116 auto *VD = dyn_cast<VarDecl>(D); 18117 18118 // OpenMP [2.14.4.2, Restrictions, p.2] 18119 // A list item that appears in a copyprivate clause may not appear in a 18120 // private or firstprivate clause on the single construct. 18121 if (!VD || !DSAStack->isThreadPrivate(VD)) { 18122 DSAStackTy::DSAVarData DVar = 18123 DSAStack->getTopDSA(D, /*FromParent=*/false); 18124 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate && 18125 DVar.RefExpr) { 18126 Diag(ELoc, diag::err_omp_wrong_dsa) 18127 << getOpenMPClauseName(DVar.CKind) 18128 << getOpenMPClauseName(OMPC_copyprivate); 18129 reportOriginalDsa(*this, DSAStack, D, DVar); 18130 continue; 18131 } 18132 18133 // OpenMP [2.11.4.2, Restrictions, p.1] 18134 // All list items that appear in a copyprivate clause must be either 18135 // threadprivate or private in the enclosing context. 18136 if (DVar.CKind == OMPC_unknown) { 18137 DVar = DSAStack->getImplicitDSA(D, false); 18138 if (DVar.CKind == OMPC_shared) { 18139 Diag(ELoc, diag::err_omp_required_access) 18140 << getOpenMPClauseName(OMPC_copyprivate) 18141 << "threadprivate or private in the enclosing context"; 18142 reportOriginalDsa(*this, DSAStack, D, DVar); 18143 continue; 18144 } 18145 } 18146 } 18147 18148 // Variably modified types are not supported. 18149 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) { 18150 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 18151 << getOpenMPClauseName(OMPC_copyprivate) << Type 18152 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 18153 bool IsDecl = 18154 !VD || 18155 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 18156 Diag(D->getLocation(), 18157 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 18158 << D; 18159 continue; 18160 } 18161 18162 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 18163 // A variable of class type (or array thereof) that appears in a 18164 // copyin clause requires an accessible, unambiguous copy assignment 18165 // operator for the class type. 18166 Type = Context.getBaseElementType(Type.getNonReferenceType()) 18167 .getUnqualifiedType(); 18168 VarDecl *SrcVD = 18169 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.src", 18170 D->hasAttrs() ? &D->getAttrs() : nullptr); 18171 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc); 18172 VarDecl *DstVD = 18173 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.dst", 18174 D->hasAttrs() ? &D->getAttrs() : nullptr); 18175 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc); 18176 ExprResult AssignmentOp = BuildBinOp( 18177 DSAStack->getCurScope(), ELoc, BO_Assign, PseudoDstExpr, PseudoSrcExpr); 18178 if (AssignmentOp.isInvalid()) 18179 continue; 18180 AssignmentOp = 18181 ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false); 18182 if (AssignmentOp.isInvalid()) 18183 continue; 18184 18185 // No need to mark vars as copyprivate, they are already threadprivate or 18186 // implicitly private. 18187 assert(VD || isOpenMPCapturedDecl(D)); 18188 Vars.push_back( 18189 VD ? RefExpr->IgnoreParens() 18190 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false)); 18191 SrcExprs.push_back(PseudoSrcExpr); 18192 DstExprs.push_back(PseudoDstExpr); 18193 AssignmentOps.push_back(AssignmentOp.get()); 18194 } 18195 18196 if (Vars.empty()) 18197 return nullptr; 18198 18199 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 18200 Vars, SrcExprs, DstExprs, AssignmentOps); 18201 } 18202 18203 OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList, 18204 SourceLocation StartLoc, 18205 SourceLocation LParenLoc, 18206 SourceLocation EndLoc) { 18207 if (VarList.empty()) 18208 return nullptr; 18209 18210 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList); 18211 } 18212 18213 /// Tries to find omp_depend_t. type. 18214 static bool findOMPDependT(Sema &S, SourceLocation Loc, DSAStackTy *Stack, 18215 bool Diagnose = true) { 18216 QualType OMPDependT = Stack->getOMPDependT(); 18217 if (!OMPDependT.isNull()) 18218 return true; 18219 IdentifierInfo *II = &S.PP.getIdentifierTable().get("omp_depend_t"); 18220 ParsedType PT = S.getTypeName(*II, Loc, S.getCurScope()); 18221 if (!PT.getAsOpaquePtr() || PT.get().isNull()) { 18222 if (Diagnose) 18223 S.Diag(Loc, diag::err_omp_implied_type_not_found) << "omp_depend_t"; 18224 return false; 18225 } 18226 Stack->setOMPDependT(PT.get()); 18227 return true; 18228 } 18229 18230 OMPClause *Sema::ActOnOpenMPDepobjClause(Expr *Depobj, SourceLocation StartLoc, 18231 SourceLocation LParenLoc, 18232 SourceLocation EndLoc) { 18233 if (!Depobj) 18234 return nullptr; 18235 18236 bool OMPDependTFound = findOMPDependT(*this, StartLoc, DSAStack); 18237 18238 // OpenMP 5.0, 2.17.10.1 depobj Construct 18239 // depobj is an lvalue expression of type omp_depend_t. 18240 if (!Depobj->isTypeDependent() && !Depobj->isValueDependent() && 18241 !Depobj->isInstantiationDependent() && 18242 !Depobj->containsUnexpandedParameterPack() && 18243 (OMPDependTFound && 18244 !Context.typesAreCompatible(DSAStack->getOMPDependT(), Depobj->getType(), 18245 /*CompareUnqualified=*/true))) { 18246 Diag(Depobj->getExprLoc(), diag::err_omp_expected_omp_depend_t_lvalue) 18247 << 0 << Depobj->getType() << Depobj->getSourceRange(); 18248 } 18249 18250 if (!Depobj->isLValue()) { 18251 Diag(Depobj->getExprLoc(), diag::err_omp_expected_omp_depend_t_lvalue) 18252 << 1 << Depobj->getSourceRange(); 18253 } 18254 18255 return OMPDepobjClause::Create(Context, StartLoc, LParenLoc, EndLoc, Depobj); 18256 } 18257 18258 OMPClause * 18259 Sema::ActOnOpenMPDependClause(Expr *DepModifier, OpenMPDependClauseKind DepKind, 18260 SourceLocation DepLoc, SourceLocation ColonLoc, 18261 ArrayRef<Expr *> VarList, SourceLocation StartLoc, 18262 SourceLocation LParenLoc, SourceLocation EndLoc) { 18263 if (DSAStack->getCurrentDirective() == OMPD_ordered && 18264 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) { 18265 Diag(DepLoc, diag::err_omp_unexpected_clause_value) 18266 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend); 18267 return nullptr; 18268 } 18269 if ((DSAStack->getCurrentDirective() != OMPD_ordered || 18270 DSAStack->getCurrentDirective() == OMPD_depobj) && 18271 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source || 18272 DepKind == OMPC_DEPEND_sink || 18273 ((LangOpts.OpenMP < 50 || 18274 DSAStack->getCurrentDirective() == OMPD_depobj) && 18275 DepKind == OMPC_DEPEND_depobj))) { 18276 SmallVector<unsigned, 3> Except; 18277 Except.push_back(OMPC_DEPEND_source); 18278 Except.push_back(OMPC_DEPEND_sink); 18279 if (LangOpts.OpenMP < 50 || DSAStack->getCurrentDirective() == OMPD_depobj) 18280 Except.push_back(OMPC_DEPEND_depobj); 18281 std::string Expected = (LangOpts.OpenMP >= 50 && !DepModifier) 18282 ? "depend modifier(iterator) or " 18283 : ""; 18284 Diag(DepLoc, diag::err_omp_unexpected_clause_value) 18285 << Expected + getListOfPossibleValues(OMPC_depend, /*First=*/0, 18286 /*Last=*/OMPC_DEPEND_unknown, 18287 Except) 18288 << getOpenMPClauseName(OMPC_depend); 18289 return nullptr; 18290 } 18291 if (DepModifier && 18292 (DepKind == OMPC_DEPEND_source || DepKind == OMPC_DEPEND_sink)) { 18293 Diag(DepModifier->getExprLoc(), 18294 diag::err_omp_depend_sink_source_with_modifier); 18295 return nullptr; 18296 } 18297 if (DepModifier && 18298 !DepModifier->getType()->isSpecificBuiltinType(BuiltinType::OMPIterator)) 18299 Diag(DepModifier->getExprLoc(), diag::err_omp_depend_modifier_not_iterator); 18300 18301 SmallVector<Expr *, 8> Vars; 18302 DSAStackTy::OperatorOffsetTy OpsOffs; 18303 llvm::APSInt DepCounter(/*BitWidth=*/32); 18304 llvm::APSInt TotalDepCount(/*BitWidth=*/32); 18305 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) { 18306 if (const Expr *OrderedCountExpr = 18307 DSAStack->getParentOrderedRegionParam().first) { 18308 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context); 18309 TotalDepCount.setIsUnsigned(/*Val=*/true); 18310 } 18311 } 18312 for (Expr *RefExpr : VarList) { 18313 assert(RefExpr && "NULL expr in OpenMP shared clause."); 18314 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 18315 // It will be analyzed later. 18316 Vars.push_back(RefExpr); 18317 continue; 18318 } 18319 18320 SourceLocation ELoc = RefExpr->getExprLoc(); 18321 Expr *SimpleExpr = RefExpr->IgnoreParenCasts(); 18322 if (DepKind == OMPC_DEPEND_sink) { 18323 if (DSAStack->getParentOrderedRegionParam().first && 18324 DepCounter >= TotalDepCount) { 18325 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr); 18326 continue; 18327 } 18328 ++DepCounter; 18329 // OpenMP [2.13.9, Summary] 18330 // depend(dependence-type : vec), where dependence-type is: 18331 // 'sink' and where vec is the iteration vector, which has the form: 18332 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn] 18333 // where n is the value specified by the ordered clause in the loop 18334 // directive, xi denotes the loop iteration variable of the i-th nested 18335 // loop associated with the loop directive, and di is a constant 18336 // non-negative integer. 18337 if (CurContext->isDependentContext()) { 18338 // It will be analyzed later. 18339 Vars.push_back(RefExpr); 18340 continue; 18341 } 18342 SimpleExpr = SimpleExpr->IgnoreImplicit(); 18343 OverloadedOperatorKind OOK = OO_None; 18344 SourceLocation OOLoc; 18345 Expr *LHS = SimpleExpr; 18346 Expr *RHS = nullptr; 18347 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) { 18348 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode()); 18349 OOLoc = BO->getOperatorLoc(); 18350 LHS = BO->getLHS()->IgnoreParenImpCasts(); 18351 RHS = BO->getRHS()->IgnoreParenImpCasts(); 18352 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) { 18353 OOK = OCE->getOperator(); 18354 OOLoc = OCE->getOperatorLoc(); 18355 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); 18356 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts(); 18357 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) { 18358 OOK = MCE->getMethodDecl() 18359 ->getNameInfo() 18360 .getName() 18361 .getCXXOverloadedOperator(); 18362 OOLoc = MCE->getCallee()->getExprLoc(); 18363 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts(); 18364 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); 18365 } 18366 SourceLocation ELoc; 18367 SourceRange ERange; 18368 auto Res = getPrivateItem(*this, LHS, ELoc, ERange); 18369 if (Res.second) { 18370 // It will be analyzed later. 18371 Vars.push_back(RefExpr); 18372 } 18373 ValueDecl *D = Res.first; 18374 if (!D) 18375 continue; 18376 18377 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) { 18378 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus); 18379 continue; 18380 } 18381 if (RHS) { 18382 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause( 18383 RHS, OMPC_depend, /*StrictlyPositive=*/false); 18384 if (RHSRes.isInvalid()) 18385 continue; 18386 } 18387 if (!CurContext->isDependentContext() && 18388 DSAStack->getParentOrderedRegionParam().first && 18389 DepCounter != DSAStack->isParentLoopControlVariable(D).first) { 18390 const ValueDecl *VD = 18391 DSAStack->getParentLoopControlVariable(DepCounter.getZExtValue()); 18392 if (VD) 18393 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) 18394 << 1 << VD; 18395 else 18396 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) << 0; 18397 continue; 18398 } 18399 OpsOffs.emplace_back(RHS, OOK); 18400 } else { 18401 bool OMPDependTFound = LangOpts.OpenMP >= 50; 18402 if (OMPDependTFound) 18403 OMPDependTFound = findOMPDependT(*this, StartLoc, DSAStack, 18404 DepKind == OMPC_DEPEND_depobj); 18405 if (DepKind == OMPC_DEPEND_depobj) { 18406 // OpenMP 5.0, 2.17.11 depend Clause, Restrictions, C/C++ 18407 // List items used in depend clauses with the depobj dependence type 18408 // must be expressions of the omp_depend_t type. 18409 if (!RefExpr->isValueDependent() && !RefExpr->isTypeDependent() && 18410 !RefExpr->isInstantiationDependent() && 18411 !RefExpr->containsUnexpandedParameterPack() && 18412 (OMPDependTFound && 18413 !Context.hasSameUnqualifiedType(DSAStack->getOMPDependT(), 18414 RefExpr->getType()))) { 18415 Diag(ELoc, diag::err_omp_expected_omp_depend_t_lvalue) 18416 << 0 << RefExpr->getType() << RefExpr->getSourceRange(); 18417 continue; 18418 } 18419 if (!RefExpr->isLValue()) { 18420 Diag(ELoc, diag::err_omp_expected_omp_depend_t_lvalue) 18421 << 1 << RefExpr->getType() << RefExpr->getSourceRange(); 18422 continue; 18423 } 18424 } else { 18425 // OpenMP 5.0 [2.17.11, Restrictions] 18426 // List items used in depend clauses cannot be zero-length array 18427 // sections. 18428 QualType ExprTy = RefExpr->getType().getNonReferenceType(); 18429 const auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr); 18430 if (OASE) { 18431 QualType BaseType = 18432 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 18433 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 18434 ExprTy = ATy->getElementType(); 18435 else 18436 ExprTy = BaseType->getPointeeType(); 18437 ExprTy = ExprTy.getNonReferenceType(); 18438 const Expr *Length = OASE->getLength(); 18439 Expr::EvalResult Result; 18440 if (Length && !Length->isValueDependent() && 18441 Length->EvaluateAsInt(Result, Context) && 18442 Result.Val.getInt().isZero()) { 18443 Diag(ELoc, 18444 diag::err_omp_depend_zero_length_array_section_not_allowed) 18445 << SimpleExpr->getSourceRange(); 18446 continue; 18447 } 18448 } 18449 18450 // OpenMP 5.0, 2.17.11 depend Clause, Restrictions, C/C++ 18451 // List items used in depend clauses with the in, out, inout or 18452 // mutexinoutset dependence types cannot be expressions of the 18453 // omp_depend_t type. 18454 if (!RefExpr->isValueDependent() && !RefExpr->isTypeDependent() && 18455 !RefExpr->isInstantiationDependent() && 18456 !RefExpr->containsUnexpandedParameterPack() && 18457 (OMPDependTFound && 18458 DSAStack->getOMPDependT().getTypePtr() == ExprTy.getTypePtr())) { 18459 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 18460 << (LangOpts.OpenMP >= 50 ? 1 : 0) << 1 18461 << RefExpr->getSourceRange(); 18462 continue; 18463 } 18464 18465 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr); 18466 if (!RefExpr->IgnoreParenImpCasts()->isLValue() || 18467 (ASE && !ASE->getBase()->isTypeDependent() && 18468 !ASE->getBase() 18469 ->getType() 18470 .getNonReferenceType() 18471 ->isPointerType() && 18472 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) { 18473 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 18474 << (LangOpts.OpenMP >= 50 ? 1 : 0) 18475 << (LangOpts.OpenMP >= 50 ? 1 : 0) << RefExpr->getSourceRange(); 18476 continue; 18477 } 18478 18479 ExprResult Res; 18480 { 18481 Sema::TentativeAnalysisScope Trap(*this); 18482 Res = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, 18483 RefExpr->IgnoreParenImpCasts()); 18484 } 18485 if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr) && 18486 !isa<OMPArrayShapingExpr>(SimpleExpr)) { 18487 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 18488 << (LangOpts.OpenMP >= 50 ? 1 : 0) 18489 << (LangOpts.OpenMP >= 50 ? 1 : 0) << RefExpr->getSourceRange(); 18490 continue; 18491 } 18492 } 18493 } 18494 Vars.push_back(RefExpr->IgnoreParenImpCasts()); 18495 } 18496 18497 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink && 18498 TotalDepCount > VarList.size() && 18499 DSAStack->getParentOrderedRegionParam().first && 18500 DSAStack->getParentLoopControlVariable(VarList.size() + 1)) { 18501 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration) 18502 << 1 << DSAStack->getParentLoopControlVariable(VarList.size() + 1); 18503 } 18504 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink && 18505 Vars.empty()) 18506 return nullptr; 18507 18508 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, 18509 DepModifier, DepKind, DepLoc, ColonLoc, 18510 Vars, TotalDepCount.getZExtValue()); 18511 if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) && 18512 DSAStack->isParentOrderedRegion()) 18513 DSAStack->addDoacrossDependClause(C, OpsOffs); 18514 return C; 18515 } 18516 18517 OMPClause *Sema::ActOnOpenMPDeviceClause(OpenMPDeviceClauseModifier Modifier, 18518 Expr *Device, SourceLocation StartLoc, 18519 SourceLocation LParenLoc, 18520 SourceLocation ModifierLoc, 18521 SourceLocation EndLoc) { 18522 assert((ModifierLoc.isInvalid() || LangOpts.OpenMP >= 50) && 18523 "Unexpected device modifier in OpenMP < 50."); 18524 18525 bool ErrorFound = false; 18526 if (ModifierLoc.isValid() && Modifier == OMPC_DEVICE_unknown) { 18527 std::string Values = 18528 getListOfPossibleValues(OMPC_device, /*First=*/0, OMPC_DEVICE_unknown); 18529 Diag(ModifierLoc, diag::err_omp_unexpected_clause_value) 18530 << Values << getOpenMPClauseName(OMPC_device); 18531 ErrorFound = true; 18532 } 18533 18534 Expr *ValExpr = Device; 18535 Stmt *HelperValStmt = nullptr; 18536 18537 // OpenMP [2.9.1, Restrictions] 18538 // The device expression must evaluate to a non-negative integer value. 18539 ErrorFound = !isNonNegativeIntegerValue(ValExpr, *this, OMPC_device, 18540 /*StrictlyPositive=*/false) || 18541 ErrorFound; 18542 if (ErrorFound) 18543 return nullptr; 18544 18545 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 18546 OpenMPDirectiveKind CaptureRegion = 18547 getOpenMPCaptureRegionForClause(DKind, OMPC_device, LangOpts.OpenMP); 18548 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 18549 ValExpr = MakeFullExpr(ValExpr).get(); 18550 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 18551 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 18552 HelperValStmt = buildPreInits(Context, Captures); 18553 } 18554 18555 return new (Context) 18556 OMPDeviceClause(Modifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc, 18557 LParenLoc, ModifierLoc, EndLoc); 18558 } 18559 18560 static bool checkTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef, 18561 DSAStackTy *Stack, QualType QTy, 18562 bool FullCheck = true) { 18563 if (SemaRef.RequireCompleteType(SL, QTy, diag::err_incomplete_type)) 18564 return false; 18565 if (FullCheck && !SemaRef.CurContext->isDependentContext() && 18566 !QTy.isTriviallyCopyableType(SemaRef.Context)) 18567 SemaRef.Diag(SL, diag::warn_omp_non_trivial_type_mapped) << QTy << SR; 18568 return true; 18569 } 18570 18571 /// Return true if it can be proven that the provided array expression 18572 /// (array section or array subscript) does NOT specify the whole size of the 18573 /// array whose base type is \a BaseQTy. 18574 static bool checkArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef, 18575 const Expr *E, 18576 QualType BaseQTy) { 18577 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E); 18578 18579 // If this is an array subscript, it refers to the whole size if the size of 18580 // the dimension is constant and equals 1. Also, an array section assumes the 18581 // format of an array subscript if no colon is used. 18582 if (isa<ArraySubscriptExpr>(E) || 18583 (OASE && OASE->getColonLocFirst().isInvalid())) { 18584 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) 18585 return ATy->getSize().getSExtValue() != 1; 18586 // Size can't be evaluated statically. 18587 return false; 18588 } 18589 18590 assert(OASE && "Expecting array section if not an array subscript."); 18591 const Expr *LowerBound = OASE->getLowerBound(); 18592 const Expr *Length = OASE->getLength(); 18593 18594 // If there is a lower bound that does not evaluates to zero, we are not 18595 // covering the whole dimension. 18596 if (LowerBound) { 18597 Expr::EvalResult Result; 18598 if (!LowerBound->EvaluateAsInt(Result, SemaRef.getASTContext())) 18599 return false; // Can't get the integer value as a constant. 18600 18601 llvm::APSInt ConstLowerBound = Result.Val.getInt(); 18602 if (ConstLowerBound.getSExtValue()) 18603 return true; 18604 } 18605 18606 // If we don't have a length we covering the whole dimension. 18607 if (!Length) 18608 return false; 18609 18610 // If the base is a pointer, we don't have a way to get the size of the 18611 // pointee. 18612 if (BaseQTy->isPointerType()) 18613 return false; 18614 18615 // We can only check if the length is the same as the size of the dimension 18616 // if we have a constant array. 18617 const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()); 18618 if (!CATy) 18619 return false; 18620 18621 Expr::EvalResult Result; 18622 if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext())) 18623 return false; // Can't get the integer value as a constant. 18624 18625 llvm::APSInt ConstLength = Result.Val.getInt(); 18626 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue(); 18627 } 18628 18629 // Return true if it can be proven that the provided array expression (array 18630 // section or array subscript) does NOT specify a single element of the array 18631 // whose base type is \a BaseQTy. 18632 static bool checkArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef, 18633 const Expr *E, 18634 QualType BaseQTy) { 18635 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E); 18636 18637 // An array subscript always refer to a single element. Also, an array section 18638 // assumes the format of an array subscript if no colon is used. 18639 if (isa<ArraySubscriptExpr>(E) || 18640 (OASE && OASE->getColonLocFirst().isInvalid())) 18641 return false; 18642 18643 assert(OASE && "Expecting array section if not an array subscript."); 18644 const Expr *Length = OASE->getLength(); 18645 18646 // If we don't have a length we have to check if the array has unitary size 18647 // for this dimension. Also, we should always expect a length if the base type 18648 // is pointer. 18649 if (!Length) { 18650 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) 18651 return ATy->getSize().getSExtValue() != 1; 18652 // We cannot assume anything. 18653 return false; 18654 } 18655 18656 // Check if the length evaluates to 1. 18657 Expr::EvalResult Result; 18658 if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext())) 18659 return false; // Can't get the integer value as a constant. 18660 18661 llvm::APSInt ConstLength = Result.Val.getInt(); 18662 return ConstLength.getSExtValue() != 1; 18663 } 18664 18665 // The base of elements of list in a map clause have to be either: 18666 // - a reference to variable or field. 18667 // - a member expression. 18668 // - an array expression. 18669 // 18670 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the 18671 // reference to 'r'. 18672 // 18673 // If we have: 18674 // 18675 // struct SS { 18676 // Bla S; 18677 // foo() { 18678 // #pragma omp target map (S.Arr[:12]); 18679 // } 18680 // } 18681 // 18682 // We want to retrieve the member expression 'this->S'; 18683 18684 // OpenMP 5.0 [2.19.7.1, map Clause, Restrictions, p.2] 18685 // If a list item is an array section, it must specify contiguous storage. 18686 // 18687 // For this restriction it is sufficient that we make sure only references 18688 // to variables or fields and array expressions, and that no array sections 18689 // exist except in the rightmost expression (unless they cover the whole 18690 // dimension of the array). E.g. these would be invalid: 18691 // 18692 // r.ArrS[3:5].Arr[6:7] 18693 // 18694 // r.ArrS[3:5].x 18695 // 18696 // but these would be valid: 18697 // r.ArrS[3].Arr[6:7] 18698 // 18699 // r.ArrS[3].x 18700 namespace { 18701 class MapBaseChecker final : public StmtVisitor<MapBaseChecker, bool> { 18702 Sema &SemaRef; 18703 OpenMPClauseKind CKind = OMPC_unknown; 18704 OpenMPDirectiveKind DKind = OMPD_unknown; 18705 OMPClauseMappableExprCommon::MappableExprComponentList &Components; 18706 bool IsNonContiguous = false; 18707 bool NoDiagnose = false; 18708 const Expr *RelevantExpr = nullptr; 18709 bool AllowUnitySizeArraySection = true; 18710 bool AllowWholeSizeArraySection = true; 18711 bool AllowAnotherPtr = true; 18712 SourceLocation ELoc; 18713 SourceRange ERange; 18714 18715 void emitErrorMsg() { 18716 // If nothing else worked, this is not a valid map clause expression. 18717 if (SemaRef.getLangOpts().OpenMP < 50) { 18718 SemaRef.Diag(ELoc, 18719 diag::err_omp_expected_named_var_member_or_array_expression) 18720 << ERange; 18721 } else { 18722 SemaRef.Diag(ELoc, diag::err_omp_non_lvalue_in_map_or_motion_clauses) 18723 << getOpenMPClauseName(CKind) << ERange; 18724 } 18725 } 18726 18727 public: 18728 bool VisitDeclRefExpr(DeclRefExpr *DRE) { 18729 if (!isa<VarDecl>(DRE->getDecl())) { 18730 emitErrorMsg(); 18731 return false; 18732 } 18733 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 18734 RelevantExpr = DRE; 18735 // Record the component. 18736 Components.emplace_back(DRE, DRE->getDecl(), IsNonContiguous); 18737 return true; 18738 } 18739 18740 bool VisitMemberExpr(MemberExpr *ME) { 18741 Expr *E = ME; 18742 Expr *BaseE = ME->getBase()->IgnoreParenCasts(); 18743 18744 if (isa<CXXThisExpr>(BaseE)) { 18745 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 18746 // We found a base expression: this->Val. 18747 RelevantExpr = ME; 18748 } else { 18749 E = BaseE; 18750 } 18751 18752 if (!isa<FieldDecl>(ME->getMemberDecl())) { 18753 if (!NoDiagnose) { 18754 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field) 18755 << ME->getSourceRange(); 18756 return false; 18757 } 18758 if (RelevantExpr) 18759 return false; 18760 return Visit(E); 18761 } 18762 18763 auto *FD = cast<FieldDecl>(ME->getMemberDecl()); 18764 18765 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3] 18766 // A bit-field cannot appear in a map clause. 18767 // 18768 if (FD->isBitField()) { 18769 if (!NoDiagnose) { 18770 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause) 18771 << ME->getSourceRange() << getOpenMPClauseName(CKind); 18772 return false; 18773 } 18774 if (RelevantExpr) 18775 return false; 18776 return Visit(E); 18777 } 18778 18779 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 18780 // If the type of a list item is a reference to a type T then the type 18781 // will be considered to be T for all purposes of this clause. 18782 QualType CurType = BaseE->getType().getNonReferenceType(); 18783 18784 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2] 18785 // A list item cannot be a variable that is a member of a structure with 18786 // a union type. 18787 // 18788 if (CurType->isUnionType()) { 18789 if (!NoDiagnose) { 18790 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed) 18791 << ME->getSourceRange(); 18792 return false; 18793 } 18794 return RelevantExpr || Visit(E); 18795 } 18796 18797 // If we got a member expression, we should not expect any array section 18798 // before that: 18799 // 18800 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7] 18801 // If a list item is an element of a structure, only the rightmost symbol 18802 // of the variable reference can be an array section. 18803 // 18804 AllowUnitySizeArraySection = false; 18805 AllowWholeSizeArraySection = false; 18806 18807 // Record the component. 18808 Components.emplace_back(ME, FD, IsNonContiguous); 18809 return RelevantExpr || Visit(E); 18810 } 18811 18812 bool VisitArraySubscriptExpr(ArraySubscriptExpr *AE) { 18813 Expr *E = AE->getBase()->IgnoreParenImpCasts(); 18814 18815 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) { 18816 if (!NoDiagnose) { 18817 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name) 18818 << 0 << AE->getSourceRange(); 18819 return false; 18820 } 18821 return RelevantExpr || Visit(E); 18822 } 18823 18824 // If we got an array subscript that express the whole dimension we 18825 // can have any array expressions before. If it only expressing part of 18826 // the dimension, we can only have unitary-size array expressions. 18827 if (checkArrayExpressionDoesNotReferToWholeSize(SemaRef, AE, 18828 E->getType())) 18829 AllowWholeSizeArraySection = false; 18830 18831 if (const auto *TE = dyn_cast<CXXThisExpr>(E->IgnoreParenCasts())) { 18832 Expr::EvalResult Result; 18833 if (!AE->getIdx()->isValueDependent() && 18834 AE->getIdx()->EvaluateAsInt(Result, SemaRef.getASTContext()) && 18835 !Result.Val.getInt().isZero()) { 18836 SemaRef.Diag(AE->getIdx()->getExprLoc(), 18837 diag::err_omp_invalid_map_this_expr); 18838 SemaRef.Diag(AE->getIdx()->getExprLoc(), 18839 diag::note_omp_invalid_subscript_on_this_ptr_map); 18840 } 18841 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 18842 RelevantExpr = TE; 18843 } 18844 18845 // Record the component - we don't have any declaration associated. 18846 Components.emplace_back(AE, nullptr, IsNonContiguous); 18847 18848 return RelevantExpr || Visit(E); 18849 } 18850 18851 bool VisitOMPArraySectionExpr(OMPArraySectionExpr *OASE) { 18852 // After OMP 5.0 Array section in reduction clause will be implicitly 18853 // mapped 18854 assert(!(SemaRef.getLangOpts().OpenMP < 50 && NoDiagnose) && 18855 "Array sections cannot be implicitly mapped."); 18856 Expr *E = OASE->getBase()->IgnoreParenImpCasts(); 18857 QualType CurType = 18858 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType(); 18859 18860 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 18861 // If the type of a list item is a reference to a type T then the type 18862 // will be considered to be T for all purposes of this clause. 18863 if (CurType->isReferenceType()) 18864 CurType = CurType->getPointeeType(); 18865 18866 bool IsPointer = CurType->isAnyPointerType(); 18867 18868 if (!IsPointer && !CurType->isArrayType()) { 18869 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name) 18870 << 0 << OASE->getSourceRange(); 18871 return false; 18872 } 18873 18874 bool NotWhole = 18875 checkArrayExpressionDoesNotReferToWholeSize(SemaRef, OASE, CurType); 18876 bool NotUnity = 18877 checkArrayExpressionDoesNotReferToUnitySize(SemaRef, OASE, CurType); 18878 18879 if (AllowWholeSizeArraySection) { 18880 // Any array section is currently allowed. Allowing a whole size array 18881 // section implies allowing a unity array section as well. 18882 // 18883 // If this array section refers to the whole dimension we can still 18884 // accept other array sections before this one, except if the base is a 18885 // pointer. Otherwise, only unitary sections are accepted. 18886 if (NotWhole || IsPointer) 18887 AllowWholeSizeArraySection = false; 18888 } else if (DKind == OMPD_target_update && 18889 SemaRef.getLangOpts().OpenMP >= 50) { 18890 if (IsPointer && !AllowAnotherPtr) 18891 SemaRef.Diag(ELoc, diag::err_omp_section_length_undefined) 18892 << /*array of unknown bound */ 1; 18893 else 18894 IsNonContiguous = true; 18895 } else if (AllowUnitySizeArraySection && NotUnity) { 18896 // A unity or whole array section is not allowed and that is not 18897 // compatible with the properties of the current array section. 18898 if (NoDiagnose) 18899 return false; 18900 SemaRef.Diag( 18901 ELoc, diag::err_array_section_does_not_specify_contiguous_storage) 18902 << OASE->getSourceRange(); 18903 return false; 18904 } 18905 18906 if (IsPointer) 18907 AllowAnotherPtr = false; 18908 18909 if (const auto *TE = dyn_cast<CXXThisExpr>(E)) { 18910 Expr::EvalResult ResultR; 18911 Expr::EvalResult ResultL; 18912 if (!OASE->getLength()->isValueDependent() && 18913 OASE->getLength()->EvaluateAsInt(ResultR, SemaRef.getASTContext()) && 18914 !ResultR.Val.getInt().isOne()) { 18915 SemaRef.Diag(OASE->getLength()->getExprLoc(), 18916 diag::err_omp_invalid_map_this_expr); 18917 SemaRef.Diag(OASE->getLength()->getExprLoc(), 18918 diag::note_omp_invalid_length_on_this_ptr_mapping); 18919 } 18920 if (OASE->getLowerBound() && !OASE->getLowerBound()->isValueDependent() && 18921 OASE->getLowerBound()->EvaluateAsInt(ResultL, 18922 SemaRef.getASTContext()) && 18923 !ResultL.Val.getInt().isZero()) { 18924 SemaRef.Diag(OASE->getLowerBound()->getExprLoc(), 18925 diag::err_omp_invalid_map_this_expr); 18926 SemaRef.Diag(OASE->getLowerBound()->getExprLoc(), 18927 diag::note_omp_invalid_lower_bound_on_this_ptr_mapping); 18928 } 18929 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 18930 RelevantExpr = TE; 18931 } 18932 18933 // Record the component - we don't have any declaration associated. 18934 Components.emplace_back(OASE, nullptr, /*IsNonContiguous=*/false); 18935 return RelevantExpr || Visit(E); 18936 } 18937 bool VisitOMPArrayShapingExpr(OMPArrayShapingExpr *E) { 18938 Expr *Base = E->getBase(); 18939 18940 // Record the component - we don't have any declaration associated. 18941 Components.emplace_back(E, nullptr, IsNonContiguous); 18942 18943 return Visit(Base->IgnoreParenImpCasts()); 18944 } 18945 18946 bool VisitUnaryOperator(UnaryOperator *UO) { 18947 if (SemaRef.getLangOpts().OpenMP < 50 || !UO->isLValue() || 18948 UO->getOpcode() != UO_Deref) { 18949 emitErrorMsg(); 18950 return false; 18951 } 18952 if (!RelevantExpr) { 18953 // Record the component if haven't found base decl. 18954 Components.emplace_back(UO, nullptr, /*IsNonContiguous=*/false); 18955 } 18956 return RelevantExpr || Visit(UO->getSubExpr()->IgnoreParenImpCasts()); 18957 } 18958 bool VisitBinaryOperator(BinaryOperator *BO) { 18959 if (SemaRef.getLangOpts().OpenMP < 50 || !BO->getType()->isPointerType()) { 18960 emitErrorMsg(); 18961 return false; 18962 } 18963 18964 // Pointer arithmetic is the only thing we expect to happen here so after we 18965 // make sure the binary operator is a pointer type, the we only thing need 18966 // to to is to visit the subtree that has the same type as root (so that we 18967 // know the other subtree is just an offset) 18968 Expr *LE = BO->getLHS()->IgnoreParenImpCasts(); 18969 Expr *RE = BO->getRHS()->IgnoreParenImpCasts(); 18970 Components.emplace_back(BO, nullptr, false); 18971 assert((LE->getType().getTypePtr() == BO->getType().getTypePtr() || 18972 RE->getType().getTypePtr() == BO->getType().getTypePtr()) && 18973 "Either LHS or RHS have base decl inside"); 18974 if (BO->getType().getTypePtr() == LE->getType().getTypePtr()) 18975 return RelevantExpr || Visit(LE); 18976 return RelevantExpr || Visit(RE); 18977 } 18978 bool VisitCXXThisExpr(CXXThisExpr *CTE) { 18979 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 18980 RelevantExpr = CTE; 18981 Components.emplace_back(CTE, nullptr, IsNonContiguous); 18982 return true; 18983 } 18984 bool VisitCXXOperatorCallExpr(CXXOperatorCallExpr *COCE) { 18985 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 18986 Components.emplace_back(COCE, nullptr, IsNonContiguous); 18987 return true; 18988 } 18989 bool VisitOpaqueValueExpr(OpaqueValueExpr *E) { 18990 Expr *Source = E->getSourceExpr(); 18991 if (!Source) { 18992 emitErrorMsg(); 18993 return false; 18994 } 18995 return Visit(Source); 18996 } 18997 bool VisitStmt(Stmt *) { 18998 emitErrorMsg(); 18999 return false; 19000 } 19001 const Expr *getFoundBase() const { 19002 return RelevantExpr; 19003 } 19004 explicit MapBaseChecker( 19005 Sema &SemaRef, OpenMPClauseKind CKind, OpenMPDirectiveKind DKind, 19006 OMPClauseMappableExprCommon::MappableExprComponentList &Components, 19007 bool NoDiagnose, SourceLocation &ELoc, SourceRange &ERange) 19008 : SemaRef(SemaRef), CKind(CKind), DKind(DKind), Components(Components), 19009 NoDiagnose(NoDiagnose), ELoc(ELoc), ERange(ERange) {} 19010 }; 19011 } // namespace 19012 19013 /// Return the expression of the base of the mappable expression or null if it 19014 /// cannot be determined and do all the necessary checks to see if the expression 19015 /// is valid as a standalone mappable expression. In the process, record all the 19016 /// components of the expression. 19017 static const Expr *checkMapClauseExpressionBase( 19018 Sema &SemaRef, Expr *E, 19019 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents, 19020 OpenMPClauseKind CKind, OpenMPDirectiveKind DKind, bool NoDiagnose) { 19021 SourceLocation ELoc = E->getExprLoc(); 19022 SourceRange ERange = E->getSourceRange(); 19023 MapBaseChecker Checker(SemaRef, CKind, DKind, CurComponents, NoDiagnose, ELoc, 19024 ERange); 19025 if (Checker.Visit(E->IgnoreParens())) { 19026 // Check if the highest dimension array section has length specified 19027 if (SemaRef.getLangOpts().OpenMP >= 50 && !CurComponents.empty() && 19028 (CKind == OMPC_to || CKind == OMPC_from)) { 19029 auto CI = CurComponents.rbegin(); 19030 auto CE = CurComponents.rend(); 19031 for (; CI != CE; ++CI) { 19032 const auto *OASE = 19033 dyn_cast<OMPArraySectionExpr>(CI->getAssociatedExpression()); 19034 if (!OASE) 19035 continue; 19036 if (OASE && OASE->getLength()) 19037 break; 19038 SemaRef.Diag(ELoc, diag::err_array_section_does_not_specify_length) 19039 << ERange; 19040 } 19041 } 19042 return Checker.getFoundBase(); 19043 } 19044 return nullptr; 19045 } 19046 19047 // Return true if expression E associated with value VD has conflicts with other 19048 // map information. 19049 static bool checkMapConflicts( 19050 Sema &SemaRef, DSAStackTy *DSAS, const ValueDecl *VD, const Expr *E, 19051 bool CurrentRegionOnly, 19052 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents, 19053 OpenMPClauseKind CKind) { 19054 assert(VD && E); 19055 SourceLocation ELoc = E->getExprLoc(); 19056 SourceRange ERange = E->getSourceRange(); 19057 19058 // In order to easily check the conflicts we need to match each component of 19059 // the expression under test with the components of the expressions that are 19060 // already in the stack. 19061 19062 assert(!CurComponents.empty() && "Map clause expression with no components!"); 19063 assert(CurComponents.back().getAssociatedDeclaration() == VD && 19064 "Map clause expression with unexpected base!"); 19065 19066 // Variables to help detecting enclosing problems in data environment nests. 19067 bool IsEnclosedByDataEnvironmentExpr = false; 19068 const Expr *EnclosingExpr = nullptr; 19069 19070 bool FoundError = DSAS->checkMappableExprComponentListsForDecl( 19071 VD, CurrentRegionOnly, 19072 [&IsEnclosedByDataEnvironmentExpr, &SemaRef, VD, CurrentRegionOnly, ELoc, 19073 ERange, CKind, &EnclosingExpr, 19074 CurComponents](OMPClauseMappableExprCommon::MappableExprComponentListRef 19075 StackComponents, 19076 OpenMPClauseKind Kind) { 19077 if (CKind == Kind && SemaRef.LangOpts.OpenMP >= 50) 19078 return false; 19079 assert(!StackComponents.empty() && 19080 "Map clause expression with no components!"); 19081 assert(StackComponents.back().getAssociatedDeclaration() == VD && 19082 "Map clause expression with unexpected base!"); 19083 (void)VD; 19084 19085 // The whole expression in the stack. 19086 const Expr *RE = StackComponents.front().getAssociatedExpression(); 19087 19088 // Expressions must start from the same base. Here we detect at which 19089 // point both expressions diverge from each other and see if we can 19090 // detect if the memory referred to both expressions is contiguous and 19091 // do not overlap. 19092 auto CI = CurComponents.rbegin(); 19093 auto CE = CurComponents.rend(); 19094 auto SI = StackComponents.rbegin(); 19095 auto SE = StackComponents.rend(); 19096 for (; CI != CE && SI != SE; ++CI, ++SI) { 19097 19098 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3] 19099 // At most one list item can be an array item derived from a given 19100 // variable in map clauses of the same construct. 19101 if (CurrentRegionOnly && 19102 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) || 19103 isa<OMPArraySectionExpr>(CI->getAssociatedExpression()) || 19104 isa<OMPArrayShapingExpr>(CI->getAssociatedExpression())) && 19105 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) || 19106 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()) || 19107 isa<OMPArrayShapingExpr>(SI->getAssociatedExpression()))) { 19108 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(), 19109 diag::err_omp_multiple_array_items_in_map_clause) 19110 << CI->getAssociatedExpression()->getSourceRange(); 19111 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(), 19112 diag::note_used_here) 19113 << SI->getAssociatedExpression()->getSourceRange(); 19114 return true; 19115 } 19116 19117 // Do both expressions have the same kind? 19118 if (CI->getAssociatedExpression()->getStmtClass() != 19119 SI->getAssociatedExpression()->getStmtClass()) 19120 break; 19121 19122 // Are we dealing with different variables/fields? 19123 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration()) 19124 break; 19125 } 19126 // Check if the extra components of the expressions in the enclosing 19127 // data environment are redundant for the current base declaration. 19128 // If they are, the maps completely overlap, which is legal. 19129 for (; SI != SE; ++SI) { 19130 QualType Type; 19131 if (const auto *ASE = 19132 dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) { 19133 Type = ASE->getBase()->IgnoreParenImpCasts()->getType(); 19134 } else if (const auto *OASE = dyn_cast<OMPArraySectionExpr>( 19135 SI->getAssociatedExpression())) { 19136 const Expr *E = OASE->getBase()->IgnoreParenImpCasts(); 19137 Type = 19138 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType(); 19139 } else if (const auto *OASE = dyn_cast<OMPArrayShapingExpr>( 19140 SI->getAssociatedExpression())) { 19141 Type = OASE->getBase()->getType()->getPointeeType(); 19142 } 19143 if (Type.isNull() || Type->isAnyPointerType() || 19144 checkArrayExpressionDoesNotReferToWholeSize( 19145 SemaRef, SI->getAssociatedExpression(), Type)) 19146 break; 19147 } 19148 19149 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4] 19150 // List items of map clauses in the same construct must not share 19151 // original storage. 19152 // 19153 // If the expressions are exactly the same or one is a subset of the 19154 // other, it means they are sharing storage. 19155 if (CI == CE && SI == SE) { 19156 if (CurrentRegionOnly) { 19157 if (CKind == OMPC_map) { 19158 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange; 19159 } else { 19160 assert(CKind == OMPC_to || CKind == OMPC_from); 19161 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update) 19162 << ERange; 19163 } 19164 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 19165 << RE->getSourceRange(); 19166 return true; 19167 } 19168 // If we find the same expression in the enclosing data environment, 19169 // that is legal. 19170 IsEnclosedByDataEnvironmentExpr = true; 19171 return false; 19172 } 19173 19174 QualType DerivedType = 19175 std::prev(CI)->getAssociatedDeclaration()->getType(); 19176 SourceLocation DerivedLoc = 19177 std::prev(CI)->getAssociatedExpression()->getExprLoc(); 19178 19179 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 19180 // If the type of a list item is a reference to a type T then the type 19181 // will be considered to be T for all purposes of this clause. 19182 DerivedType = DerivedType.getNonReferenceType(); 19183 19184 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1] 19185 // A variable for which the type is pointer and an array section 19186 // derived from that variable must not appear as list items of map 19187 // clauses of the same construct. 19188 // 19189 // Also, cover one of the cases in: 19190 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5] 19191 // If any part of the original storage of a list item has corresponding 19192 // storage in the device data environment, all of the original storage 19193 // must have corresponding storage in the device data environment. 19194 // 19195 if (DerivedType->isAnyPointerType()) { 19196 if (CI == CE || SI == SE) { 19197 SemaRef.Diag( 19198 DerivedLoc, 19199 diag::err_omp_pointer_mapped_along_with_derived_section) 19200 << DerivedLoc; 19201 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 19202 << RE->getSourceRange(); 19203 return true; 19204 } 19205 if (CI->getAssociatedExpression()->getStmtClass() != 19206 SI->getAssociatedExpression()->getStmtClass() || 19207 CI->getAssociatedDeclaration()->getCanonicalDecl() == 19208 SI->getAssociatedDeclaration()->getCanonicalDecl()) { 19209 assert(CI != CE && SI != SE); 19210 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_dereferenced) 19211 << DerivedLoc; 19212 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 19213 << RE->getSourceRange(); 19214 return true; 19215 } 19216 } 19217 19218 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4] 19219 // List items of map clauses in the same construct must not share 19220 // original storage. 19221 // 19222 // An expression is a subset of the other. 19223 if (CurrentRegionOnly && (CI == CE || SI == SE)) { 19224 if (CKind == OMPC_map) { 19225 if (CI != CE || SI != SE) { 19226 // Allow constructs like this: map(s, s.ptr[0:1]), where s.ptr is 19227 // a pointer. 19228 auto Begin = 19229 CI != CE ? CurComponents.begin() : StackComponents.begin(); 19230 auto End = CI != CE ? CurComponents.end() : StackComponents.end(); 19231 auto It = Begin; 19232 while (It != End && !It->getAssociatedDeclaration()) 19233 std::advance(It, 1); 19234 assert(It != End && 19235 "Expected at least one component with the declaration."); 19236 if (It != Begin && It->getAssociatedDeclaration() 19237 ->getType() 19238 .getCanonicalType() 19239 ->isAnyPointerType()) { 19240 IsEnclosedByDataEnvironmentExpr = false; 19241 EnclosingExpr = nullptr; 19242 return false; 19243 } 19244 } 19245 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange; 19246 } else { 19247 assert(CKind == OMPC_to || CKind == OMPC_from); 19248 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update) 19249 << ERange; 19250 } 19251 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 19252 << RE->getSourceRange(); 19253 return true; 19254 } 19255 19256 // The current expression uses the same base as other expression in the 19257 // data environment but does not contain it completely. 19258 if (!CurrentRegionOnly && SI != SE) 19259 EnclosingExpr = RE; 19260 19261 // The current expression is a subset of the expression in the data 19262 // environment. 19263 IsEnclosedByDataEnvironmentExpr |= 19264 (!CurrentRegionOnly && CI != CE && SI == SE); 19265 19266 return false; 19267 }); 19268 19269 if (CurrentRegionOnly) 19270 return FoundError; 19271 19272 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5] 19273 // If any part of the original storage of a list item has corresponding 19274 // storage in the device data environment, all of the original storage must 19275 // have corresponding storage in the device data environment. 19276 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6] 19277 // If a list item is an element of a structure, and a different element of 19278 // the structure has a corresponding list item in the device data environment 19279 // prior to a task encountering the construct associated with the map clause, 19280 // then the list item must also have a corresponding list item in the device 19281 // data environment prior to the task encountering the construct. 19282 // 19283 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) { 19284 SemaRef.Diag(ELoc, 19285 diag::err_omp_original_storage_is_shared_and_does_not_contain) 19286 << ERange; 19287 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here) 19288 << EnclosingExpr->getSourceRange(); 19289 return true; 19290 } 19291 19292 return FoundError; 19293 } 19294 19295 // Look up the user-defined mapper given the mapper name and mapped type, and 19296 // build a reference to it. 19297 static ExprResult buildUserDefinedMapperRef(Sema &SemaRef, Scope *S, 19298 CXXScopeSpec &MapperIdScopeSpec, 19299 const DeclarationNameInfo &MapperId, 19300 QualType Type, 19301 Expr *UnresolvedMapper) { 19302 if (MapperIdScopeSpec.isInvalid()) 19303 return ExprError(); 19304 // Get the actual type for the array type. 19305 if (Type->isArrayType()) { 19306 assert(Type->getAsArrayTypeUnsafe() && "Expect to get a valid array type"); 19307 Type = Type->getAsArrayTypeUnsafe()->getElementType().getCanonicalType(); 19308 } 19309 // Find all user-defined mappers with the given MapperId. 19310 SmallVector<UnresolvedSet<8>, 4> Lookups; 19311 LookupResult Lookup(SemaRef, MapperId, Sema::LookupOMPMapperName); 19312 Lookup.suppressDiagnostics(); 19313 if (S) { 19314 while (S && SemaRef.LookupParsedName(Lookup, S, &MapperIdScopeSpec)) { 19315 NamedDecl *D = Lookup.getRepresentativeDecl(); 19316 while (S && !S->isDeclScope(D)) 19317 S = S->getParent(); 19318 if (S) 19319 S = S->getParent(); 19320 Lookups.emplace_back(); 19321 Lookups.back().append(Lookup.begin(), Lookup.end()); 19322 Lookup.clear(); 19323 } 19324 } else if (auto *ULE = cast_or_null<UnresolvedLookupExpr>(UnresolvedMapper)) { 19325 // Extract the user-defined mappers with the given MapperId. 19326 Lookups.push_back(UnresolvedSet<8>()); 19327 for (NamedDecl *D : ULE->decls()) { 19328 auto *DMD = cast<OMPDeclareMapperDecl>(D); 19329 assert(DMD && "Expect valid OMPDeclareMapperDecl during instantiation."); 19330 Lookups.back().addDecl(DMD); 19331 } 19332 } 19333 // Defer the lookup for dependent types. The results will be passed through 19334 // UnresolvedMapper on instantiation. 19335 if (SemaRef.CurContext->isDependentContext() || Type->isDependentType() || 19336 Type->isInstantiationDependentType() || 19337 Type->containsUnexpandedParameterPack() || 19338 filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) { 19339 return !D->isInvalidDecl() && 19340 (D->getType()->isDependentType() || 19341 D->getType()->isInstantiationDependentType() || 19342 D->getType()->containsUnexpandedParameterPack()); 19343 })) { 19344 UnresolvedSet<8> URS; 19345 for (const UnresolvedSet<8> &Set : Lookups) { 19346 if (Set.empty()) 19347 continue; 19348 URS.append(Set.begin(), Set.end()); 19349 } 19350 return UnresolvedLookupExpr::Create( 19351 SemaRef.Context, /*NamingClass=*/nullptr, 19352 MapperIdScopeSpec.getWithLocInContext(SemaRef.Context), MapperId, 19353 /*ADL=*/false, /*Overloaded=*/true, URS.begin(), URS.end()); 19354 } 19355 SourceLocation Loc = MapperId.getLoc(); 19356 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions 19357 // The type must be of struct, union or class type in C and C++ 19358 if (!Type->isStructureOrClassType() && !Type->isUnionType() && 19359 (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default")) { 19360 SemaRef.Diag(Loc, diag::err_omp_mapper_wrong_type); 19361 return ExprError(); 19362 } 19363 // Perform argument dependent lookup. 19364 if (SemaRef.getLangOpts().CPlusPlus && !MapperIdScopeSpec.isSet()) 19365 argumentDependentLookup(SemaRef, MapperId, Loc, Type, Lookups); 19366 // Return the first user-defined mapper with the desired type. 19367 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 19368 Lookups, [&SemaRef, Type](ValueDecl *D) -> ValueDecl * { 19369 if (!D->isInvalidDecl() && 19370 SemaRef.Context.hasSameType(D->getType(), Type)) 19371 return D; 19372 return nullptr; 19373 })) 19374 return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc); 19375 // Find the first user-defined mapper with a type derived from the desired 19376 // type. 19377 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 19378 Lookups, [&SemaRef, Type, Loc](ValueDecl *D) -> ValueDecl * { 19379 if (!D->isInvalidDecl() && 19380 SemaRef.IsDerivedFrom(Loc, Type, D->getType()) && 19381 !Type.isMoreQualifiedThan(D->getType())) 19382 return D; 19383 return nullptr; 19384 })) { 19385 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, 19386 /*DetectVirtual=*/false); 19387 if (SemaRef.IsDerivedFrom(Loc, Type, VD->getType(), Paths)) { 19388 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType( 19389 VD->getType().getUnqualifiedType()))) { 19390 if (SemaRef.CheckBaseClassAccess( 19391 Loc, VD->getType(), Type, Paths.front(), 19392 /*DiagID=*/0) != Sema::AR_inaccessible) { 19393 return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc); 19394 } 19395 } 19396 } 19397 } 19398 // Report error if a mapper is specified, but cannot be found. 19399 if (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default") { 19400 SemaRef.Diag(Loc, diag::err_omp_invalid_mapper) 19401 << Type << MapperId.getName(); 19402 return ExprError(); 19403 } 19404 return ExprEmpty(); 19405 } 19406 19407 namespace { 19408 // Utility struct that gathers all the related lists associated with a mappable 19409 // expression. 19410 struct MappableVarListInfo { 19411 // The list of expressions. 19412 ArrayRef<Expr *> VarList; 19413 // The list of processed expressions. 19414 SmallVector<Expr *, 16> ProcessedVarList; 19415 // The mappble components for each expression. 19416 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents; 19417 // The base declaration of the variable. 19418 SmallVector<ValueDecl *, 16> VarBaseDeclarations; 19419 // The reference to the user-defined mapper associated with every expression. 19420 SmallVector<Expr *, 16> UDMapperList; 19421 19422 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) { 19423 // We have a list of components and base declarations for each entry in the 19424 // variable list. 19425 VarComponents.reserve(VarList.size()); 19426 VarBaseDeclarations.reserve(VarList.size()); 19427 } 19428 }; 19429 } 19430 19431 // Check the validity of the provided variable list for the provided clause kind 19432 // \a CKind. In the check process the valid expressions, mappable expression 19433 // components, variables, and user-defined mappers are extracted and used to 19434 // fill \a ProcessedVarList, \a VarComponents, \a VarBaseDeclarations, and \a 19435 // UDMapperList in MVLI. \a MapType, \a IsMapTypeImplicit, \a MapperIdScopeSpec, 19436 // and \a MapperId are expected to be valid if the clause kind is 'map'. 19437 static void checkMappableExpressionList( 19438 Sema &SemaRef, DSAStackTy *DSAS, OpenMPClauseKind CKind, 19439 MappableVarListInfo &MVLI, SourceLocation StartLoc, 19440 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo MapperId, 19441 ArrayRef<Expr *> UnresolvedMappers, 19442 OpenMPMapClauseKind MapType = OMPC_MAP_unknown, 19443 ArrayRef<OpenMPMapModifierKind> Modifiers = None, 19444 bool IsMapTypeImplicit = false, bool NoDiagnose = false) { 19445 // We only expect mappable expressions in 'to', 'from', and 'map' clauses. 19446 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) && 19447 "Unexpected clause kind with mappable expressions!"); 19448 19449 // If the identifier of user-defined mapper is not specified, it is "default". 19450 // We do not change the actual name in this clause to distinguish whether a 19451 // mapper is specified explicitly, i.e., it is not explicitly specified when 19452 // MapperId.getName() is empty. 19453 if (!MapperId.getName() || MapperId.getName().isEmpty()) { 19454 auto &DeclNames = SemaRef.getASTContext().DeclarationNames; 19455 MapperId.setName(DeclNames.getIdentifier( 19456 &SemaRef.getASTContext().Idents.get("default"))); 19457 MapperId.setLoc(StartLoc); 19458 } 19459 19460 // Iterators to find the current unresolved mapper expression. 19461 auto UMIt = UnresolvedMappers.begin(), UMEnd = UnresolvedMappers.end(); 19462 bool UpdateUMIt = false; 19463 Expr *UnresolvedMapper = nullptr; 19464 19465 bool HasHoldModifier = 19466 llvm::is_contained(Modifiers, OMPC_MAP_MODIFIER_ompx_hold); 19467 19468 // Keep track of the mappable components and base declarations in this clause. 19469 // Each entry in the list is going to have a list of components associated. We 19470 // record each set of the components so that we can build the clause later on. 19471 // In the end we should have the same amount of declarations and component 19472 // lists. 19473 19474 for (Expr *RE : MVLI.VarList) { 19475 assert(RE && "Null expr in omp to/from/map clause"); 19476 SourceLocation ELoc = RE->getExprLoc(); 19477 19478 // Find the current unresolved mapper expression. 19479 if (UpdateUMIt && UMIt != UMEnd) { 19480 UMIt++; 19481 assert( 19482 UMIt != UMEnd && 19483 "Expect the size of UnresolvedMappers to match with that of VarList"); 19484 } 19485 UpdateUMIt = true; 19486 if (UMIt != UMEnd) 19487 UnresolvedMapper = *UMIt; 19488 19489 const Expr *VE = RE->IgnoreParenLValueCasts(); 19490 19491 if (VE->isValueDependent() || VE->isTypeDependent() || 19492 VE->isInstantiationDependent() || 19493 VE->containsUnexpandedParameterPack()) { 19494 // Try to find the associated user-defined mapper. 19495 ExprResult ER = buildUserDefinedMapperRef( 19496 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId, 19497 VE->getType().getCanonicalType(), UnresolvedMapper); 19498 if (ER.isInvalid()) 19499 continue; 19500 MVLI.UDMapperList.push_back(ER.get()); 19501 // We can only analyze this information once the missing information is 19502 // resolved. 19503 MVLI.ProcessedVarList.push_back(RE); 19504 continue; 19505 } 19506 19507 Expr *SimpleExpr = RE->IgnoreParenCasts(); 19508 19509 if (!RE->isLValue()) { 19510 if (SemaRef.getLangOpts().OpenMP < 50) { 19511 SemaRef.Diag( 19512 ELoc, diag::err_omp_expected_named_var_member_or_array_expression) 19513 << RE->getSourceRange(); 19514 } else { 19515 SemaRef.Diag(ELoc, diag::err_omp_non_lvalue_in_map_or_motion_clauses) 19516 << getOpenMPClauseName(CKind) << RE->getSourceRange(); 19517 } 19518 continue; 19519 } 19520 19521 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents; 19522 ValueDecl *CurDeclaration = nullptr; 19523 19524 // Obtain the array or member expression bases if required. Also, fill the 19525 // components array with all the components identified in the process. 19526 const Expr *BE = 19527 checkMapClauseExpressionBase(SemaRef, SimpleExpr, CurComponents, CKind, 19528 DSAS->getCurrentDirective(), NoDiagnose); 19529 if (!BE) 19530 continue; 19531 19532 assert(!CurComponents.empty() && 19533 "Invalid mappable expression information."); 19534 19535 if (const auto *TE = dyn_cast<CXXThisExpr>(BE)) { 19536 // Add store "this" pointer to class in DSAStackTy for future checking 19537 DSAS->addMappedClassesQualTypes(TE->getType()); 19538 // Try to find the associated user-defined mapper. 19539 ExprResult ER = buildUserDefinedMapperRef( 19540 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId, 19541 VE->getType().getCanonicalType(), UnresolvedMapper); 19542 if (ER.isInvalid()) 19543 continue; 19544 MVLI.UDMapperList.push_back(ER.get()); 19545 // Skip restriction checking for variable or field declarations 19546 MVLI.ProcessedVarList.push_back(RE); 19547 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 19548 MVLI.VarComponents.back().append(CurComponents.begin(), 19549 CurComponents.end()); 19550 MVLI.VarBaseDeclarations.push_back(nullptr); 19551 continue; 19552 } 19553 19554 // For the following checks, we rely on the base declaration which is 19555 // expected to be associated with the last component. The declaration is 19556 // expected to be a variable or a field (if 'this' is being mapped). 19557 CurDeclaration = CurComponents.back().getAssociatedDeclaration(); 19558 assert(CurDeclaration && "Null decl on map clause."); 19559 assert( 19560 CurDeclaration->isCanonicalDecl() && 19561 "Expecting components to have associated only canonical declarations."); 19562 19563 auto *VD = dyn_cast<VarDecl>(CurDeclaration); 19564 const auto *FD = dyn_cast<FieldDecl>(CurDeclaration); 19565 19566 assert((VD || FD) && "Only variables or fields are expected here!"); 19567 (void)FD; 19568 19569 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10] 19570 // threadprivate variables cannot appear in a map clause. 19571 // OpenMP 4.5 [2.10.5, target update Construct] 19572 // threadprivate variables cannot appear in a from clause. 19573 if (VD && DSAS->isThreadPrivate(VD)) { 19574 if (NoDiagnose) 19575 continue; 19576 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false); 19577 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause) 19578 << getOpenMPClauseName(CKind); 19579 reportOriginalDsa(SemaRef, DSAS, VD, DVar); 19580 continue; 19581 } 19582 19583 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9] 19584 // A list item cannot appear in both a map clause and a data-sharing 19585 // attribute clause on the same construct. 19586 19587 // Check conflicts with other map clause expressions. We check the conflicts 19588 // with the current construct separately from the enclosing data 19589 // environment, because the restrictions are different. We only have to 19590 // check conflicts across regions for the map clauses. 19591 if (checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr, 19592 /*CurrentRegionOnly=*/true, CurComponents, CKind)) 19593 break; 19594 if (CKind == OMPC_map && 19595 (SemaRef.getLangOpts().OpenMP <= 45 || StartLoc.isValid()) && 19596 checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr, 19597 /*CurrentRegionOnly=*/false, CurComponents, CKind)) 19598 break; 19599 19600 // OpenMP 4.5 [2.10.5, target update Construct] 19601 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 19602 // If the type of a list item is a reference to a type T then the type will 19603 // be considered to be T for all purposes of this clause. 19604 auto I = llvm::find_if( 19605 CurComponents, 19606 [](const OMPClauseMappableExprCommon::MappableComponent &MC) { 19607 return MC.getAssociatedDeclaration(); 19608 }); 19609 assert(I != CurComponents.end() && "Null decl on map clause."); 19610 (void)I; 19611 QualType Type; 19612 auto *ASE = dyn_cast<ArraySubscriptExpr>(VE->IgnoreParens()); 19613 auto *OASE = dyn_cast<OMPArraySectionExpr>(VE->IgnoreParens()); 19614 auto *OAShE = dyn_cast<OMPArrayShapingExpr>(VE->IgnoreParens()); 19615 if (ASE) { 19616 Type = ASE->getType().getNonReferenceType(); 19617 } else if (OASE) { 19618 QualType BaseType = 19619 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 19620 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 19621 Type = ATy->getElementType(); 19622 else 19623 Type = BaseType->getPointeeType(); 19624 Type = Type.getNonReferenceType(); 19625 } else if (OAShE) { 19626 Type = OAShE->getBase()->getType()->getPointeeType(); 19627 } else { 19628 Type = VE->getType(); 19629 } 19630 19631 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4] 19632 // A list item in a to or from clause must have a mappable type. 19633 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9] 19634 // A list item must have a mappable type. 19635 if (!checkTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef, 19636 DSAS, Type, /*FullCheck=*/true)) 19637 continue; 19638 19639 if (CKind == OMPC_map) { 19640 // target enter data 19641 // OpenMP [2.10.2, Restrictions, p. 99] 19642 // A map-type must be specified in all map clauses and must be either 19643 // to or alloc. 19644 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective(); 19645 if (DKind == OMPD_target_enter_data && 19646 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) { 19647 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 19648 << (IsMapTypeImplicit ? 1 : 0) 19649 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 19650 << getOpenMPDirectiveName(DKind); 19651 continue; 19652 } 19653 19654 // target exit_data 19655 // OpenMP [2.10.3, Restrictions, p. 102] 19656 // A map-type must be specified in all map clauses and must be either 19657 // from, release, or delete. 19658 if (DKind == OMPD_target_exit_data && 19659 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release || 19660 MapType == OMPC_MAP_delete)) { 19661 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 19662 << (IsMapTypeImplicit ? 1 : 0) 19663 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 19664 << getOpenMPDirectiveName(DKind); 19665 continue; 19666 } 19667 19668 // The 'ompx_hold' modifier is specifically intended to be used on a 19669 // 'target' or 'target data' directive to prevent data from being unmapped 19670 // during the associated statement. It is not permitted on a 'target 19671 // enter data' or 'target exit data' directive, which have no associated 19672 // statement. 19673 if ((DKind == OMPD_target_enter_data || DKind == OMPD_target_exit_data) && 19674 HasHoldModifier) { 19675 SemaRef.Diag(StartLoc, 19676 diag::err_omp_invalid_map_type_modifier_for_directive) 19677 << getOpenMPSimpleClauseTypeName(OMPC_map, 19678 OMPC_MAP_MODIFIER_ompx_hold) 19679 << getOpenMPDirectiveName(DKind); 19680 continue; 19681 } 19682 19683 // target, target data 19684 // OpenMP 5.0 [2.12.2, Restrictions, p. 163] 19685 // OpenMP 5.0 [2.12.5, Restrictions, p. 174] 19686 // A map-type in a map clause must be to, from, tofrom or alloc 19687 if ((DKind == OMPD_target_data || 19688 isOpenMPTargetExecutionDirective(DKind)) && 19689 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_from || 19690 MapType == OMPC_MAP_tofrom || MapType == OMPC_MAP_alloc)) { 19691 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 19692 << (IsMapTypeImplicit ? 1 : 0) 19693 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 19694 << getOpenMPDirectiveName(DKind); 19695 continue; 19696 } 19697 19698 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 19699 // A list item cannot appear in both a map clause and a data-sharing 19700 // attribute clause on the same construct 19701 // 19702 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7] 19703 // A list item cannot appear in both a map clause and a data-sharing 19704 // attribute clause on the same construct unless the construct is a 19705 // combined construct. 19706 if (VD && ((SemaRef.LangOpts.OpenMP <= 45 && 19707 isOpenMPTargetExecutionDirective(DKind)) || 19708 DKind == OMPD_target)) { 19709 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false); 19710 if (isOpenMPPrivate(DVar.CKind)) { 19711 SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 19712 << getOpenMPClauseName(DVar.CKind) 19713 << getOpenMPClauseName(OMPC_map) 19714 << getOpenMPDirectiveName(DSAS->getCurrentDirective()); 19715 reportOriginalDsa(SemaRef, DSAS, CurDeclaration, DVar); 19716 continue; 19717 } 19718 } 19719 } 19720 19721 // Try to find the associated user-defined mapper. 19722 ExprResult ER = buildUserDefinedMapperRef( 19723 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId, 19724 Type.getCanonicalType(), UnresolvedMapper); 19725 if (ER.isInvalid()) 19726 continue; 19727 MVLI.UDMapperList.push_back(ER.get()); 19728 19729 // Save the current expression. 19730 MVLI.ProcessedVarList.push_back(RE); 19731 19732 // Store the components in the stack so that they can be used to check 19733 // against other clauses later on. 19734 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents, 19735 /*WhereFoundClauseKind=*/OMPC_map); 19736 19737 // Save the components and declaration to create the clause. For purposes of 19738 // the clause creation, any component list that has has base 'this' uses 19739 // null as base declaration. 19740 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 19741 MVLI.VarComponents.back().append(CurComponents.begin(), 19742 CurComponents.end()); 19743 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr 19744 : CurDeclaration); 19745 } 19746 } 19747 19748 OMPClause *Sema::ActOnOpenMPMapClause( 19749 ArrayRef<OpenMPMapModifierKind> MapTypeModifiers, 19750 ArrayRef<SourceLocation> MapTypeModifiersLoc, 19751 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId, 19752 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, SourceLocation MapLoc, 19753 SourceLocation ColonLoc, ArrayRef<Expr *> VarList, 19754 const OMPVarListLocTy &Locs, bool NoDiagnose, 19755 ArrayRef<Expr *> UnresolvedMappers) { 19756 OpenMPMapModifierKind Modifiers[] = { 19757 OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown, 19758 OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown, 19759 OMPC_MAP_MODIFIER_unknown}; 19760 SourceLocation ModifiersLoc[NumberOfOMPMapClauseModifiers]; 19761 19762 // Process map-type-modifiers, flag errors for duplicate modifiers. 19763 unsigned Count = 0; 19764 for (unsigned I = 0, E = MapTypeModifiers.size(); I < E; ++I) { 19765 if (MapTypeModifiers[I] != OMPC_MAP_MODIFIER_unknown && 19766 llvm::is_contained(Modifiers, MapTypeModifiers[I])) { 19767 Diag(MapTypeModifiersLoc[I], diag::err_omp_duplicate_map_type_modifier); 19768 continue; 19769 } 19770 assert(Count < NumberOfOMPMapClauseModifiers && 19771 "Modifiers exceed the allowed number of map type modifiers"); 19772 Modifiers[Count] = MapTypeModifiers[I]; 19773 ModifiersLoc[Count] = MapTypeModifiersLoc[I]; 19774 ++Count; 19775 } 19776 19777 MappableVarListInfo MVLI(VarList); 19778 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, Locs.StartLoc, 19779 MapperIdScopeSpec, MapperId, UnresolvedMappers, 19780 MapType, Modifiers, IsMapTypeImplicit, 19781 NoDiagnose); 19782 19783 // We need to produce a map clause even if we don't have variables so that 19784 // other diagnostics related with non-existing map clauses are accurate. 19785 return OMPMapClause::Create(Context, Locs, MVLI.ProcessedVarList, 19786 MVLI.VarBaseDeclarations, MVLI.VarComponents, 19787 MVLI.UDMapperList, Modifiers, ModifiersLoc, 19788 MapperIdScopeSpec.getWithLocInContext(Context), 19789 MapperId, MapType, IsMapTypeImplicit, MapLoc); 19790 } 19791 19792 QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc, 19793 TypeResult ParsedType) { 19794 assert(ParsedType.isUsable()); 19795 19796 QualType ReductionType = GetTypeFromParser(ParsedType.get()); 19797 if (ReductionType.isNull()) 19798 return QualType(); 19799 19800 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++ 19801 // A type name in a declare reduction directive cannot be a function type, an 19802 // array type, a reference type, or a type qualified with const, volatile or 19803 // restrict. 19804 if (ReductionType.hasQualifiers()) { 19805 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0; 19806 return QualType(); 19807 } 19808 19809 if (ReductionType->isFunctionType()) { 19810 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1; 19811 return QualType(); 19812 } 19813 if (ReductionType->isReferenceType()) { 19814 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2; 19815 return QualType(); 19816 } 19817 if (ReductionType->isArrayType()) { 19818 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3; 19819 return QualType(); 19820 } 19821 return ReductionType; 19822 } 19823 19824 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart( 19825 Scope *S, DeclContext *DC, DeclarationName Name, 19826 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes, 19827 AccessSpecifier AS, Decl *PrevDeclInScope) { 19828 SmallVector<Decl *, 8> Decls; 19829 Decls.reserve(ReductionTypes.size()); 19830 19831 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName, 19832 forRedeclarationInCurContext()); 19833 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions 19834 // A reduction-identifier may not be re-declared in the current scope for the 19835 // same type or for a type that is compatible according to the base language 19836 // rules. 19837 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes; 19838 OMPDeclareReductionDecl *PrevDRD = nullptr; 19839 bool InCompoundScope = true; 19840 if (S != nullptr) { 19841 // Find previous declaration with the same name not referenced in other 19842 // declarations. 19843 FunctionScopeInfo *ParentFn = getEnclosingFunction(); 19844 InCompoundScope = 19845 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty(); 19846 LookupName(Lookup, S); 19847 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false, 19848 /*AllowInlineNamespace=*/false); 19849 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious; 19850 LookupResult::Filter Filter = Lookup.makeFilter(); 19851 while (Filter.hasNext()) { 19852 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next()); 19853 if (InCompoundScope) { 19854 auto I = UsedAsPrevious.find(PrevDecl); 19855 if (I == UsedAsPrevious.end()) 19856 UsedAsPrevious[PrevDecl] = false; 19857 if (OMPDeclareReductionDecl *D = PrevDecl->getPrevDeclInScope()) 19858 UsedAsPrevious[D] = true; 19859 } 19860 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] = 19861 PrevDecl->getLocation(); 19862 } 19863 Filter.done(); 19864 if (InCompoundScope) { 19865 for (const auto &PrevData : UsedAsPrevious) { 19866 if (!PrevData.second) { 19867 PrevDRD = PrevData.first; 19868 break; 19869 } 19870 } 19871 } 19872 } else if (PrevDeclInScope != nullptr) { 19873 auto *PrevDRDInScope = PrevDRD = 19874 cast<OMPDeclareReductionDecl>(PrevDeclInScope); 19875 do { 19876 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] = 19877 PrevDRDInScope->getLocation(); 19878 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope(); 19879 } while (PrevDRDInScope != nullptr); 19880 } 19881 for (const auto &TyData : ReductionTypes) { 19882 const auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType()); 19883 bool Invalid = false; 19884 if (I != PreviousRedeclTypes.end()) { 19885 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition) 19886 << TyData.first; 19887 Diag(I->second, diag::note_previous_definition); 19888 Invalid = true; 19889 } 19890 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second; 19891 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second, 19892 Name, TyData.first, PrevDRD); 19893 DC->addDecl(DRD); 19894 DRD->setAccess(AS); 19895 Decls.push_back(DRD); 19896 if (Invalid) 19897 DRD->setInvalidDecl(); 19898 else 19899 PrevDRD = DRD; 19900 } 19901 19902 return DeclGroupPtrTy::make( 19903 DeclGroupRef::Create(Context, Decls.begin(), Decls.size())); 19904 } 19905 19906 void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) { 19907 auto *DRD = cast<OMPDeclareReductionDecl>(D); 19908 19909 // Enter new function scope. 19910 PushFunctionScope(); 19911 setFunctionHasBranchProtectedScope(); 19912 getCurFunction()->setHasOMPDeclareReductionCombiner(); 19913 19914 if (S != nullptr) 19915 PushDeclContext(S, DRD); 19916 else 19917 CurContext = DRD; 19918 19919 PushExpressionEvaluationContext( 19920 ExpressionEvaluationContext::PotentiallyEvaluated); 19921 19922 QualType ReductionType = DRD->getType(); 19923 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will 19924 // be replaced by '*omp_parm' during codegen. This required because 'omp_in' 19925 // uses semantics of argument handles by value, but it should be passed by 19926 // reference. C lang does not support references, so pass all parameters as 19927 // pointers. 19928 // Create 'T omp_in;' variable. 19929 VarDecl *OmpInParm = 19930 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in"); 19931 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will 19932 // be replaced by '*omp_parm' during codegen. This required because 'omp_out' 19933 // uses semantics of argument handles by value, but it should be passed by 19934 // reference. C lang does not support references, so pass all parameters as 19935 // pointers. 19936 // Create 'T omp_out;' variable. 19937 VarDecl *OmpOutParm = 19938 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out"); 19939 if (S != nullptr) { 19940 PushOnScopeChains(OmpInParm, S); 19941 PushOnScopeChains(OmpOutParm, S); 19942 } else { 19943 DRD->addDecl(OmpInParm); 19944 DRD->addDecl(OmpOutParm); 19945 } 19946 Expr *InE = 19947 ::buildDeclRefExpr(*this, OmpInParm, ReductionType, D->getLocation()); 19948 Expr *OutE = 19949 ::buildDeclRefExpr(*this, OmpOutParm, ReductionType, D->getLocation()); 19950 DRD->setCombinerData(InE, OutE); 19951 } 19952 19953 void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) { 19954 auto *DRD = cast<OMPDeclareReductionDecl>(D); 19955 DiscardCleanupsInEvaluationContext(); 19956 PopExpressionEvaluationContext(); 19957 19958 PopDeclContext(); 19959 PopFunctionScopeInfo(); 19960 19961 if (Combiner != nullptr) 19962 DRD->setCombiner(Combiner); 19963 else 19964 DRD->setInvalidDecl(); 19965 } 19966 19967 VarDecl *Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) { 19968 auto *DRD = cast<OMPDeclareReductionDecl>(D); 19969 19970 // Enter new function scope. 19971 PushFunctionScope(); 19972 setFunctionHasBranchProtectedScope(); 19973 19974 if (S != nullptr) 19975 PushDeclContext(S, DRD); 19976 else 19977 CurContext = DRD; 19978 19979 PushExpressionEvaluationContext( 19980 ExpressionEvaluationContext::PotentiallyEvaluated); 19981 19982 QualType ReductionType = DRD->getType(); 19983 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will 19984 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv' 19985 // uses semantics of argument handles by value, but it should be passed by 19986 // reference. C lang does not support references, so pass all parameters as 19987 // pointers. 19988 // Create 'T omp_priv;' variable. 19989 VarDecl *OmpPrivParm = 19990 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv"); 19991 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will 19992 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig' 19993 // uses semantics of argument handles by value, but it should be passed by 19994 // reference. C lang does not support references, so pass all parameters as 19995 // pointers. 19996 // Create 'T omp_orig;' variable. 19997 VarDecl *OmpOrigParm = 19998 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig"); 19999 if (S != nullptr) { 20000 PushOnScopeChains(OmpPrivParm, S); 20001 PushOnScopeChains(OmpOrigParm, S); 20002 } else { 20003 DRD->addDecl(OmpPrivParm); 20004 DRD->addDecl(OmpOrigParm); 20005 } 20006 Expr *OrigE = 20007 ::buildDeclRefExpr(*this, OmpOrigParm, ReductionType, D->getLocation()); 20008 Expr *PrivE = 20009 ::buildDeclRefExpr(*this, OmpPrivParm, ReductionType, D->getLocation()); 20010 DRD->setInitializerData(OrigE, PrivE); 20011 return OmpPrivParm; 20012 } 20013 20014 void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, Expr *Initializer, 20015 VarDecl *OmpPrivParm) { 20016 auto *DRD = cast<OMPDeclareReductionDecl>(D); 20017 DiscardCleanupsInEvaluationContext(); 20018 PopExpressionEvaluationContext(); 20019 20020 PopDeclContext(); 20021 PopFunctionScopeInfo(); 20022 20023 if (Initializer != nullptr) { 20024 DRD->setInitializer(Initializer, OMPDeclareReductionDecl::CallInit); 20025 } else if (OmpPrivParm->hasInit()) { 20026 DRD->setInitializer(OmpPrivParm->getInit(), 20027 OmpPrivParm->isDirectInit() 20028 ? OMPDeclareReductionDecl::DirectInit 20029 : OMPDeclareReductionDecl::CopyInit); 20030 } else { 20031 DRD->setInvalidDecl(); 20032 } 20033 } 20034 20035 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd( 20036 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) { 20037 for (Decl *D : DeclReductions.get()) { 20038 if (IsValid) { 20039 if (S) 20040 PushOnScopeChains(cast<OMPDeclareReductionDecl>(D), S, 20041 /*AddToContext=*/false); 20042 } else { 20043 D->setInvalidDecl(); 20044 } 20045 } 20046 return DeclReductions; 20047 } 20048 20049 TypeResult Sema::ActOnOpenMPDeclareMapperVarDecl(Scope *S, Declarator &D) { 20050 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); 20051 QualType T = TInfo->getType(); 20052 if (D.isInvalidType()) 20053 return true; 20054 20055 if (getLangOpts().CPlusPlus) { 20056 // Check that there are no default arguments (C++ only). 20057 CheckExtraCXXDefaultArguments(D); 20058 } 20059 20060 return CreateParsedType(T, TInfo); 20061 } 20062 20063 QualType Sema::ActOnOpenMPDeclareMapperType(SourceLocation TyLoc, 20064 TypeResult ParsedType) { 20065 assert(ParsedType.isUsable() && "Expect usable parsed mapper type"); 20066 20067 QualType MapperType = GetTypeFromParser(ParsedType.get()); 20068 assert(!MapperType.isNull() && "Expect valid mapper type"); 20069 20070 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions 20071 // The type must be of struct, union or class type in C and C++ 20072 if (!MapperType->isStructureOrClassType() && !MapperType->isUnionType()) { 20073 Diag(TyLoc, diag::err_omp_mapper_wrong_type); 20074 return QualType(); 20075 } 20076 return MapperType; 20077 } 20078 20079 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareMapperDirective( 20080 Scope *S, DeclContext *DC, DeclarationName Name, QualType MapperType, 20081 SourceLocation StartLoc, DeclarationName VN, AccessSpecifier AS, 20082 Expr *MapperVarRef, ArrayRef<OMPClause *> Clauses, Decl *PrevDeclInScope) { 20083 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPMapperName, 20084 forRedeclarationInCurContext()); 20085 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions 20086 // A mapper-identifier may not be redeclared in the current scope for the 20087 // same type or for a type that is compatible according to the base language 20088 // rules. 20089 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes; 20090 OMPDeclareMapperDecl *PrevDMD = nullptr; 20091 bool InCompoundScope = true; 20092 if (S != nullptr) { 20093 // Find previous declaration with the same name not referenced in other 20094 // declarations. 20095 FunctionScopeInfo *ParentFn = getEnclosingFunction(); 20096 InCompoundScope = 20097 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty(); 20098 LookupName(Lookup, S); 20099 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false, 20100 /*AllowInlineNamespace=*/false); 20101 llvm::DenseMap<OMPDeclareMapperDecl *, bool> UsedAsPrevious; 20102 LookupResult::Filter Filter = Lookup.makeFilter(); 20103 while (Filter.hasNext()) { 20104 auto *PrevDecl = cast<OMPDeclareMapperDecl>(Filter.next()); 20105 if (InCompoundScope) { 20106 auto I = UsedAsPrevious.find(PrevDecl); 20107 if (I == UsedAsPrevious.end()) 20108 UsedAsPrevious[PrevDecl] = false; 20109 if (OMPDeclareMapperDecl *D = PrevDecl->getPrevDeclInScope()) 20110 UsedAsPrevious[D] = true; 20111 } 20112 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] = 20113 PrevDecl->getLocation(); 20114 } 20115 Filter.done(); 20116 if (InCompoundScope) { 20117 for (const auto &PrevData : UsedAsPrevious) { 20118 if (!PrevData.second) { 20119 PrevDMD = PrevData.first; 20120 break; 20121 } 20122 } 20123 } 20124 } else if (PrevDeclInScope) { 20125 auto *PrevDMDInScope = PrevDMD = 20126 cast<OMPDeclareMapperDecl>(PrevDeclInScope); 20127 do { 20128 PreviousRedeclTypes[PrevDMDInScope->getType().getCanonicalType()] = 20129 PrevDMDInScope->getLocation(); 20130 PrevDMDInScope = PrevDMDInScope->getPrevDeclInScope(); 20131 } while (PrevDMDInScope != nullptr); 20132 } 20133 const auto I = PreviousRedeclTypes.find(MapperType.getCanonicalType()); 20134 bool Invalid = false; 20135 if (I != PreviousRedeclTypes.end()) { 20136 Diag(StartLoc, diag::err_omp_declare_mapper_redefinition) 20137 << MapperType << Name; 20138 Diag(I->second, diag::note_previous_definition); 20139 Invalid = true; 20140 } 20141 // Build expressions for implicit maps of data members with 'default' 20142 // mappers. 20143 SmallVector<OMPClause *, 4> ClausesWithImplicit(Clauses.begin(), 20144 Clauses.end()); 20145 if (LangOpts.OpenMP >= 50) 20146 processImplicitMapsWithDefaultMappers(*this, DSAStack, ClausesWithImplicit); 20147 auto *DMD = 20148 OMPDeclareMapperDecl::Create(Context, DC, StartLoc, Name, MapperType, VN, 20149 ClausesWithImplicit, PrevDMD); 20150 if (S) 20151 PushOnScopeChains(DMD, S); 20152 else 20153 DC->addDecl(DMD); 20154 DMD->setAccess(AS); 20155 if (Invalid) 20156 DMD->setInvalidDecl(); 20157 20158 auto *VD = cast<DeclRefExpr>(MapperVarRef)->getDecl(); 20159 VD->setDeclContext(DMD); 20160 VD->setLexicalDeclContext(DMD); 20161 DMD->addDecl(VD); 20162 DMD->setMapperVarRef(MapperVarRef); 20163 20164 return DeclGroupPtrTy::make(DeclGroupRef(DMD)); 20165 } 20166 20167 ExprResult 20168 Sema::ActOnOpenMPDeclareMapperDirectiveVarDecl(Scope *S, QualType MapperType, 20169 SourceLocation StartLoc, 20170 DeclarationName VN) { 20171 TypeSourceInfo *TInfo = 20172 Context.getTrivialTypeSourceInfo(MapperType, StartLoc); 20173 auto *VD = VarDecl::Create(Context, Context.getTranslationUnitDecl(), 20174 StartLoc, StartLoc, VN.getAsIdentifierInfo(), 20175 MapperType, TInfo, SC_None); 20176 if (S) 20177 PushOnScopeChains(VD, S, /*AddToContext=*/false); 20178 Expr *E = buildDeclRefExpr(*this, VD, MapperType, StartLoc); 20179 DSAStack->addDeclareMapperVarRef(E); 20180 return E; 20181 } 20182 20183 bool Sema::isOpenMPDeclareMapperVarDeclAllowed(const VarDecl *VD) const { 20184 assert(LangOpts.OpenMP && "Expected OpenMP mode."); 20185 const Expr *Ref = DSAStack->getDeclareMapperVarRef(); 20186 if (const auto *DRE = cast_or_null<DeclRefExpr>(Ref)) { 20187 if (VD->getCanonicalDecl() == DRE->getDecl()->getCanonicalDecl()) 20188 return true; 20189 if (VD->isUsableInConstantExpressions(Context)) 20190 return true; 20191 return false; 20192 } 20193 return true; 20194 } 20195 20196 const ValueDecl *Sema::getOpenMPDeclareMapperVarName() const { 20197 assert(LangOpts.OpenMP && "Expected OpenMP mode."); 20198 return cast<DeclRefExpr>(DSAStack->getDeclareMapperVarRef())->getDecl(); 20199 } 20200 20201 OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams, 20202 SourceLocation StartLoc, 20203 SourceLocation LParenLoc, 20204 SourceLocation EndLoc) { 20205 Expr *ValExpr = NumTeams; 20206 Stmt *HelperValStmt = nullptr; 20207 20208 // OpenMP [teams Constrcut, Restrictions] 20209 // The num_teams expression must evaluate to a positive integer value. 20210 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams, 20211 /*StrictlyPositive=*/true)) 20212 return nullptr; 20213 20214 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 20215 OpenMPDirectiveKind CaptureRegion = 20216 getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams, LangOpts.OpenMP); 20217 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 20218 ValExpr = MakeFullExpr(ValExpr).get(); 20219 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 20220 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 20221 HelperValStmt = buildPreInits(Context, Captures); 20222 } 20223 20224 return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion, 20225 StartLoc, LParenLoc, EndLoc); 20226 } 20227 20228 OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit, 20229 SourceLocation StartLoc, 20230 SourceLocation LParenLoc, 20231 SourceLocation EndLoc) { 20232 Expr *ValExpr = ThreadLimit; 20233 Stmt *HelperValStmt = nullptr; 20234 20235 // OpenMP [teams Constrcut, Restrictions] 20236 // The thread_limit expression must evaluate to a positive integer value. 20237 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit, 20238 /*StrictlyPositive=*/true)) 20239 return nullptr; 20240 20241 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 20242 OpenMPDirectiveKind CaptureRegion = getOpenMPCaptureRegionForClause( 20243 DKind, OMPC_thread_limit, LangOpts.OpenMP); 20244 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 20245 ValExpr = MakeFullExpr(ValExpr).get(); 20246 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 20247 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 20248 HelperValStmt = buildPreInits(Context, Captures); 20249 } 20250 20251 return new (Context) OMPThreadLimitClause( 20252 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc); 20253 } 20254 20255 OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority, 20256 SourceLocation StartLoc, 20257 SourceLocation LParenLoc, 20258 SourceLocation EndLoc) { 20259 Expr *ValExpr = Priority; 20260 Stmt *HelperValStmt = nullptr; 20261 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 20262 20263 // OpenMP [2.9.1, task Constrcut] 20264 // The priority-value is a non-negative numerical scalar expression. 20265 if (!isNonNegativeIntegerValue( 20266 ValExpr, *this, OMPC_priority, 20267 /*StrictlyPositive=*/false, /*BuildCapture=*/true, 20268 DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt)) 20269 return nullptr; 20270 20271 return new (Context) OMPPriorityClause(ValExpr, HelperValStmt, CaptureRegion, 20272 StartLoc, LParenLoc, EndLoc); 20273 } 20274 20275 OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize, 20276 SourceLocation StartLoc, 20277 SourceLocation LParenLoc, 20278 SourceLocation EndLoc) { 20279 Expr *ValExpr = Grainsize; 20280 Stmt *HelperValStmt = nullptr; 20281 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 20282 20283 // OpenMP [2.9.2, taskloop Constrcut] 20284 // The parameter of the grainsize clause must be a positive integer 20285 // expression. 20286 if (!isNonNegativeIntegerValue( 20287 ValExpr, *this, OMPC_grainsize, 20288 /*StrictlyPositive=*/true, /*BuildCapture=*/true, 20289 DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt)) 20290 return nullptr; 20291 20292 return new (Context) OMPGrainsizeClause(ValExpr, HelperValStmt, CaptureRegion, 20293 StartLoc, LParenLoc, EndLoc); 20294 } 20295 20296 OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks, 20297 SourceLocation StartLoc, 20298 SourceLocation LParenLoc, 20299 SourceLocation EndLoc) { 20300 Expr *ValExpr = NumTasks; 20301 Stmt *HelperValStmt = nullptr; 20302 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 20303 20304 // OpenMP [2.9.2, taskloop Constrcut] 20305 // The parameter of the num_tasks clause must be a positive integer 20306 // expression. 20307 if (!isNonNegativeIntegerValue( 20308 ValExpr, *this, OMPC_num_tasks, 20309 /*StrictlyPositive=*/true, /*BuildCapture=*/true, 20310 DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt)) 20311 return nullptr; 20312 20313 return new (Context) OMPNumTasksClause(ValExpr, HelperValStmt, CaptureRegion, 20314 StartLoc, LParenLoc, EndLoc); 20315 } 20316 20317 OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc, 20318 SourceLocation LParenLoc, 20319 SourceLocation EndLoc) { 20320 // OpenMP [2.13.2, critical construct, Description] 20321 // ... where hint-expression is an integer constant expression that evaluates 20322 // to a valid lock hint. 20323 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint); 20324 if (HintExpr.isInvalid()) 20325 return nullptr; 20326 return new (Context) 20327 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc); 20328 } 20329 20330 /// Tries to find omp_event_handle_t type. 20331 static bool findOMPEventHandleT(Sema &S, SourceLocation Loc, 20332 DSAStackTy *Stack) { 20333 QualType OMPEventHandleT = Stack->getOMPEventHandleT(); 20334 if (!OMPEventHandleT.isNull()) 20335 return true; 20336 IdentifierInfo *II = &S.PP.getIdentifierTable().get("omp_event_handle_t"); 20337 ParsedType PT = S.getTypeName(*II, Loc, S.getCurScope()); 20338 if (!PT.getAsOpaquePtr() || PT.get().isNull()) { 20339 S.Diag(Loc, diag::err_omp_implied_type_not_found) << "omp_event_handle_t"; 20340 return false; 20341 } 20342 Stack->setOMPEventHandleT(PT.get()); 20343 return true; 20344 } 20345 20346 OMPClause *Sema::ActOnOpenMPDetachClause(Expr *Evt, SourceLocation StartLoc, 20347 SourceLocation LParenLoc, 20348 SourceLocation EndLoc) { 20349 if (!Evt->isValueDependent() && !Evt->isTypeDependent() && 20350 !Evt->isInstantiationDependent() && 20351 !Evt->containsUnexpandedParameterPack()) { 20352 if (!findOMPEventHandleT(*this, Evt->getExprLoc(), DSAStack)) 20353 return nullptr; 20354 // OpenMP 5.0, 2.10.1 task Construct. 20355 // event-handle is a variable of the omp_event_handle_t type. 20356 auto *Ref = dyn_cast<DeclRefExpr>(Evt->IgnoreParenImpCasts()); 20357 if (!Ref) { 20358 Diag(Evt->getExprLoc(), diag::err_omp_var_expected) 20359 << "omp_event_handle_t" << 0 << Evt->getSourceRange(); 20360 return nullptr; 20361 } 20362 auto *VD = dyn_cast_or_null<VarDecl>(Ref->getDecl()); 20363 if (!VD) { 20364 Diag(Evt->getExprLoc(), diag::err_omp_var_expected) 20365 << "omp_event_handle_t" << 0 << Evt->getSourceRange(); 20366 return nullptr; 20367 } 20368 if (!Context.hasSameUnqualifiedType(DSAStack->getOMPEventHandleT(), 20369 VD->getType()) || 20370 VD->getType().isConstant(Context)) { 20371 Diag(Evt->getExprLoc(), diag::err_omp_var_expected) 20372 << "omp_event_handle_t" << 1 << VD->getType() 20373 << Evt->getSourceRange(); 20374 return nullptr; 20375 } 20376 // OpenMP 5.0, 2.10.1 task Construct 20377 // [detach clause]... The event-handle will be considered as if it was 20378 // specified on a firstprivate clause. 20379 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, /*FromParent=*/false); 20380 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate && 20381 DVar.RefExpr) { 20382 Diag(Evt->getExprLoc(), diag::err_omp_wrong_dsa) 20383 << getOpenMPClauseName(DVar.CKind) 20384 << getOpenMPClauseName(OMPC_firstprivate); 20385 reportOriginalDsa(*this, DSAStack, VD, DVar); 20386 return nullptr; 20387 } 20388 } 20389 20390 return new (Context) OMPDetachClause(Evt, StartLoc, LParenLoc, EndLoc); 20391 } 20392 20393 OMPClause *Sema::ActOnOpenMPDistScheduleClause( 20394 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, 20395 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc, 20396 SourceLocation EndLoc) { 20397 if (Kind == OMPC_DIST_SCHEDULE_unknown) { 20398 std::string Values; 20399 Values += "'"; 20400 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0); 20401 Values += "'"; 20402 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 20403 << Values << getOpenMPClauseName(OMPC_dist_schedule); 20404 return nullptr; 20405 } 20406 Expr *ValExpr = ChunkSize; 20407 Stmt *HelperValStmt = nullptr; 20408 if (ChunkSize) { 20409 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && 20410 !ChunkSize->isInstantiationDependent() && 20411 !ChunkSize->containsUnexpandedParameterPack()) { 20412 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc(); 20413 ExprResult Val = 20414 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); 20415 if (Val.isInvalid()) 20416 return nullptr; 20417 20418 ValExpr = Val.get(); 20419 20420 // OpenMP [2.7.1, Restrictions] 20421 // chunk_size must be a loop invariant integer expression with a positive 20422 // value. 20423 if (Optional<llvm::APSInt> Result = 20424 ValExpr->getIntegerConstantExpr(Context)) { 20425 if (Result->isSigned() && !Result->isStrictlyPositive()) { 20426 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) 20427 << "dist_schedule" << ChunkSize->getSourceRange(); 20428 return nullptr; 20429 } 20430 } else if (getOpenMPCaptureRegionForClause( 20431 DSAStack->getCurrentDirective(), OMPC_dist_schedule, 20432 LangOpts.OpenMP) != OMPD_unknown && 20433 !CurContext->isDependentContext()) { 20434 ValExpr = MakeFullExpr(ValExpr).get(); 20435 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 20436 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 20437 HelperValStmt = buildPreInits(Context, Captures); 20438 } 20439 } 20440 } 20441 20442 return new (Context) 20443 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, 20444 Kind, ValExpr, HelperValStmt); 20445 } 20446 20447 OMPClause *Sema::ActOnOpenMPDefaultmapClause( 20448 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind, 20449 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc, 20450 SourceLocation KindLoc, SourceLocation EndLoc) { 20451 if (getLangOpts().OpenMP < 50) { 20452 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || 20453 Kind != OMPC_DEFAULTMAP_scalar) { 20454 std::string Value; 20455 SourceLocation Loc; 20456 Value += "'"; 20457 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) { 20458 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap, 20459 OMPC_DEFAULTMAP_MODIFIER_tofrom); 20460 Loc = MLoc; 20461 } else { 20462 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap, 20463 OMPC_DEFAULTMAP_scalar); 20464 Loc = KindLoc; 20465 } 20466 Value += "'"; 20467 Diag(Loc, diag::err_omp_unexpected_clause_value) 20468 << Value << getOpenMPClauseName(OMPC_defaultmap); 20469 return nullptr; 20470 } 20471 } else { 20472 bool isDefaultmapModifier = (M != OMPC_DEFAULTMAP_MODIFIER_unknown); 20473 bool isDefaultmapKind = (Kind != OMPC_DEFAULTMAP_unknown) || 20474 (LangOpts.OpenMP >= 50 && KindLoc.isInvalid()); 20475 if (!isDefaultmapKind || !isDefaultmapModifier) { 20476 StringRef KindValue = "'scalar', 'aggregate', 'pointer'"; 20477 if (LangOpts.OpenMP == 50) { 20478 StringRef ModifierValue = "'alloc', 'from', 'to', 'tofrom', " 20479 "'firstprivate', 'none', 'default'"; 20480 if (!isDefaultmapKind && isDefaultmapModifier) { 20481 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 20482 << KindValue << getOpenMPClauseName(OMPC_defaultmap); 20483 } else if (isDefaultmapKind && !isDefaultmapModifier) { 20484 Diag(MLoc, diag::err_omp_unexpected_clause_value) 20485 << ModifierValue << getOpenMPClauseName(OMPC_defaultmap); 20486 } else { 20487 Diag(MLoc, diag::err_omp_unexpected_clause_value) 20488 << ModifierValue << getOpenMPClauseName(OMPC_defaultmap); 20489 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 20490 << KindValue << getOpenMPClauseName(OMPC_defaultmap); 20491 } 20492 } else { 20493 StringRef ModifierValue = 20494 "'alloc', 'from', 'to', 'tofrom', " 20495 "'firstprivate', 'none', 'default', 'present'"; 20496 if (!isDefaultmapKind && isDefaultmapModifier) { 20497 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 20498 << KindValue << getOpenMPClauseName(OMPC_defaultmap); 20499 } else if (isDefaultmapKind && !isDefaultmapModifier) { 20500 Diag(MLoc, diag::err_omp_unexpected_clause_value) 20501 << ModifierValue << getOpenMPClauseName(OMPC_defaultmap); 20502 } else { 20503 Diag(MLoc, diag::err_omp_unexpected_clause_value) 20504 << ModifierValue << getOpenMPClauseName(OMPC_defaultmap); 20505 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 20506 << KindValue << getOpenMPClauseName(OMPC_defaultmap); 20507 } 20508 } 20509 return nullptr; 20510 } 20511 20512 // OpenMP [5.0, 2.12.5, Restrictions, p. 174] 20513 // At most one defaultmap clause for each category can appear on the 20514 // directive. 20515 if (DSAStack->checkDefaultmapCategory(Kind)) { 20516 Diag(StartLoc, diag::err_omp_one_defaultmap_each_category); 20517 return nullptr; 20518 } 20519 } 20520 if (Kind == OMPC_DEFAULTMAP_unknown) { 20521 // Variable category is not specified - mark all categories. 20522 DSAStack->setDefaultDMAAttr(M, OMPC_DEFAULTMAP_aggregate, StartLoc); 20523 DSAStack->setDefaultDMAAttr(M, OMPC_DEFAULTMAP_scalar, StartLoc); 20524 DSAStack->setDefaultDMAAttr(M, OMPC_DEFAULTMAP_pointer, StartLoc); 20525 } else { 20526 DSAStack->setDefaultDMAAttr(M, Kind, StartLoc); 20527 } 20528 20529 return new (Context) 20530 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M); 20531 } 20532 20533 bool Sema::ActOnStartOpenMPDeclareTargetContext( 20534 DeclareTargetContextInfo &DTCI) { 20535 DeclContext *CurLexicalContext = getCurLexicalContext(); 20536 if (!CurLexicalContext->isFileContext() && 20537 !CurLexicalContext->isExternCContext() && 20538 !CurLexicalContext->isExternCXXContext() && 20539 !isa<CXXRecordDecl>(CurLexicalContext) && 20540 !isa<ClassTemplateDecl>(CurLexicalContext) && 20541 !isa<ClassTemplatePartialSpecializationDecl>(CurLexicalContext) && 20542 !isa<ClassTemplateSpecializationDecl>(CurLexicalContext)) { 20543 Diag(DTCI.Loc, diag::err_omp_region_not_file_context); 20544 return false; 20545 } 20546 DeclareTargetNesting.push_back(DTCI); 20547 return true; 20548 } 20549 20550 const Sema::DeclareTargetContextInfo 20551 Sema::ActOnOpenMPEndDeclareTargetDirective() { 20552 assert(!DeclareTargetNesting.empty() && 20553 "check isInOpenMPDeclareTargetContext() first!"); 20554 return DeclareTargetNesting.pop_back_val(); 20555 } 20556 20557 void Sema::ActOnFinishedOpenMPDeclareTargetContext( 20558 DeclareTargetContextInfo &DTCI) { 20559 for (auto &It : DTCI.ExplicitlyMapped) 20560 ActOnOpenMPDeclareTargetName(It.first, It.second.Loc, It.second.MT, 20561 DTCI.DT); 20562 } 20563 20564 NamedDecl *Sema::lookupOpenMPDeclareTargetName(Scope *CurScope, 20565 CXXScopeSpec &ScopeSpec, 20566 const DeclarationNameInfo &Id) { 20567 LookupResult Lookup(*this, Id, LookupOrdinaryName); 20568 LookupParsedName(Lookup, CurScope, &ScopeSpec, true); 20569 20570 if (Lookup.isAmbiguous()) 20571 return nullptr; 20572 Lookup.suppressDiagnostics(); 20573 20574 if (!Lookup.isSingleResult()) { 20575 VarOrFuncDeclFilterCCC CCC(*this); 20576 if (TypoCorrection Corrected = 20577 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, CCC, 20578 CTK_ErrorRecovery)) { 20579 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest) 20580 << Id.getName()); 20581 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl()); 20582 return nullptr; 20583 } 20584 20585 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName(); 20586 return nullptr; 20587 } 20588 20589 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>(); 20590 if (!isa<VarDecl>(ND) && !isa<FunctionDecl>(ND) && 20591 !isa<FunctionTemplateDecl>(ND)) { 20592 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName(); 20593 return nullptr; 20594 } 20595 return ND; 20596 } 20597 20598 void Sema::ActOnOpenMPDeclareTargetName( 20599 NamedDecl *ND, SourceLocation Loc, OMPDeclareTargetDeclAttr::MapTypeTy MT, 20600 OMPDeclareTargetDeclAttr::DevTypeTy DT) { 20601 assert((isa<VarDecl>(ND) || isa<FunctionDecl>(ND) || 20602 isa<FunctionTemplateDecl>(ND)) && 20603 "Expected variable, function or function template."); 20604 20605 // Diagnose marking after use as it may lead to incorrect diagnosis and 20606 // codegen. 20607 if (LangOpts.OpenMP >= 50 && 20608 (ND->isUsed(/*CheckUsedAttr=*/false) || ND->isReferenced())) 20609 Diag(Loc, diag::warn_omp_declare_target_after_first_use); 20610 20611 // Explicit declare target lists have precedence. 20612 const unsigned Level = -1; 20613 20614 auto *VD = cast<ValueDecl>(ND); 20615 llvm::Optional<OMPDeclareTargetDeclAttr *> ActiveAttr = 20616 OMPDeclareTargetDeclAttr::getActiveAttr(VD); 20617 if (ActiveAttr.hasValue() && ActiveAttr.getValue()->getDevType() != DT && 20618 ActiveAttr.getValue()->getLevel() == Level) { 20619 Diag(Loc, diag::err_omp_device_type_mismatch) 20620 << OMPDeclareTargetDeclAttr::ConvertDevTypeTyToStr(DT) 20621 << OMPDeclareTargetDeclAttr::ConvertDevTypeTyToStr( 20622 ActiveAttr.getValue()->getDevType()); 20623 return; 20624 } 20625 if (ActiveAttr.hasValue() && ActiveAttr.getValue()->getMapType() != MT && 20626 ActiveAttr.getValue()->getLevel() == Level) { 20627 Diag(Loc, diag::err_omp_declare_target_to_and_link) << ND; 20628 return; 20629 } 20630 20631 if (ActiveAttr.hasValue() && ActiveAttr.getValue()->getLevel() == Level) 20632 return; 20633 20634 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT, DT, Level, 20635 SourceRange(Loc, Loc)); 20636 ND->addAttr(A); 20637 if (ASTMutationListener *ML = Context.getASTMutationListener()) 20638 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A); 20639 checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Loc); 20640 } 20641 20642 static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR, 20643 Sema &SemaRef, Decl *D) { 20644 if (!D || !isa<VarDecl>(D)) 20645 return; 20646 auto *VD = cast<VarDecl>(D); 20647 Optional<OMPDeclareTargetDeclAttr::MapTypeTy> MapTy = 20648 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); 20649 if (SemaRef.LangOpts.OpenMP >= 50 && 20650 (SemaRef.getCurLambda(/*IgnoreNonLambdaCapturingScope=*/true) || 20651 SemaRef.getCurBlock() || SemaRef.getCurCapturedRegion()) && 20652 VD->hasGlobalStorage()) { 20653 if (!MapTy || *MapTy != OMPDeclareTargetDeclAttr::MT_To) { 20654 // OpenMP 5.0, 2.12.7 declare target Directive, Restrictions 20655 // If a lambda declaration and definition appears between a 20656 // declare target directive and the matching end declare target 20657 // directive, all variables that are captured by the lambda 20658 // expression must also appear in a to clause. 20659 SemaRef.Diag(VD->getLocation(), 20660 diag::err_omp_lambda_capture_in_declare_target_not_to); 20661 SemaRef.Diag(SL, diag::note_var_explicitly_captured_here) 20662 << VD << 0 << SR; 20663 return; 20664 } 20665 } 20666 if (MapTy.hasValue()) 20667 return; 20668 SemaRef.Diag(VD->getLocation(), diag::warn_omp_not_in_target_context); 20669 SemaRef.Diag(SL, diag::note_used_here) << SR; 20670 } 20671 20672 static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR, 20673 Sema &SemaRef, DSAStackTy *Stack, 20674 ValueDecl *VD) { 20675 return OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD) || 20676 checkTypeMappable(SL, SR, SemaRef, Stack, VD->getType(), 20677 /*FullCheck=*/false); 20678 } 20679 20680 void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D, 20681 SourceLocation IdLoc) { 20682 if (!D || D->isInvalidDecl()) 20683 return; 20684 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange(); 20685 SourceLocation SL = E ? E->getBeginLoc() : D->getLocation(); 20686 if (auto *VD = dyn_cast<VarDecl>(D)) { 20687 // Only global variables can be marked as declare target. 20688 if (!VD->isFileVarDecl() && !VD->isStaticLocal() && 20689 !VD->isStaticDataMember()) 20690 return; 20691 // 2.10.6: threadprivate variable cannot appear in a declare target 20692 // directive. 20693 if (DSAStack->isThreadPrivate(VD)) { 20694 Diag(SL, diag::err_omp_threadprivate_in_target); 20695 reportOriginalDsa(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false)); 20696 return; 20697 } 20698 } 20699 if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(D)) 20700 D = FTD->getTemplatedDecl(); 20701 if (auto *FD = dyn_cast<FunctionDecl>(D)) { 20702 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 20703 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(FD); 20704 if (IdLoc.isValid() && Res && *Res == OMPDeclareTargetDeclAttr::MT_Link) { 20705 Diag(IdLoc, diag::err_omp_function_in_link_clause); 20706 Diag(FD->getLocation(), diag::note_defined_here) << FD; 20707 return; 20708 } 20709 } 20710 if (auto *VD = dyn_cast<ValueDecl>(D)) { 20711 // Problem if any with var declared with incomplete type will be reported 20712 // as normal, so no need to check it here. 20713 if ((E || !VD->getType()->isIncompleteType()) && 20714 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) 20715 return; 20716 if (!E && isInOpenMPDeclareTargetContext()) { 20717 // Checking declaration inside declare target region. 20718 if (isa<VarDecl>(D) || isa<FunctionDecl>(D) || 20719 isa<FunctionTemplateDecl>(D)) { 20720 llvm::Optional<OMPDeclareTargetDeclAttr *> ActiveAttr = 20721 OMPDeclareTargetDeclAttr::getActiveAttr(VD); 20722 unsigned Level = DeclareTargetNesting.size(); 20723 if (ActiveAttr.hasValue() && ActiveAttr.getValue()->getLevel() >= Level) 20724 return; 20725 DeclareTargetContextInfo &DTCI = DeclareTargetNesting.back(); 20726 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit( 20727 Context, OMPDeclareTargetDeclAttr::MT_To, DTCI.DT, Level, 20728 SourceRange(DTCI.Loc, DTCI.Loc)); 20729 D->addAttr(A); 20730 if (ASTMutationListener *ML = Context.getASTMutationListener()) 20731 ML->DeclarationMarkedOpenMPDeclareTarget(D, A); 20732 } 20733 return; 20734 } 20735 } 20736 if (!E) 20737 return; 20738 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D); 20739 } 20740 20741 OMPClause *Sema::ActOnOpenMPToClause( 20742 ArrayRef<OpenMPMotionModifierKind> MotionModifiers, 20743 ArrayRef<SourceLocation> MotionModifiersLoc, 20744 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId, 20745 SourceLocation ColonLoc, ArrayRef<Expr *> VarList, 20746 const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) { 20747 OpenMPMotionModifierKind Modifiers[] = {OMPC_MOTION_MODIFIER_unknown, 20748 OMPC_MOTION_MODIFIER_unknown}; 20749 SourceLocation ModifiersLoc[NumberOfOMPMotionModifiers]; 20750 20751 // Process motion-modifiers, flag errors for duplicate modifiers. 20752 unsigned Count = 0; 20753 for (unsigned I = 0, E = MotionModifiers.size(); I < E; ++I) { 20754 if (MotionModifiers[I] != OMPC_MOTION_MODIFIER_unknown && 20755 llvm::is_contained(Modifiers, MotionModifiers[I])) { 20756 Diag(MotionModifiersLoc[I], diag::err_omp_duplicate_motion_modifier); 20757 continue; 20758 } 20759 assert(Count < NumberOfOMPMotionModifiers && 20760 "Modifiers exceed the allowed number of motion modifiers"); 20761 Modifiers[Count] = MotionModifiers[I]; 20762 ModifiersLoc[Count] = MotionModifiersLoc[I]; 20763 ++Count; 20764 } 20765 20766 MappableVarListInfo MVLI(VarList); 20767 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, Locs.StartLoc, 20768 MapperIdScopeSpec, MapperId, UnresolvedMappers); 20769 if (MVLI.ProcessedVarList.empty()) 20770 return nullptr; 20771 20772 return OMPToClause::Create( 20773 Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, 20774 MVLI.VarComponents, MVLI.UDMapperList, Modifiers, ModifiersLoc, 20775 MapperIdScopeSpec.getWithLocInContext(Context), MapperId); 20776 } 20777 20778 OMPClause *Sema::ActOnOpenMPFromClause( 20779 ArrayRef<OpenMPMotionModifierKind> MotionModifiers, 20780 ArrayRef<SourceLocation> MotionModifiersLoc, 20781 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId, 20782 SourceLocation ColonLoc, ArrayRef<Expr *> VarList, 20783 const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) { 20784 OpenMPMotionModifierKind Modifiers[] = {OMPC_MOTION_MODIFIER_unknown, 20785 OMPC_MOTION_MODIFIER_unknown}; 20786 SourceLocation ModifiersLoc[NumberOfOMPMotionModifiers]; 20787 20788 // Process motion-modifiers, flag errors for duplicate modifiers. 20789 unsigned Count = 0; 20790 for (unsigned I = 0, E = MotionModifiers.size(); I < E; ++I) { 20791 if (MotionModifiers[I] != OMPC_MOTION_MODIFIER_unknown && 20792 llvm::is_contained(Modifiers, MotionModifiers[I])) { 20793 Diag(MotionModifiersLoc[I], diag::err_omp_duplicate_motion_modifier); 20794 continue; 20795 } 20796 assert(Count < NumberOfOMPMotionModifiers && 20797 "Modifiers exceed the allowed number of motion modifiers"); 20798 Modifiers[Count] = MotionModifiers[I]; 20799 ModifiersLoc[Count] = MotionModifiersLoc[I]; 20800 ++Count; 20801 } 20802 20803 MappableVarListInfo MVLI(VarList); 20804 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, Locs.StartLoc, 20805 MapperIdScopeSpec, MapperId, UnresolvedMappers); 20806 if (MVLI.ProcessedVarList.empty()) 20807 return nullptr; 20808 20809 return OMPFromClause::Create( 20810 Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, 20811 MVLI.VarComponents, MVLI.UDMapperList, Modifiers, ModifiersLoc, 20812 MapperIdScopeSpec.getWithLocInContext(Context), MapperId); 20813 } 20814 20815 OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList, 20816 const OMPVarListLocTy &Locs) { 20817 MappableVarListInfo MVLI(VarList); 20818 SmallVector<Expr *, 8> PrivateCopies; 20819 SmallVector<Expr *, 8> Inits; 20820 20821 for (Expr *RefExpr : VarList) { 20822 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause."); 20823 SourceLocation ELoc; 20824 SourceRange ERange; 20825 Expr *SimpleRefExpr = RefExpr; 20826 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 20827 if (Res.second) { 20828 // It will be analyzed later. 20829 MVLI.ProcessedVarList.push_back(RefExpr); 20830 PrivateCopies.push_back(nullptr); 20831 Inits.push_back(nullptr); 20832 } 20833 ValueDecl *D = Res.first; 20834 if (!D) 20835 continue; 20836 20837 QualType Type = D->getType(); 20838 Type = Type.getNonReferenceType().getUnqualifiedType(); 20839 20840 auto *VD = dyn_cast<VarDecl>(D); 20841 20842 // Item should be a pointer or reference to pointer. 20843 if (!Type->isPointerType()) { 20844 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer) 20845 << 0 << RefExpr->getSourceRange(); 20846 continue; 20847 } 20848 20849 // Build the private variable and the expression that refers to it. 20850 auto VDPrivate = 20851 buildVarDecl(*this, ELoc, Type, D->getName(), 20852 D->hasAttrs() ? &D->getAttrs() : nullptr, 20853 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 20854 if (VDPrivate->isInvalidDecl()) 20855 continue; 20856 20857 CurContext->addDecl(VDPrivate); 20858 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 20859 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc); 20860 20861 // Add temporary variable to initialize the private copy of the pointer. 20862 VarDecl *VDInit = 20863 buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp"); 20864 DeclRefExpr *VDInitRefExpr = buildDeclRefExpr( 20865 *this, VDInit, RefExpr->getType(), RefExpr->getExprLoc()); 20866 AddInitializerToDecl(VDPrivate, 20867 DefaultLvalueConversion(VDInitRefExpr).get(), 20868 /*DirectInit=*/false); 20869 20870 // If required, build a capture to implement the privatization initialized 20871 // with the current list item value. 20872 DeclRefExpr *Ref = nullptr; 20873 if (!VD) 20874 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 20875 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref); 20876 PrivateCopies.push_back(VDPrivateRefExpr); 20877 Inits.push_back(VDInitRefExpr); 20878 20879 // We need to add a data sharing attribute for this variable to make sure it 20880 // is correctly captured. A variable that shows up in a use_device_ptr has 20881 // similar properties of a first private variable. 20882 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 20883 20884 // Create a mappable component for the list item. List items in this clause 20885 // only need a component. 20886 MVLI.VarBaseDeclarations.push_back(D); 20887 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 20888 MVLI.VarComponents.back().emplace_back(SimpleRefExpr, D, 20889 /*IsNonContiguous=*/false); 20890 } 20891 20892 if (MVLI.ProcessedVarList.empty()) 20893 return nullptr; 20894 20895 return OMPUseDevicePtrClause::Create( 20896 Context, Locs, MVLI.ProcessedVarList, PrivateCopies, Inits, 20897 MVLI.VarBaseDeclarations, MVLI.VarComponents); 20898 } 20899 20900 OMPClause *Sema::ActOnOpenMPUseDeviceAddrClause(ArrayRef<Expr *> VarList, 20901 const OMPVarListLocTy &Locs) { 20902 MappableVarListInfo MVLI(VarList); 20903 20904 for (Expr *RefExpr : VarList) { 20905 assert(RefExpr && "NULL expr in OpenMP use_device_addr clause."); 20906 SourceLocation ELoc; 20907 SourceRange ERange; 20908 Expr *SimpleRefExpr = RefExpr; 20909 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, 20910 /*AllowArraySection=*/true); 20911 if (Res.second) { 20912 // It will be analyzed later. 20913 MVLI.ProcessedVarList.push_back(RefExpr); 20914 } 20915 ValueDecl *D = Res.first; 20916 if (!D) 20917 continue; 20918 auto *VD = dyn_cast<VarDecl>(D); 20919 20920 // If required, build a capture to implement the privatization initialized 20921 // with the current list item value. 20922 DeclRefExpr *Ref = nullptr; 20923 if (!VD) 20924 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 20925 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref); 20926 20927 // We need to add a data sharing attribute for this variable to make sure it 20928 // is correctly captured. A variable that shows up in a use_device_addr has 20929 // similar properties of a first private variable. 20930 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 20931 20932 // Create a mappable component for the list item. List items in this clause 20933 // only need a component. 20934 MVLI.VarBaseDeclarations.push_back(D); 20935 MVLI.VarComponents.emplace_back(); 20936 Expr *Component = SimpleRefExpr; 20937 if (VD && (isa<OMPArraySectionExpr>(RefExpr->IgnoreParenImpCasts()) || 20938 isa<ArraySubscriptExpr>(RefExpr->IgnoreParenImpCasts()))) 20939 Component = DefaultFunctionArrayLvalueConversion(SimpleRefExpr).get(); 20940 MVLI.VarComponents.back().emplace_back(Component, D, 20941 /*IsNonContiguous=*/false); 20942 } 20943 20944 if (MVLI.ProcessedVarList.empty()) 20945 return nullptr; 20946 20947 return OMPUseDeviceAddrClause::Create(Context, Locs, MVLI.ProcessedVarList, 20948 MVLI.VarBaseDeclarations, 20949 MVLI.VarComponents); 20950 } 20951 20952 OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList, 20953 const OMPVarListLocTy &Locs) { 20954 MappableVarListInfo MVLI(VarList); 20955 for (Expr *RefExpr : VarList) { 20956 assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause."); 20957 SourceLocation ELoc; 20958 SourceRange ERange; 20959 Expr *SimpleRefExpr = RefExpr; 20960 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 20961 if (Res.second) { 20962 // It will be analyzed later. 20963 MVLI.ProcessedVarList.push_back(RefExpr); 20964 } 20965 ValueDecl *D = Res.first; 20966 if (!D) 20967 continue; 20968 20969 QualType Type = D->getType(); 20970 // item should be a pointer or array or reference to pointer or array 20971 if (!Type.getNonReferenceType()->isPointerType() && 20972 !Type.getNonReferenceType()->isArrayType()) { 20973 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr) 20974 << 0 << RefExpr->getSourceRange(); 20975 continue; 20976 } 20977 20978 // Check if the declaration in the clause does not show up in any data 20979 // sharing attribute. 20980 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 20981 if (isOpenMPPrivate(DVar.CKind)) { 20982 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 20983 << getOpenMPClauseName(DVar.CKind) 20984 << getOpenMPClauseName(OMPC_is_device_ptr) 20985 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 20986 reportOriginalDsa(*this, DSAStack, D, DVar); 20987 continue; 20988 } 20989 20990 const Expr *ConflictExpr; 20991 if (DSAStack->checkMappableExprComponentListsForDecl( 20992 D, /*CurrentRegionOnly=*/true, 20993 [&ConflictExpr]( 20994 OMPClauseMappableExprCommon::MappableExprComponentListRef R, 20995 OpenMPClauseKind) -> bool { 20996 ConflictExpr = R.front().getAssociatedExpression(); 20997 return true; 20998 })) { 20999 Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange(); 21000 Diag(ConflictExpr->getExprLoc(), diag::note_used_here) 21001 << ConflictExpr->getSourceRange(); 21002 continue; 21003 } 21004 21005 // Store the components in the stack so that they can be used to check 21006 // against other clauses later on. 21007 OMPClauseMappableExprCommon::MappableComponent MC( 21008 SimpleRefExpr, D, /*IsNonContiguous=*/false); 21009 DSAStack->addMappableExpressionComponents( 21010 D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr); 21011 21012 // Record the expression we've just processed. 21013 MVLI.ProcessedVarList.push_back(SimpleRefExpr); 21014 21015 // Create a mappable component for the list item. List items in this clause 21016 // only need a component. We use a null declaration to signal fields in 21017 // 'this'. 21018 assert((isa<DeclRefExpr>(SimpleRefExpr) || 21019 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) && 21020 "Unexpected device pointer expression!"); 21021 MVLI.VarBaseDeclarations.push_back( 21022 isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr); 21023 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 21024 MVLI.VarComponents.back().push_back(MC); 21025 } 21026 21027 if (MVLI.ProcessedVarList.empty()) 21028 return nullptr; 21029 21030 return OMPIsDevicePtrClause::Create(Context, Locs, MVLI.ProcessedVarList, 21031 MVLI.VarBaseDeclarations, 21032 MVLI.VarComponents); 21033 } 21034 21035 OMPClause *Sema::ActOnOpenMPAllocateClause( 21036 Expr *Allocator, ArrayRef<Expr *> VarList, SourceLocation StartLoc, 21037 SourceLocation ColonLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 21038 if (Allocator) { 21039 // OpenMP [2.11.4 allocate Clause, Description] 21040 // allocator is an expression of omp_allocator_handle_t type. 21041 if (!findOMPAllocatorHandleT(*this, Allocator->getExprLoc(), DSAStack)) 21042 return nullptr; 21043 21044 ExprResult AllocatorRes = DefaultLvalueConversion(Allocator); 21045 if (AllocatorRes.isInvalid()) 21046 return nullptr; 21047 AllocatorRes = PerformImplicitConversion(AllocatorRes.get(), 21048 DSAStack->getOMPAllocatorHandleT(), 21049 Sema::AA_Initializing, 21050 /*AllowExplicit=*/true); 21051 if (AllocatorRes.isInvalid()) 21052 return nullptr; 21053 Allocator = AllocatorRes.get(); 21054 } else { 21055 // OpenMP 5.0, 2.11.4 allocate Clause, Restrictions. 21056 // allocate clauses that appear on a target construct or on constructs in a 21057 // target region must specify an allocator expression unless a requires 21058 // directive with the dynamic_allocators clause is present in the same 21059 // compilation unit. 21060 if (LangOpts.OpenMPIsDevice && 21061 !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>()) 21062 targetDiag(StartLoc, diag::err_expected_allocator_expression); 21063 } 21064 // Analyze and build list of variables. 21065 SmallVector<Expr *, 8> Vars; 21066 for (Expr *RefExpr : VarList) { 21067 assert(RefExpr && "NULL expr in OpenMP private clause."); 21068 SourceLocation ELoc; 21069 SourceRange ERange; 21070 Expr *SimpleRefExpr = RefExpr; 21071 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 21072 if (Res.second) { 21073 // It will be analyzed later. 21074 Vars.push_back(RefExpr); 21075 } 21076 ValueDecl *D = Res.first; 21077 if (!D) 21078 continue; 21079 21080 auto *VD = dyn_cast<VarDecl>(D); 21081 DeclRefExpr *Ref = nullptr; 21082 if (!VD && !CurContext->isDependentContext()) 21083 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 21084 Vars.push_back((VD || CurContext->isDependentContext()) 21085 ? RefExpr->IgnoreParens() 21086 : Ref); 21087 } 21088 21089 if (Vars.empty()) 21090 return nullptr; 21091 21092 if (Allocator) 21093 DSAStack->addInnerAllocatorExpr(Allocator); 21094 return OMPAllocateClause::Create(Context, StartLoc, LParenLoc, Allocator, 21095 ColonLoc, EndLoc, Vars); 21096 } 21097 21098 OMPClause *Sema::ActOnOpenMPNontemporalClause(ArrayRef<Expr *> VarList, 21099 SourceLocation StartLoc, 21100 SourceLocation LParenLoc, 21101 SourceLocation EndLoc) { 21102 SmallVector<Expr *, 8> Vars; 21103 for (Expr *RefExpr : VarList) { 21104 assert(RefExpr && "NULL expr in OpenMP nontemporal clause."); 21105 SourceLocation ELoc; 21106 SourceRange ERange; 21107 Expr *SimpleRefExpr = RefExpr; 21108 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 21109 if (Res.second) 21110 // It will be analyzed later. 21111 Vars.push_back(RefExpr); 21112 ValueDecl *D = Res.first; 21113 if (!D) 21114 continue; 21115 21116 // OpenMP 5.0, 2.9.3.1 simd Construct, Restrictions. 21117 // A list-item cannot appear in more than one nontemporal clause. 21118 if (const Expr *PrevRef = 21119 DSAStack->addUniqueNontemporal(D, SimpleRefExpr)) { 21120 Diag(ELoc, diag::err_omp_used_in_clause_twice) 21121 << 0 << getOpenMPClauseName(OMPC_nontemporal) << ERange; 21122 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa) 21123 << getOpenMPClauseName(OMPC_nontemporal); 21124 continue; 21125 } 21126 21127 Vars.push_back(RefExpr); 21128 } 21129 21130 if (Vars.empty()) 21131 return nullptr; 21132 21133 return OMPNontemporalClause::Create(Context, StartLoc, LParenLoc, EndLoc, 21134 Vars); 21135 } 21136 21137 OMPClause *Sema::ActOnOpenMPInclusiveClause(ArrayRef<Expr *> VarList, 21138 SourceLocation StartLoc, 21139 SourceLocation LParenLoc, 21140 SourceLocation EndLoc) { 21141 SmallVector<Expr *, 8> Vars; 21142 for (Expr *RefExpr : VarList) { 21143 assert(RefExpr && "NULL expr in OpenMP nontemporal clause."); 21144 SourceLocation ELoc; 21145 SourceRange ERange; 21146 Expr *SimpleRefExpr = RefExpr; 21147 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, 21148 /*AllowArraySection=*/true); 21149 if (Res.second) 21150 // It will be analyzed later. 21151 Vars.push_back(RefExpr); 21152 ValueDecl *D = Res.first; 21153 if (!D) 21154 continue; 21155 21156 const DSAStackTy::DSAVarData DVar = 21157 DSAStack->getTopDSA(D, /*FromParent=*/true); 21158 // OpenMP 5.0, 2.9.6, scan Directive, Restrictions. 21159 // A list item that appears in the inclusive or exclusive clause must appear 21160 // in a reduction clause with the inscan modifier on the enclosing 21161 // worksharing-loop, worksharing-loop SIMD, or simd construct. 21162 if (DVar.CKind != OMPC_reduction || 21163 DVar.Modifier != OMPC_REDUCTION_inscan) 21164 Diag(ELoc, diag::err_omp_inclusive_exclusive_not_reduction) 21165 << RefExpr->getSourceRange(); 21166 21167 if (DSAStack->getParentDirective() != OMPD_unknown) 21168 DSAStack->markDeclAsUsedInScanDirective(D); 21169 Vars.push_back(RefExpr); 21170 } 21171 21172 if (Vars.empty()) 21173 return nullptr; 21174 21175 return OMPInclusiveClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 21176 } 21177 21178 OMPClause *Sema::ActOnOpenMPExclusiveClause(ArrayRef<Expr *> VarList, 21179 SourceLocation StartLoc, 21180 SourceLocation LParenLoc, 21181 SourceLocation EndLoc) { 21182 SmallVector<Expr *, 8> Vars; 21183 for (Expr *RefExpr : VarList) { 21184 assert(RefExpr && "NULL expr in OpenMP nontemporal clause."); 21185 SourceLocation ELoc; 21186 SourceRange ERange; 21187 Expr *SimpleRefExpr = RefExpr; 21188 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, 21189 /*AllowArraySection=*/true); 21190 if (Res.second) 21191 // It will be analyzed later. 21192 Vars.push_back(RefExpr); 21193 ValueDecl *D = Res.first; 21194 if (!D) 21195 continue; 21196 21197 OpenMPDirectiveKind ParentDirective = DSAStack->getParentDirective(); 21198 DSAStackTy::DSAVarData DVar; 21199 if (ParentDirective != OMPD_unknown) 21200 DVar = DSAStack->getTopDSA(D, /*FromParent=*/true); 21201 // OpenMP 5.0, 2.9.6, scan Directive, Restrictions. 21202 // A list item that appears in the inclusive or exclusive clause must appear 21203 // in a reduction clause with the inscan modifier on the enclosing 21204 // worksharing-loop, worksharing-loop SIMD, or simd construct. 21205 if (ParentDirective == OMPD_unknown || DVar.CKind != OMPC_reduction || 21206 DVar.Modifier != OMPC_REDUCTION_inscan) { 21207 Diag(ELoc, diag::err_omp_inclusive_exclusive_not_reduction) 21208 << RefExpr->getSourceRange(); 21209 } else { 21210 DSAStack->markDeclAsUsedInScanDirective(D); 21211 } 21212 Vars.push_back(RefExpr); 21213 } 21214 21215 if (Vars.empty()) 21216 return nullptr; 21217 21218 return OMPExclusiveClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 21219 } 21220 21221 /// Tries to find omp_alloctrait_t type. 21222 static bool findOMPAlloctraitT(Sema &S, SourceLocation Loc, DSAStackTy *Stack) { 21223 QualType OMPAlloctraitT = Stack->getOMPAlloctraitT(); 21224 if (!OMPAlloctraitT.isNull()) 21225 return true; 21226 IdentifierInfo &II = S.PP.getIdentifierTable().get("omp_alloctrait_t"); 21227 ParsedType PT = S.getTypeName(II, Loc, S.getCurScope()); 21228 if (!PT.getAsOpaquePtr() || PT.get().isNull()) { 21229 S.Diag(Loc, diag::err_omp_implied_type_not_found) << "omp_alloctrait_t"; 21230 return false; 21231 } 21232 Stack->setOMPAlloctraitT(PT.get()); 21233 return true; 21234 } 21235 21236 OMPClause *Sema::ActOnOpenMPUsesAllocatorClause( 21237 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc, 21238 ArrayRef<UsesAllocatorsData> Data) { 21239 // OpenMP [2.12.5, target Construct] 21240 // allocator is an identifier of omp_allocator_handle_t type. 21241 if (!findOMPAllocatorHandleT(*this, StartLoc, DSAStack)) 21242 return nullptr; 21243 // OpenMP [2.12.5, target Construct] 21244 // allocator-traits-array is an identifier of const omp_alloctrait_t * type. 21245 if (llvm::any_of( 21246 Data, 21247 [](const UsesAllocatorsData &D) { return D.AllocatorTraits; }) && 21248 !findOMPAlloctraitT(*this, StartLoc, DSAStack)) 21249 return nullptr; 21250 llvm::SmallPtrSet<CanonicalDeclPtr<Decl>, 4> PredefinedAllocators; 21251 for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) { 21252 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I); 21253 StringRef Allocator = 21254 OMPAllocateDeclAttr::ConvertAllocatorTypeTyToStr(AllocatorKind); 21255 DeclarationName AllocatorName = &Context.Idents.get(Allocator); 21256 PredefinedAllocators.insert(LookupSingleName( 21257 TUScope, AllocatorName, StartLoc, Sema::LookupAnyName)); 21258 } 21259 21260 SmallVector<OMPUsesAllocatorsClause::Data, 4> NewData; 21261 for (const UsesAllocatorsData &D : Data) { 21262 Expr *AllocatorExpr = nullptr; 21263 // Check allocator expression. 21264 if (D.Allocator->isTypeDependent()) { 21265 AllocatorExpr = D.Allocator; 21266 } else { 21267 // Traits were specified - need to assign new allocator to the specified 21268 // allocator, so it must be an lvalue. 21269 AllocatorExpr = D.Allocator->IgnoreParenImpCasts(); 21270 auto *DRE = dyn_cast<DeclRefExpr>(AllocatorExpr); 21271 bool IsPredefinedAllocator = false; 21272 if (DRE) 21273 IsPredefinedAllocator = PredefinedAllocators.count(DRE->getDecl()); 21274 if (!DRE || 21275 !(Context.hasSameUnqualifiedType( 21276 AllocatorExpr->getType(), DSAStack->getOMPAllocatorHandleT()) || 21277 Context.typesAreCompatible(AllocatorExpr->getType(), 21278 DSAStack->getOMPAllocatorHandleT(), 21279 /*CompareUnqualified=*/true)) || 21280 (!IsPredefinedAllocator && 21281 (AllocatorExpr->getType().isConstant(Context) || 21282 !AllocatorExpr->isLValue()))) { 21283 Diag(D.Allocator->getExprLoc(), diag::err_omp_var_expected) 21284 << "omp_allocator_handle_t" << (DRE ? 1 : 0) 21285 << AllocatorExpr->getType() << D.Allocator->getSourceRange(); 21286 continue; 21287 } 21288 // OpenMP [2.12.5, target Construct] 21289 // Predefined allocators appearing in a uses_allocators clause cannot have 21290 // traits specified. 21291 if (IsPredefinedAllocator && D.AllocatorTraits) { 21292 Diag(D.AllocatorTraits->getExprLoc(), 21293 diag::err_omp_predefined_allocator_with_traits) 21294 << D.AllocatorTraits->getSourceRange(); 21295 Diag(D.Allocator->getExprLoc(), diag::note_omp_predefined_allocator) 21296 << cast<NamedDecl>(DRE->getDecl())->getName() 21297 << D.Allocator->getSourceRange(); 21298 continue; 21299 } 21300 // OpenMP [2.12.5, target Construct] 21301 // Non-predefined allocators appearing in a uses_allocators clause must 21302 // have traits specified. 21303 if (!IsPredefinedAllocator && !D.AllocatorTraits) { 21304 Diag(D.Allocator->getExprLoc(), 21305 diag::err_omp_nonpredefined_allocator_without_traits); 21306 continue; 21307 } 21308 // No allocator traits - just convert it to rvalue. 21309 if (!D.AllocatorTraits) 21310 AllocatorExpr = DefaultLvalueConversion(AllocatorExpr).get(); 21311 DSAStack->addUsesAllocatorsDecl( 21312 DRE->getDecl(), 21313 IsPredefinedAllocator 21314 ? DSAStackTy::UsesAllocatorsDeclKind::PredefinedAllocator 21315 : DSAStackTy::UsesAllocatorsDeclKind::UserDefinedAllocator); 21316 } 21317 Expr *AllocatorTraitsExpr = nullptr; 21318 if (D.AllocatorTraits) { 21319 if (D.AllocatorTraits->isTypeDependent()) { 21320 AllocatorTraitsExpr = D.AllocatorTraits; 21321 } else { 21322 // OpenMP [2.12.5, target Construct] 21323 // Arrays that contain allocator traits that appear in a uses_allocators 21324 // clause must be constant arrays, have constant values and be defined 21325 // in the same scope as the construct in which the clause appears. 21326 AllocatorTraitsExpr = D.AllocatorTraits->IgnoreParenImpCasts(); 21327 // Check that traits expr is a constant array. 21328 QualType TraitTy; 21329 if (const ArrayType *Ty = 21330 AllocatorTraitsExpr->getType()->getAsArrayTypeUnsafe()) 21331 if (const auto *ConstArrayTy = dyn_cast<ConstantArrayType>(Ty)) 21332 TraitTy = ConstArrayTy->getElementType(); 21333 if (TraitTy.isNull() || 21334 !(Context.hasSameUnqualifiedType(TraitTy, 21335 DSAStack->getOMPAlloctraitT()) || 21336 Context.typesAreCompatible(TraitTy, DSAStack->getOMPAlloctraitT(), 21337 /*CompareUnqualified=*/true))) { 21338 Diag(D.AllocatorTraits->getExprLoc(), 21339 diag::err_omp_expected_array_alloctraits) 21340 << AllocatorTraitsExpr->getType(); 21341 continue; 21342 } 21343 // Do not map by default allocator traits if it is a standalone 21344 // variable. 21345 if (auto *DRE = dyn_cast<DeclRefExpr>(AllocatorTraitsExpr)) 21346 DSAStack->addUsesAllocatorsDecl( 21347 DRE->getDecl(), 21348 DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait); 21349 } 21350 } 21351 OMPUsesAllocatorsClause::Data &NewD = NewData.emplace_back(); 21352 NewD.Allocator = AllocatorExpr; 21353 NewD.AllocatorTraits = AllocatorTraitsExpr; 21354 NewD.LParenLoc = D.LParenLoc; 21355 NewD.RParenLoc = D.RParenLoc; 21356 } 21357 return OMPUsesAllocatorsClause::Create(Context, StartLoc, LParenLoc, EndLoc, 21358 NewData); 21359 } 21360 21361 OMPClause *Sema::ActOnOpenMPAffinityClause( 21362 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc, 21363 SourceLocation EndLoc, Expr *Modifier, ArrayRef<Expr *> Locators) { 21364 SmallVector<Expr *, 8> Vars; 21365 for (Expr *RefExpr : Locators) { 21366 assert(RefExpr && "NULL expr in OpenMP shared clause."); 21367 if (isa<DependentScopeDeclRefExpr>(RefExpr) || RefExpr->isTypeDependent()) { 21368 // It will be analyzed later. 21369 Vars.push_back(RefExpr); 21370 continue; 21371 } 21372 21373 SourceLocation ELoc = RefExpr->getExprLoc(); 21374 Expr *SimpleExpr = RefExpr->IgnoreParenImpCasts(); 21375 21376 if (!SimpleExpr->isLValue()) { 21377 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 21378 << 1 << 0 << RefExpr->getSourceRange(); 21379 continue; 21380 } 21381 21382 ExprResult Res; 21383 { 21384 Sema::TentativeAnalysisScope Trap(*this); 21385 Res = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, SimpleExpr); 21386 } 21387 if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr) && 21388 !isa<OMPArrayShapingExpr>(SimpleExpr)) { 21389 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 21390 << 1 << 0 << RefExpr->getSourceRange(); 21391 continue; 21392 } 21393 Vars.push_back(SimpleExpr); 21394 } 21395 21396 return OMPAffinityClause::Create(Context, StartLoc, LParenLoc, ColonLoc, 21397 EndLoc, Modifier, Vars); 21398 } 21399